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>
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;
80 #define VP8F_ERROR_RESILIENT 0x00000001
81 #define VP8F_AUTO_ALT_REF 0x00000002
104 [VP8E_UPD_ENTROPY] =
"VP8E_UPD_ENTROPY",
105 [VP8E_UPD_REFERENCE] =
"VP8E_UPD_REFERENCE",
106 [VP8E_USE_REFERENCE] =
"VP8E_USE_REFERENCE",
107 [VP8E_SET_ROI_MAP] =
"VP8E_SET_ROI_MAP",
108 [VP8E_SET_ACTIVEMAP] =
"VP8E_SET_ACTIVEMAP",
109 [VP8E_SET_SCALEMODE] =
"VP8E_SET_SCALEMODE",
110 [VP8E_SET_CPUUSED] =
"VP8E_SET_CPUUSED",
111 [VP8E_SET_ENABLEAUTOALTREF] =
"VP8E_SET_ENABLEAUTOALTREF",
112 [VP8E_SET_NOISE_SENSITIVITY] =
"VP8E_SET_NOISE_SENSITIVITY",
113 [VP8E_SET_SHARPNESS] =
"VP8E_SET_SHARPNESS",
114 [VP8E_SET_STATIC_THRESHOLD] =
"VP8E_SET_STATIC_THRESHOLD",
115 [VP8E_SET_TOKEN_PARTITIONS] =
"VP8E_SET_TOKEN_PARTITIONS",
116 [VP8E_GET_LAST_QUANTIZER] =
"VP8E_GET_LAST_QUANTIZER",
117 [VP8E_SET_ARNR_MAXFRAMES] =
"VP8E_SET_ARNR_MAXFRAMES",
118 [VP8E_SET_ARNR_STRENGTH] =
"VP8E_SET_ARNR_STRENGTH",
119 [VP8E_SET_ARNR_TYPE] =
"VP8E_SET_ARNR_TYPE",
120 [VP8E_SET_CQ_LEVEL] =
"VP8E_SET_CQ_LEVEL",
121 [VP8E_SET_MAX_INTRA_BITRATE_PCT] =
"VP8E_SET_MAX_INTRA_BITRATE_PCT",
122 #if CONFIG_LIBVPX_VP9_ENCODER
123 [VP9E_SET_LOSSLESS] =
"VP9E_SET_LOSSLESS",
124 [VP9E_SET_TILE_COLUMNS] =
"VP9E_SET_TILE_COLUMNS",
125 [VP9E_SET_TILE_ROWS] =
"VP9E_SET_TILE_ROWS",
126 [VP9E_SET_FRAME_PARALLEL_DECODING] =
"VP9E_SET_FRAME_PARALLEL_DECODING",
127 [VP9E_SET_AQ_MODE] =
"VP9E_SET_AQ_MODE",
134 const char *error = vpx_codec_error(&ctx->
encoder);
135 const char *detail = vpx_codec_error_detail(&ctx->
encoder);
143 const struct vpx_codec_enc_cfg *cfg)
148 av_log(avctx, level,
"vpx_codec_enc_cfg\n");
149 av_log(avctx, level,
"generic settings\n"
150 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
151 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
152 width,
"g_usage:", cfg->g_usage,
153 width,
"g_threads:", cfg->g_threads,
154 width,
"g_profile:", cfg->g_profile,
155 width,
"g_w:", cfg->g_w,
156 width,
"g_h:", cfg->g_h,
157 width,
"g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
158 width,
"g_error_resilient:", cfg->g_error_resilient,
159 width,
"g_pass:", cfg->g_pass,
160 width,
"g_lag_in_frames:", cfg->g_lag_in_frames);
161 av_log(avctx, level,
"rate control settings\n"
162 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
164 width,
"rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
165 width,
"rc_resize_allowed:", cfg->rc_resize_allowed,
166 width,
"rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
167 width,
"rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
168 width,
"rc_end_usage:", cfg->rc_end_usage,
169 width,
"rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
170 width,
"rc_target_bitrate:", cfg->rc_target_bitrate);
171 av_log(avctx, level,
"quantizer settings\n"
173 width,
"rc_min_quantizer:", cfg->rc_min_quantizer,
174 width,
"rc_max_quantizer:", cfg->rc_max_quantizer);
175 av_log(avctx, level,
"bitrate tolerance\n"
177 width,
"rc_undershoot_pct:", cfg->rc_undershoot_pct,
178 width,
"rc_overshoot_pct:", cfg->rc_overshoot_pct);
179 av_log(avctx, level,
"decoder buffer model\n"
180 " %*s%u\n %*s%u\n %*s%u\n",
181 width,
"rc_buf_sz:", cfg->rc_buf_sz,
182 width,
"rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
183 width,
"rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
184 av_log(avctx, level,
"2 pass rate control settings\n"
185 " %*s%u\n %*s%u\n %*s%u\n",
186 width,
"rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
187 width,
"rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
188 width,
"rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
189 av_log(avctx, level,
"keyframing settings\n"
190 " %*s%d\n %*s%u\n %*s%u\n",
191 width,
"kf_mode:", cfg->kf_mode,
192 width,
"kf_min_dist:", cfg->kf_min_dist,
193 width,
"kf_max_dist:", cfg->kf_max_dist);
194 av_log(avctx, level,
"\n");
204 cx_frame->
next = NULL;
227 enum vp8e_enc_control_id
id,
int val)
237 res = vpx_codec_control(&ctx->
encoder,
id, val);
238 if (res != VPX_CODEC_OK) {
239 snprintf(buf,
sizeof(buf),
"Failed to set %s codec control",
244 return res == VPX_CODEC_OK ? 0 :
AVERROR(EINVAL);
251 vpx_codec_destroy(&ctx->
encoder);
262 const struct vpx_codec_iface *iface)
265 struct vpx_codec_enc_cfg enccfg;
266 struct vpx_codec_enc_cfg enccfg_alpha;
276 if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
278 vpx_codec_err_to_string(res));
290 enccfg.g_w = avctx->
width;
291 enccfg.g_h = avctx->
height;
298 enccfg.g_pass = VPX_RC_FIRST_PASS;
300 enccfg.g_pass = VPX_RC_LAST_PASS;
302 enccfg.g_pass = VPX_RC_ONE_PASS;
306 enccfg.rc_end_usage = VPX_CBR;
307 }
else if (ctx->
crf >= 0) {
308 enccfg.rc_end_usage = VPX_CQ;
309 #if CONFIG_LIBVPX_VP9_ENCODER
311 enccfg.rc_end_usage = VPX_Q;
318 #if CONFIG_LIBVPX_VP9_ENCODER
319 }
else if (enccfg.rc_end_usage == VPX_Q) {
322 if (enccfg.rc_end_usage == VPX_CQ) {
323 enccfg.rc_target_bitrate = 1000000;
325 avctx->
bit_rate = enccfg.rc_target_bitrate * 1000;
327 "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
328 enccfg.rc_target_bitrate);
332 if (avctx->
qmin >= 0)
333 enccfg.rc_min_quantizer = avctx->
qmin;
334 if (avctx->
qmax >= 0)
335 enccfg.rc_max_quantizer = avctx->
qmax;
337 if (enccfg.rc_end_usage == VPX_CQ
338 #
if CONFIG_LIBVPX_VP9_ENCODER
339 || enccfg.rc_end_usage == VPX_Q
342 if (ctx->
crf < enccfg.rc_min_quantizer || ctx->
crf > enccfg.rc_max_quantizer) {
344 "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
345 ctx->
crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
355 enccfg.rc_2pass_vbr_minsection_pct =
358 enccfg.rc_2pass_vbr_maxsection_pct =
365 enccfg.rc_buf_initial_sz =
367 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
374 enccfg.kf_max_dist = avctx->
gop_size;
376 if (enccfg.g_pass == VPX_RC_FIRST_PASS)
377 enccfg.g_lag_in_frames = 0;
378 else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
396 if (decode_size < 0) {
409 enccfg.g_profile = avctx->
profile;
415 res = vpx_codec_enc_init(&ctx->
encoder, iface, &enccfg, flags);
416 if (res != VPX_CODEC_OK) {
422 enccfg_alpha = enccfg;
423 res = vpx_codec_enc_init(&ctx->
encoder_alpha, iface, &enccfg_alpha, flags);
424 if (res != VPX_CODEC_OK) {
452 #if CONFIG_LIBVPX_VP9_ENCODER
487 const struct vpx_codec_cx_pkt *
src,
488 const struct vpx_codec_cx_pkt *src_alpha,
491 dst->
pts = src->data.frame.pts;
492 dst->
duration = src->data.frame.duration;
493 dst->
flags = src->data.frame.flags;
494 dst->
sz = src->data.frame.sz;
495 dst->
buf = src->data.frame.buf;
498 if (!(dst->
flags & VPX_FRAME_IS_INVISIBLE)) {
506 memcpy(dst->
sse, ctx->
sse,
sizeof(dst->
sse));
513 dst->
buf_alpha = src_alpha->data.frame.buf;
514 dst->
sz_alpha = src_alpha->data.frame.sz;
537 coded_frame->
pts = cx_frame->
pts;
549 coded_frame->
error[0] = cx_frame->
sse[1];
550 coded_frame->
error[1] = cx_frame->
sse[2];
551 coded_frame->
error[2] = cx_frame->
sse[3];
552 coded_frame->
error[3] = 0;
553 for (i = 0; i < 4; ++i) {
588 const struct vpx_codec_cx_pkt *
pkt;
589 const struct vpx_codec_cx_pkt *pkt_alpha = NULL;
590 const void *iter = NULL;
591 const void *iter_alpha = NULL;
597 size =
storeframe(avctx, cx_frame, pkt_out, coded_frame);
606 while ((pkt = vpx_codec_get_cx_data(&ctx->
encoder, &iter)) &&
610 case VPX_CODEC_CX_FRAME_PKT:
617 cx_pktcpy(&cx_frame, pkt, pkt_alpha, ctx);
618 size =
storeframe(avctx, &cx_frame, pkt_out, coded_frame);
627 "Frame queue element alloc failed\n");
630 cx_pktcpy(cx_frame, pkt, pkt_alpha, ctx);
633 if (!cx_frame->
buf) {
640 memcpy(cx_frame->
buf, pkt->data.frame.buf, pkt->data.frame.sz);
650 memcpy(cx_frame->
buf_alpha, pkt_alpha->data.frame.buf, pkt_alpha->data.frame.sz);
655 case VPX_CODEC_STATS_PKT: {
660 pkt->data.twopass_stats.sz)) < 0) {
665 memcpy((
uint8_t*)stats->buf + stats->sz,
666 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
667 stats->sz += pkt->data.twopass_stats.sz;
670 case VPX_CODEC_PSNR_PKT:
672 ctx->
sse[0] = pkt->data.psnr.sse[0];
673 ctx->
sse[1] = pkt->data.psnr.sse[1];
674 ctx->
sse[2] = pkt->data.psnr.sse[2];
675 ctx->
sse[3] = pkt->data.psnr.sse[3];
678 case VPX_CODEC_CUSTOM_PKT:
691 struct vpx_image *rawimg = NULL;
692 struct vpx_image *rawimg_alpha = NULL;
693 int64_t timestamp = 0;
695 vpx_enc_frame_flags_t
flags = 0;
699 rawimg->planes[VPX_PLANE_Y] = frame->
data[0];
700 rawimg->planes[VPX_PLANE_U] = frame->
data[1];
701 rawimg->planes[VPX_PLANE_V] = frame->
data[2];
702 rawimg->stride[VPX_PLANE_Y] = frame->
linesize[0];
703 rawimg->stride[VPX_PLANE_U] = frame->
linesize[1];
704 rawimg->stride[VPX_PLANE_V] = frame->
linesize[2];
708 rawimg_alpha->planes[VPX_PLANE_Y] = frame->
data[3];
711 rawimg_alpha->planes[VPX_PLANE_U] = u_plane;
714 rawimg_alpha->planes[VPX_PLANE_V] = v_plane;
715 rawimg_alpha->stride[VPX_PLANE_Y] = frame->
linesize[0];
716 rawimg_alpha->stride[VPX_PLANE_U] = frame->
linesize[1];
717 rawimg_alpha->stride[VPX_PLANE_V] = frame->
linesize[2];
719 timestamp = frame->
pts;
721 flags |= VPX_EFLAG_FORCE_KF;
724 res = vpx_codec_encode(&ctx->
encoder, rawimg, timestamp,
726 if (res != VPX_CODEC_OK) {
732 res = vpx_codec_encode(&ctx->
encoder_alpha, rawimg_alpha, timestamp,
734 if (res != VPX_CODEC_OK) {
756 av_free(rawimg_alpha->planes[VPX_PLANE_U]);
757 av_free(rawimg_alpha->planes[VPX_PLANE_V]);
760 *got_packet = !!coded_size;
764 #define OFFSET(x) offsetof(VP8Context, x)
765 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
767 #ifndef VPX_ERROR_RESILIENT_DEFAULT
768 #define VPX_ERROR_RESILIENT_DEFAULT 1
769 #define VPX_ERROR_RESILIENT_PARTITIONS 2
772 #define COMMON_OPTIONS \
773 { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
774 { "auto-alt-ref", "Enable use of alternate reference " \
775 "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE}, \
776 { "lag-in-frames", "Number of frames to look ahead for " \
777 "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
778 { "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
779 { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
780 { "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"}, \
781 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" }, \
782 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" }, \
783 { "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" }, \
784 { "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
785 { "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"}, \
786 { "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"}, \
787 { "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"}, \
788 { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, \
789 { "max-intra-rate", "Maximum I-frame bitrate (pct) 0=unlimited", OFFSET(max_intra_rate), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
790 { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, \
791 { "partitions", "The frame partitions are independently decodable " \
792 "by the bool decoder, meaning that partitions can be decoded even " \
793 "though earlier partitions have been lost. Note that intra predicition" \
794 " is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"}, \
795 { "crf", "Select the quality for constant quality mode", offsetof(VP8Context, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE }, \
797 #define LEGACY_OPTIONS \
798 {"speed", "", offsetof(VP8Context, cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
799 {"quality", "", offsetof(VP8Context, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
800 {"vp8flags", "", offsetof(VP8Context, flags), FF_OPT_TYPE_FLAGS, {.i64 = 0}, 0, UINT_MAX, VE, "flags"}, \
801 {"error_resilient", "enable error resilience", 0, FF_OPT_TYPE_CONST, {.dbl = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"}, \
802 {"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"}, \
803 {"arnr_max_frames", "altref noise reduction max frame count", offsetof(VP8Context, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE}, \
804 {"arnr_strength", "altref noise reduction filter strength", offsetof(VP8Context, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE}, \
805 {"arnr_type", "altref noise reduction filter type", offsetof(VP8Context, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE}, \
806 {"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}, \
808 #if CONFIG_LIBVPX_VP8_ENCODER
809 static const AVOption vp8_options[] = {
816 #if CONFIG_LIBVPX_VP9_ENCODER
817 static const AVOption vp9_options[] = {
820 {
"tile-columns",
"Number of tile columns to use, log2",
OFFSET(tile_columns),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6,
VE},
821 {
"tile-rows",
"Number of tile rows to use, log2",
OFFSET(tile_rows),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2,
VE},
822 {
"frame-parallel",
"Enable frame parallel decodability features",
OFFSET(frame_parallel),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
VE},
823 {
"aq-mode",
"adaptive quantization mode",
OFFSET(aq_mode),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 3,
VE,
"aq_mode"},
825 {
"variance",
"Variance based Aq", 0,
AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0,
VE,
"aq_mode" }, \
826 {
"complexity",
"Complexity based Aq", 0,
AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0,
VE,
"aq_mode" }, \
827 {
"cyclic",
"Cyclic Refresh Aq", 0,
AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0,
VE,
"aq_mode" }, \
833 #undef COMMON_OPTIONS
834 #undef LEGACY_OPTIONS
840 {
"keyint_min",
"-1" },
844 #if CONFIG_LIBVPX_VP8_ENCODER
847 return vpx_init(avctx, &vpx_codec_vp8_cx_algo);
850 static const AVClass class_vp8 = {
853 .option = vp8_options,
857 AVCodec ff_libvpx_vp8_encoder = {
868 .priv_class = &class_vp8,
873 #if CONFIG_LIBVPX_VP9_ENCODER
876 return vpx_init(avctx, &vpx_codec_vp9_cx_algo);
879 static const AVClass class_vp9 = {
882 .option = vp9_options,
886 AVCodec ff_libvpx_vp9_encoder = {
887 .
name =
"libvpx-vp9",
897 .priv_class = &class_vp9,