vp8_cx_iface.c 45 KB

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