29 const char *
name, uint32_t *write_to,
30 uint32_t range_min, uint32_t range_max)
32 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;
81 k =
FFMIN(zeroes - 32, 32);
82 for (i = 0; i < k; i++)
92 for (i = 0; i < zeroes; i++)
97 for (j = 0; j < zeroes; j++)
98 bits[i++] = (bits_value >> (zeroes - j - 1) & 1) ?
'1' :
'0';
106 if (value < range_min || value > range_max) {
108 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
109 name, value, range_min, range_max);
119 uint32_t range_min, uint32_t range_max)
122 int position, zeroes;
124 if (value < range_min || value > range_max) {
126 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
127 name, value, range_min, range_max);
139 v = value - (1
U << zeroes) + 1;
149 for (j = 0; j < zeroes; j++)
152 for (j = 0; j < zeroes; j++)
153 bits[i++] = (v >> (zeroes - j - 1) & 1) ?
'1' :
'0';
163 const char *
name, uint64_t *write_to)
166 int position, err,
i;
172 for (i = 0; i < 8; i++) {
173 int subscript[2] = { 1, i };
180 value |= (uint64_t)(byte & 0x7f) << (i * 7);
185 if (value > UINT32_MAX)
198 int position, err,
len,
i;
201 len = (
av_log2(value) + 7) / 7;
206 for (i = 0; i <
len; i++) {
207 int subscript[2] = { 1, i };
209 byte = value >> (7 *
i) & 0x7f;
226 uint32_t n,
const char *
name,
227 const int *subscripts, uint32_t *write_to)
229 uint32_t m, v, extra_bit,
value;
242 "%s: bitstream ended.\n", name);
255 value = (v << 1) - m + extra_bit;
261 for (i = 0; i < w - 1; i++)
262 bits[i] = (v >> i & 1) ?
'1' :
'0';
264 bits[i++] = extra_bit ?
'1' :
'0';
268 name, subscripts, bits, value);
276 uint32_t n,
const char *
name,
277 const int *subscripts, uint32_t
value)
279 uint32_t
w, m, v, extra_bit;
284 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
302 v = m + ((value - m) >> 1);
303 extra_bit = (value - m) & 1;
311 for (i = 0; i < w - 1; i++)
312 bits[i] = (v >> i & 1) ?
'1' :
'0';
314 bits[i++] = extra_bit ?
'1' :
'0';
318 name, subscripts, bits, value);
325 uint32_t range_min, uint32_t range_max,
326 const char *
name, uint32_t *write_to)
332 av_assert0(range_min <= range_max && range_max - range_min <
sizeof(bits) - 1);
336 for (i = 0, value = range_min; value < range_max;) {
339 "%s: bitstream ended.\n", name);
354 name,
NULL, bits, value);
362 uint32_t range_min, uint32_t range_max,
367 av_assert0(range_min <= range_max && range_max - range_min < 32);
368 if (value < range_min || value > range_max) {
370 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
371 name, value, range_min, range_max);
375 if (value == range_max)
376 len = range_max - range_min;
378 len = value - range_min + 1;
385 for (i = 0; i <
len; i++) {
386 if (range_min + i == value)
393 name,
NULL, bits, value);
397 put_bits(pbc, len, (1
U << len) - 1 - (value != range_max));
403 uint32_t range_max,
const char *
name,
404 const int *subscripts, uint32_t *write_to)
408 uint32_t max_len,
len, range_offset, range_bits;
414 max_len =
av_log2(range_max - 1) - 3;
417 "subexp_more_bits", &len);
422 range_bits = 2 +
len;
423 range_offset = 1 << range_bits;
431 "subexp_bits",
NULL, &value,
438 "subexp_final_bits",
NULL, &value);
442 value += range_offset;
446 name, subscripts,
"", value);
453 uint32_t range_max,
const char *
name,
454 const int *subscripts, uint32_t
value)
457 uint32_t max_len,
len, range_offset, range_bits;
459 if (value > range_max) {
461 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
462 name, value, range_max);
470 max_len =
av_log2(range_max - 1) - 3;
478 len = range_bits - 2;
485 range_offset = 1 << range_bits;
489 "subexp_more_bits", len);
496 value - range_offset,
503 "subexp_final_bits",
NULL,
504 value - range_offset);
511 name, subscripts,
"", value);
520 for (k = 0; (blksize << k) < target; k++);
525 unsigned int a,
unsigned int b)
527 unsigned int diff, m;
532 diff = (diff & (m - 1)) - (diff & m);
548 #define HEADER(name) do { \ 549 ff_cbs_trace_header(ctx, name); \ 552 #define CHECK(call) do { \ 558 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name 559 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name) 560 #define FUNC(name) FUNC_AV1(READWRITE, name) 562 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL) 564 #define fb(width, name) \ 565 xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0, ) 566 #define fc(width, name, range_min, range_max) \ 567 xf(width, name, current->name, range_min, range_max, 0, ) 568 #define flag(name) fb(1, name) 569 #define su(width, name) \ 570 xsu(width, name, current->name, 0, ) 572 #define fbs(width, name, subs, ...) \ 573 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__) 574 #define fcs(width, name, range_min, range_max, subs, ...) \ 575 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__) 576 #define flags(name, subs, ...) \ 577 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__) 578 #define sus(width, name, subs, ...) \ 579 xsu(width, name, current->name, subs, __VA_ARGS__) 581 #define fixed(width, name, value) do { \ 582 av_unused uint32_t fixed_value = value; \ 583 xf(width, name, fixed_value, value, value, 0, ); \ 588 #define READWRITE read 589 #define RWContext GetBitContext 591 #define xf(width, name, var, range_min, range_max, subs, ...) do { \ 593 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \ 594 SUBSCRIPTS(subs, __VA_ARGS__), \ 595 &value, range_min, range_max)); \ 599 #define xsu(width, name, var, subs, ...) do { \ 601 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \ 602 SUBSCRIPTS(subs, __VA_ARGS__), &value, \ 603 MIN_INT_BITS(width), \ 604 MAX_INT_BITS(width))); \ 608 #define uvlc(name, range_min, range_max) do { \ 610 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \ 611 &value, range_min, range_max)); \ 612 current->name = value; \ 615 #define ns(max_value, name, subs, ...) do { \ 617 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \ 618 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \ 619 current->name = value; \ 622 #define increment(name, min, max) do { \ 624 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \ 625 current->name = value; \ 628 #define subexp(name, max, subs, ...) do { \ 630 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \ 631 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \ 632 current->name = value; \ 635 #define delta_q(name) do { \ 636 uint8_t delta_coded; \ 638 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \ 640 xsu(1 + 6, name.delta_q, delta_q, 0, ); \ 643 current->name = delta_q; \ 646 #define leb128(name) do { \ 648 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \ 649 current->name = value; \ 652 #define infer(name, value) do { \ 653 current->name = value; \ 656 #define byte_alignment(rw) (get_bits_count(rw) % 8) 672 #undef byte_alignment 676 #define READWRITE write 677 #define RWContext PutBitContext 679 #define xf(width, name, var, range_min, range_max, subs, ...) do { \ 680 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \ 681 SUBSCRIPTS(subs, __VA_ARGS__), \ 682 var, range_min, range_max)); \ 685 #define xsu(width, name, var, subs, ...) do { \ 686 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \ 687 SUBSCRIPTS(subs, __VA_ARGS__), var, \ 688 MIN_INT_BITS(width), \ 689 MAX_INT_BITS(width))); \ 692 #define uvlc(name, range_min, range_max) do { \ 693 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \ 694 range_min, range_max)); \ 697 #define ns(max_value, name, subs, ...) do { \ 698 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \ 699 SUBSCRIPTS(subs, __VA_ARGS__), \ 703 #define increment(name, min, max) do { \ 704 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \ 708 #define subexp(name, max, subs, ...) do { \ 709 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \ 710 SUBSCRIPTS(subs, __VA_ARGS__), \ 714 #define delta_q(name) do { \ 715 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \ 717 xsu(1 + 6, name.delta_q, current->name, 0, ); \ 720 #define leb128(name) do { \ 721 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \ 724 #define infer(name, value) do { \ 725 if (current->name != (value)) { \ 726 av_log(ctx->log_ctx, AV_LOG_ERROR, \ 727 "%s does not match inferred value: " \ 728 "%"PRId64", but should be %"PRId64".\n", \ 729 #name, (int64_t)current->name, (int64_t)(value)); \ 730 return AVERROR_INVALIDDATA; \ 734 #define byte_alignment(rw) (put_bits_count(rw) % 8) 750 #undef byte_alignment 770 if (INT_MAX / 8 < size) {
783 err = cbs_av1_read_obu_header(ctx, &gbc, &header);
803 obu_length = pos / 8 + obu_size;
805 if (size < obu_length) {
807 "%"PRIu64
", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
882 "any data in tile group (%d bits read).\n", pos);
904 int err, start_pos, end_pos;
916 err = cbs_av1_read_obu_header(ctx, &gbc, &obu->
header);
945 int in_temporal_layer =
947 int in_spatial_layer =
949 if (!in_temporal_layer || !in_spatial_layer) {
963 err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
979 err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
987 err = cbs_av1_read_frame_header_obu(ctx, &gbc,
998 err = cbs_av1_read_tile_group_obu(ctx, &gbc,
1011 err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->
obu.
frame,
1024 err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1037 err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->
obu.
metadata);
1044 err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->
obu.
padding);
1060 int nb_bits = obu->
obu_size * 8 + start_pos - end_pos;
1065 err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
1082 int err, start_pos, end_pos, data_pos;
1089 err = cbs_av1_write_obu_header(ctx, pbc, &obu->
header);
1108 err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1124 err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1132 err = cbs_av1_write_frame_header_obu(ctx, pbc,
1143 err = cbs_av1_write_tile_group_obu(ctx, pbc,
1153 err = cbs_av1_write_frame_obu(ctx, pbc, &obu->
obu.
frame,
NULL);
1162 err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->
obu.
tile_list);
1171 err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->
obu.
metadata);
1178 err = cbs_av1_write_padding_obu(ctx, pbc, &obu->
obu.
padding);
1188 header_size = (end_pos - start_pos + 7) / 8;
1191 }
else if (header_size > 0) {
1193 err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1197 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1223 memmove(pbc->
buf + data_pos,
1224 pbc->
buf + start_pos, header_size);
1228 memcpy(pbc->
buf + data_pos + header_size,
1247 for (i = 0; i < frag->
nb_units; i++)
1257 for (i = 0; i < frag->
nb_units; i++) {
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
int nb_units
Number of units in this fragment.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
AV1ReferenceFrameState write_ref[AV1_NUM_REF_FRAMES]
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static void cbs_av1_free_metadata(AV1RawMetadata *md)
AVBufferRef * sequence_header_ref
CodedBitstreamUnitType type
Codec-specific type of this unit.
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 void cbs_av1_close(CodedBitstreamContext *ctx)
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
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)
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 void cbs_av1_free_obu(void *opaque, uint8_t *content)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int trace_enable
Enable trace output during read/write operations.
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(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
static int cbs_av1_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
#define MAX_UINT_BITS(length)
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)
static int get_bits_count(const GetBitContext *s)
Coded bitstream unit structure.
void * content
Pointer to the decomposed form of this unit.
static const uint8_t header[24]
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
static void cbs_av1_free_padding(AV1RawPadding *pd)
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, 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.
size_t data_size
The number of bytes in the bitstream.
static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t n, const char *name, const int *subscripts, uint32_t *write_to)
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static int get_bits_left(GetBitContext *gb)
static void cbs_av1_free_tile_data(AV1RawTileData *td)
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void(* close)(CodedBitstreamContext *ctx)
static int put_bits_left(PutBitContext *s)
AVBufferRef * frame_header_ref
static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t *write_to)
simple assert() macros that are a bit more flexible than ISO C assert().
static int cbs_av1_tile_log2(int blksize, int target)
static int put_bits_count(PutBitContext *s)
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
const CodedBitstreamType ff_cbs_type_av1
AVBufferRef * payload_ref
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
static int cbs_av1_write_obu(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint64_t value)
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
AV1ReferenceFrameState * ref
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, GetBitContext *gbc, AV1RawTileData *td)
uint8_t * data
Pointer to the bitstream form of this fragment.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
AV1ReferenceFrameState read_ref[AV1_NUM_REF_FRAMES]
Coded bitstream fragment structure, combining one or more units.
uint8_t * data
The data buffer.
static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
static unsigned int get_bits1(GetBitContext *s)
Context structure for coded bitstream operations.
static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t value)
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
AV1RawTileGroup tile_group
AV1RawSequenceHeader * sequence_header
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t n, const char *name, const int *subscripts, uint32_t value)
AV1RawSequenceHeader sequence_header
void * priv_data
Internal codec-specific data.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
AVBufferRef * data_ref
A reference to the buffer containing data.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static av_always_inline int diff(const uint32_t a, const uint32_t b)
AV1RawTileGroup tile_group
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)
AV1RawFrameHeader frame_header
static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint64_t *write_to)
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
AVBufferRef * data_ref
A reference to the buffer containing data.
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *str, const int *subscripts, const char *bits, int64_t value)
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).