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 size_t luma_size = config->source_width * config->source_height *
124 (config->encoder_bit_depth > 8 ? 2 : 1);
126 EbSvtIOFormat *in_data;
128 svt_enc->
raw_size = luma_size * 3 / 2;
136 if (!svt_enc->
in_buf->p_buffer)
151 param->source_width = avctx->
width;
152 param->source_height = avctx->
height;
155 param->encoder_bit_depth =
desc->comp[0].depth;
157 if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 1)
158 param->encoder_color_format = EB_YUV420;
159 else if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 0)
160 param->encoder_color_format = EB_YUV422;
161 else if (!
desc->log2_chroma_w && !
desc->log2_chroma_h)
162 param->encoder_color_format = EB_YUV444;
169 param->profile = avctx->
profile;
172 param->level = avctx->
level;
174 if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
178 }
else if (param->encoder_color_format == EB_YUV444 && param->profile !=
FF_PROFILE_AV1_HIGH) {
185 param->enc_mode = svt_enc->
enc_mode;
186 param->tier = svt_enc->
tier;
187 param->rate_control_mode = svt_enc->
rc_mode;
188 param->scene_change_detection = svt_enc->
scd;
189 param->qp = svt_enc->
qp;
191 param->target_bit_rate = avctx->
bit_rate;
194 param->intra_period_length = avctx->
gop_size - 1;
204 if (param->rate_control_mode) {
205 param->max_qp_allowed = avctx->
qmax;
206 param->min_qp_allowed = avctx->
qmin;
209 param->intra_refresh_type = 2;
212 param->look_ahead_distance = svt_enc->
la_depth;
221 EbBufferHeaderType *header_ptr)
223 EbSvtIOFormat *in_data = (EbSvtIOFormat *)header_ptr->p_buffer;
224 ptrdiff_t linesizes[4];
226 int bytes_shift = param->encoder_bit_depth > 8 ? 1 : 0;
229 for (
int i = 0;
i < 4;
i++)
238 for (
int i = 0;
i < 4;
i++) {
266 if (svt_ret != EB_ErrorNone) {
267 return svt_print_error(avctx, svt_ret,
"Error initializing encoder handle");
277 if (svt_ret != EB_ErrorNone) {
278 return svt_print_error(avctx, svt_ret,
"Error setting encoder parameters");
281 svt_ret = svt_av1_enc_init(svt_enc->
svt_handle);
282 if (svt_ret != EB_ErrorNone) {
287 EbBufferHeaderType *headerPtr =
NULL;
289 svt_ret = svt_av1_enc_stream_header(svt_enc->
svt_handle, &headerPtr);
290 if (svt_ret != EB_ErrorNone) {
291 return svt_print_error(avctx, svt_ret,
"Error building stream header");
298 "Cannot allocate AV1 header of size %d.\n", avctx->
extradata_size);
304 svt_ret = svt_av1_enc_stream_header_release(headerPtr);
305 if (svt_ret != EB_ErrorNone) {
320 EbBufferHeaderType *headerPtr = svt_enc->
in_buf;
324 EbBufferHeaderType headerPtrLast;
329 headerPtrLast.n_alloc_len = 0;
330 headerPtrLast.n_filled_len = 0;
331 headerPtrLast.n_tick_count = 0;
332 headerPtrLast.p_app_private =
NULL;
333 headerPtrLast.p_buffer =
NULL;
334 headerPtrLast.flags = EB_BUFFERFLAG_EOS;
336 svt_av1_enc_send_picture(svt_enc->
svt_handle, &headerPtrLast);
345 headerPtr->flags = 0;
346 headerPtr->p_app_private =
NULL;
349 svt_av1_enc_send_picture(svt_enc->
svt_handle, headerPtr);
357 const int max_frames = 8;
360 if (filled_len > svt_enc->
raw_size * max_frames) {
381 EbBufferHeaderType *headerPtr;
385 int ret = 0, pict_type;
402 if (svt_ret == EB_NoErrorEmptyQueue)
408 svt_av1_enc_release_out_buffer(&headerPtr);
414 memcpy(
pkt->
data, headerPtr->p_buffer, headerPtr->n_filled_len);
417 pkt->
size = headerPtr->n_filled_len;
418 pkt->
pts = headerPtr->pts;
419 pkt->
dts = headerPtr->dts;
421 switch (headerPtr->pic_type) {
422 case EB_AV1_KEY_PICTURE:
425 case EB_AV1_INTRA_ONLY_PICTURE:
428 case EB_AV1_INVALID_PICTURE:
436 if (headerPtr->pic_type == EB_AV1_NON_REF_PICTURE)
439 if (headerPtr->flags & EB_BUFFERFLAG_EOS)
444 svt_av1_enc_release_out_buffer(&headerPtr);
455 svt_av1_enc_deinit_handle(svt_enc->
svt_handle);
468 #define OFFSET(x) offsetof(SvtContext, x)
469 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
471 {
"hielevel",
"Hierarchical prediction levels setting",
OFFSET(hierarchical_level),
476 {
"la_depth",
"Look ahead distance [0, 120]",
OFFSET(la_depth),
479 {
"preset",
"Encoding preset [0, 8]",
482 {
"tier",
"Set operating point tier",
OFFSET(tier),
489 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
490 { .i64 = value }, 0, 0, VE, "avctx.level"
491 {
LEVEL(
"2.0", 20) },
492 {
LEVEL(
"2.1", 21) },
493 {
LEVEL(
"2.2", 22) },
494 {
LEVEL(
"2.3", 23) },
495 {
LEVEL(
"3.0", 30) },
496 {
LEVEL(
"3.1", 31) },
497 {
LEVEL(
"3.2", 32) },
498 {
LEVEL(
"3.3", 33) },
499 {
LEVEL(
"4.0", 40) },
500 {
LEVEL(
"4.1", 41) },
501 {
LEVEL(
"4.2", 42) },
502 {
LEVEL(
"4.3", 43) },
503 {
LEVEL(
"5.0", 50) },
504 {
LEVEL(
"5.1", 51) },
505 {
LEVEL(
"5.2", 52) },
506 {
LEVEL(
"5.3", 53) },
507 {
LEVEL(
"6.0", 60) },
508 {
LEVEL(
"6.1", 61) },
509 {
LEVEL(
"6.2", 62) },
510 {
LEVEL(
"6.3", 63) },
511 {
LEVEL(
"7.0", 70) },
512 {
LEVEL(
"7.1", 71) },
513 {
LEVEL(
"7.2", 72) },
514 {
LEVEL(
"7.3", 73) },
519 {
"cqp",
"Constant quantizer", 0,
AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX,
VE,
"rc" },
520 {
"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" },
521 {
"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" },
523 {
"qp",
"Quantizer to use with cqp rate control mode",
OFFSET(qp),
526 {
"sc_detection",
"Scene change detection",
OFFSET(scd),
529 {
"tile_columns",
"Log2 of number of tile columns to use",
OFFSET(tile_columns),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4,
VE},
564 .priv_class = &
class,
567 .wrapper_name =
"libsvtav1",
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define FF_PROFILE_UNKNOWN
#define FF_PROFILE_AV1_HIGH
#define FF_PROFILE_AV1_PROFESSIONAL
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
static av_cold int init(AVCodecContext *avctx)
common internal and external API header
#define AV_CEIL_RSHIFT(a, b)
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
reference-counted frame API
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
#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_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_PKT_FLAG_DISPOSABLE
Flag is used to indicate packets that contain frames that can be discarded by the decoder.
AVBufferPool * av_buffer_pool_init(buffer_size_t size, AVBufferRef *(*alloc)(buffer_size_t size))
Allocate and initialize a buffer pool.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_EOF
End of file.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
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.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_NONE
Undefined.
@ AV_PICTURE_TYPE_P
Predicted.
#define LIBAVUTIL_VERSION_INT
static const int sizes[][2]
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
static int config_enc_params(EbSvtAv1EncConfiguration *param, AVCodecContext *avctx)
static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
static int svt_print_error(void *log_ctx, EbErrorType err, const char *error_string)
static int alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)
static const AVCodecDefault eb_enc_defaults[]
static const AVOption options[]
static int svt_map_error(EbErrorType eb_err, const char **desc)
static int eb_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
static av_cold int eb_enc_init(AVCodecContext *avctx)
static const struct @85 svt_errors[]
#define LEVEL(name, value)
static int read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame, EbBufferHeaderType *header_ptr)
static av_cold int eb_enc_close(AVCodecContext *avctx)
AVCodec ff_libsvtav1_encoder
static AVBufferRef * get_output_ref(AVCodecContext *avctx, SvtContext *svt_enc, int filled_len)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_YUV420P10
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FF_AV1_PROFILE_OPTS
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
int qmin
minimum quantizer
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
int64_t bit_rate
the average bitrate
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
int qmax
maximum quantizer
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
This structure stores compressed data.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
int flags
A combination of AV_PKT_FLAG values.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
EbComponentType * svt_handle
EbBufferHeaderType * in_buf
EbSvtAv1EncConfiguration enc_params
static int ref[MAX_W *MAX_W]