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.

1044 lines
42KB

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