2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
22 #include "bytestream.h"
24 #include "cbs_internal.h"
29 #include "h2645_parse.h"
34 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
35 const char *name, const int *subscripts,
37 uint32_t range_min, uint32_t range_max)
44 position = get_bits_count(gbc);
46 for (i = 0; i < 32; i++) {
47 if (get_bits_left(gbc) < i + 1) {
48 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
49 "%s: bitstream ended.\n", name);
50 return AVERROR_INVALIDDATA;
53 bits[i] = k ? '1' : '0';
58 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
59 "%s: more than 31 zeroes.\n", name);
60 return AVERROR_INVALIDDATA;
63 for (j = 0; j < i; j++) {
65 bits[i + j + 1] = k ? '1' : '0';
66 value = value << 1 | k;
71 if (ctx->trace_enable)
72 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
75 if (value < range_min || value > range_max) {
76 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
77 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
78 name, value, range_min, range_max);
79 return AVERROR_INVALIDDATA;
86 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
87 const char *name, const int *subscripts,
89 int32_t range_min, int32_t range_max)
97 position = get_bits_count(gbc);
99 for (i = 0; i < 32; i++) {
100 if (get_bits_left(gbc) < i + 1) {
101 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
102 "%s: bitstream ended.\n", name);
103 return AVERROR_INVALIDDATA;
106 bits[i] = k ? '1' : '0';
111 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
112 "%s: more than 31 zeroes.\n", name);
113 return AVERROR_INVALIDDATA;
116 for (j = 0; j < i; j++) {
118 bits[i + j + 1] = k ? '1' : '0';
123 value = -(int32_t)(v / 2);
127 if (ctx->trace_enable)
128 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
131 if (value < range_min || value > range_max) {
132 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
133 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
134 name, value, range_min, range_max);
135 return AVERROR_INVALIDDATA;
142 static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
143 const char *name, const int *subscripts,
145 uint32_t range_min, uint32_t range_max)
149 if (value < range_min || value > range_max) {
150 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
151 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
152 name, value, range_min, range_max);
153 return AVERROR_INVALIDDATA;
155 av_assert0(value != UINT32_MAX);
157 len = av_log2(value + 1);
158 if (put_bits_left(pbc) < 2 * len + 1)
159 return AVERROR(ENOSPC);
161 if (ctx->trace_enable) {
165 for (i = 0; i < len; i++)
168 for (i = 0; i < len; i++)
169 bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
170 bits[len + len + 1] = 0;
172 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
173 name, subscripts, bits, value);
176 put_bits(pbc, len, 0);
178 put_bits(pbc, len + 1, value + 1);
180 put_bits32(pbc, value + 1);
185 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
186 const char *name, const int *subscripts,
188 int32_t range_min, int32_t range_max)
193 if (value < range_min || value > range_max) {
194 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
195 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
196 name, value, range_min, range_max);
197 return AVERROR_INVALIDDATA;
199 av_assert0(value != INT32_MIN);
204 uvalue = 2 * (uint32_t)value - 1;
206 uvalue = 2 * (uint32_t)-value;
208 len = av_log2(uvalue + 1);
209 if (put_bits_left(pbc) < 2 * len + 1)
210 return AVERROR(ENOSPC);
212 if (ctx->trace_enable) {
216 for (i = 0; i < len; i++)
219 for (i = 0; i < len; i++)
220 bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
221 bits[len + len + 1] = 0;
223 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
224 name, subscripts, bits, value);
227 put_bits(pbc, len, 0);
229 put_bits(pbc, len + 1, uvalue + 1);
231 put_bits32(pbc, uvalue + 1);
236 #define HEADER(name) do { \
237 ff_cbs_trace_header(ctx, name); \
240 #define CHECK(call) do { \
246 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
247 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
248 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
250 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
252 #define u(width, name, range_min, range_max) \
253 xu(width, name, current->name, range_min, range_max, 0)
254 #define ub(width, name) \
255 xu(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
256 #define flag(name) ub(1, name)
257 #define ue(name, range_min, range_max) \
258 xue(name, current->name, range_min, range_max, 0)
259 #define i(width, name, range_min, range_max) \
260 xi(width, name, current->name, range_min, range_max, 0)
261 #define ib(width, name) \
262 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0)
263 #define se(name, range_min, range_max) \
264 xse(name, current->name, range_min, range_max, 0)
266 #define us(width, name, range_min, range_max, subs, ...) \
267 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
268 #define ubs(width, name, subs, ...) \
269 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
270 #define flags(name, subs, ...) \
271 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
272 #define ues(name, range_min, range_max, subs, ...) \
273 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
274 #define is(width, name, range_min, range_max, subs, ...) \
275 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
276 #define ibs(width, name, subs, ...) \
277 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
278 #define ses(name, range_min, range_max, subs, ...) \
279 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
281 #define fixed(width, name, value) do { \
282 av_unused uint32_t fixed_value = value; \
283 xu(width, name, fixed_value, value, value, 0); \
288 #define READWRITE read
289 #define RWContext GetBitContext
291 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
293 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
294 SUBSCRIPTS(subs, __VA_ARGS__), \
295 &value, range_min, range_max)); \
298 #define xue(name, var, range_min, range_max, subs, ...) do { \
300 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
301 SUBSCRIPTS(subs, __VA_ARGS__), \
302 &value, range_min, range_max)); \
305 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
307 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
308 SUBSCRIPTS(subs, __VA_ARGS__), \
309 &value, range_min, range_max)); \
312 #define xse(name, var, range_min, range_max, subs, ...) do { \
314 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
315 SUBSCRIPTS(subs, __VA_ARGS__), \
316 &value, range_min, range_max)); \
321 #define infer(name, value) do { \
322 current->name = value; \
325 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
327 int bits_left = get_bits_left(gbc);
332 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
337 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
339 #define byte_alignment(rw) (get_bits_count(rw) % 8)
341 #define allocate(name, size) do { \
342 name ## _ref = av_buffer_allocz(size + \
343 AV_INPUT_BUFFER_PADDING_SIZE); \
345 return AVERROR(ENOMEM); \
346 name = name ## _ref->data; \
349 #define FUNC(name) FUNC_H264(READWRITE, name)
350 #include "cbs_h264_syntax_template.c"
353 #define FUNC(name) FUNC_H265(READWRITE, name)
354 #include "cbs_h265_syntax_template.c"
365 #undef more_rbsp_data
366 #undef byte_alignment
371 #define READWRITE write
372 #define RWContext PutBitContext
374 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
375 uint32_t value = var; \
376 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
377 SUBSCRIPTS(subs, __VA_ARGS__), \
378 value, range_min, range_max)); \
380 #define xue(name, var, range_min, range_max, subs, ...) do { \
381 uint32_t value = var; \
382 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
383 SUBSCRIPTS(subs, __VA_ARGS__), \
384 value, range_min, range_max)); \
386 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
387 int32_t value = var; \
388 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
389 SUBSCRIPTS(subs, __VA_ARGS__), \
390 value, range_min, range_max)); \
392 #define xse(name, var, range_min, range_max, subs, ...) do { \
393 int32_t value = var; \
394 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
395 SUBSCRIPTS(subs, __VA_ARGS__), \
396 value, range_min, range_max)); \
399 #define infer(name, value) do { \
400 if (current->name != (value)) { \
401 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
402 "%s does not match inferred value: " \
403 "%"PRId64", but should be %"PRId64".\n", \
404 #name, (int64_t)current->name, (int64_t)(value)); \
408 #define more_rbsp_data(var) (var)
410 #define byte_alignment(rw) (put_bits_count(rw) % 8)
412 #define allocate(name, size) do { \
414 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
415 "for writing.\n", #name); \
416 return AVERROR_INVALIDDATA; \
420 #define FUNC(name) FUNC_H264(READWRITE, name)
421 #include "cbs_h264_syntax_template.c"
424 #define FUNC(name) FUNC_H265(READWRITE, name)
425 #include "cbs_h265_syntax_template.c"
441 #undef more_rbsp_data
442 #undef byte_alignment
446 static void cbs_h264_free_pps(void *opaque, uint8_t *content)
448 H264RawPPS *pps = (H264RawPPS*)content;
449 av_buffer_unref(&pps->slice_group_id_ref);
453 static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
455 switch (payload->payload_type) {
456 case H264_SEI_TYPE_BUFFERING_PERIOD:
457 case H264_SEI_TYPE_PIC_TIMING:
458 case H264_SEI_TYPE_PAN_SCAN_RECT:
459 case H264_SEI_TYPE_RECOVERY_POINT:
460 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
461 case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
462 case H264_SEI_TYPE_ALTERNATIVE_TRANSFER:
464 case H264_SEI_TYPE_USER_DATA_REGISTERED:
465 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
467 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
468 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
471 av_buffer_unref(&payload->payload.other.data_ref);
476 static void cbs_h264_free_sei(void *opaque, uint8_t *content)
478 H264RawSEI *sei = (H264RawSEI*)content;
480 for (i = 0; i < sei->payload_count; i++)
481 cbs_h264_free_sei_payload(&sei->payload[i]);
485 static void cbs_h264_free_slice(void *opaque, uint8_t *content)
487 H264RawSlice *slice = (H264RawSlice*)content;
488 av_buffer_unref(&slice->data_ref);
492 static void cbs_h265_free_vps(void *opaque, uint8_t *content)
494 H265RawVPS *vps = (H265RawVPS*)content;
495 av_buffer_unref(&vps->extension_data.data_ref);
499 static void cbs_h265_free_sps(void *opaque, uint8_t *content)
501 H265RawSPS *sps = (H265RawSPS*)content;
502 av_buffer_unref(&sps->extension_data.data_ref);
506 static void cbs_h265_free_pps(void *opaque, uint8_t *content)
508 H265RawPPS *pps = (H265RawPPS*)content;
509 av_buffer_unref(&pps->extension_data.data_ref);
513 static void cbs_h265_free_slice(void *opaque, uint8_t *content)
515 H265RawSlice *slice = (H265RawSlice*)content;
516 av_buffer_unref(&slice->data_ref);
520 static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
522 switch (payload->payload_type) {
523 case HEVC_SEI_TYPE_BUFFERING_PERIOD:
524 case HEVC_SEI_TYPE_PICTURE_TIMING:
525 case HEVC_SEI_TYPE_PAN_SCAN_RECT:
526 case HEVC_SEI_TYPE_RECOVERY_POINT:
527 case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
528 case HEVC_SEI_TYPE_ACTIVE_PARAMETER_SETS:
529 case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
530 case HEVC_SEI_TYPE_TIME_CODE:
531 case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
532 case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
533 case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
534 case HEVC_SEI_TYPE_ALPHA_CHANNEL_INFO:
536 case HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
537 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
539 case HEVC_SEI_TYPE_USER_DATA_UNREGISTERED:
540 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
543 av_buffer_unref(&payload->payload.other.data_ref);
548 static void cbs_h265_free_sei(void *opaque, uint8_t *content)
550 H265RawSEI *sei = (H265RawSEI*)content;
552 for (i = 0; i < sei->payload_count; i++)
553 cbs_h265_free_sei_payload(&sei->payload[i]);
557 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
558 CodedBitstreamFragment *frag,
559 const H2645Packet *packet)
563 for (i = 0; i < packet->nb_nals; i++) {
564 const H2645NAL *nal = &packet->nals[i];
566 size_t size = nal->size;
568 if (nal->nuh_layer_id > 0)
571 // Remove trailing zeroes.
572 while (size > 0 && nal->data[size - 1] == 0)
575 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
579 ref = (nal->data == nal->raw_data) ? frag->data_ref
580 : packet->rbsp.rbsp_buffer_ref;
582 err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
583 (uint8_t*)nal->data, size, ref);
591 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
592 CodedBitstreamFragment *frag,
595 enum AVCodecID codec_id = ctx->codec->codec_id;
596 CodedBitstreamH2645Context *priv = ctx->priv_data;
600 av_assert0(frag->data && frag->nb_units == 0);
601 if (frag->data_size == 0)
604 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
606 size_t size, start, end;
607 int i, count, version;
611 bytestream2_init(&gbc, frag->data, frag->data_size);
613 if (bytestream2_get_bytes_left(&gbc) < 6)
614 return AVERROR_INVALIDDATA;
616 version = bytestream2_get_byte(&gbc);
618 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
619 "first byte %u.\n", version);
620 return AVERROR_INVALIDDATA;
623 bytestream2_skip(&gbc, 3);
624 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
627 count = bytestream2_get_byte(&gbc) & 0x1f;
628 start = bytestream2_tell(&gbc);
629 for (i = 0; i < count; i++) {
630 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
631 return AVERROR_INVALIDDATA;
632 size = bytestream2_get_be16(&gbc);
633 if (bytestream2_get_bytes_left(&gbc) < size)
634 return AVERROR_INVALIDDATA;
635 bytestream2_skip(&gbc, size);
637 end = bytestream2_tell(&gbc);
639 err = ff_h2645_packet_split(&priv->read_packet,
640 frag->data + start, end - start,
641 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
643 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
646 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
651 count = bytestream2_get_byte(&gbc);
652 start = bytestream2_tell(&gbc);
653 for (i = 0; i < count; i++) {
654 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
655 return AVERROR_INVALIDDATA;
656 size = bytestream2_get_be16(&gbc);
657 if (bytestream2_get_bytes_left(&gbc) < size)
658 return AVERROR_INVALIDDATA;
659 bytestream2_skip(&gbc, size);
661 end = bytestream2_tell(&gbc);
663 err = ff_h2645_packet_split(&priv->read_packet,
664 frag->data + start, end - start,
665 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
667 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
670 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
674 if (bytestream2_get_bytes_left(&gbc) > 0) {
675 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
676 "header.\n", bytestream2_get_bytes_left(&gbc));
679 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
681 size_t size, start, end;
682 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
686 bytestream2_init(&gbc, frag->data, frag->data_size);
688 if (bytestream2_get_bytes_left(&gbc) < 23)
689 return AVERROR_INVALIDDATA;
691 version = bytestream2_get_byte(&gbc);
693 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
694 "first byte %u.\n", version);
695 return AVERROR_INVALIDDATA;
698 bytestream2_skip(&gbc, 20);
699 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
701 nb_arrays = bytestream2_get_byte(&gbc);
702 for (i = 0; i < nb_arrays; i++) {
703 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
704 nb_nals = bytestream2_get_be16(&gbc);
706 start = bytestream2_tell(&gbc);
707 for (j = 0; j < nb_nals; j++) {
708 if (bytestream2_get_bytes_left(&gbc) < 2)
709 return AVERROR_INVALIDDATA;
710 size = bytestream2_get_be16(&gbc);
711 if (bytestream2_get_bytes_left(&gbc) < size)
712 return AVERROR_INVALIDDATA;
713 bytestream2_skip(&gbc, size);
715 end = bytestream2_tell(&gbc);
717 err = ff_h2645_packet_split(&priv->read_packet,
718 frag->data + start, end - start,
719 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
721 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
722 "HVCC array %d (%d NAL units of type %d).\n",
723 i, nb_nals, nal_unit_type);
726 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
732 // Annex B, or later MP4 with already-known parameters.
734 err = ff_h2645_packet_split(&priv->read_packet,
735 frag->data, frag->data_size,
737 priv->mp4, priv->nal_length_size,
742 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
750 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
751 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
752 CodedBitstreamUnit *unit) \
754 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
755 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
756 unsigned int id = ps_var->id_element; \
757 if (id >= FF_ARRAY_ELEMS(priv->ps_var)) { \
758 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
759 " id : %d.\n", id); \
760 return AVERROR_INVALIDDATA; \
762 if (priv->ps_var[id] == priv->active_ ## ps_var) \
763 priv->active_ ## ps_var = NULL ; \
764 av_buffer_unref(&priv->ps_var ## _ref[id]); \
765 if (unit->content_ref) \
766 priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
768 priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
769 if (!priv->ps_var ## _ref[id]) \
770 return AVERROR(ENOMEM); \
771 priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
772 if (!unit->content_ref) \
773 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
777 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
778 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
779 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
780 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
781 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
783 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
784 CodedBitstreamUnit *unit)
789 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
793 switch (unit->type) {
798 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
803 err = cbs_h264_read_sps(ctx, &gbc, sps);
807 err = cbs_h264_replace_sps(ctx, unit);
813 case H264_NAL_SPS_EXT:
815 err = ff_cbs_alloc_unit_content(ctx, unit,
816 sizeof(H264RawSPSExtension),
821 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
831 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
837 err = cbs_h264_read_pps(ctx, &gbc, pps);
841 err = cbs_h264_replace_pps(ctx, unit);
848 case H264_NAL_IDR_SLICE:
849 case H264_NAL_AUXILIARY_SLICE:
854 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
855 &cbs_h264_free_slice);
858 slice = unit->content;
860 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
864 if (!cbs_h2645_read_more_rbsp_data(&gbc))
865 return AVERROR_INVALIDDATA;
867 pos = get_bits_count(&gbc);
868 len = unit->data_size;
870 slice->data_size = len - pos / 8;
871 slice->data_ref = av_buffer_ref(unit->data_ref);
872 if (!slice->data_ref)
873 return AVERROR(ENOMEM);
874 slice->data = unit->data + pos / 8;
875 slice->data_bit_start = pos % 8;
881 err = ff_cbs_alloc_unit_content(ctx, unit,
882 sizeof(H264RawAUD), NULL);
886 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
894 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
899 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
905 case H264_NAL_FILLER_DATA:
907 err = ff_cbs_alloc_unit_content(ctx, unit,
908 sizeof(H264RawFiller), NULL);
912 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
918 case H264_NAL_END_SEQUENCE:
919 case H264_NAL_END_STREAM:
921 err = ff_cbs_alloc_unit_content(ctx, unit,
922 sizeof(H264RawNALUnitHeader),
927 err = (unit->type == H264_NAL_END_SEQUENCE ?
928 cbs_h264_read_end_of_sequence :
929 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
936 return AVERROR(ENOSYS);
942 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
943 CodedBitstreamUnit *unit)
948 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
952 switch (unit->type) {
957 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
963 err = cbs_h265_read_vps(ctx, &gbc, vps);
967 err = cbs_h265_replace_vps(ctx, unit);
976 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
982 err = cbs_h265_read_sps(ctx, &gbc, sps);
986 err = cbs_h265_replace_sps(ctx, unit);
996 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
1000 pps = unit->content;
1002 err = cbs_h265_read_pps(ctx, &gbc, pps);
1006 err = cbs_h265_replace_pps(ctx, unit);
1012 case HEVC_NAL_TRAIL_N:
1013 case HEVC_NAL_TRAIL_R:
1014 case HEVC_NAL_TSA_N:
1015 case HEVC_NAL_TSA_R:
1016 case HEVC_NAL_STSA_N:
1017 case HEVC_NAL_STSA_R:
1018 case HEVC_NAL_RADL_N:
1019 case HEVC_NAL_RADL_R:
1020 case HEVC_NAL_RASL_N:
1021 case HEVC_NAL_RASL_R:
1022 case HEVC_NAL_BLA_W_LP:
1023 case HEVC_NAL_BLA_W_RADL:
1024 case HEVC_NAL_BLA_N_LP:
1025 case HEVC_NAL_IDR_W_RADL:
1026 case HEVC_NAL_IDR_N_LP:
1027 case HEVC_NAL_CRA_NUT:
1029 H265RawSlice *slice;
1032 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
1033 &cbs_h265_free_slice);
1036 slice = unit->content;
1038 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1042 if (!cbs_h2645_read_more_rbsp_data(&gbc))
1043 return AVERROR_INVALIDDATA;
1045 pos = get_bits_count(&gbc);
1046 len = unit->data_size;
1048 slice->data_size = len - pos / 8;
1049 slice->data_ref = av_buffer_ref(unit->data_ref);
1050 if (!slice->data_ref)
1051 return AVERROR(ENOMEM);
1052 slice->data = unit->data + pos / 8;
1053 slice->data_bit_start = pos % 8;
1059 err = ff_cbs_alloc_unit_content(ctx, unit,
1060 sizeof(H265RawAUD), NULL);
1064 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1070 case HEVC_NAL_SEI_PREFIX:
1071 case HEVC_NAL_SEI_SUFFIX:
1073 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1074 &cbs_h265_free_sei);
1079 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1080 unit->type == HEVC_NAL_SEI_PREFIX);
1088 return AVERROR(ENOSYS);
1094 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1095 PutBitContext *pbc, const uint8_t *data,
1096 size_t data_size, int data_bit_start)
1098 size_t rest = data_size - (data_bit_start + 7) / 8;
1099 const uint8_t *pos = data + data_bit_start / 8;
1101 av_assert0(data_bit_start >= 0 &&
1102 data_size > data_bit_start / 8);
1104 if (data_size * 8 + 8 > put_bits_left(pbc))
1105 return AVERROR(ENOSPC);
1108 goto rbsp_stop_one_bit;
1110 // First copy the remaining bits of the first byte
1111 // The above check ensures that we do not accidentally
1112 // copy beyond the rbsp_stop_one_bit.
1113 if (data_bit_start % 8)
1114 put_bits(pbc, 8 - data_bit_start % 8,
1115 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1117 if (put_bits_count(pbc) % 8 == 0) {
1118 // If the writer is aligned at this point,
1119 // memcpy can be used to improve performance.
1120 // This happens normally for CABAC.
1121 flush_put_bits(pbc);
1122 memcpy(put_bits_ptr(pbc), pos, rest);
1123 skip_put_bytes(pbc, rest);
1125 // If not, we have to copy manually.
1126 // rbsp_stop_one_bit forces us to special-case
1131 for (; rest > 4; rest -= 4, pos += 4)
1132 put_bits32(pbc, AV_RB32(pos));
1134 for (; rest > 1; rest--, pos++)
1135 put_bits(pbc, 8, *pos);
1138 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1143 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1144 put_bits(pbc, i, temp);
1145 if (put_bits_count(pbc) % 8)
1146 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1152 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1153 CodedBitstreamUnit *unit,
1158 switch (unit->type) {
1161 H264RawSPS *sps = unit->content;
1163 err = cbs_h264_write_sps(ctx, pbc, sps);
1167 err = cbs_h264_replace_sps(ctx, unit);
1173 case H264_NAL_SPS_EXT:
1175 H264RawSPSExtension *sps_ext = unit->content;
1177 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1185 H264RawPPS *pps = unit->content;
1187 err = cbs_h264_write_pps(ctx, pbc, pps);
1191 err = cbs_h264_replace_pps(ctx, unit);
1197 case H264_NAL_SLICE:
1198 case H264_NAL_IDR_SLICE:
1199 case H264_NAL_AUXILIARY_SLICE:
1201 H264RawSlice *slice = unit->content;
1203 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1208 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1210 slice->data_bit_start);
1214 // No slice data - that was just the header.
1215 // (Bitstream may be unaligned!)
1222 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1230 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1236 case H264_NAL_FILLER_DATA:
1238 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1244 case H264_NAL_END_SEQUENCE:
1246 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1252 case H264_NAL_END_STREAM:
1254 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1261 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1262 "NAL unit type %"PRIu32".\n", unit->type);
1263 return AVERROR_PATCHWELCOME;
1269 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1270 CodedBitstreamUnit *unit,
1275 switch (unit->type) {
1278 H265RawVPS *vps = unit->content;
1280 err = cbs_h265_write_vps(ctx, pbc, vps);
1284 err = cbs_h265_replace_vps(ctx, unit);
1292 H265RawSPS *sps = unit->content;
1294 err = cbs_h265_write_sps(ctx, pbc, sps);
1298 err = cbs_h265_replace_sps(ctx, unit);
1306 H265RawPPS *pps = unit->content;
1308 err = cbs_h265_write_pps(ctx, pbc, pps);
1312 err = cbs_h265_replace_pps(ctx, unit);
1318 case HEVC_NAL_TRAIL_N:
1319 case HEVC_NAL_TRAIL_R:
1320 case HEVC_NAL_TSA_N:
1321 case HEVC_NAL_TSA_R:
1322 case HEVC_NAL_STSA_N:
1323 case HEVC_NAL_STSA_R:
1324 case HEVC_NAL_RADL_N:
1325 case HEVC_NAL_RADL_R:
1326 case HEVC_NAL_RASL_N:
1327 case HEVC_NAL_RASL_R:
1328 case HEVC_NAL_BLA_W_LP:
1329 case HEVC_NAL_BLA_W_RADL:
1330 case HEVC_NAL_BLA_N_LP:
1331 case HEVC_NAL_IDR_W_RADL:
1332 case HEVC_NAL_IDR_N_LP:
1333 case HEVC_NAL_CRA_NUT:
1335 H265RawSlice *slice = unit->content;
1337 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1342 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1344 slice->data_bit_start);
1348 // No slice data - that was just the header.
1355 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1361 case HEVC_NAL_SEI_PREFIX:
1362 case HEVC_NAL_SEI_SUFFIX:
1364 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1365 unit->type == HEVC_NAL_SEI_PREFIX);
1373 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1374 "NAL unit type %"PRIu32".\n", unit->type);
1375 return AVERROR_PATCHWELCOME;
1381 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1382 CodedBitstreamFragment *frag)
1385 size_t max_size, dp, sp;
1386 int err, i, zero_run;
1388 for (i = 0; i < frag->nb_units; i++) {
1389 // Data should already all have been written when we get here.
1390 av_assert0(frag->units[i].data);
1394 for (i = 0; i < frag->nb_units; i++) {
1395 // Start code + content with worst-case emulation prevention.
1396 max_size += 4 + frag->units[i].data_size * 3 / 2;
1399 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1401 return AVERROR(ENOMEM);
1404 for (i = 0; i < frag->nb_units; i++) {
1405 CodedBitstreamUnit *unit = &frag->units[i];
1407 if (unit->data_bit_padding > 0) {
1408 if (i < frag->nb_units - 1)
1409 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1410 "unaligned padding on non-final NAL unit.\n");
1412 frag->data_bit_padding = unit->data_bit_padding;
1415 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1416 (unit->type == H264_NAL_SPS ||
1417 unit->type == H264_NAL_PPS)) ||
1418 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1419 (unit->type == HEVC_NAL_VPS ||
1420 unit->type == HEVC_NAL_SPS ||
1421 unit->type == HEVC_NAL_PPS)) ||
1422 i == 0 /* (Assume this is the start of an access unit.) */) {
1426 // start_code_prefix_one_3bytes
1432 for (sp = 0; sp < unit->data_size; sp++) {
1434 if (unit->data[sp] == 0)
1439 if ((unit->data[sp] & ~3) == 0) {
1440 // emulation_prevention_three_byte
1443 zero_run = unit->data[sp] == 0;
1445 data[dp++] = unit->data[sp];
1449 av_assert0(dp <= max_size);
1450 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1453 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1455 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1457 if (!frag->data_ref) {
1459 return AVERROR(ENOMEM);
1463 frag->data_size = dp;
1468 static void cbs_h264_close(CodedBitstreamContext *ctx)
1470 CodedBitstreamH264Context *h264 = ctx->priv_data;
1473 ff_h2645_packet_uninit(&h264->common.read_packet);
1475 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1476 av_buffer_unref(&h264->sps_ref[i]);
1477 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1478 av_buffer_unref(&h264->pps_ref[i]);
1481 static void cbs_h265_close(CodedBitstreamContext *ctx)
1483 CodedBitstreamH265Context *h265 = ctx->priv_data;
1486 ff_h2645_packet_uninit(&h265->common.read_packet);
1488 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1489 av_buffer_unref(&h265->vps_ref[i]);
1490 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1491 av_buffer_unref(&h265->sps_ref[i]);
1492 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1493 av_buffer_unref(&h265->pps_ref[i]);
1496 const CodedBitstreamType ff_cbs_type_h264 = {
1497 .codec_id = AV_CODEC_ID_H264,
1499 .priv_data_size = sizeof(CodedBitstreamH264Context),
1501 .split_fragment = &cbs_h2645_split_fragment,
1502 .read_unit = &cbs_h264_read_nal_unit,
1503 .write_unit = &cbs_h264_write_nal_unit,
1504 .assemble_fragment = &cbs_h2645_assemble_fragment,
1506 .close = &cbs_h264_close,
1509 const CodedBitstreamType ff_cbs_type_h265 = {
1510 .codec_id = AV_CODEC_ID_HEVC,
1512 .priv_data_size = sizeof(CodedBitstreamH265Context),
1514 .split_fragment = &cbs_h2645_split_fragment,
1515 .read_unit = &cbs_h265_read_nal_unit,
1516 .write_unit = &cbs_h265_write_nal_unit,
1517 .assemble_fragment = &cbs_h2645_assemble_fragment,
1519 .close = &cbs_h265_close,
1522 int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
1523 CodedBitstreamFragment *au,
1524 H264RawSEIPayload *payload)
1526 H264RawSEI *sei = NULL;
1529 // Find an existing SEI NAL unit to add to.
1530 for (i = 0; i < au->nb_units; i++) {
1531 if (au->units[i].type == H264_NAL_SEI) {
1532 sei = au->units[i].content;
1533 if (sei->payload_count < H264_MAX_SEI_PAYLOADS)
1541 // Need to make a new SEI NAL unit. Insert it before the first
1542 // slice data NAL unit; if no slice data, add at the end.
1543 AVBufferRef *sei_ref;
1545 sei = av_mallocz(sizeof(*sei));
1547 err = AVERROR(ENOMEM);
1551 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1552 sei->nal_unit_header.nal_ref_idc = 0;
1554 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1555 &cbs_h264_free_sei, NULL, 0);
1558 err = AVERROR(ENOMEM);
1562 for (i = 0; i < au->nb_units; i++) {
1563 if (au->units[i].type == H264_NAL_SLICE ||
1564 au->units[i].type == H264_NAL_IDR_SLICE)
1568 err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1570 av_buffer_unref(&sei_ref);
1575 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1576 ++sei->payload_count;
1580 cbs_h264_free_sei_payload(payload);
1584 void ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
1585 CodedBitstreamFragment *au,
1586 CodedBitstreamUnit *nal,
1589 H264RawSEI *sei = nal->content;
1591 av_assert0(nal->type == H264_NAL_SEI);
1592 av_assert0(position >= 0 && position < sei->payload_count);
1594 if (position == 0 && sei->payload_count == 1) {
1595 // Deleting NAL unit entirely.
1598 for (i = 0; i < au->nb_units; i++) {
1599 if (&au->units[i] == nal)
1603 ff_cbs_delete_unit(ctx, au, i);
1605 cbs_h264_free_sei_payload(&sei->payload[position]);
1607 --sei->payload_count;
1608 memmove(sei->payload + position,
1609 sei->payload + position + 1,
1610 (sei->payload_count - position) * sizeof(*sei->payload));