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[] = {
43 const enum AVCodecID ff_cbs_all_codec_ids[] = {
51 AV_CODEC_ID_MPEG2VIDEO,
56 int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
57 enum AVCodecID codec_id, void *log_ctx)
59 CodedBitstreamContext *ctx;
60 const CodedBitstreamType *type;
64 for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
65 if (cbs_type_table[i]->codec_id == codec_id) {
66 type = cbs_type_table[i];
71 return AVERROR(EINVAL);
73 ctx = av_mallocz(sizeof(*ctx));
75 return AVERROR(ENOMEM);
77 ctx->log_ctx = log_ctx;
80 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
81 if (!ctx->priv_data) {
83 return AVERROR(ENOMEM);
86 ctx->decompose_unit_types = NULL;
88 ctx->trace_enable = 0;
89 ctx->trace_level = AV_LOG_TRACE;
95 void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
97 CodedBitstreamContext *ctx = *ctx_ptr;
102 if (ctx->codec && ctx->codec->close)
103 ctx->codec->close(ctx);
105 av_freep(&ctx->priv_data);
109 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
110 CodedBitstreamUnit *unit)
112 av_buffer_unref(&unit->content_ref);
113 unit->content = NULL;
115 av_buffer_unref(&unit->data_ref);
118 unit->data_bit_padding = 0;
121 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
122 CodedBitstreamFragment *frag)
126 for (i = 0; i < frag->nb_units; i++)
127 cbs_unit_uninit(ctx, &frag->units[i]);
128 av_freep(&frag->units);
131 av_buffer_unref(&frag->data_ref);
134 frag->data_bit_padding = 0;
137 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
138 CodedBitstreamFragment *frag)
142 for (i = 0; i < frag->nb_units; i++) {
143 if (ctx->decompose_unit_types) {
144 for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
145 if (ctx->decompose_unit_types[j] == frag->units[i].type)
148 if (j >= ctx->nb_decompose_unit_types)
152 av_buffer_unref(&frag->units[i].content_ref);
153 frag->units[i].content = NULL;
155 err = ctx->codec->read_unit(ctx, &frag->units[i]);
156 if (err == AVERROR(ENOSYS)) {
157 av_log(ctx->log_ctx, AV_LOG_VERBOSE,
158 "Decomposition unimplemented for unit %d "
159 "(type %"PRIu32").\n", i, frag->units[i].type);
160 } else if (err < 0) {
161 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
162 "(type %"PRIu32").\n", i, frag->units[i].type);
170 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
171 CodedBitstreamFragment *frag,
172 const AVCodecParameters *par)
176 memset(frag, 0, sizeof(*frag));
178 frag->data = par->extradata;
179 frag->data_size = par->extradata_size;
181 err = ctx->codec->split_fragment(ctx, frag, 1);
188 return cbs_read_fragment_content(ctx, frag);
191 static int cbs_fill_fragment_data(CodedBitstreamContext *ctx,
192 CodedBitstreamFragment *frag,
193 const uint8_t *data, size_t size)
195 av_assert0(!frag->data && !frag->data_ref);
198 av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
200 return AVERROR(ENOMEM);
202 frag->data = frag->data_ref->data;
203 frag->data_size = size;
205 memcpy(frag->data, data, size);
206 memset(frag->data + size, 0,
207 AV_INPUT_BUFFER_PADDING_SIZE);
212 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
213 CodedBitstreamFragment *frag,
218 memset(frag, 0, sizeof(*frag));
221 frag->data_ref = av_buffer_ref(pkt->buf);
223 return AVERROR(ENOMEM);
225 frag->data = pkt->data;
226 frag->data_size = pkt->size;
229 err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
234 err = ctx->codec->split_fragment(ctx, frag, 0);
238 return cbs_read_fragment_content(ctx, frag);
241 int ff_cbs_read(CodedBitstreamContext *ctx,
242 CodedBitstreamFragment *frag,
243 const uint8_t *data, size_t size)
247 memset(frag, 0, sizeof(*frag));
249 err = cbs_fill_fragment_data(ctx, frag, data, size);
253 err = ctx->codec->split_fragment(ctx, frag, 0);
257 return cbs_read_fragment_content(ctx, frag);
261 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
262 CodedBitstreamFragment *frag)
266 for (i = 0; i < frag->nb_units; i++) {
267 CodedBitstreamUnit *unit = &frag->units[i];
272 av_buffer_unref(&unit->data_ref);
275 err = ctx->codec->write_unit(ctx, unit);
277 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
278 "(type %"PRIu32").\n", i, unit->type);
283 av_buffer_unref(&frag->data_ref);
286 err = ctx->codec->assemble_fragment(ctx, frag);
288 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
295 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
296 AVCodecParameters *par,
297 CodedBitstreamFragment *frag)
301 err = ff_cbs_write_fragment_data(ctx, frag);
305 av_freep(&par->extradata);
307 par->extradata = av_malloc(frag->data_size +
308 AV_INPUT_BUFFER_PADDING_SIZE);
310 return AVERROR(ENOMEM);
312 memcpy(par->extradata, frag->data, frag->data_size);
313 memset(par->extradata + frag->data_size, 0,
314 AV_INPUT_BUFFER_PADDING_SIZE);
315 par->extradata_size = frag->data_size;
320 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
322 CodedBitstreamFragment *frag)
327 err = ff_cbs_write_fragment_data(ctx, frag);
331 av_assert0(frag->data_ref);
332 buf = av_buffer_ref(frag->data_ref);
334 return AVERROR(ENOMEM);
338 pkt->data = frag->data;
339 pkt->size = frag->data_size;
345 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
348 if (!ctx->trace_enable)
351 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
354 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
355 const char *name, const char *bits,
358 size_t name_len, bits_len;
361 if (!ctx->trace_enable)
364 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
366 name_len = strlen(name);
367 bits_len = strlen(bits);
369 if (name_len + bits_len > 60)
374 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
375 position, name, pad, bits, value);
378 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
379 int width, const char *name, uint32_t *write_to,
380 uint32_t range_min, uint32_t range_max)
385 av_assert0(width > 0 && width <= 32);
387 if (get_bits_left(gbc) < width) {
388 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
389 "%s: bitstream ended.\n", name);
390 return AVERROR_INVALIDDATA;
393 if (ctx->trace_enable)
394 position = get_bits_count(gbc);
396 value = get_bits_long(gbc, width);
398 if (ctx->trace_enable) {
401 for (i = 0; i < width; i++)
402 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
405 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
408 if (value < range_min || value > range_max) {
409 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
410 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
411 name, value, range_min, range_max);
412 return AVERROR_INVALIDDATA;
419 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
420 int width, const char *name, uint32_t value,
421 uint32_t range_min, uint32_t range_max)
423 av_assert0(width > 0 && width <= 32);
425 if (value < range_min || value > range_max) {
426 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
427 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
428 name, value, range_min, range_max);
429 return AVERROR_INVALIDDATA;
432 if (put_bits_left(pbc) < width)
433 return AVERROR(ENOSPC);
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, put_bits_count(pbc), name, bits, value);
446 put_bits(pbc, width, value);
448 put_bits32(pbc, value);
454 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
455 CodedBitstreamUnit *unit,
457 void (*free)(void *opaque, uint8_t *data))
459 av_assert0(!unit->content && !unit->content_ref);
461 unit->content = av_mallocz(size);
463 return AVERROR(ENOMEM);
465 unit->content_ref = av_buffer_create(unit->content, size,
467 if (!unit->content_ref) {
468 av_freep(&unit->content);
469 return AVERROR(ENOMEM);
475 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
476 CodedBitstreamUnit *unit,
479 av_assert0(!unit->data && !unit->data_ref);
481 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
483 return AVERROR(ENOMEM);
485 unit->data = unit->data_ref->data;
486 unit->data_size = size;
488 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
493 static int cbs_insert_unit(CodedBitstreamContext *ctx,
494 CodedBitstreamFragment *frag,
497 CodedBitstreamUnit *units;
499 units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
501 return AVERROR(ENOMEM);
504 memcpy(units, frag->units, position * sizeof(*units));
505 if (position < frag->nb_units)
506 memcpy(units + position + 1, frag->units + position,
507 (frag->nb_units - position) * sizeof(*units));
509 memset(units + position, 0, sizeof(*units));
511 av_freep(&frag->units);
518 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
519 CodedBitstreamFragment *frag,
521 CodedBitstreamUnitType type,
523 AVBufferRef *content_buf)
525 CodedBitstreamUnit *unit;
526 AVBufferRef *content_ref;
530 position = frag->nb_units;
531 av_assert0(position >= 0 && position <= frag->nb_units);
534 content_ref = av_buffer_ref(content_buf);
536 return AVERROR(ENOMEM);
541 err = cbs_insert_unit(ctx, frag, position);
543 av_buffer_unref(&content_ref);
547 unit = &frag->units[position];
549 unit->content = content;
550 unit->content_ref = content_ref;
555 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
556 CodedBitstreamFragment *frag,
558 CodedBitstreamUnitType type,
559 uint8_t *data, size_t data_size,
560 AVBufferRef *data_buf)
562 CodedBitstreamUnit *unit;
563 AVBufferRef *data_ref;
567 position = frag->nb_units;
568 av_assert0(position >= 0 && position <= frag->nb_units);
571 data_ref = av_buffer_ref(data_buf);
573 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
575 return AVERROR(ENOMEM);
577 err = cbs_insert_unit(ctx, frag, position);
579 av_buffer_unref(&data_ref);
583 unit = &frag->units[position];
586 unit->data_size = data_size;
587 unit->data_ref = data_ref;
592 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
593 CodedBitstreamFragment *frag,
596 if (position < 0 || position >= frag->nb_units)
597 return AVERROR(EINVAL);
599 cbs_unit_uninit(ctx, &frag->units[position]);
603 if (frag->nb_units == 0) {
604 av_freep(&frag->units);
607 memmove(frag->units + position,
608 frag->units + position + 1,
609 (frag->nb_units - position) * sizeof(*frag->units));
611 // Don't bother reallocating the unit array.