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_H265(name) FUNC_NAME1(READWRITE, h265, 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_H265(name) args; \
48static int FUNC_H265(name ## _internal)(CodedBitstreamContext *ctx, \
49 RWContext *rw, void *cur, \
50 SEIMessageState *state) \
52 return FUNC_H265(name)(ctx, rw, cur, state); \
54static int FUNC_H265(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 = (void *)name ## _ref->data; \
148#define FUNC(name) FUNC_H265(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_H265(name)
267 size_t size, start, end;
268 int i, j, nb_arrays, nal_unit_type, nb_nals,
version;
277 version = bytestream2_get_byte(&gbc);
287 nb_arrays = bytestream2_get_byte(&gbc);
288 for (
i = 0;
i < nb_arrays;
i++) {
289 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
290 nb_nals = bytestream2_get_be16(&gbc);
293 for (j = 0; j < nb_nals; j++) {
296 size = bytestream2_get_be16(&gbc);
304 frag->
data + start, end - start,
309 "HVCC array %d (%d NAL units of type %d).\n",
310 i, nb_nals, nal_unit_type);
337#define cbs_h2645_replace_ps(ps_name, ps_var, id_element) \
338static int cbs_h265_replace_ ## ps_var(CodedBitstreamContext *ctx, \
339 CodedBitstreamUnit *unit) \
341 CodedBitstreamH265Context *priv = ctx->priv_data; \
342 H265Raw## ps_name *ps_var = unit->content; \
343 unsigned int id = ps_var->id_element; \
344 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
347 if (priv->ps_var[id] == priv->active_ ## ps_var) \
348 priv->active_ ## ps_var = NULL ; \
349 av_assert0(unit->content_ref); \
350 av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
368 err = ff_cbs_alloc_unit_content(
ctx, unit);
372 switch (unit->
type) {
377 err = cbs_h265_read_vps(
ctx, &gbc,
vps);
381 err = cbs_h265_replace_vps(
ctx, unit);
390 err = cbs_h265_read_sps(
ctx, &gbc,
sps);
394 err = cbs_h265_replace_sps(
ctx, unit);
404 err = cbs_h265_read_pps(
ctx, &gbc,
pps);
408 err = cbs_h265_replace_pps(
ctx, unit);
434 err = cbs_h265_read_slice_segment_header(
ctx, &gbc, &slice->
header);
455 err = cbs_h265_read_aud(
ctx, &gbc, unit->
content);
463 err = cbs_h265_read_filler(
ctx, &gbc, unit->
content);
472 err = cbs_h265_read_sei(
ctx, &gbc, unit->
content,
493 switch (unit->
type) {
498 err = cbs_h265_write_vps(
ctx, pbc,
vps);
502 err = cbs_h265_replace_vps(
ctx, unit);
512 err = cbs_h265_write_sps(
ctx, pbc,
sps);
516 err = cbs_h265_replace_sps(
ctx, unit);
526 err = cbs_h265_write_pps(
ctx, pbc,
pps);
530 err = cbs_h265_replace_pps(
ctx, unit);
555 err = cbs_h265_write_slice_segment_header(
ctx, pbc, &slice->
header);
573 err = cbs_h265_write_aud(
ctx, pbc, unit->
content);
581 err = cbs_h265_write_filler(
ctx, pbc, unit->
content);
590 err = cbs_h265_write_sei(
ctx, pbc, unit->
content,
600 "NAL unit type %"PRIu32
".\n", unit->
type);
616 switch (unit->
type) {
651 "h265 slice header is null, missing decompose?\n");
661 switch (unit->
type) {
750#define SEI_MESSAGE_RW(codec, name) \
751 .read = cbs_ ## codec ## _read_ ## name ## _internal, \
752 .write = cbs_ ## codec ## _write_ ## name ## _internal
832 .read_unit = &cbs_h265_read_nal_unit,
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 flags(name, subs,...)
#define i(width, name, range_min, range_max)
static int FUNC sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static int FUNC hrd_parameters(CodedBitstreamContext *ctx, RWContext *rw, H264RawHRD *current)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define SEI_MESSAGE_RW(codec, name)
static av_cold void cbs_h265_flush(CodedBitstreamContext *ctx)
static CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[]
static av_cold void cbs_h265_close(CodedBitstreamContext *ctx)
const CodedBitstreamType ff_cbs_type_h265
const SEIMessageTypeDescriptor ff_cbs_sei_h265_types[]
static int cbs_h265_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
#define cbs_h2645_replace_ps(ps_name, ps_var, id_element)
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
static int FUNC vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
static int FUNC extension_data(CodedBitstreamContext *ctx, RWContext *rw, H265RawExtensionData *current)
#define CBS_UNIT_TYPE_POD(type_, structure)
#define CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
@ CBS_CONTENT_TYPE_INTERNAL_REFS
#define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func)
#define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field)
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
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_ALPHA_CHANNEL_INFO
@ SEI_TYPE_DECODED_PICTURE_HASH
@ SEI_TYPE_RECOVERY_POINT
@ SEI_TYPE_ACTIVE_PARAMETER_SETS
@ SEI_TYPE_DISPLAY_ORIENTATION
@ SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO
@ 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.
const H265RawVPS * active_vps
const H265RawSPS * active_sps
H265RawVPS * vps[HEVC_MAX_VPS_COUNT]
RefStruct references.
CodedBitstreamH2645Context common
H265RawSPS * sps[HEVC_MAX_SPS_COUNT]
RefStruct references.
const H265RawPPS * active_pps
H265RawPPS * pps[HEVC_MAX_PPS_COUNT]
RefStruct references.
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.
H265RawSliceHeader header
static AVFormatContext * ctx
RefStruct is an API for creating reference-counted objects with minimal overhead.