35 const char *
name, uint32_t *write_to,
36 uint32_t range_min, uint32_t range_max)
45 for (i = 0; i < 32; i++) {
48 "%s: bitstream ended.\n", name);
52 bits[i] = k ?
'1' :
'0';
58 "%s: more than 31 zeroes.\n", name);
62 for (j = 0; j < i; j++) {
64 bits[i + j + 1] = k ?
'1' :
'0';
65 value = value << 1 | k;
75 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
76 name, value, range_min, range_max);
96 for (i = 0; i < 32; i++) {
99 "%s: bitstream ended.\n", name);
103 bits[i] = k ?
'1' :
'0';
109 "%s: more than 31 zeroes.\n", name);
113 for (j = 0; j < i; j++) {
115 bits[i + j + 1] = k ?
'1' :
'0';
129 "%"PRId32
", but must be in [%"PRId32
",%"PRId32
"].\n",
130 name, value, range_min, range_max);
140 uint32_t range_min, uint32_t range_max)
146 "%"PRIu32
", but must be in [%"PRIu32
",%"PRIu32
"].\n",
147 name, value, range_min, range_max);
160 for (i = 0; i <
len; i++)
163 for (i = 0; i <
len; i++)
164 bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ?
'1' :
'0';
165 bits[len + len + 1] = 0;
188 "%"PRId32
", but must be in [%"PRId32
",%"PRId32
"].\n",
189 name, value, range_min, range_max);
197 uvalue = 2 * (uint32_t)value - 1;
199 uvalue = 2 * (uint32_t)-value;
209 for (i = 0; i <
len; i++)
212 for (i = 0; i <
len; i++)
213 bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ?
'1' :
'0';
214 bits[len + len + 1] = 0;
228 #define HEADER(name) do { \
229 ff_cbs_trace_header(ctx, name); \
232 #define CHECK(call) do { \
238 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
239 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
240 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
244 #define READWRITE read
245 #define RWContext GetBitContext
247 #define xu(width, name, var, range_min, range_max) do { \
248 uint32_t value = range_min; \
249 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
250 &value, range_min, range_max)); \
253 #define xue(name, var, range_min, range_max) do { \
254 uint32_t value = range_min; \
255 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
256 &value, range_min, range_max)); \
259 #define xse(name, var, range_min, range_max) do { \
260 int32_t value = range_min; \
261 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
262 &value, range_min, range_max)); \
267 #define u(width, name, range_min, range_max) \
268 xu(width, name, current->name, range_min, range_max)
269 #define flag(name) u(1, name, 0, 1)
270 #define ue(name, range_min, range_max) \
271 xue(name, current->name, range_min, range_max)
272 #define se(name, range_min, range_max) \
273 xse(name, current->name, range_min, range_max)
275 #define infer(name, value) do { \
276 current->name = value; \
284 if (
show_bits(gbc, bits_left) == 1 << (bits_left - 1))
289 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
291 #define byte_alignment(rw) (get_bits_count(rw) % 8)
293 #define allocate(name, size) do { \
294 name ## _ref = av_buffer_allocz(size); \
296 return AVERROR(ENOMEM); \
297 name = name ## _ref->data; \
300 #define FUNC(name) FUNC_H264(READWRITE, name)
304 #define FUNC(name) FUNC_H265(READWRITE, name)
319 #undef more_rbsp_data
320 #undef byte_alignment
325 #define READWRITE write
326 #define RWContext PutBitContext
328 #define xu(width, name, var, range_min, range_max) do { \
329 uint32_t value = var; \
330 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
331 value, range_min, range_max)); \
333 #define xue(name, var, range_min, range_max) do { \
334 uint32_t value = var; \
335 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
336 value, range_min, range_max)); \
338 #define xse(name, var, range_min, range_max) do { \
339 int32_t value = var; \
340 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
341 value, range_min, range_max)); \
344 #define u(width, name, range_min, range_max) \
345 xu(width, name, current->name, range_min, range_max)
346 #define flag(name) u(1, name, 0, 1)
347 #define ue(name, range_min, range_max) \
348 xue(name, current->name, range_min, range_max)
349 #define se(name, range_min, range_max) \
350 xse(name, current->name, range_min, range_max)
352 #define infer(name, value) do { \
353 if (current->name != (value)) { \
354 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
355 "%s does not match inferred value: " \
356 "%"PRId64", but should be %"PRId64".\n", \
357 #name, (int64_t)current->name, (int64_t)(value)); \
361 #define more_rbsp_data(var) (var)
363 #define byte_alignment(rw) (put_bits_count(rw) % 8)
365 #define allocate(name, size) do { \
367 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
368 "for writing.\n", #name); \
369 return AVERROR_INVALIDDATA; \
373 #define FUNC(name) FUNC_H264(READWRITE, name)
377 #define FUNC(name) FUNC_H265(READWRITE, name)
392 #undef more_rbsp_data
393 #undef byte_alignment
474 for (i = 0; i < packet->
nb_nals; i++) {
480 while (size > 0 && nal->
data[size - 1] == 0)
487 memcpy(data, nal->
data, size);
526 version = bytestream2_get_byte(&gbc);
529 "first byte %u.", version);
537 count = bytestream2_get_byte(&gbc) & 0x1f;
539 for (i = 0; i <
count; i++) {
542 size = bytestream2_get_be16(&gbc);
550 frag->
data + start, end - start,
561 count = bytestream2_get_byte(&gbc);
563 for (i = 0; i <
count; i++) {
566 size = bytestream2_get_be16(&gbc);
574 frag->
data + start, end - start,
592 int i, j, nb_arrays, nal_unit_type, nb_nals,
version;
601 version = bytestream2_get_byte(&gbc);
604 "first byte %u.", version);
611 nb_arrays = bytestream2_get_byte(&gbc);
612 for (i = 0; i < nb_arrays; i++) {
613 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
614 nb_nals = bytestream2_get_be16(&gbc);
617 for (j = 0; j < nb_nals; j++) {
620 size = bytestream2_get_be16(&gbc);
628 frag->
data + start, end - start,
632 "HVCC array %d (%d NAL units of type %d).\n",
633 i, nb_nals, nal_unit_type);
660 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
661 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
662 const H26 ## h26n ## Raw ## ps_name *ps_var) \
664 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
665 unsigned int id = ps_var->id_element; \
666 if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
667 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
668 " id : %d.\n", id); \
669 return AVERROR_INVALIDDATA; \
671 if (priv->ps_var[id] == priv->active_ ## ps_var) \
672 priv->active_ ## ps_var = NULL ; \
673 av_freep(&priv->ps_var[id]); \
674 priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
675 if (!priv->ps_var[id]) \
676 return AVERROR(ENOMEM); \
677 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
683 cbs_h2645_replace_ps(5, VPS,
vps, vps_video_parameter_set_id)
684 cbs_h2645_replace_ps(5,
SPS,
sps, sps_seq_parameter_set_id)
685 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
697 switch (unit->type) {
707 err = cbs_h264_read_sps(ctx, &gbc, sps);
711 err = cbs_h264_replace_sps(ctx, sps);
725 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
741 err = cbs_h264_read_pps(ctx, &gbc, pps);
745 err = cbs_h264_replace_pps(ctx, pps);
762 slice = unit->content;
764 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->
header);
769 len = unit->data_size;
770 if (!unit->data[len - 1]) {
772 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
774 "from slice data.\n", z);
799 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
812 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
825 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
848 switch (unit->
type) {
859 err = cbs_h265_read_vps(ctx, &gbc, vps);
863 err = cbs_h265_replace_vps(ctx, vps);
878 err = cbs_h265_read_sps(ctx, &gbc, sps);
882 err = cbs_h265_replace_sps(ctx, sps);
898 err = cbs_h265_read_pps(ctx, &gbc, pps);
902 err = cbs_h265_replace_pps(ctx, pps);
934 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->
header);
940 if (!unit->
data[len - 1]) {
942 for (z = 0; z < len && !unit->
data[len - z - 1]; z++);
944 "from slice data.\n", z);
969 err = cbs_h265_read_aud(ctx, &gbc, unit->
content);
988 switch (unit->
type) {
993 err = cbs_h264_write_sps(ctx, pbc, sps);
997 err = cbs_h264_replace_sps(ctx, sps);
1007 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1017 err = cbs_h264_write_pps(ctx, pbc, pps);
1021 err = cbs_h264_replace_pps(ctx, pps);
1033 int bits_left,
end, zeroes;
1035 err = cbs_h264_write_slice_header(ctx, pbc, &slice->
header);
1057 if (bits_left > zeroes + 1)
1058 put_bits(pbc, bits_left - zeroes - 1,
1059 end >> (zeroes + 1));
1072 err = cbs_h264_write_aud(ctx, pbc, unit->
content);
1080 err = cbs_h264_write_sei(ctx, pbc, unit->
content);
1088 err = cbs_h264_write_filler(ctx, pbc, unit->
content);
1096 "NAL unit type %"PRIu32
".\n", unit->
type);
1109 switch (unit->
type) {
1114 err = cbs_h265_write_vps(ctx, pbc, vps);
1118 err = cbs_h265_replace_vps(ctx, vps);
1128 err = cbs_h265_write_sps(ctx, pbc, sps);
1132 err = cbs_h265_replace_sps(ctx, sps);
1142 err = cbs_h265_write_pps(ctx, pbc, pps);
1146 err = cbs_h265_replace_pps(ctx, pps);
1171 int bits_left,
end, zeroes;
1173 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->
header);
1195 if (bits_left > zeroes + 1)
1196 put_bits(pbc, bits_left - zeroes - 1,
1197 end >> (zeroes + 1));
1209 err = cbs_h265_write_aud(ctx, pbc, unit->
content);
1217 "NAL unit type %"PRIu32
".\n", unit->
type);
1236 reallocate_and_try_again:
1240 "sufficiently large write buffer (last attempt "
1256 goto reallocate_and_try_again;
1287 size_t max_size, dp,
sp;
1288 int err, i, zero_run;
1290 for (i = 0; i < frag->
nb_units; i++) {
1296 for (i = 0; i < frag->
nb_units; i++) {
1306 for (i = 0; i < frag->
nb_units; i++) {
1310 if (i < frag->nb_units - 1)
1312 "unaligned padding on non-final NAL unit.\n");
1334 for (sp = 0; sp < unit->
data_size; sp++) {
1336 if (unit->
data[sp] == 0)
1341 if ((unit->
data[sp] & ~3) == 0) {
1345 zero_run = unit->
data[
sp] == 0;
1347 data[dp++] = unit->
data[
sp];
1408 .read_unit = &cbs_h264_read_nal_unit,
1437 for (i = 0; i < au->
nb_units; i++) {
1439 nal = &au->
units[i];
1465 for (i = 0; i < au->
nb_units; i++) {
1498 av_assert0(position >= 0 && position < sei->payload_count);
1504 for (i = 0; i < au->
nb_units; i++) {
1505 if (&au->
units[i] == nal)
1508 av_assert0(i < au->nb_units &&
"NAL unit not in access unit.");
1515 memmove(sei->
payload + position,
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id, int small_padding)
Split an input packet into NAL units.
static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
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.
H265RawPPS * pps[HEVC_MAX_PPS_COUNT]
#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 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.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
H264RawPPS * pps[H264_MAX_PPS_COUNT]
H264RawNALUnitHeader nal_unit_header
CodedBitstreamUnitType type
Codec-specific type of this unit.
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
struct H264RawSEIPayload::@42::@43 other
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
CodedBitstreamH2645Context common
int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, const H264RawSEIPayload *payload)
Add an SEI message to an access unit.
Macro definitions for various function/variable attributes.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int trace_enable
Enable trace output during read/write operations.
static void cbs_h264_free_pps(void *unit, uint8_t *content)
static av_cold int end(AVCodecContext *avctx)
static void cbs_h265_free_slice(void *unit, uint8_t *content)
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
H265RawSPS * sps[HEVC_MAX_SPS_COUNT]
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
static int get_bits_count(const GetBitContext *s)
H264RawSPS * sps[H264_MAX_SPS_COUNT]
static void cbs_h264_close(CodedBitstreamContext *ctx)
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.
H265RawPSExtensionData extension_data
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.
H.264 common definitions.
static int get_bits_left(GetBitContext *gb)
union H264RawSEIPayload::@42 payload
AVCodecID
Identify the syntax and semantics of the bitstream.
buffering period (H.264, D.1.1)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void cbs_h265_free_pps(void *unit, uint8_t *content)
static int put_bits_left(PutBitContext *s)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
simple assert() macros that are a bit more flexible than ISO C assert().
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
static int put_bits_count(PutBitContext *s)
H264RawSEIUserDataUnregistered user_data_unregistered
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
const CodedBitstreamType ff_cbs_type_h264
static void cbs_h265_close(CodedBitstreamContext *ctx)
H265RawPSExtensionData extension_data
GLsizei GLboolean const GLfloat * value
static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, int32_t value, int32_t range_min, int32_t range_max)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
int ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
static void cbs_h265_free_vps(void *unit, uint8_t *content)
uint8_t * data
Pointer to the bitstream form of this fragment.
#define FF_ARRAY_ELEMS(a)
H264RawSEIUserDataRegistered user_data_registered
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
static av_always_inline int bytestream2_tell(GetByteContext *g)
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
static void cbs_h264_free_slice(void *unit, uint8_t *content)
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Coded bitstream fragment structure, combining one or more units.
uint8_t * data
The data buffer.
AVBufferRef * slice_group_id_ref
static unsigned int get_bits1(GetBitContext *s)
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, int32_t *write_to, int32_t range_min, int32_t range_max)
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
recovery point (frame # to decoder sync)
static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
registered user data as specified by Rec. ITU-T T.35
H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS]
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *name, const char *bits, int64_t value)
void * priv_data
Internal codec-specific data.
A reference to a data buffer.
H265RawPSExtensionData extension_data
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
const CodedBitstreamType ff_cbs_type_h265
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
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...
H265RawSliceHeader header
H264RawSliceHeader header
int ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, CodedBitstreamUnit *nal, int position)
Delete an SEI message from an access unit.
CodedBitstreamH2645Context common
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element)
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
AVBufferRef * data_ref
If data is reference counted, a reference to the buffer containing data.
static void cbs_h264_free_sei(void *unit, uint8_t *content)
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
static void cbs_h265_free_sps(void *unit, uint8_t *content)