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]->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
149  MV best_ref_mv1 = {0, 0};
150  MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
151  MV *ref_mv = &x->e_mbd.mi[0]->bmi[0].as_mv[0].as_mv;
152
153  // Save input state
154  struct buf_2d src = x->plane[0].src;
155  struct buf_2d pre = xd->plane[0].pre[0];
156
157  best_ref_mv1_full.col = best_ref_mv1.col >> 3;
158  best_ref_mv1_full.row = best_ref_mv1.row >> 3;
159
160  // Setup frame pointers
161  x->plane[0].src.buf = arf_frame_buf;
162  x->plane[0].src.stride = stride;
163  xd->plane[0].pre[0].buf = frame_ptr_buf;
164  xd->plane[0].pre[0].stride = stride;
165
166  step_param = mv_sf->reduce_first_step_size;
167  step_param = MIN(step_param, MAX_MVSEARCH_STEPS - 2);
168
169  // Ignore mv costing by sending NULL pointer instead of cost arrays
170  vp9_hex_search(x, &best_ref_mv1_full, step_param, sadpb, 1,
171                 &cpi->fn_ptr[BLOCK_16X16], 0, &best_ref_mv1, ref_mv);
172
173  // Ignore mv costing by sending NULL pointer instead of cost array
174  bestsme = cpi->find_fractional_mv_step(x, ref_mv,
175                                         &best_ref_mv1,
176                                         cpi->common.allow_high_precision_mv,
177                                         x->errorperbit,
178                                         &cpi->fn_ptr[BLOCK_16X16],
179                                         0, mv_sf->subpel_iters_per_step,
180                                         NULL, NULL,
181                                         &distortion, &sse, NULL, 0, 0);
182
183  // Restore input state
184  x->plane[0].src = src;
185  xd->plane[0].pre[0] = pre;
186
187  return bestsme;
188}
189
190static void temporal_filter_iterate_c(VP9_COMP *cpi,
191                                      int frame_count,
192                                      int alt_ref_index,
193                                      int strength,
194                                      struct scale_factors *scale) {
195  int byte;
196  int frame;
197  int mb_col, mb_row;
198  unsigned int filter_weight;
199  int mb_cols = cpi->common.mb_cols;
200  int mb_rows = cpi->common.mb_rows;
201  int mb_y_offset = 0;
202  int mb_uv_offset = 0;
203  DECLARE_ALIGNED_ARRAY(16, unsigned int, accumulator, 16 * 16 * 3);
204  DECLARE_ALIGNED_ARRAY(16, uint16_t, count, 16 * 16 * 3);
205  MACROBLOCKD *mbd = &cpi->mb.e_mbd;
206  YV12_BUFFER_CONFIG *f = cpi->frames[alt_ref_index];
207  uint8_t *dst1, *dst2;
208  DECLARE_ALIGNED_ARRAY(16, uint8_t,  predictor, 16 * 16 * 3);
209  const int mb_uv_height = 16 >> mbd->plane[1].subsampling_y;
210  const int mb_uv_width  = 16 >> mbd->plane[1].subsampling_x;
211
212  // Save input state
213  uint8_t* input_buffer[MAX_MB_PLANE];
214  int i;
215
216  for (i = 0; i < MAX_MB_PLANE; i++)
217    input_buffer[i] = mbd->plane[i].pre[0].buf;
218
219  for (mb_row = 0; mb_row < mb_rows; mb_row++) {
220    // Source frames are extended to 16 pixels. This is different than
221    //  L/A/G reference frames that have a border of 32 (VP9ENCBORDERINPIXELS)
222    // A 6/8 tap filter is used for motion search.  This requires 2 pixels
223    //  before and 3 pixels after.  So the largest Y mv on a border would
224    //  then be 16 - VP9_INTERP_EXTEND. The UV blocks are half the size of the
225    //  Y and therefore only extended by 8.  The largest mv that a UV block
226    //  can support is 8 - VP9_INTERP_EXTEND.  A UV mv is half of a Y mv.
227    //  (16 - VP9_INTERP_EXTEND) >> 1 which is greater than
228    //  8 - VP9_INTERP_EXTEND.
229    // To keep the mv in play for both Y and UV planes the max that it
230    //  can be on a border is therefore 16 - (2*VP9_INTERP_EXTEND+1).
231    cpi->mb.mv_row_min = -((mb_row * 16) + (17 - 2 * VP9_INTERP_EXTEND));
232    cpi->mb.mv_row_max = ((cpi->common.mb_rows - 1 - mb_row) * 16)
233                         + (17 - 2 * VP9_INTERP_EXTEND);
234
235    for (mb_col = 0; mb_col < mb_cols; mb_col++) {
236      int i, j, k;
237      int stride;
238
239      vpx_memset(accumulator, 0, 16 * 16 * 3 * sizeof(accumulator[0]));
240      vpx_memset(count, 0, 16 * 16 * 3 * sizeof(count[0]));
241
242      cpi->mb.mv_col_min = -((mb_col * 16) + (17 - 2 * VP9_INTERP_EXTEND));
243      cpi->mb.mv_col_max = ((cpi->common.mb_cols - 1 - mb_col) * 16)
244                           + (17 - 2 * VP9_INTERP_EXTEND);
245
246      for (frame = 0; frame < frame_count; frame++) {
247        const int thresh_low  = 10000;
248        const int thresh_high = 20000;
249
250        if (cpi->frames[frame] == NULL)
251          continue;
252
253        mbd->mi[0]->bmi[0].as_mv[0].as_mv.row = 0;
254        mbd->mi[0]->bmi[0].as_mv[0].as_mv.col = 0;
255
256        if (frame == alt_ref_index) {
257          filter_weight = 2;
258        } else {
259          // Find best match in this frame by MC
260          int err = temporal_filter_find_matching_mb_c(cpi,
261              cpi->frames[alt_ref_index]->y_buffer + mb_y_offset,
262              cpi->frames[frame]->y_buffer + mb_y_offset,
263              cpi->frames[frame]->y_stride);
264
265          // Assign higher weight to matching MB if it's error
266          // score is lower. If not applying MC default behavior
267          // is to weight all MBs equal.
268          filter_weight = err < thresh_low
269                          ? 2 : err < thresh_high ? 1 : 0;
270        }
271
272        if (filter_weight != 0) {
273          // Construct the predictors
274          temporal_filter_predictors_mb_c(mbd,
275              cpi->frames[frame]->y_buffer + mb_y_offset,
276              cpi->frames[frame]->u_buffer + mb_uv_offset,
277              cpi->frames[frame]->v_buffer + mb_uv_offset,
278              cpi->frames[frame]->y_stride,
279              mb_uv_width, mb_uv_height,
280              mbd->mi[0]->bmi[0].as_mv[0].as_mv.row,
281              mbd->mi[0]->bmi[0].as_mv[0].as_mv.col,
282              predictor, scale,
283              mb_col * 16, mb_row * 16);
284
285          // Apply the filter (YUV)
286          vp9_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
287                                    predictor, 16, 16,
288                                    strength, filter_weight,
289                                    accumulator, count);
290          vp9_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
291                                    predictor + 256,
292                                    mb_uv_width, mb_uv_height, strength,
293                                    filter_weight, accumulator + 256,
294                                    count + 256);
295          vp9_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
296                                    predictor + 512,
297                                    mb_uv_width, mb_uv_height, strength,
298                                    filter_weight, accumulator + 512,
299                                    count + 512);
300        }
301      }
302
303      // Normalize filter output to produce AltRef frame
304      dst1 = cpi->alt_ref_buffer.y_buffer;
305      stride = cpi->alt_ref_buffer.y_stride;
306      byte = mb_y_offset;
307      for (i = 0, k = 0; i < 16; i++) {
308        for (j = 0; j < 16; j++, k++) {
309          unsigned int pval = accumulator[k] + (count[k] >> 1);
310          pval *= fixed_divide[count[k]];
311          pval >>= 19;
312
313          dst1[byte] = (uint8_t)pval;
314
315          // move to next pixel
316          byte++;
317        }
318        byte += stride - 16;
319      }
320
321      dst1 = cpi->alt_ref_buffer.u_buffer;
322      dst2 = cpi->alt_ref_buffer.v_buffer;
323      stride = cpi->alt_ref_buffer.uv_stride;
324      byte = mb_uv_offset;
325      for (i = 0, k = 256; i < mb_uv_height; i++) {
326        for (j = 0; j < mb_uv_width; j++, k++) {
327          int m = k + 256;
328
329          // U
330          unsigned int pval = accumulator[k] + (count[k] >> 1);
331          pval *= fixed_divide[count[k]];
332          pval >>= 19;
333          dst1[byte] = (uint8_t)pval;
334
335          // V
336          pval = accumulator[m] + (count[m] >> 1);
337          pval *= fixed_divide[count[m]];
338          pval >>= 19;
339          dst2[byte] = (uint8_t)pval;
340
341          // move to next pixel
342          byte++;
343        }
344        byte += stride - mb_uv_width;
345      }
346      mb_y_offset += 16;
347      mb_uv_offset += mb_uv_width;
348    }
349    mb_y_offset += 16 * (f->y_stride - mb_cols);
350    mb_uv_offset += mb_uv_height * f->uv_stride - mb_uv_width * mb_cols;
351  }
352
353  // Restore input state
354  for (i = 0; i < MAX_MB_PLANE; i++)
355    mbd->plane[i].pre[0].buf = input_buffer[i];
356}
357
358// Apply buffer limits and context specific adjustments to arnr filter.
359static void adjust_arnr_filter(VP9_COMP *cpi,
360                               int distance, int group_boost) {
361  const int frames_after_arf =
362            vp9_lookahead_depth(cpi->lookahead) - distance - 1;
363  int frames_fwd = (cpi->oxcf.arnr_max_frames - 1) >> 1;
364  int frames_bwd;
365  int q;
366
367  // Define the forward and backwards filter limits for this arnr group.
368  if (frames_fwd > frames_after_arf)
369    frames_fwd = frames_after_arf;
370  if (frames_fwd > distance)
371    frames_fwd = distance;
372
373  frames_bwd = frames_fwd;
374
375  // For even length filter there is one more frame backward
376  // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
377  if (frames_bwd < distance)
378    frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
379
380  // Set the baseline active filter size.
381  cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
382
383  // Adjust the strength based on active max q.
384  if (cpi->common.current_video_frame > 1)
385    q = ((int)vp9_convert_qindex_to_q(
386        cpi->rc.avg_frame_qindex[INTER_FRAME]));
387  else
388    q = ((int)vp9_convert_qindex_to_q(
389        cpi->rc.avg_frame_qindex[KEY_FRAME]));
390  if (q > 16) {
391    cpi->active_arnr_strength = cpi->oxcf.arnr_strength;
392  } else {
393    cpi->active_arnr_strength = cpi->oxcf.arnr_strength - ((16 - q) / 2);
394    if (cpi->active_arnr_strength < 0)
395      cpi->active_arnr_strength = 0;
396  }
397
398  // Adjust number of frames in filter and strength based on gf boost level.
399  if (cpi->active_arnr_frames > (group_boost / 150)) {
400    cpi->active_arnr_frames = (group_boost / 150);
401    cpi->active_arnr_frames += !(cpi->active_arnr_frames & 1);
402  }
403  if (cpi->active_arnr_strength > (group_boost / 300)) {
404    cpi->active_arnr_strength = (group_boost / 300);
405  }
406
407  // Adjustments for second level arf in multi arf case.
408  if (cpi->oxcf.pass == 2 && cpi->multi_arf_allowed) {
409    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
410    if (gf_group->rf_level[gf_group->index] != GF_ARF_STD) {
411      cpi->active_arnr_strength >>= 1;
412    }
413  }
414}
415
416void vp9_temporal_filter(VP9_COMP *cpi, int distance) {
417  VP9_COMMON *const cm = &cpi->common;
418  RATE_CONTROL *const rc = &cpi->rc;
419  int frame;
420  int frames_to_blur;
421  int start_frame;
422  int strength;
423  int frames_to_blur_backward;
424  int frames_to_blur_forward;
425  struct scale_factors sf;
426
427  // Apply context specific adjustments to the arnr filter parameters.
428  adjust_arnr_filter(cpi, distance, rc->gfu_boost);
429  strength = cpi->active_arnr_strength;
430  frames_to_blur = cpi->active_arnr_frames;
431  frames_to_blur_backward = (frames_to_blur / 2);
432  frames_to_blur_forward = ((frames_to_blur - 1) / 2);
433  start_frame = distance + frames_to_blur_forward;
434
435  // Setup frame pointers, NULL indicates frame not included in filter.
436  vp9_zero(cpi->frames);
437  for (frame = 0; frame < frames_to_blur; ++frame) {
438    const int which_buffer = start_frame - frame;
439    struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead,
440                                                     which_buffer);
441    cpi->frames[frames_to_blur - 1 - frame] = &buf->img;
442  }
443
444  // Setup scaling factors. Scaling on each of the arnr frames is not supported
445  if (is_spatial_svc(cpi)) {
446    // In spatial svc the scaling factors might be less then 1/2. So we will use
447    // non-normative scaling.
448    int frame_used = 0;
449    vp9_setup_scale_factors_for_frame(&sf,
450                                      get_frame_new_buffer(cm)->y_crop_width,
451                                      get_frame_new_buffer(cm)->y_crop_height,
452                                      get_frame_new_buffer(cm)->y_crop_width,
453                                      get_frame_new_buffer(cm)->y_crop_height);
454
455    for (frame = 0; frame < frames_to_blur; ++frame) {
456      if (cm->mi_cols * MI_SIZE != cpi->frames[frame]->y_width ||
457          cm->mi_rows * MI_SIZE != cpi->frames[frame]->y_height) {
458        if (vp9_realloc_frame_buffer(&cpi->svc.scaled_frames[frame_used],
459                                     cm->width, cm->height,
460                                     cm->subsampling_x, cm->subsampling_y,
461                                     VP9_ENC_BORDER_IN_PIXELS, NULL, NULL,
462                                     NULL))
463          vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
464                             "Failed to reallocate alt_ref_buffer");
465
466        cpi->frames[frame] =
467            vp9_scale_if_required(cm, cpi->frames[frame],
468                                  &cpi->svc.scaled_frames[frame_used]);
469        ++frame_used;
470      }
471    }
472  } else {
473    vp9_setup_scale_factors_for_frame(&sf,
474                                      get_frame_new_buffer(cm)->y_crop_width,
475                                      get_frame_new_buffer(cm)->y_crop_height,
476                                      cm->width, cm->height);
477  }
478
479  temporal_filter_iterate_c(cpi, frames_to_blur, frames_to_blur_backward,
480                            strength, &sf);
481}
482