30#define MAX_FRAGMENT_OBUS (1 << 16)
35 const char *
name, uint32_t *write_to,
36 uint32_t range_min, uint32_t range_max)
38 uint32_t zeroes, bits_value,
value;
46 "%s: bitstream ended.\n",
name);
65 "%s uvlc code: considered invalid due to conflicting "
66 "standard and reference decoder behaviour.\n",
name);
71 "%s: bitstream ended.\n",
name);
76 value = bits_value + (UINT32_C(1) << zeroes) - 1;
83 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
96 uint32_t range_min, uint32_t range_max)
105 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
111 v =
value - (1U << zeroes) + 1;
128 const char *
name, uint64_t *write_to)
137 for (
i = 0;
i < 8;
i++) {
140 "%s: bitstream ended.\n",
name);
144 value |= (uint64_t)(
byte & 0x7f) << (
i * 7);
149 if (
value > UINT32_MAX)
161 const char *
name, uint64_t
value,
int fixed_length)
171 if (fixed_length <
len) {
173 "fixed length size field (%d > %d).\n",
180 for (
i = 0;
i <
len;
i++) {
184 byte =
value >> (7 *
i) & 0x7f;
199 uint32_t n,
const char *
name,
200 const int *subscripts, uint32_t *write_to)
202 uint32_t m, v, extra_bit,
value;
214 "%s: bitstream ended.\n",
name);
227 value = (v << 1) - m + extra_bit;
239 uint32_t n,
const char *
name,
240 const int *subscripts, uint32_t
value)
242 uint32_t
w, m, v, extra_bit;
248 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
263 v = m + ((
value - m) >> 1);
264 extra_bit = (
value - m) & 1;
277 uint32_t range_min, uint32_t range_max,
278 const char *
name, uint32_t *write_to)
284 av_assert0(range_min <= range_max && range_max - range_min < 32);
289 "%s: bitstream ended.\n",
name);
307 uint32_t range_min, uint32_t range_max,
314 av_assert0(range_min <= range_max && range_max - range_min < 32);
317 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
322 if (
value == range_max)
323 len = range_max - range_min;
340 uint32_t range_max,
const char *
name,
341 const int *subscripts, uint32_t *write_to)
343 uint32_t
value, max_len,
len, range_offset, range_bits;
349 max_len =
av_log2(range_max - 1) - 3;
351 err = cbs_av1_read_increment(
ctx, gbc, 0, max_len,
352 "subexp_more_bits", &
len);
357 range_bits = 2 +
len;
358 range_offset = 1 << range_bits;
366 "subexp_bits", &
value);
371 err = cbs_av1_read_ns(
ctx, gbc, range_max - range_offset,
376 value += range_offset;
387 uint32_t range_max,
const char *
name,
388 const int *subscripts, uint32_t
value)
391 uint32_t max_len,
len, range_offset, range_bits;
395 if (
value > range_max) {
397 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
403 max_len =
av_log2(range_max - 1) - 3;
411 len = range_bits - 2;
418 range_offset = 1 << range_bits;
421 err = cbs_av1_write_increment(
ctx, pbc, 0, max_len,
422 "subexp_more_bits",
len);
429 value - range_offset);
434 err = cbs_av1_write_ns(
ctx, pbc, range_max - range_offset,
435 "subexp_final_bits",
NULL,
436 value - range_offset);
451 for (k = 0; (blksize << k) < target; k++);
456 unsigned int a,
unsigned int b)
458 unsigned int diff, m;
479#define HEADER(name) do { \
480 CBS_FUNC(trace_header)(ctx, name); \
483#define CHECK(call) do { \
489#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
490#define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
491#define FUNC(name) FUNC_AV1(READWRITE, name)
493#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
496#define fc(width, name, range_min, range_max) \
497 xf(width, name, current->name, range_min, range_max, 0, )
498#define flag(name) fb(1, name)
499#define su(width, name) \
500 xsu(width, name, current->name, 0, )
502#define fbs(width, name, subs, ...) \
503 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
504#define fcs(width, name, range_min, range_max, subs, ...) \
505 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
506#define flags(name, subs, ...) \
507 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
508#define sus(width, name, subs, ...) \
509 xsu(width, name, current->name, subs, __VA_ARGS__)
511#define fixed(width, name, value) do { \
512 av_unused uint32_t fixed_value = value; \
513 xf(width, name, fixed_value, value, value, 0, ); \
518#define READWRITE read
519#define RWContext GetBitContext
521#define fb(width, name) do { \
523 CHECK(CBS_FUNC(read_simple_unsigned)(ctx, rw, width, \
525 current->name = value; \
528#define xf(width, name, var, range_min, range_max, subs, ...) do { \
530 CHECK(CBS_FUNC(read_unsigned)(ctx, rw, width, #name, \
531 SUBSCRIPTS(subs, __VA_ARGS__), \
532 &value, range_min, range_max)); \
536#define xsu(width, name, var, subs, ...) do { \
538 CHECK(CBS_FUNC(read_signed)(ctx, rw, width, #name, \
539 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
540 MIN_INT_BITS(width), \
541 MAX_INT_BITS(width))); \
545#define uvlc(name, range_min, range_max) do { \
547 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
548 &value, range_min, range_max)); \
549 current->name = value; \
552#define ns(max_value, name, subs, ...) do { \
554 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
555 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
556 current->name = value; \
559#define increment(name, min, max) do { \
561 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
562 current->name = value; \
565#define subexp(name, max, subs, ...) do { \
567 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
568 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
569 current->name = value; \
572#define delta_q(name) do { \
573 uint8_t delta_coded; \
575 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
577 xsu(1 + 6, name.delta_q, delta_q, 0, ); \
580 current->name = delta_q; \
583#define leb128(name) do { \
585 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
586 current->name = value; \
589#define infer(name, value) do { \
590 current->name = value; \
593#define byte_alignment(rw) (get_bits_count(rw) % 8)
616#define READWRITE write
617#define RWContext PutBitContext
619#define fb(width, name) do { \
620 CHECK(CBS_FUNC(write_simple_unsigned)(ctx, rw, width, #name, \
624#define xf(width, name, var, range_min, range_max, subs, ...) do { \
625 CHECK(CBS_FUNC(write_unsigned)(ctx, rw, width, #name, \
626 SUBSCRIPTS(subs, __VA_ARGS__), \
627 var, range_min, range_max)); \
630#define xsu(width, name, var, subs, ...) do { \
631 CHECK(CBS_FUNC(write_signed)(ctx, rw, width, #name, \
632 SUBSCRIPTS(subs, __VA_ARGS__), var, \
633 MIN_INT_BITS(width), \
634 MAX_INT_BITS(width))); \
637#define uvlc(name, range_min, range_max) do { \
638 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
639 range_min, range_max)); \
642#define ns(max_value, name, subs, ...) do { \
643 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
644 SUBSCRIPTS(subs, __VA_ARGS__), \
648#define increment(name, min, max) do { \
649 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
653#define subexp(name, max, subs, ...) do { \
654 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
655 SUBSCRIPTS(subs, __VA_ARGS__), \
659#define delta_q(name) do { \
660 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
662 xsu(1 + 6, name.delta_q, current->name, 0, ); \
665#define leb128(name) do { \
666 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name, 0)); \
669#define infer(name, value) do { \
670 if (current->name != (value)) { \
671 av_log(ctx->log_ctx, AV_LOG_ERROR, \
672 "%s does not match inferred value: " \
673 "%"PRId64", but should be %"PRId64".\n", \
674 #name, (int64_t)current->name, (int64_t)(value)); \
675 return AVERROR_INVALIDDATA; \
679#define byte_alignment(rw) (put_bits_count(rw) % 8)
711 trace =
ctx->trace_enable;
712 ctx->trace_enable = 0;
717 if (INT_MAX / 8 <
size) {
719 "too large (%zu bytes).\n",
size);
727 int config_record_version =
data[0] & 0x7f;
729 if (config_record_version != 1) {
731 "Unknown version %d of AV1CodecConfigurationRecord "
733 config_record_version);
741 "Undersized AV1CodecConfigurationRecord v%d found!\n",
742 config_record_version);
770 "too short (%zu bytes).\n",
size);
774 err = cbs_av1_read_leb128(
ctx, &gbc,
"obu_size", &obu_size);
783 obu_length =
pos / 8 + obu_size;
785 if (
size < obu_length) {
787 "%"PRIu64
", but only %zu bytes remaining in fragment.\n",
811 ctx->trace_enable = trace;
823 uint8_t **
data,
size_t *data_size)
830 "any data in tile group (%d bits read).\n",
pos);
854 int err, start_pos, end_pos;
865 err = cbs_av1_read_obu_header(
ctx, &gbc, &obu->
header);
872 err = cbs_av1_read_leb128(
ctx, &gbc,
"obu_size", &obu_size);
879 "unit too short (%zu).\n", unit->
data_size);
891 int in_temporal_layer =
893 int in_spatial_layer =
895 if (!in_temporal_layer || !in_spatial_layer) {
904 err = cbs_av1_read_sequence_header_obu(
ctx, &gbc,
914 "Must not be higher than %u.\n",
927 err = cbs_av1_read_temporal_delimiter_obu(
ctx, &gbc);
935 err = cbs_av1_read_frame_header_obu(
ctx, &gbc,
945 err = cbs_av1_read_frame_obu(
ctx, &gbc, &obu->
obu.
frame,
958 err = cbs_av1_ref_tile_data(
ctx, unit, &gbc,
965 err = cbs_av1_read_tile_group_obu(
ctx, &gbc, tile_group);
969 err = cbs_av1_ref_tile_data(
ctx, unit, &gbc,
977#if CBS_AV1_OBU_TILE_LIST
980 err = cbs_av1_read_tile_list_obu(
ctx, &gbc,
981 &obu->
obu.tile_list);
985 err = cbs_av1_ref_tile_data(
ctx, unit, &gbc,
986 &obu->
obu.tile_list.tile_data.data_ref,
987 &obu->
obu.tile_list.tile_data.data,
988 &obu->
obu.tile_list.tile_data.data_size);
994#if CBS_AV1_OBU_METADATA
997 err = cbs_av1_read_metadata_obu(
ctx, &gbc, &obu->
obu.metadata);
1003#if CBS_AV1_OBU_PADDING
1006 err = cbs_av1_read_padding_obu(
ctx, &gbc, &obu->
obu.padding);
1023 int nb_bits = obu->
obu_size * 8 + start_pos - end_pos;
1028 err = cbs_av1_read_trailing_bits(
ctx, &gbc, nb_bits);
1049 int err, start_pos, end_pos, data_pos;
1070 err = cbs_av1_write_obu_header(
ctx, pbc, &obu->
header);
1092 err = cbs_av1_write_sequence_header_obu(
ctx, pbc,
1110 err = cbs_av1_write_temporal_delimiter_obu(
ctx, pbc);
1118 err = cbs_av1_write_frame_header_obu(
ctx, pbc,
1136 err = cbs_av1_write_tile_group_obu(
ctx, pbc, tile_group);
1143#if CBS_AV1_OBU_TILE_LIST
1146 err = cbs_av1_write_tile_list_obu(
ctx, pbc, &obu->
obu.tile_list);
1150 td = &obu->
obu.tile_list.tile_data;
1154#if CBS_AV1_OBU_METADATA
1157 err = cbs_av1_write_metadata_obu(
ctx, pbc, &obu->
obu.metadata);
1163#if CBS_AV1_OBU_PADDING
1166 err = cbs_av1_write_padding_obu(
ctx, pbc, &obu->
obu.padding);
1178 header_size = (end_pos - start_pos + 7) / 8;
1181 }
else if (header_size > 0) {
1183 err = cbs_av1_write_trailing_bits(
ctx, pbc, 8 - end_pos % 8);
1187 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1201 err = cbs_av1_write_leb128(
ctx, pbc,
"obu_size", obu->
obu_size,
1220 memmove(pbc->
buf + data_pos,
1221 pbc->
buf + start_pos, header_size);
1229 memcpy(pbc->
buf + data_pos + header_size,
1289 memset(priv->
ref, 0,
sizeof(priv->
ref));
1303#if CBS_AV1_OBU_METADATA
1310 md = &obu->
obu.metadata;
1312 switch (
md->metadata_type) {
1355#if CBS_AV1_OBU_TILE_LIST
1357 obu.tile_list.tile_data.data),
1359#if CBS_AV1_OBU_PADDING
1361 obu.padding.payload),
1364#if CBS_AV1_OBU_METADATA
1366 &cbs_av1_free_metadata),
1372#define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1374 {
"operating_point",
"Set operating point to select layers to parse from a scalable bitstream",
1376 {
"fixed_obu_size_length",
"Set fixed length of the obu_size field",
1382 .class_name =
"cbs_av1",
static AVFormatContext * ctx
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.
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
static const AVOption cbs_av1_options[]
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
#define MAX_FRAGMENT_OBUS
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)
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)