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"
34 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
35 const char *name, uint32_t *write_to,
36 uint32_t range_min, uint32_t range_max)
43 position = get_bits_count(gbc);
45 for (i = 0; i < 32; i++) {
46 if (get_bits_left(gbc) < 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;
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++) {
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, GetBitContext *gbc,
85 const char *name, int32_t *write_to,
86 int32_t range_min, int32_t range_max)
94 position = get_bits_count(gbc);
96 for (i = 0; i < 32; i++) {
97 if (get_bits_left(gbc) < 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;
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++) {
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 GetBitContext
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(GetBitContext *gbc)
281 int bits_left = get_bits_left(gbc);
284 if (show_bits(gbc, 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) (get_bits_count(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,
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, 1);
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, 1);
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, 1);
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 = init_get_bits(&gbc, 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, &gbc, 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, &gbc, unit->content);
735 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
741 err = cbs_h264_read_pps(ctx, &gbc, 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, &gbc, &slice->header);
768 pos = get_bits_count(&gbc);
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, &gbc, unit->content);
807 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
812 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
818 case H264_NAL_FILLER_DATA:
820 err = ff_cbs_alloc_unit_content(ctx, unit,
821 sizeof(H264RawFiller), NULL);
825 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
832 return AVERROR(ENOSYS);
838 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
839 CodedBitstreamUnit *unit)
844 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
848 switch (unit->type) {
853 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
859 err = cbs_h265_read_vps(ctx, &gbc, vps);
863 err = cbs_h265_replace_vps(ctx, vps);
872 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
878 err = cbs_h265_read_sps(ctx, &gbc, sps);
882 err = cbs_h265_replace_sps(ctx, sps);
892 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
898 err = cbs_h265_read_pps(ctx, &gbc, pps);
902 err = cbs_h265_replace_pps(ctx, pps);
908 case HEVC_NAL_TRAIL_N:
909 case HEVC_NAL_TRAIL_R:
912 case HEVC_NAL_STSA_N:
913 case HEVC_NAL_STSA_R:
914 case HEVC_NAL_RADL_N:
915 case HEVC_NAL_RADL_R:
916 case HEVC_NAL_RASL_N:
917 case HEVC_NAL_RASL_R:
918 case HEVC_NAL_BLA_W_LP:
919 case HEVC_NAL_BLA_W_RADL:
920 case HEVC_NAL_BLA_N_LP:
921 case HEVC_NAL_IDR_W_RADL:
922 case HEVC_NAL_IDR_N_LP:
923 case HEVC_NAL_CRA_NUT:
928 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
929 &cbs_h265_free_slice);
932 slice = unit->content;
934 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
938 pos = get_bits_count(&gbc);
939 len = unit->data_size;
940 if (!unit->data[len - 1]) {
942 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
943 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
944 "from slice data.\n", z);
948 slice->data_size = len - pos / 8;
949 slice->data_ref = av_buffer_alloc(slice->data_size +
950 AV_INPUT_BUFFER_PADDING_SIZE);
951 if (!slice->data_ref)
952 return AVERROR(ENOMEM);
953 slice->data = slice->data_ref->data;
955 unit->data + pos / 8, slice->data_size);
956 memset(slice->data + slice->data_size, 0,
957 AV_INPUT_BUFFER_PADDING_SIZE);
958 slice->data_bit_start = pos % 8;
964 err = ff_cbs_alloc_unit_content(ctx, unit,
965 sizeof(H265RawAUD), NULL);
969 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
976 return AVERROR(ENOSYS);
982 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
983 CodedBitstreamUnit *unit,
988 switch (unit->type) {
991 H264RawSPS *sps = unit->content;
993 err = cbs_h264_write_sps(ctx, pbc, sps);
997 err = cbs_h264_replace_sps(ctx, sps);
1003 case H264_NAL_SPS_EXT:
1005 H264RawSPSExtension *sps_ext = unit->content;
1007 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1015 H264RawPPS *pps = unit->content;
1017 err = cbs_h264_write_pps(ctx, pbc, pps);
1021 err = cbs_h264_replace_pps(ctx, pps);
1027 case H264_NAL_SLICE:
1028 case H264_NAL_IDR_SLICE:
1029 case H264_NAL_AUXILIARY_SLICE:
1031 H264RawSlice *slice = unit->content;
1033 int bits_left, end, zeroes;
1035 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1040 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1041 return AVERROR(ENOSPC);
1043 init_get_bits(&gbc, slice->data, slice->data_size * 8);
1044 skip_bits_long(&gbc, slice->data_bit_start);
1046 // Copy in two-byte blocks, but stop before copying the
1047 // rbsp_stop_one_bit in the final byte.
1048 while (get_bits_left(&gbc) > 23)
1049 put_bits(pbc, 16, get_bits(&gbc, 16));
1051 bits_left = get_bits_left(&gbc);
1052 end = get_bits(&gbc, bits_left);
1054 // rbsp_stop_one_bit must be present here.
1056 zeroes = ff_ctz(end);
1057 if (bits_left > zeroes + 1)
1058 put_bits(pbc, bits_left - zeroes - 1,
1059 end >> (zeroes + 1));
1060 put_bits(pbc, 1, 1);
1061 while (put_bits_count(pbc) % 8 != 0)
1062 put_bits(pbc, 1, 0);
1064 // No slice data - that was just the header.
1065 // (Bitstream may be unaligned!)
1072 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1080 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1086 case H264_NAL_FILLER_DATA:
1088 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1095 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1096 "NAL unit type %"PRIu32".\n", unit->type);
1097 return AVERROR_PATCHWELCOME;
1103 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1104 CodedBitstreamUnit *unit,
1109 switch (unit->type) {
1112 H265RawVPS *vps = unit->content;
1114 err = cbs_h265_write_vps(ctx, pbc, vps);
1118 err = cbs_h265_replace_vps(ctx, vps);
1126 H265RawSPS *sps = unit->content;
1128 err = cbs_h265_write_sps(ctx, pbc, sps);
1132 err = cbs_h265_replace_sps(ctx, sps);
1140 H265RawPPS *pps = unit->content;
1142 err = cbs_h265_write_pps(ctx, pbc, pps);
1146 err = cbs_h265_replace_pps(ctx, pps);
1152 case HEVC_NAL_TRAIL_N:
1153 case HEVC_NAL_TRAIL_R:
1154 case HEVC_NAL_TSA_N:
1155 case HEVC_NAL_TSA_R:
1156 case HEVC_NAL_STSA_N:
1157 case HEVC_NAL_STSA_R:
1158 case HEVC_NAL_RADL_N:
1159 case HEVC_NAL_RADL_R:
1160 case HEVC_NAL_RASL_N:
1161 case HEVC_NAL_RASL_R:
1162 case HEVC_NAL_BLA_W_LP:
1163 case HEVC_NAL_BLA_W_RADL:
1164 case HEVC_NAL_BLA_N_LP:
1165 case HEVC_NAL_IDR_W_RADL:
1166 case HEVC_NAL_IDR_N_LP:
1167 case HEVC_NAL_CRA_NUT:
1169 H265RawSlice *slice = unit->content;
1171 int bits_left, end, zeroes;
1173 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1178 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1179 return AVERROR(ENOSPC);
1181 init_get_bits(&gbc, slice->data, slice->data_size * 8);
1182 skip_bits_long(&gbc, slice->data_bit_start);
1184 // Copy in two-byte blocks, but stop before copying the
1185 // rbsp_stop_one_bit in the final byte.
1186 while (get_bits_left(&gbc) > 23)
1187 put_bits(pbc, 16, get_bits(&gbc, 16));
1189 bits_left = get_bits_left(&gbc);
1190 end = get_bits(&gbc, bits_left);
1192 // rbsp_stop_one_bit must be present here.
1194 zeroes = ff_ctz(end);
1195 if (bits_left > zeroes + 1)
1196 put_bits(pbc, bits_left - zeroes - 1,
1197 end >> (zeroes + 1));
1198 put_bits(pbc, 1, 1);
1199 while (put_bits_count(pbc) % 8 != 0)
1200 put_bits(pbc, 1, 0);
1202 // No slice data - that was just the header.
1209 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1216 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1217 "NAL unit type %"PRIu32".\n", unit->type);
1218 return AVERROR_PATCHWELCOME;
1224 static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
1225 CodedBitstreamUnit *unit)
1227 CodedBitstreamH2645Context *priv = ctx->priv_data;
1228 enum AVCodecID codec_id = ctx->codec->codec_id;
1232 if (!priv->write_buffer) {
1233 // Initial write buffer size is 1MB.
1234 priv->write_buffer_size = 1024 * 1024;
1236 reallocate_and_try_again:
1237 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1239 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1240 "sufficiently large write buffer (last attempt "
1241 "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1246 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1248 if (codec_id == AV_CODEC_ID_H264)
1249 err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1251 err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1253 if (err == AVERROR(ENOSPC)) {
1255 priv->write_buffer_size *= 2;
1256 goto reallocate_and_try_again;
1258 // Overflow but we didn't notice.
1259 av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1262 // Write failed for some other reason.
1266 if (put_bits_count(&pbc) % 8)
1267 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1269 unit->data_bit_padding = 0;
1271 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1272 flush_put_bits(&pbc);
1274 err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1278 memcpy(unit->data, priv->write_buffer, unit->data_size);
1283 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1284 CodedBitstreamFragment *frag)
1287 size_t max_size, dp, sp;
1288 int err, i, zero_run;
1290 for (i = 0; i < frag->nb_units; i++) {
1291 // Data should already all have been written when we get here.
1292 av_assert0(frag->units[i].data);
1296 for (i = 0; i < frag->nb_units; i++) {
1297 // Start code + content with worst-case emulation prevention.
1298 max_size += 3 + frag->units[i].data_size * 3 / 2;
1301 data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1303 return AVERROR(ENOMEM);
1306 for (i = 0; i < frag->nb_units; i++) {
1307 CodedBitstreamUnit *unit = &frag->units[i];
1309 if (unit->data_bit_padding > 0) {
1310 if (i < frag->nb_units - 1)
1311 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1312 "unaligned padding on non-final NAL unit.\n");
1314 frag->data_bit_padding = unit->data_bit_padding;
1317 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1318 (unit->type == H264_NAL_SPS ||
1319 unit->type == H264_NAL_PPS)) ||
1320 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1321 (unit->type == HEVC_NAL_VPS ||
1322 unit->type == HEVC_NAL_SPS ||
1323 unit->type == HEVC_NAL_PPS)) ||
1324 i == 0 /* (Assume this is the start of an access unit.) */) {
1328 // start_code_prefix_one_3bytes
1334 for (sp = 0; sp < unit->data_size; sp++) {
1336 if (unit->data[sp] == 0)
1341 if ((unit->data[sp] & ~3) == 0) {
1342 // emulation_prevention_three_byte
1345 zero_run = unit->data[sp] == 0;
1347 data[dp++] = unit->data[sp];
1351 av_assert0(dp <= max_size);
1352 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1355 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1357 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1359 if (!frag->data_ref) {
1361 return AVERROR(ENOMEM);
1365 frag->data_size = dp;
1370 static void cbs_h264_close(CodedBitstreamContext *ctx)
1372 CodedBitstreamH264Context *h264 = ctx->priv_data;
1375 ff_h2645_packet_uninit(&h264->common.read_packet);
1377 av_freep(&h264->common.write_buffer);
1379 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1380 av_freep(&h264->sps[i]);
1381 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1382 av_freep(&h264->pps[i]);
1385 static void cbs_h265_close(CodedBitstreamContext *ctx)
1387 CodedBitstreamH265Context *h265 = ctx->priv_data;
1390 ff_h2645_packet_uninit(&h265->common.read_packet);
1392 av_freep(&h265->common.write_buffer);
1394 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1395 av_freep(&h265->vps[i]);
1396 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1397 av_freep(&h265->sps[i]);
1398 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1399 av_freep(&h265->pps[i]);
1402 const CodedBitstreamType ff_cbs_type_h264 = {
1403 .codec_id = AV_CODEC_ID_H264,
1405 .priv_data_size = sizeof(CodedBitstreamH264Context),
1407 .split_fragment = &cbs_h2645_split_fragment,
1408 .read_unit = &cbs_h264_read_nal_unit,
1409 .write_unit = &cbs_h2645_write_nal_unit,
1410 .assemble_fragment = &cbs_h2645_assemble_fragment,
1412 .close = &cbs_h264_close,
1415 const CodedBitstreamType ff_cbs_type_h265 = {
1416 .codec_id = AV_CODEC_ID_HEVC,
1418 .priv_data_size = sizeof(CodedBitstreamH265Context),
1420 .split_fragment = &cbs_h2645_split_fragment,
1421 .read_unit = &cbs_h265_read_nal_unit,
1422 .write_unit = &cbs_h2645_write_nal_unit,
1423 .assemble_fragment = &cbs_h2645_assemble_fragment,
1425 .close = &cbs_h265_close,
1428 int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
1429 CodedBitstreamFragment *au,
1430 const H264RawSEIPayload *payload)
1433 CodedBitstreamUnit *nal = NULL;
1436 // Find an existing SEI NAL unit to add to.
1437 for (i = 0; i < au->nb_units; i++) {
1438 if (au->units[i].type == H264_NAL_SEI) {
1439 nal = &au->units[i];
1447 // Need to make a new SEI NAL unit. Insert it before the first
1448 // slice data NAL unit; if no slice data, add at the end.
1449 AVBufferRef *sei_ref;
1451 sei = av_mallocz(sizeof(*sei));
1453 return AVERROR(ENOMEM);
1455 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1456 sei->nal_unit_header.nal_ref_idc = 0;
1458 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1459 &cbs_h264_free_sei, ctx, 0);
1462 return AVERROR(ENOMEM);
1465 for (i = 0; i < au->nb_units; i++) {
1466 if (au->units[i].type == H264_NAL_SLICE ||
1467 au->units[i].type == H264_NAL_IDR_SLICE)
1471 err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1473 av_buffer_unref(&sei_ref);
1478 if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1479 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1481 return AVERROR(EINVAL);
1484 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1485 ++sei->payload_count;
1490 int ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
1491 CodedBitstreamFragment *au,
1492 CodedBitstreamUnit *nal,
1495 H264RawSEI *sei = nal->content;
1497 av_assert0(nal->type == H264_NAL_SEI);
1498 av_assert0(position >= 0 && position < sei->payload_count);
1500 if (position == 0 && sei->payload_count == 1) {
1501 // Deleting NAL unit entirely.
1504 for (i = 0; i < au->nb_units; i++) {
1505 if (&au->units[i] == nal)
1508 av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1510 return ff_cbs_delete_unit(ctx, au, i);
1512 cbs_h264_free_sei_payload(&sei->payload[position]);
1514 --sei->payload_count;
1515 memmove(sei->payload + position,
1516 sei->payload + position + 1,
1517 (sei->payload_count - position) * sizeof(*sei->payload));