You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1006 lines
40KB

  1. /*
  2. * Copyright (c) 2010, Google, Inc.
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * VP8 encoder support via libvpx
  23. */
  24. #define VPX_DISABLE_CTRL_TYPECHECKS 1
  25. #define VPX_CODEC_DISABLE_COMPAT 1
  26. #include <vpx/vpx_encoder.h>
  27. #include <vpx/vp8cx.h>
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. #include "libavutil/avassert.h"
  31. #include "libvpx.h"
  32. #include "libavutil/base64.h"
  33. #include "libavutil/common.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/opt.h"
  37. /**
  38. * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
  39. * One encoded frame returned from the library.
  40. */
  41. struct FrameListData {
  42. void *buf; /**< compressed data buffer */
  43. size_t sz; /**< length of compressed data */
  44. void *buf_alpha;
  45. size_t sz_alpha;
  46. int64_t pts; /**< time stamp to show frame
  47. (in timebase units) */
  48. unsigned long duration; /**< duration to show frame
  49. (in timebase units) */
  50. uint32_t flags; /**< flags for this frame */
  51. uint64_t sse[4];
  52. int have_sse; /**< true if we have pending sse[] */
  53. uint64_t frame_number;
  54. struct FrameListData *next;
  55. };
  56. typedef struct VP8EncoderContext {
  57. AVClass *class;
  58. struct vpx_codec_ctx encoder;
  59. struct vpx_image rawimg;
  60. struct vpx_codec_ctx encoder_alpha;
  61. struct vpx_image rawimg_alpha;
  62. uint8_t is_alpha;
  63. struct vpx_fixed_buf twopass_stats;
  64. int deadline; //i.e., RT/GOOD/BEST
  65. uint64_t sse[4];
  66. int have_sse; /**< true if we have pending sse[] */
  67. uint64_t frame_number;
  68. struct FrameListData *coded_frame_list;
  69. int cpu_used;
  70. /**
  71. * VP8 specific flags, see VP8F_* below.
  72. */
  73. int flags;
  74. #define VP8F_ERROR_RESILIENT 0x00000001 ///< Enable measures appropriate for streaming over lossy links
  75. #define VP8F_AUTO_ALT_REF 0x00000002 ///< Enable automatic alternate reference frame generation
  76. int auto_alt_ref;
  77. int arnr_max_frames;
  78. int arnr_strength;
  79. int arnr_type;
  80. int lag_in_frames;
  81. int error_resilient;
  82. int crf;
  83. int static_thresh;
  84. int max_intra_rate;
  85. // VP9-only
  86. int lossless;
  87. int tile_columns;
  88. int tile_rows;
  89. int frame_parallel;
  90. int aq_mode;
  91. } VP8Context;
  92. /** String mappings for enum vp8e_enc_control_id */
  93. static const char *const ctlidstr[] = {
  94. [VP8E_UPD_ENTROPY] = "VP8E_UPD_ENTROPY",
  95. [VP8E_UPD_REFERENCE] = "VP8E_UPD_REFERENCE",
  96. [VP8E_USE_REFERENCE] = "VP8E_USE_REFERENCE",
  97. [VP8E_SET_ROI_MAP] = "VP8E_SET_ROI_MAP",
  98. [VP8E_SET_ACTIVEMAP] = "VP8E_SET_ACTIVEMAP",
  99. [VP8E_SET_SCALEMODE] = "VP8E_SET_SCALEMODE",
  100. [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED",
  101. [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF",
  102. [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
  103. [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS",
  104. [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD",
  105. [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS",
  106. [VP8E_GET_LAST_QUANTIZER] = "VP8E_GET_LAST_QUANTIZER",
  107. [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES",
  108. [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH",
  109. [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE",
  110. [VP8E_SET_CQ_LEVEL] = "VP8E_SET_CQ_LEVEL",
  111. [VP8E_SET_MAX_INTRA_BITRATE_PCT] = "VP8E_SET_MAX_INTRA_BITRATE_PCT",
  112. #if CONFIG_LIBVPX_VP9_ENCODER
  113. [VP9E_SET_LOSSLESS] = "VP9E_SET_LOSSLESS",
  114. [VP9E_SET_TILE_COLUMNS] = "VP9E_SET_TILE_COLUMNS",
  115. [VP9E_SET_TILE_ROWS] = "VP9E_SET_TILE_ROWS",
  116. [VP9E_SET_FRAME_PARALLEL_DECODING] = "VP9E_SET_FRAME_PARALLEL_DECODING",
  117. [VP9E_SET_AQ_MODE] = "VP9E_SET_AQ_MODE",
  118. #endif
  119. };
  120. static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
  121. {
  122. VP8Context *ctx = avctx->priv_data;
  123. const char *error = vpx_codec_error(&ctx->encoder);
  124. const char *detail = vpx_codec_error_detail(&ctx->encoder);
  125. av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
  126. if (detail)
  127. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
  128. }
  129. static av_cold void dump_enc_cfg(AVCodecContext *avctx,
  130. const struct vpx_codec_enc_cfg *cfg)
  131. {
  132. int width = -30;
  133. int level = AV_LOG_DEBUG;
  134. av_log(avctx, level, "vpx_codec_enc_cfg\n");
  135. av_log(avctx, level, "generic settings\n"
  136. " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
  137. #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_IMG_FMT_HIGHBITDEPTH)
  138. " %*s%u\n %*s%u\n"
  139. #endif
  140. " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
  141. width, "g_usage:", cfg->g_usage,
  142. width, "g_threads:", cfg->g_threads,
  143. width, "g_profile:", cfg->g_profile,
  144. width, "g_w:", cfg->g_w,
  145. width, "g_h:", cfg->g_h,
  146. #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_IMG_FMT_HIGHBITDEPTH)
  147. width, "g_bit_depth:", cfg->g_bit_depth,
  148. width, "g_input_bit_depth:", cfg->g_input_bit_depth,
  149. #endif
  150. width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
  151. width, "g_error_resilient:", cfg->g_error_resilient,
  152. width, "g_pass:", cfg->g_pass,
  153. width, "g_lag_in_frames:", cfg->g_lag_in_frames);
  154. av_log(avctx, level, "rate control settings\n"
  155. " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
  156. " %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n",
  157. width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
  158. width, "rc_resize_allowed:", cfg->rc_resize_allowed,
  159. width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
  160. width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
  161. width, "rc_end_usage:", cfg->rc_end_usage,
  162. width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
  163. width, "rc_target_bitrate:", cfg->rc_target_bitrate);
  164. av_log(avctx, level, "quantizer settings\n"
  165. " %*s%u\n %*s%u\n",
  166. width, "rc_min_quantizer:", cfg->rc_min_quantizer,
  167. width, "rc_max_quantizer:", cfg->rc_max_quantizer);
  168. av_log(avctx, level, "bitrate tolerance\n"
  169. " %*s%u\n %*s%u\n",
  170. width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
  171. width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
  172. av_log(avctx, level, "decoder buffer model\n"
  173. " %*s%u\n %*s%u\n %*s%u\n",
  174. width, "rc_buf_sz:", cfg->rc_buf_sz,
  175. width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
  176. width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
  177. av_log(avctx, level, "2 pass rate control settings\n"
  178. " %*s%u\n %*s%u\n %*s%u\n",
  179. width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
  180. width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
  181. width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
  182. av_log(avctx, level, "keyframing settings\n"
  183. " %*s%d\n %*s%u\n %*s%u\n",
  184. width, "kf_mode:", cfg->kf_mode,
  185. width, "kf_min_dist:", cfg->kf_min_dist,
  186. width, "kf_max_dist:", cfg->kf_max_dist);
  187. av_log(avctx, level, "\n");
  188. }
  189. static void coded_frame_add(void *list, struct FrameListData *cx_frame)
  190. {
  191. struct FrameListData **p = list;
  192. while (*p)
  193. p = &(*p)->next;
  194. *p = cx_frame;
  195. cx_frame->next = NULL;
  196. }
  197. static av_cold void free_coded_frame(struct FrameListData *cx_frame)
  198. {
  199. av_freep(&cx_frame->buf);
  200. if (cx_frame->buf_alpha)
  201. av_freep(&cx_frame->buf_alpha);
  202. av_freep(&cx_frame);
  203. }
  204. static av_cold void free_frame_list(struct FrameListData *list)
  205. {
  206. struct FrameListData *p = list;
  207. while (p) {
  208. list = list->next;
  209. free_coded_frame(p);
  210. p = list;
  211. }
  212. }
  213. static av_cold int codecctl_int(AVCodecContext *avctx,
  214. enum vp8e_enc_control_id id, int val)
  215. {
  216. VP8Context *ctx = avctx->priv_data;
  217. char buf[80];
  218. int width = -30;
  219. int res;
  220. snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
  221. av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
  222. res = vpx_codec_control(&ctx->encoder, id, val);
  223. if (res != VPX_CODEC_OK) {
  224. snprintf(buf, sizeof(buf), "Failed to set %s codec control",
  225. ctlidstr[id]);
  226. log_encoder_error(avctx, buf);
  227. }
  228. return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
  229. }
  230. static av_cold int vp8_free(AVCodecContext *avctx)
  231. {
  232. VP8Context *ctx = avctx->priv_data;
  233. vpx_codec_destroy(&ctx->encoder);
  234. if (ctx->is_alpha)
  235. vpx_codec_destroy(&ctx->encoder_alpha);
  236. av_freep(&ctx->twopass_stats.buf);
  237. av_frame_free(&avctx->coded_frame);
  238. av_freep(&avctx->stats_out);
  239. free_frame_list(ctx->coded_frame_list);
  240. return 0;
  241. }
  242. #if CONFIG_LIBVPX_VP9_ENCODER
  243. static int set_pix_fmt(AVCodecContext *avctx, vpx_codec_caps_t codec_caps,
  244. struct vpx_codec_enc_cfg *enccfg, vpx_codec_flags_t *flags,
  245. vpx_img_fmt_t *img_fmt) {
  246. #ifdef VPX_IMG_FMT_HIGHBITDEPTH
  247. enccfg->g_bit_depth = enccfg->g_input_bit_depth = 8;
  248. #endif
  249. switch (avctx->pix_fmt) {
  250. case AV_PIX_FMT_YUV420P:
  251. enccfg->g_profile = 0;
  252. *img_fmt = VPX_IMG_FMT_I420;
  253. return 0;
  254. case AV_PIX_FMT_YUV422P:
  255. case AV_PIX_FMT_YUV444P:
  256. enccfg->g_profile = 1;
  257. *img_fmt = avctx->pix_fmt == AV_PIX_FMT_YUV422P ? VPX_IMG_FMT_I422 : VPX_IMG_FMT_I444;
  258. return 0;
  259. #ifdef VPX_IMG_FMT_HIGHBITDEPTH
  260. case AV_PIX_FMT_YUV420P10LE:
  261. case AV_PIX_FMT_YUV420P12LE:
  262. if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
  263. enccfg->g_bit_depth = enccfg->g_input_bit_depth =
  264. avctx->pix_fmt == AV_PIX_FMT_YUV420P10LE ? 10 : 12;
  265. enccfg->g_profile = 2;
  266. *img_fmt = VPX_IMG_FMT_I42016;
  267. *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
  268. return 0;
  269. }
  270. break;
  271. case AV_PIX_FMT_YUV422P10LE:
  272. case AV_PIX_FMT_YUV422P12LE:
  273. if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
  274. enccfg->g_bit_depth = enccfg->g_input_bit_depth =
  275. avctx->pix_fmt == AV_PIX_FMT_YUV422P10LE ? 10 : 12;
  276. enccfg->g_profile = 3;
  277. *img_fmt = VPX_IMG_FMT_I42216;
  278. *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
  279. return 0;
  280. }
  281. break;
  282. case AV_PIX_FMT_YUV444P10LE:
  283. case AV_PIX_FMT_YUV444P12LE:
  284. if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
  285. enccfg->g_bit_depth = enccfg->g_input_bit_depth =
  286. avctx->pix_fmt == AV_PIX_FMT_YUV444P10LE ? 10 : 12;
  287. enccfg->g_profile = 3;
  288. *img_fmt = VPX_IMG_FMT_I44416;
  289. *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
  290. return 0;
  291. }
  292. break;
  293. #endif
  294. default:
  295. break;
  296. }
  297. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
  298. return AVERROR_INVALIDDATA;
  299. }
  300. #endif
  301. static av_cold int vpx_init(AVCodecContext *avctx,
  302. const struct vpx_codec_iface *iface)
  303. {
  304. VP8Context *ctx = avctx->priv_data;
  305. struct vpx_codec_enc_cfg enccfg;
  306. struct vpx_codec_enc_cfg enccfg_alpha;
  307. vpx_codec_flags_t flags = (avctx->flags & CODEC_FLAG_PSNR) ? VPX_CODEC_USE_PSNR : 0;
  308. int res;
  309. vpx_img_fmt_t img_fmt = VPX_IMG_FMT_I420;
  310. #if CONFIG_LIBVPX_VP9_ENCODER
  311. vpx_codec_caps_t codec_caps = vpx_codec_get_caps(iface);
  312. #endif
  313. av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
  314. av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
  315. if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P)
  316. ctx->is_alpha = 1;
  317. if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
  318. av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
  319. vpx_codec_err_to_string(res));
  320. return AVERROR(EINVAL);
  321. }
  322. #if CONFIG_LIBVPX_VP9_ENCODER
  323. if (avctx->codec_id == AV_CODEC_ID_VP9) {
  324. if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
  325. return AVERROR(EINVAL);
  326. }
  327. #endif
  328. if(!avctx->bit_rate)
  329. if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
  330. av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
  331. return AVERROR(EINVAL);
  332. }
  333. dump_enc_cfg(avctx, &enccfg);
  334. enccfg.g_w = avctx->width;
  335. enccfg.g_h = avctx->height;
  336. enccfg.g_timebase.num = avctx->time_base.num;
  337. enccfg.g_timebase.den = avctx->time_base.den;
  338. enccfg.g_threads = avctx->thread_count;
  339. enccfg.g_lag_in_frames= ctx->lag_in_frames;
  340. if (avctx->flags & CODEC_FLAG_PASS1)
  341. enccfg.g_pass = VPX_RC_FIRST_PASS;
  342. else if (avctx->flags & CODEC_FLAG_PASS2)
  343. enccfg.g_pass = VPX_RC_LAST_PASS;
  344. else
  345. enccfg.g_pass = VPX_RC_ONE_PASS;
  346. if (avctx->rc_min_rate == avctx->rc_max_rate &&
  347. avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
  348. enccfg.rc_end_usage = VPX_CBR;
  349. } else if (ctx->crf >= 0) {
  350. enccfg.rc_end_usage = VPX_CQ;
  351. #if CONFIG_LIBVPX_VP9_ENCODER
  352. if (!avctx->bit_rate && avctx->codec_id == AV_CODEC_ID_VP9)
  353. enccfg.rc_end_usage = VPX_Q;
  354. #endif
  355. }
  356. if (avctx->bit_rate) {
  357. enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
  358. AV_ROUND_NEAR_INF);
  359. #if CONFIG_LIBVPX_VP9_ENCODER
  360. } else if (enccfg.rc_end_usage == VPX_Q) {
  361. #endif
  362. } else {
  363. if (enccfg.rc_end_usage == VPX_CQ) {
  364. enccfg.rc_target_bitrate = 1000000;
  365. } else {
  366. avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
  367. av_log(avctx, AV_LOG_WARNING,
  368. "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
  369. enccfg.rc_target_bitrate);
  370. }
  371. }
  372. if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->lossless == 1) {
  373. enccfg.rc_min_quantizer =
  374. enccfg.rc_max_quantizer = 0;
  375. } else {
  376. if (avctx->qmin >= 0)
  377. enccfg.rc_min_quantizer = avctx->qmin;
  378. if (avctx->qmax >= 0)
  379. enccfg.rc_max_quantizer = avctx->qmax;
  380. }
  381. if (enccfg.rc_end_usage == VPX_CQ
  382. #if CONFIG_LIBVPX_VP9_ENCODER
  383. || enccfg.rc_end_usage == VPX_Q
  384. #endif
  385. ) {
  386. if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
  387. av_log(avctx, AV_LOG_ERROR,
  388. "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
  389. ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
  390. return AVERROR(EINVAL);
  391. }
  392. }
  393. enccfg.rc_dropframe_thresh = avctx->frame_skip_threshold;
  394. //0-100 (0 => CBR, 100 => VBR)
  395. enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
  396. if (avctx->bit_rate)
  397. enccfg.rc_2pass_vbr_minsection_pct =
  398. avctx->rc_min_rate * 100LL / avctx->bit_rate;
  399. if (avctx->rc_max_rate)
  400. enccfg.rc_2pass_vbr_maxsection_pct =
  401. avctx->rc_max_rate * 100LL / avctx->bit_rate;
  402. if (avctx->rc_buffer_size)
  403. enccfg.rc_buf_sz =
  404. avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
  405. if (avctx->rc_initial_buffer_occupancy)
  406. enccfg.rc_buf_initial_sz =
  407. avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
  408. enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
  409. enccfg.rc_undershoot_pct = round(avctx->rc_buffer_aggressivity * 100);
  410. //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO
  411. if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
  412. enccfg.kf_min_dist = avctx->keyint_min;
  413. if (avctx->gop_size >= 0)
  414. enccfg.kf_max_dist = avctx->gop_size;
  415. if (enccfg.g_pass == VPX_RC_FIRST_PASS)
  416. enccfg.g_lag_in_frames = 0;
  417. else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
  418. int decode_size, ret;
  419. if (!avctx->stats_in) {
  420. av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
  421. return AVERROR_INVALIDDATA;
  422. }
  423. ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
  424. ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
  425. if (ret < 0) {
  426. av_log(avctx, AV_LOG_ERROR,
  427. "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
  428. ctx->twopass_stats.sz);
  429. ctx->twopass_stats.sz = 0;
  430. return ret;
  431. }
  432. decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
  433. ctx->twopass_stats.sz);
  434. if (decode_size < 0) {
  435. av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
  436. return AVERROR_INVALIDDATA;
  437. }
  438. ctx->twopass_stats.sz = decode_size;
  439. enccfg.rc_twopass_stats_in = ctx->twopass_stats;
  440. }
  441. /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
  442. complexity playback on low powered devices at the expense of encode
  443. quality. */
  444. if (avctx->profile != FF_PROFILE_UNKNOWN)
  445. enccfg.g_profile = avctx->profile;
  446. enccfg.g_error_resilient = ctx->error_resilient || ctx->flags & VP8F_ERROR_RESILIENT;
  447. dump_enc_cfg(avctx, &enccfg);
  448. /* Construct Encoder Context */
  449. res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
  450. if (res != VPX_CODEC_OK) {
  451. log_encoder_error(avctx, "Failed to initialize encoder");
  452. return AVERROR(EINVAL);
  453. }
  454. if (ctx->is_alpha) {
  455. enccfg_alpha = enccfg;
  456. res = vpx_codec_enc_init(&ctx->encoder_alpha, iface, &enccfg_alpha, flags);
  457. if (res != VPX_CODEC_OK) {
  458. log_encoder_error(avctx, "Failed to initialize alpha encoder");
  459. return AVERROR(EINVAL);
  460. }
  461. }
  462. //codec control failures are currently treated only as warnings
  463. av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
  464. codecctl_int(avctx, VP8E_SET_CPUUSED, ctx->cpu_used);
  465. if (ctx->flags & VP8F_AUTO_ALT_REF)
  466. ctx->auto_alt_ref = 1;
  467. if (ctx->auto_alt_ref >= 0)
  468. codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
  469. if (ctx->arnr_max_frames >= 0)
  470. codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
  471. if (ctx->arnr_strength >= 0)
  472. codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH, ctx->arnr_strength);
  473. if (ctx->arnr_type >= 0)
  474. codecctl_int(avctx, VP8E_SET_ARNR_TYPE, ctx->arnr_type);
  475. if (avctx->codec_id == AV_CODEC_ID_VP8) {
  476. codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
  477. codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
  478. }
  479. #if FF_API_MPV_OPT
  480. FF_DISABLE_DEPRECATION_WARNINGS
  481. if (avctx->mb_threshold) {
  482. av_log(avctx, AV_LOG_WARNING, "The mb_threshold option is deprecated, "
  483. "use the static-thresh private option instead.\n");
  484. ctx->static_thresh = avctx->mb_threshold;
  485. }
  486. FF_ENABLE_DEPRECATION_WARNINGS
  487. #endif
  488. codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD, ctx->static_thresh);
  489. if (ctx->crf >= 0)
  490. codecctl_int(avctx, VP8E_SET_CQ_LEVEL, ctx->crf);
  491. if (ctx->max_intra_rate >= 0)
  492. codecctl_int(avctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, ctx->max_intra_rate);
  493. #if CONFIG_LIBVPX_VP9_ENCODER
  494. if (avctx->codec_id == AV_CODEC_ID_VP9) {
  495. if (ctx->lossless >= 0)
  496. codecctl_int(avctx, VP9E_SET_LOSSLESS, ctx->lossless);
  497. if (ctx->tile_columns >= 0)
  498. codecctl_int(avctx, VP9E_SET_TILE_COLUMNS, ctx->tile_columns);
  499. if (ctx->tile_rows >= 0)
  500. codecctl_int(avctx, VP9E_SET_TILE_ROWS, ctx->tile_rows);
  501. if (ctx->frame_parallel >= 0)
  502. codecctl_int(avctx, VP9E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
  503. if (ctx->aq_mode >= 0)
  504. codecctl_int(avctx, VP9E_SET_AQ_MODE, ctx->aq_mode);
  505. }
  506. #endif
  507. av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline);
  508. //provide dummy value to initialize wrapper, values will be updated each _encode()
  509. vpx_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
  510. (unsigned char*)1);
  511. #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_IMG_FMT_HIGHBITDEPTH)
  512. if (avctx->codec_id == AV_CODEC_ID_VP9 && (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH))
  513. ctx->rawimg.bit_depth = enccfg.g_bit_depth;
  514. #endif
  515. if (ctx->is_alpha)
  516. vpx_img_wrap(&ctx->rawimg_alpha, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
  517. (unsigned char*)1);
  518. avctx->coded_frame = av_frame_alloc();
  519. if (!avctx->coded_frame) {
  520. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  521. vp8_free(avctx);
  522. return AVERROR(ENOMEM);
  523. }
  524. return 0;
  525. }
  526. static inline void cx_pktcpy(struct FrameListData *dst,
  527. const struct vpx_codec_cx_pkt *src,
  528. const struct vpx_codec_cx_pkt *src_alpha,
  529. VP8Context *ctx)
  530. {
  531. dst->pts = src->data.frame.pts;
  532. dst->duration = src->data.frame.duration;
  533. dst->flags = src->data.frame.flags;
  534. dst->sz = src->data.frame.sz;
  535. dst->buf = src->data.frame.buf;
  536. dst->have_sse = 0;
  537. /* For alt-ref frame, don't store PSNR or increment frame_number */
  538. if (!(dst->flags & VPX_FRAME_IS_INVISIBLE)) {
  539. dst->frame_number = ++ctx->frame_number;
  540. dst->have_sse = ctx->have_sse;
  541. if (ctx->have_sse) {
  542. /* associate last-seen SSE to the frame. */
  543. /* Transfers ownership from ctx to dst. */
  544. /* WARNING! This makes the assumption that PSNR_PKT comes
  545. just before the frame it refers to! */
  546. memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
  547. ctx->have_sse = 0;
  548. }
  549. } else {
  550. dst->frame_number = -1; /* sanity marker */
  551. }
  552. if (src_alpha) {
  553. dst->buf_alpha = src_alpha->data.frame.buf;
  554. dst->sz_alpha = src_alpha->data.frame.sz;
  555. }
  556. else {
  557. dst->buf_alpha = NULL;
  558. dst->sz_alpha = 0;
  559. }
  560. }
  561. /**
  562. * Store coded frame information in format suitable for return from encode2().
  563. *
  564. * Write information from @a cx_frame to @a pkt
  565. * @return packet data size on success
  566. * @return a negative AVERROR on error
  567. */
  568. static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
  569. AVPacket *pkt, AVFrame *coded_frame)
  570. {
  571. int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz);
  572. uint8_t *side_data;
  573. if (ret >= 0) {
  574. memcpy(pkt->data, cx_frame->buf, pkt->size);
  575. pkt->pts = pkt->dts = cx_frame->pts;
  576. coded_frame->pts = cx_frame->pts;
  577. coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
  578. if (coded_frame->key_frame) {
  579. coded_frame->pict_type = AV_PICTURE_TYPE_I;
  580. pkt->flags |= AV_PKT_FLAG_KEY;
  581. } else
  582. coded_frame->pict_type = AV_PICTURE_TYPE_P;
  583. if (cx_frame->have_sse) {
  584. int i;
  585. /* Beware of the Y/U/V/all order! */
  586. coded_frame->error[0] = cx_frame->sse[1];
  587. coded_frame->error[1] = cx_frame->sse[2];
  588. coded_frame->error[2] = cx_frame->sse[3];
  589. coded_frame->error[3] = 0; // alpha
  590. for (i = 0; i < 4; ++i) {
  591. avctx->error[i] += coded_frame->error[i];
  592. }
  593. cx_frame->have_sse = 0;
  594. }
  595. if (cx_frame->sz_alpha > 0) {
  596. side_data = av_packet_new_side_data(pkt,
  597. AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
  598. cx_frame->sz_alpha + 8);
  599. if(!side_data) {
  600. av_free_packet(pkt);
  601. av_free(pkt);
  602. return AVERROR(ENOMEM);
  603. }
  604. AV_WB64(side_data, 1);
  605. memcpy(side_data + 8, cx_frame->buf_alpha, cx_frame->sz_alpha);
  606. }
  607. } else {
  608. return ret;
  609. }
  610. return pkt->size;
  611. }
  612. /**
  613. * Queue multiple output frames from the encoder, returning the front-most.
  614. * In cases where vpx_codec_get_cx_data() returns more than 1 frame append
  615. * the frame queue. Return the head frame if available.
  616. * @return Stored frame size
  617. * @return AVERROR(EINVAL) on output size error
  618. * @return AVERROR(ENOMEM) on coded frame queue data allocation error
  619. */
  620. static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out,
  621. AVFrame *coded_frame)
  622. {
  623. VP8Context *ctx = avctx->priv_data;
  624. const struct vpx_codec_cx_pkt *pkt;
  625. const struct vpx_codec_cx_pkt *pkt_alpha = NULL;
  626. const void *iter = NULL;
  627. const void *iter_alpha = NULL;
  628. int size = 0;
  629. if (ctx->coded_frame_list) {
  630. struct FrameListData *cx_frame = ctx->coded_frame_list;
  631. /* return the leading frame if we've already begun queueing */
  632. size = storeframe(avctx, cx_frame, pkt_out, coded_frame);
  633. if (size < 0)
  634. return size;
  635. ctx->coded_frame_list = cx_frame->next;
  636. free_coded_frame(cx_frame);
  637. }
  638. /* consume all available output from the encoder before returning. buffers
  639. are only good through the next vpx_codec call */
  640. while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter)) &&
  641. (!ctx->is_alpha ||
  642. (ctx->is_alpha && (pkt_alpha = vpx_codec_get_cx_data(&ctx->encoder_alpha, &iter_alpha))))) {
  643. switch (pkt->kind) {
  644. case VPX_CODEC_CX_FRAME_PKT:
  645. if (!size) {
  646. struct FrameListData cx_frame;
  647. /* avoid storing the frame when the list is empty and we haven't yet
  648. provided a frame for output */
  649. av_assert0(!ctx->coded_frame_list);
  650. cx_pktcpy(&cx_frame, pkt, pkt_alpha, ctx);
  651. size = storeframe(avctx, &cx_frame, pkt_out, coded_frame);
  652. if (size < 0)
  653. return size;
  654. } else {
  655. struct FrameListData *cx_frame =
  656. av_malloc(sizeof(struct FrameListData));
  657. if (!cx_frame) {
  658. av_log(avctx, AV_LOG_ERROR,
  659. "Frame queue element alloc failed\n");
  660. return AVERROR(ENOMEM);
  661. }
  662. cx_pktcpy(cx_frame, pkt, pkt_alpha, ctx);
  663. cx_frame->buf = av_malloc(cx_frame->sz);
  664. if (!cx_frame->buf) {
  665. av_log(avctx, AV_LOG_ERROR,
  666. "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
  667. cx_frame->sz);
  668. av_freep(&cx_frame);
  669. return AVERROR(ENOMEM);
  670. }
  671. memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
  672. if (ctx->is_alpha) {
  673. cx_frame->buf_alpha = av_malloc(cx_frame->sz_alpha);
  674. if (!cx_frame->buf_alpha) {
  675. av_log(avctx, AV_LOG_ERROR,
  676. "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
  677. cx_frame->sz_alpha);
  678. av_free(cx_frame);
  679. return AVERROR(ENOMEM);
  680. }
  681. memcpy(cx_frame->buf_alpha, pkt_alpha->data.frame.buf, pkt_alpha->data.frame.sz);
  682. }
  683. coded_frame_add(&ctx->coded_frame_list, cx_frame);
  684. }
  685. break;
  686. case VPX_CODEC_STATS_PKT: {
  687. struct vpx_fixed_buf *stats = &ctx->twopass_stats;
  688. int err;
  689. if ((err = av_reallocp(&stats->buf,
  690. stats->sz +
  691. pkt->data.twopass_stats.sz)) < 0) {
  692. stats->sz = 0;
  693. av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
  694. return err;
  695. }
  696. memcpy((uint8_t*)stats->buf + stats->sz,
  697. pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
  698. stats->sz += pkt->data.twopass_stats.sz;
  699. break;
  700. }
  701. case VPX_CODEC_PSNR_PKT:
  702. av_assert0(!ctx->have_sse);
  703. ctx->sse[0] = pkt->data.psnr.sse[0];
  704. ctx->sse[1] = pkt->data.psnr.sse[1];
  705. ctx->sse[2] = pkt->data.psnr.sse[2];
  706. ctx->sse[3] = pkt->data.psnr.sse[3];
  707. ctx->have_sse = 1;
  708. break;
  709. case VPX_CODEC_CUSTOM_PKT:
  710. //ignore unsupported/unrecognized packet types
  711. break;
  712. }
  713. }
  714. return size;
  715. }
  716. static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt,
  717. const AVFrame *frame, int *got_packet)
  718. {
  719. VP8Context *ctx = avctx->priv_data;
  720. struct vpx_image *rawimg = NULL;
  721. struct vpx_image *rawimg_alpha = NULL;
  722. int64_t timestamp = 0;
  723. int res, coded_size;
  724. vpx_enc_frame_flags_t flags = 0;
  725. if (frame) {
  726. rawimg = &ctx->rawimg;
  727. rawimg->planes[VPX_PLANE_Y] = frame->data[0];
  728. rawimg->planes[VPX_PLANE_U] = frame->data[1];
  729. rawimg->planes[VPX_PLANE_V] = frame->data[2];
  730. rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
  731. rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
  732. rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
  733. if (ctx->is_alpha) {
  734. uint8_t *u_plane, *v_plane;
  735. rawimg_alpha = &ctx->rawimg_alpha;
  736. rawimg_alpha->planes[VPX_PLANE_Y] = frame->data[3];
  737. u_plane = av_malloc(frame->linesize[1] * frame->height);
  738. v_plane = av_malloc(frame->linesize[2] * frame->height);
  739. if (!u_plane || !v_plane) {
  740. av_free(u_plane);
  741. av_free(v_plane);
  742. return AVERROR(ENOMEM);
  743. }
  744. memset(u_plane, 0x80, frame->linesize[1] * frame->height);
  745. rawimg_alpha->planes[VPX_PLANE_U] = u_plane;
  746. memset(v_plane, 0x80, frame->linesize[2] * frame->height);
  747. rawimg_alpha->planes[VPX_PLANE_V] = v_plane;
  748. rawimg_alpha->stride[VPX_PLANE_Y] = frame->linesize[0];
  749. rawimg_alpha->stride[VPX_PLANE_U] = frame->linesize[1];
  750. rawimg_alpha->stride[VPX_PLANE_V] = frame->linesize[2];
  751. }
  752. timestamp = frame->pts;
  753. if (frame->pict_type == AV_PICTURE_TYPE_I)
  754. flags |= VPX_EFLAG_FORCE_KF;
  755. }
  756. res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
  757. avctx->ticks_per_frame, flags, ctx->deadline);
  758. if (res != VPX_CODEC_OK) {
  759. log_encoder_error(avctx, "Error encoding frame");
  760. return AVERROR_INVALIDDATA;
  761. }
  762. if (ctx->is_alpha) {
  763. res = vpx_codec_encode(&ctx->encoder_alpha, rawimg_alpha, timestamp,
  764. avctx->ticks_per_frame, flags, ctx->deadline);
  765. if (res != VPX_CODEC_OK) {
  766. log_encoder_error(avctx, "Error encoding alpha frame");
  767. return AVERROR_INVALIDDATA;
  768. }
  769. }
  770. coded_size = queue_frames(avctx, pkt, avctx->coded_frame);
  771. if (!frame && avctx->flags & CODEC_FLAG_PASS1) {
  772. unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
  773. avctx->stats_out = av_malloc(b64_size);
  774. if (!avctx->stats_out) {
  775. av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
  776. b64_size);
  777. return AVERROR(ENOMEM);
  778. }
  779. av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
  780. ctx->twopass_stats.sz);
  781. }
  782. if (rawimg_alpha) {
  783. av_freep(&rawimg_alpha->planes[VPX_PLANE_U]);
  784. av_freep(&rawimg_alpha->planes[VPX_PLANE_V]);
  785. }
  786. *got_packet = !!coded_size;
  787. return 0;
  788. }
  789. #define OFFSET(x) offsetof(VP8Context, x)
  790. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  791. #ifndef VPX_ERROR_RESILIENT_DEFAULT
  792. #define VPX_ERROR_RESILIENT_DEFAULT 1
  793. #define VPX_ERROR_RESILIENT_PARTITIONS 2
  794. #endif
  795. #define COMMON_OPTIONS \
  796. { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
  797. { "auto-alt-ref", "Enable use of alternate reference " \
  798. "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE}, \
  799. { "lag-in-frames", "Number of frames to look ahead for " \
  800. "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
  801. { "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
  802. { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
  803. { "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"}, \
  804. { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" }, \
  805. { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" }, \
  806. { "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" }, \
  807. { "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
  808. { "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"}, \
  809. { "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"}, \
  810. { "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"}, \
  811. { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, \
  812. { "max-intra-rate", "Maximum I-frame bitrate (pct) 0=unlimited", OFFSET(max_intra_rate), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
  813. { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, \
  814. { "partitions", "The frame partitions are independently decodable " \
  815. "by the bool decoder, meaning that partitions can be decoded even " \
  816. "though earlier partitions have been lost. Note that intra predicition" \
  817. " is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"}, \
  818. { "crf", "Select the quality for constant quality mode", offsetof(VP8Context, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE }, \
  819. { "static-thresh", "A change threshold on blocks below which they will be skipped by the encoder", OFFSET(static_thresh), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, \
  820. #define LEGACY_OPTIONS \
  821. {"speed", "", offsetof(VP8Context, cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
  822. {"quality", "", offsetof(VP8Context, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
  823. {"vp8flags", "", offsetof(VP8Context, flags), FF_OPT_TYPE_FLAGS, {.i64 = 0}, 0, UINT_MAX, VE, "flags"}, \
  824. {"error_resilient", "enable error resilience", 0, FF_OPT_TYPE_CONST, {.dbl = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"}, \
  825. {"altref", "enable use of alternate reference frames (VP8/2-pass only)", 0, FF_OPT_TYPE_CONST, {.dbl = VP8F_AUTO_ALT_REF}, INT_MIN, INT_MAX, VE, "flags"}, \
  826. {"arnr_max_frames", "altref noise reduction max frame count", offsetof(VP8Context, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE}, \
  827. {"arnr_strength", "altref noise reduction filter strength", offsetof(VP8Context, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE}, \
  828. {"arnr_type", "altref noise reduction filter type", offsetof(VP8Context, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE}, \
  829. {"rc_lookahead", "Number of frames to look ahead for alternate reference frame selection", offsetof(VP8Context, lag_in_frames), AV_OPT_TYPE_INT, {.i64 = 25}, 0, 25, VE}, \
  830. #if CONFIG_LIBVPX_VP8_ENCODER
  831. static const AVOption vp8_options[] = {
  832. COMMON_OPTIONS
  833. LEGACY_OPTIONS
  834. { NULL }
  835. };
  836. #endif
  837. #if CONFIG_LIBVPX_VP9_ENCODER
  838. static const AVOption vp9_options[] = {
  839. COMMON_OPTIONS
  840. { "lossless", "Lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
  841. { "tile-columns", "Number of tile columns to use, log2", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
  842. { "tile-rows", "Number of tile rows to use, log2", OFFSET(tile_rows), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
  843. { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
  844. { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 3, VE, "aq_mode"},
  845. { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode" }, \
  846. { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode" }, \
  847. { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode" }, \
  848. { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode" }, \
  849. LEGACY_OPTIONS
  850. { NULL }
  851. };
  852. #endif
  853. #undef COMMON_OPTIONS
  854. #undef LEGACY_OPTIONS
  855. static const AVCodecDefault defaults[] = {
  856. { "qmin", "-1" },
  857. { "qmax", "-1" },
  858. { "g", "-1" },
  859. { "keyint_min", "-1" },
  860. { NULL },
  861. };
  862. #if CONFIG_LIBVPX_VP8_ENCODER
  863. static av_cold int vp8_init(AVCodecContext *avctx)
  864. {
  865. return vpx_init(avctx, vpx_codec_vp8_cx());
  866. }
  867. static const AVClass class_vp8 = {
  868. .class_name = "libvpx-vp8 encoder",
  869. .item_name = av_default_item_name,
  870. .option = vp8_options,
  871. .version = LIBAVUTIL_VERSION_INT,
  872. };
  873. AVCodec ff_libvpx_vp8_encoder = {
  874. .name = "libvpx",
  875. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  876. .type = AVMEDIA_TYPE_VIDEO,
  877. .id = AV_CODEC_ID_VP8,
  878. .priv_data_size = sizeof(VP8Context),
  879. .init = vp8_init,
  880. .encode2 = vp8_encode,
  881. .close = vp8_free,
  882. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  883. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE },
  884. .priv_class = &class_vp8,
  885. .defaults = defaults,
  886. };
  887. #endif /* CONFIG_LIBVPX_VP8_ENCODER */
  888. #if CONFIG_LIBVPX_VP9_ENCODER
  889. static av_cold int vp9_init(AVCodecContext *avctx)
  890. {
  891. return vpx_init(avctx, vpx_codec_vp9_cx());
  892. }
  893. static const AVClass class_vp9 = {
  894. .class_name = "libvpx-vp9 encoder",
  895. .item_name = av_default_item_name,
  896. .option = vp9_options,
  897. .version = LIBAVUTIL_VERSION_INT,
  898. };
  899. AVCodec ff_libvpx_vp9_encoder = {
  900. .name = "libvpx-vp9",
  901. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
  902. .type = AVMEDIA_TYPE_VIDEO,
  903. .id = AV_CODEC_ID_VP9,
  904. .priv_data_size = sizeof(VP8Context),
  905. .init = vp9_init,
  906. .encode2 = vp8_encode,
  907. .close = vp8_free,
  908. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  909. .priv_class = &class_vp9,
  910. .defaults = defaults,
  911. .init_static_data = ff_vp9_init_static,
  912. };
  913. #endif /* CONFIG_LIBVPX_VP9_ENCODER */