2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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"
34 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, BitstreamContext *bc,
35 const char *name, uint32_t *write_to,
36 uint32_t range_min, uint32_t range_max)
43 position = bitstream_tell(bc);
45 for (i = 0; i < 32; i++) {
46 if (bitstream_bits_left(bc) < i + 1) {
47 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
48 "%s: bitstream ended.\n", name);
49 return AVERROR_INVALIDDATA;
51 k = bitstream_read_bit(bc);
52 bits[i] = k ? '1' : '0';
57 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
58 "%s: more than 31 zeroes.\n", name);
59 return AVERROR_INVALIDDATA;
62 for (j = 0; j < i; j++) {
63 k = bitstream_read_bit(bc);
64 bits[i + j + 1] = k ? '1' : '0';
65 value = value << 1 | k;
70 if (ctx->trace_enable)
71 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
73 if (value < range_min || value > range_max) {
74 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
75 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
76 name, value, range_min, range_max);
77 return AVERROR_INVALIDDATA;
84 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, BitstreamContext *bc,
85 const char *name, int32_t *write_to,
86 int32_t range_min, int32_t range_max)
94 position = bitstream_tell(bc);
96 for (i = 0; i < 32; i++) {
97 if (bitstream_bits_left(bc) < i + 1) {
98 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
99 "%s: bitstream ended.\n", name);
100 return AVERROR_INVALIDDATA;
102 k = bitstream_read_bit(bc);
103 bits[i] = k ? '1' : '0';
108 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
109 "%s: more than 31 zeroes.\n", name);
110 return AVERROR_INVALIDDATA;
113 for (j = 0; j < i; j++) {
114 k = bitstream_read_bit(bc);
115 bits[i + j + 1] = k ? '1' : '0';
120 value = -(int32_t)(v / 2);
124 if (ctx->trace_enable)
125 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
127 if (value < range_min || value > range_max) {
128 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
129 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
130 name, value, range_min, range_max);
131 return AVERROR_INVALIDDATA;
138 static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
139 const char *name, uint32_t value,
140 uint32_t range_min, uint32_t range_max)
144 if (value < range_min || value > range_max) {
145 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
146 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
147 name, value, range_min, range_max);
148 return AVERROR_INVALIDDATA;
150 av_assert0(value != UINT32_MAX);
152 len = av_log2(value + 1);
153 if (put_bits_left(pbc) < 2 * len + 1)
154 return AVERROR(ENOSPC);
156 if (ctx->trace_enable) {
160 for (i = 0; i < len; i++)
163 for (i = 0; i < len; i++)
164 bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
165 bits[len + len + 1] = 0;
167 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
170 put_bits(pbc, len, 0);
172 put_bits(pbc, len + 1, value + 1);
174 put_bits32(pbc, value + 1);
179 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
180 const char *name, int32_t value,
181 int32_t range_min, int32_t range_max)
186 if (value < range_min || value > range_max) {
187 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
188 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
189 name, value, range_min, range_max);
190 return AVERROR_INVALIDDATA;
192 av_assert0(value != INT32_MIN);
197 uvalue = 2 * (uint32_t)value - 1;
199 uvalue = 2 * (uint32_t)-value;
201 len = av_log2(uvalue + 1);
202 if (put_bits_left(pbc) < 2 * len + 1)
203 return AVERROR(ENOSPC);
205 if (ctx->trace_enable) {
209 for (i = 0; i < len; i++)
212 for (i = 0; i < len; i++)
213 bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
214 bits[len + len + 1] = 0;
216 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
219 put_bits(pbc, len, 0);
221 put_bits(pbc, len + 1, uvalue + 1);
223 put_bits32(pbc, uvalue + 1);
228 #define HEADER(name) do { \
229 ff_cbs_trace_header(ctx, name); \
232 #define CHECK(call) do { \
238 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
239 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
240 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
244 #define READWRITE read
245 #define RWContext BitstreamContext
247 #define xu(width, name, var, range_min, range_max) do { \
248 uint32_t value = range_min; \
249 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
250 &value, range_min, range_max)); \
253 #define xue(name, var, range_min, range_max) do { \
254 uint32_t value = range_min; \
255 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
256 &value, range_min, range_max)); \
259 #define xse(name, var, range_min, range_max) do { \
260 int32_t value = range_min; \
261 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
262 &value, range_min, range_max)); \
267 #define u(width, name, range_min, range_max) \
268 xu(width, name, current->name, range_min, range_max)
269 #define flag(name) u(1, name, 0, 1)
270 #define ue(name, range_min, range_max) \
271 xue(name, current->name, range_min, range_max)
272 #define se(name, range_min, range_max) \
273 xse(name, current->name, range_min, range_max)
275 #define infer(name, value) do { \
276 current->name = value; \
279 static int cbs_h2645_read_more_rbsp_data(BitstreamContext *bc)
281 int bits_left = bitstream_bits_left(bc);
284 if (bitstream_peek(bc, bits_left) == 1 << (bits_left - 1))
289 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
291 #define byte_alignment(rw) (bitstream_tell(rw) % 8)
293 #define allocate(name, size) do { \
294 name ## _ref = av_buffer_allocz(size); \
296 return AVERROR(ENOMEM); \
297 name = name ## _ref->data; \
300 #define FUNC(name) FUNC_H264(READWRITE, name)
301 #include "cbs_h264_syntax_template.c"
304 #define FUNC(name) FUNC_H265(READWRITE, name)
305 #include "cbs_h265_syntax_template.c"
319 #undef more_rbsp_data
320 #undef byte_alignment
325 #define READWRITE write
326 #define RWContext PutBitContext
328 #define xu(width, name, var, range_min, range_max) do { \
329 uint32_t value = var; \
330 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
331 value, range_min, range_max)); \
333 #define xue(name, var, range_min, range_max) do { \
334 uint32_t value = var; \
335 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
336 value, range_min, range_max)); \
338 #define xse(name, var, range_min, range_max) do { \
339 int32_t value = var; \
340 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
341 value, range_min, range_max)); \
344 #define u(width, name, range_min, range_max) \
345 xu(width, name, current->name, range_min, range_max)
346 #define flag(name) u(1, name, 0, 1)
347 #define ue(name, range_min, range_max) \
348 xue(name, current->name, range_min, range_max)
349 #define se(name, range_min, range_max) \
350 xse(name, current->name, range_min, range_max)
352 #define infer(name, value) do { \
353 if (current->name != (value)) { \
354 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
355 "%s does not match inferred value: " \
356 "%"PRId64", but should be %"PRId64".\n", \
357 #name, (int64_t)current->name, (int64_t)(value)); \
361 #define more_rbsp_data(var) (var)
363 #define byte_alignment(rw) (put_bits_count(rw) % 8)
365 #define allocate(name, size) do { \
367 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
368 "for writing.\n", #name); \
369 return AVERROR_INVALIDDATA; \
373 #define FUNC(name) FUNC_H264(READWRITE, name)
374 #include "cbs_h264_syntax_template.c"
377 #define FUNC(name) FUNC_H265(READWRITE, name)
378 #include "cbs_h265_syntax_template.c"
392 #undef more_rbsp_data
393 #undef byte_alignment
397 static void cbs_h264_free_pps(void *unit, uint8_t *content)
399 H264RawPPS *pps = (H264RawPPS*)content;
400 av_buffer_unref(&pps->slice_group_id_ref);
404 static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
406 switch (payload->payload_type) {
407 case H264_SEI_TYPE_BUFFERING_PERIOD:
408 case H264_SEI_TYPE_PIC_TIMING:
409 case H264_SEI_TYPE_RECOVERY_POINT:
410 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
412 case H264_SEI_TYPE_USER_DATA_REGISTERED:
413 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
415 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
416 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
419 av_buffer_unref(&payload->payload.other.data_ref);
424 static void cbs_h264_free_sei(void *unit, uint8_t *content)
426 H264RawSEI *sei = (H264RawSEI*)content;
428 for (i = 0; i < sei->payload_count; i++)
429 cbs_h264_free_sei_payload(&sei->payload[i]);
433 static void cbs_h264_free_slice(void *unit, uint8_t *content)
435 H264RawSlice *slice = (H264RawSlice*)content;
436 av_buffer_unref(&slice->data_ref);
440 static void cbs_h265_free_vps(void *unit, uint8_t *content)
442 H265RawVPS *vps = (H265RawVPS*)content;
443 av_buffer_unref(&vps->extension_data.data_ref);
447 static void cbs_h265_free_sps(void *unit, uint8_t *content)
449 H265RawSPS *sps = (H265RawSPS*)content;
450 av_buffer_unref(&sps->extension_data.data_ref);
454 static void cbs_h265_free_pps(void *unit, uint8_t *content)
456 H265RawPPS *pps = (H265RawPPS*)content;
457 av_buffer_unref(&pps->extension_data.data_ref);
461 static void cbs_h265_free_slice(void *unit, uint8_t *content)
463 H265RawSlice *slice = (H265RawSlice*)content;
464 av_buffer_unref(&slice->data_ref);
468 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
469 CodedBitstreamFragment *frag,
470 const H2645Packet *packet)
474 for (i = 0; i < packet->nb_nals; i++) {
475 const H2645NAL *nal = &packet->nals[i];
476 size_t size = nal->size;
479 // Remove trailing zeroes.
480 while (size > 0 && nal->data[size - 1] == 0)
482 av_assert0(size > 0);
484 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
486 return AVERROR(ENOMEM);
487 memcpy(data, nal->data, size);
488 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
490 err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
491 data, nal->size, NULL);
501 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
502 CodedBitstreamFragment *frag,
505 enum AVCodecID codec_id = ctx->codec->codec_id;
506 CodedBitstreamH2645Context *priv = ctx->priv_data;
510 av_assert0(frag->data && frag->nb_units == 0);
511 if (frag->data_size == 0)
514 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
516 size_t size, start, end;
517 int i, count, version;
521 bytestream2_init(&gbc, frag->data, frag->data_size);
523 if (bytestream2_get_bytes_left(&gbc) < 6)
524 return AVERROR_INVALIDDATA;
526 version = bytestream2_get_byte(&gbc);
528 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
529 "first byte %u.", version);
530 return AVERROR_INVALIDDATA;
533 bytestream2_skip(&gbc, 3);
534 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
537 count = bytestream2_get_byte(&gbc) & 0x1f;
538 start = bytestream2_tell(&gbc);
539 for (i = 0; i < count; i++) {
540 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
541 return AVERROR_INVALIDDATA;
542 size = bytestream2_get_be16(&gbc);
543 if (bytestream2_get_bytes_left(&gbc) < size)
544 return AVERROR_INVALIDDATA;
545 bytestream2_skip(&gbc, size);
547 end = bytestream2_tell(&gbc);
549 err = ff_h2645_packet_split(&priv->read_packet,
550 frag->data + start, end - start,
551 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264);
553 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
556 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
561 count = bytestream2_get_byte(&gbc);
562 start = bytestream2_tell(&gbc);
563 for (i = 0; i < count; i++) {
564 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
565 return AVERROR_INVALIDDATA;
566 size = bytestream2_get_be16(&gbc);
567 if (bytestream2_get_bytes_left(&gbc) < size)
568 return AVERROR_INVALIDDATA;
569 bytestream2_skip(&gbc, size);
571 end = bytestream2_tell(&gbc);
573 err = ff_h2645_packet_split(&priv->read_packet,
574 frag->data + start, end - start,
575 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264);
577 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
580 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
584 if (bytestream2_get_bytes_left(&gbc) > 0) {
585 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
586 "header.\n", bytestream2_get_bytes_left(&gbc));
589 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
591 size_t size, start, end;
592 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
596 bytestream2_init(&gbc, frag->data, frag->data_size);
598 if (bytestream2_get_bytes_left(&gbc) < 23)
599 return AVERROR_INVALIDDATA;
601 version = bytestream2_get_byte(&gbc);
603 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
604 "first byte %u.", version);
605 return AVERROR_INVALIDDATA;
608 bytestream2_skip(&gbc, 20);
609 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
611 nb_arrays = bytestream2_get_byte(&gbc);
612 for (i = 0; i < nb_arrays; i++) {
613 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
614 nb_nals = bytestream2_get_be16(&gbc);
616 start = bytestream2_tell(&gbc);
617 for (j = 0; j < nb_nals; j++) {
618 if (bytestream2_get_bytes_left(&gbc) < 2)
619 return AVERROR_INVALIDDATA;
620 size = bytestream2_get_be16(&gbc);
621 if (bytestream2_get_bytes_left(&gbc) < size)
622 return AVERROR_INVALIDDATA;
623 bytestream2_skip(&gbc, size);
625 end = bytestream2_tell(&gbc);
627 err = ff_h2645_packet_split(&priv->read_packet,
628 frag->data + start, end - start,
629 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC);
631 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
632 "HVCC array %d (%d NAL units of type %d).\n",
633 i, nb_nals, nal_unit_type);
636 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
642 // Annex B, or later MP4 with already-known parameters.
644 err = ff_h2645_packet_split(&priv->read_packet,
645 frag->data, frag->data_size,
647 priv->mp4, priv->nal_length_size,
652 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
660 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
661 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
662 const H26 ## h26n ## Raw ## ps_name *ps_var) \
664 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
665 unsigned int id = ps_var->id_element; \
666 if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
667 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
668 " id : %d.\n", id); \
669 return AVERROR_INVALIDDATA; \
671 if (priv->ps_var[id] == priv->active_ ## ps_var) \
672 priv->active_ ## ps_var = NULL ; \
673 av_freep(&priv->ps_var[id]); \
674 priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
675 if (!priv->ps_var[id]) \
676 return AVERROR(ENOMEM); \
677 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
681 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
682 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
683 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
684 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
685 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
687 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
688 CodedBitstreamUnit *unit)
693 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
697 switch (unit->type) {
702 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
707 err = cbs_h264_read_sps(ctx, &bc, sps);
711 err = cbs_h264_replace_sps(ctx, sps);
717 case H264_NAL_SPS_EXT:
719 err = ff_cbs_alloc_unit_content(ctx, unit,
720 sizeof(H264RawSPSExtension),
725 err = cbs_h264_read_sps_extension(ctx, &bc, unit->content);
735 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
741 err = cbs_h264_read_pps(ctx, &bc, pps);
745 err = cbs_h264_replace_pps(ctx, pps);
752 case H264_NAL_IDR_SLICE:
753 case H264_NAL_AUXILIARY_SLICE:
758 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
759 &cbs_h264_free_slice);
762 slice = unit->content;
764 err = cbs_h264_read_slice_header(ctx, &bc, &slice->header);
768 pos = bitstream_tell(&bc);
769 len = unit->data_size;
770 if (!unit->data[len - 1]) {
772 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
773 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
774 "from slice data.\n", z);
778 slice->data_size = len - pos / 8;
779 slice->data_ref = av_buffer_alloc(slice->data_size +
780 AV_INPUT_BUFFER_PADDING_SIZE);
781 if (!slice->data_ref)
782 return AVERROR(ENOMEM);
783 slice->data = slice->data_ref->data;
785 unit->data + pos / 8, slice->data_size);
786 memset(slice->data + slice->data_size, 0,
787 AV_INPUT_BUFFER_PADDING_SIZE);
788 slice->data_bit_start = pos % 8;
794 err = ff_cbs_alloc_unit_content(ctx, unit,
795 sizeof(H264RawAUD), NULL);
799 err = cbs_h264_read_aud(ctx, &bc, unit->content);
807 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
812 err = cbs_h264_read_sei(ctx, &bc, unit->content);
819 return AVERROR(ENOSYS);
825 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
826 CodedBitstreamUnit *unit)
831 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
835 switch (unit->type) {
840 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
846 err = cbs_h265_read_vps(ctx, &bc, vps);
850 err = cbs_h265_replace_vps(ctx, vps);
859 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
865 err = cbs_h265_read_sps(ctx, &bc, sps);
869 err = cbs_h265_replace_sps(ctx, sps);
879 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
885 err = cbs_h265_read_pps(ctx, &bc, pps);
889 err = cbs_h265_replace_pps(ctx, pps);
895 case HEVC_NAL_TRAIL_N:
896 case HEVC_NAL_TRAIL_R:
899 case HEVC_NAL_STSA_N:
900 case HEVC_NAL_STSA_R:
901 case HEVC_NAL_RADL_N:
902 case HEVC_NAL_RADL_R:
903 case HEVC_NAL_RASL_N:
904 case HEVC_NAL_RASL_R:
905 case HEVC_NAL_BLA_W_LP:
906 case HEVC_NAL_BLA_W_RADL:
907 case HEVC_NAL_BLA_N_LP:
908 case HEVC_NAL_IDR_W_RADL:
909 case HEVC_NAL_IDR_N_LP:
910 case HEVC_NAL_CRA_NUT:
915 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
916 &cbs_h265_free_slice);
919 slice = unit->content;
921 err = cbs_h265_read_slice_segment_header(ctx, &bc, &slice->header);
925 pos = bitstream_tell(&bc);
926 len = unit->data_size;
927 if (!unit->data[len - 1]) {
929 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
930 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
931 "from slice data.\n", z);
935 slice->data_size = len - pos / 8;
936 slice->data_ref = av_buffer_alloc(slice->data_size +
937 AV_INPUT_BUFFER_PADDING_SIZE);
938 if (!slice->data_ref)
939 return AVERROR(ENOMEM);
940 slice->data = slice->data_ref->data;
942 unit->data + pos / 8, slice->data_size);
943 memset(slice->data + slice->data_size, 0,
944 AV_INPUT_BUFFER_PADDING_SIZE);
945 slice->data_bit_start = pos % 8;
951 err = ff_cbs_alloc_unit_content(ctx, unit,
952 sizeof(H265RawAUD), NULL);
956 err = cbs_h265_read_aud(ctx, &bc, unit->content);
963 return AVERROR(ENOSYS);
969 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
970 CodedBitstreamUnit *unit,
975 switch (unit->type) {
978 H264RawSPS *sps = unit->content;
980 err = cbs_h264_write_sps(ctx, pbc, sps);
984 err = cbs_h264_replace_sps(ctx, sps);
990 case H264_NAL_SPS_EXT:
992 H264RawSPSExtension *sps_ext = unit->content;
994 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1002 H264RawPPS *pps = unit->content;
1004 err = cbs_h264_write_pps(ctx, pbc, pps);
1008 err = cbs_h264_replace_pps(ctx, pps);
1014 case H264_NAL_SLICE:
1015 case H264_NAL_IDR_SLICE:
1016 case H264_NAL_AUXILIARY_SLICE:
1018 H264RawSlice *slice = unit->content;
1019 BitstreamContext bc;
1020 int bits_left, end, zeroes;
1022 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1027 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1028 return AVERROR(ENOSPC);
1030 bitstream_init(&bc, slice->data, slice->data_size * 8);
1031 bitstream_skip(&bc, slice->data_bit_start);
1033 // Copy in two-byte blocks, but stop before copying the
1034 // rbsp_stop_one_bit in the final byte.
1035 while (bitstream_bits_left(&bc) > 23)
1036 put_bits(pbc, 16, bitstream_read(&bc, 16));
1038 bits_left = bitstream_bits_left(&bc);
1039 end = bitstream_read(&bc, bits_left);
1041 // rbsp_stop_one_bit must be present here.
1043 zeroes = ff_ctz(end);
1044 if (bits_left > zeroes + 1)
1045 put_bits(pbc, bits_left - zeroes - 1,
1046 end >> (zeroes + 1));
1047 put_bits(pbc, 1, 1);
1048 while (put_bits_count(pbc) % 8 != 0)
1049 put_bits(pbc, 1, 0);
1051 // No slice data - that was just the header.
1052 // (Bitstream may be unaligned!)
1059 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1067 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1074 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1075 "NAL unit type %"PRIu32".\n", unit->type);
1076 return AVERROR_PATCHWELCOME;
1082 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1083 CodedBitstreamUnit *unit,
1088 switch (unit->type) {
1091 H265RawVPS *vps = unit->content;
1093 err = cbs_h265_write_vps(ctx, pbc, vps);
1097 err = cbs_h265_replace_vps(ctx, vps);
1105 H265RawSPS *sps = unit->content;
1107 err = cbs_h265_write_sps(ctx, pbc, sps);
1111 err = cbs_h265_replace_sps(ctx, sps);
1119 H265RawPPS *pps = unit->content;
1121 err = cbs_h265_write_pps(ctx, pbc, pps);
1125 err = cbs_h265_replace_pps(ctx, pps);
1131 case HEVC_NAL_TRAIL_N:
1132 case HEVC_NAL_TRAIL_R:
1133 case HEVC_NAL_TSA_N:
1134 case HEVC_NAL_TSA_R:
1135 case HEVC_NAL_STSA_N:
1136 case HEVC_NAL_STSA_R:
1137 case HEVC_NAL_RADL_N:
1138 case HEVC_NAL_RADL_R:
1139 case HEVC_NAL_RASL_N:
1140 case HEVC_NAL_RASL_R:
1141 case HEVC_NAL_BLA_W_LP:
1142 case HEVC_NAL_BLA_W_RADL:
1143 case HEVC_NAL_BLA_N_LP:
1144 case HEVC_NAL_IDR_W_RADL:
1145 case HEVC_NAL_IDR_N_LP:
1146 case HEVC_NAL_CRA_NUT:
1148 H265RawSlice *slice = unit->content;
1149 BitstreamContext bc;
1150 int bits_left, end, zeroes;
1152 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1157 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1158 return AVERROR(ENOSPC);
1160 bitstream_init(&bc, slice->data, slice->data_size * 8);
1161 bitstream_skip(&bc, slice->data_bit_start);
1163 // Copy in two-byte blocks, but stop before copying the
1164 // rbsp_stop_one_bit in the final byte.
1165 while (bitstream_bits_left(&bc) > 23)
1166 put_bits(pbc, 16, bitstream_read(&bc, 16));
1168 bits_left = bitstream_bits_left(&bc);
1169 end = bitstream_read(&bc, bits_left);
1171 // rbsp_stop_one_bit must be present here.
1173 zeroes = ff_ctz(end);
1174 if (bits_left > zeroes + 1)
1175 put_bits(pbc, bits_left - zeroes - 1,
1176 end >> (zeroes + 1));
1177 put_bits(pbc, 1, 1);
1178 while (put_bits_count(pbc) % 8 != 0)
1179 put_bits(pbc, 1, 0);
1181 // No slice data - that was just the header.
1188 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1195 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1196 "NAL unit type %"PRIu32".\n", unit->type);
1197 return AVERROR_PATCHWELCOME;
1203 static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
1204 CodedBitstreamUnit *unit)
1206 CodedBitstreamH2645Context *priv = ctx->priv_data;
1207 enum AVCodecID codec_id = ctx->codec->codec_id;
1211 if (!priv->write_buffer) {
1212 // Initial write buffer size is 1MB.
1213 priv->write_buffer_size = 1024 * 1024;
1215 reallocate_and_try_again:
1216 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1218 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1219 "sufficiently large write buffer (last attempt "
1220 "%zu bytes).\n", priv->write_buffer_size);
1225 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1227 if (codec_id == AV_CODEC_ID_H264)
1228 err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1230 err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1232 if (err == AVERROR(ENOSPC)) {
1234 priv->write_buffer_size *= 2;
1235 goto reallocate_and_try_again;
1237 // Overflow but we didn't notice.
1238 av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1241 // Write failed for some other reason.
1245 if (put_bits_count(&pbc) % 8)
1246 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1248 unit->data_bit_padding = 0;
1250 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1251 flush_put_bits(&pbc);
1253 err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1257 memcpy(unit->data, priv->write_buffer, unit->data_size);
1262 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1263 CodedBitstreamFragment *frag)
1266 size_t max_size, dp, sp;
1267 int err, i, zero_run;
1269 for (i = 0; i < frag->nb_units; i++) {
1270 // Data should already all have been written when we get here.
1271 av_assert0(frag->units[i].data);
1275 for (i = 0; i < frag->nb_units; i++) {
1276 // Start code + content with worst-case emulation prevention.
1277 max_size += 3 + frag->units[i].data_size * 3 / 2;
1280 data = av_malloc(max_size);
1282 return AVERROR(ENOMEM);
1285 for (i = 0; i < frag->nb_units; i++) {
1286 CodedBitstreamUnit *unit = &frag->units[i];
1288 if (unit->data_bit_padding > 0) {
1289 if (i < frag->nb_units - 1)
1290 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1291 "unaligned padding on non-final NAL unit.\n");
1293 frag->data_bit_padding = unit->data_bit_padding;
1296 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1297 (unit->type == H264_NAL_SPS ||
1298 unit->type == H264_NAL_PPS)) ||
1299 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1300 (unit->type == HEVC_NAL_VPS ||
1301 unit->type == HEVC_NAL_SPS ||
1302 unit->type == HEVC_NAL_PPS)) ||
1303 i == 0 /* (Assume this is the start of an access unit.) */) {
1307 // start_code_prefix_one_3bytes
1313 for (sp = 0; sp < unit->data_size; sp++) {
1315 if (unit->data[sp] == 0)
1320 if ((unit->data[sp] & ~3) == 0) {
1321 // emulation_prevention_three_byte
1324 zero_run = unit->data[sp] == 0;
1326 data[dp++] = unit->data[sp];
1330 av_assert0(dp <= max_size);
1331 err = av_reallocp(&data, dp);
1335 frag->data_ref = av_buffer_create(data, dp, NULL, NULL, 0);
1336 if (!frag->data_ref) {
1338 return AVERROR(ENOMEM);
1342 frag->data_size = dp;
1347 static void cbs_h264_close(CodedBitstreamContext *ctx)
1349 CodedBitstreamH264Context *h264 = ctx->priv_data;
1352 ff_h2645_packet_uninit(&h264->common.read_packet);
1354 av_freep(&h264->common.write_buffer);
1356 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1357 av_freep(&h264->sps[i]);
1358 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1359 av_freep(&h264->pps[i]);
1362 static void cbs_h265_close(CodedBitstreamContext *ctx)
1364 CodedBitstreamH265Context *h265 = ctx->priv_data;
1367 ff_h2645_packet_uninit(&h265->common.read_packet);
1369 av_freep(&h265->common.write_buffer);
1371 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1372 av_freep(&h265->vps[i]);
1373 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1374 av_freep(&h265->sps[i]);
1375 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1376 av_freep(&h265->pps[i]);
1379 const CodedBitstreamType ff_cbs_type_h264 = {
1380 .codec_id = AV_CODEC_ID_H264,
1382 .priv_data_size = sizeof(CodedBitstreamH264Context),
1384 .split_fragment = &cbs_h2645_split_fragment,
1385 .read_unit = &cbs_h264_read_nal_unit,
1386 .write_unit = &cbs_h2645_write_nal_unit,
1387 .assemble_fragment = &cbs_h2645_assemble_fragment,
1389 .close = &cbs_h264_close,
1392 const CodedBitstreamType ff_cbs_type_h265 = {
1393 .codec_id = AV_CODEC_ID_HEVC,
1395 .priv_data_size = sizeof(CodedBitstreamH265Context),
1397 .split_fragment = &cbs_h2645_split_fragment,
1398 .read_unit = &cbs_h265_read_nal_unit,
1399 .write_unit = &cbs_h2645_write_nal_unit,
1400 .assemble_fragment = &cbs_h2645_assemble_fragment,
1402 .close = &cbs_h265_close,
1405 int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
1406 CodedBitstreamFragment *au,
1407 const H264RawSEIPayload *payload)
1410 CodedBitstreamUnit *nal = NULL;
1413 // Find an existing SEI NAL unit to add to.
1414 for (i = 0; i < au->nb_units; i++) {
1415 if (au->units[i].type == H264_NAL_SEI) {
1416 nal = &au->units[i];
1424 // Need to make a new SEI NAL unit. Insert it before the first
1425 // slice data NAL unit; if no slice data, add at the end.
1426 AVBufferRef *sei_ref;
1428 sei = av_mallocz(sizeof(*sei));
1430 return AVERROR(ENOMEM);
1432 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1433 sei->nal_unit_header.nal_ref_idc = 0;
1435 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1436 &cbs_h264_free_sei, ctx, 0);
1439 return AVERROR(ENOMEM);
1442 for (i = 0; i < au->nb_units; i++) {
1443 if (au->units[i].type == H264_NAL_SLICE ||
1444 au->units[i].type == H264_NAL_IDR_SLICE)
1448 err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1450 av_buffer_unref(&sei_ref);
1455 if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1456 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1458 return AVERROR(EINVAL);
1461 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1462 ++sei->payload_count;
1467 int ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
1468 CodedBitstreamFragment *au,
1469 CodedBitstreamUnit *nal,
1472 H264RawSEI *sei = nal->content;
1474 av_assert0(nal->type == H264_NAL_SEI);
1475 av_assert0(position >= 0 && position < sei->payload_count);
1477 if (position == 0 && sei->payload_count == 1) {
1478 // Deleting NAL unit entirely.
1481 for (i = 0; i < au->nb_units; i++) {
1482 if (&au->units[i] == nal)
1485 av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1487 return ff_cbs_delete_unit(ctx, au, i);
1489 cbs_h264_free_sei_payload(&sei->payload[position]);
1491 --sei->payload_count;
1492 memmove(sei->payload + position,
1493 sei->payload + position + 1,
1494 (sei->payload_count - position) * sizeof(*sei->payload));