22#include <va/va_enc_av1.h>
34#define AV1_MAX_QUANT 255
49 VAConfigAttribValEncAV1
attr;
94 const char *str,
const int *subscripts,
103 if (!strcmp(str,
"base_q_idx"))
105 else if (!strcmp(str,
"loop_filter_level[0]"))
107 else if (!strcmp(str,
"cdef_damping_minus_3"))
109 else if (!strcmp(str,
"cdef_uv_sec_strength[i]"))
139 if (
ctx->rc_mode->quality) {
173 ret = ff_cbs_insert_unit_content(au, -1,
177 "type = %d.\n",
type);
185 char *
data,
size_t *data_len,
191 ret = ff_cbs_write_fragment_data(priv->
cbc, bs);
212 for (k = 0; (blkSize << k) < target; k++);
219 int mi_cols, mi_rows, sb_shift, sb_size;
220 int max_tile_area_sb, max_tile_area_sb_varied;
221 int tile_width_sb, tile_height_sb, widest_tile_sb;
233 mi_cols = 2 * ((avctx->
width + 7) >> 3);
234 mi_rows = 2 * ((avctx->
height + 7) >> 3);
236 ((mi_cols + 31) >> 5) : ((mi_cols + 15) >> 4);
238 ((mi_rows + 31) >> 5) : ((mi_rows + 15) >> 4);
240 sb_size = sb_shift + 2;
277 if ((priv->
sb_cols + tile_width_sb - 1) / tile_width_sb == priv->
tile_cols) {
286 tile_height_sb <= max_tile_area_sb / tile_width_sb) {
306 max_tile_area_sb_varied = (priv->
sb_rows * priv->
sb_cols) >> (min_log2_tiles + 1);
330 if (priv->
attr_ext2.bits.max_tile_num_minus1) {
335 priv->
attr_ext2.bits.max_tile_num_minus1 + 1);
351 char *
data,
size_t *data_len)
368 VAEncSequenceParameterBufferAV1 *vseq =
ctx->codec_sequence_params;
373 memset(sh_obu, 0,
sizeof(*sh_obu));
392 .high_bitdepth =
desc->comp[0].depth == 8 ? 0 : 1,
394 .transfer_characteristics = avctx->
color_trc,
400 .subsampling_x =
desc->log2_chroma_w,
401 .subsampling_y =
desc->log2_chroma_h,
436 "any normal level, using maximum parameters level by default.\n");
445 vseq->intra_period = base_ctx->
gop_size;
446 vseq->ip_period = base_ctx->
b_per_p + 1;
450 if (!(
ctx->va_rc_mode & VA_RC_CQP)) {
451 vseq->bits_per_second =
ctx->va_bit_rate;
452 vseq->seq_fields.bits.enable_cdef = sh->
enable_cdef = 1;
464 ff_cbs_fragment_reset(obu);
485 { 1, 0, 0, 0, -1, 0, -1, -1 };
487 memset(fh_obu, 0,
sizeof(*fh_obu));
507 href =
ref->codec_priv;
523 href =
ref->codec_priv;
539 vpic->picture_flags.bits.disable_frame_recon = 1;
546 href =
ref->codec_priv;
555 href =
ref->codec_priv;
580 if (priv->
attr_ext2.bits.tx_mode_support & 0x04)
582 else if (priv->
attr_ext2.bits.tx_mode_support & 0x02)
614#if VA_CHECK_VERSION(1, 15, 0)
618 vpic->picture_flags.bits.enable_frame_obu = 0;
619 vpic->picture_flags.bits.frame_type = fh->
frame_type;
624 vpic->mode_control_flags.bits.reference_mode = fh->
reference_select ? 2 : 0;
625 vpic->mode_control_flags.bits.tx_mode = fh->
tx_mode;
627 vpic->tile_group_obu_hdr_info.bits.obu_has_size_field = 1;
634 vpic->reference_frames[
i] = VA_INVALID_SURFACE;
637 for (
int j = 0; j < pic->
nb_refs[
i]; j++) {
641 av_assert0(vpic->reference_frames[slot] == VA_INVALID_SURFACE);
655 if (!(
ctx->va_rc_mode & VA_RC_CQP)) {
663 vpic->size_in_bits_frame_hdr_obu = priv->
fh_data_len;
683 const int chroma_den = 1 << 16;
684 const int max_luma_den = 1 << 8;
685 const int min_luma_den = 1 << 14;
687 memset(obu, 0,
sizeof(*obu));
691 for (
i = 0;
i < 3;
i++) {
724 memset(obu, 0,
sizeof(*obu));
733 ff_cbs_fragment_reset(obu);
748 vslice->tg_start = slice->
index * div;
753 vslice->tg_end = (slice->
index + 1) * div - 1;
761 char *
data,
size_t *data_len)
775 memset(fh_obu, 0,
sizeof(*fh_obu));
805 ff_cbs_fragment_reset(obu);
812 char *
data,
size_t *data_len)
834 *data_len = mh_data_len;
835 *
type = VAEncPackedHeaderRawData;
838 ff_cbs_fragment_reset(obu);
851 .default_quality = 25,
856 .sequence_header_type = VAEncPackedHeaderSequence,
857 .sequence_params_size =
sizeof(VAEncSequenceParameterBufferAV1),
862 .picture_header_type = VAEncPackedHeaderPicture,
863 .picture_params_size =
sizeof(VAEncPictureParameterBufferAV1),
867 .slice_params_size =
sizeof(VAEncTileGroupBufferAV1),
883 ctx->desired_packed_headers =
884 VA_ENC_PACKED_HEADER_SEQUENCE |
885 VA_ENC_PACKED_HEADER_PICTURE |
886 VA_ENC_PACKED_HEADER_MISC;
902 attr.type = VAConfigAttribEncAV1;
903 vas = vaGetConfigAttributes(
ctx->hwctx->display,
907 if (vas != VA_STATUS_SUCCESS) {
909 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
911 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
912 priv->
attr.value = 0;
914 "supported.\n", attr.type);
916 priv->
attr.value = attr.value;
919 attr.type = VAConfigAttribEncAV1Ext1;
920 vas = vaGetConfigAttributes(
ctx->hwctx->display,
924 if (vas != VA_STATUS_SUCCESS) {
926 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
928 }
else if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
931 "supported.\n", attr.type);
937 attr.type = VAConfigAttribEncAV1Ext2;
938 vas = vaGetConfigAttributes(
ctx->hwctx->display,
942 if (vas != VA_STATUS_SUCCESS || attr.value == VA_ATTRIB_NOT_SUPPORTED) {
944 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
951 priv->
attr_ext2.bits.obu_size_bytes_minus1 + 1, 0);
965 ff_cbs_close(&priv->
cbc);
970#define OFFSET(x) offsetof(VAAPIEncodeAV1Context, x)
971#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
977 {
"profile",
"Set profile (seq_profile)",
981#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
982 { .i64 = value }, 0, 0, FLAGS, .unit = "profile"
988 {
"tier",
"Set tier (seq_tier)",
991 { .i64 = 0 }, 0, 0,
FLAGS, .unit =
"tier" },
993 { .i64 = 1 }, 0, 0,
FLAGS, .unit =
"tier" },
994 {
"level",
"Set level (seq_level_idx)",
998#define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
999 { .i64 = value }, 0, 0, FLAGS, .unit = "level"
1000 {
LEVEL(
"2.0", 0) },
1001 {
LEVEL(
"2.1", 1) },
1002 {
LEVEL(
"3.0", 4) },
1003 {
LEVEL(
"3.1", 5) },
1004 {
LEVEL(
"4.0", 8) },
1005 {
LEVEL(
"4.1", 9) },
1006 {
LEVEL(
"5.0", 12) },
1007 {
LEVEL(
"5.1", 13) },
1008 {
LEVEL(
"5.2", 14) },
1009 {
LEVEL(
"5.3", 15) },
1010 {
LEVEL(
"6.0", 16) },
1011 {
LEVEL(
"6.1", 17) },
1012 {
LEVEL(
"6.2", 18) },
1013 {
LEVEL(
"6.3", 19) },
1016 {
"tiles",
"Tile columns x rows (Use minimal tile column/row number automatically by default)",
1018 {
"tile_groups",
"Number of tile groups for encoding",
1035 .class_name =
"av1_vaapi",
1042 .p.name =
"av1_vaapi",
1059 .p.wrapper_name =
"vaapi",
const FFCodec ff_av1_vaapi_encoder
static AVFormatContext * ctx
const AV1LevelDescriptor * ff_av1_guess_level(int64_t bitrate, int tier, int width, int height, int tiles, int tile_cols, float fps)
Guess the level of a stream from some parameters.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define i(width, name, range_min, range_max)
#define CODEC_PIXFMTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_RECEIVE_PACKET_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static __device__ float fabs(float a)
#define MAX_PARAM_BUFFER_SIZE
#define AV_PROFILE_AV1_HIGH
#define AV_PROFILE_UNKNOWN
#define AV_PROFILE_AV1_MAIN
#define AV_PROFILE_AV1_PROFESSIONAL
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#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_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_EOF
End of file.
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
@ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
@ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata associated with a video frame.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
#define LIBAVUTIL_VERSION_INT
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
#define HW_BASE_ENCODE_COMMON_OPTIONS
@ FF_HW_FLAG_TIMESTAMP_NO_DELAY
#define MAX_REFERENCE_LIST_NUM
@ AV1_METADATA_TYPE_HDR_MDCV
@ AV1_METADATA_TYPE_HDR_CLL
@ AV1_OBU_SEQUENCE_HEADER
@ AV1_TOTAL_REFS_PER_FRAME
const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[]
int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_JPEG
Full range content.
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
static int put_bits_count(PutBitContext *s)
#define FF_ARRAY_ELEMS(a)
uint8_t chroma_sample_position
AV1RawFrameHeader frame_header
AV1RawSequenceHeader sequence_header
union AV1RawOBU::@016362317061177152367125047142162076164261250366 obu
Describe the class of an AVClass context structure.
main external API structure.
int width
picture width / height.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
float b_quant_offset
qscale offset between IP and B-frames
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
int qmin
minimum quantizer
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
int level
Encoding level descriptor.
int64_t bit_rate
the average bitrate
enum AVColorSpace colorspace
YUV colorspace type.
int qmax
maximum quantizer
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
float i_quant_offset
qscale offset between P and I-frames
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
unsigned MaxFALL
Max average light level per frame (cd/m^2).
unsigned MaxCLL
Max content light level (cd/m^2).
Structure to hold side data for an AVFrame.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES]
Context structure for coded bitstream operations.
int trace_enable
Enable trace output during read/write operations.
void * trace_context
User context pointer to pass to trace callbacks.
int trace_level
Log level to use for default trace output.
void * priv_data
Internal codec-specific data.
CBSTraceWriteCallback trace_write_callback
Callback for write tracing.
Coded bitstream fragment structure, combining one or more units.
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
size_t data_size
The number of bytes in the bitstream.
uint8_t * data
Pointer to the bitstream form of this fragment.
AVHWFramesContext * input_frames
int nb_refs[MAX_REFERENCE_LIST_NUM]
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
size_t fh_data_len
bit length of fh_data.
CodedBitstreamFragment current_obu
VAConfigAttribValEncAV1Ext1 attr_ext1
char fh_data[MAX_PARAM_BUFFER_SIZE]
coded frame header data.
VAConfigAttribValEncAV1 attr
uint8_t use_128x128_superblock
int qindex_offset
bit positions in current frame header
size_t sh_data_len
bit length of sh_data.
VAAPIEncodeContext common
char sh_data[MAX_PARAM_BUFFER_SIZE]
coded sequence header data.
CodedBitstreamContext * cbc
uint8_t height_in_sbs_minus_1[AV1_MAX_TILE_ROWS]
AV1RawOBU fh
frame header.
uint8_t width_in_sbs_minus_1[AV1_MAX_TILE_COLS]
AV1RawOBU sh
sequence header.
VAConfigAttribValEncAV1Ext2 attr_ext2
AV1RawOBU mh[4]
metadata header.
void * codec_picture_params
int non_independent_frame
indicate if current frame is an independent frame that the coded data can be pushed to downstream dir...
size_t tail_size
Byte length of tail_data.
char tail_data[MAX_PARAM_BUFFER_SIZE]
Tail data of current pic, used only for repeat header of AV1.
VASurfaceID recon_surface
void * codec_slice_params
static int ref[MAX_W *MAX_W]
#define VAAPI_ENCODE_COMMON_OPTIONS
#define VAAPI_ENCODE_RC_OPTIONS
static int vaapi_encode_av1_init_picture_params(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
static av_cold int vaapi_encode_av1_close(AVCodecContext *avctx)
static int vaapi_encode_av1_write_extra_header(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, int index, int *type, char *data, size_t *data_len)
static av_cold int vaapi_encode_av1_init(AVCodecContext *avctx)
static const FFCodecDefault vaapi_encode_av1_defaults[]
static const VAAPIEncodeProfile vaapi_encode_av1_profiles[]
static int vaapi_encode_av1_add_obu(AVCodecContext *avctx, CodedBitstreamFragment *au, uint8_t type, void *obu_unit)
static const VAAPIEncodeType vaapi_encode_type_av1
static void vaapi_encode_av1_trace_write_log(void *ctx, PutBitContext *pbc, int length, const char *str, const int *subscripts, int64_t value)
static int tile_log2(int blkSize, int target)
static int vaapi_encode_av1_init_sequence_params(AVCodecContext *avctx)
static const AVClass vaapi_encode_av1_class
#define LEVEL(name, value)
static const AVOption vaapi_encode_av1_options[]
static int vaapi_encode_av1_write_picture_header(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, char *data, size_t *data_len)
static int vaapi_encode_av1_init_slice_params(AVCodecContext *avctx, FFHWBaseEncodePicture *base, VAAPIEncodeSlice *slice)
#define PROFILE(name, value)
static int vaapi_encode_av1_write_obu(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *bs)
static av_cold int vaapi_encode_av1_configure(AVCodecContext *avctx)
static int vaapi_encode_av1_set_tile(AVCodecContext *avctx)
static int vaapi_encode_av1_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
static av_cold int vaapi_encode_av1_get_encoder_caps(AVCodecContext *avctx)
static int write_sequence_header(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, uint8_t *data, size_t *data_len)