OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 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 | 11 |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <string.h> | 14 #include <string.h> |
15 #include <limits.h> | 15 #include <limits.h> |
16 #include <assert.h> | 16 #include <assert.h> |
17 #include <math.h> | 17 #include <math.h> |
18 | 18 |
19 #include "vp9/common/vp9_alloccommon.h" | 19 #include "vp9/common/vp9_alloccommon.h" |
20 #include "vp9/common/vp9_common.h" | 20 #include "vp9/common/vp9_common.h" |
21 #include "vp9/encoder/vp9_ratectrl.h" | 21 #include "vp9/encoder/vp9_ratectrl.h" |
22 #include "vp9/common/vp9_entropymode.h" | 22 #include "vp9/common/vp9_entropymode.h" |
23 #include "vpx_mem/vpx_mem.h" | 23 #include "vpx_mem/vpx_mem.h" |
24 #include "vp9/common/vp9_systemdependent.h" | 24 #include "vp9/common/vp9_systemdependent.h" |
25 #include "vp9/encoder/vp9_encodemv.h" | 25 #include "vp9/encoder/vp9_encodemv.h" |
26 #include "vp9/common/vp9_quant_common.h" | 26 #include "vp9/common/vp9_quant_common.h" |
27 #include "vp9/common/vp9_seg_common.h" | 27 #include "vp9/common/vp9_seg_common.h" |
28 | 28 |
| 29 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1 |
| 30 |
29 #define MIN_BPB_FACTOR 0.005 | 31 #define MIN_BPB_FACTOR 0.005 |
30 #define MAX_BPB_FACTOR 50 | 32 #define MAX_BPB_FACTOR 50 |
31 | 33 |
32 // Bits Per MB at different Q (Multiplied by 512) | 34 // Bits Per MB at different Q (Multiplied by 512) |
33 #define BPER_MB_NORMBITS 9 | 35 #define BPER_MB_NORMBITS 9 |
34 | 36 |
35 static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = | 37 static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = |
36 { 1, 2, 3, 4, 5 }; | 38 { 1, 2, 3, 4, 5 }; |
37 | 39 |
| 40 // Tables relating active max Q to active min Q |
| 41 static int kf_low_motion_minq[QINDEX_RANGE]; |
| 42 static int kf_high_motion_minq[QINDEX_RANGE]; |
| 43 static int gf_low_motion_minq[QINDEX_RANGE]; |
| 44 static int gf_high_motion_minq[QINDEX_RANGE]; |
| 45 static int inter_minq[QINDEX_RANGE]; |
| 46 static int afq_low_motion_minq[QINDEX_RANGE]; |
| 47 static int afq_high_motion_minq[QINDEX_RANGE]; |
| 48 static int gf_high = 2000; |
| 49 static int gf_low = 400; |
| 50 static int kf_high = 5000; |
| 51 static int kf_low = 400; |
| 52 |
| 53 // Functions to compute the active minq lookup table entries based on a |
| 54 // formulaic approach to facilitate easier adjustment of the Q tables. |
| 55 // The formulae were derived from computing a 3rd order polynomial best |
| 56 // fit to the original data (after plotting real maxq vs minq (not q index)) |
| 57 static int calculate_minq_index(double maxq, |
| 58 double x3, double x2, double x1, double c) { |
| 59 int i; |
| 60 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c, |
| 61 maxq); |
| 62 |
| 63 // Special case handling to deal with the step from q2.0 |
| 64 // down to lossless mode represented by q 1.0. |
| 65 if (minqtarget <= 2.0) |
| 66 return 0; |
| 67 |
| 68 for (i = 0; i < QINDEX_RANGE; i++) { |
| 69 if (minqtarget <= vp9_convert_qindex_to_q(i)) |
| 70 return i; |
| 71 } |
| 72 |
| 73 return QINDEX_RANGE - 1; |
| 74 } |
| 75 |
| 76 void vp9_rc_init_minq_luts(void) { |
| 77 int i; |
| 78 |
| 79 for (i = 0; i < QINDEX_RANGE; i++) { |
| 80 const double maxq = vp9_convert_qindex_to_q(i); |
| 81 |
| 82 |
| 83 kf_low_motion_minq[i] = calculate_minq_index(maxq, |
| 84 0.000001, |
| 85 -0.0004, |
| 86 0.15, |
| 87 0.0); |
| 88 kf_high_motion_minq[i] = calculate_minq_index(maxq, |
| 89 0.000002, |
| 90 -0.0012, |
| 91 0.50, |
| 92 0.0); |
| 93 |
| 94 gf_low_motion_minq[i] = calculate_minq_index(maxq, |
| 95 0.0000015, |
| 96 -0.0009, |
| 97 0.32, |
| 98 0.0); |
| 99 gf_high_motion_minq[i] = calculate_minq_index(maxq, |
| 100 0.0000021, |
| 101 -0.00125, |
| 102 0.50, |
| 103 0.0); |
| 104 afq_low_motion_minq[i] = calculate_minq_index(maxq, |
| 105 0.0000015, |
| 106 -0.0009, |
| 107 0.33, |
| 108 0.0); |
| 109 afq_high_motion_minq[i] = calculate_minq_index(maxq, |
| 110 0.0000021, |
| 111 -0.00125, |
| 112 0.55, |
| 113 0.0); |
| 114 inter_minq[i] = calculate_minq_index(maxq, |
| 115 0.00000271, |
| 116 -0.00113, |
| 117 0.75, |
| 118 0.0); |
| 119 } |
| 120 } |
| 121 |
38 // These functions use formulaic calculations to make playing with the | 122 // These functions use formulaic calculations to make playing with the |
39 // quantizer tables easier. If necessary they can be replaced by lookup | 123 // quantizer tables easier. If necessary they can be replaced by lookup |
40 // tables if and when things settle down in the experimental bitstream | 124 // tables if and when things settle down in the experimental bitstream |
41 double vp9_convert_qindex_to_q(int qindex) { | 125 double vp9_convert_qindex_to_q(int qindex) { |
42 // Convert the index to a real Q value (scaled down to match old Q values) | 126 // Convert the index to a real Q value (scaled down to match old Q values) |
43 return vp9_ac_quant(qindex, 0) / 4.0; | 127 return vp9_ac_quant(qindex, 0) / 4.0; |
44 } | 128 } |
45 | 129 |
46 int vp9_gfboost_qadjust(int qindex) { | 130 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
47 const double q = vp9_convert_qindex_to_q(qindex); | 131 double correction_factor) { |
48 return (int)((0.00000828 * q * q * q) + | |
49 (-0.0055 * q * q) + | |
50 (1.32 * q) + 79.3); | |
51 } | |
52 | |
53 static int kfboost_qadjust(int qindex) { | |
54 const double q = vp9_convert_qindex_to_q(qindex); | |
55 return (int)((0.00000973 * q * q * q) + | |
56 (-0.00613 * q * q) + | |
57 (1.316 * q) + 121.2); | |
58 } | |
59 | |
60 int vp9_bits_per_mb(FRAME_TYPE frame_type, int qindex, | |
61 double correction_factor) { | |
62 const double q = vp9_convert_qindex_to_q(qindex); | 132 const double q = vp9_convert_qindex_to_q(qindex); |
63 int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; | 133 int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; |
64 | 134 |
65 // q based adjustment to baseline enumerator | 135 // q based adjustment to baseline enumerator |
66 enumerator += (int)(enumerator * q) >> 12; | 136 enumerator += (int)(enumerator * q) >> 12; |
67 return (int)(0.5 + (enumerator * correction_factor / q)); | 137 return (int)(0.5 + (enumerator * correction_factor / q)); |
68 } | 138 } |
69 | 139 |
70 void vp9_save_coding_context(VP9_COMP *cpi) { | 140 void vp9_save_coding_context(VP9_COMP *cpi) { |
71 CODING_CONTEXT *const cc = &cpi->coding_context; | 141 CODING_CONTEXT *const cc = &cpi->coding_context; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 181 |
112 cm->fc = cc->fc; | 182 cm->fc = cc->fc; |
113 } | 183 } |
114 | 184 |
115 void vp9_setup_key_frame(VP9_COMP *cpi) { | 185 void vp9_setup_key_frame(VP9_COMP *cpi) { |
116 VP9_COMMON *cm = &cpi->common; | 186 VP9_COMMON *cm = &cpi->common; |
117 | 187 |
118 vp9_setup_past_independence(cm); | 188 vp9_setup_past_independence(cm); |
119 | 189 |
120 // interval before next GF | 190 // interval before next GF |
121 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; | 191 cpi->rc.frames_till_gf_update_due = cpi->rc.baseline_gf_interval; |
122 /* All buffers are implicitly updated on key frames. */ | 192 /* All buffers are implicitly updated on key frames. */ |
123 cpi->refresh_golden_frame = 1; | 193 cpi->refresh_golden_frame = 1; |
124 cpi->refresh_alt_ref_frame = 1; | 194 cpi->refresh_alt_ref_frame = 1; |
125 } | 195 } |
126 | 196 |
127 void vp9_setup_inter_frame(VP9_COMP *cpi) { | 197 void vp9_setup_inter_frame(VP9_COMP *cpi) { |
128 VP9_COMMON *cm = &cpi->common; | 198 VP9_COMMON *cm = &cpi->common; |
129 if (cm->error_resilient_mode || cm->intra_only) | 199 if (cm->error_resilient_mode || cm->intra_only) |
130 vp9_setup_past_independence(cm); | 200 vp9_setup_past_independence(cm); |
131 | 201 |
132 assert(cm->frame_context_idx < NUM_FRAME_CONTEXTS); | 202 assert(cm->frame_context_idx < FRAME_CONTEXTS); |
133 cm->fc = cm->frame_contexts[cm->frame_context_idx]; | 203 cm->fc = cm->frame_contexts[cm->frame_context_idx]; |
134 } | 204 } |
135 | 205 |
136 static int estimate_bits_at_q(int frame_kind, int q, int mbs, | 206 static int estimate_bits_at_q(int frame_kind, int q, int mbs, |
137 double correction_factor) { | 207 double correction_factor) { |
138 const int bpm = (int)(vp9_bits_per_mb(frame_kind, q, correction_factor)); | 208 const int bpm = (int)(vp9_rc_bits_per_mb(frame_kind, q, correction_factor)); |
139 | 209 |
140 // Attempt to retain reasonable accuracy without overflow. The cutoff is | 210 // Attempt to retain reasonable accuracy without overflow. The cutoff is |
141 // chosen such that the maximum product of Bpm and MBs fits 31 bits. The | 211 // chosen such that the maximum product of Bpm and MBs fits 31 bits. The |
142 // largest Bpm takes 20 bits. | 212 // largest Bpm takes 20 bits. |
143 return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs | 213 return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs |
144 : (bpm * mbs) >> BPER_MB_NORMBITS; | 214 : (bpm * mbs) >> BPER_MB_NORMBITS; |
145 } | 215 } |
146 | 216 |
147 | 217 |
148 static void calc_iframe_target_size(VP9_COMP *cpi) { | 218 static void calc_iframe_target_size(VP9_COMP *cpi) { |
149 // boost defaults to half second | 219 // boost defaults to half second |
150 int target; | 220 int target; |
151 | 221 |
152 // Clear down mmx registers to allow floating point in what follows | 222 // Clear down mmx registers to allow floating point in what follows |
153 vp9_clear_system_state(); // __asm emms; | 223 vp9_clear_system_state(); // __asm emms; |
154 | 224 |
155 // New Two pass RC | 225 // New Two pass RC |
156 target = cpi->per_frame_bandwidth; | 226 target = cpi->rc.per_frame_bandwidth; |
157 | 227 |
158 if (cpi->oxcf.rc_max_intra_bitrate_pct) { | 228 if (cpi->oxcf.rc_max_intra_bitrate_pct) { |
159 int max_rate = cpi->per_frame_bandwidth | 229 int max_rate = cpi->rc.per_frame_bandwidth |
160 * cpi->oxcf.rc_max_intra_bitrate_pct / 100; | 230 * cpi->oxcf.rc_max_intra_bitrate_pct / 100; |
161 | 231 |
162 if (target > max_rate) | 232 if (target > max_rate) |
163 target = max_rate; | 233 target = max_rate; |
164 } | 234 } |
165 | 235 cpi->rc.this_frame_target = target; |
166 cpi->this_frame_target = target; | |
167 } | 236 } |
168 | 237 |
169 | |
170 // Do the best we can to define the parameters for the next GF based | 238 // Do the best we can to define the parameters for the next GF based |
171 // on what information we have available. | 239 // on what information we have available. |
172 // | 240 // |
173 // In this experimental code only two pass is supported | 241 // In this experimental code only two pass is supported |
174 // so we just use the interval determined in the two pass code. | 242 // so we just use the interval determined in the two pass code. |
175 static void calc_gf_params(VP9_COMP *cpi) { | 243 static void calc_gf_params(VP9_COMP *cpi) { |
176 // Set the gf interval | 244 // Set the gf interval |
177 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; | 245 cpi->rc.frames_till_gf_update_due = cpi->rc.baseline_gf_interval; |
178 } | 246 } |
179 | 247 |
180 | 248 |
181 static void calc_pframe_target_size(VP9_COMP *cpi) { | 249 static void calc_pframe_target_size(VP9_COMP *cpi) { |
182 const int min_frame_target = MAX(cpi->min_frame_bandwidth, | 250 const int min_frame_target = MAX(cpi->rc.min_frame_bandwidth, |
183 cpi->av_per_frame_bandwidth >> 5); | 251 cpi->rc.av_per_frame_bandwidth >> 5); |
184 if (cpi->refresh_alt_ref_frame) { | 252 if (cpi->refresh_alt_ref_frame) { |
185 // Special alt reference frame case | 253 // Special alt reference frame case |
186 // Per frame bit target for the alt ref frame | 254 // Per frame bit target for the alt ref frame |
187 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; | 255 cpi->rc.per_frame_bandwidth = cpi->twopass.gf_bits; |
188 cpi->this_frame_target = cpi->per_frame_bandwidth; | 256 cpi->rc.this_frame_target = cpi->rc.per_frame_bandwidth; |
189 } else { | 257 } else { |
190 // Normal frames (gf,and inter) | 258 // Normal frames (gf,and inter) |
191 cpi->this_frame_target = cpi->per_frame_bandwidth; | 259 cpi->rc.this_frame_target = cpi->rc.per_frame_bandwidth; |
192 } | 260 } |
193 | 261 |
194 // Check that the total sum of adjustments is not above the maximum allowed. | 262 // Check that the total sum of adjustments is not above the maximum allowed. |
195 // That is, having allowed for the KF and GF penalties, we have not pushed | 263 // That is, having allowed for the KF and GF penalties, we have not pushed |
196 // the current inter-frame target too low. If the adjustment we apply here is | 264 // the current inter-frame target too low. If the adjustment we apply here is |
197 // not capable of recovering all the extra bits we have spent in the KF or GF, | 265 // not capable of recovering all the extra bits we have spent in the KF or GF, |
198 // then the remainder will have to be recovered over a longer time span via | 266 // then the remainder will have to be recovered over a longer time span via |
199 // other buffer / rate control mechanisms. | 267 // other buffer / rate control mechanisms. |
200 if (cpi->this_frame_target < min_frame_target) | 268 if (cpi->rc.this_frame_target < min_frame_target) |
201 cpi->this_frame_target = min_frame_target; | 269 cpi->rc.this_frame_target = min_frame_target; |
202 | |
203 if (!cpi->refresh_alt_ref_frame) | |
204 // Note the baseline target data rate for this inter frame. | |
205 cpi->inter_frame_target = cpi->this_frame_target; | |
206 | 270 |
207 // Adjust target frame size for Golden Frames: | 271 // Adjust target frame size for Golden Frames: |
208 if (cpi->frames_till_gf_update_due == 0) { | 272 if (cpi->rc.frames_till_gf_update_due == 0) { |
209 const int q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] | |
210 : cpi->oxcf.fixed_q; | |
211 | |
212 cpi->refresh_golden_frame = 1; | 273 cpi->refresh_golden_frame = 1; |
213 | |
214 calc_gf_params(cpi); | 274 calc_gf_params(cpi); |
215 | |
216 // If we are using alternate ref instead of gf then do not apply the boost | 275 // If we are using alternate ref instead of gf then do not apply the boost |
217 // It will instead be applied to the altref update | 276 // It will instead be applied to the altref update |
218 // Jims modified boost | 277 // Jims modified boost |
219 if (!cpi->source_alt_ref_active) { | 278 if (!cpi->source_alt_ref_active) { |
220 if (cpi->oxcf.fixed_q < 0) { | 279 // The spend on the GF is defined in the two pass code |
221 // The spend on the GF is defined in the two pass code | 280 // for two pass encodes |
222 // for two pass encodes | 281 cpi->rc.this_frame_target = cpi->rc.per_frame_bandwidth; |
223 cpi->this_frame_target = cpi->per_frame_bandwidth; | |
224 } else { | |
225 cpi->this_frame_target = | |
226 (estimate_bits_at_q(1, q, cpi->common.MBs, 1.0) | |
227 * cpi->last_boost) / 100; | |
228 } | |
229 } else { | 282 } else { |
230 // If there is an active ARF at this location use the minimum | 283 // If there is an active ARF at this location use the minimum |
231 // bits on this frame even if it is a constructed arf. | 284 // bits on this frame even if it is a constructed arf. |
232 // The active maximum quantizer insures that an appropriate | 285 // The active maximum quantizer insures that an appropriate |
233 // number of bits will be spent if needed for constructed ARFs. | 286 // number of bits will be spent if needed for constructed ARFs. |
234 cpi->this_frame_target = 0; | 287 cpi->rc.this_frame_target = 0; |
235 } | 288 } |
236 } | 289 } |
237 } | 290 } |
238 | 291 |
239 | 292 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { |
240 void vp9_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { | |
241 const int q = cpi->common.base_qindex; | 293 const int q = cpi->common.base_qindex; |
242 int correction_factor = 100; | 294 int correction_factor = 100; |
243 double rate_correction_factor; | 295 double rate_correction_factor; |
244 double adjustment_limit; | 296 double adjustment_limit; |
245 | 297 |
246 int projected_size_based_on_q = 0; | 298 int projected_size_based_on_q = 0; |
247 | 299 |
248 // Clear down mmx registers to allow floating point in what follows | 300 // Clear down mmx registers to allow floating point in what follows |
249 vp9_clear_system_state(); // __asm emms; | 301 vp9_clear_system_state(); // __asm emms; |
250 | 302 |
251 if (cpi->common.frame_type == KEY_FRAME) { | 303 if (cpi->common.frame_type == KEY_FRAME) { |
252 rate_correction_factor = cpi->key_frame_rate_correction_factor; | 304 rate_correction_factor = cpi->rc.key_frame_rate_correction_factor; |
253 } else { | 305 } else { |
254 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) | 306 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) |
255 rate_correction_factor = cpi->gf_rate_correction_factor; | 307 rate_correction_factor = cpi->rc.gf_rate_correction_factor; |
256 else | 308 else |
257 rate_correction_factor = cpi->rate_correction_factor; | 309 rate_correction_factor = cpi->rc.rate_correction_factor; |
258 } | 310 } |
259 | 311 |
260 // Work out how big we would have expected the frame to be at this Q given | 312 // Work out how big we would have expected the frame to be at this Q given |
261 // the current correction factor. | 313 // the current correction factor. |
262 // Stay in double to avoid int overflow when values are large | 314 // Stay in double to avoid int overflow when values are large |
263 projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, | 315 projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, |
264 cpi->common.MBs, | 316 cpi->common.MBs, |
265 rate_correction_factor); | 317 rate_correction_factor); |
266 | 318 |
267 // Work out a size correction factor. | 319 // Work out a size correction factor. |
268 if (projected_size_based_on_q > 0) | 320 if (projected_size_based_on_q > 0) |
269 correction_factor = | 321 correction_factor = |
270 (100 * cpi->projected_frame_size) / projected_size_based_on_q; | 322 (100 * cpi->rc.projected_frame_size) / projected_size_based_on_q; |
271 | 323 |
272 // More heavily damped adjustment used if we have been oscillating either side | 324 // More heavily damped adjustment used if we have been oscillating either side |
273 // of target. | 325 // of target. |
274 switch (damp_var) { | 326 switch (damp_var) { |
275 case 0: | 327 case 0: |
276 adjustment_limit = 0.75; | 328 adjustment_limit = 0.75; |
277 break; | 329 break; |
278 case 1: | 330 case 1: |
279 adjustment_limit = 0.375; | 331 adjustment_limit = 0.375; |
280 break; | 332 break; |
281 case 2: | 333 case 2: |
282 default: | 334 default: |
283 adjustment_limit = 0.25; | 335 adjustment_limit = 0.25; |
284 break; | 336 break; |
285 } | 337 } |
286 | 338 |
287 // if ( (correction_factor > 102) && (Q < cpi->active_worst_quality) ) | |
288 if (correction_factor > 102) { | 339 if (correction_factor > 102) { |
289 // We are not already at the worst allowable quality | 340 // We are not already at the worst allowable quality |
290 correction_factor = | 341 correction_factor = |
291 (int)(100 + ((correction_factor - 100) * adjustment_limit)); | 342 (int)(100 + ((correction_factor - 100) * adjustment_limit)); |
292 rate_correction_factor = | 343 rate_correction_factor = |
293 ((rate_correction_factor * correction_factor) / 100); | 344 ((rate_correction_factor * correction_factor) / 100); |
294 | 345 |
295 // Keep rate_correction_factor within limits | 346 // Keep rate_correction_factor within limits |
296 if (rate_correction_factor > MAX_BPB_FACTOR) | 347 if (rate_correction_factor > MAX_BPB_FACTOR) |
297 rate_correction_factor = MAX_BPB_FACTOR; | 348 rate_correction_factor = MAX_BPB_FACTOR; |
298 } else if (correction_factor < 99) { | 349 } else if (correction_factor < 99) { |
299 // We are not already at the best allowable quality | 350 // We are not already at the best allowable quality |
300 correction_factor = | 351 correction_factor = |
301 (int)(100 - ((100 - correction_factor) * adjustment_limit)); | 352 (int)(100 - ((100 - correction_factor) * adjustment_limit)); |
302 rate_correction_factor = | 353 rate_correction_factor = |
303 ((rate_correction_factor * correction_factor) / 100); | 354 ((rate_correction_factor * correction_factor) / 100); |
304 | 355 |
305 // Keep rate_correction_factor within limits | 356 // Keep rate_correction_factor within limits |
306 if (rate_correction_factor < MIN_BPB_FACTOR) | 357 if (rate_correction_factor < MIN_BPB_FACTOR) |
307 rate_correction_factor = MIN_BPB_FACTOR; | 358 rate_correction_factor = MIN_BPB_FACTOR; |
308 } | 359 } |
309 | 360 |
310 if (cpi->common.frame_type == KEY_FRAME) { | 361 if (cpi->common.frame_type == KEY_FRAME) { |
311 cpi->key_frame_rate_correction_factor = rate_correction_factor; | 362 cpi->rc.key_frame_rate_correction_factor = rate_correction_factor; |
312 } else { | 363 } else { |
313 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) | 364 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) |
314 cpi->gf_rate_correction_factor = rate_correction_factor; | 365 cpi->rc.gf_rate_correction_factor = rate_correction_factor; |
315 else | 366 else |
316 cpi->rate_correction_factor = rate_correction_factor; | 367 cpi->rc.rate_correction_factor = rate_correction_factor; |
317 } | 368 } |
318 } | 369 } |
319 | 370 |
320 | 371 |
321 int vp9_regulate_q(VP9_COMP *cpi, int target_bits_per_frame) { | 372 int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame, |
322 int q = cpi->active_worst_quality; | 373 int active_best_quality, int active_worst_quality) { |
| 374 int q = active_worst_quality; |
323 | 375 |
324 int i; | 376 int i; |
325 int last_error = INT_MAX; | 377 int last_error = INT_MAX; |
326 int target_bits_per_mb; | 378 int target_bits_per_mb; |
327 int bits_per_mb_at_this_q; | 379 int bits_per_mb_at_this_q; |
328 double correction_factor; | 380 double correction_factor; |
329 | 381 |
330 // Select the appropriate correction factor based upon type of frame. | 382 // Select the appropriate correction factor based upon type of frame. |
331 if (cpi->common.frame_type == KEY_FRAME) { | 383 if (cpi->common.frame_type == KEY_FRAME) { |
332 correction_factor = cpi->key_frame_rate_correction_factor; | 384 correction_factor = cpi->rc.key_frame_rate_correction_factor; |
333 } else { | 385 } else { |
334 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) | 386 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) |
335 correction_factor = cpi->gf_rate_correction_factor; | 387 correction_factor = cpi->rc.gf_rate_correction_factor; |
336 else | 388 else |
337 correction_factor = cpi->rate_correction_factor; | 389 correction_factor = cpi->rc.rate_correction_factor; |
338 } | 390 } |
339 | 391 |
340 // Calculate required scaling factor based on target frame size and size of | 392 // Calculate required scaling factor based on target frame size and size of |
341 // frame produced using previous Q. | 393 // frame produced using previous Q. |
342 if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) | 394 if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) |
343 target_bits_per_mb = | 395 target_bits_per_mb = |
344 (target_bits_per_frame / cpi->common.MBs) | 396 (target_bits_per_frame / cpi->common.MBs) |
345 << BPER_MB_NORMBITS; // Case where we would overflow int | 397 << BPER_MB_NORMBITS; // Case where we would overflow int |
346 else | 398 else |
347 target_bits_per_mb = | 399 target_bits_per_mb = |
348 (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs; | 400 (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs; |
349 | 401 |
350 i = cpi->active_best_quality; | 402 i = active_best_quality; |
351 | 403 |
352 do { | 404 do { |
353 bits_per_mb_at_this_q = (int)vp9_bits_per_mb(cpi->common.frame_type, i, | 405 bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cpi->common.frame_type, i, |
354 correction_factor); | 406 correction_factor); |
355 | 407 |
356 if (bits_per_mb_at_this_q <= target_bits_per_mb) { | 408 if (bits_per_mb_at_this_q <= target_bits_per_mb) { |
357 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) | 409 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) |
358 q = i; | 410 q = i; |
359 else | 411 else |
360 q = i - 1; | 412 q = i - 1; |
361 | 413 |
362 break; | 414 break; |
363 } else { | 415 } else { |
364 last_error = bits_per_mb_at_this_q - target_bits_per_mb; | 416 last_error = bits_per_mb_at_this_q - target_bits_per_mb; |
365 } | 417 } |
366 } while (++i <= cpi->active_worst_quality); | 418 } while (++i <= active_worst_quality); |
367 | 419 |
368 return q; | 420 return q; |
369 } | 421 } |
370 | 422 |
| 423 static int get_active_quality(int q, |
| 424 int gfu_boost, |
| 425 int low, |
| 426 int high, |
| 427 int *low_motion_minq, |
| 428 int *high_motion_minq) { |
| 429 int active_best_quality; |
| 430 if (gfu_boost > high) { |
| 431 active_best_quality = low_motion_minq[q]; |
| 432 } else if (gfu_boost < low) { |
| 433 active_best_quality = high_motion_minq[q]; |
| 434 } else { |
| 435 const int gap = high - low; |
| 436 const int offset = high - gfu_boost; |
| 437 const int qdiff = high_motion_minq[q] - low_motion_minq[q]; |
| 438 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; |
| 439 active_best_quality = low_motion_minq[q] + adjustment; |
| 440 } |
| 441 return active_best_quality; |
| 442 } |
| 443 |
| 444 int vp9_rc_pick_q_and_adjust_q_bounds(const VP9_COMP *cpi, |
| 445 int *bottom_index, |
| 446 int *top_index, |
| 447 int *top_index_prop) { |
| 448 const VP9_COMMON *const cm = &cpi->common; |
| 449 int active_best_quality; |
| 450 int active_worst_quality = cpi->rc.active_worst_quality; |
| 451 int q; |
| 452 |
| 453 if (frame_is_intra_only(cm)) { |
| 454 active_best_quality = cpi->rc.best_quality; |
| 455 #if !CONFIG_MULTIPLE_ARF |
| 456 // Handle the special case for key frames forced when we have75 reached |
| 457 // the maximum key frame interval. Here force the Q to a range |
| 458 // based on the ambient Q to reduce the risk of popping. |
| 459 if (cpi->this_key_frame_forced) { |
| 460 int delta_qindex; |
| 461 int qindex = cpi->rc.last_boosted_qindex; |
| 462 double last_boosted_q = vp9_convert_qindex_to_q(qindex); |
| 463 |
| 464 delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, |
| 465 (last_boosted_q * 0.75)); |
| 466 active_best_quality = MAX(qindex + delta_qindex, |
| 467 cpi->rc.best_quality); |
| 468 } else if (!(cpi->pass == 0 && cpi->common.current_video_frame == 0)) { |
| 469 // not first frame of one pass |
| 470 double q_adj_factor = 1.0; |
| 471 double q_val; |
| 472 |
| 473 // Baseline value derived from cpi->active_worst_quality and kf boost |
| 474 active_best_quality = get_active_quality(active_worst_quality, |
| 475 cpi->rc.kf_boost, |
| 476 kf_low, kf_high, |
| 477 kf_low_motion_minq, |
| 478 kf_high_motion_minq); |
| 479 |
| 480 // Allow somewhat lower kf minq with small image formats. |
| 481 if ((cm->width * cm->height) <= (352 * 288)) { |
| 482 q_adj_factor -= 0.25; |
| 483 } |
| 484 |
| 485 // Make a further adjustment based on the kf zero motion measure. |
| 486 q_adj_factor += 0.05 - (0.001 * (double)cpi->kf_zeromotion_pct); |
| 487 |
| 488 // Convert the adjustment factor to a qindex delta |
| 489 // on active_best_quality. |
| 490 q_val = vp9_convert_qindex_to_q(active_best_quality); |
| 491 active_best_quality += |
| 492 vp9_compute_qdelta(cpi, q_val, (q_val * q_adj_factor)); |
| 493 } |
| 494 #else |
| 495 double current_q; |
| 496 // Force the KF quantizer to be 30% of the active_worst_quality. |
| 497 current_q = vp9_convert_qindex_to_q(active_worst_quality); |
| 498 active_best_quality = active_worst_quality |
| 499 + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); |
| 500 #endif |
| 501 } else if (!cpi->is_src_frame_alt_ref && |
| 502 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 503 |
| 504 // Use the lower of active_worst_quality and recent |
| 505 // average Q as basis for GF/ARF best Q limit unless last frame was |
| 506 // a key frame. |
| 507 if (cpi->frames_since_key > 1 && |
| 508 cpi->rc.avg_frame_qindex < active_worst_quality) { |
| 509 q = cpi->rc.avg_frame_qindex; |
| 510 } else { |
| 511 q = active_worst_quality; |
| 512 } |
| 513 // For constrained quality dont allow Q less than the cq level |
| 514 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
| 515 if (q < cpi->cq_target_quality) |
| 516 q = cpi->cq_target_quality; |
| 517 if (cpi->frames_since_key > 1) { |
| 518 active_best_quality = get_active_quality(q, cpi->rc.gfu_boost, |
| 519 gf_low, gf_high, |
| 520 afq_low_motion_minq, |
| 521 afq_high_motion_minq); |
| 522 } else { |
| 523 active_best_quality = get_active_quality(q, cpi->rc.gfu_boost, |
| 524 gf_low, gf_high, |
| 525 gf_low_motion_minq, |
| 526 gf_high_motion_minq); |
| 527 } |
| 528 // Constrained quality use slightly lower active best. |
| 529 active_best_quality = active_best_quality * 15 / 16; |
| 530 |
| 531 } else if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
| 532 if (!cpi->refresh_alt_ref_frame) { |
| 533 active_best_quality = cpi->cq_target_quality; |
| 534 } else { |
| 535 if (cpi->frames_since_key > 1) { |
| 536 active_best_quality = get_active_quality( |
| 537 q, cpi->rc.gfu_boost, gf_low, gf_high, |
| 538 afq_low_motion_minq, afq_high_motion_minq); |
| 539 } else { |
| 540 active_best_quality = get_active_quality( |
| 541 q, cpi->rc.gfu_boost, gf_low, gf_high, |
| 542 gf_low_motion_minq, gf_high_motion_minq); |
| 543 } |
| 544 } |
| 545 } else { |
| 546 active_best_quality = get_active_quality( |
| 547 q, cpi->rc.gfu_boost, gf_low, gf_high, |
| 548 gf_low_motion_minq, gf_high_motion_minq); |
| 549 } |
| 550 } else { |
| 551 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
| 552 active_best_quality = cpi->cq_target_quality; |
| 553 } else { |
| 554 if (cpi->pass == 0 && |
| 555 cpi->rc.avg_frame_qindex < active_worst_quality) |
| 556 // 1-pass: for now, use the average Q for the active_best, if its lower |
| 557 // than active_worst. |
| 558 active_best_quality = inter_minq[cpi->rc.avg_frame_qindex]; |
| 559 else |
| 560 active_best_quality = inter_minq[active_worst_quality]; |
| 561 |
| 562 // For the constrained quality mode we don't want |
| 563 // q to fall below the cq level. |
| 564 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && |
| 565 (active_best_quality < cpi->cq_target_quality)) { |
| 566 // If we are strongly undershooting the target rate in the last |
| 567 // frames then use the user passed in cq value not the auto |
| 568 // cq value. |
| 569 if (cpi->rc.rolling_actual_bits < cpi->rc.min_frame_bandwidth) |
| 570 active_best_quality = cpi->oxcf.cq_level; |
| 571 else |
| 572 active_best_quality = cpi->cq_target_quality; |
| 573 } |
| 574 } |
| 575 } |
| 576 |
| 577 // Clip the active best and worst quality values to limits |
| 578 if (active_worst_quality > cpi->rc.worst_quality) |
| 579 active_worst_quality = cpi->rc.worst_quality; |
| 580 |
| 581 if (active_best_quality < cpi->rc.best_quality) |
| 582 active_best_quality = cpi->rc.best_quality; |
| 583 |
| 584 if (active_best_quality > cpi->rc.worst_quality) |
| 585 active_best_quality = cpi->rc.worst_quality; |
| 586 |
| 587 if (active_worst_quality < active_best_quality) |
| 588 active_worst_quality = active_best_quality; |
| 589 |
| 590 *top_index_prop = active_worst_quality; |
| 591 *top_index = active_worst_quality; |
| 592 *bottom_index = active_best_quality; |
| 593 |
| 594 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
| 595 // Limit Q range for the adaptive loop. |
| 596 if (cm->frame_type == KEY_FRAME && !cpi->this_key_frame_forced) { |
| 597 if (!(cpi->pass == 0 && cpi->common.current_video_frame == 0)) { |
| 598 *top_index = active_worst_quality; |
| 599 *top_index = |
| 600 (active_worst_quality + active_best_quality * 3) / 4; |
| 601 } |
| 602 } else if (!cpi->is_src_frame_alt_ref && |
| 603 (cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER) && |
| 604 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 605 *top_index = |
| 606 (active_worst_quality + active_best_quality) / 2; |
| 607 } |
| 608 #endif |
| 609 |
| 610 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
| 611 q = active_best_quality; |
| 612 // Special case code to try and match quality with forced key frames |
| 613 } else if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { |
| 614 q = cpi->rc.last_boosted_qindex; |
| 615 } else { |
| 616 // Determine initial Q to try. |
| 617 if (cpi->pass == 0) { |
| 618 // 1-pass: for now, use per-frame-bw for target size of frame, scaled |
| 619 // by |x| for key frame. |
| 620 int scale = (cm->frame_type == KEY_FRAME) ? 5 : 1; |
| 621 q = vp9_rc_regulate_q(cpi, scale * cpi->rc.av_per_frame_bandwidth, |
| 622 active_best_quality, active_worst_quality); |
| 623 } else { |
| 624 q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target, |
| 625 active_best_quality, active_worst_quality); |
| 626 } |
| 627 if (q > *top_index) |
| 628 q = *top_index; |
| 629 } |
| 630 #if CONFIG_MULTIPLE_ARF |
| 631 // Force the quantizer determined by the coding order pattern. |
| 632 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && |
| 633 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { |
| 634 double new_q; |
| 635 double current_q = vp9_convert_qindex_to_q(active_worst_quality); |
| 636 int level = cpi->this_frame_weight; |
| 637 assert(level >= 0); |
| 638 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); |
| 639 q = active_worst_quality + |
| 640 vp9_compute_qdelta(cpi, current_q, new_q); |
| 641 |
| 642 *bottom_index = q; |
| 643 *top_index = q; |
| 644 printf("frame:%d q:%d\n", cm->current_video_frame, q); |
| 645 } |
| 646 #endif |
| 647 return q; |
| 648 } |
371 | 649 |
372 static int estimate_keyframe_frequency(VP9_COMP *cpi) { | 650 static int estimate_keyframe_frequency(VP9_COMP *cpi) { |
373 int i; | 651 int i; |
374 | 652 |
375 // Average key frame frequency | 653 // Average key frame frequency |
376 int av_key_frame_frequency = 0; | 654 int av_key_frame_frequency = 0; |
377 | 655 |
378 /* First key frame at start of sequence is a special case. We have no | 656 /* First key frame at start of sequence is a special case. We have no |
379 * frequency data. | 657 * frequency data. |
380 */ | 658 */ |
381 if (cpi->key_frame_count == 1) { | 659 if (cpi->rc.key_frame_count == 1) { |
382 /* Assume a default of 1 kf every 2 seconds, or the max kf interval, | 660 /* Assume a default of 1 kf every 2 seconds, or the max kf interval, |
383 * whichever is smaller. | 661 * whichever is smaller. |
384 */ | 662 */ |
385 int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1; | 663 int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1; |
386 av_key_frame_frequency = (int)cpi->output_framerate * 2; | 664 av_key_frame_frequency = (int)cpi->output_framerate * 2; |
387 | 665 |
388 if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) | 666 if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) |
389 av_key_frame_frequency = cpi->oxcf.key_freq; | 667 av_key_frame_frequency = cpi->oxcf.key_freq; |
390 | 668 |
391 cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] | 669 cpi->rc.prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] |
392 = av_key_frame_frequency; | 670 = av_key_frame_frequency; |
393 } else { | 671 } else { |
394 unsigned int total_weight = 0; | 672 unsigned int total_weight = 0; |
395 int last_kf_interval = | 673 int last_kf_interval = |
396 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1; | 674 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1; |
397 | 675 |
398 /* reset keyframe context and calculate weighted average of last | 676 /* reset keyframe context and calculate weighted average of last |
399 * KEY_FRAME_CONTEXT keyframes | 677 * KEY_FRAME_CONTEXT keyframes |
400 */ | 678 */ |
401 for (i = 0; i < KEY_FRAME_CONTEXT; i++) { | 679 for (i = 0; i < KEY_FRAME_CONTEXT; i++) { |
402 if (i < KEY_FRAME_CONTEXT - 1) | 680 if (i < KEY_FRAME_CONTEXT - 1) |
403 cpi->prior_key_frame_distance[i] | 681 cpi->rc.prior_key_frame_distance[i] |
404 = cpi->prior_key_frame_distance[i + 1]; | 682 = cpi->rc.prior_key_frame_distance[i + 1]; |
405 else | 683 else |
406 cpi->prior_key_frame_distance[i] = last_kf_interval; | 684 cpi->rc.prior_key_frame_distance[i] = last_kf_interval; |
407 | 685 |
408 av_key_frame_frequency += prior_key_frame_weight[i] | 686 av_key_frame_frequency += prior_key_frame_weight[i] |
409 * cpi->prior_key_frame_distance[i]; | 687 * cpi->rc.prior_key_frame_distance[i]; |
410 total_weight += prior_key_frame_weight[i]; | 688 total_weight += prior_key_frame_weight[i]; |
411 } | 689 } |
412 | 690 |
413 av_key_frame_frequency /= total_weight; | 691 av_key_frame_frequency /= total_weight; |
414 } | 692 } |
415 return av_key_frame_frequency; | 693 return av_key_frame_frequency; |
416 } | 694 } |
417 | 695 |
418 | 696 |
419 void vp9_adjust_key_frame_context(VP9_COMP *cpi) { | 697 static void adjust_key_frame_context(VP9_COMP *cpi) { |
420 // Clear down mmx registers to allow floating point in what follows | 698 // Clear down mmx registers to allow floating point in what follows |
421 vp9_clear_system_state(); | 699 vp9_clear_system_state(); |
422 | 700 |
423 cpi->frames_since_key = 0; | 701 cpi->frames_since_key = 0; |
424 cpi->key_frame_count++; | 702 cpi->rc.key_frame_count++; |
425 } | 703 } |
426 | 704 |
427 | 705 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, |
428 void vp9_compute_frame_size_bounds(VP9_COMP *cpi, int *frame_under_shoot_limit, | 706 int this_frame_target, |
429 int *frame_over_shoot_limit) { | 707 int *frame_under_shoot_limit, |
| 708 int *frame_over_shoot_limit) { |
430 // Set-up bounds on acceptable frame size: | 709 // Set-up bounds on acceptable frame size: |
431 if (cpi->oxcf.fixed_q >= 0) { | 710 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
432 // Fixed Q scenario: frame size never outranges target (there is no target!) | |
433 *frame_under_shoot_limit = 0; | 711 *frame_under_shoot_limit = 0; |
434 *frame_over_shoot_limit = INT_MAX; | 712 *frame_over_shoot_limit = INT_MAX; |
435 } else { | 713 } else { |
436 if (cpi->common.frame_type == KEY_FRAME) { | 714 if (cpi->common.frame_type == KEY_FRAME) { |
437 *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; | 715 *frame_over_shoot_limit = this_frame_target * 9 / 8; |
438 *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; | 716 *frame_under_shoot_limit = this_frame_target * 7 / 8; |
439 } else { | 717 } else { |
440 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) { | 718 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) { |
441 *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; | 719 *frame_over_shoot_limit = this_frame_target * 9 / 8; |
442 *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; | 720 *frame_under_shoot_limit = this_frame_target * 7 / 8; |
443 } else { | 721 } else { |
444 // Stron overshoot limit for constrained quality | 722 // Stron overshoot limit for constrained quality |
445 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { | 723 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
446 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; | 724 *frame_over_shoot_limit = this_frame_target * 11 / 8; |
447 *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8; | 725 *frame_under_shoot_limit = this_frame_target * 2 / 8; |
448 } else { | 726 } else { |
449 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; | 727 *frame_over_shoot_limit = this_frame_target * 11 / 8; |
450 *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8; | 728 *frame_under_shoot_limit = this_frame_target * 5 / 8; |
451 } | 729 } |
452 } | 730 } |
453 } | 731 } |
454 | 732 |
455 // For very small rate targets where the fractional adjustment | 733 // For very small rate targets where the fractional adjustment |
456 // (eg * 7/8) may be tiny make sure there is at least a minimum | 734 // (eg * 7/8) may be tiny make sure there is at least a minimum |
457 // range. | 735 // range. |
458 *frame_over_shoot_limit += 200; | 736 *frame_over_shoot_limit += 200; |
459 *frame_under_shoot_limit -= 200; | 737 *frame_under_shoot_limit -= 200; |
460 if (*frame_under_shoot_limit < 0) | 738 if (*frame_under_shoot_limit < 0) |
461 *frame_under_shoot_limit = 0; | 739 *frame_under_shoot_limit = 0; |
462 } | 740 } |
463 } | 741 } |
464 | 742 |
465 | |
466 // return of 0 means drop frame | 743 // return of 0 means drop frame |
467 int vp9_pick_frame_size(VP9_COMP *cpi) { | 744 int vp9_rc_pick_frame_size_target(VP9_COMP *cpi) { |
468 VP9_COMMON *cm = &cpi->common; | 745 VP9_COMMON *cm = &cpi->common; |
469 | 746 |
470 if (cm->frame_type == KEY_FRAME) | 747 if (cm->frame_type == KEY_FRAME) |
471 calc_iframe_target_size(cpi); | 748 calc_iframe_target_size(cpi); |
472 else | 749 else |
473 calc_pframe_target_size(cpi); | 750 calc_pframe_target_size(cpi); |
474 | 751 |
| 752 // Target rate per SB64 (including partial SB64s. |
| 753 cpi->rc.sb64_target_rate = ((int64_t)cpi->rc.this_frame_target * 64 * 64) / |
| 754 (cpi->common.width * cpi->common.height); |
475 return 1; | 755 return 1; |
476 } | 756 } |
| 757 |
| 758 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used, |
| 759 int worst_q) { |
| 760 VP9_COMMON *const cm = &cpi->common; |
| 761 // Update rate control heuristics |
| 762 cpi->rc.projected_frame_size = (bytes_used << 3); |
| 763 |
| 764 // Post encode loop adjustment of Q prediction. |
| 765 vp9_rc_update_rate_correction_factors( |
| 766 cpi, (cpi->sf.recode_loop || |
| 767 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0); |
| 768 |
| 769 cpi->rc.last_q[cm->frame_type] = cm->base_qindex; |
| 770 cpi->rc.active_worst_quality = worst_q; |
| 771 |
| 772 // Keep record of last boosted (KF/KF/ARF) Q value. |
| 773 // If the current frame is coded at a lower Q then we also update it. |
| 774 // If all mbs in this group are skipped only update if the Q value is |
| 775 // better than that already stored. |
| 776 // This is used to help set quality in forced key frames to reduce popping |
| 777 if ((cm->base_qindex < cpi->rc.last_boosted_qindex) || |
| 778 ((cpi->static_mb_pct < 100) && |
| 779 ((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame || |
| 780 (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) { |
| 781 cpi->rc.last_boosted_qindex = cm->base_qindex; |
| 782 } |
| 783 |
| 784 if (cm->frame_type == KEY_FRAME) { |
| 785 adjust_key_frame_context(cpi); |
| 786 } |
| 787 |
| 788 // Keep a record of ambient average Q. |
| 789 if (cm->frame_type != KEY_FRAME) |
| 790 cpi->rc.avg_frame_qindex = (2 + 3 * cpi->rc.avg_frame_qindex + |
| 791 cm->base_qindex) >> 2; |
| 792 |
| 793 // Keep a record from which we can calculate the average Q excluding GF |
| 794 // updates and key frames. |
| 795 if (cm->frame_type != KEY_FRAME && |
| 796 !cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame) { |
| 797 cpi->rc.ni_frames++; |
| 798 cpi->rc.tot_q += vp9_convert_qindex_to_q(cm->base_qindex); |
| 799 cpi->rc.avg_q = cpi->rc.tot_q / (double)cpi->rc.ni_frames; |
| 800 |
| 801 // Calculate the average Q for normal inter frames (not key or GFU frames). |
| 802 cpi->rc.ni_tot_qi += cm->base_qindex; |
| 803 cpi->rc.ni_av_qi = cpi->rc.ni_tot_qi / cpi->rc.ni_frames; |
| 804 } |
| 805 |
| 806 // Update the buffer level variable. |
| 807 // Non-viewable frames are a special case and are treated as pure overhead. |
| 808 if (!cm->show_frame) |
| 809 cpi->rc.bits_off_target -= cpi->rc.projected_frame_size; |
| 810 else |
| 811 cpi->rc.bits_off_target += cpi->rc.av_per_frame_bandwidth - |
| 812 cpi->rc.projected_frame_size; |
| 813 |
| 814 // Clip the buffer level at the maximum buffer size |
| 815 if (cpi->rc.bits_off_target > cpi->oxcf.maximum_buffer_size) |
| 816 cpi->rc.bits_off_target = cpi->oxcf.maximum_buffer_size; |
| 817 |
| 818 // Rolling monitors of whether we are over or underspending used to help |
| 819 // regulate min and Max Q in two pass. |
| 820 if (cm->frame_type != KEY_FRAME) { |
| 821 cpi->rc.rolling_target_bits = |
| 822 ((cpi->rc.rolling_target_bits * 3) + |
| 823 cpi->rc.this_frame_target + 2) / 4; |
| 824 cpi->rc.rolling_actual_bits = |
| 825 ((cpi->rc.rolling_actual_bits * 3) + |
| 826 cpi->rc.projected_frame_size + 2) / 4; |
| 827 cpi->rc.long_rolling_target_bits = |
| 828 ((cpi->rc.long_rolling_target_bits * 31) + |
| 829 cpi->rc.this_frame_target + 16) / 32; |
| 830 cpi->rc.long_rolling_actual_bits = |
| 831 ((cpi->rc.long_rolling_actual_bits * 31) + |
| 832 cpi->rc.projected_frame_size + 16) / 32; |
| 833 } |
| 834 |
| 835 // Actual bits spent |
| 836 cpi->rc.total_actual_bits += cpi->rc.projected_frame_size; |
| 837 |
| 838 // Debug stats |
| 839 cpi->rc.total_target_vs_actual += (cpi->rc.this_frame_target - |
| 840 cpi->rc.projected_frame_size); |
| 841 |
| 842 cpi->rc.buffer_level = cpi->rc.bits_off_target; |
| 843 |
| 844 #ifndef DISABLE_RC_LONG_TERM_MEM |
| 845 // Update bits left to the kf and gf groups to account for overshoot or |
| 846 // undershoot on these frames |
| 847 if (cm->frame_type == KEY_FRAME) { |
| 848 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - |
| 849 cpi->rc.projected_frame_size; |
| 850 |
| 851 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); |
| 852 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { |
| 853 cpi->twopass.gf_group_bits += cpi->rc.this_frame_target - |
| 854 cpi->rc.projected_frame_size; |
| 855 |
| 856 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); |
| 857 } |
| 858 #endif |
| 859 } |
OLD | NEW |