Go to the documentation of this file.
31 const char *
name, uint32_t *write_to,
32 uint32_t range_min, uint32_t range_max)
34 uint32_t zeroes, bits_value,
value;
42 "%s: bitstream ended.\n",
name);
61 "%s uvlc code: considered invalid due to conflicting "
62 "standard and reference decoder behaviour.\n",
name);
67 "%s: bitstream ended.\n",
name);
72 value = bits_value + (UINT32_C(1) << zeroes) - 1;
77 if (value < range_min || value > range_max) {
79 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
90 uint32_t range_min, uint32_t range_max)
97 if (value < range_min || value > range_max) {
99 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
105 v =
value - (1
U << zeroes) + 1;
120 const char *
name, uint64_t *write_to)
129 for (
i = 0;
i < 8;
i++) {
132 "%s: bitstream ended.\n",
name);
136 value |= (uint64_t)(
byte & 0x7f) << (
i * 7);
141 if (
value > UINT32_MAX)
151 const char *
name, uint64_t
value,
int fixed_length)
161 if (fixed_length <
len) {
163 "fixed length size field (%d > %d).\n",
170 for (
i = 0;
i <
len;
i++) {
174 byte =
value >> (7 *
i) & 0x7f;
187 uint32_t n,
const char *
name,
188 const int *subscripts, uint32_t *write_to)
190 uint32_t m, v, extra_bit,
value;
202 "%s: bitstream ended.\n",
name);
215 value = (v << 1) - m + extra_bit;
225 uint32_t n,
const char *
name,
226 const int *subscripts, uint32_t
value)
228 uint32_t
w, m, v, extra_bit;
234 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
249 v = m + ((
value - m) >> 1);
250 extra_bit = (
value - m) & 1;
261 uint32_t range_min, uint32_t range_max,
262 const char *
name, uint32_t *write_to)
268 av_assert0(range_min <= range_max && range_max - range_min < 32);
273 "%s: bitstream ended.\n",
name);
289 uint32_t range_min, uint32_t range_max,
296 av_assert0(range_min <= range_max && range_max - range_min < 32);
297 if (value < range_min || value > range_max) {
299 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
304 if (
value == range_max)
305 len = range_max - range_min;
320 uint32_t range_max,
const char *
name,
321 const int *subscripts, uint32_t *write_to)
323 uint32_t
value, max_len,
len, range_offset, range_bits;
329 max_len =
av_log2(range_max - 1) - 3;
332 "subexp_more_bits", &
len);
337 range_bits = 2 +
len;
338 range_offset = 1 << range_bits;
346 "subexp_bits", &
value);
356 value += range_offset;
365 uint32_t range_max,
const char *
name,
366 const int *subscripts, uint32_t
value)
369 uint32_t max_len,
len, range_offset, range_bits;
373 if (
value > range_max) {
375 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
381 max_len =
av_log2(range_max - 1) - 3;
389 len = range_bits - 2;
396 range_offset = 1 << range_bits;
400 "subexp_more_bits",
len);
407 value - range_offset);
413 "subexp_final_bits",
NULL,
414 value - range_offset);
428 for (k = 0; (blksize << k) < target; k++);
433 unsigned int a,
unsigned int b)
435 unsigned int diff, m;
456 #define HEADER(name) do { \
457 ff_cbs_trace_header(ctx, name); \
460 #define CHECK(call) do { \
466 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
467 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
468 #define FUNC(name) FUNC_AV1(READWRITE, name)
470 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
472 #define fc(width, name, range_min, range_max) \
473 xf(width, name, current->name, range_min, range_max, 0, )
474 #define flag(name) fb(1, name)
475 #define su(width, name) \
476 xsu(width, name, current->name, 0, )
478 #define fbs(width, name, subs, ...) \
479 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
480 #define fcs(width, name, range_min, range_max, subs, ...) \
481 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
482 #define flags(name, subs, ...) \
483 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
484 #define sus(width, name, subs, ...) \
485 xsu(width, name, current->name, subs, __VA_ARGS__)
487 #define fixed(width, name, value) do { \
488 av_unused uint32_t fixed_value = value; \
489 xf(width, name, fixed_value, value, value, 0, ); \
494 #define READWRITE read
495 #define RWContext GetBitContext
497 #define fb(width, name) do { \
499 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, \
501 current->name = value; \
504 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
506 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
507 SUBSCRIPTS(subs, __VA_ARGS__), \
508 &value, range_min, range_max)); \
512 #define xsu(width, name, var, subs, ...) do { \
514 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
515 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
516 MIN_INT_BITS(width), \
517 MAX_INT_BITS(width))); \
521 #define uvlc(name, range_min, range_max) do { \
523 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
524 &value, range_min, range_max)); \
525 current->name = value; \
528 #define ns(max_value, name, subs, ...) do { \
530 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
531 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
532 current->name = value; \
535 #define increment(name, min, max) do { \
537 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
538 current->name = value; \
541 #define subexp(name, max, subs, ...) do { \
543 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
544 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
545 current->name = value; \
548 #define delta_q(name) do { \
549 uint8_t delta_coded; \
551 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
553 xsu(1 + 6, name.delta_q, delta_q, 0, ); \
556 current->name = delta_q; \
559 #define leb128(name) do { \
561 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
562 current->name = value; \
565 #define infer(name, value) do { \
566 current->name = value; \
569 #define byte_alignment(rw) (get_bits_count(rw) % 8)
586 #undef byte_alignment
590 #define READWRITE write
591 #define RWContext PutBitContext
593 #define fb(width, name) do { \
594 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
598 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
599 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
600 SUBSCRIPTS(subs, __VA_ARGS__), \
601 var, range_min, range_max)); \
604 #define xsu(width, name, var, subs, ...) do { \
605 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
606 SUBSCRIPTS(subs, __VA_ARGS__), var, \
607 MIN_INT_BITS(width), \
608 MAX_INT_BITS(width))); \
611 #define uvlc(name, range_min, range_max) do { \
612 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
613 range_min, range_max)); \
616 #define ns(max_value, name, subs, ...) do { \
617 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
618 SUBSCRIPTS(subs, __VA_ARGS__), \
622 #define increment(name, min, max) do { \
623 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
627 #define subexp(name, max, subs, ...) do { \
628 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
629 SUBSCRIPTS(subs, __VA_ARGS__), \
633 #define delta_q(name) do { \
634 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
636 xsu(1 + 6, name.delta_q, current->name, 0, ); \
639 #define leb128(name) do { \
640 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name, 0)); \
643 #define infer(name, value) do { \
644 if (current->name != (value)) { \
645 av_log(ctx->log_ctx, AV_LOG_ERROR, \
646 "%s does not match inferred value: " \
647 "%"PRId64", but should be %"PRId64".\n", \
648 #name, (int64_t)current->name, (int64_t)(value)); \
649 return AVERROR_INVALIDDATA; \
653 #define byte_alignment(rw) (put_bits_count(rw) % 8)
670 #undef byte_alignment
684 trace =
ctx->trace_enable;
685 ctx->trace_enable = 0;
690 if (INT_MAX / 8 <
size) {
700 int config_record_version =
data[0] & 0x7f;
702 if (config_record_version != 1) {
704 "Unknown version %d of AV1CodecConfigurationRecord "
706 config_record_version);
714 "Undersized AV1CodecConfigurationRecord v%d found!\n",
715 config_record_version);
736 err = cbs_av1_read_obu_header(
ctx, &gbc, &
header);
740 if (
header.obu_has_size_field) {
751 obu_size =
size - 1 -
header.obu_extension_flag;
756 obu_length =
pos / 8 + obu_size;
758 if (
size < obu_length) {
760 "%"PRIu64
", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
778 ctx->trace_enable = trace;
792 "any data in tile group (%d bits read).\n",
pos);
814 int err, start_pos, end_pos;
825 err = cbs_av1_read_obu_header(
ctx, &gbc, &obu->
header);
851 int in_temporal_layer =
853 int in_spatial_layer =
855 if (!in_temporal_layer || !in_spatial_layer) {
864 err = cbs_av1_read_sequence_header_obu(
ctx, &gbc,
874 "Must not be higher than %u.\n",
887 err = cbs_av1_read_temporal_delimiter_obu(
ctx, &gbc);
895 err = cbs_av1_read_frame_header_obu(
ctx, &gbc,
906 err = cbs_av1_read_tile_group_obu(
ctx, &gbc,
919 err = cbs_av1_read_frame_obu(
ctx, &gbc, &obu->
obu.
frame,
932 err = cbs_av1_read_tile_list_obu(
ctx, &gbc,
952 err = cbs_av1_read_padding_obu(
ctx, &gbc, &obu->
obu.
padding);
968 int nb_bits = obu->
obu_size * 8 + start_pos - end_pos;
973 err = cbs_av1_read_trailing_bits(
ctx, &gbc, nb_bits);
990 int err, start_pos, end_pos, data_pos;
1011 err = cbs_av1_write_obu_header(
ctx, pbc, &obu->
header);
1033 err = cbs_av1_write_sequence_header_obu(
ctx, pbc,
1051 err = cbs_av1_write_temporal_delimiter_obu(
ctx, pbc);
1059 err = cbs_av1_write_frame_header_obu(
ctx, pbc,
1070 err = cbs_av1_write_tile_group_obu(
ctx, pbc,
1105 err = cbs_av1_write_padding_obu(
ctx, pbc, &obu->
obu.
padding);
1116 header_size = (end_pos - start_pos + 7) / 8;
1119 }
else if (header_size > 0) {
1121 err = cbs_av1_write_trailing_bits(
ctx, pbc, 8 - end_pos % 8);
1125 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1158 memmove(pbc->
buf + data_pos,
1159 pbc->
buf + start_pos, header_size);
1167 memcpy(pbc->
buf + data_pos + header_size,
1220 memset(priv->
ref, 0,
sizeof(priv->
ref));
1242 switch (
md->metadata_type) {
1277 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1279 {
"operating_point",
"Set operating point to select layers to parse from a scalable bitstream",
1281 {
"fixed_obu_size_length",
"Set fixed length of the obu_size field",
static void error(const char *err)
#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 const AVClass cbs_av1_class
static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[]
void * ff_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
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.
static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t range_min, uint32_t range_max, const char *name, uint32_t value)
void(* close)(CodedBitstreamContext *ctx)
uint8_t * data
The data buffer.
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)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
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)
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)
static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t n, const char *name, const int *subscripts, uint32_t *write_to)
CodedBitstreamUnitType type
Codec-specific type of this unit.
RefStruct is an API for creating reference-counted objects with minimal overhead.
@ AV1_METADATA_TYPE_ITUT_T35
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
@ AV1_OBU_TEMPORAL_DELIMITER
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Coded bitstream unit structure.
static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t *write_to)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
#define CBS_TRACE_WRITE_END_VALUE_ONLY()
AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES]
static int put_bits_left(PutBitContext *s)
static void cbs_av1_close(CodedBitstreamContext *ctx)
const CodedBitstreamType ff_cbs_type_av1
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
static int cbs_av1_write_obu(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
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.
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)
Coded bitstream fragment structure, combining one or more units.
size_t data_size
The number of bytes in the bitstream.
static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, GetBitContext *gbc, AV1RawTileData *td)
static void cbs_av1_free_metadata(FFRefStructOpaque unused, void *content)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
@ AV1_METADATA_TYPE_HDR_CLL
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
int ff_cbs_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.
#define CBS_TRACE_WRITE_END()
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
#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.
static int cbs_av1_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
static const AVOption cbs_av1_options[]
#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_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t value)
int ff_cbs_read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
#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
AV1RawSequenceHeader sequence_header
uint8_t * data
Pointer to the bitstream form of this fragment.
@ AV1_OBU_SEQUENCE_HEADER
const CodedBitstreamUnitTypeDescriptor * unit_types
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
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
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
static int cbs_av1_tile_log2(int blksize, int target)
#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)
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static int put_bits_count(PutBitContext *s)
static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint64_t value, int fixed_length)
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
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
AV1RawTileGroup tile_group
#define CBS_UNIT_TYPE_END_OF_LIST
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
#define AV_INPUT_BUFFER_PADDING_SIZE
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
@ AV1_METADATA_TYPE_TIMECODE
int ff_cbs_write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
AV1RawSequenceHeader * sequence_header
@ AV_OPT_TYPE_INT
Underlying C type is int.
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
AV1RawTileGroup tile_group
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
@ AV1_MAX_OPERATING_POINTS
static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t n, const char *name, const int *subscripts, uint32_t value)
AV1RawOBU * sequence_header_ref
A RefStruct reference backing sequence_header.
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
AVBufferRef * data_ref
A reference to the buffer containing data.
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
int fixed_obu_size_length
#define CBS_TRACE_READ_START()
void * priv_data
Format private data.
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
int nb_units
Number of units in this fragment.
#define CBS_TRACE_READ_END()
#define CBS_TRACE_WRITE_START()