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 "vp9/common/vp9_alloccommon.h"
15#include "vp9/common/vp9_onyxc_int.h"
16#include "vp9/common/vp9_quant_common.h"
17#include "vp9/common/vp9_reconinter.h"
18#include "vp9/common/vp9_systemdependent.h"
19#include "vp9/encoder/vp9_extend.h"
20#include "vp9/encoder/vp9_firstpass.h"
21#include "vp9/encoder/vp9_mcomp.h"
22#include "vp9/encoder/vp9_encoder.h"
23#include "vp9/encoder/vp9_quantize.h"
24#include "vp9/encoder/vp9_ratectrl.h"
25#include "vp9/encoder/vp9_segmentation.h"
26#include "vpx_mem/vpx_mem.h"
27#include "vpx_ports/vpx_timer.h"
28#include "vpx_scale/vpx_scale.h"
29
30static int fixed_divide[512];
31
32static void temporal_filter_predictors_mb_c(MACROBLOCKD *xd,
33                                            uint8_t *y_mb_ptr,
34                                            uint8_t *u_mb_ptr,
35                                            uint8_t *v_mb_ptr,
36                                            int stride,
37                                            int uv_block_width,
38                                            int uv_block_height,
39                                            int mv_row,
40                                            int mv_col,
41                                            uint8_t *pred,
42                                            struct scale_factors *scale,
43                                            int x, int y) {
44  const int which_mv = 0;
45  const MV mv = { mv_row, mv_col };
46  const InterpKernel *const kernel =
47    vp9_get_interp_kernel(xd->mi[0].src_mi->mbmi.interp_filter);
48
49  enum mv_precision mv_precision_uv;
50  int uv_stride;
51  if (uv_block_width == 8) {
52    uv_stride = (stride + 1) >> 1;
53    mv_precision_uv = MV_PRECISION_Q4;
54  } else {
55    uv_stride = stride;
56    mv_precision_uv = MV_PRECISION_Q3;
57  }
58
59  vp9_build_inter_predictor(y_mb_ptr, stride,
60                            &pred[0], 16,
61                            &mv,
62                            scale,
63                            16, 16,
64                            which_mv,
65                            kernel, MV_PRECISION_Q3, x, y);
66
67  vp9_build_inter_predictor(u_mb_ptr, uv_stride,
68                            &pred[256], uv_block_width,
69                            &mv,
70                            scale,
71                            uv_block_width, uv_block_height,
72                            which_mv,
73                            kernel, mv_precision_uv, x, y);
74
75  vp9_build_inter_predictor(v_mb_ptr, uv_stride,
76                            &pred[512], uv_block_width,
77                            &mv,
78                            scale,
79                            uv_block_width, uv_block_height,
80                            which_mv,
81                            kernel, mv_precision_uv, x, y);
82}
83
84void vp9_temporal_filter_init() {
85  int i;
86
87  fixed_divide[0] = 0;
88  for (i = 1; i < 512; ++i)
89    fixed_divide[i] = 0x80000 / i;
90}
91
92void vp9_temporal_filter_apply_c(uint8_t *frame1,
93                                 unsigned int stride,
94                                 uint8_t *frame2,
95                                 unsigned int block_width,
96                                 unsigned int block_height,
97                                 int strength,
98                                 int filter_weight,
99                                 unsigned int *accumulator,
100                                 uint16_t *count) {
101  unsigned int i, j, k;
102  int modifier;
103  int byte = 0;
104  const int rounding = strength > 0 ? 1 << (strength - 1) : 0;
105
106  for (i = 0, k = 0; i < block_height; i++) {
107    for (j = 0; j < block_width; j++, k++) {
108      int src_byte = frame1[byte];
109      int pixel_value = *frame2++;
110
111      modifier   = src_byte - pixel_value;
112      // This is an integer approximation of:
113      // float coeff = (3.0 * modifer * modifier) / pow(2, strength);
114      // modifier =  (int)roundf(coeff > 16 ? 0 : 16-coeff);
115      modifier  *= modifier;
116      modifier  *= 3;
117      modifier  += rounding;
118      modifier >>= strength;
119
120      if (modifier > 16)
121        modifier = 16;
122
123      modifier = 16 - modifier;
124      modifier *= filter_weight;
125
126      count[k] += modifier;
127      accumulator[k] += modifier * pixel_value;
128
129      byte++;
130    }
131
132    byte += stride - block_width;
133  }
134}
135
136static int temporal_filter_find_matching_mb_c(VP9_COMP *cpi,
137                                              uint8_t *arf_frame_buf,
138                                              uint8_t *frame_ptr_buf,
139                                              int stride) {
140  MACROBLOCK *const x = &cpi->mb;
141  MACROBLOCKD *const xd = &x->e_mbd;
142  const MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
143  int step_param;
144  int sadpb = x->sadperbit16;
145  int bestsme = INT_MAX;
146  int distortion;
147  unsigned int sse;
148  int sad_list[5];
149
150  MV best_ref_mv1 = {0, 0};
151  MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
152  MV *ref_mv = &x->e_mbd.mi[0].src_mi->bmi[0].as_mv[0].as_mv;
153
154  // Save input state
155  struct buf_2d src = x->plane[0].src;
156  struct buf_2d pre = xd->plane[0].pre[0];
157
158  best_ref_mv1_full.col = best_ref_mv1.col >> 3;
159  best_ref_mv1_full.row = best_ref_mv1.row >> 3;
160
161  // Setup frame pointers
162  x->plane[0].src.buf = arf_frame_buf;
163  x->plane[0].src.stride = stride;
164  xd->plane[0].pre[0].buf = frame_ptr_buf;
165  xd->plane[0].pre[0].stride = stride;
166
167  step_param = mv_sf->reduce_first_step_size;
168  step_param = MIN(step_param, MAX_MVSEARCH_STEPS - 2);
169
170  // Ignore mv costing by sending NULL pointer instead of cost arrays
171  vp9_hex_search(x, &best_ref_mv1_full, step_param, sadpb, 1,
172                 cond_sad_list(cpi, sad_list),
173                 &cpi->fn_ptr[BLOCK_16X16], 0, &best_ref_mv1, ref_mv);
174
175  // Ignore mv costing by sending NULL pointer instead of cost array
176  bestsme = cpi->find_fractional_mv_step(x, ref_mv,
177                                         &best_ref_mv1,
178                                         cpi->common.allow_high_precision_mv,
179                                         x->errorperbit,
180                                         &cpi->fn_ptr[BLOCK_16X16],
181                                         0, mv_sf->subpel_iters_per_step,
182                                         cond_sad_list(cpi, sad_list),
183                                         NULL, NULL,
184                                         &distortion, &sse, NULL, 0, 0);
185
186  // Restore input state
187  x->plane[0].src = src;
188  xd->plane[0].pre[0] = pre;
189
190  return bestsme;
191}
192
193static void temporal_filter_iterate_c(VP9_COMP *cpi,
194                                      YV12_BUFFER_CONFIG **frames,
195                                      int frame_count,
196                                      int alt_ref_index,
197                                      int strength,
198                                      struct scale_factors *scale) {
199  int byte;
200  int frame;
201  int mb_col, mb_row;
202  unsigned int filter_weight;
203  int mb_cols = (frames[alt_ref_index]->y_crop_width + 15) >> 4;
204  int mb_rows = (frames[alt_ref_index]->y_crop_height + 15) >> 4;
205  int mb_y_offset = 0;
206  int mb_uv_offset = 0;
207  DECLARE_ALIGNED_ARRAY(16, unsigned int, accumulator, 16 * 16 * 3);
208  DECLARE_ALIGNED_ARRAY(16, uint16_t, count, 16 * 16 * 3);
209  MACROBLOCKD *mbd = &cpi->mb.e_mbd;
210  YV12_BUFFER_CONFIG *f = frames[alt_ref_index];
211  uint8_t *dst1, *dst2;
212  DECLARE_ALIGNED_ARRAY(16, uint8_t,  predictor, 16 * 16 * 3);
213  const int mb_uv_height = 16 >> mbd->plane[1].subsampling_y;
214  const int mb_uv_width  = 16 >> mbd->plane[1].subsampling_x;
215
216  // Save input state
217  uint8_t* input_buffer[MAX_MB_PLANE];
218  int i;
219
220  for (i = 0; i < MAX_MB_PLANE; i++)
221    input_buffer[i] = mbd->plane[i].pre[0].buf;
222
223  for (mb_row = 0; mb_row < mb_rows; mb_row++) {
224    // Source frames are extended to 16 pixels. This is different than
225    //  L/A/G reference frames that have a border of 32 (VP9ENCBORDERINPIXELS)
226    // A 6/8 tap filter is used for motion search.  This requires 2 pixels
227    //  before and 3 pixels after.  So the largest Y mv on a border would
228    //  then be 16 - VP9_INTERP_EXTEND. The UV blocks are half the size of the
229    //  Y and therefore only extended by 8.  The largest mv that a UV block
230    //  can support is 8 - VP9_INTERP_EXTEND.  A UV mv is half of a Y mv.
231    //  (16 - VP9_INTERP_EXTEND) >> 1 which is greater than
232    //  8 - VP9_INTERP_EXTEND.
233    // To keep the mv in play for both Y and UV planes the max that it
234    //  can be on a border is therefore 16 - (2*VP9_INTERP_EXTEND+1).
235    cpi->mb.mv_row_min = -((mb_row * 16) + (17 - 2 * VP9_INTERP_EXTEND));
236    cpi->mb.mv_row_max = ((mb_rows - 1 - mb_row) * 16)
237                         + (17 - 2 * VP9_INTERP_EXTEND);
238
239    for (mb_col = 0; mb_col < mb_cols; mb_col++) {
240      int i, j, k;
241      int stride;
242
243      vpx_memset(accumulator, 0, 16 * 16 * 3 * sizeof(accumulator[0]));
244      vpx_memset(count, 0, 16 * 16 * 3 * sizeof(count[0]));
245
246      cpi->mb.mv_col_min = -((mb_col * 16) + (17 - 2 * VP9_INTERP_EXTEND));
247      cpi->mb.mv_col_max = ((mb_cols - 1 - mb_col) * 16)
248                           + (17 - 2 * VP9_INTERP_EXTEND);
249
250      for (frame = 0; frame < frame_count; frame++) {
251        const int thresh_low  = 10000;
252        const int thresh_high = 20000;
253
254        if (frames[frame] == NULL)
255          continue;
256
257        mbd->mi[0].src_mi->bmi[0].as_mv[0].as_mv.row = 0;
258        mbd->mi[0].src_mi->bmi[0].as_mv[0].as_mv.col = 0;
259
260        if (frame == alt_ref_index) {
261          filter_weight = 2;
262        } else {
263          // Find best match in this frame by MC
264          int err = temporal_filter_find_matching_mb_c(cpi,
265              frames[alt_ref_index]->y_buffer + mb_y_offset,
266              frames[frame]->y_buffer + mb_y_offset,
267              frames[frame]->y_stride);
268
269          // Assign higher weight to matching MB if it's error
270          // score is lower. If not applying MC default behavior
271          // is to weight all MBs equal.
272          filter_weight = err < thresh_low
273                          ? 2 : err < thresh_high ? 1 : 0;
274        }
275
276        if (filter_weight != 0) {
277          // Construct the predictors
278          temporal_filter_predictors_mb_c(mbd,
279              frames[frame]->y_buffer + mb_y_offset,
280              frames[frame]->u_buffer + mb_uv_offset,
281              frames[frame]->v_buffer + mb_uv_offset,
282              frames[frame]->y_stride,
283              mb_uv_width, mb_uv_height,
284              mbd->mi[0].src_mi->bmi[0].as_mv[0].as_mv.row,
285              mbd->mi[0].src_mi->bmi[0].as_mv[0].as_mv.col,
286              predictor, scale,
287              mb_col * 16, mb_row * 16);
288
289          // Apply the filter (YUV)
290          vp9_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
291                                    predictor, 16, 16,
292                                    strength, filter_weight,
293                                    accumulator, count);
294          vp9_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
295                                    predictor + 256,
296                                    mb_uv_width, mb_uv_height, strength,
297                                    filter_weight, accumulator + 256,
298                                    count + 256);
299          vp9_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
300                                    predictor + 512,
301                                    mb_uv_width, mb_uv_height, strength,
302                                    filter_weight, accumulator + 512,
303                                    count + 512);
304        }
305      }
306
307      // Normalize filter output to produce AltRef frame
308      dst1 = cpi->alt_ref_buffer.y_buffer;
309      stride = cpi->alt_ref_buffer.y_stride;
310      byte = mb_y_offset;
311      for (i = 0, k = 0; i < 16; i++) {
312        for (j = 0; j < 16; j++, k++) {
313          unsigned int pval = accumulator[k] + (count[k] >> 1);
314          pval *= fixed_divide[count[k]];
315          pval >>= 19;
316
317          dst1[byte] = (uint8_t)pval;
318
319          // move to next pixel
320          byte++;
321        }
322        byte += stride - 16;
323      }
324
325      dst1 = cpi->alt_ref_buffer.u_buffer;
326      dst2 = cpi->alt_ref_buffer.v_buffer;
327      stride = cpi->alt_ref_buffer.uv_stride;
328      byte = mb_uv_offset;
329      for (i = 0, k = 256; i < mb_uv_height; i++) {
330        for (j = 0; j < mb_uv_width; j++, k++) {
331          int m = k + 256;
332
333          // U
334          unsigned int pval = accumulator[k] + (count[k] >> 1);
335          pval *= fixed_divide[count[k]];
336          pval >>= 19;
337          dst1[byte] = (uint8_t)pval;
338
339          // V
340          pval = accumulator[m] + (count[m] >> 1);
341          pval *= fixed_divide[count[m]];
342          pval >>= 19;
343          dst2[byte] = (uint8_t)pval;
344
345          // move to next pixel
346          byte++;
347        }
348        byte += stride - mb_uv_width;
349      }
350      mb_y_offset += 16;
351      mb_uv_offset += mb_uv_width;
352    }
353    mb_y_offset += 16 * (f->y_stride - mb_cols);
354    mb_uv_offset += mb_uv_height * f->uv_stride - mb_uv_width * mb_cols;
355  }
356
357  // Restore input state
358  for (i = 0; i < MAX_MB_PLANE; i++)
359    mbd->plane[i].pre[0].buf = input_buffer[i];
360}
361
362// Apply buffer limits and context specific adjustments to arnr filter.
363static void adjust_arnr_filter(VP9_COMP *cpi,
364                               int distance, int group_boost,
365                               int *arnr_frames, int *arnr_strength) {
366  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
367  const int frames_after_arf =
368      vp9_lookahead_depth(cpi->lookahead) - distance - 1;
369  int frames_fwd = (cpi->oxcf.arnr_max_frames - 1) >> 1;
370  int frames_bwd;
371  int q, frames, strength;
372
373  // Define the forward and backwards filter limits for this arnr group.
374  if (frames_fwd > frames_after_arf)
375    frames_fwd = frames_after_arf;
376  if (frames_fwd > distance)
377    frames_fwd = distance;
378
379  frames_bwd = frames_fwd;
380
381  // For even length filter there is one more frame backward
382  // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
383  if (frames_bwd < distance)
384    frames_bwd += (oxcf->arnr_max_frames + 1) & 0x1;
385
386  // Set the baseline active filter size.
387  frames = frames_bwd + 1 + frames_fwd;
388
389  // Adjust the strength based on active max q.
390  if (cpi->common.current_video_frame > 1)
391    q = ((int)vp9_convert_qindex_to_q(
392        cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth));
393  else
394    q = ((int)vp9_convert_qindex_to_q(
395        cpi->rc.avg_frame_qindex[KEY_FRAME], cpi->common.bit_depth));
396  if (q > 16) {
397    strength = oxcf->arnr_strength;
398  } else {
399    strength = oxcf->arnr_strength - ((16 - q) / 2);
400    if (strength < 0)
401      strength = 0;
402  }
403
404  // Adjust number of frames in filter and strength based on gf boost level.
405  if (frames > group_boost / 150) {
406    frames = group_boost / 150;
407    frames += !(frames & 1);
408  }
409
410  if (strength > group_boost / 300) {
411    strength = group_boost / 300;
412  }
413
414  // Adjustments for second level arf in multi arf case.
415  if (cpi->oxcf.pass == 2 && cpi->multi_arf_allowed) {
416    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
417    if (gf_group->rf_level[gf_group->index] != GF_ARF_STD) {
418      strength >>= 1;
419    }
420  }
421
422  *arnr_frames = frames;
423  *arnr_strength = strength;
424}
425
426void vp9_temporal_filter(VP9_COMP *cpi, int distance) {
427  VP9_COMMON *const cm = &cpi->common;
428  RATE_CONTROL *const rc = &cpi->rc;
429  int frame;
430  int frames_to_blur;
431  int start_frame;
432  int strength;
433  int frames_to_blur_backward;
434  int frames_to_blur_forward;
435  struct scale_factors sf;
436  YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = {NULL};
437
438  // Apply context specific adjustments to the arnr filter parameters.
439  adjust_arnr_filter(cpi, distance, rc->gfu_boost, &frames_to_blur, &strength);
440  frames_to_blur_backward = (frames_to_blur / 2);
441  frames_to_blur_forward = ((frames_to_blur - 1) / 2);
442  start_frame = distance + frames_to_blur_forward;
443
444  // Setup frame pointers, NULL indicates frame not included in filter.
445  for (frame = 0; frame < frames_to_blur; ++frame) {
446    const int which_buffer = start_frame - frame;
447    struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead,
448                                                     which_buffer);
449    frames[frames_to_blur - 1 - frame] = &buf->img;
450  }
451
452  // Setup scaling factors. Scaling on each of the arnr frames is not supported
453  if (is_two_pass_svc(cpi)) {
454    // In spatial svc the scaling factors might be less then 1/2. So we will use
455    // non-normative scaling.
456    int frame_used = 0;
457#if CONFIG_VP9_HIGHBITDEPTH
458    vp9_setup_scale_factors_for_frame(&sf,
459                                      get_frame_new_buffer(cm)->y_crop_width,
460                                      get_frame_new_buffer(cm)->y_crop_height,
461                                      get_frame_new_buffer(cm)->y_crop_width,
462                                      get_frame_new_buffer(cm)->y_crop_height,
463                                      cm->use_highbitdepth);
464#else
465    vp9_setup_scale_factors_for_frame(&sf,
466                                      get_frame_new_buffer(cm)->y_crop_width,
467                                      get_frame_new_buffer(cm)->y_crop_height,
468                                      get_frame_new_buffer(cm)->y_crop_width,
469                                      get_frame_new_buffer(cm)->y_crop_height);
470#endif
471    for (frame = 0; frame < frames_to_blur; ++frame) {
472      if (cm->mi_cols * MI_SIZE != frames[frame]->y_width ||
473          cm->mi_rows * MI_SIZE != frames[frame]->y_height) {
474        if (vp9_realloc_frame_buffer(&cpi->svc.scaled_frames[frame_used],
475                                     cm->width, cm->height,
476                                     cm->subsampling_x, cm->subsampling_y,
477#if CONFIG_VP9_HIGHBITDEPTH
478                                     cm->use_highbitdepth,
479#endif
480                                     VP9_ENC_BORDER_IN_PIXELS, NULL, NULL,
481                                     NULL))
482          vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
483                             "Failed to reallocate alt_ref_buffer");
484
485        frames[frame] = vp9_scale_if_required(cm, frames[frame],
486                            &cpi->svc.scaled_frames[frame_used]);
487        ++frame_used;
488      }
489    }
490  } else {
491    // ARF is produced at the native frame size and resized when coded.
492#if CONFIG_VP9_HIGHBITDEPTH
493    vp9_setup_scale_factors_for_frame(&sf,
494                                      frames[0]->y_crop_width,
495                                      frames[0]->y_crop_height,
496                                      frames[0]->y_crop_width,
497                                      frames[0]->y_crop_height,
498                                      cm->use_highbitdepth);
499#else
500    vp9_setup_scale_factors_for_frame(&sf,
501                                      frames[0]->y_crop_width,
502                                      frames[0]->y_crop_height,
503                                      frames[0]->y_crop_width,
504                                      frames[0]->y_crop_height);
505#endif
506  }
507
508  temporal_filter_iterate_c(cpi, frames, frames_to_blur,
509                            frames_to_blur_backward, strength, &sf);
510}
511