30 const char *
name, uint32_t *write_to,
31 uint32_t range_min, uint32_t range_max)
33 uint32_t zeroes, bits_value,
value;
43 "%s: bitstream ended.\n", name);
57 "%s: bitstream ended.\n", name);
62 value = bits_value + (UINT32_C(1) << zeroes) - 1;
71 k =
FFMIN(zeroes - 32, 32);
72 for (i = 0; i < k; i++)
82 for (i = 0; i < zeroes; i++)
87 for (j = 0; j < zeroes; j++)
88 bits[i++] = (bits_value >> (zeroes - j - 1) & 1) ?
'1' :
'0';
98 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
99 name, value, range_min, range_max);
109 uint32_t range_min, uint32_t range_max)
112 int position, zeroes;
116 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
117 name, value, range_min, range_max);
125 v = value - (1
U << zeroes) + 1;
134 for (j = 0; j < zeroes; j++)
137 for (j = 0; j < zeroes; j++)
138 bits[i++] = (v >> (zeroes - j - 1) & 1) ?
'1' :
'0';
148 const char *
name, uint64_t *write_to)
151 int position, err,
i;
157 for (i = 0; i < 8; i++) {
158 int subscript[2] = { 1, i };
165 value |= (uint64_t)(byte & 0x7f) << (i * 7);
170 if (value > UINT32_MAX)
183 int position, err,
len,
i;
186 len = (
av_log2(value) + 7) / 7;
191 for (i = 0; i <
len; i++) {
192 int subscript[2] = { 1, i };
194 byte = value >> (7 *
i) & 0x7f;
211 uint32_t n,
const char *
name,
212 const int *subscripts, uint32_t *write_to)
214 uint32_t m, v, extra_bit,
value;
227 "%s: bitstream ended.\n", name);
240 value = (v << 1) - m + extra_bit;
246 for (i = 0; i < w - 1; i++)
247 bits[i] = (v >> i & 1) ?
'1' :
'0';
249 bits[i++] = extra_bit ?
'1' :
'0';
253 name, subscripts, bits, value);
261 uint32_t n,
const char *
name,
262 const int *subscripts, uint32_t
value)
264 uint32_t
w, m, v, extra_bit;
269 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
287 v = m + ((value - m) >> 1);
288 extra_bit = (value - m) & 1;
296 for (i = 0; i < w - 1; i++)
297 bits[i] = (v >> i & 1) ?
'1' :
'0';
299 bits[i++] = extra_bit ?
'1' :
'0';
303 name, subscripts, bits, value);
310 uint32_t range_min, uint32_t range_max,
311 const char *
name, uint32_t *write_to)
317 av_assert0(range_min <= range_max && range_max - range_min <
sizeof(bits) - 1);
321 for (i = 0, value = range_min; value < range_max;) {
324 "%s: bitstream ended.\n", name);
339 name,
NULL, bits, value);
347 uint32_t range_min, uint32_t range_max,
352 av_assert0(range_min <= range_max && range_max - range_min < 32);
355 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
356 name, value, range_min, range_max);
360 if (value == range_max)
361 len = range_max - range_min;
363 len = value - range_min + 1;
370 for (i = 0; i <
len; i++) {
371 if (range_min + i == value)
378 name,
NULL, bits, value);
382 put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
388 uint32_t range_max,
const char *
name,
389 const int *subscripts, uint32_t *write_to)
393 uint32_t max_len,
len, range_offset, range_bits;
399 max_len =
av_log2(range_max - 1) - 3;
402 "subexp_more_bits", &len);
407 range_bits = 2 +
len;
408 range_offset = 1 << range_bits;
416 "subexp_bits",
NULL, &value,
423 "subexp_final_bits",
NULL, &value);
427 value += range_offset;
431 name, subscripts,
"", value);
438 uint32_t range_max,
const char *
name,
439 const int *subscripts, uint32_t
value)
442 uint32_t max_len,
len, range_offset, range_bits;
444 if (value > range_max) {
446 "%"PRIu32
", but must be in [0,%"PRIu32
"].\n",
447 name, value, range_max);
455 max_len =
av_log2(range_max - 1) - 3;
463 len = range_bits - 2;
470 range_offset = 1 << range_bits;
474 "subexp_more_bits", len);
481 value - range_offset,
488 "subexp_final_bits",
NULL,
489 value - range_offset);
496 name, subscripts,
"", value);
505 for (k = 0; (blksize << k) < target; k++);
510 unsigned int a,
unsigned int b)
512 unsigned int diff, m;
517 diff = (diff & (m - 1)) - (diff & m);
533 #define HEADER(name) do { \ 534 ff_cbs_trace_header(ctx, name); \ 537 #define CHECK(call) do { \ 543 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name 544 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name) 545 #define FUNC(name) FUNC_AV1(READWRITE, name) 547 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL) 549 #define fb(width, name) \ 550 xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0, ) 551 #define fc(width, name, range_min, range_max) \ 552 xf(width, name, current->name, range_min, range_max, 0, ) 553 #define flag(name) fb(1, name) 554 #define su(width, name) \ 555 xsu(width, name, current->name, 0, ) 557 #define fbs(width, name, subs, ...) \ 558 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__) 559 #define fcs(width, name, range_min, range_max, subs, ...) \ 560 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__) 561 #define flags(name, subs, ...) \ 562 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__) 563 #define sus(width, name, subs, ...) \ 564 xsu(width, name, current->name, subs, __VA_ARGS__) 566 #define fixed(width, name, value) do { \ 567 av_unused uint32_t fixed_value = value; \ 568 xf(width, name, fixed_value, value, value, 0, ); \ 573 #define READWRITE read 574 #define RWContext GetBitContext 576 #define xf(width, name, var, range_min, range_max, subs, ...) do { \ 578 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \ 579 SUBSCRIPTS(subs, __VA_ARGS__), \ 580 &value, range_min, range_max)); \ 584 #define xsu(width, name, var, subs, ...) do { \ 586 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \ 587 SUBSCRIPTS(subs, __VA_ARGS__), &value, \ 588 MIN_INT_BITS(width), \ 589 MAX_INT_BITS(width))); \ 593 #define uvlc(name, range_min, range_max) do { \ 595 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \ 596 &value, range_min, range_max)); \ 597 current->name = value; \ 600 #define ns(max_value, name, subs, ...) do { \ 602 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \ 603 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \ 604 current->name = value; \ 607 #define increment(name, min, max) do { \ 609 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \ 610 current->name = value; \ 613 #define subexp(name, max, subs, ...) do { \ 615 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \ 616 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \ 617 current->name = value; \ 620 #define delta_q(name) do { \ 621 uint8_t delta_coded; \ 623 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \ 625 xsu(1 + 6, name.delta_q, delta_q, 0, ); \ 628 current->name = delta_q; \ 631 #define leb128(name) do { \ 633 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \ 634 current->name = value; \ 637 #define infer(name, value) do { \ 638 current->name = value; \ 641 #define byte_alignment(rw) (get_bits_count(rw) % 8) 657 #undef byte_alignment 661 #define READWRITE write 662 #define RWContext PutBitContext 664 #define xf(width, name, var, range_min, range_max, subs, ...) do { \ 665 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \ 666 SUBSCRIPTS(subs, __VA_ARGS__), \ 667 var, range_min, range_max)); \ 670 #define xsu(width, name, var, subs, ...) do { \ 671 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \ 672 SUBSCRIPTS(subs, __VA_ARGS__), var, \ 673 MIN_INT_BITS(width), \ 674 MAX_INT_BITS(width))); \ 677 #define uvlc(name, range_min, range_max) do { \ 678 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \ 679 range_min, range_max)); \ 682 #define ns(max_value, name, subs, ...) do { \ 683 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \ 684 SUBSCRIPTS(subs, __VA_ARGS__), \ 688 #define increment(name, min, max) do { \ 689 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \ 693 #define subexp(name, max, subs, ...) do { \ 694 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \ 695 SUBSCRIPTS(subs, __VA_ARGS__), \ 699 #define delta_q(name) do { \ 700 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \ 702 xsu(1 + 6, name.delta_q, current->name, 0, ); \ 705 #define leb128(name) do { \ 706 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \ 709 #define infer(name, value) do { \ 710 if (current->name != (value)) { \ 711 av_log(ctx->log_ctx, AV_LOG_ERROR, \ 712 "%s does not match inferred value: " \ 713 "%"PRId64", but should be %"PRId64".\n", \ 714 #name, (int64_t)current->name, (int64_t)(value)); \ 715 return AVERROR_INVALIDDATA; \ 719 #define byte_alignment(rw) (put_bits_count(rw) % 8) 735 #undef byte_alignment 755 if (INT_MAX / 8 < size) {
762 if (header && size && data[0] & 0x80) {
765 int config_record_version = data[0] & 0x7f;
767 if (config_record_version != 1) {
769 "Unknown version %d of AV1CodecConfigurationRecord " 771 config_record_version);
779 "Undersized AV1CodecConfigurationRecord v%d found!\n",
780 config_record_version);
801 err = cbs_av1_read_obu_header(ctx, &gbc, &header);
821 obu_length = pos / 8 + obu_size;
823 if (size < obu_length) {
825 "%"PRIu64
", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
857 "any data in tile group (%d bits read).\n", pos);
879 int err, start_pos, end_pos;
890 err = cbs_av1_read_obu_header(ctx, &gbc, &obu->
header);
916 int in_temporal_layer =
918 int in_spatial_layer =
920 if (!in_temporal_layer || !in_spatial_layer) {
929 err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
939 "Must not be higher than %u.\n",
957 err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
965 err = cbs_av1_read_frame_header_obu(ctx, &gbc,
976 err = cbs_av1_read_tile_group_obu(ctx, &gbc,
989 err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->
obu.
frame,
1002 err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1015 err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->
obu.
metadata);
1022 err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->
obu.
padding);
1038 int nb_bits = obu->
obu_size * 8 + start_pos - end_pos;
1043 err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
1060 int err, start_pos, end_pos, data_pos;
1067 err = cbs_av1_write_obu_header(ctx, pbc, &obu->
header);
1084 err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1104 err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1112 err = cbs_av1_write_frame_header_obu(ctx, pbc,
1123 err = cbs_av1_write_tile_group_obu(ctx, pbc,
1133 err = cbs_av1_write_frame_obu(ctx, pbc, &obu->
obu.
frame,
NULL);
1142 err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->
obu.
tile_list);
1151 err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->
obu.
metadata);
1158 err = cbs_av1_write_padding_obu(ctx, pbc, &obu->
obu.
padding);
1168 header_size = (end_pos - start_pos + 7) / 8;
1171 }
else if (header_size > 0) {
1173 err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1177 obu->
obu_size = header_size = (end_pos - start_pos + 7) / 8;
1203 memmove(pbc->
buf + data_pos,
1204 pbc->
buf + start_pos, header_size);
1208 memcpy(pbc->
buf + data_pos + header_size,
1227 for (i = 0; i < frag->
nb_units; i++)
1237 for (i = 0; i < frag->
nb_units; i++) {
1256 memset(priv->
ref, 0,
sizeof(priv->
ref));
1307 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x) 1309 {
"operating_point",
"Set operating point to select layers to parse from a scalable bitstream",
int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
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.
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 flush(AVCodecContext *avctx)
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.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
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)
const char * av_default_item_name(void *ptr)
Return the context name.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
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)
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.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#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)
static void cbs_av1_free_metadata(void *unit, uint8_t *content)
#define MAX_UINT_BITS(length)
static const AVOption cbs_av1_options[]
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)
GLsizei GLboolean const GLfloat * value
const CodedBitstreamUnitTypeDescriptor * unit_types
static int get_bits_count(const GetBitContext *s)
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
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.
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)
#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
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
#define CBS_UNIT_TYPE_POD(type, structure)
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)
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.
#define CBS_UNIT_TYPE_END_OF_LIST
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
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 size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
static unsigned int get_bits1(GetBitContext *s)
Describe the class of an AVClass context structure.
Context structure for coded bitstream operations.
AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES]
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.
static void cbs_av1_flush(CodedBitstreamContext *ctx)
AV1RawTileGroup tile_group
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
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)
static const AVClass cbs_av1_class
static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[]
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)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
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).