vp9_svc_layercontext.c 33 KB

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