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);
362 pkt->data = frag->data;
363 pkt->size = frag->data_size;
369 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
372 if (!ctx->trace_enable)
375 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
378 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
379 const char *str, const int *subscripts,
380 const char *bits, int64_t value)
383 size_t name_len, bits_len;
384 int pad, subs, i, j, k, n;
386 if (!ctx->trace_enable)
389 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
391 subs = subscripts ? subscripts[0] : 0;
393 for (i = j = 0; str[i];) {
397 k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
398 av_assert0(k > 0 && j + k < sizeof(name));
400 for (++i; str[i] && str[i] != ']'; i++);
401 av_assert0(str[i] == ']');
403 while (str[i] && str[i] != ']')
404 name[j++] = str[i++];
405 av_assert0(str[i] == ']');
408 av_assert0(j + 1 < sizeof(name));
409 name[j++] = str[i++];
412 av_assert0(j + 1 < sizeof(name));
414 av_assert0(n == subs);
416 name_len = strlen(name);
417 bits_len = strlen(bits);
419 if (name_len + bits_len > 60)
424 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
425 position, name, pad, bits, value);
428 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
429 int width, const char *name,
430 const int *subscripts, uint32_t *write_to,
431 uint32_t range_min, uint32_t range_max)
436 av_assert0(width > 0 && width <= 32);
438 if (get_bits_left(gbc) < width) {
439 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
440 "%s: bitstream ended.\n", name);
441 return AVERROR_INVALIDDATA;
444 if (ctx->trace_enable)
445 position = get_bits_count(gbc);
447 value = get_bits_long(gbc, width);
449 if (ctx->trace_enable) {
452 for (i = 0; i < width; i++)
453 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
456 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
460 if (value < range_min || value > range_max) {
461 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
462 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
463 name, value, range_min, range_max);
464 return AVERROR_INVALIDDATA;
471 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
472 int width, const char *name,
473 const int *subscripts, uint32_t value,
474 uint32_t range_min, uint32_t range_max)
476 av_assert0(width > 0 && width <= 32);
478 if (value < range_min || value > range_max) {
479 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
480 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
481 name, value, range_min, range_max);
482 return AVERROR_INVALIDDATA;
485 if (put_bits_left(pbc) < width)
486 return AVERROR(ENOSPC);
488 if (ctx->trace_enable) {
491 for (i = 0; i < width; i++)
492 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
495 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
496 name, subscripts, bits, value);
500 put_bits(pbc, width, value);
502 put_bits32(pbc, value);
508 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
509 CodedBitstreamUnit *unit,
511 void (*free)(void *opaque, uint8_t *data))
513 av_assert0(!unit->content && !unit->content_ref);
515 unit->content = av_mallocz(size);
517 return AVERROR(ENOMEM);
519 unit->content_ref = av_buffer_create(unit->content, size,
521 if (!unit->content_ref) {
522 av_freep(&unit->content);
523 return AVERROR(ENOMEM);
529 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
530 CodedBitstreamUnit *unit,
533 av_assert0(!unit->data && !unit->data_ref);
535 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
537 return AVERROR(ENOMEM);
539 unit->data = unit->data_ref->data;
540 unit->data_size = size;
542 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
547 static int cbs_insert_unit(CodedBitstreamContext *ctx,
548 CodedBitstreamFragment *frag,
551 CodedBitstreamUnit *units;
553 if (frag->nb_units < frag->nb_units_allocated) {
556 if (position < frag->nb_units)
557 memmove(units + position + 1, units + position,
558 (frag->nb_units - position) * sizeof(*units));
560 units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
562 return AVERROR(ENOMEM);
564 ++frag->nb_units_allocated;
567 memcpy(units, frag->units, position * sizeof(*units));
569 if (position < frag->nb_units)
570 memcpy(units + position + 1, frag->units + position,
571 (frag->nb_units - position) * sizeof(*units));
574 memset(units + position, 0, sizeof(*units));
576 if (units != frag->units) {
577 av_free(frag->units);
586 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
587 CodedBitstreamFragment *frag,
589 CodedBitstreamUnitType type,
591 AVBufferRef *content_buf)
593 CodedBitstreamUnit *unit;
594 AVBufferRef *content_ref;
598 position = frag->nb_units;
599 av_assert0(position >= 0 && position <= frag->nb_units);
602 content_ref = av_buffer_ref(content_buf);
604 return AVERROR(ENOMEM);
609 err = cbs_insert_unit(ctx, frag, position);
611 av_buffer_unref(&content_ref);
615 unit = &frag->units[position];
617 unit->content = content;
618 unit->content_ref = content_ref;
623 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
624 CodedBitstreamFragment *frag,
626 CodedBitstreamUnitType type,
627 uint8_t *data, size_t data_size,
628 AVBufferRef *data_buf)
630 CodedBitstreamUnit *unit;
631 AVBufferRef *data_ref;
635 position = frag->nb_units;
636 av_assert0(position >= 0 && position <= frag->nb_units);
639 data_ref = av_buffer_ref(data_buf);
641 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
643 return AVERROR(ENOMEM);
645 err = cbs_insert_unit(ctx, frag, position);
647 av_buffer_unref(&data_ref);
651 unit = &frag->units[position];
654 unit->data_size = data_size;
655 unit->data_ref = data_ref;
660 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
661 CodedBitstreamFragment *frag,
664 if (position < 0 || position >= frag->nb_units)
665 return AVERROR(EINVAL);
667 cbs_unit_uninit(ctx, &frag->units[position]);
671 if (frag->nb_units > 0)
672 memmove(frag->units + position,
673 frag->units + position + 1,
674 (frag->nb_units - position) * sizeof(*frag->units));