tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

av1_temporal_denoiser.c (28975B)


      1 /*
      2 * Copyright (c) 2020, Alliance for Open Media. All rights reserved.
      3 *
      4 * This source code is subject to the terms of the BSD 2 Clause License and
      5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
      6 * was not distributed with this source code in the LICENSE file, you can
      7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
      8 * Media Patent License 1.0 was not distributed with this source code in the
      9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
     10 */
     11 
     12 #include <assert.h>
     13 #include <limits.h>
     14 #include <math.h>
     15 
     16 #include "config/aom_dsp_rtcd.h"
     17 #include "aom_dsp/aom_dsp_common.h"
     18 #include "aom_scale/yv12config.h"
     19 #include "aom/aom_integer.h"
     20 #include "av1/common/reconinter.h"
     21 #include "av1/encoder/reconinter_enc.h"
     22 #include "av1/encoder/context_tree.h"
     23 #include "av1/encoder/av1_temporal_denoiser.h"
     24 #include "av1/encoder/encoder.h"
     25 
     26 #ifdef OUTPUT_YUV_DENOISED
     27 static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
     28 #endif
     29 
     30 static int absdiff_thresh(BLOCK_SIZE bs, int increase_denoising) {
     31  (void)bs;
     32  return 3 + (increase_denoising ? 1 : 0);
     33 }
     34 
     35 static int delta_thresh(BLOCK_SIZE bs, int increase_denoising) {
     36  (void)bs;
     37  (void)increase_denoising;
     38  return 4;
     39 }
     40 
     41 static int noise_motion_thresh(BLOCK_SIZE bs, int increase_denoising) {
     42  (void)bs;
     43  (void)increase_denoising;
     44  return 625;
     45 }
     46 
     47 static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
     48  return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 80 : 40);
     49 }
     50 
     51 static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
     52                           int motion_magnitude) {
     53  if (motion_magnitude > noise_motion_thresh(bs, increase_denoising)) {
     54    if (increase_denoising)
     55      return (1 << num_pels_log2_lookup[bs]) << 2;
     56    else
     57      return 0;
     58  } else {
     59    return (1 << num_pels_log2_lookup[bs]) << 4;
     60  }
     61 }
     62 
     63 static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
     64  return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
     65 }
     66 
     67 // TODO(kyslov): If increase_denoising is enabled in the future,
     68 // we might need to update the code for calculating 'total_adj' in
     69 // case the C code is not bit-exact with corresponding sse2 code.
     70 int av1_denoiser_filter_c(const uint8_t *sig, int sig_stride,
     71                          const uint8_t *mc_avg, int mc_avg_stride,
     72                          uint8_t *avg, int avg_stride, int increase_denoising,
     73                          BLOCK_SIZE bs, int motion_magnitude) {
     74  int r, c;
     75  const uint8_t *sig_start = sig;
     76  const uint8_t *mc_avg_start = mc_avg;
     77  uint8_t *avg_start = avg;
     78  int diff, adj, absdiff, delta;
     79  int adj_val[] = { 3, 4, 6 };
     80  int total_adj = 0;
     81  int shift_inc = 1;
     82 
     83  // If motion_magnitude is small, making the denoiser more aggressive by
     84  // increasing the adjustment for each level. Add another increment for
     85  // blocks that are labeled for increase denoising.
     86  if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) {
     87    if (increase_denoising) {
     88      shift_inc = 2;
     89    }
     90    adj_val[0] += shift_inc;
     91    adj_val[1] += shift_inc;
     92    adj_val[2] += shift_inc;
     93  }
     94 
     95  // First attempt to apply a strong temporal denoising filter.
     96  for (r = 0; r < block_size_high[bs]; ++r) {
     97    for (c = 0; c < block_size_wide[bs]; ++c) {
     98      diff = mc_avg[c] - sig[c];
     99      absdiff = abs(diff);
    100 
    101      if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
    102        avg[c] = mc_avg[c];
    103        total_adj += diff;
    104      } else {
    105        switch (absdiff) {
    106          case 4:
    107          case 5:
    108          case 6:
    109          case 7: adj = adj_val[0]; break;
    110          case 8:
    111          case 9:
    112          case 10:
    113          case 11:
    114          case 12:
    115          case 13:
    116          case 14:
    117          case 15: adj = adj_val[1]; break;
    118          default: adj = adj_val[2];
    119        }
    120        if (diff > 0) {
    121          avg[c] = AOMMIN(UINT8_MAX, sig[c] + adj);
    122          total_adj += adj;
    123        } else {
    124          avg[c] = AOMMAX(0, sig[c] - adj);
    125          total_adj -= adj;
    126        }
    127      }
    128    }
    129    sig += sig_stride;
    130    avg += avg_stride;
    131    mc_avg += mc_avg_stride;
    132  }
    133 
    134  // If the strong filter did not modify the signal too much, we're all set.
    135  if (abs(total_adj) <= total_adj_strong_thresh(bs, increase_denoising)) {
    136    return FILTER_BLOCK;
    137  }
    138 
    139  // Otherwise, we try to dampen the filter if the delta is not too high.
    140  delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising)) >>
    141           num_pels_log2_lookup[bs]) +
    142          1;
    143 
    144  if (delta >= delta_thresh(bs, increase_denoising)) {
    145    return COPY_BLOCK;
    146  }
    147 
    148  mc_avg = mc_avg_start;
    149  avg = avg_start;
    150  sig = sig_start;
    151  for (r = 0; r < block_size_high[bs]; ++r) {
    152    for (c = 0; c < block_size_wide[bs]; ++c) {
    153      diff = mc_avg[c] - sig[c];
    154      adj = abs(diff);
    155      if (adj > delta) {
    156        adj = delta;
    157      }
    158      if (diff > 0) {
    159        // Diff positive means we made positive adjustment above
    160        // (in first try/attempt), so now make negative adjustment to bring
    161        // denoised signal down.
    162        avg[c] = AOMMAX(0, avg[c] - adj);
    163        total_adj -= adj;
    164      } else {
    165        // Diff negative means we made negative adjustment above
    166        // (in first try/attempt), so now make positive adjustment to bring
    167        // denoised signal up.
    168        avg[c] = AOMMIN(UINT8_MAX, avg[c] + adj);
    169        total_adj += adj;
    170      }
    171    }
    172    sig += sig_stride;
    173    avg += avg_stride;
    174    mc_avg += mc_avg_stride;
    175  }
    176 
    177  // We can use the filter if it has been sufficiently dampened
    178  if (abs(total_adj) <= total_adj_weak_thresh(bs, increase_denoising)) {
    179    return FILTER_BLOCK;
    180  }
    181  return COPY_BLOCK;
    182 }
    183 
    184 static uint8_t *block_start(uint8_t *framebuf, int stride, int mi_row,
    185                            int mi_col) {
    186  return framebuf + (stride * mi_row << 2) + (mi_col << 2);
    187 }
    188 
    189 static AV1_DENOISER_DECISION perform_motion_compensation(
    190    AV1_COMMON *const cm, AV1_DENOISER *denoiser, MACROBLOCK *mb, BLOCK_SIZE bs,
    191    int increase_denoising, int mi_row, int mi_col, PICK_MODE_CONTEXT *ctx,
    192    int motion_magnitude, int *zeromv_filter, int num_spatial_layers, int width,
    193    int lst_fb_idx, int gld_fb_idx, int use_svc, int spatial_layer,
    194    int use_gf_temporal_ref) {
    195  const int sse_diff = (ctx->newmv_sse == UINT_MAX)
    196                           ? 0
    197                           : ((int)ctx->zeromv_sse - (int)ctx->newmv_sse);
    198  int frame;
    199  int denoise_layer_idx = 0;
    200  MACROBLOCKD *filter_mbd = &mb->e_mbd;
    201  MB_MODE_INFO *mi = filter_mbd->mi[0];
    202  MB_MODE_INFO saved_mi;
    203  int i;
    204  struct buf_2d saved_dst[MAX_MB_PLANE];
    205  struct buf_2d saved_pre[MAX_MB_PLANE];
    206  // const RefBuffer *saved_block_refs[2];
    207  MV_REFERENCE_FRAME saved_frame;
    208 
    209  frame = ctx->best_reference_frame;
    210 
    211  saved_mi = *mi;
    212 
    213  // Avoid denoising small blocks. When noise > kDenLow or frame width > 480,
    214  // denoise 16x16 blocks.
    215  if (bs == BLOCK_8X8 || bs == BLOCK_8X16 || bs == BLOCK_16X8 ||
    216      (bs == BLOCK_16X16 && width > 480 &&
    217       denoiser->denoising_level <= kDenLow))
    218    return COPY_BLOCK;
    219 
    220  // If the best reference frame uses inter-prediction and there is enough of a
    221  // difference in sum-squared-error, use it.
    222  if (frame != INTRA_FRAME && frame != ALTREF_FRAME && frame != GOLDEN_FRAME &&
    223      sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
    224    mi->ref_frame[0] = ctx->best_reference_frame;
    225    mi->mode = ctx->best_sse_inter_mode;
    226    mi->mv[0] = ctx->best_sse_mv;
    227  } else {
    228    // Otherwise, use the zero reference frame.
    229    frame = ctx->best_zeromv_reference_frame;
    230    ctx->newmv_sse = ctx->zeromv_sse;
    231    // Bias to last reference.
    232    if ((num_spatial_layers > 1 && !use_gf_temporal_ref) ||
    233        frame == ALTREF_FRAME ||
    234        (frame == GOLDEN_FRAME && use_gf_temporal_ref) ||
    235        (frame != LAST_FRAME &&
    236         ((ctx->zeromv_lastref_sse < (5 * ctx->zeromv_sse) >> 2) ||
    237          denoiser->denoising_level >= kDenHigh))) {
    238      frame = LAST_FRAME;
    239      ctx->newmv_sse = ctx->zeromv_lastref_sse;
    240    }
    241    mi->ref_frame[0] = frame;
    242    mi->mode = GLOBALMV;
    243    mi->mv[0].as_int = 0;
    244    ctx->best_sse_inter_mode = GLOBALMV;
    245    ctx->best_sse_mv.as_int = 0;
    246    *zeromv_filter = 1;
    247    if (denoiser->denoising_level > kDenMedium) {
    248      motion_magnitude = 0;
    249    }
    250  }
    251 
    252  saved_frame = frame;
    253  // When using SVC, we need to map REF_FRAME to the frame buffer index.
    254  if (use_svc) {
    255    if (frame == LAST_FRAME)
    256      frame = lst_fb_idx + 1;
    257    else if (frame == GOLDEN_FRAME)
    258      frame = gld_fb_idx + 1;
    259    // Shift for the second spatial layer.
    260    if (num_spatial_layers - spatial_layer == 2)
    261      frame = frame + denoiser->num_ref_frames;
    262    denoise_layer_idx = num_spatial_layers - spatial_layer - 1;
    263  }
    264 
    265  // Force copy (no denoise, copy source in denoised buffer) if
    266  // running_avg_y[frame] is NULL.
    267  if (denoiser->running_avg_y[frame].buffer_alloc == NULL) {
    268    // Restore everything to its original state
    269    *mi = saved_mi;
    270    return COPY_BLOCK;
    271  }
    272 
    273  if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
    274    // Restore everything to its original state
    275    *mi = saved_mi;
    276    return COPY_BLOCK;
    277  }
    278  if (motion_magnitude > (noise_motion_thresh(bs, increase_denoising) << 3)) {
    279    // Restore everything to its original state
    280    *mi = saved_mi;
    281    return COPY_BLOCK;
    282  }
    283 
    284  // We will restore these after motion compensation.
    285  for (i = 0; i < MAX_MB_PLANE; ++i) {
    286    saved_pre[i] = filter_mbd->plane[i].pre[0];
    287    saved_dst[i] = filter_mbd->plane[i].dst;
    288  }
    289 
    290  // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
    291  // struct.
    292  set_ref_ptrs(cm, filter_mbd, saved_frame, NONE);
    293  av1_setup_pre_planes(filter_mbd, 0, &(denoiser->running_avg_y[frame]), mi_row,
    294                       mi_col, filter_mbd->block_ref_scale_factors[0], 1);
    295  av1_setup_dst_planes(filter_mbd->plane, bs,
    296                       &(denoiser->mc_running_avg_y[denoise_layer_idx]), mi_row,
    297                       mi_col, 0, 1);
    298 
    299  av1_enc_build_inter_predictor_y(filter_mbd, mi_row, mi_col);
    300 
    301  // Restore everything to its original state
    302  *mi = saved_mi;
    303  for (i = 0; i < MAX_MB_PLANE; ++i) {
    304    filter_mbd->plane[i].pre[0] = saved_pre[i];
    305    filter_mbd->plane[i].dst = saved_dst[i];
    306  }
    307 
    308  return FILTER_BLOCK;
    309 }
    310 
    311 void av1_denoiser_denoise(AV1_COMP *cpi, MACROBLOCK *mb, int mi_row, int mi_col,
    312                          BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
    313                          AV1_DENOISER_DECISION *denoiser_decision,
    314                          int use_gf_temporal_ref) {
    315  int mv_col, mv_row;
    316  int motion_magnitude = 0;
    317  int zeromv_filter = 0;
    318  AV1_DENOISER *denoiser = &cpi->denoiser;
    319  AV1_DENOISER_DECISION decision = COPY_BLOCK;
    320 
    321  const int shift =
    322      cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id == 2
    323          ? denoiser->num_ref_frames
    324          : 0;
    325  YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME + shift];
    326  const int denoise_layer_index =
    327      cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id - 1;
    328  YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y[denoise_layer_index];
    329  uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col);
    330 
    331  uint8_t *mc_avg_start =
    332      block_start(mc_avg.y_buffer, mc_avg.y_stride, mi_row, mi_col);
    333  struct buf_2d src = mb->plane[0].src;
    334  int increase_denoising = 0;
    335  int last_is_reference = cpi->ref_frame_flags & AOM_LAST_FLAG;
    336  mv_col = ctx->best_sse_mv.as_mv.col;
    337  mv_row = ctx->best_sse_mv.as_mv.row;
    338  motion_magnitude = mv_row * mv_row + mv_col * mv_col;
    339 
    340  if (denoiser->denoising_level == kDenHigh) increase_denoising = 1;
    341 
    342  // Copy block if LAST_FRAME is not a reference.
    343  // Last doesn't always exist when SVC layers are dynamically changed, e.g. top
    344  // spatial layer doesn't have last reference when it's brought up for the
    345  // first time on the fly.
    346  if (last_is_reference && denoiser->denoising_level >= kDenLow &&
    347      !ctx->sb_skip_denoising)
    348    decision = perform_motion_compensation(
    349        &cpi->common, denoiser, mb, bs, increase_denoising, mi_row, mi_col, ctx,
    350        motion_magnitude, &zeromv_filter, cpi->svc.number_spatial_layers,
    351        cpi->source->y_width, cpi->ppi->rtc_ref.ref_idx[0],
    352        cpi->ppi->rtc_ref.ref_idx[3], cpi->ppi->use_svc,
    353        cpi->svc.spatial_layer_id, use_gf_temporal_ref);
    354 
    355  if (decision == FILTER_BLOCK) {
    356    decision = av1_denoiser_filter(src.buf, src.stride, mc_avg_start,
    357                                   mc_avg.y_stride, avg_start, avg.y_stride,
    358                                   increase_denoising, bs, motion_magnitude);
    359  }
    360 
    361  if (decision == FILTER_BLOCK) {
    362    aom_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride,
    363                      block_size_wide[bs], block_size_high[bs]);
    364  } else {  // COPY_BLOCK
    365    aom_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride,
    366                      block_size_wide[bs], block_size_high[bs]);
    367  }
    368  *denoiser_decision = decision;
    369  if (decision == FILTER_BLOCK && zeromv_filter == 1)
    370    *denoiser_decision = FILTER_ZEROMV_BLOCK;
    371 }
    372 
    373 static void copy_frame(YV12_BUFFER_CONFIG *const dest,
    374                       const YV12_BUFFER_CONFIG *const src) {
    375  int r;
    376  const uint8_t *srcbuf = src->y_buffer;
    377  uint8_t *destbuf = dest->y_buffer;
    378 
    379  assert(dest->y_width == src->y_width);
    380  assert(dest->y_height == src->y_height);
    381 
    382  for (r = 0; r < dest->y_height; ++r) {
    383    memcpy(destbuf, srcbuf, dest->y_width);
    384    destbuf += dest->y_stride;
    385    srcbuf += src->y_stride;
    386  }
    387 }
    388 
    389 static void swap_frame_buffer(YV12_BUFFER_CONFIG *const dest,
    390                              YV12_BUFFER_CONFIG *const src) {
    391  uint8_t *tmp_buf = dest->y_buffer;
    392  assert(dest->y_width == src->y_width);
    393  assert(dest->y_height == src->y_height);
    394  dest->y_buffer = src->y_buffer;
    395  src->y_buffer = tmp_buf;
    396 }
    397 
    398 void av1_denoiser_update_frame_info(
    399    AV1_DENOISER *denoiser, YV12_BUFFER_CONFIG src, struct RTC_REF *rtc_ref,
    400    struct SVC *svc, FRAME_TYPE frame_type, int refresh_alt_ref_frame,
    401    int refresh_golden_frame, int refresh_last_frame, int alt_fb_idx,
    402    int gld_fb_idx, int lst_fb_idx, int resized,
    403    int svc_refresh_denoiser_buffers, int second_spatial_layer) {
    404  const int shift = second_spatial_layer ? denoiser->num_ref_frames : 0;
    405  // Copy source into denoised reference buffers on KEY_FRAME or
    406  // if the just encoded frame was resized. For SVC, copy source if the base
    407  // spatial layer was key frame.
    408  if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset ||
    409      svc_refresh_denoiser_buffers) {
    410    int i;
    411    // Start at 1 so as not to overwrite the INTRA_FRAME
    412    for (i = 1; i < denoiser->num_ref_frames; ++i) {
    413      if (denoiser->running_avg_y[i + shift].buffer_alloc != NULL)
    414        copy_frame(&denoiser->running_avg_y[i + shift], &src);
    415    }
    416    denoiser->reset = 0;
    417    return;
    418  }
    419 
    420  if (rtc_ref->set_ref_frame_config) {
    421    int i;
    422    for (i = 0; i < REF_FRAMES; i++) {
    423      if (rtc_ref->refresh[svc->spatial_layer_id] & (1 << i))
    424        copy_frame(&denoiser->running_avg_y[i + 1 + shift],
    425                   &denoiser->running_avg_y[INTRA_FRAME + shift]);
    426    }
    427  } else {
    428    // If more than one refresh occurs, must copy frame buffer.
    429    if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame) >
    430        1) {
    431      if (refresh_alt_ref_frame) {
    432        copy_frame(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
    433                   &denoiser->running_avg_y[INTRA_FRAME + shift]);
    434      }
    435      if (refresh_golden_frame) {
    436        copy_frame(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
    437                   &denoiser->running_avg_y[INTRA_FRAME + shift]);
    438      }
    439      if (refresh_last_frame) {
    440        copy_frame(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
    441                   &denoiser->running_avg_y[INTRA_FRAME + shift]);
    442      }
    443    } else {
    444      if (refresh_alt_ref_frame) {
    445        swap_frame_buffer(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
    446                          &denoiser->running_avg_y[INTRA_FRAME + shift]);
    447      }
    448      if (refresh_golden_frame) {
    449        swap_frame_buffer(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
    450                          &denoiser->running_avg_y[INTRA_FRAME + shift]);
    451      }
    452      if (refresh_last_frame) {
    453        swap_frame_buffer(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
    454                          &denoiser->running_avg_y[INTRA_FRAME + shift]);
    455      }
    456    }
    457  }
    458 }
    459 
    460 void av1_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
    461  ctx->zeromv_sse = INT64_MAX;
    462  ctx->newmv_sse = INT64_MAX;
    463  ctx->zeromv_lastref_sse = INT64_MAX;
    464  ctx->best_sse_mv.as_int = 0;
    465 }
    466 
    467 void av1_denoiser_update_frame_stats(MB_MODE_INFO *mi, int64_t sse,
    468                                     PREDICTION_MODE mode,
    469                                     PICK_MODE_CONTEXT *ctx) {
    470  if (mi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
    471    ctx->zeromv_sse = sse;
    472    ctx->best_zeromv_reference_frame = mi->ref_frame[0];
    473    if (mi->ref_frame[0] == LAST_FRAME) ctx->zeromv_lastref_sse = sse;
    474  }
    475 
    476  if (mi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
    477    ctx->newmv_sse = sse;
    478    ctx->best_sse_inter_mode = mode;
    479    ctx->best_sse_mv = mi->mv[0];
    480    ctx->best_reference_frame = mi->ref_frame[0];
    481  }
    482 }
    483 
    484 static int av1_denoiser_realloc_svc_helper(AV1_COMMON *cm,
    485                                           AV1_DENOISER *denoiser, int fb_idx) {
    486  int fail = 0;
    487  if (denoiser->running_avg_y[fb_idx].buffer_alloc == NULL) {
    488    fail = aom_alloc_frame_buffer(
    489        &denoiser->running_avg_y[fb_idx], cm->width, cm->height,
    490        cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
    491        cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
    492        cm->features.byte_alignment, false, 0);
    493    if (fail) {
    494      av1_denoiser_free(denoiser);
    495      return 1;
    496    }
    497  }
    498  return 0;
    499 }
    500 
    501 int av1_denoiser_realloc_svc(AV1_COMMON *cm, AV1_DENOISER *denoiser,
    502                             struct RTC_REF *rtc_ref, struct SVC *svc,
    503                             int svc_buf_shift, int refresh_alt,
    504                             int refresh_gld, int refresh_lst, int alt_fb_idx,
    505                             int gld_fb_idx, int lst_fb_idx) {
    506  int fail = 0;
    507  if (rtc_ref->set_ref_frame_config) {
    508    int i;
    509    for (i = 0; i < REF_FRAMES; i++) {
    510      if (cm->current_frame.frame_type == KEY_FRAME ||
    511          rtc_ref->refresh[svc->spatial_layer_id] & (1 << i)) {
    512        fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
    513                                               i + 1 + svc_buf_shift);
    514      }
    515    }
    516  } else {
    517    if (refresh_alt) {
    518      // Increase the frame buffer index by 1 to map it to the buffer index in
    519      // the denoiser.
    520      fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
    521                                             alt_fb_idx + 1 + svc_buf_shift);
    522      if (fail) return 1;
    523    }
    524    if (refresh_gld) {
    525      fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
    526                                             gld_fb_idx + 1 + svc_buf_shift);
    527      if (fail) return 1;
    528    }
    529    if (refresh_lst) {
    530      fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
    531                                             lst_fb_idx + 1 + svc_buf_shift);
    532      if (fail) return 1;
    533    }
    534  }
    535  return 0;
    536 }
    537 
    538 int av1_denoiser_alloc(AV1_COMMON *cm, struct SVC *svc, AV1_DENOISER *denoiser,
    539                       int use_svc, int noise_sen, int width, int height,
    540                       int ssx, int ssy, int use_highbitdepth, int border) {
    541  int i, layer, fail, init_num_ref_frames;
    542  const int legacy_byte_alignment = 0;
    543  int num_layers = 1;
    544  int scaled_width = width;
    545  int scaled_height = height;
    546  if (use_svc) {
    547    LAYER_CONTEXT *lc = &svc->layer_context[svc->spatial_layer_id *
    548                                                svc->number_temporal_layers +
    549                                            svc->temporal_layer_id];
    550    av1_get_layer_resolution(width, height, lc->scaling_factor_num,
    551                             lc->scaling_factor_den, &scaled_width,
    552                             &scaled_height);
    553    // For SVC: only denoise at most 2 spatial (highest) layers.
    554    if (noise_sen >= 2)
    555      // Denoise from one spatial layer below the top.
    556      svc->first_layer_denoise = AOMMAX(svc->number_spatial_layers - 2, 0);
    557    else
    558      // Only denoise the top spatial layer.
    559      svc->first_layer_denoise = AOMMAX(svc->number_spatial_layers - 1, 0);
    560    num_layers = svc->number_spatial_layers - svc->first_layer_denoise;
    561  }
    562  assert(denoiser != NULL);
    563  denoiser->num_ref_frames = use_svc ? SVC_REF_FRAMES : NONSVC_REF_FRAMES;
    564  init_num_ref_frames = use_svc ? REF_FRAMES : NONSVC_REF_FRAMES;
    565  denoiser->num_layers = num_layers;
    566  CHECK_MEM_ERROR(cm, denoiser->running_avg_y,
    567                  aom_calloc(denoiser->num_ref_frames * num_layers,
    568                             sizeof(denoiser->running_avg_y[0])));
    569  CHECK_MEM_ERROR(
    570      cm, denoiser->mc_running_avg_y,
    571      aom_calloc(num_layers, sizeof(denoiser->mc_running_avg_y[0])));
    572 
    573  for (layer = 0; layer < num_layers; ++layer) {
    574    const int denoise_width = (layer == 0) ? width : scaled_width;
    575    const int denoise_height = (layer == 0) ? height : scaled_height;
    576    for (i = 0; i < init_num_ref_frames; ++i) {
    577      fail = aom_alloc_frame_buffer(
    578          &denoiser->running_avg_y[i + denoiser->num_ref_frames * layer],
    579          denoise_width, denoise_height, ssx, ssy, use_highbitdepth, border,
    580          legacy_byte_alignment, false, 0);
    581      if (fail) {
    582        av1_denoiser_free(denoiser);
    583        return 1;
    584      }
    585 #ifdef OUTPUT_YUV_DENOISED
    586      make_grayscale(&denoiser->running_avg_y[i]);
    587 #endif
    588    }
    589 
    590    fail = aom_alloc_frame_buffer(
    591        &denoiser->mc_running_avg_y[layer], denoise_width, denoise_height, ssx,
    592        ssy, use_highbitdepth, border, legacy_byte_alignment, false, 0);
    593    if (fail) {
    594      av1_denoiser_free(denoiser);
    595      return 1;
    596    }
    597  }
    598 
    599  // denoiser->last_source only used for noise_estimation, so only for top
    600  // layer.
    601  fail = aom_alloc_frame_buffer(&denoiser->last_source, width, height, ssx, ssy,
    602                                use_highbitdepth, border, legacy_byte_alignment,
    603                                false, 0);
    604  if (fail) {
    605    av1_denoiser_free(denoiser);
    606    return 1;
    607  }
    608 #ifdef OUTPUT_YUV_DENOISED
    609  make_grayscale(&denoiser->running_avg_y[i]);
    610 #endif
    611  denoiser->frame_buffer_initialized = 1;
    612  denoiser->denoising_level = kDenMedium;
    613  denoiser->prev_denoising_level = kDenMedium;
    614  denoiser->reset = 0;
    615  denoiser->current_denoiser_frame = 0;
    616  return 0;
    617 }
    618 
    619 void av1_denoiser_free(AV1_DENOISER *denoiser) {
    620  int i;
    621  if (denoiser == NULL) {
    622    return;
    623  }
    624  denoiser->frame_buffer_initialized = 0;
    625  for (i = 0; i < denoiser->num_ref_frames * denoiser->num_layers; ++i) {
    626    aom_free_frame_buffer(&denoiser->running_avg_y[i]);
    627  }
    628  aom_free(denoiser->running_avg_y);
    629  denoiser->running_avg_y = NULL;
    630 
    631  for (i = 0; i < denoiser->num_layers; ++i) {
    632    aom_free_frame_buffer(&denoiser->mc_running_avg_y[i]);
    633  }
    634 
    635  aom_free(denoiser->mc_running_avg_y);
    636  denoiser->mc_running_avg_y = NULL;
    637  aom_free_frame_buffer(&denoiser->last_source);
    638 }
    639 
    640 // TODO(kyslov) Enable when SVC temporal denosing is implemented
    641 #if 0
    642 static void force_refresh_longterm_ref(AV1_COMP *const cpi) {
    643  SVC *const svc = &cpi->svc;
    644  // If long term reference is used, force refresh of that slot, so
    645  // denoiser buffer for long term reference stays in sync.
    646  if (svc->use_gf_temporal_ref_current_layer) {
    647    int index = svc->spatial_layer_id;
    648    if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
    649    assert(index >= 0);
    650    cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
    651    cpi->refresh_alt_ref_frame = 1;
    652  }
    653 }
    654 #endif
    655 
    656 void av1_denoiser_set_noise_level(AV1_COMP *const cpi, int noise_level) {
    657  AV1_DENOISER *const denoiser = &cpi->denoiser;
    658  denoiser->denoising_level = noise_level;
    659  if (denoiser->denoising_level > kDenLowLow &&
    660      denoiser->prev_denoising_level == kDenLowLow) {
    661    denoiser->reset = 1;
    662 // TODO(kyslov) Enable when SVC temporal denosing is implemented
    663 #if 0
    664    force_refresh_longterm_ref(cpi);
    665 #endif
    666  } else {
    667    denoiser->reset = 0;
    668  }
    669  denoiser->prev_denoising_level = denoiser->denoising_level;
    670 }
    671 
    672 // Scale/increase the partition threshold
    673 // for denoiser speed-up.
    674 int64_t av1_scale_part_thresh(int64_t threshold, AV1_DENOISER_LEVEL noise_level,
    675                              CONTENT_STATE_SB content_state,
    676                              int temporal_layer_id) {
    677  if ((content_state.source_sad_nonrd <= kLowSad &&
    678       content_state.low_sumdiff) ||
    679      (content_state.source_sad_nonrd == kHighSad &&
    680       content_state.low_sumdiff) ||
    681      (content_state.lighting_change && !content_state.low_sumdiff) ||
    682      (noise_level == kDenHigh) || (temporal_layer_id != 0)) {
    683    int64_t scaled_thr =
    684        (temporal_layer_id < 2) ? (3 * threshold) >> 1 : (7 * threshold) >> 2;
    685    return scaled_thr;
    686  } else {
    687    return (5 * threshold) >> 2;
    688  }
    689 }
    690 
    691 //  Scale/increase the ac skip threshold for
    692 //  denoiser speed-up.
    693 int64_t av1_scale_acskip_thresh(int64_t threshold,
    694                                AV1_DENOISER_LEVEL noise_level, int abs_sumdiff,
    695                                int temporal_layer_id) {
    696  if (noise_level >= kDenLow && abs_sumdiff < 5)
    697    threshold *= (noise_level == kDenLow)   ? 2
    698                 : (temporal_layer_id == 2) ? 10
    699                                            : 6;
    700  return threshold;
    701 }
    702 
    703 void av1_denoiser_reset_on_first_frame(AV1_COMP *const cpi) {
    704  if (/*av1_denoise_svc_non_key(cpi) &&*/
    705      cpi->denoiser.current_denoiser_frame == 0) {
    706    cpi->denoiser.reset = 1;
    707 // TODO(kyslov) Enable when SVC temporal denosing is implemented
    708 #if 0
    709    force_refresh_longterm_ref(cpi);
    710 #endif
    711  }
    712 }
    713 
    714 void av1_denoiser_update_ref_frame(AV1_COMP *const cpi) {
    715  AV1_COMMON *const cm = &cpi->common;
    716  RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
    717  SVC *const svc = &cpi->svc;
    718 
    719  if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
    720      cpi->denoiser.denoising_level > kDenLowLow) {
    721    int svc_refresh_denoiser_buffers = 0;
    722    int denoise_svc_second_layer = 0;
    723    FRAME_TYPE frame_type = cm->current_frame.frame_type == INTRA_ONLY_FRAME
    724                                ? KEY_FRAME
    725                                : cm->current_frame.frame_type;
    726    cpi->denoiser.current_denoiser_frame++;
    727    const int resize_pending = is_frame_resize_pending(cpi);
    728 
    729    if (cpi->ppi->use_svc) {
    730 // TODO(kyslov) Enable when SVC temporal denosing is implemented
    731 #if 0
    732      const int svc_buf_shift =
    733          svc->number_spatial_layers - svc->spatial_layer_id == 2
    734              ? cpi->denoiser.num_ref_frames
    735              : 0;
    736      int layer =
    737          LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
    738                           svc->number_temporal_layers);
    739      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
    740      svc_refresh_denoiser_buffers =
    741          lc->is_key_frame || svc->spatial_layer_sync[svc->spatial_layer_id];
    742      denoise_svc_second_layer =
    743          svc->number_spatial_layers - svc->spatial_layer_id == 2 ? 1 : 0;
    744      // Check if we need to allocate extra buffers in the denoiser
    745      // for refreshed frames.
    746      if (av1_denoiser_realloc_svc(cm, &cpi->denoiser, rtc_ref,
    747                                   svc, svc_buf_shift,
    748                                   cpi->refresh_alt_ref_frame,
    749                                   cpi->refresh_golden_frame,
    750                                   cpi->refresh_last_frame, cpi->alt_fb_idx,
    751                                   cpi->gld_fb_idx, cpi->lst_fb_idx))
    752        aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
    753                           "Failed to re-allocate denoiser for SVC");
    754 #endif
    755    }
    756    av1_denoiser_update_frame_info(
    757        &cpi->denoiser, *cpi->source, rtc_ref, svc, frame_type,
    758        cpi->refresh_frame.alt_ref_frame, cpi->refresh_frame.golden_frame, 1,
    759        rtc_ref->ref_idx[6], rtc_ref->ref_idx[3], rtc_ref->ref_idx[0],
    760        resize_pending, svc_refresh_denoiser_buffers, denoise_svc_second_layer);
    761  }
    762 }
    763 
    764 #ifdef OUTPUT_YUV_DENOISED
    765 static void make_grayscale(YV12_BUFFER_CONFIG *yuv) {
    766  int r, c;
    767  uint8_t *u = yuv->u_buffer;
    768  uint8_t *v = yuv->v_buffer;
    769 
    770  for (r = 0; r < yuv->uv_height; ++r) {
    771    for (c = 0; c < yuv->uv_width; ++c) {
    772      u[c] = UINT8_MAX / 2;
    773      v[c] = UINT8_MAX / 2;
    774    }
    775    u += yuv->uv_stride;
    776    v += yuv->uv_stride;
    777  }
    778 }
    779 
    780 void aom_write_yuv_frame(FILE *yuv_file, YV12_BUFFER_CONFIG *s) {
    781  unsigned char *src = s->y_buffer;
    782  int h = s->y_crop_height;
    783 
    784  do {
    785    fwrite(src, s->y_width, 1, yuv_file);
    786    src += s->y_stride;
    787  } while (--h);
    788 
    789  src = s->u_buffer;
    790  h = s->uv_crop_height;
    791 
    792  do {
    793    fwrite(src, s->uv_width, 1, yuv_file);
    794    src += s->uv_stride;
    795  } while (--h);
    796 
    797  src = s->v_buffer;
    798  h = s->uv_crop_height;
    799 
    800  do {
    801    fwrite(src, s->uv_width, 1, yuv_file);
    802    src += s->uv_stride;
    803  } while (--h);
    804 }
    805 #endif