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 CodedBitstreamUnit *unit = &frag->units[i];
145 if (ctx->decompose_unit_types) {
146 for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
147 if (ctx->decompose_unit_types[j] == unit->type)
150 if (j >= ctx->nb_decompose_unit_types)
154 av_buffer_unref(&unit->content_ref);
155 unit->content = NULL;
157 av_assert0(unit->data && unit->data_ref);
159 err = ctx->codec->read_unit(ctx, unit);
160 if (err == AVERROR(ENOSYS)) {
161 av_log(ctx->log_ctx, AV_LOG_VERBOSE,
162 "Decomposition unimplemented for unit %d "
163 "(type %"PRIu32").\n", i, unit->type);
164 } else if (err < 0) {
165 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
166 "(type %"PRIu32").\n", i, unit->type);
174 static int cbs_fill_fragment_data(CodedBitstreamContext *ctx,
175 CodedBitstreamFragment *frag,
176 const uint8_t *data, size_t size)
178 av_assert0(!frag->data && !frag->data_ref);
181 av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
183 return AVERROR(ENOMEM);
185 frag->data = frag->data_ref->data;
186 frag->data_size = size;
188 memcpy(frag->data, data, size);
189 memset(frag->data + size, 0,
190 AV_INPUT_BUFFER_PADDING_SIZE);
195 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
196 CodedBitstreamFragment *frag,
197 const AVCodecParameters *par)
201 memset(frag, 0, sizeof(*frag));
203 err = cbs_fill_fragment_data(ctx, frag, par->extradata,
204 par->extradata_size);
208 err = ctx->codec->split_fragment(ctx, frag, 1);
212 return cbs_read_fragment_content(ctx, frag);
215 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
216 CodedBitstreamFragment *frag,
221 memset(frag, 0, sizeof(*frag));
224 frag->data_ref = av_buffer_ref(pkt->buf);
226 return AVERROR(ENOMEM);
228 frag->data = pkt->data;
229 frag->data_size = pkt->size;
232 err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
237 err = ctx->codec->split_fragment(ctx, frag, 0);
241 return cbs_read_fragment_content(ctx, frag);
244 int ff_cbs_read(CodedBitstreamContext *ctx,
245 CodedBitstreamFragment *frag,
246 const uint8_t *data, size_t size)
250 memset(frag, 0, sizeof(*frag));
252 err = cbs_fill_fragment_data(ctx, frag, data, size);
256 err = ctx->codec->split_fragment(ctx, frag, 0);
260 return cbs_read_fragment_content(ctx, frag);
264 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
265 CodedBitstreamFragment *frag)
269 for (i = 0; i < frag->nb_units; i++) {
270 CodedBitstreamUnit *unit = &frag->units[i];
275 av_buffer_unref(&unit->data_ref);
278 err = ctx->codec->write_unit(ctx, unit);
280 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
281 "(type %"PRIu32").\n", i, unit->type);
284 av_assert0(unit->data && unit->data_ref);
287 av_buffer_unref(&frag->data_ref);
290 err = ctx->codec->assemble_fragment(ctx, frag);
292 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
295 av_assert0(frag->data && frag->data_ref);
300 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
301 AVCodecParameters *par,
302 CodedBitstreamFragment *frag)
306 err = ff_cbs_write_fragment_data(ctx, frag);
310 av_freep(&par->extradata);
312 par->extradata = av_malloc(frag->data_size +
313 AV_INPUT_BUFFER_PADDING_SIZE);
315 return AVERROR(ENOMEM);
317 memcpy(par->extradata, frag->data, frag->data_size);
318 memset(par->extradata + frag->data_size, 0,
319 AV_INPUT_BUFFER_PADDING_SIZE);
320 par->extradata_size = frag->data_size;
325 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
327 CodedBitstreamFragment *frag)
332 err = ff_cbs_write_fragment_data(ctx, frag);
336 buf = av_buffer_ref(frag->data_ref);
338 return AVERROR(ENOMEM);
342 pkt->data = frag->data;
343 pkt->size = frag->data_size;
349 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
352 if (!ctx->trace_enable)
355 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
358 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
359 const char *name, const char *bits,
362 size_t name_len, bits_len;
365 if (!ctx->trace_enable)
368 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
370 name_len = strlen(name);
371 bits_len = strlen(bits);
373 if (name_len + bits_len > 60)
378 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
379 position, name, pad, bits, value);
382 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
383 int width, const char *name, uint32_t *write_to,
384 uint32_t range_min, uint32_t range_max)
389 av_assert0(width > 0 && width <= 32);
391 if (get_bits_left(gbc) < width) {
392 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
393 "%s: bitstream ended.\n", name);
394 return AVERROR_INVALIDDATA;
397 if (ctx->trace_enable)
398 position = get_bits_count(gbc);
400 value = get_bits_long(gbc, width);
402 if (ctx->trace_enable) {
405 for (i = 0; i < width; i++)
406 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
409 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
412 if (value < range_min || value > range_max) {
413 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
414 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
415 name, value, range_min, range_max);
416 return AVERROR_INVALIDDATA;
423 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
424 int width, const char *name, uint32_t value,
425 uint32_t range_min, uint32_t range_max)
427 av_assert0(width > 0 && width <= 32);
429 if (value < range_min || value > range_max) {
430 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
431 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
432 name, value, range_min, range_max);
433 return AVERROR_INVALIDDATA;
436 if (put_bits_left(pbc) < width)
437 return AVERROR(ENOSPC);
439 if (ctx->trace_enable) {
442 for (i = 0; i < width; i++)
443 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
446 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
450 put_bits(pbc, width, value);
452 put_bits32(pbc, value);
458 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
459 CodedBitstreamUnit *unit,
461 void (*free)(void *opaque, uint8_t *data))
463 av_assert0(!unit->content && !unit->content_ref);
465 unit->content = av_mallocz(size);
467 return AVERROR(ENOMEM);
469 unit->content_ref = av_buffer_create(unit->content, size,
471 if (!unit->content_ref) {
472 av_freep(&unit->content);
473 return AVERROR(ENOMEM);
479 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
480 CodedBitstreamUnit *unit,
483 av_assert0(!unit->data && !unit->data_ref);
485 unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
487 return AVERROR(ENOMEM);
489 unit->data = unit->data_ref->data;
490 unit->data_size = size;
492 memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
497 static int cbs_insert_unit(CodedBitstreamContext *ctx,
498 CodedBitstreamFragment *frag,
501 CodedBitstreamUnit *units;
503 units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
505 return AVERROR(ENOMEM);
508 memcpy(units, frag->units, position * sizeof(*units));
509 if (position < frag->nb_units)
510 memcpy(units + position + 1, frag->units + position,
511 (frag->nb_units - position) * sizeof(*units));
513 memset(units + position, 0, sizeof(*units));
515 av_freep(&frag->units);
522 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
523 CodedBitstreamFragment *frag,
525 CodedBitstreamUnitType type,
527 AVBufferRef *content_buf)
529 CodedBitstreamUnit *unit;
530 AVBufferRef *content_ref;
534 position = frag->nb_units;
535 av_assert0(position >= 0 && position <= frag->nb_units);
538 content_ref = av_buffer_ref(content_buf);
540 return AVERROR(ENOMEM);
545 err = cbs_insert_unit(ctx, frag, position);
547 av_buffer_unref(&content_ref);
551 unit = &frag->units[position];
553 unit->content = content;
554 unit->content_ref = content_ref;
559 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
560 CodedBitstreamFragment *frag,
562 CodedBitstreamUnitType type,
563 uint8_t *data, size_t data_size,
564 AVBufferRef *data_buf)
566 CodedBitstreamUnit *unit;
567 AVBufferRef *data_ref;
571 position = frag->nb_units;
572 av_assert0(position >= 0 && position <= frag->nb_units);
575 data_ref = av_buffer_ref(data_buf);
577 data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
579 return AVERROR(ENOMEM);
581 err = cbs_insert_unit(ctx, frag, position);
583 av_buffer_unref(&data_ref);
587 unit = &frag->units[position];
590 unit->data_size = data_size;
591 unit->data_ref = data_ref;
596 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
597 CodedBitstreamFragment *frag,
600 if (position < 0 || position >= frag->nb_units)
601 return AVERROR(EINVAL);
603 cbs_unit_uninit(ctx, &frag->units[position]);
607 if (frag->nb_units == 0) {
608 av_freep(&frag->units);
611 memmove(frag->units + position,
612 frag->units + position + 1,
613 (frag->nb_units - position) * sizeof(*frag->units));
615 // Don't bother reallocating the unit array.