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> 54 typedef struct VP8EncoderContext {
56 struct vpx_codec_ctx encoder;
57 struct vpx_image rawimg;
58 struct vpx_fixed_buf twopass_stats;
76 [VP8E_SET_ARNR_MAXFRAMES] =
"VP8E_SET_ARNR_MAXFRAMES",
77 [VP8E_SET_ARNR_STRENGTH] =
"VP8E_SET_ARNR_STRENGTH",
78 [VP8E_SET_ARNR_TYPE] =
"VP8E_SET_ARNR_TYPE",
79 [VP8E_SET_CPUUSED] =
"VP8E_SET_CPUUSED",
80 [VP8E_SET_CQ_LEVEL] =
"VP8E_SET_CQ_LEVEL",
81 [VP8E_SET_ENABLEAUTOALTREF] =
"VP8E_SET_ENABLEAUTOALTREF",
82 [VP8E_SET_NOISE_SENSITIVITY] =
"VP8E_SET_NOISE_SENSITIVITY",
83 [VP8E_SET_STATIC_THRESHOLD] =
"VP8E_SET_STATIC_THRESHOLD",
84 [VP8E_SET_TOKEN_PARTITIONS] =
"VP8E_SET_TOKEN_PARTITIONS",
90 const char *error = vpx_codec_error(&ctx->
encoder);
91 const char *detail = vpx_codec_error_detail(&ctx->
encoder);
99 const struct vpx_codec_enc_cfg *cfg)
104 av_log(avctx, level,
"vpx_codec_enc_cfg\n");
105 av_log(avctx, level,
"generic settings\n" 106 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n" 107 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
108 width,
"g_usage:", cfg->g_usage,
109 width,
"g_threads:", cfg->g_threads,
110 width,
"g_profile:", cfg->g_profile,
111 width,
"g_w:", cfg->g_w,
112 width,
"g_h:", cfg->g_h,
113 width,
"g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
114 width,
"g_error_resilient:", cfg->g_error_resilient,
115 width,
"g_pass:", cfg->g_pass,
116 width,
"g_lag_in_frames:", cfg->g_lag_in_frames);
117 av_log(avctx, level,
"rate control settings\n" 118 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n" 119 " %*s%d\n %*s%p(%zu)\n %*s%u\n",
120 width,
"rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
121 width,
"rc_resize_allowed:", cfg->rc_resize_allowed,
122 width,
"rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
123 width,
"rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
124 width,
"rc_end_usage:", cfg->rc_end_usage,
125 width,
"rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
126 width,
"rc_target_bitrate:", cfg->rc_target_bitrate);
127 av_log(avctx, level,
"quantizer settings\n" 129 width,
"rc_min_quantizer:", cfg->rc_min_quantizer,
130 width,
"rc_max_quantizer:", cfg->rc_max_quantizer);
131 av_log(avctx, level,
"bitrate tolerance\n" 133 width,
"rc_undershoot_pct:", cfg->rc_undershoot_pct,
134 width,
"rc_overshoot_pct:", cfg->rc_overshoot_pct);
135 av_log(avctx, level,
"decoder buffer model\n" 136 " %*s%u\n %*s%u\n %*s%u\n",
137 width,
"rc_buf_sz:", cfg->rc_buf_sz,
138 width,
"rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
139 width,
"rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
140 av_log(avctx, level,
"2 pass rate control settings\n" 141 " %*s%u\n %*s%u\n %*s%u\n",
142 width,
"rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
143 width,
"rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
144 width,
"rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
145 av_log(avctx, level,
"keyframing settings\n" 146 " %*s%d\n %*s%u\n %*s%u\n",
147 width,
"kf_mode:", cfg->kf_mode,
148 width,
"kf_min_dist:", cfg->kf_min_dist,
149 width,
"kf_max_dist:", cfg->kf_max_dist);
150 av_log(avctx, level,
"\n");
181 enum vp8e_enc_control_id
id,
int val)
188 snprintf(buf,
sizeof(buf),
"%s:",
ctlidstr[
id]);
191 res = vpx_codec_control(&ctx->
encoder,
id, val);
192 if (res != VPX_CODEC_OK) {
193 snprintf(buf,
sizeof(buf),
"Failed to set %s codec control",
198 return res == VPX_CODEC_OK ? 0 :
AVERROR(EINVAL);
205 vpx_codec_destroy(&ctx->
encoder);
213 const struct vpx_codec_iface *iface)
216 struct vpx_codec_enc_cfg enccfg = { 0 };
223 if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
225 vpx_codec_err_to_string(res));
230 enccfg.g_w = avctx->
width;
231 enccfg.g_h = avctx->
height;
240 enccfg.g_pass = VPX_RC_FIRST_PASS;
242 enccfg.g_pass = VPX_RC_LAST_PASS;
244 enccfg.g_pass = VPX_RC_ONE_PASS;
247 avctx->
bit_rate = enccfg.rc_target_bitrate * 1000;
253 enccfg.rc_end_usage = VPX_CQ;
256 enccfg.rc_end_usage = VPX_CBR;
259 enccfg.rc_min_quantizer = avctx->
qmin;
261 enccfg.rc_max_quantizer = avctx->
qmax;
263 #if FF_API_PRIVATE_OPT 273 enccfg.rc_2pass_vbr_minsection_pct =
276 enccfg.rc_2pass_vbr_maxsection_pct =
283 enccfg.rc_buf_initial_sz =
285 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
291 enccfg.kf_max_dist = avctx->
gop_size;
293 if (enccfg.g_pass == VPX_RC_FIRST_PASS)
294 enccfg.g_lag_in_frames = 0;
295 else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
296 int decode_size, ret;
307 "Stat buffer alloc (%zu bytes) failed\n",
313 if (decode_size < 0) {
326 enccfg.g_profile = avctx->
profile;
336 res = vpx_codec_enc_init(&ctx->
encoder, iface, &enccfg, 0);
337 if (res != VPX_CODEC_OK) {
356 #if FF_API_PRIVATE_OPT 369 "use the static-thresh private option instead.\n");
379 avctx->
width, avctx->
height, 1, (
unsigned char *)1);
385 if (enccfg.rc_end_usage == VPX_CBR ||
386 enccfg.g_pass != VPX_RC_ONE_PASS) {
397 const struct vpx_codec_cx_pkt *
src)
399 dst->
pts = src->data.frame.pts;
400 dst->
duration = src->data.frame.duration;
401 dst->
flags = src->data.frame.flags;
402 dst->
sz = src->data.frame.sz;
403 dst->
buf = src->data.frame.buf;
420 #if FF_API_CODED_FRAME 427 if (!!(cx_frame->
flags & VPX_FRAME_IS_KEY)) {
428 #if FF_API_CODED_FRAME 435 #if FF_API_CODED_FRAME 443 "Error getting output packet of size %zu.\n", cx_frame->
sz);
460 const struct vpx_codec_cx_pkt *pkt;
461 const void *iter =
NULL;
476 while ((pkt = vpx_codec_get_cx_data(&ctx->
encoder, &iter))) {
478 case VPX_CODEC_CX_FRAME_PKT:
495 "Frame queue element alloc failed\n");
501 if (!cx_frame->
buf) {
503 "Data buffer alloc (%zu bytes) failed\n",
508 memcpy(cx_frame->
buf, pkt->data.frame.buf, pkt->data.frame.sz);
512 case VPX_CODEC_STATS_PKT: {
517 pkt->data.twopass_stats.sz)) < 0) {
522 memcpy((
uint8_t*)stats->buf + stats->sz,
523 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
524 stats->sz += pkt->data.twopass_stats.sz;
527 case VPX_CODEC_PSNR_PKT:
528 case VPX_CODEC_CUSTOM_PKT:
538 const AVFrame *frame,
int *got_packet)
541 struct vpx_image *rawimg =
NULL;
542 int64_t timestamp = 0;
544 vpx_enc_frame_flags_t
flags = 0;
548 rawimg->planes[VPX_PLANE_Y] = frame->
data[0];
549 rawimg->planes[VPX_PLANE_U] = frame->
data[1];
550 rawimg->planes[VPX_PLANE_V] = frame->
data[2];
551 rawimg->stride[VPX_PLANE_Y] = frame->
linesize[0];
552 rawimg->stride[VPX_PLANE_U] = frame->
linesize[1];
553 rawimg->stride[VPX_PLANE_V] = frame->
linesize[2];
554 timestamp = frame->
pts;
555 #if VPX_IMAGE_ABI_VERSION >= 4 558 rawimg->range = VPX_CR_STUDIO_RANGE;
561 rawimg->range = VPX_CR_FULL_RANGE;
566 flags |= VPX_EFLAG_FORCE_KF;
569 res = vpx_codec_encode(&ctx->
encoder, rawimg, timestamp,
571 if (res != VPX_CODEC_OK) {
590 *got_packet = !!coded_size;
594 #define OFFSET(x) offsetof(VP8Context, x) 595 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 597 {
"cpu-used",
"Quality/Speed ratio modifier",
OFFSET(cpu_used),
AV_OPT_TYPE_INT, {.i64 = 1}, INT_MIN, INT_MAX,
VE},
598 {
"auto-alt-ref",
"Enable use of alternate reference " 600 {
"lag-in-frames",
"Number of frames to look ahead for " 601 "alternate reference frame selection",
OFFSET(lag_in_frames),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
602 {
"arnr-maxframes",
"altref noise reduction max frame count",
OFFSET(arnr_max_frames),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
603 {
"arnr-strength",
"altref noise reduction filter strength",
OFFSET(arnr_strength),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
604 {
"arnr-type",
"altref noise reduction filter type",
OFFSET(arnr_type),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE,
"arnr_type"},
608 {
"deadline",
"Time to spend encoding, in microseconds.",
OFFSET(deadline),
AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX,
VE,
"quality"},
612 {
"error-resilient",
"Error resilience configuration",
OFFSET(error_resilient),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
VE,
"er"},
613 #ifdef VPX_ERROR_RESILIENT_DEFAULT 614 {
"default",
"Improve resiliency against losses of whole frames", 0,
AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0,
VE,
"er"},
615 {
"partitions",
"The frame partitions are independently decodable " 616 "by the bool decoder, meaning that partitions can be decoded even " 617 "though earlier partitions have been lost. Note that intra predicition" 618 " is still done over the partition boundary.", 0,
AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0,
VE,
"er"},
620 {
"crf",
"Select the quality for constant quality mode", offsetof(
VP8Context, crf),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 63, VE },
621 {
"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 },
622 {
"drop-threshold",
"Frame drop threshold", offsetof(
VP8Context, drop_threshold),
AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
623 {
"noise-sensitivity",
"Noise sensitivity",
OFFSET(noise_sensitivity),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE},
631 {
"keyint_min",
"-1" },
635 #if CONFIG_LIBVPX_VP8_ENCODER 638 return vpx_init(avctx, &vpx_codec_vp8_cx_algo);
641 static const AVClass class_vp8 = {
648 AVCodec ff_libvpx_vp8_encoder = {
659 .priv_class = &class_vp8,
664 #if CONFIG_LIBVPX_VP9_ENCODER 667 return vpx_init(avctx, &vpx_codec_vp9_cx_algo);
670 static const AVClass class_vp9 = {
685 AVCodec ff_libvpx_vp9_encoder = {
686 .
name =
"libvpx-vp9",
697 #if VPX_IMAGE_ABI_VERSION >= 3 705 .priv_class = &class_vp9,
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
struct vpx_codec_ctx encoder
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define AV_LOG_WARNING
Something somehow does not look correct.
int max_bitrate
Maximum bitrate of the stream, in bits per second.
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
size_t sz
length of compressed data
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
int min_bitrate
Minimum bitrate of the stream, in bits per second.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
static void cx_pktcpy(struct FrameListData *dst, const struct vpx_codec_cx_pkt *src)
static av_cold int codecctl_int(AVCodecContext *avctx, enum vp8e_enc_control_id id, int val)
int64_t pts
time stamp to show frame (in timebase units)
static void coded_frame_add(void *list, struct FrameListData *cx_frame)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
#define AV_LOG_VERBOSE
Detailed information.
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
char * stats_out
pass1 encoding statistics output buffer
attribute_deprecated int frame_skip_threshold
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt)
Store coded frame information in format suitable for return from encode2().
#define CONFIG_LIBVPX_VP8_ENCODER
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
enum AVColorRange color_range
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int flags
AV_CODEC_FLAG_*.
Round to nearest and halfway cases away from zero.
static const AVOption options[]
int rc_max_rate
maximum bitrate
const char * name
Name of the codec implementation.
static av_always_inline av_const double round(double x)
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
int flags
A combination of AV_PKT_FLAG values.
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int rc_buffer_size
decoder bitstream buffer size
static av_cold void dump_enc_cfg(AVCodecContext *avctx, const struct vpx_codec_enc_cfg *cfg)
int bit_rate
the average bitrate
enum AVPictureType pict_type
Picture type of the frame.
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes.
vpx_img_fmt_t ff_vpx_pixfmt_to_imgfmt(enum AVPixelFormat pix)
int width
picture width / height.
#define FF_PROFILE_UNKNOWN
attribute_deprecated int noise_reduction
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
static av_cold int vp8_free(AVCodecContext *avctx)
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
attribute_deprecated int mb_threshold
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
struct FrameListData * next
the normal 2^n-1 "JPEG" YUV ranges
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
This structure describes the bitrate properties of an encoded bitstream.
static const AVCodecDefault defaults[]
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static av_cold int vp9_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
main external API structure.
static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
Queue multiple output frames from the encoder, returning the front-most.
int qmin
minimum quantizer
static const AVProfile profiles[]
Describe the class of an AVClass context structure.
uint32_t flags
flags for this frame
static av_cold void free_coded_frame(struct FrameListData *cx_frame)
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static enum AVPixelFormat pix_fmts[]
void * buf
compressed data buffer
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
the normal 219*2^(n-8) "MPEG" YUV ranges
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
static const char *const ctlidstr[]
String mappings for enum vp8e_enc_control_id.
struct vpx_fixed_buf twopass_stats
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
common internal and external API header
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
static av_cold int init(AVCodecParserContext *s)
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
static av_cold void free_frame_list(struct FrameListData *list)
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
int slices
Number of slices.
#define FF_ENABLE_DEPRECATION_WARNINGS
Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
int avg_bitrate
Average bitrate of the stream, in bits per second.
int key_frame
1 -> keyframe, 0-> not
unsigned long duration
duration to show frame (in timebase units)
int av_base64_decode(uint8_t *out, const char *in, int out_size)
Decode a base64-encoded string.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
int rc_min_rate
minimum bitrate
AVPixelFormat
Pixel format.
This structure stores compressed data.
struct FrameListData * coded_frame_list
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int keyint_min
minimum GOP size