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 "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 
98  if (type->priv_data_size) {
100  if (!ctx->priv_data) {
101  av_freep(&ctx);
102  return AVERROR(ENOMEM);
103  }
104  }
105 
106  ctx->decompose_unit_types = NULL;
107 
108  ctx->trace_enable = 0;
109  ctx->trace_level = AV_LOG_TRACE;
110 
111  *ctx_ptr = ctx;
112  return 0;
113 }
114 
116 {
117  if (ctx->codec && ctx->codec->flush)
118  ctx->codec->flush(ctx);
119 }
120 
122 {
123  CodedBitstreamContext *ctx = *ctx_ptr;
124 
125  if (!ctx)
126  return;
127 
128  if (ctx->codec && ctx->codec->close)
129  ctx->codec->close(ctx);
130 
131  av_freep(&ctx->write_buffer);
132  av_freep(&ctx->priv_data);
133  av_freep(ctx_ptr);
134 }
135 
137 {
139  unit->content = NULL;
140 
141  av_buffer_unref(&unit->data_ref);
142  unit->data = NULL;
143  unit->data_size = 0;
144  unit->data_bit_padding = 0;
145 }
146 
148 {
149  int i;
150 
151  for (i = 0; i < frag->nb_units; i++)
152  cbs_unit_uninit(&frag->units[i]);
153  frag->nb_units = 0;
154 
155  av_buffer_unref(&frag->data_ref);
156  frag->data = NULL;
157  frag->data_size = 0;
158  frag->data_bit_padding = 0;
159 }
160 
162 {
163  ff_cbs_fragment_reset(frag);
164 
165  av_freep(&frag->units);
166  frag->nb_units_allocated = 0;
167 }
168 
171 {
172  int err, i, j;
173 
174  for (i = 0; i < frag->nb_units; i++) {
175  CodedBitstreamUnit *unit = &frag->units[i];
176 
177  if (ctx->decompose_unit_types) {
178  for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
179  if (ctx->decompose_unit_types[j] == unit->type)
180  break;
181  }
182  if (j >= ctx->nb_decompose_unit_types)
183  continue;
184  }
185 
187  unit->content = NULL;
188 
189  av_assert0(unit->data && unit->data_ref);
190 
191  err = ctx->codec->read_unit(ctx, unit);
192  if (err == AVERROR(ENOSYS)) {
194  "Decomposition unimplemented for unit %d "
195  "(type %"PRIu32").\n", i, unit->type);
196  } else if (err < 0) {
197  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
198  "(type %"PRIu32").\n", i, unit->type);
199  return err;
200  }
201  }
202 
203  return 0;
204 }
205 
207  const uint8_t *data, size_t size)
208 {
209  av_assert0(!frag->data && !frag->data_ref);
210 
211  frag->data_ref =
213  if (!frag->data_ref)
214  return AVERROR(ENOMEM);
215 
216  frag->data = frag->data_ref->data;
217  frag->data_size = size;
218 
219  memcpy(frag->data, data, size);
220  memset(frag->data + size, 0,
222 
223  return 0;
224 }
225 
228  AVBufferRef *buf,
229  const uint8_t *data, size_t size,
230  int header)
231 {
232  int err;
233 
234  if (buf) {
235  frag->data_ref = av_buffer_ref(buf);
236  if (!frag->data_ref)
237  return AVERROR(ENOMEM);
238 
239  frag->data = (uint8_t *)data;
240  frag->data_size = size;
241 
242  } else {
243  err = cbs_fill_fragment_data(frag, data, size);
244  if (err < 0)
245  return err;
246  }
247 
248  err = ctx->codec->split_fragment(ctx, frag, header);
249  if (err < 0)
250  return err;
251 
252  return cbs_read_fragment_content(ctx, frag);
253 }
254 
257  const AVCodecParameters *par)
258 {
259  return cbs_read_data(ctx, frag, NULL,
260  par->extradata,
261  par->extradata_size, 1);
262 }
263 
266  const AVCodecContext *avctx)
267 {
268  return cbs_read_data(ctx, frag, NULL,
269  avctx->extradata,
270  avctx->extradata_size, 1);
271 }
272 
275  const AVPacket *pkt)
276 {
277  return cbs_read_data(ctx, frag, pkt->buf,
278  pkt->data, pkt->size, 0);
279 }
280 
283  const uint8_t *data, size_t size)
284 {
285  return cbs_read_data(ctx, frag, NULL,
286  data, size, 0);
287 }
288 
290  CodedBitstreamUnit *unit)
291 {
292  PutBitContext pbc;
293  int ret;
294 
295  if (!ctx->write_buffer) {
296  // Initial write buffer size is 1MB.
297  ctx->write_buffer_size = 1024 * 1024;
298 
299  reallocate_and_try_again:
300  ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
301  if (ret < 0) {
302  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
303  "sufficiently large write buffer (last attempt "
304  "%"SIZE_SPECIFIER" bytes).\n", ctx->write_buffer_size);
305  return ret;
306  }
307  }
308 
309  init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
310 
311  ret = ctx->codec->write_unit(ctx, unit, &pbc);
312  if (ret < 0) {
313  if (ret == AVERROR(ENOSPC)) {
314  // Overflow.
315  if (ctx->write_buffer_size == INT_MAX / 8)
316  return AVERROR(ENOMEM);
317  ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
318  goto reallocate_and_try_again;
319  }
320  // Write failed for some other reason.
321  return ret;
322  }
323 
324  // Overflow but we didn't notice.
325  av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
326 
327  if (put_bits_count(&pbc) % 8)
328  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
329  else
330  unit->data_bit_padding = 0;
331 
332  flush_put_bits(&pbc);
333 
334  ret = ff_cbs_alloc_unit_data(unit, put_bits_count(&pbc) / 8);
335  if (ret < 0)
336  return ret;
337 
338  memcpy(unit->data, ctx->write_buffer, unit->data_size);
339 
340  return 0;
341 }
342 
345 {
346  int err, i;
347 
348  for (i = 0; i < frag->nb_units; i++) {
349  CodedBitstreamUnit *unit = &frag->units[i];
350 
351  if (!unit->content)
352  continue;
353 
354  av_buffer_unref(&unit->data_ref);
355  unit->data = NULL;
356 
357  err = cbs_write_unit_data(ctx, unit);
358  if (err < 0) {
359  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
360  "(type %"PRIu32").\n", i, unit->type);
361  return err;
362  }
363  av_assert0(unit->data && unit->data_ref);
364  }
365 
366  av_buffer_unref(&frag->data_ref);
367  frag->data = NULL;
368 
369  err = ctx->codec->assemble_fragment(ctx, frag);
370  if (err < 0) {
371  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
372  return err;
373  }
374  av_assert0(frag->data && frag->data_ref);
375 
376  return 0;
377 }
378 
380  AVCodecParameters *par,
382 {
383  int err;
384 
385  err = ff_cbs_write_fragment_data(ctx, frag);
386  if (err < 0)
387  return err;
388 
389  av_freep(&par->extradata);
390 
391  par->extradata = av_malloc(frag->data_size +
393  if (!par->extradata)
394  return AVERROR(ENOMEM);
395 
396  memcpy(par->extradata, frag->data, frag->data_size);
397  memset(par->extradata + frag->data_size, 0,
399  par->extradata_size = frag->data_size;
400 
401  return 0;
402 }
403 
405  AVPacket *pkt,
407 {
408  AVBufferRef *buf;
409  int err;
410 
411  err = ff_cbs_write_fragment_data(ctx, frag);
412  if (err < 0)
413  return err;
414 
415  buf = av_buffer_ref(frag->data_ref);
416  if (!buf)
417  return AVERROR(ENOMEM);
418 
419  av_buffer_unref(&pkt->buf);
420 
421  pkt->buf = buf;
422  pkt->data = frag->data;
423  pkt->size = frag->data_size;
424 
425  return 0;
426 }
427 
428 
430  const char *name)
431 {
432  if (!ctx->trace_enable)
433  return;
434 
435  av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
436 }
437 
439  const char *str, const int *subscripts,
440  const char *bits, int64_t value)
441 {
442  char name[256];
443  size_t name_len, bits_len;
444  int pad, subs, i, j, k, n;
445 
446  if (!ctx->trace_enable)
447  return;
448 
449  av_assert0(value >= INT_MIN && value <= UINT32_MAX);
450 
451  subs = subscripts ? subscripts[0] : 0;
452  n = 0;
453  for (i = j = 0; str[i];) {
454  if (str[i] == '[') {
455  if (n < subs) {
456  ++n;
457  k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
458  av_assert0(k > 0 && j + k < sizeof(name));
459  j += k;
460  for (++i; str[i] && str[i] != ']'; i++);
461  av_assert0(str[i] == ']');
462  } else {
463  while (str[i] && str[i] != ']')
464  name[j++] = str[i++];
465  av_assert0(str[i] == ']');
466  }
467  } else {
468  av_assert0(j + 1 < sizeof(name));
469  name[j++] = str[i++];
470  }
471  }
472  av_assert0(j + 1 < sizeof(name));
473  name[j] = 0;
474  av_assert0(n == subs);
475 
476  name_len = strlen(name);
477  bits_len = strlen(bits);
478 
479  if (name_len + bits_len > 60)
480  pad = bits_len + 2;
481  else
482  pad = 61 - name_len;
483 
484  av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
485  position, name, pad, bits, value);
486 }
487 
489  int width, const char *name,
490  const int *subscripts, uint32_t *write_to,
491  uint32_t range_min, uint32_t range_max)
492 {
493  uint32_t value;
494  int position;
495 
496  av_assert0(width > 0 && width <= 32);
497 
498  if (get_bits_left(gbc) < width) {
499  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
500  "%s: bitstream ended.\n", name);
501  return AVERROR_INVALIDDATA;
502  }
503 
504  if (ctx->trace_enable)
505  position = get_bits_count(gbc);
506 
507  value = get_bits_long(gbc, width);
508 
509  if (ctx->trace_enable) {
510  char bits[33];
511  int i;
512  for (i = 0; i < width; i++)
513  bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
514  bits[i] = 0;
515 
516  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
517  bits, value);
518  }
519 
520  if (value < range_min || value > range_max) {
521  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
522  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
523  name, value, range_min, range_max);
524  return AVERROR_INVALIDDATA;
525  }
526 
527  *write_to = value;
528  return 0;
529 }
530 
532  int width, const char *name,
533  const int *subscripts, uint32_t value,
534  uint32_t range_min, uint32_t range_max)
535 {
536  av_assert0(width > 0 && width <= 32);
537 
538  if (value < range_min || value > range_max) {
539  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
540  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
541  name, value, range_min, range_max);
542  return AVERROR_INVALIDDATA;
543  }
544 
545  if (put_bits_left(pbc) < width)
546  return AVERROR(ENOSPC);
547 
548  if (ctx->trace_enable) {
549  char bits[33];
550  int i;
551  for (i = 0; i < width; i++)
552  bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
553  bits[i] = 0;
554 
556  name, subscripts, bits, value);
557  }
558 
559  if (width < 32)
560  put_bits(pbc, width, value);
561  else
562  put_bits32(pbc, value);
563 
564  return 0;
565 }
566 
568  int width, const char *name,
569  const int *subscripts, int32_t *write_to,
570  int32_t range_min, int32_t range_max)
571 {
572  int32_t value;
573  int position;
574 
575  av_assert0(width > 0 && width <= 32);
576 
577  if (get_bits_left(gbc) < width) {
578  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
579  "%s: bitstream ended.\n", name);
580  return AVERROR_INVALIDDATA;
581  }
582 
583  if (ctx->trace_enable)
584  position = get_bits_count(gbc);
585 
586  value = get_sbits_long(gbc, width);
587 
588  if (ctx->trace_enable) {
589  char bits[33];
590  int i;
591  for (i = 0; i < width; i++)
592  bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
593  bits[i] = 0;
594 
595  ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
596  bits, value);
597  }
598 
599  if (value < range_min || value > range_max) {
600  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
601  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
602  name, value, range_min, range_max);
603  return AVERROR_INVALIDDATA;
604  }
605 
606  *write_to = value;
607  return 0;
608 }
609 
611  int width, const char *name,
612  const int *subscripts, int32_t value,
613  int32_t range_min, int32_t range_max)
614 {
615  av_assert0(width > 0 && width <= 32);
616 
617  if (value < range_min || value > range_max) {
618  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
619  "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
620  name, value, range_min, range_max);
621  return AVERROR_INVALIDDATA;
622  }
623 
624  if (put_bits_left(pbc) < width)
625  return AVERROR(ENOSPC);
626 
627  if (ctx->trace_enable) {
628  char bits[33];
629  int i;
630  for (i = 0; i < width; i++)
631  bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
632  bits[i] = 0;
633 
635  name, subscripts, bits, value);
636  }
637 
638  if (width < 32)
639  put_sbits(pbc, width, value);
640  else
641  put_bits32(pbc, value);
642 
643  return 0;
644 }
645 
646 
648  size_t size,
649  void (*free)(void *opaque, uint8_t *data))
650 {
651  av_assert0(!unit->content && !unit->content_ref);
652 
653  unit->content = av_mallocz(size);
654  if (!unit->content)
655  return AVERROR(ENOMEM);
656 
657  unit->content_ref = av_buffer_create(unit->content, size,
658  free, NULL, 0);
659  if (!unit->content_ref) {
660  av_freep(&unit->content);
661  return AVERROR(ENOMEM);
662  }
663 
664  return 0;
665 }
666 
668  size_t size)
669 {
670  av_assert0(!unit->data && !unit->data_ref);
671 
673  if (!unit->data_ref)
674  return AVERROR(ENOMEM);
675 
676  unit->data = unit->data_ref->data;
677  unit->data_size = size;
678 
679  memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
680 
681  return 0;
682 }
683 
685  int position)
686 {
687  CodedBitstreamUnit *units;
688 
689  if (frag->nb_units < frag->nb_units_allocated) {
690  units = frag->units;
691 
692  if (position < frag->nb_units)
693  memmove(units + position + 1, units + position,
694  (frag->nb_units - position) * sizeof(*units));
695  } else {
696  units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
697  if (!units)
698  return AVERROR(ENOMEM);
699 
700  frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
701 
702  if (position > 0)
703  memcpy(units, frag->units, position * sizeof(*units));
704 
705  if (position < frag->nb_units)
706  memcpy(units + position + 1, frag->units + position,
707  (frag->nb_units - position) * sizeof(*units));
708  }
709 
710  memset(units + position, 0, sizeof(*units));
711 
712  if (units != frag->units) {
713  av_free(frag->units);
714  frag->units = units;
715  }
716 
717  ++frag->nb_units;
718 
719  return 0;
720 }
721 
723  int position,
725  void *content,
726  AVBufferRef *content_buf)
727 {
728  CodedBitstreamUnit *unit;
729  AVBufferRef *content_ref;
730  int err;
731 
732  if (position == -1)
733  position = frag->nb_units;
734  av_assert0(position >= 0 && position <= frag->nb_units);
735 
736  if (content_buf) {
737  content_ref = av_buffer_ref(content_buf);
738  if (!content_ref)
739  return AVERROR(ENOMEM);
740  } else {
741  content_ref = NULL;
742  }
743 
744  err = cbs_insert_unit(frag, position);
745  if (err < 0) {
746  av_buffer_unref(&content_ref);
747  return err;
748  }
749 
750  unit = &frag->units[position];
751  unit->type = type;
752  unit->content = content;
753  unit->content_ref = content_ref;
754 
755  return 0;
756 }
757 
759  int position,
761  uint8_t *data, size_t data_size,
762  AVBufferRef *data_buf)
763 {
764  CodedBitstreamUnit *unit;
765  AVBufferRef *data_ref;
766  int err;
767 
768  if (position == -1)
769  position = frag->nb_units;
770  av_assert0(position >= 0 && position <= frag->nb_units);
771 
772  if (data_buf)
773  data_ref = av_buffer_ref(data_buf);
774  else
775  data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
776  if (!data_ref) {
777  if (!data_buf)
778  av_free(data);
779  return AVERROR(ENOMEM);
780  }
781 
782  err = cbs_insert_unit(frag, position);
783  if (err < 0) {
784  av_buffer_unref(&data_ref);
785  return err;
786  }
787 
788  unit = &frag->units[position];
789  unit->type = type;
790  unit->data = data;
791  unit->data_size = data_size;
792  unit->data_ref = data_ref;
793 
794  return 0;
795 }
796 
798  int position)
799 {
800  av_assert0(0 <= position && position < frag->nb_units
801  && "Unit to be deleted not in fragment.");
802 
803  cbs_unit_uninit(&frag->units[position]);
804 
805  --frag->nb_units;
806 
807  if (frag->nb_units > 0)
808  memmove(frag->units + position,
809  frag->units + position + 1,
810  (frag->nb_units - position) * sizeof(*frag->units));
811 }
812 
813 static void cbs_default_free_unit_content(void *opaque, uint8_t *data)
814 {
815  const CodedBitstreamUnitTypeDescriptor *desc = opaque;
817  int i;
818  for (i = 0; i < desc->nb_ref_offsets; i++) {
819  void **ptr = (void**)(data + desc->ref_offsets[i]);
820  av_buffer_unref((AVBufferRef**)(ptr + 1));
821  }
822  }
823  av_free(data);
824 }
825 
828  CodedBitstreamUnit *unit)
829 {
831  int i, j;
832 
833  if (!ctx->codec->unit_types)
834  return NULL;
835 
836  for (i = 0;; i++) {
837  desc = &ctx->codec->unit_types[i];
838  if (desc->nb_unit_types == 0)
839  break;
840  if (desc->nb_unit_types == CBS_UNIT_TYPE_RANGE) {
841  if (unit->type >= desc->unit_type_range_start &&
842  unit->type <= desc->unit_type_range_end)
843  return desc;
844  } else {
845  for (j = 0; j < desc->nb_unit_types; j++) {
846  if (desc->unit_types[j] == unit->type)
847  return desc;
848  }
849  }
850  }
851  return NULL;
852 }
853 
855  CodedBitstreamUnit *unit)
856 {
858 
859  av_assert0(!unit->content && !unit->content_ref);
860 
861  desc = cbs_find_unit_type_desc(ctx, unit);
862  if (!desc)
863  return AVERROR(ENOSYS);
864 
865  unit->content = av_mallocz(desc->content_size);
866  if (!unit->content)
867  return AVERROR(ENOMEM);
868 
869  unit->content_ref =
871  desc->content_free ? desc->content_free
873  (void*)desc, 0);
874  if (!unit->content_ref) {
875  av_freep(&unit->content);
876  return AVERROR(ENOMEM);
877  }
878 
879  return 0;
880 }
881 
882 static int cbs_clone_unit_content(AVBufferRef **clone_ref,
883  CodedBitstreamUnit *unit,
885 {
886  uint8_t *src, *copy;
887  uint8_t **src_ptr, **copy_ptr;
888  AVBufferRef **src_buf, **copy_buf;
889  int err, i;
890 
891  av_assert0(unit->content);
892  src = unit->content;
893 
894  copy = av_memdup(src, desc->content_size);
895  if (!copy)
896  return AVERROR(ENOMEM);
897 
898  for (i = 0; i < desc->nb_ref_offsets; i++) {
899  src_ptr = (uint8_t**)(src + desc->ref_offsets[i]);
900  src_buf = (AVBufferRef**)(src_ptr + 1);
901  copy_ptr = (uint8_t**)(copy + desc->ref_offsets[i]);
902  copy_buf = (AVBufferRef**)(copy_ptr + 1);
903 
904  if (!*src_ptr) {
905  av_assert0(!*src_buf);
906  continue;
907  }
908  if (!*src_buf) {
909  // We can't handle a non-refcounted pointer here - we don't
910  // have enough information to handle whatever structure lies
911  // at the other end of it.
912  err = AVERROR(EINVAL);
913  goto fail;
914  }
915 
916  // src_ptr is required to point somewhere inside src_buf. If it
917  // doesn't, there is a bug somewhere.
918  av_assert0(*src_ptr >= (*src_buf)->data &&
919  *src_ptr < (*src_buf)->data + (*src_buf)->size);
920 
921  *copy_buf = av_buffer_ref(*src_buf);
922  if (!*copy_buf) {
923  err = AVERROR(ENOMEM);
924  goto fail;
925  }
926  *copy_ptr = (*copy_buf)->data + (*src_ptr - (*src_buf)->data);
927  }
928 
929  *clone_ref = av_buffer_create(copy, desc->content_size,
930  desc->content_free ? desc->content_free :
932  (void*)desc, 0);
933  if (!*clone_ref) {
934  err = AVERROR(ENOMEM);
935  goto fail;
936  }
937 
938  return 0;
939 
940 fail:
941  for (--i; i >= 0; i--)
942  av_buffer_unref((AVBufferRef**)(copy + desc->ref_offsets[i]));
943  av_freep(&copy);
944  *clone_ref = NULL;
945  return err;
946 }
947 
949  CodedBitstreamUnit *unit)
950 {
952  AVBufferRef *ref;
953  int err;
954 
955  av_assert0(unit->content);
956  if (unit->content_ref) {
957  // Already refcounted, nothing to do.
958  return 0;
959  }
960 
961  desc = cbs_find_unit_type_desc(ctx, unit);
962  if (!desc)
963  return AVERROR(ENOSYS);
964 
965  switch (desc->content_type) {
967  ref = av_buffer_alloc(desc->content_size);
968  if (!ref)
969  return AVERROR(ENOMEM);
970  memcpy(ref->data, unit->content, desc->content_size);
971  err = 0;
972  break;
973 
975  err = cbs_clone_unit_content(&ref, unit, desc);
976  break;
977 
979  if (!desc->content_clone)
980  return AVERROR_PATCHWELCOME;
981  err = desc->content_clone(&ref, unit);
982  break;
983 
984  default:
985  av_assert0(0 && "Invalid content type.");
986  }
987 
988  if (err < 0)
989  return err;
990 
991  unit->content_ref = ref;
992  unit->content = ref->data;
993  return 0;
994 }
995 
997  CodedBitstreamUnit *unit)
998 {
1000  AVBufferRef *ref;
1001  int err;
1002 
1003  // This can only be applied to refcounted units.
1004  err = ff_cbs_make_unit_refcounted(ctx, unit);
1005  if (err < 0)
1006  return err;
1007  av_assert0(unit->content && unit->content_ref);
1008 
1010  return 0;
1011 
1012  desc = cbs_find_unit_type_desc(ctx, unit);
1013  if (!desc)
1014  return AVERROR(ENOSYS);
1015 
1016  switch (desc->content_type) {
1017  case CBS_CONTENT_TYPE_POD:
1018  err = av_buffer_make_writable(&unit->content_ref);
1019  break;
1020 
1022  err = cbs_clone_unit_content(&ref, unit, desc);
1023  break;
1024 
1026  if (!desc->content_clone)
1027  return AVERROR_PATCHWELCOME;
1028  err = desc->content_clone(&ref, unit);
1029  break;
1030 
1031  default:
1032  av_assert0(0 && "Invalid content type.");
1033  }
1034  if (err < 0)
1035  return err;
1036 
1037  if (desc->content_type != CBS_CONTENT_TYPE_POD) {
1038  av_buffer_unref(&unit->content_ref);
1039  unit->content_ref = ref;
1040  }
1041  unit->content = unit->content_ref->data;
1042  return 0;
1043 }
int av_buffer_make_writable(AVBufferRef **pbuf)
Create a writable reference from a given buffer reference, avoiding data copy if possible.
Definition: buffer.c:151
int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition: cbs.c:854
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:270
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:149
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void copy(const float *p1, float *p2, const int length)
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
const CodedBitstreamUnitType unit_types[CBS_MAX_UNIT_TYPES]
Definition: cbs_internal.h:59
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:260
const char * desc
Definition: libsvtav1.c:79
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:404
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
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:70
int size
Definition: packet.h:364
void ff_cbs_trace_header(CodedBitstreamContext *ctx, const char *name)
Definition: cbs.c:429
GLint GLenum type
Definition: opengl_enc.c:104
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
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:531
int ff_cbs_insert_unit_data(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:758
#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:208
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_internal.h:117
uint8_t
#define av_malloc(s)
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
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:488
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
uint8_t * write_buffer
Write buffer.
Definition: cbs.h:220
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:88
static const CodedBitstreamType * cbs_type_table[]
Definition: cbs.c:31
static int cbs_read_fragment_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs.c:169
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:273
const CodedBitstreamUnitTypeDescriptor * unit_types
Definition: cbs_internal.h:93
uint8_t * data
Definition: packet.h:363
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:610
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
Coded bitstream unit structure.
Definition: cbs.h:66
ptrdiff_t size
Definition: opengl_enc.c:100
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:103
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:164
static int cbs_insert_unit(CodedBitstreamFragment *frag, int position)
Definition: cbs.c:684
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:77
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:567
#define av_log(a,...)
void ff_cbs_fragment_reset(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:147
int(* content_clone)(AVBufferRef **ref, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:83
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:131
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_internal.h:111
#define U(x)
Definition: vp56_arith.h:37
#define src
Definition: vp8dsp.c:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
void(* flush)(CodedBitstreamContext *ctx)
Definition: cbs_internal.h:121
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void(* close)(CodedBitstreamContext *ctx)
Definition: cbs_internal.h:124
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:109
const CodedBitstreamUnitType unit_type_range_end
Definition: cbs_internal.h:64
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:346
simple assert() macros that are a bit more flexible than ISO C assert().
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:29
int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
Definition: cbs.c:647
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:285
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:135
#define fail()
Definition: checkasm.h:123
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:83
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
const CodedBitstreamType ff_cbs_type_mpeg2
Definition: cbs_mpeg2.c:421
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:175
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1511
size_t ref_offsets[CBS_MAX_REF_OFFSETS]
Definition: cbs_internal.h:80
const CodedBitstreamType ff_cbs_type_av1
Definition: cbs_av1.c:1290
#define FFMIN(a, b)
Definition: common.h:96
static int cbs_write_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs.c:289
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:379
#define width
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
int av_buffer_is_writable(const AVBufferRef *buf)
Definition: buffer.c:133
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
#define AV_CODEC_ID_H265
Definition: codec_id.h:224
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:124
#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:343
int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecContext *avctx)
Read the extradata bitstream found in a codec context into a fragment, then split into units and deco...
Definition: cbs.c:264
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
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:161
static void cbs_unit_uninit(CodedBitstreamUnit *unit)
Definition: cbs.c:136
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:203
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:118
const CodedBitstreamType ff_cbs_type_jpeg
Definition: cbs_jpeg.c:461
main external API structure.
Definition: avcodec.h:526
uint8_t * data
The data buffer.
Definition: buffer.h:89
int trace_level
Log level to use for trace output.
Definition: cbs.h:214
int extradata_size
Definition: avcodec.h:628
Context structure for coded bitstream operations.
Definition: cbs.h:170
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:108
void ff_cbs_delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:797
int ff_cbs_insert_unit_content(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:722
refcounted data buffer API
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
Definition: cbs.c:948
static void cbs_default_free_unit_content(void *opaque, uint8_t *data)
Definition: cbs.c:813
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:121
#define snprintf
Definition: snprintf.h:34
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:106
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:255
#define SIZE_SPECIFIER
Definition: internal.h:229
static int cbs_clone_unit_content(AVBufferRef **clone_ref, CodedBitstreamUnit *unit, const CodedBitstreamUnitTypeDescriptor *desc)
Definition: cbs.c:882
int nb_units_allocated
Number of allocated units.
Definition: cbs.h:156
void * priv_data
Internal codec-specific data.
Definition: cbs.h:191
A reference to a data buffer.
Definition: buffer.h:81
enum CBSContentType content_type
Definition: cbs_internal.h:67
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:117
common internal and external API header
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_internal.h:100
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:94
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:199
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:1527
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:64
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:215
const CodedBitstreamUnitType unit_type_range_start
Definition: cbs_internal.h:63
#define av_free(p)
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:161
void(* content_free)(void *opaque, uint8_t *data)
Definition: cbs_internal.h:82
size_t write_buffer_size
Definition: cbs.h:221
#define av_freep(p)
static const CodedBitstreamUnitTypeDescriptor * cbs_find_unit_type_desc(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs.c:827
void ff_cbs_flush(CodedBitstreamContext *ctx)
Reset all internal state in a context.
Definition: cbs.c:115
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:180
#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:281
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
int ff_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:667
static int cbs_fill_fragment_data(CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Definition: cbs.c:206
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:141
const CodedBitstreamType ff_cbs_type_vp9
Definition: cbs_vp9.c:649
This structure stores compressed data.
Definition: packet.h:340
static int cbs_read_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, AVBufferRef *buf, const uint8_t *data, size_t size, int header)
Definition: cbs.c:226
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:438
int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit writable so that internal fields can be modified.
Definition: cbs.c:996
int i
Definition: input.c:407
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:82
const char * name
Definition: opengl_enc.c:102