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;
73 [VP8E_SET_ARNR_MAXFRAMES] =
"VP8E_SET_ARNR_MAXFRAMES",
74 [VP8E_SET_ARNR_STRENGTH] =
"VP8E_SET_ARNR_STRENGTH",
75 [VP8E_SET_ARNR_TYPE] =
"VP8E_SET_ARNR_TYPE",
76 [VP8E_SET_CPUUSED] =
"VP8E_SET_CPUUSED",
77 [VP8E_SET_CQ_LEVEL] =
"VP8E_SET_CQ_LEVEL",
78 [VP8E_SET_ENABLEAUTOALTREF] =
"VP8E_SET_ENABLEAUTOALTREF",
79 [VP8E_SET_NOISE_SENSITIVITY] =
"VP8E_SET_NOISE_SENSITIVITY",
80 [VP8E_SET_STATIC_THRESHOLD] =
"VP8E_SET_STATIC_THRESHOLD",
81 [VP8E_SET_TOKEN_PARTITIONS] =
"VP8E_SET_TOKEN_PARTITIONS",
87 const char *error = vpx_codec_error(&ctx->
encoder);
88 const char *detail = vpx_codec_error_detail(&ctx->
encoder);
96 const struct vpx_codec_enc_cfg *cfg)
101 av_log(avctx, level,
"vpx_codec_enc_cfg\n");
102 av_log(avctx, level,
"generic settings\n" 103 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n" 104 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
105 width,
"g_usage:", cfg->g_usage,
106 width,
"g_threads:", cfg->g_threads,
107 width,
"g_profile:", cfg->g_profile,
108 width,
"g_w:", cfg->g_w,
109 width,
"g_h:", cfg->g_h,
110 width,
"g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
111 width,
"g_error_resilient:", cfg->g_error_resilient,
112 width,
"g_pass:", cfg->g_pass,
113 width,
"g_lag_in_frames:", cfg->g_lag_in_frames);
114 av_log(avctx, level,
"rate control settings\n" 115 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n" 116 " %*s%d\n %*s%p(%zu)\n %*s%u\n",
117 width,
"rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
118 width,
"rc_resize_allowed:", cfg->rc_resize_allowed,
119 width,
"rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
120 width,
"rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
121 width,
"rc_end_usage:", cfg->rc_end_usage,
122 width,
"rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
123 width,
"rc_target_bitrate:", cfg->rc_target_bitrate);
124 av_log(avctx, level,
"quantizer settings\n" 126 width,
"rc_min_quantizer:", cfg->rc_min_quantizer,
127 width,
"rc_max_quantizer:", cfg->rc_max_quantizer);
128 av_log(avctx, level,
"bitrate tolerance\n" 130 width,
"rc_undershoot_pct:", cfg->rc_undershoot_pct,
131 width,
"rc_overshoot_pct:", cfg->rc_overshoot_pct);
132 av_log(avctx, level,
"decoder buffer model\n" 133 " %*s%u\n %*s%u\n %*s%u\n",
134 width,
"rc_buf_sz:", cfg->rc_buf_sz,
135 width,
"rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
136 width,
"rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
137 av_log(avctx, level,
"2 pass rate control settings\n" 138 " %*s%u\n %*s%u\n %*s%u\n",
139 width,
"rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
140 width,
"rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
141 width,
"rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
142 av_log(avctx, level,
"keyframing settings\n" 143 " %*s%d\n %*s%u\n %*s%u\n",
144 width,
"kf_mode:", cfg->kf_mode,
145 width,
"kf_min_dist:", cfg->kf_min_dist,
146 width,
"kf_max_dist:", cfg->kf_max_dist);
147 av_log(avctx, level,
"\n");
178 enum vp8e_enc_control_id
id,
int val)
185 snprintf(buf,
sizeof(buf),
"%s:",
ctlidstr[
id]);
188 res = vpx_codec_control(&ctx->
encoder,
id, val);
189 if (res != VPX_CODEC_OK) {
190 snprintf(buf,
sizeof(buf),
"Failed to set %s codec control",
195 return res == VPX_CODEC_OK ? 0 :
AVERROR(EINVAL);
202 vpx_codec_destroy(&ctx->
encoder);
211 const struct vpx_codec_iface *iface)
214 struct vpx_codec_enc_cfg enccfg;
220 if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
222 vpx_codec_err_to_string(res));
227 enccfg.g_w = avctx->
width;
228 enccfg.g_h = avctx->
height;
237 enccfg.g_pass = VPX_RC_FIRST_PASS;
239 enccfg.g_pass = VPX_RC_LAST_PASS;
241 enccfg.g_pass = VPX_RC_ONE_PASS;
244 avctx->
bit_rate = enccfg.rc_target_bitrate * 1000;
250 enccfg.rc_end_usage = VPX_CQ;
253 enccfg.rc_end_usage = VPX_CBR;
256 enccfg.rc_min_quantizer = avctx->
qmin;
258 enccfg.rc_max_quantizer = avctx->
qmax;
263 enccfg.rc_2pass_vbr_minsection_pct =
266 enccfg.rc_2pass_vbr_maxsection_pct =
273 enccfg.rc_buf_initial_sz =
275 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
281 enccfg.kf_max_dist = avctx->
gop_size;
283 if (enccfg.g_pass == VPX_RC_FIRST_PASS)
284 enccfg.g_lag_in_frames = 0;
285 else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
286 int decode_size, ret;
297 "Stat buffer alloc (%zu bytes) failed\n",
303 if (decode_size < 0) {
316 enccfg.g_profile = avctx->
profile;
322 res = vpx_codec_enc_init(&ctx->
encoder, iface, &enccfg, 0);
323 if (res != VPX_CODEC_OK) {
359 const struct vpx_codec_cx_pkt *src)
361 dst->
pts = src->data.frame.pts;
362 dst->
duration = src->data.frame.duration;
363 dst->
flags = src->data.frame.flags;
364 dst->
sz = src->data.frame.sz;
365 dst->
buf = src->data.frame.buf;
382 coded_frame->
pts = cx_frame->
pts;
392 "Error getting output packet of size %zu.\n", cx_frame->
sz);
410 const struct vpx_codec_cx_pkt *pkt;
411 const void *iter =
NULL;
417 size =
storeframe(avctx, cx_frame, pkt_out, coded_frame);
426 while ((pkt = vpx_codec_get_cx_data(&ctx->
encoder, &iter))) {
428 case VPX_CODEC_CX_FRAME_PKT:
436 size =
storeframe(avctx, &cx_frame, pkt_out, coded_frame);
445 "Frame queue element alloc failed\n");
451 if (!cx_frame->
buf) {
453 "Data buffer alloc (%zu bytes) failed\n",
457 memcpy(cx_frame->
buf, pkt->data.frame.buf, pkt->data.frame.sz);
461 case VPX_CODEC_STATS_PKT: {
466 pkt->data.twopass_stats.sz)) < 0) {
471 memcpy((
uint8_t*)stats->buf + stats->sz,
472 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
473 stats->sz += pkt->data.twopass_stats.sz;
476 case VPX_CODEC_PSNR_PKT:
477 case VPX_CODEC_CUSTOM_PKT:
487 const AVFrame *frame,
int *got_packet)
490 struct vpx_image *rawimg =
NULL;
491 int64_t timestamp = 0;
493 vpx_enc_frame_flags_t
flags = 0;
497 rawimg->planes[VPX_PLANE_Y] = frame->
data[0];
498 rawimg->planes[VPX_PLANE_U] = frame->
data[1];
499 rawimg->planes[VPX_PLANE_V] = frame->
data[2];
500 rawimg->stride[VPX_PLANE_Y] = frame->
linesize[0];
501 rawimg->stride[VPX_PLANE_U] = frame->
linesize[1];
502 rawimg->stride[VPX_PLANE_V] = frame->
linesize[2];
503 timestamp = frame->
pts;
505 flags |= VPX_EFLAG_FORCE_KF;
508 res = vpx_codec_encode(&ctx->
encoder, rawimg, timestamp,
510 if (res != VPX_CODEC_OK) {
529 *got_packet = !!coded_size;
533 #define OFFSET(x) offsetof(VP8Context, x) 534 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 536 {
"cpu-used",
"Quality/Speed ratio modifier",
OFFSET(cpu_used),
AV_OPT_TYPE_INT, {.i64 = 1}, INT_MIN, INT_MAX,
VE},
537 {
"auto-alt-ref",
"Enable use of alternate reference " 539 {
"lag-in-frames",
"Number of frames to look ahead for " 540 "alternate reference frame selection",
OFFSET(lag_in_frames),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
541 {
"arnr-maxframes",
"altref noise reduction max frame count",
OFFSET(arnr_max_frames),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
542 {
"arnr-strength",
"altref noise reduction filter strength",
OFFSET(arnr_strength),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
543 {
"arnr-type",
"altref noise reduction filter type",
OFFSET(arnr_type),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE,
"arnr_type"},
547 {
"deadline",
"Time to spend encoding, in microseconds.",
OFFSET(deadline),
AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX,
VE,
"quality"},
551 {
"error-resilient",
"Error resilience configuration",
OFFSET(error_resilient),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
VE,
"er"},
552 #ifdef VPX_ERROR_RESILIENT_DEFAULT 553 {
"default",
"Improve resiliency against losses of whole frames", 0,
AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0,
VE,
"er"},
554 {
"partitions",
"The frame partitions are independently decodable " 555 "by the bool decoder, meaning that partitions can be decoded even " 556 "though earlier partitions have been lost. Note that intra predicition" 557 " is still done over the partition boundary.", 0,
AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0,
VE,
"er"},
559 {
"crf",
"Select the quality for constant quality mode", offsetof(
VP8Context, crf),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 63, VE },
567 {
"keyint_min",
"-1" },
571 #if CONFIG_LIBVPX_VP8_ENCODER 574 return vpx_init(avctx, &vpx_codec_vp8_cx_algo);
577 static const AVClass class_vp8 = {
584 AVCodec ff_libvpx_vp8_encoder = {
595 .priv_class = &class_vp8,
600 #if CONFIG_LIBVPX_VP9_ENCODER 606 return vpx_init(avctx, &vpx_codec_vp9_cx_algo);
609 static const AVClass class_vp9 = {
616 AVCodec ff_libvpx_vp9_encoder = {
617 .
name =
"libvpx-vp9",
627 .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
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
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.
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
AVFrame * coded_frame
the picture in the bitstream
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
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...
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...
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)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
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.
int mb_threshold
Macroblock threshold below which the user specified macroblock types will be used.
#define AV_LOG_VERBOSE
Detailed information.
char * stats_out
pass1 encoding statistics output buffer
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int ff_vp9_check_experimental(AVCodecContext *avctx)
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
int frame_skip_threshold
frame skip threshold
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Round to nearest and halfway cases away from zero.
static const AVOption options[]
int rc_max_rate
maximum bitrate
void av_log(void *avcl, int level, const char *fmt,...)
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.
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.
int width
picture width / height.
#define FF_PROFILE_UNKNOWN
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
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)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
struct FrameListData * next
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
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.
main external API structure.
static void close(AVCodecParserContext *s)
int qmin
minimum quantizer
static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt, AVFrame *coded_frame)
Store coded frame information in format suitable for return from encode2().
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)
void * buf
compressed data buffer
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int noise_reduction
noise reduction strength
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
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
common internal and external API header
static av_cold int init(AVCodecParserContext *s)
static av_cold void free_frame_list(struct FrameListData *list)
int slices
Number of slices.
Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
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...
static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out, AVFrame *coded_frame)
Queue multiple output frames from the encoder, returning the front-most.
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