Home | History | Annotate | Download | only in encoder
      1 /*
      2  * Copyright (c) 2016, 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 <math.h>
     13 
     14 #include "aom_ports/mem.h"
     15 
     16 #include "av1/encoder/aq_variance.h"
     17 #include "av1/common/seg_common.h"
     18 #include "av1/encoder/encodeframe.h"
     19 #include "av1/encoder/ratectrl.h"
     20 #include "av1/encoder/rd.h"
     21 #include "av1/encoder/segmentation.h"
     22 #include "av1/encoder/dwt.h"
     23 #include "aom_ports/system_state.h"
     24 
     25 static const double rate_ratio[MAX_SEGMENTS] = { 2.2, 1.7, 1.3, 1.0,
     26                                                  0.9, .8,  .7,  .6 };
     27 
     28 static const double deltaq_rate_ratio[MAX_SEGMENTS] = { 2.5,  2.0, 1.5, 1.0,
     29                                                         0.75, 1.0, 1.0, 1.0 };
     30 #define ENERGY_MIN (-4)
     31 #define ENERGY_MAX (1)
     32 #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1)
     33 #define ENERGY_IN_BOUNDS(energy) \
     34   assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX)
     35 
     36 DECLARE_ALIGNED(16, static const uint8_t, av1_all_zeros[MAX_SB_SIZE]) = { 0 };
     37 
     38 DECLARE_ALIGNED(16, static const uint16_t,
     39                 av1_highbd_all_zeros[MAX_SB_SIZE]) = { 0 };
     40 
     41 static const int segment_id[ENERGY_SPAN] = { 0, 1, 1, 2, 3, 4 };
     42 
     43 #define SEGMENT_ID(i) segment_id[(i)-ENERGY_MIN]
     44 
     45 void av1_vaq_frame_setup(AV1_COMP *cpi) {
     46   AV1_COMMON *cm = &cpi->common;
     47   struct segmentation *seg = &cm->seg;
     48   int i;
     49 
     50   int resolution_change =
     51       cm->prev_frame && (cm->width != cm->prev_frame->width ||
     52                          cm->height != cm->prev_frame->height);
     53   int avg_energy = (int)(cpi->twopass.mb_av_energy - 2);
     54   double avg_ratio;
     55   if (avg_energy > 7) avg_energy = 7;
     56   if (avg_energy < 0) avg_energy = 0;
     57   avg_ratio = rate_ratio[avg_energy];
     58 
     59   if (resolution_change) {
     60     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
     61     av1_clearall_segfeatures(seg);
     62     aom_clear_system_state();
     63     av1_disable_segmentation(seg);
     64     return;
     65   }
     66   if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
     67       cpi->refresh_alt_ref_frame ||
     68       (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
     69     cpi->vaq_refresh = 1;
     70 
     71     av1_enable_segmentation(seg);
     72     av1_clearall_segfeatures(seg);
     73 
     74     aom_clear_system_state();
     75 
     76     for (i = 0; i < MAX_SEGMENTS; ++i) {
     77       // Set up avg segment id to be 1.0 and adjust the other segments around
     78       // it.
     79       int qindex_delta = av1_compute_qdelta_by_rate(
     80           &cpi->rc, cm->current_frame.frame_type, cm->base_qindex,
     81           rate_ratio[i] / avg_ratio, cm->seq_params.bit_depth);
     82 
     83       // We don't allow qindex 0 in a segment if the base value is not 0.
     84       // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment
     85       // Q delta is sometimes applied without going back around the rd loop.
     86       // This could lead to an illegal combination of partition size and q.
     87       if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
     88         qindex_delta = -cm->base_qindex + 1;
     89       }
     90 
     91       av1_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta);
     92       av1_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
     93     }
     94   }
     95 }
     96 
     97 int av1_log_block_var(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
     98   // This functions returns a score for the blocks local variance as calculated
     99   // by: sum of the log of the (4x4 variances) of each subblock to the current
    100   // block (x,bs)
    101   // * 32 / number of pixels in the block_size.
    102   // This is used for segmentation because to avoid situations in which a large
    103   // block with a gentle gradient gets marked high variance even though each
    104   // subblock has a low variance.   This allows us to assign the same segment
    105   // number for the same sorts of area regardless of how the partitioning goes.
    106 
    107   MACROBLOCKD *xd = &x->e_mbd;
    108   double var = 0;
    109   unsigned int sse;
    110   int i, j;
    111 
    112   int right_overflow =
    113       (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0;
    114   int bottom_overflow =
    115       (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0;
    116 
    117   const int bw = MI_SIZE * mi_size_wide[bs] - right_overflow;
    118   const int bh = MI_SIZE * mi_size_high[bs] - bottom_overflow;
    119 
    120   aom_clear_system_state();
    121 
    122   for (i = 0; i < bh; i += 4) {
    123     for (j = 0; j < bw; j += 4) {
    124       if (is_cur_buf_hbd(xd)) {
    125         var +=
    126             log(1.0 + cpi->fn_ptr[BLOCK_4X4].vf(
    127                           x->plane[0].src.buf + i * x->plane[0].src.stride + j,
    128                           x->plane[0].src.stride,
    129                           CONVERT_TO_BYTEPTR(av1_highbd_all_zeros), 0, &sse) /
    130                           16);
    131       } else {
    132         var +=
    133             log(1.0 + cpi->fn_ptr[BLOCK_4X4].vf(
    134                           x->plane[0].src.buf + i * x->plane[0].src.stride + j,
    135                           x->plane[0].src.stride, av1_all_zeros, 0, &sse) /
    136                           16);
    137       }
    138     }
    139   }
    140   // Use average of 4x4 log variance. The range for 8 bit 0 - 9.704121561.
    141   var /= (bw / 4 * bh / 4);
    142   if (var > 7) var = 7;
    143 
    144   aom_clear_system_state();
    145   return (int)(var);
    146 }
    147 
    148 #define DEFAULT_E_MIDPOINT 10.0
    149 
    150 static unsigned int haar_ac_energy(MACROBLOCK *x, BLOCK_SIZE bs) {
    151   MACROBLOCKD *xd = &x->e_mbd;
    152   int stride = x->plane[0].src.stride;
    153   uint8_t *buf = x->plane[0].src.buf;
    154   const int bw = MI_SIZE * mi_size_wide[bs];
    155   const int bh = MI_SIZE * mi_size_high[bs];
    156   const int hbd = is_cur_buf_hbd(xd);
    157 
    158   int var = 0;
    159   for (int r = 0; r < bh; r += 8)
    160     for (int c = 0; c < bw; c += 8) {
    161       var += av1_haar_ac_sad_8x8_uint8_input(buf + c + r * stride, stride, hbd);
    162     }
    163 
    164   return (unsigned int)((uint64_t)var * 256) >> num_pels_log2_lookup[bs];
    165 }
    166 
    167 double av1_log_block_wavelet_energy(MACROBLOCK *x, BLOCK_SIZE bs) {
    168   unsigned int haar_sad = haar_ac_energy(x, bs);
    169   aom_clear_system_state();
    170   return log(haar_sad + 1.0);
    171 }
    172 
    173 int av1_block_wavelet_energy_level(const AV1_COMP *cpi, MACROBLOCK *x,
    174                                    BLOCK_SIZE bs) {
    175   double energy, energy_midpoint;
    176   aom_clear_system_state();
    177   energy_midpoint = (cpi->oxcf.pass == 2) ? cpi->twopass.frame_avg_haar_energy
    178                                           : DEFAULT_E_MIDPOINT;
    179   energy = av1_log_block_wavelet_energy(x, bs) - energy_midpoint;
    180   return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX);
    181 }
    182 
    183 int av1_compute_deltaq_from_energy_level(const AV1_COMP *const cpi,
    184                                          int block_var_level) {
    185   int rate_level;
    186   const AV1_COMMON *const cm = &cpi->common;
    187 
    188   if (DELTAQ_MODULATION == 1) {
    189     ENERGY_IN_BOUNDS(block_var_level);
    190     rate_level = SEGMENT_ID(block_var_level);
    191   } else {
    192     rate_level = block_var_level;
    193   }
    194   int qindex_delta = av1_compute_qdelta_by_rate(
    195       &cpi->rc, cm->current_frame.frame_type, cm->base_qindex,
    196       deltaq_rate_ratio[rate_level], cm->seq_params.bit_depth);
    197 
    198   if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
    199     qindex_delta = -cm->base_qindex + 1;
    200   }
    201   return qindex_delta;
    202 }
    203