temporal_filter.c 24 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
12
13
14
 */

#include <math.h>
#include <limits.h>

15
16
17
18
19
20
21
22
23
24
25
26
27
#include "av1/common/alloccommon.h"
#include "av1/common/onyxc_int.h"
#include "av1/common/quant_common.h"
#include "av1/common/reconinter.h"
#include "av1/common/odintrin.h"
#include "av1/encoder/extend.h"
#include "av1/encoder/firstpass.h"
#include "av1/encoder/mcomp.h"
#include "av1/encoder/encoder.h"
#include "av1/encoder/quantize.h"
#include "av1/encoder/ratectrl.h"
#include "av1/encoder/segmentation.h"
#include "av1/encoder/temporal_filter.h"
Yaowu Xu's avatar
Yaowu Xu committed
28
29
#include "aom_dsp/aom_dsp_common.h"
#include "aom_mem/aom_mem.h"
30
#include "aom_ports/mem.h"
Yaowu Xu's avatar
Yaowu Xu committed
31
32
#include "aom_ports/aom_timer.h"
#include "aom_scale/aom_scale.h"
Jingning Han's avatar
Jingning Han committed
33

34
35
36
37
static void temporal_filter_predictors_mb_c(
    MACROBLOCKD *xd, uint8_t *y_mb_ptr, uint8_t *u_mb_ptr, uint8_t *v_mb_ptr,
    int stride, int uv_block_width, int uv_block_height, int mv_row, int mv_col,
    uint8_t *pred, struct scale_factors *scale, int x, int y) {
Jingning Han's avatar
Jingning Han committed
38
39
40
41
  const int which_mv = 0;
  const MV mv = { mv_row, mv_col };
  enum mv_precision mv_precision_uv;
  int uv_stride;
42
43
  // TODO(angiebird): change plane setting accordingly
  ConvolveParams conv_params = get_conv_params(which_mv, 0);
44

45
#if USE_TEMPORALFILTER_12TAP
46
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
47
48
49
50
  const InterpFilter interp_filter[4] = { TEMPORALFILTER_12TAP,
                                          TEMPORALFILTER_12TAP,
                                          TEMPORALFILTER_12TAP,
                                          TEMPORALFILTER_12TAP };
51
#else
James Zern's avatar
James Zern committed
52
  const InterpFilter interp_filter = TEMPORALFILTER_12TAP;
53
#endif
54
  (void)xd;
55
#else
James Zern's avatar
James Zern committed
56
  const InterpFilter interp_filter = xd->mi[0]->mbmi.interp_filter;
57
#endif  // USE_TEMPORALFILTER_12TAP
58

Jingning Han's avatar
Jingning Han committed
59
60
61
62
63
64
65
66
  if (uv_block_width == 8) {
    uv_stride = (stride + 1) >> 1;
    mv_precision_uv = MV_PRECISION_Q4;
  } else {
    uv_stride = stride;
    mv_precision_uv = MV_PRECISION_Q3;
  }

Yaowu Xu's avatar
Yaowu Xu committed
67
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
68
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xu's avatar
Yaowu Xu committed
69
70
71
72
73
74
75
76
77
78
79
80
81
    av1_highbd_build_inter_predictor(y_mb_ptr, stride, &pred[0], 16, &mv, scale,
                                     16, 16, which_mv, interp_filter,
                                     MV_PRECISION_Q3, x, y, xd->bd);

    av1_highbd_build_inter_predictor(u_mb_ptr, uv_stride, &pred[256],
                                     uv_block_width, &mv, scale, uv_block_width,
                                     uv_block_height, which_mv, interp_filter,
                                     mv_precision_uv, x, y, xd->bd);

    av1_highbd_build_inter_predictor(v_mb_ptr, uv_stride, &pred[512],
                                     uv_block_width, &mv, scale, uv_block_width,
                                     uv_block_height, which_mv, interp_filter,
                                     mv_precision_uv, x, y, xd->bd);
Jingning Han's avatar
Jingning Han committed
82
83
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
84
85
#endif  // CONFIG_AOM_HIGHBITDEPTH
  av1_build_inter_predictor(y_mb_ptr, stride, &pred[0], 16, &mv, scale, 16, 16,
86
                            &conv_params, interp_filter, MV_PRECISION_Q3, x, y);
87

Yaowu Xu's avatar
Yaowu Xu committed
88
89
  av1_build_inter_predictor(u_mb_ptr, uv_stride, &pred[256], uv_block_width,
                            &mv, scale, uv_block_width, uv_block_height,
90
                            &conv_params, interp_filter, mv_precision_uv, x, y);
91

Yaowu Xu's avatar
Yaowu Xu committed
92
93
  av1_build_inter_predictor(v_mb_ptr, uv_stride, &pred[512], uv_block_width,
                            &mv, scale, uv_block_width, uv_block_height,
94
                            &conv_params, interp_filter, mv_precision_uv, x, y);
Jingning Han's avatar
Jingning Han committed
95
96
}

Yaowu Xu's avatar
Yaowu Xu committed
97
98
99
100
101
void av1_temporal_filter_apply_c(uint8_t *frame1, unsigned int stride,
                                 uint8_t *frame2, unsigned int block_width,
                                 unsigned int block_height, int strength,
                                 int filter_weight, unsigned int *accumulator,
                                 uint16_t *count) {
Jingning Han's avatar
Jingning Han committed
102
103
104
105
106
107
108
  unsigned int i, j, k;
  int modifier;
  int byte = 0;
  const int rounding = strength > 0 ? 1 << (strength - 1) : 0;

  for (i = 0, k = 0; i < block_height; i++) {
    for (j = 0; j < block_width; j++, k++) {
Jingning Han's avatar
Jingning Han committed
109
110
111
112
113
114
115
116
      int pixel_value = *frame2;

      // non-local mean approach
      int diff_sse[9] = { 0 };
      int idx, idy, index = 0;

      for (idy = -1; idy <= 1; ++idy) {
        for (idx = -1; idx <= 1; ++idx) {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
117
118
          int row = (int)i + idy;
          int col = (int)j + idx;
Jingning Han's avatar
Jingning Han committed
119

120
121
          if (row >= 0 && row < (int)block_height && col >= 0 &&
              col < (int)block_width) {
Jingning Han's avatar
Jingning Han committed
122
            int diff = frame1[byte + idy * (int)stride + idx] -
123
                       frame2[idy * (int)block_width + idx];
Jingning Han's avatar
Jingning Han committed
124
125
126
127
128
129
130
131
132
            diff_sse[index] = diff * diff;
            ++index;
          }
        }
      }

      assert(index > 0);

      modifier = 0;
133
      for (idx = 0; idx < 9; ++idx) modifier += diff_sse[idx];
Jingning Han's avatar
Jingning Han committed
134
135
136
137
138

      modifier *= 3;
      modifier /= index;

      ++frame2;
Jingning Han's avatar
Jingning Han committed
139

140
      modifier += rounding;
Jingning Han's avatar
Jingning Han committed
141
142
      modifier >>= strength;

143
      if (modifier > 16) modifier = 16;
Jingning Han's avatar
Jingning Han committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157

      modifier = 16 - modifier;
      modifier *= filter_weight;

      count[k] += modifier;
      accumulator[k] += modifier * pixel_value;

      byte++;
    }

    byte += stride - block_width;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
158
159
#if CONFIG_AOM_HIGHBITDEPTH
void av1_highbd_temporal_filter_apply_c(
160
161
162
    uint8_t *frame1_8, unsigned int stride, uint8_t *frame2_8,
    unsigned int block_width, unsigned int block_height, int strength,
    int filter_weight, unsigned int *accumulator, uint16_t *count) {
Jingning Han's avatar
Jingning Han committed
163
164
165
166
167
168
169
170
171
  uint16_t *frame1 = CONVERT_TO_SHORTPTR(frame1_8);
  uint16_t *frame2 = CONVERT_TO_SHORTPTR(frame2_8);
  unsigned int i, j, k;
  int modifier;
  int byte = 0;
  const int rounding = strength > 0 ? 1 << (strength - 1) : 0;

  for (i = 0, k = 0; i < block_height; i++) {
    for (j = 0; j < block_width; j++, k++) {
172
173
174
175
176
177
178
179
      int pixel_value = *frame2;

      // non-local mean approach
      int diff_sse[9] = { 0 };
      int idx, idy, index = 0;

      for (idy = -1; idy <= 1; ++idy) {
        for (idx = -1; idx <= 1; ++idx) {
Yaowu Xu's avatar
Yaowu Xu committed
180
181
          int row = (int)i + idy;
          int col = (int)j + idx;
182

183
184
          if (row >= 0 && row < (int)block_height && col >= 0 &&
              col < (int)block_width) {
185
            int diff = frame1[byte + idy * (int)stride + idx] -
186
                       frame2[idy * (int)block_width + idx];
187
188
189
190
191
192
193
194
195
            diff_sse[index] = diff * diff;
            ++index;
          }
        }
      }

      assert(index > 0);

      modifier = 0;
196
      for (idx = 0; idx < 9; ++idx) modifier += diff_sse[idx];
197

Jingning Han's avatar
Jingning Han committed
198
      modifier *= 3;
199
200
201
202
      modifier /= index;

      ++frame2;

Jingning Han's avatar
Jingning Han committed
203
204
205
      modifier += rounding;
      modifier >>= strength;

206
      if (modifier > 16) modifier = 16;
Jingning Han's avatar
Jingning Han committed
207
208
209
210
211
212
213
214
215
216
217
218
219

      modifier = 16 - modifier;
      modifier *= filter_weight;

      count[k] += modifier;
      accumulator[k] += modifier * pixel_value;

      byte++;
    }

    byte += stride - block_width;
  }
}
Yaowu Xu's avatar
Yaowu Xu committed
220
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
221

Yaowu Xu's avatar
Yaowu Xu committed
222
static int temporal_filter_find_matching_mb_c(AV1_COMP *cpi,
Jingning Han's avatar
Jingning Han committed
223
224
225
226
227
228
229
230
231
232
233
234
235
                                              uint8_t *arf_frame_buf,
                                              uint8_t *frame_ptr_buf,
                                              int stride) {
  MACROBLOCK *const x = &cpi->td.mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
  int step_param;
  int sadpb = x->sadperbit16;
  int bestsme = INT_MAX;
  int distortion;
  unsigned int sse;
  int cost_list[5];

236
  MV best_ref_mv1 = { 0, 0 };
Jingning Han's avatar
Jingning Han committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */

  // Save input state
  struct buf_2d src = x->plane[0].src;
  struct buf_2d pre = xd->plane[0].pre[0];

  best_ref_mv1_full.col = best_ref_mv1.col >> 3;
  best_ref_mv1_full.row = best_ref_mv1.row >> 3;

  // Setup frame pointers
  x->plane[0].src.buf = arf_frame_buf;
  x->plane[0].src.stride = stride;
  xd->plane[0].pre[0].buf = frame_ptr_buf;
  xd->plane[0].pre[0].stride = stride;

  step_param = mv_sf->reduce_first_step_size;
Yaowu Xu's avatar
Yaowu Xu committed
253
  step_param = AOMMIN(step_param, MAX_MVSEARCH_STEPS - 2);
Jingning Han's avatar
Jingning Han committed
254

Jingning Han's avatar
Jingning Han committed
255
256
257
258
259
260
261
#if CONFIG_REF_MV
  x->mvcost = x->mv_cost_stack[0];
  x->nmvjointcost = x->nmv_vec_cost[0];
  x->mvsadcost = x->mvcost;
  x->nmvjointsadcost = x->nmvjointcost;
#endif

Jingning Han's avatar
Jingning Han committed
262
  // Ignore mv costing by sending NULL pointer instead of cost arrays
Yaowu Xu's avatar
Yaowu Xu committed
263
264
265
  av1_hex_search(x, &best_ref_mv1_full, step_param, sadpb, 1,
                 cond_cost_list(cpi, cost_list), &cpi->fn_ptr[BLOCK_16X16], 0,
                 &best_ref_mv1);
Jingning Han's avatar
Jingning Han committed
266
267

  // Ignore mv costing by sending NULL pointer instead of cost array
268
269
270
271
272
  bestsme = cpi->find_fractional_mv_step(
      x, &best_ref_mv1, cpi->common.allow_high_precision_mv, x->errorperbit,
      &cpi->fn_ptr[BLOCK_16X16], 0, mv_sf->subpel_iters_per_step,
      cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0, 0,
      0);
Jingning Han's avatar
Jingning Han committed
273

hui su's avatar
hui su committed
274
275
  x->e_mbd.mi[0]->bmi[0].as_mv[0] = x->best_mv;

Jingning Han's avatar
Jingning Han committed
276
277
278
279
280
281
282
  // Restore input state
  x->plane[0].src = src;
  xd->plane[0].pre[0] = pre;

  return bestsme;
}

Yaowu Xu's avatar
Yaowu Xu committed
283
static void temporal_filter_iterate_c(AV1_COMP *cpi,
Jingning Han's avatar
Jingning Han committed
284
                                      YV12_BUFFER_CONFIG **frames,
285
                                      int frame_count, int alt_ref_index,
Jingning Han's avatar
Jingning Han committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
                                      int strength,
                                      struct scale_factors *scale) {
  int byte;
  int frame;
  int mb_col, mb_row;
  unsigned int filter_weight;
  int mb_cols = (frames[alt_ref_index]->y_crop_width + 15) >> 4;
  int mb_rows = (frames[alt_ref_index]->y_crop_height + 15) >> 4;
  int mb_y_offset = 0;
  int mb_uv_offset = 0;
  DECLARE_ALIGNED(16, unsigned int, accumulator[16 * 16 * 3]);
  DECLARE_ALIGNED(16, uint16_t, count[16 * 16 * 3]);
  MACROBLOCKD *mbd = &cpi->td.mb.e_mbd;
  YV12_BUFFER_CONFIG *f = frames[alt_ref_index];
  uint8_t *dst1, *dst2;
Yaowu Xu's avatar
Yaowu Xu committed
301
#if CONFIG_AOM_HIGHBITDEPTH
302
303
  DECLARE_ALIGNED(16, uint16_t, predictor16[16 * 16 * 3]);
  DECLARE_ALIGNED(16, uint8_t, predictor8[16 * 16 * 3]);
Jingning Han's avatar
Jingning Han committed
304
305
  uint8_t *predictor;
#else
306
  DECLARE_ALIGNED(16, uint8_t, predictor[16 * 16 * 3]);
Jingning Han's avatar
Jingning Han committed
307
308
#endif
  const int mb_uv_height = 16 >> mbd->plane[1].subsampling_y;
309
  const int mb_uv_width = 16 >> mbd->plane[1].subsampling_x;
Jingning Han's avatar
Jingning Han committed
310
311

  // Save input state
312
  uint8_t *input_buffer[MAX_MB_PLANE];
Jingning Han's avatar
Jingning Han committed
313
  int i;
Yaowu Xu's avatar
Yaowu Xu committed
314
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
315
316
317
318
319
320
321
  if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    predictor = CONVERT_TO_BYTEPTR(predictor16);
  } else {
    predictor = predictor8;
  }
#endif

322
  for (i = 0; i < MAX_MB_PLANE; i++) input_buffer[i] = mbd->plane[i].pre[0].buf;
Jingning Han's avatar
Jingning Han committed
323
324
325

  for (mb_row = 0; mb_row < mb_rows; mb_row++) {
    // Source frames are extended to 16 pixels. This is different than
Yaowu Xu's avatar
Yaowu Xu committed
326
    //  L/A/G reference frames that have a border of 32 (AV1ENCBORDERINPIXELS)
Jingning Han's avatar
Jingning Han committed
327
328
    // A 6/8 tap filter is used for motion search.  This requires 2 pixels
    //  before and 3 pixels after.  So the largest Y mv on a border would
Yaowu Xu's avatar
Yaowu Xu committed
329
    //  then be 16 - AOM_INTERP_EXTEND. The UV blocks are half the size of the
Jingning Han's avatar
Jingning Han committed
330
    //  Y and therefore only extended by 8.  The largest mv that a UV block
Yaowu Xu's avatar
Yaowu Xu committed
331
332
333
    //  can support is 8 - AOM_INTERP_EXTEND.  A UV mv is half of a Y mv.
    //  (16 - AOM_INTERP_EXTEND) >> 1 which is greater than
    //  8 - AOM_INTERP_EXTEND.
Jingning Han's avatar
Jingning Han committed
334
    // To keep the mv in play for both Y and UV planes the max that it
Yaowu Xu's avatar
Yaowu Xu committed
335
336
    //  can be on a border is therefore 16 - (2*AOM_INTERP_EXTEND+1).
    cpi->td.mb.mv_row_min = -((mb_row * 16) + (17 - 2 * AOM_INTERP_EXTEND));
337
    cpi->td.mb.mv_row_max =
Yaowu Xu's avatar
Yaowu Xu committed
338
        ((mb_rows - 1 - mb_row) * 16) + (17 - 2 * AOM_INTERP_EXTEND);
Jingning Han's avatar
Jingning Han committed
339
340

    for (mb_col = 0; mb_col < mb_cols; mb_col++) {
341
      int j, k;
Jingning Han's avatar
Jingning Han committed
342
343
344
345
346
      int stride;

      memset(accumulator, 0, 16 * 16 * 3 * sizeof(accumulator[0]));
      memset(count, 0, 16 * 16 * 3 * sizeof(count[0]));

Yaowu Xu's avatar
Yaowu Xu committed
347
      cpi->td.mb.mv_col_min = -((mb_col * 16) + (17 - 2 * AOM_INTERP_EXTEND));
348
      cpi->td.mb.mv_col_max =
Yaowu Xu's avatar
Yaowu Xu committed
349
          ((mb_cols - 1 - mb_col) * 16) + (17 - 2 * AOM_INTERP_EXTEND);
Jingning Han's avatar
Jingning Han committed
350
351

      for (frame = 0; frame < frame_count; frame++) {
352
        const int thresh_low = 10000;
Jingning Han's avatar
Jingning Han committed
353
354
        const int thresh_high = 20000;

355
        if (frames[frame] == NULL) continue;
Jingning Han's avatar
Jingning Han committed
356
357
358
359
360
361
362
363

        mbd->mi[0]->bmi[0].as_mv[0].as_mv.row = 0;
        mbd->mi[0]->bmi[0].as_mv[0].as_mv.col = 0;

        if (frame == alt_ref_index) {
          filter_weight = 2;
        } else {
          // Find best match in this frame by MC
364
365
366
          int err = temporal_filter_find_matching_mb_c(
              cpi, frames[alt_ref_index]->y_buffer + mb_y_offset,
              frames[frame]->y_buffer + mb_y_offset, frames[frame]->y_stride);
Jingning Han's avatar
Jingning Han committed
367
368
369
370

          // Assign higher weight to matching MB if it's error
          // score is lower. If not applying MC default behavior
          // is to weight all MBs equal.
371
          filter_weight = err < thresh_low ? 2 : err < thresh_high ? 1 : 0;
Jingning Han's avatar
Jingning Han committed
372
373
374
375
        }

        if (filter_weight != 0) {
          // Construct the predictors
376
377
          temporal_filter_predictors_mb_c(
              mbd, frames[frame]->y_buffer + mb_y_offset,
Jingning Han's avatar
Jingning Han committed
378
              frames[frame]->u_buffer + mb_uv_offset,
379
380
381
              frames[frame]->v_buffer + mb_uv_offset, frames[frame]->y_stride,
              mb_uv_width, mb_uv_height, mbd->mi[0]->bmi[0].as_mv[0].as_mv.row,
              mbd->mi[0]->bmi[0].as_mv[0].as_mv.col, predictor, scale,
Jingning Han's avatar
Jingning Han committed
382
383
              mb_col * 16, mb_row * 16);

Yaowu Xu's avatar
Yaowu Xu committed
384
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
385
386
387
          if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
            int adj_strength = strength + 2 * (mbd->bd - 8);
            // Apply the filter (YUV)
Yaowu Xu's avatar
Yaowu Xu committed
388
            av1_highbd_temporal_filter_apply(
389
390
                f->y_buffer + mb_y_offset, f->y_stride, predictor, 16, 16,
                adj_strength, filter_weight, accumulator, count);
Yaowu Xu's avatar
Yaowu Xu committed
391
            av1_highbd_temporal_filter_apply(
392
393
394
                f->u_buffer + mb_uv_offset, f->uv_stride, predictor + 256,
                mb_uv_width, mb_uv_height, adj_strength, filter_weight,
                accumulator + 256, count + 256);
Yaowu Xu's avatar
Yaowu Xu committed
395
            av1_highbd_temporal_filter_apply(
396
397
398
                f->v_buffer + mb_uv_offset, f->uv_stride, predictor + 512,
                mb_uv_width, mb_uv_height, adj_strength, filter_weight,
                accumulator + 512, count + 512);
Jingning Han's avatar
Jingning Han committed
399
400
          } else {
            // Apply the filter (YUV)
Yaowu Xu's avatar
Yaowu Xu committed
401
402
403
404
            av1_temporal_filter_apply_c(f->y_buffer + mb_y_offset, f->y_stride,
                                        predictor, 16, 16, strength,
                                        filter_weight, accumulator, count);
            av1_temporal_filter_apply_c(
405
406
407
                f->u_buffer + mb_uv_offset, f->uv_stride, predictor + 256,
                mb_uv_width, mb_uv_height, strength, filter_weight,
                accumulator + 256, count + 256);
Yaowu Xu's avatar
Yaowu Xu committed
408
            av1_temporal_filter_apply_c(
409
410
411
                f->v_buffer + mb_uv_offset, f->uv_stride, predictor + 512,
                mb_uv_width, mb_uv_height, strength, filter_weight,
                accumulator + 512, count + 512);
Jingning Han's avatar
Jingning Han committed
412
413
414
          }
#else
          // Apply the filter (YUV)
Yaowu Xu's avatar
Yaowu Xu committed
415
416
417
418
419
420
421
422
423
424
425
426
          av1_temporal_filter_apply_c(f->y_buffer + mb_y_offset, f->y_stride,
                                      predictor, 16, 16, strength,
                                      filter_weight, accumulator, count);
          av1_temporal_filter_apply_c(f->u_buffer + mb_uv_offset, f->uv_stride,
                                      predictor + 256, mb_uv_width,
                                      mb_uv_height, strength, filter_weight,
                                      accumulator + 256, count + 256);
          av1_temporal_filter_apply_c(f->v_buffer + mb_uv_offset, f->uv_stride,
                                      predictor + 512, mb_uv_width,
                                      mb_uv_height, strength, filter_weight,
                                      accumulator + 512, count + 512);
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
427
428
429
        }
      }

Yaowu Xu's avatar
Yaowu Xu committed
430
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
431
432
433
434
435
436
437
438
439
440
      if (mbd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        uint16_t *dst1_16;
        uint16_t *dst2_16;
        // Normalize filter output to produce AltRef frame
        dst1 = cpi->alt_ref_buffer.y_buffer;
        dst1_16 = CONVERT_TO_SHORTPTR(dst1);
        stride = cpi->alt_ref_buffer.y_stride;
        byte = mb_y_offset;
        for (i = 0, k = 0; i < 16; i++) {
          for (j = 0; j < 16; j++, k++) {
Yaowu Xu's avatar
Yaowu Xu committed
441
442
            dst1_16[byte] =
                (uint16_t)OD_DIVU(accumulator[k] + (count[k] >> 1), count[k]);
Jingning Han's avatar
Jingning Han committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461

            // move to next pixel
            byte++;
          }

          byte += stride - 16;
        }

        dst1 = cpi->alt_ref_buffer.u_buffer;
        dst2 = cpi->alt_ref_buffer.v_buffer;
        dst1_16 = CONVERT_TO_SHORTPTR(dst1);
        dst2_16 = CONVERT_TO_SHORTPTR(dst2);
        stride = cpi->alt_ref_buffer.uv_stride;
        byte = mb_uv_offset;
        for (i = 0, k = 256; i < mb_uv_height; i++) {
          for (j = 0; j < mb_uv_width; j++, k++) {
            int m = k + 256;

            // U
Yaowu Xu's avatar
Yaowu Xu committed
462
463
            dst1_16[byte] =
                (uint16_t)OD_DIVU(accumulator[k] + (count[k] >> 1), count[k]);
Jingning Han's avatar
Jingning Han committed
464
465

            // V
Yaowu Xu's avatar
Yaowu Xu committed
466
467
            dst2_16[byte] =
                (uint16_t)OD_DIVU(accumulator[m] + (count[m] >> 1), count[m]);
Jingning Han's avatar
Jingning Han committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481

            // move to next pixel
            byte++;
          }

          byte += stride - mb_uv_width;
        }
      } else {
        // Normalize filter output to produce AltRef frame
        dst1 = cpi->alt_ref_buffer.y_buffer;
        stride = cpi->alt_ref_buffer.y_stride;
        byte = mb_y_offset;
        for (i = 0, k = 0; i < 16; i++) {
          for (j = 0; j < 16; j++, k++) {
Yaowu Xu's avatar
Yaowu Xu committed
482
483
            dst1[byte] =
                (uint8_t)OD_DIVU(accumulator[k] + (count[k] >> 1), count[k]);
Jingning Han's avatar
Jingning Han committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

            // move to next pixel
            byte++;
          }
          byte += stride - 16;
        }

        dst1 = cpi->alt_ref_buffer.u_buffer;
        dst2 = cpi->alt_ref_buffer.v_buffer;
        stride = cpi->alt_ref_buffer.uv_stride;
        byte = mb_uv_offset;
        for (i = 0, k = 256; i < mb_uv_height; i++) {
          for (j = 0; j < mb_uv_width; j++, k++) {
            int m = k + 256;

            // U
Yaowu Xu's avatar
Yaowu Xu committed
500
501
            dst1[byte] =
                (uint8_t)OD_DIVU(accumulator[k] + (count[k] >> 1), count[k]);
Jingning Han's avatar
Jingning Han committed
502
503

            // V
Yaowu Xu's avatar
Yaowu Xu committed
504
505
            dst2[byte] =
                (uint8_t)OD_DIVU(accumulator[m] + (count[m] >> 1), count[m]);
Jingning Han's avatar
Jingning Han committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519

            // move to next pixel
            byte++;
          }
          byte += stride - mb_uv_width;
        }
      }
#else
      // Normalize filter output to produce AltRef frame
      dst1 = cpi->alt_ref_buffer.y_buffer;
      stride = cpi->alt_ref_buffer.y_stride;
      byte = mb_y_offset;
      for (i = 0, k = 0; i < 16; i++) {
        for (j = 0; j < 16; j++, k++) {
Yaowu Xu's avatar
Yaowu Xu committed
520
521
          dst1[byte] =
              (uint8_t)OD_DIVU(accumulator[k] + (count[k] >> 1), count[k]);
Jingning Han's avatar
Jingning Han committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537

          // move to next pixel
          byte++;
        }
        byte += stride - 16;
      }

      dst1 = cpi->alt_ref_buffer.u_buffer;
      dst2 = cpi->alt_ref_buffer.v_buffer;
      stride = cpi->alt_ref_buffer.uv_stride;
      byte = mb_uv_offset;
      for (i = 0, k = 256; i < mb_uv_height; i++) {
        for (j = 0; j < mb_uv_width; j++, k++) {
          int m = k + 256;

          // U
Yaowu Xu's avatar
Yaowu Xu committed
538
539
          dst1[byte] =
              (uint8_t)OD_DIVU(accumulator[k] + (count[k] >> 1), count[k]);
Jingning Han's avatar
Jingning Han committed
540
541

          // V
Yaowu Xu's avatar
Yaowu Xu committed
542
543
          dst2[byte] =
              (uint8_t)OD_DIVU(accumulator[m] + (count[m] >> 1), count[m]);
Jingning Han's avatar
Jingning Han committed
544
545
546
547
548
549

          // move to next pixel
          byte++;
        }
        byte += stride - mb_uv_width;
      }
Yaowu Xu's avatar
Yaowu Xu committed
550
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
551
552
553
554
555
556
557
558
      mb_y_offset += 16;
      mb_uv_offset += mb_uv_width;
    }
    mb_y_offset += 16 * (f->y_stride - mb_cols);
    mb_uv_offset += mb_uv_height * f->uv_stride - mb_uv_width * mb_cols;
  }

  // Restore input state
559
  for (i = 0; i < MAX_MB_PLANE; i++) mbd->plane[i].pre[0].buf = input_buffer[i];
Jingning Han's avatar
Jingning Han committed
560
561
562
}

// Apply buffer limits and context specific adjustments to arnr filter.
Yaowu Xu's avatar
Yaowu Xu committed
563
static void adjust_arnr_filter(AV1_COMP *cpi, int distance, int group_boost,
Jingning Han's avatar
Jingning Han committed
564
                               int *arnr_frames, int *arnr_strength) {
Yaowu Xu's avatar
Yaowu Xu committed
565
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Jingning Han's avatar
Jingning Han committed
566
  const int frames_after_arf =
Yaowu Xu's avatar
Yaowu Xu committed
567
      av1_lookahead_depth(cpi->lookahead) - distance - 1;
Jingning Han's avatar
Jingning Han committed
568
569
570
571
572
  int frames_fwd = (cpi->oxcf.arnr_max_frames - 1) >> 1;
  int frames_bwd;
  int q, frames, strength;

  // Define the forward and backwards filter limits for this arnr group.
573
574
  if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
  if (frames_fwd > distance) frames_fwd = distance;
Jingning Han's avatar
Jingning Han committed
575
576
577
578
579

  frames_bwd = frames_fwd;

  // For even length filter there is one more frame backward
  // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
580
  if (frames_bwd < distance) frames_bwd += (oxcf->arnr_max_frames + 1) & 0x1;
Jingning Han's avatar
Jingning Han committed
581
582
583
584
585
586

  // Set the baseline active filter size.
  frames = frames_bwd + 1 + frames_fwd;

  // Adjust the strength based on active max q.
  if (cpi->common.current_video_frame > 1)
Yaowu Xu's avatar
Yaowu Xu committed
587
588
    q = ((int)av1_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME],
                                      cpi->common.bit_depth));
Jingning Han's avatar
Jingning Han committed
589
  else
Yaowu Xu's avatar
Yaowu Xu committed
590
591
    q = ((int)av1_convert_qindex_to_q(cpi->rc.avg_frame_qindex[KEY_FRAME],
                                      cpi->common.bit_depth));
Jingning Han's avatar
Jingning Han committed
592
593
594
595
  if (q > 16) {
    strength = oxcf->arnr_strength;
  } else {
    strength = oxcf->arnr_strength - ((16 - q) / 2);
596
    if (strength < 0) strength = 0;
Jingning Han's avatar
Jingning Han committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  }

  // Adjust number of frames in filter and strength based on gf boost level.
  if (frames > group_boost / 150) {
    frames = group_boost / 150;
    frames += !(frames & 1);
  }

  if (strength > group_boost / 300) {
    strength = group_boost / 300;
  }

  // Adjustments for second level arf in multi arf case.
  if (cpi->oxcf.pass == 2 && cpi->multi_arf_allowed) {
    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
    if (gf_group->rf_level[gf_group->index] != GF_ARF_STD) {
      strength >>= 1;
    }
  }

  *arnr_frames = frames;
  *arnr_strength = strength;
}

Yaowu Xu's avatar
Yaowu Xu committed
621
void av1_temporal_filter(AV1_COMP *cpi, int distance) {
Jingning Han's avatar
Jingning Han committed
622
623
624
625
626
627
628
629
  RATE_CONTROL *const rc = &cpi->rc;
  int frame;
  int frames_to_blur;
  int start_frame;
  int strength;
  int frames_to_blur_backward;
  int frames_to_blur_forward;
  struct scale_factors sf;
630
  YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
631
632
633
#if CONFIG_EXT_REFS
  const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
#endif
Jingning Han's avatar
Jingning Han committed
634
635
636

  // Apply context specific adjustments to the arnr filter parameters.
  adjust_arnr_filter(cpi, distance, rc->gfu_boost, &frames_to_blur, &strength);
Yaowu Xu's avatar
Yaowu Xu committed
637
638
639
640
// TODO(weitinglin): Currently, we enforce the filtering strength on
//                   extra ARFs' to be zeros. We should investigate in which
//                   case it is more beneficial to use non-zero strength
//                   filtering.
641
#if CONFIG_EXT_REFS
642
643
644
645
646
647
648
649
650
651
652
653
  if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW) {
    strength = 0;
    frames_to_blur = 1;
  }
#endif

#if CONFIG_EXT_REFS
  if (strength == 0 && frames_to_blur == 1) {
    cpi->is_arf_filter_off[gf_group->arf_update_idx[gf_group->index]] = 1;
  } else {
    cpi->is_arf_filter_off[gf_group->arf_update_idx[gf_group->index]] = 0;
  }
654
#endif
655

Jingning Han's avatar
Jingning Han committed
656
657
658
659
660
661
662
  frames_to_blur_backward = (frames_to_blur / 2);
  frames_to_blur_forward = ((frames_to_blur - 1) / 2);
  start_frame = distance + frames_to_blur_forward;

  // Setup frame pointers, NULL indicates frame not included in filter.
  for (frame = 0; frame < frames_to_blur; ++frame) {
    const int which_buffer = start_frame - frame;
663
    struct lookahead_entry *buf =
Yaowu Xu's avatar
Yaowu Xu committed
664
        av1_lookahead_peek(cpi->lookahead, which_buffer);
Jingning Han's avatar
Jingning Han committed
665
666
667
668
    frames[frames_to_blur - 1 - frame] = &buf->img;
  }

  if (frames_to_blur > 0) {
669
670
671
// Setup scaling factors. Scaling on each of the arnr frames is not
// supported.
// ARF is produced at the native frame size and resized when coded.
Yaowu Xu's avatar
Yaowu Xu committed
672
673
#if CONFIG_AOM_HIGHBITDEPTH
    av1_setup_scale_factors_for_frame(
674
675
676
        &sf, frames[0]->y_crop_width, frames[0]->y_crop_height,
        frames[0]->y_crop_width, frames[0]->y_crop_height,
        cpi->common.use_highbitdepth);
Jingning Han's avatar
Jingning Han committed
677
#else
Yaowu Xu's avatar
Yaowu Xu committed
678
    av1_setup_scale_factors_for_frame(
679
680
        &sf, frames[0]->y_crop_width, frames[0]->y_crop_height,
        frames[0]->y_crop_width, frames[0]->y_crop_height);
Yaowu Xu's avatar
Yaowu Xu committed
681
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
682
683
684
685
686
  }

  temporal_filter_iterate_c(cpi, frames, frames_to_blur,
                            frames_to_blur_backward, strength, &sf);
}