vp9_svc_layercontext.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. void vp9_init_layer_context(VP9_COMP *const cpi) {
  19. SVC *const svc = &cpi->svc;
  20. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  21. int mi_rows = cpi->common.mi_rows;
  22. int mi_cols = cpi->common.mi_cols;
  23. int sl, tl, i;
  24. int alt_ref_idx = svc->number_spatial_layers;
  25. svc->spatial_layer_id = 0;
  26. svc->temporal_layer_id = 0;
  27. svc->first_spatial_layer_to_encode = 0;
  28. svc->rc_drop_superframe = 0;
  29. svc->force_zero_mode_spatial_ref = 0;
  30. svc->use_base_mv = 0;
  31. svc->scaled_temp_is_alloc = 0;
  32. svc->scaled_one_half = 0;
  33. svc->current_superframe = 0;
  34. for (i = 0; i < REF_FRAMES; ++i) svc->ref_frame_index[i] = -1;
  35. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  36. cpi->svc.ext_frame_flags[sl] = 0;
  37. cpi->svc.ext_lst_fb_idx[sl] = 0;
  38. cpi->svc.ext_gld_fb_idx[sl] = 1;
  39. cpi->svc.ext_alt_fb_idx[sl] = 2;
  40. }
  41. if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
  42. if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img, SMALL_FRAME_WIDTH,
  43. SMALL_FRAME_HEIGHT, cpi->common.subsampling_x,
  44. cpi->common.subsampling_y,
  45. #if CONFIG_VP9_HIGHBITDEPTH
  46. cpi->common.use_highbitdepth,
  47. #endif
  48. VP9_ENC_BORDER_IN_PIXELS,
  49. cpi->common.byte_alignment, NULL, NULL, NULL))
  50. vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
  51. "Failed to allocate empty frame for multiple frame "
  52. "contexts");
  53. memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
  54. cpi->svc.empty_frame.img.buffer_alloc_sz);
  55. }
  56. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  57. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  58. int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
  59. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  60. RATE_CONTROL *const lrc = &lc->rc;
  61. int i;
  62. lc->current_video_frame_in_layer = 0;
  63. lc->layer_size = 0;
  64. lc->frames_from_key_frame = 0;
  65. lc->last_frame_type = FRAME_TYPES;
  66. lrc->ni_av_qi = oxcf->worst_allowed_q;
  67. lrc->total_actual_bits = 0;
  68. lrc->total_target_vs_actual = 0;
  69. lrc->ni_tot_qi = 0;
  70. lrc->tot_q = 0.0;
  71. lrc->avg_q = 0.0;
  72. lrc->ni_frames = 0;
  73. lrc->decimation_count = 0;
  74. lrc->decimation_factor = 0;
  75. for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
  76. lrc->rate_correction_factors[i] = 1.0;
  77. }
  78. if (cpi->oxcf.rc_mode == VPX_CBR) {
  79. lc->target_bandwidth = oxcf->layer_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->layer_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] =
  88. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  89. lrc->avg_frame_qindex[INTER_FRAME] =
  90. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  91. if (oxcf->ss_enable_auto_arf[sl])
  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. lrc->buffer_level =
  98. oxcf->starting_buffer_level_ms * lc->target_bandwidth / 1000;
  99. lrc->bits_off_target = lrc->buffer_level;
  100. // Initialize the cyclic refresh parameters. If spatial layers are used
  101. // (i.e., ss_number_layers > 1), these need to be updated per spatial
  102. // layer.
  103. // Cyclic refresh is only applied on base temporal layer.
  104. if (oxcf->ss_number_layers > 1 && tl == 0) {
  105. size_t last_coded_q_map_size;
  106. size_t consec_zero_mv_size;
  107. VP9_COMMON *const cm = &cpi->common;
  108. lc->sb_index = 0;
  109. CHECK_MEM_ERROR(cm, lc->map,
  110. vpx_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
  111. memset(lc->map, 0, mi_rows * mi_cols);
  112. last_coded_q_map_size =
  113. mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
  114. CHECK_MEM_ERROR(cm, lc->last_coded_q_map,
  115. vpx_malloc(last_coded_q_map_size));
  116. assert(MAXQ <= 255);
  117. memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
  118. consec_zero_mv_size = mi_rows * mi_cols * sizeof(*lc->consec_zero_mv);
  119. CHECK_MEM_ERROR(cm, lc->consec_zero_mv,
  120. vpx_malloc(consec_zero_mv_size));
  121. memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
  122. }
  123. }
  124. }
  125. // Still have extra buffer for base layer golden frame
  126. if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) &&
  127. alt_ref_idx < REF_FRAMES)
  128. svc->layer_context[0].gold_ref_idx = alt_ref_idx;
  129. }
  130. // Update the layer context from a change_config() call.
  131. void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
  132. const int target_bandwidth) {
  133. SVC *const svc = &cpi->svc;
  134. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  135. const RATE_CONTROL *const rc = &cpi->rc;
  136. int sl, tl, layer = 0, spatial_layer_target;
  137. float bitrate_alloc = 1.0;
  138. if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
  139. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  140. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  141. layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
  142. svc->layer_context[layer].target_bandwidth =
  143. oxcf->layer_target_bitrate[layer];
  144. }
  145. layer = LAYER_IDS_TO_IDX(
  146. sl,
  147. ((oxcf->ts_number_layers - 1) < 0 ? 0 : (oxcf->ts_number_layers - 1)),
  148. oxcf->ts_number_layers);
  149. spatial_layer_target = svc->layer_context[layer].target_bandwidth =
  150. oxcf->layer_target_bitrate[layer];
  151. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  152. LAYER_CONTEXT *const lc =
  153. &svc->layer_context[sl * oxcf->ts_number_layers + tl];
  154. RATE_CONTROL *const lrc = &lc->rc;
  155. lc->spatial_layer_target_bandwidth = spatial_layer_target;
  156. bitrate_alloc = (float)lc->target_bandwidth / spatial_layer_target;
  157. lrc->starting_buffer_level =
  158. (int64_t)(rc->starting_buffer_level * bitrate_alloc);
  159. lrc->optimal_buffer_level =
  160. (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
  161. lrc->maximum_buffer_size =
  162. (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
  163. lrc->bits_off_target =
  164. VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  165. lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
  166. lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
  167. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  168. lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
  169. lrc->worst_quality = rc->worst_quality;
  170. lrc->best_quality = rc->best_quality;
  171. }
  172. }
  173. } else {
  174. int layer_end;
  175. if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
  176. layer_end = svc->number_temporal_layers;
  177. } else {
  178. layer_end = svc->number_spatial_layers;
  179. }
  180. for (layer = 0; layer < layer_end; ++layer) {
  181. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  182. RATE_CONTROL *const lrc = &lc->rc;
  183. lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
  184. bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
  185. // Update buffer-related quantities.
  186. lrc->starting_buffer_level =
  187. (int64_t)(rc->starting_buffer_level * bitrate_alloc);
  188. lrc->optimal_buffer_level =
  189. (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
  190. lrc->maximum_buffer_size =
  191. (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
  192. lrc->bits_off_target =
  193. VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  194. lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
  195. // Update framerate-related quantities.
  196. if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
  197. lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
  198. } else {
  199. lc->framerate = cpi->framerate;
  200. }
  201. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  202. lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
  203. // Update qp-related quantities.
  204. lrc->worst_quality = rc->worst_quality;
  205. lrc->best_quality = rc->best_quality;
  206. }
  207. }
  208. }
  209. static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
  210. if (is_one_pass_cbr_svc(cpi))
  211. return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
  212. cpi->svc.number_temporal_layers +
  213. cpi->svc.temporal_layer_id];
  214. else
  215. return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
  216. ? &cpi->svc.layer_context[cpi->svc.temporal_layer_id]
  217. : &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
  218. }
  219. void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
  220. SVC *const svc = &cpi->svc;
  221. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  222. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  223. RATE_CONTROL *const lrc = &lc->rc;
  224. // Index into spatial+temporal arrays.
  225. const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
  226. svc->temporal_layer_id;
  227. const int tl = svc->temporal_layer_id;
  228. lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
  229. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  230. lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
  231. // Update the average layer frame size (non-cumulative per-frame-bw).
  232. if (tl == 0) {
  233. lc->avg_frame_size = lrc->avg_frame_bandwidth;
  234. } else {
  235. const double prev_layer_framerate =
  236. cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
  237. const int prev_layer_target_bandwidth =
  238. oxcf->layer_target_bitrate[st_idx - 1];
  239. lc->avg_frame_size =
  240. (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
  241. (lc->framerate - prev_layer_framerate));
  242. }
  243. }
  244. void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
  245. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  246. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  247. RATE_CONTROL *const lrc = &lc->rc;
  248. lc->framerate = framerate;
  249. lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  250. lrc->min_frame_bandwidth =
  251. (int)(lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
  252. lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
  253. oxcf->two_pass_vbrmax_section) /
  254. 100);
  255. vp9_rc_set_gf_interval_range(cpi, lrc);
  256. }
  257. void vp9_restore_layer_context(VP9_COMP *const cpi) {
  258. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  259. const int old_frame_since_key = cpi->rc.frames_since_key;
  260. const int old_frame_to_key = cpi->rc.frames_to_key;
  261. cpi->rc = lc->rc;
  262. cpi->twopass = lc->twopass;
  263. cpi->oxcf.target_bandwidth = lc->target_bandwidth;
  264. cpi->alt_ref_source = lc->alt_ref_source;
  265. // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
  266. // does not use speed = 0).
  267. if (is_one_pass_cbr_svc(cpi) && lc->speed > 0) {
  268. cpi->oxcf.speed = lc->speed;
  269. }
  270. // Reset the frames_since_key and frames_to_key counters to their values
  271. // before the layer restore. Keep these defined for the stream (not layer).
  272. if (cpi->svc.number_temporal_layers > 1 ||
  273. (cpi->svc.number_spatial_layers > 1 && !is_two_pass_svc(cpi))) {
  274. cpi->rc.frames_since_key = old_frame_since_key;
  275. cpi->rc.frames_to_key = old_frame_to_key;
  276. }
  277. // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
  278. // for the base temporal layer.
  279. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
  280. cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
  281. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  282. signed char *temp = cr->map;
  283. uint8_t *temp2 = cr->last_coded_q_map;
  284. uint8_t *temp3 = cpi->consec_zero_mv;
  285. cr->map = lc->map;
  286. lc->map = temp;
  287. cr->last_coded_q_map = lc->last_coded_q_map;
  288. lc->last_coded_q_map = temp2;
  289. cpi->consec_zero_mv = lc->consec_zero_mv;
  290. lc->consec_zero_mv = temp3;
  291. cr->sb_index = lc->sb_index;
  292. }
  293. }
  294. void vp9_save_layer_context(VP9_COMP *const cpi) {
  295. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  296. LAYER_CONTEXT *const lc = get_layer_context(cpi);
  297. lc->rc = cpi->rc;
  298. lc->twopass = cpi->twopass;
  299. lc->target_bandwidth = (int)oxcf->target_bandwidth;
  300. lc->alt_ref_source = cpi->alt_ref_source;
  301. // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
  302. // for the base temporal layer.
  303. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
  304. cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
  305. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  306. signed char *temp = lc->map;
  307. uint8_t *temp2 = lc->last_coded_q_map;
  308. uint8_t *temp3 = lc->consec_zero_mv;
  309. lc->map = cr->map;
  310. cr->map = temp;
  311. lc->last_coded_q_map = cr->last_coded_q_map;
  312. cr->last_coded_q_map = temp2;
  313. lc->consec_zero_mv = cpi->consec_zero_mv;
  314. cpi->consec_zero_mv = temp3;
  315. lc->sb_index = cr->sb_index;
  316. }
  317. }
  318. void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
  319. SVC *const svc = &cpi->svc;
  320. int i;
  321. for (i = 0; i < svc->number_spatial_layers; ++i) {
  322. TWO_PASS *const twopass = &svc->layer_context[i].twopass;
  323. svc->spatial_layer_id = i;
  324. vp9_init_second_pass(cpi);
  325. twopass->total_stats.spatial_layer_id = i;
  326. twopass->total_left_stats.spatial_layer_id = i;
  327. }
  328. svc->spatial_layer_id = 0;
  329. }
  330. void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
  331. LAYER_CONTEXT *const lc =
  332. &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
  333. cpi->svc.number_temporal_layers];
  334. ++lc->current_video_frame_in_layer;
  335. ++lc->frames_from_key_frame;
  336. if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
  337. ++cpi->svc.current_superframe;
  338. }
  339. int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
  340. return is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0 &&
  341. cpi->svc
  342. .layer_context[cpi->svc.spatial_layer_id *
  343. cpi->svc.number_temporal_layers +
  344. cpi->svc.temporal_layer_id]
  345. .is_key_frame;
  346. }
  347. static void get_layer_resolution(const int width_org, const int height_org,
  348. const int num, const int den, int *width_out,
  349. int *height_out) {
  350. int w, h;
  351. if (width_out == NULL || height_out == NULL || den == 0) return;
  352. w = width_org * num / den;
  353. h = height_org * num / den;
  354. // make height and width even to make chrome player happy
  355. w += w % 2;
  356. h += h % 2;
  357. *width_out = w;
  358. *height_out = h;
  359. }
  360. // The function sets proper ref_frame_flags, buffer indices, and buffer update
  361. // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
  362. // scheme.
  363. static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
  364. int frame_num_within_temporal_struct = 0;
  365. int spatial_id, temporal_id;
  366. spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  367. frame_num_within_temporal_struct =
  368. cpi->svc
  369. .layer_context[cpi->svc.spatial_layer_id *
  370. cpi->svc.number_temporal_layers]
  371. .current_video_frame_in_layer %
  372. 4;
  373. temporal_id = cpi->svc.temporal_layer_id =
  374. (frame_num_within_temporal_struct & 1)
  375. ? 2
  376. : (frame_num_within_temporal_struct >> 1);
  377. cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
  378. cpi->ext_refresh_alt_ref_frame = 0;
  379. if (!temporal_id) {
  380. cpi->ext_refresh_frame_flags_pending = 1;
  381. cpi->ext_refresh_last_frame = 1;
  382. if (!spatial_id) {
  383. cpi->ref_frame_flags = VP9_LAST_FLAG;
  384. } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  385. // base layer is a key frame.
  386. cpi->ref_frame_flags = VP9_LAST_FLAG;
  387. cpi->ext_refresh_last_frame = 0;
  388. cpi->ext_refresh_golden_frame = 1;
  389. } else {
  390. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  391. }
  392. } else if (temporal_id == 1) {
  393. cpi->ext_refresh_frame_flags_pending = 1;
  394. cpi->ext_refresh_alt_ref_frame = 1;
  395. if (!spatial_id) {
  396. cpi->ref_frame_flags = VP9_LAST_FLAG;
  397. } else {
  398. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  399. }
  400. } else {
  401. if (frame_num_within_temporal_struct == 1) {
  402. // the first tl2 picture
  403. if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
  404. cpi->ext_refresh_frame_flags_pending = 1;
  405. if (!spatial_id)
  406. cpi->ref_frame_flags = VP9_LAST_FLAG;
  407. else
  408. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  409. } else if (!spatial_id) {
  410. cpi->ext_refresh_frame_flags_pending = 1;
  411. cpi->ext_refresh_alt_ref_frame = 1;
  412. cpi->ref_frame_flags = VP9_LAST_FLAG;
  413. } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
  414. cpi->ext_refresh_frame_flags_pending = 1;
  415. cpi->ext_refresh_alt_ref_frame = 1;
  416. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  417. }
  418. } else {
  419. // The second tl2 picture
  420. if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
  421. cpi->ext_refresh_frame_flags_pending = 1;
  422. if (!spatial_id)
  423. cpi->ref_frame_flags = VP9_LAST_FLAG;
  424. else
  425. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  426. } else if (!spatial_id) {
  427. cpi->ext_refresh_frame_flags_pending = 1;
  428. cpi->ref_frame_flags = VP9_LAST_FLAG;
  429. cpi->ext_refresh_alt_ref_frame = 1;
  430. } else { // top layer
  431. cpi->ext_refresh_frame_flags_pending = 1;
  432. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  433. cpi->ext_refresh_alt_ref_frame = 1;
  434. }
  435. }
  436. }
  437. if (temporal_id == 0) {
  438. cpi->lst_fb_idx = spatial_id;
  439. if (spatial_id) {
  440. if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  441. cpi->lst_fb_idx = spatial_id - 1;
  442. cpi->gld_fb_idx = spatial_id;
  443. } else {
  444. cpi->gld_fb_idx = spatial_id - 1;
  445. }
  446. } else {
  447. cpi->gld_fb_idx = 0;
  448. }
  449. cpi->alt_fb_idx = 0;
  450. } else if (temporal_id == 1) {
  451. cpi->lst_fb_idx = spatial_id;
  452. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  453. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  454. } else if (frame_num_within_temporal_struct == 1) {
  455. cpi->lst_fb_idx = spatial_id;
  456. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  457. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  458. } else {
  459. cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  460. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  461. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  462. }
  463. }
  464. // The function sets proper ref_frame_flags, buffer indices, and buffer update
  465. // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
  466. // scheme.
  467. static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
  468. int spatial_id, temporal_id;
  469. spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  470. temporal_id = cpi->svc.temporal_layer_id =
  471. cpi->svc
  472. .layer_context[cpi->svc.spatial_layer_id *
  473. cpi->svc.number_temporal_layers]
  474. .current_video_frame_in_layer &
  475. 1;
  476. cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
  477. cpi->ext_refresh_alt_ref_frame = 0;
  478. if (!temporal_id) {
  479. cpi->ext_refresh_frame_flags_pending = 1;
  480. cpi->ext_refresh_last_frame = 1;
  481. if (!spatial_id) {
  482. cpi->ref_frame_flags = VP9_LAST_FLAG;
  483. } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  484. // base layer is a key frame.
  485. cpi->ref_frame_flags = VP9_LAST_FLAG;
  486. cpi->ext_refresh_last_frame = 0;
  487. cpi->ext_refresh_golden_frame = 1;
  488. } else {
  489. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  490. }
  491. } else if (temporal_id == 1) {
  492. cpi->ext_refresh_frame_flags_pending = 1;
  493. cpi->ext_refresh_alt_ref_frame = 1;
  494. if (!spatial_id) {
  495. cpi->ref_frame_flags = VP9_LAST_FLAG;
  496. } else {
  497. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  498. }
  499. }
  500. if (temporal_id == 0) {
  501. cpi->lst_fb_idx = spatial_id;
  502. if (spatial_id) {
  503. if (cpi->svc.layer_context[temporal_id].is_key_frame) {
  504. cpi->lst_fb_idx = spatial_id - 1;
  505. cpi->gld_fb_idx = spatial_id;
  506. } else {
  507. cpi->gld_fb_idx = spatial_id - 1;
  508. }
  509. } else {
  510. cpi->gld_fb_idx = 0;
  511. }
  512. cpi->alt_fb_idx = 0;
  513. } else if (temporal_id == 1) {
  514. cpi->lst_fb_idx = spatial_id;
  515. cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
  516. cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
  517. }
  518. }
  519. // The function sets proper ref_frame_flags, buffer indices, and buffer update
  520. // variables for temporal layering mode 0 - that has no temporal layering.
  521. static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
  522. VP9_COMP *const cpi) {
  523. int spatial_id;
  524. spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  525. cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
  526. cpi->ext_refresh_alt_ref_frame = 0;
  527. cpi->ext_refresh_frame_flags_pending = 1;
  528. cpi->ext_refresh_last_frame = 1;
  529. if (!spatial_id) {
  530. cpi->ref_frame_flags = VP9_LAST_FLAG;
  531. } else if (cpi->svc.layer_context[0].is_key_frame) {
  532. cpi->ref_frame_flags = VP9_LAST_FLAG;
  533. cpi->ext_refresh_last_frame = 0;
  534. cpi->ext_refresh_golden_frame = 1;
  535. } else {
  536. cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
  537. }
  538. cpi->lst_fb_idx = spatial_id;
  539. if (spatial_id) {
  540. if (cpi->svc.layer_context[0].is_key_frame) {
  541. cpi->lst_fb_idx = spatial_id - 1;
  542. cpi->gld_fb_idx = spatial_id;
  543. } else {
  544. cpi->gld_fb_idx = spatial_id - 1;
  545. }
  546. } else {
  547. cpi->gld_fb_idx = 0;
  548. }
  549. }
  550. int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) {
  551. int width = 0, height = 0;
  552. LAYER_CONTEXT *lc = NULL;
  553. if (cpi->svc.number_spatial_layers > 1) cpi->svc.use_base_mv = 1;
  554. cpi->svc.force_zero_mode_spatial_ref = 1;
  555. if (cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
  556. set_flags_and_fb_idx_for_temporal_mode3(cpi);
  557. } else if (cpi->svc.temporal_layering_mode ==
  558. VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
  559. set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
  560. } else if (cpi->svc.temporal_layering_mode ==
  561. VP9E_TEMPORAL_LAYERING_MODE_0101) {
  562. set_flags_and_fb_idx_for_temporal_mode2(cpi);
  563. } else if (cpi->svc.temporal_layering_mode ==
  564. VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
  565. // In the BYPASS/flexible mode, the encoder is relying on the application
  566. // to specify, for each spatial layer, the flags and buffer indices for the
  567. // layering.
  568. // Note that the check (cpi->ext_refresh_frame_flags_pending == 0) is
  569. // needed to support the case where the frame flags may be passed in via
  570. // vpx_codec_encode(), which can be used for the temporal-only svc case.
  571. // TODO(marpan): Consider adding an enc_config parameter to better handle
  572. // this case.
  573. if (cpi->ext_refresh_frame_flags_pending == 0) {
  574. int sl;
  575. cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  576. sl = cpi->svc.spatial_layer_id;
  577. vp9_apply_encoding_flags(cpi, cpi->svc.ext_frame_flags[sl]);
  578. cpi->lst_fb_idx = cpi->svc.ext_lst_fb_idx[sl];
  579. cpi->gld_fb_idx = cpi->svc.ext_gld_fb_idx[sl];
  580. cpi->alt_fb_idx = cpi->svc.ext_alt_fb_idx[sl];
  581. }
  582. }
  583. if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode)
  584. cpi->svc.rc_drop_superframe = 0;
  585. lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
  586. cpi->svc.number_temporal_layers +
  587. cpi->svc.temporal_layer_id];
  588. // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
  589. // only for non-BYPASS mode for now.
  590. if (cpi->svc.temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
  591. RATE_CONTROL *const lrc = &lc->rc;
  592. lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
  593. lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
  594. }
  595. get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
  596. lc->scaling_factor_num, lc->scaling_factor_den, &width,
  597. &height);
  598. if (vp9_set_size_literal(cpi, width, height) != 0)
  599. return VPX_CODEC_INVALID_PARAM;
  600. return 0;
  601. }
  602. #if CONFIG_SPATIAL_SVC
  603. #define SMALL_FRAME_FB_IDX 7
  604. int vp9_svc_start_frame(VP9_COMP *const cpi) {
  605. int width = 0, height = 0;
  606. LAYER_CONTEXT *lc;
  607. struct lookahead_entry *buf;
  608. int count = 1 << (cpi->svc.number_temporal_layers - 1);
  609. cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
  610. lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
  611. cpi->svc.temporal_layer_id = 0;
  612. while ((lc->current_video_frame_in_layer % count) != 0) {
  613. ++cpi->svc.temporal_layer_id;
  614. count >>= 1;
  615. }
  616. cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
  617. cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
  618. if (cpi->svc.spatial_layer_id == 0)
  619. cpi->gld_fb_idx =
  620. (lc->gold_ref_idx >= 0) ? lc->gold_ref_idx : cpi->lst_fb_idx;
  621. else
  622. cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
  623. if (lc->current_video_frame_in_layer == 0) {
  624. if (cpi->svc.spatial_layer_id >= 2) {
  625. cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
  626. } else {
  627. cpi->alt_fb_idx = cpi->lst_fb_idx;
  628. cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
  629. }
  630. } else {
  631. if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]) {
  632. cpi->alt_fb_idx = lc->alt_ref_idx;
  633. if (!lc->has_alt_frame) cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
  634. } else {
  635. // Find a proper alt_fb_idx for layers that don't have alt ref frame
  636. if (cpi->svc.spatial_layer_id == 0) {
  637. cpi->alt_fb_idx = cpi->lst_fb_idx;
  638. } else {
  639. LAYER_CONTEXT *lc_lower =
  640. &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];
  641. if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id - 1] &&
  642. lc_lower->alt_ref_source != NULL)
  643. cpi->alt_fb_idx = lc_lower->alt_ref_idx;
  644. else if (cpi->svc.spatial_layer_id >= 2)
  645. cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
  646. else
  647. cpi->alt_fb_idx = cpi->lst_fb_idx;
  648. }
  649. }
  650. }
  651. get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
  652. lc->scaling_factor_num, lc->scaling_factor_den, &width,
  653. &height);
  654. // Workaround for multiple frame contexts. In some frames we can't use prev_mi
  655. // since its previous frame could be changed during decoding time. The idea is
  656. // we put a empty invisible frame in front of them, then we will not use
  657. // prev_mi when encoding these frames.
  658. buf = vp9_lookahead_peek(cpi->lookahead, 0);
  659. if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 &&
  660. cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE &&
  661. lc->rc.frames_to_key != 0 &&
  662. !(buf != NULL && (buf->flags & VPX_EFLAG_FORCE_KF))) {
  663. if ((cpi->svc.number_temporal_layers > 1 &&
  664. cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) ||
  665. (cpi->svc.number_spatial_layers > 1 &&
  666. cpi->svc.spatial_layer_id == 0)) {
  667. struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0);
  668. if (buf != NULL) {
  669. cpi->svc.empty_frame.ts_start = buf->ts_start;
  670. cpi->svc.empty_frame.ts_end = buf->ts_end;
  671. cpi->svc.encode_empty_frame_state = ENCODING;
  672. cpi->common.show_frame = 0;
  673. cpi->ref_frame_flags = 0;
  674. cpi->common.frame_type = INTER_FRAME;
  675. cpi->lst_fb_idx = cpi->gld_fb_idx = cpi->alt_fb_idx =
  676. SMALL_FRAME_FB_IDX;
  677. if (cpi->svc.encode_intra_empty_frame != 0) cpi->common.intra_only = 1;
  678. width = SMALL_FRAME_WIDTH;
  679. height = SMALL_FRAME_HEIGHT;
  680. }
  681. }
  682. }
  683. cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
  684. cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
  685. vp9_change_config(cpi, &cpi->oxcf);
  686. if (vp9_set_size_literal(cpi, width, height) != 0)
  687. return VPX_CODEC_INVALID_PARAM;
  688. vp9_set_high_precision_mv(cpi, 1);
  689. cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source;
  690. return 0;
  691. }
  692. #undef SMALL_FRAME_FB_IDX
  693. #endif // CONFIG_SPATIAL_SVC
  694. struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
  695. struct lookahead_ctx *ctx,
  696. int drain) {
  697. struct lookahead_entry *buf = NULL;
  698. if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
  699. buf = vp9_lookahead_peek(ctx, 0);
  700. if (buf != NULL) {
  701. // Only remove the buffer when pop the highest layer.
  702. if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
  703. vp9_lookahead_pop(ctx, drain);
  704. }
  705. }
  706. }
  707. return buf;
  708. }
  709. void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
  710. int sl, tl;
  711. SVC *const svc = &cpi->svc;
  712. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  713. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  714. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  715. int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
  716. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  717. if (lc->map) vpx_free(lc->map);
  718. if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
  719. if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
  720. }
  721. }
  722. }
  723. // Reset on key frame: reset counters, references and buffer updates.
  724. void vp9_svc_reset_key_frame(VP9_COMP *const cpi) {
  725. int sl, tl;
  726. SVC *const svc = &cpi->svc;
  727. LAYER_CONTEXT *lc = NULL;
  728. for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
  729. for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
  730. lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
  731. lc->current_video_frame_in_layer = 0;
  732. lc->frames_from_key_frame = 0;
  733. }
  734. }
  735. if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
  736. set_flags_and_fb_idx_for_temporal_mode3(cpi);
  737. } else if (svc->temporal_layering_mode ==
  738. VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
  739. set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
  740. } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
  741. set_flags_and_fb_idx_for_temporal_mode2(cpi);
  742. }
  743. vp9_update_temporal_layer_framerate(cpi);
  744. vp9_restore_layer_context(cpi);
  745. }