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.

1090 lines
43KB

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