vp9_cx_iface.c 59 KB

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