vp8_cx_iface.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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 "./vpx_config.h"
  11. #include "./vp8_rtcd.h"
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "./vpx_scale_rtcd.h"
  14. #include "vpx/vpx_codec.h"
  15. #include "vpx/internal/vpx_codec_internal.h"
  16. #include "vpx_version.h"
  17. #include "vpx_mem/vpx_mem.h"
  18. #include "vp8/encoder/onyx_int.h"
  19. #include "vpx/vp8cx.h"
  20. #include "vp8/encoder/firstpass.h"
  21. #include "vp8/common/onyx.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. struct vp8_extracfg
  25. {
  26. struct vpx_codec_pkt_list *pkt_list;
  27. int cpu_used; /** available cpu percentage in 1/16*/
  28. unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
  29. unsigned int noise_sensitivity;
  30. unsigned int Sharpness;
  31. unsigned int static_thresh;
  32. unsigned int token_partitions;
  33. unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
  34. unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
  35. unsigned int arnr_type; /* alt_ref filter type */
  36. vp8e_tuning tuning;
  37. unsigned int cq_level; /* constrained quality level */
  38. unsigned int rc_max_intra_bitrate_pct;
  39. unsigned int screen_content_mode;
  40. };
  41. static struct vp8_extracfg default_extracfg = {
  42. NULL,
  43. #if !(CONFIG_REALTIME_ONLY)
  44. 0, /* cpu_used */
  45. #else
  46. 4, /* cpu_used */
  47. #endif
  48. 0, /* enable_auto_alt_ref */
  49. 0, /* noise_sensitivity */
  50. 0, /* Sharpness */
  51. 0, /* static_thresh */
  52. #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  53. VP8_EIGHT_TOKENPARTITION,
  54. #else
  55. VP8_ONE_TOKENPARTITION, /* token_partitions */
  56. #endif
  57. 0, /* arnr_max_frames */
  58. 3, /* arnr_strength */
  59. 3, /* arnr_type*/
  60. 0, /* tuning*/
  61. 10, /* cq_level */
  62. 0, /* rc_max_intra_bitrate_pct */
  63. 0, /* screen_content_mode */
  64. };
  65. struct vpx_codec_alg_priv
  66. {
  67. vpx_codec_priv_t base;
  68. vpx_codec_enc_cfg_t cfg;
  69. struct vp8_extracfg vp8_cfg;
  70. VP8_CONFIG oxcf;
  71. struct VP8_COMP *cpi;
  72. unsigned char *cx_data;
  73. unsigned int cx_data_sz;
  74. vpx_image_t preview_img;
  75. unsigned int next_frame_flag;
  76. vp8_postproc_cfg_t preview_ppcfg;
  77. /* pkt_list size depends on the maximum number of lagged frames allowed. */
  78. vpx_codec_pkt_list_decl(64) pkt_list;
  79. unsigned int fixed_kf_cntr;
  80. vpx_enc_frame_flags_t control_frame_flags;
  81. };
  82. static vpx_codec_err_t
  83. update_error_state(vpx_codec_alg_priv_t *ctx,
  84. const struct vpx_internal_error_info *error)
  85. {
  86. vpx_codec_err_t res;
  87. if ((res = error->error_code))
  88. ctx->base.err_detail = error->has_detail
  89. ? error->detail
  90. : NULL;
  91. return res;
  92. }
  93. #undef ERROR
  94. #define ERROR(str) do {\
  95. ctx->base.err_detail = str;\
  96. return VPX_CODEC_INVALID_PARAM;\
  97. } while(0)
  98. #define RANGE_CHECK(p,memb,lo,hi) do {\
  99. if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
  100. ERROR(#memb " out of range ["#lo".."#hi"]");\
  101. } while(0)
  102. #define RANGE_CHECK_HI(p,memb,hi) do {\
  103. if(!((p)->memb <= (hi))) \
  104. ERROR(#memb " out of range [.."#hi"]");\
  105. } while(0)
  106. #define RANGE_CHECK_LO(p,memb,lo) do {\
  107. if(!((p)->memb >= (lo))) \
  108. ERROR(#memb " out of range ["#lo"..]");\
  109. } while(0)
  110. #define RANGE_CHECK_BOOL(p,memb) do {\
  111. if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
  112. } while(0)
  113. static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
  114. const vpx_codec_enc_cfg_t *cfg,
  115. const struct vp8_extracfg *vp8_cfg,
  116. int finalize)
  117. {
  118. RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
  119. RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
  120. RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
  121. RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
  122. RANGE_CHECK_HI(cfg, g_profile, 3);
  123. RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
  124. RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
  125. RANGE_CHECK_HI(cfg, g_threads, 64);
  126. #if CONFIG_REALTIME_ONLY
  127. RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
  128. #elif CONFIG_MULTI_RES_ENCODING
  129. if (ctx->base.enc.total_encoders > 1)
  130. RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
  131. #else
  132. RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
  133. #endif
  134. RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
  135. RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
  136. RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
  137. RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
  138. RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
  139. /* TODO: add spatial re-sampling support and frame dropping in
  140. * multi-res-encoder.*/
  141. #if CONFIG_MULTI_RES_ENCODING
  142. if (ctx->base.enc.total_encoders > 1)
  143. RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
  144. #else
  145. RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
  146. #endif
  147. RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
  148. RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
  149. RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
  150. #if CONFIG_REALTIME_ONLY
  151. RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
  152. #elif CONFIG_MULTI_RES_ENCODING
  153. if (ctx->base.enc.total_encoders > 1)
  154. RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
  155. #else
  156. RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
  157. #endif
  158. /* VP8 does not support a lower bound on the keyframe interval in
  159. * automatic keyframe placement mode.
  160. */
  161. if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
  162. && cfg->kf_min_dist > 0)
  163. ERROR("kf_min_dist not supported in auto mode, use 0 "
  164. "or kf_max_dist instead.");
  165. RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
  166. RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
  167. #if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
  168. RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
  169. #else
  170. RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
  171. #endif
  172. RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
  173. VP8_EIGHT_TOKENPARTITION);
  174. RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
  175. RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
  176. RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
  177. RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
  178. RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
  179. RANGE_CHECK_HI(vp8_cfg, screen_content_mode, 2);
  180. if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
  181. RANGE_CHECK(vp8_cfg, cq_level,
  182. cfg->rc_min_quantizer, cfg->rc_max_quantizer);
  183. #if !(CONFIG_REALTIME_ONLY)
  184. if (cfg->g_pass == VPX_RC_LAST_PASS)
  185. {
  186. size_t packet_sz = sizeof(FIRSTPASS_STATS);
  187. int n_packets = (int)(cfg->rc_twopass_stats_in.sz /
  188. packet_sz);
  189. FIRSTPASS_STATS *stats;
  190. if (!cfg->rc_twopass_stats_in.buf)
  191. ERROR("rc_twopass_stats_in.buf not set.");
  192. if (cfg->rc_twopass_stats_in.sz % packet_sz)
  193. ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
  194. if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
  195. ERROR("rc_twopass_stats_in requires at least two packets.");
  196. stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
  197. + (n_packets - 1) * packet_sz);
  198. if ((int)(stats->count + 0.5) != n_packets - 1)
  199. ERROR("rc_twopass_stats_in missing EOS stats packet");
  200. }
  201. #endif
  202. RANGE_CHECK(cfg, ts_number_layers, 1, 5);
  203. if (cfg->ts_number_layers > 1)
  204. {
  205. unsigned int i;
  206. RANGE_CHECK_HI(cfg, ts_periodicity, 16);
  207. for (i=1; i<cfg->ts_number_layers; i++)
  208. if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1] &&
  209. cfg->rc_target_bitrate > 0)
  210. ERROR("ts_target_bitrate entries are not strictly increasing");
  211. RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
  212. for (i=cfg->ts_number_layers-2; i>0; i--)
  213. if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i])
  214. ERROR("ts_rate_decimator factors are not powers of 2");
  215. RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1);
  216. }
  217. #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  218. if(cfg->g_threads > (1 << vp8_cfg->token_partitions))
  219. ERROR("g_threads cannot be bigger than number of token partitions");
  220. #endif
  221. return VPX_CODEC_OK;
  222. }
  223. static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
  224. const vpx_image_t *img)
  225. {
  226. switch (img->fmt)
  227. {
  228. case VPX_IMG_FMT_YV12:
  229. case VPX_IMG_FMT_I420:
  230. case VPX_IMG_FMT_VPXI420:
  231. case VPX_IMG_FMT_VPXYV12:
  232. break;
  233. default:
  234. ERROR("Invalid image format. Only YV12 and I420 images are supported");
  235. }
  236. if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
  237. ERROR("Image size must match encoder init configuration size");
  238. return VPX_CODEC_OK;
  239. }
  240. static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
  241. vpx_codec_enc_cfg_t cfg,
  242. struct vp8_extracfg vp8_cfg,
  243. vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
  244. {
  245. oxcf->multi_threaded = cfg.g_threads;
  246. oxcf->Version = cfg.g_profile;
  247. oxcf->Width = cfg.g_w;
  248. oxcf->Height = cfg.g_h;
  249. oxcf->timebase = cfg.g_timebase;
  250. oxcf->error_resilient_mode = cfg.g_error_resilient;
  251. switch (cfg.g_pass)
  252. {
  253. case VPX_RC_ONE_PASS:
  254. oxcf->Mode = MODE_BESTQUALITY;
  255. break;
  256. case VPX_RC_FIRST_PASS:
  257. oxcf->Mode = MODE_FIRSTPASS;
  258. break;
  259. case VPX_RC_LAST_PASS:
  260. oxcf->Mode = MODE_SECONDPASS_BEST;
  261. break;
  262. }
  263. if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS)
  264. {
  265. oxcf->allow_lag = 0;
  266. oxcf->lag_in_frames = 0;
  267. }
  268. else
  269. {
  270. oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
  271. oxcf->lag_in_frames = cfg.g_lag_in_frames;
  272. }
  273. oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
  274. oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
  275. oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
  276. oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
  277. oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
  278. if (cfg.rc_end_usage == VPX_VBR) {
  279. oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
  280. } else if (cfg.rc_end_usage == VPX_CBR) {
  281. oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
  282. } else if (cfg.rc_end_usage == VPX_CQ) {
  283. oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
  284. } else if (cfg.rc_end_usage == VPX_Q) {
  285. oxcf->end_usage = USAGE_CONSTANT_QUALITY;
  286. }
  287. oxcf->target_bandwidth = cfg.rc_target_bitrate;
  288. oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
  289. oxcf->best_allowed_q = cfg.rc_min_quantizer;
  290. oxcf->worst_allowed_q = cfg.rc_max_quantizer;
  291. oxcf->cq_level = vp8_cfg.cq_level;
  292. oxcf->fixed_q = -1;
  293. oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
  294. oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
  295. oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
  296. oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
  297. oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
  298. oxcf->maximum_buffer_size = cfg.rc_buf_sz;
  299. oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
  300. oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
  301. oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
  302. oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
  303. oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
  304. oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
  305. && cfg.kf_min_dist != cfg.kf_max_dist;
  306. oxcf->key_freq = cfg.kf_max_dist;
  307. oxcf->number_of_layers = cfg.ts_number_layers;
  308. oxcf->periodicity = cfg.ts_periodicity;
  309. if (oxcf->number_of_layers > 1)
  310. {
  311. memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate,
  312. sizeof(cfg.ts_target_bitrate));
  313. memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator,
  314. sizeof(cfg.ts_rate_decimator));
  315. memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
  316. }
  317. #if CONFIG_MULTI_RES_ENCODING
  318. /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
  319. * are both memset to 0, which ensures the correct logic under this
  320. * situation.
  321. */
  322. if(mr_cfg)
  323. {
  324. oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
  325. oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
  326. oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
  327. oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
  328. oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
  329. }
  330. #else
  331. (void)mr_cfg;
  332. #endif
  333. oxcf->cpu_used = vp8_cfg.cpu_used;
  334. oxcf->encode_breakout = vp8_cfg.static_thresh;
  335. oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
  336. oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
  337. oxcf->Sharpness = vp8_cfg.Sharpness;
  338. oxcf->token_partitions = vp8_cfg.token_partitions;
  339. oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
  340. oxcf->output_pkt_list = vp8_cfg.pkt_list;
  341. oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
  342. oxcf->arnr_strength = vp8_cfg.arnr_strength;
  343. oxcf->arnr_type = vp8_cfg.arnr_type;
  344. oxcf->tuning = vp8_cfg.tuning;
  345. oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
  346. /*
  347. printf("Current VP8 Settings: \n");
  348. printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
  349. printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
  350. printf("Sharpness: %d\n", oxcf->Sharpness);
  351. printf("cpu_used: %d\n", oxcf->cpu_used);
  352. printf("Mode: %d\n", oxcf->Mode);
  353. printf("auto_key: %d\n", oxcf->auto_key);
  354. printf("key_freq: %d\n", oxcf->key_freq);
  355. printf("end_usage: %d\n", oxcf->end_usage);
  356. printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
  357. printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
  358. printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
  359. printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
  360. printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
  361. printf("fixed_q: %d\n", oxcf->fixed_q);
  362. printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
  363. printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
  364. printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
  365. printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
  366. printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
  367. printf("allow_df: %d\n", oxcf->allow_df);
  368. printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
  369. printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
  370. printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
  371. printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
  372. printf("allow_lag: %d\n", oxcf->allow_lag);
  373. printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
  374. printf("play_alternate: %d\n", oxcf->play_alternate);
  375. printf("Version: %d\n", oxcf->Version);
  376. printf("multi_threaded: %d\n", oxcf->multi_threaded);
  377. printf("encode_breakout: %d\n", oxcf->encode_breakout);
  378. */
  379. return VPX_CODEC_OK;
  380. }
  381. static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
  382. const vpx_codec_enc_cfg_t *cfg)
  383. {
  384. vpx_codec_err_t res;
  385. if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h)
  386. {
  387. if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
  388. ERROR("Cannot change width or height after initialization");
  389. if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
  390. (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
  391. ERROR("Cannot increast width or height larger than their initial values");
  392. }
  393. /* Prevent increasing lag_in_frames. This check is stricter than it needs
  394. * to be -- the limit is not increasing past the first lag_in_frames
  395. * value, but we don't track the initial config, only the last successful
  396. * config.
  397. */
  398. if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
  399. ERROR("Cannot increase lag_in_frames");
  400. res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
  401. if (!res)
  402. {
  403. ctx->cfg = *cfg;
  404. set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
  405. vp8_change_config(ctx->cpi, &ctx->oxcf);
  406. }
  407. return res;
  408. }
  409. static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args)
  410. {
  411. int *const arg = va_arg(args, int *);
  412. if (arg == NULL)
  413. return VPX_CODEC_INVALID_PARAM;
  414. *arg = vp8_get_quantizer(ctx->cpi);
  415. return VPX_CODEC_OK;
  416. }
  417. static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx, va_list args)
  418. {
  419. int *const arg = va_arg(args, int *);
  420. if (arg == NULL)
  421. return VPX_CODEC_INVALID_PARAM;
  422. *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
  423. return VPX_CODEC_OK;
  424. }
  425. static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
  426. const struct vp8_extracfg *extra_cfg)
  427. {
  428. const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
  429. if (res == VPX_CODEC_OK) {
  430. ctx->vp8_cfg = *extra_cfg;
  431. set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
  432. vp8_change_config(ctx->cpi, &ctx->oxcf);
  433. }
  434. return res;
  435. }
  436. static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args)
  437. {
  438. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  439. extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
  440. return update_extracfg(ctx, &extra_cfg);
  441. }
  442. static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
  443. va_list args)
  444. {
  445. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  446. extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
  447. return update_extracfg(ctx, &extra_cfg);
  448. }
  449. static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
  450. va_list args)
  451. {
  452. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  453. extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
  454. return update_extracfg(ctx, &extra_cfg);
  455. }
  456. static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args)
  457. {
  458. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  459. extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args);
  460. return update_extracfg(ctx, &extra_cfg);
  461. }
  462. static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx,
  463. va_list args)
  464. {
  465. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  466. extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
  467. return update_extracfg(ctx, &extra_cfg);
  468. }
  469. static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
  470. va_list args)
  471. {
  472. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  473. extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
  474. return update_extracfg(ctx, &extra_cfg);
  475. }
  476. static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
  477. va_list args)
  478. {
  479. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  480. extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
  481. return update_extracfg(ctx, &extra_cfg);
  482. }
  483. static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
  484. va_list args)
  485. {
  486. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  487. extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
  488. return update_extracfg(ctx, &extra_cfg);
  489. }
  490. static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args)
  491. {
  492. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  493. extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
  494. return update_extracfg(ctx, &extra_cfg);
  495. }
  496. static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args)
  497. {
  498. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  499. extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
  500. return update_extracfg(ctx, &extra_cfg);
  501. }
  502. static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args)
  503. {
  504. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  505. extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
  506. return update_extracfg(ctx, &extra_cfg);
  507. }
  508. static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
  509. va_list args)
  510. {
  511. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  512. extra_cfg.rc_max_intra_bitrate_pct =
  513. CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
  514. return update_extracfg(ctx, &extra_cfg);
  515. }
  516. static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx,
  517. va_list args)
  518. {
  519. struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
  520. extra_cfg.screen_content_mode =
  521. CAST(VP8E_SET_SCREEN_CONTENT_MODE, args);
  522. return update_extracfg(ctx, &extra_cfg);
  523. }
  524. static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
  525. void **mem_loc)
  526. {
  527. vpx_codec_err_t res = 0;
  528. #if CONFIG_MULTI_RES_ENCODING
  529. LOWER_RES_FRAME_INFO *shared_mem_loc;
  530. int mb_rows = ((cfg->g_w + 15) >>4);
  531. int mb_cols = ((cfg->g_h + 15) >>4);
  532. shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
  533. if(!shared_mem_loc)
  534. {
  535. res = VPX_CODEC_MEM_ERROR;
  536. }
  537. shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO));
  538. if(!(shared_mem_loc->mb_info))
  539. {
  540. res = VPX_CODEC_MEM_ERROR;
  541. }
  542. else
  543. {
  544. *mem_loc = (void *)shared_mem_loc;
  545. res = VPX_CODEC_OK;
  546. }
  547. #else
  548. (void)cfg;
  549. (void)mem_loc;
  550. #endif
  551. return res;
  552. }
  553. static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
  554. vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
  555. {
  556. vpx_codec_err_t res = VPX_CODEC_OK;
  557. vp8_rtcd();
  558. vpx_dsp_rtcd();
  559. vpx_scale_rtcd();
  560. if (!ctx->priv)
  561. {
  562. struct vpx_codec_alg_priv *priv =
  563. (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
  564. if (!priv)
  565. {
  566. return VPX_CODEC_MEM_ERROR;
  567. }
  568. ctx->priv = (vpx_codec_priv_t *)priv;
  569. ctx->priv->init_flags = ctx->init_flags;
  570. if (ctx->config.enc)
  571. {
  572. /* Update the reference to the config structure to an
  573. * internal copy.
  574. */
  575. priv->cfg = *ctx->config.enc;
  576. ctx->config.enc = &priv->cfg;
  577. }
  578. priv->vp8_cfg = default_extracfg;
  579. priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
  580. priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
  581. if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
  582. priv->cx_data = malloc(priv->cx_data_sz);
  583. if (!priv->cx_data)
  584. {
  585. return VPX_CODEC_MEM_ERROR;
  586. }
  587. if(mr_cfg)
  588. ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
  589. else
  590. ctx->priv->enc.total_encoders = 1;
  591. res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
  592. if (!res)
  593. {
  594. set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
  595. priv->cpi = vp8_create_compressor(&priv->oxcf);
  596. if (!priv->cpi)
  597. res = VPX_CODEC_MEM_ERROR;
  598. }
  599. }
  600. return res;
  601. }
  602. static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
  603. {
  604. #if CONFIG_MULTI_RES_ENCODING
  605. /* Free multi-encoder shared memory */
  606. if (ctx->oxcf.mr_total_resolutions > 0 && (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions-1))
  607. {
  608. LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
  609. free(shared_mem_loc->mb_info);
  610. free(ctx->oxcf.mr_low_res_mode_info);
  611. }
  612. #endif
  613. free(ctx->cx_data);
  614. vp8_remove_compressor(&ctx->cpi);
  615. vpx_free(ctx);
  616. return VPX_CODEC_OK;
  617. }
  618. static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
  619. YV12_BUFFER_CONFIG *yv12)
  620. {
  621. const int y_w = img->d_w;
  622. const int y_h = img->d_h;
  623. const int uv_w = (img->d_w + 1) / 2;
  624. const int uv_h = (img->d_h + 1) / 2;
  625. vpx_codec_err_t res = VPX_CODEC_OK;
  626. yv12->y_buffer = img->planes[VPX_PLANE_Y];
  627. yv12->u_buffer = img->planes[VPX_PLANE_U];
  628. yv12->v_buffer = img->planes[VPX_PLANE_V];
  629. yv12->y_crop_width = y_w;
  630. yv12->y_crop_height = y_h;
  631. yv12->y_width = y_w;
  632. yv12->y_height = y_h;
  633. yv12->uv_crop_width = uv_w;
  634. yv12->uv_crop_height = uv_h;
  635. yv12->uv_width = uv_w;
  636. yv12->uv_height = uv_h;
  637. yv12->y_stride = img->stride[VPX_PLANE_Y];
  638. yv12->uv_stride = img->stride[VPX_PLANE_U];
  639. yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
  640. return res;
  641. }
  642. static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
  643. unsigned long duration,
  644. unsigned long deadline)
  645. {
  646. unsigned int new_qc;
  647. #if !(CONFIG_REALTIME_ONLY)
  648. /* Use best quality mode if no deadline is given. */
  649. new_qc = MODE_BESTQUALITY;
  650. if (deadline)
  651. {
  652. uint64_t duration_us;
  653. /* Convert duration parameter from stream timebase to microseconds */
  654. duration_us = (uint64_t)duration * 1000000
  655. * (uint64_t)ctx->cfg.g_timebase.num
  656. / (uint64_t)ctx->cfg.g_timebase.den;
  657. /* If the deadline is more that the duration this frame is to be shown,
  658. * use good quality mode. Otherwise use realtime mode.
  659. */
  660. new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
  661. }
  662. #else
  663. new_qc = MODE_REALTIME;
  664. #endif
  665. if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
  666. new_qc = MODE_FIRSTPASS;
  667. else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
  668. new_qc = (new_qc == MODE_BESTQUALITY)
  669. ? MODE_SECONDPASS_BEST
  670. : MODE_SECONDPASS;
  671. if (ctx->oxcf.Mode != new_qc)
  672. {
  673. ctx->oxcf.Mode = new_qc;
  674. vp8_change_config(ctx->cpi, &ctx->oxcf);
  675. }
  676. }
  677. static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
  678. int flags)
  679. {
  680. /* Handle Flags */
  681. if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
  682. || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
  683. {
  684. ctx->base.err_detail = "Conflicting flags.";
  685. return VPX_CODEC_INVALID_PARAM;
  686. }
  687. if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
  688. | VP8_EFLAG_NO_REF_ARF))
  689. {
  690. int ref = 7;
  691. if (flags & VP8_EFLAG_NO_REF_LAST)
  692. ref ^= VP8_LAST_FRAME;
  693. if (flags & VP8_EFLAG_NO_REF_GF)
  694. ref ^= VP8_GOLD_FRAME;
  695. if (flags & VP8_EFLAG_NO_REF_ARF)
  696. ref ^= VP8_ALTR_FRAME;
  697. vp8_use_as_reference(ctx->cpi, ref);
  698. }
  699. if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
  700. | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
  701. | VP8_EFLAG_FORCE_ARF))
  702. {
  703. int upd = 7;
  704. if (flags & VP8_EFLAG_NO_UPD_LAST)
  705. upd ^= VP8_LAST_FRAME;
  706. if (flags & VP8_EFLAG_NO_UPD_GF)
  707. upd ^= VP8_GOLD_FRAME;
  708. if (flags & VP8_EFLAG_NO_UPD_ARF)
  709. upd ^= VP8_ALTR_FRAME;
  710. vp8_update_reference(ctx->cpi, upd);
  711. }
  712. if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
  713. {
  714. vp8_update_entropy(ctx->cpi, 0);
  715. }
  716. return VPX_CODEC_OK;
  717. }
  718. static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
  719. const vpx_image_t *img,
  720. vpx_codec_pts_t pts,
  721. unsigned long duration,
  722. vpx_enc_frame_flags_t flags,
  723. unsigned long deadline)
  724. {
  725. vpx_codec_err_t res = VPX_CODEC_OK;
  726. if (!ctx->cfg.rc_target_bitrate)
  727. return res;
  728. if (img)
  729. res = validate_img(ctx, img);
  730. if (!res)
  731. res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
  732. pick_quickcompress_mode(ctx, duration, deadline);
  733. vpx_codec_pkt_list_init(&ctx->pkt_list);
  734. // If no flags are set in the encode call, then use the frame flags as
  735. // defined via the control function: vp8e_set_frame_flags.
  736. if (!flags) {
  737. flags = ctx->control_frame_flags;
  738. }
  739. ctx->control_frame_flags = 0;
  740. res = set_reference_and_update(ctx, flags);
  741. /* Handle fixed keyframe intervals */
  742. if (ctx->cfg.kf_mode == VPX_KF_AUTO
  743. && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
  744. {
  745. if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
  746. {
  747. flags |= VPX_EFLAG_FORCE_KF;
  748. ctx->fixed_kf_cntr = 1;
  749. }
  750. }
  751. /* Initialize the encoder instance on the first frame*/
  752. if (!res && ctx->cpi)
  753. {
  754. unsigned int lib_flags;
  755. YV12_BUFFER_CONFIG sd;
  756. int64_t dst_time_stamp, dst_end_time_stamp;
  757. unsigned long size, cx_data_sz;
  758. unsigned char *cx_data;
  759. unsigned char *cx_data_end;
  760. int comp_data_state = 0;
  761. /* Set up internal flags */
  762. if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
  763. ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
  764. if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
  765. ((VP8_COMP *)ctx->cpi)->output_partition = 1;
  766. /* Convert API flags to internal codec lib flags */
  767. lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
  768. /* vp8 use 10,000,000 ticks/second as time stamp */
  769. dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
  770. dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
  771. if (img != NULL)
  772. {
  773. res = image2yuvconfig(img, &sd);
  774. if (sd.y_width != ctx->cfg.g_w || sd.y_height != ctx->cfg.g_h) {
  775. /* from vpx_encoder.h for g_w/g_h:
  776. "Note that the frames passed as input to the encoder must have this resolution"
  777. */
  778. ctx->base.err_detail = "Invalid input frame resolution";
  779. res = VPX_CODEC_INVALID_PARAM;
  780. } else {
  781. if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
  782. &sd, dst_time_stamp, dst_end_time_stamp))
  783. {
  784. VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
  785. res = update_error_state(ctx, &cpi->common.error);
  786. }
  787. }
  788. /* reset for next frame */
  789. ctx->next_frame_flag = 0;
  790. }
  791. cx_data = ctx->cx_data;
  792. cx_data_sz = ctx->cx_data_sz;
  793. cx_data_end = ctx->cx_data + cx_data_sz;
  794. lib_flags = 0;
  795. while (cx_data_sz >= ctx->cx_data_sz / 2)
  796. {
  797. comp_data_state = vp8_get_compressed_data(ctx->cpi,
  798. &lib_flags,
  799. &size,
  800. cx_data,
  801. cx_data_end,
  802. &dst_time_stamp,
  803. &dst_end_time_stamp,
  804. !img);
  805. if(comp_data_state == VPX_CODEC_CORRUPT_FRAME)
  806. return VPX_CODEC_CORRUPT_FRAME;
  807. else if(comp_data_state == -1)
  808. break;
  809. if (size)
  810. {
  811. vpx_codec_pts_t round, delta;
  812. vpx_codec_cx_pkt_t pkt;
  813. VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
  814. /* Add the frame packet to the list of returned packets. */
  815. round = (vpx_codec_pts_t)10000000
  816. * ctx->cfg.g_timebase.num / 2 - 1;
  817. delta = (dst_end_time_stamp - dst_time_stamp);
  818. pkt.kind = VPX_CODEC_CX_FRAME_PKT;
  819. pkt.data.frame.pts =
  820. (dst_time_stamp * ctx->cfg.g_timebase.den + round)
  821. / ctx->cfg.g_timebase.num / 10000000;
  822. pkt.data.frame.duration = (unsigned long)
  823. ((delta * ctx->cfg.g_timebase.den + round)
  824. / ctx->cfg.g_timebase.num / 10000000);
  825. pkt.data.frame.flags = lib_flags << 16;
  826. if (lib_flags & FRAMEFLAGS_KEY)
  827. pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
  828. if (!cpi->common.show_frame)
  829. {
  830. pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
  831. /* This timestamp should be as close as possible to the
  832. * prior PTS so that if a decoder uses pts to schedule when
  833. * to do this, we start right after last frame was decoded.
  834. * Invisible frames have no duration.
  835. */
  836. pkt.data.frame.pts = ((cpi->last_time_stamp_seen
  837. * ctx->cfg.g_timebase.den + round)
  838. / ctx->cfg.g_timebase.num / 10000000) + 1;
  839. pkt.data.frame.duration = 0;
  840. }
  841. if (cpi->droppable)
  842. pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
  843. if (cpi->output_partition)
  844. {
  845. int i;
  846. const int num_partitions =
  847. (1 << cpi->common.multi_token_partition) + 1;
  848. pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
  849. for (i = 0; i < num_partitions; ++i)
  850. {
  851. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  852. pkt.data.frame.buf = cpi->partition_d[i];
  853. #else
  854. pkt.data.frame.buf = cx_data;
  855. cx_data += cpi->partition_sz[i];
  856. cx_data_sz -= cpi->partition_sz[i];
  857. #endif
  858. pkt.data.frame.sz = cpi->partition_sz[i];
  859. pkt.data.frame.partition_id = i;
  860. /* don't set the fragment bit for the last partition */
  861. if (i == (num_partitions - 1))
  862. pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
  863. vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
  864. }
  865. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  866. /* In lagged mode the encoder can buffer multiple frames.
  867. * We don't want this in partitioned output because
  868. * partitions are spread all over the output buffer.
  869. * So, force an exit!
  870. */
  871. cx_data_sz -= ctx->cx_data_sz / 2;
  872. #endif
  873. }
  874. else
  875. {
  876. pkt.data.frame.buf = cx_data;
  877. pkt.data.frame.sz = size;
  878. pkt.data.frame.partition_id = -1;
  879. vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
  880. cx_data += size;
  881. cx_data_sz -= size;
  882. }
  883. }
  884. }
  885. }
  886. return res;
  887. }
  888. static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
  889. vpx_codec_iter_t *iter)
  890. {
  891. return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
  892. }
  893. static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
  894. va_list args)
  895. {
  896. vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
  897. if (data)
  898. {
  899. vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
  900. YV12_BUFFER_CONFIG sd;
  901. image2yuvconfig(&frame->img, &sd);
  902. vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
  903. return VPX_CODEC_OK;
  904. }
  905. else
  906. return VPX_CODEC_INVALID_PARAM;
  907. }
  908. static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
  909. va_list args)
  910. {
  911. vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
  912. if (data)
  913. {
  914. vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
  915. YV12_BUFFER_CONFIG sd;
  916. image2yuvconfig(&frame->img, &sd);
  917. vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
  918. return VPX_CODEC_OK;
  919. }
  920. else
  921. return VPX_CODEC_INVALID_PARAM;
  922. }
  923. static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
  924. va_list args)
  925. {
  926. #if CONFIG_POSTPROC
  927. vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
  928. if (data)
  929. {
  930. ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
  931. return VPX_CODEC_OK;
  932. }
  933. else
  934. return VPX_CODEC_INVALID_PARAM;
  935. #else
  936. (void)ctx;
  937. (void)args;
  938. return VPX_CODEC_INCAPABLE;
  939. #endif
  940. }
  941. static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
  942. {
  943. YV12_BUFFER_CONFIG sd;
  944. vp8_ppflags_t flags = {0};
  945. if (ctx->preview_ppcfg.post_proc_flag)
  946. {
  947. flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
  948. flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
  949. flags.noise_level = ctx->preview_ppcfg.noise_level;
  950. }
  951. if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
  952. {
  953. /*
  954. vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
  955. sd.y_width + 2*VP8BORDERINPIXELS,
  956. sd.y_height + 2*VP8BORDERINPIXELS,
  957. 1,
  958. sd.buffer_alloc);
  959. vpx_img_set_rect(&ctx->preview_img,
  960. VP8BORDERINPIXELS, VP8BORDERINPIXELS,
  961. sd.y_width, sd.y_height);
  962. */
  963. ctx->preview_img.bps = 12;
  964. ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
  965. ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
  966. ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
  967. ctx->preview_img.fmt = VPX_IMG_FMT_I420;
  968. ctx->preview_img.x_chroma_shift = 1;
  969. ctx->preview_img.y_chroma_shift = 1;
  970. ctx->preview_img.d_w = sd.y_width;
  971. ctx->preview_img.d_h = sd.y_height;
  972. ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
  973. ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
  974. ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
  975. ctx->preview_img.w = sd.y_width;
  976. ctx->preview_img.h = sd.y_height;
  977. return &ctx->preview_img;
  978. }
  979. else
  980. return NULL;
  981. }
  982. static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
  983. va_list args)
  984. {
  985. int update = va_arg(args, int);
  986. vp8_update_entropy(ctx->cpi, update);
  987. return VPX_CODEC_OK;
  988. }
  989. static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
  990. va_list args)
  991. {
  992. int update = va_arg(args, int);
  993. vp8_update_reference(ctx->cpi, update);
  994. return VPX_CODEC_OK;
  995. }
  996. static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
  997. va_list args)
  998. {
  999. int reference_flag = va_arg(args, int);
  1000. vp8_use_as_reference(ctx->cpi, reference_flag);
  1001. return VPX_CODEC_OK;
  1002. }
  1003. static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
  1004. va_list args)
  1005. {
  1006. int frame_flags = va_arg(args, int);
  1007. ctx->control_frame_flags = frame_flags;
  1008. return set_reference_and_update(ctx, frame_flags);
  1009. }
  1010. static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
  1011. va_list args)
  1012. {
  1013. int layer_id = va_arg(args, int);
  1014. if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
  1015. return VPX_CODEC_INVALID_PARAM;
  1016. }
  1017. ctx->cpi->temporal_layer_id = layer_id;
  1018. return VPX_CODEC_OK;
  1019. }
  1020. static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
  1021. va_list args)
  1022. {
  1023. vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
  1024. if (data)
  1025. {
  1026. vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
  1027. if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
  1028. return VPX_CODEC_OK;
  1029. else
  1030. return VPX_CODEC_INVALID_PARAM;
  1031. }
  1032. else
  1033. return VPX_CODEC_INVALID_PARAM;
  1034. }
  1035. static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
  1036. va_list args)
  1037. {
  1038. vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
  1039. if (data)
  1040. {
  1041. vpx_active_map_t *map = (vpx_active_map_t *)data;
  1042. if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
  1043. return VPX_CODEC_OK;
  1044. else
  1045. return VPX_CODEC_INVALID_PARAM;
  1046. }
  1047. else
  1048. return VPX_CODEC_INVALID_PARAM;
  1049. }
  1050. static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
  1051. va_list args)
  1052. {
  1053. vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
  1054. if (data)
  1055. {
  1056. int res;
  1057. vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
  1058. res = vp8_set_internal_size(ctx->cpi,
  1059. (VPX_SCALING)scalemode.h_scaling_mode,
  1060. (VPX_SCALING)scalemode.v_scaling_mode);
  1061. if (!res)
  1062. {
  1063. /*force next frame a key frame to effect scaling mode */
  1064. ctx->next_frame_flag |= FRAMEFLAGS_KEY;
  1065. return VPX_CODEC_OK;
  1066. }
  1067. else
  1068. return VPX_CODEC_INVALID_PARAM;
  1069. }
  1070. else
  1071. return VPX_CODEC_INVALID_PARAM;
  1072. }
  1073. static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
  1074. {
  1075. {VP8_SET_REFERENCE, vp8e_set_reference},
  1076. {VP8_COPY_REFERENCE, vp8e_get_reference},
  1077. {VP8_SET_POSTPROC, vp8e_set_previewpp},
  1078. {VP8E_UPD_ENTROPY, vp8e_update_entropy},
  1079. {VP8E_UPD_REFERENCE, vp8e_update_reference},
  1080. {VP8E_USE_REFERENCE, vp8e_use_reference},
  1081. {VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags},
  1082. {VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id},
  1083. {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
  1084. {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
  1085. {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
  1086. {VP8E_SET_CPUUSED, set_cpu_used},
  1087. {VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity},
  1088. {VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref},
  1089. {VP8E_SET_SHARPNESS, set_sharpness},
  1090. {VP8E_SET_STATIC_THRESHOLD, set_static_thresh},
  1091. {VP8E_SET_TOKEN_PARTITIONS, set_token_partitions},
  1092. {VP8E_GET_LAST_QUANTIZER, get_quantizer},
  1093. {VP8E_GET_LAST_QUANTIZER_64, get_quantizer64},
  1094. {VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames},
  1095. {VP8E_SET_ARNR_STRENGTH , set_arnr_strength},
  1096. {VP8E_SET_ARNR_TYPE , set_arnr_type},
  1097. {VP8E_SET_TUNING, set_tuning},
  1098. {VP8E_SET_CQ_LEVEL, set_cq_level},
  1099. {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct},
  1100. {VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode},
  1101. { -1, NULL},
  1102. };
  1103. static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
  1104. {
  1105. {
  1106. 0,
  1107. {
  1108. 0, /* g_usage */
  1109. 0, /* g_threads */
  1110. 0, /* g_profile */
  1111. 320, /* g_width */
  1112. 240, /* g_height */
  1113. VPX_BITS_8, /* g_bit_depth */
  1114. 8, /* g_input_bit_depth */
  1115. {1, 30}, /* g_timebase */
  1116. 0, /* g_error_resilient */
  1117. VPX_RC_ONE_PASS, /* g_pass */
  1118. 0, /* g_lag_in_frames */
  1119. 0, /* rc_dropframe_thresh */
  1120. 0, /* rc_resize_allowed */
  1121. 1, /* rc_scaled_width */
  1122. 1, /* rc_scaled_height */
  1123. 60, /* rc_resize_down_thresold */
  1124. 30, /* rc_resize_up_thresold */
  1125. VPX_VBR, /* rc_end_usage */
  1126. {0}, /* rc_twopass_stats_in */
  1127. {0}, /* rc_firstpass_mb_stats_in */
  1128. 256, /* rc_target_bandwidth */
  1129. 4, /* rc_min_quantizer */
  1130. 63, /* rc_max_quantizer */
  1131. 100, /* rc_undershoot_pct */
  1132. 100, /* rc_overshoot_pct */
  1133. 6000, /* rc_max_buffer_size */
  1134. 4000, /* rc_buffer_initial_size; */
  1135. 5000, /* rc_buffer_optimal_size; */
  1136. 50, /* rc_two_pass_vbrbias */
  1137. 0, /* rc_two_pass_vbrmin_section */
  1138. 400, /* rc_two_pass_vbrmax_section */
  1139. /* keyframing settings (kf) */
  1140. VPX_KF_AUTO, /* g_kfmode*/
  1141. 0, /* kf_min_dist */
  1142. 128, /* kf_max_dist */
  1143. VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
  1144. {0},
  1145. {0}, /* ss_target_bitrate */
  1146. 1, /* ts_number_layers */
  1147. {0}, /* ts_target_bitrate */
  1148. {0}, /* ts_rate_decimator */
  1149. 0, /* ts_periodicity */
  1150. {0}, /* ts_layer_id */
  1151. }},
  1152. };
  1153. #ifndef VERSION_STRING
  1154. #define VERSION_STRING
  1155. #endif
  1156. CODEC_INTERFACE(vpx_codec_vp8_cx) =
  1157. {
  1158. "WebM Project VP8 Encoder" VERSION_STRING,
  1159. VPX_CODEC_INTERNAL_ABI_VERSION,
  1160. VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
  1161. VPX_CODEC_CAP_OUTPUT_PARTITION,
  1162. /* vpx_codec_caps_t caps; */
  1163. vp8e_init, /* vpx_codec_init_fn_t init; */
  1164. vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
  1165. vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
  1166. {
  1167. NULL, /* vpx_codec_peek_si_fn_t peek_si; */
  1168. NULL, /* vpx_codec_get_si_fn_t get_si; */
  1169. NULL, /* vpx_codec_decode_fn_t decode; */
  1170. NULL, /* vpx_codec_frame_get_fn_t frame_get; */
  1171. NULL, /* vpx_codec_set_fb_fn_t set_fb_fn; */
  1172. },
  1173. {
  1174. 1, /* 1 cfg map */
  1175. vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t cfg_maps; */
  1176. vp8e_encode, /* vpx_codec_encode_fn_t encode; */
  1177. vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t get_cx_data; */
  1178. vp8e_set_config,
  1179. NULL,
  1180. vp8e_get_preview,
  1181. vp8e_mr_alloc_mem,
  1182. } /* encoder functions */
  1183. };