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[] = {
46 const enum AVCodecID ff_cbs_all_codec_ids[] = {
54 AV_CODEC_ID_MPEG2VIDEO,
62 int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
63 enum AVCodecID codec_id, void *log_ctx)
65 CodedBitstreamContext *ctx;
66 const CodedBitstreamType *type;
70 for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
71 if (cbs_type_table[i]->codec_id == codec_id) {
72 type = cbs_type_table[i];
77 return AVERROR(EINVAL);
79 ctx = av_mallocz(sizeof(*ctx));
81 return AVERROR(ENOMEM);
83 ctx->log_ctx = log_ctx;
86 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
87 if (!ctx->priv_data) {
89 return AVERROR(ENOMEM);
92 ctx->decompose_unit_types = NULL;
94 ctx->trace_enable = 0;
95 ctx->trace_level = AV_LOG_TRACE;
101 void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
103 CodedBitstreamContext *ctx = *ctx_ptr;
108 if (ctx->codec && ctx->codec->close)
109 ctx->codec->close(ctx);
111 av_freep(&ctx->priv_data);
115 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
116 CodedBitstreamUnit *unit)
118 av_buffer_unref(&unit->content_ref);
119 unit->content = NULL;
121 av_buffer_unref(&unit->data_ref);
124 unit->data_bit_padding = 0;
127 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
128 CodedBitstreamFragment *frag)
132 for (i = 0; i < frag->nb_units; i++)
133 cbs_unit_uninit(ctx, &frag->units[i]);
134 av_freep(&frag->units);
137 av_buffer_unref(&frag->data_ref);
140 frag->data_bit_padding = 0;
143 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
144 CodedBitstreamFragment *frag)
148 for (i = 0; i < frag->nb_units; i++) {
149 CodedBitstreamUnit *unit = &frag->units[i];
151 if (ctx->decompose_unit_types) {
152 for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
153 if (ctx->decompose_unit_types[j] == unit->type)
156 if (j >= ctx->nb_decompose_unit_types)
160 av_buffer_unref(&unit->content_ref);
161 unit->content = NULL;
163 av_assert0(unit->data && unit->data_ref);
165 err = ctx->codec->read_unit(ctx, unit);
166 if (err == AVERROR(ENOSYS)) {
167 av_log(ctx->log_ctx, AV_LOG_VERBOSE,
168 "Decomposition unimplemented for unit %d "
169 "(type %"PRIu32").\n", i, unit->type);
170 } else if (err < 0) {
171 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
172 "(type %"PRIu32").\n", i, unit->type);
180 static int cbs_fill_fragment_data(CodedBitstreamContext *ctx,
181 CodedBitstreamFragment *frag,
182 const uint8_t *data, size_t size)
184 av_assert0(!frag->data && !frag->data_ref);
187 av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
189 return AVERROR(ENOMEM);
191 frag->data = frag->data_ref->data;
192 frag->data_size = size;
194 memcpy(frag->data, data, size);
195 memset(frag->data + size, 0,
196 AV_INPUT_BUFFER_PADDING_SIZE);
201 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
202 CodedBitstreamFragment *frag,
203 const AVCodecParameters *par)
207 memset(frag, 0, sizeof(*frag));
209 err = cbs_fill_fragment_data(ctx, frag, par->extradata,
210 par->extradata_size);
214 err = ctx->codec->split_fragment(ctx, frag, 1);
218 return cbs_read_fragment_content(ctx, frag);
221 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
222 CodedBitstreamFragment *frag,
227 memset(frag, 0, sizeof(*frag));
230 frag->data_ref = av_buffer_ref(pkt->buf);
232 return AVERROR(ENOMEM);
234 frag->data = pkt->data;
235 frag->data_size = pkt->size;
238 err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
243 err = ctx->codec->split_fragment(ctx, frag, 0);
247 return cbs_read_fragment_content(ctx, frag);
250 int ff_cbs_read(CodedBitstreamContext *ctx,
251 CodedBitstreamFragment *frag,
252 const uint8_t *data, size_t size)
256 memset(frag, 0, sizeof(*frag));
258 err = cbs_fill_fragment_data(ctx, frag, data, size);
262 err = ctx->codec->split_fragment(ctx, frag, 0);
266 return cbs_read_fragment_content(ctx, frag);
270 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
271 CodedBitstreamFragment *frag)
275 for (i = 0; i < frag->nb_units; i++) {
276 CodedBitstreamUnit *unit = &frag->units[i];
281 av_buffer_unref(&unit->data_ref);
284 err = ctx->codec->write_unit(ctx, unit);
286 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
287 "(type %"PRIu32").\n", i, unit->type);
290 av_assert0(unit->data && unit->data_ref);
293 av_buffer_unref(&frag->data_ref);
296 err = ctx->codec->assemble_fragment(ctx, frag);
298 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
301 av_assert0(frag->data && frag->data_ref);
306 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
307 AVCodecParameters *par,
308 CodedBitstreamFragment *frag)
312 err = ff_cbs_write_fragment_data(ctx, frag);
316 av_freep(&par->extradata);
318 par->extradata = av_malloc(frag->data_size +
319 AV_INPUT_BUFFER_PADDING_SIZE);
321 return AVERROR(ENOMEM);
323 memcpy(par->extradata, frag->data, frag->data_size);
324 memset(par->extradata + frag->data_size, 0,
325 AV_INPUT_BUFFER_PADDING_SIZE);
326 par->extradata_size = frag->data_size;
331 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
333 CodedBitstreamFragment *frag)
338 err = ff_cbs_write_fragment_data(ctx, frag);
342 buf = av_buffer_ref(frag->data_ref);
344 return AVERROR(ENOMEM);
348 pkt->data = frag->data;
349 pkt->size = frag->data_size;
355 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
358 if (!ctx->trace_enable)
361 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
364 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
365 const char *str, const int *subscripts,
366 const char *bits, int64_t value)
369 size_t name_len, bits_len;
370 int pad, subs, i, j, k, n;
372 if (!ctx->trace_enable)
375 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
377 subs = subscripts ? subscripts[0] : 0;
379 for (i = j = 0; str[i];) {
383 k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
384 av_assert0(k > 0 && j + k < sizeof(name));
386 for (++i; str[i] && str[i] != ']'; i++);
387 av_assert0(str[i] == ']');
389 while (str[i] && str[i] != ']')
390 name[j++] = str[i++];
391 av_assert0(str[i] == ']');
394 av_assert0(j + 1 < sizeof(name));
395 name[j++] = str[i++];
398 av_assert0(j + 1 < sizeof(name));
400 av_assert0(n == subs);
402 name_len = strlen(name);
403 bits_len = strlen(bits);
405 if (name_len + bits_len > 60)
410 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
411 position, name, pad, bits, value);
414 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
415 int width, const char *name,
416 const int *subscripts, uint32_t *write_to,
417 uint32_t range_min, uint32_t range_max)
422 av_assert0(width > 0 && width <= 32);
424 if (get_bits_left(gbc) < width) {
425 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
426 "%s: bitstream ended.\n", name);
427 return AVERROR_INVALIDDATA;
430 if (ctx->trace_enable)
431 position = get_bits_count(gbc);
433 value = get_bits_long(gbc, width);
435 if (ctx->trace_enable) {
438 for (i = 0; i < width; i++)
439 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
442 ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
446 if (value < range_min || value > range_max) {
447 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
448 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
449 name, value, range_min, range_max);
450 return AVERROR_INVALIDDATA;
457 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
458 int width, const char *name,
459 const int *subscripts, uint32_t value,
460 uint32_t range_min, uint32_t range_max)
462 av_assert0(width > 0 && width <= 32);
464 if (value < range_min || value > range_max) {
465 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
466 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
467 name, value, range_min, range_max);
468 return AVERROR_INVALIDDATA;
471 if (put_bits_left(pbc) < width)
472 return AVERROR(ENOSPC);
474 if (ctx->trace_enable) {
477 for (i = 0; i < width; i++)
478 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
481 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
482 name, subscripts, bits, value);
486 put_bits(pbc, width, value);
488 put_bits32(pbc, value);
494 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
495 CodedBitstreamUnit *unit,
497 void (*free)(void *opaque, uint8_t *data))
499 av_assert0(!unit->content && !unit->content_ref);
501 unit->content = av_mallocz(size);
503 return AVERROR(ENOMEM);
505 unit->content_ref = av_buffer_create(unit->content, size,
507 if (!unit->content_ref) {
508 av_freep(&unit->content);
509 return AVERROR(ENOMEM);
515 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
516 CodedBitstreamUnit *unit,
519 av_assert0(!unit->data && !unit->data_ref);
521 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
523 return AVERROR(ENOMEM);
525 unit->data = unit->data_ref->data;
526 unit->data_size = size;
528 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
533 static int cbs_insert_unit(CodedBitstreamContext *ctx,
534 CodedBitstreamFragment *frag,
537 CodedBitstreamUnit *units;
539 units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
541 return AVERROR(ENOMEM);
544 memcpy(units, frag->units, position * sizeof(*units));
545 if (position < frag->nb_units)
546 memcpy(units + position + 1, frag->units + position,
547 (frag->nb_units - position) * sizeof(*units));
549 memset(units + position, 0, sizeof(*units));
551 av_freep(&frag->units);
558 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
559 CodedBitstreamFragment *frag,
561 CodedBitstreamUnitType type,
563 AVBufferRef *content_buf)
565 CodedBitstreamUnit *unit;
566 AVBufferRef *content_ref;
570 position = frag->nb_units;
571 av_assert0(position >= 0 && position <= frag->nb_units);
574 content_ref = av_buffer_ref(content_buf);
576 return AVERROR(ENOMEM);
581 err = cbs_insert_unit(ctx, frag, position);
583 av_buffer_unref(&content_ref);
587 unit = &frag->units[position];
589 unit->content = content;
590 unit->content_ref = content_ref;
595 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
596 CodedBitstreamFragment *frag,
598 CodedBitstreamUnitType type,
599 uint8_t *data, size_t data_size,
600 AVBufferRef *data_buf)
602 CodedBitstreamUnit *unit;
603 AVBufferRef *data_ref;
607 position = frag->nb_units;
608 av_assert0(position >= 0 && position <= frag->nb_units);
611 data_ref = av_buffer_ref(data_buf);
613 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
615 return AVERROR(ENOMEM);
617 err = cbs_insert_unit(ctx, frag, position);
619 av_buffer_unref(&data_ref);
623 unit = &frag->units[position];
626 unit->data_size = data_size;
627 unit->data_ref = data_ref;
632 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
633 CodedBitstreamFragment *frag,
636 if (position < 0 || position >= frag->nb_units)
637 return AVERROR(EINVAL);
639 cbs_unit_uninit(ctx, &frag->units[position]);
643 if (frag->nb_units == 0) {
644 av_freep(&frag->units);
647 memmove(frag->units + position,
648 frag->units + position + 1,
649 (frag->nb_units - position) * sizeof(*frag->units));
651 // Don't bother reallocating the unit array.