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.
15 #include "vp9/common/vp9_alloccommon.h"
16 #include "vp9/common/vp9_common.h"
17 #include "vp9/common/vp9_onyxc_int.h"
18 #include "vp9/common/vp9_quant_common.h"
19 #include "vp9/common/vp9_reconinter.h"
20 #include "vp9/encoder/vp9_encodeframe.h"
21 #include "vp9/encoder/vp9_ethread.h"
22 #include "vp9/encoder/vp9_extend.h"
23 #include "vp9/encoder/vp9_firstpass.h"
24 #include "vp9/encoder/vp9_mcomp.h"
25 #include "vp9/encoder/vp9_encoder.h"
26 #include "vp9/encoder/vp9_quantize.h"
27 #include "vp9/encoder/vp9_ratectrl.h"
28 #include "vp9/encoder/vp9_segmentation.h"
29 #include "vp9/encoder/vp9_temporal_filter.h"
30 #include "vpx_dsp/vpx_dsp_common.h"
31 #include "vpx_mem/vpx_mem.h"
32 #include "vpx_ports/mem.h"
33 #include "vpx_ports/vpx_timer.h"
34 #include "vpx_scale/vpx_scale.h"
36 static int fixed_divide[512];
38 static void temporal_filter_predictors_mb_c(
39 MACROBLOCKD *xd, uint8_t *y_mb_ptr, uint8_t *u_mb_ptr, uint8_t *v_mb_ptr,
40 int stride, int uv_block_width, int uv_block_height, int mv_row, int mv_col,
41 uint8_t *pred, struct scale_factors *scale, int x, int y) {
42 const int which_mv = 0;
43 const MV mv = { mv_row, mv_col };
44 const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP_SHARP];
46 enum mv_precision mv_precision_uv;
48 if (uv_block_width == 8) {
49 uv_stride = (stride + 1) >> 1;
50 mv_precision_uv = MV_PRECISION_Q4;
53 mv_precision_uv = MV_PRECISION_Q3;
56 #if CONFIG_VP9_HIGHBITDEPTH
57 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
58 vp9_highbd_build_inter_predictor(CONVERT_TO_SHORTPTR(y_mb_ptr), stride,
59 CONVERT_TO_SHORTPTR(&pred[0]), 16, &mv,
60 scale, 16, 16, which_mv, kernel,
61 MV_PRECISION_Q3, x, y, xd->bd);
63 vp9_highbd_build_inter_predictor(CONVERT_TO_SHORTPTR(u_mb_ptr), uv_stride,
64 CONVERT_TO_SHORTPTR(&pred[256]),
65 uv_block_width, &mv, scale, uv_block_width,
66 uv_block_height, which_mv, kernel,
67 mv_precision_uv, x, y, xd->bd);
69 vp9_highbd_build_inter_predictor(CONVERT_TO_SHORTPTR(v_mb_ptr), uv_stride,
70 CONVERT_TO_SHORTPTR(&pred[512]),
71 uv_block_width, &mv, scale, uv_block_width,
72 uv_block_height, which_mv, kernel,
73 mv_precision_uv, x, y, xd->bd);
76 #endif // CONFIG_VP9_HIGHBITDEPTH
78 vp9_build_inter_predictor(y_mb_ptr, stride, &pred[0], 16, &mv, scale, 16, 16,
79 which_mv, kernel, MV_PRECISION_Q3, x, y);
81 vp9_build_inter_predictor(u_mb_ptr, uv_stride, &pred[256], uv_block_width,
82 &mv, scale, uv_block_width, uv_block_height,
83 which_mv, kernel, mv_precision_uv, x, y);
85 vp9_build_inter_predictor(v_mb_ptr, uv_stride, &pred[512], uv_block_width,
86 &mv, scale, uv_block_width, uv_block_height,
87 which_mv, kernel, mv_precision_uv, x, y);
90 void vp9_temporal_filter_init(void) {
94 for (i = 1; i < 512; ++i) fixed_divide[i] = 0x80000 / i;
97 void vp9_temporal_filter_apply_c(const uint8_t *frame1, unsigned int stride,
98 const uint8_t *frame2,
99 unsigned int block_width,
100 unsigned int block_height, int strength,
101 int filter_weight, uint32_t *accumulator,
103 unsigned int i, j, k;
106 const int rounding = strength > 0 ? 1 << (strength - 1) : 0;
108 assert(strength >= 0);
109 assert(strength <= 6);
111 assert(filter_weight >= 0);
112 assert(filter_weight <= 2);
114 for (i = 0, k = 0; i < block_height; i++) {
115 for (j = 0; j < block_width; j++, k++) {
116 int pixel_value = *frame2;
118 // non-local mean approach
119 int diff_sse[9] = { 0 };
120 int idx, idy, index = 0;
122 for (idy = -1; idy <= 1; ++idy) {
123 for (idx = -1; idx <= 1; ++idx) {
124 int row = (int)i + idy;
125 int col = (int)j + idx;
127 if (row >= 0 && row < (int)block_height && col >= 0 &&
128 col < (int)block_width) {
129 int diff = frame1[byte + idy * (int)stride + idx] -
130 frame2[idy * (int)block_width + idx];
131 diff_sse[index] = diff * diff;
140 for (idx = 0; idx < 9; ++idx) modifier += diff_sse[idx];
147 modifier += rounding;
148 modifier >>= strength;
150 if (modifier > 16) modifier = 16;
152 modifier = 16 - modifier;
153 modifier *= filter_weight;
155 count[k] += modifier;
156 accumulator[k] += modifier * pixel_value;
161 byte += stride - block_width;
165 #if CONFIG_VP9_HIGHBITDEPTH
166 void vp9_highbd_temporal_filter_apply_c(
167 const uint8_t *frame1_8, unsigned int stride, const uint8_t *frame2_8,
168 unsigned int block_width, unsigned int block_height, int strength,
169 int filter_weight, uint32_t *accumulator, uint16_t *count) {
170 const uint16_t *frame1 = CONVERT_TO_SHORTPTR(frame1_8);
171 const uint16_t *frame2 = CONVERT_TO_SHORTPTR(frame2_8);
172 unsigned int i, j, k;
175 const int rounding = strength > 0 ? 1 << (strength - 1) : 0;
177 for (i = 0, k = 0; i < block_height; i++) {
178 for (j = 0; j < block_width; j++, k++) {
179 int pixel_value = *frame2;
180 int diff_sse[9] = { 0 };
181 int idx, idy, index = 0;
183 for (idy = -1; idy <= 1; ++idy) {
184 for (idx = -1; idx <= 1; ++idx) {
185 int row = (int)i + idy;
186 int col = (int)j + idx;
188 if (row >= 0 && row < (int)block_height && col >= 0 &&
189 col < (int)block_width) {
190 int diff = frame1[byte + idy * (int)stride + idx] -
191 frame2[idy * (int)block_width + idx];
192 diff_sse[index] = diff * diff;
200 for (idx = 0; idx < 9; ++idx) modifier += diff_sse[idx];
206 modifier += rounding;
207 modifier >>= strength;
209 if (modifier > 16) modifier = 16;
211 modifier = 16 - modifier;
212 modifier *= filter_weight;
214 count[k] += modifier;
215 accumulator[k] += modifier * pixel_value;
220 byte += stride - block_width;
223 #endif // CONFIG_VP9_HIGHBITDEPTH
225 static uint32_t temporal_filter_find_matching_mb_c(VP9_COMP *cpi,
227 uint8_t *arf_frame_buf,
228 uint8_t *frame_ptr_buf,
229 int stride, MV *ref_mv) {
230 MACROBLOCK *const x = &td->mb;
231 MACROBLOCKD *const xd = &x->e_mbd;
232 MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
233 const SEARCH_METHODS search_method = HEX;
235 int sadpb = x->sadperbit16;
236 uint32_t bestsme = UINT_MAX;
240 const MvLimits tmp_mv_limits = x->mv_limits;
242 MV best_ref_mv1 = { 0, 0 };
243 MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
246 struct buf_2d src = x->plane[0].src;
247 struct buf_2d pre = xd->plane[0].pre[0];
249 best_ref_mv1_full.col = best_ref_mv1.col >> 3;
250 best_ref_mv1_full.row = best_ref_mv1.row >> 3;
252 // Setup frame pointers
253 x->plane[0].src.buf = arf_frame_buf;
254 x->plane[0].src.stride = stride;
255 xd->plane[0].pre[0].buf = frame_ptr_buf;
256 xd->plane[0].pre[0].stride = stride;
258 step_param = mv_sf->reduce_first_step_size;
259 step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
261 vp9_set_mv_search_range(&x->mv_limits, &best_ref_mv1);
263 vp9_full_pixel_search(cpi, x, BLOCK_16X16, &best_ref_mv1_full, step_param,
264 search_method, sadpb, cond_cost_list(cpi, cost_list),
265 &best_ref_mv1, ref_mv, 0, 0);
267 /* restore UMV window */
268 x->mv_limits = tmp_mv_limits;
270 // Ignore mv costing by sending NULL pointer instead of cost array
271 bestsme = cpi->find_fractional_mv_step(
272 x, ref_mv, &best_ref_mv1, cpi->common.allow_high_precision_mv,
273 x->errorperbit, &cpi->fn_ptr[BLOCK_16X16], 0,
274 mv_sf->subpel_iters_per_step, cond_cost_list(cpi, cost_list), NULL, NULL,
275 &distortion, &sse, NULL, 0, 0);
277 // Restore input state
278 x->plane[0].src = src;
279 xd->plane[0].pre[0] = pre;
284 void vp9_temporal_filter_iterate_row_c(VP9_COMP *cpi, ThreadData *td,
285 int mb_row, int mb_col_start,
287 ARNRFilterData *arnr_filter_data = &cpi->arnr_filter_data;
288 YV12_BUFFER_CONFIG **frames = arnr_filter_data->frames;
289 int frame_count = arnr_filter_data->frame_count;
290 int alt_ref_index = arnr_filter_data->alt_ref_index;
291 int strength = arnr_filter_data->strength;
292 struct scale_factors *scale = &arnr_filter_data->sf;
296 unsigned int filter_weight;
297 int mb_cols = (frames[alt_ref_index]->y_crop_width + 15) >> 4;
298 int mb_rows = (frames[alt_ref_index]->y_crop_height + 15) >> 4;
299 DECLARE_ALIGNED(16, uint32_t, accumulator[16 * 16 * 3]);
300 DECLARE_ALIGNED(16, uint16_t, count[16 * 16 * 3]);
301 MACROBLOCKD *mbd = &td->mb.e_mbd;
302 YV12_BUFFER_CONFIG *f = frames[alt_ref_index];
303 uint8_t *dst1, *dst2;
304 #if CONFIG_VP9_HIGHBITDEPTH
305 DECLARE_ALIGNED(16, uint16_t, predictor16[16 * 16 * 3]);
306 DECLARE_ALIGNED(16, uint8_t, predictor8[16 * 16 * 3]);
309 DECLARE_ALIGNED(16, uint8_t, predictor[16 * 16 * 3]);
311 const int mb_uv_height = 16 >> mbd->plane[1].subsampling_y;
312 const int mb_uv_width = 16 >> mbd->plane[1].subsampling_x;
313 // Addition of the tile col level offsets
314 int mb_y_offset = mb_row * 16 * (f->y_stride) + 16 * mb_col_start;
316 mb_row * mb_uv_height * f->uv_stride + mb_uv_width * mb_col_start;
318 #if CONFIG_VP9_HIGHBITDEPTH
319 if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
320 predictor = CONVERT_TO_BYTEPTR(predictor16);
322 predictor = predictor8;
326 // Source frames are extended to 16 pixels. This is different than
327 // L/A/G reference frames that have a border of 32 (VP9ENCBORDERINPIXELS)
328 // A 6/8 tap filter is used for motion search. This requires 2 pixels
329 // before and 3 pixels after. So the largest Y mv on a border would
330 // then be 16 - VP9_INTERP_EXTEND. The UV blocks are half the size of the
331 // Y and therefore only extended by 8. The largest mv that a UV block
332 // can support is 8 - VP9_INTERP_EXTEND. A UV mv is half of a Y mv.
333 // (16 - VP9_INTERP_EXTEND) >> 1 which is greater than
334 // 8 - VP9_INTERP_EXTEND.
335 // To keep the mv in play for both Y and UV planes the max that it
336 // can be on a border is therefore 16 - (2*VP9_INTERP_EXTEND+1).
337 td->mb.mv_limits.row_min = -((mb_row * 16) + (17 - 2 * VP9_INTERP_EXTEND));
338 td->mb.mv_limits.row_max =
339 ((mb_rows - 1 - mb_row) * 16) + (17 - 2 * VP9_INTERP_EXTEND);
341 for (mb_col = mb_col_start; mb_col < mb_col_end; mb_col++) {
346 vp9_zero_array(accumulator, 16 * 16 * 3);
347 vp9_zero_array(count, 16 * 16 * 3);
349 td->mb.mv_limits.col_min = -((mb_col * 16) + (17 - 2 * VP9_INTERP_EXTEND));
350 td->mb.mv_limits.col_max =
351 ((mb_cols - 1 - mb_col) * 16) + (17 - 2 * VP9_INTERP_EXTEND);
353 if (cpi->oxcf.content == VP9E_CONTENT_FILM) {
354 unsigned int src_variance;
357 src.buf = f->y_buffer + mb_y_offset;
358 src.stride = f->y_stride;
360 #if CONFIG_VP9_HIGHBITDEPTH
361 if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
363 vp9_high_get_sby_perpixel_variance(cpi, &src, BLOCK_16X16, mbd->bd);
365 src_variance = vp9_get_sby_perpixel_variance(cpi, &src, BLOCK_16X16);
368 src_variance = vp9_get_sby_perpixel_variance(cpi, &src, BLOCK_16X16);
369 #endif // CONFIG_VP9_HIGHBITDEPTH
371 if (src_variance <= 2) strength = VPXMAX(0, (int)strength - 2);
374 for (frame = 0; frame < frame_count; frame++) {
375 const uint32_t thresh_low = 10000;
376 const uint32_t thresh_high = 20000;
378 if (frames[frame] == NULL) continue;
383 if (frame == alt_ref_index) {
386 // Find best match in this frame by MC
387 uint32_t err = temporal_filter_find_matching_mb_c(
388 cpi, td, frames[alt_ref_index]->y_buffer + mb_y_offset,
389 frames[frame]->y_buffer + mb_y_offset, frames[frame]->y_stride,
392 // Assign higher weight to matching MB if its error
393 // score is lower. If not applying MC default behavior
394 // is to weight all MBs equal.
395 filter_weight = err < thresh_low ? 2 : err < thresh_high ? 1 : 0;
398 if (filter_weight != 0) {
399 // Construct the predictors
400 temporal_filter_predictors_mb_c(
401 mbd, frames[frame]->y_buffer + mb_y_offset,
402 frames[frame]->u_buffer + mb_uv_offset,
403 frames[frame]->v_buffer + mb_uv_offset, frames[frame]->y_stride,
404 mb_uv_width, mb_uv_height, ref_mv.row, ref_mv.col, predictor, scale,
405 mb_col * 16, mb_row * 16);
407 #if CONFIG_VP9_HIGHBITDEPTH
408 if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
409 int adj_strength = strength + 2 * (mbd->bd - 8);
410 // Apply the filter (YUV)
411 vp9_highbd_temporal_filter_apply(
412 f->y_buffer + mb_y_offset, f->y_stride, predictor, 16, 16,
413 adj_strength, filter_weight, accumulator, count);
414 vp9_highbd_temporal_filter_apply(
415 f->u_buffer + mb_uv_offset, f->uv_stride, predictor + 256,
416 mb_uv_width, mb_uv_height, adj_strength, filter_weight,
417 accumulator + 256, count + 256);
418 vp9_highbd_temporal_filter_apply(
419 f->v_buffer + mb_uv_offset, f->uv_stride, predictor + 512,
420 mb_uv_width, mb_uv_height, adj_strength, filter_weight,
421 accumulator + 512, count + 512);
423 // Apply the filter (YUV)
424 vp9_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
425 predictor, 16, 16, strength, filter_weight,
427 vp9_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
428 predictor + 256, mb_uv_width, mb_uv_height,
429 strength, filter_weight, accumulator + 256,
431 vp9_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
432 predictor + 512, mb_uv_width, mb_uv_height,
433 strength, filter_weight, accumulator + 512,
437 // Apply the filter (YUV)
438 vp9_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
439 predictor, 16, 16, strength, filter_weight,
441 vp9_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
442 predictor + 256, mb_uv_width, mb_uv_height,
443 strength, filter_weight, accumulator + 256,
445 vp9_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
446 predictor + 512, mb_uv_width, mb_uv_height,
447 strength, filter_weight, accumulator + 512,
449 #endif // CONFIG_VP9_HIGHBITDEPTH
453 #if CONFIG_VP9_HIGHBITDEPTH
454 if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
457 // Normalize filter output to produce AltRef frame
458 dst1 = cpi->alt_ref_buffer.y_buffer;
459 dst1_16 = CONVERT_TO_SHORTPTR(dst1);
460 stride = cpi->alt_ref_buffer.y_stride;
462 for (i = 0, k = 0; i < 16; i++) {
463 for (j = 0; j < 16; j++, k++) {
464 unsigned int pval = accumulator[k] + (count[k] >> 1);
465 pval *= fixed_divide[count[k]];
468 dst1_16[byte] = (uint16_t)pval;
470 // move to next pixel
477 dst1 = cpi->alt_ref_buffer.u_buffer;
478 dst2 = cpi->alt_ref_buffer.v_buffer;
479 dst1_16 = CONVERT_TO_SHORTPTR(dst1);
480 dst2_16 = CONVERT_TO_SHORTPTR(dst2);
481 stride = cpi->alt_ref_buffer.uv_stride;
483 for (i = 0, k = 256; i < mb_uv_height; i++) {
484 for (j = 0; j < mb_uv_width; j++, k++) {
488 unsigned int pval = accumulator[k] + (count[k] >> 1);
489 pval *= fixed_divide[count[k]];
491 dst1_16[byte] = (uint16_t)pval;
494 pval = accumulator[m] + (count[m] >> 1);
495 pval *= fixed_divide[count[m]];
497 dst2_16[byte] = (uint16_t)pval;
499 // move to next pixel
503 byte += stride - mb_uv_width;
506 // Normalize filter output to produce AltRef frame
507 dst1 = cpi->alt_ref_buffer.y_buffer;
508 stride = cpi->alt_ref_buffer.y_stride;
510 for (i = 0, k = 0; i < 16; i++) {
511 for (j = 0; j < 16; j++, k++) {
512 unsigned int pval = accumulator[k] + (count[k] >> 1);
513 pval *= fixed_divide[count[k]];
516 dst1[byte] = (uint8_t)pval;
518 // move to next pixel
524 dst1 = cpi->alt_ref_buffer.u_buffer;
525 dst2 = cpi->alt_ref_buffer.v_buffer;
526 stride = cpi->alt_ref_buffer.uv_stride;
528 for (i = 0, k = 256; i < mb_uv_height; i++) {
529 for (j = 0; j < mb_uv_width; j++, k++) {
533 unsigned int pval = accumulator[k] + (count[k] >> 1);
534 pval *= fixed_divide[count[k]];
536 dst1[byte] = (uint8_t)pval;
539 pval = accumulator[m] + (count[m] >> 1);
540 pval *= fixed_divide[count[m]];
542 dst2[byte] = (uint8_t)pval;
544 // move to next pixel
547 byte += stride - mb_uv_width;
551 // Normalize filter output to produce AltRef frame
552 dst1 = cpi->alt_ref_buffer.y_buffer;
553 stride = cpi->alt_ref_buffer.y_stride;
555 for (i = 0, k = 0; i < 16; i++) {
556 for (j = 0; j < 16; j++, k++) {
557 unsigned int pval = accumulator[k] + (count[k] >> 1);
558 pval *= fixed_divide[count[k]];
561 dst1[byte] = (uint8_t)pval;
563 // move to next pixel
569 dst1 = cpi->alt_ref_buffer.u_buffer;
570 dst2 = cpi->alt_ref_buffer.v_buffer;
571 stride = cpi->alt_ref_buffer.uv_stride;
573 for (i = 0, k = 256; i < mb_uv_height; i++) {
574 for (j = 0; j < mb_uv_width; j++, k++) {
578 unsigned int pval = accumulator[k] + (count[k] >> 1);
579 pval *= fixed_divide[count[k]];
581 dst1[byte] = (uint8_t)pval;
584 pval = accumulator[m] + (count[m] >> 1);
585 pval *= fixed_divide[count[m]];
587 dst2[byte] = (uint8_t)pval;
589 // move to next pixel
592 byte += stride - mb_uv_width;
594 #endif // CONFIG_VP9_HIGHBITDEPTH
596 mb_uv_offset += mb_uv_width;
600 static void temporal_filter_iterate_tile_c(VP9_COMP *cpi, int tile_row,
602 VP9_COMMON *const cm = &cpi->common;
603 const int tile_cols = 1 << cm->log2_tile_cols;
604 TileInfo *tile_info =
605 &cpi->tile_data[tile_row * tile_cols + tile_col].tile_info;
606 const int mb_row_start = (tile_info->mi_row_start) >> 1;
607 const int mb_row_end = (tile_info->mi_row_end + 1) >> 1;
608 const int mb_col_start = (tile_info->mi_col_start) >> 1;
609 const int mb_col_end = (tile_info->mi_col_end + 1) >> 1;
612 for (mb_row = mb_row_start; mb_row < mb_row_end; mb_row++) {
613 vp9_temporal_filter_iterate_row_c(cpi, &cpi->td, mb_row, mb_col_start,
618 static void temporal_filter_iterate_c(VP9_COMP *cpi) {
619 VP9_COMMON *const cm = &cpi->common;
620 const int tile_cols = 1 << cm->log2_tile_cols;
621 const int tile_rows = 1 << cm->log2_tile_rows;
622 int tile_row, tile_col;
623 MACROBLOCKD *mbd = &cpi->td.mb.e_mbd;
625 uint8_t *input_buffer[MAX_MB_PLANE];
628 for (i = 0; i < MAX_MB_PLANE; i++) input_buffer[i] = mbd->plane[i].pre[0].buf;
630 vp9_init_tile_data(cpi);
632 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
633 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
634 temporal_filter_iterate_tile_c(cpi, tile_row, tile_col);
638 // Restore input state
639 for (i = 0; i < MAX_MB_PLANE; i++) mbd->plane[i].pre[0].buf = input_buffer[i];
642 // Apply buffer limits and context specific adjustments to arnr filter.
643 static void adjust_arnr_filter(VP9_COMP *cpi, int distance, int group_boost,
644 int *arnr_frames, int *arnr_strength) {
645 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
646 const int frames_after_arf =
647 vp9_lookahead_depth(cpi->lookahead) - distance - 1;
648 int frames_fwd = (cpi->oxcf.arnr_max_frames - 1) >> 1;
650 int q, frames, base_strength, strength;
652 // Context dependent two pass adjustment to strength.
653 if (oxcf->pass == 2) {
654 base_strength = oxcf->arnr_strength + cpi->twopass.arnr_strength_adjustment;
655 // Clip to allowed range.
656 base_strength = VPXMIN(6, VPXMAX(0, base_strength));
658 base_strength = oxcf->arnr_strength;
661 // Define the forward and backwards filter limits for this arnr group.
662 if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
663 if (frames_fwd > distance) frames_fwd = distance;
665 frames_bwd = frames_fwd;
667 // For even length filter there is one more frame backward
668 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
669 if (frames_bwd < distance) frames_bwd += (oxcf->arnr_max_frames + 1) & 0x1;
671 // Set the baseline active filter size.
672 frames = frames_bwd + 1 + frames_fwd;
674 // Adjust the strength based on active max q.
675 if (cpi->common.current_video_frame > 1)
676 q = ((int)vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME],
677 cpi->common.bit_depth));
679 q = ((int)vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[KEY_FRAME],
680 cpi->common.bit_depth));
682 strength = base_strength;
684 strength = base_strength - ((16 - q) / 2);
685 if (strength < 0) strength = 0;
688 // Adjust number of frames in filter and strength based on gf boost level.
689 if (frames > group_boost / 150) {
690 frames = group_boost / 150;
691 frames += !(frames & 1);
694 if (strength > group_boost / 300) {
695 strength = group_boost / 300;
698 // Adjustments for second level arf in multi arf case.
699 if (cpi->oxcf.pass == 2 && cpi->multi_arf_allowed) {
700 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
701 if (gf_group->rf_level[gf_group->index] != GF_ARF_STD) {
706 *arnr_frames = frames;
707 *arnr_strength = strength;
710 void vp9_temporal_filter(VP9_COMP *cpi, int distance) {
711 VP9_COMMON *const cm = &cpi->common;
712 RATE_CONTROL *const rc = &cpi->rc;
713 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
714 ARNRFilterData *arnr_filter_data = &cpi->arnr_filter_data;
719 int frames_to_blur_backward;
720 int frames_to_blur_forward;
721 struct scale_factors *sf = &arnr_filter_data->sf;
722 YV12_BUFFER_CONFIG **frames = arnr_filter_data->frames;
725 // Apply context specific adjustments to the arnr filter parameters.
726 adjust_arnr_filter(cpi, distance, rc->gfu_boost, &frames_to_blur, &strength);
727 frames_to_blur_backward = (frames_to_blur / 2);
728 frames_to_blur_forward = ((frames_to_blur - 1) / 2);
729 start_frame = distance + frames_to_blur_forward;
731 arnr_filter_data->strength = strength;
732 arnr_filter_data->frame_count = frames_to_blur;
733 arnr_filter_data->alt_ref_index = frames_to_blur_backward;
735 // Setup frame pointers, NULL indicates frame not included in filter.
736 for (frame = 0; frame < frames_to_blur; ++frame) {
737 const int which_buffer = start_frame - frame;
738 struct lookahead_entry *buf =
739 vp9_lookahead_peek(cpi->lookahead, which_buffer);
740 frames[frames_to_blur - 1 - frame] = &buf->img;
743 if (frames_to_blur > 0) {
744 // Setup scaling factors. Scaling on each of the arnr frames is not
747 // In spatial svc the scaling factors might be less then 1/2.
748 // So we will use non-normative scaling.
750 #if CONFIG_VP9_HIGHBITDEPTH
751 vp9_setup_scale_factors_for_frame(
752 sf, get_frame_new_buffer(cm)->y_crop_width,
753 get_frame_new_buffer(cm)->y_crop_height,
754 get_frame_new_buffer(cm)->y_crop_width,
755 get_frame_new_buffer(cm)->y_crop_height, cm->use_highbitdepth);
757 vp9_setup_scale_factors_for_frame(
758 sf, get_frame_new_buffer(cm)->y_crop_width,
759 get_frame_new_buffer(cm)->y_crop_height,
760 get_frame_new_buffer(cm)->y_crop_width,
761 get_frame_new_buffer(cm)->y_crop_height);
762 #endif // CONFIG_VP9_HIGHBITDEPTH
764 for (frame = 0; frame < frames_to_blur; ++frame) {
765 if (cm->mi_cols * MI_SIZE != frames[frame]->y_width ||
766 cm->mi_rows * MI_SIZE != frames[frame]->y_height) {
767 if (vpx_realloc_frame_buffer(&cpi->svc.scaled_frames[frame_used],
768 cm->width, cm->height, cm->subsampling_x,
770 #if CONFIG_VP9_HIGHBITDEPTH
771 cm->use_highbitdepth,
773 VP9_ENC_BORDER_IN_PIXELS,
774 cm->byte_alignment, NULL, NULL, NULL)) {
775 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
776 "Failed to reallocate alt_ref_buffer");
778 frames[frame] = vp9_scale_if_required(
779 cm, frames[frame], &cpi->svc.scaled_frames[frame_used], 0,
784 cm->mi = cm->mip + cm->mi_stride + 1;
785 xd->mi = cm->mi_grid_visible;
788 // ARF is produced at the native frame size and resized when coded.
789 #if CONFIG_VP9_HIGHBITDEPTH
790 vp9_setup_scale_factors_for_frame(
791 sf, frames[0]->y_crop_width, frames[0]->y_crop_height,
792 frames[0]->y_crop_width, frames[0]->y_crop_height,
793 cm->use_highbitdepth);
795 vp9_setup_scale_factors_for_frame(
796 sf, frames[0]->y_crop_width, frames[0]->y_crop_height,
797 frames[0]->y_crop_width, frames[0]->y_crop_height);
798 #endif // CONFIG_VP9_HIGHBITDEPTH
802 // Initialize errorperbit and sabperbit.
803 rdmult = (int)vp9_compute_rd_mult_based_on_qindex(cpi, ARNR_FILT_QINDEX);
804 if (rdmult < 1) rdmult = 1;
805 set_error_per_bit(&cpi->td.mb, rdmult);
806 vp9_initialize_me_consts(cpi, &cpi->td.mb, ARNR_FILT_QINDEX);
809 temporal_filter_iterate_c(cpi);
811 vp9_temporal_filter_row_mt(cpi);