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.

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