33 const char *
name, uint32_t *write_to,
34 uint32_t range_min, uint32_t range_max)
36 uint32_t zeroes, bits_value,
value;
44 "%s: bitstream ended.\n",
name);
63 "%s uvlc code: considered invalid due to conflicting "
64 "standard and reference decoder behaviour.\n",
name);
69 "%s: bitstream ended.\n",
name);
74 value = bits_value + (UINT32_C(1) << zeroes) - 1;
81 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
94 uint32_t range_min, uint32_t range_max)
103 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
109 v =
value - (1U << zeroes) + 1;
126 const char *
name, uint64_t *write_to)
135 for (
i = 0;
i < 8;
i++) {
138 "%s: bitstream ended.\n",
name);
142 value |= (uint64_t)(
byte & 0x7f) << (
i * 7);
147 if (
value > UINT32_MAX)
159 const char *
name, uint64_t
value,
int fixed_length)
169 if (fixed_length <
len) {
171 "fixed length size field (%d > %d).\n",
178 for (
i = 0;
i <
len;
i++) {
182 byte =
value >> (7 *
i) & 0x7f;
197 uint32_t n,
const char *
name,
198 const int *subscripts, uint32_t *write_to)
200 uint32_t m, v, extra_bit,
value;
212 "%s: bitstream ended.\n",
name);
225 value = (v << 1) - m + extra_bit;
237 uint32_t n,
const char *
name,
238 const int *subscripts, uint32_t
value)
240 uint32_t
w, m, v, extra_bit;
246 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
261 v = m + ((
value - m) >> 1);
262 extra_bit = (
value - m) & 1;
275 uint32_t range_min, uint32_t range_max,
276 const char *
name, uint32_t *write_to)
282 av_assert0(range_min <= range_max && range_max - range_min < 32);
287 "%s: bitstream ended.\n",
name);
305 uint32_t range_min, uint32_t range_max,
312 av_assert0(range_min <= range_max && range_max - range_min < 32);
315 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
320 if (
value == range_max)
321 len = range_max - range_min;
338 uint32_t range_max,
const char *
name,
339 const int *subscripts, uint32_t *write_to)
341 uint32_t
value, max_len,
len, range_offset, range_bits;
347 max_len =
av_log2(range_max - 1) - 3;
349 err = cbs_av1_read_increment(
ctx, gbc, 0, max_len,
350 "subexp_more_bits", &
len);
355 range_bits = 2 +
len;
356 range_offset = 1 << range_bits;
364 "subexp_bits", &
value);
369 err = cbs_av1_read_ns(
ctx, gbc, range_max - range_offset,
374 value += range_offset;
385 uint32_t range_max,
const char *
name,
386 const int *subscripts, uint32_t
value)
389 uint32_t max_len,
len, range_offset, range_bits;
393 if (
value > range_max) {
395 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
401 max_len =
av_log2(range_max - 1) - 3;
409 len = range_bits - 2;
416 range_offset = 1 << range_bits;
419 err = cbs_av1_write_increment(
ctx, pbc, 0, max_len,
420 "subexp_more_bits",
len);
427 value - range_offset);
432 err = cbs_av1_write_ns(
ctx, pbc, range_max - range_offset,
433 "subexp_final_bits",
NULL,
434 value - range_offset);
449 for (k = 0; (blksize << k) < target; k++);
454 unsigned int a,
unsigned int b)
456 unsigned int diff, m;
477#define HEADER(name) do { \
478 CBS_FUNC(trace_header)(ctx, name); \
481#define CHECK(call) do { \
487#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
488#define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
489#define FUNC(name) FUNC_AV1(READWRITE, name)
491#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
494#define fc(width, name, range_min, range_max) \
495 xf(width, name, current->name, range_min, range_max, 0, )
496#define flag(name) fb(1, name)
497#define su(width, name) \
498 xsu(width, name, current->name, 0, )
500#define fbs(width, name, subs, ...) \
501 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
502#define fcs(width, name, range_min, range_max, subs, ...) \
503 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
504#define flags(name, subs, ...) \
505 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
506#define sus(width, name, subs, ...) \
507 xsu(width, name, current->name, subs, __VA_ARGS__)
509#define fixed(width, name, value) do { \
510 av_unused uint32_t fixed_value = value; \
511 xf(width, name, fixed_value, value, value, 0, ); \
516#define READWRITE read
517#define RWContext GetBitContext
519#define fb(width, name) do { \
521 CHECK(CBS_FUNC(read_simple_unsigned)(ctx, rw, width, \
523 current->name = value; \
526#define xf(width, name, var, range_min, range_max, subs, ...) do { \
528 CHECK(CBS_FUNC(read_unsigned)(ctx, rw, width, #name, \
529 SUBSCRIPTS(subs, __VA_ARGS__), \
530 &value, range_min, range_max)); \
534#define xsu(width, name, var, subs, ...) do { \
536 CHECK(CBS_FUNC(read_signed)(ctx, rw, width, #name, \
537 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
538 MIN_INT_BITS(width), \
539 MAX_INT_BITS(width))); \
543#define uvlc(name, range_min, range_max) do { \
545 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
546 &value, range_min, range_max)); \
547 current->name = value; \
550#define ns(max_value, name, subs, ...) do { \
552 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
553 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
554 current->name = value; \
557#define increment(name, min, max) do { \
559 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
560 current->name = value; \
563#define subexp(name, max, subs, ...) do { \
565 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
566 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
567 current->name = value; \
570#define delta_q(name) do { \
571 uint8_t delta_coded; \
573 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
575 xsu(1 + 6, name.delta_q, delta_q, 0, ); \
578 current->name = delta_q; \
581#define leb128(name) do { \
583 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
584 current->name = value; \
587#define infer(name, value) do { \
588 current->name = value; \
591#define byte_alignment(rw) (get_bits_count(rw) % 8)
614#define READWRITE write
615#define RWContext PutBitContext
617#define fb(width, name) do { \
618 CHECK(CBS_FUNC(write_simple_unsigned)(ctx, rw, width, #name, \
622#define xf(width, name, var, range_min, range_max, subs, ...) do { \
623 CHECK(CBS_FUNC(write_unsigned)(ctx, rw, width, #name, \
624 SUBSCRIPTS(subs, __VA_ARGS__), \
625 var, range_min, range_max)); \
628#define xsu(width, name, var, subs, ...) do { \
629 CHECK(CBS_FUNC(write_signed)(ctx, rw, width, #name, \
630 SUBSCRIPTS(subs, __VA_ARGS__), var, \
631 MIN_INT_BITS(width), \
632 MAX_INT_BITS(width))); \
635#define uvlc(name, range_min, range_max) do { \
636 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
637 range_min, range_max)); \
640#define ns(max_value, name, subs, ...) do { \
641 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
642 SUBSCRIPTS(subs, __VA_ARGS__), \
646#define increment(name, min, max) do { \
647 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
651#define subexp(name, max, subs, ...) do { \
652 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
653 SUBSCRIPTS(subs, __VA_ARGS__), \
657#define delta_q(name) do { \
658 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
660 xsu(1 + 6, name.delta_q, current->name, 0, ); \
663#define leb128(name) do { \
664 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name, 0)); \
667#define infer(name, value) do { \
668 if (current->name != (value)) { \
669 av_log(ctx->log_ctx, AV_LOG_ERROR, \
670 "%s does not match inferred value: " \
671 "%"PRId64", but should be %"PRId64".\n", \
672 #name, (int64_t)current->name, (int64_t)(value)); \
673 return AVERROR_INVALIDDATA; \
677#define byte_alignment(rw) (put_bits_count(rw) % 8)
709 trace =
ctx->trace_enable;
710 ctx->trace_enable = 0;
715 if (INT_MAX / 8 <
size) {
717 "too large (%zu bytes).\n",
size);
725 int config_record_version =
data[0] & 0x7f;
727 if (config_record_version != 1) {
729 "Unknown version %d of AV1CodecConfigurationRecord "
731 config_record_version);
739 "Undersized AV1CodecConfigurationRecord v%d found!\n",
740 config_record_version);
768 "too short (%zu bytes).\n",
size);
772 err = cbs_av1_read_leb128(
ctx, &gbc,
"obu_size", &obu_size);
781 obu_length =
pos / 8 + obu_size;
783 if (
size < obu_length) {
785 "%"PRIu64
", but only %zu bytes remaining in fragment.\n",
803 ctx->trace_enable = trace;
815 uint8_t **
data,
size_t *data_size)
822 "any data in tile group (%d bits read).\n",
pos);
846 int err, start_pos, end_pos;
857 err = cbs_av1_read_obu_header(
ctx, &gbc, &obu->
header);
864 err = cbs_av1_read_leb128(
ctx, &gbc,
"obu_size", &obu_size);
871 "unit too short (%zu).\n", unit->
data_size);
883 int in_temporal_layer =
885 int in_spatial_layer =
887 if (!in_temporal_layer || !in_spatial_layer) {
896 err = cbs_av1_read_sequence_header_obu(
ctx, &gbc,
906 "Must not be higher than %u.\n",
919 err = cbs_av1_read_temporal_delimiter_obu(
ctx, &gbc);
927 err = cbs_av1_read_frame_header_obu(
ctx, &gbc,
937 err = cbs_av1_read_frame_obu(
ctx, &gbc, &obu->
obu.
frame,
950 err = cbs_av1_ref_tile_data(
ctx, unit, &gbc,
957 err = cbs_av1_read_tile_group_obu(
ctx, &gbc, tile_group);
961 err = cbs_av1_ref_tile_data(
ctx, unit, &gbc,
969#if CBS_AV1_OBU_TILE_LIST
972 err = cbs_av1_read_tile_list_obu(
ctx, &gbc,
973 &obu->
obu.tile_list);
977 err = cbs_av1_ref_tile_data(
ctx, unit, &gbc,
978 &obu->
obu.tile_list.tile_data.data_ref,
979 &obu->
obu.tile_list.tile_data.data,
980 &obu->
obu.tile_list.tile_data.data_size);
986#if CBS_AV1_OBU_METADATA
989 err = cbs_av1_read_metadata_obu(
ctx, &gbc, &obu->
obu.metadata);
995#if CBS_AV1_OBU_PADDING
998 err = cbs_av1_read_padding_obu(
ctx, &gbc, &obu->
obu.padding);
1015 int nb_bits = obu->
obu_size * 8 + start_pos - end_pos;
1020 err = cbs_av1_read_trailing_bits(
ctx, &gbc, nb_bits);
1041 int err, start_pos, end_pos, data_pos;
1062 err = cbs_av1_write_obu_header(
ctx, pbc, &obu->
header);
1084 err = cbs_av1_write_sequence_header_obu(
ctx, pbc,
1102 err = cbs_av1_write_temporal_delimiter_obu(
ctx, pbc);
1110 err = cbs_av1_write_frame_header_obu(
ctx, pbc,
1128 err = cbs_av1_write_tile_group_obu(
ctx, pbc, tile_group);
1135#if CBS_AV1_OBU_TILE_LIST
1138 err = cbs_av1_write_tile_list_obu(
ctx, pbc, &obu->
obu.tile_list);
1142 td = &obu->
obu.tile_list.tile_data;
1146#if CBS_AV1_OBU_METADATA
1149 err = cbs_av1_write_metadata_obu(
ctx, pbc, &obu->
obu.metadata);
1155#if CBS_AV1_OBU_PADDING
1158 err = cbs_av1_write_padding_obu(
ctx, pbc, &obu->
obu.padding);
1170 header_size = (end_pos - start_pos + 7) / 8;
1173 }
else if (header_size > 0) {
1175 err = cbs_av1_write_trailing_bits(
ctx, pbc, 8 - end_pos % 8);
1179 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1193 err = cbs_av1_write_leb128(
ctx, pbc,
"obu_size", obu->
obu_size,
1212 memmove(pbc->
buf + data_pos,
1213 pbc->
buf + start_pos, header_size);
1221 memcpy(pbc->
buf + data_pos + header_size,
1281 memset(priv->
ref, 0,
sizeof(priv->
ref));
1295#if CBS_AV1_OBU_METADATA
1302 md = &obu->
obu.metadata;
1304 switch (
md->metadata_type) {
1347#if CBS_AV1_OBU_TILE_LIST
1349 obu.tile_list.tile_data.data),
1351#if CBS_AV1_OBU_PADDING
1353 obu.padding.payload),
1356#if CBS_AV1_OBU_METADATA
1358 &cbs_av1_free_metadata),
1364#define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1366 {
"operating_point",
"Set operating point to select layers to parse from a scalable bitstream",
1368 {
"fixed_obu_size_length",
"Set fixed length of the obu_size field",
1374 .class_name =
"cbs_av1",
static av_cold void close(AVCodecParserContext *s)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
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
static int FUNC obu_header(CodedBitstreamContext *ctx, RWContext *rw, AV1RawOBUHeader *current)
#define i(width, name, range_min, range_max)
#define CBS_UNIT_TYPE_POD(type_, structure)
int CBS_FUNC read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
#define CBS_TRACE_READ_END()
#define CBS_UNIT_TYPE_END_OF_LIST
#define CBS_TRACE_READ_END_VALUE_ONLY()
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
@ CBS_CONTENT_TYPE_INTERNAL_REFS
#define CBS_TRACE_WRITE_END_NO_SUBSCRIPTS()
#define CBS_TRACE_WRITE_END_VALUE_ONLY()
#define CBS_TRACE_READ_START()
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
#define CBS_TRACE_READ_END_NO_SUBSCRIPTS()
int CBS_FUNC write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
#define CBS_TRACE_WRITE_END()
#define CBS_TRACE_WRITE_START()
static int FUNC sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
Misc types and constants that do not belong anywhere else.
void(* flush)(AVBSFContext *ctx)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int get_bits_left(GetBitContext *gb)
static unsigned int get_bits1(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#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.
#define LIBAVUTIL_VERSION_INT
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
@ AV1_METADATA_TYPE_SCALABILITY
@ AV1_METADATA_TYPE_HDR_MDCV
@ AV1_METADATA_TYPE_TIMECODE
@ AV1_METADATA_TYPE_ITUT_T35
@ AV1_METADATA_TYPE_HDR_CLL
@ AV1_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_REDUNDANT_FRAME_HEADER
@ AV1_OBU_SEQUENCE_HEADER
@ AV1_MAX_OPERATING_POINTS
int CBS_FUNC alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
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.
int CBS_FUNC make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
static const AVOption cbs_av1_options[]
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
static int cbs_av1_write_obu(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[]
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
static av_cold void cbs_av1_close(CodedBitstreamContext *ctx)
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
static const AVClass cbs_av1_class
static av_cold void cbs_av1_flush(CodedBitstreamContext *ctx)
static av_unused size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
static int cbs_av1_tile_log2(int blksize, int target)
static int cbs_av1_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Macro definitions for various function/variable attributes.
static int put_bits_count(PutBitContext *s)
static int put_bits_left(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
static const uint8_t header[24]
AV1RawTileGroup tile_group
AV1RawFrameHeader frame_header
AV1RawTileGroup tile_group
AV1RawSequenceHeader sequence_header
union AV1RawOBU::@016362317061177152367125047142162076164261250366 obu
A reference to a data buffer.
uint8_t * data
The data buffer.
Describe the class of an AVClass context structure.
AV1RawOBU * sequence_header_ref
A RefStruct reference backing sequence_header.
int fixed_obu_size_length
AV1RawSequenceHeader * sequence_header
AVBufferRef * frame_header_ref
AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES]
Context structure for coded bitstream operations.
Coded bitstream fragment structure, combining one or more units.
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
int nb_units
Number of units in this fragment.
size_t data_size
The number of bytes in the bitstream.
uint8_t * data
Pointer to the bitstream form of this fragment.
AVBufferRef * data_ref
A reference to the buffer containing data.
Coded bitstream unit structure.
void * content
Pointer to the decomposed form of this unit.
void * content_ref
If content is reference counted, a RefStruct reference backing content.
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
CodedBitstreamUnitType type
Codec-specific type of this unit.
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
AVBufferRef * data_ref
A reference to the buffer containing data.
static void error(const char *err)
static AVFormatContext * ctx
RefStruct is an API for creating reference-counted objects with minimal overhead.
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)