vp9_cx_iface.c 56 KB

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