vp9_svc_layercontext.c 24 KB

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