2 * Copyright (c) 2010, Google, Inc.
4 * This file is part of FFmpeg.
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.
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.
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
23 * VP8 encoder support via libvpx
26 #define VPX_DISABLE_CTRL_TYPECHECKS 1
27 #define VPX_CODEC_DISABLE_COMPAT 1
28 #include <vpx/vpx_encoder.h>
29 #include <vpx/vp8cx.h>
33 #include "libavutil/avassert.h"
35 #include "libavutil/base64.h"
36 #include "libavutil/common.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/opt.h"
42 * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
43 * One encoded frame returned from the library.
45 struct FrameListData {
46 void *buf; /**< compressed data buffer */
47 size_t sz; /**< length of compressed data */
50 int64_t pts; /**< time stamp to show frame
51 (in timebase units) */
52 unsigned long duration; /**< duration to show frame
53 (in timebase units) */
54 uint32_t flags; /**< flags for this frame */
56 int have_sse; /**< true if we have pending sse[] */
57 uint64_t frame_number;
58 struct FrameListData *next;
61 typedef struct VP8EncoderContext {
63 struct vpx_codec_ctx encoder;
64 struct vpx_image rawimg;
65 struct vpx_codec_ctx encoder_alpha;
66 struct vpx_image rawimg_alpha;
68 struct vpx_fixed_buf twopass_stats;
69 int deadline; //i.e., RT/GOOD/BEST
71 int have_sse; /**< true if we have pending sse[] */
72 uint64_t frame_number;
73 struct FrameListData *coded_frame_list;
77 * VP8 specific flags, see VP8F_* below.
80 #define VP8F_ERROR_RESILIENT 0x00000001 ///< Enable measures appropriate for streaming over lossy links
81 #define VP8F_AUTO_ALT_REF 0x00000002 ///< Enable automatic alternate reference frame generation
94 int rc_undershoot_pct;
105 /** String mappings for enum vp8e_enc_control_id */
106 static const char *const ctlidstr[] = {
107 [VP8E_UPD_ENTROPY] = "VP8E_UPD_ENTROPY",
108 [VP8E_UPD_REFERENCE] = "VP8E_UPD_REFERENCE",
109 [VP8E_USE_REFERENCE] = "VP8E_USE_REFERENCE",
110 [VP8E_SET_ROI_MAP] = "VP8E_SET_ROI_MAP",
111 [VP8E_SET_ACTIVEMAP] = "VP8E_SET_ACTIVEMAP",
112 [VP8E_SET_SCALEMODE] = "VP8E_SET_SCALEMODE",
113 [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED",
114 [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF",
115 [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
116 [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS",
117 [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD",
118 [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS",
119 [VP8E_GET_LAST_QUANTIZER] = "VP8E_GET_LAST_QUANTIZER",
120 [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES",
121 [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH",
122 [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE",
123 [VP8E_SET_CQ_LEVEL] = "VP8E_SET_CQ_LEVEL",
124 [VP8E_SET_MAX_INTRA_BITRATE_PCT] = "VP8E_SET_MAX_INTRA_BITRATE_PCT",
125 #if CONFIG_LIBVPX_VP9_ENCODER
126 [VP9E_SET_LOSSLESS] = "VP9E_SET_LOSSLESS",
127 [VP9E_SET_TILE_COLUMNS] = "VP9E_SET_TILE_COLUMNS",
128 [VP9E_SET_TILE_ROWS] = "VP9E_SET_TILE_ROWS",
129 [VP9E_SET_FRAME_PARALLEL_DECODING] = "VP9E_SET_FRAME_PARALLEL_DECODING",
130 [VP9E_SET_AQ_MODE] = "VP9E_SET_AQ_MODE",
131 #if VPX_ENCODER_ABI_VERSION > 8
132 [VP9E_SET_COLOR_SPACE] = "VP9E_SET_COLOR_SPACE",
137 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
139 VP8Context *ctx = avctx->priv_data;
140 const char *error = vpx_codec_error(&ctx->encoder);
141 const char *detail = vpx_codec_error_detail(&ctx->encoder);
143 av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
145 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
148 static av_cold void dump_enc_cfg(AVCodecContext *avctx,
149 const struct vpx_codec_enc_cfg *cfg)
152 int level = AV_LOG_DEBUG;
154 av_log(avctx, level, "vpx_codec_enc_cfg\n");
155 av_log(avctx, level, "generic settings\n"
156 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
157 #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_IMG_FMT_HIGHBITDEPTH)
160 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
161 width, "g_usage:", cfg->g_usage,
162 width, "g_threads:", cfg->g_threads,
163 width, "g_profile:", cfg->g_profile,
164 width, "g_w:", cfg->g_w,
165 width, "g_h:", cfg->g_h,
166 #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_IMG_FMT_HIGHBITDEPTH)
167 width, "g_bit_depth:", cfg->g_bit_depth,
168 width, "g_input_bit_depth:", cfg->g_input_bit_depth,
170 width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
171 width, "g_error_resilient:", cfg->g_error_resilient,
172 width, "g_pass:", cfg->g_pass,
173 width, "g_lag_in_frames:", cfg->g_lag_in_frames);
174 av_log(avctx, level, "rate control settings\n"
175 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
176 " %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n",
177 width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
178 width, "rc_resize_allowed:", cfg->rc_resize_allowed,
179 width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
180 width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
181 width, "rc_end_usage:", cfg->rc_end_usage,
182 width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
183 width, "rc_target_bitrate:", cfg->rc_target_bitrate);
184 av_log(avctx, level, "quantizer settings\n"
186 width, "rc_min_quantizer:", cfg->rc_min_quantizer,
187 width, "rc_max_quantizer:", cfg->rc_max_quantizer);
188 av_log(avctx, level, "bitrate tolerance\n"
190 width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
191 width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
192 av_log(avctx, level, "decoder buffer model\n"
193 " %*s%u\n %*s%u\n %*s%u\n",
194 width, "rc_buf_sz:", cfg->rc_buf_sz,
195 width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
196 width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
197 av_log(avctx, level, "2 pass rate control settings\n"
198 " %*s%u\n %*s%u\n %*s%u\n",
199 width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
200 width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
201 width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
202 av_log(avctx, level, "keyframing settings\n"
203 " %*s%d\n %*s%u\n %*s%u\n",
204 width, "kf_mode:", cfg->kf_mode,
205 width, "kf_min_dist:", cfg->kf_min_dist,
206 width, "kf_max_dist:", cfg->kf_max_dist);
207 av_log(avctx, level, "\n");
210 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
212 struct FrameListData **p = list;
217 cx_frame->next = NULL;
220 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
222 av_freep(&cx_frame->buf);
223 if (cx_frame->buf_alpha)
224 av_freep(&cx_frame->buf_alpha);
228 static av_cold void free_frame_list(struct FrameListData *list)
230 struct FrameListData *p = list;
239 static av_cold int codecctl_int(AVCodecContext *avctx,
240 enum vp8e_enc_control_id id, int val)
242 VP8Context *ctx = avctx->priv_data;
247 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
248 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
250 res = vpx_codec_control(&ctx->encoder, id, val);
251 if (res != VPX_CODEC_OK) {
252 snprintf(buf, sizeof(buf), "Failed to set %s codec control",
254 log_encoder_error(avctx, buf);
257 return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
260 static av_cold int vp8_free(AVCodecContext *avctx)
262 VP8Context *ctx = avctx->priv_data;
264 vpx_codec_destroy(&ctx->encoder);
266 vpx_codec_destroy(&ctx->encoder_alpha);
267 av_freep(&ctx->twopass_stats.buf);
268 av_freep(&avctx->stats_out);
269 free_frame_list(ctx->coded_frame_list);
273 #if CONFIG_LIBVPX_VP9_ENCODER
274 static int set_pix_fmt(AVCodecContext *avctx, vpx_codec_caps_t codec_caps,
275 struct vpx_codec_enc_cfg *enccfg, vpx_codec_flags_t *flags,
276 vpx_img_fmt_t *img_fmt)
278 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
279 enccfg->g_bit_depth = enccfg->g_input_bit_depth = 8;
281 switch (avctx->pix_fmt) {
282 case AV_PIX_FMT_YUV420P:
283 enccfg->g_profile = 0;
284 *img_fmt = VPX_IMG_FMT_I420;
286 case AV_PIX_FMT_YUV422P:
287 enccfg->g_profile = 1;
288 *img_fmt = VPX_IMG_FMT_I422;
290 #if VPX_IMAGE_ABI_VERSION >= 3
291 case AV_PIX_FMT_YUV440P:
292 enccfg->g_profile = 1;
293 *img_fmt = VPX_IMG_FMT_I440;
296 case AV_PIX_FMT_YUV444P:
297 enccfg->g_profile = 1;
298 *img_fmt = VPX_IMG_FMT_I444;
300 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
301 case AV_PIX_FMT_YUV420P10LE:
302 case AV_PIX_FMT_YUV420P12LE:
303 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
304 enccfg->g_bit_depth = enccfg->g_input_bit_depth =
305 avctx->pix_fmt == AV_PIX_FMT_YUV420P10LE ? 10 : 12;
306 enccfg->g_profile = 2;
307 *img_fmt = VPX_IMG_FMT_I42016;
308 *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
312 case AV_PIX_FMT_YUV422P10LE:
313 case AV_PIX_FMT_YUV422P12LE:
314 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
315 enccfg->g_bit_depth = enccfg->g_input_bit_depth =
316 avctx->pix_fmt == AV_PIX_FMT_YUV422P10LE ? 10 : 12;
317 enccfg->g_profile = 3;
318 *img_fmt = VPX_IMG_FMT_I42216;
319 *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
323 #if VPX_IMAGE_ABI_VERSION >= 3
324 case AV_PIX_FMT_YUV440P10LE:
325 case AV_PIX_FMT_YUV440P12LE:
326 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
327 enccfg->g_bit_depth = enccfg->g_input_bit_depth =
328 avctx->pix_fmt == AV_PIX_FMT_YUV440P10LE ? 10 : 12;
329 enccfg->g_profile = 3;
330 *img_fmt = VPX_IMG_FMT_I44016;
331 *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
336 case AV_PIX_FMT_YUV444P10LE:
337 case AV_PIX_FMT_YUV444P12LE:
338 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
339 enccfg->g_bit_depth = enccfg->g_input_bit_depth =
340 avctx->pix_fmt == AV_PIX_FMT_YUV444P10LE ? 10 : 12;
341 enccfg->g_profile = 3;
342 *img_fmt = VPX_IMG_FMT_I44416;
343 *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
351 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
352 return AVERROR_INVALIDDATA;
355 #if VPX_ENCODER_ABI_VERSION > 8
356 static void set_colorspace(AVCodecContext *avctx)
358 enum vpx_color_space vpx_cs;
360 switch (avctx->colorspace) {
361 case AVCOL_SPC_RGB: vpx_cs = VPX_CS_SRGB; break;
362 case AVCOL_SPC_BT709: vpx_cs = VPX_CS_BT_709; break;
363 case AVCOL_SPC_UNSPECIFIED: vpx_cs = VPX_CS_UNKNOWN; break;
364 case AVCOL_SPC_RESERVED: vpx_cs = VPX_CS_RESERVED; break;
365 case AVCOL_SPC_BT470BG: vpx_cs = VPX_CS_BT_601; break;
366 case AVCOL_SPC_SMPTE170M: vpx_cs = VPX_CS_SMPTE_170; break;
367 case AVCOL_SPC_SMPTE240M: vpx_cs = VPX_CS_SMPTE_240; break;
368 case AVCOL_SPC_BT2020_NCL: vpx_cs = VPX_CS_BT_2020; break;
370 av_log(avctx, AV_LOG_WARNING, "Unsupported colorspace (%d)\n",
374 codecctl_int(avctx, VP9E_SET_COLOR_SPACE, vpx_cs);
379 static av_cold int vpx_init(AVCodecContext *avctx,
380 const struct vpx_codec_iface *iface)
382 VP8Context *ctx = avctx->priv_data;
383 struct vpx_codec_enc_cfg enccfg = { 0 };
384 struct vpx_codec_enc_cfg enccfg_alpha;
385 vpx_codec_flags_t flags = (avctx->flags & AV_CODEC_FLAG_PSNR) ? VPX_CODEC_USE_PSNR : 0;
387 vpx_img_fmt_t img_fmt = VPX_IMG_FMT_I420;
388 #if CONFIG_LIBVPX_VP9_ENCODER
389 vpx_codec_caps_t codec_caps = vpx_codec_get_caps(iface);
392 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
393 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
395 if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P)
398 if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
399 av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
400 vpx_codec_err_to_string(res));
401 return AVERROR(EINVAL);
404 #if CONFIG_LIBVPX_VP9_ENCODER
405 if (avctx->codec_id == AV_CODEC_ID_VP9) {
406 if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
407 return AVERROR(EINVAL);
412 if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
413 av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
414 return AVERROR(EINVAL);
417 dump_enc_cfg(avctx, &enccfg);
419 enccfg.g_w = avctx->width;
420 enccfg.g_h = avctx->height;
421 enccfg.g_timebase.num = avctx->time_base.num;
422 enccfg.g_timebase.den = avctx->time_base.den;
423 enccfg.g_threads = avctx->thread_count;
424 enccfg.g_lag_in_frames= ctx->lag_in_frames;
426 if (avctx->flags & AV_CODEC_FLAG_PASS1)
427 enccfg.g_pass = VPX_RC_FIRST_PASS;
428 else if (avctx->flags & AV_CODEC_FLAG_PASS2)
429 enccfg.g_pass = VPX_RC_LAST_PASS;
431 enccfg.g_pass = VPX_RC_ONE_PASS;
433 if (avctx->rc_min_rate == avctx->rc_max_rate &&
434 avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
435 enccfg.rc_end_usage = VPX_CBR;
436 } else if (ctx->crf >= 0) {
437 enccfg.rc_end_usage = VPX_CQ;
438 #if CONFIG_LIBVPX_VP9_ENCODER
439 if (!avctx->bit_rate && avctx->codec_id == AV_CODEC_ID_VP9)
440 enccfg.rc_end_usage = VPX_Q;
444 if (avctx->bit_rate) {
445 enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
447 #if CONFIG_LIBVPX_VP9_ENCODER
448 } else if (enccfg.rc_end_usage == VPX_Q) {
451 if (enccfg.rc_end_usage == VPX_CQ) {
452 enccfg.rc_target_bitrate = 1000000;
454 avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
455 av_log(avctx, AV_LOG_WARNING,
456 "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
457 enccfg.rc_target_bitrate);
461 if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->lossless == 1) {
462 enccfg.rc_min_quantizer =
463 enccfg.rc_max_quantizer = 0;
465 if (avctx->qmin >= 0)
466 enccfg.rc_min_quantizer = avctx->qmin;
467 if (avctx->qmax >= 0)
468 enccfg.rc_max_quantizer = avctx->qmax;
471 if (enccfg.rc_end_usage == VPX_CQ
472 #if CONFIG_LIBVPX_VP9_ENCODER
473 || enccfg.rc_end_usage == VPX_Q
476 if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
477 av_log(avctx, AV_LOG_ERROR,
478 "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
479 ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
480 return AVERROR(EINVAL);
484 enccfg.rc_dropframe_thresh = avctx->frame_skip_threshold;
486 //0-100 (0 => CBR, 100 => VBR)
487 enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
489 enccfg.rc_2pass_vbr_minsection_pct =
490 avctx->rc_min_rate * 100LL / avctx->bit_rate;
491 if (avctx->rc_max_rate)
492 enccfg.rc_2pass_vbr_maxsection_pct =
493 avctx->rc_max_rate * 100LL / avctx->bit_rate;
495 if (avctx->rc_buffer_size)
497 avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
498 if (avctx->rc_initial_buffer_occupancy)
499 enccfg.rc_buf_initial_sz =
500 avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
501 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
503 FF_DISABLE_DEPRECATION_WARNINGS
504 if (avctx->rc_buffer_aggressivity != 1.0) {
505 av_log(avctx, AV_LOG_WARNING, "The rc_buffer_aggressivity option is "
506 "deprecated, use the undershoot-pct private option instead.\n");
507 enccfg.rc_undershoot_pct = round(avctx->rc_buffer_aggressivity * 100);
509 FF_ENABLE_DEPRECATION_WARNINGS
511 if (ctx->rc_undershoot_pct >= 0)
512 enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct;
513 if (ctx->rc_overshoot_pct >= 0)
514 enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct;
516 //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO
517 if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
518 enccfg.kf_min_dist = avctx->keyint_min;
519 if (avctx->gop_size >= 0)
520 enccfg.kf_max_dist = avctx->gop_size;
522 if (enccfg.g_pass == VPX_RC_FIRST_PASS)
523 enccfg.g_lag_in_frames = 0;
524 else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
525 int decode_size, ret;
527 if (!avctx->stats_in) {
528 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
529 return AVERROR_INVALIDDATA;
532 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
533 ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
535 av_log(avctx, AV_LOG_ERROR,
536 "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
537 ctx->twopass_stats.sz);
538 ctx->twopass_stats.sz = 0;
541 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
542 ctx->twopass_stats.sz);
543 if (decode_size < 0) {
544 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
545 return AVERROR_INVALIDDATA;
548 ctx->twopass_stats.sz = decode_size;
549 enccfg.rc_twopass_stats_in = ctx->twopass_stats;
552 /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
553 complexity playback on low powered devices at the expense of encode
555 if (avctx->profile != FF_PROFILE_UNKNOWN)
556 enccfg.g_profile = avctx->profile;
558 enccfg.g_error_resilient = ctx->error_resilient || ctx->flags & VP8F_ERROR_RESILIENT;
560 dump_enc_cfg(avctx, &enccfg);
561 /* Construct Encoder Context */
562 res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
563 if (res != VPX_CODEC_OK) {
564 log_encoder_error(avctx, "Failed to initialize encoder");
565 return AVERROR(EINVAL);
569 enccfg_alpha = enccfg;
570 res = vpx_codec_enc_init(&ctx->encoder_alpha, iface, &enccfg_alpha, flags);
571 if (res != VPX_CODEC_OK) {
572 log_encoder_error(avctx, "Failed to initialize alpha encoder");
573 return AVERROR(EINVAL);
577 //codec control failures are currently treated only as warnings
578 av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
579 codecctl_int(avctx, VP8E_SET_CPUUSED, ctx->cpu_used);
580 if (ctx->flags & VP8F_AUTO_ALT_REF)
581 ctx->auto_alt_ref = 1;
582 if (ctx->auto_alt_ref >= 0)
583 codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
584 if (ctx->arnr_max_frames >= 0)
585 codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
586 if (ctx->arnr_strength >= 0)
587 codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH, ctx->arnr_strength);
588 if (ctx->arnr_type >= 0)
589 codecctl_int(avctx, VP8E_SET_ARNR_TYPE, ctx->arnr_type);
591 if (CONFIG_LIBVPX_VP8_ENCODER && avctx->codec_id == AV_CODEC_ID_VP8) {
592 codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
593 codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
596 FF_DISABLE_DEPRECATION_WARNINGS
597 if (avctx->mb_threshold) {
598 av_log(avctx, AV_LOG_WARNING, "The mb_threshold option is deprecated, "
599 "use the static-thresh private option instead.\n");
600 ctx->static_thresh = avctx->mb_threshold;
602 FF_ENABLE_DEPRECATION_WARNINGS
604 codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD, ctx->static_thresh);
606 codecctl_int(avctx, VP8E_SET_CQ_LEVEL, ctx->crf);
607 if (ctx->max_intra_rate >= 0)
608 codecctl_int(avctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, ctx->max_intra_rate);
610 #if CONFIG_LIBVPX_VP9_ENCODER
611 if (avctx->codec_id == AV_CODEC_ID_VP9) {
612 if (ctx->lossless >= 0)
613 codecctl_int(avctx, VP9E_SET_LOSSLESS, ctx->lossless);
614 if (ctx->tile_columns >= 0)
615 codecctl_int(avctx, VP9E_SET_TILE_COLUMNS, ctx->tile_columns);
616 if (ctx->tile_rows >= 0)
617 codecctl_int(avctx, VP9E_SET_TILE_ROWS, ctx->tile_rows);
618 if (ctx->frame_parallel >= 0)
619 codecctl_int(avctx, VP9E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
620 if (ctx->aq_mode >= 0)
621 codecctl_int(avctx, VP9E_SET_AQ_MODE, ctx->aq_mode);
622 #if VPX_ENCODER_ABI_VERSION > 8
623 set_colorspace(avctx);
628 av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline);
630 //provide dummy value to initialize wrapper, values will be updated each _encode()
631 vpx_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
633 #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_IMG_FMT_HIGHBITDEPTH)
634 if (avctx->codec_id == AV_CODEC_ID_VP9 && (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH))
635 ctx->rawimg.bit_depth = enccfg.g_bit_depth;
639 vpx_img_wrap(&ctx->rawimg_alpha, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
645 static inline void cx_pktcpy(struct FrameListData *dst,
646 const struct vpx_codec_cx_pkt *src,
647 const struct vpx_codec_cx_pkt *src_alpha,
650 dst->pts = src->data.frame.pts;
651 dst->duration = src->data.frame.duration;
652 dst->flags = src->data.frame.flags;
653 dst->sz = src->data.frame.sz;
654 dst->buf = src->data.frame.buf;
656 /* For alt-ref frame, don't store PSNR or increment frame_number */
657 if (!(dst->flags & VPX_FRAME_IS_INVISIBLE)) {
658 dst->frame_number = ++ctx->frame_number;
659 dst->have_sse = ctx->have_sse;
661 /* associate last-seen SSE to the frame. */
662 /* Transfers ownership from ctx to dst. */
663 /* WARNING! This makes the assumption that PSNR_PKT comes
664 just before the frame it refers to! */
665 memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
669 dst->frame_number = -1; /* sanity marker */
672 dst->buf_alpha = src_alpha->data.frame.buf;
673 dst->sz_alpha = src_alpha->data.frame.sz;
675 dst->buf_alpha = NULL;
681 * Store coded frame information in format suitable for return from encode2().
683 * Write information from @a cx_frame to @a pkt
684 * @return packet data size on success
685 * @return a negative AVERROR on error
687 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
690 int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0);
694 memcpy(pkt->data, cx_frame->buf, pkt->size);
695 pkt->pts = pkt->dts = cx_frame->pts;
696 #if FF_API_CODED_FRAME
697 FF_DISABLE_DEPRECATION_WARNINGS
698 avctx->coded_frame->pts = cx_frame->pts;
699 avctx->coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
700 FF_ENABLE_DEPRECATION_WARNINGS
703 if (!!(cx_frame->flags & VPX_FRAME_IS_KEY)) {
704 pict_type = AV_PICTURE_TYPE_I;
705 #if FF_API_CODED_FRAME
706 FF_DISABLE_DEPRECATION_WARNINGS
707 avctx->coded_frame->pict_type = pict_type;
708 FF_ENABLE_DEPRECATION_WARNINGS
710 pkt->flags |= AV_PKT_FLAG_KEY;
712 pict_type = AV_PICTURE_TYPE_P;
713 #if FF_API_CODED_FRAME
714 FF_DISABLE_DEPRECATION_WARNINGS
715 avctx->coded_frame->pict_type = pict_type;
716 FF_ENABLE_DEPRECATION_WARNINGS
720 ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1,
721 cx_frame->have_sse ? 3 : 0, pict_type);
723 if (cx_frame->have_sse) {
725 /* Beware of the Y/U/V/all order! */
726 #if FF_API_CODED_FRAME
727 FF_DISABLE_DEPRECATION_WARNINGS
728 avctx->coded_frame->error[0] = cx_frame->sse[1];
729 avctx->coded_frame->error[1] = cx_frame->sse[2];
730 avctx->coded_frame->error[2] = cx_frame->sse[3];
731 avctx->coded_frame->error[3] = 0; // alpha
732 FF_ENABLE_DEPRECATION_WARNINGS
734 for (i = 0; i < 3; ++i) {
735 avctx->error[i] += cx_frame->sse[i + 1];
737 cx_frame->have_sse = 0;
739 if (cx_frame->sz_alpha > 0) {
740 side_data = av_packet_new_side_data(pkt,
741 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
742 cx_frame->sz_alpha + 8);
746 return AVERROR(ENOMEM);
748 AV_WB64(side_data, 1);
749 memcpy(side_data + 8, cx_frame->buf_alpha, cx_frame->sz_alpha);
758 * Queue multiple output frames from the encoder, returning the front-most.
759 * In cases where vpx_codec_get_cx_data() returns more than 1 frame append
760 * the frame queue. Return the head frame if available.
761 * @return Stored frame size
762 * @return AVERROR(EINVAL) on output size error
763 * @return AVERROR(ENOMEM) on coded frame queue data allocation error
765 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
767 VP8Context *ctx = avctx->priv_data;
768 const struct vpx_codec_cx_pkt *pkt;
769 const struct vpx_codec_cx_pkt *pkt_alpha = NULL;
770 const void *iter = NULL;
771 const void *iter_alpha = NULL;
774 if (ctx->coded_frame_list) {
775 struct FrameListData *cx_frame = ctx->coded_frame_list;
776 /* return the leading frame if we've already begun queueing */
777 size = storeframe(avctx, cx_frame, pkt_out);
780 ctx->coded_frame_list = cx_frame->next;
781 free_coded_frame(cx_frame);
784 /* consume all available output from the encoder before returning. buffers
785 are only good through the next vpx_codec call */
786 while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter)) &&
788 (ctx->is_alpha && (pkt_alpha = vpx_codec_get_cx_data(&ctx->encoder_alpha, &iter_alpha))))) {
790 case VPX_CODEC_CX_FRAME_PKT:
792 struct FrameListData cx_frame;
794 /* avoid storing the frame when the list is empty and we haven't yet
795 provided a frame for output */
796 av_assert0(!ctx->coded_frame_list);
797 cx_pktcpy(&cx_frame, pkt, pkt_alpha, ctx);
798 size = storeframe(avctx, &cx_frame, pkt_out);
802 struct FrameListData *cx_frame =
803 av_malloc(sizeof(struct FrameListData));
806 av_log(avctx, AV_LOG_ERROR,
807 "Frame queue element alloc failed\n");
808 return AVERROR(ENOMEM);
810 cx_pktcpy(cx_frame, pkt, pkt_alpha, ctx);
811 cx_frame->buf = av_malloc(cx_frame->sz);
813 if (!cx_frame->buf) {
814 av_log(avctx, AV_LOG_ERROR,
815 "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
818 return AVERROR(ENOMEM);
820 memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
822 cx_frame->buf_alpha = av_malloc(cx_frame->sz_alpha);
823 if (!cx_frame->buf_alpha) {
824 av_log(avctx, AV_LOG_ERROR,
825 "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
828 return AVERROR(ENOMEM);
830 memcpy(cx_frame->buf_alpha, pkt_alpha->data.frame.buf, pkt_alpha->data.frame.sz);
832 coded_frame_add(&ctx->coded_frame_list, cx_frame);
835 case VPX_CODEC_STATS_PKT: {
836 struct vpx_fixed_buf *stats = &ctx->twopass_stats;
838 if ((err = av_reallocp(&stats->buf,
840 pkt->data.twopass_stats.sz)) < 0) {
842 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
845 memcpy((uint8_t*)stats->buf + stats->sz,
846 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
847 stats->sz += pkt->data.twopass_stats.sz;
850 case VPX_CODEC_PSNR_PKT:
851 av_assert0(!ctx->have_sse);
852 ctx->sse[0] = pkt->data.psnr.sse[0];
853 ctx->sse[1] = pkt->data.psnr.sse[1];
854 ctx->sse[2] = pkt->data.psnr.sse[2];
855 ctx->sse[3] = pkt->data.psnr.sse[3];
858 case VPX_CODEC_CUSTOM_PKT:
859 //ignore unsupported/unrecognized packet types
867 static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt,
868 const AVFrame *frame, int *got_packet)
870 VP8Context *ctx = avctx->priv_data;
871 struct vpx_image *rawimg = NULL;
872 struct vpx_image *rawimg_alpha = NULL;
873 int64_t timestamp = 0;
875 vpx_enc_frame_flags_t flags = 0;
878 rawimg = &ctx->rawimg;
879 rawimg->planes[VPX_PLANE_Y] = frame->data[0];
880 rawimg->planes[VPX_PLANE_U] = frame->data[1];
881 rawimg->planes[VPX_PLANE_V] = frame->data[2];
882 rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
883 rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
884 rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
886 uint8_t *u_plane, *v_plane;
887 rawimg_alpha = &ctx->rawimg_alpha;
888 rawimg_alpha->planes[VPX_PLANE_Y] = frame->data[3];
889 u_plane = av_malloc(frame->linesize[1] * frame->height);
890 v_plane = av_malloc(frame->linesize[2] * frame->height);
891 if (!u_plane || !v_plane) {
894 return AVERROR(ENOMEM);
896 memset(u_plane, 0x80, frame->linesize[1] * frame->height);
897 rawimg_alpha->planes[VPX_PLANE_U] = u_plane;
898 memset(v_plane, 0x80, frame->linesize[2] * frame->height);
899 rawimg_alpha->planes[VPX_PLANE_V] = v_plane;
900 rawimg_alpha->stride[VPX_PLANE_Y] = frame->linesize[0];
901 rawimg_alpha->stride[VPX_PLANE_U] = frame->linesize[1];
902 rawimg_alpha->stride[VPX_PLANE_V] = frame->linesize[2];
904 timestamp = frame->pts;
905 if (frame->pict_type == AV_PICTURE_TYPE_I)
906 flags |= VPX_EFLAG_FORCE_KF;
909 res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
910 avctx->ticks_per_frame, flags, ctx->deadline);
911 if (res != VPX_CODEC_OK) {
912 log_encoder_error(avctx, "Error encoding frame");
913 return AVERROR_INVALIDDATA;
917 res = vpx_codec_encode(&ctx->encoder_alpha, rawimg_alpha, timestamp,
918 avctx->ticks_per_frame, flags, ctx->deadline);
919 if (res != VPX_CODEC_OK) {
920 log_encoder_error(avctx, "Error encoding alpha frame");
921 return AVERROR_INVALIDDATA;
925 coded_size = queue_frames(avctx, pkt);
927 if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
928 unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
930 avctx->stats_out = av_malloc(b64_size);
931 if (!avctx->stats_out) {
932 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
934 return AVERROR(ENOMEM);
936 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
937 ctx->twopass_stats.sz);
941 av_freep(&rawimg_alpha->planes[VPX_PLANE_U]);
942 av_freep(&rawimg_alpha->planes[VPX_PLANE_V]);
945 *got_packet = !!coded_size;
949 #define OFFSET(x) offsetof(VP8Context, x)
950 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
952 #ifndef VPX_ERROR_RESILIENT_DEFAULT
953 #define VPX_ERROR_RESILIENT_DEFAULT 1
954 #define VPX_ERROR_RESILIENT_PARTITIONS 2
957 #define COMMON_OPTIONS \
958 { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
959 { "auto-alt-ref", "Enable use of alternate reference " \
960 "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE}, \
961 { "lag-in-frames", "Number of frames to look ahead for " \
962 "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
963 { "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
964 { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
965 { "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"}, \
966 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" }, \
967 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" }, \
968 { "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" }, \
969 { "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
970 { "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"}, \
971 { "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"}, \
972 { "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"}, \
973 { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, \
974 { "max-intra-rate", "Maximum I-frame bitrate (pct) 0=unlimited", OFFSET(max_intra_rate), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
975 { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, \
976 { "partitions", "The frame partitions are independently decodable " \
977 "by the bool decoder, meaning that partitions can be decoded even " \
978 "though earlier partitions have been lost. Note that intra predicition" \
979 " is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"}, \
980 { "crf", "Select the quality for constant quality mode", offsetof(VP8Context, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE }, \
981 { "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 }, \
982 { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, VE }, \
983 { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1000, VE }, \
985 #define LEGACY_OPTIONS \
986 {"speed", "", offsetof(VP8Context, cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
987 {"quality", "", offsetof(VP8Context, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
988 {"vp8flags", "", offsetof(VP8Context, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, UINT_MAX, VE, "flags"}, \
989 {"error_resilient", "enable error resilience", 0, AV_OPT_TYPE_CONST, {.i64 = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"}, \
990 {"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"}, \
991 {"arnr_max_frames", "altref noise reduction max frame count", offsetof(VP8Context, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE}, \
992 {"arnr_strength", "altref noise reduction filter strength", offsetof(VP8Context, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE}, \
993 {"arnr_type", "altref noise reduction filter type", offsetof(VP8Context, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE}, \
994 {"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}, \
996 #if CONFIG_LIBVPX_VP8_ENCODER
997 static const AVOption vp8_options[] = {
1004 #if CONFIG_LIBVPX_VP9_ENCODER
1005 static const AVOption vp9_options[] = {
1007 { "lossless", "Lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
1008 { "tile-columns", "Number of tile columns to use, log2", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1009 { "tile-rows", "Number of tile rows to use, log2", OFFSET(tile_rows), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
1010 { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
1011 { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 3, VE, "aq_mode"},
1012 { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode" },
1013 { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode" },
1014 { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode" },
1015 { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode" },
1021 #undef COMMON_OPTIONS
1022 #undef LEGACY_OPTIONS
1024 static const AVCodecDefault defaults[] = {
1028 { "keyint_min", "-1" },
1032 #if CONFIG_LIBVPX_VP8_ENCODER
1033 static av_cold int vp8_init(AVCodecContext *avctx)
1035 return vpx_init(avctx, vpx_codec_vp8_cx());
1038 static const AVClass class_vp8 = {
1039 .class_name = "libvpx-vp8 encoder",
1040 .item_name = av_default_item_name,
1041 .option = vp8_options,
1042 .version = LIBAVUTIL_VERSION_INT,
1045 AVCodec ff_libvpx_vp8_encoder = {
1047 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
1048 .type = AVMEDIA_TYPE_VIDEO,
1049 .id = AV_CODEC_ID_VP8,
1050 .priv_data_size = sizeof(VP8Context),
1052 .encode2 = vp8_encode,
1054 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
1055 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE },
1056 .priv_class = &class_vp8,
1057 .defaults = defaults,
1059 #endif /* CONFIG_LIBVPX_VP8_ENCODER */
1061 #if CONFIG_LIBVPX_VP9_ENCODER
1062 static av_cold int vp9_init(AVCodecContext *avctx)
1064 return vpx_init(avctx, vpx_codec_vp9_cx());
1067 static const AVClass class_vp9 = {
1068 .class_name = "libvpx-vp9 encoder",
1069 .item_name = av_default_item_name,
1070 .option = vp9_options,
1071 .version = LIBAVUTIL_VERSION_INT,
1074 static const AVProfile profiles[] = {
1075 { FF_PROFILE_VP9_0, "Profile 0" },
1076 { FF_PROFILE_VP9_1, "Profile 1" },
1077 { FF_PROFILE_VP9_2, "Profile 2" },
1078 { FF_PROFILE_VP9_3, "Profile 3" },
1079 { FF_PROFILE_UNKNOWN },
1082 AVCodec ff_libvpx_vp9_encoder = {
1083 .name = "libvpx-vp9",
1084 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
1085 .type = AVMEDIA_TYPE_VIDEO,
1086 .id = AV_CODEC_ID_VP9,
1087 .priv_data_size = sizeof(VP8Context),
1089 .encode2 = vp8_encode,
1091 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
1092 .profiles = NULL_IF_CONFIG_SMALL(profiles),
1093 .priv_class = &class_vp9,
1094 .defaults = defaults,
1095 .init_static_data = ff_vp9_init_static,
1097 #endif /* CONFIG_LIBVPX_VP9_ENCODER */