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_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
257 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
258 #define FUNC_H264(name) FUNC_NAME1(READWRITE, h264, name)
259 #define FUNC_H265(name) FUNC_NAME1(READWRITE, h265, name)
260 #define FUNC_SEI(name) FUNC_NAME1(READWRITE, sei, name)
262 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
264 #define u(width, name, range_min, range_max) \
265 xu(width, name, current->name, range_min, range_max, 0, )
266 #define ub(width, name) \
267 xu(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
268 #define flag(name) ub(1, name)
269 #define ue(name, range_min, range_max) \
270 xue(name, current->name, range_min, range_max, 0, )
271 #define i(width, name, range_min, range_max) \
272 xi(width, name, current->name, range_min, range_max, 0, )
273 #define ib(width, name) \
274 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
275 #define se(name, range_min, range_max) \
276 xse(name, current->name, range_min, range_max, 0, )
278 #define us(width, name, range_min, range_max, subs, ...) \
279 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
280 #define ubs(width, name, subs, ...) \
281 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
282 #define flags(name, subs, ...) \
283 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
284 #define ues(name, range_min, range_max, subs, ...) \
285 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
286 #define is(width, name, range_min, range_max, subs, ...) \
287 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
288 #define ibs(width, name, subs, ...) \
289 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
290 #define ses(name, range_min, range_max, subs, ...) \
291 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
293 #define fixed(width, name, value) do { \
294 av_unused uint32_t fixed_value = value; \
295 xu(width, name, fixed_value, value, value, 0, ); \
300 #define READWRITE read
301 #define RWContext GetBitContext
303 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
305 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
306 SUBSCRIPTS(subs, __VA_ARGS__), \
307 &value, range_min, range_max)); \
310 #define xue(name, var, range_min, range_max, subs, ...) do { \
312 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
313 SUBSCRIPTS(subs, __VA_ARGS__), \
314 &value, range_min, range_max)); \
317 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
319 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
320 SUBSCRIPTS(subs, __VA_ARGS__), \
321 &value, range_min, range_max)); \
324 #define xse(name, var, range_min, range_max, subs, ...) do { \
326 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
327 SUBSCRIPTS(subs, __VA_ARGS__), \
328 &value, range_min, range_max)); \
333 #define infer(name, value) do { \
334 current->name = value; \
337 static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
339 int bits_left = get_bits_left(gbc);
344 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
349 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
351 #define byte_alignment(rw) (get_bits_count(rw) % 8)
353 #define allocate(name, size) do { \
354 name ## _ref = av_buffer_allocz(size + \
355 AV_INPUT_BUFFER_PADDING_SIZE); \
357 return AVERROR(ENOMEM); \
358 name = name ## _ref->data; \
361 #define FUNC(name) FUNC_SEI(name)
362 #include "cbs_sei_syntax_template.c"
365 #define FUNC(name) FUNC_H264(name)
366 #include "cbs_h264_syntax_template.c"
369 #define FUNC(name) FUNC_H265(name)
370 #include "cbs_h265_syntax_template.c"
381 #undef more_rbsp_data
382 #undef byte_alignment
387 #define READWRITE write
388 #define RWContext PutBitContext
390 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
391 uint32_t value = var; \
392 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
393 SUBSCRIPTS(subs, __VA_ARGS__), \
394 value, range_min, range_max)); \
396 #define xue(name, var, range_min, range_max, subs, ...) do { \
397 uint32_t value = var; \
398 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
399 SUBSCRIPTS(subs, __VA_ARGS__), \
400 value, range_min, range_max)); \
402 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
403 int32_t value = var; \
404 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
405 SUBSCRIPTS(subs, __VA_ARGS__), \
406 value, range_min, range_max)); \
408 #define xse(name, var, range_min, range_max, subs, ...) do { \
409 int32_t value = var; \
410 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
411 SUBSCRIPTS(subs, __VA_ARGS__), \
412 value, range_min, range_max)); \
415 #define infer(name, value) do { \
416 if (current->name != (value)) { \
417 av_log(ctx->log_ctx, AV_LOG_ERROR, \
418 "%s does not match inferred value: " \
419 "%"PRId64", but should be %"PRId64".\n", \
420 #name, (int64_t)current->name, (int64_t)(value)); \
421 return AVERROR_INVALIDDATA; \
425 #define more_rbsp_data(var) (var)
427 #define byte_alignment(rw) (put_bits_count(rw) % 8)
429 #define allocate(name, size) do { \
431 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
432 "for writing.\n", #name); \
433 return AVERROR_INVALIDDATA; \
437 #define FUNC(name) FUNC_SEI(name)
438 #include "cbs_sei_syntax_template.c"
441 #define FUNC(name) FUNC_H264(name)
442 #include "cbs_h264_syntax_template.c"
445 #define FUNC(name) FUNC_H265(name)
446 #include "cbs_h265_syntax_template.c"
462 #undef more_rbsp_data
463 #undef byte_alignment
467 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
468 CodedBitstreamFragment *frag,
469 const H2645Packet *packet)
473 for (i = 0; i < packet->nb_nals; i++) {
474 const H2645NAL *nal = &packet->nals[i];
476 size_t size = nal->size;
478 if (nal->nuh_layer_id > 0)
481 // Remove trailing zeroes.
482 while (size > 0 && nal->data[size - 1] == 0)
485 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
489 ref = (nal->data == nal->raw_data) ? frag->data_ref
490 : packet->rbsp.rbsp_buffer_ref;
492 err = ff_cbs_insert_unit_data(frag, -1, nal->type,
493 (uint8_t*)nal->data, size, ref);
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.\n", 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, 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, 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.\n", 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, 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 CodedBitstreamUnit *unit) \
664 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
665 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
666 unsigned int id = ps_var->id_element; \
668 if (id >= FF_ARRAY_ELEMS(priv->ps_var)) { \
669 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
670 " id : %d.\n", id); \
671 return AVERROR_INVALIDDATA; \
673 err = ff_cbs_make_unit_refcounted(ctx, unit); \
676 if (priv->ps_var[id] == priv->active_ ## ps_var) \
677 priv->active_ ## ps_var = NULL ; \
678 av_buffer_unref(&priv->ps_var ## _ref[id]); \
679 av_assert0(unit->content_ref); \
680 priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
681 if (!priv->ps_var ## _ref[id]) \
682 return AVERROR(ENOMEM); \
683 priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
687 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
688 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
689 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
690 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
691 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
693 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
694 CodedBitstreamUnit *unit)
699 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
703 err = ff_cbs_alloc_unit_content2(ctx, unit);
707 switch (unit->type) {
710 H264RawSPS *sps = unit->content;
712 err = cbs_h264_read_sps(ctx, &gbc, sps);
716 err = cbs_h264_replace_sps(ctx, unit);
722 case H264_NAL_SPS_EXT:
724 err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
732 H264RawPPS *pps = unit->content;
734 err = cbs_h264_read_pps(ctx, &gbc, pps);
738 err = cbs_h264_replace_pps(ctx, unit);
745 case H264_NAL_IDR_SLICE:
746 case H264_NAL_AUXILIARY_SLICE:
748 H264RawSlice *slice = unit->content;
751 err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
755 if (!cbs_h2645_read_more_rbsp_data(&gbc))
756 return AVERROR_INVALIDDATA;
758 pos = get_bits_count(&gbc);
759 len = unit->data_size;
761 slice->data_size = len - pos / 8;
762 slice->data_ref = av_buffer_ref(unit->data_ref);
763 if (!slice->data_ref)
764 return AVERROR(ENOMEM);
765 slice->data = unit->data + pos / 8;
766 slice->data_bit_start = pos % 8;
772 err = cbs_h264_read_aud(ctx, &gbc, unit->content);
780 err = cbs_h264_read_sei(ctx, &gbc, unit->content);
786 case H264_NAL_FILLER_DATA:
788 err = cbs_h264_read_filler(ctx, &gbc, unit->content);
794 case H264_NAL_END_SEQUENCE:
795 case H264_NAL_END_STREAM:
797 err = (unit->type == H264_NAL_END_SEQUENCE ?
798 cbs_h264_read_end_of_sequence :
799 cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
806 return AVERROR(ENOSYS);
812 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
813 CodedBitstreamUnit *unit)
818 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
822 err = ff_cbs_alloc_unit_content2(ctx, unit);
826 switch (unit->type) {
829 H265RawVPS *vps = unit->content;
831 err = cbs_h265_read_vps(ctx, &gbc, vps);
835 err = cbs_h265_replace_vps(ctx, unit);
842 H265RawSPS *sps = unit->content;
844 err = cbs_h265_read_sps(ctx, &gbc, sps);
848 err = cbs_h265_replace_sps(ctx, unit);
856 H265RawPPS *pps = unit->content;
858 err = cbs_h265_read_pps(ctx, &gbc, pps);
862 err = cbs_h265_replace_pps(ctx, unit);
868 case HEVC_NAL_TRAIL_N:
869 case HEVC_NAL_TRAIL_R:
872 case HEVC_NAL_STSA_N:
873 case HEVC_NAL_STSA_R:
874 case HEVC_NAL_RADL_N:
875 case HEVC_NAL_RADL_R:
876 case HEVC_NAL_RASL_N:
877 case HEVC_NAL_RASL_R:
878 case HEVC_NAL_BLA_W_LP:
879 case HEVC_NAL_BLA_W_RADL:
880 case HEVC_NAL_BLA_N_LP:
881 case HEVC_NAL_IDR_W_RADL:
882 case HEVC_NAL_IDR_N_LP:
883 case HEVC_NAL_CRA_NUT:
885 H265RawSlice *slice = unit->content;
888 err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
892 if (!cbs_h2645_read_more_rbsp_data(&gbc))
893 return AVERROR_INVALIDDATA;
895 pos = get_bits_count(&gbc);
896 len = unit->data_size;
898 slice->data_size = len - pos / 8;
899 slice->data_ref = av_buffer_ref(unit->data_ref);
900 if (!slice->data_ref)
901 return AVERROR(ENOMEM);
902 slice->data = unit->data + pos / 8;
903 slice->data_bit_start = pos % 8;
909 err = cbs_h265_read_aud(ctx, &gbc, unit->content);
915 case HEVC_NAL_SEI_PREFIX:
916 case HEVC_NAL_SEI_SUFFIX:
918 err = cbs_h265_read_sei(ctx, &gbc, unit->content,
919 unit->type == HEVC_NAL_SEI_PREFIX);
927 return AVERROR(ENOSYS);
933 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
934 PutBitContext *pbc, const uint8_t *data,
935 size_t data_size, int data_bit_start)
937 size_t rest = data_size - (data_bit_start + 7) / 8;
938 const uint8_t *pos = data + data_bit_start / 8;
940 av_assert0(data_bit_start >= 0 &&
941 data_size > data_bit_start / 8);
943 if (data_size * 8 + 8 > put_bits_left(pbc))
944 return AVERROR(ENOSPC);
947 goto rbsp_stop_one_bit;
949 // First copy the remaining bits of the first byte
950 // The above check ensures that we do not accidentally
951 // copy beyond the rbsp_stop_one_bit.
952 if (data_bit_start % 8)
953 put_bits(pbc, 8 - data_bit_start % 8,
954 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
956 if (put_bits_count(pbc) % 8 == 0) {
957 // If the writer is aligned at this point,
958 // memcpy can be used to improve performance.
959 // This happens normally for CABAC.
961 memcpy(put_bits_ptr(pbc), pos, rest);
962 skip_put_bytes(pbc, rest);
964 // If not, we have to copy manually.
965 // rbsp_stop_one_bit forces us to special-case
970 for (; rest > 4; rest -= 4, pos += 4)
971 put_bits32(pbc, AV_RB32(pos));
973 for (; rest > 1; rest--, pos++)
974 put_bits(pbc, 8, *pos);
977 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
982 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
983 put_bits(pbc, i, temp);
984 if (put_bits_count(pbc) % 8)
985 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
991 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
992 CodedBitstreamUnit *unit,
997 switch (unit->type) {
1000 H264RawSPS *sps = unit->content;
1002 err = cbs_h264_write_sps(ctx, pbc, sps);
1006 err = cbs_h264_replace_sps(ctx, unit);
1012 case H264_NAL_SPS_EXT:
1014 H264RawSPSExtension *sps_ext = unit->content;
1016 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1024 H264RawPPS *pps = unit->content;
1026 err = cbs_h264_write_pps(ctx, pbc, pps);
1030 err = cbs_h264_replace_pps(ctx, unit);
1036 case H264_NAL_SLICE:
1037 case H264_NAL_IDR_SLICE:
1038 case H264_NAL_AUXILIARY_SLICE:
1040 H264RawSlice *slice = unit->content;
1042 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1047 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1049 slice->data_bit_start);
1053 // No slice data - that was just the header.
1054 // (Bitstream may be unaligned!)
1061 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1069 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1075 case H264_NAL_FILLER_DATA:
1077 err = cbs_h264_write_filler(ctx, pbc, unit->content);
1083 case H264_NAL_END_SEQUENCE:
1085 err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1091 case H264_NAL_END_STREAM:
1093 err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1100 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1101 "NAL unit type %"PRIu32".\n", unit->type);
1102 return AVERROR_PATCHWELCOME;
1108 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1109 CodedBitstreamUnit *unit,
1114 switch (unit->type) {
1117 H265RawVPS *vps = unit->content;
1119 err = cbs_h265_write_vps(ctx, pbc, vps);
1123 err = cbs_h265_replace_vps(ctx, unit);
1131 H265RawSPS *sps = unit->content;
1133 err = cbs_h265_write_sps(ctx, pbc, sps);
1137 err = cbs_h265_replace_sps(ctx, unit);
1145 H265RawPPS *pps = unit->content;
1147 err = cbs_h265_write_pps(ctx, pbc, pps);
1151 err = cbs_h265_replace_pps(ctx, unit);
1157 case HEVC_NAL_TRAIL_N:
1158 case HEVC_NAL_TRAIL_R:
1159 case HEVC_NAL_TSA_N:
1160 case HEVC_NAL_TSA_R:
1161 case HEVC_NAL_STSA_N:
1162 case HEVC_NAL_STSA_R:
1163 case HEVC_NAL_RADL_N:
1164 case HEVC_NAL_RADL_R:
1165 case HEVC_NAL_RASL_N:
1166 case HEVC_NAL_RASL_R:
1167 case HEVC_NAL_BLA_W_LP:
1168 case HEVC_NAL_BLA_W_RADL:
1169 case HEVC_NAL_BLA_N_LP:
1170 case HEVC_NAL_IDR_W_RADL:
1171 case HEVC_NAL_IDR_N_LP:
1172 case HEVC_NAL_CRA_NUT:
1174 H265RawSlice *slice = unit->content;
1176 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1181 err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1183 slice->data_bit_start);
1187 // No slice data - that was just the header.
1194 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1200 case HEVC_NAL_SEI_PREFIX:
1201 case HEVC_NAL_SEI_SUFFIX:
1203 err = cbs_h265_write_sei(ctx, pbc, unit->content,
1204 unit->type == HEVC_NAL_SEI_PREFIX);
1212 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1213 "NAL unit type %"PRIu32".\n", unit->type);
1214 return AVERROR_PATCHWELCOME;
1220 static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id,
1221 CodedBitstreamUnitType type,
1224 // Section B.1.2 in H.264, section B.2.2 in H.265.
1225 if (nal_unit_index == 0) {
1226 // Assume that this is the first NAL unit in an access unit.
1229 if (codec_id == AV_CODEC_ID_H264)
1230 return type == H264_NAL_SPS || type == H264_NAL_PPS;
1231 if (codec_id == AV_CODEC_ID_HEVC)
1232 return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1236 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1237 CodedBitstreamFragment *frag)
1240 size_t max_size, dp, sp;
1241 int err, i, zero_run;
1243 for (i = 0; i < frag->nb_units; i++) {
1244 // Data should already all have been written when we get here.
1245 av_assert0(frag->units[i].data);
1249 for (i = 0; i < frag->nb_units; i++) {
1250 // Start code + content with worst-case emulation prevention.
1251 max_size += 4 + frag->units[i].data_size * 3 / 2;
1254 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1256 return AVERROR(ENOMEM);
1259 for (i = 0; i < frag->nb_units; i++) {
1260 CodedBitstreamUnit *unit = &frag->units[i];
1262 if (unit->data_bit_padding > 0) {
1263 if (i < frag->nb_units - 1)
1264 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1265 "unaligned padding on non-final NAL unit.\n");
1267 frag->data_bit_padding = unit->data_bit_padding;
1270 if (cbs_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
1274 // start_code_prefix_one_3bytes
1280 for (sp = 0; sp < unit->data_size; sp++) {
1282 if (unit->data[sp] == 0)
1287 if ((unit->data[sp] & ~3) == 0) {
1288 // emulation_prevention_three_byte
1291 zero_run = unit->data[sp] == 0;
1293 data[dp++] = unit->data[sp];
1297 av_assert0(dp <= max_size);
1298 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1301 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1303 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1305 if (!frag->data_ref) {
1307 return AVERROR(ENOMEM);
1311 frag->data_size = dp;
1316 static void cbs_h264_flush(CodedBitstreamContext *ctx)
1318 CodedBitstreamH264Context *h264 = ctx->priv_data;
1320 for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++) {
1321 av_buffer_unref(&h264->sps_ref[i]);
1322 h264->sps[i] = NULL;
1324 for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++) {
1325 av_buffer_unref(&h264->pps_ref[i]);
1326 h264->pps[i] = NULL;
1329 h264->active_sps = NULL;
1330 h264->active_pps = NULL;
1331 h264->last_slice_nal_unit_type = 0;
1334 static void cbs_h264_close(CodedBitstreamContext *ctx)
1336 CodedBitstreamH264Context *h264 = ctx->priv_data;
1339 ff_h2645_packet_uninit(&h264->common.read_packet);
1341 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1342 av_buffer_unref(&h264->sps_ref[i]);
1343 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1344 av_buffer_unref(&h264->pps_ref[i]);
1347 static void cbs_h265_flush(CodedBitstreamContext *ctx)
1349 CodedBitstreamH265Context *h265 = ctx->priv_data;
1351 for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++) {
1352 av_buffer_unref(&h265->vps_ref[i]);
1353 h265->vps[i] = NULL;
1355 for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++) {
1356 av_buffer_unref(&h265->sps_ref[i]);
1357 h265->sps[i] = NULL;
1359 for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++) {
1360 av_buffer_unref(&h265->pps_ref[i]);
1361 h265->pps[i] = NULL;
1364 h265->active_vps = NULL;
1365 h265->active_sps = NULL;
1366 h265->active_pps = NULL;
1369 static void cbs_h265_close(CodedBitstreamContext *ctx)
1371 CodedBitstreamH265Context *h265 = ctx->priv_data;
1374 ff_h2645_packet_uninit(&h265->common.read_packet);
1376 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1377 av_buffer_unref(&h265->vps_ref[i]);
1378 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1379 av_buffer_unref(&h265->sps_ref[i]);
1380 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1381 av_buffer_unref(&h265->pps_ref[i]);
1384 static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
1386 switch (payload->payload_type) {
1387 case H264_SEI_TYPE_BUFFERING_PERIOD:
1388 case H264_SEI_TYPE_PIC_TIMING:
1389 case H264_SEI_TYPE_PAN_SCAN_RECT:
1390 case H264_SEI_TYPE_RECOVERY_POINT:
1391 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
1392 case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
1393 case H264_SEI_TYPE_ALTERNATIVE_TRANSFER:
1395 case H264_SEI_TYPE_USER_DATA_REGISTERED:
1396 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
1398 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
1399 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
1402 av_buffer_unref(&payload->payload.other.data_ref);
1407 static void cbs_h264_free_sei(void *opaque, uint8_t *content)
1409 H264RawSEI *sei = (H264RawSEI*)content;
1411 for (i = 0; i < sei->payload_count; i++)
1412 cbs_h264_free_sei_payload(&sei->payload[i]);
1416 static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
1417 CBS_UNIT_TYPE_POD(H264_NAL_SPS, H264RawSPS),
1418 CBS_UNIT_TYPE_POD(H264_NAL_SPS_EXT, H264RawSPSExtension),
1420 CBS_UNIT_TYPE_INTERNAL_REF(H264_NAL_PPS, H264RawPPS, slice_group_id),
1427 H264_NAL_AUXILIARY_SLICE,
1429 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
1430 .content_size = sizeof(H264RawSlice),
1431 .nb_ref_offsets = 1,
1432 .ref_offsets = { offsetof(H264RawSlice, data) },
1435 CBS_UNIT_TYPE_POD(H264_NAL_AUD, H264RawAUD),
1436 CBS_UNIT_TYPE_POD(H264_NAL_FILLER_DATA, H264RawFiller),
1437 CBS_UNIT_TYPE_POD(H264_NAL_END_SEQUENCE, H264RawNALUnitHeader),
1438 CBS_UNIT_TYPE_POD(H264_NAL_END_STREAM, H264RawNALUnitHeader),
1440 CBS_UNIT_TYPE_COMPLEX(H264_NAL_SEI, H264RawSEI, &cbs_h264_free_sei),
1442 CBS_UNIT_TYPE_END_OF_LIST
1445 static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
1447 switch (payload->payload_type) {
1448 case HEVC_SEI_TYPE_BUFFERING_PERIOD:
1449 case HEVC_SEI_TYPE_PICTURE_TIMING:
1450 case HEVC_SEI_TYPE_PAN_SCAN_RECT:
1451 case HEVC_SEI_TYPE_RECOVERY_POINT:
1452 case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
1453 case HEVC_SEI_TYPE_ACTIVE_PARAMETER_SETS:
1454 case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
1455 case HEVC_SEI_TYPE_TIME_CODE:
1456 case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
1457 case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
1458 case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
1459 case HEVC_SEI_TYPE_ALPHA_CHANNEL_INFO:
1461 case HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
1462 av_buffer_unref(&payload->payload.user_data_registered.data_ref);
1464 case HEVC_SEI_TYPE_USER_DATA_UNREGISTERED:
1465 av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
1468 av_buffer_unref(&payload->payload.other.data_ref);
1471 av_buffer_unref(&payload->extension_data.data_ref);
1474 static void cbs_h265_free_sei(void *opaque, uint8_t *content)
1476 H265RawSEI *sei = (H265RawSEI*)content;
1478 for (i = 0; i < sei->payload_count; i++)
1479 cbs_h265_free_sei_payload(&sei->payload[i]);
1483 static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
1484 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_VPS, H265RawVPS, extension_data.data),
1485 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_SPS, H265RawSPS, extension_data.data),
1486 CBS_UNIT_TYPE_INTERNAL_REF(HEVC_NAL_PPS, H265RawPPS, extension_data.data),
1488 CBS_UNIT_TYPE_POD(HEVC_NAL_AUD, H265RawAUD),
1491 // Slices of non-IRAP pictures.
1492 .nb_unit_types = CBS_UNIT_TYPE_RANGE,
1493 .unit_type_range_start = HEVC_NAL_TRAIL_N,
1494 .unit_type_range_end = HEVC_NAL_RASL_R,
1496 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
1497 .content_size = sizeof(H265RawSlice),
1498 .nb_ref_offsets = 1,
1499 .ref_offsets = { offsetof(H265RawSlice, data) },
1503 // Slices of IRAP pictures.
1504 .nb_unit_types = CBS_UNIT_TYPE_RANGE,
1505 .unit_type_range_start = HEVC_NAL_BLA_W_LP,
1506 .unit_type_range_end = HEVC_NAL_CRA_NUT,
1508 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
1509 .content_size = sizeof(H265RawSlice),
1510 .nb_ref_offsets = 1,
1511 .ref_offsets = { offsetof(H265RawSlice, data) },
1517 HEVC_NAL_SEI_PREFIX,
1520 .content_type = CBS_CONTENT_TYPE_COMPLEX,
1521 .content_size = sizeof(H265RawSEI),
1522 .content_free = &cbs_h265_free_sei,
1525 CBS_UNIT_TYPE_END_OF_LIST
1528 const CodedBitstreamType ff_cbs_type_h264 = {
1529 .codec_id = AV_CODEC_ID_H264,
1531 .priv_data_size = sizeof(CodedBitstreamH264Context),
1533 .unit_types = cbs_h264_unit_types,
1535 .split_fragment = &cbs_h2645_split_fragment,
1536 .read_unit = &cbs_h264_read_nal_unit,
1537 .write_unit = &cbs_h264_write_nal_unit,
1538 .assemble_fragment = &cbs_h2645_assemble_fragment,
1540 .flush = &cbs_h264_flush,
1541 .close = &cbs_h264_close,
1544 const CodedBitstreamType ff_cbs_type_h265 = {
1545 .codec_id = AV_CODEC_ID_HEVC,
1547 .priv_data_size = sizeof(CodedBitstreamH265Context),
1549 .unit_types = cbs_h265_unit_types,
1551 .split_fragment = &cbs_h2645_split_fragment,
1552 .read_unit = &cbs_h265_read_nal_unit,
1553 .write_unit = &cbs_h265_write_nal_unit,
1554 .assemble_fragment = &cbs_h2645_assemble_fragment,
1556 .flush = &cbs_h265_flush,
1557 .close = &cbs_h265_close,
1560 int ff_cbs_h264_add_sei_message(CodedBitstreamFragment *au,
1561 H264RawSEIPayload *payload)
1563 H264RawSEI *sei = NULL;
1566 // Find an existing SEI NAL unit to add to.
1567 for (i = 0; i < au->nb_units; i++) {
1568 if (au->units[i].type == H264_NAL_SEI) {
1569 sei = au->units[i].content;
1570 if (sei->payload_count < H264_MAX_SEI_PAYLOADS)
1578 // Need to make a new SEI NAL unit. Insert it before the first
1579 // slice data NAL unit; if no slice data, add at the end.
1580 AVBufferRef *sei_ref;
1582 sei = av_mallocz(sizeof(*sei));
1584 err = AVERROR(ENOMEM);
1588 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1589 sei->nal_unit_header.nal_ref_idc = 0;
1591 sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1592 &cbs_h264_free_sei, NULL, 0);
1595 err = AVERROR(ENOMEM);
1599 for (i = 0; i < au->nb_units; i++) {
1600 if (au->units[i].type == H264_NAL_SLICE ||
1601 au->units[i].type == H264_NAL_IDR_SLICE)
1605 err = ff_cbs_insert_unit_content(au, i, H264_NAL_SEI,
1607 av_buffer_unref(&sei_ref);
1612 memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1613 ++sei->payload_count;
1617 cbs_h264_free_sei_payload(payload);
1621 void ff_cbs_h264_delete_sei_message(CodedBitstreamFragment *au,
1622 CodedBitstreamUnit *nal,
1625 H264RawSEI *sei = nal->content;
1627 av_assert0(nal->type == H264_NAL_SEI);
1628 av_assert0(position >= 0 && position < sei->payload_count);
1630 if (position == 0 && sei->payload_count == 1) {
1631 // Deleting NAL unit entirely.
1634 for (i = 0; i < au->nb_units; i++) {
1635 if (&au->units[i] == nal)
1639 ff_cbs_delete_unit(au, i);
1641 cbs_h264_free_sei_payload(&sei->payload[position]);
1643 --sei->payload_count;
1644 memmove(sei->payload + position,
1645 sei->payload + position + 1,
1646 (sei->payload_count - position) * sizeof(*sei->payload));