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 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
99 if (!ctx->priv_data) {
101 return AVERROR(ENOMEM);
104 ctx->decompose_unit_types = NULL;
106 ctx->trace_enable = 0;
107 ctx->trace_level = AV_LOG_TRACE;
113 void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
115 CodedBitstreamContext *ctx = *ctx_ptr;
120 if (ctx->codec && ctx->codec->close)
121 ctx->codec->close(ctx);
123 av_freep(&ctx->priv_data);
127 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
128 CodedBitstreamUnit *unit)
130 av_buffer_unref(&unit->content_ref);
131 unit->content = NULL;
133 av_buffer_unref(&unit->data_ref);
136 unit->data_bit_padding = 0;
139 void ff_cbs_fragment_reset(CodedBitstreamContext *ctx,
140 CodedBitstreamFragment *frag)
144 for (i = 0; i < frag->nb_units; i++)
145 cbs_unit_uninit(ctx, &frag->units[i]);
148 av_buffer_unref(&frag->data_ref);
151 frag->data_bit_padding = 0;
154 void ff_cbs_fragment_free(CodedBitstreamContext *ctx,
155 CodedBitstreamFragment *frag)
157 ff_cbs_fragment_reset(ctx, 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(CodedBitstreamContext *ctx,
201 CodedBitstreamFragment *frag,
202 const uint8_t *data, size_t size)
204 av_assert0(!frag->data && !frag->data_ref);
207 av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
209 return AVERROR(ENOMEM);
211 frag->data = frag->data_ref->data;
212 frag->data_size = size;
214 memcpy(frag->data, data, size);
215 memset(frag->data + size, 0,
216 AV_INPUT_BUFFER_PADDING_SIZE);
221 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
222 CodedBitstreamFragment *frag,
223 const AVCodecParameters *par)
227 err = cbs_fill_fragment_data(ctx, frag, par->extradata,
228 par->extradata_size);
232 err = ctx->codec->split_fragment(ctx, frag, 1);
236 return cbs_read_fragment_content(ctx, frag);
239 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
240 CodedBitstreamFragment *frag,
246 frag->data_ref = av_buffer_ref(pkt->buf);
248 return AVERROR(ENOMEM);
250 frag->data = pkt->data;
251 frag->data_size = pkt->size;
254 err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
259 err = ctx->codec->split_fragment(ctx, frag, 0);
263 return cbs_read_fragment_content(ctx, frag);
266 int ff_cbs_read(CodedBitstreamContext *ctx,
267 CodedBitstreamFragment *frag,
268 const uint8_t *data, size_t size)
272 err = cbs_fill_fragment_data(ctx, frag, data, size);
276 err = ctx->codec->split_fragment(ctx, frag, 0);
280 return cbs_read_fragment_content(ctx, frag);
284 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
285 CodedBitstreamFragment *frag)
289 for (i = 0; i < frag->nb_units; i++) {
290 CodedBitstreamUnit *unit = &frag->units[i];
295 av_buffer_unref(&unit->data_ref);
298 err = ctx->codec->write_unit(ctx, unit);
300 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
301 "(type %"PRIu32").\n", i, unit->type);
304 av_assert0(unit->data && unit->data_ref);
307 av_buffer_unref(&frag->data_ref);
310 err = ctx->codec->assemble_fragment(ctx, frag);
312 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
315 av_assert0(frag->data && frag->data_ref);
320 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
321 AVCodecParameters *par,
322 CodedBitstreamFragment *frag)
326 err = ff_cbs_write_fragment_data(ctx, frag);
330 av_freep(&par->extradata);
332 par->extradata = av_malloc(frag->data_size +
333 AV_INPUT_BUFFER_PADDING_SIZE);
335 return AVERROR(ENOMEM);
337 memcpy(par->extradata, frag->data, frag->data_size);
338 memset(par->extradata + frag->data_size, 0,
339 AV_INPUT_BUFFER_PADDING_SIZE);
340 par->extradata_size = frag->data_size;
345 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
347 CodedBitstreamFragment *frag)
352 err = ff_cbs_write_fragment_data(ctx, frag);
356 buf = av_buffer_ref(frag->data_ref);
358 return AVERROR(ENOMEM);
360 av_buffer_unref(&pkt->buf);
363 pkt->data = frag->data;
364 pkt->size = frag->data_size;
370 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
373 if (!ctx->trace_enable)
376 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
379 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
380 const char *str, const int *subscripts,
381 const char *bits, int64_t value)
384 size_t name_len, bits_len;
385 int pad, subs, i, j, k, n;
387 if (!ctx->trace_enable)
390 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
392 subs = subscripts ? subscripts[0] : 0;
394 for (i = j = 0; str[i];) {
398 k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
399 av_assert0(k > 0 && j + k < sizeof(name));
401 for (++i; str[i] && str[i] != ']'; i++);
402 av_assert0(str[i] == ']');
404 while (str[i] && str[i] != ']')
405 name[j++] = str[i++];
406 av_assert0(str[i] == ']');
409 av_assert0(j + 1 < sizeof(name));
410 name[j++] = str[i++];
413 av_assert0(j + 1 < sizeof(name));
415 av_assert0(n == subs);
417 name_len = strlen(name);
418 bits_len = strlen(bits);
420 if (name_len + bits_len > 60)
425 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
426 position, name, pad, bits, value);
429 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
430 int width, const char *name,
431 const int *subscripts, uint32_t *write_to,
432 uint32_t range_min, uint32_t range_max)
437 av_assert0(width > 0 && width <= 32);
439 if (get_bits_left(gbc) < width) {
440 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
441 "%s: bitstream ended.\n", name);
442 return AVERROR_INVALIDDATA;
445 if (ctx->trace_enable)
446 position = get_bits_count(gbc);
448 value = get_bits_long(gbc, width);
450 if (ctx->trace_enable) {
453 for (i = 0; i < width; i++)
454 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
457 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
461 if (value < range_min || value > range_max) {
462 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
463 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
464 name, value, range_min, range_max);
465 return AVERROR_INVALIDDATA;
472 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
473 int width, const char *name,
474 const int *subscripts, uint32_t value,
475 uint32_t range_min, uint32_t range_max)
477 av_assert0(width > 0 && width <= 32);
479 if (value < range_min || value > range_max) {
480 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
481 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
482 name, value, range_min, range_max);
483 return AVERROR_INVALIDDATA;
486 if (put_bits_left(pbc) < width)
487 return AVERROR(ENOSPC);
489 if (ctx->trace_enable) {
492 for (i = 0; i < width; i++)
493 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
496 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
497 name, subscripts, bits, value);
501 put_bits(pbc, width, value);
503 put_bits32(pbc, value);
508 int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
509 int width, const char *name,
510 const int *subscripts, int32_t *write_to,
511 int32_t range_min, int32_t range_max)
516 av_assert0(width > 0 && width <= 32);
518 if (get_bits_left(gbc) < width) {
519 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
520 "%s: bitstream ended.\n", name);
521 return AVERROR_INVALIDDATA;
524 if (ctx->trace_enable)
525 position = get_bits_count(gbc);
527 value = get_sbits_long(gbc, width);
529 if (ctx->trace_enable) {
532 for (i = 0; i < width; i++)
533 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
536 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
540 if (value < range_min || value > range_max) {
541 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
542 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
543 name, value, range_min, range_max);
544 return AVERROR_INVALIDDATA;
551 int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
552 int width, const char *name,
553 const int *subscripts, int32_t value,
554 int32_t range_min, int32_t range_max)
556 av_assert0(width > 0 && width <= 32);
558 if (value < range_min || value > range_max) {
559 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
560 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
561 name, value, range_min, range_max);
562 return AVERROR_INVALIDDATA;
565 if (put_bits_left(pbc) < width)
566 return AVERROR(ENOSPC);
568 if (ctx->trace_enable) {
571 for (i = 0; i < width; i++)
572 bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
575 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
576 name, subscripts, bits, value);
580 put_sbits(pbc, width, value);
582 put_bits32(pbc, value);
588 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
589 CodedBitstreamUnit *unit,
591 void (*free)(void *opaque, uint8_t *data))
593 av_assert0(!unit->content && !unit->content_ref);
595 unit->content = av_mallocz(size);
597 return AVERROR(ENOMEM);
599 unit->content_ref = av_buffer_create(unit->content, size,
601 if (!unit->content_ref) {
602 av_freep(&unit->content);
603 return AVERROR(ENOMEM);
609 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
610 CodedBitstreamUnit *unit,
613 av_assert0(!unit->data && !unit->data_ref);
615 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
617 return AVERROR(ENOMEM);
619 unit->data = unit->data_ref->data;
620 unit->data_size = size;
622 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
627 static int cbs_insert_unit(CodedBitstreamContext *ctx,
628 CodedBitstreamFragment *frag,
631 CodedBitstreamUnit *units;
633 if (frag->nb_units < frag->nb_units_allocated) {
636 if (position < frag->nb_units)
637 memmove(units + position + 1, units + position,
638 (frag->nb_units - position) * sizeof(*units));
640 units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
642 return AVERROR(ENOMEM);
644 ++frag->nb_units_allocated;
647 memcpy(units, frag->units, position * sizeof(*units));
649 if (position < frag->nb_units)
650 memcpy(units + position + 1, frag->units + position,
651 (frag->nb_units - position) * sizeof(*units));
654 memset(units + position, 0, sizeof(*units));
656 if (units != frag->units) {
657 av_free(frag->units);
666 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
667 CodedBitstreamFragment *frag,
669 CodedBitstreamUnitType type,
671 AVBufferRef *content_buf)
673 CodedBitstreamUnit *unit;
674 AVBufferRef *content_ref;
678 position = frag->nb_units;
679 av_assert0(position >= 0 && position <= frag->nb_units);
682 content_ref = av_buffer_ref(content_buf);
684 return AVERROR(ENOMEM);
689 err = cbs_insert_unit(ctx, frag, position);
691 av_buffer_unref(&content_ref);
695 unit = &frag->units[position];
697 unit->content = content;
698 unit->content_ref = content_ref;
703 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
704 CodedBitstreamFragment *frag,
706 CodedBitstreamUnitType type,
707 uint8_t *data, size_t data_size,
708 AVBufferRef *data_buf)
710 CodedBitstreamUnit *unit;
711 AVBufferRef *data_ref;
715 position = frag->nb_units;
716 av_assert0(position >= 0 && position <= frag->nb_units);
719 data_ref = av_buffer_ref(data_buf);
721 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
723 return AVERROR(ENOMEM);
725 err = cbs_insert_unit(ctx, frag, position);
727 av_buffer_unref(&data_ref);
731 unit = &frag->units[position];
734 unit->data_size = data_size;
735 unit->data_ref = data_ref;
740 void ff_cbs_delete_unit(CodedBitstreamContext *ctx,
741 CodedBitstreamFragment *frag,
744 av_assert0(0 <= position && position < frag->nb_units
745 && "Unit to be deleted not in fragment.");
747 cbs_unit_uninit(ctx, &frag->units[position]);
751 if (frag->nb_units > 0)
752 memmove(frag->units + position,
753 frag->units + position + 1,
754 (frag->nb_units - position) * sizeof(*frag->units));