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.", 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.", 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 pos = get_bits_count(&gbc);
865 len = unit->data_size;
866 if (!unit->data[len - 1]) {
868 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
869 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
870 "from slice data.\n", z);
874 slice->data_size = len - pos / 8;
875 slice->data_ref = av_buffer_ref(unit->data_ref);
876 if (!slice->data_ref)
877 return AVERROR(ENOMEM);
878 slice->data = unit->data + pos / 8;
879 slice->data_bit_start = pos % 8;
885 err = ff_cbs_alloc_unit_content(ctx, unit,
886 sizeof(H264RawAUD), NULL);
890 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
898 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
903 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
909 case H264_NAL_FILLER_DATA:
911 err = ff_cbs_alloc_unit_content(ctx, unit,
912 sizeof(H264RawFiller), NULL);
916 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
922 case H264_NAL_END_SEQUENCE:
923 case H264_NAL_END_STREAM:
925 err = ff_cbs_alloc_unit_content(ctx, unit,
926 sizeof(H264RawNALUnitHeader),
931 err = (unit->type == H264_NAL_END_SEQUENCE ?
932 cbs_h264_read_end_of_sequence :
933 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
940 return AVERROR(ENOSYS);
946 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
947 CodedBitstreamUnit *unit)
952 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
956 switch (unit->type) {
961 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
967 err = cbs_h265_read_vps(ctx, &gbc, vps);
971 err = cbs_h265_replace_vps(ctx, unit);
980 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
986 err = cbs_h265_read_sps(ctx, &gbc, sps);
990 err = cbs_h265_replace_sps(ctx, unit);
1000 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
1001 &cbs_h265_free_pps);
1004 pps = unit->content;
1006 err = cbs_h265_read_pps(ctx, &gbc, pps);
1010 err = cbs_h265_replace_pps(ctx, unit);
1016 case HEVC_NAL_TRAIL_N:
1017 case HEVC_NAL_TRAIL_R:
1018 case HEVC_NAL_TSA_N:
1019 case HEVC_NAL_TSA_R:
1020 case HEVC_NAL_STSA_N:
1021 case HEVC_NAL_STSA_R:
1022 case HEVC_NAL_RADL_N:
1023 case HEVC_NAL_RADL_R:
1024 case HEVC_NAL_RASL_N:
1025 case HEVC_NAL_RASL_R:
1026 case HEVC_NAL_BLA_W_LP:
1027 case HEVC_NAL_BLA_W_RADL:
1028 case HEVC_NAL_BLA_N_LP:
1029 case HEVC_NAL_IDR_W_RADL:
1030 case HEVC_NAL_IDR_N_LP:
1031 case HEVC_NAL_CRA_NUT:
1033 H265RawSlice *slice;
1036 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
1037 &cbs_h265_free_slice);
1040 slice = unit->content;
1042 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1046 pos = get_bits_count(&gbc);
1047 len = unit->data_size;
1048 if (!unit->data[len - 1]) {
1050 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
1051 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
1052 "from slice data.\n", z);
1056 slice->data_size = len - pos / 8;
1057 slice->data_ref = av_buffer_ref(unit->data_ref);
1058 if (!slice->data_ref)
1059 return AVERROR(ENOMEM);
1060 slice->data = unit->data + pos / 8;
1061 slice->data_bit_start = pos % 8;
1067 err = ff_cbs_alloc_unit_content(ctx, unit,
1068 sizeof(H265RawAUD), NULL);
1072 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1078 case HEVC_NAL_SEI_PREFIX:
1079 case HEVC_NAL_SEI_SUFFIX:
1081 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1082 &cbs_h265_free_sei);
1087 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1088 unit->type == HEVC_NAL_SEI_PREFIX);
1096 return AVERROR(ENOSYS);
1102 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1103 PutBitContext *pbc, const uint8_t *data,
1104 size_t data_size, int data_bit_start)
1106 size_t rest = data_size - (data_bit_start + 7) / 8;
1107 const uint8_t *pos = data + data_bit_start / 8;
1109 av_assert0(data_bit_start >= 0 &&
1110 data_size > data_bit_start / 8);
1112 if (data_size * 8 + 8 > put_bits_left(pbc))
1113 return AVERROR(ENOSPC);
1116 goto rbsp_stop_one_bit;
1118 // First copy the remaining bits of the first byte
1119 // The above check ensures that we do not accidentally
1120 // copy beyond the rbsp_stop_one_bit.
1121 if (data_bit_start % 8)
1122 put_bits(pbc, 8 - data_bit_start % 8,
1123 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1125 if (put_bits_count(pbc) % 8 == 0) {
1126 // If the writer is aligned at this point,
1127 // memcpy can be used to improve performance.
1128 // This happens normally for CABAC.
1129 flush_put_bits(pbc);
1130 memcpy(put_bits_ptr(pbc), pos, rest);
1131 skip_put_bytes(pbc, rest);
1133 // If not, we have to copy manually.
1134 // rbsp_stop_one_bit forces us to special-case
1139 for (; rest > 4; rest -= 4, pos += 4)
1140 put_bits32(pbc, AV_RB32(pos));
1142 for (; rest > 1; rest--, pos++)
1143 put_bits(pbc, 8, *pos);
1146 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1151 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1152 put_bits(pbc, i, temp);
1153 if (put_bits_count(pbc) % 8)
1154 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1160 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1161 CodedBitstreamUnit *unit,
1166 switch (unit->type) {
1169 H264RawSPS *sps = unit->content;
1171 err = cbs_h264_write_sps(ctx, pbc, sps);
1175 err = cbs_h264_replace_sps(ctx, unit);
1181 case H264_NAL_SPS_EXT:
1183 H264RawSPSExtension *sps_ext = unit->content;
1185 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1193 H264RawPPS *pps = unit->content;
1195 err = cbs_h264_write_pps(ctx, pbc, pps);
1199 err = cbs_h264_replace_pps(ctx, unit);
1205 case H264_NAL_SLICE:
1206 case H264_NAL_IDR_SLICE:
1207 case H264_NAL_AUXILIARY_SLICE:
1209 H264RawSlice *slice = unit->content;
1211 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1216 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1218 slice->data_bit_start);
1222 // No slice data - that was just the header.
1223 // (Bitstream may be unaligned!)
1230 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1238 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1244 case H264_NAL_FILLER_DATA:
1246 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1252 case H264_NAL_END_SEQUENCE:
1254 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1260 case H264_NAL_END_STREAM:
1262 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1269 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1270 "NAL unit type %"PRIu32".\n", unit->type);
1271 return AVERROR_PATCHWELCOME;
1277 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1278 CodedBitstreamUnit *unit,
1283 switch (unit->type) {
1286 H265RawVPS *vps = unit->content;
1288 err = cbs_h265_write_vps(ctx, pbc, vps);
1292 err = cbs_h265_replace_vps(ctx, unit);
1300 H265RawSPS *sps = unit->content;
1302 err = cbs_h265_write_sps(ctx, pbc, sps);
1306 err = cbs_h265_replace_sps(ctx, unit);
1314 H265RawPPS *pps = unit->content;
1316 err = cbs_h265_write_pps(ctx, pbc, pps);
1320 err = cbs_h265_replace_pps(ctx, unit);
1326 case HEVC_NAL_TRAIL_N:
1327 case HEVC_NAL_TRAIL_R:
1328 case HEVC_NAL_TSA_N:
1329 case HEVC_NAL_TSA_R:
1330 case HEVC_NAL_STSA_N:
1331 case HEVC_NAL_STSA_R:
1332 case HEVC_NAL_RADL_N:
1333 case HEVC_NAL_RADL_R:
1334 case HEVC_NAL_RASL_N:
1335 case HEVC_NAL_RASL_R:
1336 case HEVC_NAL_BLA_W_LP:
1337 case HEVC_NAL_BLA_W_RADL:
1338 case HEVC_NAL_BLA_N_LP:
1339 case HEVC_NAL_IDR_W_RADL:
1340 case HEVC_NAL_IDR_N_LP:
1341 case HEVC_NAL_CRA_NUT:
1343 H265RawSlice *slice = unit->content;
1345 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1350 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1352 slice->data_bit_start);
1356 // No slice data - that was just the header.
1363 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1369 case HEVC_NAL_SEI_PREFIX:
1370 case HEVC_NAL_SEI_SUFFIX:
1372 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1373 unit->type == HEVC_NAL_SEI_PREFIX);
1381 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1382 "NAL unit type %"PRIu32".\n", unit->type);
1383 return AVERROR_PATCHWELCOME;
1389 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1390 CodedBitstreamFragment *frag)
1393 size_t max_size, dp, sp;
1394 int err, i, zero_run;
1396 for (i = 0; i < frag->nb_units; i++) {
1397 // Data should already all have been written when we get here.
1398 av_assert0(frag->units[i].data);
1402 for (i = 0; i < frag->nb_units; i++) {
1403 // Start code + content with worst-case emulation prevention.
1404 max_size += 4 + frag->units[i].data_size * 3 / 2;
1407 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1409 return AVERROR(ENOMEM);
1412 for (i = 0; i < frag->nb_units; i++) {
1413 CodedBitstreamUnit *unit = &frag->units[i];
1415 if (unit->data_bit_padding > 0) {
1416 if (i < frag->nb_units - 1)
1417 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1418 "unaligned padding on non-final NAL unit.\n");
1420 frag->data_bit_padding = unit->data_bit_padding;
1423 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1424 (unit->type == H264_NAL_SPS ||
1425 unit->type == H264_NAL_PPS)) ||
1426 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1427 (unit->type == HEVC_NAL_VPS ||
1428 unit->type == HEVC_NAL_SPS ||
1429 unit->type == HEVC_NAL_PPS)) ||
1430 i == 0 /* (Assume this is the start of an access unit.) */) {
1434 // start_code_prefix_one_3bytes
1440 for (sp = 0; sp < unit->data_size; sp++) {
1442 if (unit->data[sp] == 0)
1447 if ((unit->data[sp] & ~3) == 0) {
1448 // emulation_prevention_three_byte
1451 zero_run = unit->data[sp] == 0;
1453 data[dp++] = unit->data[sp];
1457 av_assert0(dp <= max_size);
1458 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1461 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1463 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1465 if (!frag->data_ref) {
1467 return AVERROR(ENOMEM);
1471 frag->data_size = dp;
1476 static void cbs_h264_close(CodedBitstreamContext *ctx)
1478 CodedBitstreamH264Context *h264 = ctx->priv_data;
1481 ff_h2645_packet_uninit(&h264->common.read_packet);
1483 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1484 av_buffer_unref(&h264->sps_ref[i]);
1485 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1486 av_buffer_unref(&h264->pps_ref[i]);
1489 static void cbs_h265_close(CodedBitstreamContext *ctx)
1491 CodedBitstreamH265Context *h265 = ctx->priv_data;
1494 ff_h2645_packet_uninit(&h265->common.read_packet);
1496 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1497 av_buffer_unref(&h265->vps_ref[i]);
1498 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1499 av_buffer_unref(&h265->sps_ref[i]);
1500 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1501 av_buffer_unref(&h265->pps_ref[i]);
1504 const CodedBitstreamType ff_cbs_type_h264 = {
1505 .codec_id = AV_CODEC_ID_H264,
1507 .priv_data_size = sizeof(CodedBitstreamH264Context),
1509 .split_fragment = &cbs_h2645_split_fragment,
1510 .read_unit = &cbs_h264_read_nal_unit,
1511 .write_unit = &cbs_h264_write_nal_unit,
1512 .assemble_fragment = &cbs_h2645_assemble_fragment,
1514 .close = &cbs_h264_close,
1517 const CodedBitstreamType ff_cbs_type_h265 = {
1518 .codec_id = AV_CODEC_ID_HEVC,
1520 .priv_data_size = sizeof(CodedBitstreamH265Context),
1522 .split_fragment = &cbs_h2645_split_fragment,
1523 .read_unit = &cbs_h265_read_nal_unit,
1524 .write_unit = &cbs_h265_write_nal_unit,
1525 .assemble_fragment = &cbs_h2645_assemble_fragment,
1527 .close = &cbs_h265_close,
1530 int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
1531 CodedBitstreamFragment *au,
1532 H264RawSEIPayload *payload)
1534 H264RawSEI *sei = NULL;
1537 // Find an existing SEI NAL unit to add to.
1538 for (i = 0; i < au->nb_units; i++) {
1539 if (au->units[i].type == H264_NAL_SEI) {
1540 sei = au->units[i].content;
1541 if (sei->payload_count < H264_MAX_SEI_PAYLOADS)
1549 // Need to make a new SEI NAL unit. Insert it before the first
1550 // slice data NAL unit; if no slice data, add at the end.
1551 AVBufferRef *sei_ref;
1553 sei = av_mallocz(sizeof(*sei));
1555 err = AVERROR(ENOMEM);
1559 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1560 sei->nal_unit_header.nal_ref_idc = 0;
1562 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1563 &cbs_h264_free_sei, NULL, 0);
1566 err = AVERROR(ENOMEM);
1570 for (i = 0; i < au->nb_units; i++) {
1571 if (au->units[i].type == H264_NAL_SLICE ||
1572 au->units[i].type == H264_NAL_IDR_SLICE)
1576 err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1578 av_buffer_unref(&sei_ref);
1583 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1584 ++sei->payload_count;
1588 cbs_h264_free_sei_payload(payload);
1592 void ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
1593 CodedBitstreamFragment *au,
1594 CodedBitstreamUnit *nal,
1597 H264RawSEI *sei = nal->content;
1599 av_assert0(nal->type == H264_NAL_SEI);
1600 av_assert0(position >= 0 && position < sei->payload_count);
1602 if (position == 0 && sei->payload_count == 1) {
1603 // Deleting NAL unit entirely.
1606 for (i = 0; i < au->nb_units; i++) {
1607 if (&au->units[i] == nal)
1611 ff_cbs_delete_unit(ctx, au, i);
1613 cbs_h264_free_sei_payload(&sei->payload[position]);
1615 --sei->payload_count;
1616 memmove(sei->payload + position,
1617 sei->payload + position + 1,
1618 (sei->payload_count - position) * sizeof(*sei->payload));