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/common.h"
27 #include "cbs_internal.h"
30 static const CodedBitstreamType *cbs_type_table[] = {
42 int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
43 enum AVCodecID codec_id, void *log_ctx)
45 CodedBitstreamContext *ctx;
46 const CodedBitstreamType *type;
50 for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
51 if (cbs_type_table[i]->codec_id == codec_id) {
52 type = cbs_type_table[i];
57 return AVERROR(EINVAL);
59 ctx = av_mallocz(sizeof(*ctx));
61 return AVERROR(ENOMEM);
63 ctx->log_ctx = log_ctx;
66 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
67 if (!ctx->priv_data) {
69 return AVERROR(ENOMEM);
72 ctx->decompose_unit_types = NULL;
74 ctx->trace_enable = 0;
75 ctx->trace_level = AV_LOG_TRACE;
81 void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
83 CodedBitstreamContext *ctx = *ctx_ptr;
88 if (ctx->codec && ctx->codec->close)
89 ctx->codec->close(ctx);
91 av_freep(&ctx->priv_data);
95 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
96 CodedBitstreamUnit *unit)
98 if (ctx->codec->free_unit && unit->content && !unit->content_external)
99 ctx->codec->free_unit(unit);
101 av_freep(&unit->data);
103 unit->data_bit_padding = 0;
106 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
107 CodedBitstreamFragment *frag)
111 for (i = 0; i < frag->nb_units; i++)
112 cbs_unit_uninit(ctx, &frag->units[i]);
113 av_freep(&frag->units);
116 av_freep(&frag->data);
118 frag->data_bit_padding = 0;
121 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
122 CodedBitstreamFragment *frag)
126 for (i = 0; i < frag->nb_units; i++) {
127 if (ctx->decompose_unit_types) {
128 for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
129 if (ctx->decompose_unit_types[j] == frag->units[i].type)
132 if (j >= ctx->nb_decompose_unit_types)
136 err = ctx->codec->read_unit(ctx, &frag->units[i]);
137 if (err == AVERROR(ENOSYS)) {
138 av_log(ctx->log_ctx, AV_LOG_WARNING,
139 "Decomposition unimplemented for unit %d "
140 "(type %"PRIu32").\n", i, frag->units[i].type);
141 } else if (err < 0) {
142 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
143 "(type %"PRIu32").\n", i, frag->units[i].type);
151 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
152 CodedBitstreamFragment *frag,
153 const AVCodecParameters *par)
157 memset(frag, 0, sizeof(*frag));
159 frag->data = par->extradata;
160 frag->data_size = par->extradata_size;
162 err = ctx->codec->split_fragment(ctx, frag, 1);
169 return cbs_read_fragment_content(ctx, frag);
172 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
173 CodedBitstreamFragment *frag,
178 memset(frag, 0, sizeof(*frag));
180 frag->data = pkt->data;
181 frag->data_size = pkt->size;
183 err = ctx->codec->split_fragment(ctx, frag, 0);
190 return cbs_read_fragment_content(ctx, frag);
193 int ff_cbs_read(CodedBitstreamContext *ctx,
194 CodedBitstreamFragment *frag,
195 const uint8_t *data, size_t size)
199 memset(frag, 0, sizeof(*frag));
201 // (We won't write to this during split.)
202 frag->data = (uint8_t*)data;
203 frag->data_size = size;
205 err = ctx->codec->split_fragment(ctx, frag, 0);
212 return cbs_read_fragment_content(ctx, frag);
216 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
217 CodedBitstreamFragment *frag)
221 for (i = 0; i < frag->nb_units; i++) {
222 if (!frag->units[i].content)
225 err = ctx->codec->write_unit(ctx, &frag->units[i]);
227 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
228 "(type %"PRIu32").\n", i, frag->units[i].type);
233 err = ctx->codec->assemble_fragment(ctx, frag);
235 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
242 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
243 AVCodecParameters *par,
244 CodedBitstreamFragment *frag)
248 err = ff_cbs_write_fragment_data(ctx, frag);
252 av_freep(&par->extradata);
254 par->extradata = av_malloc(frag->data_size +
255 AV_INPUT_BUFFER_PADDING_SIZE);
257 return AVERROR(ENOMEM);
259 memcpy(par->extradata, frag->data, frag->data_size);
260 memset(par->extradata + frag->data_size, 0,
261 AV_INPUT_BUFFER_PADDING_SIZE);
262 par->extradata_size = frag->data_size;
267 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
269 CodedBitstreamFragment *frag)
273 err = ff_cbs_write_fragment_data(ctx, frag);
277 err = av_new_packet(pkt, frag->data_size);
281 memcpy(pkt->data, frag->data, frag->data_size);
282 pkt->size = frag->data_size;
288 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
291 if (!ctx->trace_enable)
294 av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
297 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
298 const char *name, const char *bits,
301 size_t name_len, bits_len;
304 if (!ctx->trace_enable)
307 av_assert0(value >= INT_MIN && value <= UINT32_MAX);
309 name_len = strlen(name);
310 bits_len = strlen(bits);
312 if (name_len + bits_len > 60)
317 av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
318 position, name, pad, bits, value);
321 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
322 int width, const char *name, uint32_t *write_to,
323 uint32_t range_min, uint32_t range_max)
328 av_assert0(width > 0 && width <= 32);
330 if (get_bits_left(gbc) < width) {
331 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
332 "%s: bitstream ended.\n", name);
333 return AVERROR_INVALIDDATA;
336 if (ctx->trace_enable)
337 position = get_bits_count(gbc);
339 value = get_bits_long(gbc, width);
341 if (ctx->trace_enable) {
344 for (i = 0; i < width; i++)
345 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
348 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
351 if (value < range_min || value > range_max) {
352 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
353 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
354 name, value, range_min, range_max);
355 return AVERROR_INVALIDDATA;
362 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
363 int width, const char *name, uint32_t value,
364 uint32_t range_min, uint32_t range_max)
366 av_assert0(width > 0 && width <= 32);
368 if (value < range_min || value > range_max) {
369 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
370 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
371 name, value, range_min, range_max);
372 return AVERROR_INVALIDDATA;
375 if (put_bits_left(pbc) < width)
376 return AVERROR(ENOSPC);
378 if (ctx->trace_enable) {
381 for (i = 0; i < width; i++)
382 bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
385 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
389 put_bits(pbc, width, value);
391 put_bits32(pbc, value);
397 static int cbs_insert_unit(CodedBitstreamContext *ctx,
398 CodedBitstreamFragment *frag,
401 CodedBitstreamUnit *units;
403 units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
405 return AVERROR(ENOMEM);
408 memcpy(units, frag->units, position * sizeof(*units));
409 if (position < frag->nb_units)
410 memcpy(units + position + 1, frag->units + position,
411 (frag->nb_units - position) * sizeof(*units));
413 memset(units + position, 0, sizeof(*units));
415 av_freep(&frag->units);
422 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
423 CodedBitstreamFragment *frag,
425 CodedBitstreamUnitType type,
431 position = frag->nb_units;
432 av_assert0(position >= 0 && position <= frag->nb_units);
434 err = cbs_insert_unit(ctx, frag, position);
438 frag->units[position].type = type;
439 frag->units[position].content = content;
440 frag->units[position].content_external = 1;
445 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
446 CodedBitstreamFragment *frag,
448 CodedBitstreamUnitType type,
449 uint8_t *data, size_t data_size)
454 position = frag->nb_units;
455 av_assert0(position >= 0 && position <= frag->nb_units);
457 err = cbs_insert_unit(ctx, frag, position);
461 frag->units[position].type = type;
462 frag->units[position].data = data;
463 frag->units[position].data_size = data_size;
468 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
469 CodedBitstreamFragment *frag,
472 if (position < 0 || position >= frag->nb_units)
473 return AVERROR(EINVAL);
475 cbs_unit_uninit(ctx, &frag->units[position]);
479 if (frag->nb_units == 0) {
480 av_freep(&frag->units);
483 memmove(frag->units + position,
484 frag->units + position + 1,
485 (frag->nb_units - position) * sizeof(*frag->units));
487 // Don't bother reallocating the unit array.