vp9_svc_layercontext.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <math.h>
  11. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  12. #include "vp9/encoder/vp9_encoder.h"
  13. #include "vp9/encoder/vp9_svc_layercontext.h"
  14. #include "vp9/encoder/vp9_extend.h"
  15. #include "vpx_dsp/vpx_dsp_common.h"
  16. #define SMALL_FRAME_WIDTH 32
  17. #define SMALL_FRAME_HEIGHT 16
  18. static void swap_ptr(void *a, void *b) {
  19. void **a_p = (void **)a;
  20. void **b_p = (void **)b;
  21. void *c = *a_p;
  22. *a_p = *b_p;
  23. *b_p = c;
  24. }
  25. void vp9_init_layer_context(VP9_COMP *const cpi) {
  26. SVC *const svc = &cpi->svc;
  27. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  28. int mi_rows = cpi->common.mi_rows;
  29. int mi_cols = cpi->common.mi_cols;
  30. int sl, tl, i;
  31. int alt_ref_idx = svc->number_spatial_layers;
  32. svc->spatial_layer_id = 0;
  33. svc->temporal_layer_id = 0;
  34. svc->force_zero_mode_spatial_ref = 0;
  35. svc->use_base_mv = 0;
  36. svc->use_partition_reuse = 0;
  37. svc->use_gf_temporal_ref = 1;
  38. svc->use_gf_temporal_ref_current_layer = 0;
  39. svc->scaled_temp_is_alloc = 0;
  40. svc->scaled_one_half = 0;
  41. svc->current_superframe = 0;
  42. svc->non_reference_frame = 0;
  43. svc->skip_enhancement_layer = 0;
  44. svc->disable_inter_layer_pred = INTER_LAYER_PRED_ON;
  45. svc->framedrop_mode = CONSTRAINED_LAYER_DROP;
  46. svc->set_intra_only_frame = 0;
  47. svc->previous_frame_is_intra_only = 0;
  48. svc->superframe_has_layer_sync = 0;
  49. svc->use_set_ref_frame_config = 0;
  50. svc->num_encoded_top_layer = 0;
  51. svc->simulcast_mode = 0;
  52. for (i = 0; i < REF_FRAMES; ++i) {
  53. svc->fb_idx_spatial_layer_id[i] = -1;
  54. svc->fb_idx_temporal_layer_id[i] = -1;
  55. svc->fb_idx_base[i] = 0;
  56. }
  57. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  58. svc->last_layer_dropped[sl] = 0;
  59. svc->drop_spatial_layer[sl] = 0;
  60. svc->ext_frame_flags[sl] = 0;
  61. svc->lst_fb_idx[sl] = 0;
  62. svc->gld_fb_idx[sl] = 1;
  63. svc->alt_fb_idx[sl] = 2;
  64. svc->downsample_filter_type[sl] = BILINEAR;
  65. svc->downsample_filter_phase[sl] = 8; // Set to 8 for averaging filter.
  66. svc->framedrop_thresh[sl] = oxcf->drop_frames_water_mark;
  67. svc->fb_idx_upd_tl0[sl] = -1;
  68. svc->drop_count[sl] = 0;
  69. svc->spatial_layer_sync[sl] = 0;
  70. }
  71. svc->max_consec_drop = INT_MAX;
  72. svc->buffer_gf_temporal_ref[1].idx = 7;
  73. svc->buffer_gf_temporal_ref[0].idx = 6;
  74. svc->buffer_gf_temporal_ref[1].is_used = 0;
  75. svc->buffer_gf_temporal_ref[0].is_used = 0;
  76. if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
  77. if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img, SMALL_FRAME_WIDTH,
  78. SMALL_FRAME_HEIGHT, cpi->common.subsampling_x,
  79. cpi->common.subsampling_y,
  80. #if CONFIG_VP9_HIGHBITDEPTH
  81. cpi->common.use_highbitdepth,
  82. #endif
  83. VP9_ENC_BORDER_IN_PIXELS,
  84. cpi->common.byte_alignment, NULL, NULL, NULL))
  85. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  86. "Failed to allocate empty frame for multiple frame "
  87. "contexts");
  88. memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
  89. cpi->svc.empty_frame.img.buffer_alloc_sz);
  90. }
  91. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  92. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  93. int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
  94. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  95. RATE_CONTROL *const lrc = &lc->rc;
  96. int i;
  97. lc->current_video_frame_in_layer = 0;
  98. lc->layer_size = 0;
  99. lc->frames_from_key_frame = 0;
  100. lc->last_frame_type = FRAME_TYPES;
  101. lrc->ni_av_qi = oxcf->worst_allowed_q;
  102. lrc->total_actual_bits = 0;
  103. lrc->total_target_vs_actual = 0;
  104. lrc->ni_tot_qi = 0;
  105. lrc->tot_q = 0.0;
  106. lrc->avg_q = 0.0;
  107. lrc->ni_frames = 0;
  108. lrc->decimation_count = 0;
  109. lrc->decimation_factor = 0;
  110. lrc->worst_quality = oxcf->worst_allowed_q;
  111. lrc->best_quality = oxcf->best_allowed_q;
  112. for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
  113. lrc->rate_correction_factors[i] = 1.0;
  114. }
  115. if (cpi->oxcf.rc_mode == VPX_CBR) {
  116. lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
  117. lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
  118. lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
  119. lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
  120. } else {
  121. lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
  122. lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
  123. lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
  124. lrc->avg_frame_qindex[KEY_FRAME] =
  125. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  126. lrc->avg_frame_qindex[INTER_FRAME] =
  127. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  128. if (oxcf->ss_enable_auto_arf[sl])
  129. lc->alt_ref_idx = alt_ref_idx++;
  130. else
  131. lc->alt_ref_idx = INVALID_IDX;
  132. lc->gold_ref_idx = INVALID_IDX;
  133. }
  134. lrc->buffer_level =
  135. oxcf->starting_buffer_level_ms * lc->target_bandwidth / 1000;
  136. lrc->bits_off_target = lrc->buffer_level;
  137. // Initialize the cyclic refresh parameters. If spatial layers are used
  138. // (i.e., ss_number_layers > 1), these need to be updated per spatial
  139. // layer.
  140. // Cyclic refresh is only applied on base temporal layer.
  141. if (oxcf->ss_number_layers > 1 && tl == 0) {
  142. size_t last_coded_q_map_size;
  143. size_t consec_zero_mv_size;
  144. VP9_COMMON *const cm = &cpi->common;
  145. lc->sb_index = 0;
  146. lc->actual_num_seg1_blocks = 0;
  147. lc->actual_num_seg2_blocks = 0;
  148. lc->counter_encode_maxq_scene_change = 0;
  149. CHECK_MEM_ERROR(cm, lc->map,
  150. vpx_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
  151. memset(lc->map, 0, mi_rows * mi_cols);
  152. last_coded_q_map_size =
  153. mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
  154. CHECK_MEM_ERROR(cm, lc->last_coded_q_map,
  155. vpx_malloc(last_coded_q_map_size));
  156. assert(MAXQ <= 255);
  157. memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
  158. consec_zero_mv_size = mi_rows * mi_cols * sizeof(*lc->consec_zero_mv);
  159. CHECK_MEM_ERROR(cm, lc->consec_zero_mv,
  160. vpx_malloc(consec_zero_mv_size));
  161. memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
  162. }
  163. }
  164. }
  165. // Still have extra buffer for base layer golden frame
  166. if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) &&
  167. alt_ref_idx < REF_FRAMES)
  168. svc->layer_context[0].gold_ref_idx = alt_ref_idx;
  169. }
  170. // Update the layer context from a change_config() call.
  171. void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
  172. const int target_bandwidth) {
  173. SVC *const svc = &cpi->svc;
  174. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  175. const RATE_CONTROL *const rc = &cpi->rc;
  176. int sl, tl, layer = 0, spatial_layer_target;
  177. float bitrate_alloc = 1.0;
  178. cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
  179. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
  180. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  181. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  182. layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
  183. svc->layer_context[layer].target_bandwidth =
  184. oxcf->layer_target_bitrate[layer];
  185. }
  186. layer = LAYER_IDS_TO_IDX(
  187. sl,
  188. ((oxcf->ts_number_layers - 1) < 0 ? 0 : (oxcf->ts_number_layers - 1)),
  189. oxcf->ts_number_layers);
  190. spatial_layer_target = svc->layer_context[layer].target_bandwidth =
  191. oxcf->layer_target_bitrate[layer];
  192. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  193. LAYER_CONTEXT *const lc =
  194. &svc->layer_context[sl * oxcf->ts_number_layers + tl];
  195. RATE_CONTROL *const lrc = &lc->rc;
  196. lc->spatial_layer_target_bandwidth = spatial_layer_target;
  197. bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
  198. lrc->starting_buffer_level =
  199. (int64_t)(rc->starting_buffer_level * bitrate_alloc);
  200. lrc->optimal_buffer_level =
  201. (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
  202. lrc->maximum_buffer_size =
  203. (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
  204. lrc->bits_off_target =
  205. VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  206. lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
  207. lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
  208. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  209. lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
  210. lrc->worst_quality = rc->worst_quality;
  211. lrc->best_quality = rc->best_quality;
  212. }
  213. }
  214. } else {
  215. int layer_end;
  216. if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
  217. layer_end = svc->number_temporal_layers;
  218. } else {
  219. layer_end = svc->number_spatial_layers;
  220. }
  221. for (layer = 0; layer < layer_end; ++layer) {
  222. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  223. RATE_CONTROL *const lrc = &lc->rc;
  224. lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
  225. bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
  226. // Update buffer-related quantities.
  227. lrc->starting_buffer_level =
  228. (int64_t)(rc->starting_buffer_level * bitrate_alloc);
  229. lrc->optimal_buffer_level =
  230. (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
  231. lrc->maximum_buffer_size =
  232. (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
  233. lrc->bits_off_target =
  234. VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  235. lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
  236. // Update framerate-related quantities.
  237. if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
  238. lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
  239. } else {
  240. lc->framerate = cpi->framerate;
  241. }
  242. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  243. lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
  244. // Update qp-related quantities.
  245. lrc->worst_quality = rc->worst_quality;
  246. lrc->best_quality = rc->best_quality;
  247. }
  248. }
  249. }
  250. static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
  251. if (is_one_pass_cbr_svc(cpi))
  252. return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
  253. cpi->svc.number_temporal_layers +
  254. cpi->svc.temporal_layer_id];
  255. else
  256. return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
  257. ? &cpi->svc.layer_context[cpi->svc.temporal_layer_id]
  258. : &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
  259. }
  260. void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
  261. SVC *const svc = &cpi->svc;
  262. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  263. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  264. RATE_CONTROL *const lrc = &lc->rc;
  265. // Index into spatial+temporal arrays.
  266. const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
  267. svc->temporal_layer_id;
  268. const int tl = svc->temporal_layer_id;
  269. lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
  270. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  271. lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
  272. // Update the average layer frame size (non-cumulative per-frame-bw).
  273. if (tl == 0) {
  274. lc->avg_frame_size = lrc->avg_frame_bandwidth;
  275. } else {
  276. const double prev_layer_framerate =
  277. cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
  278. const int prev_layer_target_bandwidth =
  279. oxcf->layer_target_bitrate[st_idx - 1];
  280. lc->avg_frame_size =
  281. (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
  282. (lc->framerate - prev_layer_framerate));
  283. }
  284. }
  285. void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
  286. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  287. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  288. RATE_CONTROL *const lrc = &lc->rc;
  289. lc->framerate = framerate;
  290. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  291. lrc->min_frame_bandwidth =
  292. (int)(lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
  293. lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
  294. oxcf->two_pass_vbrmax_section) /
  295. 100);
  296. vp9_rc_set_gf_interval_range(cpi, lrc);
  297. }
  298. void vp9_restore_layer_context(VP9_COMP *const cpi) {
  299. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  300. const int old_frame_since_key = cpi->rc.frames_since_key;
  301. const int old_frame_to_key = cpi->rc.frames_to_key;
  302. const int old_ext_use_post_encode_drop = cpi->rc.ext_use_post_encode_drop;
  303. cpi->rc = lc->rc;
  304. cpi->twopass = lc->twopass;
  305. cpi->oxcf.target_bandwidth = lc->target_bandwidth;
  306. cpi->alt_ref_source = lc->alt_ref_source;
  307. // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
  308. // does not use speed = 0).
  309. if (is_one_pass_cbr_svc(cpi) && lc->speed > 0) {
  310. cpi->oxcf.speed = lc->speed;
  311. }
  312. // Reset the frames_since_key and frames_to_key counters to their values
  313. // before the layer restore. Keep these defined for the stream (not layer).
  314. if (cpi->svc.number_temporal_layers > 1 ||
  315. cpi->svc.number_spatial_layers > 1) {
  316. cpi->rc.frames_since_key = old_frame_since_key;
  317. cpi->rc.frames_to_key = old_frame_to_key;
  318. }
  319. cpi->rc.ext_use_post_encode_drop = old_ext_use_post_encode_drop;
  320. // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
  321. // for the base temporal layer.
  322. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
  323. cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
  324. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  325. swap_ptr(&cr->map, &lc->map);
  326. swap_ptr(&cr->last_coded_q_map, &lc->last_coded_q_map);
  327. swap_ptr(&cpi->consec_zero_mv, &lc->consec_zero_mv);
  328. cr->sb_index = lc->sb_index;
  329. cr->actual_num_seg1_blocks = lc->actual_num_seg1_blocks;
  330. cr->actual_num_seg2_blocks = lc->actual_num_seg2_blocks;
  331. cr->counter_encode_maxq_scene_change = lc->counter_encode_maxq_scene_change;
  332. }
  333. }
  334. void vp9_save_layer_context(VP9_COMP *const cpi) {
  335. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  336. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  337. lc->rc = cpi->rc;
  338. lc->twopass = cpi->twopass;
  339. lc->target_bandwidth = (int)oxcf->target_bandwidth;
  340. lc->alt_ref_source = cpi->alt_ref_source;
  341. // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
  342. // for the base temporal layer.
  343. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
  344. cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
  345. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  346. signed char *temp = lc->map;
  347. uint8_t *temp2 = lc->last_coded_q_map;
  348. uint8_t *temp3 = lc->consec_zero_mv;
  349. lc->map = cr->map;
  350. cr->map = temp;
  351. lc->last_coded_q_map = cr->last_coded_q_map;
  352. cr->last_coded_q_map = temp2;
  353. lc->consec_zero_mv = cpi->consec_zero_mv;
  354. cpi->consec_zero_mv = temp3;
  355. lc->sb_index = cr->sb_index;
  356. lc->actual_num_seg1_blocks = cr->actual_num_seg1_blocks;
  357. lc->actual_num_seg2_blocks = cr->actual_num_seg2_blocks;
  358. lc->counter_encode_maxq_scene_change = cr->counter_encode_maxq_scene_change;
  359. }
  360. }
  361. #if !CONFIG_REALTIME_ONLY
  362. void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
  363. SVC *const svc = &cpi->svc;
  364. int i;
  365. for (i = 0; i < svc->number_spatial_layers; ++i) {
  366. TWO_PASS *const twopass = &svc->layer_context[i].twopass;
  367. svc->spatial_layer_id = i;
  368. vp9_init_second_pass(cpi);
  369. twopass->total_stats.spatial_layer_id = i;
  370. twopass->total_left_stats.spatial_layer_id = i;
  371. }
  372. svc->spatial_layer_id = 0;
  373. }
  374. #endif // !CONFIG_REALTIME_ONLY
  375. void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
  376. LAYER_CONTEXT *const lc =
  377. &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
  378. cpi->svc.number_temporal_layers];
  379. ++lc->current_video_frame_in_layer;
  380. ++lc->frames_from_key_frame;
  381. if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
  382. ++cpi->svc.current_superframe;
  383. }
  384. void get_layer_resolution(const int width_org, const int height_org,
  385. const int num, const int den, int *width_out,
  386. int *height_out) {
  387. int w, h;
  388. if (width_out == NULL || height_out == NULL || den == 0) return;
  389. w = width_org * num / den;
  390. h = height_org * num / den;
  391. // make height and width even to make chrome player happy
  392. w += w % 2;
  393. h += h % 2;
  394. *width_out = w;
  395. *height_out = h;
  396. }
  397. static void reset_fb_idx_unused(VP9_COMP *const cpi) {
  398. // If a reference frame is not referenced or refreshed, then set the
  399. // fb_idx for that reference to the first one used/referenced.
  400. // This is to avoid setting fb_idx for a reference to a slot that is not
  401. // used/needed (i.e., since that reference is not referenced or refreshed).
  402. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  403. VP9_ALT_FLAG };
  404. MV_REFERENCE_FRAME ref_frame;
  405. MV_REFERENCE_FRAME first_ref = 0;
  406. int first_fb_idx = 0;
  407. int fb_idx[3] = { cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx };
  408. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
  409. if (cpi->ref_frame_flags & flag_list[ref_frame]) {
  410. first_ref = ref_frame;
  411. first_fb_idx = fb_idx[ref_frame - 1];
  412. break;
  413. }
  414. }
  415. if (first_ref > 0) {
  416. if (first_ref != LAST_FRAME &&
  417. !(cpi->ref_frame_flags & flag_list[LAST_FRAME]) &&
  418. !cpi->ext_refresh_last_frame)
  419. cpi->lst_fb_idx = first_fb_idx;
  420. else if (first_ref != GOLDEN_FRAME &&
  421. !(cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]) &&
  422. !cpi->ext_refresh_golden_frame)
  423. cpi->gld_fb_idx = first_fb_idx;
  424. else if (first_ref != ALTREF_FRAME &&
  425. !(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]) &&
  426. !cpi->ext_refresh_alt_ref_frame)
  427. cpi->alt_fb_idx = first_fb_idx;
  428. }
  429. }
  430. // Never refresh any reference frame buffers on top temporal layers in
  431. // simulcast mode, which has interlayer prediction disabled.
  432. static void non_reference_frame_simulcast(VP9_COMP *const cpi) {
  433. if (cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1 &&
  434. cpi->svc.temporal_layer_id > 0) {
  435. cpi->ext_refresh_last_frame = 0;
  436. cpi->ext_refresh_golden_frame = 0;
  437. cpi->ext_refresh_alt_ref_frame = 0;
  438. }
  439. }
  440. // The function sets proper ref_frame_flags, buffer indices, and buffer update
  441. // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
  442. // scheme.
  443. static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
  444. int frame_num_within_temporal_struct = 0;
  445. int spatial_id, temporal_id;
  446. spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  447. frame_num_within_temporal_struct =
  448. cpi->svc
  449. .layer_context[cpi->svc.spatial_layer_id *
  450. cpi->svc.number_temporal_layers]
  451. .current_video_frame_in_layer %
  452. 4;
  453. temporal_id = cpi->svc.temporal_layer_id =
  454. (frame_num_within_temporal_struct & 1)
  455. ? 2
  456. : (frame_num_within_temporal_struct >> 1);
  457. cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
  458. cpi->ext_refresh_alt_ref_frame = 0;
  459. if (!temporal_id) {
  460. cpi->ext_refresh_frame_flags_pending = 1;
  461. cpi->ext_refresh_last_frame = 1;
  462. if (!spatial_id) {
  463. cpi->ref_frame_flags = VP9_LAST_FLAG;
  464. } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  465. // base layer is a key frame.
  466. cpi->ref_frame_flags = VP9_LAST_FLAG;
  467. cpi->ext_refresh_last_frame = 0;
  468. cpi->ext_refresh_golden_frame = 1;
  469. } else {
  470. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  471. }
  472. } else if (temporal_id == 1) {
  473. cpi->ext_refresh_frame_flags_pending = 1;
  474. cpi->ext_refresh_alt_ref_frame = 1;
  475. if (!spatial_id) {
  476. cpi->ref_frame_flags = VP9_LAST_FLAG;
  477. } else {
  478. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  479. }
  480. } else {
  481. if (frame_num_within_temporal_struct == 1) {
  482. // the first tl2 picture
  483. if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
  484. cpi->ext_refresh_frame_flags_pending = 1;
  485. if (!spatial_id)
  486. cpi->ref_frame_flags = VP9_LAST_FLAG;
  487. else
  488. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  489. } else if (!spatial_id) {
  490. cpi->ext_refresh_frame_flags_pending = 1;
  491. cpi->ext_refresh_alt_ref_frame = 1;
  492. cpi->ref_frame_flags = VP9_LAST_FLAG;
  493. } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
  494. cpi->ext_refresh_frame_flags_pending = 1;
  495. cpi->ext_refresh_alt_ref_frame = 1;
  496. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  497. }
  498. } else {
  499. // The second tl2 picture
  500. if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
  501. cpi->ext_refresh_frame_flags_pending = 1;
  502. if (!spatial_id)
  503. cpi->ref_frame_flags = VP9_LAST_FLAG;
  504. else
  505. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  506. } else if (!spatial_id) {
  507. cpi->ext_refresh_frame_flags_pending = 1;
  508. cpi->ref_frame_flags = VP9_LAST_FLAG;
  509. cpi->ext_refresh_alt_ref_frame = 1;
  510. } else { // top layer
  511. cpi->ext_refresh_frame_flags_pending = 1;
  512. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  513. cpi->ext_refresh_alt_ref_frame = 1;
  514. }
  515. }
  516. }
  517. if (temporal_id == 0) {
  518. cpi->lst_fb_idx = spatial_id;
  519. if (spatial_id) {
  520. if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  521. cpi->lst_fb_idx = spatial_id - 1;
  522. cpi->gld_fb_idx = spatial_id;
  523. } else {
  524. cpi->gld_fb_idx = spatial_id - 1;
  525. }
  526. } else {
  527. cpi->gld_fb_idx = 0;
  528. }
  529. cpi->alt_fb_idx = 0;
  530. } else if (temporal_id == 1) {
  531. cpi->lst_fb_idx = spatial_id;
  532. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  533. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  534. } else if (frame_num_within_temporal_struct == 1) {
  535. cpi->lst_fb_idx = spatial_id;
  536. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  537. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  538. } else {
  539. cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  540. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  541. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  542. }
  543. if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
  544. reset_fb_idx_unused(cpi);
  545. }
  546. // The function sets proper ref_frame_flags, buffer indices, and buffer update
  547. // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
  548. // scheme.
  549. static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
  550. int spatial_id, temporal_id;
  551. spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  552. temporal_id = cpi->svc.temporal_layer_id =
  553. cpi->svc
  554. .layer_context[cpi->svc.spatial_layer_id *
  555. cpi->svc.number_temporal_layers]
  556. .current_video_frame_in_layer &
  557. 1;
  558. cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
  559. cpi->ext_refresh_alt_ref_frame = 0;
  560. if (!temporal_id) {
  561. cpi->ext_refresh_frame_flags_pending = 1;
  562. cpi->ext_refresh_last_frame = 1;
  563. if (!spatial_id) {
  564. cpi->ref_frame_flags = VP9_LAST_FLAG;
  565. } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  566. // base layer is a key frame.
  567. cpi->ref_frame_flags = VP9_LAST_FLAG;
  568. cpi->ext_refresh_last_frame = 0;
  569. cpi->ext_refresh_golden_frame = 1;
  570. } else {
  571. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  572. }
  573. } else if (temporal_id == 1) {
  574. cpi->ext_refresh_frame_flags_pending = 1;
  575. cpi->ext_refresh_alt_ref_frame = 1;
  576. if (!spatial_id) {
  577. cpi->ref_frame_flags = VP9_LAST_FLAG;
  578. } else {
  579. if (spatial_id == cpi->svc.number_spatial_layers - 1)
  580. cpi->ext_refresh_alt_ref_frame = 0;
  581. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  582. }
  583. }
  584. if (temporal_id == 0) {
  585. cpi->lst_fb_idx = spatial_id;
  586. if (spatial_id) {
  587. if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  588. cpi->lst_fb_idx = spatial_id - 1;
  589. cpi->gld_fb_idx = spatial_id;
  590. } else {
  591. cpi->gld_fb_idx = spatial_id - 1;
  592. }
  593. } else {
  594. cpi->gld_fb_idx = 0;
  595. }
  596. cpi->alt_fb_idx = 0;
  597. } else if (temporal_id == 1) {
  598. cpi->lst_fb_idx = spatial_id;
  599. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  600. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  601. }
  602. if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
  603. reset_fb_idx_unused(cpi);
  604. }
  605. // The function sets proper ref_frame_flags, buffer indices, and buffer update
  606. // variables for temporal layering mode 0 - that has no temporal layering.
  607. static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
  608. VP9_COMP *const cpi) {
  609. int spatial_id;
  610. spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  611. cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
  612. cpi->ext_refresh_alt_ref_frame = 0;
  613. cpi->ext_refresh_frame_flags_pending = 1;
  614. cpi->ext_refresh_last_frame = 1;
  615. if (!spatial_id) {
  616. cpi->ref_frame_flags = VP9_LAST_FLAG;
  617. } else if (cpi->svc.layer_context[0].is_key_frame) {
  618. cpi->ref_frame_flags = VP9_LAST_FLAG;
  619. cpi->ext_refresh_last_frame = 0;
  620. cpi->ext_refresh_golden_frame = 1;
  621. } else {
  622. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  623. }
  624. cpi->lst_fb_idx = spatial_id;
  625. if (spatial_id) {
  626. if (cpi->svc.layer_context[0].is_key_frame) {
  627. cpi->lst_fb_idx = spatial_id - 1;
  628. cpi->gld_fb_idx = spatial_id;
  629. } else {
  630. cpi->gld_fb_idx = spatial_id - 1;
  631. }
  632. } else {
  633. cpi->gld_fb_idx = 0;
  634. }
  635. if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
  636. reset_fb_idx_unused(cpi);
  637. }
  638. static void set_flags_and_fb_idx_bypass_via_set_ref_frame_config(
  639. VP9_COMP *const cpi) {
  640. SVC *const svc = &cpi->svc;
  641. int sl = svc->spatial_layer_id = svc->spatial_layer_to_encode;
  642. cpi->svc.temporal_layer_id = cpi->svc.temporal_layer_id_per_spatial[sl];
  643. cpi->ext_refresh_frame_flags_pending = 1;
  644. cpi->lst_fb_idx = svc->lst_fb_idx[sl];
  645. cpi->gld_fb_idx = svc->gld_fb_idx[sl];
  646. cpi->alt_fb_idx = svc->alt_fb_idx[sl];
  647. cpi->ext_refresh_last_frame = 0;
  648. cpi->ext_refresh_golden_frame = 0;
  649. cpi->ext_refresh_alt_ref_frame = 0;
  650. cpi->ref_frame_flags = 0;
  651. if (svc->reference_last[sl]) cpi->ref_frame_flags |= VP9_LAST_FLAG;
  652. if (svc->reference_golden[sl]) cpi->ref_frame_flags |= VP9_GOLD_FLAG;
  653. if (svc->reference_altref[sl]) cpi->ref_frame_flags |= VP9_ALT_FLAG;
  654. }
  655. void vp9_copy_flags_ref_update_idx(VP9_COMP *const cpi) {
  656. SVC *const svc = &cpi->svc;
  657. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  658. VP9_ALT_FLAG };
  659. int sl = svc->spatial_layer_id;
  660. svc->lst_fb_idx[sl] = cpi->lst_fb_idx;
  661. svc->gld_fb_idx[sl] = cpi->gld_fb_idx;
  662. svc->alt_fb_idx[sl] = cpi->alt_fb_idx;
  663. // For the fixed SVC mode: pass the refresh_lst/gld/alt_frame flags to the
  664. // update_buffer_slot, this is needed for the GET_SVC_REF_FRAME_CONFIG api.
  665. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
  666. int ref;
  667. for (ref = 0; ref < REF_FRAMES; ++ref) {
  668. svc->update_buffer_slot[sl] &= ~(1 << ref);
  669. if ((ref == svc->lst_fb_idx[sl] && cpi->refresh_last_frame) ||
  670. (ref == svc->gld_fb_idx[sl] && cpi->refresh_golden_frame) ||
  671. (ref == svc->alt_fb_idx[sl] && cpi->refresh_alt_ref_frame))
  672. svc->update_buffer_slot[sl] |= (1 << ref);
  673. }
  674. }
  675. // TODO(jianj): Remove these 3, deprecated.
  676. svc->update_last[sl] = (uint8_t)cpi->refresh_last_frame;
  677. svc->update_golden[sl] = (uint8_t)cpi->refresh_golden_frame;
  678. svc->update_altref[sl] = (uint8_t)cpi->refresh_alt_ref_frame;
  679. svc->reference_last[sl] =
  680. (uint8_t)(cpi->ref_frame_flags & flag_list[LAST_FRAME]);
  681. svc->reference_golden[sl] =
  682. (uint8_t)(cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]);
  683. svc->reference_altref[sl] =
  684. (uint8_t)(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]);
  685. }
  686. int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) {
  687. int width = 0, height = 0;
  688. SVC *const svc = &cpi->svc;
  689. LAYER_CONTEXT *lc = NULL;
  690. svc->skip_enhancement_layer = 0;
  691. if (svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF &&
  692. svc->number_spatial_layers > 1 && svc->number_spatial_layers <= 3 &&
  693. svc->number_temporal_layers <= 3 &&
  694. !(svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  695. svc->use_set_ref_frame_config))
  696. svc->simulcast_mode = 1;
  697. else
  698. svc->simulcast_mode = 0;
  699. if (svc->number_spatial_layers > 1) {
  700. svc->use_base_mv = 1;
  701. svc->use_partition_reuse = 1;
  702. }
  703. svc->force_zero_mode_spatial_ref = 1;
  704. svc->mi_stride[svc->spatial_layer_id] = cpi->common.mi_stride;
  705. svc->mi_rows[svc->spatial_layer_id] = cpi->common.mi_rows;
  706. svc->mi_cols[svc->spatial_layer_id] = cpi->common.mi_cols;
  707. if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
  708. set_flags_and_fb_idx_for_temporal_mode3(cpi);
  709. } else if (svc->temporal_layering_mode ==
  710. VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
  711. set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
  712. } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
  713. set_flags_and_fb_idx_for_temporal_mode2(cpi);
  714. } else if (svc->temporal_layering_mode ==
  715. VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  716. svc->use_set_ref_frame_config) {
  717. set_flags_and_fb_idx_bypass_via_set_ref_frame_config(cpi);
  718. }
  719. if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
  720. cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
  721. cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[0].idx)
  722. svc->buffer_gf_temporal_ref[0].is_used = 1;
  723. if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
  724. cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
  725. cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[1].idx)
  726. svc->buffer_gf_temporal_ref[1].is_used = 1;
  727. // For the fixed (non-flexible/bypass) SVC mode:
  728. // If long term temporal reference is enabled at the sequence level
  729. // (use_gf_temporal_ref == 1), and inter_layer is disabled (on inter-frames),
  730. // we can use golden as a second temporal reference
  731. // (since the spatial/inter-layer reference is disabled).
  732. // We check that the fb_idx for this reference (buffer_gf_temporal_ref.idx) is
  733. // unused (slot 7 and 6 should be available for 3-3 layer system).
  734. // For now usage of this second temporal reference will only be used for
  735. // highest and next to highest spatial layer (i.e., top and middle layer for
  736. // 3 spatial layers).
  737. svc->use_gf_temporal_ref_current_layer = 0;
  738. if (svc->use_gf_temporal_ref && !svc->buffer_gf_temporal_ref[0].is_used &&
  739. !svc->buffer_gf_temporal_ref[1].is_used &&
  740. svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  741. svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON &&
  742. svc->number_spatial_layers <= 3 && svc->number_temporal_layers <= 3 &&
  743. svc->spatial_layer_id >= svc->number_spatial_layers - 2) {
  744. // Enable the second (long-term) temporal reference at the frame-level.
  745. svc->use_gf_temporal_ref_current_layer = 1;
  746. }
  747. // Check if current superframe has any layer sync, only check once on
  748. // base layer.
  749. if (svc->spatial_layer_id == 0) {
  750. int sl = 0;
  751. // Default is no sync.
  752. svc->superframe_has_layer_sync = 0;
  753. for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
  754. if (cpi->svc.spatial_layer_sync[sl]) svc->superframe_has_layer_sync = 1;
  755. }
  756. }
  757. // Reset the drop flags for all spatial layers, on the base layer.
  758. if (svc->spatial_layer_id == 0) {
  759. vp9_zero(svc->drop_spatial_layer);
  760. // TODO(jianj/marpan): Investigate why setting svc->lst/gld/alt_fb_idx
  761. // causes an issue with frame dropping and temporal layers, when the frame
  762. // flags are passed via the encode call (bypass mode). Issue is that we're
  763. // resetting ext_refresh_frame_flags_pending to 0 on frame drops.
  764. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
  765. memset(&svc->lst_fb_idx, -1, sizeof(svc->lst_fb_idx));
  766. memset(&svc->gld_fb_idx, -1, sizeof(svc->lst_fb_idx));
  767. memset(&svc->alt_fb_idx, -1, sizeof(svc->lst_fb_idx));
  768. // These are set by API before the superframe is encoded and they are
  769. // passed to encoder layer by layer. Don't reset them on layer 0 in bypass
  770. // mode.
  771. vp9_zero(svc->update_buffer_slot);
  772. vp9_zero(svc->reference_last);
  773. vp9_zero(svc->reference_golden);
  774. vp9_zero(svc->reference_altref);
  775. // TODO(jianj): Remove these 3, deprecated.
  776. vp9_zero(svc->update_last);
  777. vp9_zero(svc->update_golden);
  778. vp9_zero(svc->update_altref);
  779. }
  780. }
  781. lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
  782. svc->temporal_layer_id];
  783. // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
  784. // only for non-BYPASS mode for now.
  785. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
  786. svc->use_set_ref_frame_config) {
  787. RATE_CONTROL *const lrc = &lc->rc;
  788. lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
  789. lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
  790. }
  791. get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
  792. lc->scaling_factor_num, lc->scaling_factor_den, &width,
  793. &height);
  794. // Use Eightap_smooth for low resolutions.
  795. if (width * height <= 320 * 240)
  796. svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
  797. // For scale factors > 0.75, set the phase to 0 (aligns decimated pixel
  798. // to source pixel).
  799. lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
  800. svc->temporal_layer_id];
  801. if (lc->scaling_factor_num > (3 * lc->scaling_factor_den) >> 2)
  802. svc->downsample_filter_phase[svc->spatial_layer_id] = 0;
  803. // The usage of use_base_mv or partition_reuse assumes down-scale of 2x2.
  804. // For now, turn off use of base motion vectors and partition reuse if the
  805. // spatial scale factors for any layers are not 2,
  806. // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
  807. // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
  808. if (svc->number_spatial_layers > 1) {
  809. int sl;
  810. for (sl = 0; sl < svc->number_spatial_layers - 1; ++sl) {
  811. lc = &svc->layer_context[sl * svc->number_temporal_layers +
  812. svc->temporal_layer_id];
  813. if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
  814. !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
  815. svc->number_spatial_layers == 3)) {
  816. svc->use_base_mv = 0;
  817. svc->use_partition_reuse = 0;
  818. break;
  819. }
  820. }
  821. // For non-zero spatial layers: if the previous spatial layer was dropped
  822. // disable the base_mv and partition_reuse features.
  823. if (svc->spatial_layer_id > 0 &&
  824. svc->drop_spatial_layer[svc->spatial_layer_id - 1]) {
  825. svc->use_base_mv = 0;
  826. svc->use_partition_reuse = 0;
  827. }
  828. }
  829. svc->non_reference_frame = 0;
  830. if (cpi->common.frame_type != KEY_FRAME && !cpi->ext_refresh_last_frame &&
  831. !cpi->ext_refresh_golden_frame && !cpi->ext_refresh_alt_ref_frame)
  832. svc->non_reference_frame = 1;
  833. // For non-flexible mode, where update_buffer_slot is used, need to check if
  834. // all buffer slots are not refreshed.
  835. if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
  836. if (svc->update_buffer_slot[svc->spatial_layer_id] != 0)
  837. svc->non_reference_frame = 0;
  838. }
  839. if (svc->spatial_layer_id == 0) {
  840. svc->high_source_sad_superframe = 0;
  841. svc->high_num_blocks_with_motion = 0;
  842. }
  843. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  844. svc->last_layer_dropped[svc->spatial_layer_id] &&
  845. svc->fb_idx_upd_tl0[svc->spatial_layer_id] != -1 &&
  846. !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
  847. // For fixed/non-flexible mode, if the previous frame (same spatial layer
  848. // from previous superframe) was dropped, make sure the lst_fb_idx
  849. // for this frame corresponds to the buffer index updated on (last) encoded
  850. // TL0 frame (with same spatial layer).
  851. cpi->lst_fb_idx = svc->fb_idx_upd_tl0[svc->spatial_layer_id];
  852. }
  853. if (vp9_set_size_literal(cpi, width, height) != 0)
  854. return VPX_CODEC_INVALID_PARAM;
  855. return 0;
  856. }
  857. struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
  858. struct lookahead_ctx *ctx,
  859. int drain) {
  860. struct lookahead_entry *buf = NULL;
  861. if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
  862. buf = vp9_lookahead_peek(ctx, 0);
  863. if (buf != NULL) {
  864. // Only remove the buffer when pop the highest layer.
  865. if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
  866. vp9_lookahead_pop(ctx, drain);
  867. }
  868. }
  869. }
  870. return buf;
  871. }
  872. void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
  873. int sl, tl;
  874. SVC *const svc = &cpi->svc;
  875. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  876. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  877. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  878. int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
  879. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  880. if (lc->map) vpx_free(lc->map);
  881. if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
  882. if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
  883. }
  884. }
  885. }
  886. // Reset on key frame: reset counters, references and buffer updates.
  887. void vp9_svc_reset_temporal_layers(VP9_COMP *const cpi, int is_key) {
  888. int sl, tl;
  889. SVC *const svc = &cpi->svc;
  890. LAYER_CONTEXT *lc = NULL;
  891. for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
  892. for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
  893. lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
  894. lc->current_video_frame_in_layer = 0;
  895. if (is_key) lc->frames_from_key_frame = 0;
  896. }
  897. }
  898. if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
  899. set_flags_and_fb_idx_for_temporal_mode3(cpi);
  900. } else if (svc->temporal_layering_mode ==
  901. VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
  902. set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
  903. } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
  904. set_flags_and_fb_idx_for_temporal_mode2(cpi);
  905. }
  906. vp9_update_temporal_layer_framerate(cpi);
  907. vp9_restore_layer_context(cpi);
  908. }
  909. void vp9_svc_check_reset_layer_rc_flag(VP9_COMP *const cpi) {
  910. SVC *svc = &cpi->svc;
  911. int sl, tl;
  912. for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
  913. // Check for reset based on avg_frame_bandwidth for spatial layer sl.
  914. int layer = LAYER_IDS_TO_IDX(sl, svc->number_temporal_layers - 1,
  915. svc->number_temporal_layers);
  916. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  917. RATE_CONTROL *lrc = &lc->rc;
  918. if (lrc->avg_frame_bandwidth > (3 * lrc->last_avg_frame_bandwidth >> 1) ||
  919. lrc->avg_frame_bandwidth < (lrc->last_avg_frame_bandwidth >> 1)) {
  920. // Reset for all temporal layers with spatial layer sl.
  921. for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
  922. int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
  923. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  924. RATE_CONTROL *lrc = &lc->rc;
  925. lrc->rc_1_frame = 0;
  926. lrc->rc_2_frame = 0;
  927. lrc->bits_off_target = lrc->optimal_buffer_level;
  928. lrc->buffer_level = lrc->optimal_buffer_level;
  929. }
  930. }
  931. }
  932. }
  933. void vp9_svc_constrain_inter_layer_pred(VP9_COMP *const cpi) {
  934. VP9_COMMON *const cm = &cpi->common;
  935. SVC *const svc = &cpi->svc;
  936. const int sl = svc->spatial_layer_id;
  937. // Check for disabling inter-layer (spatial) prediction, if
  938. // svc.disable_inter_layer_pred is set. If the previous spatial layer was
  939. // dropped then disable the prediction from this (scaled) reference.
  940. // For INTER_LAYER_PRED_OFF_NONKEY: inter-layer prediction is disabled
  941. // on key frames or if any spatial layer is a sync layer.
  942. if ((svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF_NONKEY &&
  943. !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
  944. !svc->superframe_has_layer_sync) ||
  945. svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF ||
  946. svc->drop_spatial_layer[sl - 1]) {
  947. MV_REFERENCE_FRAME ref_frame;
  948. static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
  949. VP9_ALT_FLAG };
  950. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  951. const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
  952. if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
  953. const struct scale_factors *const scale_fac =
  954. &cm->frame_refs[ref_frame - 1].sf;
  955. if (vp9_is_scaled(scale_fac)) {
  956. cpi->ref_frame_flags &= (~flag_list[ref_frame]);
  957. // Point golden/altref frame buffer index to last.
  958. if (!svc->simulcast_mode) {
  959. if (ref_frame == GOLDEN_FRAME)
  960. cpi->gld_fb_idx = cpi->lst_fb_idx;
  961. else if (ref_frame == ALTREF_FRAME)
  962. cpi->alt_fb_idx = cpi->lst_fb_idx;
  963. }
  964. }
  965. }
  966. }
  967. }
  968. // For fixed/non-flexible SVC: check for disabling inter-layer prediction.
  969. // If the reference for inter-layer prediction (the reference that is scaled)
  970. // is not the previous spatial layer from the same superframe, then we disable
  971. // inter-layer prediction. Only need to check when inter_layer prediction is
  972. // not set to OFF mode.
  973. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  974. svc->disable_inter_layer_pred != INTER_LAYER_PRED_OFF) {
  975. // We only use LAST and GOLDEN for prediction in real-time mode, so we
  976. // check both here.
  977. MV_REFERENCE_FRAME ref_frame;
  978. for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ref_frame++) {
  979. struct scale_factors *scale_fac = &cm->frame_refs[ref_frame - 1].sf;
  980. if (vp9_is_scaled(scale_fac)) {
  981. // If this reference was updated on the previous spatial layer of the
  982. // current superframe, then we keep this reference (don't disable).
  983. // Otherwise we disable the inter-layer prediction.
  984. // This condition is verified by checking if the current frame buffer
  985. // index is equal to any of the slots for the previous spatial layer,
  986. // and if so, check if that slot was updated/refreshed. If that is the
  987. // case, then this reference is valid for inter-layer prediction under
  988. // the mode INTER_LAYER_PRED_ON_CONSTRAINED.
  989. int fb_idx =
  990. ref_frame == LAST_FRAME ? cpi->lst_fb_idx : cpi->gld_fb_idx;
  991. int ref_flag = ref_frame == LAST_FRAME ? VP9_LAST_FLAG : VP9_GOLD_FLAG;
  992. int disable = 1;
  993. if (fb_idx < 0) continue;
  994. if ((fb_idx == svc->lst_fb_idx[sl - 1] &&
  995. (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
  996. (fb_idx == svc->gld_fb_idx[sl - 1] &&
  997. (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
  998. (fb_idx == svc->alt_fb_idx[sl - 1] &&
  999. (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))))
  1000. disable = 0;
  1001. if (disable) cpi->ref_frame_flags &= (~ref_flag);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. void vp9_svc_assert_constraints_pattern(VP9_COMP *const cpi) {
  1007. SVC *const svc = &cpi->svc;
  1008. // For fixed/non-flexible mode, the following constraint are expected,
  1009. // when inter-layer prediciton is on (default).
  1010. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  1011. svc->disable_inter_layer_pred == INTER_LAYER_PRED_ON &&
  1012. svc->framedrop_mode != LAYER_DROP) {
  1013. if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
  1014. // On non-key frames: LAST is always temporal reference, GOLDEN is
  1015. // spatial reference.
  1016. if (svc->temporal_layer_id == 0)
  1017. // Base temporal only predicts from base temporal.
  1018. assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] == 0);
  1019. else
  1020. // Non-base temporal only predicts from lower temporal layer.
  1021. assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] <
  1022. svc->temporal_layer_id);
  1023. if (svc->spatial_layer_id > 0 && cpi->ref_frame_flags & VP9_GOLD_FLAG &&
  1024. svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
  1025. // Non-base spatial only predicts from lower spatial layer with same
  1026. // temporal_id.
  1027. assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
  1028. svc->spatial_layer_id - 1);
  1029. assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
  1030. svc->temporal_layer_id);
  1031. }
  1032. } else if (svc->spatial_layer_id > 0 &&
  1033. svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
  1034. // Only 1 reference for frame whose base is key; reference may be LAST
  1035. // or GOLDEN, so we check both.
  1036. if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
  1037. assert(svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] ==
  1038. svc->spatial_layer_id - 1);
  1039. assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] ==
  1040. svc->temporal_layer_id);
  1041. } else if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
  1042. assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
  1043. svc->spatial_layer_id - 1);
  1044. assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
  1045. svc->temporal_layer_id);
  1046. }
  1047. }
  1048. } else if (svc->use_gf_temporal_ref_current_layer &&
  1049. !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
  1050. // For the usage of golden as second long term reference: the
  1051. // temporal_layer_id of that reference must be base temporal layer 0, and
  1052. // spatial_layer_id of that reference must be same as current
  1053. // spatial_layer_id. If not, disable feature.
  1054. // TODO(marpan): Investigate when this can happen, and maybe put this check
  1055. // and reset in a different place.
  1056. if (svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] !=
  1057. svc->spatial_layer_id ||
  1058. svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] != 0)
  1059. svc->use_gf_temporal_ref_current_layer = 0;
  1060. }
  1061. }
  1062. #if CONFIG_VP9_TEMPORAL_DENOISING
  1063. int vp9_denoise_svc_non_key(VP9_COMP *const cpi) {
  1064. int layer =
  1065. LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
  1066. cpi->svc.number_temporal_layers);
  1067. LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  1068. return denoise_svc(cpi) && !lc->is_key_frame;
  1069. }
  1070. #endif
  1071. void vp9_svc_check_spatial_layer_sync(VP9_COMP *const cpi) {
  1072. SVC *const svc = &cpi->svc;
  1073. // Only for superframes whose base is not key, as those are
  1074. // already sync frames.
  1075. if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
  1076. if (svc->spatial_layer_id == 0) {
  1077. // On base spatial layer: if the current superframe has a layer sync then
  1078. // reset the pattern counters and reset to base temporal layer.
  1079. if (svc->superframe_has_layer_sync)
  1080. vp9_svc_reset_temporal_layers(cpi, cpi->common.frame_type == KEY_FRAME);
  1081. }
  1082. // If the layer sync is set for this current spatial layer then
  1083. // disable the temporal reference.
  1084. if (svc->spatial_layer_id > 0 &&
  1085. svc->spatial_layer_sync[svc->spatial_layer_id]) {
  1086. cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
  1087. if (svc->use_gf_temporal_ref_current_layer) {
  1088. int index = svc->spatial_layer_id;
  1089. // If golden is used as second reference: need to remove it from
  1090. // prediction, reset refresh period to 0, and update the reference.
  1091. svc->use_gf_temporal_ref_current_layer = 0;
  1092. cpi->rc.baseline_gf_interval = 0;
  1093. cpi->rc.frames_till_gf_update_due = 0;
  1094. // On layer sync frame we must update the buffer index used for long
  1095. // term reference. Use the alt_ref since it is not used or updated on
  1096. // sync frames.
  1097. if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
  1098. assert(index >= 0);
  1099. cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
  1100. cpi->ext_refresh_alt_ref_frame = 1;
  1101. }
  1102. }
  1103. }
  1104. }
  1105. void vp9_svc_update_ref_frame_buffer_idx(VP9_COMP *const cpi) {
  1106. SVC *const svc = &cpi->svc;
  1107. // Update the usage of frame buffer index for base spatial layers.
  1108. if (svc->spatial_layer_id == 0) {
  1109. if ((cpi->ref_frame_flags & VP9_LAST_FLAG) || cpi->refresh_last_frame)
  1110. svc->fb_idx_base[cpi->lst_fb_idx] = 1;
  1111. if ((cpi->ref_frame_flags & VP9_GOLD_FLAG) || cpi->refresh_golden_frame)
  1112. svc->fb_idx_base[cpi->gld_fb_idx] = 1;
  1113. if ((cpi->ref_frame_flags & VP9_ALT_FLAG) || cpi->refresh_alt_ref_frame)
  1114. svc->fb_idx_base[cpi->alt_fb_idx] = 1;
  1115. }
  1116. }
  1117. static void vp9_svc_update_ref_frame_bypass_mode(VP9_COMP *const cpi) {
  1118. // For non-flexible/bypass SVC mode: check for refreshing other buffer
  1119. // slots.
  1120. SVC *const svc = &cpi->svc;
  1121. VP9_COMMON *const cm = &cpi->common;
  1122. BufferPool *const pool = cm->buffer_pool;
  1123. int i;
  1124. for (i = 0; i < REF_FRAMES; i++) {
  1125. if (cm->frame_type == KEY_FRAME ||
  1126. svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
  1127. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
  1128. svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
  1129. svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
  1130. }
  1131. }
  1132. }
  1133. void vp9_svc_update_ref_frame(VP9_COMP *const cpi) {
  1134. VP9_COMMON *const cm = &cpi->common;
  1135. SVC *const svc = &cpi->svc;
  1136. BufferPool *const pool = cm->buffer_pool;
  1137. if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  1138. svc->use_set_ref_frame_config) {
  1139. vp9_svc_update_ref_frame_bypass_mode(cpi);
  1140. } else if (cm->frame_type == KEY_FRAME && !svc->simulcast_mode) {
  1141. // Keep track of frame index for each reference frame.
  1142. int i;
  1143. // On key frame update all reference frame slots.
  1144. for (i = 0; i < REF_FRAMES; i++) {
  1145. svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
  1146. svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
  1147. // LAST/GOLDEN/ALTREF is already updated above.
  1148. if (i != cpi->lst_fb_idx && i != cpi->gld_fb_idx && i != cpi->alt_fb_idx)
  1149. ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
  1150. }
  1151. } else {
  1152. if (cpi->refresh_last_frame) {
  1153. svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] = svc->spatial_layer_id;
  1154. svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] = svc->temporal_layer_id;
  1155. }
  1156. if (cpi->refresh_golden_frame) {
  1157. svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] = svc->spatial_layer_id;
  1158. svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] = svc->temporal_layer_id;
  1159. }
  1160. if (cpi->refresh_alt_ref_frame) {
  1161. svc->fb_idx_spatial_layer_id[cpi->alt_fb_idx] = svc->spatial_layer_id;
  1162. svc->fb_idx_temporal_layer_id[cpi->alt_fb_idx] = svc->temporal_layer_id;
  1163. }
  1164. }
  1165. // Copy flags from encoder to SVC struct.
  1166. vp9_copy_flags_ref_update_idx(cpi);
  1167. vp9_svc_update_ref_frame_buffer_idx(cpi);
  1168. }
  1169. void vp9_svc_adjust_frame_rate(VP9_COMP *const cpi) {
  1170. int64_t this_duration =
  1171. cpi->svc.timebase_fac * cpi->svc.duration[cpi->svc.spatial_layer_id];
  1172. vp9_new_framerate(cpi, 10000000.0 / this_duration);
  1173. }
  1174. void vp9_svc_adjust_avg_frame_qindex(VP9_COMP *const cpi) {
  1175. VP9_COMMON *const cm = &cpi->common;
  1176. SVC *const svc = &cpi->svc;
  1177. RATE_CONTROL *const rc = &cpi->rc;
  1178. // On key frames in CBR mode: reset the avg_frame_index for base layer
  1179. // (to level closer to worst_quality) if the overshoot is significant.
  1180. // Reset it for all temporal layers on base spatial layer.
  1181. if (cm->frame_type == KEY_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
  1182. !svc->simulcast_mode &&
  1183. rc->projected_frame_size > 3 * rc->avg_frame_bandwidth) {
  1184. int tl;
  1185. rc->avg_frame_qindex[INTER_FRAME] =
  1186. VPXMAX(rc->avg_frame_qindex[INTER_FRAME],
  1187. (cm->base_qindex + rc->worst_quality) >> 1);
  1188. for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
  1189. const int layer = LAYER_IDS_TO_IDX(0, tl, svc->number_temporal_layers);
  1190. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1191. RATE_CONTROL *lrc = &lc->rc;
  1192. lrc->avg_frame_qindex[INTER_FRAME] = rc->avg_frame_qindex[INTER_FRAME];
  1193. }
  1194. }
  1195. }