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); \
324 return AVERROR(ENOMEM); \
325 name = name ## _ref->data; \
328 #define FUNC(name) FUNC_H264(READWRITE, name)
329 #include "cbs_h264_syntax_template.c"
332 #define FUNC(name) FUNC_H265(READWRITE, name)
333 #include "cbs_h265_syntax_template.c"
343 #undef more_rbsp_data
344 #undef byte_alignment
349 #define READWRITE write
350 #define RWContext PutBitContext
352 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
353 uint32_t value = var; \
354 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
355 SUBSCRIPTS(subs, __VA_ARGS__), \
356 value, range_min, range_max)); \
358 #define xue(name, var, range_min, range_max, subs, ...) do { \
359 uint32_t value = var; \
360 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
361 SUBSCRIPTS(subs, __VA_ARGS__), \
362 value, range_min, range_max)); \
364 #define xse(name, var, range_min, range_max, subs, ...) do { \
365 int32_t value = var; \
366 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
367 SUBSCRIPTS(subs, __VA_ARGS__), \
368 value, range_min, range_max)); \
371 #define infer(name, value) do { \
372 if (current->name != (value)) { \
373 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
374 "%s does not match inferred value: " \
375 "%"PRId64", but should be %"PRId64".\n", \
376 #name, (int64_t)current->name, (int64_t)(value)); \
380 #define more_rbsp_data(var) (var)
382 #define byte_alignment(rw) (put_bits_count(rw) % 8)
384 #define allocate(name, size) do { \
386 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
387 "for writing.\n", #name); \
388 return AVERROR_INVALIDDATA; \
392 #define FUNC(name) FUNC_H264(READWRITE, name)
393 #include "cbs_h264_syntax_template.c"
396 #define FUNC(name) FUNC_H265(READWRITE, name)
397 #include "cbs_h265_syntax_template.c"
411 #undef more_rbsp_data
412 #undef byte_alignment
416 static void cbs_h264_free_pps(void *unit, uint8_t *content)
418 H264RawPPS *pps = (H264RawPPS*)content;
419 av_buffer_unref(&pps->slice_group_id_ref);
423 static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
425 switch (payload->payload_type) {
426 case H264_SEI_TYPE_BUFFERING_PERIOD:
427 case H264_SEI_TYPE_PIC_TIMING:
428 case H264_SEI_TYPE_PAN_SCAN_RECT:
429 case H264_SEI_TYPE_RECOVERY_POINT:
430 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
432 case H264_SEI_TYPE_USER_DATA_REGISTERED:
433 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
435 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
436 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
439 av_buffer_unref(&payload->payload.other.data_ref);
444 static void cbs_h264_free_sei(void *unit, uint8_t *content)
446 H264RawSEI *sei = (H264RawSEI*)content;
448 for (i = 0; i < sei->payload_count; i++)
449 cbs_h264_free_sei_payload(&sei->payload[i]);
453 static void cbs_h264_free_slice(void *unit, uint8_t *content)
455 H264RawSlice *slice = (H264RawSlice*)content;
456 av_buffer_unref(&slice->data_ref);
460 static void cbs_h265_free_vps(void *unit, uint8_t *content)
462 H265RawVPS *vps = (H265RawVPS*)content;
463 av_buffer_unref(&vps->extension_data.data_ref);
467 static void cbs_h265_free_sps(void *unit, uint8_t *content)
469 H265RawSPS *sps = (H265RawSPS*)content;
470 av_buffer_unref(&sps->extension_data.data_ref);
474 static void cbs_h265_free_pps(void *unit, uint8_t *content)
476 H265RawPPS *pps = (H265RawPPS*)content;
477 av_buffer_unref(&pps->extension_data.data_ref);
481 static void cbs_h265_free_slice(void *unit, uint8_t *content)
483 H265RawSlice *slice = (H265RawSlice*)content;
484 av_buffer_unref(&slice->data_ref);
488 static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
490 switch (payload->payload_type) {
491 case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
492 case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
495 av_buffer_unref(&payload->payload.other.data_ref);
500 static void cbs_h265_free_sei(void *unit, uint8_t *content)
502 H265RawSEI *sei = (H265RawSEI*)content;
504 for (i = 0; i < sei->payload_count; i++)
505 cbs_h265_free_sei_payload(&sei->payload[i]);
509 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
510 CodedBitstreamFragment *frag,
511 const H2645Packet *packet)
515 for (i = 0; i < packet->nb_nals; i++) {
516 const H2645NAL *nal = &packet->nals[i];
517 size_t size = nal->size;
520 // Remove trailing zeroes.
521 while (size > 0 && nal->data[size - 1] == 0)
523 av_assert0(size > 0);
525 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
527 return AVERROR(ENOMEM);
528 memcpy(data, nal->data, size);
529 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
531 err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
542 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
543 CodedBitstreamFragment *frag,
546 enum AVCodecID codec_id = ctx->codec->codec_id;
547 CodedBitstreamH2645Context *priv = ctx->priv_data;
551 av_assert0(frag->data && frag->nb_units == 0);
552 if (frag->data_size == 0)
555 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
557 size_t size, start, end;
558 int i, count, version;
562 bytestream2_init(&gbc, frag->data, frag->data_size);
564 if (bytestream2_get_bytes_left(&gbc) < 6)
565 return AVERROR_INVALIDDATA;
567 version = bytestream2_get_byte(&gbc);
569 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
570 "first byte %u.", version);
571 return AVERROR_INVALIDDATA;
574 bytestream2_skip(&gbc, 3);
575 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
578 count = bytestream2_get_byte(&gbc) & 0x1f;
579 start = bytestream2_tell(&gbc);
580 for (i = 0; i < count; i++) {
581 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
582 return AVERROR_INVALIDDATA;
583 size = bytestream2_get_be16(&gbc);
584 if (bytestream2_get_bytes_left(&gbc) < size)
585 return AVERROR_INVALIDDATA;
586 bytestream2_skip(&gbc, size);
588 end = bytestream2_tell(&gbc);
590 err = ff_h2645_packet_split(&priv->read_packet,
591 frag->data + start, end - start,
592 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
594 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
597 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
602 count = bytestream2_get_byte(&gbc);
603 start = bytestream2_tell(&gbc);
604 for (i = 0; i < count; i++) {
605 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
606 return AVERROR_INVALIDDATA;
607 size = bytestream2_get_be16(&gbc);
608 if (bytestream2_get_bytes_left(&gbc) < size)
609 return AVERROR_INVALIDDATA;
610 bytestream2_skip(&gbc, size);
612 end = bytestream2_tell(&gbc);
614 err = ff_h2645_packet_split(&priv->read_packet,
615 frag->data + start, end - start,
616 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
618 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
621 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
625 if (bytestream2_get_bytes_left(&gbc) > 0) {
626 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
627 "header.\n", bytestream2_get_bytes_left(&gbc));
630 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
632 size_t size, start, end;
633 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
637 bytestream2_init(&gbc, frag->data, frag->data_size);
639 if (bytestream2_get_bytes_left(&gbc) < 23)
640 return AVERROR_INVALIDDATA;
642 version = bytestream2_get_byte(&gbc);
644 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
645 "first byte %u.", version);
646 return AVERROR_INVALIDDATA;
649 bytestream2_skip(&gbc, 20);
650 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
652 nb_arrays = bytestream2_get_byte(&gbc);
653 for (i = 0; i < nb_arrays; i++) {
654 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
655 nb_nals = bytestream2_get_be16(&gbc);
657 start = bytestream2_tell(&gbc);
658 for (j = 0; j < nb_nals; j++) {
659 if (bytestream2_get_bytes_left(&gbc) < 2)
660 return AVERROR_INVALIDDATA;
661 size = bytestream2_get_be16(&gbc);
662 if (bytestream2_get_bytes_left(&gbc) < size)
663 return AVERROR_INVALIDDATA;
664 bytestream2_skip(&gbc, size);
666 end = bytestream2_tell(&gbc);
668 err = ff_h2645_packet_split(&priv->read_packet,
669 frag->data + start, end - start,
670 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
672 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
673 "HVCC array %d (%d NAL units of type %d).\n",
674 i, nb_nals, nal_unit_type);
677 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
683 // Annex B, or later MP4 with already-known parameters.
685 err = ff_h2645_packet_split(&priv->read_packet,
686 frag->data, frag->data_size,
688 priv->mp4, priv->nal_length_size,
693 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
701 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
702 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
703 CodedBitstreamUnit *unit) \
705 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
706 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
707 unsigned int id = ps_var->id_element; \
708 if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
709 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
710 " id : %d.\n", id); \
711 return AVERROR_INVALIDDATA; \
713 if (priv->ps_var[id] == priv->active_ ## ps_var) \
714 priv->active_ ## ps_var = NULL ; \
715 av_buffer_unref(&priv->ps_var ## _ref[id]); \
716 if (unit->content_ref) \
717 priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
719 priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
720 if (!priv->ps_var ## _ref[id]) \
721 return AVERROR(ENOMEM); \
722 priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
723 if (!unit->content_ref) \
724 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
728 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
729 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
730 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
731 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
732 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
734 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
735 CodedBitstreamUnit *unit)
740 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
744 switch (unit->type) {
749 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
754 err = cbs_h264_read_sps(ctx, &gbc, sps);
758 err = cbs_h264_replace_sps(ctx, unit);
764 case H264_NAL_SPS_EXT:
766 err = ff_cbs_alloc_unit_content(ctx, unit,
767 sizeof(H264RawSPSExtension),
772 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
782 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
788 err = cbs_h264_read_pps(ctx, &gbc, pps);
792 err = cbs_h264_replace_pps(ctx, unit);
799 case H264_NAL_IDR_SLICE:
800 case H264_NAL_AUXILIARY_SLICE:
805 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
806 &cbs_h264_free_slice);
809 slice = unit->content;
811 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
815 pos = get_bits_count(&gbc);
816 len = unit->data_size;
817 if (!unit->data[len - 1]) {
819 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
820 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
821 "from slice data.\n", z);
825 slice->data_size = len - pos / 8;
826 slice->data_ref = av_buffer_ref(unit->data_ref);
827 if (!slice->data_ref)
828 return AVERROR(ENOMEM);
829 slice->data = unit->data + pos / 8;
830 slice->data_bit_start = pos % 8;
836 err = ff_cbs_alloc_unit_content(ctx, unit,
837 sizeof(H264RawAUD), NULL);
841 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
849 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
854 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
860 case H264_NAL_FILLER_DATA:
862 err = ff_cbs_alloc_unit_content(ctx, unit,
863 sizeof(H264RawFiller), NULL);
867 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
874 return AVERROR(ENOSYS);
880 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
881 CodedBitstreamUnit *unit)
886 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
890 switch (unit->type) {
895 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
901 err = cbs_h265_read_vps(ctx, &gbc, vps);
905 err = cbs_h265_replace_vps(ctx, unit);
914 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
920 err = cbs_h265_read_sps(ctx, &gbc, sps);
924 err = cbs_h265_replace_sps(ctx, unit);
934 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
940 err = cbs_h265_read_pps(ctx, &gbc, pps);
944 err = cbs_h265_replace_pps(ctx, unit);
950 case HEVC_NAL_TRAIL_N:
951 case HEVC_NAL_TRAIL_R:
954 case HEVC_NAL_STSA_N:
955 case HEVC_NAL_STSA_R:
956 case HEVC_NAL_RADL_N:
957 case HEVC_NAL_RADL_R:
958 case HEVC_NAL_RASL_N:
959 case HEVC_NAL_RASL_R:
960 case HEVC_NAL_BLA_W_LP:
961 case HEVC_NAL_BLA_W_RADL:
962 case HEVC_NAL_BLA_N_LP:
963 case HEVC_NAL_IDR_W_RADL:
964 case HEVC_NAL_IDR_N_LP:
965 case HEVC_NAL_CRA_NUT:
970 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
971 &cbs_h265_free_slice);
974 slice = unit->content;
976 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
980 pos = get_bits_count(&gbc);
981 len = unit->data_size;
982 if (!unit->data[len - 1]) {
984 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
985 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
986 "from slice data.\n", z);
990 slice->data_size = len - pos / 8;
991 slice->data_ref = av_buffer_ref(unit->data_ref);
992 if (!slice->data_ref)
993 return AVERROR(ENOMEM);
994 slice->data = unit->data + pos / 8;
995 slice->data_bit_start = pos % 8;
1001 err = ff_cbs_alloc_unit_content(ctx, unit,
1002 sizeof(H265RawAUD), NULL);
1006 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1012 case HEVC_NAL_SEI_PREFIX:
1014 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1015 &cbs_h265_free_sei);
1020 err = cbs_h265_read_sei(ctx, &gbc, unit->content);
1028 return AVERROR(ENOSYS);
1034 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1035 CodedBitstreamUnit *unit,
1040 switch (unit->type) {
1043 H264RawSPS *sps = unit->content;
1045 err = cbs_h264_write_sps(ctx, pbc, sps);
1049 err = cbs_h264_replace_sps(ctx, unit);
1055 case H264_NAL_SPS_EXT:
1057 H264RawSPSExtension *sps_ext = unit->content;
1059 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1067 H264RawPPS *pps = unit->content;
1069 err = cbs_h264_write_pps(ctx, pbc, pps);
1073 err = cbs_h264_replace_pps(ctx, unit);
1079 case H264_NAL_SLICE:
1080 case H264_NAL_IDR_SLICE:
1081 case H264_NAL_AUXILIARY_SLICE:
1083 H264RawSlice *slice = unit->content;
1085 int bits_left, end, zeroes;
1087 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1092 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1093 return AVERROR(ENOSPC);
1095 init_get_bits(&gbc, slice->data, slice->data_size * 8);
1096 skip_bits_long(&gbc, slice->data_bit_start);
1098 // Copy in two-byte blocks, but stop before copying the
1099 // rbsp_stop_one_bit in the final byte.
1100 while (get_bits_left(&gbc) > 23)
1101 put_bits(pbc, 16, get_bits(&gbc, 16));
1103 bits_left = get_bits_left(&gbc);
1104 end = get_bits(&gbc, bits_left);
1106 // rbsp_stop_one_bit must be present here.
1108 zeroes = ff_ctz(end);
1109 if (bits_left > zeroes + 1)
1110 put_bits(pbc, bits_left - zeroes - 1,
1111 end >> (zeroes + 1));
1112 put_bits(pbc, 1, 1);
1113 while (put_bits_count(pbc) % 8 != 0)
1114 put_bits(pbc, 1, 0);
1116 // No slice data - that was just the header.
1117 // (Bitstream may be unaligned!)
1124 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1132 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1138 case H264_NAL_FILLER_DATA:
1140 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1147 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1148 "NAL unit type %"PRIu32".\n", unit->type);
1149 return AVERROR_PATCHWELCOME;
1155 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1156 CodedBitstreamUnit *unit,
1161 switch (unit->type) {
1164 H265RawVPS *vps = unit->content;
1166 err = cbs_h265_write_vps(ctx, pbc, vps);
1170 err = cbs_h265_replace_vps(ctx, unit);
1178 H265RawSPS *sps = unit->content;
1180 err = cbs_h265_write_sps(ctx, pbc, sps);
1184 err = cbs_h265_replace_sps(ctx, unit);
1192 H265RawPPS *pps = unit->content;
1194 err = cbs_h265_write_pps(ctx, pbc, pps);
1198 err = cbs_h265_replace_pps(ctx, unit);
1204 case HEVC_NAL_TRAIL_N:
1205 case HEVC_NAL_TRAIL_R:
1206 case HEVC_NAL_TSA_N:
1207 case HEVC_NAL_TSA_R:
1208 case HEVC_NAL_STSA_N:
1209 case HEVC_NAL_STSA_R:
1210 case HEVC_NAL_RADL_N:
1211 case HEVC_NAL_RADL_R:
1212 case HEVC_NAL_RASL_N:
1213 case HEVC_NAL_RASL_R:
1214 case HEVC_NAL_BLA_W_LP:
1215 case HEVC_NAL_BLA_W_RADL:
1216 case HEVC_NAL_BLA_N_LP:
1217 case HEVC_NAL_IDR_W_RADL:
1218 case HEVC_NAL_IDR_N_LP:
1219 case HEVC_NAL_CRA_NUT:
1221 H265RawSlice *slice = unit->content;
1223 int bits_left, end, zeroes;
1225 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1230 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1231 return AVERROR(ENOSPC);
1233 init_get_bits(&gbc, slice->data, slice->data_size * 8);
1234 skip_bits_long(&gbc, slice->data_bit_start);
1236 // Copy in two-byte blocks, but stop before copying the
1237 // rbsp_stop_one_bit in the final byte.
1238 while (get_bits_left(&gbc) > 23)
1239 put_bits(pbc, 16, get_bits(&gbc, 16));
1241 bits_left = get_bits_left(&gbc);
1242 end = get_bits(&gbc, bits_left);
1244 // rbsp_stop_one_bit must be present here.
1246 zeroes = ff_ctz(end);
1247 if (bits_left > zeroes + 1)
1248 put_bits(pbc, bits_left - zeroes - 1,
1249 end >> (zeroes + 1));
1250 put_bits(pbc, 1, 1);
1251 while (put_bits_count(pbc) % 8 != 0)
1252 put_bits(pbc, 1, 0);
1254 // No slice data - that was just the header.
1261 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1267 case HEVC_NAL_SEI_PREFIX:
1269 err = cbs_h265_write_sei(ctx, pbc, unit->content);
1277 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1278 "NAL unit type %"PRIu32".\n", unit->type);
1279 return AVERROR_PATCHWELCOME;
1285 static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
1286 CodedBitstreamUnit *unit)
1288 CodedBitstreamH2645Context *priv = ctx->priv_data;
1289 enum AVCodecID codec_id = ctx->codec->codec_id;
1293 if (!priv->write_buffer) {
1294 // Initial write buffer size is 1MB.
1295 priv->write_buffer_size = 1024 * 1024;
1297 reallocate_and_try_again:
1298 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1300 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1301 "sufficiently large write buffer (last attempt "
1302 "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1307 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1309 if (codec_id == AV_CODEC_ID_H264)
1310 err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1312 err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1314 if (err == AVERROR(ENOSPC)) {
1316 priv->write_buffer_size *= 2;
1317 goto reallocate_and_try_again;
1319 // Overflow but we didn't notice.
1320 av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1323 // Write failed for some other reason.
1327 if (put_bits_count(&pbc) % 8)
1328 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1330 unit->data_bit_padding = 0;
1332 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1333 flush_put_bits(&pbc);
1335 err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1339 memcpy(unit->data, priv->write_buffer, unit->data_size);
1344 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1345 CodedBitstreamFragment *frag)
1348 size_t max_size, dp, sp;
1349 int err, i, zero_run;
1351 for (i = 0; i < frag->nb_units; i++) {
1352 // Data should already all have been written when we get here.
1353 av_assert0(frag->units[i].data);
1357 for (i = 0; i < frag->nb_units; i++) {
1358 // Start code + content with worst-case emulation prevention.
1359 max_size += 3 + frag->units[i].data_size * 3 / 2;
1362 data = av_malloc(max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1364 return AVERROR(ENOMEM);
1367 for (i = 0; i < frag->nb_units; i++) {
1368 CodedBitstreamUnit *unit = &frag->units[i];
1370 if (unit->data_bit_padding > 0) {
1371 if (i < frag->nb_units - 1)
1372 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1373 "unaligned padding on non-final NAL unit.\n");
1375 frag->data_bit_padding = unit->data_bit_padding;
1378 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1379 (unit->type == H264_NAL_SPS ||
1380 unit->type == H264_NAL_PPS)) ||
1381 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1382 (unit->type == HEVC_NAL_VPS ||
1383 unit->type == HEVC_NAL_SPS ||
1384 unit->type == HEVC_NAL_PPS)) ||
1385 i == 0 /* (Assume this is the start of an access unit.) */) {
1389 // start_code_prefix_one_3bytes
1395 for (sp = 0; sp < unit->data_size; sp++) {
1397 if (unit->data[sp] == 0)
1402 if ((unit->data[sp] & ~3) == 0) {
1403 // emulation_prevention_three_byte
1406 zero_run = unit->data[sp] == 0;
1408 data[dp++] = unit->data[sp];
1412 av_assert0(dp <= max_size);
1413 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1416 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1418 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1420 if (!frag->data_ref) {
1422 return AVERROR(ENOMEM);
1426 frag->data_size = dp;
1431 static void cbs_h264_close(CodedBitstreamContext *ctx)
1433 CodedBitstreamH264Context *h264 = ctx->priv_data;
1436 ff_h2645_packet_uninit(&h264->common.read_packet);
1438 av_freep(&h264->common.write_buffer);
1440 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1441 av_buffer_unref(&h264->sps_ref[i]);
1442 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1443 av_buffer_unref(&h264->pps_ref[i]);
1446 static void cbs_h265_close(CodedBitstreamContext *ctx)
1448 CodedBitstreamH265Context *h265 = ctx->priv_data;
1451 ff_h2645_packet_uninit(&h265->common.read_packet);
1453 av_freep(&h265->common.write_buffer);
1455 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1456 av_buffer_unref(&h265->vps_ref[i]);
1457 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1458 av_buffer_unref(&h265->sps_ref[i]);
1459 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1460 av_buffer_unref(&h265->pps_ref[i]);
1463 const CodedBitstreamType ff_cbs_type_h264 = {
1464 .codec_id = AV_CODEC_ID_H264,
1466 .priv_data_size = sizeof(CodedBitstreamH264Context),
1468 .split_fragment = &cbs_h2645_split_fragment,
1469 .read_unit = &cbs_h264_read_nal_unit,
1470 .write_unit = &cbs_h2645_write_nal_unit,
1471 .assemble_fragment = &cbs_h2645_assemble_fragment,
1473 .close = &cbs_h264_close,
1476 const CodedBitstreamType ff_cbs_type_h265 = {
1477 .codec_id = AV_CODEC_ID_HEVC,
1479 .priv_data_size = sizeof(CodedBitstreamH265Context),
1481 .split_fragment = &cbs_h2645_split_fragment,
1482 .read_unit = &cbs_h265_read_nal_unit,
1483 .write_unit = &cbs_h2645_write_nal_unit,
1484 .assemble_fragment = &cbs_h2645_assemble_fragment,
1486 .close = &cbs_h265_close,
1489 int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
1490 CodedBitstreamFragment *au,
1491 const H264RawSEIPayload *payload)
1494 CodedBitstreamUnit *nal = NULL;
1497 // Find an existing SEI NAL unit to add to.
1498 for (i = 0; i < au->nb_units; i++) {
1499 if (au->units[i].type == H264_NAL_SEI) {
1500 nal = &au->units[i];
1508 // Need to make a new SEI NAL unit. Insert it before the first
1509 // slice data NAL unit; if no slice data, add at the end.
1510 AVBufferRef *sei_ref;
1512 sei = av_mallocz(sizeof(*sei));
1514 return AVERROR(ENOMEM);
1516 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1517 sei->nal_unit_header.nal_ref_idc = 0;
1519 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1520 &cbs_h264_free_sei, ctx, 0);
1523 return AVERROR(ENOMEM);
1526 for (i = 0; i < au->nb_units; i++) {
1527 if (au->units[i].type == H264_NAL_SLICE ||
1528 au->units[i].type == H264_NAL_IDR_SLICE)
1532 err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1534 av_buffer_unref(&sei_ref);
1539 if (sei->payload_count >= H264_MAX_SEI_PAYLOADS) {
1540 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
1542 return AVERROR(EINVAL);
1545 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1546 ++sei->payload_count;
1551 int ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
1552 CodedBitstreamFragment *au,
1553 CodedBitstreamUnit *nal,
1556 H264RawSEI *sei = nal->content;
1558 av_assert0(nal->type == H264_NAL_SEI);
1559 av_assert0(position >= 0 && position < sei->payload_count);
1561 if (position == 0 && sei->payload_count == 1) {
1562 // Deleting NAL unit entirely.
1565 for (i = 0; i < au->nb_units; i++) {
1566 if (&au->units[i] == nal)
1569 av_assert0(i < au->nb_units && "NAL unit not in access unit.");
1571 return ff_cbs_delete_unit(ctx, au, i);
1573 cbs_h264_free_sei_payload(&sei->payload[position]);
1575 --sei->payload_count;
1576 memmove(sei->payload + position,
1577 sei->payload + position + 1,
1578 (sei->payload_count - position) * sizeof(*sei->payload));