2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "vp8/common/onyxc_int.h"
14 #include "vp8/common/systemdependent.h"
16 #include "vp8/common/alloccommon.h"
18 #include "firstpass.h"
20 #include "vpx_scale/vpxscale.h"
21 #include "vp8/common/extend.h"
23 #include "vp8/common/quant_common.h"
24 #include "segmentation.h"
25 #include "vpx_mem/vpx_mem.h"
26 #include "vp8/common/swapyv12buffer.h"
27 #include "vp8/common/threading.h"
28 #include "vpx_ports/vpx_timer.h"
33 #define ALT_REF_MC_ENABLED 1 // dis/enable MC in AltRef filtering
34 #define ALT_REF_SUBPEL_ENABLED 1 // dis/enable subpel in MC AltRef filtering
36 #if VP8_TEMPORAL_ALT_REF
38 static void vp8_temporal_filter_predictors_mb_c
41 unsigned char *y_mb_ptr,
42 unsigned char *u_mb_ptr,
43 unsigned char *v_mb_ptr,
51 unsigned char *yptr, *uptr, *vptr;
54 yptr = y_mb_ptr + (mv_row >> 3) * stride + (mv_col >> 3);
56 if ((mv_row | mv_col) & 7)
58 x->subpixel_predict16x16(yptr, stride,
59 mv_col & 7, mv_row & 7, &pred[0], 16);
63 vp8_copy_mem16x16(yptr, stride, &pred[0], 16);
69 stride = (stride + 1) >> 1;
70 offset = (mv_row >> 3) * stride + (mv_col >> 3);
71 uptr = u_mb_ptr + offset;
72 vptr = v_mb_ptr + offset;
74 if ((mv_row | mv_col) & 7)
76 x->subpixel_predict8x8(uptr, stride,
77 mv_col & 7, mv_row & 7, &pred[256], 8);
78 x->subpixel_predict8x8(vptr, stride,
79 mv_col & 7, mv_row & 7, &pred[320], 8);
83 vp8_copy_mem8x8(uptr, stride, &pred[256], 8);
84 vp8_copy_mem8x8(vptr, stride, &pred[320], 8);
87 void vp8_temporal_filter_apply_c
89 unsigned char *frame1,
91 unsigned char *frame2,
92 unsigned int block_size,
95 unsigned int *accumulator,
103 for (i = 0,k = 0; i < block_size; i++)
105 for (j = 0; j < block_size; j++, k++)
108 int src_byte = frame1[byte];
109 int pixel_value = *frame2++;
111 modifier = src_byte - pixel_value;
112 // This is an integer approximation of:
113 // float coeff = (3.0 * modifer * modifier) / pow(2, strength);
114 // modifier = (int)roundf(coeff > 16 ? 0 : 16-coeff);
115 modifier *= modifier;
117 modifier += 1 << (strength - 1);
118 modifier >>= strength;
123 modifier = 16 - modifier;
124 modifier *= filter_weight;
126 count[k] += modifier;
127 accumulator[k] += modifier * pixel_value;
132 byte += stride - block_size;
136 #if ALT_REF_MC_ENABLED
138 static int vp8_temporal_filter_find_matching_mb_c
141 YV12_BUFFER_CONFIG *arf_frame,
142 YV12_BUFFER_CONFIG *frame_ptr,
147 MACROBLOCK *x = &cpi->mb;
150 int sadpb = x->sadperbit16;
151 int bestsme = INT_MAX;
153 BLOCK *b = &x->block[0];
154 BLOCKD *d = &x->e_mbd.block[0];
156 int_mv best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
159 unsigned char **base_src = b->base_src;
161 int src_stride = b->src_stride;
162 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
164 int pre_stride = x->e_mbd.pre.y_stride;
166 best_ref_mv1.as_int = 0;
167 best_ref_mv1_full.as_mv.col = best_ref_mv1.as_mv.col >>3;
168 best_ref_mv1_full.as_mv.row = best_ref_mv1.as_mv.row >>3;
170 // Setup frame pointers
171 b->base_src = &arf_frame->y_buffer;
172 b->src_stride = arf_frame->y_stride;
175 x->e_mbd.pre.y_buffer = frame_ptr->y_buffer;
176 x->e_mbd.pre.y_stride = frame_ptr->y_stride;
177 d->offset = mb_offset;
179 // Further step/diamond searches as necessary
182 step_param = cpi->sf.first_step +
185 (cpi->sf.max_step_search_steps - 1)-step_param;
189 step_param = cpi->sf.first_step + 2;
193 /*cpi->sf.search_method == HEX*/
194 // TODO Check that the 16x16 vf & sdf are selected here
195 // Ignore mv costing by sending NULL cost arrays
196 bestsme = vp8_hex_search(x, b, d, &best_ref_mv1_full, &d->bmi.mv,
198 &cpi->fn_ptr[BLOCK_16X16],
199 NULL, NULL, &best_ref_mv1);
201 #if ALT_REF_SUBPEL_ENABLED
203 //if (bestsme > error_thresh && bestsme < INT_MAX)
207 // Ignore mv costing by sending NULL cost array
208 bestsme = cpi->find_fractional_mv_step(x, b, d,
212 &cpi->fn_ptr[BLOCK_16X16],
213 NULL, &distortion, &sse);
218 b->base_src = base_src;
220 b->src_stride = src_stride;
221 x->e_mbd.pre.y_buffer = base_pre;
223 x->e_mbd.pre.y_stride = pre_stride;
229 static void vp8_temporal_filter_iterate_c
240 unsigned int filter_weight;
241 int mb_cols = cpi->common.mb_cols;
242 int mb_rows = cpi->common.mb_rows;
244 int mb_uv_offset = 0;
245 DECLARE_ALIGNED_ARRAY(16, unsigned int, accumulator, 16*16 + 8*8 + 8*8);
246 DECLARE_ALIGNED_ARRAY(16, unsigned short, count, 16*16 + 8*8 + 8*8);
247 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
248 YV12_BUFFER_CONFIG *f = cpi->frames[alt_ref_index];
249 unsigned char *dst1, *dst2;
250 DECLARE_ALIGNED_ARRAY(16, unsigned char, predictor, 16*16 + 8*8 + 8*8);
253 unsigned char *y_buffer = mbd->pre.y_buffer;
254 unsigned char *u_buffer = mbd->pre.u_buffer;
255 unsigned char *v_buffer = mbd->pre.v_buffer;
257 for (mb_row = 0; mb_row < mb_rows; mb_row++)
259 #if ALT_REF_MC_ENABLED
260 // Source frames are extended to 16 pixels. This is different than
261 // L/A/G reference frames that have a border of 32 (VP8BORDERINPIXELS)
262 // A 6 tap filter is used for motion search. This requires 2 pixels
263 // before and 3 pixels after. So the largest Y mv on a border would
264 // then be 16 - 3. The UV blocks are half the size of the Y and
265 // therefore only extended by 8. The largest mv that a UV block
266 // can support is 8 - 3. A UV mv is half of a Y mv.
267 // (16 - 3) >> 1 == 6 which is greater than 8 - 3.
268 // To keep the mv in play for both Y and UV planes the max that it
269 // can be on a border is therefore 16 - 5.
270 cpi->mb.mv_row_min = -((mb_row * 16) + (16 - 5));
271 cpi->mb.mv_row_max = ((cpi->common.mb_rows - 1 - mb_row) * 16)
275 for (mb_col = 0; mb_col < mb_cols; mb_col++)
280 vpx_memset(accumulator, 0, 384*sizeof(unsigned int));
281 vpx_memset(count, 0, 384*sizeof(unsigned short));
283 #if ALT_REF_MC_ENABLED
284 cpi->mb.mv_col_min = -((mb_col * 16) + (16 - 5));
285 cpi->mb.mv_col_max = ((cpi->common.mb_cols - 1 - mb_col) * 16)
289 for (frame = 0; frame < frame_count; frame++)
293 if (cpi->frames[frame] == NULL)
296 mbd->block[0].bmi.mv.as_mv.row = 0;
297 mbd->block[0].bmi.mv.as_mv.col = 0;
299 #if ALT_REF_MC_ENABLED
300 #define THRESH_LOW 10000
301 #define THRESH_HIGH 20000
303 // Find best match in this frame by MC
304 err = vp8_temporal_filter_find_matching_mb_c
306 cpi->frames[alt_ref_index],
311 // Assign higher weight to matching MB if it's error
312 // score is lower. If not applying MC default behavior
313 // is to weight all MBs equal.
314 filter_weight = err<THRESH_LOW
315 ? 2 : err<THRESH_HIGH ? 1 : 0;
317 if (filter_weight != 0)
319 // Construct the predictors
320 vp8_temporal_filter_predictors_mb_c
322 cpi->frames[frame]->y_buffer + mb_y_offset,
323 cpi->frames[frame]->u_buffer + mb_uv_offset,
324 cpi->frames[frame]->v_buffer + mb_uv_offset,
325 cpi->frames[frame]->y_stride,
326 mbd->block[0].bmi.mv.as_mv.row,
327 mbd->block[0].bmi.mv.as_mv.col,
330 // Apply the filter (YUV)
331 vp8_temporal_filter_apply
332 (f->y_buffer + mb_y_offset,
341 vp8_temporal_filter_apply
342 (f->u_buffer + mb_uv_offset,
351 vp8_temporal_filter_apply
352 (f->v_buffer + mb_uv_offset,
363 // Normalize filter output to produce AltRef frame
364 dst1 = cpi->alt_ref_buffer.y_buffer;
365 stride = cpi->alt_ref_buffer.y_stride;
367 for (i = 0,k = 0; i < 16; i++)
369 for (j = 0; j < 16; j++, k++)
371 unsigned int pval = accumulator[k] + (count[k] >> 1);
372 pval *= cpi->fixed_divide[count[k]];
375 dst1[byte] = (unsigned char)pval;
377 // move to next pixel
384 dst1 = cpi->alt_ref_buffer.u_buffer;
385 dst2 = cpi->alt_ref_buffer.v_buffer;
386 stride = cpi->alt_ref_buffer.uv_stride;
388 for (i = 0,k = 256; i < 8; i++)
390 for (j = 0; j < 8; j++, k++)
395 unsigned int pval = accumulator[k] + (count[k] >> 1);
396 pval *= cpi->fixed_divide[count[k]];
398 dst1[byte] = (unsigned char)pval;
401 pval = accumulator[m] + (count[m] >> 1);
402 pval *= cpi->fixed_divide[count[m]];
404 dst2[byte] = (unsigned char)pval;
406 // move to next pixel
417 mb_y_offset += 16*(f->y_stride-mb_cols);
418 mb_uv_offset += 8*(f->uv_stride-mb_cols);
421 // Restore input state
422 mbd->pre.y_buffer = y_buffer;
423 mbd->pre.u_buffer = u_buffer;
424 mbd->pre.v_buffer = v_buffer;
427 void vp8_temporal_filter_prepare_c
435 int num_frames_backward = 0;
436 int num_frames_forward = 0;
437 int frames_to_blur_backward = 0;
438 int frames_to_blur_forward = 0;
439 int frames_to_blur = 0;
442 int strength = cpi->oxcf.arnr_strength;
444 int blur_type = cpi->oxcf.arnr_type;
446 int max_frames = cpi->active_arnr_frames;
448 num_frames_backward = distance;
449 num_frames_forward = vp8_lookahead_depth(cpi->lookahead)
450 - (num_frames_backward + 1);
455 /////////////////////////////////////////
458 frames_to_blur_backward = num_frames_backward;
460 if (frames_to_blur_backward >= max_frames)
461 frames_to_blur_backward = max_frames - 1;
463 frames_to_blur = frames_to_blur_backward + 1;
467 /////////////////////////////////////////
470 frames_to_blur_forward = num_frames_forward;
472 if (frames_to_blur_forward >= max_frames)
473 frames_to_blur_forward = max_frames - 1;
475 frames_to_blur = frames_to_blur_forward + 1;
480 /////////////////////////////////////////
482 frames_to_blur_forward = num_frames_forward;
483 frames_to_blur_backward = num_frames_backward;
485 if (frames_to_blur_forward > frames_to_blur_backward)
486 frames_to_blur_forward = frames_to_blur_backward;
488 if (frames_to_blur_backward > frames_to_blur_forward)
489 frames_to_blur_backward = frames_to_blur_forward;
491 // When max_frames is even we have 1 more frame backward than forward
492 if (frames_to_blur_forward > (max_frames - 1) / 2)
493 frames_to_blur_forward = ((max_frames - 1) / 2);
495 if (frames_to_blur_backward > (max_frames / 2))
496 frames_to_blur_backward = (max_frames / 2);
498 frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1;
502 start_frame = distance + frames_to_blur_forward;
506 printf("max:%d FBCK:%d FFWD:%d ftb:%d ftbbck:%d ftbfwd:%d sei:%d lasei:%d start:%d"
508 , num_frames_backward
511 , frames_to_blur_backward
512 , frames_to_blur_forward
513 , cpi->source_encode_index
514 , cpi->last_alt_ref_sei
518 // Setup frame pointers, NULL indicates frame not included in filter
519 vpx_memset(cpi->frames, 0, max_frames*sizeof(YV12_BUFFER_CONFIG *));
520 for (frame = 0; frame < frames_to_blur; frame++)
522 int which_buffer = start_frame - frame;
523 struct lookahead_entry* buf = vp8_lookahead_peek(cpi->lookahead,
526 cpi->frames[frames_to_blur-1-frame] = &buf->img;
529 vp8_temporal_filter_iterate_c (
532 frames_to_blur_backward,