FFmpeg  4.2.1
cbs.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include <string.h>
20 
21 #include "config.h"
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/buffer.h"
25 #include "libavutil/common.h"
26 
27 #include "cbs.h"
28 #include "cbs_internal.h"
29 
30 
32 #if CONFIG_CBS_AV1
34 #endif
35 #if CONFIG_CBS_H264
37 #endif
38 #if CONFIG_CBS_H265
40 #endif
41 #if CONFIG_CBS_JPEG
43 #endif
44 #if CONFIG_CBS_MPEG2
46 #endif
47 #if CONFIG_CBS_VP9
49 #endif
50 };
51 
53 #if CONFIG_CBS_AV1
55 #endif
56 #if CONFIG_CBS_H264
58 #endif
59 #if CONFIG_CBS_H265
61 #endif
62 #if CONFIG_CBS_JPEG
64 #endif
65 #if CONFIG_CBS_MPEG2
67 #endif
68 #if CONFIG_CBS_VP9
70 #endif
72 };
73 
75  enum AVCodecID codec_id, void *log_ctx)
76 {
78  const CodedBitstreamType *type;
79  int i;
80 
81  type = NULL;
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];
85  break;
86  }
87  }
88  if (!type)
89  return AVERROR(EINVAL);
90 
91  ctx = av_mallocz(sizeof(*ctx));
92  if (!ctx)
93  return AVERROR(ENOMEM);
94 
95  ctx->log_ctx = log_ctx;
96  ctx->codec = type;
97 
99  if (!ctx->priv_data) {
100  av_freep(&ctx);
101  return AVERROR(ENOMEM);
102  }
103 
104  ctx->decompose_unit_types = NULL;
105 
106  ctx->trace_enable = 0;
107  ctx->trace_level = AV_LOG_TRACE;
108 
109  *ctx_ptr = ctx;
110  return 0;
111 }
112 
114 {
115  CodedBitstreamContext *ctx = *ctx_ptr;
116 
117  if (!ctx)
118  return;
119 
120  if (ctx->codec && ctx->codec->close)
121  ctx->codec->close(ctx);
122 
123  av_freep(&ctx->priv_data);
124  av_freep(ctx_ptr);
125 }
126 
128  CodedBitstreamUnit *unit)
129 {
131  unit->content = NULL;
132 
133  av_buffer_unref(&unit->data_ref);
134  unit->data = NULL;
135  unit->data_size = 0;
136  unit->data_bit_padding = 0;
137 }
138 
141 {
142  int i;
143 
144  for (i = 0; i < frag->nb_units; i++)
145  cbs_unit_uninit(ctx, &frag->units[i]);
146  frag->nb_units = 0;
147 
148  av_buffer_unref(&frag->data_ref);
149  frag->data = NULL;
150  frag->data_size = 0;
151  frag->data_bit_padding = 0;
152 }
153 
156 {
157  ff_cbs_fragment_reset(ctx, frag);
158 
159  av_freep(&frag->units);
160  frag->nb_units_allocated = 0;
161 }
162 
165 {
166  int err, i, j;
167 
168  for (i = 0; i < frag->nb_units; i++) {
169  CodedBitstreamUnit *unit = &frag->units[i];
170 
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)
174  break;
175  }
176  if (j >= ctx->nb_decompose_unit_types)
177  continue;
178  }
179 
181  unit->content = NULL;
182 
183  av_assert0(unit->data && unit->data_ref);
184 
185  err = ctx->codec->read_unit(ctx, unit);
186  if (err == AVERROR(ENOSYS)) {
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);
193  return err;
194  }
195  }
196 
197  return 0;
198 }
199 
202  const uint8_t *data, size_t size)
203 {
204  av_assert0(!frag->data && !frag->data_ref);
205 
206  frag->data_ref =
208  if (!frag->data_ref)
209  return AVERROR(ENOMEM);
210 
211  frag->data = frag->data_ref->data;
212  frag->data_size = size;
213 
214  memcpy(frag->data, data, size);
215  memset(frag->data + size, 0,
217 
218  return 0;
219 }
220 
223  const AVCodecParameters *par)
224 {
225  int err;
226 
227  err = cbs_fill_fragment_data(ctx, frag, par->extradata,
228  par->extradata_size);
229  if (err < 0)
230  return err;
231 
232  err = ctx->codec->split_fragment(ctx, frag, 1);
233  if (err < 0)
234  return err;
235 
236  return cbs_read_fragment_content(ctx, frag);
237 }
238 
241  const AVPacket *pkt)
242 {
243  int err;
244 
245  if (pkt->buf) {
246  frag->data_ref = av_buffer_ref(pkt->buf);
247  if (!frag->data_ref)
248  return AVERROR(ENOMEM);
249 
250  frag->data = pkt->data;
251  frag->data_size = pkt->size;
252 
253  } else {
254  err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
255  if (err < 0)
256  return err;
257  }
258 
259  err = ctx->codec->split_fragment(ctx, frag, 0);
260  if (err < 0)
261  return err;
262 
263  return cbs_read_fragment_content(ctx, frag);
264 }
265 
268  const uint8_t *data, size_t size)
269 {
270  int err;
271 
272  err = cbs_fill_fragment_data(ctx, frag, data, size);
273  if (err < 0)
274  return err;
275 
276  err = ctx->codec->split_fragment(ctx, frag, 0);
277  if (err < 0)
278  return err;
279 
280  return cbs_read_fragment_content(ctx, frag);
281 }
282 
283 
286 {
287  int err, i;
288 
289  for (i = 0; i < frag->nb_units; i++) {
290  CodedBitstreamUnit *unit = &frag->units[i];
291 
292  if (!unit->content)
293  continue;
294 
295  av_buffer_unref(&unit->data_ref);
296  unit->data = NULL;
297 
298  err = ctx->codec->write_unit(ctx, unit);
299  if (err < 0) {
300  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
301  "(type %"PRIu32").\n", i, unit->type);
302  return err;
303  }
304  av_assert0(unit->data && unit->data_ref);
305  }
306 
307  av_buffer_unref(&frag->data_ref);
308  frag->data = NULL;
309 
310  err = ctx->codec->assemble_fragment(ctx, frag);
311  if (err < 0) {
312  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
313  return err;
314  }
315  av_assert0(frag->data && frag->data_ref);
316 
317  return 0;
318 }
319 
321  AVCodecParameters *par,
323 {
324  int err;
325 
326  err = ff_cbs_write_fragment_data(ctx, frag);
327  if (err < 0)
328  return err;
329 
330  av_freep(&par->extradata);
331 
332  par->extradata = av_malloc(frag->data_size +
334  if (!par->extradata)
335  return AVERROR(ENOMEM);
336 
337  memcpy(par->extradata, frag->data, frag->data_size);
338  memset(par->extradata + frag->data_size, 0,
340  par->extradata_size = frag->data_size;
341 
342  return 0;
343 }
344 
346  AVPacket *pkt,
348 {
349  AVBufferRef *buf;
350  int err;
351 
352  err = ff_cbs_write_fragment_data(ctx, frag);
353  if (err < 0)
354  return err;
355 
356  buf = av_buffer_ref(frag->data_ref);
357  if (!buf)
358  return AVERROR(ENOMEM);
359 
360  av_buffer_unref(&pkt->buf);
361 
362  pkt->buf = buf;
363  pkt->data = frag->data;
364  pkt->size = frag->data_size;
365 
366  return 0;
367 }
368 
369 
371  const char *name)
372 {
373  if (!ctx->trace_enable)
374  return;
375 
376  av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
377 }
378 
380  const char *str, const int *subscripts,
381  const char *bits, int64_t value)
382 {
383  char name[256];
384  size_t name_len, bits_len;
385  int pad, subs, i, j, k, n;
386 
387  if (!ctx->trace_enable)
388  return;
389 
390  av_assert0(value >= INT_MIN && value <= UINT32_MAX);
391 
392  subs = subscripts ? subscripts[0] : 0;
393  n = 0;
394  for (i = j = 0; str[i];) {
395  if (str[i] == '[') {
396  if (n < subs) {
397  ++n;
398  k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
399  av_assert0(k > 0 && j + k < sizeof(name));
400  j += k;
401  for (++i; str[i] && str[i] != ']'; i++);
402  av_assert0(str[i] == ']');
403  } else {
404  while (str[i] && str[i] != ']')
405  name[j++] = str[i++];
406  av_assert0(str[i] == ']');
407  }
408  } else {
409  av_assert0(j + 1 < sizeof(name));
410  name[j++] = str[i++];
411  }
412  }
413  av_assert0(j + 1 < sizeof(name));
414  name[j] = 0;
415  av_assert0(n == subs);
416 
417  name_len = strlen(name);
418  bits_len = strlen(bits);
419 
420  if (name_len + bits_len > 60)
421  pad = bits_len + 2;
422  else
423  pad = 61 - name_len;
424 
425  av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
426  position, name, pad, bits, value);
427 }
428 
430  int width, const char *name,
431  const int *subscripts, uint32_t *write_to,
432  uint32_t range_min, uint32_t range_max)
433 {
434  uint32_t value;
435  int position;
436 
437  av_assert0(width > 0 && width <= 32);
438 
439  if (get_bits_left(gbc) < width) {
440  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
441  "%s: bitstream ended.\n", name);
442  return AVERROR_INVALIDDATA;
443  }
444 
445  if (ctx->trace_enable)
446  position = get_bits_count(gbc);
447 
448  value = get_bits_long(gbc, width);
449 
450  if (ctx->trace_enable) {
451  char bits[33];
452  int i;
453  for (i = 0; i < width; i++)
454  bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
455  bits[i] = 0;
456 
457  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
458  bits, value);
459  }
460 
461  if (value < range_min || value > range_max) {
462  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
463  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
464  name, value, range_min, range_max);
465  return AVERROR_INVALIDDATA;
466  }
467 
468  *write_to = value;
469  return 0;
470 }
471 
473  int width, const char *name,
474  const int *subscripts, uint32_t value,
475  uint32_t range_min, uint32_t range_max)
476 {
477  av_assert0(width > 0 && width <= 32);
478 
479  if (value < range_min || value > range_max) {
480  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
481  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
482  name, value, range_min, range_max);
483  return AVERROR_INVALIDDATA;
484  }
485 
486  if (put_bits_left(pbc) < width)
487  return AVERROR(ENOSPC);
488 
489  if (ctx->trace_enable) {
490  char bits[33];
491  int i;
492  for (i = 0; i < width; i++)
493  bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
494  bits[i] = 0;
495 
497  name, subscripts, bits, value);
498  }
499 
500  if (width < 32)
501  put_bits(pbc, width, value);
502  else
503  put_bits32(pbc, value);
504 
505  return 0;
506 }
507 
509  int width, const char *name,
510  const int *subscripts, int32_t *write_to,
511  int32_t range_min, int32_t range_max)
512 {
513  int32_t value;
514  int position;
515 
516  av_assert0(width > 0 && width <= 32);
517 
518  if (get_bits_left(gbc) < width) {
519  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
520  "%s: bitstream ended.\n", name);
521  return AVERROR_INVALIDDATA;
522  }
523 
524  if (ctx->trace_enable)
525  position = get_bits_count(gbc);
526 
527  value = get_sbits_long(gbc, width);
528 
529  if (ctx->trace_enable) {
530  char bits[33];
531  int i;
532  for (i = 0; i < width; i++)
533  bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
534  bits[i] = 0;
535 
536  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
537  bits, value);
538  }
539 
540  if (value < range_min || value > range_max) {
541  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
542  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
543  name, value, range_min, range_max);
544  return AVERROR_INVALIDDATA;
545  }
546 
547  *write_to = value;
548  return 0;
549 }
550 
552  int width, const char *name,
553  const int *subscripts, int32_t value,
554  int32_t range_min, int32_t range_max)
555 {
556  av_assert0(width > 0 && width <= 32);
557 
558  if (value < range_min || value > range_max) {
559  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
560  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
561  name, value, range_min, range_max);
562  return AVERROR_INVALIDDATA;
563  }
564 
565  if (put_bits_left(pbc) < width)
566  return AVERROR(ENOSPC);
567 
568  if (ctx->trace_enable) {
569  char bits[33];
570  int i;
571  for (i = 0; i < width; i++)
572  bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
573  bits[i] = 0;
574 
576  name, subscripts, bits, value);
577  }
578 
579  if (width < 32)
580  put_sbits(pbc, width, value);
581  else
582  put_bits32(pbc, value);
583 
584  return 0;
585 }
586 
587 
589  CodedBitstreamUnit *unit,
590  size_t size,
591  void (*free)(void *opaque, uint8_t *data))
592 {
593  av_assert0(!unit->content && !unit->content_ref);
594 
595  unit->content = av_mallocz(size);
596  if (!unit->content)
597  return AVERROR(ENOMEM);
598 
599  unit->content_ref = av_buffer_create(unit->content, size,
600  free, ctx, 0);
601  if (!unit->content_ref) {
602  av_freep(&unit->content);
603  return AVERROR(ENOMEM);
604  }
605 
606  return 0;
607 }
608 
610  CodedBitstreamUnit *unit,
611  size_t size)
612 {
613  av_assert0(!unit->data && !unit->data_ref);
614 
616  if (!unit->data_ref)
617  return AVERROR(ENOMEM);
618 
619  unit->data = unit->data_ref->data;
620  unit->data_size = size;
621 
622  memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
623 
624  return 0;
625 }
626 
629  int position)
630 {
631  CodedBitstreamUnit *units;
632 
633  if (frag->nb_units < frag->nb_units_allocated) {
634  units = frag->units;
635 
636  if (position < frag->nb_units)
637  memmove(units + position + 1, units + position,
638  (frag->nb_units - position) * sizeof(*units));
639  } else {
640  units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
641  if (!units)
642  return AVERROR(ENOMEM);
643 
644  ++frag->nb_units_allocated;
645 
646  if (position > 0)
647  memcpy(units, frag->units, position * sizeof(*units));
648 
649  if (position < frag->nb_units)
650  memcpy(units + position + 1, frag->units + position,
651  (frag->nb_units - position) * sizeof(*units));
652  }
653 
654  memset(units + position, 0, sizeof(*units));
655 
656  if (units != frag->units) {
657  av_free(frag->units);
658  frag->units = units;
659  }
660 
661  ++frag->nb_units;
662 
663  return 0;
664 }
665 
668  int position,
670  void *content,
671  AVBufferRef *content_buf)
672 {
673  CodedBitstreamUnit *unit;
674  AVBufferRef *content_ref;
675  int err;
676 
677  if (position == -1)
678  position = frag->nb_units;
679  av_assert0(position >= 0 && position <= frag->nb_units);
680 
681  if (content_buf) {
682  content_ref = av_buffer_ref(content_buf);
683  if (!content_ref)
684  return AVERROR(ENOMEM);
685  } else {
686  content_ref = NULL;
687  }
688 
689  err = cbs_insert_unit(ctx, frag, position);
690  if (err < 0) {
691  av_buffer_unref(&content_ref);
692  return err;
693  }
694 
695  unit = &frag->units[position];
696  unit->type = type;
697  unit->content = content;
698  unit->content_ref = content_ref;
699 
700  return 0;
701 }
702 
705  int position,
707  uint8_t *data, size_t data_size,
708  AVBufferRef *data_buf)
709 {
710  CodedBitstreamUnit *unit;
711  AVBufferRef *data_ref;
712  int err;
713 
714  if (position == -1)
715  position = frag->nb_units;
716  av_assert0(position >= 0 && position <= frag->nb_units);
717 
718  if (data_buf)
719  data_ref = av_buffer_ref(data_buf);
720  else
721  data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
722  if (!data_ref)
723  return AVERROR(ENOMEM);
724 
725  err = cbs_insert_unit(ctx, frag, position);
726  if (err < 0) {
727  av_buffer_unref(&data_ref);
728  return err;
729  }
730 
731  unit = &frag->units[position];
732  unit->type = type;
733  unit->data = data;
734  unit->data_size = data_size;
735  unit->data_ref = data_ref;
736 
737  return 0;
738 }
739 
742  int position)
743 {
744  av_assert0(0 <= position && position < frag->nb_units
745  && "Unit to be deleted not in fragment.");
746 
747  cbs_unit_uninit(ctx, &frag->units[position]);
748 
749  --frag->nb_units;
750 
751  if (frag->nb_units > 0)
752  memmove(frag->units + position,
753  frag->units + position + 1,
754  (frag->nb_units - position) * sizeof(*frag->units));
755 }
const char * name
Definition: avisynth_c.h:867
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:250
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
int size
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:240
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:345
static void cbs_unit_uninit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs.c:127
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:74
void ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:740
enum AVCodecID codec_id
Definition: qsv.c:72
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
int size
Definition: avcodec.h:1478
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
Definition: cbs.c:588
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:666
void ff_cbs_trace_header(CodedBitstreamContext *ctx, const char *name)
Definition: cbs.c:370
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3949
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:590
int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition: cbs.c:472
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:206
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_internal.h:53
uint8_t
#define av_malloc(s)
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition: cbs.c:429
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:86
static const CodedBitstreamType * cbs_type_table[]
Definition: cbs.c:31
const char data[16]
Definition: mxf.c:91
static int cbs_read_fragment_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs.c:163
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:239
uint8_t * data
Definition: avcodec.h:1477
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, int32_t value, int32_t range_min, int32_t range_max)
Definition: cbs.c:551
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:609
Coded bitstream unit structure.
Definition: cbs.h:64
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:75
int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, int32_t *write_to, int32_t range_min, int32_t range_max)
Definition: cbs.c:508
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:703
#define av_log(a,...)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
void ff_cbs_fragment_free(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:154
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* close)(CodedBitstreamContext *ctx)
Definition: cbs_internal.h:57
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
#define AVERROR(e)
Definition: error.h:43
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1460
simple assert() macros that are a bit more flexible than ISO C assert().
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
uint8_t bits
Definition: vp3data.h:202
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:133
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3975
const CodedBitstreamType ff_cbs_type_mpeg2
Definition: cbs_mpeg2.c:436
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:173
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1561
const CodedBitstreamType ff_cbs_type_av1
Definition: cbs_av1.c:1305
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:48
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:320
#define width
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
int n
Definition: avisynth_c.h:760
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
#define FF_ARRAY_ELEMS(a)
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:284
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:201
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
const CodedBitstreamType ff_cbs_type_jpeg
Definition: cbs_jpeg.c:509
uint8_t * data
The data buffer.
Definition: buffer.h:89
int trace_level
Log level to use for trace output.
Definition: cbs.h:212
void * buf
Definition: avisynth_c.h:766
static int cbs_fill_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Definition: cbs.c:200
double value
Definition: eval.c:98
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment&#39;s own data buffer, but not the units a...
Definition: cbs.c:139
Context structure for coded bitstream operations.
Definition: cbs.h:168
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:106
cl_device_type type
refcounted data buffer API
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:113
#define snprintf
Definition: snprintf.h:34
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:44
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
Definition: cbs.c:221
int nb_units_allocated
Number of allocated units.
Definition: cbs.h:154
void * priv_data
Internal codec-specific data.
Definition: cbs.h:189
A reference to a data buffer.
Definition: buffer.h:81
common internal and external API header
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_internal.h:38
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:92
static int cbs_insert_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Definition: cbs.c:627
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:197
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:1574
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
Definition: cbs.c:52
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:790
#define av_free(p)
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3971
#define av_freep(p)
#define AV_CODEC_ID_H265
Definition: avcodec.h:393
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:178
#define av_malloc_array(a, b)
int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Read a bitstream from a memory region into a fragment, then split into units and decompose.
Definition: cbs.c:266
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:139
const CodedBitstreamType ff_cbs_type_vp9
Definition: cbs_vp9.c:681
This structure stores compressed data.
Definition: avcodec.h:1454
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *str, const int *subscripts, const char *bits, int64_t value)
Definition: cbs.c:379
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:80