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/avassert.h"
20 #include "libavutil/opt.h"
21 #include "libavutil/pixfmt.h"
24 #include "cbs_internal.h"
29 static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc,
30 const char *name, uint32_t *write_to,
31 uint32_t range_min, uint32_t range_max)
33 uint32_t zeroes, bits_value, value;
36 if (ctx->trace_enable)
37 position = get_bits_count(gbc);
41 if (get_bits_left(gbc) < 1) {
42 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
43 "%s: bitstream ended.\n", name);
44 return AVERROR_INVALIDDATA;
53 value = MAX_UINT_BITS(32);
55 if (get_bits_left(gbc) < zeroes) {
56 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
57 "%s: bitstream ended.\n", name);
58 return AVERROR_INVALIDDATA;
61 bits_value = get_bits_long(gbc, zeroes);
62 value = bits_value + (UINT32_C(1) << zeroes) - 1;
65 if (ctx->trace_enable) {
71 k = FFMIN(zeroes - 32, 32);
72 for (i = 0; i < k; i++)
75 ff_cbs_trace_syntax_element(ctx, position, name,
82 for (i = 0; i < zeroes; i++)
87 for (j = 0; j < zeroes; j++)
88 bits[i++] = (bits_value >> (zeroes - j - 1) & 1) ? '1' : '0';
92 ff_cbs_trace_syntax_element(ctx, position, name,
96 if (value < range_min || value > range_max) {
97 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
98 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
99 name, value, range_min, range_max);
100 return AVERROR_INVALIDDATA;
107 static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc,
108 const char *name, uint32_t value,
109 uint32_t range_min, uint32_t range_max)
112 int position, zeroes;
114 if (value < range_min || value > range_max) {
115 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
116 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
117 name, value, range_min, range_max);
118 return AVERROR_INVALIDDATA;
121 if (ctx->trace_enable)
122 position = put_bits_count(pbc);
124 zeroes = av_log2(value + 1);
125 v = value - (1U << zeroes) + 1;
126 put_bits(pbc, zeroes, 0);
128 put_bits(pbc, zeroes, v);
130 if (ctx->trace_enable) {
134 for (j = 0; j < zeroes; j++)
137 for (j = 0; j < zeroes; j++)
138 bits[i++] = (v >> (zeroes - j - 1) & 1) ? '1' : '0';
140 ff_cbs_trace_syntax_element(ctx, position, name, NULL,
147 static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc,
148 const char *name, uint64_t *write_to)
151 int position, err, i;
153 if (ctx->trace_enable)
154 position = get_bits_count(gbc);
157 for (i = 0; i < 8; i++) {
158 int subscript[2] = { 1, i };
160 err = ff_cbs_read_unsigned(ctx, gbc, 8, "leb128_byte[i]", subscript,
165 value |= (uint64_t)(byte & 0x7f) << (i * 7);
170 if (value > UINT32_MAX)
171 return AVERROR_INVALIDDATA;
173 if (ctx->trace_enable)
174 ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
180 static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc,
181 const char *name, uint64_t value)
183 int position, err, len, i;
186 len = (av_log2(value) + 7) / 7;
188 if (ctx->trace_enable)
189 position = put_bits_count(pbc);
191 for (i = 0; i < len; i++) {
192 int subscript[2] = { 1, i };
194 byte = value >> (7 * i) & 0x7f;
198 err = ff_cbs_write_unsigned(ctx, pbc, 8, "leb128_byte[i]", subscript,
204 if (ctx->trace_enable)
205 ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
210 static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc,
211 uint32_t n, const char *name,
212 const int *subscripts, uint32_t *write_to)
214 uint32_t m, v, extra_bit, value;
219 if (ctx->trace_enable)
220 position = get_bits_count(gbc);
225 if (get_bits_left(gbc) < w) {
226 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid non-symmetric value at "
227 "%s: bitstream ended.\n", name);
228 return AVERROR_INVALIDDATA;
232 v = get_bits(gbc, w - 1);
239 extra_bit = get_bits1(gbc);
240 value = (v << 1) - m + extra_bit;
243 if (ctx->trace_enable) {
246 for (i = 0; i < w - 1; i++)
247 bits[i] = (v >> i & 1) ? '1' : '0';
249 bits[i++] = extra_bit ? '1' : '0';
252 ff_cbs_trace_syntax_element(ctx, position,
253 name, subscripts, bits, value);
260 static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc,
261 uint32_t n, const char *name,
262 const int *subscripts, uint32_t value)
264 uint32_t w, m, v, extra_bit;
268 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
269 "%"PRIu32", but must be in [0,%"PRIu32"].\n",
271 return AVERROR_INVALIDDATA;
274 if (ctx->trace_enable)
275 position = put_bits_count(pbc);
280 if (put_bits_left(pbc) < w)
281 return AVERROR(ENOSPC);
285 put_bits(pbc, w - 1, v);
287 v = m + ((value - m) >> 1);
288 extra_bit = (value - m) & 1;
289 put_bits(pbc, w - 1, v);
290 put_bits(pbc, 1, extra_bit);
293 if (ctx->trace_enable) {
296 for (i = 0; i < w - 1; i++)
297 bits[i] = (v >> i & 1) ? '1' : '0';
299 bits[i++] = extra_bit ? '1' : '0';
302 ff_cbs_trace_syntax_element(ctx, position,
303 name, subscripts, bits, value);
309 static int cbs_av1_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc,
310 uint32_t range_min, uint32_t range_max,
311 const char *name, uint32_t *write_to)
317 av_assert0(range_min <= range_max && range_max - range_min < sizeof(bits) - 1);
318 if (ctx->trace_enable)
319 position = get_bits_count(gbc);
321 for (i = 0, value = range_min; value < range_max;) {
322 if (get_bits_left(gbc) < 1) {
323 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
324 "%s: bitstream ended.\n", name);
325 return AVERROR_INVALIDDATA;
327 if (get_bits1(gbc)) {
336 if (ctx->trace_enable) {
338 ff_cbs_trace_syntax_element(ctx, position,
339 name, NULL, bits, value);
346 static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc,
347 uint32_t range_min, uint32_t range_max,
348 const char *name, uint32_t value)
352 av_assert0(range_min <= range_max && range_max - range_min < 32);
353 if (value < range_min || value > range_max) {
354 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
355 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
356 name, value, range_min, range_max);
357 return AVERROR_INVALIDDATA;
360 if (value == range_max)
361 len = range_max - range_min;
363 len = value - range_min + 1;
364 if (put_bits_left(pbc) < len)
365 return AVERROR(ENOSPC);
367 if (ctx->trace_enable) {
370 for (i = 0; i < len; i++) {
371 if (range_min + i == value)
377 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
378 name, NULL, bits, value);
382 put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
387 static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc,
388 uint32_t range_max, const char *name,
389 const int *subscripts, uint32_t *write_to)
393 uint32_t max_len, len, range_offset, range_bits;
395 if (ctx->trace_enable)
396 position = get_bits_count(gbc);
398 av_assert0(range_max > 0);
399 max_len = av_log2(range_max - 1) - 3;
401 err = cbs_av1_read_increment(ctx, gbc, 0, max_len,
402 "subexp_more_bits", &len);
407 range_bits = 2 + len;
408 range_offset = 1 << range_bits;
415 err = ff_cbs_read_unsigned(ctx, gbc, range_bits,
416 "subexp_bits", NULL, &value,
417 0, MAX_UINT_BITS(range_bits));
422 err = cbs_av1_read_ns(ctx, gbc, range_max - range_offset,
423 "subexp_final_bits", NULL, &value);
427 value += range_offset;
429 if (ctx->trace_enable)
430 ff_cbs_trace_syntax_element(ctx, position,
431 name, subscripts, "", value);
437 static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc,
438 uint32_t range_max, const char *name,
439 const int *subscripts, uint32_t value)
442 uint32_t max_len, len, range_offset, range_bits;
444 if (value > range_max) {
445 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
446 "%"PRIu32", but must be in [0,%"PRIu32"].\n",
447 name, value, range_max);
448 return AVERROR_INVALIDDATA;
451 if (ctx->trace_enable)
452 position = put_bits_count(pbc);
454 av_assert0(range_max > 0);
455 max_len = av_log2(range_max - 1) - 3;
462 range_bits = av_log2(value);
463 len = range_bits - 2;
465 // The top bin is combined with the one below it.
466 av_assert0(len == max_len + 1);
470 range_offset = 1 << range_bits;
473 err = cbs_av1_write_increment(ctx, pbc, 0, max_len,
474 "subexp_more_bits", len);
479 err = ff_cbs_write_unsigned(ctx, pbc, range_bits,
481 value - range_offset,
482 0, MAX_UINT_BITS(range_bits));
487 err = cbs_av1_write_ns(ctx, pbc, range_max - range_offset,
488 "subexp_final_bits", NULL,
489 value - range_offset);
494 if (ctx->trace_enable)
495 ff_cbs_trace_syntax_element(ctx, position,
496 name, subscripts, "", value);
502 static int cbs_av1_tile_log2(int blksize, int target)
505 for (k = 0; (blksize << k) < target; k++);
509 static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq,
510 unsigned int a, unsigned int b)
512 unsigned int diff, m;
513 if (!seq->enable_order_hint)
516 m = 1 << seq->order_hint_bits_minus_1;
517 diff = (diff & (m - 1)) - (diff & m);
521 static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
523 GetBitContext tmp = *gbc;
525 for (int i = 0; get_bits_left(&tmp) >= 8; i++) {
526 if (get_bits(&tmp, 8))
533 #define HEADER(name) do { \
534 ff_cbs_trace_header(ctx, name); \
537 #define CHECK(call) do { \
543 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
544 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
545 #define FUNC(name) FUNC_AV1(READWRITE, name)
547 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
549 #define fb(width, name) \
550 xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
551 #define fc(width, name, range_min, range_max) \
552 xf(width, name, current->name, range_min, range_max, 0, )
553 #define flag(name) fb(1, name)
554 #define su(width, name) \
555 xsu(width, name, current->name, 0, )
557 #define fbs(width, name, subs, ...) \
558 xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
559 #define fcs(width, name, range_min, range_max, subs, ...) \
560 xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
561 #define flags(name, subs, ...) \
562 xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
563 #define sus(width, name, subs, ...) \
564 xsu(width, name, current->name, subs, __VA_ARGS__)
566 #define fixed(width, name, value) do { \
567 av_unused uint32_t fixed_value = value; \
568 xf(width, name, fixed_value, value, value, 0, ); \
573 #define READWRITE read
574 #define RWContext GetBitContext
576 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
578 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
579 SUBSCRIPTS(subs, __VA_ARGS__), \
580 &value, range_min, range_max)); \
584 #define xsu(width, name, var, subs, ...) do { \
586 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
587 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
588 MIN_INT_BITS(width), \
589 MAX_INT_BITS(width))); \
593 #define uvlc(name, range_min, range_max) do { \
595 CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
596 &value, range_min, range_max)); \
597 current->name = value; \
600 #define ns(max_value, name, subs, ...) do { \
602 CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
603 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
604 current->name = value; \
607 #define increment(name, min, max) do { \
609 CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
610 current->name = value; \
613 #define subexp(name, max, subs, ...) do { \
615 CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
616 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
617 current->name = value; \
620 #define delta_q(name) do { \
621 uint8_t delta_coded; \
623 xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
625 xsu(1 + 6, name.delta_q, delta_q, 0, ); \
628 current->name = delta_q; \
631 #define leb128(name) do { \
633 CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
634 current->name = value; \
637 #define infer(name, value) do { \
638 current->name = value; \
641 #define byte_alignment(rw) (get_bits_count(rw) % 8)
643 #include "cbs_av1_syntax_template.c"
657 #undef byte_alignment
661 #define READWRITE write
662 #define RWContext PutBitContext
664 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
665 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
666 SUBSCRIPTS(subs, __VA_ARGS__), \
667 var, range_min, range_max)); \
670 #define xsu(width, name, var, subs, ...) do { \
671 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
672 SUBSCRIPTS(subs, __VA_ARGS__), var, \
673 MIN_INT_BITS(width), \
674 MAX_INT_BITS(width))); \
677 #define uvlc(name, range_min, range_max) do { \
678 CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
679 range_min, range_max)); \
682 #define ns(max_value, name, subs, ...) do { \
683 CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
684 SUBSCRIPTS(subs, __VA_ARGS__), \
688 #define increment(name, min, max) do { \
689 CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
693 #define subexp(name, max, subs, ...) do { \
694 CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
695 SUBSCRIPTS(subs, __VA_ARGS__), \
699 #define delta_q(name) do { \
700 xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
702 xsu(1 + 6, name.delta_q, current->name, 0, ); \
705 #define leb128(name) do { \
706 CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \
709 #define infer(name, value) do { \
710 if (current->name != (value)) { \
711 av_log(ctx->log_ctx, AV_LOG_ERROR, \
712 "%s does not match inferred value: " \
713 "%"PRId64", but should be %"PRId64".\n", \
714 #name, (int64_t)current->name, (int64_t)(value)); \
715 return AVERROR_INVALIDDATA; \
719 #define byte_alignment(rw) (put_bits_count(rw) % 8)
721 #include "cbs_av1_syntax_template.c"
735 #undef byte_alignment
738 static int cbs_av1_split_fragment(CodedBitstreamContext *ctx,
739 CodedBitstreamFragment *frag,
748 // Don't include this parsing in trace output.
749 trace = ctx->trace_enable;
750 ctx->trace_enable = 0;
753 size = frag->data_size;
755 if (INT_MAX / 8 < size) {
756 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: "
757 "too large (%"SIZE_SPECIFIER" bytes).\n", size);
758 err = AVERROR_INVALIDDATA;
762 if (header && size && data[0] & 0x80) {
763 // first bit is nonzero, the extradata does not consist purely of
764 // OBUs. Expect MP4/Matroska AV1CodecConfigurationRecord
765 int config_record_version = data[0] & 0x7f;
767 if (config_record_version != 1) {
768 av_log(ctx->log_ctx, AV_LOG_ERROR,
769 "Unknown version %d of AV1CodecConfigurationRecord "
771 config_record_version);
772 err = AVERROR_INVALIDDATA;
778 av_log(ctx->log_ctx, AV_LOG_WARNING,
779 "Undersized AV1CodecConfigurationRecord v%d found!\n",
780 config_record_version);
781 err = AVERROR_INVALIDDATA;
788 // In AV1CodecConfigurationRecord v1, actual OBUs start after
789 // four bytes. Thus set the offset as required for properly
796 AV1RawOBUHeader header;
799 init_get_bits(&gbc, data, 8 * size);
801 err = cbs_av1_read_obu_header(ctx, &gbc, &header);
805 if (header.obu_has_size_field) {
806 if (get_bits_left(&gbc) < 8) {
807 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment "
808 "too short (%"SIZE_SPECIFIER" bytes).\n", size);
809 err = AVERROR_INVALIDDATA;
812 err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
816 obu_size = size - 1 - header.obu_extension_flag;
818 pos = get_bits_count(&gbc);
819 av_assert0(pos % 8 == 0 && pos / 8 <= size);
821 obu_length = pos / 8 + obu_size;
823 if (size < obu_length) {
824 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
825 "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
827 err = AVERROR_INVALIDDATA;
831 err = ff_cbs_insert_unit_data(frag, -1, header.obu_type,
832 data, obu_length, frag->data_ref);
843 ctx->trace_enable = trace;
847 static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx,
848 CodedBitstreamUnit *unit,
854 pos = get_bits_count(gbc);
855 if (pos >= 8 * unit->data_size) {
856 av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before "
857 "any data in tile group (%d bits read).\n", pos);
858 return AVERROR_INVALIDDATA;
860 // Must be byte-aligned at this point.
861 av_assert0(pos % 8 == 0);
863 td->data_ref = av_buffer_ref(unit->data_ref);
865 return AVERROR(ENOMEM);
867 td->data = unit->data + pos / 8;
868 td->data_size = unit->data_size - pos / 8;
873 static int cbs_av1_read_unit(CodedBitstreamContext *ctx,
874 CodedBitstreamUnit *unit)
876 CodedBitstreamAV1Context *priv = ctx->priv_data;
879 int err, start_pos, end_pos;
881 err = ff_cbs_alloc_unit_content2(ctx, unit);
886 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
890 err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header);
893 av_assert0(obu->header.obu_type == unit->type);
895 if (obu->header.obu_has_size_field) {
897 err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
900 obu->obu_size = obu_size;
902 if (unit->data_size < 1 + obu->header.obu_extension_flag) {
903 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
904 "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size);
905 return AVERROR_INVALIDDATA;
907 obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
910 start_pos = get_bits_count(&gbc);
912 if (obu->header.obu_extension_flag) {
913 if (obu->header.obu_type != AV1_OBU_SEQUENCE_HEADER &&
914 obu->header.obu_type != AV1_OBU_TEMPORAL_DELIMITER &&
915 priv->operating_point_idc) {
916 int in_temporal_layer =
917 (priv->operating_point_idc >> priv->temporal_id ) & 1;
918 int in_spatial_layer =
919 (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1;
920 if (!in_temporal_layer || !in_spatial_layer) {
921 return AVERROR(EAGAIN); // drop_obu()
926 switch (obu->header.obu_type) {
927 case AV1_OBU_SEQUENCE_HEADER:
929 err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
930 &obu->obu.sequence_header);
934 if (priv->operating_point >= 0) {
935 AV1RawSequenceHeader *sequence_header = &obu->obu.sequence_header;
937 if (priv->operating_point > sequence_header->operating_points_cnt_minus_1) {
938 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid Operating Point %d requested. "
939 "Must not be higher than %u.\n",
940 priv->operating_point, sequence_header->operating_points_cnt_minus_1);
941 return AVERROR(EINVAL);
943 priv->operating_point_idc = sequence_header->operating_point_idc[priv->operating_point];
946 av_buffer_unref(&priv->sequence_header_ref);
947 priv->sequence_header = NULL;
949 priv->sequence_header_ref = av_buffer_ref(unit->content_ref);
950 if (!priv->sequence_header_ref)
951 return AVERROR(ENOMEM);
952 priv->sequence_header = &obu->obu.sequence_header;
955 case AV1_OBU_TEMPORAL_DELIMITER:
957 err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
962 case AV1_OBU_FRAME_HEADER:
963 case AV1_OBU_REDUNDANT_FRAME_HEADER:
965 err = cbs_av1_read_frame_header_obu(ctx, &gbc,
966 &obu->obu.frame_header,
967 obu->header.obu_type ==
968 AV1_OBU_REDUNDANT_FRAME_HEADER,
974 case AV1_OBU_TILE_GROUP:
976 err = cbs_av1_read_tile_group_obu(ctx, &gbc,
977 &obu->obu.tile_group);
981 err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
982 &obu->obu.tile_group.tile_data);
989 err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame,
994 err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
995 &obu->obu.frame.tile_group.tile_data);
1000 case AV1_OBU_TILE_LIST:
1002 err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1003 &obu->obu.tile_list);
1007 err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1008 &obu->obu.tile_list.tile_data);
1013 case AV1_OBU_METADATA:
1015 err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata);
1020 case AV1_OBU_PADDING:
1022 err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->obu.padding);
1028 return AVERROR(ENOSYS);
1031 end_pos = get_bits_count(&gbc);
1032 av_assert0(end_pos <= unit->data_size * 8);
1034 if (obu->obu_size > 0 &&
1035 obu->header.obu_type != AV1_OBU_TILE_GROUP &&
1036 obu->header.obu_type != AV1_OBU_TILE_LIST &&
1037 obu->header.obu_type != AV1_OBU_FRAME) {
1038 int nb_bits = obu->obu_size * 8 + start_pos - end_pos;
1041 return AVERROR_INVALIDDATA;
1043 err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
1051 static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
1052 CodedBitstreamUnit *unit,
1055 CodedBitstreamAV1Context *priv = ctx->priv_data;
1056 AV1RawOBU *obu = unit->content;
1057 PutBitContext pbc_tmp;
1060 int err, start_pos, end_pos, data_pos;
1062 // OBUs in the normal bitstream format must contain a size field
1063 // in every OBU (in annex B it is optional, but we don't support
1065 obu->header.obu_has_size_field = 1;
1067 err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
1071 if (obu->header.obu_has_size_field) {
1073 // Add space for the size field to fill later.
1079 start_pos = put_bits_count(pbc);
1081 switch (obu->header.obu_type) {
1082 case AV1_OBU_SEQUENCE_HEADER:
1084 err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1085 &obu->obu.sequence_header);
1089 av_buffer_unref(&priv->sequence_header_ref);
1090 priv->sequence_header = NULL;
1092 err = ff_cbs_make_unit_refcounted(ctx, unit);
1096 priv->sequence_header_ref = av_buffer_ref(unit->content_ref);
1097 if (!priv->sequence_header_ref)
1098 return AVERROR(ENOMEM);
1099 priv->sequence_header = &obu->obu.sequence_header;
1102 case AV1_OBU_TEMPORAL_DELIMITER:
1104 err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1109 case AV1_OBU_FRAME_HEADER:
1110 case AV1_OBU_REDUNDANT_FRAME_HEADER:
1112 err = cbs_av1_write_frame_header_obu(ctx, pbc,
1113 &obu->obu.frame_header,
1114 obu->header.obu_type ==
1115 AV1_OBU_REDUNDANT_FRAME_HEADER,
1121 case AV1_OBU_TILE_GROUP:
1123 err = cbs_av1_write_tile_group_obu(ctx, pbc,
1124 &obu->obu.tile_group);
1128 td = &obu->obu.tile_group.tile_data;
1133 err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
1137 td = &obu->obu.frame.tile_group.tile_data;
1140 case AV1_OBU_TILE_LIST:
1142 err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list);
1146 td = &obu->obu.tile_list.tile_data;
1149 case AV1_OBU_METADATA:
1151 err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
1156 case AV1_OBU_PADDING:
1158 err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding);
1164 return AVERROR(ENOSYS);
1167 end_pos = put_bits_count(pbc);
1168 header_size = (end_pos - start_pos + 7) / 8;
1170 obu->obu_size = header_size + td->data_size;
1171 } else if (header_size > 0) {
1172 // Add trailing bits and recalculate.
1173 err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1176 end_pos = put_bits_count(pbc);
1177 obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
1183 end_pos = put_bits_count(pbc);
1184 // Must now be byte-aligned.
1185 av_assert0(end_pos % 8 == 0);
1186 flush_put_bits(pbc);
1191 err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size);
1195 data_pos = put_bits_count(pbc) / 8;
1196 flush_put_bits(pbc);
1197 av_assert0(data_pos <= start_pos);
1199 if (8 * obu->obu_size > put_bits_left(pbc))
1200 return AVERROR(ENOSPC);
1202 if (obu->obu_size > 0) {
1203 memmove(pbc->buf + data_pos,
1204 pbc->buf + start_pos, header_size);
1205 skip_put_bytes(pbc, header_size);
1208 memcpy(pbc->buf + data_pos + header_size,
1209 td->data, td->data_size);
1210 skip_put_bytes(pbc, td->data_size);
1214 // OBU data must be byte-aligned.
1215 av_assert0(put_bits_count(pbc) % 8 == 0);
1220 static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx,
1221 CodedBitstreamFragment *frag)
1227 for (i = 0; i < frag->nb_units; i++)
1228 size += frag->units[i].data_size;
1230 frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
1231 if (!frag->data_ref)
1232 return AVERROR(ENOMEM);
1233 frag->data = frag->data_ref->data;
1234 memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1237 for (i = 0; i < frag->nb_units; i++) {
1238 memcpy(frag->data + pos, frag->units[i].data,
1239 frag->units[i].data_size);
1240 pos += frag->units[i].data_size;
1242 av_assert0(pos == size);
1243 frag->data_size = size;
1248 static void cbs_av1_flush(CodedBitstreamContext *ctx)
1250 CodedBitstreamAV1Context *priv = ctx->priv_data;
1252 av_buffer_unref(&priv->frame_header_ref);
1253 priv->sequence_header = NULL;
1254 priv->frame_header = NULL;
1256 memset(priv->ref, 0, sizeof(priv->ref));
1257 priv->operating_point_idc = 0;
1258 priv->seen_frame_header = 0;
1262 static void cbs_av1_close(CodedBitstreamContext *ctx)
1264 CodedBitstreamAV1Context *priv = ctx->priv_data;
1266 av_buffer_unref(&priv->sequence_header_ref);
1267 av_buffer_unref(&priv->frame_header_ref);
1270 static void cbs_av1_free_metadata(void *unit, uint8_t *content)
1272 AV1RawOBU *obu = (AV1RawOBU*)content;
1275 av_assert0(obu->header.obu_type == AV1_OBU_METADATA);
1276 md = &obu->obu.metadata;
1278 switch (md->metadata_type) {
1279 case AV1_METADATA_TYPE_ITUT_T35:
1280 av_buffer_unref(&md->metadata.itut_t35.payload_ref);
1286 static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[] = {
1287 CBS_UNIT_TYPE_POD(AV1_OBU_SEQUENCE_HEADER, AV1RawOBU),
1288 CBS_UNIT_TYPE_POD(AV1_OBU_TEMPORAL_DELIMITER, AV1RawOBU),
1289 CBS_UNIT_TYPE_POD(AV1_OBU_FRAME_HEADER, AV1RawOBU),
1290 CBS_UNIT_TYPE_POD(AV1_OBU_REDUNDANT_FRAME_HEADER, AV1RawOBU),
1292 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_TILE_GROUP, AV1RawOBU,
1293 obu.tile_group.tile_data.data),
1294 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_FRAME, AV1RawOBU,
1295 obu.frame.tile_group.tile_data.data),
1296 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_TILE_LIST, AV1RawOBU,
1297 obu.tile_list.tile_data.data),
1298 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_PADDING, AV1RawOBU,
1299 obu.padding.payload),
1301 CBS_UNIT_TYPE_COMPLEX(AV1_OBU_METADATA, AV1RawOBU,
1302 &cbs_av1_free_metadata),
1304 CBS_UNIT_TYPE_END_OF_LIST
1307 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1308 static const AVOption cbs_av1_options[] = {
1309 { "operating_point", "Set operating point to select layers to parse from a scalable bitstream",
1310 OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AV1_MAX_OPERATING_POINTS - 1, 0 },
1314 static const AVClass cbs_av1_class = {
1315 .class_name = "cbs_av1",
1316 .item_name = av_default_item_name,
1317 .option = cbs_av1_options,
1318 .version = LIBAVUTIL_VERSION_INT,
1321 const CodedBitstreamType ff_cbs_type_av1 = {
1322 .codec_id = AV_CODEC_ID_AV1,
1324 .priv_class = &cbs_av1_class,
1325 .priv_data_size = sizeof(CodedBitstreamAV1Context),
1327 .unit_types = cbs_av1_unit_types,
1329 .split_fragment = &cbs_av1_split_fragment,
1330 .read_unit = &cbs_av1_read_unit,
1331 .write_unit = &cbs_av1_write_obu,
1332 .assemble_fragment = &cbs_av1_assemble_fragment,
1334 .flush = &cbs_av1_flush,
1335 .close = &cbs_av1_close,