24 #include <EbSvtAv1ErrorCodes.h> 25 #include <EbSvtAv1Enc.h> 40 typedef enum eos_status {
81 { EB_ErrorNone, 0,
"success" },
82 { EB_ErrorInsufficientResources,
AVERROR(ENOMEM),
"insufficient resources" },
83 { EB_ErrorUndefined,
AVERROR(EINVAL),
"undefined error" },
84 { EB_ErrorInvalidComponent,
AVERROR(EINVAL),
"invalid component" },
85 { EB_ErrorBadParameter,
AVERROR(EINVAL),
"bad parameter" },
86 { EB_ErrorDestroyThreadFailed,
AVERROR_EXTERNAL,
"failed to destroy thread" },
87 { EB_ErrorSemaphoreUnresponsive,
AVERROR_EXTERNAL,
"semaphore unresponsive" },
88 { EB_ErrorDestroySemaphoreFailed,
AVERROR_EXTERNAL,
"failed to destroy semaphore"},
92 { EB_NoErrorEmptyQueue,
AVERROR(EAGAIN),
"empty queue" },
106 *desc =
"unknown error";
111 const char *error_string)
123 const int pack_mode_10bit =
124 (config->encoder_bit_depth > 8) && (config->compressed_ten_bit_format == 0) ? 1 : 0;
125 const size_t luma_size_8bit =
126 config->source_width * config->source_height * (1 << pack_mode_10bit);
127 const size_t luma_size_10bit =
128 (config->encoder_bit_depth > 8 && pack_mode_10bit == 0) ? luma_size_8bit : 0;
130 EbSvtIOFormat *in_data;
132 svt_enc->
raw_size = (luma_size_8bit + luma_size_10bit) * 3 / 2;
140 if (!svt_enc->
in_buf->p_buffer)
155 param->source_width = avctx->
width;
156 param->source_height = avctx->
height;
159 param->encoder_bit_depth = desc->
comp[0].
depth;
162 param->encoder_color_format = EB_YUV420;
164 param->encoder_color_format = EB_YUV422;
166 param->encoder_color_format = EB_YUV444;
173 param->profile = avctx->
profile;
176 param->level = avctx->
level;
178 if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
182 }
else if (param->encoder_color_format == EB_YUV444 && param->profile !=
FF_PROFILE_AV1_HIGH) {
189 param->enc_mode = svt_enc->
enc_mode;
190 param->tier = svt_enc->
tier;
191 param->rate_control_mode = svt_enc->
rc_mode;
192 param->scene_change_detection = svt_enc->
scd;
193 param->qp = svt_enc->
qp;
195 param->target_bit_rate = avctx->
bit_rate;
198 param->intra_period_length = avctx->
gop_size - 1;
208 if (param->rate_control_mode) {
209 param->max_qp_allowed = avctx->
qmax;
210 param->min_qp_allowed = avctx->
qmin;
213 param->intra_refresh_type = 2;
216 param->look_ahead_distance = svt_enc->
la_depth;
225 EbBufferHeaderType *header_ptr)
227 EbSvtIOFormat *in_data = (EbSvtIOFormat *)header_ptr->p_buffer;
228 ptrdiff_t linesizes[4];
230 int bytes_shift = param->encoder_bit_depth > 8 ? 1 : 0;
233 for (
int i = 0;
i < 4;
i++)
242 for (
int i = 0;
i < 4;
i++) {
248 in_data->luma = frame->
data[0];
249 in_data->cb = frame->
data[1];
250 in_data->cr = frame->
data[2];
270 if (svt_ret != EB_ErrorNone) {
271 return svt_print_error(avctx, svt_ret,
"Error initializing encoder handle");
281 if (svt_ret != EB_ErrorNone) {
282 return svt_print_error(avctx, svt_ret,
"Error setting encoder parameters");
285 svt_ret = svt_av1_enc_init(svt_enc->
svt_handle);
286 if (svt_ret != EB_ErrorNone) {
291 EbBufferHeaderType *headerPtr =
NULL;
293 svt_ret = svt_av1_enc_stream_header(svt_enc->
svt_handle, &headerPtr);
294 if (svt_ret != EB_ErrorNone) {
295 return svt_print_error(avctx, svt_ret,
"Error building stream header");
302 "Cannot allocate AV1 header of size %d.\n", avctx->
extradata_size);
308 svt_ret = svt_av1_enc_stream_header_release(headerPtr);
309 if (svt_ret != EB_ErrorNone) {
324 EbBufferHeaderType *headerPtr = svt_enc->
in_buf;
328 EbBufferHeaderType headerPtrLast;
333 headerPtrLast.n_alloc_len = 0;
334 headerPtrLast.n_filled_len = 0;
335 headerPtrLast.n_tick_count = 0;
336 headerPtrLast.p_app_private =
NULL;
337 headerPtrLast.p_buffer =
NULL;
338 headerPtrLast.flags = EB_BUFFERFLAG_EOS;
340 svt_av1_enc_send_picture(svt_enc->
svt_handle, &headerPtrLast);
349 headerPtr->flags = 0;
350 headerPtr->p_app_private =
NULL;
351 headerPtr->pts = frame->
pts;
353 svt_av1_enc_send_picture(svt_enc->
svt_handle, headerPtr);
361 const int max_frames = 8;
364 if (filled_len > svt_enc->
raw_size * max_frames) {
385 EbBufferHeaderType *headerPtr;
389 int ret = 0, pict_type;
406 if (svt_ret == EB_NoErrorEmptyQueue)
412 svt_av1_enc_release_out_buffer(&headerPtr);
418 memcpy(pkt->
data, headerPtr->p_buffer, headerPtr->n_filled_len);
421 pkt->
size = headerPtr->n_filled_len;
422 pkt->
pts = headerPtr->pts;
423 pkt->
dts = headerPtr->dts;
425 switch (headerPtr->pic_type) {
426 case EB_AV1_KEY_PICTURE:
429 case EB_AV1_INTRA_ONLY_PICTURE:
432 case EB_AV1_INVALID_PICTURE:
440 if (headerPtr->pic_type == EB_AV1_NON_REF_PICTURE)
443 if (headerPtr->flags & EB_BUFFERFLAG_EOS)
448 svt_av1_enc_release_out_buffer(&headerPtr);
459 svt_av1_enc_deinit_handle(svt_enc->
svt_handle);
472 #define OFFSET(x) offsetof(SvtContext, x) 473 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 483 {
"preset",
"Encoding preset [0, 8]",
486 {
"tier",
"Set operating point tier",
OFFSET(
tier),
493 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ 494 { .i64 = value }, 0, 0, VE, "avctx.level" 495 {
LEVEL(
"2.0", 20) },
496 {
LEVEL(
"2.1", 21) },
497 {
LEVEL(
"2.2", 22) },
498 {
LEVEL(
"2.3", 23) },
499 {
LEVEL(
"3.0", 30) },
500 {
LEVEL(
"3.1", 31) },
501 {
LEVEL(
"3.2", 32) },
502 {
LEVEL(
"3.3", 33) },
503 {
LEVEL(
"4.0", 40) },
504 {
LEVEL(
"4.1", 41) },
505 {
LEVEL(
"4.2", 42) },
506 {
LEVEL(
"4.3", 43) },
507 {
LEVEL(
"5.0", 50) },
508 {
LEVEL(
"5.1", 51) },
509 {
LEVEL(
"5.2", 52) },
510 {
LEVEL(
"5.3", 53) },
511 {
LEVEL(
"6.0", 60) },
512 {
LEVEL(
"6.1", 61) },
513 {
LEVEL(
"6.2", 62) },
514 {
LEVEL(
"6.3", 63) },
515 {
LEVEL(
"7.0", 70) },
516 {
LEVEL(
"7.1", 71) },
517 {
LEVEL(
"7.2", 72) },
518 {
LEVEL(
"7.3", 73) },
523 {
"cqp",
"Constant quantizer", 0,
AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX,
VE,
"rc" },
524 {
"vbr",
"Variable Bit Rate, use a target bitrate for the entire stream", 0,
AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX,
VE,
"rc" },
525 {
"cvbr",
"Constrained Variable Bit Rate, use a target bitrate for each GOP", 0,
AV_OPT_TYPE_CONST,{ .i64 = 2 }, INT_MIN, INT_MAX,
VE,
"rc" },
527 {
"qp",
"Quantizer to use with cqp rate control mode",
OFFSET(
qp),
530 {
"sc_detection",
"Scene change detection",
OFFSET(
scd),
567 .priv_class = &
class,
570 .wrapper_name =
"libsvtav1",
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
const char * av_default_item_name(void *ptr)
Return the context name.
static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define FF_ARRAY_ELEMS(a)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define FF_AV1_PROFILE_OPTS
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
#define AV_PKT_FLAG_DISPOSABLE
Flag is used to indicate packets that contain frames that can be discarded by the decoder...
static int read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame, EbBufferHeaderType *header_ptr)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
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...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AVERROR_EOF
End of file.
static const struct @87 svt_errors[]
EbBufferHeaderType * in_buf
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static av_cold int eb_enc_init(AVCodecContext *avctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
static const int sizes[][2]
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
int flags
AV_CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
static int svt_map_error(EbErrorType eb_err, const char **desc)
int flags
A combination of AV_PKT_FLAG values.
reference-counted frame API
static int alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)
static const AVCodecDefault eb_enc_defaults[]
AVCodec ff_libsvtav1_encoder
int width
picture width / height.
#define FF_PROFILE_UNKNOWN
static int svt_print_error(void *log_ctx, EbErrorType err, const char *error_string)
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
static int config_enc_params(EbSvtAv1EncConfiguration *param, AVCodecContext *avctx)
static av_cold int eb_enc_close(AVCodecContext *avctx)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Libavcodec external API header.
EbComponentType * svt_handle
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
main external API structure.
uint8_t * data
The data buffer.
int qmin
minimum quantizer
#define AV_PIX_FMT_YUV420P10
Describe the class of an AVClass context structure.
#define FF_PROFILE_AV1_PROFESSIONAL
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
Fill plane sizes for an image with pixel format pix_fmt and height height.
EbSvtAv1EncConfiguration enc_params
static enum AVPixelFormat pix_fmts[]
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
A reference to a data buffer.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
common internal and external API header
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
static int ref[MAX_W *MAX_W]
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static AVBufferRef * get_output_ref(AVCodecContext *avctx, SvtContext *svt_enc, int filled_len)
#define LEVEL(name, value)
static const AVOption options[]
static int eb_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int depth
Number of bits in the component.
#define AVERROR_EXTERNAL
Generic error in an external library.
AVPixelFormat
Pixel format.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define FF_PROFILE_AV1_HIGH
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
#define AV_CEIL_RSHIFT(a, b)