vpx_temporal_svc_encoder.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * Copyright (c) 2012 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. // This is an example demonstrating how to implement a multi-layer VPx
  11. // encoding scheme based on temporal scalability for video applications
  12. // that benefit from a scalable bitstream.
  13. #include <assert.h>
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "./vpx_config.h"
  19. #include "./y4minput.h"
  20. #include "../vpx_ports/vpx_timer.h"
  21. #include "vpx/vp8cx.h"
  22. #include "vpx/vpx_encoder.h"
  23. #include "vpx_ports/bitops.h"
  24. #include "../tools_common.h"
  25. #include "../video_writer.h"
  26. #define ROI_MAP 0
  27. #define zero(Dest) memset(&(Dest), 0, sizeof(Dest));
  28. static const char *exec_name;
  29. void usage_exit(void) { exit(EXIT_FAILURE); }
  30. // Denoiser states for vp8, for temporal denoising.
  31. enum denoiserStateVp8 {
  32. kVp8DenoiserOff,
  33. kVp8DenoiserOnYOnly,
  34. kVp8DenoiserOnYUV,
  35. kVp8DenoiserOnYUVAggressive,
  36. kVp8DenoiserOnAdaptive
  37. };
  38. // Denoiser states for vp9, for temporal denoising.
  39. enum denoiserStateVp9 {
  40. kVp9DenoiserOff,
  41. kVp9DenoiserOnYOnly,
  42. // For SVC: denoise the top two spatial layers.
  43. kVp9DenoiserOnYTwoSpatialLayers
  44. };
  45. static int mode_to_num_layers[13] = { 1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3, 3 };
  46. // For rate control encoding stats.
  47. struct RateControlMetrics {
  48. // Number of input frames per layer.
  49. int layer_input_frames[VPX_TS_MAX_LAYERS];
  50. // Total (cumulative) number of encoded frames per layer.
  51. int layer_tot_enc_frames[VPX_TS_MAX_LAYERS];
  52. // Number of encoded non-key frames per layer.
  53. int layer_enc_frames[VPX_TS_MAX_LAYERS];
  54. // Framerate per layer layer (cumulative).
  55. double layer_framerate[VPX_TS_MAX_LAYERS];
  56. // Target average frame size per layer (per-frame-bandwidth per layer).
  57. double layer_pfb[VPX_TS_MAX_LAYERS];
  58. // Actual average frame size per layer.
  59. double layer_avg_frame_size[VPX_TS_MAX_LAYERS];
  60. // Average rate mismatch per layer (|target - actual| / target).
  61. double layer_avg_rate_mismatch[VPX_TS_MAX_LAYERS];
  62. // Actual encoding bitrate per layer (cumulative).
  63. double layer_encoding_bitrate[VPX_TS_MAX_LAYERS];
  64. // Average of the short-time encoder actual bitrate.
  65. // TODO(marpan): Should we add these short-time stats for each layer?
  66. double avg_st_encoding_bitrate;
  67. // Variance of the short-time encoder actual bitrate.
  68. double variance_st_encoding_bitrate;
  69. // Window (number of frames) for computing short-timee encoding bitrate.
  70. int window_size;
  71. // Number of window measurements.
  72. int window_count;
  73. int layer_target_bitrate[VPX_MAX_LAYERS];
  74. };
  75. // Note: these rate control metrics assume only 1 key frame in the
  76. // sequence (i.e., first frame only). So for temporal pattern# 7
  77. // (which has key frame for every frame on base layer), the metrics
  78. // computation will be off/wrong.
  79. // TODO(marpan): Update these metrics to account for multiple key frames
  80. // in the stream.
  81. static void set_rate_control_metrics(struct RateControlMetrics *rc,
  82. vpx_codec_enc_cfg_t *cfg) {
  83. int i = 0;
  84. // Set the layer (cumulative) framerate and the target layer (non-cumulative)
  85. // per-frame-bandwidth, for the rate control encoding stats below.
  86. const double framerate = cfg->g_timebase.den / cfg->g_timebase.num;
  87. const int ts_number_layers = cfg->ts_number_layers;
  88. rc->layer_framerate[0] = framerate / cfg->ts_rate_decimator[0];
  89. rc->layer_pfb[0] =
  90. 1000.0 * rc->layer_target_bitrate[0] / rc->layer_framerate[0];
  91. for (i = 0; i < ts_number_layers; ++i) {
  92. if (i > 0) {
  93. rc->layer_framerate[i] = framerate / cfg->ts_rate_decimator[i];
  94. rc->layer_pfb[i] =
  95. 1000.0 *
  96. (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
  97. (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
  98. }
  99. rc->layer_input_frames[i] = 0;
  100. rc->layer_enc_frames[i] = 0;
  101. rc->layer_tot_enc_frames[i] = 0;
  102. rc->layer_encoding_bitrate[i] = 0.0;
  103. rc->layer_avg_frame_size[i] = 0.0;
  104. rc->layer_avg_rate_mismatch[i] = 0.0;
  105. }
  106. rc->window_count = 0;
  107. rc->window_size = 15;
  108. rc->avg_st_encoding_bitrate = 0.0;
  109. rc->variance_st_encoding_bitrate = 0.0;
  110. // Target bandwidth for the whole stream.
  111. // Set to layer_target_bitrate for highest layer (total bitrate).
  112. cfg->rc_target_bitrate = rc->layer_target_bitrate[ts_number_layers - 1];
  113. }
  114. static void printout_rate_control_summary(struct RateControlMetrics *rc,
  115. vpx_codec_enc_cfg_t *cfg,
  116. int frame_cnt) {
  117. unsigned int i = 0;
  118. int tot_num_frames = 0;
  119. double perc_fluctuation = 0.0;
  120. printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
  121. printf("Rate control layer stats for %d layer(s):\n\n",
  122. cfg->ts_number_layers);
  123. for (i = 0; i < cfg->ts_number_layers; ++i) {
  124. const int num_dropped =
  125. (i > 0) ? (rc->layer_input_frames[i] - rc->layer_enc_frames[i])
  126. : (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
  127. tot_num_frames += rc->layer_input_frames[i];
  128. rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
  129. rc->layer_encoding_bitrate[i] /
  130. tot_num_frames;
  131. rc->layer_avg_frame_size[i] =
  132. rc->layer_avg_frame_size[i] / rc->layer_enc_frames[i];
  133. rc->layer_avg_rate_mismatch[i] =
  134. 100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[i];
  135. printf("For layer#: %d \n", i);
  136. printf("Bitrate (target vs actual): %d %f \n", rc->layer_target_bitrate[i],
  137. rc->layer_encoding_bitrate[i]);
  138. printf("Average frame size (target vs actual): %f %f \n", rc->layer_pfb[i],
  139. rc->layer_avg_frame_size[i]);
  140. printf("Average rate_mismatch: %f \n", rc->layer_avg_rate_mismatch[i]);
  141. printf(
  142. "Number of input frames, encoded (non-key) frames, "
  143. "and perc dropped frames: %d %d %f \n",
  144. rc->layer_input_frames[i], rc->layer_enc_frames[i],
  145. 100.0 * num_dropped / rc->layer_input_frames[i]);
  146. printf("\n");
  147. }
  148. rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
  149. rc->variance_st_encoding_bitrate =
  150. rc->variance_st_encoding_bitrate / rc->window_count -
  151. (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
  152. perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
  153. rc->avg_st_encoding_bitrate;
  154. printf("Short-time stats, for window of %d frames: \n", rc->window_size);
  155. printf("Average, rms-variance, and percent-fluct: %f %f %f \n",
  156. rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
  157. perc_fluctuation);
  158. if ((frame_cnt - 1) != tot_num_frames)
  159. die("Error: Number of input frames not equal to output! \n");
  160. }
  161. #if ROI_MAP
  162. static void set_roi_map(const char *enc_name, vpx_codec_enc_cfg_t *cfg,
  163. vpx_roi_map_t *roi) {
  164. unsigned int i, j;
  165. int block_size = 0;
  166. uint8_t is_vp8 = strncmp(enc_name, "vp8", 3) == 0 ? 1 : 0;
  167. uint8_t is_vp9 = strncmp(enc_name, "vp9", 3) == 0 ? 1 : 0;
  168. if (!is_vp8 && !is_vp9) {
  169. die("unsupported codec.");
  170. }
  171. zero(*roi);
  172. block_size = is_vp9 && !is_vp8 ? 8 : 16;
  173. // ROI is based on the segments (4 for vp8, 8 for vp9), smallest unit for
  174. // segment is 16x16 for vp8, 8x8 for vp9.
  175. roi->rows = (cfg->g_h + block_size - 1) / block_size;
  176. roi->cols = (cfg->g_w + block_size - 1) / block_size;
  177. // Applies delta QP on the segment blocks, varies from -63 to 63.
  178. // Setting to negative means lower QP (better quality).
  179. // Below we set delta_q to the extreme (-63) to show strong effect.
  180. // VP8 uses the first 4 segments. VP9 uses all 8 segments.
  181. zero(roi->delta_q);
  182. roi->delta_q[1] = -63;
  183. // Applies delta loopfilter strength on the segment blocks, varies from -63 to
  184. // 63. Setting to positive means stronger loopfilter. VP8 uses the first 4
  185. // segments. VP9 uses all 8 segments.
  186. zero(roi->delta_lf);
  187. if (is_vp8) {
  188. // Applies skip encoding threshold on the segment blocks, varies from 0 to
  189. // UINT_MAX. Larger value means more skipping of encoding is possible.
  190. // This skip threshold only applies on delta frames.
  191. zero(roi->static_threshold);
  192. }
  193. if (is_vp9) {
  194. // Apply skip segment. Setting to 1 means this block will be copied from
  195. // previous frame.
  196. zero(roi->skip);
  197. }
  198. if (is_vp9) {
  199. // Apply ref frame segment.
  200. // -1 : Do not apply this segment.
  201. // 0 : Froce using intra.
  202. // 1 : Force using last.
  203. // 2 : Force using golden.
  204. // 3 : Force using alfref but not used in non-rd pickmode for 0 lag.
  205. memset(roi->ref_frame, -1, sizeof(roi->ref_frame));
  206. roi->ref_frame[1] = 1;
  207. }
  208. // Use 2 states: 1 is center square, 0 is the rest.
  209. roi->roi_map =
  210. (uint8_t *)calloc(roi->rows * roi->cols, sizeof(*roi->roi_map));
  211. for (i = 0; i < roi->rows; ++i) {
  212. for (j = 0; j < roi->cols; ++j) {
  213. if (i > (roi->rows >> 2) && i < ((roi->rows * 3) >> 2) &&
  214. j > (roi->cols >> 2) && j < ((roi->cols * 3) >> 2)) {
  215. roi->roi_map[i * roi->cols + j] = 1;
  216. }
  217. }
  218. }
  219. }
  220. #endif
  221. // Temporal scaling parameters:
  222. // NOTE: The 3 prediction frames cannot be used interchangeably due to
  223. // differences in the way they are handled throughout the code. The
  224. // frames should be allocated to layers in the order LAST, GF, ARF.
  225. // Other combinations work, but may produce slightly inferior results.
  226. static void set_temporal_layer_pattern(int layering_mode,
  227. vpx_codec_enc_cfg_t *cfg,
  228. int *layer_flags,
  229. int *flag_periodicity) {
  230. switch (layering_mode) {
  231. case 0: {
  232. // 1-layer.
  233. int ids[1] = { 0 };
  234. cfg->ts_periodicity = 1;
  235. *flag_periodicity = 1;
  236. cfg->ts_number_layers = 1;
  237. cfg->ts_rate_decimator[0] = 1;
  238. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  239. // Update L only.
  240. layer_flags[0] =
  241. VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
  242. break;
  243. }
  244. case 1: {
  245. // 2-layers, 2-frame period.
  246. int ids[2] = { 0, 1 };
  247. cfg->ts_periodicity = 2;
  248. *flag_periodicity = 2;
  249. cfg->ts_number_layers = 2;
  250. cfg->ts_rate_decimator[0] = 2;
  251. cfg->ts_rate_decimator[1] = 1;
  252. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  253. #if 1
  254. // 0=L, 1=GF, Intra-layer prediction enabled.
  255. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
  256. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF |
  257. VP8_EFLAG_NO_REF_ARF;
  258. layer_flags[1] =
  259. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_REF_ARF;
  260. #else
  261. // 0=L, 1=GF, Intra-layer prediction disabled.
  262. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
  263. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF |
  264. VP8_EFLAG_NO_REF_ARF;
  265. layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
  266. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_LAST;
  267. #endif
  268. break;
  269. }
  270. case 2: {
  271. // 2-layers, 3-frame period.
  272. int ids[3] = { 0, 1, 1 };
  273. cfg->ts_periodicity = 3;
  274. *flag_periodicity = 3;
  275. cfg->ts_number_layers = 2;
  276. cfg->ts_rate_decimator[0] = 3;
  277. cfg->ts_rate_decimator[1] = 1;
  278. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  279. // 0=L, 1=GF, Intra-layer prediction enabled.
  280. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
  281. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
  282. VP8_EFLAG_NO_UPD_ARF;
  283. layer_flags[1] = layer_flags[2] =
  284. VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
  285. VP8_EFLAG_NO_UPD_LAST;
  286. break;
  287. }
  288. case 3: {
  289. // 3-layers, 6-frame period.
  290. int ids[6] = { 0, 2, 2, 1, 2, 2 };
  291. cfg->ts_periodicity = 6;
  292. *flag_periodicity = 6;
  293. cfg->ts_number_layers = 3;
  294. cfg->ts_rate_decimator[0] = 6;
  295. cfg->ts_rate_decimator[1] = 3;
  296. cfg->ts_rate_decimator[2] = 1;
  297. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  298. // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
  299. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
  300. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
  301. VP8_EFLAG_NO_UPD_ARF;
  302. layer_flags[3] =
  303. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
  304. layer_flags[1] = layer_flags[2] = layer_flags[4] = layer_flags[5] =
  305. VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
  306. break;
  307. }
  308. case 4: {
  309. // 3-layers, 4-frame period.
  310. int ids[4] = { 0, 2, 1, 2 };
  311. cfg->ts_periodicity = 4;
  312. *flag_periodicity = 4;
  313. cfg->ts_number_layers = 3;
  314. cfg->ts_rate_decimator[0] = 4;
  315. cfg->ts_rate_decimator[1] = 2;
  316. cfg->ts_rate_decimator[2] = 1;
  317. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  318. // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
  319. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
  320. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
  321. VP8_EFLAG_NO_UPD_ARF;
  322. layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
  323. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
  324. layer_flags[1] = layer_flags[3] =
  325. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
  326. VP8_EFLAG_NO_UPD_ARF;
  327. break;
  328. }
  329. case 5: {
  330. // 3-layers, 4-frame period.
  331. int ids[4] = { 0, 2, 1, 2 };
  332. cfg->ts_periodicity = 4;
  333. *flag_periodicity = 4;
  334. cfg->ts_number_layers = 3;
  335. cfg->ts_rate_decimator[0] = 4;
  336. cfg->ts_rate_decimator[1] = 2;
  337. cfg->ts_rate_decimator[2] = 1;
  338. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  339. // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
  340. // in layer 2.
  341. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
  342. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
  343. VP8_EFLAG_NO_UPD_ARF;
  344. layer_flags[2] =
  345. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
  346. layer_flags[1] = layer_flags[3] =
  347. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
  348. VP8_EFLAG_NO_UPD_ARF;
  349. break;
  350. }
  351. case 6: {
  352. // 3-layers, 4-frame period.
  353. int ids[4] = { 0, 2, 1, 2 };
  354. cfg->ts_periodicity = 4;
  355. *flag_periodicity = 4;
  356. cfg->ts_number_layers = 3;
  357. cfg->ts_rate_decimator[0] = 4;
  358. cfg->ts_rate_decimator[1] = 2;
  359. cfg->ts_rate_decimator[2] = 1;
  360. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  361. // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
  362. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
  363. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
  364. VP8_EFLAG_NO_UPD_ARF;
  365. layer_flags[2] =
  366. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
  367. layer_flags[1] = layer_flags[3] =
  368. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
  369. break;
  370. }
  371. case 7: {
  372. // NOTE: Probably of academic interest only.
  373. // 5-layers, 16-frame period.
  374. int ids[16] = { 0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4 };
  375. cfg->ts_periodicity = 16;
  376. *flag_periodicity = 16;
  377. cfg->ts_number_layers = 5;
  378. cfg->ts_rate_decimator[0] = 16;
  379. cfg->ts_rate_decimator[1] = 8;
  380. cfg->ts_rate_decimator[2] = 4;
  381. cfg->ts_rate_decimator[3] = 2;
  382. cfg->ts_rate_decimator[4] = 1;
  383. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  384. layer_flags[0] = VPX_EFLAG_FORCE_KF;
  385. layer_flags[1] = layer_flags[3] = layer_flags[5] = layer_flags[7] =
  386. layer_flags[9] = layer_flags[11] = layer_flags[13] = layer_flags[15] =
  387. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
  388. VP8_EFLAG_NO_UPD_ARF;
  389. layer_flags[2] = layer_flags[6] = layer_flags[10] = layer_flags[14] =
  390. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
  391. layer_flags[4] = layer_flags[12] =
  392. VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_UPD_ARF;
  393. layer_flags[8] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
  394. break;
  395. }
  396. case 8: {
  397. // 2-layers, with sync point at first frame of layer 1.
  398. int ids[2] = { 0, 1 };
  399. cfg->ts_periodicity = 2;
  400. *flag_periodicity = 8;
  401. cfg->ts_number_layers = 2;
  402. cfg->ts_rate_decimator[0] = 2;
  403. cfg->ts_rate_decimator[1] = 1;
  404. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  405. // 0=L, 1=GF.
  406. // ARF is used as predictor for all frames, and is only updated on
  407. // key frame. Sync point every 8 frames.
  408. // Layer 0: predict from L and ARF, update L and G.
  409. layer_flags[0] =
  410. VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF;
  411. // Layer 1: sync point: predict from L and ARF, and update G.
  412. layer_flags[1] =
  413. VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
  414. // Layer 0, predict from L and ARF, update L.
  415. layer_flags[2] =
  416. VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
  417. // Layer 1: predict from L, G and ARF, and update G.
  418. layer_flags[3] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
  419. VP8_EFLAG_NO_UPD_ENTROPY;
  420. // Layer 0.
  421. layer_flags[4] = layer_flags[2];
  422. // Layer 1.
  423. layer_flags[5] = layer_flags[3];
  424. // Layer 0.
  425. layer_flags[6] = layer_flags[4];
  426. // Layer 1.
  427. layer_flags[7] = layer_flags[5];
  428. break;
  429. }
  430. case 9: {
  431. // 3-layers: Sync points for layer 1 and 2 every 8 frames.
  432. int ids[4] = { 0, 2, 1, 2 };
  433. cfg->ts_periodicity = 4;
  434. *flag_periodicity = 8;
  435. cfg->ts_number_layers = 3;
  436. cfg->ts_rate_decimator[0] = 4;
  437. cfg->ts_rate_decimator[1] = 2;
  438. cfg->ts_rate_decimator[2] = 1;
  439. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  440. // 0=L, 1=GF, 2=ARF.
  441. layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
  442. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
  443. VP8_EFLAG_NO_UPD_ARF;
  444. layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
  445. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
  446. layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
  447. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
  448. layer_flags[3] = layer_flags[5] =
  449. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
  450. layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
  451. VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
  452. layer_flags[6] =
  453. VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
  454. layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
  455. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_ENTROPY;
  456. break;
  457. }
  458. case 10: {
  459. // 3-layers structure where ARF is used as predictor for all frames,
  460. // and is only updated on key frame.
  461. // Sync points for layer 1 and 2 every 8 frames.
  462. int ids[4] = { 0, 2, 1, 2 };
  463. cfg->ts_periodicity = 4;
  464. *flag_periodicity = 8;
  465. cfg->ts_number_layers = 3;
  466. cfg->ts_rate_decimator[0] = 4;
  467. cfg->ts_rate_decimator[1] = 2;
  468. cfg->ts_rate_decimator[2] = 1;
  469. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  470. // 0=L, 1=GF, 2=ARF.
  471. // Layer 0: predict from L and ARF; update L and G.
  472. layer_flags[0] =
  473. VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
  474. // Layer 2: sync point: predict from L and ARF; update none.
  475. layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
  476. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
  477. VP8_EFLAG_NO_UPD_ENTROPY;
  478. // Layer 1: sync point: predict from L and ARF; update G.
  479. layer_flags[2] =
  480. VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
  481. // Layer 2: predict from L, G, ARF; update none.
  482. layer_flags[3] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
  483. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
  484. // Layer 0: predict from L and ARF; update L.
  485. layer_flags[4] =
  486. VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
  487. // Layer 2: predict from L, G, ARF; update none.
  488. layer_flags[5] = layer_flags[3];
  489. // Layer 1: predict from L, G, ARF; update G.
  490. layer_flags[6] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
  491. // Layer 2: predict from L, G, ARF; update none.
  492. layer_flags[7] = layer_flags[3];
  493. break;
  494. }
  495. case 11: {
  496. // 3-layers structure with one reference frame.
  497. // This works same as temporal_layering_mode 3.
  498. // This was added to compare with vp9_spatial_svc_encoder.
  499. // 3-layers, 4-frame period.
  500. int ids[4] = { 0, 2, 1, 2 };
  501. cfg->ts_periodicity = 4;
  502. *flag_periodicity = 4;
  503. cfg->ts_number_layers = 3;
  504. cfg->ts_rate_decimator[0] = 4;
  505. cfg->ts_rate_decimator[1] = 2;
  506. cfg->ts_rate_decimator[2] = 1;
  507. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  508. // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
  509. layer_flags[0] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
  510. VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
  511. layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
  512. VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
  513. layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
  514. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
  515. layer_flags[3] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF |
  516. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
  517. break;
  518. }
  519. case 12:
  520. default: {
  521. // 3-layers structure as in case 10, but no sync/refresh points for
  522. // layer 1 and 2.
  523. int ids[4] = { 0, 2, 1, 2 };
  524. cfg->ts_periodicity = 4;
  525. *flag_periodicity = 8;
  526. cfg->ts_number_layers = 3;
  527. cfg->ts_rate_decimator[0] = 4;
  528. cfg->ts_rate_decimator[1] = 2;
  529. cfg->ts_rate_decimator[2] = 1;
  530. memcpy(cfg->ts_layer_id, ids, sizeof(ids));
  531. // 0=L, 1=GF, 2=ARF.
  532. // Layer 0: predict from L and ARF; update L.
  533. layer_flags[0] =
  534. VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
  535. layer_flags[4] = layer_flags[0];
  536. // Layer 1: predict from L, G, ARF; update G.
  537. layer_flags[2] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
  538. layer_flags[6] = layer_flags[2];
  539. // Layer 2: predict from L, G, ARF; update none.
  540. layer_flags[1] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
  541. VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
  542. layer_flags[3] = layer_flags[1];
  543. layer_flags[5] = layer_flags[1];
  544. layer_flags[7] = layer_flags[1];
  545. break;
  546. }
  547. }
  548. }
  549. int main(int argc, char **argv) {
  550. VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS] = { NULL };
  551. vpx_codec_ctx_t codec;
  552. vpx_codec_enc_cfg_t cfg;
  553. int frame_cnt = 0;
  554. vpx_image_t raw;
  555. vpx_codec_err_t res;
  556. unsigned int width;
  557. unsigned int height;
  558. uint32_t error_resilient = 0;
  559. int speed;
  560. int frame_avail;
  561. int got_data;
  562. int flags = 0;
  563. unsigned int i;
  564. int pts = 0; // PTS starts at 0.
  565. int frame_duration = 1; // 1 timebase tick per frame.
  566. int layering_mode = 0;
  567. int layer_flags[VPX_TS_MAX_PERIODICITY] = { 0 };
  568. int flag_periodicity = 1;
  569. #if ROI_MAP
  570. vpx_roi_map_t roi;
  571. #endif
  572. vpx_svc_layer_id_t layer_id;
  573. const VpxInterface *encoder = NULL;
  574. struct VpxInputContext input_ctx;
  575. struct RateControlMetrics rc;
  576. int64_t cx_time = 0;
  577. const int min_args_base = 13;
  578. #if CONFIG_VP9_HIGHBITDEPTH
  579. vpx_bit_depth_t bit_depth = VPX_BITS_8;
  580. int input_bit_depth = 8;
  581. const int min_args = min_args_base + 1;
  582. #else
  583. const int min_args = min_args_base;
  584. #endif // CONFIG_VP9_HIGHBITDEPTH
  585. double sum_bitrate = 0.0;
  586. double sum_bitrate2 = 0.0;
  587. double framerate = 30.0;
  588. zero(rc.layer_target_bitrate);
  589. memset(&layer_id, 0, sizeof(vpx_svc_layer_id_t));
  590. memset(&input_ctx, 0, sizeof(input_ctx));
  591. /* Setup default input stream settings */
  592. input_ctx.framerate.numerator = 30;
  593. input_ctx.framerate.denominator = 1;
  594. input_ctx.only_i420 = 1;
  595. input_ctx.bit_depth = 0;
  596. exec_name = argv[0];
  597. // Check usage and arguments.
  598. if (argc < min_args) {
  599. #if CONFIG_VP9_HIGHBITDEPTH
  600. die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
  601. "<rate_num> <rate_den> <speed> <frame_drop_threshold> "
  602. "<error_resilient> <threads> <mode> "
  603. "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n",
  604. argv[0]);
  605. #else
  606. die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
  607. "<rate_num> <rate_den> <speed> <frame_drop_threshold> "
  608. "<error_resilient> <threads> <mode> "
  609. "<Rate_0> ... <Rate_nlayers-1> \n",
  610. argv[0]);
  611. #endif // CONFIG_VP9_HIGHBITDEPTH
  612. }
  613. encoder = get_vpx_encoder_by_name(argv[3]);
  614. if (!encoder) die("Unsupported codec.");
  615. printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
  616. width = (unsigned int)strtoul(argv[4], NULL, 0);
  617. height = (unsigned int)strtoul(argv[5], NULL, 0);
  618. if (width < 16 || width % 2 || height < 16 || height % 2) {
  619. die("Invalid resolution: %d x %d", width, height);
  620. }
  621. layering_mode = (int)strtol(argv[12], NULL, 0);
  622. if (layering_mode < 0 || layering_mode > 13) {
  623. die("Invalid layering mode (0..12) %s", argv[12]);
  624. }
  625. if (argc != min_args + mode_to_num_layers[layering_mode]) {
  626. die("Invalid number of arguments");
  627. }
  628. input_ctx.filename = argv[1];
  629. open_input_file(&input_ctx);
  630. #if CONFIG_VP9_HIGHBITDEPTH
  631. switch (strtol(argv[argc - 1], NULL, 0)) {
  632. case 8:
  633. bit_depth = VPX_BITS_8;
  634. input_bit_depth = 8;
  635. break;
  636. case 10:
  637. bit_depth = VPX_BITS_10;
  638. input_bit_depth = 10;
  639. break;
  640. case 12:
  641. bit_depth = VPX_BITS_12;
  642. input_bit_depth = 12;
  643. break;
  644. default: die("Invalid bit depth (8, 10, 12) %s", argv[argc - 1]);
  645. }
  646. // Y4M reader has its own allocation.
  647. if (input_ctx.file_type != FILE_TYPE_Y4M) {
  648. if (!vpx_img_alloc(
  649. &raw,
  650. bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_I42016,
  651. width, height, 32)) {
  652. die("Failed to allocate image", width, height);
  653. }
  654. }
  655. #else
  656. // Y4M reader has its own allocation.
  657. if (input_ctx.file_type != FILE_TYPE_Y4M) {
  658. if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
  659. die("Failed to allocate image", width, height);
  660. }
  661. }
  662. #endif // CONFIG_VP9_HIGHBITDEPTH
  663. // Populate encoder configuration.
  664. res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
  665. if (res) {
  666. printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
  667. return EXIT_FAILURE;
  668. }
  669. // Update the default configuration with our settings.
  670. cfg.g_w = width;
  671. cfg.g_h = height;
  672. #if CONFIG_VP9_HIGHBITDEPTH
  673. if (bit_depth != VPX_BITS_8) {
  674. cfg.g_bit_depth = bit_depth;
  675. cfg.g_input_bit_depth = input_bit_depth;
  676. cfg.g_profile = 2;
  677. }
  678. #endif // CONFIG_VP9_HIGHBITDEPTH
  679. // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
  680. cfg.g_timebase.num = (int)strtol(argv[6], NULL, 0);
  681. cfg.g_timebase.den = (int)strtol(argv[7], NULL, 0);
  682. speed = (int)strtol(argv[8], NULL, 0);
  683. if (speed < 0) {
  684. die("Invalid speed setting: must be positive");
  685. }
  686. if (strncmp(encoder->name, "vp9", 3) == 0 && speed > 9) {
  687. warn("Mapping speed %d to speed 9.\n", speed);
  688. }
  689. for (i = min_args_base;
  690. (int)i < min_args_base + mode_to_num_layers[layering_mode]; ++i) {
  691. rc.layer_target_bitrate[i - 13] = (int)strtol(argv[i], NULL, 0);
  692. if (strncmp(encoder->name, "vp8", 3) == 0)
  693. cfg.ts_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
  694. else if (strncmp(encoder->name, "vp9", 3) == 0)
  695. cfg.layer_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
  696. }
  697. // Real time parameters.
  698. cfg.rc_dropframe_thresh = (unsigned int)strtoul(argv[9], NULL, 0);
  699. cfg.rc_end_usage = VPX_CBR;
  700. cfg.rc_min_quantizer = 2;
  701. cfg.rc_max_quantizer = 56;
  702. if (strncmp(encoder->name, "vp9", 3) == 0) cfg.rc_max_quantizer = 52;
  703. cfg.rc_undershoot_pct = 50;
  704. cfg.rc_overshoot_pct = 50;
  705. cfg.rc_buf_initial_sz = 600;
  706. cfg.rc_buf_optimal_sz = 600;
  707. cfg.rc_buf_sz = 1000;
  708. // Disable dynamic resizing by default.
  709. cfg.rc_resize_allowed = 0;
  710. // Use 1 thread as default.
  711. cfg.g_threads = (unsigned int)strtoul(argv[11], NULL, 0);
  712. error_resilient = (uint32_t)strtoul(argv[10], NULL, 0);
  713. if (error_resilient != 0 && error_resilient != 1) {
  714. die("Invalid value for error resilient (0, 1): %d.", error_resilient);
  715. }
  716. // Enable error resilient mode.
  717. cfg.g_error_resilient = error_resilient;
  718. cfg.g_lag_in_frames = 0;
  719. cfg.kf_mode = VPX_KF_AUTO;
  720. // Disable automatic keyframe placement.
  721. cfg.kf_min_dist = cfg.kf_max_dist = 3000;
  722. cfg.temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
  723. set_temporal_layer_pattern(layering_mode, &cfg, layer_flags,
  724. &flag_periodicity);
  725. set_rate_control_metrics(&rc, &cfg);
  726. if (input_ctx.file_type == FILE_TYPE_Y4M) {
  727. if (input_ctx.width != cfg.g_w || input_ctx.height != cfg.g_h) {
  728. die("Incorrect width or height: %d x %d", cfg.g_w, cfg.g_h);
  729. }
  730. if (input_ctx.framerate.numerator != cfg.g_timebase.den ||
  731. input_ctx.framerate.denominator != cfg.g_timebase.num) {
  732. die("Incorrect framerate: numerator %d denominator %d",
  733. cfg.g_timebase.num, cfg.g_timebase.den);
  734. }
  735. }
  736. framerate = cfg.g_timebase.den / cfg.g_timebase.num;
  737. // Open an output file for each stream.
  738. for (i = 0; i < cfg.ts_number_layers; ++i) {
  739. char file_name[PATH_MAX];
  740. VpxVideoInfo info;
  741. info.codec_fourcc = encoder->fourcc;
  742. info.frame_width = cfg.g_w;
  743. info.frame_height = cfg.g_h;
  744. info.time_base.numerator = cfg.g_timebase.num;
  745. info.time_base.denominator = cfg.g_timebase.den;
  746. snprintf(file_name, sizeof(file_name), "%s_%d.ivf", argv[2], i);
  747. outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
  748. if (!outfile[i]) die("Failed to open %s for writing", file_name);
  749. assert(outfile[i] != NULL);
  750. }
  751. // No spatial layers in this encoder.
  752. cfg.ss_number_layers = 1;
  753. // Initialize codec.
  754. #if CONFIG_VP9_HIGHBITDEPTH
  755. if (vpx_codec_enc_init(
  756. &codec, encoder->codec_interface(), &cfg,
  757. bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH))
  758. #else
  759. if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
  760. #endif // CONFIG_VP9_HIGHBITDEPTH
  761. die_codec(&codec, "Failed to initialize encoder");
  762. if (strncmp(encoder->name, "vp8", 3) == 0) {
  763. vpx_codec_control(&codec, VP8E_SET_CPUUSED, -speed);
  764. vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, kVp8DenoiserOff);
  765. vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
  766. vpx_codec_control(&codec, VP8E_SET_GF_CBR_BOOST_PCT, 0);
  767. #if ROI_MAP
  768. set_roi_map(encoder->name, &cfg, &roi);
  769. if (vpx_codec_control(&codec, VP8E_SET_ROI_MAP, &roi))
  770. die_codec(&codec, "Failed to set ROI map");
  771. #endif
  772. } else if (strncmp(encoder->name, "vp9", 3) == 0) {
  773. vpx_svc_extra_cfg_t svc_params;
  774. memset(&svc_params, 0, sizeof(svc_params));
  775. vpx_codec_control(&codec, VP8E_SET_CPUUSED, speed);
  776. vpx_codec_control(&codec, VP9E_SET_AQ_MODE, 3);
  777. vpx_codec_control(&codec, VP9E_SET_GF_CBR_BOOST_PCT, 0);
  778. vpx_codec_control(&codec, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
  779. vpx_codec_control(&codec, VP9E_SET_FRAME_PERIODIC_BOOST, 0);
  780. vpx_codec_control(&codec, VP9E_SET_NOISE_SENSITIVITY, kVp9DenoiserOff);
  781. vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
  782. vpx_codec_control(&codec, VP9E_SET_TUNE_CONTENT, 0);
  783. vpx_codec_control(&codec, VP9E_SET_TILE_COLUMNS, get_msb(cfg.g_threads));
  784. #if ROI_MAP
  785. set_roi_map(encoder->name, &cfg, &roi);
  786. if (vpx_codec_control(&codec, VP9E_SET_ROI_MAP, &roi))
  787. die_codec(&codec, "Failed to set ROI map");
  788. vpx_codec_control(&codec, VP9E_SET_AQ_MODE, 0);
  789. #endif
  790. // TODO(marpan/jianj): There is an issue with row-mt for low resolutons at
  791. // high speed settings, disable its use for those cases for now.
  792. if (cfg.g_threads > 1 && ((cfg.g_w > 320 && cfg.g_h > 240) || speed < 7))
  793. vpx_codec_control(&codec, VP9E_SET_ROW_MT, 1);
  794. else
  795. vpx_codec_control(&codec, VP9E_SET_ROW_MT, 0);
  796. if (vpx_codec_control(&codec, VP9E_SET_SVC, layering_mode > 0 ? 1 : 0))
  797. die_codec(&codec, "Failed to set SVC");
  798. for (i = 0; i < cfg.ts_number_layers; ++i) {
  799. svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
  800. svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
  801. }
  802. svc_params.scaling_factor_num[0] = cfg.g_h;
  803. svc_params.scaling_factor_den[0] = cfg.g_h;
  804. vpx_codec_control(&codec, VP9E_SET_SVC_PARAMETERS, &svc_params);
  805. }
  806. if (strncmp(encoder->name, "vp8", 3) == 0) {
  807. vpx_codec_control(&codec, VP8E_SET_SCREEN_CONTENT_MODE, 0);
  808. }
  809. vpx_codec_control(&codec, VP8E_SET_TOKEN_PARTITIONS, 1);
  810. // This controls the maximum target size of the key frame.
  811. // For generating smaller key frames, use a smaller max_intra_size_pct
  812. // value, like 100 or 200.
  813. {
  814. const int max_intra_size_pct = 1000;
  815. vpx_codec_control(&codec, VP8E_SET_MAX_INTRA_BITRATE_PCT,
  816. max_intra_size_pct);
  817. }
  818. frame_avail = 1;
  819. while (frame_avail || got_data) {
  820. struct vpx_usec_timer timer;
  821. vpx_codec_iter_t iter = NULL;
  822. const vpx_codec_cx_pkt_t *pkt;
  823. // Update the temporal layer_id. No spatial layers in this test.
  824. layer_id.spatial_layer_id = 0;
  825. layer_id.temporal_layer_id =
  826. cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
  827. layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id;
  828. if (strncmp(encoder->name, "vp9", 3) == 0) {
  829. vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
  830. } else if (strncmp(encoder->name, "vp8", 3) == 0) {
  831. vpx_codec_control(&codec, VP8E_SET_TEMPORAL_LAYER_ID,
  832. layer_id.temporal_layer_id);
  833. }
  834. flags = layer_flags[frame_cnt % flag_periodicity];
  835. if (layering_mode == 0) flags = 0;
  836. frame_avail = read_frame(&input_ctx, &raw);
  837. if (frame_avail) ++rc.layer_input_frames[layer_id.temporal_layer_id];
  838. vpx_usec_timer_start(&timer);
  839. if (vpx_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags,
  840. VPX_DL_REALTIME)) {
  841. die_codec(&codec, "Failed to encode frame");
  842. }
  843. vpx_usec_timer_mark(&timer);
  844. cx_time += vpx_usec_timer_elapsed(&timer);
  845. // Reset KF flag.
  846. if (layering_mode != 7) {
  847. layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
  848. }
  849. got_data = 0;
  850. while ((pkt = vpx_codec_get_cx_data(&codec, &iter))) {
  851. got_data = 1;
  852. switch (pkt->kind) {
  853. case VPX_CODEC_CX_FRAME_PKT:
  854. for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
  855. i < cfg.ts_number_layers; ++i) {
  856. vpx_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
  857. pkt->data.frame.sz, pts);
  858. ++rc.layer_tot_enc_frames[i];
  859. rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
  860. // Keep count of rate control stats per layer (for non-key frames).
  861. if (i == cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity] &&
  862. !(pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
  863. rc.layer_avg_frame_size[i] += 8.0 * pkt->data.frame.sz;
  864. rc.layer_avg_rate_mismatch[i] +=
  865. fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[i]) /
  866. rc.layer_pfb[i];
  867. ++rc.layer_enc_frames[i];
  868. }
  869. }
  870. // Update for short-time encoding bitrate states, for moving window
  871. // of size rc->window, shifted by rc->window / 2.
  872. // Ignore first window segment, due to key frame.
  873. if (frame_cnt > rc.window_size) {
  874. sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
  875. if (frame_cnt % rc.window_size == 0) {
  876. rc.window_count += 1;
  877. rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
  878. rc.variance_st_encoding_bitrate +=
  879. (sum_bitrate / rc.window_size) *
  880. (sum_bitrate / rc.window_size);
  881. sum_bitrate = 0.0;
  882. }
  883. }
  884. // Second shifted window.
  885. if (frame_cnt > rc.window_size + rc.window_size / 2) {
  886. sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
  887. if (frame_cnt > 2 * rc.window_size &&
  888. frame_cnt % rc.window_size == 0) {
  889. rc.window_count += 1;
  890. rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
  891. rc.variance_st_encoding_bitrate +=
  892. (sum_bitrate2 / rc.window_size) *
  893. (sum_bitrate2 / rc.window_size);
  894. sum_bitrate2 = 0.0;
  895. }
  896. }
  897. break;
  898. default: break;
  899. }
  900. }
  901. ++frame_cnt;
  902. pts += frame_duration;
  903. }
  904. close_input_file(&input_ctx);
  905. printout_rate_control_summary(&rc, &cfg, frame_cnt);
  906. printf("\n");
  907. printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
  908. frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
  909. 1000000 * (double)frame_cnt / (double)cx_time);
  910. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
  911. // Try to rewrite the output file headers with the actual frame count.
  912. for (i = 0; i < cfg.ts_number_layers; ++i) vpx_video_writer_close(outfile[i]);
  913. if (input_ctx.file_type != FILE_TYPE_Y4M) {
  914. vpx_img_free(&raw);
  915. }
  916. #if ROI_MAP
  917. free(roi.roi_map);
  918. #endif
  919. return EXIT_SUCCESS;
  920. }