]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_aq_variance.c
Merge "[svc] Make size of empty frame to be 16x16 all the time"
[libvpx] / vp9 / encoder / vp9_aq_variance.c
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 "vpx_ports/mem.h"
14
15 #include "vp9/encoder/vp9_aq_variance.h"
16
17 #include "vp9/common/vp9_seg_common.h"
18
19 #include "vp9/encoder/vp9_ratectrl.h"
20 #include "vp9/encoder/vp9_rd.h"
21 #include "vp9/encoder/vp9_segmentation.h"
22 #include "vp9/common/vp9_systemdependent.h"
23
24 #define ENERGY_MIN (-4)
25 #define ENERGY_MAX (1)
26 #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN +  1)
27 #define ENERGY_IN_BOUNDS(energy)\
28   assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX)
29
30 static const double rate_ratio[MAX_SEGMENTS] =
31   {2.5, 2.0, 1.5, 1.0, 0.75, 1.0, 1.0, 1.0};
32 static const int segment_id[ENERGY_SPAN] = {0, 1, 1, 2, 3, 4};
33
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 #if CONFIG_VP9_HIGHBITDEPTH
38 DECLARE_ALIGNED(16, static const uint16_t, vp9_highbd_64_zeros[64]) = {0};
39 #endif
40
41 unsigned int vp9_vaq_segment_id(int energy) {
42   ENERGY_IN_BOUNDS(energy);
43   return SEGMENT_ID(energy);
44 }
45
46 void vp9_vaq_frame_setup(VP9_COMP *cpi) {
47   VP9_COMMON *cm = &cpi->common;
48   struct segmentation *seg = &cm->seg;
49   int i;
50
51   if (cm->frame_type == KEY_FRAME ||
52       cpi->refresh_alt_ref_frame ||
53       (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
54     vp9_enable_segmentation(seg);
55     vp9_clearall_segfeatures(seg);
56
57     seg->abs_delta = SEGMENT_DELTADATA;
58
59     vp9_clear_system_state();
60
61     for (i = 0; i < MAX_SEGMENTS; ++i) {
62       int qindex_delta =
63           vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex,
64                                      rate_ratio[i], cm->bit_depth);
65
66       // We don't allow qindex 0 in a segment if the base value is not 0.
67       // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment
68       // Q delta is sometimes applied without going back around the rd loop.
69       // This could lead to an illegal combination of partition size and q.
70       if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
71         qindex_delta = -cm->base_qindex + 1;
72       }
73
74       // No need to enable SEG_LVL_ALT_Q for this segment.
75       if (rate_ratio[i] == 1.0) {
76         continue;
77       }
78
79       vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta);
80       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
81     }
82   }
83 }
84
85
86 static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x,
87                                    BLOCK_SIZE bs) {
88   MACROBLOCKD *xd = &x->e_mbd;
89   unsigned int var, sse;
90   int right_overflow = (xd->mb_to_right_edge < 0) ?
91       ((-xd->mb_to_right_edge) >> 3) : 0;
92   int bottom_overflow = (xd->mb_to_bottom_edge < 0) ?
93       ((-xd->mb_to_bottom_edge) >> 3) : 0;
94
95   if (right_overflow || bottom_overflow) {
96     const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow;
97     const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow;
98     int avg;
99 #if CONFIG_VP9_HIGHBITDEPTH
100     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
101       highbd_8_variance(x->plane[0].src.buf, x->plane[0].src.stride,
102                         CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros), 0, bw, bh,
103                         &sse, &avg);
104       sse >>= 2 * (xd->bd - 8);
105       avg >>= (xd->bd - 8);
106     } else {
107       variance(x->plane[0].src.buf, x->plane[0].src.stride,
108                vp9_64_zeros, 0, bw, bh, &sse, &avg);
109     }
110 #else
111     variance(x->plane[0].src.buf, x->plane[0].src.stride,
112              vp9_64_zeros, 0, bw, bh, &sse, &avg);
113 #endif  // CONFIG_VP9_HIGHBITDEPTH
114     var = sse - (((int64_t)avg * avg) / (bw * bh));
115     return (256 * var) / (bw * bh);
116   } else {
117 #if CONFIG_VP9_HIGHBITDEPTH
118     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
119       var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
120                                x->plane[0].src.stride,
121                                CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros),
122                                0, &sse);
123     } else {
124       var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
125                                x->plane[0].src.stride,
126                                vp9_64_zeros, 0, &sse);
127     }
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 #endif  // CONFIG_VP9_HIGHBITDEPTH
133     return (256 * var) >> num_pels_log2_lookup[bs];
134   }
135 }
136
137 double vp9_log_block_var(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
138   unsigned int var = block_variance(cpi, x, bs);
139   vp9_clear_system_state();
140   return log(var + 1.0);
141 }
142
143 #define DEFAULT_E_MIDPOINT 10.0
144 int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
145   double energy;
146   double energy_midpoint;
147   vp9_clear_system_state();
148   energy_midpoint =
149     (cpi->oxcf.pass == 2) ? cpi->twopass.mb_av_energy : DEFAULT_E_MIDPOINT;
150   energy = vp9_log_block_var(cpi, x, bs) - energy_midpoint;
151   return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX);
152 }