29 const char *
name, uint32_t *write_to,
30 uint32_t range_min, uint32_t range_max)
33 int position, zeroes, i, j;
43 "%s: bitstream ended.\n", name);
61 for (j = 0; j < zeroes; j++)
62 bits[i++] = (value >> (zeroes - j - 1) & 1) ?
'1' :
'0';
64 value += (1 << zeroes) - 1;
75 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
76 name, value, range_min, range_max);
86 uint32_t range_min, uint32_t range_max)
93 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
94 name, value, range_min, range_max);
106 v = value - (1 << zeroes) + 1;
115 for (j = 0; j < zeroes; j++)
118 for (j = 0; j < zeroes; j++)
119 bits[i++] = (v >> (zeroes - j - 1) & 1) ?
'1' :
'0';
129 const char *
name, uint64_t *write_to)
132 int position, err, i;
138 for (i = 0; i < 8; i++) {
139 int subscript[2] = { 1, i };
146 value |= (uint64_t)(byte & 0x7f) << (i * 7);
161 int position, err,
len, i;
164 len = (
av_log2(value) + 7) / 7;
169 for (i = 0; i <
len; i++) {
170 int subscript[2] = { 1, i };
172 byte = value >> (7 * i) & 0x7f;
190 const int *subscripts,
int32_t *write_to)
201 "%s: bitstream ended.\n", name);
207 value = sign ? -(
int32_t)magnitude : magnitude;
212 for (i = 0; i <
width; i++)
213 bits[i] = magnitude >> (width - i - 1) & 1 ?
'1' :
'0';
214 bits[i] = sign ?
'1' :
'0';
218 name, subscripts, bits, value);
236 magnitude = sign ? -value :
value;
241 for (i = 0; i <
width; i++)
242 bits[i] = magnitude >> (width - i - 1) & 1 ?
'1' :
'0';
243 bits[i] = sign ?
'1' :
'0';
247 name, subscripts, bits, value);
257 uint32_t
n,
const char *
name,
258 const int *subscripts, uint32_t *write_to)
260 uint32_t
w, m, v, extra_bit,
value;
273 "%s: bitstream ended.\n", name);
286 value = (v << 1) - m + extra_bit;
292 for (i = 0; i < w - 1; i++)
293 bits[i] = (v >> i & 1) ?
'1' :
'0';
295 bits[i++] = extra_bit ?
'1' :
'0';
299 name, subscripts, bits, value);
307 uint32_t
n,
const char *
name,
308 const int *subscripts, uint32_t
value)
310 uint32_t
w, m, v, extra_bit;
315 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
333 v = m + ((value - m) >> 1);
334 extra_bit = (value - m) & 1;
342 for (i = 0; i < w - 1; i++)
343 bits[i] = (v >> i & 1) ?
'1' :
'0';
345 bits[i++] = extra_bit ?
'1' :
'0';
349 name, subscripts, bits, value);
356 uint32_t range_min, uint32_t range_max,
357 const char *
name, uint32_t *write_to)
363 av_assert0(range_min <= range_max && range_max - range_min <
sizeof(bits) - 1);
367 for (i = 0, value = range_min; value < range_max;) {
370 "%s: bitstream ended.\n", name);
385 name,
NULL, bits, value);
393 uint32_t range_min, uint32_t range_max,
398 av_assert0(range_min <= range_max && range_max - range_min < 32);
401 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
402 name, value, range_min, range_max);
406 if (value == range_max)
407 len = range_max - range_min;
409 len = value - range_min + 1;
416 for (i = 0; i <
len; i++) {
417 if (range_min + i == value)
424 name,
NULL, bits, value);
428 put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
434 uint32_t range_max,
const char *
name,
435 const int *subscripts, uint32_t *write_to)
439 uint32_t max_len,
len, range_offset, range_bits;
445 max_len =
av_log2(range_max - 1) - 3;
448 "subexp_more_bits", &len);
453 range_bits = 2 +
len;
454 range_offset = 1 << range_bits;
462 "subexp_bits",
NULL, &value,
469 "subexp_final_bits",
NULL, &value);
473 value += range_offset;
477 name, subscripts,
"", value);
484 uint32_t range_max,
const char *
name,
485 const int *subscripts, uint32_t
value)
488 uint32_t max_len,
len, range_offset, range_bits;
490 if (value > range_max) {
492 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
493 name, value, range_max);
501 max_len =
av_log2(range_max - 1) - 3;
509 len = range_bits - 2;
516 range_offset = 1 << range_bits;
520 "subexp_more_bits", len);
527 value - range_offset,
534 "subexp_final_bits",
NULL,
535 value - range_offset);
542 name, subscripts,
"", value);
551 for (k = 0; (blksize << k) < target; k++);
556 unsigned int a,
unsigned int b)
558 unsigned int diff, m;
563 diff = (diff & (m - 1)) - (diff & m);
568 #define HEADER(name) do { \
569 ff_cbs_trace_header(ctx, name); \
572 #define CHECK(call) do { \
578 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
579 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
580 #define FUNC(name) FUNC_AV1(READWRITE, name)
582 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
584 #define fb(width, name) \
585 xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
586 #define fc(width, name, range_min, range_max) \
587 xf(width, name, current->name, range_min, range_max, 0)
588 #define flag(name) fb(1, name)
589 #define su(width, name) \
590 xsu(width, name, current->name, 0)
592 #define fbs(width, name, subs, ...) \
593 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
594 #define fcs(width, name, range_min, range_max, subs, ...) \
595 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
596 #define flags(name, subs, ...) \
597 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
598 #define sus(width, name, subs, ...) \
599 xsu(width, name, current->name, subs, __VA_ARGS__)
601 #define fixed(width, name, value) do { \
602 av_unused uint32_t fixed_value = value; \
603 xf(width, name, fixed_value, value, value, 0); \
608 #define READWRITE read
609 #define RWContext GetBitContext
611 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
612 uint32_t value = range_min; \
613 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
614 SUBSCRIPTS(subs, __VA_ARGS__), \
615 &value, range_min, range_max)); \
619 #define xsu(width, name, var, subs, ...) do { \
621 CHECK(cbs_av1_read_su(ctx, rw, width, #name, \
622 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
626 #define uvlc(name, range_min, range_max) do { \
627 uint32_t value = range_min; \
628 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
629 &value, range_min, range_max)); \
630 current->name = value; \
633 #define ns(max_value, name, subs, ...) do { \
634 uint32_t value = 0; \
635 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
636 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
637 current->name = value; \
640 #define increment(name, min, max) do { \
641 uint32_t value = 0; \
642 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
643 current->name = value; \
646 #define subexp(name, max, subs, ...) do { \
647 uint32_t value = 0; \
648 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
649 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
650 current->name = value; \
653 #define delta_q(name) do { \
654 uint8_t delta_coded; \
656 xf(1, name.delta_coded, delta_coded, 0, 1, 0); \
658 xsu(1 + 6, name.delta_q, delta_q, 0); \
661 current->name = delta_q; \
664 #define leb128(name) do { \
665 uint64_t value = 0; \
666 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
667 current->name = value; \
670 #define infer(name, value) do { \
671 current->name = value; \
674 #define byte_alignment(rw) (get_bits_count(rw) % 8)
691 #undef byte_alignment
695 #define READWRITE write
696 #define RWContext PutBitContext
698 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
699 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
700 SUBSCRIPTS(subs, __VA_ARGS__), \
701 var, range_min, range_max)); \
704 #define xsu(width, name, var, subs, ...) do { \
705 CHECK(cbs_av1_write_su(ctx, rw, width, #name, \
706 SUBSCRIPTS(subs, __VA_ARGS__), var)); \
709 #define uvlc(name, range_min, range_max) do { \
710 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
711 range_min, range_max)); \
714 #define ns(max_value, name, subs, ...) do { \
715 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
716 SUBSCRIPTS(subs, __VA_ARGS__), \
720 #define increment(name, min, max) do { \
721 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
725 #define subexp(name, max, subs, ...) do { \
726 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
727 SUBSCRIPTS(subs, __VA_ARGS__), \
731 #define delta_q(name) do { \
732 xf(1, name.delta_coded, current->name != 0, 0, 1, 0); \
734 xsu(1 + 6, name.delta_q, current->name, 0); \
737 #define leb128(name) do { \
738 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \
741 #define infer(name, value) do { \
742 if (current->name != (value)) { \
743 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
744 "%s does not match inferred value: " \
745 "%"PRId64", but should be %"PRId64".\n", \
746 #name, (int64_t)current->name, (int64_t)(value)); \
750 #define byte_alignment(rw) (put_bits_count(rw) % 8)
766 #undef byte_alignment
786 if (INT_MAX / 8 < size) {
788 "too large (%zu bytes).\n", size);
799 err = cbs_av1_read_obu_header(ctx, &gbc, &header);
805 "stream: size field must be present.\n");
812 "too short (%zu bytes).\n", size);
824 obu_length = pos / 8 + obu_size;
826 if (size < obu_length) {
828 "%"PRIu64
", but only %zu bytes remaining in fragment.\n",
895 "any data in tile group (%d bits read).\n", pos);
917 int err, start_pos, end_pos;
929 err = cbs_av1_read_obu_header(ctx, &gbc, &obu->
header);
943 "unit too short (%zu).\n", unit->
data_size);
958 int in_temporal_layer =
960 int in_spatial_layer =
962 if (!in_temporal_layer || !in_spatial_layer) {
974 err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
990 err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
998 err = cbs_av1_read_frame_header_obu(ctx, &gbc,
1009 err = cbs_av1_read_tile_group_obu(ctx, &gbc,
1022 err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->
obu.
frame,
1035 err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1048 err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->
obu.
metadata);
1064 err = cbs_av1_read_trailing_bits(ctx, &gbc,
1065 obu->
obu_size * 8 + start_pos - end_pos);
1082 int err, start_pos, end_pos, data_pos;
1089 err = cbs_av1_write_obu_header(ctx, pbc, &obu->
header);
1106 err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1122 err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1130 err = cbs_av1_write_frame_header_obu(ctx, pbc,
1141 err = cbs_av1_write_tile_group_obu(ctx, pbc,
1151 err = cbs_av1_write_frame_obu(ctx, pbc, &obu->
obu.
frame,
NULL);
1160 err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->
obu.
tile_list);
1169 err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->
obu.
metadata);
1180 header_size = (end_pos - start_pos + 7) / 8;
1183 }
else if (header_size > 0) {
1185 err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1189 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1240 reallocate_and_try_again:
1244 "sufficiently large write buffer (last attempt "
1256 goto reallocate_and_try_again;
1286 for (i = 0; i < frag->
nb_units; i++)
1296 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.
static void cbs_av1_free_obu(void *unit, uint8_t *content)
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
ptrdiff_t const GLvoid * data
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)
static int cbs_av1_write_su(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, int32_t value)
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)
#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)
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)
void(* close)(CodedBitstreamContext *ctx)
static int get_bits_count(const GetBitContext *s)
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
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.
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
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)
static int get_bits_left(GetBitContext *gb)
static void cbs_av1_free_tile_data(AV1RawTileData *td)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int put_bits_left(PutBitContext *s)
AVBufferRef * frame_header_ref
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
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().
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
static int cbs_av1_read_su(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, int32_t *write_to)
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
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
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)
GLsizei GLboolean const GLfloat * value
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.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
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
Coded bitstream fragment structure, combining one or more units.
uint8_t * data
The data buffer.
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
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int cbs_av1_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
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.
AVBufferRef * data_ref
A reference to the buffer containing data.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
#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).
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)