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_scale/yv12extend.h"
26 #include "vpx_mem/vpx_mem.h"
27 #include "vp8/common/swapyv12buffer.h"
28 #include "vp8/common/threading.h"
29 #include "vpx_ports/vpx_timer.h"
34 #define ALT_REF_MC_ENABLED 1 // dis/enable MC in AltRef filtering
35 #define ALT_REF_SUBPEL_ENABLED 1 // dis/enable subpel in MC AltRef filtering
37 #if VP8_TEMPORAL_ALT_REF
39 static void vp8_temporal_filter_predictors_mb_c
42 unsigned char *y_mb_ptr,
43 unsigned char *u_mb_ptr,
44 unsigned char *v_mb_ptr,
52 unsigned char *yptr, *uptr, *vptr;
55 yptr = y_mb_ptr + (mv_row >> 3) * stride + (mv_col >> 3);
57 if ((mv_row | mv_col) & 7)
59 x->subpixel_predict16x16(yptr, stride,
60 mv_col & 7, mv_row & 7, &pred[0], 16);
64 vp8_copy_mem16x16(yptr, stride, &pred[0], 16);
70 stride = (stride + 1) >> 1;
71 offset = (mv_row >> 3) * stride + (mv_col >> 3);
72 uptr = u_mb_ptr + offset;
73 vptr = v_mb_ptr + offset;
75 if ((mv_row | mv_col) & 7)
77 x->subpixel_predict8x8(uptr, stride,
78 mv_col & 7, mv_row & 7, &pred[256], 8);
79 x->subpixel_predict8x8(vptr, stride,
80 mv_col & 7, mv_row & 7, &pred[320], 8);
84 vp8_copy_mem8x8(uptr, stride, &pred[256], 8);
85 vp8_copy_mem8x8(vptr, stride, &pred[320], 8);
88 void vp8_temporal_filter_apply_c
90 unsigned char *frame1,
92 unsigned char *frame2,
93 unsigned int block_size,
96 unsigned int *accumulator,
100 unsigned int i, j, k;
104 for (i = 0,k = 0; i < block_size; i++)
106 for (j = 0; j < block_size; j++, k++)
109 int src_byte = frame1[byte];
110 int pixel_value = *frame2++;
112 modifier = src_byte - pixel_value;
113 // This is an integer approximation of:
114 // float coeff = (3.0 * modifer * modifier) / pow(2, strength);
115 // modifier = (int)roundf(coeff > 16 ? 0 : 16-coeff);
116 modifier *= modifier;
118 modifier += 1 << (strength - 1);
119 modifier >>= strength;
124 modifier = 16 - modifier;
125 modifier *= filter_weight;
127 count[k] += modifier;
128 accumulator[k] += modifier * pixel_value;
133 byte += stride - block_size;
137 #if ALT_REF_MC_ENABLED
138 static int dummy_cost[2*mv_max+1];
140 static int vp8_temporal_filter_find_matching_mb_c
143 YV12_BUFFER_CONFIG *arf_frame,
144 YV12_BUFFER_CONFIG *frame_ptr,
149 MACROBLOCK *x = &cpi->mb;
152 int sadpb = x->sadperbit16;
153 int bestsme = INT_MAX;
155 BLOCK *b = &x->block[0];
156 BLOCKD *d = &x->e_mbd.block[0];
158 int_mv best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
160 int *mvcost[2] = { &dummy_cost[mv_max+1], &dummy_cost[mv_max+1] };
161 int *mvsadcost[2] = { &dummy_cost[mv_max+1], &dummy_cost[mv_max+1] };
164 unsigned char **base_src = b->base_src;
166 int src_stride = b->src_stride;
167 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
169 int pre_stride = x->e_mbd.pre.y_stride;
171 best_ref_mv1.as_int = 0;
172 best_ref_mv1_full.as_mv.col = best_ref_mv1.as_mv.col >>3;
173 best_ref_mv1_full.as_mv.row = best_ref_mv1.as_mv.row >>3;
175 // Setup frame pointers
176 b->base_src = &arf_frame->y_buffer;
177 b->src_stride = arf_frame->y_stride;
180 x->e_mbd.pre.y_buffer = frame_ptr->y_buffer;
181 x->e_mbd.pre.y_stride = frame_ptr->y_stride;
182 d->offset = mb_offset;
184 // Further step/diamond searches as necessary
187 step_param = cpi->sf.first_step +
190 (cpi->sf.max_step_search_steps - 1)-step_param;
194 step_param = cpi->sf.first_step + 2;
198 /*cpi->sf.search_method == HEX*/
199 // TODO Check that the 16x16 vf & sdf are selected here
200 bestsme = vp8_hex_search(x, b, d,
201 &best_ref_mv1_full, &d->bmi.mv,
204 &cpi->fn_ptr[BLOCK_16X16],
205 mvsadcost, mvcost, &best_ref_mv1);
207 #if ALT_REF_SUBPEL_ENABLED
209 //if (bestsme > error_thresh && bestsme < INT_MAX)
213 bestsme = cpi->find_fractional_mv_step(x, b, d,
214 &d->bmi.mv, &best_ref_mv1,
215 x->errorperbit, &cpi->fn_ptr[BLOCK_16X16],
216 mvcost, &distortion, &sse);
221 b->base_src = base_src;
223 b->src_stride = src_stride;
224 x->e_mbd.pre.y_buffer = base_pre;
226 x->e_mbd.pre.y_stride = pre_stride;
232 static void vp8_temporal_filter_iterate_c
243 unsigned int filter_weight;
244 int mb_cols = cpi->common.mb_cols;
245 int mb_rows = cpi->common.mb_rows;
247 int mb_uv_offset = 0;
248 DECLARE_ALIGNED_ARRAY(16, unsigned int, accumulator, 16*16 + 8*8 + 8*8);
249 DECLARE_ALIGNED_ARRAY(16, unsigned short, count, 16*16 + 8*8 + 8*8);
250 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
251 YV12_BUFFER_CONFIG *f = cpi->frames[alt_ref_index];
252 unsigned char *dst1, *dst2;
253 DECLARE_ALIGNED_ARRAY(16, unsigned char, predictor, 16*16 + 8*8 + 8*8);
256 unsigned char *y_buffer = mbd->pre.y_buffer;
257 unsigned char *u_buffer = mbd->pre.u_buffer;
258 unsigned char *v_buffer = mbd->pre.v_buffer;
260 for (mb_row = 0; mb_row < mb_rows; mb_row++)
262 #if ALT_REF_MC_ENABLED
263 // Source frames are extended to 16 pixels. This is different than
264 // L/A/G reference frames that have a border of 32 (VP8BORDERINPIXELS)
265 // A 6 tap filter is used for motion search. This requires 2 pixels
266 // before and 3 pixels after. So the largest Y mv on a border would
267 // then be 16 - 3. The UV blocks are half the size of the Y and
268 // therefore only extended by 8. The largest mv that a UV block
269 // can support is 8 - 3. A UV mv is half of a Y mv.
270 // (16 - 3) >> 1 == 6 which is greater than 8 - 3.
271 // To keep the mv in play for both Y and UV planes the max that it
272 // can be on a border is therefore 16 - 5.
273 cpi->mb.mv_row_min = -((mb_row * 16) + (16 - 5));
274 cpi->mb.mv_row_max = ((cpi->common.mb_rows - 1 - mb_row) * 16)
278 for (mb_col = 0; mb_col < mb_cols; mb_col++)
283 vpx_memset(accumulator, 0, 384*sizeof(unsigned int));
284 vpx_memset(count, 0, 384*sizeof(unsigned short));
286 #if ALT_REF_MC_ENABLED
287 cpi->mb.mv_col_min = -((mb_col * 16) + (16 - 5));
288 cpi->mb.mv_col_max = ((cpi->common.mb_cols - 1 - mb_col) * 16)
292 for (frame = 0; frame < frame_count; frame++)
296 if (cpi->frames[frame] == NULL)
299 mbd->block[0].bmi.mv.as_mv.row = 0;
300 mbd->block[0].bmi.mv.as_mv.col = 0;
302 #if ALT_REF_MC_ENABLED
303 #define THRESH_LOW 10000
304 #define THRESH_HIGH 20000
306 // Find best match in this frame by MC
307 err = vp8_temporal_filter_find_matching_mb_c
309 cpi->frames[alt_ref_index],
315 // Assign higher weight to matching MB if it's error
316 // score is lower. If not applying MC default behavior
317 // is to weight all MBs equal.
318 filter_weight = err<THRESH_LOW
319 ? 2 : err<THRESH_HIGH ? 1 : 0;
321 if (filter_weight != 0)
323 // Construct the predictors
324 vp8_temporal_filter_predictors_mb_c
326 cpi->frames[frame]->y_buffer + mb_y_offset,
327 cpi->frames[frame]->u_buffer + mb_uv_offset,
328 cpi->frames[frame]->v_buffer + mb_uv_offset,
329 cpi->frames[frame]->y_stride,
330 mbd->block[0].bmi.mv.as_mv.row,
331 mbd->block[0].bmi.mv.as_mv.col,
334 // Apply the filter (YUV)
335 vp8_temporal_filter_apply
336 (f->y_buffer + mb_y_offset,
345 vp8_temporal_filter_apply
346 (f->u_buffer + mb_uv_offset,
355 vp8_temporal_filter_apply
356 (f->v_buffer + mb_uv_offset,
367 // Normalize filter output to produce AltRef frame
368 dst1 = cpi->alt_ref_buffer.y_buffer;
369 stride = cpi->alt_ref_buffer.y_stride;
371 for (i = 0,k = 0; i < 16; i++)
373 for (j = 0; j < 16; j++, k++)
375 unsigned int pval = accumulator[k] + (count[k] >> 1);
376 pval *= cpi->fixed_divide[count[k]];
379 dst1[byte] = (unsigned char)pval;
381 // move to next pixel
388 dst1 = cpi->alt_ref_buffer.u_buffer;
389 dst2 = cpi->alt_ref_buffer.v_buffer;
390 stride = cpi->alt_ref_buffer.uv_stride;
392 for (i = 0,k = 256; i < 8; i++)
394 for (j = 0; j < 8; j++, k++)
399 unsigned int pval = accumulator[k] + (count[k] >> 1);
400 pval *= cpi->fixed_divide[count[k]];
402 dst1[byte] = (unsigned char)pval;
405 pval = accumulator[m] + (count[m] >> 1);
406 pval *= cpi->fixed_divide[count[m]];
408 dst2[byte] = (unsigned char)pval;
410 // move to next pixel
421 mb_y_offset += 16*(f->y_stride-mb_cols);
422 mb_uv_offset += 8*(f->uv_stride-mb_cols);
425 // Restore input state
426 mbd->pre.y_buffer = y_buffer;
427 mbd->pre.u_buffer = u_buffer;
428 mbd->pre.v_buffer = v_buffer;
431 void vp8_temporal_filter_prepare_c
439 int num_frames_backward = 0;
440 int num_frames_forward = 0;
441 int frames_to_blur_backward = 0;
442 int frames_to_blur_forward = 0;
443 int frames_to_blur = 0;
446 int strength = cpi->oxcf.arnr_strength;
448 int blur_type = cpi->oxcf.arnr_type;
450 int max_frames = cpi->active_arnr_frames;
452 num_frames_backward = distance;
453 num_frames_forward = vp8_lookahead_depth(cpi->lookahead)
454 - (num_frames_backward + 1);
459 /////////////////////////////////////////
462 frames_to_blur_backward = num_frames_backward;
464 if (frames_to_blur_backward >= max_frames)
465 frames_to_blur_backward = max_frames - 1;
467 frames_to_blur = frames_to_blur_backward + 1;
471 /////////////////////////////////////////
474 frames_to_blur_forward = num_frames_forward;
476 if (frames_to_blur_forward >= max_frames)
477 frames_to_blur_forward = max_frames - 1;
479 frames_to_blur = frames_to_blur_forward + 1;
484 /////////////////////////////////////////
486 frames_to_blur_forward = num_frames_forward;
487 frames_to_blur_backward = num_frames_backward;
489 if (frames_to_blur_forward > frames_to_blur_backward)
490 frames_to_blur_forward = frames_to_blur_backward;
492 if (frames_to_blur_backward > frames_to_blur_forward)
493 frames_to_blur_backward = frames_to_blur_forward;
495 // When max_frames is even we have 1 more frame backward than forward
496 if (frames_to_blur_forward > (max_frames - 1) / 2)
497 frames_to_blur_forward = ((max_frames - 1) / 2);
499 if (frames_to_blur_backward > (max_frames / 2))
500 frames_to_blur_backward = (max_frames / 2);
502 frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1;
506 start_frame = distance + frames_to_blur_forward;
510 printf("max:%d FBCK:%d FFWD:%d ftb:%d ftbbck:%d ftbfwd:%d sei:%d lasei:%d start:%d"
512 , num_frames_backward
515 , frames_to_blur_backward
516 , frames_to_blur_forward
517 , cpi->source_encode_index
518 , cpi->last_alt_ref_sei
522 // Setup frame pointers, NULL indicates frame not included in filter
523 vpx_memset(cpi->frames, 0, max_frames*sizeof(YV12_BUFFER_CONFIG *));
524 for (frame = 0; frame < frames_to_blur; frame++)
526 int which_buffer = start_frame - frame;
527 struct lookahead_entry* buf = vp8_lookahead_peek(cpi->lookahead,
530 cpi->frames[frames_to_blur-1-frame] = &buf->img;
533 vp8_temporal_filter_iterate_c (
536 frames_to_blur_backward,