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"
30 #include "h2645_parse.h"
35 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
36 const char *name, const int *subscripts,
38 uint32_t range_min, uint32_t range_max)
45 position = get_bits_count(gbc);
47 for (i = 0; i < 32; i++) {
48 if (get_bits_left(gbc) < i + 1) {
49 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
50 "%s: bitstream ended.\n", name);
51 return AVERROR_INVALIDDATA;
54 bits[i] = k ? '1' : '0';
59 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
60 "%s: more than 31 zeroes.\n", name);
61 return AVERROR_INVALIDDATA;
64 for (j = 0; j < i; j++) {
66 bits[i + j + 1] = k ? '1' : '0';
67 value = value << 1 | k;
72 if (ctx->trace_enable)
73 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
76 if (value < range_min || value > range_max) {
77 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
78 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
79 name, value, range_min, range_max);
80 return AVERROR_INVALIDDATA;
87 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
88 const char *name, const int *subscripts,
90 int32_t range_min, int32_t range_max)
98 position = get_bits_count(gbc);
100 for (i = 0; i < 32; i++) {
101 if (get_bits_left(gbc) < i + 1) {
102 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
103 "%s: bitstream ended.\n", name);
104 return AVERROR_INVALIDDATA;
107 bits[i] = k ? '1' : '0';
112 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
113 "%s: more than 31 zeroes.\n", name);
114 return AVERROR_INVALIDDATA;
117 for (j = 0; j < i; j++) {
119 bits[i + j + 1] = k ? '1' : '0';
124 value = -(int32_t)(v / 2);
128 if (ctx->trace_enable)
129 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
132 if (value < range_min || value > range_max) {
133 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
134 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
135 name, value, range_min, range_max);
136 return AVERROR_INVALIDDATA;
143 static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
144 const char *name, const int *subscripts,
146 uint32_t range_min, uint32_t range_max)
150 if (value < range_min || value > range_max) {
151 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
152 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
153 name, value, range_min, range_max);
154 return AVERROR_INVALIDDATA;
156 av_assert0(value != UINT32_MAX);
158 len = av_log2(value + 1);
159 if (put_bits_left(pbc) < 2 * len + 1)
160 return AVERROR(ENOSPC);
162 if (ctx->trace_enable) {
166 for (i = 0; i < len; i++)
169 for (i = 0; i < len; i++)
170 bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
171 bits[len + len + 1] = 0;
173 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
174 name, subscripts, bits, value);
177 put_bits(pbc, len, 0);
179 put_bits(pbc, len + 1, value + 1);
181 put_bits32(pbc, value + 1);
186 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
187 const char *name, const int *subscripts,
189 int32_t range_min, int32_t range_max)
194 if (value < range_min || value > range_max) {
195 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
196 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
197 name, value, range_min, range_max);
198 return AVERROR_INVALIDDATA;
200 av_assert0(value != INT32_MIN);
205 uvalue = 2 * (uint32_t)value - 1;
207 uvalue = 2 * (uint32_t)-value;
209 len = av_log2(uvalue + 1);
210 if (put_bits_left(pbc) < 2 * len + 1)
211 return AVERROR(ENOSPC);
213 if (ctx->trace_enable) {
217 for (i = 0; i < len; i++)
220 for (i = 0; i < len; i++)
221 bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
222 bits[len + len + 1] = 0;
224 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
225 name, subscripts, bits, value);
228 put_bits(pbc, len, 0);
230 put_bits(pbc, len + 1, uvalue + 1);
232 put_bits32(pbc, uvalue + 1);
237 #define HEADER(name) do { \
238 ff_cbs_trace_header(ctx, name); \
241 #define CHECK(call) do { \
247 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
248 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
249 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
251 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
253 #define u(width, name, range_min, range_max) \
254 xu(width, name, current->name, range_min, range_max, 0)
255 #define flag(name) u(1, name, 0, 1)
256 #define ue(name, range_min, range_max) \
257 xue(name, current->name, range_min, range_max, 0)
258 #define se(name, range_min, range_max) \
259 xse(name, current->name, range_min, range_max, 0)
261 #define us(width, name, range_min, range_max, subs, ...) \
262 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
263 #define flags(name, subs, ...) \
264 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
265 #define ues(name, range_min, range_max, subs, ...) \
266 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
267 #define ses(name, range_min, range_max, subs, ...) \
268 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
270 #define fixed(width, name, value) do { \
271 av_unused uint32_t fixed_value = value; \
272 xu(width, name, fixed_value, value, value, 0); \
277 #define READWRITE read
278 #define RWContext GetBitContext
280 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
281 uint32_t value = range_min; \
282 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
283 SUBSCRIPTS(subs, __VA_ARGS__), \
284 &value, range_min, range_max)); \
287 #define xue(name, var, range_min, range_max, subs, ...) do { \
288 uint32_t value = range_min; \
289 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
290 SUBSCRIPTS(subs, __VA_ARGS__), \
291 &value, range_min, range_max)); \
294 #define xse(name, var, range_min, range_max, subs, ...) do { \
295 int32_t value = range_min; \
296 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
297 SUBSCRIPTS(subs, __VA_ARGS__), \
298 &value, range_min, range_max)); \
303 #define infer(name, value) do { \
304 current->name = value; \
307 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
309 int bits_left = get_bits_left(gbc);
312 if (show_bits(gbc, bits_left) == 1 << (bits_left - 1))
317 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
319 #define byte_alignment(rw) (get_bits_count(rw) % 8)
321 #define allocate(name, size) do { \
322 name ## _ref = av_buffer_allocz(size + \
323 AV_INPUT_BUFFER_PADDING_SIZE); \
325 return AVERROR(ENOMEM); \
326 name = name ## _ref->data; \
329 #define FUNC(name) FUNC_H264(READWRITE, name)
330 #include "cbs_h264_syntax_template.c"
333 #define FUNC(name) FUNC_H265(READWRITE, name)
334 #include "cbs_h265_syntax_template.c"
344 #undef more_rbsp_data
345 #undef byte_alignment
350 #define READWRITE write
351 #define RWContext PutBitContext
353 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
354 uint32_t value = var; \
355 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
356 SUBSCRIPTS(subs, __VA_ARGS__), \
357 value, range_min, range_max)); \
359 #define xue(name, var, range_min, range_max, subs, ...) do { \
360 uint32_t value = var; \
361 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
362 SUBSCRIPTS(subs, __VA_ARGS__), \
363 value, range_min, range_max)); \
365 #define xse(name, var, range_min, range_max, subs, ...) do { \
366 int32_t value = var; \
367 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
368 SUBSCRIPTS(subs, __VA_ARGS__), \
369 value, range_min, range_max)); \
372 #define infer(name, value) do { \
373 if (current->name != (value)) { \
374 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
375 "%s does not match inferred value: " \
376 "%"PRId64", but should be %"PRId64".\n", \
377 #name, (int64_t)current->name, (int64_t)(value)); \
381 #define more_rbsp_data(var) (var)
383 #define byte_alignment(rw) (put_bits_count(rw) % 8)
385 #define allocate(name, size) do { \
387 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
388 "for writing.\n", #name); \
389 return AVERROR_INVALIDDATA; \
393 #define FUNC(name) FUNC_H264(READWRITE, name)
394 #include "cbs_h264_syntax_template.c"
397 #define FUNC(name) FUNC_H265(READWRITE, name)
398 #include "cbs_h265_syntax_template.c"
412 #undef more_rbsp_data
413 #undef byte_alignment
417 static void cbs_h264_free_pps(void *unit, uint8_t *content)
419 H264RawPPS *pps = (H264RawPPS*)content;
420 av_buffer_unref(&pps->slice_group_id_ref);
424 static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
426 switch (payload->payload_type) {
427 case H264_SEI_TYPE_BUFFERING_PERIOD:
428 case H264_SEI_TYPE_PIC_TIMING:
429 case H264_SEI_TYPE_PAN_SCAN_RECT:
430 case H264_SEI_TYPE_RECOVERY_POINT:
431 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
432 case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
434 case H264_SEI_TYPE_USER_DATA_REGISTERED:
435 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
437 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
438 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
441 av_buffer_unref(&payload->payload.other.data_ref);
446 static void cbs_h264_free_sei(void *unit, uint8_t *content)
448 H264RawSEI *sei = (H264RawSEI*)content;
450 for (i = 0; i < sei->payload_count; i++)
451 cbs_h264_free_sei_payload(&sei->payload[i]);
455 static void cbs_h264_free_slice(void *unit, uint8_t *content)
457 H264RawSlice *slice = (H264RawSlice*)content;
458 av_buffer_unref(&slice->data_ref);
462 static void cbs_h265_free_vps(void *unit, uint8_t *content)
464 H265RawVPS *vps = (H265RawVPS*)content;
465 av_buffer_unref(&vps->extension_data.data_ref);
469 static void cbs_h265_free_sps(void *unit, uint8_t *content)
471 H265RawSPS *sps = (H265RawSPS*)content;
472 av_buffer_unref(&sps->extension_data.data_ref);
476 static void cbs_h265_free_pps(void *unit, uint8_t *content)
478 H265RawPPS *pps = (H265RawPPS*)content;
479 av_buffer_unref(&pps->extension_data.data_ref);
483 static void cbs_h265_free_slice(void *unit, uint8_t *content)
485 H265RawSlice *slice = (H265RawSlice*)content;
486 av_buffer_unref(&slice->data_ref);
490 static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
492 switch (payload->payload_type) {
493 case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
494 case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
497 av_buffer_unref(&payload->payload.other.data_ref);
502 static void cbs_h265_free_sei(void *unit, uint8_t *content)
504 H265RawSEI *sei = (H265RawSEI*)content;
506 for (i = 0; i < sei->payload_count; i++)
507 cbs_h265_free_sei_payload(&sei->payload[i]);
511 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
512 CodedBitstreamFragment *frag,
513 const H2645Packet *packet)
517 for (i = 0; i < packet->nb_nals; i++) {
518 const H2645NAL *nal = &packet->nals[i];
519 size_t size = nal->size;
522 // Remove trailing zeroes.
523 while (size > 0 && nal->data[size - 1] == 0)
525 av_assert0(size > 0);
527 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
529 return AVERROR(ENOMEM);
530 memcpy(data, nal->data, size);
531 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
533 err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
544 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
545 CodedBitstreamFragment *frag,
548 enum AVCodecID codec_id = ctx->codec->codec_id;
549 CodedBitstreamH2645Context *priv = ctx->priv_data;
553 av_assert0(frag->data && frag->nb_units == 0);
554 if (frag->data_size == 0)
557 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
559 size_t size, start, end;
560 int i, count, version;
564 bytestream2_init(&gbc, frag->data, frag->data_size);
566 if (bytestream2_get_bytes_left(&gbc) < 6)
567 return AVERROR_INVALIDDATA;
569 version = bytestream2_get_byte(&gbc);
571 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
572 "first byte %u.", version);
573 return AVERROR_INVALIDDATA;
576 bytestream2_skip(&gbc, 3);
577 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
580 count = bytestream2_get_byte(&gbc) & 0x1f;
581 start = bytestream2_tell(&gbc);
582 for (i = 0; i < count; i++) {
583 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
584 return AVERROR_INVALIDDATA;
585 size = bytestream2_get_be16(&gbc);
586 if (bytestream2_get_bytes_left(&gbc) < size)
587 return AVERROR_INVALIDDATA;
588 bytestream2_skip(&gbc, size);
590 end = bytestream2_tell(&gbc);
592 err = ff_h2645_packet_split(&priv->read_packet,
593 frag->data + start, end - start,
594 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
596 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
599 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
604 count = bytestream2_get_byte(&gbc);
605 start = bytestream2_tell(&gbc);
606 for (i = 0; i < count; i++) {
607 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
608 return AVERROR_INVALIDDATA;
609 size = bytestream2_get_be16(&gbc);
610 if (bytestream2_get_bytes_left(&gbc) < size)
611 return AVERROR_INVALIDDATA;
612 bytestream2_skip(&gbc, size);
614 end = bytestream2_tell(&gbc);
616 err = ff_h2645_packet_split(&priv->read_packet,
617 frag->data + start, end - start,
618 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
620 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
623 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
627 if (bytestream2_get_bytes_left(&gbc) > 0) {
628 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
629 "header.\n", bytestream2_get_bytes_left(&gbc));
632 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
634 size_t size, start, end;
635 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
639 bytestream2_init(&gbc, frag->data, frag->data_size);
641 if (bytestream2_get_bytes_left(&gbc) < 23)
642 return AVERROR_INVALIDDATA;
644 version = bytestream2_get_byte(&gbc);
646 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
647 "first byte %u.", version);
648 return AVERROR_INVALIDDATA;
651 bytestream2_skip(&gbc, 20);
652 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
654 nb_arrays = bytestream2_get_byte(&gbc);
655 for (i = 0; i < nb_arrays; i++) {
656 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
657 nb_nals = bytestream2_get_be16(&gbc);
659 start = bytestream2_tell(&gbc);
660 for (j = 0; j < nb_nals; j++) {
661 if (bytestream2_get_bytes_left(&gbc) < 2)
662 return AVERROR_INVALIDDATA;
663 size = bytestream2_get_be16(&gbc);
664 if (bytestream2_get_bytes_left(&gbc) < size)
665 return AVERROR_INVALIDDATA;
666 bytestream2_skip(&gbc, size);
668 end = bytestream2_tell(&gbc);
670 err = ff_h2645_packet_split(&priv->read_packet,
671 frag->data + start, end - start,
672 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
674 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
675 "HVCC array %d (%d NAL units of type %d).\n",
676 i, nb_nals, nal_unit_type);
679 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
685 // Annex B, or later MP4 with already-known parameters.
687 err = ff_h2645_packet_split(&priv->read_packet,
688 frag->data, frag->data_size,
690 priv->mp4, priv->nal_length_size,
695 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
703 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
704 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
705 CodedBitstreamUnit *unit) \
707 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
708 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
709 unsigned int id = ps_var->id_element; \
710 if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
711 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
712 " id : %d.\n", id); \
713 return AVERROR_INVALIDDATA; \
715 if (priv->ps_var[id] == priv->active_ ## ps_var) \
716 priv->active_ ## ps_var = NULL ; \
717 av_buffer_unref(&priv->ps_var ## _ref[id]); \
718 if (unit->content_ref) \
719 priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
721 priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
722 if (!priv->ps_var ## _ref[id]) \
723 return AVERROR(ENOMEM); \
724 priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
725 if (!unit->content_ref) \
726 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
730 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
731 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
732 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
733 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
734 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
736 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
737 CodedBitstreamUnit *unit)
742 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
746 switch (unit->type) {
751 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
756 err = cbs_h264_read_sps(ctx, &gbc, sps);
760 err = cbs_h264_replace_sps(ctx, unit);
766 case H264_NAL_SPS_EXT:
768 err = ff_cbs_alloc_unit_content(ctx, unit,
769 sizeof(H264RawSPSExtension),
774 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
784 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
790 err = cbs_h264_read_pps(ctx, &gbc, pps);
794 err = cbs_h264_replace_pps(ctx, unit);
801 case H264_NAL_IDR_SLICE:
802 case H264_NAL_AUXILIARY_SLICE:
807 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
808 &cbs_h264_free_slice);
811 slice = unit->content;
813 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
817 pos = get_bits_count(&gbc);
818 len = unit->data_size;
819 if (!unit->data[len - 1]) {
821 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
822 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
823 "from slice data.\n", z);
827 slice->data_size = len - pos / 8;
828 slice->data_ref = av_buffer_ref(unit->data_ref);
829 if (!slice->data_ref)
830 return AVERROR(ENOMEM);
831 slice->data = unit->data + pos / 8;
832 slice->data_bit_start = pos % 8;
838 err = ff_cbs_alloc_unit_content(ctx, unit,
839 sizeof(H264RawAUD), NULL);
843 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
851 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
856 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
862 case H264_NAL_FILLER_DATA:
864 err = ff_cbs_alloc_unit_content(ctx, unit,
865 sizeof(H264RawFiller), NULL);
869 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
875 case H264_NAL_END_SEQUENCE:
876 case H264_NAL_END_STREAM:
878 err = ff_cbs_alloc_unit_content(ctx, unit,
879 sizeof(H264RawNALUnitHeader),
884 err = (unit->type == H264_NAL_END_SEQUENCE ?
885 cbs_h264_read_end_of_sequence :
886 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
893 return AVERROR(ENOSYS);
899 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
900 CodedBitstreamUnit *unit)
905 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
909 switch (unit->type) {
914 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
920 err = cbs_h265_read_vps(ctx, &gbc, vps);
924 err = cbs_h265_replace_vps(ctx, unit);
933 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
939 err = cbs_h265_read_sps(ctx, &gbc, sps);
943 err = cbs_h265_replace_sps(ctx, unit);
953 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
959 err = cbs_h265_read_pps(ctx, &gbc, pps);
963 err = cbs_h265_replace_pps(ctx, unit);
969 case HEVC_NAL_TRAIL_N:
970 case HEVC_NAL_TRAIL_R:
973 case HEVC_NAL_STSA_N:
974 case HEVC_NAL_STSA_R:
975 case HEVC_NAL_RADL_N:
976 case HEVC_NAL_RADL_R:
977 case HEVC_NAL_RASL_N:
978 case HEVC_NAL_RASL_R:
979 case HEVC_NAL_BLA_W_LP:
980 case HEVC_NAL_BLA_W_RADL:
981 case HEVC_NAL_BLA_N_LP:
982 case HEVC_NAL_IDR_W_RADL:
983 case HEVC_NAL_IDR_N_LP:
984 case HEVC_NAL_CRA_NUT:
989 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
990 &cbs_h265_free_slice);
993 slice = unit->content;
995 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
999 pos = get_bits_count(&gbc);
1000 len = unit->data_size;
1001 if (!unit->data[len - 1]) {
1003 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
1004 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
1005 "from slice data.\n", z);
1009 slice->data_size = len - pos / 8;
1010 slice->data_ref = av_buffer_ref(unit->data_ref);
1011 if (!slice->data_ref)
1012 return AVERROR(ENOMEM);
1013 slice->data = unit->data + pos / 8;
1014 slice->data_bit_start = pos % 8;
1020 err = ff_cbs_alloc_unit_content(ctx, unit,
1021 sizeof(H265RawAUD), NULL);
1025 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1031 case HEVC_NAL_SEI_PREFIX:
1033 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1034 &cbs_h265_free_sei);
1039 err = cbs_h265_read_sei(ctx, &gbc, unit->content);
1047 return AVERROR(ENOSYS);
1053 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1054 PutBitContext *pbc, const uint8_t *data,
1055 size_t data_size, int data_bit_start)
1057 size_t rest = data_size - (data_bit_start + 7) / 8;
1058 const uint8_t *pos = data + data_bit_start / 8;
1060 av_assert0(data_bit_start >= 0 &&
1061 8 * data_size > data_bit_start);
1063 if (data_size * 8 + 8 > put_bits_left(pbc))
1064 return AVERROR(ENOSPC);
1067 goto rbsp_stop_one_bit;
1069 // First copy the remaining bits of the first byte
1070 // The above check ensures that we do not accidentally
1071 // copy beyond the rbsp_stop_one_bit.
1072 if (data_bit_start % 8)
1073 put_bits(pbc, 8 - data_bit_start % 8,
1074 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1076 if (put_bits_count(pbc) % 8 == 0) {
1077 // If the writer is aligned at this point,
1078 // memcpy can be used to improve performance.
1079 // This happens normally for CABAC.
1080 flush_put_bits(pbc);
1081 memcpy(put_bits_ptr(pbc), pos, rest);
1082 skip_put_bytes(pbc, rest);
1084 // If not, we have to copy manually.
1085 // rbsp_stop_one_bit forces us to special-case
1090 for (; rest > 4; rest -= 4, pos += 4)
1091 put_bits32(pbc, AV_RB32(pos));
1093 for (; rest > 1; rest--, pos++)
1094 put_bits(pbc, 8, *pos);
1097 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1102 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1103 put_bits(pbc, i, temp);
1104 if (put_bits_count(pbc) % 8)
1105 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1111 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1112 CodedBitstreamUnit *unit,
1117 switch (unit->type) {
1120 H264RawSPS *sps = unit->content;
1122 err = cbs_h264_write_sps(ctx, pbc, sps);
1126 err = cbs_h264_replace_sps(ctx, unit);
1132 case H264_NAL_SPS_EXT:
1134 H264RawSPSExtension *sps_ext = unit->content;
1136 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1144 H264RawPPS *pps = unit->content;
1146 err = cbs_h264_write_pps(ctx, pbc, pps);
1150 err = cbs_h264_replace_pps(ctx, unit);
1156 case H264_NAL_SLICE:
1157 case H264_NAL_IDR_SLICE:
1158 case H264_NAL_AUXILIARY_SLICE:
1160 H264RawSlice *slice = unit->content;
1162 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1167 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1169 slice->data_bit_start);
1173 // No slice data - that was just the header.
1174 // (Bitstream may be unaligned!)
1181 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1189 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1195 case H264_NAL_FILLER_DATA:
1197 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1203 case H264_NAL_END_SEQUENCE:
1205 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1211 case H264_NAL_END_STREAM:
1213 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1220 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1221 "NAL unit type %"PRIu32".\n", unit->type);
1222 return AVERROR_PATCHWELCOME;
1228 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1229 CodedBitstreamUnit *unit,
1234 switch (unit->type) {
1237 H265RawVPS *vps = unit->content;
1239 err = cbs_h265_write_vps(ctx, pbc, vps);
1243 err = cbs_h265_replace_vps(ctx, unit);
1251 H265RawSPS *sps = unit->content;
1253 err = cbs_h265_write_sps(ctx, pbc, sps);
1257 err = cbs_h265_replace_sps(ctx, unit);
1265 H265RawPPS *pps = unit->content;
1267 err = cbs_h265_write_pps(ctx, pbc, pps);
1271 err = cbs_h265_replace_pps(ctx, unit);
1277 case HEVC_NAL_TRAIL_N:
1278 case HEVC_NAL_TRAIL_R:
1279 case HEVC_NAL_TSA_N:
1280 case HEVC_NAL_TSA_R:
1281 case HEVC_NAL_STSA_N:
1282 case HEVC_NAL_STSA_R:
1283 case HEVC_NAL_RADL_N:
1284 case HEVC_NAL_RADL_R:
1285 case HEVC_NAL_RASL_N:
1286 case HEVC_NAL_RASL_R:
1287 case HEVC_NAL_BLA_W_LP:
1288 case HEVC_NAL_BLA_W_RADL:
1289 case HEVC_NAL_BLA_N_LP:
1290 case HEVC_NAL_IDR_W_RADL:
1291 case HEVC_NAL_IDR_N_LP:
1292 case HEVC_NAL_CRA_NUT:
1294 H265RawSlice *slice = unit->content;
1296 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1301 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1303 slice->data_bit_start);
1307 // No slice data - that was just the header.
1314 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1320 case HEVC_NAL_SEI_PREFIX:
1322 err = cbs_h265_write_sei(ctx, pbc, unit->content);
1330 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1331 "NAL unit type %"PRIu32".\n", unit->type);
1332 return AVERROR_PATCHWELCOME;
1338 static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
1339 CodedBitstreamUnit *unit)
1341 CodedBitstreamH2645Context *priv = ctx->priv_data;
1342 enum AVCodecID codec_id = ctx->codec->codec_id;
1346 if (!priv->write_buffer) {
1347 // Initial write buffer size is 1MB.
1348 priv->write_buffer_size = 1024 * 1024;
1350 reallocate_and_try_again:
1351 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1353 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1354 "sufficiently large write buffer (last attempt "
1355 "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1360 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1362 if (codec_id == AV_CODEC_ID_H264)
1363 err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1365 err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1367 if (err == AVERROR(ENOSPC)) {
1369 priv->write_buffer_size *= 2;
1370 goto reallocate_and_try_again;
1372 // Overflow but we didn't notice.
1373 av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1376 // Write failed for some other reason.
1380 if (put_bits_count(&pbc) % 8)
1381 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1383 unit->data_bit_padding = 0;
1385 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1386 flush_put_bits(&pbc);
1388 err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1392 memcpy(unit->data, priv->write_buffer, unit->data_size);
1397 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1398 CodedBitstreamFragment *frag)
1401 size_t max_size, dp, sp;
1402 int err, i, zero_run;
1404 for (i = 0; i < frag->nb_units; i++) {
1405 // Data should already all have been written when we get here.
1406 av_assert0(frag->units[i].data);
1410 for (i = 0; i < frag->nb_units; i++) {
1411 // Start code + content with worst-case emulation prevention.
1412 max_size += 3 + frag->units[i].data_size * 3 / 2;
1415 data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1417 return AVERROR(ENOMEM);
1420 for (i = 0; i < frag->nb_units; i++) {
1421 CodedBitstreamUnit *unit = &frag->units[i];
1423 if (unit->data_bit_padding > 0) {
1424 if (i < frag->nb_units - 1)
1425 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1426 "unaligned padding on non-final NAL unit.\n");
1428 frag->data_bit_padding = unit->data_bit_padding;
1431 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1432 (unit->type == H264_NAL_SPS ||
1433 unit->type == H264_NAL_PPS)) ||
1434 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1435 (unit->type == HEVC_NAL_VPS ||
1436 unit->type == HEVC_NAL_SPS ||
1437 unit->type == HEVC_NAL_PPS)) ||
1438 i == 0 /* (Assume this is the start of an access unit.) */) {
1442 // start_code_prefix_one_3bytes
1448 for (sp = 0; sp < unit->data_size; sp++) {
1450 if (unit->data[sp] == 0)
1455 if ((unit->data[sp] & ~3) == 0) {
1456 // emulation_prevention_three_byte
1459 zero_run = unit->data[sp] == 0;
1461 data[dp++] = unit->data[sp];
1465 av_assert0(dp <= max_size);
1466 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1469 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1471 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1473 if (!frag->data_ref) {
1475 return AVERROR(ENOMEM);
1479 frag->data_size = dp;
1484 static void cbs_h264_close(CodedBitstreamContext *ctx)
1486 CodedBitstreamH264Context *h264 = ctx->priv_data;
1489 ff_h2645_packet_uninit(&h264->common.read_packet);
1491 av_freep(&h264->common.write_buffer);
1493 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1494 av_buffer_unref(&h264->sps_ref[i]);
1495 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1496 av_buffer_unref(&h264->pps_ref[i]);
1499 static void cbs_h265_close(CodedBitstreamContext *ctx)
1501 CodedBitstreamH265Context *h265 = ctx->priv_data;
1504 ff_h2645_packet_uninit(&h265->common.read_packet);
1506 av_freep(&h265->common.write_buffer);
1508 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1509 av_buffer_unref(&h265->vps_ref[i]);
1510 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1511 av_buffer_unref(&h265->sps_ref[i]);
1512 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1513 av_buffer_unref(&h265->pps_ref[i]);
1516 const CodedBitstreamType ff_cbs_type_h264 = {
1517 .codec_id = AV_CODEC_ID_H264,
1519 .priv_data_size = sizeof(CodedBitstreamH264Context),
1521 .split_fragment = &cbs_h2645_split_fragment,
1522 .read_unit = &cbs_h264_read_nal_unit,
1523 .write_unit = &cbs_h2645_write_nal_unit,
1524 .assemble_fragment = &cbs_h2645_assemble_fragment,
1526 .close = &cbs_h264_close,
1529 const CodedBitstreamType ff_cbs_type_h265 = {
1530 .codec_id = AV_CODEC_ID_HEVC,
1532 .priv_data_size = sizeof(CodedBitstreamH265Context),
1534 .split_fragment = &cbs_h2645_split_fragment,
1535 .read_unit = &cbs_h265_read_nal_unit,
1536 .write_unit = &cbs_h2645_write_nal_unit,
1537 .assemble_fragment = &cbs_h2645_assemble_fragment,
1539 .close = &cbs_h265_close,
1542 int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
1543 CodedBitstreamFragment *au,
1544 const H264RawSEIPayload *payload)
1547 CodedBitstreamUnit *nal = NULL;
1550 // Find an existing SEI NAL unit to add to.
1551 for (i = 0; i < au->nb_units; i++) {
1552 if (au->units[i].type == H264_NAL_SEI) {
1553 nal = &au->units[i];
1561 // Need to make a new SEI NAL unit. Insert it before the first
1562 // slice data NAL unit; if no slice data, add at the end.
1563 AVBufferRef *sei_ref;
1565 sei = av_mallocz(sizeof(*sei));
1567 return AVERROR(ENOMEM);
1569 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1570 sei->nal_unit_header.nal_ref_idc = 0;
1572 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1573 &cbs_h264_free_sei, ctx, 0);
1576 return AVERROR(ENOMEM);
1579 for (i = 0; i < au->nb_units; i++) {
1580 if (au->units[i].type == H264_NAL_SLICE ||
1581 au->units[i].type == H264_NAL_IDR_SLICE)
1585 err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1587 av_buffer_unref(&sei_ref);
1592 if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1593 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1595 return AVERROR(EINVAL);
1598 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1599 ++sei->payload_count;
1604 int ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
1605 CodedBitstreamFragment *au,
1606 CodedBitstreamUnit *nal,
1609 H264RawSEI *sei = nal->content;
1611 av_assert0(nal->type == H264_NAL_SEI);
1612 av_assert0(position >= 0 && position < sei->payload_count);
1614 if (position == 0 && sei->payload_count == 1) {
1615 // Deleting NAL unit entirely.
1618 for (i = 0; i < au->nb_units; i++) {
1619 if (&au->units[i] == nal)
1622 av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1624 return ff_cbs_delete_unit(ctx, au, i);
1626 cbs_h264_free_sei_payload(&sei->payload[position]);
1628 --sei->payload_count;
1629 memmove(sei->payload + position,
1630 sei->payload + position + 1,
1631 (sei->payload_count - position) * sizeof(*sei->payload));