| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <math.h> | 11 #include <math.h> |
| 12 | 12 |
| 13 #include "vp9/encoder/vp9_aq_variance.h" | 13 #include "vp9/encoder/vp9_aq_variance.h" |
| 14 | 14 |
| 15 #include "vp9/common/vp9_seg_common.h" | 15 #include "vp9/common/vp9_seg_common.h" |
| 16 | 16 |
| 17 #include "vp9/encoder/vp9_ratectrl.h" | 17 #include "vp9/encoder/vp9_ratectrl.h" |
| 18 #include "vp9/encoder/vp9_rd.h" | 18 #include "vp9/encoder/vp9_rd.h" |
| 19 #include "vp9/encoder/vp9_segmentation.h" | 19 #include "vp9/encoder/vp9_segmentation.h" |
| 20 #include "vp9/common/vp9_systemdependent.h" | 20 #include "vp9/common/vp9_systemdependent.h" |
| 21 | 21 |
| 22 #define ENERGY_MIN (-1) | 22 #define ENERGY_MIN (-1) |
| 23 #define ENERGY_MAX (1) | 23 #define ENERGY_MAX (1) |
| 24 #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1) | 24 #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1) |
| 25 #define ENERGY_IN_BOUNDS(energy)\ | 25 #define ENERGY_IN_BOUNDS(energy)\ |
| 26 assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX) | 26 assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX) |
| 27 | 27 |
| 28 static double q_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 }; | 28 static const double rate_ratio[MAX_SEGMENTS] = |
| 29 static double rdmult_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 }; | 29 {1.143, 1.0, 0.875, 1.0, 1.0, 1.0, 1.0, 1.0}; |
| 30 static int segment_id[MAX_SEGMENTS] = { 5, 3, 1, 0, 2, 4, 6, 7 }; | 30 static const int segment_id[ENERGY_SPAN] = {0, 1, 2}; |
| 31 | 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] | 32 #define SEGMENT_ID(i) segment_id[(i) - ENERGY_MIN] |
| 35 | 33 |
| 36 DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = {0}; | 34 DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = {0}; |
| 37 #if CONFIG_VP9_HIGHBITDEPTH | 35 #if CONFIG_VP9_HIGHBITDEPTH |
| 38 DECLARE_ALIGNED(16, static const uint16_t, vp9_highbd_64_zeros[64]) = {0}; | 36 DECLARE_ALIGNED(16, static const uint16_t, vp9_highbd_64_zeros[64]) = {0}; |
| 39 #endif | 37 #endif |
| 40 | 38 |
| 41 unsigned int vp9_vaq_segment_id(int energy) { | 39 unsigned int vp9_vaq_segment_id(int energy) { |
| 42 ENERGY_IN_BOUNDS(energy); | 40 ENERGY_IN_BOUNDS(energy); |
| 43 | |
| 44 return SEGMENT_ID(energy); | 41 return SEGMENT_ID(energy); |
| 45 } | 42 } |
| 46 | 43 |
| 47 double vp9_vaq_rdmult_ratio(int energy) { | |
| 48 ENERGY_IN_BOUNDS(energy); | |
| 49 | |
| 50 vp9_clear_system_state(); | |
| 51 | |
| 52 return RDMULT_RATIO(energy); | |
| 53 } | |
| 54 | |
| 55 double vp9_vaq_inv_q_ratio(int energy) { | |
| 56 ENERGY_IN_BOUNDS(energy); | |
| 57 | |
| 58 vp9_clear_system_state(); | |
| 59 | |
| 60 return Q_RATIO(-energy); | |
| 61 } | |
| 62 | |
| 63 void vp9_vaq_init() { | |
| 64 int i; | |
| 65 double base_ratio; | |
| 66 | |
| 67 assert(ENERGY_SPAN <= MAX_SEGMENTS); | |
| 68 | |
| 69 vp9_clear_system_state(); | |
| 70 | |
| 71 base_ratio = 1.5; | |
| 72 | |
| 73 for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) { | |
| 74 Q_RATIO(i) = pow(base_ratio, i/3.0); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void vp9_vaq_frame_setup(VP9_COMP *cpi) { | 44 void vp9_vaq_frame_setup(VP9_COMP *cpi) { |
| 79 VP9_COMMON *cm = &cpi->common; | 45 VP9_COMMON *cm = &cpi->common; |
| 80 struct segmentation *seg = &cm->seg; | 46 struct segmentation *seg = &cm->seg; |
| 81 const double base_q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth); | |
| 82 const int base_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + | |
| 83 cm->y_dc_delta_q); | |
| 84 int i; | 47 int i; |
| 85 | 48 |
| 86 if (cm->frame_type == KEY_FRAME || | 49 if (cm->frame_type == KEY_FRAME || |
| 87 cpi->refresh_alt_ref_frame || | 50 cpi->refresh_alt_ref_frame || |
| 88 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) { | 51 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) { |
| 89 vp9_enable_segmentation(seg); | 52 vp9_enable_segmentation(seg); |
| 90 vp9_clearall_segfeatures(seg); | 53 vp9_clearall_segfeatures(seg); |
| 91 | 54 |
| 92 seg->abs_delta = SEGMENT_DELTADATA; | 55 seg->abs_delta = SEGMENT_DELTADATA; |
| 93 | 56 |
| 94 vp9_clear_system_state(); | 57 vp9_clear_system_state(); |
| 95 | 58 |
| 96 for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) { | 59 for (i = 0; i < MAX_SEGMENTS; ++i) { |
| 97 int qindex_delta, segment_rdmult; | 60 int qindex_delta = |
| 61 vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex, |
| 62 rate_ratio[i], cm->bit_depth); |
| 98 | 63 |
| 99 if (Q_RATIO(i) == 1) { | 64 // We don't allow qindex 0 in a segment if the base value is not 0. |
| 100 // No need to enable SEG_LVL_ALT_Q for this segment | 65 // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment |
| 101 RDMULT_RATIO(i) = 1; | 66 // Q delta is sometimes applied without going back around the rd loop. |
| 67 // This could lead to an illegal combination of partition size and q. |
| 68 if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) { |
| 69 qindex_delta = -cm->base_qindex + 1; |
| 70 } |
| 71 |
| 72 // No need to enable SEG_LVL_ALT_Q for this segment. |
| 73 if (rate_ratio[i] == 1.0) { |
| 102 continue; | 74 continue; |
| 103 } | 75 } |
| 104 | 76 |
| 105 qindex_delta = vp9_compute_qdelta(&cpi->rc, base_q, base_q * Q_RATIO(i), | 77 vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta); |
| 106 cm->bit_depth); | 78 vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q); |
| 107 vp9_set_segdata(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q, qindex_delta); | |
| 108 vp9_enable_segfeature(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q); | |
| 109 | |
| 110 segment_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + qindex_delta + | |
| 111 cm->y_dc_delta_q); | |
| 112 | |
| 113 RDMULT_RATIO(i) = (double) segment_rdmult / base_rdmult; | |
| 114 } | 79 } |
| 115 } | 80 } |
| 116 } | 81 } |
| 117 | 82 |
| 118 | 83 |
| 119 static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x, | 84 static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x, |
| 120 BLOCK_SIZE bs) { | 85 BLOCK_SIZE bs) { |
| 121 MACROBLOCKD *xd = &x->e_mbd; | 86 MACROBLOCKD *xd = &x->e_mbd; |
| 122 unsigned int var, sse; | 87 unsigned int var, sse; |
| 123 int right_overflow = (xd->mb_to_right_edge < 0) ? | 88 int right_overflow = (xd->mb_to_right_edge < 0) ? |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 } | 125 } |
| 161 #else | 126 #else |
| 162 var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, | 127 var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, |
| 163 x->plane[0].src.stride, | 128 x->plane[0].src.stride, |
| 164 vp9_64_zeros, 0, &sse); | 129 vp9_64_zeros, 0, &sse); |
| 165 #endif // CONFIG_VP9_HIGHBITDEPTH | 130 #endif // CONFIG_VP9_HIGHBITDEPTH |
| 166 return (256 * var) >> num_pels_log2_lookup[bs]; | 131 return (256 * var) >> num_pels_log2_lookup[bs]; |
| 167 } | 132 } |
| 168 } | 133 } |
| 169 | 134 |
| 135 double vp9_log_block_var(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) { |
| 136 unsigned int var = block_variance(cpi, x, bs); |
| 137 vp9_clear_system_state(); |
| 138 return log(var + 1.0); |
| 139 } |
| 140 |
| 141 #define DEFAULT_E_MIDPOINT 10.0 |
| 170 int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) { | 142 int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) { |
| 171 double energy; | 143 double energy; |
| 172 unsigned int var = block_variance(cpi, x, bs); | 144 double energy_midpoint; |
| 173 | |
| 174 vp9_clear_system_state(); | 145 vp9_clear_system_state(); |
| 175 | 146 energy_midpoint = |
| 176 energy = 0.9 * (log(var + 1.0) - 10.0); | 147 (cpi->oxcf.pass == 2) ? cpi->twopass.mb_av_energy : DEFAULT_E_MIDPOINT; |
| 148 energy = vp9_log_block_var(cpi, x, bs) - energy_midpoint; |
| 177 return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX); | 149 return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX); |
| 178 } | 150 } |
| OLD | NEW |