30 const char *
name, uint32_t *write_to,
31 uint32_t range_min, uint32_t range_max)
33 uint32_t zeroes, bits_value,
value;
36 if (
ctx->trace_enable)
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;
76 if (
ctx->trace_enable) {
82 k =
FFMIN(zeroes - 32, 32);
83 for (
i = 0;
i < k;
i++)
93 for (
i = 0;
i < zeroes;
i++)
98 for (j = 0; j < zeroes; j++)
99 bits[
i++] = (bits_value >> (zeroes - j - 1) & 1) ?
'1' :
'0';
107 if (value < range_min || value > range_max) {
109 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
120 uint32_t range_min, uint32_t range_max)
123 int position, zeroes;
125 if (value < range_min || value > range_max) {
127 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
132 if (
ctx->trace_enable)
136 v =
value - (1U << zeroes) + 1;
141 if (
ctx->trace_enable) {
145 for (j = 0; j < zeroes; j++)
148 for (j = 0; j < zeroes; j++)
149 bits[
i++] = (v >> (zeroes - j - 1) & 1) ?
'1' :
'0';
159 const char *
name, uint64_t *write_to)
162 int position, err,
i;
164 if (
ctx->trace_enable)
168 for (
i = 0;
i < 8;
i++) {
169 int subscript[2] = { 1,
i };
176 value |= (uint64_t)(
byte & 0x7f) << (
i * 7);
181 if (
value > UINT32_MAX)
184 if (
ctx->trace_enable)
194 int position, err,
len,
i;
199 if (
ctx->trace_enable)
202 for (
i = 0;
i <
len;
i++) {
203 int subscript[2] = { 1,
i };
205 byte =
value >> (7 *
i) & 0x7f;
215 if (
ctx->trace_enable)
222 uint32_t n,
const char *
name,
223 const int *subscripts, uint32_t *write_to)
225 uint32_t m, v, extra_bit,
value;
230 if (
ctx->trace_enable)
238 "%s: bitstream ended.\n",
name);
251 value = (v << 1) - m + extra_bit;
254 if (
ctx->trace_enable) {
257 for (
i = 0;
i <
w - 1;
i++)
258 bits[
i] = (v >>
i & 1) ?
'1' :
'0';
260 bits[
i++] = extra_bit ?
'1' :
'0';
272 uint32_t n,
const char *
name,
273 const int *subscripts, uint32_t
value)
275 uint32_t
w, m, v, extra_bit;
280 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
285 if (
ctx->trace_enable)
298 v = m + ((
value - m) >> 1);
299 extra_bit = (
value - m) & 1;
304 if (
ctx->trace_enable) {
307 for (
i = 0;
i <
w - 1;
i++)
308 bits[
i] = (v >>
i & 1) ?
'1' :
'0';
310 bits[
i++] = extra_bit ?
'1' :
'0';
321 uint32_t range_min, uint32_t range_max,
322 const char *
name, uint32_t *write_to)
328 av_assert0(range_min <= range_max && range_max - range_min <
sizeof(
bits) - 1);
329 if (
ctx->trace_enable)
332 for (
i = 0,
value = range_min;
value < range_max;) {
335 "%s: bitstream ended.\n",
name);
347 if (
ctx->trace_enable) {
358 uint32_t range_min, uint32_t range_max,
363 av_assert0(range_min <= range_max && range_max - range_min < 32);
364 if (value < range_min || value > range_max) {
366 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
371 if (
value == range_max)
372 len = range_max - range_min;
378 if (
ctx->trace_enable) {
381 for (
i = 0;
i <
len;
i++) {
382 if (range_min +
i ==
value)
399 uint32_t range_max,
const char *
name,
400 const int *subscripts, uint32_t *write_to)
404 uint32_t max_len,
len, range_offset, range_bits;
406 if (
ctx->trace_enable)
410 max_len =
av_log2(range_max - 1) - 3;
413 "subexp_more_bits", &
len);
418 range_bits = 2 +
len;
419 range_offset = 1 << range_bits;
438 value += range_offset;
440 if (
ctx->trace_enable)
449 uint32_t range_max,
const char *
name,
450 const int *subscripts, uint32_t
value)
453 uint32_t max_len,
len, range_offset, range_bits;
455 if (
value > range_max) {
457 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
462 if (
ctx->trace_enable)
466 max_len =
av_log2(range_max - 1) - 3;
474 len = range_bits - 2;
481 range_offset = 1 << range_bits;
485 "subexp_more_bits",
len);
492 value - range_offset,
499 "subexp_final_bits",
NULL,
500 value - range_offset);
505 if (
ctx->trace_enable)
516 for (k = 0; (blksize << k) < target; k++);
521 unsigned int a,
unsigned int b)
523 unsigned int diff, m;
544 #define HEADER(name) do { \
545 ff_cbs_trace_header(ctx, name); \
548 #define CHECK(call) do { \
554 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
555 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
556 #define FUNC(name) FUNC_AV1(READWRITE, name)
558 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
560 #define fb(width, name) \
561 xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
562 #define fc(width, name, range_min, range_max) \
563 xf(width, name, current->name, range_min, range_max, 0, )
564 #define flag(name) fb(1, name)
565 #define su(width, name) \
566 xsu(width, name, current->name, 0, )
568 #define fbs(width, name, subs, ...) \
569 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
570 #define fcs(width, name, range_min, range_max, subs, ...) \
571 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
572 #define flags(name, subs, ...) \
573 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
574 #define sus(width, name, subs, ...) \
575 xsu(width, name, current->name, subs, __VA_ARGS__)
577 #define fixed(width, name, value) do { \
578 av_unused uint32_t fixed_value = value; \
579 xf(width, name, fixed_value, value, value, 0, ); \
584 #define READWRITE read
585 #define RWContext GetBitContext
587 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
589 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
590 SUBSCRIPTS(subs, __VA_ARGS__), \
591 &value, range_min, range_max)); \
595 #define xsu(width, name, var, subs, ...) do { \
597 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
598 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
599 MIN_INT_BITS(width), \
600 MAX_INT_BITS(width))); \
604 #define uvlc(name, range_min, range_max) do { \
606 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
607 &value, range_min, range_max)); \
608 current->name = value; \
611 #define ns(max_value, name, subs, ...) do { \
613 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
614 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
615 current->name = value; \
618 #define increment(name, min, max) do { \
620 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
621 current->name = value; \
624 #define subexp(name, max, subs, ...) do { \
626 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
627 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
628 current->name = value; \
631 #define delta_q(name) do { \
632 uint8_t delta_coded; \
634 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
636 xsu(1 + 6, name.delta_q, delta_q, 0, ); \
639 current->name = delta_q; \
642 #define leb128(name) do { \
644 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
645 current->name = value; \
648 #define infer(name, value) do { \
649 current->name = value; \
652 #define byte_alignment(rw) (get_bits_count(rw) % 8)
668 #undef byte_alignment
672 #define READWRITE write
673 #define RWContext PutBitContext
675 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
676 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
677 SUBSCRIPTS(subs, __VA_ARGS__), \
678 var, range_min, range_max)); \
681 #define xsu(width, name, var, subs, ...) do { \
682 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
683 SUBSCRIPTS(subs, __VA_ARGS__), var, \
684 MIN_INT_BITS(width), \
685 MAX_INT_BITS(width))); \
688 #define uvlc(name, range_min, range_max) do { \
689 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
690 range_min, range_max)); \
693 #define ns(max_value, name, subs, ...) do { \
694 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
695 SUBSCRIPTS(subs, __VA_ARGS__), \
699 #define increment(name, min, max) do { \
700 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
704 #define subexp(name, max, subs, ...) do { \
705 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
706 SUBSCRIPTS(subs, __VA_ARGS__), \
710 #define delta_q(name) do { \
711 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
713 xsu(1 + 6, name.delta_q, current->name, 0, ); \
716 #define leb128(name) do { \
717 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \
720 #define infer(name, value) do { \
721 if (current->name != (value)) { \
722 av_log(ctx->log_ctx, AV_LOG_ERROR, \
723 "%s does not match inferred value: " \
724 "%"PRId64", but should be %"PRId64".\n", \
725 #name, (int64_t)current->name, (int64_t)(value)); \
726 return AVERROR_INVALIDDATA; \
730 #define byte_alignment(rw) (put_bits_count(rw) % 8)
746 #undef byte_alignment
760 trace =
ctx->trace_enable;
761 ctx->trace_enable = 0;
766 if (INT_MAX / 8 <
size) {
776 int config_record_version =
data[0] & 0x7f;
778 if (config_record_version != 1) {
780 "Unknown version %d of AV1CodecConfigurationRecord "
782 config_record_version);
790 "Undersized AV1CodecConfigurationRecord v%d found!\n",
791 config_record_version);
812 err = cbs_av1_read_obu_header(
ctx, &gbc, &
header);
816 if (
header.obu_has_size_field) {
827 obu_size =
size - 1 -
header.obu_extension_flag;
832 obu_length =
pos / 8 + obu_size;
834 if (
size < obu_length) {
836 "%"PRIu64
", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
854 ctx->trace_enable = trace;
868 "any data in tile group (%d bits read).\n",
pos);
890 int err, start_pos, end_pos;
901 err = cbs_av1_read_obu_header(
ctx, &gbc, &obu->
header);
927 int in_temporal_layer =
929 int in_spatial_layer =
931 if (!in_temporal_layer || !in_spatial_layer) {
940 err = cbs_av1_read_sequence_header_obu(
ctx, &gbc,
950 "Must not be higher than %u.\n",
968 err = cbs_av1_read_temporal_delimiter_obu(
ctx, &gbc);
976 err = cbs_av1_read_frame_header_obu(
ctx, &gbc,
987 err = cbs_av1_read_tile_group_obu(
ctx, &gbc,
1000 err = cbs_av1_read_frame_obu(
ctx, &gbc, &obu->
obu.
frame,
1013 err = cbs_av1_read_tile_list_obu(
ctx, &gbc,
1033 err = cbs_av1_read_padding_obu(
ctx, &gbc, &obu->
obu.
padding);
1049 int nb_bits = obu->
obu_size * 8 + start_pos - end_pos;
1054 err = cbs_av1_read_trailing_bits(
ctx, &gbc, nb_bits);
1071 int err, start_pos, end_pos, data_pos;
1078 err = cbs_av1_write_obu_header(
ctx, pbc, &obu->
header);
1095 err = cbs_av1_write_sequence_header_obu(
ctx, pbc,
1115 err = cbs_av1_write_temporal_delimiter_obu(
ctx, pbc);
1123 err = cbs_av1_write_frame_header_obu(
ctx, pbc,
1134 err = cbs_av1_write_tile_group_obu(
ctx, pbc,
1169 err = cbs_av1_write_padding_obu(
ctx, pbc, &obu->
obu.
padding);
1179 header_size = (end_pos - start_pos + 7) / 8;
1182 }
else if (header_size > 0) {
1184 err = cbs_av1_write_trailing_bits(
ctx, pbc, 8 - end_pos % 8);
1188 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1214 memmove(pbc->
buf + data_pos,
1215 pbc->
buf + start_pos, header_size);
1219 memcpy(pbc->
buf + data_pos + header_size,
1220 td->data,
td->data_size);
1267 memset(priv->
ref, 0,
sizeof(priv->
ref));
1289 switch (
md->metadata_type) {
1318 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1320 {
"operating_point",
"Set operating point to select layers to parse from a scalable bitstream",
static void flush(AVCodecContext *avctx)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_cbs_insert_unit_data(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Insert a new unit into a fragment with the given data bitstream.
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, uint32_t value, uint32_t range_min, uint32_t range_max)
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *str, const int *subscripts, const char *bits, int64_t value)
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
static const AVOption cbs_av1_options[]
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t n, const char *name, const int *subscripts, uint32_t *write_to)
static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint64_t *write_to)
static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t n, 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)
static int cbs_av1_write_obu(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
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_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
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)
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
static void cbs_av1_free_metadata(void *unit, uint8_t *content)
static void cbs_av1_close(CodedBitstreamContext *ctx)
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 const AVClass cbs_av1_class
static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
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 int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, GetBitContext *gbc, AV1RawTileData *td)
static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[]
static void cbs_av1_flush(CodedBitstreamContext *ctx)
static int cbs_av1_tile_log2(int blksize, int target)
const CodedBitstreamType ff_cbs_type_av1
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)
static int cbs_av1_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
#define CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
#define CBS_UNIT_TYPE_POD(type, structure)
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
#define MAX_UINT_BITS(length)
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
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.
#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_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
#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_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_REDUNDANT_FRAME_HEADER
@ AV1_OBU_SEQUENCE_HEADER
@ AV1_METADATA_TYPE_ITUT_T35
@ AV1_MAX_OPERATING_POINTS
common internal API header
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
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 const uint8_t header[24]
AV1RawTileGroup tile_group
AV1RawFrameHeader frame_header
AV1RawTileGroup tile_group
AV1RawSequenceHeader sequence_header
uint8_t * data
The data buffer.
Describe the class of an AVClass context structure.
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 * priv_data
Format private data.
AVBufferRef * sequence_header_ref
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.
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing 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 av_always_inline int diff(const uint32_t a, const uint32_t b)