29#define HEADER(name) do { \
30 ff_cbs_trace_header(ctx, name); \
33#define CHECK(call) do { \
39#define FUNC_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
40#define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
41#define FUNC_H264(name) FUNC_NAME1(READWRITE, h264, name)
42#define FUNC_NAME2_EXPORT(rw, codec, name) ff_cbs_ ## codec ## _ ## rw ## _ ## name
43#define FUNC_NAME1_EXPORT(rw, codec, name) FUNC_NAME2_EXPORT(rw, codec, name)
44#define FUNC_SEI(name) FUNC_NAME1_EXPORT(READWRITE, sei, name)
46#define SEI_FUNC(name, args) \
47static int FUNC_H264(name) args; \
48static int FUNC_H264(name ## _internal)(CodedBitstreamContext *ctx, \
49 RWContext *rw, void *cur, \
50 SEIMessageState *state) \
52 return FUNC_H264(name)(ctx, rw, cur, state); \
54static int FUNC_H264(name) args
56#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
58#define u(width, name, range_min, range_max) \
59 xu(width, name, current->name, range_min, range_max, 0, )
60#define flag(name) ub(1, name)
61#define ue(name, range_min, range_max) \
62 xue(name, current->name, range_min, range_max, 0, )
63#define i(width, name, range_min, range_max) \
64 xi(width, name, current->name, range_min, range_max, 0, )
65#define ib(width, name) \
66 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
67#define se(name, range_min, range_max) \
68 xse(name, current->name, range_min, range_max, 0, )
70#define us(width, name, range_min, range_max, subs, ...) \
71 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
72#define ubs(width, name, subs, ...) \
73 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
74#define flags(name, subs, ...) \
75 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
76#define ues(name, range_min, range_max, subs, ...) \
77 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
78#define is(width, name, range_min, range_max, subs, ...) \
79 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
80#define ibs(width, name, subs, ...) \
81 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
82#define ses(name, range_min, range_max, subs, ...) \
83 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
85#define fixed(width, name, value) do { \
86 av_unused uint32_t fixed_value = value; \
87 xu(width, name, fixed_value, value, value, 0, ); \
93#define RWContext GetBitContext
95#define ub(width, name) do { \
97 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
99 current->name = value; \
101#define xu(width, name, var, range_min, range_max, subs, ...) do { \
103 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
104 SUBSCRIPTS(subs, __VA_ARGS__), \
105 &value, range_min, range_max)); \
108#define xue(name, var, range_min, range_max, subs, ...) do { \
110 CHECK(ff_cbs_read_ue_golomb(ctx, rw, #name, \
111 SUBSCRIPTS(subs, __VA_ARGS__), \
112 &value, range_min, range_max)); \
115#define xi(width, name, var, range_min, range_max, subs, ...) do { \
117 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
118 SUBSCRIPTS(subs, __VA_ARGS__), \
119 &value, range_min, range_max)); \
122#define xse(name, var, range_min, range_max, subs, ...) do { \
124 CHECK(ff_cbs_read_se_golomb(ctx, rw, #name, \
125 SUBSCRIPTS(subs, __VA_ARGS__), \
126 &value, range_min, range_max)); \
131#define infer(name, value) do { \
132 current->name = value; \
135#define more_rbsp_data(var) ((var) = ff_cbs_h2645_read_more_rbsp_data(rw))
137#define bit_position(rw) (get_bits_count(rw))
138#define byte_alignment(rw) (get_bits_count(rw) % 8)
140#define allocate(name, size) do { \
141 name ## _ref = av_buffer_allocz(size + \
142 AV_INPUT_BUFFER_PADDING_SIZE); \
144 return AVERROR(ENOMEM); \
145 name = name ## _ref->data; \
148#define FUNC(name) FUNC_H264(name)
166#undef allocate_struct
170#define READWRITE write
171#define RWContext PutBitContext
173#define ub(width, name) do { \
174 uint32_t value = current->name; \
175 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
178#define xu(width, name, var, range_min, range_max, subs, ...) do { \
179 uint32_t value = var; \
180 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
181 SUBSCRIPTS(subs, __VA_ARGS__), \
182 value, range_min, range_max)); \
184#define xue(name, var, range_min, range_max, subs, ...) do { \
185 uint32_t value = var; \
186 CHECK(ff_cbs_write_ue_golomb(ctx, rw, #name, \
187 SUBSCRIPTS(subs, __VA_ARGS__), \
188 value, range_min, range_max)); \
190#define xi(width, name, var, range_min, range_max, subs, ...) do { \
191 int32_t value = var; \
192 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
193 SUBSCRIPTS(subs, __VA_ARGS__), \
194 value, range_min, range_max)); \
196#define xse(name, var, range_min, range_max, subs, ...) do { \
197 int32_t value = var; \
198 CHECK(ff_cbs_write_se_golomb(ctx, rw, #name, \
199 SUBSCRIPTS(subs, __VA_ARGS__), \
200 value, range_min, range_max)); \
203#define infer(name, value) do { \
204 if (current->name != (value)) { \
205 av_log(ctx->log_ctx, AV_LOG_ERROR, \
206 "%s does not match inferred value: " \
207 "%"PRId64", but should be %"PRId64".\n", \
208 #name, (int64_t)current->name, (int64_t)(value)); \
209 return AVERROR_INVALIDDATA; \
213#define more_rbsp_data(var) (var)
215#define bit_position(rw) (put_bits_count(rw))
216#define byte_alignment(rw) (put_bits_count(rw) % 8)
218#define allocate(name, size) do { \
220 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
221 "for writing.\n", #name); \
222 return AVERROR_INVALIDDATA; \
226#define FUNC(name) FUNC_H264(name)
267 size_t size, start, end;
277 version = bytestream2_get_byte(&gbc);
288 count = bytestream2_get_byte(&gbc) & 0x1f;
290 for (
i = 0;
i < count;
i++) {
293 size = bytestream2_get_be16(&gbc);
301 frag->
data + start, end - start,
313 count = bytestream2_get_byte(&gbc);
315 for (
i = 0;
i < count;
i++) {
318 size = bytestream2_get_be16(&gbc);
326 frag->
data + start, end - start,
361#define cbs_h2645_replace_ps(ps_name, ps_var, id_element) \
362static int cbs_h264_replace_ ## ps_var(CodedBitstreamContext *ctx, \
363 CodedBitstreamUnit *unit) \
365 CodedBitstreamH264Context *priv = ctx->priv_data; \
366 H264Raw ## ps_name *ps_var = unit->content; \
367 unsigned int id = ps_var->id_element; \
368 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
371 if (priv->ps_var[id] == priv->active_ ## ps_var) \
372 priv->active_ ## ps_var = NULL ; \
373 av_assert0(unit->content_ref); \
374 av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
391 err = ff_cbs_alloc_unit_content(
ctx, unit);
395 switch (unit->
type) {
400 err = cbs_h264_read_sps(
ctx, &gbc,
sps);
404 err = cbs_h264_replace_sps(
ctx, unit);
412 err = cbs_h264_read_sps_extension(
ctx, &gbc, unit->
content);
422 err = cbs_h264_read_pps(
ctx, &gbc,
pps);
426 err = cbs_h264_replace_pps(
ctx, unit);
439 err = cbs_h264_read_slice_header(
ctx, &gbc, &slice->
header);
460 err = cbs_h264_read_aud(
ctx, &gbc, unit->
content);
468 err = cbs_h264_read_sei(
ctx, &gbc, unit->
content);
476 err = cbs_h264_read_filler(
ctx, &gbc, unit->
content);
486 cbs_h264_read_end_of_sequence :
487 cbs_h264_read_end_of_stream)(
ctx, &gbc, unit->
content);
506 switch (unit->
type) {
511 err = cbs_h264_write_sps(
ctx, pbc,
sps);
515 err = cbs_h264_replace_sps(
ctx, unit);
525 err = cbs_h264_write_sps_extension(
ctx, pbc, sps_ext);
535 err = cbs_h264_write_pps(
ctx, pbc,
pps);
539 err = cbs_h264_replace_pps(
ctx, unit);
551 err = cbs_h264_write_slice_header(
ctx, pbc, &slice->
header);
570 err = cbs_h264_write_aud(
ctx, pbc, unit->
content);
578 err = cbs_h264_write_sei(
ctx, pbc, unit->
content);
586 err = cbs_h264_write_filler(
ctx, pbc, unit->
content);
594 err = cbs_h264_write_end_of_sequence(
ctx, pbc, unit->
content);
602 err = cbs_h264_write_end_of_stream(
ctx, pbc, unit->
content);
610 "NAL unit type %"PRIu32
".\n", unit->
type);
623 int slice_type_i, slice_type_b, slice_type_si;
643 "h264 nal unit header is null, missing decompose?\n");
653 "h264 slice header is null, missing decompose?\n");
730 .read_unit = &cbs_h264_read_nal_unit,
740#define SEI_MESSAGE_RW(codec, name) \
741 .read = cbs_ ## codec ## _read_ ## name ## _internal, \
742 .write = cbs_ ## codec ## _write_ ## name ## _internal
static av_cold void close(AVCodecParserContext *s)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
static av_always_inline int bytestream2_tell(const GetByteContext *g)
int ff_cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
int ff_cbs_h2645_write_slice_data(CodedBitstreamContext *ctx, PutBitContext *pbc, const uint8_t *data, size_t data_size, int data_bit_start)
int ff_cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
int ff_cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
#define SEI_MESSAGE_RW(codec, name)
#define flags(name, subs,...)
const SEIMessageTypeDescriptor ff_cbs_sei_h264_types[]
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static av_cold void cbs_h264_close(CodedBitstreamContext *ctx)
#define i(width, name, range_min, range_max)
#define cbs_h2645_replace_ps(ps_name, ps_var, id_element)
static av_cold void cbs_h264_flush(CodedBitstreamContext *ctx)
static void cbs_h264_free_sei(AVRefStructOpaque unused, void *content)
const CodedBitstreamType ff_cbs_type_h264
static int cbs_h264_discarded_nal_unit(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
static int cbs_h264_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
static CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[]
static int FUNC sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define CBS_UNIT_TYPE_POD(type_, structure)
#define CBS_UNIT_TYPES_INTERNAL_REF(types, structure, ref_field)
#define CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
void ff_cbs_sei_free_message_list(SEIRawMessageList *list)
Free all SEI messages in a message list.
#define SEI_MESSAGE_TYPE_END
void(* flush)(AVBSFContext *ctx)
bitstream reader API header.
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.
@ AVDISCARD_ALL
discard all
@ AVDISCARD_NONKEY
discard all frames except keyframes
@ AVDISCARD_BIDIR
discard all bidirectional frames
@ AVDISCARD_DEFAULT
discard useless packets like 0 size packets in avi
@ AVDISCARD_NONINTRA
discard all non intra frames
@ AVDISCARD_NONREF
discard all non reference
AVBufferRef * av_buffer_ref(const 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_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ H2645_FLAG_SMALL_PADDING
@ H264_NAL_AUXILIARY_SLICE
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int nal_length_size, enum AVCodecID codec_id, int flags)
Split an input packet into NAL units.
Memory handling functions.
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
static const uint8_t header[24]
@ SEI_TYPE_RECOVERY_POINT
@ SEI_TYPE_DISPLAY_ORIENTATION
@ SEI_TYPE_FRAME_PACKING_ARRANGEMENT
@ SEI_TYPE_FILM_GRAIN_CHARACTERISTICS
@ SEI_TYPE_BUFFERING_PERIOD
#define FF_ARRAY_ELEMS(a)
Context structure for coded bitstream operations.
Coded bitstream fragment structure, combining one or more units.
int nb_units
Number of units in this fragment.
size_t data_size
The number of bytes in the bitstream.
uint8_t * data
Pointer to the bitstream form of this fragment.
uint8_t last_slice_nal_unit_type
const H264RawPPS * active_pps
const H264RawSPS * active_sps
H264RawSPS * sps[H264_MAX_SPS_COUNT]
RefStruct references.
H264RawPPS * pps[H264_MAX_PPS_COUNT]
RefStruct references.
CodedBitstreamH2645Context common
Coded bitstream unit structure.
void * content
Pointer to the decomposed form of this unit.
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
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.
H264RawSliceHeader header
static AVFormatContext * ctx
RefStruct is an API for creating reference-counted objects with minimal overhead.