OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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 |
(...skipping 28 matching lines...) Expand all Loading... |
39 lrc->ni_tot_qi = 0; | 39 lrc->ni_tot_qi = 0; |
40 lrc->tot_q = 0.0; | 40 lrc->tot_q = 0.0; |
41 lrc->avg_q = 0.0; | 41 lrc->avg_q = 0.0; |
42 lrc->ni_frames = 0; | 42 lrc->ni_frames = 0; |
43 lrc->decimation_count = 0; | 43 lrc->decimation_count = 0; |
44 lrc->decimation_factor = 0; | 44 lrc->decimation_factor = 0; |
45 lrc->rate_correction_factor = 1.0; | 45 lrc->rate_correction_factor = 1.0; |
46 lrc->key_frame_rate_correction_factor = 1.0; | 46 lrc->key_frame_rate_correction_factor = 1.0; |
47 | 47 |
48 if (svc->number_temporal_layers > 1) { | 48 if (svc->number_temporal_layers > 1) { |
49 lc->target_bandwidth = oxcf->ts_target_bitrate[layer] * 1000; | 49 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; |
50 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; | 50 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
51 } else { | 51 } else { |
52 lc->target_bandwidth = oxcf->ss_target_bitrate[layer] * 1000; | 52 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; |
53 lrc->last_q[0] = oxcf->best_allowed_q; | 53 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
54 lrc->last_q[1] = oxcf->best_allowed_q; | 54 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; |
55 lrc->last_q[2] = oxcf->best_allowed_q; | |
56 } | 55 } |
57 | 56 |
58 lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level), | 57 lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level_ms), |
59 lc->target_bandwidth, 1000); | 58 lc->target_bandwidth, 1000); |
60 lrc->bits_off_target = lrc->buffer_level; | 59 lrc->bits_off_target = lrc->buffer_level; |
61 } | 60 } |
62 } | 61 } |
63 | 62 |
64 // Update the layer context from a change_config() call. | 63 // Update the layer context from a change_config() call. |
65 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, | 64 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, |
66 const int target_bandwidth) { | 65 const int target_bandwidth) { |
67 SVC *const svc = &cpi->svc; | 66 SVC *const svc = &cpi->svc; |
68 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 67 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
69 const RATE_CONTROL *const rc = &cpi->rc; | 68 const RATE_CONTROL *const rc = &cpi->rc; |
70 int layer; | 69 int layer; |
71 int layer_end; | 70 int layer_end; |
72 float bitrate_alloc = 1.0; | 71 float bitrate_alloc = 1.0; |
73 | 72 |
74 if (svc->number_temporal_layers > 1) { | 73 if (svc->number_temporal_layers > 1) { |
75 layer_end = svc->number_temporal_layers; | 74 layer_end = svc->number_temporal_layers; |
76 } else { | 75 } else { |
77 layer_end = svc->number_spatial_layers; | 76 layer_end = svc->number_spatial_layers; |
78 } | 77 } |
79 | 78 |
80 for (layer = 0; layer < layer_end; ++layer) { | 79 for (layer = 0; layer < layer_end; ++layer) { |
81 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | 80 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
82 RATE_CONTROL *const lrc = &lc->rc; | 81 RATE_CONTROL *const lrc = &lc->rc; |
83 | 82 |
84 if (svc->number_temporal_layers > 1) { | 83 if (svc->number_temporal_layers > 1) { |
85 lc->target_bandwidth = oxcf->ts_target_bitrate[layer] * 1000; | 84 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; |
86 } else { | 85 } else { |
87 lc->target_bandwidth = oxcf->ss_target_bitrate[layer] * 1000; | 86 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; |
88 } | 87 } |
89 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth; | 88 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth; |
90 // Update buffer-related quantities. | 89 // Update buffer-related quantities. |
91 lc->starting_buffer_level = | 90 lrc->starting_buffer_level = |
92 (int64_t)(oxcf->starting_buffer_level * bitrate_alloc); | 91 (int64_t)(rc->starting_buffer_level * bitrate_alloc); |
93 lc->optimal_buffer_level = | 92 lrc->optimal_buffer_level = |
94 (int64_t)(oxcf->optimal_buffer_level * bitrate_alloc); | 93 (int64_t)(rc->optimal_buffer_level * bitrate_alloc); |
95 lc->maximum_buffer_size = | 94 lrc->maximum_buffer_size = |
96 (int64_t)(oxcf->maximum_buffer_size * bitrate_alloc); | 95 (int64_t)(rc->maximum_buffer_size * bitrate_alloc); |
97 lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size); | 96 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size); |
98 lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size); | 97 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size); |
99 // Update framerate-related quantities. | 98 // Update framerate-related quantities. |
100 if (svc->number_temporal_layers > 1) { | 99 if (svc->number_temporal_layers > 1) { |
101 lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer]; | 100 lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer]; |
102 } else { | 101 } else { |
103 lc->framerate = oxcf->framerate; | 102 lc->framerate = oxcf->framerate; |
104 } | 103 } |
105 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 104 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
106 lrc->max_frame_bandwidth = rc->max_frame_bandwidth; | 105 lrc->max_frame_bandwidth = rc->max_frame_bandwidth; |
107 // Update qp-related quantities. | 106 // Update qp-related quantities. |
108 lrc->worst_quality = rc->worst_quality; | 107 lrc->worst_quality = rc->worst_quality; |
(...skipping 16 matching lines...) Expand all Loading... |
125 | 124 |
126 lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer]; | 125 lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer]; |
127 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 126 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
128 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth; | 127 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth; |
129 // Update the average layer frame size (non-cumulative per-frame-bw). | 128 // Update the average layer frame size (non-cumulative per-frame-bw). |
130 if (layer == 0) { | 129 if (layer == 0) { |
131 lc->avg_frame_size = lrc->avg_frame_bandwidth; | 130 lc->avg_frame_size = lrc->avg_frame_bandwidth; |
132 } else { | 131 } else { |
133 const double prev_layer_framerate = | 132 const double prev_layer_framerate = |
134 oxcf->framerate / oxcf->ts_rate_decimator[layer - 1]; | 133 oxcf->framerate / oxcf->ts_rate_decimator[layer - 1]; |
135 const int prev_layer_target_bandwidth = | 134 const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1]; |
136 oxcf->ts_target_bitrate[layer - 1] * 1000; | |
137 lc->avg_frame_size = | 135 lc->avg_frame_size = |
138 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) / | 136 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) / |
139 (lc->framerate - prev_layer_framerate)); | 137 (lc->framerate - prev_layer_framerate)); |
140 } | 138 } |
141 } | 139 } |
142 | 140 |
143 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) { | 141 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) { |
144 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 142 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
145 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 143 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); |
146 RATE_CONTROL *const lrc = &lc->rc; | 144 RATE_CONTROL *const lrc = &lc->rc; |
147 | 145 |
148 lc->framerate = framerate; | 146 lc->framerate = framerate; |
149 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 147 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
150 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth * | 148 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth * |
151 oxcf->two_pass_vbrmin_section / 100); | 149 oxcf->two_pass_vbrmin_section / 100); |
152 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth * | 150 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth * |
153 oxcf->two_pass_vbrmax_section) / 100); | 151 oxcf->two_pass_vbrmax_section) / 100); |
154 lrc->max_gf_interval = 16; | 152 vp9_rc_set_gf_max_interval(oxcf, lrc); |
155 | |
156 lrc->static_scene_max_gf_interval = cpi->oxcf.key_freq >> 1; | |
157 | |
158 if (oxcf->play_alternate && oxcf->lag_in_frames) { | |
159 if (lrc->max_gf_interval > oxcf->lag_in_frames - 1) | |
160 lrc->max_gf_interval = oxcf->lag_in_frames - 1; | |
161 | |
162 if (lrc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) | |
163 lrc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1; | |
164 } | |
165 | |
166 if (lrc->max_gf_interval > lrc->static_scene_max_gf_interval) | |
167 lrc->max_gf_interval = lrc->static_scene_max_gf_interval; | |
168 } | 153 } |
169 | 154 |
170 void vp9_restore_layer_context(VP9_COMP *const cpi) { | 155 void vp9_restore_layer_context(VP9_COMP *const cpi) { |
171 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 156 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); |
172 const int old_frame_since_key = cpi->rc.frames_since_key; | 157 const int old_frame_since_key = cpi->rc.frames_since_key; |
173 const int old_frame_to_key = cpi->rc.frames_to_key; | 158 const int old_frame_to_key = cpi->rc.frames_to_key; |
174 | 159 |
175 cpi->rc = lc->rc; | 160 cpi->rc = lc->rc; |
176 cpi->twopass = lc->twopass; | 161 cpi->twopass = lc->twopass; |
177 cpi->oxcf.target_bandwidth = lc->target_bandwidth; | 162 cpi->oxcf.target_bandwidth = lc->target_bandwidth; |
178 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level; | |
179 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level; | |
180 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size; | |
181 // Reset the frames_since_key and frames_to_key counters to their values | 163 // Reset the frames_since_key and frames_to_key counters to their values |
182 // before the layer restore. Keep these defined for the stream (not layer). | 164 // before the layer restore. Keep these defined for the stream (not layer). |
183 if (cpi->svc.number_temporal_layers > 1) { | 165 if (cpi->svc.number_temporal_layers > 1) { |
184 cpi->rc.frames_since_key = old_frame_since_key; | 166 cpi->rc.frames_since_key = old_frame_since_key; |
185 cpi->rc.frames_to_key = old_frame_to_key; | 167 cpi->rc.frames_to_key = old_frame_to_key; |
186 } | 168 } |
187 } | 169 } |
188 | 170 |
189 void vp9_save_layer_context(VP9_COMP *const cpi) { | 171 void vp9_save_layer_context(VP9_COMP *const cpi) { |
190 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 172 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
191 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 173 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); |
192 | 174 |
193 lc->rc = cpi->rc; | 175 lc->rc = cpi->rc; |
194 lc->twopass = cpi->twopass; | 176 lc->twopass = cpi->twopass; |
195 lc->target_bandwidth = (int)oxcf->target_bandwidth; | 177 lc->target_bandwidth = (int)oxcf->target_bandwidth; |
196 lc->starting_buffer_level = oxcf->starting_buffer_level; | |
197 lc->optimal_buffer_level = oxcf->optimal_buffer_level; | |
198 lc->maximum_buffer_size = oxcf->maximum_buffer_size; | |
199 } | 178 } |
200 | 179 |
201 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) { | 180 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) { |
202 SVC *const svc = &cpi->svc; | 181 SVC *const svc = &cpi->svc; |
203 int i; | 182 int i; |
204 | 183 |
205 for (i = 0; i < svc->number_spatial_layers; ++i) { | 184 for (i = 0; i < svc->number_spatial_layers; ++i) { |
206 struct twopass_rc *const twopass = &svc->layer_context[i].twopass; | 185 TWO_PASS *const twopass = &svc->layer_context[i].twopass; |
207 | 186 |
208 svc->spatial_layer_id = i; | 187 svc->spatial_layer_id = i; |
209 vp9_init_second_pass(cpi); | 188 vp9_init_second_pass(cpi); |
210 | 189 |
211 twopass->total_stats.spatial_layer_id = i; | 190 twopass->total_stats.spatial_layer_id = i; |
212 twopass->total_left_stats.spatial_layer_id = i; | 191 twopass->total_left_stats.spatial_layer_id = i; |
213 } | 192 } |
214 svc->spatial_layer_id = 0; | 193 svc->spatial_layer_id = 0; |
215 } | 194 } |
216 | 195 |
217 void vp9_inc_frame_in_layer(SVC *svc) { | 196 void vp9_inc_frame_in_layer(SVC *svc) { |
218 LAYER_CONTEXT *const lc = (svc->number_temporal_layers > 1) | 197 LAYER_CONTEXT *const lc = (svc->number_temporal_layers > 1) |
219 ? &svc->layer_context[svc->temporal_layer_id] | 198 ? &svc->layer_context[svc->temporal_layer_id] |
220 : &svc->layer_context[svc->spatial_layer_id]; | 199 : &svc->layer_context[svc->spatial_layer_id]; |
221 ++lc->current_video_frame_in_layer; | 200 ++lc->current_video_frame_in_layer; |
222 } | 201 } |
223 | 202 |
224 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { | 203 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { |
225 return cpi->use_svc && | 204 return cpi->use_svc && |
226 cpi->svc.number_temporal_layers == 1 && | 205 cpi->svc.number_temporal_layers == 1 && |
227 cpi->svc.spatial_layer_id > 0 && | 206 cpi->svc.spatial_layer_id > 0 && |
228 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame; | 207 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame; |
229 } | 208 } |
OLD | NEW |