| 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 #define SMALL_FRAME_FB_IDX 7 | 17 #define SMALL_FRAME_FB_IDX 7 |
| 18 #define SMALL_FRAME_WIDTH 16 | 18 #define SMALL_FRAME_WIDTH 16 |
| 19 #define SMALL_FRAME_HEIGHT 16 | 19 #define SMALL_FRAME_HEIGHT 16 |
| 20 | 20 |
| 21 void vp9_init_layer_context(VP9_COMP *const cpi) { | 21 void vp9_init_layer_context(VP9_COMP *const cpi) { |
| 22 SVC *const svc = &cpi->svc; | 22 SVC *const svc = &cpi->svc; |
| 23 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 23 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 24 int layer; | 24 int sl, tl; |
| 25 int layer_end; | |
| 26 int alt_ref_idx = svc->number_spatial_layers; | 25 int alt_ref_idx = svc->number_spatial_layers; |
| 27 | 26 |
| 28 svc->spatial_layer_id = 0; | 27 svc->spatial_layer_id = 0; |
| 29 svc->temporal_layer_id = 0; | 28 svc->temporal_layer_id = 0; |
| 30 | 29 |
| 31 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { | 30 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) { |
| 32 layer_end = svc->number_temporal_layers; | 31 if (vp9_realloc_frame_buffer(&cpi->svc.empty_frame.img, |
| 33 } else { | 32 SMALL_FRAME_WIDTH, SMALL_FRAME_HEIGHT, |
| 34 layer_end = svc->number_spatial_layers; | 33 cpi->common.subsampling_x, |
| 35 | 34 cpi->common.subsampling_y, |
| 36 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) { | |
| 37 if (vp9_realloc_frame_buffer(&cpi->svc.empty_frame.img, | |
| 38 SMALL_FRAME_WIDTH, SMALL_FRAME_HEIGHT, | |
| 39 cpi->common.subsampling_x, | |
| 40 cpi->common.subsampling_y, | |
| 41 #if CONFIG_VP9_HIGHBITDEPTH | 35 #if CONFIG_VP9_HIGHBITDEPTH |
| 42 cpi->common.use_highbitdepth, | 36 cpi->common.use_highbitdepth, |
| 43 #endif | 37 #endif |
| 44 VP9_ENC_BORDER_IN_PIXELS, | 38 VP9_ENC_BORDER_IN_PIXELS, |
| 45 cpi->common.byte_alignment, | 39 cpi->common.byte_alignment, |
| 46 NULL, NULL, NULL)) | 40 NULL, NULL, NULL)) |
| 47 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, | 41 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
| 48 "Failed to allocate empty frame for multiple frame " | 42 "Failed to allocate empty frame for multiple frame " |
| 49 "contexts"); | 43 "contexts"); |
| 50 | 44 |
| 51 memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80, | 45 memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80, |
| 52 cpi->svc.empty_frame.img.buffer_alloc_sz); | 46 cpi->svc.empty_frame.img.buffer_alloc_sz); |
| 47 } |
| 48 |
| 49 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) { |
| 50 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) { |
| 51 int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers); |
| 52 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
| 53 RATE_CONTROL *const lrc = &lc->rc; |
| 54 int i; |
| 55 lc->current_video_frame_in_layer = 0; |
| 56 lc->layer_size = 0; |
| 57 lc->frames_from_key_frame = 0; |
| 58 lc->last_frame_type = FRAME_TYPES; |
| 59 lrc->ni_av_qi = oxcf->worst_allowed_q; |
| 60 lrc->total_actual_bits = 0; |
| 61 lrc->total_target_vs_actual = 0; |
| 62 lrc->ni_tot_qi = 0; |
| 63 lrc->tot_q = 0.0; |
| 64 lrc->avg_q = 0.0; |
| 65 lrc->ni_frames = 0; |
| 66 lrc->decimation_count = 0; |
| 67 lrc->decimation_factor = 0; |
| 68 |
| 69 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
| 70 lrc->rate_correction_factors[i] = 1.0; |
| 71 } |
| 72 |
| 73 if (cpi->oxcf.rc_mode == VPX_CBR) { |
| 74 lc->target_bandwidth = oxcf->layer_target_bitrate[layer]; |
| 75 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
| 76 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
| 77 lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; |
| 78 } else { |
| 79 lc->target_bandwidth = oxcf->layer_target_bitrate[layer]; |
| 80 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
| 81 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; |
| 82 lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + |
| 83 oxcf->best_allowed_q) / 2; |
| 84 lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + |
| 85 oxcf->best_allowed_q) / 2; |
| 86 if (oxcf->ss_enable_auto_arf[sl]) |
| 87 lc->alt_ref_idx = alt_ref_idx++; |
| 88 else |
| 89 lc->alt_ref_idx = INVALID_IDX; |
| 90 lc->gold_ref_idx = INVALID_IDX; |
| 91 } |
| 92 |
| 93 lrc->buffer_level = oxcf->starting_buffer_level_ms * |
| 94 lc->target_bandwidth / 1000; |
| 95 lrc->bits_off_target = lrc->buffer_level; |
| 53 } | 96 } |
| 54 } | 97 } |
| 55 | 98 |
| 56 for (layer = 0; layer < layer_end; ++layer) { | |
| 57 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | |
| 58 RATE_CONTROL *const lrc = &lc->rc; | |
| 59 int i; | |
| 60 lc->current_video_frame_in_layer = 0; | |
| 61 lc->layer_size = 0; | |
| 62 lc->frames_from_key_frame = 0; | |
| 63 lc->last_frame_type = FRAME_TYPES; | |
| 64 lrc->ni_av_qi = oxcf->worst_allowed_q; | |
| 65 lrc->total_actual_bits = 0; | |
| 66 lrc->total_target_vs_actual = 0; | |
| 67 lrc->ni_tot_qi = 0; | |
| 68 lrc->tot_q = 0.0; | |
| 69 lrc->avg_q = 0.0; | |
| 70 lrc->ni_frames = 0; | |
| 71 lrc->decimation_count = 0; | |
| 72 lrc->decimation_factor = 0; | |
| 73 | |
| 74 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { | |
| 75 lrc->rate_correction_factors[i] = 1.0; | |
| 76 } | |
| 77 | |
| 78 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { | |
| 79 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; | |
| 80 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; | |
| 81 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; | |
| 82 lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; | |
| 83 } else { | |
| 84 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; | |
| 85 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; | |
| 86 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; | |
| 87 lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + | |
| 88 oxcf->best_allowed_q) / 2; | |
| 89 lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + | |
| 90 oxcf->best_allowed_q) / 2; | |
| 91 if (oxcf->ss_enable_auto_arf[layer]) | |
| 92 lc->alt_ref_idx = alt_ref_idx++; | |
| 93 else | |
| 94 lc->alt_ref_idx = INVALID_IDX; | |
| 95 lc->gold_ref_idx = INVALID_IDX; | |
| 96 } | |
| 97 | |
| 98 lrc->buffer_level = oxcf->starting_buffer_level_ms * | |
| 99 lc->target_bandwidth / 1000; | |
| 100 lrc->bits_off_target = lrc->buffer_level; | |
| 101 } | |
| 102 | |
| 103 // Still have extra buffer for base layer golden frame | 99 // Still have extra buffer for base layer golden frame |
| 104 if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) | 100 if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) |
| 105 && alt_ref_idx < REF_FRAMES) | 101 && alt_ref_idx < REF_FRAMES) |
| 106 svc->layer_context[0].gold_ref_idx = alt_ref_idx; | 102 svc->layer_context[0].gold_ref_idx = alt_ref_idx; |
| 107 } | 103 } |
| 108 | 104 |
| 109 // Update the layer context from a change_config() call. | 105 // Update the layer context from a change_config() call. |
| 110 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, | 106 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, |
| 111 const int target_bandwidth) { | 107 const int target_bandwidth) { |
| 112 SVC *const svc = &cpi->svc; | 108 SVC *const svc = &cpi->svc; |
| 113 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 109 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 114 const RATE_CONTROL *const rc = &cpi->rc; | 110 const RATE_CONTROL *const rc = &cpi->rc; |
| 115 int layer; | 111 int sl, tl, layer = 0, spatial_layer_target; |
| 116 int layer_end; | |
| 117 float bitrate_alloc = 1.0; | 112 float bitrate_alloc = 1.0; |
| 118 | 113 |
| 119 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { | 114 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) { |
| 120 layer_end = svc->number_temporal_layers; | 115 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) { |
| 116 spatial_layer_target = 0; |
| 117 |
| 118 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) { |
| 119 layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers); |
| 120 svc->layer_context[layer].target_bandwidth = |
| 121 oxcf->layer_target_bitrate[layer]; |
| 122 } |
| 123 |
| 124 layer = LAYER_IDS_TO_IDX(sl, ((oxcf->ts_number_layers - 1) < 0 ? |
| 125 0 : (oxcf->ts_number_layers - 1)), oxcf->ts_number_layers); |
| 126 spatial_layer_target = |
| 127 svc->layer_context[layer].target_bandwidth = |
| 128 oxcf->layer_target_bitrate[layer]; |
| 129 |
| 130 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) { |
| 131 LAYER_CONTEXT *const lc = |
| 132 &svc->layer_context[sl * oxcf->ts_number_layers + tl]; |
| 133 RATE_CONTROL *const lrc = &lc->rc; |
| 134 |
| 135 lc->spatial_layer_target_bandwidth = spatial_layer_target; |
| 136 bitrate_alloc = (float)lc->target_bandwidth / spatial_layer_target; |
| 137 lrc->starting_buffer_level = |
| 138 (int64_t)(rc->starting_buffer_level * bitrate_alloc); |
| 139 lrc->optimal_buffer_level = |
| 140 (int64_t)(rc->optimal_buffer_level * bitrate_alloc); |
| 141 lrc->maximum_buffer_size = |
| 142 (int64_t)(rc->maximum_buffer_size * bitrate_alloc); |
| 143 lrc->bits_off_target = |
| 144 MIN(lrc->bits_off_target, lrc->maximum_buffer_size); |
| 145 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size); |
| 146 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl]; |
| 147 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
| 148 lrc->max_frame_bandwidth = rc->max_frame_bandwidth; |
| 149 lrc->worst_quality = rc->worst_quality; |
| 150 lrc->best_quality = rc->best_quality; |
| 151 } |
| 152 } |
| 121 } else { | 153 } else { |
| 122 layer_end = svc->number_spatial_layers; | 154 int layer_end; |
| 123 } | 155 float bitrate_alloc = 1.0; |
| 124 | |
| 125 for (layer = 0; layer < layer_end; ++layer) { | |
| 126 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | |
| 127 RATE_CONTROL *const lrc = &lc->rc; | |
| 128 | 156 |
| 129 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { | 157 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 130 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; | 158 layer_end = svc->number_temporal_layers; |
| 131 } else { | 159 } else { |
| 132 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; | 160 layer_end = svc->number_spatial_layers; |
| 133 } | 161 } |
| 134 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth; | 162 |
| 135 // Update buffer-related quantities. | 163 for (layer = 0; layer < layer_end; ++layer) { |
| 136 lrc->starting_buffer_level = | 164 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
| 137 (int64_t)(rc->starting_buffer_level * bitrate_alloc); | 165 RATE_CONTROL *const lrc = &lc->rc; |
| 138 lrc->optimal_buffer_level = | 166 |
| 139 (int64_t)(rc->optimal_buffer_level * bitrate_alloc); | 167 lc->target_bandwidth = oxcf->layer_target_bitrate[layer]; |
| 140 lrc->maximum_buffer_size = | 168 |
| 141 (int64_t)(rc->maximum_buffer_size * bitrate_alloc); | 169 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth; |
| 142 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size); | 170 // Update buffer-related quantities. |
| 143 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size); | 171 lrc->starting_buffer_level = |
| 144 // Update framerate-related quantities. | 172 (int64_t)(rc->starting_buffer_level * bitrate_alloc); |
| 145 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { | 173 lrc->optimal_buffer_level = |
| 146 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer]; | 174 (int64_t)(rc->optimal_buffer_level * bitrate_alloc); |
| 147 } else { | 175 lrc->maximum_buffer_size = |
| 148 lc->framerate = cpi->framerate; | 176 (int64_t)(rc->maximum_buffer_size * bitrate_alloc); |
| 177 lrc->bits_off_target = MIN(lrc->bits_off_target, |
| 178 lrc->maximum_buffer_size); |
| 179 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size); |
| 180 // Update framerate-related quantities. |
| 181 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 182 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer]; |
| 183 } else { |
| 184 lc->framerate = cpi->framerate; |
| 185 } |
| 186 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
| 187 lrc->max_frame_bandwidth = rc->max_frame_bandwidth; |
| 188 // Update qp-related quantities. |
| 189 lrc->worst_quality = rc->worst_quality; |
| 190 lrc->best_quality = rc->best_quality; |
| 149 } | 191 } |
| 150 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | |
| 151 lrc->max_frame_bandwidth = rc->max_frame_bandwidth; | |
| 152 // Update qp-related quantities. | |
| 153 lrc->worst_quality = rc->worst_quality; | |
| 154 lrc->best_quality = rc->best_quality; | |
| 155 } | 192 } |
| 156 } | 193 } |
| 157 | 194 |
| 158 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) { | 195 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) { |
| 159 return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ? | 196 if (is_one_pass_cbr_svc(cpi)) |
| 160 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] : | 197 return &cpi->svc.layer_context[cpi->svc.spatial_layer_id * |
| 161 &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 198 cpi->svc.number_temporal_layers + cpi->svc.temporal_layer_id]; |
| 199 else |
| 200 return (cpi->svc.number_temporal_layers > 1 && |
| 201 cpi->oxcf.rc_mode == VPX_CBR) ? |
| 202 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] : |
| 203 &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 162 } | 204 } |
| 163 | 205 |
| 164 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) { | 206 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) { |
| 165 SVC *const svc = &cpi->svc; | 207 SVC *const svc = &cpi->svc; |
| 166 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 208 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 167 LAYER_CONTEXT *const lc = get_layer_context(cpi); | 209 LAYER_CONTEXT *const lc = get_layer_context(cpi); |
| 168 RATE_CONTROL *const lrc = &lc->rc; | 210 RATE_CONTROL *const lrc = &lc->rc; |
| 169 const int layer = svc->temporal_layer_id; | 211 // Index into spatial+temporal arrays. |
| 212 const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers + |
| 213 svc->temporal_layer_id; |
| 214 const int tl = svc->temporal_layer_id; |
| 170 | 215 |
| 171 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer]; | 216 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl]; |
| 172 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 217 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
| 173 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth; | 218 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth; |
| 174 // Update the average layer frame size (non-cumulative per-frame-bw). | 219 // Update the average layer frame size (non-cumulative per-frame-bw). |
| 175 if (layer == 0) { | 220 if (tl == 0) { |
| 176 lc->avg_frame_size = lrc->avg_frame_bandwidth; | 221 lc->avg_frame_size = lrc->avg_frame_bandwidth; |
| 177 } else { | 222 } else { |
| 178 const double prev_layer_framerate = | 223 const double prev_layer_framerate = |
| 179 cpi->framerate / oxcf->ts_rate_decimator[layer - 1]; | 224 cpi->framerate / oxcf->ts_rate_decimator[tl - 1]; |
| 180 const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1]; | 225 const int prev_layer_target_bandwidth = |
| 226 oxcf->layer_target_bitrate[st_idx - 1]; |
| 181 lc->avg_frame_size = | 227 lc->avg_frame_size = |
| 182 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) / | 228 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) / |
| 183 (lc->framerate - prev_layer_framerate)); | 229 (lc->framerate - prev_layer_framerate)); |
| 184 } | 230 } |
| 185 } | 231 } |
| 186 | 232 |
| 187 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) { | 233 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) { |
| 188 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 234 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 189 LAYER_CONTEXT *const lc = get_layer_context(cpi); | 235 LAYER_CONTEXT *const lc = get_layer_context(cpi); |
| 190 RATE_CONTROL *const lrc = &lc->rc; | 236 RATE_CONTROL *const lrc = &lc->rc; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 vp9_init_second_pass(cpi); | 282 vp9_init_second_pass(cpi); |
| 237 | 283 |
| 238 twopass->total_stats.spatial_layer_id = i; | 284 twopass->total_stats.spatial_layer_id = i; |
| 239 twopass->total_left_stats.spatial_layer_id = i; | 285 twopass->total_left_stats.spatial_layer_id = i; |
| 240 } | 286 } |
| 241 svc->spatial_layer_id = 0; | 287 svc->spatial_layer_id = 0; |
| 242 } | 288 } |
| 243 | 289 |
| 244 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) { | 290 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) { |
| 245 LAYER_CONTEXT *const lc = | 291 LAYER_CONTEXT *const lc = |
| 246 (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ? | 292 &cpi->svc.layer_context[cpi->svc.spatial_layer_id * |
| 247 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] : | 293 cpi->svc.number_temporal_layers]; |
| 248 &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | |
| 249 ++lc->current_video_frame_in_layer; | 294 ++lc->current_video_frame_in_layer; |
| 250 ++lc->frames_from_key_frame; | 295 ++lc->frames_from_key_frame; |
| 251 } | 296 } |
| 252 | 297 |
| 253 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { | 298 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { |
| 254 return is_two_pass_svc(cpi) && | 299 return is_two_pass_svc(cpi) && |
| 255 cpi->svc.spatial_layer_id > 0 && | 300 cpi->svc.spatial_layer_id > 0 && |
| 256 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame; | 301 cpi->svc.layer_context[cpi->svc.spatial_layer_id * |
| 302 cpi->svc.number_temporal_layers + |
| 303 cpi->svc.temporal_layer_id].is_key_frame; |
| 257 } | 304 } |
| 258 | 305 |
| 259 #if CONFIG_SPATIAL_SVC | |
| 260 static void get_layer_resolution(const int width_org, const int height_org, | 306 static void get_layer_resolution(const int width_org, const int height_org, |
| 261 const int num, const int den, | 307 const int num, const int den, |
| 262 int *width_out, int *height_out) { | 308 int *width_out, int *height_out) { |
| 263 int w, h; | 309 int w, h; |
| 264 | 310 |
| 265 if (width_out == NULL || height_out == NULL || den == 0) | 311 if (width_out == NULL || height_out == NULL || den == 0) |
| 266 return; | 312 return; |
| 267 | 313 |
| 268 w = width_org * num / den; | 314 w = width_org * num / den; |
| 269 h = height_org * num / den; | 315 h = height_org * num / den; |
| 270 | 316 |
| 271 // make height and width even to make chrome player happy | 317 // make height and width even to make chrome player happy |
| 272 w += w % 2; | 318 w += w % 2; |
| 273 h += h % 2; | 319 h += h % 2; |
| 274 | 320 |
| 275 *width_out = w; | 321 *width_out = w; |
| 276 *height_out = h; | 322 *height_out = h; |
| 277 } | 323 } |
| 278 | 324 |
| 325 // The function sets proper ref_frame_flags, buffer indices, and buffer update |
| 326 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering |
| 327 // scheme. |
| 328 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) { |
| 329 int frame_num_within_temporal_struct = 0; |
| 330 int spatial_id, temporal_id; |
| 331 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode; |
| 332 frame_num_within_temporal_struct = |
| 333 cpi->svc.layer_context[cpi->svc.spatial_layer_id * |
| 334 cpi->svc.number_temporal_layers].current_video_frame_in_layer % 4; |
| 335 temporal_id = cpi->svc.temporal_layer_id = |
| 336 (frame_num_within_temporal_struct & 1) ? 2 : |
| 337 (frame_num_within_temporal_struct >> 1); |
| 338 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame = |
| 339 cpi->ext_refresh_alt_ref_frame = 0; |
| 340 if (!temporal_id) { |
| 341 cpi->ext_refresh_frame_flags_pending = 1; |
| 342 cpi->ext_refresh_last_frame = 1; |
| 343 if (!spatial_id) { |
| 344 cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 345 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) { |
| 346 // base layer is a key frame. |
| 347 cpi->ref_frame_flags = VP9_GOLD_FLAG; |
| 348 } else { |
| 349 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 350 } |
| 351 } else if (temporal_id == 1) { |
| 352 cpi->ext_refresh_frame_flags_pending = 1; |
| 353 cpi->ext_refresh_alt_ref_frame = 1; |
| 354 if (!spatial_id) { |
| 355 cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 356 } else { |
| 357 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 358 } |
| 359 } else { |
| 360 if (frame_num_within_temporal_struct == 1) { |
| 361 // the first tl2 picture |
| 362 if (!spatial_id) { |
| 363 cpi->ext_refresh_frame_flags_pending = 1; |
| 364 cpi->ext_refresh_alt_ref_frame = 1; |
| 365 cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 366 } else if (spatial_id < cpi->svc.number_spatial_layers - 1) { |
| 367 cpi->ext_refresh_frame_flags_pending = 1; |
| 368 cpi->ext_refresh_alt_ref_frame = 1; |
| 369 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 370 } else { // Top layer |
| 371 cpi->ext_refresh_frame_flags_pending = 0; |
| 372 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 373 } |
| 374 } else { |
| 375 // The second tl2 picture |
| 376 if (!spatial_id) { |
| 377 cpi->ext_refresh_frame_flags_pending = 1; |
| 378 cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 379 cpi->ext_refresh_last_frame = 1; |
| 380 } else if (spatial_id < cpi->svc.number_spatial_layers - 1) { |
| 381 cpi->ext_refresh_frame_flags_pending = 1; |
| 382 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 383 cpi->ext_refresh_last_frame = 1; |
| 384 } else { // top layer |
| 385 cpi->ext_refresh_frame_flags_pending = 0; |
| 386 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 387 } |
| 388 } |
| 389 } |
| 390 if (temporal_id == 0) { |
| 391 cpi->lst_fb_idx = spatial_id; |
| 392 if (spatial_id) |
| 393 cpi->gld_fb_idx = spatial_id - 1; |
| 394 else |
| 395 cpi->gld_fb_idx = 0; |
| 396 cpi->alt_fb_idx = 0; |
| 397 } else if (temporal_id == 1) { |
| 398 cpi->lst_fb_idx = spatial_id; |
| 399 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1; |
| 400 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id; |
| 401 } else if (frame_num_within_temporal_struct == 1) { |
| 402 cpi->lst_fb_idx = spatial_id; |
| 403 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1; |
| 404 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id; |
| 405 } else { |
| 406 cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id; |
| 407 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1; |
| 408 cpi->alt_fb_idx = 0; |
| 409 } |
| 410 } |
| 411 |
| 412 // The function sets proper ref_frame_flags, buffer indices, and buffer update |
| 413 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering |
| 414 // scheme. |
| 415 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) { |
| 416 int spatial_id, temporal_id; |
| 417 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode; |
| 418 temporal_id = cpi->svc.temporal_layer_id = |
| 419 cpi->svc.layer_context[cpi->svc.spatial_layer_id * |
| 420 cpi->svc.number_temporal_layers].current_video_frame_in_layer & 1; |
| 421 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame = |
| 422 cpi->ext_refresh_alt_ref_frame = 0; |
| 423 if (!temporal_id) { |
| 424 cpi->ext_refresh_frame_flags_pending = 1; |
| 425 cpi->ext_refresh_last_frame = 1; |
| 426 if (!spatial_id) { |
| 427 cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 428 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) { |
| 429 // base layer is a key frame. |
| 430 cpi->ref_frame_flags = VP9_GOLD_FLAG; |
| 431 } else { |
| 432 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 433 } |
| 434 } else if (temporal_id == 1) { |
| 435 cpi->ext_refresh_frame_flags_pending = 1; |
| 436 cpi->ext_refresh_alt_ref_frame = 1; |
| 437 if (!spatial_id) { |
| 438 cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 439 } else { |
| 440 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 441 } |
| 442 } |
| 443 |
| 444 if (temporal_id == 0) { |
| 445 cpi->lst_fb_idx = spatial_id; |
| 446 if (spatial_id) |
| 447 cpi->gld_fb_idx = spatial_id - 1; |
| 448 else |
| 449 cpi->gld_fb_idx = 0; |
| 450 cpi->alt_fb_idx = 0; |
| 451 } else if (temporal_id == 1) { |
| 452 cpi->lst_fb_idx = spatial_id; |
| 453 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1; |
| 454 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id; |
| 455 } |
| 456 } |
| 457 |
| 458 // The function sets proper ref_frame_flags, buffer indices, and buffer update |
| 459 // variables for temporal layering mode 0 - that has no temporal layering. |
| 460 static void set_flags_and_fb_idx_for_temporal_mode_noLayering( |
| 461 VP9_COMP *const cpi) { |
| 462 int spatial_id; |
| 463 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode; |
| 464 cpi->ext_refresh_last_frame = |
| 465 cpi->ext_refresh_golden_frame = cpi->ext_refresh_alt_ref_frame = 0; |
| 466 cpi->ext_refresh_frame_flags_pending = 1; |
| 467 cpi->ext_refresh_last_frame = 1; |
| 468 if (!spatial_id) { |
| 469 cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 470 } else if (cpi->svc.layer_context[0].is_key_frame) { |
| 471 cpi->ref_frame_flags = VP9_GOLD_FLAG; |
| 472 } else { |
| 473 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
| 474 } |
| 475 cpi->lst_fb_idx = spatial_id; |
| 476 if (spatial_id) |
| 477 cpi->gld_fb_idx = spatial_id - 1; |
| 478 else |
| 479 cpi->gld_fb_idx = 0; |
| 480 } |
| 481 |
| 482 int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) { |
| 483 int width = 0, height = 0; |
| 484 LAYER_CONTEXT *lc = NULL; |
| 485 |
| 486 if (cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) { |
| 487 set_flags_and_fb_idx_for_temporal_mode3(cpi); |
| 488 } else if (cpi->svc.temporal_layering_mode == |
| 489 VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) { |
| 490 set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi); |
| 491 } else if (cpi->svc.temporal_layering_mode == |
| 492 VP9E_TEMPORAL_LAYERING_MODE_0101) { |
| 493 set_flags_and_fb_idx_for_temporal_mode2(cpi); |
| 494 } else if (cpi->svc.temporal_layering_mode == |
| 495 VP9E_TEMPORAL_LAYERING_MODE_BYPASS) { |
| 496 // VP9E_TEMPORAL_LAYERING_MODE_BYPASS : |
| 497 // if the code goes here, it means the encoder will be relying on the |
| 498 // flags from outside for layering. |
| 499 // However, since when spatial+temporal layering is used, the buffer indices |
| 500 // cannot be derived automatically, the bypass mode will only work when the |
| 501 // number of spatial layers equals 1. |
| 502 assert(cpi->svc.number_spatial_layers == 1); |
| 503 } |
| 504 |
| 505 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id * |
| 506 cpi->svc.number_temporal_layers + |
| 507 cpi->svc.temporal_layer_id]; |
| 508 |
| 509 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height, |
| 510 lc->scaling_factor_num, lc->scaling_factor_den, |
| 511 &width, &height); |
| 512 |
| 513 if (vp9_set_size_literal(cpi, width, height) != 0) |
| 514 return VPX_CODEC_INVALID_PARAM; |
| 515 |
| 516 return 0; |
| 517 } |
| 518 |
| 519 #if CONFIG_SPATIAL_SVC |
| 279 int vp9_svc_start_frame(VP9_COMP *const cpi) { | 520 int vp9_svc_start_frame(VP9_COMP *const cpi) { |
| 280 int width = 0, height = 0; | 521 int width = 0, height = 0; |
| 281 LAYER_CONTEXT *lc; | 522 LAYER_CONTEXT *lc; |
| 282 struct lookahead_entry *buf; | 523 struct lookahead_entry *buf; |
| 283 int count = 1 << (cpi->svc.number_temporal_layers - 1); | 524 int count = 1 << (cpi->svc.number_temporal_layers - 1); |
| 284 | 525 |
| 285 cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode; | 526 cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode; |
| 286 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 527 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 287 | 528 |
| 288 cpi->svc.temporal_layer_id = 0; | 529 cpi->svc.temporal_layer_id = 0; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 if (vp9_set_size_literal(cpi, width, height) != 0) | 620 if (vp9_set_size_literal(cpi, width, height) != 0) |
| 380 return VPX_CODEC_INVALID_PARAM; | 621 return VPX_CODEC_INVALID_PARAM; |
| 381 | 622 |
| 382 vp9_set_high_precision_mv(cpi, 1); | 623 vp9_set_high_precision_mv(cpi, 1); |
| 383 | 624 |
| 384 cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source; | 625 cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source; |
| 385 | 626 |
| 386 return 0; | 627 return 0; |
| 387 } | 628 } |
| 388 | 629 |
| 630 #endif |
| 631 |
| 389 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi, | 632 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi, |
| 390 struct lookahead_ctx *ctx, | 633 struct lookahead_ctx *ctx, |
| 391 int drain) { | 634 int drain) { |
| 392 struct lookahead_entry *buf = NULL; | 635 struct lookahead_entry *buf = NULL; |
| 393 | |
| 394 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) { | 636 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) { |
| 395 buf = vp9_lookahead_peek(ctx, 0); | 637 buf = vp9_lookahead_peek(ctx, 0); |
| 396 if (buf != NULL) { | 638 if (buf != NULL) { |
| 397 // Only remove the buffer when pop the highest layer. | 639 // Only remove the buffer when pop the highest layer. |
| 398 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { | 640 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { |
| 399 vp9_lookahead_pop(ctx, drain); | 641 vp9_lookahead_pop(ctx, drain); |
| 400 } | 642 } |
| 401 } | 643 } |
| 402 } | 644 } |
| 403 | |
| 404 return buf; | 645 return buf; |
| 405 } | 646 } |
| 406 #endif | |
| OLD | NEW |