Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2013 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 
     13 #include "vp9/encoder/vp9_vaq.h"
     14 
     15 #include "vp9/common/vp9_seg_common.h"
     16 
     17 #include "vp9/encoder/vp9_ratectrl.h"
     18 #include "vp9/encoder/vp9_rdopt.h"
     19 #include "vp9/encoder/vp9_segmentation.h"
     20 #include "vp9/common/vp9_systemdependent.h"
     21 
     22 #define ENERGY_MIN (-3)
     23 #define ENERGY_MAX (3)
     24 #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN +  1)
     25 #define ENERGY_IN_BOUNDS(energy)\
     26   assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX)
     27 
     28 static double q_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
     29 static double rdmult_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
     30 static int segment_id[MAX_SEGMENTS] = { 5, 3, 1, 0, 2, 4, 6, 7 };
     31 
     32 #define Q_RATIO(i) q_ratio[(i) - ENERGY_MIN]
     33 #define RDMULT_RATIO(i) rdmult_ratio[(i) - ENERGY_MIN]
     34 #define SEGMENT_ID(i) segment_id[(i) - ENERGY_MIN]
     35 
     36 DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = {0};
     37 
     38 unsigned int vp9_vaq_segment_id(int energy) {
     39   ENERGY_IN_BOUNDS(energy);
     40 
     41   return SEGMENT_ID(energy);
     42 }
     43 
     44 double vp9_vaq_rdmult_ratio(int energy) {
     45   ENERGY_IN_BOUNDS(energy);
     46 
     47   vp9_clear_system_state();  // __asm emms;
     48 
     49   return RDMULT_RATIO(energy);
     50 }
     51 
     52 double vp9_vaq_inv_q_ratio(int energy) {
     53   ENERGY_IN_BOUNDS(energy);
     54 
     55   vp9_clear_system_state();  // __asm emms;
     56 
     57   return Q_RATIO(-energy);
     58 }
     59 
     60 void vp9_vaq_init() {
     61   int i;
     62   double base_ratio;
     63 
     64   assert(ENERGY_SPAN <= MAX_SEGMENTS);
     65 
     66   vp9_clear_system_state();  // __asm emms;
     67 
     68   base_ratio = 1.8;
     69 
     70   for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) {
     71     Q_RATIO(i) = pow(base_ratio, i/3.0);
     72   }
     73 }
     74 
     75 void vp9_vaq_frame_setup(VP9_COMP *cpi) {
     76   VP9_COMMON *cm = &cpi->common;
     77   struct segmentation *seg = &cm->seg;
     78   int base_q = vp9_convert_qindex_to_q(cm->base_qindex);
     79   int base_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex +
     80                                         cm->y_dc_delta_q);
     81   int i;
     82 
     83   vp9_enable_segmentation((VP9_PTR)cpi);
     84   vp9_clearall_segfeatures(seg);
     85 
     86   seg->abs_delta = SEGMENT_DELTADATA;
     87 
     88   vp9_clear_system_state();  // __asm emms;
     89 
     90   for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) {
     91     int qindex_delta, segment_rdmult;
     92 
     93     if (Q_RATIO(i) == 1) {
     94       // No need to enable SEG_LVL_ALT_Q for this segment
     95       RDMULT_RATIO(i) = 1;
     96       continue;
     97     }
     98 
     99     qindex_delta = vp9_compute_qdelta(cpi, base_q, base_q * Q_RATIO(i));
    100     vp9_set_segdata(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q, qindex_delta);
    101     vp9_enable_segfeature(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q);
    102 
    103     segment_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + qindex_delta +
    104                                          cm->y_dc_delta_q);
    105 
    106     RDMULT_RATIO(i) = (double) segment_rdmult / base_rdmult;
    107   }
    108 }
    109 
    110 
    111 static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x,
    112                                    BLOCK_SIZE bs) {
    113   MACROBLOCKD *xd = &x->e_mbd;
    114   unsigned int var, sse;
    115   int right_overflow = (xd->mb_to_right_edge < 0) ?
    116       ((-xd->mb_to_right_edge) >> 3) : 0;
    117   int bottom_overflow = (xd->mb_to_bottom_edge < 0) ?
    118       ((-xd->mb_to_bottom_edge) >> 3) : 0;
    119 
    120   if (right_overflow || bottom_overflow) {
    121     const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow;
    122     const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow;
    123     int avg;
    124     variance(x->plane[0].src.buf, x->plane[0].src.stride,
    125              vp9_64_zeros, 0, bw, bh, &sse, &avg);
    126     var = sse - (((int64_t)avg * avg) / (bw * bh));
    127     return (256 * var) / (bw * bh);
    128   } else {
    129     var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
    130                              x->plane[0].src.stride,
    131                              vp9_64_zeros, 0, &sse);
    132     return (256 * var) >> num_pels_log2_lookup[bs];
    133   }
    134 }
    135 
    136 int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
    137   double energy;
    138   unsigned int var = block_variance(cpi, x, bs);
    139 
    140   vp9_clear_system_state();  // __asm emms;
    141 
    142   // if (var <= 1000)
    143   //   return 0;
    144 
    145   energy = 0.9*(logf(var + 1) - 10.0);
    146   return clamp(round(energy), ENERGY_MIN, ENERGY_MAX);
    147 }
    148