vpx_temporal_svc_encoder.c 33 KB

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