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.
14 #include "vp9/common/vp9_onyxc_int.h"
15 #include "vp9/common/vp9_reconinter.h"
16 #include "vp9/encoder/vp9_onyx_int.h"
17 #include "vp9/common/vp9_systemdependent.h"
18 #include "vp9/encoder/vp9_quantize.h"
19 #include "vp9/common/vp9_alloccommon.h"
20 #include "vp9/encoder/vp9_mcomp.h"
21 #include "vp9/encoder/vp9_firstpass.h"
22 #include "vp9/encoder/vp9_psnr.h"
23 #include "vpx_scale/vpx_scale.h"
24 #include "vp9/common/vp9_extend.h"
25 #include "vp9/encoder/vp9_ratectrl.h"
26 #include "vp9/common/vp9_quant_common.h"
27 #include "vp9/encoder/vp9_segmentation.h"
28 #include "vpx_mem/vpx_mem.h"
29 #include "vpx_ports/vpx_timer.h"
31 #define ALT_REF_MC_ENABLED 1 // dis/enable MC in AltRef filtering
32 #define ALT_REF_SUBPEL_ENABLED 1 // dis/enable subpel in MC AltRef filtering
34 static void temporal_filter_predictors_mb_c(MACROBLOCKD *xd,
42 struct scale_factors *scale) {
43 const int which_mv = 0;
44 MV mv = { mv_row, mv_col };
46 vp9_build_inter_predictor(y_mb_ptr, stride,
52 &xd->subpix, MV_PRECISION_Q3);
54 stride = (stride + 1) >> 1;
56 vp9_build_inter_predictor(u_mb_ptr, stride,
62 &xd->subpix, MV_PRECISION_Q4);
64 vp9_build_inter_predictor(v_mb_ptr, stride,
70 &xd->subpix, MV_PRECISION_Q4);
73 void vp9_temporal_filter_apply_c(uint8_t *frame1,
76 unsigned int block_size,
79 unsigned int *accumulator,
85 for (i = 0, k = 0; i < block_size; i++) {
86 for (j = 0; j < block_size; j++, k++) {
87 int src_byte = frame1[byte];
88 int pixel_value = *frame2++;
90 modifier = src_byte - pixel_value;
91 // This is an integer approximation of:
92 // float coeff = (3.0 * modifer * modifier) / pow(2, strength);
93 // modifier = (int)roundf(coeff > 16 ? 0 : 16-coeff);
96 modifier += 1 << (strength - 1);
97 modifier >>= strength;
102 modifier = 16 - modifier;
103 modifier *= filter_weight;
105 count[k] += modifier;
106 accumulator[k] += modifier * pixel_value;
111 byte += stride - block_size;
115 #if ALT_REF_MC_ENABLED
117 static int temporal_filter_find_matching_mb_c(VP9_COMP *cpi,
118 uint8_t *arf_frame_buf,
119 uint8_t *frame_ptr_buf,
122 MACROBLOCK *x = &cpi->mb;
123 MACROBLOCKD* const xd = &x->e_mbd;
125 int sadpb = x->sadperbit16;
126 int bestsme = INT_MAX;
129 int_mv best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
133 struct buf_2d src = x->plane[0].src;
134 struct buf_2d pre = xd->plane[0].pre[0];
136 best_ref_mv1.as_int = 0;
137 best_ref_mv1_full.as_mv.col = best_ref_mv1.as_mv.col >> 3;
138 best_ref_mv1_full.as_mv.row = best_ref_mv1.as_mv.row >> 3;
140 // Setup frame pointers
141 x->plane[0].src.buf = arf_frame_buf;
142 x->plane[0].src.stride = stride;
143 xd->plane[0].pre[0].buf = frame_ptr_buf;
144 xd->plane[0].pre[0].stride = stride;
146 // Further step/diamond searches as necessary
148 step_param = cpi->sf.reduce_first_step_size + ((cpi->speed > 5) ? 1 : 0);
150 step_param = cpi->sf.reduce_first_step_size + 2;
151 step_param = MIN(step_param, (cpi->sf.max_step_search_steps - 2));
153 /*cpi->sf.search_method == HEX*/
154 // Ignore mv costing by sending NULL pointer instead of cost arrays
155 ref_mv = &x->e_mbd.mi_8x8[0]->bmi[0].as_mv[0];
156 bestsme = vp9_hex_search(x, &best_ref_mv1_full.as_mv,
157 step_param, sadpb, 1,
158 &cpi->fn_ptr[BLOCK_16X16],
159 0, &best_ref_mv1.as_mv, &ref_mv->as_mv);
161 #if ALT_REF_SUBPEL_ENABLED
163 // if (bestsme > error_thresh && bestsme < INT_MAX)
167 // Ignore mv costing by sending NULL pointer instead of cost array
168 bestsme = cpi->find_fractional_mv_step(x, &ref_mv->as_mv,
170 cpi->common.allow_high_precision_mv,
172 &cpi->fn_ptr[BLOCK_16X16],
173 0, cpi->sf.subpel_iters_per_step,
179 // Restore input state
180 x->plane[0].src = src;
181 xd->plane[0].pre[0] = pre;
187 static void temporal_filter_iterate_c(VP9_COMP *cpi,
191 struct scale_factors *scale) {
195 unsigned int filter_weight;
196 int mb_cols = cpi->common.mb_cols;
197 int mb_rows = cpi->common.mb_rows;
199 int mb_uv_offset = 0;
200 DECLARE_ALIGNED_ARRAY(16, unsigned int, accumulator, 16 * 16 + 8 * 8 + 8 * 8);
201 DECLARE_ALIGNED_ARRAY(16, uint16_t, count, 16 * 16 + 8 * 8 + 8 * 8);
202 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
203 YV12_BUFFER_CONFIG *f = cpi->frames[alt_ref_index];
204 uint8_t *dst1, *dst2;
205 DECLARE_ALIGNED_ARRAY(16, uint8_t, predictor, 16 * 16 + 8 * 8 + 8 * 8);
208 uint8_t* input_buffer[MAX_MB_PLANE];
211 for (i = 0; i < MAX_MB_PLANE; i++)
212 input_buffer[i] = mbd->plane[i].pre[0].buf;
214 for (mb_row = 0; mb_row < mb_rows; mb_row++) {
215 #if ALT_REF_MC_ENABLED
216 // Source frames are extended to 16 pixels. This is different than
217 // L/A/G reference frames that have a border of 32 (VP9BORDERINPIXELS)
218 // A 6/8 tap filter is used for motion search. This requires 2 pixels
219 // before and 3 pixels after. So the largest Y mv on a border would
220 // then be 16 - VP9_INTERP_EXTEND. The UV blocks are half the size of the
221 // Y and therefore only extended by 8. The largest mv that a UV block
222 // can support is 8 - VP9_INTERP_EXTEND. A UV mv is half of a Y mv.
223 // (16 - VP9_INTERP_EXTEND) >> 1 which is greater than
224 // 8 - VP9_INTERP_EXTEND.
225 // To keep the mv in play for both Y and UV planes the max that it
226 // can be on a border is therefore 16 - (2*VP9_INTERP_EXTEND+1).
227 cpi->mb.mv_row_min = -((mb_row * 16) + (17 - 2 * VP9_INTERP_EXTEND));
228 cpi->mb.mv_row_max = ((cpi->common.mb_rows - 1 - mb_row) * 16)
229 + (17 - 2 * VP9_INTERP_EXTEND);
232 for (mb_col = 0; mb_col < mb_cols; mb_col++) {
236 vpx_memset(accumulator, 0, 384 * sizeof(unsigned int));
237 vpx_memset(count, 0, 384 * sizeof(uint16_t));
239 #if ALT_REF_MC_ENABLED
240 cpi->mb.mv_col_min = -((mb_col * 16) + (17 - 2 * VP9_INTERP_EXTEND));
241 cpi->mb.mv_col_max = ((cpi->common.mb_cols - 1 - mb_col) * 16)
242 + (17 - 2 * VP9_INTERP_EXTEND);
245 for (frame = 0; frame < frame_count; frame++) {
246 if (cpi->frames[frame] == NULL)
249 mbd->mi_8x8[0]->bmi[0].as_mv[0].as_mv.row = 0;
250 mbd->mi_8x8[0]->bmi[0].as_mv[0].as_mv.col = 0;
252 if (frame == alt_ref_index) {
256 #if ALT_REF_MC_ENABLED
257 #define THRESH_LOW 10000
258 #define THRESH_HIGH 20000
260 // Find best match in this frame by MC
261 err = temporal_filter_find_matching_mb_c
263 cpi->frames[alt_ref_index]->y_buffer + mb_y_offset,
264 cpi->frames[frame]->y_buffer + mb_y_offset,
265 cpi->frames[frame]->y_stride,
268 // Assign higher weight to matching MB if it's error
269 // score is lower. If not applying MC default behavior
270 // is to weight all MBs equal.
271 filter_weight = err < THRESH_LOW
272 ? 2 : err < THRESH_HIGH ? 1 : 0;
275 if (filter_weight != 0) {
276 // Construct the predictors
277 temporal_filter_predictors_mb_c
279 cpi->frames[frame]->y_buffer + mb_y_offset,
280 cpi->frames[frame]->u_buffer + mb_uv_offset,
281 cpi->frames[frame]->v_buffer + mb_uv_offset,
282 cpi->frames[frame]->y_stride,
283 mbd->mi_8x8[0]->bmi[0].as_mv[0].as_mv.row,
284 mbd->mi_8x8[0]->bmi[0].as_mv[0].as_mv.col,
287 // Apply the filter (YUV)
288 vp9_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
289 predictor, 16, strength, filter_weight,
292 vp9_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
293 predictor + 256, 8, strength, filter_weight,
294 accumulator + 256, count + 256);
296 vp9_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
297 predictor + 320, 8, strength, filter_weight,
298 accumulator + 320, count + 320);
302 // Normalize filter output to produce AltRef frame
303 dst1 = cpi->alt_ref_buffer.y_buffer;
304 stride = cpi->alt_ref_buffer.y_stride;
306 for (i = 0, k = 0; i < 16; i++) {
307 for (j = 0; j < 16; j++, k++) {
308 unsigned int pval = accumulator[k] + (count[k] >> 1);
309 pval *= cpi->fixed_divide[count[k]];
312 dst1[byte] = (uint8_t)pval;
314 // move to next pixel
321 dst1 = cpi->alt_ref_buffer.u_buffer;
322 dst2 = cpi->alt_ref_buffer.v_buffer;
323 stride = cpi->alt_ref_buffer.uv_stride;
325 for (i = 0, k = 256; i < 8; i++) {
326 for (j = 0; j < 8; j++, k++) {
330 unsigned int pval = accumulator[k] + (count[k] >> 1);
331 pval *= cpi->fixed_divide[count[k]];
333 dst1[byte] = (uint8_t)pval;
336 pval = accumulator[m] + (count[m] >> 1);
337 pval *= cpi->fixed_divide[count[m]];
339 dst2[byte] = (uint8_t)pval;
341 // move to next pixel
352 mb_y_offset += 16 * (f->y_stride - mb_cols);
353 mb_uv_offset += 8 * (f->uv_stride - mb_cols);
356 // Restore input state
357 for (i = 0; i < MAX_MB_PLANE; i++)
358 mbd->plane[i].pre[0].buf = input_buffer[i];
361 void vp9_temporal_filter_prepare(VP9_COMP *cpi, int distance) {
362 VP9_COMMON *const cm = &cpi->common;
366 int frames_to_blur_backward = 0;
367 int frames_to_blur_forward = 0;
368 int frames_to_blur = 0;
371 int strength = cpi->active_arnr_strength;
372 int blur_type = cpi->oxcf.arnr_type;
373 int max_frames = cpi->active_arnr_frames;
375 const int num_frames_backward = distance;
376 const int num_frames_forward = vp9_lookahead_depth(cpi->lookahead)
377 - (num_frames_backward + 1);
379 struct scale_factors scale;
380 struct scale_factors_common scale_comm;
385 frames_to_blur_backward = num_frames_backward;
387 if (frames_to_blur_backward >= max_frames)
388 frames_to_blur_backward = max_frames - 1;
390 frames_to_blur = frames_to_blur_backward + 1;
396 frames_to_blur_forward = num_frames_forward;
398 if (frames_to_blur_forward >= max_frames)
399 frames_to_blur_forward = max_frames - 1;
401 frames_to_blur = frames_to_blur_forward + 1;
407 frames_to_blur_forward = num_frames_forward;
408 frames_to_blur_backward = num_frames_backward;
410 if (frames_to_blur_forward > frames_to_blur_backward)
411 frames_to_blur_forward = frames_to_blur_backward;
413 if (frames_to_blur_backward > frames_to_blur_forward)
414 frames_to_blur_backward = frames_to_blur_forward;
416 // When max_frames is even we have 1 more frame backward than forward
417 if (frames_to_blur_forward > (max_frames - 1) / 2)
418 frames_to_blur_forward = ((max_frames - 1) / 2);
420 if (frames_to_blur_backward > (max_frames / 2))
421 frames_to_blur_backward = (max_frames / 2);
423 frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1;
427 start_frame = distance + frames_to_blur_forward;
432 "max:%d FBCK:%d FFWD:%d ftb:%d ftbbck:%d ftbfwd:%d sei:%d lasei:%d "
434 max_frames, num_frames_backward, num_frames_forward, frames_to_blur,
435 frames_to_blur_backward, frames_to_blur_forward, cpi->source_encode_index,
436 cpi->last_alt_ref_sei, start_frame);
439 // Setup scaling factors. Scaling on each of the arnr frames is not supported
440 vp9_setup_scale_factors_for_frame(&scale, &scale_comm,
441 get_frame_new_buffer(cm)->y_crop_width,
442 get_frame_new_buffer(cm)->y_crop_height,
443 cm->width, cm->height);
445 // Setup frame pointers, NULL indicates frame not included in filter
446 vp9_zero(cpi->frames);
447 for (frame = 0; frame < frames_to_blur; frame++) {
448 int which_buffer = start_frame - frame;
449 struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead,
451 cpi->frames[frames_to_blur - 1 - frame] = &buf->img;
454 temporal_filter_iterate_c(cpi, frames_to_blur, frames_to_blur_backward,
458 void configure_arnr_filter(VP9_COMP *cpi, const unsigned int this_frame,
459 const int group_boost) {
461 int frames_after_arf;
462 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
463 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
466 // Define the arnr filter width for this group of frames:
467 // We only filter frames that lie within a distance of half
468 // the GF interval from the ARF frame. We also have to trap
469 // cases where the filter extends beyond the end of clip.
470 // Note: this_frame->frame has been updated in the loop
471 // so it now points at the ARF frame.
472 half_gf_int = cpi->baseline_gf_interval >> 1;
473 frames_after_arf = (int)(cpi->twopass.total_stats.count - this_frame - 1);
475 switch (cpi->oxcf.arnr_type) {
476 case 1: // Backward filter
478 if (frames_bwd > half_gf_int)
479 frames_bwd = half_gf_int;
482 case 2: // Forward filter
483 if (frames_fwd > half_gf_int)
484 frames_fwd = half_gf_int;
485 if (frames_fwd > frames_after_arf)
486 frames_fwd = frames_after_arf;
490 case 3: // Centered filter
493 if (frames_fwd > frames_after_arf)
494 frames_fwd = frames_after_arf;
495 if (frames_fwd > half_gf_int)
496 frames_fwd = half_gf_int;
498 frames_bwd = frames_fwd;
500 // For even length filter there is one more frame backward
501 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
502 if (frames_bwd < half_gf_int)
503 frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
507 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
509 // Adjust the strength based on active max q
510 q = ((int)vp9_convert_qindex_to_q(cpi->active_worst_quality) >> 1);
512 cpi->active_arnr_strength = cpi->oxcf.arnr_strength;
514 cpi->active_arnr_strength = cpi->oxcf.arnr_strength - (8 - q);
515 if (cpi->active_arnr_strength < 0)
516 cpi->active_arnr_strength = 0;
519 // Adjust number of frames in filter and strength based on gf boost level.
520 if (cpi->active_arnr_frames > (group_boost / 150)) {
521 cpi->active_arnr_frames = (group_boost / 150);
522 cpi->active_arnr_frames += !(cpi->active_arnr_frames & 1);
524 if (cpi->active_arnr_strength > (group_boost / 300)) {
525 cpi->active_arnr_strength = (group_boost / 300);