2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
22 #include "bytestream.h"
24 #include "cbs_internal.h"
29 #include "h2645_parse.h"
34 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
35 const char *name, const int *subscripts,
37 uint32_t range_min, uint32_t range_max)
44 position = get_bits_count(gbc);
46 for (i = 0; i < 32; i++) {
47 if (get_bits_left(gbc) < i + 1) {
48 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
49 "%s: bitstream ended.\n", name);
50 return AVERROR_INVALIDDATA;
53 bits[i] = k ? '1' : '0';
58 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
59 "%s: more than 31 zeroes.\n", name);
60 return AVERROR_INVALIDDATA;
63 for (j = 0; j < i; j++) {
65 bits[i + j + 1] = k ? '1' : '0';
66 value = value << 1 | k;
71 if (ctx->trace_enable)
72 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
75 if (value < range_min || value > range_max) {
76 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
77 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
78 name, value, range_min, range_max);
79 return AVERROR_INVALIDDATA;
86 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
87 const char *name, const int *subscripts,
89 int32_t range_min, int32_t range_max)
97 position = get_bits_count(gbc);
99 for (i = 0; i < 32; i++) {
100 if (get_bits_left(gbc) < i + 1) {
101 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
102 "%s: bitstream ended.\n", name);
103 return AVERROR_INVALIDDATA;
106 bits[i] = k ? '1' : '0';
111 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
112 "%s: more than 31 zeroes.\n", name);
113 return AVERROR_INVALIDDATA;
116 for (j = 0; j < i; j++) {
118 bits[i + j + 1] = k ? '1' : '0';
123 value = -(int32_t)(v / 2);
127 if (ctx->trace_enable)
128 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
131 if (value < range_min || value > range_max) {
132 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
133 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
134 name, value, range_min, range_max);
135 return AVERROR_INVALIDDATA;
142 static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
143 const char *name, const int *subscripts,
145 uint32_t range_min, uint32_t range_max)
149 if (value < range_min || value > range_max) {
150 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
151 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
152 name, value, range_min, range_max);
153 return AVERROR_INVALIDDATA;
155 av_assert0(value != UINT32_MAX);
157 len = av_log2(value + 1);
158 if (put_bits_left(pbc) < 2 * len + 1)
159 return AVERROR(ENOSPC);
161 if (ctx->trace_enable) {
165 for (i = 0; i < len; i++)
168 for (i = 0; i < len; i++)
169 bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
170 bits[len + len + 1] = 0;
172 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
173 name, subscripts, bits, value);
176 put_bits(pbc, len, 0);
178 put_bits(pbc, len + 1, value + 1);
180 put_bits32(pbc, value + 1);
185 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
186 const char *name, const int *subscripts,
188 int32_t range_min, int32_t range_max)
193 if (value < range_min || value > range_max) {
194 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
195 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
196 name, value, range_min, range_max);
197 return AVERROR_INVALIDDATA;
199 av_assert0(value != INT32_MIN);
204 uvalue = 2 * (uint32_t)value - 1;
206 uvalue = 2 * (uint32_t)-value;
208 len = av_log2(uvalue + 1);
209 if (put_bits_left(pbc) < 2 * len + 1)
210 return AVERROR(ENOSPC);
212 if (ctx->trace_enable) {
216 for (i = 0; i < len; i++)
219 for (i = 0; i < len; i++)
220 bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
221 bits[len + len + 1] = 0;
223 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
224 name, subscripts, bits, value);
227 put_bits(pbc, len, 0);
229 put_bits(pbc, len + 1, uvalue + 1);
231 put_bits32(pbc, uvalue + 1);
236 // payload_extension_present() - true if we are before the last 1-bit
237 // in the payload structure, which must be in the last byte.
238 static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t payload_size,
241 int bits_left = payload_size * 8 - cur_pos;
242 return (bits_left > 0 &&
243 (bits_left > 7 || show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1)));
246 #define HEADER(name) do { \
247 ff_cbs_trace_header(ctx, name); \
250 #define CHECK(call) do { \
256 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
257 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
258 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
260 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
262 #define u(width, name, range_min, range_max) \
263 xu(width, name, current->name, range_min, range_max, 0, )
264 #define ub(width, name) \
265 xu(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
266 #define flag(name) ub(1, name)
267 #define ue(name, range_min, range_max) \
268 xue(name, current->name, range_min, range_max, 0, )
269 #define i(width, name, range_min, range_max) \
270 xi(width, name, current->name, range_min, range_max, 0, )
271 #define ib(width, name) \
272 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
273 #define se(name, range_min, range_max) \
274 xse(name, current->name, range_min, range_max, 0, )
276 #define us(width, name, range_min, range_max, subs, ...) \
277 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
278 #define ubs(width, name, subs, ...) \
279 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
280 #define flags(name, subs, ...) \
281 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
282 #define ues(name, range_min, range_max, subs, ...) \
283 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
284 #define is(width, name, range_min, range_max, subs, ...) \
285 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
286 #define ibs(width, name, subs, ...) \
287 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
288 #define ses(name, range_min, range_max, subs, ...) \
289 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
291 #define fixed(width, name, value) do { \
292 av_unused uint32_t fixed_value = value; \
293 xu(width, name, fixed_value, value, value, 0, ); \
298 #define READWRITE read
299 #define RWContext GetBitContext
301 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
303 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
304 SUBSCRIPTS(subs, __VA_ARGS__), \
305 &value, range_min, range_max)); \
308 #define xue(name, var, range_min, range_max, subs, ...) do { \
310 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
311 SUBSCRIPTS(subs, __VA_ARGS__), \
312 &value, range_min, range_max)); \
315 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
317 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
318 SUBSCRIPTS(subs, __VA_ARGS__), \
319 &value, range_min, range_max)); \
322 #define xse(name, var, range_min, range_max, subs, ...) do { \
324 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
325 SUBSCRIPTS(subs, __VA_ARGS__), \
326 &value, range_min, range_max)); \
331 #define infer(name, value) do { \
332 current->name = value; \
335 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
337 int bits_left = get_bits_left(gbc);
342 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
347 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
349 #define byte_alignment(rw) (get_bits_count(rw) % 8)
351 #define allocate(name, size) do { \
352 name ## _ref = av_buffer_allocz(size + \
353 AV_INPUT_BUFFER_PADDING_SIZE); \
355 return AVERROR(ENOMEM); \
356 name = name ## _ref->data; \
359 #define FUNC(name) FUNC_H264(READWRITE, name)
360 #include "cbs_h264_syntax_template.c"
363 #define FUNC(name) FUNC_H265(READWRITE, name)
364 #include "cbs_h265_syntax_template.c"
375 #undef more_rbsp_data
376 #undef byte_alignment
381 #define READWRITE write
382 #define RWContext PutBitContext
384 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
385 uint32_t value = var; \
386 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
387 SUBSCRIPTS(subs, __VA_ARGS__), \
388 value, range_min, range_max)); \
390 #define xue(name, var, range_min, range_max, subs, ...) do { \
391 uint32_t value = var; \
392 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
393 SUBSCRIPTS(subs, __VA_ARGS__), \
394 value, range_min, range_max)); \
396 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
397 int32_t value = var; \
398 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
399 SUBSCRIPTS(subs, __VA_ARGS__), \
400 value, range_min, range_max)); \
402 #define xse(name, var, range_min, range_max, subs, ...) do { \
403 int32_t value = var; \
404 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
405 SUBSCRIPTS(subs, __VA_ARGS__), \
406 value, range_min, range_max)); \
409 #define infer(name, value) do { \
410 if (current->name != (value)) { \
411 av_log(ctx->log_ctx, AV_LOG_ERROR, \
412 "%s does not match inferred value: " \
413 "%"PRId64", but should be %"PRId64".\n", \
414 #name, (int64_t)current->name, (int64_t)(value)); \
415 return AVERROR_INVALIDDATA; \
419 #define more_rbsp_data(var) (var)
421 #define byte_alignment(rw) (put_bits_count(rw) % 8)
423 #define allocate(name, size) do { \
425 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
426 "for writing.\n", #name); \
427 return AVERROR_INVALIDDATA; \
431 #define FUNC(name) FUNC_H264(READWRITE, name)
432 #include "cbs_h264_syntax_template.c"
435 #define FUNC(name) FUNC_H265(READWRITE, name)
436 #include "cbs_h265_syntax_template.c"
452 #undef more_rbsp_data
453 #undef byte_alignment
457 static void cbs_h265_free_vps(void *opaque, uint8_t *content)
459 H265RawVPS *vps = (H265RawVPS*)content;
460 av_buffer_unref(&vps->extension_data.data_ref);
464 static void cbs_h265_free_sps(void *opaque, uint8_t *content)
466 H265RawSPS *sps = (H265RawSPS*)content;
467 av_buffer_unref(&sps->extension_data.data_ref);
471 static void cbs_h265_free_pps(void *opaque, uint8_t *content)
473 H265RawPPS *pps = (H265RawPPS*)content;
474 av_buffer_unref(&pps->extension_data.data_ref);
478 static void cbs_h265_free_slice(void *opaque, uint8_t *content)
480 H265RawSlice *slice = (H265RawSlice*)content;
481 av_buffer_unref(&slice->data_ref);
485 static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
487 switch (payload->payload_type) {
488 case HEVC_SEI_TYPE_BUFFERING_PERIOD:
489 case HEVC_SEI_TYPE_PICTURE_TIMING:
490 case HEVC_SEI_TYPE_PAN_SCAN_RECT:
491 case HEVC_SEI_TYPE_RECOVERY_POINT:
492 case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
493 case HEVC_SEI_TYPE_ACTIVE_PARAMETER_SETS:
494 case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
495 case HEVC_SEI_TYPE_TIME_CODE:
496 case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
497 case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
498 case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
499 case HEVC_SEI_TYPE_ALPHA_CHANNEL_INFO:
501 case HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
502 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
504 case HEVC_SEI_TYPE_USER_DATA_UNREGISTERED:
505 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
508 av_buffer_unref(&payload->payload.other.data_ref);
511 av_buffer_unref(&payload->extension_data.data_ref);
514 static void cbs_h265_free_sei(void *opaque, uint8_t *content)
516 H265RawSEI *sei = (H265RawSEI*)content;
518 for (i = 0; i < sei->payload_count; i++)
519 cbs_h265_free_sei_payload(&sei->payload[i]);
523 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
524 CodedBitstreamFragment *frag,
525 const H2645Packet *packet)
529 for (i = 0; i < packet->nb_nals; i++) {
530 const H2645NAL *nal = &packet->nals[i];
532 size_t size = nal->size;
534 if (nal->nuh_layer_id > 0)
537 // Remove trailing zeroes.
538 while (size > 0 && nal->data[size - 1] == 0)
541 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
545 ref = (nal->data == nal->raw_data) ? frag->data_ref
546 : packet->rbsp.rbsp_buffer_ref;
548 err = ff_cbs_insert_unit_data(frag, -1, nal->type,
549 (uint8_t*)nal->data, size, ref);
557 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
558 CodedBitstreamFragment *frag,
561 enum AVCodecID codec_id = ctx->codec->codec_id;
562 CodedBitstreamH2645Context *priv = ctx->priv_data;
566 av_assert0(frag->data && frag->nb_units == 0);
567 if (frag->data_size == 0)
570 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
572 size_t size, start, end;
573 int i, count, version;
577 bytestream2_init(&gbc, frag->data, frag->data_size);
579 if (bytestream2_get_bytes_left(&gbc) < 6)
580 return AVERROR_INVALIDDATA;
582 version = bytestream2_get_byte(&gbc);
584 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
585 "first byte %u.\n", version);
586 return AVERROR_INVALIDDATA;
589 bytestream2_skip(&gbc, 3);
590 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
593 count = bytestream2_get_byte(&gbc) & 0x1f;
594 start = bytestream2_tell(&gbc);
595 for (i = 0; i < count; i++) {
596 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
597 return AVERROR_INVALIDDATA;
598 size = bytestream2_get_be16(&gbc);
599 if (bytestream2_get_bytes_left(&gbc) < size)
600 return AVERROR_INVALIDDATA;
601 bytestream2_skip(&gbc, size);
603 end = bytestream2_tell(&gbc);
605 err = ff_h2645_packet_split(&priv->read_packet,
606 frag->data + start, end - start,
607 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
609 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
612 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
617 count = bytestream2_get_byte(&gbc);
618 start = bytestream2_tell(&gbc);
619 for (i = 0; i < count; i++) {
620 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
621 return AVERROR_INVALIDDATA;
622 size = bytestream2_get_be16(&gbc);
623 if (bytestream2_get_bytes_left(&gbc) < size)
624 return AVERROR_INVALIDDATA;
625 bytestream2_skip(&gbc, size);
627 end = bytestream2_tell(&gbc);
629 err = ff_h2645_packet_split(&priv->read_packet,
630 frag->data + start, end - start,
631 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
633 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
636 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
640 if (bytestream2_get_bytes_left(&gbc) > 0) {
641 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
642 "header.\n", bytestream2_get_bytes_left(&gbc));
645 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
647 size_t size, start, end;
648 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
652 bytestream2_init(&gbc, frag->data, frag->data_size);
654 if (bytestream2_get_bytes_left(&gbc) < 23)
655 return AVERROR_INVALIDDATA;
657 version = bytestream2_get_byte(&gbc);
659 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
660 "first byte %u.\n", version);
661 return AVERROR_INVALIDDATA;
664 bytestream2_skip(&gbc, 20);
665 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
667 nb_arrays = bytestream2_get_byte(&gbc);
668 for (i = 0; i < nb_arrays; i++) {
669 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
670 nb_nals = bytestream2_get_be16(&gbc);
672 start = bytestream2_tell(&gbc);
673 for (j = 0; j < nb_nals; j++) {
674 if (bytestream2_get_bytes_left(&gbc) < 2)
675 return AVERROR_INVALIDDATA;
676 size = bytestream2_get_be16(&gbc);
677 if (bytestream2_get_bytes_left(&gbc) < size)
678 return AVERROR_INVALIDDATA;
679 bytestream2_skip(&gbc, size);
681 end = bytestream2_tell(&gbc);
683 err = ff_h2645_packet_split(&priv->read_packet,
684 frag->data + start, end - start,
685 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
687 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
688 "HVCC array %d (%d NAL units of type %d).\n",
689 i, nb_nals, nal_unit_type);
692 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
698 // Annex B, or later MP4 with already-known parameters.
700 err = ff_h2645_packet_split(&priv->read_packet,
701 frag->data, frag->data_size,
703 priv->mp4, priv->nal_length_size,
708 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
716 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
717 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
718 CodedBitstreamUnit *unit) \
720 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
721 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
722 unsigned int id = ps_var->id_element; \
723 if (id >= FF_ARRAY_ELEMS(priv->ps_var)) { \
724 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
725 " id : %d.\n", id); \
726 return AVERROR_INVALIDDATA; \
728 if (priv->ps_var[id] == priv->active_ ## ps_var) \
729 priv->active_ ## ps_var = NULL ; \
730 av_buffer_unref(&priv->ps_var ## _ref[id]); \
731 if (unit->content_ref) \
732 priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
734 priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
735 if (!priv->ps_var ## _ref[id]) \
736 return AVERROR(ENOMEM); \
737 priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
738 if (!unit->content_ref) \
739 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
743 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
744 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
745 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
746 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
747 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
749 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
750 CodedBitstreamUnit *unit)
755 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
759 err = ff_cbs_alloc_unit_content2(ctx, unit);
763 switch (unit->type) {
766 H264RawSPS *sps = unit->content;
768 err = cbs_h264_read_sps(ctx, &gbc, sps);
772 err = cbs_h264_replace_sps(ctx, unit);
778 case H264_NAL_SPS_EXT:
780 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
788 H264RawPPS *pps = unit->content;
790 err = cbs_h264_read_pps(ctx, &gbc, pps);
794 err = cbs_h264_replace_pps(ctx, unit);
801 case H264_NAL_IDR_SLICE:
802 case H264_NAL_AUXILIARY_SLICE:
804 H264RawSlice *slice = unit->content;
807 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
811 if (!cbs_h2645_read_more_rbsp_data(&gbc))
812 return AVERROR_INVALIDDATA;
814 pos = get_bits_count(&gbc);
815 len = unit->data_size;
817 slice->data_size = len - pos / 8;
818 slice->data_ref = av_buffer_ref(unit->data_ref);
819 if (!slice->data_ref)
820 return AVERROR(ENOMEM);
821 slice->data = unit->data + pos / 8;
822 slice->data_bit_start = pos % 8;
828 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
836 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
842 case H264_NAL_FILLER_DATA:
844 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
850 case H264_NAL_END_SEQUENCE:
851 case H264_NAL_END_STREAM:
853 err = (unit->type == H264_NAL_END_SEQUENCE ?
854 cbs_h264_read_end_of_sequence :
855 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
862 return AVERROR(ENOSYS);
868 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
869 CodedBitstreamUnit *unit)
874 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
878 switch (unit->type) {
883 err = ff_cbs_alloc_unit_content(unit, sizeof(*vps),
889 err = cbs_h265_read_vps(ctx, &gbc, vps);
893 err = cbs_h265_replace_vps(ctx, unit);
902 err = ff_cbs_alloc_unit_content(unit, sizeof(*sps),
908 err = cbs_h265_read_sps(ctx, &gbc, sps);
912 err = cbs_h265_replace_sps(ctx, unit);
922 err = ff_cbs_alloc_unit_content(unit, sizeof(*pps),
928 err = cbs_h265_read_pps(ctx, &gbc, pps);
932 err = cbs_h265_replace_pps(ctx, unit);
938 case HEVC_NAL_TRAIL_N:
939 case HEVC_NAL_TRAIL_R:
942 case HEVC_NAL_STSA_N:
943 case HEVC_NAL_STSA_R:
944 case HEVC_NAL_RADL_N:
945 case HEVC_NAL_RADL_R:
946 case HEVC_NAL_RASL_N:
947 case HEVC_NAL_RASL_R:
948 case HEVC_NAL_BLA_W_LP:
949 case HEVC_NAL_BLA_W_RADL:
950 case HEVC_NAL_BLA_N_LP:
951 case HEVC_NAL_IDR_W_RADL:
952 case HEVC_NAL_IDR_N_LP:
953 case HEVC_NAL_CRA_NUT:
958 err = ff_cbs_alloc_unit_content(unit, sizeof(*slice),
959 &cbs_h265_free_slice);
962 slice = unit->content;
964 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
968 if (!cbs_h2645_read_more_rbsp_data(&gbc))
969 return AVERROR_INVALIDDATA;
971 pos = get_bits_count(&gbc);
972 len = unit->data_size;
974 slice->data_size = len - pos / 8;
975 slice->data_ref = av_buffer_ref(unit->data_ref);
976 if (!slice->data_ref)
977 return AVERROR(ENOMEM);
978 slice->data = unit->data + pos / 8;
979 slice->data_bit_start = pos % 8;
985 err = ff_cbs_alloc_unit_content(unit,
986 sizeof(H265RawAUD), NULL);
990 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
996 case HEVC_NAL_SEI_PREFIX:
997 case HEVC_NAL_SEI_SUFFIX:
999 err = ff_cbs_alloc_unit_content(unit, sizeof(H265RawSEI),
1000 &cbs_h265_free_sei);
1005 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1006 unit->type == HEVC_NAL_SEI_PREFIX);
1014 return AVERROR(ENOSYS);
1020 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1021 PutBitContext *pbc, const uint8_t *data,
1022 size_t data_size, int data_bit_start)
1024 size_t rest = data_size - (data_bit_start + 7) / 8;
1025 const uint8_t *pos = data + data_bit_start / 8;
1027 av_assert0(data_bit_start >= 0 &&
1028 data_size > data_bit_start / 8);
1030 if (data_size * 8 + 8 > put_bits_left(pbc))
1031 return AVERROR(ENOSPC);
1034 goto rbsp_stop_one_bit;
1036 // First copy the remaining bits of the first byte
1037 // The above check ensures that we do not accidentally
1038 // copy beyond the rbsp_stop_one_bit.
1039 if (data_bit_start % 8)
1040 put_bits(pbc, 8 - data_bit_start % 8,
1041 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1043 if (put_bits_count(pbc) % 8 == 0) {
1044 // If the writer is aligned at this point,
1045 // memcpy can be used to improve performance.
1046 // This happens normally for CABAC.
1047 flush_put_bits(pbc);
1048 memcpy(put_bits_ptr(pbc), pos, rest);
1049 skip_put_bytes(pbc, rest);
1051 // If not, we have to copy manually.
1052 // rbsp_stop_one_bit forces us to special-case
1057 for (; rest > 4; rest -= 4, pos += 4)
1058 put_bits32(pbc, AV_RB32(pos));
1060 for (; rest > 1; rest--, pos++)
1061 put_bits(pbc, 8, *pos);
1064 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1069 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1070 put_bits(pbc, i, temp);
1071 if (put_bits_count(pbc) % 8)
1072 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1078 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1079 CodedBitstreamUnit *unit,
1084 switch (unit->type) {
1087 H264RawSPS *sps = unit->content;
1089 err = cbs_h264_write_sps(ctx, pbc, sps);
1093 err = cbs_h264_replace_sps(ctx, unit);
1099 case H264_NAL_SPS_EXT:
1101 H264RawSPSExtension *sps_ext = unit->content;
1103 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1111 H264RawPPS *pps = unit->content;
1113 err = cbs_h264_write_pps(ctx, pbc, pps);
1117 err = cbs_h264_replace_pps(ctx, unit);
1123 case H264_NAL_SLICE:
1124 case H264_NAL_IDR_SLICE:
1125 case H264_NAL_AUXILIARY_SLICE:
1127 H264RawSlice *slice = unit->content;
1129 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1134 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1136 slice->data_bit_start);
1140 // No slice data - that was just the header.
1141 // (Bitstream may be unaligned!)
1148 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1156 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1162 case H264_NAL_FILLER_DATA:
1164 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1170 case H264_NAL_END_SEQUENCE:
1172 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1178 case H264_NAL_END_STREAM:
1180 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1187 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1188 "NAL unit type %"PRIu32".\n", unit->type);
1189 return AVERROR_PATCHWELCOME;
1195 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1196 CodedBitstreamUnit *unit,
1201 switch (unit->type) {
1204 H265RawVPS *vps = unit->content;
1206 err = cbs_h265_write_vps(ctx, pbc, vps);
1210 err = cbs_h265_replace_vps(ctx, unit);
1218 H265RawSPS *sps = unit->content;
1220 err = cbs_h265_write_sps(ctx, pbc, sps);
1224 err = cbs_h265_replace_sps(ctx, unit);
1232 H265RawPPS *pps = unit->content;
1234 err = cbs_h265_write_pps(ctx, pbc, pps);
1238 err = cbs_h265_replace_pps(ctx, unit);
1244 case HEVC_NAL_TRAIL_N:
1245 case HEVC_NAL_TRAIL_R:
1246 case HEVC_NAL_TSA_N:
1247 case HEVC_NAL_TSA_R:
1248 case HEVC_NAL_STSA_N:
1249 case HEVC_NAL_STSA_R:
1250 case HEVC_NAL_RADL_N:
1251 case HEVC_NAL_RADL_R:
1252 case HEVC_NAL_RASL_N:
1253 case HEVC_NAL_RASL_R:
1254 case HEVC_NAL_BLA_W_LP:
1255 case HEVC_NAL_BLA_W_RADL:
1256 case HEVC_NAL_BLA_N_LP:
1257 case HEVC_NAL_IDR_W_RADL:
1258 case HEVC_NAL_IDR_N_LP:
1259 case HEVC_NAL_CRA_NUT:
1261 H265RawSlice *slice = unit->content;
1263 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1268 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1270 slice->data_bit_start);
1274 // No slice data - that was just the header.
1281 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1287 case HEVC_NAL_SEI_PREFIX:
1288 case HEVC_NAL_SEI_SUFFIX:
1290 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1291 unit->type == HEVC_NAL_SEI_PREFIX);
1299 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1300 "NAL unit type %"PRIu32".\n", unit->type);
1301 return AVERROR_PATCHWELCOME;
1307 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1308 CodedBitstreamFragment *frag)
1311 size_t max_size, dp, sp;
1312 int err, i, zero_run;
1314 for (i = 0; i < frag->nb_units; i++) {
1315 // Data should already all have been written when we get here.
1316 av_assert0(frag->units[i].data);
1320 for (i = 0; i < frag->nb_units; i++) {
1321 // Start code + content with worst-case emulation prevention.
1322 max_size += 4 + frag->units[i].data_size * 3 / 2;
1325 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1327 return AVERROR(ENOMEM);
1330 for (i = 0; i < frag->nb_units; i++) {
1331 CodedBitstreamUnit *unit = &frag->units[i];
1333 if (unit->data_bit_padding > 0) {
1334 if (i < frag->nb_units - 1)
1335 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1336 "unaligned padding on non-final NAL unit.\n");
1338 frag->data_bit_padding = unit->data_bit_padding;
1341 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1342 (unit->type == H264_NAL_SPS ||
1343 unit->type == H264_NAL_PPS)) ||
1344 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1345 (unit->type == HEVC_NAL_VPS ||
1346 unit->type == HEVC_NAL_SPS ||
1347 unit->type == HEVC_NAL_PPS)) ||
1348 i == 0 /* (Assume this is the start of an access unit.) */) {
1352 // start_code_prefix_one_3bytes
1358 for (sp = 0; sp < unit->data_size; sp++) {
1360 if (unit->data[sp] == 0)
1365 if ((unit->data[sp] & ~3) == 0) {
1366 // emulation_prevention_three_byte
1369 zero_run = unit->data[sp] == 0;
1371 data[dp++] = unit->data[sp];
1375 av_assert0(dp <= max_size);
1376 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1379 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1381 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1383 if (!frag->data_ref) {
1385 return AVERROR(ENOMEM);
1389 frag->data_size = dp;
1394 static void cbs_h264_close(CodedBitstreamContext *ctx)
1396 CodedBitstreamH264Context *h264 = ctx->priv_data;
1399 ff_h2645_packet_uninit(&h264->common.read_packet);
1401 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1402 av_buffer_unref(&h264->sps_ref[i]);
1403 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1404 av_buffer_unref(&h264->pps_ref[i]);
1407 static void cbs_h265_close(CodedBitstreamContext *ctx)
1409 CodedBitstreamH265Context *h265 = ctx->priv_data;
1412 ff_h2645_packet_uninit(&h265->common.read_packet);
1414 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1415 av_buffer_unref(&h265->vps_ref[i]);
1416 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1417 av_buffer_unref(&h265->sps_ref[i]);
1418 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1419 av_buffer_unref(&h265->pps_ref[i]);
1422 static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
1424 switch (payload->payload_type) {
1425 case H264_SEI_TYPE_BUFFERING_PERIOD:
1426 case H264_SEI_TYPE_PIC_TIMING:
1427 case H264_SEI_TYPE_PAN_SCAN_RECT:
1428 case H264_SEI_TYPE_RECOVERY_POINT:
1429 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
1430 case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
1431 case H264_SEI_TYPE_ALTERNATIVE_TRANSFER:
1433 case H264_SEI_TYPE_USER_DATA_REGISTERED:
1434 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
1436 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
1437 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
1440 av_buffer_unref(&payload->payload.other.data_ref);
1445 static void cbs_h264_free_sei(void *opaque, uint8_t *content)
1447 H264RawSEI *sei = (H264RawSEI*)content;
1449 for (i = 0; i < sei->payload_count; i++)
1450 cbs_h264_free_sei_payload(&sei->payload[i]);
1454 static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
1455 CBS_UNIT_TYPE_POD(H264_NAL_SPS, H264RawSPS),
1456 CBS_UNIT_TYPE_POD(H264_NAL_SPS_EXT, H264RawSPSExtension),
1458 CBS_UNIT_TYPE_INTERNAL_REF(H264_NAL_PPS, H264RawPPS, slice_group_id),
1465 H264_NAL_AUXILIARY_SLICE,
1467 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
1468 .content_size = sizeof(H264RawSlice),
1469 .nb_ref_offsets = 1,
1470 .ref_offsets = { offsetof(H264RawSlice, data) },
1473 CBS_UNIT_TYPE_POD(H264_NAL_AUD, H264RawAUD),
1474 CBS_UNIT_TYPE_POD(H264_NAL_FILLER_DATA, H264RawFiller),
1475 CBS_UNIT_TYPE_POD(H264_NAL_END_SEQUENCE, H264RawNALUnitHeader),
1476 CBS_UNIT_TYPE_POD(H264_NAL_END_STREAM, H264RawNALUnitHeader),
1478 CBS_UNIT_TYPE_COMPLEX(H264_NAL_SEI, H264RawSEI, &cbs_h264_free_sei),
1480 CBS_UNIT_TYPE_END_OF_LIST
1483 const CodedBitstreamType ff_cbs_type_h264 = {
1484 .codec_id = AV_CODEC_ID_H264,
1486 .priv_data_size = sizeof(CodedBitstreamH264Context),
1488 .unit_types = cbs_h264_unit_types,
1490 .split_fragment = &cbs_h2645_split_fragment,
1491 .read_unit = &cbs_h264_read_nal_unit,
1492 .write_unit = &cbs_h264_write_nal_unit,
1493 .assemble_fragment = &cbs_h2645_assemble_fragment,
1495 .close = &cbs_h264_close,
1498 const CodedBitstreamType ff_cbs_type_h265 = {
1499 .codec_id = AV_CODEC_ID_HEVC,
1501 .priv_data_size = sizeof(CodedBitstreamH265Context),
1503 .split_fragment = &cbs_h2645_split_fragment,
1504 .read_unit = &cbs_h265_read_nal_unit,
1505 .write_unit = &cbs_h265_write_nal_unit,
1506 .assemble_fragment = &cbs_h2645_assemble_fragment,
1508 .close = &cbs_h265_close,
1511 int ff_cbs_h264_add_sei_message(CodedBitstreamFragment *au,
1512 H264RawSEIPayload *payload)
1514 H264RawSEI *sei = NULL;
1517 // Find an existing SEI NAL unit to add to.
1518 for (i = 0; i < au->nb_units; i++) {
1519 if (au->units[i].type == H264_NAL_SEI) {
1520 sei = au->units[i].content;
1521 if (sei->payload_count < H264_MAX_SEI_PAYLOADS)
1529 // Need to make a new SEI NAL unit. Insert it before the first
1530 // slice data NAL unit; if no slice data, add at the end.
1531 AVBufferRef *sei_ref;
1533 sei = av_mallocz(sizeof(*sei));
1535 err = AVERROR(ENOMEM);
1539 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1540 sei->nal_unit_header.nal_ref_idc = 0;
1542 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1543 &cbs_h264_free_sei, NULL, 0);
1546 err = AVERROR(ENOMEM);
1550 for (i = 0; i < au->nb_units; i++) {
1551 if (au->units[i].type == H264_NAL_SLICE ||
1552 au->units[i].type == H264_NAL_IDR_SLICE)
1556 err = ff_cbs_insert_unit_content(au, i, H264_NAL_SEI,
1558 av_buffer_unref(&sei_ref);
1563 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1564 ++sei->payload_count;
1568 cbs_h264_free_sei_payload(payload);
1572 void ff_cbs_h264_delete_sei_message(CodedBitstreamFragment *au,
1573 CodedBitstreamUnit *nal,
1576 H264RawSEI *sei = nal->content;
1578 av_assert0(nal->type == H264_NAL_SEI);
1579 av_assert0(position >= 0 && position < sei->payload_count);
1581 if (position == 0 && sei->payload_count == 1) {
1582 // Deleting NAL unit entirely.
1585 for (i = 0; i < au->nb_units; i++) {
1586 if (&au->units[i] == nal)
1590 ff_cbs_delete_unit(au, i);
1592 cbs_h264_free_sei_payload(&sei->payload[position]);
1594 --sei->payload_count;
1595 memmove(sei->payload + position,
1596 sei->payload + position + 1,
1597 (sei->payload_count - position) * sizeof(*sei->payload));