vp9_cx_iface.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. /*
  2. * Copyright (c) 2010 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 <stdlib.h>
  11. #include <string.h>
  12. #include "./vpx_config.h"
  13. #include "vpx/vpx_encoder.h"
  14. #include "vpx_ports/vpx_once.h"
  15. #include "vpx_ports/system_state.h"
  16. #include "vpx/internal/vpx_codec_internal.h"
  17. #include "./vpx_version.h"
  18. #include "vp9/encoder/vp9_encoder.h"
  19. #include "vpx/vp8cx.h"
  20. #include "vp9/encoder/vp9_firstpass.h"
  21. #include "vp9/vp9_iface_common.h"
  22. struct vp9_extracfg {
  23. int cpu_used; // available cpu percentage in 1/16
  24. unsigned int enable_auto_alt_ref;
  25. unsigned int noise_sensitivity;
  26. unsigned int sharpness;
  27. unsigned int static_thresh;
  28. unsigned int tile_columns;
  29. unsigned int tile_rows;
  30. unsigned int arnr_max_frames;
  31. unsigned int arnr_strength;
  32. unsigned int min_gf_interval;
  33. unsigned int max_gf_interval;
  34. vp8e_tuning tuning;
  35. unsigned int cq_level; // constrained quality level
  36. unsigned int rc_max_intra_bitrate_pct;
  37. unsigned int rc_max_inter_bitrate_pct;
  38. unsigned int gf_cbr_boost_pct;
  39. unsigned int lossless;
  40. unsigned int target_level;
  41. unsigned int frame_parallel_decoding_mode;
  42. AQ_MODE aq_mode;
  43. int alt_ref_aq;
  44. unsigned int frame_periodic_boost;
  45. vpx_bit_depth_t bit_depth;
  46. vp9e_tune_content content;
  47. vpx_color_space_t color_space;
  48. vpx_color_range_t color_range;
  49. int render_width;
  50. int render_height;
  51. unsigned int row_mt;
  52. unsigned int motion_vector_unit_test;
  53. };
  54. static struct vp9_extracfg default_extra_cfg = {
  55. 0, // cpu_used
  56. 1, // enable_auto_alt_ref
  57. 0, // noise_sensitivity
  58. 0, // sharpness
  59. 0, // static_thresh
  60. 6, // tile_columns
  61. 0, // tile_rows
  62. 7, // arnr_max_frames
  63. 5, // arnr_strength
  64. 0, // min_gf_interval; 0 -> default decision
  65. 0, // max_gf_interval; 0 -> default decision
  66. VP8_TUNE_PSNR, // tuning
  67. 10, // cq_level
  68. 0, // rc_max_intra_bitrate_pct
  69. 0, // rc_max_inter_bitrate_pct
  70. 0, // gf_cbr_boost_pct
  71. 0, // lossless
  72. 255, // target_level
  73. 1, // frame_parallel_decoding_mode
  74. NO_AQ, // aq_mode
  75. 0, // alt_ref_aq
  76. 0, // frame_periodic_delta_q
  77. VPX_BITS_8, // Bit depth
  78. VP9E_CONTENT_DEFAULT, // content
  79. VPX_CS_UNKNOWN, // color space
  80. 0, // color range
  81. 0, // render width
  82. 0, // render height
  83. 0, // row_mt
  84. 0, // motion_vector_unit_test
  85. };
  86. struct vpx_codec_alg_priv {
  87. vpx_codec_priv_t base;
  88. vpx_codec_enc_cfg_t cfg;
  89. struct vp9_extracfg extra_cfg;
  90. VP9EncoderConfig oxcf;
  91. VP9_COMP *cpi;
  92. unsigned char *cx_data;
  93. size_t cx_data_sz;
  94. unsigned char *pending_cx_data;
  95. size_t pending_cx_data_sz;
  96. int pending_frame_count;
  97. size_t pending_frame_sizes[8];
  98. size_t pending_frame_magnitude;
  99. vpx_image_t preview_img;
  100. vpx_enc_frame_flags_t next_frame_flags;
  101. vp8_postproc_cfg_t preview_ppcfg;
  102. vpx_codec_pkt_list_decl(256) pkt_list;
  103. unsigned int fixed_kf_cntr;
  104. vpx_codec_priv_output_cx_pkt_cb_pair_t output_cx_pkt_cb;
  105. // BufferPool that holds all reference frames.
  106. BufferPool *buffer_pool;
  107. };
  108. static vpx_codec_err_t update_error_state(
  109. vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
  110. const vpx_codec_err_t res = error->error_code;
  111. if (res != VPX_CODEC_OK)
  112. ctx->base.err_detail = error->has_detail ? error->detail : NULL;
  113. return res;
  114. }
  115. #undef ERROR
  116. #define ERROR(str) \
  117. do { \
  118. ctx->base.err_detail = str; \
  119. return VPX_CODEC_INVALID_PARAM; \
  120. } while (0)
  121. #define RANGE_CHECK(p, memb, lo, hi) \
  122. do { \
  123. if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
  124. ERROR(#memb " out of range [" #lo ".." #hi "]"); \
  125. } while (0)
  126. #define RANGE_CHECK_HI(p, memb, hi) \
  127. do { \
  128. if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
  129. } while (0)
  130. #define RANGE_CHECK_LO(p, memb, lo) \
  131. do { \
  132. if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
  133. } while (0)
  134. #define RANGE_CHECK_BOOL(p, memb) \
  135. do { \
  136. if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
  137. } while (0)
  138. static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
  139. const vpx_codec_enc_cfg_t *cfg,
  140. const struct vp9_extracfg *extra_cfg) {
  141. RANGE_CHECK(cfg, g_w, 1, 65535); // 16 bits available
  142. RANGE_CHECK(cfg, g_h, 1, 65535); // 16 bits available
  143. RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
  144. RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
  145. RANGE_CHECK_HI(cfg, g_profile, 3);
  146. RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
  147. RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
  148. RANGE_CHECK_BOOL(extra_cfg, lossless);
  149. RANGE_CHECK_BOOL(extra_cfg, frame_parallel_decoding_mode);
  150. RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 2);
  151. RANGE_CHECK(extra_cfg, alt_ref_aq, 0, 1);
  152. RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
  153. RANGE_CHECK_HI(cfg, g_threads, 64);
  154. RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
  155. RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
  156. RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
  157. RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
  158. RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
  159. RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
  160. RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
  161. RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
  162. RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
  163. RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
  164. #if CONFIG_REALTIME_ONLY
  165. RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
  166. #else
  167. RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
  168. #endif
  169. RANGE_CHECK(extra_cfg, min_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
  170. RANGE_CHECK(extra_cfg, max_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
  171. if (extra_cfg->max_gf_interval > 0) {
  172. RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
  173. }
  174. if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
  175. RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
  176. (MAX_LAG_BUFFERS - 1));
  177. }
  178. // For formation of valid ARF groups lag_in _frames should be 0 or greater
  179. // than the max_gf_interval + 2
  180. if (cfg->g_lag_in_frames > 0 && extra_cfg->max_gf_interval > 0 &&
  181. cfg->g_lag_in_frames < extra_cfg->max_gf_interval + 2) {
  182. ERROR("Set lag in frames to 0 (low delay) or >= (max-gf-interval + 2)");
  183. }
  184. if (cfg->rc_resize_allowed == 1) {
  185. RANGE_CHECK(cfg, rc_scaled_width, 0, cfg->g_w);
  186. RANGE_CHECK(cfg, rc_scaled_height, 0, cfg->g_h);
  187. }
  188. RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
  189. RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
  190. {
  191. unsigned int level = extra_cfg->target_level;
  192. if (level != LEVEL_1 && level != LEVEL_1_1 && level != LEVEL_2 &&
  193. level != LEVEL_2_1 && level != LEVEL_3 && level != LEVEL_3_1 &&
  194. level != LEVEL_4 && level != LEVEL_4_1 && level != LEVEL_5 &&
  195. level != LEVEL_5_1 && level != LEVEL_5_2 && level != LEVEL_6 &&
  196. level != LEVEL_6_1 && level != LEVEL_6_2 && level != LEVEL_UNKNOWN &&
  197. level != LEVEL_AUTO && level != LEVEL_MAX)
  198. ERROR("target_level is invalid");
  199. }
  200. if (cfg->ss_number_layers * cfg->ts_number_layers > VPX_MAX_LAYERS)
  201. ERROR("ss_number_layers * ts_number_layers is out of range");
  202. if (cfg->ts_number_layers > 1) {
  203. unsigned int sl, tl;
  204. for (sl = 1; sl < cfg->ss_number_layers; ++sl) {
  205. for (tl = 1; tl < cfg->ts_number_layers; ++tl) {
  206. const int layer = LAYER_IDS_TO_IDX(sl, tl, cfg->ts_number_layers);
  207. if (cfg->layer_target_bitrate[layer] <
  208. cfg->layer_target_bitrate[layer - 1])
  209. ERROR("ts_target_bitrate entries are not increasing");
  210. }
  211. }
  212. RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
  213. for (tl = cfg->ts_number_layers - 2; tl > 0; --tl)
  214. if (cfg->ts_rate_decimator[tl - 1] != 2 * cfg->ts_rate_decimator[tl])
  215. ERROR("ts_rate_decimator factors are not powers of 2");
  216. }
  217. #if CONFIG_SPATIAL_SVC
  218. if ((cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) &&
  219. cfg->g_pass == VPX_RC_LAST_PASS) {
  220. unsigned int i, alt_ref_sum = 0;
  221. for (i = 0; i < cfg->ss_number_layers; ++i) {
  222. if (cfg->ss_enable_auto_alt_ref[i]) ++alt_ref_sum;
  223. }
  224. if (alt_ref_sum > REF_FRAMES - cfg->ss_number_layers)
  225. ERROR("Not enough ref buffers for svc alt ref frames");
  226. if (cfg->ss_number_layers * cfg->ts_number_layers > 3 &&
  227. cfg->g_error_resilient == 0)
  228. ERROR("Multiple frame context are not supported for more than 3 layers");
  229. }
  230. #endif
  231. // VP9 does not support a lower bound on the keyframe interval in
  232. // automatic keyframe placement mode.
  233. if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
  234. cfg->kf_min_dist > 0)
  235. ERROR(
  236. "kf_min_dist not supported in auto mode, use 0 "
  237. "or kf_max_dist instead.");
  238. RANGE_CHECK(extra_cfg, row_mt, 0, 1);
  239. RANGE_CHECK(extra_cfg, motion_vector_unit_test, 0, 2);
  240. RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, 2);
  241. RANGE_CHECK(extra_cfg, cpu_used, -8, 8);
  242. RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
  243. RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
  244. RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
  245. RANGE_CHECK_HI(extra_cfg, sharpness, 7);
  246. RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
  247. RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
  248. RANGE_CHECK(extra_cfg, cq_level, 0, 63);
  249. RANGE_CHECK(cfg, g_bit_depth, VPX_BITS_8, VPX_BITS_12);
  250. RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
  251. RANGE_CHECK(extra_cfg, content, VP9E_CONTENT_DEFAULT,
  252. VP9E_CONTENT_INVALID - 1);
  253. // TODO(yaowu): remove this when ssim tuning is implemented for vp9
  254. if (extra_cfg->tuning == VP8_TUNE_SSIM)
  255. ERROR("Option --tune=ssim is not currently supported in VP9.");
  256. #if !CONFIG_REALTIME_ONLY
  257. if (cfg->g_pass == VPX_RC_LAST_PASS) {
  258. const size_t packet_sz = sizeof(FIRSTPASS_STATS);
  259. const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
  260. const FIRSTPASS_STATS *stats;
  261. if (cfg->rc_twopass_stats_in.buf == NULL)
  262. ERROR("rc_twopass_stats_in.buf not set.");
  263. if (cfg->rc_twopass_stats_in.sz % packet_sz)
  264. ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
  265. if (cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) {
  266. int i;
  267. unsigned int n_packets_per_layer[VPX_SS_MAX_LAYERS] = { 0 };
  268. stats = cfg->rc_twopass_stats_in.buf;
  269. for (i = 0; i < n_packets; ++i) {
  270. const int layer_id = (int)stats[i].spatial_layer_id;
  271. if (layer_id >= 0 && layer_id < (int)cfg->ss_number_layers) {
  272. ++n_packets_per_layer[layer_id];
  273. }
  274. }
  275. for (i = 0; i < (int)cfg->ss_number_layers; ++i) {
  276. unsigned int layer_id;
  277. if (n_packets_per_layer[i] < 2) {
  278. ERROR(
  279. "rc_twopass_stats_in requires at least two packets for each "
  280. "layer.");
  281. }
  282. stats = (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf +
  283. n_packets - cfg->ss_number_layers + i;
  284. layer_id = (int)stats->spatial_layer_id;
  285. if (layer_id >= cfg->ss_number_layers ||
  286. (unsigned int)(stats->count + 0.5) !=
  287. n_packets_per_layer[layer_id] - 1)
  288. ERROR("rc_twopass_stats_in missing EOS stats packet");
  289. }
  290. } else {
  291. if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
  292. ERROR("rc_twopass_stats_in requires at least two packets.");
  293. stats =
  294. (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
  295. if ((int)(stats->count + 0.5) != n_packets - 1)
  296. ERROR("rc_twopass_stats_in missing EOS stats packet");
  297. }
  298. }
  299. #endif // !CONFIG_REALTIME_ONLY
  300. #if !CONFIG_VP9_HIGHBITDEPTH
  301. if (cfg->g_profile > (unsigned int)PROFILE_1) {
  302. ERROR("Profile > 1 not supported in this build configuration");
  303. }
  304. #endif
  305. if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
  306. cfg->g_bit_depth > VPX_BITS_8) {
  307. ERROR("Codec high bit-depth not supported in profile < 2");
  308. }
  309. if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
  310. ERROR("Source high bit-depth not supported in profile < 2");
  311. }
  312. if (cfg->g_profile > (unsigned int)PROFILE_1 &&
  313. cfg->g_bit_depth == VPX_BITS_8) {
  314. ERROR("Codec bit-depth 8 not supported in profile > 1");
  315. }
  316. RANGE_CHECK(extra_cfg, color_space, VPX_CS_UNKNOWN, VPX_CS_SRGB);
  317. RANGE_CHECK(extra_cfg, color_range, VPX_CR_STUDIO_RANGE, VPX_CR_FULL_RANGE);
  318. return VPX_CODEC_OK;
  319. }
  320. static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
  321. const vpx_image_t *img) {
  322. switch (img->fmt) {
  323. case VPX_IMG_FMT_YV12:
  324. case VPX_IMG_FMT_I420:
  325. case VPX_IMG_FMT_I42016: break;
  326. case VPX_IMG_FMT_I422:
  327. case VPX_IMG_FMT_I444:
  328. case VPX_IMG_FMT_I440:
  329. if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
  330. ERROR(
  331. "Invalid image format. I422, I444, I440 images are "
  332. "not supported in profile.");
  333. }
  334. break;
  335. case VPX_IMG_FMT_I42216:
  336. case VPX_IMG_FMT_I44416:
  337. case VPX_IMG_FMT_I44016:
  338. if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
  339. ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
  340. ERROR(
  341. "Invalid image format. 16-bit I422, I444, I440 images are "
  342. "not supported in profile.");
  343. }
  344. break;
  345. default:
  346. ERROR(
  347. "Invalid image format. Only YV12, I420, I422, I444 images are "
  348. "supported.");
  349. break;
  350. }
  351. if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
  352. ERROR("Image size must match encoder init configuration size");
  353. return VPX_CODEC_OK;
  354. }
  355. static int get_image_bps(const vpx_image_t *img) {
  356. switch (img->fmt) {
  357. case VPX_IMG_FMT_YV12:
  358. case VPX_IMG_FMT_I420: return 12;
  359. case VPX_IMG_FMT_I422: return 16;
  360. case VPX_IMG_FMT_I444: return 24;
  361. case VPX_IMG_FMT_I440: return 16;
  362. case VPX_IMG_FMT_I42016: return 24;
  363. case VPX_IMG_FMT_I42216: return 32;
  364. case VPX_IMG_FMT_I44416: return 48;
  365. case VPX_IMG_FMT_I44016: return 32;
  366. default: assert(0 && "Invalid image format"); break;
  367. }
  368. return 0;
  369. }
  370. // Modify the encoder config for the target level.
  371. static void config_target_level(VP9EncoderConfig *oxcf) {
  372. double max_average_bitrate; // in bits per second
  373. int max_over_shoot_pct;
  374. const int target_level_index = get_level_index(oxcf->target_level);
  375. vpx_clear_system_state();
  376. assert(target_level_index >= 0);
  377. assert(target_level_index < VP9_LEVELS);
  378. // Maximum target bit-rate is level_limit * 80%.
  379. max_average_bitrate =
  380. vp9_level_defs[target_level_index].average_bitrate * 800.0;
  381. if ((double)oxcf->target_bandwidth > max_average_bitrate)
  382. oxcf->target_bandwidth = (int64_t)(max_average_bitrate);
  383. if (oxcf->ss_number_layers == 1 && oxcf->pass != 0)
  384. oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
  385. // Adjust max over-shoot percentage.
  386. max_over_shoot_pct =
  387. (int)((max_average_bitrate * 1.10 - (double)oxcf->target_bandwidth) *
  388. 100 / (double)(oxcf->target_bandwidth));
  389. if (oxcf->over_shoot_pct > max_over_shoot_pct)
  390. oxcf->over_shoot_pct = max_over_shoot_pct;
  391. // Adjust worst allowed quantizer.
  392. oxcf->worst_allowed_q = vp9_quantizer_to_qindex(63);
  393. // Adjust minimum art-ref distance.
  394. // min_gf_interval should be no less than min_altref_distance + 1,
  395. // as the encoder may produce bitstream with alt-ref distance being
  396. // min_gf_interval - 1.
  397. if (oxcf->min_gf_interval <=
  398. (int)vp9_level_defs[target_level_index].min_altref_distance) {
  399. oxcf->min_gf_interval =
  400. (int)vp9_level_defs[target_level_index].min_altref_distance + 1;
  401. // If oxcf->max_gf_interval == 0, it will be assigned with a default value
  402. // in vp9_rc_set_gf_interval_range().
  403. if (oxcf->max_gf_interval != 0) {
  404. oxcf->max_gf_interval =
  405. VPXMAX(oxcf->max_gf_interval, oxcf->min_gf_interval);
  406. }
  407. }
  408. // Adjust maximum column tiles.
  409. if (vp9_level_defs[target_level_index].max_col_tiles <
  410. (1 << oxcf->tile_columns)) {
  411. while (oxcf->tile_columns > 0 &&
  412. vp9_level_defs[target_level_index].max_col_tiles <
  413. (1 << oxcf->tile_columns))
  414. --oxcf->tile_columns;
  415. }
  416. }
  417. static vpx_codec_err_t set_encoder_config(
  418. VP9EncoderConfig *oxcf, const vpx_codec_enc_cfg_t *cfg,
  419. const struct vp9_extracfg *extra_cfg) {
  420. const int is_vbr = cfg->rc_end_usage == VPX_VBR;
  421. int sl, tl;
  422. oxcf->profile = cfg->g_profile;
  423. oxcf->max_threads = (int)cfg->g_threads;
  424. oxcf->width = cfg->g_w;
  425. oxcf->height = cfg->g_h;
  426. oxcf->bit_depth = cfg->g_bit_depth;
  427. oxcf->input_bit_depth = cfg->g_input_bit_depth;
  428. // guess a frame rate if out of whack, use 30
  429. oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
  430. if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
  431. oxcf->mode = GOOD;
  432. switch (cfg->g_pass) {
  433. case VPX_RC_ONE_PASS: oxcf->pass = 0; break;
  434. case VPX_RC_FIRST_PASS: oxcf->pass = 1; break;
  435. case VPX_RC_LAST_PASS: oxcf->pass = 2; break;
  436. }
  437. oxcf->lag_in_frames =
  438. cfg->g_pass == VPX_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
  439. oxcf->rc_mode = cfg->rc_end_usage;
  440. // Convert target bandwidth from Kbit/s to Bit/s
  441. oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
  442. oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
  443. oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
  444. oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
  445. oxcf->best_allowed_q =
  446. extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
  447. oxcf->worst_allowed_q =
  448. extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
  449. oxcf->cq_level = vp9_quantizer_to_qindex(extra_cfg->cq_level);
  450. oxcf->fixed_q = -1;
  451. oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
  452. oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
  453. oxcf->scaled_frame_width = cfg->rc_scaled_width;
  454. oxcf->scaled_frame_height = cfg->rc_scaled_height;
  455. if (cfg->rc_resize_allowed == 1) {
  456. oxcf->resize_mode =
  457. (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0)
  458. ? RESIZE_DYNAMIC
  459. : RESIZE_FIXED;
  460. } else {
  461. oxcf->resize_mode = RESIZE_NONE;
  462. }
  463. oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
  464. oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
  465. oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
  466. oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
  467. oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
  468. oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
  469. oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
  470. oxcf->auto_key =
  471. cfg->kf_mode == VPX_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
  472. oxcf->key_freq = cfg->kf_max_dist;
  473. oxcf->speed = abs(extra_cfg->cpu_used);
  474. oxcf->encode_breakout = extra_cfg->static_thresh;
  475. oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
  476. oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
  477. oxcf->sharpness = extra_cfg->sharpness;
  478. oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
  479. #if CONFIG_FP_MB_STATS
  480. oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
  481. #endif
  482. oxcf->color_space = extra_cfg->color_space;
  483. oxcf->color_range = extra_cfg->color_range;
  484. oxcf->render_width = extra_cfg->render_width;
  485. oxcf->render_height = extra_cfg->render_height;
  486. oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
  487. oxcf->arnr_strength = extra_cfg->arnr_strength;
  488. oxcf->min_gf_interval = extra_cfg->min_gf_interval;
  489. oxcf->max_gf_interval = extra_cfg->max_gf_interval;
  490. oxcf->tuning = extra_cfg->tuning;
  491. oxcf->content = extra_cfg->content;
  492. oxcf->tile_columns = extra_cfg->tile_columns;
  493. // TODO(yunqing): The dependencies between row tiles cause error in multi-
  494. // threaded encoding. For now, tile_rows is forced to be 0 in this case.
  495. // The further fix can be done by adding synchronizations after a tile row
  496. // is encoded. But this will hurt multi-threaded encoder performance. So,
  497. // it is recommended to use tile-rows=0 while encoding with threads > 1.
  498. if (oxcf->max_threads > 1 && oxcf->tile_columns > 0)
  499. oxcf->tile_rows = 0;
  500. else
  501. oxcf->tile_rows = extra_cfg->tile_rows;
  502. oxcf->error_resilient_mode = cfg->g_error_resilient;
  503. oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
  504. oxcf->aq_mode = extra_cfg->aq_mode;
  505. oxcf->alt_ref_aq = extra_cfg->alt_ref_aq;
  506. oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
  507. oxcf->ss_number_layers = cfg->ss_number_layers;
  508. oxcf->ts_number_layers = cfg->ts_number_layers;
  509. oxcf->temporal_layering_mode =
  510. (enum vp9e_temporal_layering_mode)cfg->temporal_layering_mode;
  511. oxcf->target_level = extra_cfg->target_level;
  512. oxcf->row_mt = extra_cfg->row_mt;
  513. oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
  514. for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
  515. #if CONFIG_SPATIAL_SVC
  516. oxcf->ss_enable_auto_arf[sl] = cfg->ss_enable_auto_alt_ref[sl];
  517. #endif
  518. for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
  519. oxcf->layer_target_bitrate[sl * oxcf->ts_number_layers + tl] =
  520. 1000 * cfg->layer_target_bitrate[sl * oxcf->ts_number_layers + tl];
  521. }
  522. }
  523. if (oxcf->ss_number_layers == 1 && oxcf->pass != 0) {
  524. oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
  525. #if CONFIG_SPATIAL_SVC
  526. oxcf->ss_enable_auto_arf[0] = extra_cfg->enable_auto_alt_ref;
  527. #endif
  528. }
  529. if (oxcf->ts_number_layers > 1) {
  530. for (tl = 0; tl < VPX_TS_MAX_LAYERS; ++tl) {
  531. oxcf->ts_rate_decimator[tl] =
  532. cfg->ts_rate_decimator[tl] ? cfg->ts_rate_decimator[tl] : 1;
  533. }
  534. } else if (oxcf->ts_number_layers == 1) {
  535. oxcf->ts_rate_decimator[0] = 1;
  536. }
  537. if (get_level_index(oxcf->target_level) >= 0) config_target_level(oxcf);
  538. /*
  539. printf("Current VP9 Settings: \n");
  540. printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
  541. printf("target_level: %d\n", oxcf->target_level);
  542. printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
  543. printf("sharpness: %d\n", oxcf->sharpness);
  544. printf("cpu_used: %d\n", oxcf->cpu_used);
  545. printf("Mode: %d\n", oxcf->mode);
  546. printf("auto_key: %d\n", oxcf->auto_key);
  547. printf("key_freq: %d\n", oxcf->key_freq);
  548. printf("end_usage: %d\n", oxcf->end_usage);
  549. printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
  550. printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
  551. printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
  552. printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
  553. printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
  554. printf("fixed_q: %d\n", oxcf->fixed_q);
  555. printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
  556. printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
  557. printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
  558. printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
  559. printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
  560. printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
  561. printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
  562. printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
  563. printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
  564. printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
  565. printf("Version: %d\n", oxcf->Version);
  566. printf("encode_breakout: %d\n", oxcf->encode_breakout);
  567. printf("error resilient: %d\n", oxcf->error_resilient_mode);
  568. printf("frame parallel detokenization: %d\n",
  569. oxcf->frame_parallel_decoding_mode);
  570. */
  571. return VPX_CODEC_OK;
  572. }
  573. static vpx_codec_err_t encoder_set_config(vpx_codec_alg_priv_t *ctx,
  574. const vpx_codec_enc_cfg_t *cfg) {
  575. vpx_codec_err_t res;
  576. int force_key = 0;
  577. if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
  578. if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
  579. ERROR("Cannot change width or height after initialization");
  580. if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
  581. (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
  582. (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
  583. force_key = 1;
  584. }
  585. // Prevent increasing lag_in_frames. This check is stricter than it needs
  586. // to be -- the limit is not increasing past the first lag_in_frames
  587. // value, but we don't track the initial config, only the last successful
  588. // config.
  589. if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
  590. ERROR("Cannot increase lag_in_frames");
  591. res = validate_config(ctx, cfg, &ctx->extra_cfg);
  592. if (res == VPX_CODEC_OK) {
  593. ctx->cfg = *cfg;
  594. set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
  595. // On profile change, request a key frame
  596. force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
  597. vp9_change_config(ctx->cpi, &ctx->oxcf);
  598. }
  599. if (force_key) ctx->next_frame_flags |= VPX_EFLAG_FORCE_KF;
  600. return res;
  601. }
  602. static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
  603. va_list args) {
  604. int *const arg = va_arg(args, int *);
  605. if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
  606. *arg = vp9_get_quantizer(ctx->cpi);
  607. return VPX_CODEC_OK;
  608. }
  609. static vpx_codec_err_t ctrl_get_quantizer64(vpx_codec_alg_priv_t *ctx,
  610. va_list args) {
  611. int *const arg = va_arg(args, int *);
  612. if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
  613. *arg = vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi));
  614. return VPX_CODEC_OK;
  615. }
  616. static vpx_codec_err_t update_extra_cfg(vpx_codec_alg_priv_t *ctx,
  617. const struct vp9_extracfg *extra_cfg) {
  618. const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
  619. if (res == VPX_CODEC_OK) {
  620. ctx->extra_cfg = *extra_cfg;
  621. set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
  622. vp9_change_config(ctx->cpi, &ctx->oxcf);
  623. }
  624. return res;
  625. }
  626. static vpx_codec_err_t ctrl_set_cpuused(vpx_codec_alg_priv_t *ctx,
  627. va_list args) {
  628. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  629. extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
  630. return update_extra_cfg(ctx, &extra_cfg);
  631. }
  632. static vpx_codec_err_t ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
  633. va_list args) {
  634. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  635. extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
  636. return update_extra_cfg(ctx, &extra_cfg);
  637. }
  638. static vpx_codec_err_t ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
  639. va_list args) {
  640. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  641. extra_cfg.noise_sensitivity = CAST(VP9E_SET_NOISE_SENSITIVITY, args);
  642. return update_extra_cfg(ctx, &extra_cfg);
  643. }
  644. static vpx_codec_err_t ctrl_set_sharpness(vpx_codec_alg_priv_t *ctx,
  645. va_list args) {
  646. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  647. extra_cfg.sharpness = CAST(VP8E_SET_SHARPNESS, args);
  648. return update_extra_cfg(ctx, &extra_cfg);
  649. }
  650. static vpx_codec_err_t ctrl_set_static_thresh(vpx_codec_alg_priv_t *ctx,
  651. va_list args) {
  652. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  653. extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
  654. return update_extra_cfg(ctx, &extra_cfg);
  655. }
  656. static vpx_codec_err_t ctrl_set_tile_columns(vpx_codec_alg_priv_t *ctx,
  657. va_list args) {
  658. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  659. extra_cfg.tile_columns = CAST(VP9E_SET_TILE_COLUMNS, args);
  660. return update_extra_cfg(ctx, &extra_cfg);
  661. }
  662. static vpx_codec_err_t ctrl_set_tile_rows(vpx_codec_alg_priv_t *ctx,
  663. va_list args) {
  664. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  665. extra_cfg.tile_rows = CAST(VP9E_SET_TILE_ROWS, args);
  666. return update_extra_cfg(ctx, &extra_cfg);
  667. }
  668. static vpx_codec_err_t ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
  669. va_list args) {
  670. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  671. extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
  672. return update_extra_cfg(ctx, &extra_cfg);
  673. }
  674. static vpx_codec_err_t ctrl_set_arnr_strength(vpx_codec_alg_priv_t *ctx,
  675. va_list args) {
  676. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  677. extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
  678. return update_extra_cfg(ctx, &extra_cfg);
  679. }
  680. static vpx_codec_err_t ctrl_set_arnr_type(vpx_codec_alg_priv_t *ctx,
  681. va_list args) {
  682. (void)ctx;
  683. (void)args;
  684. return VPX_CODEC_OK;
  685. }
  686. static vpx_codec_err_t ctrl_set_tuning(vpx_codec_alg_priv_t *ctx,
  687. va_list args) {
  688. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  689. extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
  690. return update_extra_cfg(ctx, &extra_cfg);
  691. }
  692. static vpx_codec_err_t ctrl_set_cq_level(vpx_codec_alg_priv_t *ctx,
  693. va_list args) {
  694. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  695. extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
  696. return update_extra_cfg(ctx, &extra_cfg);
  697. }
  698. static vpx_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
  699. vpx_codec_alg_priv_t *ctx, va_list args) {
  700. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  701. extra_cfg.rc_max_intra_bitrate_pct =
  702. CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
  703. return update_extra_cfg(ctx, &extra_cfg);
  704. }
  705. static vpx_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
  706. vpx_codec_alg_priv_t *ctx, va_list args) {
  707. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  708. extra_cfg.rc_max_inter_bitrate_pct =
  709. CAST(VP8E_SET_MAX_INTER_BITRATE_PCT, args);
  710. return update_extra_cfg(ctx, &extra_cfg);
  711. }
  712. static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t *ctx,
  713. va_list args) {
  714. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  715. extra_cfg.gf_cbr_boost_pct = CAST(VP9E_SET_GF_CBR_BOOST_PCT, args);
  716. return update_extra_cfg(ctx, &extra_cfg);
  717. }
  718. static vpx_codec_err_t ctrl_set_lossless(vpx_codec_alg_priv_t *ctx,
  719. va_list args) {
  720. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  721. extra_cfg.lossless = CAST(VP9E_SET_LOSSLESS, args);
  722. return update_extra_cfg(ctx, &extra_cfg);
  723. }
  724. static vpx_codec_err_t ctrl_set_frame_parallel_decoding_mode(
  725. vpx_codec_alg_priv_t *ctx, va_list args) {
  726. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  727. extra_cfg.frame_parallel_decoding_mode =
  728. CAST(VP9E_SET_FRAME_PARALLEL_DECODING, args);
  729. return update_extra_cfg(ctx, &extra_cfg);
  730. }
  731. static vpx_codec_err_t ctrl_set_aq_mode(vpx_codec_alg_priv_t *ctx,
  732. va_list args) {
  733. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  734. extra_cfg.aq_mode = CAST(VP9E_SET_AQ_MODE, args);
  735. return update_extra_cfg(ctx, &extra_cfg);
  736. }
  737. static vpx_codec_err_t ctrl_set_alt_ref_aq(vpx_codec_alg_priv_t *ctx,
  738. va_list args) {
  739. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  740. extra_cfg.alt_ref_aq = CAST(VP9E_SET_ALT_REF_AQ, args);
  741. return update_extra_cfg(ctx, &extra_cfg);
  742. }
  743. static vpx_codec_err_t ctrl_set_min_gf_interval(vpx_codec_alg_priv_t *ctx,
  744. va_list args) {
  745. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  746. extra_cfg.min_gf_interval = CAST(VP9E_SET_MIN_GF_INTERVAL, args);
  747. return update_extra_cfg(ctx, &extra_cfg);
  748. }
  749. static vpx_codec_err_t ctrl_set_max_gf_interval(vpx_codec_alg_priv_t *ctx,
  750. va_list args) {
  751. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  752. extra_cfg.max_gf_interval = CAST(VP9E_SET_MAX_GF_INTERVAL, args);
  753. return update_extra_cfg(ctx, &extra_cfg);
  754. }
  755. static vpx_codec_err_t ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t *ctx,
  756. va_list args) {
  757. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  758. extra_cfg.frame_periodic_boost = CAST(VP9E_SET_FRAME_PERIODIC_BOOST, args);
  759. return update_extra_cfg(ctx, &extra_cfg);
  760. }
  761. static vpx_codec_err_t ctrl_set_target_level(vpx_codec_alg_priv_t *ctx,
  762. va_list args) {
  763. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  764. extra_cfg.target_level = CAST(VP9E_SET_TARGET_LEVEL, args);
  765. return update_extra_cfg(ctx, &extra_cfg);
  766. }
  767. static vpx_codec_err_t ctrl_set_row_mt(vpx_codec_alg_priv_t *ctx,
  768. va_list args) {
  769. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  770. extra_cfg.row_mt = CAST(VP9E_SET_ROW_MT, args);
  771. return update_extra_cfg(ctx, &extra_cfg);
  772. }
  773. static vpx_codec_err_t ctrl_enable_motion_vector_unit_test(
  774. vpx_codec_alg_priv_t *ctx, va_list args) {
  775. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  776. extra_cfg.motion_vector_unit_test =
  777. CAST(VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
  778. return update_extra_cfg(ctx, &extra_cfg);
  779. }
  780. static vpx_codec_err_t ctrl_get_level(vpx_codec_alg_priv_t *ctx, va_list args) {
  781. int *const arg = va_arg(args, int *);
  782. if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
  783. *arg = (int)vp9_get_level(&ctx->cpi->level_info.level_spec);
  784. return VPX_CODEC_OK;
  785. }
  786. static vpx_codec_err_t encoder_init(vpx_codec_ctx_t *ctx,
  787. vpx_codec_priv_enc_mr_cfg_t *data) {
  788. vpx_codec_err_t res = VPX_CODEC_OK;
  789. (void)data;
  790. if (ctx->priv == NULL) {
  791. vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
  792. if (priv == NULL) return VPX_CODEC_MEM_ERROR;
  793. ctx->priv = (vpx_codec_priv_t *)priv;
  794. ctx->priv->init_flags = ctx->init_flags;
  795. ctx->priv->enc.total_encoders = 1;
  796. priv->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
  797. if (priv->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
  798. if (ctx->config.enc) {
  799. // Update the reference to the config structure to an internal copy.
  800. priv->cfg = *ctx->config.enc;
  801. ctx->config.enc = &priv->cfg;
  802. }
  803. priv->extra_cfg = default_extra_cfg;
  804. once(vp9_initialize_enc);
  805. res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
  806. if (res == VPX_CODEC_OK) {
  807. set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
  808. #if CONFIG_VP9_HIGHBITDEPTH
  809. priv->oxcf.use_highbitdepth =
  810. (ctx->init_flags & VPX_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
  811. #endif
  812. priv->cpi = vp9_create_compressor(&priv->oxcf, priv->buffer_pool);
  813. if (priv->cpi == NULL)
  814. res = VPX_CODEC_MEM_ERROR;
  815. else
  816. priv->cpi->output_pkt_list = &priv->pkt_list.head;
  817. }
  818. }
  819. return res;
  820. }
  821. static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
  822. free(ctx->cx_data);
  823. vp9_remove_compressor(ctx->cpi);
  824. vpx_free(ctx->buffer_pool);
  825. vpx_free(ctx);
  826. return VPX_CODEC_OK;
  827. }
  828. static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
  829. unsigned long duration,
  830. unsigned long deadline) {
  831. MODE new_mode = BEST;
  832. #if CONFIG_REALTIME_ONLY
  833. (void)duration;
  834. deadline = VPX_DL_REALTIME;
  835. #else
  836. switch (ctx->cfg.g_pass) {
  837. case VPX_RC_ONE_PASS:
  838. if (deadline > 0) {
  839. const vpx_codec_enc_cfg_t *const cfg = &ctx->cfg;
  840. // Convert duration parameter from stream timebase to microseconds.
  841. const uint64_t duration_us = (uint64_t)duration * 1000000 *
  842. (uint64_t)cfg->g_timebase.num /
  843. (uint64_t)cfg->g_timebase.den;
  844. // If the deadline is more that the duration this frame is to be shown,
  845. // use good quality mode. Otherwise use realtime mode.
  846. new_mode = (deadline > duration_us) ? GOOD : REALTIME;
  847. } else {
  848. new_mode = BEST;
  849. }
  850. break;
  851. case VPX_RC_FIRST_PASS: break;
  852. case VPX_RC_LAST_PASS: new_mode = deadline > 0 ? GOOD : BEST; break;
  853. }
  854. #endif // CONFIG_REALTIME_ONLY
  855. if (deadline == VPX_DL_REALTIME) {
  856. ctx->oxcf.pass = 0;
  857. new_mode = REALTIME;
  858. }
  859. if (ctx->oxcf.mode != new_mode) {
  860. ctx->oxcf.mode = new_mode;
  861. vp9_change_config(ctx->cpi, &ctx->oxcf);
  862. }
  863. }
  864. // Turn on to test if supplemental superframe data breaks decoding
  865. // #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
  866. static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
  867. uint8_t marker = 0xc0;
  868. unsigned int mask;
  869. int mag, index_sz;
  870. assert(ctx->pending_frame_count);
  871. assert(ctx->pending_frame_count <= 8);
  872. // Add the number of frames to the marker byte
  873. marker |= ctx->pending_frame_count - 1;
  874. // Choose the magnitude
  875. for (mag = 0, mask = 0xff; mag < 4; mag++) {
  876. if (ctx->pending_frame_magnitude < mask) break;
  877. mask <<= 8;
  878. mask |= 0xff;
  879. }
  880. marker |= mag << 3;
  881. // Write the index
  882. index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
  883. if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
  884. uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
  885. int i, j;
  886. #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
  887. uint8_t marker_test = 0xc0;
  888. int mag_test = 2; // 1 - 4
  889. int frames_test = 4; // 1 - 8
  890. int index_sz_test = 2 + mag_test * frames_test;
  891. marker_test |= frames_test - 1;
  892. marker_test |= (mag_test - 1) << 3;
  893. *x++ = marker_test;
  894. for (i = 0; i < mag_test * frames_test; ++i)
  895. *x++ = 0; // fill up with arbitrary data
  896. *x++ = marker_test;
  897. ctx->pending_cx_data_sz += index_sz_test;
  898. printf("Added supplemental superframe data\n");
  899. #endif
  900. *x++ = marker;
  901. for (i = 0; i < ctx->pending_frame_count; i++) {
  902. unsigned int this_sz = (unsigned int)ctx->pending_frame_sizes[i];
  903. for (j = 0; j <= mag; j++) {
  904. *x++ = this_sz & 0xff;
  905. this_sz >>= 8;
  906. }
  907. }
  908. *x++ = marker;
  909. ctx->pending_cx_data_sz += index_sz;
  910. #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
  911. index_sz += index_sz_test;
  912. #endif
  913. }
  914. return index_sz;
  915. }
  916. static int64_t timebase_units_to_ticks(const vpx_rational_t *timebase,
  917. int64_t n) {
  918. return n * TICKS_PER_SEC * timebase->num / timebase->den;
  919. }
  920. static int64_t ticks_to_timebase_units(const vpx_rational_t *timebase,
  921. int64_t n) {
  922. const int64_t round = (int64_t)TICKS_PER_SEC * timebase->num / 2 - 1;
  923. return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
  924. }
  925. static vpx_codec_frame_flags_t get_frame_pkt_flags(const VP9_COMP *cpi,
  926. unsigned int lib_flags) {
  927. vpx_codec_frame_flags_t flags = lib_flags << 16;
  928. if (lib_flags & FRAMEFLAGS_KEY ||
  929. (cpi->use_svc &&
  930. cpi->svc
  931. .layer_context[cpi->svc.spatial_layer_id *
  932. cpi->svc.number_temporal_layers +
  933. cpi->svc.temporal_layer_id]
  934. .is_key_frame))
  935. flags |= VPX_FRAME_IS_KEY;
  936. if (cpi->droppable) flags |= VPX_FRAME_IS_DROPPABLE;
  937. return flags;
  938. }
  939. const size_t kMinCompressedSize = 8192;
  940. static vpx_codec_err_t encoder_encode(vpx_codec_alg_priv_t *ctx,
  941. const vpx_image_t *img,
  942. vpx_codec_pts_t pts,
  943. unsigned long duration,
  944. vpx_enc_frame_flags_t enc_flags,
  945. unsigned long deadline) {
  946. volatile vpx_codec_err_t res = VPX_CODEC_OK;
  947. volatile vpx_enc_frame_flags_t flags = enc_flags;
  948. VP9_COMP *const cpi = ctx->cpi;
  949. const vpx_rational_t *const timebase = &ctx->cfg.g_timebase;
  950. size_t data_sz;
  951. if (cpi == NULL) return VPX_CODEC_INVALID_PARAM;
  952. if (cpi->oxcf.pass == 2 && cpi->level_constraint.level_index >= 0 &&
  953. !cpi->level_constraint.rc_config_updated) {
  954. SVC *const svc = &cpi->svc;
  955. const int is_two_pass_svc =
  956. (svc->number_spatial_layers > 1) || (svc->number_temporal_layers > 1);
  957. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  958. TWO_PASS *const twopass = &cpi->twopass;
  959. FIRSTPASS_STATS *stats = &twopass->total_stats;
  960. if (is_two_pass_svc) {
  961. const double frame_rate = 10000000.0 * stats->count / stats->duration;
  962. vp9_update_spatial_layer_framerate(cpi, frame_rate);
  963. twopass->bits_left =
  964. (int64_t)(stats->duration *
  965. svc->layer_context[svc->spatial_layer_id].target_bandwidth /
  966. 10000000.0);
  967. } else {
  968. twopass->bits_left =
  969. (int64_t)(stats->duration * oxcf->target_bandwidth / 10000000.0);
  970. }
  971. cpi->level_constraint.rc_config_updated = 1;
  972. }
  973. if (img != NULL) {
  974. res = validate_img(ctx, img);
  975. if (res == VPX_CODEC_OK) {
  976. // There's no codec control for multiple alt-refs so check the encoder
  977. // instance for its status to determine the compressed data size.
  978. data_sz = ctx->cfg.g_w * ctx->cfg.g_h * get_image_bps(img) / 8 *
  979. (cpi->multi_arf_allowed ? 8 : 2);
  980. if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
  981. if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
  982. ctx->cx_data_sz = data_sz;
  983. free(ctx->cx_data);
  984. ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
  985. if (ctx->cx_data == NULL) {
  986. return VPX_CODEC_MEM_ERROR;
  987. }
  988. }
  989. }
  990. }
  991. pick_quickcompress_mode(ctx, duration, deadline);
  992. vpx_codec_pkt_list_init(&ctx->pkt_list);
  993. // Handle Flags
  994. if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
  995. ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
  996. ctx->base.err_detail = "Conflicting flags.";
  997. return VPX_CODEC_INVALID_PARAM;
  998. }
  999. if (setjmp(cpi->common.error.jmp)) {
  1000. cpi->common.error.setjmp = 0;
  1001. res = update_error_state(ctx, &cpi->common.error);
  1002. vpx_clear_system_state();
  1003. return res;
  1004. }
  1005. cpi->common.error.setjmp = 1;
  1006. if (res == VPX_CODEC_OK) vp9_apply_encoding_flags(cpi, flags);
  1007. // Handle fixed keyframe intervals
  1008. if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
  1009. ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
  1010. if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
  1011. flags |= VPX_EFLAG_FORCE_KF;
  1012. ctx->fixed_kf_cntr = 1;
  1013. }
  1014. }
  1015. if (res == VPX_CODEC_OK) {
  1016. unsigned int lib_flags = 0;
  1017. YV12_BUFFER_CONFIG sd;
  1018. int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
  1019. int64_t dst_end_time_stamp =
  1020. timebase_units_to_ticks(timebase, pts + duration);
  1021. size_t size, cx_data_sz;
  1022. unsigned char *cx_data;
  1023. // Set up internal flags
  1024. if (ctx->base.init_flags & VPX_CODEC_USE_PSNR) cpi->b_calculate_psnr = 1;
  1025. if (img != NULL) {
  1026. res = image2yuvconfig(img, &sd);
  1027. // Store the original flags in to the frame buffer. Will extract the
  1028. // key frame flag when we actually encode this frame.
  1029. if (vp9_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
  1030. dst_time_stamp, dst_end_time_stamp)) {
  1031. res = update_error_state(ctx, &cpi->common.error);
  1032. }
  1033. ctx->next_frame_flags = 0;
  1034. }
  1035. cx_data = ctx->cx_data;
  1036. cx_data_sz = ctx->cx_data_sz;
  1037. /* Any pending invisible frames? */
  1038. if (ctx->pending_cx_data) {
  1039. memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
  1040. ctx->pending_cx_data = cx_data;
  1041. cx_data += ctx->pending_cx_data_sz;
  1042. cx_data_sz -= ctx->pending_cx_data_sz;
  1043. /* TODO: this is a minimal check, the underlying codec doesn't respect
  1044. * the buffer size anyway.
  1045. */
  1046. if (cx_data_sz < ctx->cx_data_sz / 2) {
  1047. vpx_internal_error(&cpi->common.error, VPX_CODEC_ERROR,
  1048. "Compressed data buffer too small");
  1049. return VPX_CODEC_ERROR;
  1050. }
  1051. }
  1052. while (cx_data_sz >= ctx->cx_data_sz / 2 &&
  1053. -1 != vp9_get_compressed_data(cpi, &lib_flags, &size, cx_data,
  1054. &dst_time_stamp, &dst_end_time_stamp,
  1055. !img)) {
  1056. if (size) {
  1057. vpx_codec_cx_pkt_t pkt;
  1058. #if CONFIG_SPATIAL_SVC
  1059. if (cpi->use_svc)
  1060. cpi->svc
  1061. .layer_context[cpi->svc.spatial_layer_id *
  1062. cpi->svc.number_temporal_layers]
  1063. .layer_size += size;
  1064. #endif
  1065. // Pack invisible frames with the next visible frame
  1066. if (!cpi->common.show_frame ||
  1067. (cpi->use_svc &&
  1068. cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)) {
  1069. if (ctx->pending_cx_data == 0) ctx->pending_cx_data = cx_data;
  1070. ctx->pending_cx_data_sz += size;
  1071. ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
  1072. ctx->pending_frame_magnitude |= size;
  1073. cx_data += size;
  1074. cx_data_sz -= size;
  1075. if (ctx->output_cx_pkt_cb.output_cx_pkt) {
  1076. pkt.kind = VPX_CODEC_CX_FRAME_PKT;
  1077. pkt.data.frame.pts =
  1078. ticks_to_timebase_units(timebase, dst_time_stamp);
  1079. pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
  1080. timebase, dst_end_time_stamp - dst_time_stamp);
  1081. pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
  1082. pkt.data.frame.buf = ctx->pending_cx_data;
  1083. pkt.data.frame.sz = size;
  1084. ctx->pending_cx_data = NULL;
  1085. ctx->pending_cx_data_sz = 0;
  1086. ctx->pending_frame_count = 0;
  1087. ctx->pending_frame_magnitude = 0;
  1088. ctx->output_cx_pkt_cb.output_cx_pkt(
  1089. &pkt, ctx->output_cx_pkt_cb.user_priv);
  1090. }
  1091. continue;
  1092. }
  1093. // Add the frame packet to the list of returned packets.
  1094. pkt.kind = VPX_CODEC_CX_FRAME_PKT;
  1095. pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
  1096. pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
  1097. timebase, dst_end_time_stamp - dst_time_stamp);
  1098. pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
  1099. if (ctx->pending_cx_data) {
  1100. ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
  1101. ctx->pending_frame_magnitude |= size;
  1102. ctx->pending_cx_data_sz += size;
  1103. // write the superframe only for the case when
  1104. if (!ctx->output_cx_pkt_cb.output_cx_pkt)
  1105. size += write_superframe_index(ctx);
  1106. pkt.data.frame.buf = ctx->pending_cx_data;
  1107. pkt.data.frame.sz = ctx->pending_cx_data_sz;
  1108. ctx->pending_cx_data = NULL;
  1109. ctx->pending_cx_data_sz = 0;
  1110. ctx->pending_frame_count = 0;
  1111. ctx->pending_frame_magnitude = 0;
  1112. } else {
  1113. pkt.data.frame.buf = cx_data;
  1114. pkt.data.frame.sz = size;
  1115. }
  1116. pkt.data.frame.partition_id = -1;
  1117. if (ctx->output_cx_pkt_cb.output_cx_pkt)
  1118. ctx->output_cx_pkt_cb.output_cx_pkt(&pkt,
  1119. ctx->output_cx_pkt_cb.user_priv);
  1120. else
  1121. vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
  1122. cx_data += size;
  1123. cx_data_sz -= size;
  1124. #if VPX_ENCODER_ABI_VERSION > (5 + VPX_CODEC_ABI_VERSION)
  1125. #if CONFIG_SPATIAL_SVC
  1126. if (cpi->use_svc && !ctx->output_cx_pkt_cb.output_cx_pkt) {
  1127. vpx_codec_cx_pkt_t pkt_sizes, pkt_psnr;
  1128. int sl;
  1129. vp9_zero(pkt_sizes);
  1130. vp9_zero(pkt_psnr);
  1131. pkt_sizes.kind = VPX_CODEC_SPATIAL_SVC_LAYER_SIZES;
  1132. pkt_psnr.kind = VPX_CODEC_SPATIAL_SVC_LAYER_PSNR;
  1133. for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
  1134. LAYER_CONTEXT *lc =
  1135. &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers];
  1136. pkt_sizes.data.layer_sizes[sl] = lc->layer_size;
  1137. pkt_psnr.data.layer_psnr[sl] = lc->psnr_pkt;
  1138. lc->layer_size = 0;
  1139. }
  1140. vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_sizes);
  1141. vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_psnr);
  1142. }
  1143. #endif
  1144. #endif
  1145. if (is_one_pass_cbr_svc(cpi) &&
  1146. (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
  1147. // Encoded all spatial layers; exit loop.
  1148. break;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. cpi->common.error.setjmp = 0;
  1154. return res;
  1155. }
  1156. static const vpx_codec_cx_pkt_t *encoder_get_cxdata(vpx_codec_alg_priv_t *ctx,
  1157. vpx_codec_iter_t *iter) {
  1158. return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
  1159. }
  1160. static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
  1161. va_list args) {
  1162. vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
  1163. if (frame != NULL) {
  1164. YV12_BUFFER_CONFIG sd;
  1165. image2yuvconfig(&frame->img, &sd);
  1166. vp9_set_reference_enc(ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type),
  1167. &sd);
  1168. return VPX_CODEC_OK;
  1169. } else {
  1170. return VPX_CODEC_INVALID_PARAM;
  1171. }
  1172. }
  1173. static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
  1174. va_list args) {
  1175. vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
  1176. if (frame != NULL) {
  1177. YV12_BUFFER_CONFIG sd;
  1178. image2yuvconfig(&frame->img, &sd);
  1179. vp9_copy_reference_enc(ctx->cpi,
  1180. ref_frame_to_vp9_reframe(frame->frame_type), &sd);
  1181. return VPX_CODEC_OK;
  1182. } else {
  1183. return VPX_CODEC_INVALID_PARAM;
  1184. }
  1185. }
  1186. static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
  1187. va_list args) {
  1188. vp9_ref_frame_t *const frame = va_arg(args, vp9_ref_frame_t *);
  1189. if (frame != NULL) {
  1190. YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
  1191. if (fb == NULL) return VPX_CODEC_ERROR;
  1192. yuvconfig2image(&frame->img, fb, NULL);
  1193. return VPX_CODEC_OK;
  1194. } else {
  1195. return VPX_CODEC_INVALID_PARAM;
  1196. }
  1197. }
  1198. static vpx_codec_err_t ctrl_set_previewpp(vpx_codec_alg_priv_t *ctx,
  1199. va_list args) {
  1200. #if CONFIG_VP9_POSTPROC
  1201. vp8_postproc_cfg_t *config = va_arg(args, vp8_postproc_cfg_t *);
  1202. if (config != NULL) {
  1203. ctx->preview_ppcfg = *config;
  1204. return VPX_CODEC_OK;
  1205. } else {
  1206. return VPX_CODEC_INVALID_PARAM;
  1207. }
  1208. #else
  1209. (void)ctx;
  1210. (void)args;
  1211. return VPX_CODEC_INCAPABLE;
  1212. #endif
  1213. }
  1214. static vpx_image_t *encoder_get_preview(vpx_codec_alg_priv_t *ctx) {
  1215. YV12_BUFFER_CONFIG sd;
  1216. vp9_ppflags_t flags;
  1217. vp9_zero(flags);
  1218. if (ctx->preview_ppcfg.post_proc_flag) {
  1219. flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
  1220. flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
  1221. flags.noise_level = ctx->preview_ppcfg.noise_level;
  1222. }
  1223. if (vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags) == 0) {
  1224. yuvconfig2image(&ctx->preview_img, &sd, NULL);
  1225. return &ctx->preview_img;
  1226. } else {
  1227. return NULL;
  1228. }
  1229. }
  1230. static vpx_codec_err_t ctrl_set_roi_map(vpx_codec_alg_priv_t *ctx,
  1231. va_list args) {
  1232. (void)ctx;
  1233. (void)args;
  1234. // TODO(yaowu): Need to re-implement and test for VP9.
  1235. return VPX_CODEC_INVALID_PARAM;
  1236. }
  1237. static vpx_codec_err_t ctrl_set_active_map(vpx_codec_alg_priv_t *ctx,
  1238. va_list args) {
  1239. vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
  1240. if (map) {
  1241. if (!vp9_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
  1242. (int)map->cols))
  1243. return VPX_CODEC_OK;
  1244. else
  1245. return VPX_CODEC_INVALID_PARAM;
  1246. } else {
  1247. return VPX_CODEC_INVALID_PARAM;
  1248. }
  1249. }
  1250. static vpx_codec_err_t ctrl_get_active_map(vpx_codec_alg_priv_t *ctx,
  1251. va_list args) {
  1252. vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
  1253. if (map) {
  1254. if (!vp9_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
  1255. (int)map->cols))
  1256. return VPX_CODEC_OK;
  1257. else
  1258. return VPX_CODEC_INVALID_PARAM;
  1259. } else {
  1260. return VPX_CODEC_INVALID_PARAM;
  1261. }
  1262. }
  1263. static vpx_codec_err_t ctrl_set_scale_mode(vpx_codec_alg_priv_t *ctx,
  1264. va_list args) {
  1265. vpx_scaling_mode_t *const mode = va_arg(args, vpx_scaling_mode_t *);
  1266. if (mode) {
  1267. const int res =
  1268. vp9_set_internal_size(ctx->cpi, (VPX_SCALING)mode->h_scaling_mode,
  1269. (VPX_SCALING)mode->v_scaling_mode);
  1270. return (res == 0) ? VPX_CODEC_OK : VPX_CODEC_INVALID_PARAM;
  1271. } else {
  1272. return VPX_CODEC_INVALID_PARAM;
  1273. }
  1274. }
  1275. static vpx_codec_err_t ctrl_set_svc(vpx_codec_alg_priv_t *ctx, va_list args) {
  1276. int data = va_arg(args, int);
  1277. const vpx_codec_enc_cfg_t *cfg = &ctx->cfg;
  1278. // Both one-pass and two-pass RC are supported now.
  1279. // User setting this has to make sure of the following.
  1280. // In two-pass setting: either (but not both)
  1281. // cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
  1282. // In one-pass setting:
  1283. // either or both cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
  1284. vp9_set_svc(ctx->cpi, data);
  1285. if (data == 1 &&
  1286. (cfg->g_pass == VPX_RC_FIRST_PASS || cfg->g_pass == VPX_RC_LAST_PASS) &&
  1287. cfg->ss_number_layers > 1 && cfg->ts_number_layers > 1) {
  1288. return VPX_CODEC_INVALID_PARAM;
  1289. }
  1290. vp9_set_row_mt(ctx->cpi);
  1291. return VPX_CODEC_OK;
  1292. }
  1293. static vpx_codec_err_t ctrl_set_svc_layer_id(vpx_codec_alg_priv_t *ctx,
  1294. va_list args) {
  1295. vpx_svc_layer_id_t *const data = va_arg(args, vpx_svc_layer_id_t *);
  1296. VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
  1297. SVC *const svc = &cpi->svc;
  1298. svc->first_spatial_layer_to_encode = data->spatial_layer_id;
  1299. svc->spatial_layer_to_encode = data->spatial_layer_id;
  1300. svc->temporal_layer_id = data->temporal_layer_id;
  1301. // Checks on valid layer_id input.
  1302. if (svc->temporal_layer_id < 0 ||
  1303. svc->temporal_layer_id >= (int)ctx->cfg.ts_number_layers) {
  1304. return VPX_CODEC_INVALID_PARAM;
  1305. }
  1306. if (svc->first_spatial_layer_to_encode < 0 ||
  1307. svc->first_spatial_layer_to_encode >= (int)ctx->cfg.ss_number_layers) {
  1308. return VPX_CODEC_INVALID_PARAM;
  1309. }
  1310. // First spatial layer to encode not implemented for two-pass.
  1311. if (is_two_pass_svc(cpi) && svc->first_spatial_layer_to_encode > 0)
  1312. return VPX_CODEC_INVALID_PARAM;
  1313. return VPX_CODEC_OK;
  1314. }
  1315. static vpx_codec_err_t ctrl_get_svc_layer_id(vpx_codec_alg_priv_t *ctx,
  1316. va_list args) {
  1317. vpx_svc_layer_id_t *data = va_arg(args, vpx_svc_layer_id_t *);
  1318. VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
  1319. SVC *const svc = &cpi->svc;
  1320. data->spatial_layer_id = svc->spatial_layer_id;
  1321. data->temporal_layer_id = svc->temporal_layer_id;
  1322. return VPX_CODEC_OK;
  1323. }
  1324. static vpx_codec_err_t ctrl_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
  1325. va_list args) {
  1326. VP9_COMP *const cpi = ctx->cpi;
  1327. vpx_svc_extra_cfg_t *const params = va_arg(args, vpx_svc_extra_cfg_t *);
  1328. int sl, tl;
  1329. // Number of temporal layers and number of spatial layers have to be set
  1330. // properly before calling this control function.
  1331. for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
  1332. for (tl = 0; tl < cpi->svc.number_temporal_layers; ++tl) {
  1333. const int layer =
  1334. LAYER_IDS_TO_IDX(sl, tl, cpi->svc.number_temporal_layers);
  1335. LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  1336. lc->max_q = params->max_quantizers[layer];
  1337. lc->min_q = params->min_quantizers[layer];
  1338. lc->scaling_factor_num = params->scaling_factor_num[sl];
  1339. lc->scaling_factor_den = params->scaling_factor_den[sl];
  1340. lc->speed = params->speed_per_layer[sl];
  1341. }
  1342. }
  1343. return VPX_CODEC_OK;
  1344. }
  1345. static vpx_codec_err_t ctrl_set_svc_ref_frame_config(vpx_codec_alg_priv_t *ctx,
  1346. va_list args) {
  1347. VP9_COMP *const cpi = ctx->cpi;
  1348. vpx_svc_ref_frame_config_t *data = va_arg(args, vpx_svc_ref_frame_config_t *);
  1349. int sl;
  1350. for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
  1351. cpi->svc.ext_frame_flags[sl] = data->frame_flags[sl];
  1352. cpi->svc.ext_lst_fb_idx[sl] = data->lst_fb_idx[sl];
  1353. cpi->svc.ext_gld_fb_idx[sl] = data->gld_fb_idx[sl];
  1354. cpi->svc.ext_alt_fb_idx[sl] = data->alt_fb_idx[sl];
  1355. }
  1356. return VPX_CODEC_OK;
  1357. }
  1358. static vpx_codec_err_t ctrl_register_cx_callback(vpx_codec_alg_priv_t *ctx,
  1359. va_list args) {
  1360. vpx_codec_priv_output_cx_pkt_cb_pair_t *cbp =
  1361. (vpx_codec_priv_output_cx_pkt_cb_pair_t *)va_arg(args, void *);
  1362. ctx->output_cx_pkt_cb.output_cx_pkt = cbp->output_cx_pkt;
  1363. ctx->output_cx_pkt_cb.user_priv = cbp->user_priv;
  1364. return VPX_CODEC_OK;
  1365. }
  1366. static vpx_codec_err_t ctrl_set_tune_content(vpx_codec_alg_priv_t *ctx,
  1367. va_list args) {
  1368. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  1369. extra_cfg.content = CAST(VP9E_SET_TUNE_CONTENT, args);
  1370. return update_extra_cfg(ctx, &extra_cfg);
  1371. }
  1372. static vpx_codec_err_t ctrl_set_color_space(vpx_codec_alg_priv_t *ctx,
  1373. va_list args) {
  1374. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  1375. extra_cfg.color_space = CAST(VP9E_SET_COLOR_SPACE, args);
  1376. return update_extra_cfg(ctx, &extra_cfg);
  1377. }
  1378. static vpx_codec_err_t ctrl_set_color_range(vpx_codec_alg_priv_t *ctx,
  1379. va_list args) {
  1380. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  1381. extra_cfg.color_range = CAST(VP9E_SET_COLOR_RANGE, args);
  1382. return update_extra_cfg(ctx, &extra_cfg);
  1383. }
  1384. static vpx_codec_err_t ctrl_set_render_size(vpx_codec_alg_priv_t *ctx,
  1385. va_list args) {
  1386. struct vp9_extracfg extra_cfg = ctx->extra_cfg;
  1387. int *const render_size = va_arg(args, int *);
  1388. extra_cfg.render_width = render_size[0];
  1389. extra_cfg.render_height = render_size[1];
  1390. return update_extra_cfg(ctx, &extra_cfg);
  1391. }
  1392. static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
  1393. { VP8_COPY_REFERENCE, ctrl_copy_reference },
  1394. // Setters
  1395. { VP8_SET_REFERENCE, ctrl_set_reference },
  1396. { VP8_SET_POSTPROC, ctrl_set_previewpp },
  1397. { VP8E_SET_ROI_MAP, ctrl_set_roi_map },
  1398. { VP8E_SET_ACTIVEMAP, ctrl_set_active_map },
  1399. { VP8E_SET_SCALEMODE, ctrl_set_scale_mode },
  1400. { VP8E_SET_CPUUSED, ctrl_set_cpuused },
  1401. { VP8E_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
  1402. { VP8E_SET_SHARPNESS, ctrl_set_sharpness },
  1403. { VP8E_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
  1404. { VP9E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
  1405. { VP9E_SET_TILE_ROWS, ctrl_set_tile_rows },
  1406. { VP8E_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
  1407. { VP8E_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
  1408. { VP8E_SET_ARNR_TYPE, ctrl_set_arnr_type },
  1409. { VP8E_SET_TUNING, ctrl_set_tuning },
  1410. { VP8E_SET_CQ_LEVEL, ctrl_set_cq_level },
  1411. { VP8E_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
  1412. { VP9E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
  1413. { VP9E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
  1414. { VP9E_SET_LOSSLESS, ctrl_set_lossless },
  1415. { VP9E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
  1416. { VP9E_SET_AQ_MODE, ctrl_set_aq_mode },
  1417. { VP9E_SET_ALT_REF_AQ, ctrl_set_alt_ref_aq },
  1418. { VP9E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
  1419. { VP9E_SET_SVC, ctrl_set_svc },
  1420. { VP9E_SET_SVC_PARAMETERS, ctrl_set_svc_parameters },
  1421. { VP9E_REGISTER_CX_CALLBACK, ctrl_register_cx_callback },
  1422. { VP9E_SET_SVC_LAYER_ID, ctrl_set_svc_layer_id },
  1423. { VP9E_SET_TUNE_CONTENT, ctrl_set_tune_content },
  1424. { VP9E_SET_COLOR_SPACE, ctrl_set_color_space },
  1425. { VP9E_SET_COLOR_RANGE, ctrl_set_color_range },
  1426. { VP9E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
  1427. { VP9E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
  1428. { VP9E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
  1429. { VP9E_SET_SVC_REF_FRAME_CONFIG, ctrl_set_svc_ref_frame_config },
  1430. { VP9E_SET_RENDER_SIZE, ctrl_set_render_size },
  1431. { VP9E_SET_TARGET_LEVEL, ctrl_set_target_level },
  1432. { VP9E_SET_ROW_MT, ctrl_set_row_mt },
  1433. { VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
  1434. // Getters
  1435. { VP8E_GET_LAST_QUANTIZER, ctrl_get_quantizer },
  1436. { VP8E_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
  1437. { VP9_GET_REFERENCE, ctrl_get_reference },
  1438. { VP9E_GET_SVC_LAYER_ID, ctrl_get_svc_layer_id },
  1439. { VP9E_GET_ACTIVEMAP, ctrl_get_active_map },
  1440. { VP9E_GET_LEVEL, ctrl_get_level },
  1441. { -1, NULL },
  1442. };
  1443. static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
  1444. { 0,
  1445. {
  1446. // NOLINT
  1447. 0, // g_usage
  1448. 8, // g_threads
  1449. 0, // g_profile
  1450. 320, // g_width
  1451. 240, // g_height
  1452. VPX_BITS_8, // g_bit_depth
  1453. 8, // g_input_bit_depth
  1454. { 1, 30 }, // g_timebase
  1455. 0, // g_error_resilient
  1456. VPX_RC_ONE_PASS, // g_pass
  1457. 25, // g_lag_in_frames
  1458. 0, // rc_dropframe_thresh
  1459. 0, // rc_resize_allowed
  1460. 0, // rc_scaled_width
  1461. 0, // rc_scaled_height
  1462. 60, // rc_resize_down_thresold
  1463. 30, // rc_resize_up_thresold
  1464. VPX_VBR, // rc_end_usage
  1465. { NULL, 0 }, // rc_twopass_stats_in
  1466. { NULL, 0 }, // rc_firstpass_mb_stats_in
  1467. 256, // rc_target_bandwidth
  1468. 0, // rc_min_quantizer
  1469. 63, // rc_max_quantizer
  1470. 25, // rc_undershoot_pct
  1471. 25, // rc_overshoot_pct
  1472. 6000, // rc_max_buffer_size
  1473. 4000, // rc_buffer_initial_size
  1474. 5000, // rc_buffer_optimal_size
  1475. 50, // rc_two_pass_vbrbias
  1476. 0, // rc_two_pass_vbrmin_section
  1477. 2000, // rc_two_pass_vbrmax_section
  1478. // keyframing settings (kf)
  1479. VPX_KF_AUTO, // g_kfmode
  1480. 0, // kf_min_dist
  1481. 128, // kf_max_dist
  1482. VPX_SS_DEFAULT_LAYERS, // ss_number_layers
  1483. { 0 },
  1484. { 0 }, // ss_target_bitrate
  1485. 1, // ts_number_layers
  1486. { 0 }, // ts_target_bitrate
  1487. { 0 }, // ts_rate_decimator
  1488. 0, // ts_periodicity
  1489. { 0 }, // ts_layer_id
  1490. { 0 }, // layer_taget_bitrate
  1491. 0 // temporal_layering_mode
  1492. } },
  1493. };
  1494. #ifndef VERSION_STRING
  1495. #define VERSION_STRING
  1496. #endif
  1497. CODEC_INTERFACE(vpx_codec_vp9_cx) = {
  1498. "WebM Project VP9 Encoder" VERSION_STRING,
  1499. VPX_CODEC_INTERNAL_ABI_VERSION,
  1500. #if CONFIG_VP9_HIGHBITDEPTH
  1501. VPX_CODEC_CAP_HIGHBITDEPTH |
  1502. #endif
  1503. VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR, // vpx_codec_caps_t
  1504. encoder_init, // vpx_codec_init_fn_t
  1505. encoder_destroy, // vpx_codec_destroy_fn_t
  1506. encoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
  1507. {
  1508. // NOLINT
  1509. NULL, // vpx_codec_peek_si_fn_t
  1510. NULL, // vpx_codec_get_si_fn_t
  1511. NULL, // vpx_codec_decode_fn_t
  1512. NULL, // vpx_codec_frame_get_fn_t
  1513. NULL // vpx_codec_set_fb_fn_t
  1514. },
  1515. {
  1516. // NOLINT
  1517. 1, // 1 cfg map
  1518. encoder_usage_cfg_map, // vpx_codec_enc_cfg_map_t
  1519. encoder_encode, // vpx_codec_encode_fn_t
  1520. encoder_get_cxdata, // vpx_codec_get_cx_data_fn_t
  1521. encoder_set_config, // vpx_codec_enc_config_set_fn_t
  1522. NULL, // vpx_codec_get_global_headers_fn_t
  1523. encoder_get_preview, // vpx_codec_get_preview_frame_fn_t
  1524. NULL // vpx_codec_enc_mr_get_mem_loc_fn_t
  1525. }
  1526. };