| 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 int alt_ref_idx = svc->number_spatial_layers * svc->number_temporal_layers; |
| 23 | 23 |
| 24 svc->spatial_layer_id = 0; | 24 svc->spatial_layer_id = 0; |
| 25 svc->temporal_layer_id = 0; | 25 svc->temporal_layer_id = 0; |
| 26 | 26 |
| 27 if (svc->number_temporal_layers > 1) { | 27 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 28 layer_end = svc->number_temporal_layers; | 28 layer_end = svc->number_temporal_layers; |
| 29 } else { | 29 } else { |
| 30 layer_end = svc->number_spatial_layers; | 30 layer_end = svc->number_spatial_layers; |
| 31 } | 31 } |
| 32 | 32 |
| 33 for (layer = 0; layer < layer_end; ++layer) { | 33 for (layer = 0; layer < layer_end; ++layer) { |
| 34 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | 34 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
| 35 RATE_CONTROL *const lrc = &lc->rc; | 35 RATE_CONTROL *const lrc = &lc->rc; |
| 36 int i; | 36 int i; |
| 37 lc->current_video_frame_in_layer = 0; | 37 lc->current_video_frame_in_layer = 0; |
| 38 lc->layer_size = 0; | 38 lc->layer_size = 0; |
| 39 lc->frames_from_key_frame = 0; |
| 40 lc->last_frame_type = FRAME_TYPES; |
| 39 lrc->ni_av_qi = oxcf->worst_allowed_q; | 41 lrc->ni_av_qi = oxcf->worst_allowed_q; |
| 40 lrc->total_actual_bits = 0; | 42 lrc->total_actual_bits = 0; |
| 41 lrc->total_target_vs_actual = 0; | 43 lrc->total_target_vs_actual = 0; |
| 42 lrc->ni_tot_qi = 0; | 44 lrc->ni_tot_qi = 0; |
| 43 lrc->tot_q = 0.0; | 45 lrc->tot_q = 0.0; |
| 44 lrc->avg_q = 0.0; | 46 lrc->avg_q = 0.0; |
| 45 lrc->ni_frames = 0; | 47 lrc->ni_frames = 0; |
| 46 lrc->decimation_count = 0; | 48 lrc->decimation_count = 0; |
| 47 lrc->decimation_factor = 0; | 49 lrc->decimation_factor = 0; |
| 48 | 50 |
| 49 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { | 51 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
| 50 lrc->rate_correction_factors[i] = 1.0; | 52 lrc->rate_correction_factors[i] = 1.0; |
| 51 } | 53 } |
| 52 | 54 |
| 53 if (svc->number_temporal_layers > 1) { | 55 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 54 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; | 56 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; |
| 55 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; | 57 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
| 56 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; | 58 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
| 57 } else { | 59 } else { |
| 58 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; | 60 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; |
| 59 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; | 61 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
| 60 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; | 62 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q; |
| 61 lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + | 63 lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + |
| 62 oxcf->best_allowed_q) / 2; | 64 oxcf->best_allowed_q) / 2; |
| 63 lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + | 65 lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + |
| 64 oxcf->best_allowed_q) / 2; | 66 oxcf->best_allowed_q) / 2; |
| 65 if (oxcf->ss_play_alternate[layer]) | 67 if (oxcf->ss_play_alternate[layer]) |
| 66 lc->alt_ref_idx = alt_ref_idx++; | 68 lc->alt_ref_idx = alt_ref_idx++; |
| 67 else | 69 else |
| 68 lc->alt_ref_idx = -1; | 70 lc->alt_ref_idx = -1; |
| 69 lc->gold_ref_idx = -1; | 71 lc->gold_ref_idx = -1; |
| 70 } | 72 } |
| 71 | 73 |
| 72 lrc->buffer_level = oxcf->starting_buffer_level_ms * | 74 lrc->buffer_level = oxcf->starting_buffer_level_ms * |
| 73 lc->target_bandwidth / 1000; | 75 lc->target_bandwidth / 1000; |
| 74 lrc->bits_off_target = lrc->buffer_level; | 76 lrc->bits_off_target = lrc->buffer_level; |
| 75 } | 77 } |
| 76 | 78 |
| 77 // Still have extra buffer for base layer golden frame | 79 // Still have extra buffer for base layer golden frame |
| 78 if (svc->number_spatial_layers > 1 && alt_ref_idx < REF_FRAMES) | 80 if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) |
| 81 && alt_ref_idx < REF_FRAMES) |
| 79 svc->layer_context[0].gold_ref_idx = alt_ref_idx; | 82 svc->layer_context[0].gold_ref_idx = alt_ref_idx; |
| 80 } | 83 } |
| 81 | 84 |
| 82 // Update the layer context from a change_config() call. | 85 // Update the layer context from a change_config() call. |
| 83 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, | 86 void vp9_update_layer_context_change_config(VP9_COMP *const cpi, |
| 84 const int target_bandwidth) { | 87 const int target_bandwidth) { |
| 85 SVC *const svc = &cpi->svc; | 88 SVC *const svc = &cpi->svc; |
| 86 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 89 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 87 const RATE_CONTROL *const rc = &cpi->rc; | 90 const RATE_CONTROL *const rc = &cpi->rc; |
| 88 int layer; | 91 int layer; |
| 89 int layer_end; | 92 int layer_end; |
| 90 float bitrate_alloc = 1.0; | 93 float bitrate_alloc = 1.0; |
| 91 | 94 |
| 92 if (svc->number_temporal_layers > 1) { | 95 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 93 layer_end = svc->number_temporal_layers; | 96 layer_end = svc->number_temporal_layers; |
| 94 } else { | 97 } else { |
| 95 layer_end = svc->number_spatial_layers; | 98 layer_end = svc->number_spatial_layers; |
| 96 } | 99 } |
| 97 | 100 |
| 98 for (layer = 0; layer < layer_end; ++layer) { | 101 for (layer = 0; layer < layer_end; ++layer) { |
| 99 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | 102 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
| 100 RATE_CONTROL *const lrc = &lc->rc; | 103 RATE_CONTROL *const lrc = &lc->rc; |
| 101 | 104 |
| 102 if (svc->number_temporal_layers > 1) { | 105 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 103 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; | 106 lc->target_bandwidth = oxcf->ts_target_bitrate[layer]; |
| 104 } else { | 107 } else { |
| 105 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; | 108 lc->target_bandwidth = oxcf->ss_target_bitrate[layer]; |
| 106 } | 109 } |
| 107 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth; | 110 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth; |
| 108 // Update buffer-related quantities. | 111 // Update buffer-related quantities. |
| 109 lrc->starting_buffer_level = | 112 lrc->starting_buffer_level = |
| 110 (int64_t)(rc->starting_buffer_level * bitrate_alloc); | 113 (int64_t)(rc->starting_buffer_level * bitrate_alloc); |
| 111 lrc->optimal_buffer_level = | 114 lrc->optimal_buffer_level = |
| 112 (int64_t)(rc->optimal_buffer_level * bitrate_alloc); | 115 (int64_t)(rc->optimal_buffer_level * bitrate_alloc); |
| 113 lrc->maximum_buffer_size = | 116 lrc->maximum_buffer_size = |
| 114 (int64_t)(rc->maximum_buffer_size * bitrate_alloc); | 117 (int64_t)(rc->maximum_buffer_size * bitrate_alloc); |
| 115 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size); | 118 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size); |
| 116 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size); | 119 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size); |
| 117 // Update framerate-related quantities. | 120 // Update framerate-related quantities. |
| 118 if (svc->number_temporal_layers > 1) { | 121 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 119 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer]; | 122 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer]; |
| 120 } else { | 123 } else { |
| 121 lc->framerate = cpi->framerate; | 124 lc->framerate = cpi->framerate; |
| 122 } | 125 } |
| 123 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 126 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
| 124 lrc->max_frame_bandwidth = rc->max_frame_bandwidth; | 127 lrc->max_frame_bandwidth = rc->max_frame_bandwidth; |
| 125 // Update qp-related quantities. | 128 // Update qp-related quantities. |
| 126 lrc->worst_quality = rc->worst_quality; | 129 lrc->worst_quality = rc->worst_quality; |
| 127 lrc->best_quality = rc->best_quality; | 130 lrc->best_quality = rc->best_quality; |
| 128 } | 131 } |
| 129 } | 132 } |
| 130 | 133 |
| 131 static LAYER_CONTEXT *get_layer_context(SVC *svc) { | 134 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) { |
| 132 return svc->number_temporal_layers > 1 ? | 135 return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ? |
| 133 &svc->layer_context[svc->temporal_layer_id] : | 136 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] : |
| 134 &svc->layer_context[svc->spatial_layer_id]; | 137 &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 135 } | 138 } |
| 136 | 139 |
| 137 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) { | 140 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) { |
| 138 SVC *const svc = &cpi->svc; | 141 SVC *const svc = &cpi->svc; |
| 139 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 142 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 140 LAYER_CONTEXT *const lc = get_layer_context(svc); | 143 LAYER_CONTEXT *const lc = get_layer_context(cpi); |
| 141 RATE_CONTROL *const lrc = &lc->rc; | 144 RATE_CONTROL *const lrc = &lc->rc; |
| 142 const int layer = svc->temporal_layer_id; | 145 const int layer = svc->temporal_layer_id; |
| 143 | 146 |
| 144 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer]; | 147 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer]; |
| 145 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 148 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
| 146 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth; | 149 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth; |
| 147 // Update the average layer frame size (non-cumulative per-frame-bw). | 150 // Update the average layer frame size (non-cumulative per-frame-bw). |
| 148 if (layer == 0) { | 151 if (layer == 0) { |
| 149 lc->avg_frame_size = lrc->avg_frame_bandwidth; | 152 lc->avg_frame_size = lrc->avg_frame_bandwidth; |
| 150 } else { | 153 } else { |
| 151 const double prev_layer_framerate = | 154 const double prev_layer_framerate = |
| 152 cpi->framerate / oxcf->ts_rate_decimator[layer - 1]; | 155 cpi->framerate / oxcf->ts_rate_decimator[layer - 1]; |
| 153 const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1]; | 156 const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1]; |
| 154 lc->avg_frame_size = | 157 lc->avg_frame_size = |
| 155 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) / | 158 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) / |
| 156 (lc->framerate - prev_layer_framerate)); | 159 (lc->framerate - prev_layer_framerate)); |
| 157 } | 160 } |
| 158 } | 161 } |
| 159 | 162 |
| 160 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) { | 163 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) { |
| 161 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 164 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 162 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 165 LAYER_CONTEXT *const lc = get_layer_context(cpi); |
| 163 RATE_CONTROL *const lrc = &lc->rc; | 166 RATE_CONTROL *const lrc = &lc->rc; |
| 164 | 167 |
| 165 lc->framerate = framerate; | 168 lc->framerate = framerate; |
| 166 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); | 169 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate); |
| 167 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth * | 170 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth * |
| 168 oxcf->two_pass_vbrmin_section / 100); | 171 oxcf->two_pass_vbrmin_section / 100); |
| 169 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth * | 172 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth * |
| 170 oxcf->two_pass_vbrmax_section) / 100); | 173 oxcf->two_pass_vbrmax_section) / 100); |
| 171 vp9_rc_set_gf_max_interval(cpi, lrc); | 174 vp9_rc_set_gf_max_interval(cpi, lrc); |
| 172 } | 175 } |
| 173 | 176 |
| 174 void vp9_restore_layer_context(VP9_COMP *const cpi) { | 177 void vp9_restore_layer_context(VP9_COMP *const cpi) { |
| 175 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 178 LAYER_CONTEXT *const lc = get_layer_context(cpi); |
| 176 const int old_frame_since_key = cpi->rc.frames_since_key; | 179 const int old_frame_since_key = cpi->rc.frames_since_key; |
| 177 const int old_frame_to_key = cpi->rc.frames_to_key; | 180 const int old_frame_to_key = cpi->rc.frames_to_key; |
| 178 | 181 |
| 179 cpi->rc = lc->rc; | 182 cpi->rc = lc->rc; |
| 180 cpi->twopass = lc->twopass; | 183 cpi->twopass = lc->twopass; |
| 181 cpi->oxcf.target_bandwidth = lc->target_bandwidth; | 184 cpi->oxcf.target_bandwidth = lc->target_bandwidth; |
| 182 cpi->alt_ref_source = lc->alt_ref_source; | 185 cpi->alt_ref_source = lc->alt_ref_source; |
| 183 // Reset the frames_since_key and frames_to_key counters to their values | 186 // Reset the frames_since_key and frames_to_key counters to their values |
| 184 // before the layer restore. Keep these defined for the stream (not layer). | 187 // before the layer restore. Keep these defined for the stream (not layer). |
| 185 if (cpi->svc.number_temporal_layers > 1) { | 188 if (cpi->svc.number_temporal_layers > 1) { |
| 186 cpi->rc.frames_since_key = old_frame_since_key; | 189 cpi->rc.frames_since_key = old_frame_since_key; |
| 187 cpi->rc.frames_to_key = old_frame_to_key; | 190 cpi->rc.frames_to_key = old_frame_to_key; |
| 188 } | 191 } |
| 189 } | 192 } |
| 190 | 193 |
| 191 void vp9_save_layer_context(VP9_COMP *const cpi) { | 194 void vp9_save_layer_context(VP9_COMP *const cpi) { |
| 192 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 195 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 193 LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc); | 196 LAYER_CONTEXT *const lc = get_layer_context(cpi); |
| 194 | 197 |
| 195 lc->rc = cpi->rc; | 198 lc->rc = cpi->rc; |
| 196 lc->twopass = cpi->twopass; | 199 lc->twopass = cpi->twopass; |
| 197 lc->target_bandwidth = (int)oxcf->target_bandwidth; | 200 lc->target_bandwidth = (int)oxcf->target_bandwidth; |
| 198 lc->alt_ref_source = cpi->alt_ref_source; | 201 lc->alt_ref_source = cpi->alt_ref_source; |
| 199 } | 202 } |
| 200 | 203 |
| 201 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) { | 204 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) { |
| 202 SVC *const svc = &cpi->svc; | 205 SVC *const svc = &cpi->svc; |
| 203 int i; | 206 int i; |
| 204 | 207 |
| 205 for (i = 0; i < svc->number_spatial_layers; ++i) { | 208 for (i = 0; i < svc->number_spatial_layers; ++i) { |
| 206 TWO_PASS *const twopass = &svc->layer_context[i].twopass; | 209 TWO_PASS *const twopass = &svc->layer_context[i].twopass; |
| 207 | 210 |
| 208 svc->spatial_layer_id = i; | 211 svc->spatial_layer_id = i; |
| 209 vp9_init_second_pass(cpi); | 212 vp9_init_second_pass(cpi); |
| 210 | 213 |
| 211 twopass->total_stats.spatial_layer_id = i; | 214 twopass->total_stats.spatial_layer_id = i; |
| 212 twopass->total_left_stats.spatial_layer_id = i; | 215 twopass->total_left_stats.spatial_layer_id = i; |
| 213 } | 216 } |
| 214 svc->spatial_layer_id = 0; | 217 svc->spatial_layer_id = 0; |
| 215 } | 218 } |
| 216 | 219 |
| 217 void vp9_inc_frame_in_layer(SVC *svc) { | 220 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) { |
| 218 LAYER_CONTEXT *const lc = (svc->number_temporal_layers > 1) | 221 LAYER_CONTEXT *const lc = |
| 219 ? &svc->layer_context[svc->temporal_layer_id] | 222 (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ? |
| 220 : &svc->layer_context[svc->spatial_layer_id]; | 223 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] : |
| 224 &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 221 ++lc->current_video_frame_in_layer; | 225 ++lc->current_video_frame_in_layer; |
| 226 ++lc->frames_from_key_frame; |
| 222 } | 227 } |
| 223 | 228 |
| 224 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { | 229 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) { |
| 225 return is_spatial_svc(cpi) && | 230 return is_two_pass_svc(cpi) && |
| 226 cpi->svc.spatial_layer_id > 0 && | 231 cpi->svc.spatial_layer_id > 0 && |
| 227 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame; | 232 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame; |
| 228 } | 233 } |
| 229 | 234 |
| 230 #if CONFIG_SPATIAL_SVC | 235 #if CONFIG_SPATIAL_SVC |
| 231 int vp9_svc_lookahead_push(const VP9_COMP *const cpi, struct lookahead_ctx *ctx, | 236 int vp9_svc_lookahead_push(const VP9_COMP *const cpi, struct lookahead_ctx *ctx, |
| 232 YV12_BUFFER_CONFIG *src, int64_t ts_start, | 237 YV12_BUFFER_CONFIG *src, int64_t ts_start, |
| 233 int64_t ts_end, unsigned int flags) { | 238 int64_t ts_end, unsigned int flags) { |
| 234 struct lookahead_entry *buf; | 239 struct lookahead_entry *buf; |
| 235 int i, index; | 240 int i, index; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 250 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) | 255 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) |
| 251 buf->svc_params[i] = cpi->svc.layer_context[i].svc_params_received; | 256 buf->svc_params[i] = cpi->svc.layer_context[i].svc_params_received; |
| 252 | 257 |
| 253 return 0; | 258 return 0; |
| 254 } | 259 } |
| 255 | 260 |
| 256 static int copy_svc_params(VP9_COMP *const cpi, struct lookahead_entry *buf) { | 261 static int copy_svc_params(VP9_COMP *const cpi, struct lookahead_entry *buf) { |
| 257 int layer_id; | 262 int layer_id; |
| 258 vpx_svc_parameters_t *layer_param; | 263 vpx_svc_parameters_t *layer_param; |
| 259 LAYER_CONTEXT *lc; | 264 LAYER_CONTEXT *lc; |
| 265 int count = 1 << (cpi->svc.number_temporal_layers - 1); |
| 260 | 266 |
| 261 // Find the next layer to be encoded | 267 // Find the next layer to be encoded |
| 262 for (layer_id = 0; layer_id < cpi->svc.number_spatial_layers; ++layer_id) { | 268 for (layer_id = 0; layer_id < cpi->svc.number_spatial_layers; ++layer_id) { |
| 263 if (buf->svc_params[layer_id].spatial_layer >=0) | 269 if (buf->svc_params[layer_id].spatial_layer >=0) |
| 264 break; | 270 break; |
| 265 } | 271 } |
| 266 | 272 |
| 267 if (layer_id == cpi->svc.number_spatial_layers) | 273 if (layer_id == cpi->svc.number_spatial_layers) |
| 268 return 1; | 274 return 1; |
| 269 | 275 |
| 270 layer_param = &buf->svc_params[layer_id]; | 276 layer_param = &buf->svc_params[layer_id]; |
| 271 cpi->svc.spatial_layer_id = layer_param->spatial_layer; | 277 cpi->svc.spatial_layer_id = layer_param->spatial_layer; |
| 272 cpi->svc.temporal_layer_id = layer_param->temporal_layer; | 278 cpi->svc.temporal_layer_id = layer_param->temporal_layer; |
| 273 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; | 279 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; |
| 274 | 280 |
| 275 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 281 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 276 | 282 |
| 277 cpi->lst_fb_idx = cpi->svc.spatial_layer_id; | 283 cpi->svc.temporal_layer_id = 0; |
| 284 while ((lc->current_video_frame_in_layer % count) != 0) { |
| 285 ++cpi->svc.temporal_layer_id; |
| 286 count >>= 1; |
| 287 } |
| 278 | 288 |
| 279 if (cpi->svc.spatial_layer_id < 1) | 289 cpi->lst_fb_idx = |
| 290 cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers + |
| 291 cpi->svc.temporal_layer_id; |
| 292 if (lc->frames_from_key_frame < cpi->svc.number_temporal_layers) |
| 293 cpi->ref_frame_flags &= ~VP9_LAST_FLAG; |
| 294 |
| 295 if (cpi->svc.spatial_layer_id == 0) { |
| 296 if (cpi->svc.temporal_layer_id == 0) |
| 280 cpi->gld_fb_idx = lc->gold_ref_idx >= 0 ? | 297 cpi->gld_fb_idx = lc->gold_ref_idx >= 0 ? |
| 281 lc->gold_ref_idx : cpi->lst_fb_idx; | 298 lc->gold_ref_idx : cpi->lst_fb_idx; |
| 282 else | 299 else |
| 283 cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1; | 300 cpi->gld_fb_idx = cpi->lst_fb_idx - 1; |
| 301 } else { |
| 302 if (cpi->svc.temporal_layer_id == 0) |
| 303 cpi->gld_fb_idx = cpi->svc.spatial_layer_id - |
| 304 cpi->svc.number_temporal_layers; |
| 305 else |
| 306 cpi->gld_fb_idx = cpi->lst_fb_idx - 1; |
| 307 } |
| 284 | 308 |
| 285 if (lc->current_video_frame_in_layer == 0) { | 309 if (lc->current_video_frame_in_layer == 0) { |
| 286 if (cpi->svc.spatial_layer_id >= 2) { | 310 if (cpi->svc.spatial_layer_id >= 2) { |
| 287 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2; | 311 cpi->alt_fb_idx = |
| 312 cpi->svc.spatial_layer_id - 2 * cpi->svc.number_temporal_layers; |
| 288 } else { | 313 } else { |
| 289 cpi->alt_fb_idx = cpi->lst_fb_idx; | 314 cpi->alt_fb_idx = cpi->lst_fb_idx; |
| 290 cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG); | 315 cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG); |
| 291 } | 316 } |
| 292 } else { | 317 } else { |
| 293 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id]) { | 318 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id]) { |
| 294 cpi->alt_fb_idx = lc->alt_ref_idx; | 319 cpi->alt_fb_idx = lc->alt_ref_idx; |
| 295 if (!lc->has_alt_frame) | 320 if (!lc->has_alt_frame) |
| 296 cpi->ref_frame_flags &= (~VP9_ALT_FLAG); | 321 cpi->ref_frame_flags &= (~VP9_ALT_FLAG); |
| 297 } else { | 322 } else { |
| 298 // Find a proper alt_fb_idx for layers that don't have alt ref frame | 323 // Find a proper alt_fb_idx for layers that don't have alt ref frame |
| 299 if (cpi->svc.spatial_layer_id == 0) { | 324 if (cpi->svc.spatial_layer_id == 0) { |
| 300 cpi->alt_fb_idx = cpi->lst_fb_idx; | 325 cpi->alt_fb_idx = cpi->lst_fb_idx; |
| 301 } else { | 326 } else { |
| 302 LAYER_CONTEXT *lc_lower = | 327 LAYER_CONTEXT *lc_lower = |
| 303 &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1]; | 328 &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1]; |
| 304 | 329 |
| 305 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id - 1] && | 330 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id - 1] && |
| 306 lc_lower->alt_ref_source != NULL) | 331 lc_lower->alt_ref_source != NULL) |
| 307 cpi->alt_fb_idx = lc_lower->alt_ref_idx; | 332 cpi->alt_fb_idx = lc_lower->alt_ref_idx; |
| 308 else if (cpi->svc.spatial_layer_id >= 2) | 333 else if (cpi->svc.spatial_layer_id >= 2) |
| 309 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2; | 334 cpi->alt_fb_idx = |
| 335 cpi->svc.spatial_layer_id - 2 * cpi->svc.number_temporal_layers; |
| 310 else | 336 else |
| 311 cpi->alt_fb_idx = cpi->lst_fb_idx; | 337 cpi->alt_fb_idx = cpi->lst_fb_idx; |
| 312 } | 338 } |
| 313 } | 339 } |
| 314 } | 340 } |
| 315 | 341 |
| 316 if (vp9_set_size_literal(cpi, layer_param->width, layer_param->height) != 0) | 342 if (vp9_set_size_literal(cpi, layer_param->width, layer_param->height) != 0) |
| 317 return VPX_CODEC_INVALID_PARAM; | 343 return VPX_CODEC_INVALID_PARAM; |
| 318 | 344 |
| 319 cpi->oxcf.worst_allowed_q = | 345 cpi->oxcf.worst_allowed_q = |
| 320 vp9_quantizer_to_qindex(layer_param->max_quantizer); | 346 vp9_quantizer_to_qindex(layer_param->max_quantizer); |
| 321 cpi->oxcf.best_allowed_q = | 347 cpi->oxcf.best_allowed_q = |
| 322 vp9_quantizer_to_qindex(layer_param->min_quantizer); | 348 vp9_quantizer_to_qindex(layer_param->min_quantizer); |
| 323 | 349 |
| 324 vp9_change_config(cpi, &cpi->oxcf); | 350 vp9_change_config(cpi, &cpi->oxcf); |
| 325 | 351 |
| 326 vp9_set_high_precision_mv(cpi, 1); | 352 vp9_set_high_precision_mv(cpi, 1); |
| 327 | 353 |
| 328 cpi->alt_ref_source = get_layer_context(&cpi->svc)->alt_ref_source; | 354 cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source; |
| 329 | 355 |
| 330 return 0; | 356 return 0; |
| 331 } | 357 } |
| 332 | 358 |
| 333 struct lookahead_entry *vp9_svc_lookahead_peek(VP9_COMP *const cpi, | 359 struct lookahead_entry *vp9_svc_lookahead_peek(VP9_COMP *const cpi, |
| 334 struct lookahead_ctx *ctx, | 360 struct lookahead_ctx *ctx, |
| 335 int index, int copy_params) { | 361 int index, int copy_params) { |
| 336 struct lookahead_entry *buf = vp9_lookahead_peek(ctx, index); | 362 struct lookahead_entry *buf = vp9_lookahead_peek(ctx, index); |
| 337 | 363 |
| 338 if (buf != NULL && copy_params != 0) { | 364 if (buf != NULL && copy_params != 0) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 355 buf->svc_params[cpi->svc.spatial_layer_id].spatial_layer = -1; | 381 buf->svc_params[cpi->svc.spatial_layer_id].spatial_layer = -1; |
| 356 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { | 382 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { |
| 357 vp9_lookahead_pop(ctx, drain); | 383 vp9_lookahead_pop(ctx, drain); |
| 358 } | 384 } |
| 359 } | 385 } |
| 360 } | 386 } |
| 361 | 387 |
| 362 return buf; | 388 return buf; |
| 363 } | 389 } |
| 364 #endif | 390 #endif |
| OLD | NEW |