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.

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