87 ctx->log_ctx = log_ctx;
90 if (
type->priv_data_size) {
92 if (!
ctx->priv_data) {
96 if (
type->priv_class) {
102 ctx->decompose_unit_types =
NULL;
104 ctx->trace_enable = 0;
114 if (
ctx->codec->flush)
125 if (
ctx->codec->close)
130 if (
ctx->codec->priv_class &&
ctx->priv_data)
152 for (
i = 0;
i < frag->nb_units;
i++)
159 frag->data_bit_padding = 0;
167 frag->nb_units_allocated = 0;
179 if (
ctx->decompose_unit_types) {
180 for (j = 0; j <
ctx->nb_decompose_unit_types; j++) {
181 if (
ctx->decompose_unit_types[j] == unit->
type)
184 if (j >=
ctx->nb_decompose_unit_types)
193 err =
ctx->codec->read_unit(
ctx, unit);
196 "Decomposition unimplemented for unit %d "
197 "(type %"PRIu32
").\n",
i, unit->
type);
198 }
else if (err ==
AVERROR(EAGAIN)) {
200 "Skipping decomposition of unit %d "
201 "(type %"PRIu32
").\n",
i, unit->
type);
204 }
else if (err < 0) {
206 "(type %"PRIu32
"): %s.\n",
252 err = cbs_fill_fragment_data(frag,
data,
size);
261 return cbs_read_fragment_content(
ctx, frag);
268 return cbs_read_data(
ctx, frag,
NULL,
270 par->extradata_size, 1);
277 return cbs_read_data(
ctx, frag,
NULL,
279 avctx->extradata_size, 1);
286 return cbs_read_data(
ctx, frag,
pkt->buf,
294 size_t side_data_size;
295 const uint8_t *side_data =
299 return cbs_read_data(
ctx, frag,
NULL,
300 side_data, side_data_size, 1);
308 return cbs_read_data(
ctx, frag, buf,
342 if (!
ctx->write_buffer) {
344 ctx->write_buffer_size = 1024 * 1024;
346 reallocate_and_try_again:
350 "sufficiently large write buffer (last attempt "
351 "%zu bytes).\n",
ctx->write_buffer_size);
358 ret =
ctx->codec->write_unit(
ctx, unit, &pbc);
362 if (
ctx->write_buffer_size == INT_MAX / 8)
364 ctx->write_buffer_size =
FFMIN(2 *
ctx->write_buffer_size, INT_MAX / 8);
365 goto reallocate_and_try_again;
395 for (
i = 0;
i < frag->nb_units;
i++) {
404 err = cbs_write_unit_data(
ctx, unit);
407 "(type %"PRIu32
").\n",
i, unit->
type);
416 err =
ctx->codec->assemble_fragment(
ctx, frag);
437 par->extradata_size = 0;
439 if (!frag->data_size)
442 par->extradata =
av_malloc(frag->data_size +
447 memcpy(par->extradata, frag->data, frag->data_size);
448 memset(par->extradata + frag->data_size, 0,
450 par->extradata_size = frag->data_size;
473 pkt->data = frag->data;
474 pkt->size = frag->data_size;
485 if (!
ctx->trace_enable)
494 const char *str,
const int *subscripts,
501 size_t name_len, bits_len;
502 int pad, subs,
i, j, k, n;
510 for (
i = 0;
i < length;
i++)
514 subs = subscripts ? subscripts[0] : 0;
516 for (
i = j = 0; str[
i];) {
523 for (++
i; str[
i] && str[
i] !=
']';
i++);
526 while (str[
i] && str[
i] !=
']')
527 name[j++] = str[
i++];
532 name[j++] = str[
i++];
539 name_len = strlen(
name);
542 if (name_len + bits_len > 60)
547 av_log(
ctx->log_ctx,
ctx->trace_level,
"%-10d %s%*s = %"PRId64
"\n",
554 const char *str,
const int *subscripts,
588 const int *subscripts,
601 "%s: bitstream ended.\n",
name);
611 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
622 const int *subscripts, uint32_t *write_to,
623 uint32_t range_min, uint32_t range_max)
625 return cbs_read_unsigned(
ctx, gbc,
width,
name, subscripts,
626 write_to, range_min, range_max);
630 int width,
const char *
name, uint32_t *write_to)
633 write_to, 0, UINT32_MAX);
640 const int *subscripts, uint32_t
value,
641 uint32_t range_min, uint32_t range_max)
649 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
675 const int *subscripts,
int32_t *write_to,
686 "%s: bitstream ended.\n",
name);
696 "%"PRId32
", but must be in [%"PRId32
",%"PRId32
"].\n",
718 "%"PRId32
", but must be in [%"PRId32
",%"PRId32
"].\n",
743 if (position < frag->nb_units)
744 memmove(units + position + 1, units + position,
745 (frag->
nb_units - position) *
sizeof(*units));
754 memcpy(units, frag->
units, position *
sizeof(*units));
756 if (position < frag->nb_units)
757 memcpy(units + position + 1, frag->
units + position,
758 (frag->
nb_units - position) *
sizeof(*units));
764 memset(units + position, 0,
sizeof(*units));
781 position = frag->nb_units;
782 av_assert0(position >= 0 && position <= frag->nb_units);
793 unit = &frag->units[position];
803 uint8_t *
data,
size_t data_size,
811 av_assert0(position >= 0 && position <= frag->nb_units);
829 unit = &frag->
units[position];
840 uint8_t *
data,
size_t data_size,
844 data, data_size, data_buf,
851 av_assert0(0 <= position && position < frag->nb_units
852 &&
"Unit to be deleted not in fragment.");
858 if (frag->nb_units > 0)
859 memmove(frag->units + position,
860 frag->units + position + 1,
861 (frag->nb_units - position) *
sizeof(*frag->units));
868 for (
int i = 0;
i <
desc->type.ref.nb_offsets;
i++) {
869 void **ptr = (
void**)((
char*)content +
desc->type.ref.offsets[
i]);
881 if (!
ctx->codec->unit_types)
886 if (
desc->nb_unit_types == 0)
889 if (unit->
type >=
desc->unit_type.range.start &&
890 unit->
type <=
desc->unit_type.range.end)
893 for (j = 0; j <
desc->nb_unit_types; j++) {
894 if (
desc->unit_type.list[j] == unit->
type)
907 ?
desc->type.complex.content_free
945 for (
int i = 0;
i <
desc->type.ref.nb_offsets;
i++) {
946 void **ptr = (
void**)(
copy +
desc->type.ref.offsets[
i]);
951 for (
int i = 0;
i <
desc->type.ref.nb_offsets;
i++) {
952 const uint8_t *
const *src_ptr = (
const uint8_t*
const*)(
src +
desc->type.ref.offsets[
i]);
954 uint8_t **copy_ptr = (uint8_t**)(
copy +
desc->type.ref.offsets[
i]);
1000 switch (
desc->content_type) {
1006 if (!
desc->type.complex.content_clone)
1008 err =
desc->type.complex.content_clone(&new_content, unit);
1054 if (!
ctx->codec->discarded_unit)
1057 for (
int i = frag->nb_units - 1;
i >= 0;
i--) {
1058 if (
ctx->codec->discarded_unit(
ctx, &frag->units[
i],
skip)) {
static av_cold void close(AVCodecParserContext *s)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
static int32_t BS_FUNC read_signed(BSCTX *bc, unsigned int n)
Return n bits from the buffer as a signed integer.
refcounted data buffer API
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
int CBS_FUNC read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
#define CBS_TRACE_READ_END()
int CBS_FUNC 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)
@ CBS_CONTENT_TYPE_INTERNAL_REFS
@ CBS_CONTENT_TYPE_COMPLEX
#define CBS_TRACE_READ_START()
#define MAX_UINT_BITS(length)
int CBS_FUNC write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
#define CBS_TRACE_WRITE_END()
int CBS_FUNC 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 CBS_FUNC 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)
#define CBS_TRACE_WRITE_START()
common internal and external API header
void(* flush)(AVBSFContext *ctx)
int(* init)(AVBSFContext *ctx)
static const uint8_t bits[8]
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
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 void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static unsigned int get_bits1(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
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_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#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.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
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.
int CBS_FUNC insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, void *content_ref)
Insert a new unit into a fragment with the given content.
void CBS_FUNC trace_read_log(void *trace_context, GetBitContext *gbc, int length, const char *str, const int *subscripts, int64_t value)
Helper function for read tracing which formats the syntax element and logs the result.
static void * cbs_alloc_content(CodedBitstreamUnitTypeDescriptor *desc)
void CBS_FUNC trace_header(CodedBitstreamContext *ctx, const char *name)
static int cbs_clone_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
void CBS_FUNC discard_units(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, enum AVDiscard skip, int flags)
Discard units according to 'skip'.
static void cbs_default_free_unit_content(AVRefStructOpaque opaque, void *content)
av_cold void CBS_FUNC fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
void CBS_FUNC delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
static CodedBitstreamUnitTypeDescriptor * cbs_find_unit_type_desc(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
static int cbs_insert_unit(CodedBitstreamFragment *frag, int position)
int CBS_FUNC alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
int CBS_FUNC make_unit_writable(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit writable so that internal fields can be modified.
static int cbs_insert_unit_data(CodedBitstreamFragment *frag, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf, int position)
void CBS_FUNC trace_write_log(void *trace_context, PutBitContext *pbc, int length, const char *str, const int *subscripts, int64_t value)
Helper function for write tracing which formats the syntax element and logs the result.
static void cbs_unit_uninit(CodedBitstreamUnit *unit)
int CBS_FUNC append_unit_data(CodedBitstreamFragment *frag, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Add a new unit to a fragment with the given data bitstream.
void CBS_FUNC 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...
static int cbs_clone_noncomplex_unit_content(void **clonep, const CodedBitstreamUnit *unit, CodedBitstreamUnitTypeDescriptor *desc)
int CBS_FUNC make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
static const CodedBitstreamType *const cbs_type_table[]
int CBS_FUNC read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const struct AVCodecContext *avctx)
Read the extradata bitstream found in a codec context into a fragment, then split into units and deco...
int CBS_FUNC write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
int CBS_FUNC read_packet_side_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
@ DISCARD_FLAG_KEEP_NON_VCL
keep non-vcl units even if the picture has been dropped.
int CBS_FUNC write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
int CBS_FUNC 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...
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
static av_const unsigned zero_extend(unsigned val, unsigned bits)
Memory handling functions.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
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 put_bits63(PutBitContext *s, int n, uint64_t value)
Write up to 63 bits into a bitstream.
static int put_bytes_output(const PutBitContext *s)
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
void * av_refstruct_alloc_ext_c(size_t size, unsigned flags, AVRefStructOpaque opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
Allocate a refcounted object of usable size size managed via the RefStruct API.
int av_refstruct_exclusive(const void *obj)
Check whether the reference count of an object managed via this API is 1.
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
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.
This struct describes the properties of an encoded stream.
This structure stores compressed data.
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_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.
void * content_ref
If content is reference counted, a RefStruct reference backing 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 AVFormatContext * ctx
RefStruct is an API for creating reference-counted objects with minimal overhead.
static void copy(const float *p1, float *p2, const int length)