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 |
11 #include <math.h> | 11 #include <math.h> |
12 | 12 |
13 #include "vp9/encoder/vp9_encoder.h" | 13 #include "vp9/encoder/vp9_encoder.h" |
14 #include "vp9/encoder/vp9_svc_layercontext.h" | 14 #include "vp9/encoder/vp9_svc_layercontext.h" |
| 15 #include "vp9/encoder/vp9_extend.h" |
15 | 16 |
16 void vp9_init_layer_context(VP9_COMP *const cpi) { | 17 void vp9_init_layer_context(VP9_COMP *const cpi) { |
17 SVC *const svc = &cpi->svc; | 18 SVC *const svc = &cpi->svc; |
18 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 19 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
19 int layer; | 20 int layer; |
20 int layer_end; | 21 int layer_end; |
21 | 22 |
22 svc->spatial_layer_id = 0; | 23 svc->spatial_layer_id = 0; |
23 svc->temporal_layer_id = 0; | 24 svc->temporal_layer_id = 0; |
24 | 25 |
25 if (svc->number_temporal_layers > 1) { | 26 if (svc->number_temporal_layers > 1) { |
26 layer_end = svc->number_temporal_layers; | 27 layer_end = svc->number_temporal_layers; |
27 } else { | 28 } else { |
28 layer_end = svc->number_spatial_layers; | 29 layer_end = svc->number_spatial_layers; |
29 } | 30 } |
30 | 31 |
31 for (layer = 0; layer < layer_end; ++layer) { | 32 for (layer = 0; layer < layer_end; ++layer) { |
32 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | 33 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
33 RATE_CONTROL *const lrc = &lc->rc; | 34 RATE_CONTROL *const lrc = &lc->rc; |
| 35 int i; |
34 lc->current_video_frame_in_layer = 0; | 36 lc->current_video_frame_in_layer = 0; |
35 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; | 37 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
36 lrc->ni_av_qi = oxcf->worst_allowed_q; | 38 lrc->ni_av_qi = oxcf->worst_allowed_q; |
37 lrc->total_actual_bits = 0; | 39 lrc->total_actual_bits = 0; |
38 lrc->total_target_vs_actual = 0; | 40 lrc->total_target_vs_actual = 0; |
39 lrc->ni_tot_qi = 0; | 41 lrc->ni_tot_qi = 0; |
40 lrc->tot_q = 0.0; | 42 lrc->tot_q = 0.0; |
41 lrc->avg_q = 0.0; | 43 lrc->avg_q = 0.0; |
42 lrc->ni_frames = 0; | 44 lrc->ni_frames = 0; |
43 lrc->decimation_count = 0; | 45 lrc->decimation_count = 0; |
44 lrc->decimation_factor = 0; | 46 lrc->decimation_factor = 0; |
45 lrc->rate_correction_factor = 1.0; | 47 |
46 lrc->key_frame_rate_correction_factor = 1.0; | 48 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
| 49 lrc->rate_correction_factors[i] = 1.0; |
| 50 } |
47 | 51 |
48 if (svc->number_temporal_layers > 1) { | 52 if (svc->number_temporal_layers > 1) { |
49 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; | 53 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; |
50 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; | 54 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
51 } else { | 55 } else { |
52 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; | 56 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; |
53 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; | 57 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
54 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; | 58 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; |
55 } | 59 } |
56 | 60 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 : &svc->layer_context[svc->spatial_layer_id]; | 203 : &svc->layer_context[svc->spatial_layer_id]; |
200 ++lc->current_video_frame_in_layer; | 204 ++lc->current_video_frame_in_layer; |
201 } | 205 } |
202 | 206 |
203 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { | 207 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { |
204 return cpi->use_svc && | 208 return cpi->use_svc && |
205 cpi->svc.number_temporal_layers == 1 && | 209 cpi->svc.number_temporal_layers == 1 && |
206 cpi->svc.spatial_layer_id > 0 && | 210 cpi->svc.spatial_layer_id > 0 && |
207 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame; | 211 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame; |
208 } | 212 } |
| 213 |
| 214 int vp9_svc_lookahead_push(const VP9_COMP *const cpi, struct lookahead_ctx *ctx, |
| 215 YV12_BUFFER_CONFIG *src, int64_t ts_start, |
| 216 int64_t ts_end, unsigned int flags) { |
| 217 struct lookahead_entry *buf; |
| 218 int i, index; |
| 219 |
| 220 if (vp9_lookahead_push(ctx, src, ts_start, ts_end, flags)) |
| 221 return 1; |
| 222 |
| 223 index = ctx->write_idx - 1; |
| 224 if (index < 0) |
| 225 index += ctx->max_sz; |
| 226 |
| 227 buf = ctx->buf + index; |
| 228 |
| 229 if (buf == NULL) |
| 230 return 1; |
| 231 |
| 232 // Store svc parameters for each layer |
| 233 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) |
| 234 buf->svc_params[i] = cpi->svc.layer_context[i].svc_params_received; |
| 235 |
| 236 return 0; |
| 237 } |
| 238 |
| 239 static int copy_svc_params(VP9_COMP *const cpi, struct lookahead_entry *buf) { |
| 240 int layer_id; |
| 241 vpx_svc_parameters_t *layer_param; |
| 242 vpx_enc_frame_flags_t flags; |
| 243 |
| 244 // Find the next layer to be encoded |
| 245 for (layer_id = 0; layer_id < cpi->svc.number_spatial_layers; ++layer_id) { |
| 246 if (buf->svc_params[layer_id].spatial_layer >=0) |
| 247 break; |
| 248 } |
| 249 |
| 250 if (layer_id == cpi->svc.number_spatial_layers) |
| 251 return 1; |
| 252 |
| 253 layer_param = &buf->svc_params[layer_id]; |
| 254 buf->flags = flags = layer_param->flags; |
| 255 cpi->svc.spatial_layer_id = layer_param->spatial_layer; |
| 256 cpi->svc.temporal_layer_id = layer_param->temporal_layer; |
| 257 cpi->lst_fb_idx = layer_param->lst_fb_idx; |
| 258 cpi->gld_fb_idx = layer_param->gld_fb_idx; |
| 259 cpi->alt_fb_idx = layer_param->alt_fb_idx; |
| 260 |
| 261 if (vp9_set_size_literal(cpi, layer_param->width, layer_param->height) != 0) |
| 262 return VPX_CODEC_INVALID_PARAM; |
| 263 |
| 264 cpi->oxcf.worst_allowed_q = |
| 265 vp9_quantizer_to_qindex(layer_param->max_quantizer); |
| 266 cpi->oxcf.best_allowed_q = |
| 267 vp9_quantizer_to_qindex(layer_param->min_quantizer); |
| 268 |
| 269 vp9_change_config(cpi, &cpi->oxcf); |
| 270 |
| 271 vp9_set_high_precision_mv(cpi, 1); |
| 272 |
| 273 // Retrieve the encoding flags for each layer and apply it to encoder. |
| 274 // It includes reference frame flags and update frame flags. |
| 275 vp9_apply_encoding_flags(cpi, flags); |
| 276 |
| 277 return 0; |
| 278 } |
| 279 |
| 280 struct lookahead_entry *vp9_svc_lookahead_peek(VP9_COMP *const cpi, |
| 281 struct lookahead_ctx *ctx, |
| 282 int index, int copy_params) { |
| 283 struct lookahead_entry *buf = vp9_lookahead_peek(ctx, index); |
| 284 |
| 285 if (buf != NULL && copy_params != 0) { |
| 286 if (copy_svc_params(cpi, buf) != 0) |
| 287 return NULL; |
| 288 } |
| 289 return buf; |
| 290 } |
| 291 |
| 292 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi, |
| 293 struct lookahead_ctx *ctx, |
| 294 int drain) { |
| 295 struct lookahead_entry *buf = NULL; |
| 296 |
| 297 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) { |
| 298 buf = vp9_svc_lookahead_peek(cpi, ctx, 0, 1); |
| 299 if (buf != NULL) { |
| 300 // Only remove the buffer when pop the highest layer. Simply set the |
| 301 // spatial_layer to -1 for lower layers. |
| 302 buf->svc_params[cpi->svc.spatial_layer_id].spatial_layer = -1; |
| 303 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { |
| 304 vp9_lookahead_pop(ctx, drain); |
| 305 } |
| 306 } |
| 307 } |
| 308 |
| 309 return buf; |
| 310 } |
OLD | NEW |