| 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 #include "vp9/encoder/vp9_extend.h" |
| 16 | 16 |
| 17 void vp9_init_layer_context(VP9_COMP *const cpi) { | 17 void vp9_init_layer_context(VP9_COMP *const cpi) { |
| 18 SVC *const svc = &cpi->svc; | 18 SVC *const svc = &cpi->svc; |
| 19 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 19 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 20 int layer; | 20 int layer; |
| 21 int layer_end; | 21 int layer_end; |
| 22 int alt_ref_idx = svc->number_spatial_layers; |
| 22 | 23 |
| 23 svc->spatial_layer_id = 0; | 24 svc->spatial_layer_id = 0; |
| 24 svc->temporal_layer_id = 0; | 25 svc->temporal_layer_id = 0; |
| 25 | 26 |
| 26 if (svc->number_temporal_layers > 1) { | 27 if (svc->number_temporal_layers > 1) { |
| 27 layer_end = svc->number_temporal_layers; | 28 layer_end = svc->number_temporal_layers; |
| 28 } else { | 29 } else { |
| 29 layer_end = svc->number_spatial_layers; | 30 layer_end = svc->number_spatial_layers; |
| 30 } | 31 } |
| 31 | 32 |
| 32 for (layer = 0; layer < layer_end; ++layer) { | 33 for (layer = 0; layer < layer_end; ++layer) { |
| 33 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | 34 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
| 34 RATE_CONTROL *const lrc = &lc->rc; | 35 RATE_CONTROL *const lrc = &lc->rc; |
| 35 int i; | 36 int i; |
| 36 lc->current_video_frame_in_layer = 0; | 37 lc->current_video_frame_in_layer = 0; |
| 37 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; | |
| 38 lrc->ni_av_qi = oxcf->worst_allowed_q; | 38 lrc->ni_av_qi = oxcf->worst_allowed_q; |
| 39 lrc->total_actual_bits = 0; | 39 lrc->total_actual_bits = 0; |
| 40 lrc->total_target_vs_actual = 0; | 40 lrc->total_target_vs_actual = 0; |
| 41 lrc->ni_tot_qi = 0; | 41 lrc->ni_tot_qi = 0; |
| 42 lrc->tot_q = 0.0; | 42 lrc->tot_q = 0.0; |
| 43 lrc->avg_q = 0.0; | 43 lrc->avg_q = 0.0; |
| 44 lrc->ni_frames = 0; | 44 lrc->ni_frames = 0; |
| 45 lrc->decimation_count = 0; | 45 lrc->decimation_count = 0; |
| 46 lrc->decimation_factor = 0; | 46 lrc->decimation_factor = 0; |
| 47 | 47 |
| 48 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { | 48 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
| 49 lrc->rate_correction_factors[i] = 1.0; | 49 lrc->rate_correction_factors[i] = 1.0; |
| 50 } | 50 } |
| 51 lc->layer_size = 0; |
| 51 | 52 |
| 52 if (svc->number_temporal_layers > 1) { | 53 if (svc->number_temporal_layers > 1) { |
| 53 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; | 54 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; |
| 54 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; | 55 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
| 56 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
| 55 } else { | 57 } else { |
| 56 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; | 58 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; |
| 57 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; | 59 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
| 58 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; | 60 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; |
| 61 lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + |
| 62 oxcf->best_allowed_q) / 2; |
| 63 lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + |
| 64 oxcf->best_allowed_q) / 2; |
| 65 if (oxcf->ss_play_alternate[layer]) |
| 66 lc->alt_ref_idx = alt_ref_idx++; |
| 67 else |
| 68 lc->alt_ref_idx = -1; |
| 59 } | 69 } |
| 60 | 70 |
| 61 lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level_ms), | 71 lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level_ms), |
| 62 lc->target_bandwidth, 1000); | 72 lc->target_bandwidth, 1000); |
| 63 lrc->bits_off_target = lrc->buffer_level; | 73 lrc->bits_off_target = lrc->buffer_level; |
| 64 } | 74 } |
| 65 } | 75 } |
| 66 | 76 |
| 67 // Update the layer context from a change_config() call. | 77 // Update the layer context from a change_config() call. |
| 68 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, | 78 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 156 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 147 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 157 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); |
| 148 RATE_CONTROL *const lrc = &lc->rc; | 158 RATE_CONTROL *const lrc = &lc->rc; |
| 149 | 159 |
| 150 lc->framerate = framerate; | 160 lc->framerate = framerate; |
| 151 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 161 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
| 152 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth * | 162 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth * |
| 153 oxcf->two_pass_vbrmin_section / 100); | 163 oxcf->two_pass_vbrmin_section / 100); |
| 154 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth * | 164 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth * |
| 155 oxcf->two_pass_vbrmax_section) / 100); | 165 oxcf->two_pass_vbrmax_section) / 100); |
| 156 vp9_rc_set_gf_max_interval(oxcf, lrc); | 166 vp9_rc_set_gf_max_interval(cpi, lrc); |
| 157 } | 167 } |
| 158 | 168 |
| 159 void vp9_restore_layer_context(VP9_COMP *const cpi) { | 169 void vp9_restore_layer_context(VP9_COMP *const cpi) { |
| 160 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 170 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); |
| 161 const int old_frame_since_key = cpi->rc.frames_since_key; | 171 const int old_frame_since_key = cpi->rc.frames_since_key; |
| 162 const int old_frame_to_key = cpi->rc.frames_to_key; | 172 const int old_frame_to_key = cpi->rc.frames_to_key; |
| 163 | 173 |
| 164 cpi->rc = lc->rc; | 174 cpi->rc = lc->rc; |
| 165 cpi->twopass = lc->twopass; | 175 cpi->twopass = lc->twopass; |
| 166 cpi->oxcf.target_bandwidth = lc->target_bandwidth; | 176 cpi->oxcf.target_bandwidth = lc->target_bandwidth; |
| 177 cpi->alt_ref_source = lc->alt_ref_source; |
| 167 // Reset the frames_since_key and frames_to_key counters to their values | 178 // Reset the frames_since_key and frames_to_key counters to their values |
| 168 // before the layer restore. Keep these defined for the stream (not layer). | 179 // before the layer restore. Keep these defined for the stream (not layer). |
| 169 if (cpi->svc.number_temporal_layers > 1) { | 180 if (cpi->svc.number_temporal_layers > 1) { |
| 170 cpi->rc.frames_since_key = old_frame_since_key; | 181 cpi->rc.frames_since_key = old_frame_since_key; |
| 171 cpi->rc.frames_to_key = old_frame_to_key; | 182 cpi->rc.frames_to_key = old_frame_to_key; |
| 172 } | 183 } |
| 173 } | 184 } |
| 174 | 185 |
| 175 void vp9_save_layer_context(VP9_COMP *const cpi) { | 186 void vp9_save_layer_context(VP9_COMP *const cpi) { |
| 176 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 187 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 177 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 188 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); |
| 178 | 189 |
| 179 lc->rc = cpi->rc; | 190 lc->rc = cpi->rc; |
| 180 lc->twopass = cpi->twopass; | 191 lc->twopass = cpi->twopass; |
| 181 lc->target_bandwidth = (int)oxcf->target_bandwidth; | 192 lc->target_bandwidth = (int)oxcf->target_bandwidth; |
| 193 lc->alt_ref_source = cpi->alt_ref_source; |
| 182 } | 194 } |
| 183 | 195 |
| 184 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) { | 196 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) { |
| 185 SVC *const svc = &cpi->svc; | 197 SVC *const svc = &cpi->svc; |
| 186 int i; | 198 int i; |
| 187 | 199 |
| 188 for (i = 0; i < svc->number_spatial_layers; ++i) { | 200 for (i = 0; i < svc->number_spatial_layers; ++i) { |
| 189 TWO_PASS *const twopass = &svc->layer_context[i].twopass; | 201 TWO_PASS *const twopass = &svc->layer_context[i].twopass; |
| 190 | 202 |
| 191 svc->spatial_layer_id = i; | 203 svc->spatial_layer_id = i; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // Store svc parameters for each layer | 244 // Store svc parameters for each layer |
| 233 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) | 245 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) |
| 234 buf->svc_params[i] = cpi->svc.layer_context[i].svc_params_received; | 246 buf->svc_params[i] = cpi->svc.layer_context[i].svc_params_received; |
| 235 | 247 |
| 236 return 0; | 248 return 0; |
| 237 } | 249 } |
| 238 | 250 |
| 239 static int copy_svc_params(VP9_COMP *const cpi, struct lookahead_entry *buf) { | 251 static int copy_svc_params(VP9_COMP *const cpi, struct lookahead_entry *buf) { |
| 240 int layer_id; | 252 int layer_id; |
| 241 vpx_svc_parameters_t *layer_param; | 253 vpx_svc_parameters_t *layer_param; |
| 242 vpx_enc_frame_flags_t flags; | 254 LAYER_CONTEXT *lc; |
| 243 | 255 |
| 244 // Find the next layer to be encoded | 256 // Find the next layer to be encoded |
| 245 for (layer_id = 0; layer_id < cpi->svc.number_spatial_layers; ++layer_id) { | 257 for (layer_id = 0; layer_id < cpi->svc.number_spatial_layers; ++layer_id) { |
| 246 if (buf->svc_params[layer_id].spatial_layer >=0) | 258 if (buf->svc_params[layer_id].spatial_layer >=0) |
| 247 break; | 259 break; |
| 248 } | 260 } |
| 249 | 261 |
| 250 if (layer_id == cpi->svc.number_spatial_layers) | 262 if (layer_id == cpi->svc.number_spatial_layers) |
| 251 return 1; | 263 return 1; |
| 252 | 264 |
| 253 layer_param = &buf->svc_params[layer_id]; | 265 layer_param = &buf->svc_params[layer_id]; |
| 254 buf->flags = flags = layer_param->flags; | |
| 255 cpi->svc.spatial_layer_id = layer_param->spatial_layer; | 266 cpi->svc.spatial_layer_id = layer_param->spatial_layer; |
| 256 cpi->svc.temporal_layer_id = layer_param->temporal_layer; | 267 cpi->svc.temporal_layer_id = layer_param->temporal_layer; |
| 257 cpi->lst_fb_idx = layer_param->lst_fb_idx; | 268 |
| 258 cpi->gld_fb_idx = layer_param->gld_fb_idx; | 269 cpi->lst_fb_idx = cpi->svc.spatial_layer_id; |
| 259 cpi->alt_fb_idx = layer_param->alt_fb_idx; | 270 |
| 271 if (cpi->svc.spatial_layer_id < 1) |
| 272 cpi->gld_fb_idx = cpi->lst_fb_idx; |
| 273 else |
| 274 cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1; |
| 275 |
| 276 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 277 |
| 278 if (lc->current_video_frame_in_layer == 0) { |
| 279 if (cpi->svc.spatial_layer_id >= 2) |
| 280 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2; |
| 281 else |
| 282 cpi->alt_fb_idx = cpi->lst_fb_idx; |
| 283 } else { |
| 284 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id]) { |
| 285 cpi->alt_fb_idx = lc->alt_ref_idx; |
| 286 if (!lc->has_alt_frame) |
| 287 cpi->ref_frame_flags &= (~VP9_ALT_FLAG); |
| 288 } else { |
| 289 // Find a proper alt_fb_idx for layers that don't have alt ref frame |
| 290 if (cpi->svc.spatial_layer_id == 0) { |
| 291 cpi->alt_fb_idx = cpi->lst_fb_idx; |
| 292 } else { |
| 293 LAYER_CONTEXT *lc_lower = |
| 294 &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1]; |
| 295 |
| 296 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id - 1] && |
| 297 lc_lower->alt_ref_source != NULL) |
| 298 cpi->alt_fb_idx = lc_lower->alt_ref_idx; |
| 299 else if (cpi->svc.spatial_layer_id >= 2) |
| 300 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2; |
| 301 else |
| 302 cpi->alt_fb_idx = cpi->lst_fb_idx; |
| 303 } |
| 304 } |
| 305 } |
| 260 | 306 |
| 261 if (vp9_set_size_literal(cpi, layer_param->width, layer_param->height) != 0) | 307 if (vp9_set_size_literal(cpi, layer_param->width, layer_param->height) != 0) |
| 262 return VPX_CODEC_INVALID_PARAM; | 308 return VPX_CODEC_INVALID_PARAM; |
| 263 | 309 |
| 264 cpi->oxcf.worst_allowed_q = | 310 cpi->oxcf.worst_allowed_q = |
| 265 vp9_quantizer_to_qindex(layer_param->max_quantizer); | 311 vp9_quantizer_to_qindex(layer_param->max_quantizer); |
| 266 cpi->oxcf.best_allowed_q = | 312 cpi->oxcf.best_allowed_q = |
| 267 vp9_quantizer_to_qindex(layer_param->min_quantizer); | 313 vp9_quantizer_to_qindex(layer_param->min_quantizer); |
| 268 | 314 |
| 269 vp9_change_config(cpi, &cpi->oxcf); | 315 vp9_change_config(cpi, &cpi->oxcf); |
| 270 | 316 |
| 271 vp9_set_high_precision_mv(cpi, 1); | 317 vp9_set_high_precision_mv(cpi, 1); |
| 272 | 318 |
| 273 // Retrieve the encoding flags for each layer and apply it to encoder. | 319 cpi->alt_ref_source = get_layer_context(&cpi->svc)->alt_ref_source; |
| 274 // It includes reference frame flags and update frame flags. | |
| 275 vp9_apply_encoding_flags(cpi, flags); | |
| 276 | 320 |
| 277 return 0; | 321 return 0; |
| 278 } | 322 } |
| 279 | 323 |
| 280 struct lookahead_entry *vp9_svc_lookahead_peek(VP9_COMP *const cpi, | 324 struct lookahead_entry *vp9_svc_lookahead_peek(VP9_COMP *const cpi, |
| 281 struct lookahead_ctx *ctx, | 325 struct lookahead_ctx *ctx, |
| 282 int index, int copy_params) { | 326 int index, int copy_params) { |
| 283 struct lookahead_entry *buf = vp9_lookahead_peek(ctx, index); | 327 struct lookahead_entry *buf = vp9_lookahead_peek(ctx, index); |
| 284 | 328 |
| 285 if (buf != NULL && copy_params != 0) { | 329 if (buf != NULL && copy_params != 0) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 301 // spatial_layer to -1 for lower layers. | 345 // spatial_layer to -1 for lower layers. |
| 302 buf->svc_params[cpi->svc.spatial_layer_id].spatial_layer = -1; | 346 buf->svc_params[cpi->svc.spatial_layer_id].spatial_layer = -1; |
| 303 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { | 347 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { |
| 304 vp9_lookahead_pop(ctx, drain); | 348 vp9_lookahead_pop(ctx, drain); |
| 305 } | 349 } |
| 306 } | 350 } |
| 307 } | 351 } |
| 308 | 352 |
| 309 return buf; | 353 return buf; |
| 310 } | 354 } |
| OLD | NEW |