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(CodedBitstreamUnit *unit)
132 av_buffer_unref(&unit->content_ref);
133 unit->content = NULL;
135 av_buffer_unref(&unit->data_ref);
138 unit->data_bit_padding = 0;
141 void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
145 for (i = 0; i < frag->nb_units; i++)
146 cbs_unit_uninit(&frag->units[i]);
149 av_buffer_unref(&frag->data_ref);
152 frag->data_bit_padding = 0;
155 void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
157 ff_cbs_fragment_reset(frag);
159 av_freep(&frag->units);
160 frag->nb_units_allocated = 0;
163 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
164 CodedBitstreamFragment *frag)
168 for (i = 0; i < frag->nb_units; i++) {
169 CodedBitstreamUnit *unit = &frag->units[i];
171 if (ctx->decompose_unit_types) {
172 for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
173 if (ctx->decompose_unit_types[j] == unit->type)
176 if (j >= ctx->nb_decompose_unit_types)
180 av_buffer_unref(&unit->content_ref);
181 unit->content = NULL;
183 av_assert0(unit->data && unit->data_ref);
185 err = ctx->codec->read_unit(ctx, unit);
186 if (err == AVERROR(ENOSYS)) {
187 av_log(ctx->log_ctx, AV_LOG_VERBOSE,
188 "Decomposition unimplemented for unit %d "
189 "(type %"PRIu32").\n", i, unit->type);
190 } else if (err < 0) {
191 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
192 "(type %"PRIu32").\n", i, unit->type);
200 static int cbs_fill_fragment_data(CodedBitstreamFragment *frag,
201 const uint8_t *data, size_t size)
203 av_assert0(!frag->data && !frag->data_ref);
206 av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
208 return AVERROR(ENOMEM);
210 frag->data = frag->data_ref->data;
211 frag->data_size = size;
213 memcpy(frag->data, data, size);
214 memset(frag->data + size, 0,
215 AV_INPUT_BUFFER_PADDING_SIZE);
220 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
221 CodedBitstreamFragment *frag,
222 const AVCodecParameters *par)
226 err = cbs_fill_fragment_data(frag, par->extradata,
227 par->extradata_size);
231 err = ctx->codec->split_fragment(ctx, frag, 1);
235 return cbs_read_fragment_content(ctx, frag);
238 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
239 CodedBitstreamFragment *frag,
245 frag->data_ref = av_buffer_ref(pkt->buf);
247 return AVERROR(ENOMEM);
249 frag->data = pkt->data;
250 frag->data_size = pkt->size;
253 err = cbs_fill_fragment_data(frag, pkt->data, pkt->size);
258 err = ctx->codec->split_fragment(ctx, frag, 0);
262 return cbs_read_fragment_content(ctx, frag);
265 int ff_cbs_read(CodedBitstreamContext *ctx,
266 CodedBitstreamFragment *frag,
267 const uint8_t *data, size_t size)
271 err = cbs_fill_fragment_data(frag, data, size);
275 err = ctx->codec->split_fragment(ctx, frag, 0);
279 return cbs_read_fragment_content(ctx, frag);
282 static int cbs_write_unit_data(CodedBitstreamContext *ctx,
283 CodedBitstreamUnit *unit)
288 if (!ctx->write_buffer) {
289 // Initial write buffer size is 1MB.
290 ctx->write_buffer_size = 1024 * 1024;
292 reallocate_and_try_again:
293 ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
295 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
296 "sufficiently large write buffer (last attempt "
297 "%"SIZE_SPECIFIER" bytes).\n", ctx->write_buffer_size);
302 init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
304 ret = ctx->codec->write_unit(ctx, unit, &pbc);
306 if (ret == AVERROR(ENOSPC)) {
308 if (ctx->write_buffer_size == INT_MAX / 8)
309 return AVERROR(ENOMEM);
310 ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
311 goto reallocate_and_try_again;
313 // Write failed for some other reason.
317 // Overflow but we didn't notice.
318 av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
320 if (put_bits_count(&pbc) % 8)
321 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
323 unit->data_bit_padding = 0;
325 flush_put_bits(&pbc);
327 ret = ff_cbs_alloc_unit_data(unit, put_bits_count(&pbc) / 8);
331 memcpy(unit->data, ctx->write_buffer, unit->data_size);
336 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
337 CodedBitstreamFragment *frag)
341 for (i = 0; i < frag->nb_units; i++) {
342 CodedBitstreamUnit *unit = &frag->units[i];
347 av_buffer_unref(&unit->data_ref);
350 err = cbs_write_unit_data(ctx, unit);
352 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
353 "(type %"PRIu32").\n", i, unit->type);
356 av_assert0(unit->data && unit->data_ref);
359 av_buffer_unref(&frag->data_ref);
362 err = ctx->codec->assemble_fragment(ctx, frag);
364 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
367 av_assert0(frag->data && frag->data_ref);
372 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
373 AVCodecParameters *par,
374 CodedBitstreamFragment *frag)
378 err = ff_cbs_write_fragment_data(ctx, frag);
382 av_freep(&par->extradata);
384 par->extradata = av_malloc(frag->data_size +
385 AV_INPUT_BUFFER_PADDING_SIZE);
387 return AVERROR(ENOMEM);
389 memcpy(par->extradata, frag->data, frag->data_size);
390 memset(par->extradata + frag->data_size, 0,
391 AV_INPUT_BUFFER_PADDING_SIZE);
392 par->extradata_size = frag->data_size;
397 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
399 CodedBitstreamFragment *frag)
404 err = ff_cbs_write_fragment_data(ctx, frag);
408 buf = av_buffer_ref(frag->data_ref);
410 return AVERROR(ENOMEM);
412 av_buffer_unref(&pkt->buf);
415 pkt->data = frag->data;
416 pkt->size = frag->data_size;
422 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
425 if (!ctx->trace_enable)
428 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
431 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
432 const char *str, const int *subscripts,
433 const char *bits, int64_t value)
436 size_t name_len, bits_len;
437 int pad, subs, i, j, k, n;
439 if (!ctx->trace_enable)
442 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
444 subs = subscripts ? subscripts[0] : 0;
446 for (i = j = 0; str[i];) {
450 k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
451 av_assert0(k > 0 && j + k < sizeof(name));
453 for (++i; str[i] && str[i] != ']'; i++);
454 av_assert0(str[i] == ']');
456 while (str[i] && str[i] != ']')
457 name[j++] = str[i++];
458 av_assert0(str[i] == ']');
461 av_assert0(j + 1 < sizeof(name));
462 name[j++] = str[i++];
465 av_assert0(j + 1 < sizeof(name));
467 av_assert0(n == subs);
469 name_len = strlen(name);
470 bits_len = strlen(bits);
472 if (name_len + bits_len > 60)
477 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
478 position, name, pad, bits, value);
481 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
482 int width, const char *name,
483 const int *subscripts, uint32_t *write_to,
484 uint32_t range_min, uint32_t range_max)
489 av_assert0(width > 0 && width <= 32);
491 if (get_bits_left(gbc) < width) {
492 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
493 "%s: bitstream ended.\n", name);
494 return AVERROR_INVALIDDATA;
497 if (ctx->trace_enable)
498 position = get_bits_count(gbc);
500 value = get_bits_long(gbc, width);
502 if (ctx->trace_enable) {
505 for (i = 0; i < width; i++)
506 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
509 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
513 if (value < range_min || value > range_max) {
514 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
515 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
516 name, value, range_min, range_max);
517 return AVERROR_INVALIDDATA;
524 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
525 int width, const char *name,
526 const int *subscripts, uint32_t value,
527 uint32_t range_min, uint32_t range_max)
529 av_assert0(width > 0 && width <= 32);
531 if (value < range_min || value > range_max) {
532 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
533 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
534 name, value, range_min, range_max);
535 return AVERROR_INVALIDDATA;
538 if (put_bits_left(pbc) < width)
539 return AVERROR(ENOSPC);
541 if (ctx->trace_enable) {
544 for (i = 0; i < width; i++)
545 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
548 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
549 name, subscripts, bits, value);
553 put_bits(pbc, width, value);
555 put_bits32(pbc, value);
560 int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
561 int width, const char *name,
562 const int *subscripts, int32_t *write_to,
563 int32_t range_min, int32_t range_max)
568 av_assert0(width > 0 && width <= 32);
570 if (get_bits_left(gbc) < width) {
571 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
572 "%s: bitstream ended.\n", name);
573 return AVERROR_INVALIDDATA;
576 if (ctx->trace_enable)
577 position = get_bits_count(gbc);
579 value = get_sbits_long(gbc, width);
581 if (ctx->trace_enable) {
584 for (i = 0; i < width; i++)
585 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
588 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
592 if (value < range_min || value > range_max) {
593 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
594 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
595 name, value, range_min, range_max);
596 return AVERROR_INVALIDDATA;
603 int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
604 int width, const char *name,
605 const int *subscripts, int32_t value,
606 int32_t range_min, int32_t range_max)
608 av_assert0(width > 0 && width <= 32);
610 if (value < range_min || value > range_max) {
611 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
612 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
613 name, value, range_min, range_max);
614 return AVERROR_INVALIDDATA;
617 if (put_bits_left(pbc) < width)
618 return AVERROR(ENOSPC);
620 if (ctx->trace_enable) {
623 for (i = 0; i < width; i++)
624 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
627 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
628 name, subscripts, bits, value);
632 put_sbits(pbc, width, value);
634 put_bits32(pbc, value);
640 int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit,
642 void (*free)(void *opaque, uint8_t *data))
644 av_assert0(!unit->content && !unit->content_ref);
646 unit->content = av_mallocz(size);
648 return AVERROR(ENOMEM);
650 unit->content_ref = av_buffer_create(unit->content, size,
652 if (!unit->content_ref) {
653 av_freep(&unit->content);
654 return AVERROR(ENOMEM);
660 int ff_cbs_alloc_unit_data(CodedBitstreamUnit *unit,
663 av_assert0(!unit->data && !unit->data_ref);
665 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
667 return AVERROR(ENOMEM);
669 unit->data = unit->data_ref->data;
670 unit->data_size = size;
672 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
677 static int cbs_insert_unit(CodedBitstreamFragment *frag,
680 CodedBitstreamUnit *units;
682 if (frag->nb_units < frag->nb_units_allocated) {
685 if (position < frag->nb_units)
686 memmove(units + position + 1, units + position,
687 (frag->nb_units - position) * sizeof(*units));
689 units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
691 return AVERROR(ENOMEM);
693 frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
696 memcpy(units, frag->units, position * sizeof(*units));
698 if (position < frag->nb_units)
699 memcpy(units + position + 1, frag->units + position,
700 (frag->nb_units - position) * sizeof(*units));
703 memset(units + position, 0, sizeof(*units));
705 if (units != frag->units) {
706 av_free(frag->units);
715 int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
717 CodedBitstreamUnitType type,
719 AVBufferRef *content_buf)
721 CodedBitstreamUnit *unit;
722 AVBufferRef *content_ref;
726 position = frag->nb_units;
727 av_assert0(position >= 0 && position <= frag->nb_units);
730 content_ref = av_buffer_ref(content_buf);
732 return AVERROR(ENOMEM);
737 err = cbs_insert_unit(frag, position);
739 av_buffer_unref(&content_ref);
743 unit = &frag->units[position];
745 unit->content = content;
746 unit->content_ref = content_ref;
751 int ff_cbs_insert_unit_data(CodedBitstreamFragment *frag,
753 CodedBitstreamUnitType type,
754 uint8_t *data, size_t data_size,
755 AVBufferRef *data_buf)
757 CodedBitstreamUnit *unit;
758 AVBufferRef *data_ref;
762 position = frag->nb_units;
763 av_assert0(position >= 0 && position <= frag->nb_units);
766 data_ref = av_buffer_ref(data_buf);
768 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
772 return AVERROR(ENOMEM);
775 err = cbs_insert_unit(frag, position);
777 av_buffer_unref(&data_ref);
781 unit = &frag->units[position];
784 unit->data_size = data_size;
785 unit->data_ref = data_ref;
790 void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
793 av_assert0(0 <= position && position < frag->nb_units
794 && "Unit to be deleted not in fragment.");
796 cbs_unit_uninit(&frag->units[position]);
800 if (frag->nb_units > 0)
801 memmove(frag->units + position,
802 frag->units + position + 1,
803 (frag->nb_units - position) * sizeof(*frag->units));