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_onyx_int.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
30#define ALT_REF_MC_ENABLED 1    // dis/enable MC in AltRef filtering
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_size,
38                                            int mv_row,
39                                            int mv_col,
40                                            uint8_t *pred,
41                                            struct scale_factors *scale,
42                                            int x, int y) {
43  const int which_mv = 0;
44  const MV mv = { mv_row, mv_col };
45  const InterpKernel *const kernel =
46    vp9_get_interp_kernel(xd->mi[0]->mbmi.interp_filter);
47
48  enum mv_precision mv_precision_uv;
49  int uv_stride;
50  if (uv_block_size == 8) {
51    uv_stride = (stride + 1) >> 1;
52    mv_precision_uv = MV_PRECISION_Q4;
53  } else {
54    uv_stride = stride;
55    mv_precision_uv = MV_PRECISION_Q3;
56  }
57
58  vp9_build_inter_predictor(y_mb_ptr, stride,
59                            &pred[0], 16,
60                            &mv,
61                            scale,
62                            16, 16,
63                            which_mv,
64                            kernel, MV_PRECISION_Q3, x, y);
65
66  vp9_build_inter_predictor(u_mb_ptr, uv_stride,
67                            &pred[256], uv_block_size,
68                            &mv,
69                            scale,
70                            uv_block_size, uv_block_size,
71                            which_mv,
72                            kernel, mv_precision_uv, x, y);
73
74  vp9_build_inter_predictor(v_mb_ptr, uv_stride,
75                            &pred[512], uv_block_size,
76                            &mv,
77                            scale,
78                            uv_block_size, uv_block_size,
79                            which_mv,
80                            kernel, mv_precision_uv, x, y);
81}
82
83void vp9_temporal_filter_apply_c(uint8_t *frame1,
84                                 unsigned int stride,
85                                 uint8_t *frame2,
86                                 unsigned int block_size,
87                                 int strength,
88                                 int filter_weight,
89                                 unsigned int *accumulator,
90                                 uint16_t *count) {
91  unsigned int i, j, k;
92  int modifier;
93  int byte = 0;
94
95  for (i = 0, k = 0; i < block_size; i++) {
96    for (j = 0; j < block_size; j++, k++) {
97      int src_byte = frame1[byte];
98      int pixel_value = *frame2++;
99
100      modifier   = src_byte - pixel_value;
101      // This is an integer approximation of:
102      // float coeff = (3.0 * modifer * modifier) / pow(2, strength);
103      // modifier =  (int)roundf(coeff > 16 ? 0 : 16-coeff);
104      modifier  *= modifier;
105      modifier  *= 3;
106      modifier  += 1 << (strength - 1);
107      modifier >>= strength;
108
109      if (modifier > 16)
110        modifier = 16;
111
112      modifier = 16 - modifier;
113      modifier *= filter_weight;
114
115      count[k] += modifier;
116      accumulator[k] += modifier * pixel_value;
117
118      byte++;
119    }
120
121    byte += stride - block_size;
122  }
123}
124
125#if ALT_REF_MC_ENABLED
126
127static int temporal_filter_find_matching_mb_c(VP9_COMP *cpi,
128                                              uint8_t *arf_frame_buf,
129                                              uint8_t *frame_ptr_buf,
130                                              int stride) {
131  MACROBLOCK *x = &cpi->mb;
132  MACROBLOCKD* const xd = &x->e_mbd;
133  int step_param;
134  int sadpb = x->sadperbit16;
135  int bestsme = INT_MAX;
136
137  MV best_ref_mv1 = {0, 0};
138  MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
139  MV *ref_mv = &x->e_mbd.mi[0]->bmi[0].as_mv[0].as_mv;
140
141  // Save input state
142  struct buf_2d src = x->plane[0].src;
143  struct buf_2d pre = xd->plane[0].pre[0];
144
145  best_ref_mv1_full.col = best_ref_mv1.col >> 3;
146  best_ref_mv1_full.row = best_ref_mv1.row >> 3;
147
148  // Setup frame pointers
149  x->plane[0].src.buf = arf_frame_buf;
150  x->plane[0].src.stride = stride;
151  xd->plane[0].pre[0].buf = frame_ptr_buf;
152  xd->plane[0].pre[0].stride = stride;
153
154  // Further step/diamond searches as necessary
155  if (cpi->speed < 8)
156    step_param = cpi->sf.reduce_first_step_size + ((cpi->speed > 5) ? 1 : 0);
157  else
158    step_param = cpi->sf.reduce_first_step_size + 2;
159  step_param = MIN(step_param, (cpi->sf.max_step_search_steps - 2));
160
161  /*cpi->sf.search_method == HEX*/
162  // Ignore mv costing by sending NULL pointer instead of cost arrays
163  vp9_hex_search(x, &best_ref_mv1_full, step_param, sadpb, 1,
164                 &cpi->fn_ptr[BLOCK_16X16], 0, &best_ref_mv1, ref_mv);
165
166  // Try sub-pixel MC?
167  // if (bestsme > error_thresh && bestsme < INT_MAX)
168  {
169    int distortion;
170    unsigned int sse;
171    // Ignore mv costing by sending NULL pointer instead of cost array
172    bestsme = cpi->find_fractional_mv_step(x, ref_mv,
173                                           &best_ref_mv1,
174                                           cpi->common.allow_high_precision_mv,
175                                           x->errorperbit,
176                                           &cpi->fn_ptr[BLOCK_16X16],
177                                           0, cpi->sf.subpel_iters_per_step,
178                                           NULL, NULL,
179                                           &distortion, &sse);
180  }
181
182  // Restore input state
183  x->plane[0].src = src;
184  xd->plane[0].pre[0] = pre;
185
186  return bestsme;
187}
188#endif
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
211  // Save input state
212  uint8_t* input_buffer[MAX_MB_PLANE];
213  int i;
214
215  // TODO(aconverse): Add 4:2:2 support
216  assert(mbd->plane[1].subsampling_x == mbd->plane[1].subsampling_y);
217
218  for (i = 0; i < MAX_MB_PLANE; i++)
219    input_buffer[i] = mbd->plane[i].pre[0].buf;
220
221  for (mb_row = 0; mb_row < mb_rows; mb_row++) {
222#if ALT_REF_MC_ENABLED
223    // Source frames are extended to 16 pixels.  This is different than
224    //  L/A/G reference frames that have a border of 32 (VP9ENCBORDERINPIXELS)
225    // A 6/8 tap filter is used for motion search.  This requires 2 pixels
226    //  before and 3 pixels after.  So the largest Y mv on a border would
227    //  then be 16 - VP9_INTERP_EXTEND. The UV blocks are half the size of the
228    //  Y and therefore only extended by 8.  The largest mv that a UV block
229    //  can support is 8 - VP9_INTERP_EXTEND.  A UV mv is half of a Y mv.
230    //  (16 - VP9_INTERP_EXTEND) >> 1 which is greater than
231    //  8 - VP9_INTERP_EXTEND.
232    // To keep the mv in play for both Y and UV planes the max that it
233    //  can be on a border is therefore 16 - (2*VP9_INTERP_EXTEND+1).
234    cpi->mb.mv_row_min = -((mb_row * 16) + (17 - 2 * VP9_INTERP_EXTEND));
235    cpi->mb.mv_row_max = ((cpi->common.mb_rows - 1 - mb_row) * 16)
236                         + (17 - 2 * VP9_INTERP_EXTEND);
237#endif
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#if ALT_REF_MC_ENABLED
247      cpi->mb.mv_col_min = -((mb_col * 16) + (17 - 2 * VP9_INTERP_EXTEND));
248      cpi->mb.mv_col_max = ((cpi->common.mb_cols - 1 - mb_col) * 16)
249                           + (17 - 2 * VP9_INTERP_EXTEND);
250#endif
251
252      for (frame = 0; frame < frame_count; frame++) {
253        if (cpi->frames[frame] == NULL)
254          continue;
255
256        mbd->mi[0]->bmi[0].as_mv[0].as_mv.row = 0;
257        mbd->mi[0]->bmi[0].as_mv[0].as_mv.col = 0;
258
259        if (frame == alt_ref_index) {
260          filter_weight = 2;
261        } else {
262          int err = 0;
263#if ALT_REF_MC_ENABLED
264#define THRESH_LOW   10000
265#define THRESH_HIGH  20000
266
267          // Find best match in this frame by MC
268          err = temporal_filter_find_matching_mb_c
269                (cpi,
270                 cpi->frames[alt_ref_index]->y_buffer + mb_y_offset,
271                 cpi->frames[frame]->y_buffer + mb_y_offset,
272                 cpi->frames[frame]->y_stride);
273#endif
274          // Assign higher weight to matching MB if it's error
275          // score is lower. If not applying MC default behavior
276          // is to weight all MBs equal.
277          filter_weight = err < THRESH_LOW
278                          ? 2 : err < THRESH_HIGH ? 1 : 0;
279        }
280
281        if (filter_weight != 0) {
282          // Construct the predictors
283          temporal_filter_predictors_mb_c
284          (mbd,
285           cpi->frames[frame]->y_buffer + mb_y_offset,
286           cpi->frames[frame]->u_buffer + mb_uv_offset,
287           cpi->frames[frame]->v_buffer + mb_uv_offset,
288           cpi->frames[frame]->y_stride,
289           mb_uv_height,
290           mbd->mi[0]->bmi[0].as_mv[0].as_mv.row,
291           mbd->mi[0]->bmi[0].as_mv[0].as_mv.col,
292           predictor, scale,
293           mb_col * 16, mb_row * 16);
294
295          // Apply the filter (YUV)
296          vp9_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
297                                    predictor, 16, strength, filter_weight,
298                                    accumulator, count);
299
300          vp9_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
301                                    predictor + 256, mb_uv_height, strength,
302                                    filter_weight, accumulator + 256,
303                                    count + 256);
304
305          vp9_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
306                                    predictor + 512, mb_uv_height, strength,
307                                    filter_weight, accumulator + 512,
308                                    count + 512);
309        }
310      }
311
312      // Normalize filter output to produce AltRef frame
313      dst1 = cpi->alt_ref_buffer.y_buffer;
314      stride = cpi->alt_ref_buffer.y_stride;
315      byte = mb_y_offset;
316      for (i = 0, k = 0; i < 16; i++) {
317        for (j = 0; j < 16; j++, k++) {
318          unsigned int pval = accumulator[k] + (count[k] >> 1);
319          pval *= cpi->fixed_divide[count[k]];
320          pval >>= 19;
321
322          dst1[byte] = (uint8_t)pval;
323
324          // move to next pixel
325          byte++;
326        }
327
328        byte += stride - 16;
329      }
330
331      dst1 = cpi->alt_ref_buffer.u_buffer;
332      dst2 = cpi->alt_ref_buffer.v_buffer;
333      stride = cpi->alt_ref_buffer.uv_stride;
334      byte = mb_uv_offset;
335      for (i = 0, k = 256; i < mb_uv_height; i++) {
336        for (j = 0; j < mb_uv_height; j++, k++) {
337          int m = k + 256;
338
339          // U
340          unsigned int pval = accumulator[k] + (count[k] >> 1);
341          pval *= cpi->fixed_divide[count[k]];
342          pval >>= 19;
343          dst1[byte] = (uint8_t)pval;
344
345          // V
346          pval = accumulator[m] + (count[m] >> 1);
347          pval *= cpi->fixed_divide[count[m]];
348          pval >>= 19;
349          dst2[byte] = (uint8_t)pval;
350
351          // move to next pixel
352          byte++;
353        }
354
355        byte += stride - mb_uv_height;
356      }
357
358      mb_y_offset += 16;
359      mb_uv_offset += mb_uv_height;
360    }
361
362    mb_y_offset += 16 * (f->y_stride - mb_cols);
363    mb_uv_offset += mb_uv_height * (f->uv_stride - mb_cols);
364  }
365
366  // Restore input state
367  for (i = 0; i < MAX_MB_PLANE; i++)
368    mbd->plane[i].pre[0].buf = input_buffer[i];
369}
370
371void vp9_temporal_filter_prepare(VP9_COMP *cpi, int distance) {
372  VP9_COMMON *const cm = &cpi->common;
373
374  int frame = 0;
375
376  int frames_to_blur_backward = 0;
377  int frames_to_blur_forward = 0;
378  int frames_to_blur = 0;
379  int start_frame = 0;
380
381  int strength = cpi->active_arnr_strength;
382  int blur_type = cpi->oxcf.arnr_type;
383  int max_frames = cpi->active_arnr_frames;
384
385  const int num_frames_backward = distance;
386  const int num_frames_forward = vp9_lookahead_depth(cpi->lookahead)
387                               - (num_frames_backward + 1);
388  struct scale_factors sf;
389
390  switch (blur_type) {
391    case 1:
392      // Backward Blur
393      frames_to_blur_backward = num_frames_backward;
394
395      if (frames_to_blur_backward >= max_frames)
396        frames_to_blur_backward = max_frames - 1;
397
398      frames_to_blur = frames_to_blur_backward + 1;
399      break;
400
401    case 2:
402      // Forward Blur
403      frames_to_blur_forward = num_frames_forward;
404
405      if (frames_to_blur_forward >= max_frames)
406        frames_to_blur_forward = max_frames - 1;
407
408      frames_to_blur = frames_to_blur_forward + 1;
409      break;
410
411    case 3:
412    default:
413      // Center Blur
414      frames_to_blur_forward = num_frames_forward;
415      frames_to_blur_backward = num_frames_backward;
416
417      if (frames_to_blur_forward > frames_to_blur_backward)
418        frames_to_blur_forward = frames_to_blur_backward;
419
420      if (frames_to_blur_backward > frames_to_blur_forward)
421        frames_to_blur_backward = frames_to_blur_forward;
422
423      // When max_frames is even we have 1 more frame backward than forward
424      if (frames_to_blur_forward > (max_frames - 1) / 2)
425        frames_to_blur_forward = ((max_frames - 1) / 2);
426
427      if (frames_to_blur_backward > (max_frames / 2))
428        frames_to_blur_backward = (max_frames / 2);
429
430      frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1;
431      break;
432  }
433
434  start_frame = distance + frames_to_blur_forward;
435
436#ifdef DEBUGFWG
437  // DEBUG FWG
438  printf(
439      "max:%d FBCK:%d FFWD:%d ftb:%d ftbbck:%d ftbfwd:%d sei:%d lasei:%d "
440      "start:%d",
441      max_frames, num_frames_backward, num_frames_forward, frames_to_blur,
442      frames_to_blur_backward, frames_to_blur_forward, cpi->source_encode_index,
443      cpi->last_alt_ref_sei, start_frame);
444#endif
445
446  // Setup scaling factors. Scaling on each of the arnr frames is not supported
447  vp9_setup_scale_factors_for_frame(&sf,
448      get_frame_new_buffer(cm)->y_crop_width,
449      get_frame_new_buffer(cm)->y_crop_height,
450      cm->width, cm->height);
451
452  // Setup frame pointers, NULL indicates frame not included in filter
453  vp9_zero(cpi->frames);
454  for (frame = 0; frame < frames_to_blur; frame++) {
455    int which_buffer = start_frame - frame;
456    struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead,
457                                                     which_buffer);
458    cpi->frames[frames_to_blur - 1 - frame] = &buf->img;
459  }
460
461  temporal_filter_iterate_c(cpi, frames_to_blur, frames_to_blur_backward,
462                            strength, &sf);
463}
464
465void vp9_configure_arnr_filter(VP9_COMP *cpi,
466                               const unsigned int frames_to_arnr,
467                               const int group_boost) {
468  int half_gf_int;
469  int frames_after_arf;
470  int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
471  int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
472  int q;
473
474  // Define the arnr filter width for this group of frames. We only
475  // filter frames that lie within a distance of half the GF interval
476  // from the ARF frame. We also have to trap cases where the filter
477  // extends beyond the end of the lookahead buffer.
478  // Note: frames_to_arnr parameter is the offset of the arnr
479  // frame from the current frame.
480  half_gf_int = cpi->rc.baseline_gf_interval >> 1;
481  frames_after_arf = vp9_lookahead_depth(cpi->lookahead)
482      - frames_to_arnr - 1;
483
484  switch (cpi->oxcf.arnr_type) {
485    case 1:  // Backward filter
486      frames_fwd = 0;
487      if (frames_bwd > half_gf_int)
488        frames_bwd = half_gf_int;
489      break;
490
491    case 2:  // Forward filter
492      if (frames_fwd > half_gf_int)
493        frames_fwd = half_gf_int;
494      if (frames_fwd > frames_after_arf)
495        frames_fwd = frames_after_arf;
496      frames_bwd = 0;
497      break;
498
499    case 3:  // Centered filter
500    default:
501      frames_fwd >>= 1;
502      if (frames_fwd > frames_after_arf)
503        frames_fwd = frames_after_arf;
504      if (frames_fwd > half_gf_int)
505        frames_fwd = half_gf_int;
506
507      frames_bwd = frames_fwd;
508
509      // For even length filter there is one more frame backward
510      // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
511      if (frames_bwd < half_gf_int)
512        frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
513      break;
514  }
515
516  cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
517
518  // Adjust the strength based on active max q
519  if (cpi->common.current_video_frame > 1)
520    q = ((int)vp9_convert_qindex_to_q(
521        cpi->rc.avg_frame_qindex[INTER_FRAME]));
522  else
523    q = ((int)vp9_convert_qindex_to_q(
524        cpi->rc.avg_frame_qindex[KEY_FRAME]));
525  if (q > 16) {
526    cpi->active_arnr_strength = cpi->oxcf.arnr_strength;
527  } else {
528    cpi->active_arnr_strength = cpi->oxcf.arnr_strength - ((16 - q) / 2);
529    if (cpi->active_arnr_strength < 0)
530      cpi->active_arnr_strength = 0;
531  }
532
533  // Adjust number of frames in filter and strength based on gf boost level.
534  if (cpi->active_arnr_frames > (group_boost / 150)) {
535    cpi->active_arnr_frames = (group_boost / 150);
536    cpi->active_arnr_frames += !(cpi->active_arnr_frames & 1);
537  }
538  if (cpi->active_arnr_strength > (group_boost / 300)) {
539    cpi->active_arnr_strength = (group_boost / 300);
540  }
541}
542