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
23 #include "libavutil/avassert.h"
24 #include "libavutil/buffer.h"
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
30 #include "cbs_internal.h"
33 static const CodedBitstreamType *const cbs_type_table[] = {
54 const enum AVCodecID ff_cbs_all_codec_ids[] = {
68 AV_CODEC_ID_MPEG2VIDEO,
76 av_cold int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
77 enum AVCodecID codec_id, void *log_ctx)
79 CodedBitstreamContext *ctx;
80 const CodedBitstreamType *type;
84 for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
85 if (cbs_type_table[i]->codec_id == codec_id) {
86 type = cbs_type_table[i];
91 return AVERROR(EINVAL);
93 ctx = av_mallocz(sizeof(*ctx));
95 return AVERROR(ENOMEM);
97 ctx->log_ctx = log_ctx;
98 ctx->codec = type; /* Must be before any error */
100 if (type->priv_data_size) {
101 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
102 if (!ctx->priv_data) {
104 return AVERROR(ENOMEM);
106 if (type->priv_class) {
107 *(const AVClass **)ctx->priv_data = type->priv_class;
108 av_opt_set_defaults(ctx->priv_data);
112 ctx->decompose_unit_types = NULL;
114 ctx->trace_enable = 0;
115 ctx->trace_level = AV_LOG_TRACE;
121 av_cold void ff_cbs_flush(CodedBitstreamContext *ctx)
123 if (ctx->codec->flush)
124 ctx->codec->flush(ctx);
127 av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
129 CodedBitstreamContext *ctx = *ctx_ptr;
134 if (ctx->codec->close)
135 ctx->codec->close(ctx);
137 av_freep(&ctx->write_buffer);
139 if (ctx->codec->priv_class && ctx->priv_data)
140 av_opt_free(ctx->priv_data);
142 av_freep(&ctx->priv_data);
146 static void cbs_unit_uninit(CodedBitstreamUnit *unit)
148 av_buffer_unref(&unit->content_ref);
149 unit->content = NULL;
151 av_buffer_unref(&unit->data_ref);
154 unit->data_bit_padding = 0;
157 void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
161 for (i = 0; i < frag->nb_units; i++)
162 cbs_unit_uninit(&frag->units[i]);
165 av_buffer_unref(&frag->data_ref);
168 frag->data_bit_padding = 0;
171 av_cold void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
173 ff_cbs_fragment_reset(frag);
175 av_freep(&frag->units);
176 frag->nb_units_allocated = 0;
179 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
180 CodedBitstreamFragment *frag)
184 for (i = 0; i < frag->nb_units; i++) {
185 CodedBitstreamUnit *unit = &frag->units[i];
187 if (ctx->decompose_unit_types) {
188 for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
189 if (ctx->decompose_unit_types[j] == unit->type)
192 if (j >= ctx->nb_decompose_unit_types)
196 av_buffer_unref(&unit->content_ref);
197 unit->content = NULL;
199 av_assert0(unit->data && unit->data_ref);
201 err = ctx->codec->read_unit(ctx, unit);
202 if (err == AVERROR(ENOSYS)) {
203 av_log(ctx->log_ctx, AV_LOG_VERBOSE,
204 "Decomposition unimplemented for unit %d "
205 "(type %"PRIu32").\n", i, unit->type);
206 } else if (err == AVERROR(EAGAIN)) {
207 av_log(ctx->log_ctx, AV_LOG_VERBOSE,
208 "Skipping decomposition of unit %d "
209 "(type %"PRIu32").\n", i, unit->type);
210 av_buffer_unref(&unit->content_ref);
211 unit->content = NULL;
212 } else if (err < 0) {
213 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
214 "(type %"PRIu32").\n", i, unit->type);
222 static int cbs_fill_fragment_data(CodedBitstreamFragment *frag,
223 const uint8_t *data, size_t size)
225 av_assert0(!frag->data && !frag->data_ref);
228 av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
230 return AVERROR(ENOMEM);
232 frag->data = frag->data_ref->data;
233 frag->data_size = size;
235 memcpy(frag->data, data, size);
236 memset(frag->data + size, 0,
237 AV_INPUT_BUFFER_PADDING_SIZE);
242 static int cbs_read_data(CodedBitstreamContext *ctx,
243 CodedBitstreamFragment *frag,
245 const uint8_t *data, size_t size,
251 frag->data_ref = av_buffer_ref(buf);
253 return AVERROR(ENOMEM);
255 frag->data = (uint8_t *)data;
256 frag->data_size = size;
259 err = cbs_fill_fragment_data(frag, data, size);
264 err = ctx->codec->split_fragment(ctx, frag, header);
268 return cbs_read_fragment_content(ctx, frag);
271 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
272 CodedBitstreamFragment *frag,
273 const AVCodecParameters *par)
275 return cbs_read_data(ctx, frag, NULL,
277 par->extradata_size, 1);
280 int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx,
281 CodedBitstreamFragment *frag,
282 const AVCodecContext *avctx)
284 return cbs_read_data(ctx, frag, NULL,
286 avctx->extradata_size, 1);
289 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
290 CodedBitstreamFragment *frag,
293 return cbs_read_data(ctx, frag, pkt->buf,
294 pkt->data, pkt->size, 0);
297 int ff_cbs_read_packet_side_data(CodedBitstreamContext *ctx,
298 CodedBitstreamFragment *frag,
301 size_t side_data_size;
302 const uint8_t *side_data =
303 av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
306 return cbs_read_data(ctx, frag, NULL,
307 side_data, side_data_size, 1);
310 int ff_cbs_read(CodedBitstreamContext *ctx,
311 CodedBitstreamFragment *frag,
312 const uint8_t *data, size_t size)
314 return cbs_read_data(ctx, frag, NULL,
319 * Allocate a new internal data buffer of the given size in the unit.
321 * The data buffer will have input padding.
323 static int cbs_alloc_unit_data(CodedBitstreamUnit *unit,
326 av_assert0(!unit->data && !unit->data_ref);
328 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
330 return AVERROR(ENOMEM);
332 unit->data = unit->data_ref->data;
333 unit->data_size = size;
335 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
340 static int cbs_write_unit_data(CodedBitstreamContext *ctx,
341 CodedBitstreamUnit *unit)
346 if (!ctx->write_buffer) {
347 // Initial write buffer size is 1MB.
348 ctx->write_buffer_size = 1024 * 1024;
350 reallocate_and_try_again:
351 ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
353 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
354 "sufficiently large write buffer (last attempt "
355 "%"SIZE_SPECIFIER" bytes).\n", ctx->write_buffer_size);
360 init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
362 ret = ctx->codec->write_unit(ctx, unit, &pbc);
364 if (ret == AVERROR(ENOSPC)) {
366 if (ctx->write_buffer_size == INT_MAX / 8)
367 return AVERROR(ENOMEM);
368 ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
369 goto reallocate_and_try_again;
371 // Write failed for some other reason.
375 // Overflow but we didn't notice.
376 av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
378 if (put_bits_count(&pbc) % 8)
379 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
381 unit->data_bit_padding = 0;
383 flush_put_bits(&pbc);
385 ret = cbs_alloc_unit_data(unit, put_bytes_output(&pbc));
389 memcpy(unit->data, ctx->write_buffer, unit->data_size);
394 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
395 CodedBitstreamFragment *frag)
399 for (i = 0; i < frag->nb_units; i++) {
400 CodedBitstreamUnit *unit = &frag->units[i];
405 av_buffer_unref(&unit->data_ref);
408 err = cbs_write_unit_data(ctx, unit);
410 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
411 "(type %"PRIu32").\n", i, unit->type);
414 av_assert0(unit->data && unit->data_ref);
417 av_buffer_unref(&frag->data_ref);
420 err = ctx->codec->assemble_fragment(ctx, frag);
422 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
425 av_assert0(frag->data && frag->data_ref);
430 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
431 AVCodecParameters *par,
432 CodedBitstreamFragment *frag)
436 err = ff_cbs_write_fragment_data(ctx, frag);
440 av_freep(&par->extradata);
442 par->extradata = av_malloc(frag->data_size +
443 AV_INPUT_BUFFER_PADDING_SIZE);
445 return AVERROR(ENOMEM);
447 memcpy(par->extradata, frag->data, frag->data_size);
448 memset(par->extradata + frag->data_size, 0,
449 AV_INPUT_BUFFER_PADDING_SIZE);
450 par->extradata_size = frag->data_size;
455 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
457 CodedBitstreamFragment *frag)
462 err = ff_cbs_write_fragment_data(ctx, frag);
466 buf = av_buffer_ref(frag->data_ref);
468 return AVERROR(ENOMEM);
470 av_buffer_unref(&pkt->buf);
473 pkt->data = frag->data;
474 pkt->size = frag->data_size;
480 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
483 if (!ctx->trace_enable)
486 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
489 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
490 const char *str, const int *subscripts,
491 const char *bits, int64_t value)
494 size_t name_len, bits_len;
495 int pad, subs, i, j, k, n;
497 if (!ctx->trace_enable)
500 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
502 subs = subscripts ? subscripts[0] : 0;
504 for (i = j = 0; str[i];) {
508 k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
509 av_assert0(k > 0 && j + k < sizeof(name));
511 for (++i; str[i] && str[i] != ']'; i++);
512 av_assert0(str[i] == ']');
514 while (str[i] && str[i] != ']')
515 name[j++] = str[i++];
516 av_assert0(str[i] == ']');
519 av_assert0(j + 1 < sizeof(name));
520 name[j++] = str[i++];
523 av_assert0(j + 1 < sizeof(name));
525 av_assert0(n == subs);
527 name_len = strlen(name);
528 bits_len = strlen(bits);
530 if (name_len + bits_len > 60)
535 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
536 position, name, pad, bits, value);
539 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
540 int width, const char *name,
541 const int *subscripts, uint32_t *write_to,
542 uint32_t range_min, uint32_t range_max)
547 av_assert0(width > 0 && width <= 32);
549 if (get_bits_left(gbc) < width) {
550 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
551 "%s: bitstream ended.\n", name);
552 return AVERROR_INVALIDDATA;
555 if (ctx->trace_enable)
556 position = get_bits_count(gbc);
558 value = get_bits_long(gbc, width);
560 if (ctx->trace_enable) {
563 for (i = 0; i < width; i++)
564 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
567 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
571 if (value < range_min || value > range_max) {
572 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
573 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
574 name, value, range_min, range_max);
575 return AVERROR_INVALIDDATA;
582 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
583 int width, const char *name,
584 const int *subscripts, uint32_t value,
585 uint32_t range_min, uint32_t range_max)
587 av_assert0(width > 0 && width <= 32);
589 if (value < range_min || value > range_max) {
590 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
591 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
592 name, value, range_min, range_max);
593 return AVERROR_INVALIDDATA;
596 if (put_bits_left(pbc) < width)
597 return AVERROR(ENOSPC);
599 if (ctx->trace_enable) {
602 for (i = 0; i < width; i++)
603 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
606 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
607 name, subscripts, bits, value);
611 put_bits(pbc, width, value);
613 put_bits32(pbc, value);
618 int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
619 int width, const char *name,
620 const int *subscripts, int32_t *write_to,
621 int32_t range_min, int32_t range_max)
626 av_assert0(width > 0 && width <= 32);
628 if (get_bits_left(gbc) < width) {
629 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
630 "%s: bitstream ended.\n", name);
631 return AVERROR_INVALIDDATA;
634 if (ctx->trace_enable)
635 position = get_bits_count(gbc);
637 value = get_sbits_long(gbc, width);
639 if (ctx->trace_enable) {
642 for (i = 0; i < width; i++)
643 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
646 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
650 if (value < range_min || value > range_max) {
651 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
652 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
653 name, value, range_min, range_max);
654 return AVERROR_INVALIDDATA;
661 int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
662 int width, const char *name,
663 const int *subscripts, int32_t value,
664 int32_t range_min, int32_t range_max)
666 av_assert0(width > 0 && width <= 32);
668 if (value < range_min || value > range_max) {
669 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
670 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
671 name, value, range_min, range_max);
672 return AVERROR_INVALIDDATA;
675 if (put_bits_left(pbc) < width)
676 return AVERROR(ENOSPC);
678 if (ctx->trace_enable) {
681 for (i = 0; i < width; i++)
682 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
685 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
686 name, subscripts, bits, value);
690 put_sbits(pbc, width, value);
692 put_bits32(pbc, value);
698 int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit,
700 void (*free)(void *opaque, uint8_t *data))
702 av_assert0(!unit->content && !unit->content_ref);
704 unit->content = av_mallocz(size);
706 return AVERROR(ENOMEM);
708 unit->content_ref = av_buffer_create(unit->content, size,
710 if (!unit->content_ref) {
711 av_freep(&unit->content);
712 return AVERROR(ENOMEM);
718 static int cbs_insert_unit(CodedBitstreamFragment *frag,
721 CodedBitstreamUnit *units;
723 if (frag->nb_units < frag->nb_units_allocated) {
726 if (position < frag->nb_units)
727 memmove(units + position + 1, units + position,
728 (frag->nb_units - position) * sizeof(*units));
730 units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
732 return AVERROR(ENOMEM);
734 frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
737 memcpy(units, frag->units, position * sizeof(*units));
739 if (position < frag->nb_units)
740 memcpy(units + position + 1, frag->units + position,
741 (frag->nb_units - position) * sizeof(*units));
744 memset(units + position, 0, sizeof(*units));
746 if (units != frag->units) {
747 av_free(frag->units);
756 int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
758 CodedBitstreamUnitType type,
760 AVBufferRef *content_buf)
762 CodedBitstreamUnit *unit;
763 AVBufferRef *content_ref;
767 position = frag->nb_units;
768 av_assert0(position >= 0 && position <= frag->nb_units);
771 content_ref = av_buffer_ref(content_buf);
773 return AVERROR(ENOMEM);
778 err = cbs_insert_unit(frag, position);
780 av_buffer_unref(&content_ref);
784 unit = &frag->units[position];
786 unit->content = content;
787 unit->content_ref = content_ref;
792 static int cbs_insert_unit_data(CodedBitstreamFragment *frag,
793 CodedBitstreamUnitType type,
794 uint8_t *data, size_t data_size,
795 AVBufferRef *data_buf,
798 CodedBitstreamUnit *unit;
799 AVBufferRef *data_ref;
802 av_assert0(position >= 0 && position <= frag->nb_units);
805 data_ref = av_buffer_ref(data_buf);
807 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
811 return AVERROR(ENOMEM);
814 err = cbs_insert_unit(frag, position);
816 av_buffer_unref(&data_ref);
820 unit = &frag->units[position];
823 unit->data_size = data_size;
824 unit->data_ref = data_ref;
829 int ff_cbs_append_unit_data(CodedBitstreamFragment *frag,
830 CodedBitstreamUnitType type,
831 uint8_t *data, size_t data_size,
832 AVBufferRef *data_buf)
834 return cbs_insert_unit_data(frag, type,
835 data, data_size, data_buf,
839 void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
842 av_assert0(0 <= position && position < frag->nb_units
843 && "Unit to be deleted not in fragment.");
845 cbs_unit_uninit(&frag->units[position]);
849 if (frag->nb_units > 0)
850 memmove(frag->units + position,
851 frag->units + position + 1,
852 (frag->nb_units - position) * sizeof(*frag->units));
855 static void cbs_default_free_unit_content(void *opaque, uint8_t *data)
857 const CodedBitstreamUnitTypeDescriptor *desc = opaque;
858 if (desc->content_type == CBS_CONTENT_TYPE_INTERNAL_REFS) {
860 for (i = 0; i < desc->nb_ref_offsets; i++) {
861 void **ptr = (void**)(data + desc->ref_offsets[i]);
862 av_buffer_unref((AVBufferRef**)(ptr + 1));
868 static const CodedBitstreamUnitTypeDescriptor
869 *cbs_find_unit_type_desc(CodedBitstreamContext *ctx,
870 CodedBitstreamUnit *unit)
872 const CodedBitstreamUnitTypeDescriptor *desc;
875 if (!ctx->codec->unit_types)
879 desc = &ctx->codec->unit_types[i];
880 if (desc->nb_unit_types == 0)
882 if (desc->nb_unit_types == CBS_UNIT_TYPE_RANGE) {
883 if (unit->type >= desc->unit_type_range_start &&
884 unit->type <= desc->unit_type_range_end)
887 for (j = 0; j < desc->nb_unit_types; j++) {
888 if (desc->unit_types[j] == unit->type)
896 int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx,
897 CodedBitstreamUnit *unit)
899 const CodedBitstreamUnitTypeDescriptor *desc;
901 av_assert0(!unit->content && !unit->content_ref);
903 desc = cbs_find_unit_type_desc(ctx, unit);
905 return AVERROR(ENOSYS);
907 unit->content = av_mallocz(desc->content_size);
909 return AVERROR(ENOMEM);
912 av_buffer_create(unit->content, desc->content_size,
913 desc->content_free ? desc->content_free
914 : cbs_default_free_unit_content,
916 if (!unit->content_ref) {
917 av_freep(&unit->content);
918 return AVERROR(ENOMEM);
924 static int cbs_clone_unit_content(AVBufferRef **clone_ref,
925 CodedBitstreamUnit *unit,
926 const CodedBitstreamUnitTypeDescriptor *desc)
929 uint8_t **src_ptr, **copy_ptr;
930 AVBufferRef **src_buf, **copy_buf;
933 av_assert0(unit->content);
936 copy = av_memdup(src, desc->content_size);
938 return AVERROR(ENOMEM);
940 for (i = 0; i < desc->nb_ref_offsets; i++) {
941 src_ptr = (uint8_t**)(src + desc->ref_offsets[i]);
942 src_buf = (AVBufferRef**)(src_ptr + 1);
943 copy_ptr = (uint8_t**)(copy + desc->ref_offsets[i]);
944 copy_buf = (AVBufferRef**)(copy_ptr + 1);
947 av_assert0(!*src_buf);
951 // We can't handle a non-refcounted pointer here - we don't
952 // have enough information to handle whatever structure lies
953 // at the other end of it.
954 err = AVERROR(EINVAL);
958 // src_ptr is required to point somewhere inside src_buf. If it
959 // doesn't, there is a bug somewhere.
960 av_assert0(*src_ptr >= (*src_buf)->data &&
961 *src_ptr < (*src_buf)->data + (*src_buf)->size);
963 *copy_buf = av_buffer_ref(*src_buf);
965 err = AVERROR(ENOMEM);
968 *copy_ptr = (*copy_buf)->data + (*src_ptr - (*src_buf)->data);
971 *clone_ref = av_buffer_create(copy, desc->content_size,
972 desc->content_free ? desc->content_free :
973 cbs_default_free_unit_content,
976 err = AVERROR(ENOMEM);
983 for (--i; i >= 0; i--)
984 av_buffer_unref((AVBufferRef**)(copy + desc->ref_offsets[i]));
990 int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx,
991 CodedBitstreamUnit *unit)
993 const CodedBitstreamUnitTypeDescriptor *desc;
997 av_assert0(unit->content);
998 if (unit->content_ref) {
999 // Already refcounted, nothing to do.
1003 desc = cbs_find_unit_type_desc(ctx, unit);
1005 return AVERROR(ENOSYS);
1007 switch (desc->content_type) {
1008 case CBS_CONTENT_TYPE_POD:
1009 ref = av_buffer_alloc(desc->content_size);
1011 return AVERROR(ENOMEM);
1012 memcpy(ref->data, unit->content, desc->content_size);
1016 case CBS_CONTENT_TYPE_INTERNAL_REFS:
1017 err = cbs_clone_unit_content(&ref, unit, desc);
1020 case CBS_CONTENT_TYPE_COMPLEX:
1021 if (!desc->content_clone)
1022 return AVERROR_PATCHWELCOME;
1023 err = desc->content_clone(&ref, unit);
1027 av_assert0(0 && "Invalid content type.");
1033 unit->content_ref = ref;
1034 unit->content = ref->data;
1038 int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
1039 CodedBitstreamUnit *unit)
1041 const CodedBitstreamUnitTypeDescriptor *desc;
1045 // This can only be applied to refcounted units.
1046 err = ff_cbs_make_unit_refcounted(ctx, unit);
1049 av_assert0(unit->content && unit->content_ref);
1051 if (av_buffer_is_writable(unit->content_ref))
1054 desc = cbs_find_unit_type_desc(ctx, unit);
1056 return AVERROR(ENOSYS);
1058 switch (desc->content_type) {
1059 case CBS_CONTENT_TYPE_POD:
1060 err = av_buffer_make_writable(&unit->content_ref);
1063 case CBS_CONTENT_TYPE_INTERNAL_REFS:
1064 err = cbs_clone_unit_content(&ref, unit, desc);
1067 case CBS_CONTENT_TYPE_COMPLEX:
1068 if (!desc->content_clone)
1069 return AVERROR_PATCHWELCOME;
1070 err = desc->content_clone(&ref, unit);
1074 av_assert0(0 && "Invalid content type.");
1079 if (desc->content_type != CBS_CONTENT_TYPE_POD) {
1080 av_buffer_unref(&unit->content_ref);
1081 unit->content_ref = ref;
1083 unit->content = unit->content_ref->data;