Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_ratectrl.c

Issue 232133009: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rdopt.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <assert.h> 11 #include <assert.h>
12 #include <limits.h> 12 #include <limits.h>
13 #include <math.h> 13 #include <math.h>
14 #include <stdio.h> 14 #include <stdio.h>
15 #include <stdlib.h> 15 #include <stdlib.h>
16 #include <string.h> 16 #include <string.h>
17 17
18 #include "vpx_mem/vpx_mem.h" 18 #include "vpx_mem/vpx_mem.h"
19 19
20 #include "vp9/common/vp9_alloccommon.h" 20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_common.h" 21 #include "vp9/common/vp9_common.h"
22 #include "vp9/common/vp9_entropymode.h" 22 #include "vp9/common/vp9_entropymode.h"
23 #include "vp9/common/vp9_quant_common.h" 23 #include "vp9/common/vp9_quant_common.h"
24 #include "vp9/common/vp9_seg_common.h" 24 #include "vp9/common/vp9_seg_common.h"
25 #include "vp9/common/vp9_systemdependent.h" 25 #include "vp9/common/vp9_systemdependent.h"
26 26
27 #include "vp9/encoder/vp9_encodemv.h" 27 #include "vp9/encoder/vp9_encodemv.h"
28 #include "vp9/encoder/vp9_ratectrl.h" 28 #include "vp9/encoder/vp9_ratectrl.h"
29 29
30 #define DEFAULT_KF_BOOST 2000
31 #define DEFAULT_GF_BOOST 2000
32
30 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1 33 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
31 34
32 #define MIN_BPB_FACTOR 0.005 35 #define MIN_BPB_FACTOR 0.005
33 #define MAX_BPB_FACTOR 50 36 #define MAX_BPB_FACTOR 50
34 37
35 // Bits Per MB at different Q (Multiplied by 512)
36 #define BPER_MB_NORMBITS 9
37
38 // Tables relating active max Q to active min Q 38 // Tables relating active max Q to active min Q
39 static int kf_low_motion_minq[QINDEX_RANGE]; 39 static int kf_low_motion_minq[QINDEX_RANGE];
40 static int kf_high_motion_minq[QINDEX_RANGE]; 40 static int kf_high_motion_minq[QINDEX_RANGE];
41 static int gf_low_motion_minq[QINDEX_RANGE]; 41 static int gf_low_motion_minq[QINDEX_RANGE];
42 static int gf_high_motion_minq[QINDEX_RANGE]; 42 static int gf_high_motion_minq[QINDEX_RANGE];
43 static int inter_minq[QINDEX_RANGE]; 43 static int inter_minq[QINDEX_RANGE];
44 static int afq_low_motion_minq[QINDEX_RANGE]; 44 static int afq_low_motion_minq[QINDEX_RANGE];
45 static int afq_high_motion_minq[QINDEX_RANGE]; 45 static int afq_high_motion_minq[QINDEX_RANGE];
46 static int gf_high = 2000; 46 static int gf_high = 2000;
47 static int gf_low = 400; 47 static int gf_low = 400;
48 static int kf_high = 5000; 48 static int kf_high = 5000;
49 static int kf_low = 400; 49 static int kf_low = 400;
50 50
51 // Functions to compute the active minq lookup table entries based on a 51 // Functions to compute the active minq lookup table entries based on a
52 // formulaic approach to facilitate easier adjustment of the Q tables. 52 // formulaic approach to facilitate easier adjustment of the Q tables.
53 // The formulae were derived from computing a 3rd order polynomial best 53 // The formulae were derived from computing a 3rd order polynomial best
54 // fit to the original data (after plotting real maxq vs minq (not q index)) 54 // fit to the original data (after plotting real maxq vs minq (not q index))
55 static int calculate_minq_index(double maxq, 55 static int get_minq_index(double maxq, double x3, double x2, double x1) {
56 double x3, double x2, double x1, double c) {
57 int i; 56 int i;
58 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c, 57 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq,
59 maxq); 58 maxq);
60 59
61 // Special case handling to deal with the step from q2.0 60 // Special case handling to deal with the step from q2.0
62 // down to lossless mode represented by q 1.0. 61 // down to lossless mode represented by q 1.0.
63 if (minqtarget <= 2.0) 62 if (minqtarget <= 2.0)
64 return 0; 63 return 0;
65 64
66 for (i = 0; i < QINDEX_RANGE; i++) { 65 for (i = 0; i < QINDEX_RANGE; i++)
67 if (minqtarget <= vp9_convert_qindex_to_q(i)) 66 if (minqtarget <= vp9_convert_qindex_to_q(i))
68 return i; 67 return i;
69 }
70 68
71 return QINDEX_RANGE - 1; 69 return QINDEX_RANGE - 1;
72 } 70 }
73 71
74 void vp9_rc_init_minq_luts(void) { 72 void vp9_rc_init_minq_luts() {
75 int i; 73 int i;
76 74
77 for (i = 0; i < QINDEX_RANGE; i++) { 75 for (i = 0; i < QINDEX_RANGE; i++) {
78 const double maxq = vp9_convert_qindex_to_q(i); 76 const double maxq = vp9_convert_qindex_to_q(i);
79 77
80 78 kf_low_motion_minq[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.15);
81 kf_low_motion_minq[i] = calculate_minq_index(maxq, 79 kf_high_motion_minq[i] = get_minq_index(maxq, 0.000002, -0.0012, 0.50);
82 0.000001, 80 gf_low_motion_minq[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.32);
83 -0.0004, 81 gf_high_motion_minq[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.50);
84 0.15, 82 afq_low_motion_minq[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.33);
85 0.0); 83 afq_high_motion_minq[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55);
86 kf_high_motion_minq[i] = calculate_minq_index(maxq, 84 inter_minq[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.75);
87 0.000002,
88 -0.0012,
89 0.50,
90 0.0);
91
92 gf_low_motion_minq[i] = calculate_minq_index(maxq,
93 0.0000015,
94 -0.0009,
95 0.32,
96 0.0);
97 gf_high_motion_minq[i] = calculate_minq_index(maxq,
98 0.0000021,
99 -0.00125,
100 0.50,
101 0.0);
102 afq_low_motion_minq[i] = calculate_minq_index(maxq,
103 0.0000015,
104 -0.0009,
105 0.33,
106 0.0);
107 afq_high_motion_minq[i] = calculate_minq_index(maxq,
108 0.0000021,
109 -0.00125,
110 0.55,
111 0.0);
112 inter_minq[i] = calculate_minq_index(maxq,
113 0.00000271,
114 -0.00113,
115 0.75,
116 0.0);
117 } 85 }
118 } 86 }
119 87
120 // These functions use formulaic calculations to make playing with the 88 // These functions use formulaic calculations to make playing with the
121 // quantizer tables easier. If necessary they can be replaced by lookup 89 // quantizer tables easier. If necessary they can be replaced by lookup
122 // tables if and when things settle down in the experimental bitstream 90 // tables if and when things settle down in the experimental bitstream
123 double vp9_convert_qindex_to_q(int qindex) { 91 double vp9_convert_qindex_to_q(int qindex) {
124 // Convert the index to a real Q value (scaled down to match old Q values) 92 // Convert the index to a real Q value (scaled down to match old Q values)
125 return vp9_ac_quant(qindex, 0) / 4.0; 93 return vp9_ac_quant(qindex, 0) / 4.0;
126 } 94 }
127 95
128 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, 96 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
129 double correction_factor) { 97 double correction_factor) {
130 const double q = vp9_convert_qindex_to_q(qindex); 98 const double q = vp9_convert_qindex_to_q(qindex);
131 int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; 99 int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000;
132 100
133 // q based adjustment to baseline enumerator 101 // q based adjustment to baseline enumerator
134 enumerator += (int)(enumerator * q) >> 12; 102 enumerator += (int)(enumerator * q) >> 12;
135 return (int)(0.5 + (enumerator * correction_factor / q)); 103 return (int)(0.5 + (enumerator * correction_factor / q));
136 } 104 }
137 105
138 void vp9_save_coding_context(VP9_COMP *cpi) { 106 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
139 CODING_CONTEXT *const cc = &cpi->coding_context;
140 VP9_COMMON *cm = &cpi->common;
141
142 // Stores a snapshot of key state variables which can subsequently be
143 // restored with a call to vp9_restore_coding_context. These functions are
144 // intended for use in a re-code loop in vp9_compress_frame where the
145 // quantizer value is adjusted between loop iterations.
146 vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost);
147 vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts);
148 vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp);
149
150 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
151
152 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
153 cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
154
155 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
156 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
157
158 cc->fc = cm->fc;
159 }
160
161 void vp9_restore_coding_context(VP9_COMP *cpi) {
162 CODING_CONTEXT *const cc = &cpi->coding_context;
163 VP9_COMMON *cm = &cpi->common;
164
165 // Restore key state variables to the snapshot state stored in the
166 // previous call to vp9_save_coding_context.
167 vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
168 vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts);
169 vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp);
170
171 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
172
173 vpx_memcpy(cm->last_frame_seg_map,
174 cpi->coding_context.last_frame_seg_map_copy,
175 (cm->mi_rows * cm->mi_cols));
176
177 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
178 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
179
180 cm->fc = cc->fc;
181 }
182
183 void vp9_setup_key_frame(VP9_COMP *cpi) {
184 VP9_COMMON *cm = &cpi->common;
185
186 vp9_setup_past_independence(cm);
187
188 /* All buffers are implicitly updated on key frames. */
189 cpi->refresh_golden_frame = 1;
190 cpi->refresh_alt_ref_frame = 1;
191 }
192
193 void vp9_setup_inter_frame(VP9_COMP *cpi) {
194 VP9_COMMON *cm = &cpi->common;
195 if (cm->error_resilient_mode || cm->intra_only)
196 vp9_setup_past_independence(cm);
197
198 assert(cm->frame_context_idx < FRAME_CONTEXTS);
199 cm->fc = cm->frame_contexts[cm->frame_context_idx];
200 }
201
202 static int estimate_bits_at_q(int frame_kind, int q, int mbs,
203 double correction_factor) { 107 double correction_factor) {
204 const int bpm = (int)(vp9_rc_bits_per_mb(frame_kind, q, correction_factor)); 108 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor));
205 109 return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS;
206 // Attempt to retain reasonable accuracy without overflow. The cutoff is
207 // chosen such that the maximum product of Bpm and MBs fits 31 bits. The
208 // largest Bpm takes 20 bits.
209 return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs
210 : (bpm * mbs) >> BPER_MB_NORMBITS;
211 } 110 }
212 111
213 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { 112 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
214 const RATE_CONTROL *rc = &cpi->rc; 113 const RATE_CONTROL *rc = &cpi->rc;
215 const int min_frame_target = MAX(rc->min_frame_bandwidth, 114 const int min_frame_target = MAX(rc->min_frame_bandwidth,
216 rc->av_per_frame_bandwidth >> 5); 115 rc->av_per_frame_bandwidth >> 5);
217 if (target < min_frame_target) 116 if (target < min_frame_target)
218 target = min_frame_target; 117 target = min_frame_target;
219 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { 118 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
220 // If there is an active ARF at this location use the minimum 119 // If there is an active ARF at this location use the minimum
(...skipping 16 matching lines...) Expand all
237 oxcf->rc_max_intra_bitrate_pct / 100; 136 oxcf->rc_max_intra_bitrate_pct / 100;
238 target = MIN(target, max_rate); 137 target = MIN(target, max_rate);
239 } 138 }
240 if (target > rc->max_frame_bandwidth) 139 if (target > rc->max_frame_bandwidth)
241 target = rc->max_frame_bandwidth; 140 target = rc->max_frame_bandwidth;
242 return target; 141 return target;
243 } 142 }
244 143
245 144
246 // Update the buffer level for higher layers, given the encoded current layer. 145 // Update the buffer level for higher layers, given the encoded current layer.
247 static void update_layer_buffer_level(VP9_COMP *const cpi, 146 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
248 int encoded_frame_size) {
249 int temporal_layer = 0; 147 int temporal_layer = 0;
250 int current_temporal_layer = cpi->svc.temporal_layer_id; 148 int current_temporal_layer = svc->temporal_layer_id;
251 for (temporal_layer = current_temporal_layer + 1; 149 for (temporal_layer = current_temporal_layer + 1;
252 temporal_layer < cpi->svc.number_temporal_layers; ++temporal_layer) { 150 temporal_layer < svc->number_temporal_layers; ++temporal_layer) {
253 LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer]; 151 LAYER_CONTEXT *lc = &svc->layer_context[temporal_layer];
254 RATE_CONTROL *lrc = &lc->rc; 152 RATE_CONTROL *lrc = &lc->rc;
255 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate - 153 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
256 encoded_frame_size); 154 encoded_frame_size);
257 lrc->bits_off_target += bits_off_for_this_layer; 155 lrc->bits_off_target += bits_off_for_this_layer;
258 156
259 // Clip buffer level to maximum buffer size for the layer. 157 // Clip buffer level to maximum buffer size for the layer.
260 lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size); 158 lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
261 lrc->buffer_level = lrc->bits_off_target; 159 lrc->buffer_level = lrc->bits_off_target;
262 } 160 }
263 } 161 }
264 162
265 // Update the buffer level: leaky bucket model. 163 // Update the buffer level: leaky bucket model.
266 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { 164 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
267 const VP9_COMMON *const cm = &cpi->common; 165 const VP9_COMMON *const cm = &cpi->common;
268 const VP9_CONFIG *oxcf = &cpi->oxcf; 166 const VP9_CONFIG *oxcf = &cpi->oxcf;
269 RATE_CONTROL *const rc = &cpi->rc; 167 RATE_CONTROL *const rc = &cpi->rc;
270 168
271 // Non-viewable frames are a special case and are treated as pure overhead. 169 // Non-viewable frames are a special case and are treated as pure overhead.
272 if (!cm->show_frame) { 170 if (!cm->show_frame) {
273 rc->bits_off_target -= encoded_frame_size; 171 rc->bits_off_target -= encoded_frame_size;
274 } else { 172 } else {
275 rc->bits_off_target += rc->av_per_frame_bandwidth - encoded_frame_size; 173 rc->bits_off_target += rc->av_per_frame_bandwidth - encoded_frame_size;
276 } 174 }
277 175
278 // Clip the buffer level to the maximum specified buffer size. 176 // Clip the buffer level to the maximum specified buffer size.
279 rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size); 177 rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size);
280 rc->buffer_level = rc->bits_off_target; 178 rc->buffer_level = rc->bits_off_target;
281 179
282 if (cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { 180 if (cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
283 update_layer_buffer_level(cpi, encoded_frame_size); 181 update_layer_buffer_level(&cpi->svc, encoded_frame_size);
284 } 182 }
285 } 183 }
286 184
185 void vp9_rc_init(const VP9_CONFIG *oxcf, int pass, RATE_CONTROL *rc) {
186 if (pass == 0 && oxcf->end_usage == USAGE_STREAM_FROM_SERVER) {
187 rc->avg_frame_qindex[0] = oxcf->worst_allowed_q;
188 rc->avg_frame_qindex[1] = oxcf->worst_allowed_q;
189 rc->avg_frame_qindex[2] = oxcf->worst_allowed_q;
190 } else {
191 rc->avg_frame_qindex[0] = (oxcf->worst_allowed_q +
192 oxcf->best_allowed_q) / 2;
193 rc->avg_frame_qindex[1] = (oxcf->worst_allowed_q +
194 oxcf->best_allowed_q) / 2;
195 rc->avg_frame_qindex[2] = (oxcf->worst_allowed_q +
196 oxcf->best_allowed_q) / 2;
197 }
198
199 rc->last_q[0] = oxcf->best_allowed_q;
200 rc->last_q[1] = oxcf->best_allowed_q;
201 rc->last_q[2] = oxcf->best_allowed_q;
202
203 rc->buffer_level = oxcf->starting_buffer_level;
204 rc->bits_off_target = oxcf->starting_buffer_level;
205
206 rc->rolling_target_bits = rc->av_per_frame_bandwidth;
207 rc->rolling_actual_bits = rc->av_per_frame_bandwidth;
208 rc->long_rolling_target_bits = rc->av_per_frame_bandwidth;
209 rc->long_rolling_actual_bits = rc->av_per_frame_bandwidth;
210
211 rc->total_actual_bits = 0;
212 rc->total_target_vs_actual = 0;
213
214 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
215 rc->frames_since_key = 8; // Sensible default for first frame.
216 rc->this_key_frame_forced = 0;
217 rc->next_key_frame_forced = 0;
218 rc->source_alt_ref_pending = 0;
219 rc->source_alt_ref_active = 0;
220
221 rc->frames_till_gf_update_due = 0;
222
223 rc->ni_av_qi = oxcf->worst_allowed_q;
224 rc->ni_tot_qi = 0;
225 rc->ni_frames = 0;
226
227 rc->tot_q = 0.0;
228 rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q);
229
230 rc->rate_correction_factor = 1.0;
231 rc->key_frame_rate_correction_factor = 1.0;
232 rc->gf_rate_correction_factor = 1.0;
233 }
234
287 int vp9_rc_drop_frame(VP9_COMP *cpi) { 235 int vp9_rc_drop_frame(VP9_COMP *cpi) {
288 const VP9_CONFIG *oxcf = &cpi->oxcf; 236 const VP9_CONFIG *oxcf = &cpi->oxcf;
289 RATE_CONTROL *const rc = &cpi->rc; 237 RATE_CONTROL *const rc = &cpi->rc;
290 238
291 if (!oxcf->drop_frames_water_mark) { 239 if (!oxcf->drop_frames_water_mark) {
292 return 0; 240 return 0;
293 } else { 241 } else {
294 if (rc->buffer_level < 0) { 242 if (rc->buffer_level < 0) {
295 // Always drop if buffer is below 0. 243 // Always drop if buffer is below 0.
296 return 1; 244 return 1;
(...skipping 23 matching lines...) Expand all
320 } 268 }
321 } 269 }
322 } 270 }
323 } 271 }
324 272
325 static double get_rate_correction_factor(const VP9_COMP *cpi) { 273 static double get_rate_correction_factor(const VP9_COMP *cpi) {
326 if (cpi->common.frame_type == KEY_FRAME) { 274 if (cpi->common.frame_type == KEY_FRAME) {
327 return cpi->rc.key_frame_rate_correction_factor; 275 return cpi->rc.key_frame_rate_correction_factor;
328 } else { 276 } else {
329 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 277 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
278 !cpi->rc.is_src_frame_alt_ref &&
330 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) 279 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
331 return cpi->rc.gf_rate_correction_factor; 280 return cpi->rc.gf_rate_correction_factor;
332 else 281 else
333 return cpi->rc.rate_correction_factor; 282 return cpi->rc.rate_correction_factor;
334 } 283 }
335 } 284 }
336 285
337 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { 286 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
338 if (cpi->common.frame_type == KEY_FRAME) { 287 if (cpi->common.frame_type == KEY_FRAME) {
339 cpi->rc.key_frame_rate_correction_factor = factor; 288 cpi->rc.key_frame_rate_correction_factor = factor;
340 } else { 289 } else {
341 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 290 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
291 !cpi->rc.is_src_frame_alt_ref &&
342 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) 292 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
343 cpi->rc.gf_rate_correction_factor = factor; 293 cpi->rc.gf_rate_correction_factor = factor;
344 else 294 else
345 cpi->rc.rate_correction_factor = factor; 295 cpi->rc.rate_correction_factor = factor;
346 } 296 }
347 } 297 }
348 298
349 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { 299 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) {
350 const int q = cpi->common.base_qindex; 300 const VP9_COMMON *const cm = &cpi->common;
351 int correction_factor = 100; 301 int correction_factor = 100;
352 double rate_correction_factor = get_rate_correction_factor(cpi); 302 double rate_correction_factor = get_rate_correction_factor(cpi);
353 double adjustment_limit; 303 double adjustment_limit;
354 304
355 int projected_size_based_on_q = 0; 305 int projected_size_based_on_q = 0;
356 306
357 // Clear down mmx registers to allow floating point in what follows 307 // Clear down mmx registers to allow floating point in what follows
358 vp9_clear_system_state(); 308 vp9_clear_system_state();
359 309
360 // Work out how big we would have expected the frame to be at this Q given 310 // Work out how big we would have expected the frame to be at this Q given
361 // the current correction factor. 311 // the current correction factor.
362 // Stay in double to avoid int overflow when values are large 312 // Stay in double to avoid int overflow when values are large
363 projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, 313 projected_size_based_on_q = estimate_bits_at_q(cm->frame_type,
364 cpi->common.MBs, 314 cm->base_qindex, cm->MBs,
365 rate_correction_factor); 315 rate_correction_factor);
366 // Work out a size correction factor. 316 // Work out a size correction factor.
367 if (projected_size_based_on_q > 0) 317 if (projected_size_based_on_q > 0)
368 correction_factor = (100 * cpi->rc.projected_frame_size) / 318 correction_factor = (100 * cpi->rc.projected_frame_size) /
369 projected_size_based_on_q; 319 projected_size_based_on_q;
370 320
371 // More heavily damped adjustment used if we have been oscillating either side 321 // More heavily damped adjustment used if we have been oscillating either side
372 // of target. 322 // of target.
373 switch (damp_var) { 323 switch (damp_var) {
374 case 0: 324 case 0:
375 adjustment_limit = 0.75; 325 adjustment_limit = 0.75;
376 break; 326 break;
377 case 1: 327 case 1:
378 adjustment_limit = 0.375; 328 adjustment_limit = 0.375;
379 break; 329 break;
380 case 2: 330 case 2:
381 default: 331 default:
382 adjustment_limit = 0.25; 332 adjustment_limit = 0.25;
383 break; 333 break;
384 } 334 }
385 335
386 if (correction_factor > 102) { 336 if (correction_factor > 102) {
387 // We are not already at the worst allowable quality 337 // We are not already at the worst allowable quality
388 correction_factor = 338 correction_factor = (int)(100 + ((correction_factor - 100) *
389 (int)(100 + ((correction_factor - 100) * adjustment_limit)); 339 adjustment_limit));
390 rate_correction_factor = 340 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
391 ((rate_correction_factor * correction_factor) / 100);
392 341
393 // Keep rate_correction_factor within limits 342 // Keep rate_correction_factor within limits
394 if (rate_correction_factor > MAX_BPB_FACTOR) 343 if (rate_correction_factor > MAX_BPB_FACTOR)
395 rate_correction_factor = MAX_BPB_FACTOR; 344 rate_correction_factor = MAX_BPB_FACTOR;
396 } else if (correction_factor < 99) { 345 } else if (correction_factor < 99) {
397 // We are not already at the best allowable quality 346 // We are not already at the best allowable quality
398 correction_factor = 347 correction_factor = (int)(100 - ((100 - correction_factor) *
399 (int)(100 - ((100 - correction_factor) * adjustment_limit)); 348 adjustment_limit));
400 rate_correction_factor = 349 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
401 ((rate_correction_factor * correction_factor) / 100);
402 350
403 // Keep rate_correction_factor within limits 351 // Keep rate_correction_factor within limits
404 if (rate_correction_factor < MIN_BPB_FACTOR) 352 if (rate_correction_factor < MIN_BPB_FACTOR)
405 rate_correction_factor = MIN_BPB_FACTOR; 353 rate_correction_factor = MIN_BPB_FACTOR;
406 } 354 }
407 355
408 set_rate_correction_factor(cpi, rate_correction_factor); 356 set_rate_correction_factor(cpi, rate_correction_factor);
409 } 357 }
410 358
411 359
412 int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame, 360 int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
413 int active_best_quality, int active_worst_quality) { 361 int active_best_quality, int active_worst_quality) {
414 const VP9_COMMON *const cm = &cpi->common; 362 const VP9_COMMON *const cm = &cpi->common;
415 int q = active_worst_quality; 363 int q = active_worst_quality;
416 int last_error = INT_MAX; 364 int last_error = INT_MAX;
417 int i, target_bits_per_mb; 365 int i, target_bits_per_mb;
418 const double correction_factor = get_rate_correction_factor(cpi); 366 const double correction_factor = get_rate_correction_factor(cpi);
419 367
420 // Calculate required scaling factor based on target frame size and size of 368 // Calculate required scaling factor based on target frame size and size of
421 // frame produced using previous Q. 369 // frame produced using previous Q.
422 if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) 370 target_bits_per_mb =
423 // Case where we would overflow int 371 ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
424 target_bits_per_mb = (target_bits_per_frame / cm->MBs) << BPER_MB_NORMBITS;
425 else
426 target_bits_per_mb = (target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
427 372
428 i = active_best_quality; 373 i = active_best_quality;
429 374
430 do { 375 do {
431 const int bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i, 376 const int bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i,
432 correction_factor); 377 correction_factor);
433 378
434 if (bits_per_mb_at_this_q <= target_bits_per_mb) { 379 if (bits_per_mb_at_this_q <= target_bits_per_mb) {
435 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) 380 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
436 q = i; 381 q = i;
(...skipping 18 matching lines...) Expand all
455 } else { 400 } else {
456 const int gap = high - low; 401 const int gap = high - low;
457 const int offset = high - gfu_boost; 402 const int offset = high - gfu_boost;
458 const int qdiff = high_motion_minq[q] - low_motion_minq[q]; 403 const int qdiff = high_motion_minq[q] - low_motion_minq[q];
459 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; 404 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
460 return low_motion_minq[q] + adjustment; 405 return low_motion_minq[q] + adjustment;
461 } 406 }
462 } 407 }
463 408
464 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) { 409 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
410 const RATE_CONTROL *const rc = &cpi->rc;
411 const unsigned int curr_frame = cpi->common.current_video_frame;
465 int active_worst_quality; 412 int active_worst_quality;
413
466 if (cpi->common.frame_type == KEY_FRAME) { 414 if (cpi->common.frame_type == KEY_FRAME) {
467 if (cpi->common.current_video_frame == 0) { 415 active_worst_quality = curr_frame == 0 ? rc->worst_quality
468 active_worst_quality = cpi->rc.worst_quality; 416 : rc->last_q[KEY_FRAME] * 2;
417 } else {
418 if (!rc->is_src_frame_alt_ref &&
419 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
420 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
421 : rc->last_q[INTER_FRAME];
469 } else { 422 } else {
470 // Choose active worst quality twice as large as the last q. 423 active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
471 active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2; 424 : rc->last_q[INTER_FRAME] * 2;
472 }
473 } else if (!cpi->rc.is_src_frame_alt_ref &&
474 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
475 if (cpi->common.current_video_frame == 1) {
476 active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 5 / 4;
477 } else {
478 // Choose active worst quality twice as large as the last q.
479 active_worst_quality = cpi->rc.last_q[INTER_FRAME];
480 }
481 } else {
482 if (cpi->common.current_video_frame == 1) {
483 active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2;
484 } else {
485 // Choose active worst quality twice as large as the last q.
486 active_worst_quality = cpi->rc.last_q[INTER_FRAME] * 2;
487 } 425 }
488 } 426 }
489 if (active_worst_quality > cpi->rc.worst_quality) 427
490 active_worst_quality = cpi->rc.worst_quality; 428 return MIN(active_worst_quality, rc->worst_quality);
491 return active_worst_quality;
492 } 429 }
493 430
494 // Adjust active_worst_quality level based on buffer level. 431 // Adjust active_worst_quality level based on buffer level.
495 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) { 432 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
496 // Adjust active_worst_quality: If buffer is above the optimal/target level, 433 // Adjust active_worst_quality: If buffer is above the optimal/target level,
497 // bring active_worst_quality down depending on fullness of buffer. 434 // bring active_worst_quality down depending on fullness of buffer.
498 // If buffer is below the optimal level, let the active_worst_quality go from 435 // If buffer is below the optimal level, let the active_worst_quality go from
499 // ambient Q (at buffer = optimal level) to worst_quality level 436 // ambient Q (at buffer = optimal level) to worst_quality level
500 // (at buffer = critical level). 437 // (at buffer = critical level).
438 const VP9_COMMON *const cm = &cpi->common;
501 const VP9_CONFIG *oxcf = &cpi->oxcf; 439 const VP9_CONFIG *oxcf = &cpi->oxcf;
502 const RATE_CONTROL *rc = &cpi->rc; 440 const RATE_CONTROL *rc = &cpi->rc;
503 // Buffer level below which we push active_worst to worst_quality. 441 // Buffer level below which we push active_worst to worst_quality.
504 int64_t critical_level = oxcf->optimal_buffer_level >> 2; 442 int64_t critical_level = oxcf->optimal_buffer_level >> 2;
505 int64_t buff_lvl_step = 0; 443 int64_t buff_lvl_step = 0;
506 int adjustment = 0; 444 int adjustment = 0;
507 int active_worst_quality; 445 int active_worst_quality;
508 if (cpi->common.frame_type == KEY_FRAME) 446 if (cm->frame_type == KEY_FRAME)
509 return rc->worst_quality; 447 return rc->worst_quality;
510 if (cpi->common.current_video_frame > 1) 448 if (cm->current_video_frame > 1)
511 active_worst_quality = MIN(rc->worst_quality, 449 active_worst_quality = MIN(rc->worst_quality,
512 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4); 450 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4);
513 else 451 else
514 active_worst_quality = MIN(rc->worst_quality, 452 active_worst_quality = MIN(rc->worst_quality,
515 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2); 453 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2);
516 if (rc->buffer_level > oxcf->optimal_buffer_level) { 454 if (rc->buffer_level > oxcf->optimal_buffer_level) {
517 // Adjust down. 455 // Adjust down.
518 // Maximum limit for down adjustment, ~30%. 456 // Maximum limit for down adjustment, ~30%.
519 int max_adjustment_down = active_worst_quality / 3; 457 int max_adjustment_down = active_worst_quality / 3;
520 if (max_adjustment_down) { 458 if (max_adjustment_down) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 int q; 492 int q;
555 493
556 if (frame_is_intra_only(cm)) { 494 if (frame_is_intra_only(cm)) {
557 active_best_quality = rc->best_quality; 495 active_best_quality = rc->best_quality;
558 // Handle the special case for key frames forced when we have75 reached 496 // Handle the special case for key frames forced when we have75 reached
559 // the maximum key frame interval. Here force the Q to a range 497 // the maximum key frame interval. Here force the Q to a range
560 // based on the ambient Q to reduce the risk of popping. 498 // based on the ambient Q to reduce the risk of popping.
561 if (rc->this_key_frame_forced) { 499 if (rc->this_key_frame_forced) {
562 int qindex = rc->last_boosted_qindex; 500 int qindex = rc->last_boosted_qindex;
563 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 501 double last_boosted_q = vp9_convert_qindex_to_q(qindex);
564 int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, 502 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
565 (last_boosted_q * 0.75)); 503 (last_boosted_q * 0.75));
566 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 504 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
567 } else if (cm->current_video_frame > 0) { 505 } else if (cm->current_video_frame > 0) {
568 // not first frame of one pass and kf_boost is set 506 // not first frame of one pass and kf_boost is set
569 double q_adj_factor = 1.0; 507 double q_adj_factor = 1.0;
570 double q_val; 508 double q_val;
571 509
572 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME], 510 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME],
573 rc->kf_boost, 511 rc->kf_boost,
574 kf_low, kf_high, 512 kf_low, kf_high,
575 kf_low_motion_minq, 513 kf_low_motion_minq,
576 kf_high_motion_minq); 514 kf_high_motion_minq);
577 515
578 // Allow somewhat lower kf minq with small image formats. 516 // Allow somewhat lower kf minq with small image formats.
579 if ((cm->width * cm->height) <= (352 * 288)) { 517 if ((cm->width * cm->height) <= (352 * 288)) {
580 q_adj_factor -= 0.25; 518 q_adj_factor -= 0.25;
581 } 519 }
582 520
583 // Convert the adjustment factor to a qindex delta 521 // Convert the adjustment factor to a qindex delta
584 // on active_best_quality. 522 // on active_best_quality.
585 q_val = vp9_convert_qindex_to_q(active_best_quality); 523 q_val = vp9_convert_qindex_to_q(active_best_quality);
586 active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * 524 active_best_quality += vp9_compute_qdelta(rc, q_val,
587 q_adj_factor); 525 q_val * q_adj_factor);
588 } 526 }
589 } else if (!rc->is_src_frame_alt_ref && 527 } else if (!rc->is_src_frame_alt_ref &&
528 !cpi->use_svc &&
590 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 529 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
591 // Use the lower of active_worst_quality and recent 530 // Use the lower of active_worst_quality and recent
592 // average Q as basis for GF/ARF best Q limit unless last frame was 531 // average Q as basis for GF/ARF best Q limit unless last frame was
593 // a key frame. 532 // a key frame.
594 if (rc->frames_since_key > 1 && 533 if (rc->frames_since_key > 1 &&
595 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 534 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
596 q = rc->avg_frame_qindex[INTER_FRAME]; 535 q = rc->avg_frame_qindex[INTER_FRAME];
597 } else { 536 } else {
598 q = active_worst_quality; 537 q = active_worst_quality;
599 } 538 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 } 571 }
633 #endif 572 #endif
634 // Special case code to try and match quality with forced key frames 573 // Special case code to try and match quality with forced key frames
635 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) { 574 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
636 q = rc->last_boosted_qindex; 575 q = rc->last_boosted_qindex;
637 } else { 576 } else {
638 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 577 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
639 active_best_quality, active_worst_quality); 578 active_best_quality, active_worst_quality);
640 if (q > *top_index) { 579 if (q > *top_index) {
641 // Special case when we are targeting the max allowed rate 580 // Special case when we are targeting the max allowed rate
642 if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) 581 if (rc->this_frame_target >= rc->max_frame_bandwidth)
643 *top_index = q; 582 *top_index = q;
644 else 583 else
645 q = *top_index; 584 q = *top_index;
646 } 585 }
647 } 586 }
648 assert(*top_index <= rc->worst_quality && 587 assert(*top_index <= rc->worst_quality &&
649 *top_index >= rc->best_quality); 588 *top_index >= rc->best_quality);
650 assert(*bottom_index <= rc->worst_quality && 589 assert(*bottom_index <= rc->worst_quality &&
651 *bottom_index >= rc->best_quality); 590 *bottom_index >= rc->best_quality);
652 assert(q <= rc->worst_quality && q >= rc->best_quality); 591 assert(q <= rc->worst_quality && q >= rc->best_quality);
(...skipping 12 matching lines...) Expand all
665 604
666 if (frame_is_intra_only(cm)) { 605 if (frame_is_intra_only(cm)) {
667 active_best_quality = rc->best_quality; 606 active_best_quality = rc->best_quality;
668 #if !CONFIG_MULTIPLE_ARF 607 #if !CONFIG_MULTIPLE_ARF
669 // Handle the special case for key frames forced when we have75 reached 608 // Handle the special case for key frames forced when we have75 reached
670 // the maximum key frame interval. Here force the Q to a range 609 // the maximum key frame interval. Here force the Q to a range
671 // based on the ambient Q to reduce the risk of popping. 610 // based on the ambient Q to reduce the risk of popping.
672 if (rc->this_key_frame_forced) { 611 if (rc->this_key_frame_forced) {
673 int qindex = rc->last_boosted_qindex; 612 int qindex = rc->last_boosted_qindex;
674 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 613 double last_boosted_q = vp9_convert_qindex_to_q(qindex);
675 int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, 614 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
676 (last_boosted_q * 0.75)); 615 last_boosted_q * 0.75);
677 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 616 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
678 } else if (cm->current_video_frame > 0) { 617 } else if (cm->current_video_frame > 0) {
679 // not first frame of one pass and kf_boost is set 618 // not first frame of one pass and kf_boost is set
680 double q_adj_factor = 1.0; 619 double q_adj_factor = 1.0;
681 double q_val; 620 double q_val;
682 621
683 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME], 622 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME],
684 rc->kf_boost, 623 rc->kf_boost,
685 kf_low, kf_high, 624 kf_low, kf_high,
686 kf_low_motion_minq, 625 kf_low_motion_minq,
687 kf_high_motion_minq); 626 kf_high_motion_minq);
688 627
689 // Allow somewhat lower kf minq with small image formats. 628 // Allow somewhat lower kf minq with small image formats.
690 if ((cm->width * cm->height) <= (352 * 288)) { 629 if ((cm->width * cm->height) <= (352 * 288)) {
691 q_adj_factor -= 0.25; 630 q_adj_factor -= 0.25;
692 } 631 }
693 632
694 // Convert the adjustment factor to a qindex delta 633 // Convert the adjustment factor to a qindex delta
695 // on active_best_quality. 634 // on active_best_quality.
696 q_val = vp9_convert_qindex_to_q(active_best_quality); 635 q_val = vp9_convert_qindex_to_q(active_best_quality);
697 active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * 636 active_best_quality += vp9_compute_qdelta(rc, q_val,
698 q_adj_factor); 637 q_val * q_adj_factor);
699 } 638 }
700 #else 639 #else
701 double current_q; 640 double current_q;
702 // Force the KF quantizer to be 30% of the active_worst_quality. 641 // Force the KF quantizer to be 30% of the active_worst_quality.
703 current_q = vp9_convert_qindex_to_q(active_worst_quality); 642 current_q = vp9_convert_qindex_to_q(active_worst_quality);
704 active_best_quality = active_worst_quality 643 active_best_quality = active_worst_quality
705 + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); 644 + vp9_compute_qdelta(rc, current_q, current_q * 0.3);
706 #endif 645 #endif
707 } else if (!rc->is_src_frame_alt_ref && 646 } else if (!rc->is_src_frame_alt_ref &&
708 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 647 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
709 // Use the lower of active_worst_quality and recent 648 // Use the lower of active_worst_quality and recent
710 // average Q as basis for GF/ARF best Q limit unless last frame was 649 // average Q as basis for GF/ARF best Q limit unless last frame was
711 // a key frame. 650 // a key frame.
712 if (rc->frames_since_key > 1 && 651 if (rc->frames_since_key > 1 &&
713 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 652 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
714 q = rc->avg_frame_qindex[INTER_FRAME]; 653 q = rc->avg_frame_qindex[INTER_FRAME];
715 } else { 654 } else {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { 737 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) {
799 q = active_best_quality; 738 q = active_best_quality;
800 // Special case code to try and match quality with forced key frames 739 // Special case code to try and match quality with forced key frames
801 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { 740 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
802 q = rc->last_boosted_qindex; 741 q = rc->last_boosted_qindex;
803 } else { 742 } else {
804 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 743 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
805 active_best_quality, active_worst_quality); 744 active_best_quality, active_worst_quality);
806 if (q > *top_index) { 745 if (q > *top_index) {
807 // Special case when we are targeting the max allowed rate 746 // Special case when we are targeting the max allowed rate
808 if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) 747 if (rc->this_frame_target >= rc->max_frame_bandwidth)
809 *top_index = q; 748 *top_index = q;
810 else 749 else
811 q = *top_index; 750 q = *top_index;
812 } 751 }
813 } 752 }
814 #if CONFIG_MULTIPLE_ARF 753 #if CONFIG_MULTIPLE_ARF
815 // Force the quantizer determined by the coding order pattern. 754 // Force the quantizer determined by the coding order pattern.
816 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 755 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
817 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { 756 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) {
818 double new_q; 757 double new_q;
819 double current_q = vp9_convert_qindex_to_q(active_worst_quality); 758 double current_q = vp9_convert_qindex_to_q(active_worst_quality);
820 int level = cpi->this_frame_weight; 759 int level = cpi->this_frame_weight;
821 assert(level >= 0); 760 assert(level >= 0);
822 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); 761 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
823 q = active_worst_quality + 762 q = active_worst_quality +
824 vp9_compute_qdelta(cpi, current_q, new_q); 763 vp9_compute_qdelta(rc, current_q, new_q);
825 764
826 *bottom_index = q; 765 *bottom_index = q;
827 *top_index = q; 766 *top_index = q;
828 printf("frame:%d q:%d\n", cm->current_video_frame, q); 767 printf("frame:%d q:%d\n", cm->current_video_frame, q);
829 } 768 }
830 #endif 769 #endif
831 assert(*top_index <= rc->worst_quality && 770 assert(*top_index <= rc->worst_quality &&
832 *top_index >= rc->best_quality); 771 *top_index >= rc->best_quality);
833 assert(*bottom_index <= rc->worst_quality && 772 assert(*bottom_index <= rc->worst_quality &&
834 *bottom_index >= rc->best_quality); 773 *bottom_index >= rc->best_quality);
(...skipping 12 matching lines...) Expand all
847 int q; 786 int q;
848 787
849 if (frame_is_intra_only(cm)) { 788 if (frame_is_intra_only(cm)) {
850 #if !CONFIG_MULTIPLE_ARF 789 #if !CONFIG_MULTIPLE_ARF
851 // Handle the special case for key frames forced when we have75 reached 790 // Handle the special case for key frames forced when we have75 reached
852 // the maximum key frame interval. Here force the Q to a range 791 // the maximum key frame interval. Here force the Q to a range
853 // based on the ambient Q to reduce the risk of popping. 792 // based on the ambient Q to reduce the risk of popping.
854 if (rc->this_key_frame_forced) { 793 if (rc->this_key_frame_forced) {
855 int qindex = rc->last_boosted_qindex; 794 int qindex = rc->last_boosted_qindex;
856 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 795 double last_boosted_q = vp9_convert_qindex_to_q(qindex);
857 int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, 796 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
858 (last_boosted_q * 0.75)); 797 last_boosted_q * 0.75);
859 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 798 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
860 } else { 799 } else {
861 // Not forced keyframe. 800 // Not forced keyframe.
862 double q_adj_factor = 1.0; 801 double q_adj_factor = 1.0;
863 double q_val; 802 double q_val;
864 // Baseline value derived from cpi->active_worst_quality and kf boost. 803 // Baseline value derived from cpi->active_worst_quality and kf boost.
865 active_best_quality = get_active_quality(active_worst_quality, 804 active_best_quality = get_active_quality(active_worst_quality,
866 rc->kf_boost, 805 rc->kf_boost,
867 kf_low, kf_high, 806 kf_low, kf_high,
868 kf_low_motion_minq, 807 kf_low_motion_minq,
869 kf_high_motion_minq); 808 kf_high_motion_minq);
870 809
871 // Allow somewhat lower kf minq with small image formats. 810 // Allow somewhat lower kf minq with small image formats.
872 if ((cm->width * cm->height) <= (352 * 288)) { 811 if ((cm->width * cm->height) <= (352 * 288)) {
873 q_adj_factor -= 0.25; 812 q_adj_factor -= 0.25;
874 } 813 }
875 814
876 // Make a further adjustment based on the kf zero motion measure. 815 // Make a further adjustment based on the kf zero motion measure.
877 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct); 816 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
878 817
879 // Convert the adjustment factor to a qindex delta 818 // Convert the adjustment factor to a qindex delta
880 // on active_best_quality. 819 // on active_best_quality.
881 q_val = vp9_convert_qindex_to_q(active_best_quality); 820 q_val = vp9_convert_qindex_to_q(active_best_quality);
882 active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * 821 active_best_quality += vp9_compute_qdelta(rc, q_val,
883 q_adj_factor); 822 q_val * q_adj_factor);
884 } 823 }
885 #else 824 #else
886 double current_q; 825 double current_q;
887 // Force the KF quantizer to be 30% of the active_worst_quality. 826 // Force the KF quantizer to be 30% of the active_worst_quality.
888 current_q = vp9_convert_qindex_to_q(active_worst_quality); 827 current_q = vp9_convert_qindex_to_q(active_worst_quality);
889 active_best_quality = active_worst_quality 828 active_best_quality = active_worst_quality
890 + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); 829 + vp9_compute_qdelta(rc, current_q, current_q * 0.3);
891 #endif 830 #endif
892 } else if (!rc->is_src_frame_alt_ref && 831 } else if (!rc->is_src_frame_alt_ref &&
893 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 832 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
894 // Use the lower of active_worst_quality and recent 833 // Use the lower of active_worst_quality and recent
895 // average Q as basis for GF/ARF best Q limit unless last frame was 834 // average Q as basis for GF/ARF best Q limit unless last frame was
896 // a key frame. 835 // a key frame.
897 if (rc->frames_since_key > 1 && 836 if (rc->frames_since_key > 1 &&
898 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 837 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
899 q = rc->avg_frame_qindex[INTER_FRAME]; 838 q = rc->avg_frame_qindex[INTER_FRAME];
900 } else { 839 } else {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
981 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { 920 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) {
982 q = active_best_quality; 921 q = active_best_quality;
983 // Special case code to try and match quality with forced key frames. 922 // Special case code to try and match quality with forced key frames.
984 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { 923 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
985 q = rc->last_boosted_qindex; 924 q = rc->last_boosted_qindex;
986 } else { 925 } else {
987 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 926 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
988 active_best_quality, active_worst_quality); 927 active_best_quality, active_worst_quality);
989 if (q > *top_index) { 928 if (q > *top_index) {
990 // Special case when we are targeting the max allowed rate. 929 // Special case when we are targeting the max allowed rate.
991 if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) 930 if (rc->this_frame_target >= rc->max_frame_bandwidth)
992 *top_index = q; 931 *top_index = q;
993 else 932 else
994 q = *top_index; 933 q = *top_index;
995 } 934 }
996 } 935 }
997 #if CONFIG_MULTIPLE_ARF 936 #if CONFIG_MULTIPLE_ARF
998 // Force the quantizer determined by the coding order pattern. 937 // Force the quantizer determined by the coding order pattern.
999 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 938 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
1000 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { 939 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) {
1001 double new_q; 940 double new_q;
1002 double current_q = vp9_convert_qindex_to_q(active_worst_quality); 941 double current_q = vp9_convert_qindex_to_q(active_worst_quality);
1003 int level = cpi->this_frame_weight; 942 int level = cpi->this_frame_weight;
1004 assert(level >= 0); 943 assert(level >= 0);
1005 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); 944 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
1006 q = active_worst_quality + 945 q = active_worst_quality +
1007 vp9_compute_qdelta(cpi, current_q, new_q); 946 vp9_compute_qdelta(rc, current_q, new_q);
1008 947
1009 *bottom_index = q; 948 *bottom_index = q;
1010 *top_index = q; 949 *top_index = q;
1011 printf("frame:%d q:%d\n", cm->current_video_frame, q); 950 printf("frame:%d q:%d\n", cm->current_video_frame, q);
1012 } 951 }
1013 #endif 952 #endif
1014 assert(*top_index <= rc->worst_quality && 953 assert(*top_index <= rc->worst_quality &&
1015 *top_index >= rc->best_quality); 954 *top_index >= rc->best_quality);
1016 assert(*bottom_index <= rc->worst_quality && 955 assert(*bottom_index <= rc->worst_quality &&
1017 *bottom_index >= rc->best_quality); 956 *bottom_index >= rc->best_quality);
1018 assert(q <= rc->worst_quality && q >= rc->best_quality); 957 assert(q <= rc->worst_quality && q >= rc->best_quality);
1019 return q; 958 return q;
1020 } 959 }
1021 960
1022 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, 961 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
1023 int *bottom_index, 962 int *bottom_index, int *top_index) {
1024 int *top_index) {
1025 int q; 963 int q;
1026 if (cpi->pass == 0) { 964 if (cpi->pass == 0) {
1027 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 965 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1028 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index); 966 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
1029 else 967 else
1030 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index); 968 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
1031 } else { 969 } else {
1032 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index); 970 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
1033 } 971 }
1034 972
1035 // JBB : This is realtime mode. In real time mode the first frame 973 // Q of 0 is disabled because we force tx size to be
1036 // should be larger. Q of 0 is disabled because we force tx size to be
1037 // 16x16... 974 // 16x16...
1038 if (cpi->sf.use_nonrd_pick_mode) { 975 if (cpi->sf.use_nonrd_pick_mode) {
1039 if (cpi->common.current_video_frame == 0)
1040 q /= 3;
1041 if (q == 0) 976 if (q == 0)
1042 q++; 977 q++;
978 if (cpi->sf.force_frame_boost == 1)
979 q -= cpi->sf.max_delta_qindex;
980
1043 if (q < *bottom_index) 981 if (q < *bottom_index)
1044 *bottom_index = q; 982 *bottom_index = q;
1045 else if (q > *top_index) 983 else if (q > *top_index)
1046 *top_index = q; 984 *top_index = q;
1047 } 985 }
1048 return q; 986 return q;
1049 } 987 }
1050 988
1051 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, 989 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
1052 int this_frame_target, 990 int this_frame_target,
1053 int *frame_under_shoot_limit, 991 int *frame_under_shoot_limit,
1054 int *frame_over_shoot_limit) { 992 int *frame_over_shoot_limit) {
1055 // Set-up bounds on acceptable frame size: 993 // Set-up bounds on acceptable frame size:
1056 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 994 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
1057 *frame_under_shoot_limit = 0; 995 *frame_under_shoot_limit = 0;
1058 *frame_over_shoot_limit = INT_MAX; 996 *frame_over_shoot_limit = INT_MAX;
1059 } else { 997 } else {
1060 if (cpi->common.frame_type == KEY_FRAME) { 998 int recode_tolerance =
1061 *frame_over_shoot_limit = this_frame_target * 9 / 8; 999 (cpi->sf.recode_tolerance * this_frame_target) / 100;
1062 *frame_under_shoot_limit = this_frame_target * 7 / 8; 1000
1063 } else { 1001 *frame_over_shoot_limit = this_frame_target + recode_tolerance;
1064 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) { 1002 *frame_under_shoot_limit = this_frame_target - recode_tolerance;
1065 *frame_over_shoot_limit = this_frame_target * 9 / 8;
1066 *frame_under_shoot_limit = this_frame_target * 7 / 8;
1067 } else {
1068 // Stron overshoot limit for constrained quality
1069 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
1070 *frame_over_shoot_limit = this_frame_target * 11 / 8;
1071 *frame_under_shoot_limit = this_frame_target * 2 / 8;
1072 } else {
1073 *frame_over_shoot_limit = this_frame_target * 11 / 8;
1074 *frame_under_shoot_limit = this_frame_target * 5 / 8;
1075 }
1076 }
1077 }
1078 1003
1079 // For very small rate targets where the fractional adjustment 1004 // For very small rate targets where the fractional adjustment
1080 // (eg * 7/8) may be tiny make sure there is at least a minimum 1005 // may be tiny make sure there is at least a minimum range.
1081 // range.
1082 *frame_over_shoot_limit += 200; 1006 *frame_over_shoot_limit += 200;
1083 *frame_under_shoot_limit -= 200; 1007 *frame_under_shoot_limit -= 200;
1084 if (*frame_under_shoot_limit < 0) 1008 if (*frame_under_shoot_limit < 0)
1085 *frame_under_shoot_limit = 0; 1009 *frame_under_shoot_limit = 0;
1086 1010
1087 // Clip to maximum allowed rate for a frame. 1011 // Clip to maximum allowed rate for a frame.
1088 if (*frame_over_shoot_limit > cpi->rc.max_frame_bandwidth) { 1012 if (*frame_over_shoot_limit > cpi->rc.max_frame_bandwidth) {
1089 *frame_over_shoot_limit = cpi->rc.max_frame_bandwidth; 1013 *frame_over_shoot_limit = cpi->rc.max_frame_bandwidth;
1090 } 1014 }
1091 } 1015 }
1092 } 1016 }
1093 1017
1094 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) { 1018 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
1095 const VP9_COMMON *const cm = &cpi->common; 1019 const VP9_COMMON *const cm = &cpi->common;
1096 RATE_CONTROL *const rc = &cpi->rc; 1020 RATE_CONTROL *const rc = &cpi->rc;
1097 1021
1098 rc->this_frame_target = target; 1022 rc->this_frame_target = target;
1099 // Target rate per SB64 (including partial SB64s. 1023 // Target rate per SB64 (including partial SB64s.
1100 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) / 1024 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) /
1101 (cm->width * cm->height); 1025 (cm->width * cm->height);
1102 } 1026 }
1103 1027
1104 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { 1028 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
1105 // this frame refreshes means next frames don't unless specified by user 1029 // this frame refreshes means next frames don't unless specified by user
1106 cpi->rc.frames_since_golden = 0; 1030 RATE_CONTROL *const rc = &cpi->rc;
1031 rc->frames_since_golden = 0;
1107 1032
1108 #if CONFIG_MULTIPLE_ARF 1033 #if CONFIG_MULTIPLE_ARF
1109 if (!cpi->multi_arf_enabled) 1034 if (!cpi->multi_arf_enabled)
1110 #endif 1035 #endif
1111 // Clear the alternate reference update pending flag. 1036 // Clear the alternate reference update pending flag.
1112 cpi->rc.source_alt_ref_pending = 0; 1037 rc->source_alt_ref_pending = 0;
1113 1038
1114 // Set the alternate reference frame active flag 1039 // Set the alternate reference frame active flag
1115 cpi->rc.source_alt_ref_active = 1; 1040 rc->source_alt_ref_active = 1;
1116 } 1041 }
1117 1042
1118 static void update_golden_frame_stats(VP9_COMP *cpi) { 1043 static void update_golden_frame_stats(VP9_COMP *cpi) {
1119 RATE_CONTROL *const rc = &cpi->rc; 1044 RATE_CONTROL *const rc = &cpi->rc;
1120 1045
1121 // Update the Golden frame usage counts. 1046 // Update the Golden frame usage counts.
1122 if (cpi->refresh_golden_frame) { 1047 if (cpi->refresh_golden_frame) {
1123 // this frame refreshes means next frames don't unless specified by user 1048 // this frame refreshes means next frames don't unless specified by user
1124 rc->frames_since_golden = 0; 1049 rc->frames_since_golden = 0;
1125 1050
1126 if (!rc->source_alt_ref_pending) 1051 if (!rc->source_alt_ref_pending)
1127 rc->source_alt_ref_active = 0; 1052 rc->source_alt_ref_active = 0;
1128 1053
1129 // Decrement count down till next gf 1054 // Decrement count down till next gf
1130 if (rc->frames_till_gf_update_due > 0) 1055 if (rc->frames_till_gf_update_due > 0)
1131 rc->frames_till_gf_update_due--; 1056 rc->frames_till_gf_update_due--;
1132 1057
1133 } else if (!cpi->refresh_alt_ref_frame) { 1058 } else if (!cpi->refresh_alt_ref_frame) {
1134 // Decrement count down till next gf 1059 // Decrement count down till next gf
1135 if (rc->frames_till_gf_update_due > 0) 1060 if (rc->frames_till_gf_update_due > 0)
1136 rc->frames_till_gf_update_due--; 1061 rc->frames_till_gf_update_due--;
1137 1062
1138 rc->frames_since_golden++; 1063 rc->frames_since_golden++;
1139 } 1064 }
1140 } 1065 }
1141 1066
1142 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { 1067 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
1143 VP9_COMMON *const cm = &cpi->common; 1068 VP9_COMMON *const cm = &cpi->common;
1069 const VP9_CONFIG *const oxcf = &cpi->oxcf;
1144 RATE_CONTROL *const rc = &cpi->rc; 1070 RATE_CONTROL *const rc = &cpi->rc;
1145 1071
1146 cm->last_frame_type = cm->frame_type; 1072 cm->last_frame_type = cm->frame_type;
1147 // Update rate control heuristics 1073 // Update rate control heuristics
1148 rc->projected_frame_size = (int)(bytes_used << 3); 1074 rc->projected_frame_size = (int)(bytes_used << 3);
1149 1075
1150 // Post encode loop adjustment of Q prediction. 1076 // Post encode loop adjustment of Q prediction.
1151 vp9_rc_update_rate_correction_factors( 1077 vp9_rc_update_rate_correction_factors(
1152 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF || 1078 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF ||
1153 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0); 1079 oxcf->end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0);
1154 1080
1155 // Keep a record of last Q and ambient average Q. 1081 // Keep a record of last Q and ambient average Q.
1156 if (cm->frame_type == KEY_FRAME) { 1082 if (cm->frame_type == KEY_FRAME) {
1157 rc->last_q[KEY_FRAME] = cm->base_qindex; 1083 rc->last_q[KEY_FRAME] = cm->base_qindex;
1158 rc->avg_frame_qindex[KEY_FRAME] = ROUND_POWER_OF_TWO( 1084 rc->avg_frame_qindex[KEY_FRAME] = ROUND_POWER_OF_TWO(
1159 3 * rc->avg_frame_qindex[KEY_FRAME] + cm->base_qindex, 2); 1085 3 * rc->avg_frame_qindex[KEY_FRAME] + cm->base_qindex, 2);
1160 } else if (!rc->is_src_frame_alt_ref && 1086 } else if (!rc->is_src_frame_alt_ref &&
1161 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) && 1087 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) &&
1162 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) { 1088 !(cpi->use_svc && oxcf->end_usage == USAGE_STREAM_FROM_SERVER)) {
1163 rc->last_q[2] = cm->base_qindex; 1089 rc->last_q[2] = cm->base_qindex;
1164 rc->avg_frame_qindex[2] = ROUND_POWER_OF_TWO( 1090 rc->avg_frame_qindex[2] = ROUND_POWER_OF_TWO(
1165 3 * rc->avg_frame_qindex[2] + cm->base_qindex, 2); 1091 3 * rc->avg_frame_qindex[2] + cm->base_qindex, 2);
1166 } else { 1092 } else {
1167 rc->last_q[INTER_FRAME] = cm->base_qindex; 1093 rc->last_q[INTER_FRAME] = cm->base_qindex;
1168 rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO( 1094 rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO(
1169 3 * rc->avg_frame_qindex[INTER_FRAME] + cm->base_qindex, 2); 1095 3 * rc->avg_frame_qindex[INTER_FRAME] + cm->base_qindex, 2);
1170 rc->ni_frames++; 1096 rc->ni_frames++;
1171 rc->tot_q += vp9_convert_qindex_to_q(cm->base_qindex); 1097 rc->tot_q += vp9_convert_qindex_to_q(cm->base_qindex);
1172 rc->avg_q = rc->tot_q / (double)rc->ni_frames; 1098 rc->avg_q = rc->tot_q / (double)rc->ni_frames;
(...skipping 25 matching lines...) Expand all
1198 rc->rolling_actual_bits = ROUND_POWER_OF_TWO( 1124 rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
1199 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2); 1125 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
1200 rc->long_rolling_target_bits = ROUND_POWER_OF_TWO( 1126 rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
1201 rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5); 1127 rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
1202 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO( 1128 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
1203 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5); 1129 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
1204 } 1130 }
1205 1131
1206 // Actual bits spent 1132 // Actual bits spent
1207 rc->total_actual_bits += rc->projected_frame_size; 1133 rc->total_actual_bits += rc->projected_frame_size;
1134 rc->total_target_bits += (cm->show_frame ? rc->av_per_frame_bandwidth : 0);
1208 1135
1209 // Debug stats 1136 rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
1210 rc->total_target_vs_actual += (rc->this_frame_target -
1211 rc->projected_frame_size);
1212 1137
1213 if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame && 1138 if (oxcf->play_alternate && cpi->refresh_alt_ref_frame &&
1214 (cm->frame_type != KEY_FRAME)) 1139 (cm->frame_type != KEY_FRAME))
1215 // Update the alternate reference frame stats as appropriate. 1140 // Update the alternate reference frame stats as appropriate.
1216 update_alt_ref_frame_stats(cpi); 1141 update_alt_ref_frame_stats(cpi);
1217 else 1142 else
1218 // Update the Golden frame stats as appropriate. 1143 // Update the Golden frame stats as appropriate.
1219 update_golden_frame_stats(cpi); 1144 update_golden_frame_stats(cpi);
1220 1145
1221 if (cm->frame_type == KEY_FRAME) 1146 if (cm->frame_type == KEY_FRAME)
1222 rc->frames_since_key = 0; 1147 rc->frames_since_key = 0;
1223 if (cm->show_frame) { 1148 if (cm->show_frame) {
(...skipping 12 matching lines...) Expand all
1236 1161
1237 static int test_for_kf_one_pass(VP9_COMP *cpi) { 1162 static int test_for_kf_one_pass(VP9_COMP *cpi) {
1238 // Placeholder function for auto key frame 1163 // Placeholder function for auto key frame
1239 return 0; 1164 return 0;
1240 } 1165 }
1241 // Use this macro to turn on/off use of alt-refs in one-pass mode. 1166 // Use this macro to turn on/off use of alt-refs in one-pass mode.
1242 #define USE_ALTREF_FOR_ONE_PASS 1 1167 #define USE_ALTREF_FOR_ONE_PASS 1
1243 1168
1244 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { 1169 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1245 static const int af_ratio = 10; 1170 static const int af_ratio = 10;
1246 const RATE_CONTROL *rc = &cpi->rc; 1171 const RATE_CONTROL *const rc = &cpi->rc;
1247 int target; 1172 int target;
1248 #if USE_ALTREF_FOR_ONE_PASS 1173 #if USE_ALTREF_FOR_ONE_PASS
1249 target = (!rc->is_src_frame_alt_ref && 1174 target = (!rc->is_src_frame_alt_ref &&
1250 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ? 1175 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ?
1251 (rc->av_per_frame_bandwidth * cpi->rc.baseline_gf_interval * af_ratio) / 1176 (rc->av_per_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
1252 (cpi->rc.baseline_gf_interval + af_ratio - 1) : 1177 (rc->baseline_gf_interval + af_ratio - 1) :
1253 (rc->av_per_frame_bandwidth * cpi->rc.baseline_gf_interval) / 1178 (rc->av_per_frame_bandwidth * rc->baseline_gf_interval) /
1254 (cpi->rc.baseline_gf_interval + af_ratio - 1); 1179 (rc->baseline_gf_interval + af_ratio - 1);
1255 #else 1180 #else
1256 target = rc->av_per_frame_bandwidth; 1181 target = rc->av_per_frame_bandwidth;
1257 #endif 1182 #endif
1258 return vp9_rc_clamp_pframe_target_size(cpi, target); 1183 return vp9_rc_clamp_pframe_target_size(cpi, target);
1259 } 1184 }
1260 1185
1261 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { 1186 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1262 static const int kf_ratio = 25; 1187 static const int kf_ratio = 25;
1263 const RATE_CONTROL *rc = &cpi->rc; 1188 const RATE_CONTROL *rc = &cpi->rc;
1264 int target = rc->av_per_frame_bandwidth * kf_ratio; 1189 int target = rc->av_per_frame_bandwidth * kf_ratio;
1265 return vp9_rc_clamp_iframe_target_size(cpi, target); 1190 return vp9_rc_clamp_iframe_target_size(cpi, target);
1266 } 1191 }
1267 1192
1268 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) { 1193 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
1269 VP9_COMMON *const cm = &cpi->common; 1194 VP9_COMMON *const cm = &cpi->common;
1270 RATE_CONTROL *const rc = &cpi->rc; 1195 RATE_CONTROL *const rc = &cpi->rc;
1271 int target; 1196 int target;
1272 if (!cpi->refresh_alt_ref_frame && 1197 if (!cpi->refresh_alt_ref_frame &&
1273 (cm->current_video_frame == 0 || 1198 (cm->current_video_frame == 0 ||
1274 cm->frame_flags & FRAMEFLAGS_KEY || 1199 (cm->frame_flags & FRAMEFLAGS_KEY) ||
1275 rc->frames_to_key == 0 || 1200 rc->frames_to_key == 0 ||
1276 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) { 1201 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) {
1277 cm->frame_type = KEY_FRAME; 1202 cm->frame_type = KEY_FRAME;
1278 rc->this_key_frame_forced = cm->current_video_frame != 0 && 1203 rc->this_key_frame_forced = cm->current_video_frame != 0 &&
1279 rc->frames_to_key == 0; 1204 rc->frames_to_key == 0;
1280 rc->frames_to_key = cpi->key_frame_frequency; 1205 rc->frames_to_key = cpi->key_frame_frequency;
1281 rc->kf_boost = DEFAULT_KF_BOOST; 1206 rc->kf_boost = DEFAULT_KF_BOOST;
1282 rc->source_alt_ref_active = 0; 1207 rc->source_alt_ref_active = 0;
1283 } else { 1208 } else {
1284 cm->frame_type = INTER_FRAME; 1209 cm->frame_type = INTER_FRAME;
(...skipping 11 matching lines...) Expand all
1296 if (cm->frame_type == KEY_FRAME) 1221 if (cm->frame_type == KEY_FRAME)
1297 target = calc_iframe_target_size_one_pass_vbr(cpi); 1222 target = calc_iframe_target_size_one_pass_vbr(cpi);
1298 else 1223 else
1299 target = calc_pframe_target_size_one_pass_vbr(cpi); 1224 target = calc_pframe_target_size_one_pass_vbr(cpi);
1300 vp9_rc_set_frame_target(cpi, target); 1225 vp9_rc_set_frame_target(cpi, target);
1301 } 1226 }
1302 1227
1303 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { 1228 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1304 const VP9_CONFIG *oxcf = &cpi->oxcf; 1229 const VP9_CONFIG *oxcf = &cpi->oxcf;
1305 const RATE_CONTROL *rc = &cpi->rc; 1230 const RATE_CONTROL *rc = &cpi->rc;
1231 const SVC *const svc = &cpi->svc;
1306 const int64_t diff = oxcf->optimal_buffer_level - rc->buffer_level; 1232 const int64_t diff = oxcf->optimal_buffer_level - rc->buffer_level;
1307 const int64_t one_pct_bits = 1 + oxcf->optimal_buffer_level / 100; 1233 const int64_t one_pct_bits = 1 + oxcf->optimal_buffer_level / 100;
1308 int min_frame_target = MAX(rc->av_per_frame_bandwidth >> 4, 1234 int min_frame_target = MAX(rc->av_per_frame_bandwidth >> 4,
1309 FRAME_OVERHEAD_BITS); 1235 FRAME_OVERHEAD_BITS);
1310 int target = rc->av_per_frame_bandwidth; 1236 int target = rc->av_per_frame_bandwidth;
1311 if (cpi->svc.number_temporal_layers > 1 && 1237 if (svc->number_temporal_layers > 1 &&
1312 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { 1238 oxcf->end_usage == USAGE_STREAM_FROM_SERVER) {
1313 // Note that for layers, av_per_frame_bandwidth is the cumulative 1239 // Note that for layers, av_per_frame_bandwidth is the cumulative
1314 // per-frame-bandwidth. For the target size of this frame, use the 1240 // per-frame-bandwidth. For the target size of this frame, use the
1315 // layer average frame size (i.e., non-cumulative per-frame-bw). 1241 // layer average frame size (i.e., non-cumulative per-frame-bw).
1316 int current_temporal_layer = cpi->svc.temporal_layer_id; 1242 int current_temporal_layer = svc->temporal_layer_id;
1317 const LAYER_CONTEXT *lc = &cpi->svc.layer_context[current_temporal_layer]; 1243 const LAYER_CONTEXT *lc = &svc->layer_context[current_temporal_layer];
1318 target = lc->avg_frame_size; 1244 target = lc->avg_frame_size;
1319 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); 1245 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1320 } 1246 }
1321 if (diff > 0) { 1247 if (diff > 0) {
1322 // Lower the target bandwidth for this frame. 1248 // Lower the target bandwidth for this frame.
1323 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct); 1249 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1324 target -= (target * pct_low) / 200; 1250 target -= (target * pct_low) / 200;
1325 } else if (diff < 0) { 1251 } else if (diff < 0) {
1326 // Increase the target bandwidth for this frame. 1252 // Increase the target bandwidth for this frame.
1327 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); 1253 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
(...skipping 16 matching lines...) Expand all
1344 kf_boost = (int)(kf_boost * rc->frames_since_key / 1270 kf_boost = (int)(kf_boost * rc->frames_since_key /
1345 (cpi->output_framerate / 2)); 1271 (cpi->output_framerate / 2));
1346 } 1272 }
1347 target = ((16 + kf_boost) * rc->av_per_frame_bandwidth) >> 4; 1273 target = ((16 + kf_boost) * rc->av_per_frame_bandwidth) >> 4;
1348 } 1274 }
1349 return vp9_rc_clamp_iframe_target_size(cpi, target); 1275 return vp9_rc_clamp_iframe_target_size(cpi, target);
1350 } 1276 }
1351 1277
1352 void vp9_rc_get_svc_params(VP9_COMP *cpi) { 1278 void vp9_rc_get_svc_params(VP9_COMP *cpi) {
1353 VP9_COMMON *const cm = &cpi->common; 1279 VP9_COMMON *const cm = &cpi->common;
1354 int target = cpi->rc.av_per_frame_bandwidth; 1280 RATE_CONTROL *const rc = &cpi->rc;
1281 int target = rc->av_per_frame_bandwidth;
1355 if ((cm->current_video_frame == 0) || 1282 if ((cm->current_video_frame == 0) ||
1356 (cm->frame_flags & FRAMEFLAGS_KEY) || 1283 (cm->frame_flags & FRAMEFLAGS_KEY) ||
1357 (cpi->oxcf.auto_key && (cpi->rc.frames_since_key % 1284 (cpi->oxcf.auto_key && (rc->frames_since_key %
1358 cpi->key_frame_frequency == 0))) { 1285 cpi->key_frame_frequency == 0))) {
1359 cm->frame_type = KEY_FRAME; 1286 cm->frame_type = KEY_FRAME;
1360 cpi->rc.source_alt_ref_active = 0; 1287 rc->source_alt_ref_active = 0;
1361 if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { 1288 if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1362 target = calc_iframe_target_size_one_pass_cbr(cpi); 1289 target = calc_iframe_target_size_one_pass_cbr(cpi);
1363 } 1290 }
1364 } else { 1291 } else {
1365 cm->frame_type = INTER_FRAME; 1292 cm->frame_type = INTER_FRAME;
1366 if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { 1293 if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1367 target = calc_pframe_target_size_one_pass_cbr(cpi); 1294 target = calc_pframe_target_size_one_pass_cbr(cpi);
1368 } 1295 }
1369 } 1296 }
1370 vp9_rc_set_frame_target(cpi, target); 1297 vp9_rc_set_frame_target(cpi, target);
1371 cpi->rc.frames_till_gf_update_due = INT_MAX; 1298 rc->frames_till_gf_update_due = INT_MAX;
1372 cpi->rc.baseline_gf_interval = INT_MAX; 1299 rc->baseline_gf_interval = INT_MAX;
1373 } 1300 }
1374 1301
1375 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { 1302 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
1376 VP9_COMMON *const cm = &cpi->common; 1303 VP9_COMMON *const cm = &cpi->common;
1377 RATE_CONTROL *const rc = &cpi->rc; 1304 RATE_CONTROL *const rc = &cpi->rc;
1378 int target; 1305 int target;
1379 if ((cm->current_video_frame == 0 || 1306 if ((cm->current_video_frame == 0 ||
1380 cm->frame_flags & FRAMEFLAGS_KEY || 1307 (cm->frame_flags & FRAMEFLAGS_KEY) ||
1381 rc->frames_to_key == 0 || 1308 rc->frames_to_key == 0 ||
1382 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) { 1309 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) {
1383 cm->frame_type = KEY_FRAME; 1310 cm->frame_type = KEY_FRAME;
1384 rc->this_key_frame_forced = cm->current_video_frame != 0 && 1311 rc->this_key_frame_forced = cm->current_video_frame != 0 &&
1385 rc->frames_to_key == 0; 1312 rc->frames_to_key == 0;
1386 rc->frames_to_key = cpi->key_frame_frequency; 1313 rc->frames_to_key = cpi->key_frame_frequency;
1387 rc->kf_boost = DEFAULT_KF_BOOST; 1314 rc->kf_boost = DEFAULT_KF_BOOST;
1388 rc->source_alt_ref_active = 0; 1315 rc->source_alt_ref_active = 0;
1389 target = calc_iframe_target_size_one_pass_cbr(cpi); 1316 target = calc_iframe_target_size_one_pass_cbr(cpi);
1390 } else { 1317 } else {
1391 cm->frame_type = INTER_FRAME; 1318 cm->frame_type = INTER_FRAME;
1392 target = calc_pframe_target_size_one_pass_cbr(cpi); 1319 target = calc_pframe_target_size_one_pass_cbr(cpi);
1393 } 1320 }
1394 vp9_rc_set_frame_target(cpi, target); 1321 vp9_rc_set_frame_target(cpi, target);
1395 // Don't use gf_update by default in CBR mode. 1322 // Don't use gf_update by default in CBR mode.
1396 rc->frames_till_gf_update_due = INT_MAX; 1323 rc->frames_till_gf_update_due = INT_MAX;
1397 rc->baseline_gf_interval = INT_MAX; 1324 rc->baseline_gf_interval = INT_MAX;
1398 } 1325 }
1326
1327 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget) {
1328 int start_index = rc->worst_quality;
1329 int target_index = rc->worst_quality;
1330 int i;
1331
1332 // Convert the average q value to an index.
1333 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1334 start_index = i;
1335 if (vp9_convert_qindex_to_q(i) >= qstart)
1336 break;
1337 }
1338
1339 // Convert the q target to an index
1340 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1341 target_index = i;
1342 if (vp9_convert_qindex_to_q(i) >= qtarget)
1343 break;
1344 }
1345
1346 return target_index - start_index;
1347 }
1348
1349 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
1350 int qindex, double rate_target_ratio) {
1351 int target_index = rc->worst_quality;
1352 int i;
1353
1354 // Look up the current projected bits per block for the base index
1355 const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0);
1356
1357 // Find the target bits per mb based on the base value and given ratio.
1358 const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
1359
1360 // Convert the q target to an index
1361 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
1362 target_index = i;
1363 if (vp9_rc_bits_per_mb(frame_type, i, 1.0) <= target_bits_per_mb )
1364 break;
1365 }
1366
1367 return target_index - qindex;
1368 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rdopt.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698