FFmpeg
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 "libavutil/avassert.h"
22 #include "libavutil/buffer.h"
23 #include "libavutil/common.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26 
27 #include "avcodec.h"
28 #include "cbs.h"
29 #include "cbs_internal.h"
30 #include "libavutil/refstruct.h"
31 
32 
33 static const CodedBitstreamType *const cbs_type_table[] = {
34 #if CBS_APV
35  &CBS_FUNC(type_apv),
36 #endif
37 #if CBS_AV1
38  &CBS_FUNC(type_av1),
39 #endif
40 #if CBS_H264
41  &CBS_FUNC(type_h264),
42 #endif
43 #if CBS_H265
44  &CBS_FUNC(type_h265),
45 #endif
46 #if CBS_H266
47  &CBS_FUNC(type_h266),
48 #endif
49 #if CBS_LCEVC
50  &CBS_FUNC(type_lcevc),
51 #endif
52 #if CBS_JPEG
53  &CBS_FUNC(type_jpeg),
54 #endif
55 #if CBS_MPEG2
56  &CBS_FUNC(type_mpeg2),
57 #endif
58 #if CBS_VP8
59  &CBS_FUNC(type_vp8),
60 #endif
61 #if CBS_VP9
62  &CBS_FUNC(type_vp9),
63 #endif
64 };
65 
66 const enum AVCodecID CBS_FUNC(all_codec_ids)[] = {
67 #if CBS_APV
69 #endif
70 #if CBS_AV1
72 #endif
73 #if CBS_H264
75 #endif
76 #if CBS_H265
78 #endif
79 #if CBS_H266
81 #endif
82 #if CBS_LCEVC
84 #endif
85 #if CBS_JPEG
87 #endif
88 #if CBS_MPEG2
90 #endif
91 #if CBS_VP8
93 #endif
94 #if CBS_VP9
96 #endif
98 };
99 
101  enum AVCodecID codec_id, void *log_ctx)
102 {
104  const CodedBitstreamType *type;
105  int i;
106 
107  type = NULL;
108  for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
109  if (cbs_type_table[i]->codec_id == codec_id) {
110  type = cbs_type_table[i];
111  break;
112  }
113  }
114  if (!type)
115  return AVERROR(EINVAL);
116 
117  ctx = av_mallocz(sizeof(*ctx));
118  if (!ctx)
119  return AVERROR(ENOMEM);
120 
121  ctx->log_ctx = log_ctx;
122  ctx->codec = type; /* Must be before any error */
123 
124  if (type->priv_data_size) {
125  ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
126  if (!ctx->priv_data) {
127  av_freep(&ctx);
128  return AVERROR(ENOMEM);
129  }
130  if (type->priv_class) {
131  *(const AVClass **)ctx->priv_data = type->priv_class;
133  }
134  }
135 
136  ctx->decompose_unit_types = NULL;
137 
138  ctx->trace_enable = 0;
139  ctx->trace_level = AV_LOG_TRACE;
140  ctx->trace_context = ctx;
141 
142  *ctx_ptr = ctx;
143  return 0;
144 }
145 
147 {
148  if (ctx->codec->flush)
149  ctx->codec->flush(ctx);
150 }
151 
153 {
154  CodedBitstreamContext *ctx = *ctx_ptr;
155 
156  if (!ctx)
157  return;
158 
159  if (ctx->codec->close)
160  ctx->codec->close(ctx);
161 
162  av_freep(&ctx->write_buffer);
163 
164  if (ctx->codec->priv_class && ctx->priv_data)
166 
168  av_freep(ctx_ptr);
169 }
170 
172 {
174  unit->content = NULL;
175 
176  av_buffer_unref(&unit->data_ref);
177  unit->data = NULL;
178  unit->data_size = 0;
179  unit->data_bit_padding = 0;
180 }
181 
183 {
184  int i;
185 
186  for (i = 0; i < frag->nb_units; i++)
187  cbs_unit_uninit(&frag->units[i]);
188  frag->nb_units = 0;
189 
190  av_buffer_unref(&frag->data_ref);
191  frag->data = NULL;
192  frag->data_size = 0;
193  frag->data_bit_padding = 0;
194 }
195 
197 {
198  CBS_FUNC(fragment_reset)(frag);
199 
200  av_freep(&frag->units);
201  frag->nb_units_allocated = 0;
202 }
203 
204 #if CBS_READ
207 {
208  int err, i, j;
209 
210  for (i = 0; i < frag->nb_units; i++) {
211  CodedBitstreamUnit *unit = &frag->units[i];
212 
213  if (ctx->decompose_unit_types) {
214  for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
215  if (ctx->decompose_unit_types[j] == unit->type)
216  break;
217  }
218  if (j >= ctx->nb_decompose_unit_types)
219  continue;
220  }
221 
223  unit->content = NULL;
224 
225  av_assert0(unit->data && unit->data_ref);
226 
227  err = ctx->codec->read_unit(ctx, unit);
228  if (err == AVERROR(ENOSYS)) {
229  av_log(ctx->log_ctx, AV_LOG_VERBOSE,
230  "Decomposition unimplemented for unit %d "
231  "(type %"PRIu32").\n", i, unit->type);
232  } else if (err == AVERROR(EAGAIN)) {
233  av_log(ctx->log_ctx, AV_LOG_VERBOSE,
234  "Skipping decomposition of unit %d "
235  "(type %"PRIu32").\n", i, unit->type);
237  unit->content = NULL;
238  } else if (err < 0) {
239  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
240  "(type %"PRIu32"): %s.\n",
241  i, unit->type, av_err2str(err));
242  return err;
243  }
244  }
245 
246  return 0;
247 }
248 
250  const uint8_t *data, size_t size)
251 {
252  av_assert0(!frag->data && !frag->data_ref);
253 
254  frag->data_ref =
256  if (!frag->data_ref)
257  return AVERROR(ENOMEM);
258 
259  frag->data = frag->data_ref->data;
260  frag->data_size = size;
261 
262  memcpy(frag->data, data, size);
263  memset(frag->data + size, 0,
265 
266  return 0;
267 }
268 
271  const AVBufferRef *buf,
272  const uint8_t *data, size_t size,
273  int header)
274 {
275  int err;
276 
277  if (buf) {
278  frag->data_ref = av_buffer_ref(buf);
279  if (!frag->data_ref)
280  return AVERROR(ENOMEM);
281 
282  frag->data = (uint8_t *)data;
283  frag->data_size = size;
284 
285  } else {
286  err = cbs_fill_fragment_data(frag, data, size);
287  if (err < 0)
288  return err;
289  }
290 
291  err = ctx->codec->split_fragment(ctx, frag, header);
292  if (err < 0)
293  return err;
294 
295  return cbs_read_fragment_content(ctx, frag);
296 }
297 
300  const AVCodecParameters *par)
301 {
302  return cbs_read_data(ctx, frag, NULL,
303  par->extradata,
304  par->extradata_size, 1);
305 }
306 
309  const AVCodecContext *avctx)
310 {
311  return cbs_read_data(ctx, frag, NULL,
312  avctx->extradata,
313  avctx->extradata_size, 1);
314 }
315 
318  const AVPacket *pkt)
319 {
320  return cbs_read_data(ctx, frag, pkt->buf,
321  pkt->data, pkt->size, 0);
322 }
323 
326  const AVPacket *pkt)
327 {
328  size_t side_data_size;
329  const uint8_t *side_data =
331  &side_data_size);
332 
333  return cbs_read_data(ctx, frag, NULL,
334  side_data, side_data_size, 1);
335 }
336 
339  const AVBufferRef *buf,
340  const uint8_t *data, size_t size)
341 {
342  return cbs_read_data(ctx, frag, buf,
343  data, size, 0);
344 }
345 #endif
346 
347 #if CBS_WRITE
348 /**
349  * Allocate a new internal data buffer of the given size in the unit.
350  *
351  * The data buffer will have input padding.
352  */
354  size_t size)
355 {
356  av_assert0(!unit->data && !unit->data_ref);
357 
359  if (!unit->data_ref)
360  return AVERROR(ENOMEM);
361 
362  unit->data = unit->data_ref->data;
363  unit->data_size = size;
364 
365  memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
366 
367  return 0;
368 }
369 
371  CodedBitstreamUnit *unit)
372 {
373  PutBitContext pbc;
374  int ret;
375 
376  if (!ctx->write_buffer) {
377  // Initial write buffer size is 1MB.
378  ctx->write_buffer_size = 1024 * 1024;
379 
380  reallocate_and_try_again:
381  ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
382  if (ret < 0) {
383  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
384  "sufficiently large write buffer (last attempt "
385  "%zu bytes).\n", ctx->write_buffer_size);
386  return ret;
387  }
388  }
389 
390  init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
391 
392  ret = ctx->codec->write_unit(ctx, unit, &pbc);
393  if (ret < 0) {
394  if (ret == AVERROR(ENOSPC)) {
395  // Overflow.
396  if (ctx->write_buffer_size == INT_MAX / 8)
397  return AVERROR(ENOMEM);
398  ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
399  goto reallocate_and_try_again;
400  }
401  // Write failed for some other reason.
402  return ret;
403  }
404 
405  // Overflow but we didn't notice.
406  av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
407 
408  if (put_bits_count(&pbc) % 8)
409  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
410  else
411  unit->data_bit_padding = 0;
412 
413  flush_put_bits(&pbc);
414 
416  if (ret < 0)
417  return ret;
418 
419  memcpy(unit->data, ctx->write_buffer, unit->data_size);
420 
421  return 0;
422 }
423 
426 {
427  int err, i;
428 
429  for (i = 0; i < frag->nb_units; i++) {
430  CodedBitstreamUnit *unit = &frag->units[i];
431 
432  if (!unit->content)
433  continue;
434 
435  av_buffer_unref(&unit->data_ref);
436  unit->data = NULL;
437 
438  err = cbs_write_unit_data(ctx, unit);
439  if (err < 0) {
440  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
441  "(type %"PRIu32").\n", i, unit->type);
442  return err;
443  }
444  av_assert0(unit->data && unit->data_ref);
445  }
446 
447  av_buffer_unref(&frag->data_ref);
448  frag->data = NULL;
449 
450  err = ctx->codec->assemble_fragment(ctx, frag);
451  if (err < 0) {
452  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
453  return err;
454  }
455  av_assert0(frag->data && frag->data_ref);
456 
457  return 0;
458 }
459 
461  AVCodecParameters *par,
463 {
464  int err;
465 
466  err = CBS_FUNC(write_fragment_data)(ctx, frag);
467  if (err < 0)
468  return err;
469 
470  av_freep(&par->extradata);
471  par->extradata_size = 0;
472 
473  if (!frag->data_size)
474  return 0;
475 
476  par->extradata = av_malloc(frag->data_size +
478  if (!par->extradata)
479  return AVERROR(ENOMEM);
480 
481  memcpy(par->extradata, frag->data, frag->data_size);
482  memset(par->extradata + frag->data_size, 0,
484  par->extradata_size = frag->data_size;
485 
486  return 0;
487 }
488 
490  AVPacket *pkt,
492 {
493  AVBufferRef *buf;
494  int err;
495 
496  err = CBS_FUNC(write_fragment_data)(ctx, frag);
497  if (err < 0)
498  return err;
499 
500  buf = av_buffer_ref(frag->data_ref);
501  if (!buf)
502  return AVERROR(ENOMEM);
503 
505 
506  pkt->buf = buf;
507  pkt->data = frag->data;
508  pkt->size = frag->data_size;
509 
510  return 0;
511 }
512 #endif
513 
514 
516  const char *name)
517 {
518 #if CBS_TRACE
519  if (!ctx->trace_enable)
520  return;
521 
522  av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
523 #endif
524 }
525 
526 void CBS_FUNC(trace_read_log)(void *trace_context,
527  GetBitContext *gbc, int length,
528  const char *str, const int *subscripts,
529  int64_t value)
530 {
531 #if CBS_TRACE
532  CodedBitstreamContext *ctx = trace_context;
533  char name[256];
534  char bits[256];
535  size_t name_len, bits_len;
536  int pad, subs, i, j, k, n;
537  int position;
538 
539  av_assert0(value >= INT_MIN && value <= UINT32_MAX);
540 
541  position = get_bits_count(gbc);
542 
543  av_assert0(length < 256);
544  for (i = 0; i < length; i++)
545  bits[i] = get_bits1(gbc) ? '1' : '0';
546  bits[length] = 0;
547 
548  subs = subscripts ? subscripts[0] : 0;
549  n = 0;
550  for (i = j = 0; str[i];) {
551  if (str[i] == '[') {
552  if (n < subs) {
553  ++n;
554  k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
555  av_assert0(k > 0 && j + k < sizeof(name));
556  j += k;
557  for (++i; str[i] && str[i] != ']'; i++);
558  av_assert0(str[i] == ']');
559  } else {
560  while (str[i] && str[i] != ']')
561  name[j++] = str[i++];
562  av_assert0(str[i] == ']');
563  }
564  } else {
565  av_assert0(j + 1 < sizeof(name));
566  name[j++] = str[i++];
567  }
568  }
569  av_assert0(j + 1 < sizeof(name));
570  name[j] = 0;
571  av_assert0(n == subs);
572 
573  name_len = strlen(name);
574  bits_len = length;
575 
576  if (name_len + bits_len > 60)
577  pad = bits_len + 2;
578  else
579  pad = 61 - name_len;
580 
581  av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
582  position, name, pad, bits, value);
583 #endif
584 }
585 
586 void CBS_FUNC(trace_write_log)(void *trace_context,
587  PutBitContext *pbc, int length,
588  const char *str, const int *subscripts,
589  int64_t value)
590 {
591 #if CBS_TRACE
592  CodedBitstreamContext *ctx = trace_context;
593 
594  // Ensure that the syntax element is written to the output buffer,
595  // make a GetBitContext pointed at the start position, then call the
596  // read log function which can read the bits back to log them.
597 
598  GetBitContext gbc;
599  int position;
600 
601  if (length > 0) {
603  flush = *pbc;
605  }
606 
607  position = put_bits_count(pbc);
608  av_assert0(position >= length);
609 
610  init_get_bits(&gbc, pbc->buf, position);
611 
612  skip_bits_long(&gbc, position - length);
613 
614  CBS_FUNC(trace_read_log)(ctx, &gbc, length, str, subscripts, value);
615 #endif
616 }
617 
618 #if CBS_READ
620  GetBitContext *gbc,
621  int width, const char *name,
622  const int *subscripts,
623  uint32_t *write_to,
624  uint32_t range_min,
625  uint32_t range_max)
626 {
627  uint32_t value;
628 
630 
631  av_assert0(width > 0 && width <= 32);
632 
633  if (get_bits_left(gbc) < width) {
634  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
635  "%s: bitstream ended.\n", name);
636  return AVERROR_INVALIDDATA;
637  }
638 
639  value = get_bits_long(gbc, width);
640 
642 
643  if (value < range_min || value > range_max) {
644  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
645  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
646  name, value, range_min, range_max);
647  return AVERROR_INVALIDDATA;
648  }
649 
650  *write_to = value;
651  return 0;
652 }
653 
655  int width, const char *name,
656  const int *subscripts, uint32_t *write_to,
657  uint32_t range_min, uint32_t range_max)
658 {
659  return cbs_read_unsigned(ctx, gbc, width, name, subscripts,
660  write_to, range_min, range_max);
661 }
662 
664  int width, const char *name, uint32_t *write_to)
665 {
666  return cbs_read_unsigned(ctx, gbc, width, name, NULL,
667  write_to, 0, UINT32_MAX);
668 }
669 #endif
670 
671 #if CBS_WRITE
673  int width, const char *name,
674  const int *subscripts, uint32_t value,
675  uint32_t range_min, uint32_t range_max)
676 {
678 
679  av_assert0(width > 0 && width <= 32);
680 
681  if (value < range_min || value > range_max) {
682  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
683  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
684  name, value, range_min, range_max);
685  return AVERROR_INVALIDDATA;
686  }
687 
688  if (put_bits_left(pbc) < width)
689  return AVERROR(ENOSPC);
690 
691  put_bits63(pbc, width, value);
692 
694 
695  return 0;
696 }
697 
699  int width, const char *name, uint32_t value)
700 {
701  return CBS_FUNC(write_unsigned)(ctx, pbc, width, name, NULL,
702  value, 0, MAX_UINT_BITS(width));
703 }
704 #endif
705 
706 #if CBS_READ
708  int width, const char *name,
709  const int *subscripts, int32_t *write_to,
710  int32_t range_min, int32_t range_max)
711 {
712  int32_t value;
713 
715 
716  av_assert0(width > 0 && width <= 32);
717 
718  if (get_bits_left(gbc) < width) {
719  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
720  "%s: bitstream ended.\n", name);
721  return AVERROR_INVALIDDATA;
722  }
723 
724  value = get_sbits_long(gbc, width);
725 
727 
728  if (value < range_min || value > range_max) {
729  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
730  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
731  name, value, range_min, range_max);
732  return AVERROR_INVALIDDATA;
733  }
734 
735  *write_to = value;
736  return 0;
737 }
738 #endif
739 
740 #if CBS_WRITE
742  int width, const char *name,
743  const int *subscripts, int32_t value,
744  int32_t range_min, int32_t range_max)
745 {
747 
748  av_assert0(width > 0 && width <= 32);
749 
750  if (value < range_min || value > range_max) {
751  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
752  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
753  name, value, range_min, range_max);
754  return AVERROR_INVALIDDATA;
755  }
756 
757  if (put_bits_left(pbc) < width)
758  return AVERROR(ENOSPC);
759 
761 
763 
764  return 0;
765 }
766 #endif
767 
768 
770  int position)
771 {
772  CodedBitstreamUnit *units;
773 
774  if (frag->nb_units < frag->nb_units_allocated) {
775  units = frag->units;
776 
777  if (position < frag->nb_units)
778  memmove(units + position + 1, units + position,
779  (frag->nb_units - position) * sizeof(*units));
780  } else {
781  units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
782  if (!units)
783  return AVERROR(ENOMEM);
784 
785  frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
786 
787  if (position > 0)
788  memcpy(units, frag->units, position * sizeof(*units));
789 
790  if (position < frag->nb_units)
791  memcpy(units + position + 1, frag->units + position,
792  (frag->nb_units - position) * sizeof(*units));
793 
794  av_free(frag->units);
795  frag->units = units;
796  }
797 
798  memset(units + position, 0, sizeof(*units));
799 
800  ++frag->nb_units;
801 
802  return 0;
803 }
804 
806  int position,
808  void *content,
809  void *content_ref)
810 {
811  CodedBitstreamUnit *unit;
812  int err;
813 
814  if (position == -1)
815  position = frag->nb_units;
816  av_assert0(position >= 0 && position <= frag->nb_units);
817 
818  err = cbs_insert_unit(frag, position);
819  if (err < 0)
820  return err;
821 
822  if (content_ref) {
823  // Create our own reference out of the user-supplied one.
824  content_ref = av_refstruct_ref(content_ref);
825  }
826 
827  unit = &frag->units[position];
828  unit->type = type;
829  unit->content = content;
830  unit->content_ref = content_ref;
831 
832  return 0;
833 }
834 
837  uint8_t *data, size_t data_size,
838  AVBufferRef *data_buf,
839  int position)
840 {
841  CodedBitstreamUnit *unit;
842  AVBufferRef *data_ref;
843  int err;
844 
845  av_assert0(position >= 0 && position <= frag->nb_units);
846 
847  if (data_buf)
848  data_ref = av_buffer_ref(data_buf);
849  else
850  data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
851  if (!data_ref) {
852  if (!data_buf)
853  av_free(data);
854  return AVERROR(ENOMEM);
855  }
856 
857  err = cbs_insert_unit(frag, position);
858  if (err < 0) {
859  av_buffer_unref(&data_ref);
860  return err;
861  }
862 
863  unit = &frag->units[position];
864  unit->type = type;
865  unit->data = data;
866  unit->data_size = data_size;
867  unit->data_ref = data_ref;
868 
869  return 0;
870 }
871 
874  uint8_t *data, size_t data_size,
875  AVBufferRef *data_buf)
876 {
877  return cbs_insert_unit_data(frag, type,
878  data, data_size, data_buf,
879  frag->nb_units);
880 }
881 
883  int position)
884 {
885  av_assert0(0 <= position && position < frag->nb_units
886  && "Unit to be deleted not in fragment.");
887 
888  cbs_unit_uninit(&frag->units[position]);
889 
890  --frag->nb_units;
891 
892  if (frag->nb_units > 0)
893  memmove(frag->units + position,
894  frag->units + position + 1,
895  (frag->nb_units - position) * sizeof(*frag->units));
896 }
897 
898 static void cbs_default_free_unit_content(AVRefStructOpaque opaque, void *content)
899 {
901 
902  for (int i = 0; i < desc->type.ref.nb_offsets; i++) {
903  void **ptr = (void**)((char*)content + desc->type.ref.offsets[i]);
904  av_buffer_unref((AVBufferRef**)(ptr + 1));
905  }
906 }
907 
910  CodedBitstreamUnit *unit)
911 {
913  int i, j;
914 
915  if (!ctx->codec->unit_types)
916  return NULL;
917 
918  for (i = 0;; i++) {
919  desc = &ctx->codec->unit_types[i];
920  if (desc->nb_unit_types == 0)
921  break;
922  if (desc->nb_unit_types == CBS_UNIT_TYPE_RANGE) {
923  if (unit->type >= desc->unit_type.range.start &&
924  unit->type <= desc->unit_type.range.end)
925  return desc;
926  } else {
927  for (j = 0; j < desc->nb_unit_types; j++) {
928  if (desc->unit_type.list[j] == unit->type)
929  return desc;
930  }
931  }
932  }
933  return NULL;
934 }
935 
937 {
938  return av_refstruct_alloc_ext_c(desc->content_size, 0,
939  (AVRefStructOpaque){ .c = desc },
940  desc->content_type == CBS_CONTENT_TYPE_COMPLEX
941  ? desc->type.complex.content_free
943 }
944 
946  CodedBitstreamUnit *unit)
947 {
949 
950  av_assert0(!unit->content && !unit->content_ref);
951 
953  if (!desc)
954  return AVERROR(ENOSYS);
955 
956  unit->content_ref = cbs_alloc_content(desc);
957  if (!unit->content_ref)
958  return AVERROR(ENOMEM);
959  unit->content = unit->content_ref;
960 
961  return 0;
962 }
963 
964 static int cbs_clone_noncomplex_unit_content(void **clonep,
965  const CodedBitstreamUnit *unit,
967 {
968  const uint8_t *src;
969  uint8_t *copy;
970  int err;
971 
972  av_assert0(unit->content);
973  src = unit->content;
974 
976  if (!copy)
977  return AVERROR(ENOMEM);
978  memcpy(copy, src, desc->content_size);
979  for (int i = 0; i < desc->type.ref.nb_offsets; i++) {
980  void **ptr = (void**)(copy + desc->type.ref.offsets[i]);
981  /* Zero all the AVBufferRefs as they are owned by src. */
982  *(ptr + 1) = NULL;
983  }
984 
985  for (int i = 0; i < desc->type.ref.nb_offsets; i++) {
986  const uint8_t *const *src_ptr = (const uint8_t* const*)(src + desc->type.ref.offsets[i]);
987  const AVBufferRef *src_buf = *(AVBufferRef**)(src_ptr + 1);
988  uint8_t **copy_ptr = (uint8_t**)(copy + desc->type.ref.offsets[i]);
989  AVBufferRef **copy_buf = (AVBufferRef**)(copy_ptr + 1);
990 
991  if (!*src_ptr) {
992  av_assert0(!src_buf);
993  continue;
994  }
995  if (!src_buf) {
996  // We can't handle a non-refcounted pointer here - we don't
997  // have enough information to handle whatever structure lies
998  // at the other end of it.
999  err = AVERROR(EINVAL);
1000  goto fail;
1001  }
1002 
1003  *copy_buf = av_buffer_ref(src_buf);
1004  if (!*copy_buf) {
1005  err = AVERROR(ENOMEM);
1006  goto fail;
1007  }
1008  }
1009  *clonep = copy;
1010 
1011  return 0;
1012 
1013 fail:
1015  return err;
1016 }
1017 
1018 /*
1019  * On success, unit->content and unit->content_ref are updated with
1020  * the new content; unit is untouched on failure.
1021  * Any old content_ref is simply overwritten and not freed.
1022  */
1024  CodedBitstreamUnit *unit)
1025 {
1027  void *new_content;
1028  int err;
1029 
1030  desc = cbs_find_unit_type_desc(ctx, unit);
1031  if (!desc)
1032  return AVERROR(ENOSYS);
1033 
1034  switch (desc->content_type) {
1036  err = cbs_clone_noncomplex_unit_content(&new_content, unit, desc);
1037  break;
1038 
1040  if (!desc->type.complex.content_clone)
1041  return AVERROR_PATCHWELCOME;
1042  err = desc->type.complex.content_clone(&new_content, unit);
1043  break;
1044 
1045  default:
1046  av_assert0(0 && "Invalid content type.");
1047  }
1048 
1049  if (err < 0)
1050  return err;
1051 
1052  unit->content_ref = new_content;
1053  unit->content = new_content;
1054  return 0;
1055 }
1056 
1058  CodedBitstreamUnit *unit)
1059 {
1060  av_assert0(unit->content);
1061  if (unit->content_ref)
1062  return 0;
1063  return cbs_clone_unit_content(ctx, unit);
1064 }
1065 
1067  CodedBitstreamUnit *unit)
1068 {
1069  void *ref = unit->content_ref;
1070  int err;
1071 
1072  av_assert0(unit->content);
1073  if (ref && av_refstruct_exclusive(ref))
1074  return 0;
1075 
1076  err = cbs_clone_unit_content(ctx, unit);
1077  if (err < 0)
1078  return err;
1080  return 0;
1081 }
1082 
1084  CodedBitstreamFragment *frag,
1085  enum AVDiscard skip,
1086  int flags)
1087 {
1088  if (!ctx->codec->discarded_unit)
1089  return;
1090 
1091  for (int i = frag->nb_units - 1; i >= 0; i--) {
1092  if (ctx->codec->discarded_unit(ctx, &frag->units[i], skip)) {
1093  // discard all units
1094  if (!(flags & DISCARD_FLAG_KEEP_NON_VCL)) {
1095  CBS_FUNC(fragment_free)(frag);
1096  return;
1097  }
1098 
1099  CBS_FUNC(delete_unit)(frag, i);
1100  }
1101  }
1102 }
make_unit_writable
int CBS_FUNC() make_unit_writable(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit writable so that internal fields can be modified.
Definition: cbs.c:1066
flags
const SwsFlags flags[]
Definition: swscale.c:62
cbs.h
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:280
read_extradata
int CBS_FUNC() 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:298
CodedBitstreamUnit::content_ref
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition: cbs.h:119
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
av_opt_set_defaults
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1682
delete_unit
void CBS_FUNC() delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:882
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:688
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
cbs_find_unit_type_desc
static CodedBitstreamUnitTypeDescriptor * cbs_find_unit_type_desc(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs.c:909
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:99
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
read_packet_side_data
int CBS_FUNC() read_packet_side_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Definition: cbs.c:324
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:424
CBS_UNIT_TYPE_RANGE
@ CBS_UNIT_TYPE_RANGE
Definition: cbs_internal.h:95
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:114
int64_t
long long int64_t
Definition: coverity.c:34
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
read_simple_unsigned
int CBS_FUNC() read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
Definition: cbs.c:663
append_unit_data
int CBS_FUNC() append_unit_data(CodedBitstreamFragment *frag, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Add a new unit to a fragment with the given data bitstream.
Definition: cbs.c:872
AVPacket::data
uint8_t * data
Definition: packet.h:588
cbs_fill_fragment_data
static int cbs_fill_fragment_data(CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Definition: cbs.c:249
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:226
trace_read_log
void CBS_FUNC() trace_read_log(void *trace_context, GetBitContext *gbc, int length, const char *str, const int *subscripts, int64_t value)
Helper function for read tracing which formats the syntax element and logs the result.
Definition: cbs.c:526
data
const char data[16]
Definition: mxf.c:149
zero_extend
static av_const unsigned zero_extend(unsigned val, unsigned bits)
Definition: mathops.h:153
discard_units
void CBS_FUNC() discard_units(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, enum AVDiscard skip, int flags)
Discard units according to 'skip'.
Definition: cbs.c:1083
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:81
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVRefStructOpaque::c
const void * c
Definition: refstruct.h:60
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:77
write_unsigned
int CBS_FUNC() 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:672
av_refstruct_exclusive
int av_refstruct_exclusive(const void *obj)
Check whether the reference count of an object managed via this API is 1.
Definition: refstruct.c:174
cbs_unit_uninit
static void cbs_unit_uninit(CodedBitstreamUnit *unit)
Definition: cbs.c:171
write_simple_unsigned
int CBS_FUNC() write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
Definition: cbs.c:698
cbs_read_unsigned
static av_always_inline int 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:619
fail
#define fail()
Definition: checkasm.h:220
GetBitContext
Definition: get_bits.h:109
CBS_CONTENT_TYPE_INTERNAL_REFS
@ CBS_CONTENT_TYPE_INTERNAL_REFS
Definition: cbs_internal.h:80
put_bits_left
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:135
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1953
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
alloc_unit_content
int CBS_FUNC() alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition: cbs.c:945
CodedBitstreamUnit::data
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:88
refstruct.h
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:175
insert_unit_content
int CBS_FUNC() insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, void *content_ref)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:805
avassert.h
CodedBitstreamUnitTypeDescriptor
Definition: cbs_internal.h:98
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:106
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:129
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:142
av_refstruct_alloc_ext_c
void * av_refstruct_alloc_ext_c(size_t size, unsigned flags, AVRefStructOpaque opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
Allocate a refcounted object of usable size size managed via the RefStruct API.
Definition: refstruct.c:102
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:222
cbs_insert_unit_data
static int cbs_insert_unit_data(CodedBitstreamFragment *frag, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf, int position)
Definition: cbs.c:835
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:54
read_unsigned
int CBS_FUNC() 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:654
put_bits63
static void put_bits63(PutBitContext *s, int n, uint64_t value)
Write up to 63 bits into a bitstream.
Definition: put_bits.h:344
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
cbs_internal.h
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
MAX_UINT_BITS
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:238
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
PutBitContext
Definition: put_bits.h:50
CBS_TRACE_WRITE_END
#define CBS_TRACE_WRITE_END()
Definition: cbs_internal.h:297
init
av_cold int CBS_FUNC() init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:100
cbs_clone_noncomplex_unit_content
static int cbs_clone_noncomplex_unit_content(void **clonep, const CodedBitstreamUnit *unit, CodedBitstreamUnitTypeDescriptor *desc)
Definition: cbs.c:964
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:571
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
av_buffer_unref
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:139
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
AV_CODEC_ID_H266
#define AV_CODEC_ID_H266
Definition: codec_id.h:253
write_packet
int CBS_FUNC() write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:489
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
CodedBitstreamUnit::data_size
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:93
close
av_cold void CBS_FUNC() close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:152
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:55
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
read_extradata_from_codec
int CBS_FUNC() read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecContext *avctx)
Definition: cbs.c:307
CBS_FUNC
enum AVCodecID CBS_FUNC(all_codec_ids)[]
cbs_alloc_unit_data
static int cbs_alloc_unit_data(CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:353
AVPacket::size
int size
Definition: packet.h:589
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:186
flush
av_cold void CBS_FUNC() flush(CodedBitstreamContext *ctx)
Reset all internal state in a context.
Definition: cbs.c:146
write_fragment_data
int CBS_FUNC() write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:424
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
size
int size
Definition: twinvq_data.h:10344
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:135
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:188
CodedBitstreamUnit::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:99
make_unit_refcounted
int CBS_FUNC() make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
Definition: cbs.c:1057
CBS_CONTENT_TYPE_COMPLEX
@ CBS_CONTENT_TYPE_COMPLEX
Definition: cbs_internal.h:83
trace_header
void CBS_FUNC() trace_header(CodedBitstreamContext *ctx, const char *name)
Definition: cbs.c:515
header
static const uint8_t header[24]
Definition: sdr2.c:68
buffer.h
cbs_insert_unit
static int cbs_insert_unit(CodedBitstreamFragment *frag, int position)
Definition: cbs.c:769
av_refstruct_ref
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:140
CodedBitstreamType
Definition: cbs_internal.h:144
AV_CODEC_ID_LCEVC
@ AV_CODEC_ID_LCEVC
Definition: codec_id.h:615
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
cbs_write_unit_data
static int cbs_write_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs.c:370
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:105
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:90
read_packet
int CBS_FUNC() 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:316
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: packet.c:252
trace_write_log
void CBS_FUNC() trace_write_log(void *trace_context, PutBitContext *pbc, int length, const char *str, const int *subscripts, int64_t value)
Helper function for write tracing which formats the syntax element and logs the result.
Definition: cbs.c:586
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
common.h
av_always_inline
#define av_always_inline
Definition: attributes.h:63
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
write_signed
int CBS_FUNC() 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:741
cbs_default_free_unit_content
static void cbs_default_free_unit_content(AVRefStructOpaque opaque, void *content)
Definition: cbs.c:898
CodedBitstreamFragment::nb_units_allocated
int nb_units_allocated
Number of allocated units.
Definition: cbs.h:167
avcodec.h
ret
ret
Definition: filter_design.txt:187
AV_CODEC_ID_APV
@ AV_CODEC_ID_APV
Definition: codec_id.h:332
av_malloc
void * av_malloc(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:98
cbs_type_table
static const CodedBitstreamType *const cbs_type_table[]
Definition: cbs.c:33
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
cbs_clone_unit_content
static int cbs_clone_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs.c:1023
AVCodecContext
main external API structure.
Definition: avcodec.h:439
DISCARD_FLAG_KEEP_NON_VCL
@ DISCARD_FLAG_KEEP_NON_VCL
keep non-vcl units even if the picture has been dropped.
Definition: cbs.h:515
read
int CBS_FUNC() read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVBufferRef *buf, 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:337
AV_CODEC_ID_H265
#define AV_CODEC_ID_H265
Definition: codec_id.h:229
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
desc
const char * desc
Definition: libsvtav1.c:82
cbs_read_fragment_content
static int cbs_read_fragment_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs.c:205
read_signed
int CBS_FUNC() 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:707
mem.h
fragment_free
av_cold void CBS_FUNC() fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:196
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:153
fragment_reset
void CBS_FUNC() fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:182
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:565
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
int32_t
int32_t
Definition: audioconvert.c:56
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
write_extradata
int CBS_FUNC() write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:460
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
CodedBitstreamFragment::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:152
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
width
#define width
Definition: dsp.h:89
AVDiscard
AVDiscard
Definition: defs.h:223
get_sbits_long
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:474
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
CBS_TRACE_READ_START
#define CBS_TRACE_READ_START()
Definition: cbs_internal.h:251
snprintf
#define snprintf
Definition: snprintf.h:34
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1291
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:383
src
#define src
Definition: vp8dsp.c:248
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:160
cbs_read_data
static int cbs_read_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVBufferRef *buf, const uint8_t *data, size_t size, int header)
Definition: cbs.c:269
CBS_TRACE_READ_END
#define CBS_TRACE_READ_END()
Definition: cbs_internal.h:259
cbs_alloc_content
static void * cbs_alloc_content(CodedBitstreamUnitTypeDescriptor *desc)
Definition: cbs.c:936
CBS_TRACE_WRITE_START
#define CBS_TRACE_WRITE_START()
Definition: cbs_internal.h:289