96 ctx->log_ctx = log_ctx;
99 if (
type->priv_data_size) {
105 if (
type->priv_class) {
111 ctx->decompose_unit_types =
NULL;
113 ctx->trace_enable = 0;
122 if (
ctx->codec->flush)
133 if (
ctx->codec->close)
186 if (
ctx->decompose_unit_types) {
187 for (j = 0; j <
ctx->nb_decompose_unit_types; j++) {
188 if (
ctx->decompose_unit_types[j] == unit->
type)
191 if (j >=
ctx->nb_decompose_unit_types)
200 err =
ctx->codec->read_unit(
ctx, unit);
203 "Decomposition unimplemented for unit %d "
204 "(type %"PRIu32
").\n",
i, unit->
type);
205 }
else if (err ==
AVERROR(EAGAIN)) {
207 "Skipping decomposition of unit %d "
208 "(type %"PRIu32
").\n",
i, unit->
type);
211 }
else if (err < 0) {
213 "(type %"PRIu32
").\n",
i, unit->
type);
310 if (!
ctx->write_buffer) {
312 ctx->write_buffer_size = 1024 * 1024;
314 reallocate_and_try_again:
318 "sufficiently large write buffer (last attempt "
326 ret =
ctx->codec->write_unit(
ctx, unit, &pbc);
330 if (
ctx->write_buffer_size == INT_MAX / 8)
332 ctx->write_buffer_size =
FFMIN(2 *
ctx->write_buffer_size, INT_MAX / 8);
333 goto reallocate_and_try_again;
375 "(type %"PRIu32
").\n",
i, unit->
type);
384 err =
ctx->codec->assemble_fragment(
ctx, frag);
447 if (!
ctx->trace_enable)
454 const char *
str,
const int *subscripts,
458 size_t name_len, bits_len;
459 int pad, subs,
i, j, k, n;
461 if (!
ctx->trace_enable)
466 subs = subscripts ? subscripts[0] : 0;
468 for (
i = j = 0;
str[
i];) {
491 name_len = strlen(
name);
492 bits_len = strlen(
bits);
494 if (name_len + bits_len > 60)
499 av_log(
ctx->log_ctx,
ctx->trace_level,
"%-10d %s%*s = %"PRId64
"\n",
505 const int *subscripts, uint32_t *write_to,
506 uint32_t range_min, uint32_t range_max)
515 "%s: bitstream ended.\n",
name);
519 if (
ctx->trace_enable)
524 if (
ctx->trace_enable) {
535 if (value < range_min || value > range_max) {
537 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
548 const int *subscripts, uint32_t
value,
549 uint32_t range_min, uint32_t range_max)
553 if (value < range_min || value > range_max) {
555 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
563 if (
ctx->trace_enable) {
584 const int *subscripts,
int32_t *write_to,
594 "%s: bitstream ended.\n",
name);
598 if (
ctx->trace_enable)
603 if (
ctx->trace_enable) {
614 if (value < range_min || value > range_max) {
616 "%"PRId32
", but must be in [%"PRId32
",%"PRId32
"].\n",
632 if (value < range_min || value > range_max) {
634 "%"PRId32
", but must be in [%"PRId32
",%"PRId32
"].\n",
642 if (
ctx->trace_enable) {
707 if (position < frag->nb_units)
708 memmove(units + position + 1, units + position,
709 (frag->
nb_units - position) *
sizeof(*units));
718 memcpy(units, frag->
units, position *
sizeof(*units));
720 if (position < frag->nb_units)
721 memcpy(units + position + 1, frag->
units + position,
722 (frag->
nb_units - position) *
sizeof(*units));
725 memset(units + position, 0,
sizeof(*units));
727 if (units != frag->
units) {
749 av_assert0(position >= 0 && position <= frag->nb_units);
765 unit = &frag->
units[position];
785 av_assert0(position >= 0 && position <= frag->nb_units);
803 unit = &frag->
units[position];
815 av_assert0(0 <= position && position < frag->nb_units
816 &&
"Unit to be deleted not in fragment.");
823 memmove(frag->
units + position,
824 frag->
units + position + 1,
833 for (
i = 0;
i <
desc->nb_ref_offsets;
i++) {
834 void **ptr = (
void**)(
data +
desc->ref_offsets[
i]);
848 if (!
ctx->codec->unit_types)
853 if (
desc->nb_unit_types == 0)
856 if (unit->
type >=
desc->unit_type_range_start &&
857 unit->
type <=
desc->unit_type_range_end)
860 for (j = 0; j <
desc->nb_unit_types; j++) {
861 if (
desc->unit_types[j] == unit->
type)
886 desc->content_free ?
desc->content_free
913 for (
i = 0;
i <
desc->nb_ref_offsets;
i++) {
934 *src_ptr < (*src_buf)->data + (*src_buf)->size);
941 *copy_ptr = (*copy_buf)->data + (*src_ptr - (*src_buf)->data);
945 desc->content_free ?
desc->content_free :
956 for (--
i;
i >= 0;
i--)
980 switch (
desc->content_type) {
994 if (!
desc->content_clone)
996 err =
desc->content_clone(&
ref, unit);
1031 switch (
desc->content_type) {
1041 if (!
desc->content_clone)
1043 err =
desc->content_clone(&
ref, unit);
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
refcounted data buffer API
int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, int32_t value, int32_t range_min, int32_t range_max)
int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
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.
static int cbs_clone_unit_content(AVBufferRef **clone_ref, CodedBitstreamUnit *unit, const CodedBitstreamUnitTypeDescriptor *desc)
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
static int cbs_read_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, AVBufferRef *buf, const uint8_t *data, size_t size, int header)
static int cbs_read_fragment_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
static void cbs_default_free_unit_content(void *opaque, uint8_t *data)
int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecContext *avctx)
Read the extradata bitstream found in a codec context into a fragment, then split into units and deco...
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
void ff_cbs_delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, int32_t *write_to, int32_t range_min, int32_t range_max)
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
static int cbs_insert_unit(CodedBitstreamFragment *frag, int position)
void ff_cbs_flush(CodedBitstreamContext *ctx)
Reset all internal state in a context.
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)
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit writable so that internal fields can be modified.
int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Read a bitstream from a memory region into a fragment, then split into units and decompose.
static void cbs_unit_uninit(CodedBitstreamUnit *unit)
static int cbs_fill_fragment_data(CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *str, const int *subscripts, const char *bits, int64_t value)
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
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_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
static int cbs_write_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
void ff_cbs_trace_header(CodedBitstreamContext *ctx, const char *name)
int ff_cbs_alloc_unit_data(CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
static const CodedBitstreamUnitTypeDescriptor * cbs_find_unit_type_desc(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
static const CodedBitstreamType *const cbs_type_table[]
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
const CodedBitstreamType ff_cbs_type_av1
const CodedBitstreamType ff_cbs_type_h265
const CodedBitstreamType ff_cbs_type_h264
const CodedBitstreamType ff_cbs_type_jpeg
const CodedBitstreamType ff_cbs_type_mpeg2
@ CBS_CONTENT_TYPE_INTERNAL_REFS
@ CBS_CONTENT_TYPE_COMPLEX
const CodedBitstreamType ff_cbs_type_vp9
common internal and external API header
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int get_bits_left(GetBitContext *gb)
static int get_bits_count(const GetBitContext *s)
void av_opt_free(void *obj)
Free all allocated objects in obj.
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
AVCodecID
Identify the syntax and semantics of the bitstream.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
int av_buffer_is_writable(const AVBufferRef *buf)
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_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
int av_buffer_make_writable(AVBufferRef **pbuf)
Create a writable reference from a given buffer reference, avoiding data copy if possible.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
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 const uint8_t header[24]
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
uint8_t * data
The data buffer.
Describe the class of an AVClass context structure.
main external API structure.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
This struct describes the properties of an encoded stream.
int extradata_size
Size of the extradata content in bytes.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
void * priv_data
Format private data.
This structure stores compressed data.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
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.
int nb_units_allocated
Number of allocated units.
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
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.
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
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.
#define av_malloc_array(a, b)
static int ref[MAX_W *MAX_W]
static void copy(const float *p1, float *p2, const int length)