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.

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