LCOV - code coverage report
Current view: top level - media/libvpx/libvpx/vp8/encoder - temporal_filter.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 192 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "vp8/common/onyxc_int.h"
      12             : #include "onyx_int.h"
      13             : #include "vp8/common/systemdependent.h"
      14             : #include "vp8/encoder/quantize.h"
      15             : #include "vp8/common/alloccommon.h"
      16             : #include "mcomp.h"
      17             : #include "firstpass.h"
      18             : #include "vpx_scale/vpx_scale.h"
      19             : #include "vp8/common/extend.h"
      20             : #include "ratectrl.h"
      21             : #include "vp8/common/quant_common.h"
      22             : #include "segmentation.h"
      23             : #include "vpx_mem/vpx_mem.h"
      24             : #include "vp8/common/swapyv12buffer.h"
      25             : #include "vp8/common/threading.h"
      26             : #include "vpx_ports/vpx_timer.h"
      27             : 
      28             : #include <math.h>
      29             : #include <limits.h>
      30             : 
      31             : #define ALT_REF_MC_ENABLED 1     /* toggle MC in AltRef filtering */
      32             : #define ALT_REF_SUBPEL_ENABLED 1 /* toggle subpel in MC AltRef filtering */
      33             : 
      34             : #if VP8_TEMPORAL_ALT_REF
      35             : 
      36           0 : static void vp8_temporal_filter_predictors_mb_c(
      37             :     MACROBLOCKD *x, unsigned char *y_mb_ptr, unsigned char *u_mb_ptr,
      38             :     unsigned char *v_mb_ptr, int stride, int mv_row, int mv_col,
      39             :     unsigned char *pred) {
      40             :   int offset;
      41             :   unsigned char *yptr, *uptr, *vptr;
      42             : 
      43             :   /* Y */
      44           0 :   yptr = y_mb_ptr + (mv_row >> 3) * stride + (mv_col >> 3);
      45             : 
      46           0 :   if ((mv_row | mv_col) & 7) {
      47           0 :     x->subpixel_predict16x16(yptr, stride, mv_col & 7, mv_row & 7, &pred[0],
      48             :                              16);
      49             :   } else {
      50           0 :     vp8_copy_mem16x16(yptr, stride, &pred[0], 16);
      51             :   }
      52             : 
      53             :   /* U & V */
      54           0 :   mv_row >>= 1;
      55           0 :   mv_col >>= 1;
      56           0 :   stride = (stride + 1) >> 1;
      57           0 :   offset = (mv_row >> 3) * stride + (mv_col >> 3);
      58           0 :   uptr = u_mb_ptr + offset;
      59           0 :   vptr = v_mb_ptr + offset;
      60             : 
      61           0 :   if ((mv_row | mv_col) & 7) {
      62           0 :     x->subpixel_predict8x8(uptr, stride, mv_col & 7, mv_row & 7, &pred[256], 8);
      63           0 :     x->subpixel_predict8x8(vptr, stride, mv_col & 7, mv_row & 7, &pred[320], 8);
      64             :   } else {
      65           0 :     vp8_copy_mem8x8(uptr, stride, &pred[256], 8);
      66           0 :     vp8_copy_mem8x8(vptr, stride, &pred[320], 8);
      67             :   }
      68           0 : }
      69           0 : void vp8_temporal_filter_apply_c(unsigned char *frame1, unsigned int stride,
      70             :                                  unsigned char *frame2, unsigned int block_size,
      71             :                                  int strength, int filter_weight,
      72             :                                  unsigned int *accumulator,
      73             :                                  unsigned short *count) {
      74             :   unsigned int i, j, k;
      75             :   int modifier;
      76           0 :   int byte = 0;
      77           0 :   const int rounding = strength > 0 ? 1 << (strength - 1) : 0;
      78             : 
      79           0 :   for (i = 0, k = 0; i < block_size; ++i) {
      80           0 :     for (j = 0; j < block_size; j++, k++) {
      81           0 :       int src_byte = frame1[byte];
      82           0 :       int pixel_value = *frame2++;
      83             : 
      84           0 :       modifier = src_byte - pixel_value;
      85             :       /* This is an integer approximation of:
      86             :        * float coeff = (3.0 * modifer * modifier) / pow(2, strength);
      87             :        * modifier =  (int)roundf(coeff > 16 ? 0 : 16-coeff);
      88             :        */
      89           0 :       modifier *= modifier;
      90           0 :       modifier *= 3;
      91           0 :       modifier += rounding;
      92           0 :       modifier >>= strength;
      93             : 
      94           0 :       if (modifier > 16) modifier = 16;
      95             : 
      96           0 :       modifier = 16 - modifier;
      97           0 :       modifier *= filter_weight;
      98             : 
      99           0 :       count[k] += modifier;
     100           0 :       accumulator[k] += modifier * pixel_value;
     101             : 
     102           0 :       byte++;
     103             :     }
     104             : 
     105           0 :     byte += stride - block_size;
     106             :   }
     107           0 : }
     108             : 
     109             : #if ALT_REF_MC_ENABLED
     110             : 
     111           0 : static int vp8_temporal_filter_find_matching_mb_c(VP8_COMP *cpi,
     112             :                                                   YV12_BUFFER_CONFIG *arf_frame,
     113             :                                                   YV12_BUFFER_CONFIG *frame_ptr,
     114             :                                                   int mb_offset,
     115             :                                                   int error_thresh) {
     116           0 :   MACROBLOCK *x = &cpi->mb;
     117             :   int step_param;
     118           0 :   int sadpb = x->sadperbit16;
     119           0 :   int bestsme = INT_MAX;
     120             : 
     121           0 :   BLOCK *b = &x->block[0];
     122           0 :   BLOCKD *d = &x->e_mbd.block[0];
     123             :   int_mv best_ref_mv1;
     124             :   int_mv best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
     125             : 
     126             :   /* Save input state */
     127           0 :   unsigned char **base_src = b->base_src;
     128           0 :   int src = b->src;
     129           0 :   int src_stride = b->src_stride;
     130           0 :   unsigned char *base_pre = x->e_mbd.pre.y_buffer;
     131           0 :   int pre = d->offset;
     132           0 :   int pre_stride = x->e_mbd.pre.y_stride;
     133             : 
     134             :   (void)error_thresh;
     135             : 
     136           0 :   best_ref_mv1.as_int = 0;
     137           0 :   best_ref_mv1_full.as_mv.col = best_ref_mv1.as_mv.col >> 3;
     138           0 :   best_ref_mv1_full.as_mv.row = best_ref_mv1.as_mv.row >> 3;
     139             : 
     140             :   /* Setup frame pointers */
     141           0 :   b->base_src = &arf_frame->y_buffer;
     142           0 :   b->src_stride = arf_frame->y_stride;
     143           0 :   b->src = mb_offset;
     144             : 
     145           0 :   x->e_mbd.pre.y_buffer = frame_ptr->y_buffer;
     146           0 :   x->e_mbd.pre.y_stride = frame_ptr->y_stride;
     147           0 :   d->offset = mb_offset;
     148             : 
     149             :   /* Further step/diamond searches as necessary */
     150           0 :   if (cpi->Speed < 8) {
     151           0 :     step_param = cpi->sf.first_step + (cpi->Speed > 5);
     152             :   } else {
     153           0 :     step_param = cpi->sf.first_step + 2;
     154             :   }
     155             : 
     156             :   /* TODO Check that the 16x16 vf & sdf are selected here */
     157             :   /* Ignore mv costing by sending NULL cost arrays */
     158           0 :   bestsme =
     159           0 :       vp8_hex_search(x, b, d, &best_ref_mv1_full, &d->bmi.mv, step_param, sadpb,
     160           0 :                      &cpi->fn_ptr[BLOCK_16X16], NULL, NULL, &best_ref_mv1);
     161             : 
     162             : #if ALT_REF_SUBPEL_ENABLED
     163             :   /* Try sub-pixel MC? */
     164             :   {
     165             :     int distortion;
     166             :     unsigned int sse;
     167             :     /* Ignore mv costing by sending NULL cost array */
     168           0 :     bestsme = cpi->find_fractional_mv_step(
     169             :         x, b, d, &d->bmi.mv, &best_ref_mv1, x->errorperbit,
     170           0 :         &cpi->fn_ptr[BLOCK_16X16], NULL, &distortion, &sse);
     171             :   }
     172             : #endif
     173             : 
     174             :   /* Save input state */
     175           0 :   b->base_src = base_src;
     176           0 :   b->src = src;
     177           0 :   b->src_stride = src_stride;
     178           0 :   x->e_mbd.pre.y_buffer = base_pre;
     179           0 :   d->offset = pre;
     180           0 :   x->e_mbd.pre.y_stride = pre_stride;
     181             : 
     182           0 :   return bestsme;
     183             : }
     184             : #endif
     185             : 
     186           0 : static void vp8_temporal_filter_iterate_c(VP8_COMP *cpi, int frame_count,
     187             :                                           int alt_ref_index, int strength) {
     188             :   int byte;
     189             :   int frame;
     190             :   int mb_col, mb_row;
     191             :   unsigned int filter_weight;
     192           0 :   int mb_cols = cpi->common.mb_cols;
     193           0 :   int mb_rows = cpi->common.mb_rows;
     194           0 :   int mb_y_offset = 0;
     195           0 :   int mb_uv_offset = 0;
     196             :   DECLARE_ALIGNED(16, unsigned int, accumulator[16 * 16 + 8 * 8 + 8 * 8]);
     197             :   DECLARE_ALIGNED(16, unsigned short, count[16 * 16 + 8 * 8 + 8 * 8]);
     198           0 :   MACROBLOCKD *mbd = &cpi->mb.e_mbd;
     199           0 :   YV12_BUFFER_CONFIG *f = cpi->frames[alt_ref_index];
     200             :   unsigned char *dst1, *dst2;
     201             :   DECLARE_ALIGNED(16, unsigned char, predictor[16 * 16 + 8 * 8 + 8 * 8]);
     202             : 
     203             :   /* Save input state */
     204           0 :   unsigned char *y_buffer = mbd->pre.y_buffer;
     205           0 :   unsigned char *u_buffer = mbd->pre.u_buffer;
     206           0 :   unsigned char *v_buffer = mbd->pre.v_buffer;
     207             : 
     208           0 :   for (mb_row = 0; mb_row < mb_rows; ++mb_row) {
     209             : #if ALT_REF_MC_ENABLED
     210             :     /* Source frames are extended to 16 pixels.  This is different than
     211             :      *  L/A/G reference frames that have a border of 32 (VP8BORDERINPIXELS)
     212             :      * A 6 tap filter is used for motion search.  This requires 2 pixels
     213             :      *  before and 3 pixels after.  So the largest Y mv on a border would
     214             :      *  then be 16 - 3.  The UV blocks are half the size of the Y and
     215             :      *  therefore only extended by 8.  The largest mv that a UV block
     216             :      *  can support is 8 - 3.  A UV mv is half of a Y mv.
     217             :      *  (16 - 3) >> 1 == 6 which is greater than 8 - 3.
     218             :      * To keep the mv in play for both Y and UV planes the max that it
     219             :      *  can be on a border is therefore 16 - 5.
     220             :      */
     221           0 :     cpi->mb.mv_row_min = -((mb_row * 16) + (16 - 5));
     222           0 :     cpi->mb.mv_row_max = ((cpi->common.mb_rows - 1 - mb_row) * 16) + (16 - 5);
     223             : #endif
     224             : 
     225           0 :     for (mb_col = 0; mb_col < mb_cols; ++mb_col) {
     226             :       int i, j, k;
     227             :       int stride;
     228             : 
     229           0 :       memset(accumulator, 0, 384 * sizeof(unsigned int));
     230           0 :       memset(count, 0, 384 * sizeof(unsigned short));
     231             : 
     232             : #if ALT_REF_MC_ENABLED
     233           0 :       cpi->mb.mv_col_min = -((mb_col * 16) + (16 - 5));
     234           0 :       cpi->mb.mv_col_max = ((cpi->common.mb_cols - 1 - mb_col) * 16) + (16 - 5);
     235             : #endif
     236             : 
     237           0 :       for (frame = 0; frame < frame_count; ++frame) {
     238           0 :         if (cpi->frames[frame] == NULL) continue;
     239             : 
     240           0 :         mbd->block[0].bmi.mv.as_mv.row = 0;
     241           0 :         mbd->block[0].bmi.mv.as_mv.col = 0;
     242             : 
     243           0 :         if (frame == alt_ref_index) {
     244           0 :           filter_weight = 2;
     245             :         } else {
     246           0 :           int err = 0;
     247             : #if ALT_REF_MC_ENABLED
     248             : #define THRESH_LOW 10000
     249             : #define THRESH_HIGH 20000
     250             :           /* Find best match in this frame by MC */
     251           0 :           err = vp8_temporal_filter_find_matching_mb_c(
     252             :               cpi, cpi->frames[alt_ref_index], cpi->frames[frame], mb_y_offset,
     253             :               THRESH_LOW);
     254             : #endif
     255             :           /* Assign higher weight to matching MB if it's error
     256             :            * score is lower. If not applying MC default behavior
     257             :            * is to weight all MBs equal.
     258             :            */
     259           0 :           filter_weight = err < THRESH_LOW ? 2 : err < THRESH_HIGH ? 1 : 0;
     260             :         }
     261             : 
     262           0 :         if (filter_weight != 0) {
     263             :           /* Construct the predictors */
     264           0 :           vp8_temporal_filter_predictors_mb_c(
     265           0 :               mbd, cpi->frames[frame]->y_buffer + mb_y_offset,
     266           0 :               cpi->frames[frame]->u_buffer + mb_uv_offset,
     267           0 :               cpi->frames[frame]->v_buffer + mb_uv_offset,
     268           0 :               cpi->frames[frame]->y_stride, mbd->block[0].bmi.mv.as_mv.row,
     269           0 :               mbd->block[0].bmi.mv.as_mv.col, predictor);
     270             : 
     271             :           /* Apply the filter (YUV) */
     272           0 :           vp8_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride,
     273             :                                     predictor, 16, strength, filter_weight,
     274             :                                     accumulator, count);
     275             : 
     276           0 :           vp8_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride,
     277             :                                     predictor + 256, 8, strength, filter_weight,
     278             :                                     accumulator + 256, count + 256);
     279             : 
     280           0 :           vp8_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride,
     281             :                                     predictor + 320, 8, strength, filter_weight,
     282             :                                     accumulator + 320, count + 320);
     283             :         }
     284             :       }
     285             : 
     286             :       /* Normalize filter output to produce AltRef frame */
     287           0 :       dst1 = cpi->alt_ref_buffer.y_buffer;
     288           0 :       stride = cpi->alt_ref_buffer.y_stride;
     289           0 :       byte = mb_y_offset;
     290           0 :       for (i = 0, k = 0; i < 16; ++i) {
     291           0 :         for (j = 0; j < 16; j++, k++) {
     292           0 :           unsigned int pval = accumulator[k] + (count[k] >> 1);
     293           0 :           pval *= cpi->fixed_divide[count[k]];
     294           0 :           pval >>= 19;
     295             : 
     296           0 :           dst1[byte] = (unsigned char)pval;
     297             : 
     298             :           /* move to next pixel */
     299           0 :           byte++;
     300             :         }
     301             : 
     302           0 :         byte += stride - 16;
     303             :       }
     304             : 
     305           0 :       dst1 = cpi->alt_ref_buffer.u_buffer;
     306           0 :       dst2 = cpi->alt_ref_buffer.v_buffer;
     307           0 :       stride = cpi->alt_ref_buffer.uv_stride;
     308           0 :       byte = mb_uv_offset;
     309           0 :       for (i = 0, k = 256; i < 8; ++i) {
     310           0 :         for (j = 0; j < 8; j++, k++) {
     311           0 :           int m = k + 64;
     312             : 
     313             :           /* U */
     314           0 :           unsigned int pval = accumulator[k] + (count[k] >> 1);
     315           0 :           pval *= cpi->fixed_divide[count[k]];
     316           0 :           pval >>= 19;
     317           0 :           dst1[byte] = (unsigned char)pval;
     318             : 
     319             :           /* V */
     320           0 :           pval = accumulator[m] + (count[m] >> 1);
     321           0 :           pval *= cpi->fixed_divide[count[m]];
     322           0 :           pval >>= 19;
     323           0 :           dst2[byte] = (unsigned char)pval;
     324             : 
     325             :           /* move to next pixel */
     326           0 :           byte++;
     327             :         }
     328             : 
     329           0 :         byte += stride - 8;
     330             :       }
     331             : 
     332           0 :       mb_y_offset += 16;
     333           0 :       mb_uv_offset += 8;
     334             :     }
     335             : 
     336           0 :     mb_y_offset += 16 * (f->y_stride - mb_cols);
     337           0 :     mb_uv_offset += 8 * (f->uv_stride - mb_cols);
     338             :   }
     339             : 
     340             :   /* Restore input state */
     341           0 :   mbd->pre.y_buffer = y_buffer;
     342           0 :   mbd->pre.u_buffer = u_buffer;
     343           0 :   mbd->pre.v_buffer = v_buffer;
     344           0 : }
     345             : 
     346           0 : void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance) {
     347           0 :   int frame = 0;
     348             : 
     349           0 :   int num_frames_backward = 0;
     350           0 :   int num_frames_forward = 0;
     351           0 :   int frames_to_blur_backward = 0;
     352           0 :   int frames_to_blur_forward = 0;
     353           0 :   int frames_to_blur = 0;
     354           0 :   int start_frame = 0;
     355             : 
     356           0 :   int strength = cpi->oxcf.arnr_strength;
     357             : 
     358           0 :   int blur_type = cpi->oxcf.arnr_type;
     359             : 
     360           0 :   int max_frames = cpi->active_arnr_frames;
     361             : 
     362           0 :   num_frames_backward = distance;
     363           0 :   num_frames_forward =
     364           0 :       vp8_lookahead_depth(cpi->lookahead) - (num_frames_backward + 1);
     365             : 
     366           0 :   switch (blur_type) {
     367             :     case 1:
     368             :       /* Backward Blur */
     369             : 
     370           0 :       frames_to_blur_backward = num_frames_backward;
     371             : 
     372           0 :       if (frames_to_blur_backward >= max_frames) {
     373           0 :         frames_to_blur_backward = max_frames - 1;
     374             :       }
     375             : 
     376           0 :       frames_to_blur = frames_to_blur_backward + 1;
     377           0 :       break;
     378             : 
     379             :     case 2:
     380             :       /* Forward Blur */
     381             : 
     382           0 :       frames_to_blur_forward = num_frames_forward;
     383             : 
     384           0 :       if (frames_to_blur_forward >= max_frames) {
     385           0 :         frames_to_blur_forward = max_frames - 1;
     386             :       }
     387             : 
     388           0 :       frames_to_blur = frames_to_blur_forward + 1;
     389           0 :       break;
     390             : 
     391             :     case 3:
     392             :     default:
     393             :       /* Center Blur */
     394           0 :       frames_to_blur_forward = num_frames_forward;
     395           0 :       frames_to_blur_backward = num_frames_backward;
     396             : 
     397           0 :       if (frames_to_blur_forward > frames_to_blur_backward) {
     398           0 :         frames_to_blur_forward = frames_to_blur_backward;
     399             :       }
     400             : 
     401           0 :       if (frames_to_blur_backward > frames_to_blur_forward) {
     402           0 :         frames_to_blur_backward = frames_to_blur_forward;
     403             :       }
     404             : 
     405             :       /* When max_frames is even we have 1 more frame backward than forward */
     406           0 :       if (frames_to_blur_forward > (max_frames - 1) / 2) {
     407           0 :         frames_to_blur_forward = ((max_frames - 1) / 2);
     408             :       }
     409             : 
     410           0 :       if (frames_to_blur_backward > (max_frames / 2)) {
     411           0 :         frames_to_blur_backward = (max_frames / 2);
     412             :       }
     413             : 
     414           0 :       frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1;
     415           0 :       break;
     416             :   }
     417             : 
     418           0 :   start_frame = distance + frames_to_blur_forward;
     419             : 
     420             :   /* Setup frame pointers, NULL indicates frame not included in filter */
     421           0 :   memset(cpi->frames, 0, max_frames * sizeof(YV12_BUFFER_CONFIG *));
     422           0 :   for (frame = 0; frame < frames_to_blur; ++frame) {
     423           0 :     int which_buffer = start_frame - frame;
     424           0 :     struct lookahead_entry *buf =
     425           0 :         vp8_lookahead_peek(cpi->lookahead, which_buffer, PEEK_FORWARD);
     426           0 :     cpi->frames[frames_to_blur - 1 - frame] = &buf->img;
     427             :   }
     428             : 
     429           0 :   vp8_temporal_filter_iterate_c(cpi, frames_to_blur, frames_to_blur_backward,
     430             :                                 strength);
     431           0 : }
     432             : #endif

Generated by: LCOV version 1.13