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