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"
28 #include "cbs_internal.h"
31 static const CodedBitstreamType *cbs_type_table[] = {
52 const enum AVCodecID ff_cbs_all_codec_ids[] = {
66 AV_CODEC_ID_MPEG2VIDEO,
74 int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
75 enum AVCodecID codec_id, void *log_ctx)
77 CodedBitstreamContext *ctx;
78 const CodedBitstreamType *type;
82 for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
83 if (cbs_type_table[i]->codec_id == codec_id) {
84 type = cbs_type_table[i];
89 return AVERROR(EINVAL);
91 ctx = av_mallocz(sizeof(*ctx));
93 return AVERROR(ENOMEM);
95 ctx->log_ctx = log_ctx;
98 if (type->priv_data_size) {
99 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
100 if (!ctx->priv_data) {
102 return AVERROR(ENOMEM);
106 ctx->decompose_unit_types = NULL;
108 ctx->trace_enable = 0;
109 ctx->trace_level = AV_LOG_TRACE;
115 void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
117 CodedBitstreamContext *ctx = *ctx_ptr;
122 if (ctx->codec && ctx->codec->close)
123 ctx->codec->close(ctx);
125 av_freep(&ctx->write_buffer);
126 av_freep(&ctx->priv_data);
130 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
131 CodedBitstreamUnit *unit)
133 av_buffer_unref(&unit->content_ref);
134 unit->content = NULL;
136 av_buffer_unref(&unit->data_ref);
139 unit->data_bit_padding = 0;
142 void ff_cbs_fragment_reset(CodedBitstreamContext *ctx,
143 CodedBitstreamFragment *frag)
147 for (i = 0; i < frag->nb_units; i++)
148 cbs_unit_uninit(ctx, &frag->units[i]);
151 av_buffer_unref(&frag->data_ref);
154 frag->data_bit_padding = 0;
157 void ff_cbs_fragment_free(CodedBitstreamContext *ctx,
158 CodedBitstreamFragment *frag)
160 ff_cbs_fragment_reset(ctx, frag);
162 av_freep(&frag->units);
163 frag->nb_units_allocated = 0;
166 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
167 CodedBitstreamFragment *frag)
171 for (i = 0; i < frag->nb_units; i++) {
172 CodedBitstreamUnit *unit = &frag->units[i];
174 if (ctx->decompose_unit_types) {
175 for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
176 if (ctx->decompose_unit_types[j] == unit->type)
179 if (j >= ctx->nb_decompose_unit_types)
183 av_buffer_unref(&unit->content_ref);
184 unit->content = NULL;
186 av_assert0(unit->data && unit->data_ref);
188 err = ctx->codec->read_unit(ctx, unit);
189 if (err == AVERROR(ENOSYS)) {
190 av_log(ctx->log_ctx, AV_LOG_VERBOSE,
191 "Decomposition unimplemented for unit %d "
192 "(type %"PRIu32").\n", i, unit->type);
193 } else if (err < 0) {
194 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
195 "(type %"PRIu32").\n", i, unit->type);
203 static int cbs_fill_fragment_data(CodedBitstreamContext *ctx,
204 CodedBitstreamFragment *frag,
205 const uint8_t *data, size_t size)
207 av_assert0(!frag->data && !frag->data_ref);
210 av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
212 return AVERROR(ENOMEM);
214 frag->data = frag->data_ref->data;
215 frag->data_size = size;
217 memcpy(frag->data, data, size);
218 memset(frag->data + size, 0,
219 AV_INPUT_BUFFER_PADDING_SIZE);
224 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
225 CodedBitstreamFragment *frag,
226 const AVCodecParameters *par)
230 err = cbs_fill_fragment_data(ctx, frag, par->extradata,
231 par->extradata_size);
235 err = ctx->codec->split_fragment(ctx, frag, 1);
239 return cbs_read_fragment_content(ctx, frag);
242 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
243 CodedBitstreamFragment *frag,
249 frag->data_ref = av_buffer_ref(pkt->buf);
251 return AVERROR(ENOMEM);
253 frag->data = pkt->data;
254 frag->data_size = pkt->size;
257 err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
262 err = ctx->codec->split_fragment(ctx, frag, 0);
266 return cbs_read_fragment_content(ctx, frag);
269 int ff_cbs_read(CodedBitstreamContext *ctx,
270 CodedBitstreamFragment *frag,
271 const uint8_t *data, size_t size)
275 err = cbs_fill_fragment_data(ctx, frag, data, size);
279 err = ctx->codec->split_fragment(ctx, frag, 0);
283 return cbs_read_fragment_content(ctx, frag);
286 static int cbs_write_unit_data(CodedBitstreamContext *ctx,
287 CodedBitstreamUnit *unit)
292 if (!ctx->write_buffer) {
293 // Initial write buffer size is 1MB.
294 ctx->write_buffer_size = 1024 * 1024;
296 reallocate_and_try_again:
297 ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
299 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
300 "sufficiently large write buffer (last attempt "
301 "%"SIZE_SPECIFIER" bytes).\n", ctx->write_buffer_size);
306 init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
308 ret = ctx->codec->write_unit(ctx, unit, &pbc);
310 if (ret == AVERROR(ENOSPC)) {
312 if (ctx->write_buffer_size == INT_MAX / 8)
313 return AVERROR(ENOMEM);
314 ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
315 goto reallocate_and_try_again;
317 // Write failed for some other reason.
321 // Overflow but we didn't notice.
322 av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
324 if (put_bits_count(&pbc) % 8)
325 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
327 unit->data_bit_padding = 0;
329 flush_put_bits(&pbc);
331 ret = ff_cbs_alloc_unit_data(ctx, unit, put_bits_count(&pbc) / 8);
335 memcpy(unit->data, ctx->write_buffer, unit->data_size);
340 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
341 CodedBitstreamFragment *frag)
345 for (i = 0; i < frag->nb_units; i++) {
346 CodedBitstreamUnit *unit = &frag->units[i];
351 av_buffer_unref(&unit->data_ref);
354 err = cbs_write_unit_data(ctx, unit);
356 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
357 "(type %"PRIu32").\n", i, unit->type);
360 av_assert0(unit->data && unit->data_ref);
363 av_buffer_unref(&frag->data_ref);
366 err = ctx->codec->assemble_fragment(ctx, frag);
368 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
371 av_assert0(frag->data && frag->data_ref);
376 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
377 AVCodecParameters *par,
378 CodedBitstreamFragment *frag)
382 err = ff_cbs_write_fragment_data(ctx, frag);
386 av_freep(&par->extradata);
388 par->extradata = av_malloc(frag->data_size +
389 AV_INPUT_BUFFER_PADDING_SIZE);
391 return AVERROR(ENOMEM);
393 memcpy(par->extradata, frag->data, frag->data_size);
394 memset(par->extradata + frag->data_size, 0,
395 AV_INPUT_BUFFER_PADDING_SIZE);
396 par->extradata_size = frag->data_size;
401 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
403 CodedBitstreamFragment *frag)
408 err = ff_cbs_write_fragment_data(ctx, frag);
412 buf = av_buffer_ref(frag->data_ref);
414 return AVERROR(ENOMEM);
416 av_buffer_unref(&pkt->buf);
419 pkt->data = frag->data;
420 pkt->size = frag->data_size;
426 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
429 if (!ctx->trace_enable)
432 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
435 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
436 const char *str, const int *subscripts,
437 const char *bits, int64_t value)
440 size_t name_len, bits_len;
441 int pad, subs, i, j, k, n;
443 if (!ctx->trace_enable)
446 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
448 subs = subscripts ? subscripts[0] : 0;
450 for (i = j = 0; str[i];) {
454 k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
455 av_assert0(k > 0 && j + k < sizeof(name));
457 for (++i; str[i] && str[i] != ']'; i++);
458 av_assert0(str[i] == ']');
460 while (str[i] && str[i] != ']')
461 name[j++] = str[i++];
462 av_assert0(str[i] == ']');
465 av_assert0(j + 1 < sizeof(name));
466 name[j++] = str[i++];
469 av_assert0(j + 1 < sizeof(name));
471 av_assert0(n == subs);
473 name_len = strlen(name);
474 bits_len = strlen(bits);
476 if (name_len + bits_len > 60)
481 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
482 position, name, pad, bits, value);
485 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
486 int width, const char *name,
487 const int *subscripts, uint32_t *write_to,
488 uint32_t range_min, uint32_t range_max)
493 av_assert0(width > 0 && width <= 32);
495 if (get_bits_left(gbc) < width) {
496 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
497 "%s: bitstream ended.\n", name);
498 return AVERROR_INVALIDDATA;
501 if (ctx->trace_enable)
502 position = get_bits_count(gbc);
504 value = get_bits_long(gbc, width);
506 if (ctx->trace_enable) {
509 for (i = 0; i < width; i++)
510 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
513 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
517 if (value < range_min || value > range_max) {
518 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
519 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
520 name, value, range_min, range_max);
521 return AVERROR_INVALIDDATA;
528 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
529 int width, const char *name,
530 const int *subscripts, uint32_t value,
531 uint32_t range_min, uint32_t range_max)
533 av_assert0(width > 0 && width <= 32);
535 if (value < range_min || value > range_max) {
536 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
537 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
538 name, value, range_min, range_max);
539 return AVERROR_INVALIDDATA;
542 if (put_bits_left(pbc) < width)
543 return AVERROR(ENOSPC);
545 if (ctx->trace_enable) {
548 for (i = 0; i < width; i++)
549 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
552 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
553 name, subscripts, bits, value);
557 put_bits(pbc, width, value);
559 put_bits32(pbc, value);
564 int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
565 int width, const char *name,
566 const int *subscripts, int32_t *write_to,
567 int32_t range_min, int32_t range_max)
572 av_assert0(width > 0 && width <= 32);
574 if (get_bits_left(gbc) < width) {
575 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
576 "%s: bitstream ended.\n", name);
577 return AVERROR_INVALIDDATA;
580 if (ctx->trace_enable)
581 position = get_bits_count(gbc);
583 value = get_sbits_long(gbc, width);
585 if (ctx->trace_enable) {
588 for (i = 0; i < width; i++)
589 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
592 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
596 if (value < range_min || value > range_max) {
597 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
598 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
599 name, value, range_min, range_max);
600 return AVERROR_INVALIDDATA;
607 int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
608 int width, const char *name,
609 const int *subscripts, int32_t value,
610 int32_t range_min, int32_t range_max)
612 av_assert0(width > 0 && width <= 32);
614 if (value < range_min || value > range_max) {
615 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
616 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
617 name, value, range_min, range_max);
618 return AVERROR_INVALIDDATA;
621 if (put_bits_left(pbc) < width)
622 return AVERROR(ENOSPC);
624 if (ctx->trace_enable) {
627 for (i = 0; i < width; i++)
628 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
631 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
632 name, subscripts, bits, value);
636 put_sbits(pbc, width, value);
638 put_bits32(pbc, value);
644 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
645 CodedBitstreamUnit *unit,
647 void (*free)(void *opaque, uint8_t *data))
649 av_assert0(!unit->content && !unit->content_ref);
651 unit->content = av_mallocz(size);
653 return AVERROR(ENOMEM);
655 unit->content_ref = av_buffer_create(unit->content, size,
657 if (!unit->content_ref) {
658 av_freep(&unit->content);
659 return AVERROR(ENOMEM);
665 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
666 CodedBitstreamUnit *unit,
669 av_assert0(!unit->data && !unit->data_ref);
671 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
673 return AVERROR(ENOMEM);
675 unit->data = unit->data_ref->data;
676 unit->data_size = size;
678 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
683 static int cbs_insert_unit(CodedBitstreamContext *ctx,
684 CodedBitstreamFragment *frag,
687 CodedBitstreamUnit *units;
689 if (frag->nb_units < frag->nb_units_allocated) {
692 if (position < frag->nb_units)
693 memmove(units + position + 1, units + position,
694 (frag->nb_units - position) * sizeof(*units));
696 units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
698 return AVERROR(ENOMEM);
700 frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
703 memcpy(units, frag->units, position * sizeof(*units));
705 if (position < frag->nb_units)
706 memcpy(units + position + 1, frag->units + position,
707 (frag->nb_units - position) * sizeof(*units));
710 memset(units + position, 0, sizeof(*units));
712 if (units != frag->units) {
713 av_free(frag->units);
722 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
723 CodedBitstreamFragment *frag,
725 CodedBitstreamUnitType type,
727 AVBufferRef *content_buf)
729 CodedBitstreamUnit *unit;
730 AVBufferRef *content_ref;
734 position = frag->nb_units;
735 av_assert0(position >= 0 && position <= frag->nb_units);
738 content_ref = av_buffer_ref(content_buf);
740 return AVERROR(ENOMEM);
745 err = cbs_insert_unit(ctx, frag, position);
747 av_buffer_unref(&content_ref);
751 unit = &frag->units[position];
753 unit->content = content;
754 unit->content_ref = content_ref;
759 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
760 CodedBitstreamFragment *frag,
762 CodedBitstreamUnitType type,
763 uint8_t *data, size_t data_size,
764 AVBufferRef *data_buf)
766 CodedBitstreamUnit *unit;
767 AVBufferRef *data_ref;
771 position = frag->nb_units;
772 av_assert0(position >= 0 && position <= frag->nb_units);
775 data_ref = av_buffer_ref(data_buf);
777 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
781 return AVERROR(ENOMEM);
784 err = cbs_insert_unit(ctx, frag, position);
786 av_buffer_unref(&data_ref);
790 unit = &frag->units[position];
793 unit->data_size = data_size;
794 unit->data_ref = data_ref;
799 void ff_cbs_delete_unit(CodedBitstreamContext *ctx,
800 CodedBitstreamFragment *frag,
803 av_assert0(0 <= position && position < frag->nb_units
804 && "Unit to be deleted not in fragment.");
806 cbs_unit_uninit(ctx, &frag->units[position]);
810 if (frag->nb_units > 0)
811 memmove(frag->units + position,
812 frag->units + position + 1,
813 (frag->nb_units - position) * sizeof(*frag->units));