]> granicus.if.org Git - libvpx/blob - vp10/encoder/temporal_filter.c
Merge changes from topic 'rm-loopfilter-count-param'
[libvpx] / vp10 / encoder / temporal_filter.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include <math.h>
12 #include <limits.h>
13
14 #include "vp10/common/alloccommon.h"
15 #include "vp10/common/onyxc_int.h"
16 #include "vp10/common/quant_common.h"
17 #include "vp10/common/reconinter.h"
18 #include "vp10/encoder/extend.h"
19 #include "vp10/encoder/firstpass.h"
20 #include "vp10/encoder/mcomp.h"
21 #include "vp10/encoder/encoder.h"
22 #include "vp10/encoder/quantize.h"
23 #include "vp10/encoder/ratectrl.h"
24 #include "vp10/encoder/segmentation.h"
25 #include "vp10/encoder/temporal_filter.h"
26 #include "vpx_dsp/vpx_dsp_common.h"
27 #include "vpx_mem/vpx_mem.h"
28 #include "vpx_ports/mem.h"
29 #include "vpx_ports/vpx_timer.h"
30 #include "vpx_scale/vpx_scale.h"
31
32 static int fixed_divide[512];
33
34 static void temporal_filter_predictors_mb_c(MACROBLOCKD *xd,
35                                             uint8_t *y_mb_ptr,
36                                             uint8_t *u_mb_ptr,
37                                             uint8_t *v_mb_ptr,
38                                             int stride,
39                                             int uv_block_width,
40                                             int uv_block_height,
41                                             int mv_row,
42                                             int mv_col,
43                                             uint8_t *pred,
44                                             struct scale_factors *scale,
45                                             int x, int y) {
46   const int which_mv = 0;
47   const MV mv = { mv_row, mv_col };
48   const InterpKernel *const kernel =
49     vp10_filter_kernels[xd->mi[0]->mbmi.interp_filter];
50
51   enum mv_precision mv_precision_uv;
52   int uv_stride;
53   if (uv_block_width == 8) {
54     uv_stride = (stride + 1) >> 1;
55     mv_precision_uv = MV_PRECISION_Q4;
56   } else {
57     uv_stride = stride;
58     mv_precision_uv = MV_PRECISION_Q3;
59   }
60
61 #if CONFIG_VP9_HIGHBITDEPTH
62   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
63     vp10_highbd_build_inter_predictor(y_mb_ptr, stride,
64                                      &pred[0], 16,
65                                      &mv,
66                                      scale,
67                                      16, 16,
68                                      which_mv,
69                                      kernel, MV_PRECISION_Q3, x, y, xd->bd);
70
71     vp10_highbd_build_inter_predictor(u_mb_ptr, uv_stride,
72                                      &pred[256], uv_block_width,
73                                      &mv,
74                                      scale,
75                                      uv_block_width, uv_block_height,
76                                      which_mv,
77                                      kernel, mv_precision_uv, x, y, xd->bd);
78
79     vp10_highbd_build_inter_predictor(v_mb_ptr, uv_stride,
80                                      &pred[512], uv_block_width,
81                                      &mv,
82                                      scale,
83                                      uv_block_width, uv_block_height,
84                                      which_mv,
85                                      kernel, mv_precision_uv, x, y, xd->bd);
86     return;
87   }
88 #endif  // CONFIG_VP9_HIGHBITDEPTH
89   vp10_build_inter_predictor(y_mb_ptr, stride,
90                             &pred[0], 16,
91                             &mv,
92                             scale,
93                             16, 16,
94                             which_mv,
95                             kernel, MV_PRECISION_Q3, x, y);
96
97   vp10_build_inter_predictor(u_mb_ptr, uv_stride,
98                             &pred[256], uv_block_width,
99                             &mv,
100                             scale,
101                             uv_block_width, uv_block_height,
102                             which_mv,
103                             kernel, mv_precision_uv, x, y);
104
105   vp10_build_inter_predictor(v_mb_ptr, uv_stride,
106                             &pred[512], uv_block_width,
107                             &mv,
108                             scale,
109                             uv_block_width, uv_block_height,
110                             which_mv,
111                             kernel, mv_precision_uv, x, y);
112 }
113
114 void vp10_temporal_filter_init(void) {
115   int i;
116
117   fixed_divide[0] = 0;
118   for (i = 1; i < 512; ++i)
119     fixed_divide[i] = 0x80000 / i;
120 }
121
122 void vp10_temporal_filter_apply_c(uint8_t *frame1,
123                                  unsigned int stride,
124                                  uint8_t *frame2,
125                                  unsigned int block_width,
126                                  unsigned int block_height,
127                                  int strength,
128                                  int filter_weight,
129                                  unsigned int *accumulator,
130                                  uint16_t *count) {
131   unsigned int i, j, k;
132   int modifier;
133   int byte = 0;
134   const int rounding = strength > 0 ? 1 << (strength - 1) : 0;
135
136   for (i = 0, k = 0; i < block_height; i++) {
137     for (j = 0; j < block_width; j++, k++) {
138       int src_byte = frame1[byte];
139       int pixel_value = *frame2++;
140
141       modifier   = src_byte - pixel_value;
142       // This is an integer approximation of:
143       // float coeff = (3.0 * modifer * modifier) / pow(2, strength);
144       // modifier =  (int)roundf(coeff > 16 ? 0 : 16-coeff);
145       modifier  *= modifier;
146       modifier  *= 3;
147       modifier  += rounding;
148       modifier >>= strength;
149
150       if (modifier > 16)
151         modifier = 16;
152
153       modifier = 16 - modifier;
154       modifier *= filter_weight;
155
156       count[k] += modifier;
157       accumulator[k] += modifier * pixel_value;
158
159       byte++;
160     }
161
162     byte += stride - block_width;
163   }
164 }
165
166 #if CONFIG_VP9_HIGHBITDEPTH
167 void vp10_highbd_temporal_filter_apply_c(uint8_t *frame1_8,
168                                         unsigned int stride,
169                                         uint8_t *frame2_8,
170                                         unsigned int block_width,
171                                         unsigned int block_height,
172                                         int strength,
173                                         int filter_weight,
174                                         unsigned int *accumulator,
175                                         uint16_t *count) {
176   uint16_t *frame1 = CONVERT_TO_SHORTPTR(frame1_8);
177   uint16_t *frame2 = CONVERT_TO_SHORTPTR(frame2_8);
178   unsigned int i, j, k;
179   int modifier;
180   int byte = 0;
181   const int rounding = strength > 0 ? 1 << (strength - 1) : 0;
182
183   for (i = 0, k = 0; i < block_height; i++) {
184     for (j = 0; j < block_width; j++, k++) {
185       int src_byte = frame1[byte];
186       int pixel_value = *frame2++;
187
188       modifier   = src_byte - pixel_value;
189       // This is an integer approximation of:
190       // float coeff = (3.0 * modifer * modifier) / pow(2, strength);
191       // modifier =  (int)roundf(coeff > 16 ? 0 : 16-coeff);
192       modifier *= modifier;
193       modifier *= 3;
194       modifier += rounding;
195       modifier >>= strength;
196
197       if (modifier > 16)
198         modifier = 16;
199
200       modifier = 16 - modifier;
201       modifier *= filter_weight;
202
203       count[k] += modifier;
204       accumulator[k] += modifier * pixel_value;
205
206       byte++;
207     }
208
209     byte += stride - block_width;
210   }
211 }
212 #endif  // CONFIG_VP9_HIGHBITDEPTH
213
214 static int temporal_filter_find_matching_mb_c(VP10_COMP *cpi,
215                                               uint8_t *arf_frame_buf,
216                                               uint8_t *frame_ptr_buf,
217                                               int stride) {
218   MACROBLOCK *const x = &cpi->td.mb;
219   MACROBLOCKD *const xd = &x->e_mbd;
220   const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
221   int step_param;
222   int sadpb = x->sadperbit16;
223   int bestsme = INT_MAX;
224   int distortion;
225   unsigned int sse;
226   int cost_list[5];
227
228   MV best_ref_mv1 = {0, 0};
229   MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
230   MV *ref_mv = &x->e_mbd.mi[0]->bmi[0].as_mv[0].as_mv;
231
232   // Save input state
233   struct buf_2d src = x->plane[0].src;
234   struct buf_2d pre = xd->plane[0].pre[0];
235
236   best_ref_mv1_full.col = best_ref_mv1.col >> 3;
237   best_ref_mv1_full.row = best_ref_mv1.row >> 3;
238
239   // Setup frame pointers
240   x->plane[0].src.buf = arf_frame_buf;
241   x->plane[0].src.stride = stride;
242   xd->plane[0].pre[0].buf = frame_ptr_buf;
243   xd->plane[0].pre[0].stride = stride;
244
245   step_param = mv_sf->reduce_first_step_size;
246   step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
247
248   // Ignore mv costing by sending NULL pointer instead of cost arrays
249   vp10_hex_search(x, &best_ref_mv1_full, step_param, sadpb, 1,
250                  cond_cost_list(cpi, cost_list),
251                  &cpi->fn_ptr[BLOCK_16X16], 0, &best_ref_mv1, ref_mv);
252
253   // Ignore mv costing by sending NULL pointer instead of cost array
254   bestsme = cpi->find_fractional_mv_step(x, ref_mv,
255                                          &best_ref_mv1,
256                                          cpi->common.allow_high_precision_mv,
257                                          x->errorperbit,
258                                          &cpi->fn_ptr[BLOCK_16X16],
259                                          0, mv_sf->subpel_iters_per_step,
260                                          cond_cost_list(cpi, cost_list),
261                                          NULL, NULL,
262                                          &distortion, &sse, NULL, 0, 0);
263
264   // Restore input state
265   x->plane[0].src = src;
266   xd->plane[0].pre[0] = pre;
267
268   return bestsme;
269 }
270
271 static void temporal_filter_iterate_c(VP10_COMP *cpi,
272                                       YV12_BUFFER_CONFIG **frames,
273                                       int frame_count,
274                                       int alt_ref_index,
275                                       int strength,
276                                       struct scale_factors *scale) {
277   int byte;
278   int frame;
279   int mb_col, mb_row;
280   unsigned int filter_weight;
281   int mb_cols = (frames[alt_ref_index]->y_crop_width + 15) >> 4;
282   int mb_rows = (frames[alt_ref_index]->y_crop_height + 15) >> 4;
283   int mb_y_offset = 0;
284   int mb_uv_offset = 0;
285   DECLARE_ALIGNED(16, unsigned int, accumulator[16 * 16 * 3]);
286   DECLARE_ALIGNED(16, uint16_t, count[16 * 16 * 3]);
287   MACROBLOCKD *mbd = &cpi->td.mb.e_mbd;
288   YV12_BUFFER_CONFIG *f = frames[alt_ref_index];
289   uint8_t *dst1, *dst2;
290 #if CONFIG_VP9_HIGHBITDEPTH
291   DECLARE_ALIGNED(16, uint16_t,  predictor16[16 * 16 * 3]);
292   DECLARE_ALIGNED(16, uint8_t,  predictor8[16 * 16 * 3]);
293   uint8_t *predictor;
294 #else
295   DECLARE_ALIGNED(16, uint8_t,  predictor[16 * 16 * 3]);
296 #endif
297   const int mb_uv_height = 16 >> mbd->plane[1].subsampling_y;
298   const int mb_uv_width  = 16 >> mbd->plane[1].subsampling_x;
299
300   // Save input state
301   uint8_t* input_buffer[MAX_MB_PLANE];
302   int i;
303 #if CONFIG_VP9_HIGHBITDEPTH
304   if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
305     predictor = CONVERT_TO_BYTEPTR(predictor16);
306   } else {
307     predictor = predictor8;
308   }
309 #endif
310
311   for (i = 0; i < MAX_MB_PLANE; i++)
312     input_buffer[i] = mbd->plane[i].pre[0].buf;
313
314   for (mb_row = 0; mb_row < mb_rows; mb_row++) {
315     // Source frames are extended to 16 pixels. This is different than
316     //  L/A/G reference frames that have a border of 32 (VP9ENCBORDERINPIXELS)
317     // A 6/8 tap filter is used for motion search.  This requires 2 pixels
318     //  before and 3 pixels after.  So the largest Y mv on a border would
319     //  then be 16 - VP9_INTERP_EXTEND. The UV blocks are half the size of the
320     //  Y and therefore only extended by 8.  The largest mv that a UV block
321     //  can support is 8 - VP9_INTERP_EXTEND.  A UV mv is half of a Y mv.
322     //  (16 - VP9_INTERP_EXTEND) >> 1 which is greater than
323     //  8 - VP9_INTERP_EXTEND.
324     // To keep the mv in play for both Y and UV planes the max that it
325     //  can be on a border is therefore 16 - (2*VP9_INTERP_EXTEND+1).
326     cpi->td.mb.mv_row_min = -((mb_row * 16) + (17 - 2 * VP9_INTERP_EXTEND));
327     cpi->td.mb.mv_row_max = ((mb_rows - 1 - mb_row) * 16)
328                          + (17 - 2 * VP9_INTERP_EXTEND);
329
330     for (mb_col = 0; mb_col < mb_cols; mb_col++) {
331       int i, j, k;
332       int stride;
333
334       memset(accumulator, 0, 16 * 16 * 3 * sizeof(accumulator[0]));
335       memset(count, 0, 16 * 16 * 3 * sizeof(count[0]));
336
337       cpi->td.mb.mv_col_min = -((mb_col * 16) + (17 - 2 * VP9_INTERP_EXTEND));
338       cpi->td.mb.mv_col_max = ((mb_cols - 1 - mb_col) * 16)
339                            + (17 - 2 * VP9_INTERP_EXTEND);
340
341       for (frame = 0; frame < frame_count; frame++) {
342         const int thresh_low  = 10000;
343         const int thresh_high = 20000;
344
345         if (frames[frame] == NULL)
346           continue;
347
348         mbd->mi[0]->bmi[0].as_mv[0].as_mv.row = 0;
349         mbd->mi[0]->bmi[0].as_mv[0].as_mv.col = 0;
350
351         if (frame == alt_ref_index) {
352           filter_weight = 2;
353         } else {
354           // Find best match in this frame by MC
355           int err = temporal_filter_find_matching_mb_c(cpi,
356               frames[alt_ref_index]->y_buffer + mb_y_offset,
357               frames[frame]->y_buffer + mb_y_offset,
358               frames[frame]->y_stride);
359
360           // Assign higher weight to matching MB if it's error
361           // score is lower. If not applying MC default behavior
362           // is to weight all MBs equal.
363           filter_weight = err < thresh_low
364                           ? 2 : err < thresh_high ? 1 : 0;
365         }
366
367         if (filter_weight != 0) {
368           // Construct the predictors
369           temporal_filter_predictors_mb_c(mbd,
370               frames[frame]->y_buffer + mb_y_offset,
371               frames[frame]->u_buffer + mb_uv_offset,
372               frames[frame]->v_buffer + mb_uv_offset,
373               frames[frame]->y_stride,
374               mb_uv_width, mb_uv_height,
375               mbd->mi[0]->bmi[0].as_mv[0].as_mv.row,
376               mbd->mi[0]->bmi[0].as_mv[0].as_mv.col,
377               predictor, scale,
378               mb_col * 16, mb_row * 16);
379
380 #if CONFIG_VP9_HIGHBITDEPTH
381           if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
382             int adj_strength = strength + 2 * (mbd->bd - 8);
383             // Apply the filter (YUV)
384             vp10_highbd_temporal_filter_apply(f->y_buffer + mb_y_offset,
385                                              f->y_stride,
386                                              predictor, 16, 16, adj_strength,
387                                              filter_weight,
388                                              accumulator, count);
389             vp10_highbd_temporal_filter_apply(f->u_buffer + mb_uv_offset,
390                                              f->uv_stride, predictor + 256,
391                                              mb_uv_width, mb_uv_height,
392                                              adj_strength,
393                                              filter_weight, accumulator + 256,
394                                              count + 256);
395             vp10_highbd_temporal_filter_apply(f->v_buffer + mb_uv_offset,
396                                              f->uv_stride, predictor + 512,
397                                              mb_uv_width, mb_uv_height,
398                                              adj_strength, filter_weight,
399                                              accumulator + 512, count + 512);
400           } else {
401             // Apply the filter (YUV)
402             vp10_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
403                                       predictor, 16, 16,
404                                       strength, filter_weight,
405                                       accumulator, count);
406             vp10_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
407                                       predictor + 256,
408                                       mb_uv_width, mb_uv_height, strength,
409                                       filter_weight, accumulator + 256,
410                                       count + 256);
411             vp10_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
412                                       predictor + 512,
413                                       mb_uv_width, mb_uv_height, strength,
414                                       filter_weight, accumulator + 512,
415                                       count + 512);
416           }
417 #else
418           // Apply the filter (YUV)
419           vp10_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
420                                     predictor, 16, 16,
421                                     strength, filter_weight,
422                                     accumulator, count);
423           vp10_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
424                                     predictor + 256,
425                                     mb_uv_width, mb_uv_height, strength,
426                                     filter_weight, accumulator + 256,
427                                     count + 256);
428           vp10_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
429                                     predictor + 512,
430                                     mb_uv_width, mb_uv_height, strength,
431                                     filter_weight, accumulator + 512,
432                                     count + 512);
433 #endif  // CONFIG_VP9_HIGHBITDEPTH
434         }
435       }
436
437 #if CONFIG_VP9_HIGHBITDEPTH
438       if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
439         uint16_t *dst1_16;
440         uint16_t *dst2_16;
441         // Normalize filter output to produce AltRef frame
442         dst1 = cpi->alt_ref_buffer.y_buffer;
443         dst1_16 = CONVERT_TO_SHORTPTR(dst1);
444         stride = cpi->alt_ref_buffer.y_stride;
445         byte = mb_y_offset;
446         for (i = 0, k = 0; i < 16; i++) {
447           for (j = 0; j < 16; j++, k++) {
448             unsigned int pval = accumulator[k] + (count[k] >> 1);
449             pval *= fixed_divide[count[k]];
450             pval >>= 19;
451
452             dst1_16[byte] = (uint16_t)pval;
453
454             // move to next pixel
455             byte++;
456           }
457
458           byte += stride - 16;
459         }
460
461         dst1 = cpi->alt_ref_buffer.u_buffer;
462         dst2 = cpi->alt_ref_buffer.v_buffer;
463         dst1_16 = CONVERT_TO_SHORTPTR(dst1);
464         dst2_16 = CONVERT_TO_SHORTPTR(dst2);
465         stride = cpi->alt_ref_buffer.uv_stride;
466         byte = mb_uv_offset;
467         for (i = 0, k = 256; i < mb_uv_height; i++) {
468           for (j = 0; j < mb_uv_width; j++, k++) {
469             int m = k + 256;
470
471             // U
472             unsigned int pval = accumulator[k] + (count[k] >> 1);
473             pval *= fixed_divide[count[k]];
474             pval >>= 19;
475             dst1_16[byte] = (uint16_t)pval;
476
477             // V
478             pval = accumulator[m] + (count[m] >> 1);
479             pval *= fixed_divide[count[m]];
480             pval >>= 19;
481             dst2_16[byte] = (uint16_t)pval;
482
483             // move to next pixel
484             byte++;
485           }
486
487           byte += stride - mb_uv_width;
488         }
489       } else {
490         // Normalize filter output to produce AltRef frame
491         dst1 = cpi->alt_ref_buffer.y_buffer;
492         stride = cpi->alt_ref_buffer.y_stride;
493         byte = mb_y_offset;
494         for (i = 0, k = 0; i < 16; i++) {
495           for (j = 0; j < 16; j++, k++) {
496             unsigned int pval = accumulator[k] + (count[k] >> 1);
497             pval *= fixed_divide[count[k]];
498             pval >>= 19;
499
500             dst1[byte] = (uint8_t)pval;
501
502             // move to next pixel
503             byte++;
504           }
505           byte += stride - 16;
506         }
507
508         dst1 = cpi->alt_ref_buffer.u_buffer;
509         dst2 = cpi->alt_ref_buffer.v_buffer;
510         stride = cpi->alt_ref_buffer.uv_stride;
511         byte = mb_uv_offset;
512         for (i = 0, k = 256; i < mb_uv_height; i++) {
513           for (j = 0; j < mb_uv_width; j++, k++) {
514             int m = k + 256;
515
516             // U
517             unsigned int pval = accumulator[k] + (count[k] >> 1);
518             pval *= fixed_divide[count[k]];
519             pval >>= 19;
520             dst1[byte] = (uint8_t)pval;
521
522             // V
523             pval = accumulator[m] + (count[m] >> 1);
524             pval *= fixed_divide[count[m]];
525             pval >>= 19;
526             dst2[byte] = (uint8_t)pval;
527
528             // move to next pixel
529             byte++;
530           }
531           byte += stride - mb_uv_width;
532         }
533       }
534 #else
535       // Normalize filter output to produce AltRef frame
536       dst1 = cpi->alt_ref_buffer.y_buffer;
537       stride = cpi->alt_ref_buffer.y_stride;
538       byte = mb_y_offset;
539       for (i = 0, k = 0; i < 16; i++) {
540         for (j = 0; j < 16; j++, k++) {
541           unsigned int pval = accumulator[k] + (count[k] >> 1);
542           pval *= fixed_divide[count[k]];
543           pval >>= 19;
544
545           dst1[byte] = (uint8_t)pval;
546
547           // move to next pixel
548           byte++;
549         }
550         byte += stride - 16;
551       }
552
553       dst1 = cpi->alt_ref_buffer.u_buffer;
554       dst2 = cpi->alt_ref_buffer.v_buffer;
555       stride = cpi->alt_ref_buffer.uv_stride;
556       byte = mb_uv_offset;
557       for (i = 0, k = 256; i < mb_uv_height; i++) {
558         for (j = 0; j < mb_uv_width; j++, k++) {
559           int m = k + 256;
560
561           // U
562           unsigned int pval = accumulator[k] + (count[k] >> 1);
563           pval *= fixed_divide[count[k]];
564           pval >>= 19;
565           dst1[byte] = (uint8_t)pval;
566
567           // V
568           pval = accumulator[m] + (count[m] >> 1);
569           pval *= fixed_divide[count[m]];
570           pval >>= 19;
571           dst2[byte] = (uint8_t)pval;
572
573           // move to next pixel
574           byte++;
575         }
576         byte += stride - mb_uv_width;
577       }
578 #endif  // CONFIG_VP9_HIGHBITDEPTH
579       mb_y_offset += 16;
580       mb_uv_offset += mb_uv_width;
581     }
582     mb_y_offset += 16 * (f->y_stride - mb_cols);
583     mb_uv_offset += mb_uv_height * f->uv_stride - mb_uv_width * mb_cols;
584   }
585
586   // Restore input state
587   for (i = 0; i < MAX_MB_PLANE; i++)
588     mbd->plane[i].pre[0].buf = input_buffer[i];
589 }
590
591 // Apply buffer limits and context specific adjustments to arnr filter.
592 static void adjust_arnr_filter(VP10_COMP *cpi,
593                                int distance, int group_boost,
594                                int *arnr_frames, int *arnr_strength) {
595   const VP10EncoderConfig *const oxcf = &cpi->oxcf;
596   const int frames_after_arf =
597       vp10_lookahead_depth(cpi->lookahead) - distance - 1;
598   int frames_fwd = (cpi->oxcf.arnr_max_frames - 1) >> 1;
599   int frames_bwd;
600   int q, frames, strength;
601
602   // Define the forward and backwards filter limits for this arnr group.
603   if (frames_fwd > frames_after_arf)
604     frames_fwd = frames_after_arf;
605   if (frames_fwd > distance)
606     frames_fwd = distance;
607
608   frames_bwd = frames_fwd;
609
610   // For even length filter there is one more frame backward
611   // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
612   if (frames_bwd < distance)
613     frames_bwd += (oxcf->arnr_max_frames + 1) & 0x1;
614
615   // Set the baseline active filter size.
616   frames = frames_bwd + 1 + frames_fwd;
617
618   // Adjust the strength based on active max q.
619   if (cpi->common.current_video_frame > 1)
620     q = ((int)vp10_convert_qindex_to_q(
621         cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth));
622   else
623     q = ((int)vp10_convert_qindex_to_q(
624         cpi->rc.avg_frame_qindex[KEY_FRAME], cpi->common.bit_depth));
625   if (q > 16) {
626     strength = oxcf->arnr_strength;
627   } else {
628     strength = oxcf->arnr_strength - ((16 - q) / 2);
629     if (strength < 0)
630       strength = 0;
631   }
632
633   // Adjust number of frames in filter and strength based on gf boost level.
634   if (frames > group_boost / 150) {
635     frames = group_boost / 150;
636     frames += !(frames & 1);
637   }
638
639   if (strength > group_boost / 300) {
640     strength = group_boost / 300;
641   }
642
643   // Adjustments for second level arf in multi arf case.
644   if (cpi->oxcf.pass == 2 && cpi->multi_arf_allowed) {
645     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
646     if (gf_group->rf_level[gf_group->index] != GF_ARF_STD) {
647       strength >>= 1;
648     }
649   }
650
651   *arnr_frames = frames;
652   *arnr_strength = strength;
653 }
654
655 void vp10_temporal_filter(VP10_COMP *cpi, int distance) {
656   RATE_CONTROL *const rc = &cpi->rc;
657   int frame;
658   int frames_to_blur;
659   int start_frame;
660   int strength;
661   int frames_to_blur_backward;
662   int frames_to_blur_forward;
663   struct scale_factors sf;
664   YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = {NULL};
665
666   // Apply context specific adjustments to the arnr filter parameters.
667   adjust_arnr_filter(cpi, distance, rc->gfu_boost, &frames_to_blur, &strength);
668   frames_to_blur_backward = (frames_to_blur / 2);
669   frames_to_blur_forward = ((frames_to_blur - 1) / 2);
670   start_frame = distance + frames_to_blur_forward;
671
672   // Setup frame pointers, NULL indicates frame not included in filter.
673   for (frame = 0; frame < frames_to_blur; ++frame) {
674     const int which_buffer = start_frame - frame;
675     struct lookahead_entry *buf = vp10_lookahead_peek(cpi->lookahead,
676                                                      which_buffer);
677     frames[frames_to_blur - 1 - frame] = &buf->img;
678   }
679
680   if (frames_to_blur > 0) {
681     // Setup scaling factors. Scaling on each of the arnr frames is not
682     // supported.
683     // ARF is produced at the native frame size and resized when coded.
684 #if CONFIG_VP9_HIGHBITDEPTH
685     vp10_setup_scale_factors_for_frame(&sf,
686                                       frames[0]->y_crop_width,
687                                       frames[0]->y_crop_height,
688                                       frames[0]->y_crop_width,
689                                       frames[0]->y_crop_height,
690                                       cpi->common.use_highbitdepth);
691 #else
692     vp10_setup_scale_factors_for_frame(&sf,
693                                       frames[0]->y_crop_width,
694                                       frames[0]->y_crop_height,
695                                       frames[0]->y_crop_width,
696                                       frames[0]->y_crop_height);
697 #endif  // CONFIG_VP9_HIGHBITDEPTH
698   }
699
700   temporal_filter_iterate_c(cpi, frames, frames_to_blur,
701                             frames_to_blur_backward, strength, &sf);
702 }