Go to the documentation of this file.
32 const char *
name, uint32_t *write_to,
33 uint32_t range_min, uint32_t range_max)
35 uint32_t zeroes, bits_value,
value;
43 "%s: bitstream ended.\n",
name);
62 "%s uvlc code: considered invalid due to conflicting "
63 "standard and reference decoder behaviour.\n",
name);
68 "%s: bitstream ended.\n",
name);
73 value = bits_value + (UINT32_C(1) << zeroes) - 1;
78 if (value < range_min || value > range_max) {
80 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
93 uint32_t range_min, uint32_t range_max)
100 if (value < range_min || value > range_max) {
102 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
108 v =
value - (1
U << zeroes) + 1;
125 const char *
name, uint64_t *write_to)
134 for (
i = 0;
i < 8;
i++) {
137 "%s: bitstream ended.\n",
name);
141 value |= (uint64_t)(
byte & 0x7f) << (
i * 7);
146 if (
value > UINT32_MAX)
158 const char *
name, uint64_t
value,
int fixed_length)
168 if (fixed_length <
len) {
170 "fixed length size field (%d > %d).\n",
177 for (
i = 0;
i <
len;
i++) {
181 byte =
value >> (7 *
i) & 0x7f;
196 uint32_t n,
const char *
name,
197 const int *subscripts, uint32_t *write_to)
199 uint32_t m, v, extra_bit,
value;
211 "%s: bitstream ended.\n",
name);
224 value = (v << 1) - m + extra_bit;
236 uint32_t n,
const char *
name,
237 const int *subscripts, uint32_t
value)
239 uint32_t
w, m, v, extra_bit;
245 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
260 v = m + ((
value - m) >> 1);
261 extra_bit = (
value - m) & 1;
274 uint32_t range_min, uint32_t range_max,
275 const char *
name, uint32_t *write_to)
281 av_assert0(range_min <= range_max && range_max - range_min < 32);
286 "%s: bitstream ended.\n",
name);
304 uint32_t range_min, uint32_t range_max,
311 av_assert0(range_min <= range_max && range_max - range_min < 32);
312 if (value < range_min || value > range_max) {
314 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
319 if (
value == range_max)
320 len = range_max - range_min;
337 uint32_t range_max,
const char *
name,
338 const int *subscripts, uint32_t *write_to)
340 uint32_t
value, max_len,
len, range_offset, range_bits;
346 max_len =
av_log2(range_max - 1) - 3;
349 "subexp_more_bits", &
len);
354 range_bits = 2 +
len;
355 range_offset = 1 << range_bits;
363 "subexp_bits", &
value);
373 value += range_offset;
384 uint32_t range_max,
const char *
name,
385 const int *subscripts, uint32_t
value)
388 uint32_t max_len,
len, range_offset, range_bits;
392 if (
value > range_max) {
394 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
400 max_len =
av_log2(range_max - 1) - 3;
408 len = range_bits - 2;
415 range_offset = 1 << range_bits;
419 "subexp_more_bits",
len);
426 value - range_offset);
432 "subexp_final_bits",
NULL,
433 value - range_offset);
448 for (k = 0; (blksize << k) < target; k++);
453 unsigned int a,
unsigned int b)
455 unsigned int diff, m;
476 #define HEADER(name) do { \
477 CBS_FUNC(trace_header)(ctx, name); \
480 #define CHECK(call) do { \
486 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
487 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
488 #define FUNC(name) FUNC_AV1(READWRITE, name)
490 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
493 #define fc(width, name, range_min, range_max) \
494 xf(width, name, current->name, range_min, range_max, 0, )
495 #define flag(name) fb(1, name)
496 #define su(width, name) \
497 xsu(width, name, current->name, 0, )
499 #define fbs(width, name, subs, ...) \
500 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
501 #define fcs(width, name, range_min, range_max, subs, ...) \
502 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
503 #define flags(name, subs, ...) \
504 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
505 #define sus(width, name, subs, ...) \
506 xsu(width, name, current->name, subs, __VA_ARGS__)
508 #define fixed(width, name, value) do { \
509 av_unused uint32_t fixed_value = value; \
510 xf(width, name, fixed_value, value, value, 0, ); \
515 #define READWRITE read
516 #define RWContext GetBitContext
518 #define fb(width, name) do { \
520 CHECK(CBS_FUNC(read_simple_unsigned)(ctx, rw, width, \
522 current->name = value; \
525 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
527 CHECK(CBS_FUNC(read_unsigned)(ctx, rw, width, #name, \
528 SUBSCRIPTS(subs, __VA_ARGS__), \
529 &value, range_min, range_max)); \
533 #define xsu(width, name, var, subs, ...) do { \
535 CHECK(CBS_FUNC(read_signed)(ctx, rw, width, #name, \
536 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
537 MIN_INT_BITS(width), \
538 MAX_INT_BITS(width))); \
542 #define uvlc(name, range_min, range_max) do { \
544 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
545 &value, range_min, range_max)); \
546 current->name = value; \
549 #define ns(max_value, name, subs, ...) do { \
551 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
552 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
553 current->name = value; \
556 #define increment(name, min, max) do { \
558 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
559 current->name = value; \
562 #define subexp(name, max, subs, ...) do { \
564 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
565 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
566 current->name = value; \
569 #define delta_q(name) do { \
570 uint8_t delta_coded; \
572 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
574 xsu(1 + 6, name.delta_q, delta_q, 0, ); \
577 current->name = delta_q; \
580 #define leb128(name) do { \
582 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
583 current->name = value; \
586 #define infer(name, value) do { \
587 current->name = value; \
590 #define byte_alignment(rw) (get_bits_count(rw) % 8)
607 #undef byte_alignment
613 #define READWRITE write
614 #define RWContext PutBitContext
616 #define fb(width, name) do { \
617 CHECK(CBS_FUNC(write_simple_unsigned)(ctx, rw, width, #name, \
621 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
622 CHECK(CBS_FUNC(write_unsigned)(ctx, rw, width, #name, \
623 SUBSCRIPTS(subs, __VA_ARGS__), \
624 var, range_min, range_max)); \
627 #define xsu(width, name, var, subs, ...) do { \
628 CHECK(CBS_FUNC(write_signed)(ctx, rw, width, #name, \
629 SUBSCRIPTS(subs, __VA_ARGS__), var, \
630 MIN_INT_BITS(width), \
631 MAX_INT_BITS(width))); \
634 #define uvlc(name, range_min, range_max) do { \
635 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
636 range_min, range_max)); \
639 #define ns(max_value, name, subs, ...) do { \
640 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
641 SUBSCRIPTS(subs, __VA_ARGS__), \
645 #define increment(name, min, max) do { \
646 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
650 #define subexp(name, max, subs, ...) do { \
651 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
652 SUBSCRIPTS(subs, __VA_ARGS__), \
656 #define delta_q(name) do { \
657 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
659 xsu(1 + 6, name.delta_q, current->name, 0, ); \
662 #define leb128(name) do { \
663 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name, 0)); \
666 #define infer(name, value) do { \
667 if (current->name != (value)) { \
668 av_log(ctx->log_ctx, AV_LOG_ERROR, \
669 "%s does not match inferred value: " \
670 "%"PRId64", but should be %"PRId64".\n", \
671 #name, (int64_t)current->name, (int64_t)(value)); \
672 return AVERROR_INVALIDDATA; \
676 #define byte_alignment(rw) (put_bits_count(rw) % 8)
693 #undef byte_alignment
708 trace =
ctx->trace_enable;
709 ctx->trace_enable = 0;
714 if (INT_MAX / 8 <
size) {
724 int config_record_version =
data[0] & 0x7f;
726 if (config_record_version != 1) {
728 "Unknown version %d of AV1CodecConfigurationRecord "
730 config_record_version);
738 "Undersized AV1CodecConfigurationRecord v%d found!\n",
739 config_record_version);
780 obu_length =
pos / 8 + obu_size;
782 if (
size < obu_length) {
784 "%"PRIu64
", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
802 ctx->trace_enable = trace;
814 uint8_t **
data,
size_t *data_size)
821 "any data in tile group (%d bits read).\n",
pos);
845 int err, start_pos, end_pos;
856 err = cbs_av1_read_obu_header(
ctx, &gbc, &obu->
header);
882 int in_temporal_layer =
884 int in_spatial_layer =
886 if (!in_temporal_layer || !in_spatial_layer) {
895 err = cbs_av1_read_sequence_header_obu(
ctx, &gbc,
905 "Must not be higher than %u.\n",
918 err = cbs_av1_read_temporal_delimiter_obu(
ctx, &gbc);
926 err = cbs_av1_read_frame_header_obu(
ctx, &gbc,
936 err = cbs_av1_read_frame_obu(
ctx, &gbc, &obu->
obu.
frame,
952 err = cbs_av1_read_tile_group_obu(
ctx, &gbc, tile_group);
964 #if CBS_AV1_OBU_TILE_LIST
967 err = cbs_av1_read_tile_list_obu(
ctx, &gbc,
981 #if CBS_AV1_OBU_METADATA
990 #if CBS_AV1_OBU_PADDING
993 err = cbs_av1_read_padding_obu(
ctx, &gbc, &obu->
obu.
padding);
1010 int nb_bits = obu->
obu_size * 8 + start_pos - end_pos;
1015 err = cbs_av1_read_trailing_bits(
ctx, &gbc, nb_bits);
1036 int err, start_pos, end_pos, data_pos;
1057 err = cbs_av1_write_obu_header(
ctx, pbc, &obu->
header);
1079 err = cbs_av1_write_sequence_header_obu(
ctx, pbc,
1097 err = cbs_av1_write_temporal_delimiter_obu(
ctx, pbc);
1105 err = cbs_av1_write_frame_header_obu(
ctx, pbc,
1123 err = cbs_av1_write_tile_group_obu(
ctx, pbc, tile_group);
1130 #if CBS_AV1_OBU_TILE_LIST
1141 #if CBS_AV1_OBU_METADATA
1150 #if CBS_AV1_OBU_PADDING
1153 err = cbs_av1_write_padding_obu(
ctx, pbc, &obu->
obu.
padding);
1165 header_size = (end_pos - start_pos + 7) / 8;
1168 }
else if (header_size > 0) {
1170 err = cbs_av1_write_trailing_bits(
ctx, pbc, 8 - end_pos % 8);
1174 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1207 memmove(pbc->
buf + data_pos,
1208 pbc->
buf + start_pos, header_size);
1216 memcpy(pbc->
buf + data_pos + header_size,
1276 memset(priv->
ref, 0,
sizeof(priv->
ref));
1290 #if CBS_AV1_OBU_METADATA
1299 switch (
md->metadata_type) {
1342 #if CBS_AV1_OBU_TILE_LIST
1346 #if CBS_AV1_OBU_PADDING
1351 #if CBS_AV1_OBU_METADATA
1359 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1361 {
"operating_point",
"Set operating point to select layers to parse from a scalable bitstream",
1363 {
"fixed_obu_size_length",
"Set fixed length of the obu_size field",
static void error(const char *err)
static av_unused size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
#define AV_LOG_WARNING
Something somehow does not look correct.
void * content_ref
If content is reference counted, a RefStruct reference backing content.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
@ AV1_OBU_REDUNDANT_FRAME_HEADER
static int get_bits_left(GetBitContext *gb)
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
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
uint8_t * data
The data buffer.
RefStruct is an API for creating reference-counted objects with minimal overhead.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
void * content
Pointer to the decomposed form of this unit.
static int get_bits_count(const GetBitContext *s)
int CBS_FUNC() read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
int CBS_FUNC() append_unit_data(CodedBitstreamFragment *frag, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Add a new unit to a fragment with the given data bitstream.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Context structure for coded bitstream operations.
AVBufferRef * frame_header_ref
static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint64_t *write_to)
CodedBitstreamUnitType type
Codec-specific type of this unit.
@ AV1_METADATA_TYPE_ITUT_T35
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
static void cbs_av1_close(CodedBitstreamContext *ctx)
@ AV1_OBU_TEMPORAL_DELIMITER
static int cbs_av1_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Coded bitstream unit structure.
static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t range_min, uint32_t range_max, const char *name, uint32_t value)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
int CBS_FUNC() write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
static int FUNC() obu_header(CodedBitstreamContext *ctx, RWContext *rw, AV1RawOBUHeader *current)
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
#define CBS_TRACE_WRITE_END_VALUE_ONLY()
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES]
static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t n, const char *name, const int *subscripts, uint32_t value)
@ CBS_CONTENT_TYPE_INTERNAL_REFS
static int put_bits_left(PutBitContext *s)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
int CBS_FUNC() alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Coded bitstream fragment structure, combining one or more units.
static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
size_t data_size
The number of bytes in the bitstream.
static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, GetBitContext *gbc, AVBufferRef **data_ref, uint8_t **data, size_t *data_size)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
@ AV1_METADATA_TYPE_HDR_CLL
#define CBS_TRACE_WRITE_END()
static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t n, const char *name, const int *subscripts, uint32_t *write_to)
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
#define CBS_TRACE_READ_END_NO_SUBSCRIPTS()
const char * av_default_item_name(void *ptr)
Return the context name.
static unsigned int get_bits1(GetBitContext *s)
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
void(* flush)(AVBSFContext *ctx)
@ AV1_METADATA_TYPE_SCALABILITY
static int cbs_av1_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t range_min, uint32_t range_max, const char *name, uint32_t *write_to)
av_cold void CBS_FUNC() close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
#define CBS_TRACE_READ_END_VALUE_ONLY()
static void cbs_av1_flush(CodedBitstreamContext *ctx)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
const CodedBitstreamType CBS_FUNC(type_av1)
AV1RawSequenceHeader sequence_header
uint8_t * data
Pointer to the bitstream form of this fragment.
static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
@ AV1_OBU_SEQUENCE_HEADER
int CBS_FUNC() make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
static const uint8_t header[24]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
@ AV1_METADATA_TYPE_HDR_MDCV
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
#define CBS_UNIT_TYPE_POD(type_, structure)
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
AV1RawFrameHeader frame_header
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
#define CBS_TRACE_WRITE_END_NO_SUBSCRIPTS()
AVBufferRef * data_ref
A reference to the buffer containing data.
#define i(width, name, range_min, range_max)
static int put_bits_count(PutBitContext *s)
static int cbs_av1_tile_log2(int blksize, int target)
static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[]
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
AV1RawTileGroup tile_group
static const AVClass cbs_av1_class
#define CBS_UNIT_TYPE_END_OF_LIST
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
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_INPUT_BUFFER_PADDING_SIZE
@ AV1_METADATA_TYPE_TIMECODE
AV1RawSequenceHeader * sequence_header
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
@ AV_OPT_TYPE_INT
Underlying C type is int.
static int cbs_av1_write_obu(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t value)
static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint64_t value, int fixed_length)
A reference to a data buffer.
AV1RawTileGroup tile_group
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void cbs_av1_free_metadata(AVRefStructOpaque unused, void *content)
@ AV1_MAX_OPERATING_POINTS
AV1RawOBU * sequence_header_ref
A RefStruct reference backing sequence_header.
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
static const AVOption cbs_av1_options[]
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t *write_to)
AVBufferRef * data_ref
A reference to the buffer containing data.
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
int fixed_obu_size_length
#define CBS_TRACE_READ_START()
void * priv_data
Format private data.
int nb_units
Number of units in this fragment.
#define CBS_TRACE_READ_END()
#define CBS_TRACE_WRITE_START()