FFmpeg
cbs_av1.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 "libavutil/avassert.h"
20 #include "libavutil/opt.h"
21 #include "libavutil/pixfmt.h"
22 
23 #include "cbs.h"
24 #include "cbs_internal.h"
25 #include "cbs_av1.h"
26 #include "defs.h"
27 #include "refstruct.h"
28 
29 
31  const char *name, uint32_t *write_to,
32  uint32_t range_min, uint32_t range_max)
33 {
34  uint32_t zeroes, bits_value, value;
35 
37 
38  zeroes = 0;
39  while (zeroes < 32) {
40  if (get_bits_left(gbc) < 1) {
41  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
42  "%s: bitstream ended.\n", name);
43  return AVERROR_INVALIDDATA;
44  }
45 
46  if (get_bits1(gbc))
47  break;
48  ++zeroes;
49  }
50 
51  if (zeroes >= 32) {
52  // The spec allows at least thirty-two zero bits followed by a
53  // one to mean 2^32-1, with no constraint on the number of
54  // zeroes. The libaom reference decoder does not match this,
55  // instead reading thirty-two zeroes but not the following one
56  // to mean 2^32-1. These two interpretations are incompatible
57  // and other implementations may follow one or the other.
58  // Therefore we reject thirty-two zeroes because the intended
59  // behaviour is not clear.
60  av_log(ctx->log_ctx, AV_LOG_ERROR, "Thirty-two zero bits in "
61  "%s uvlc code: considered invalid due to conflicting "
62  "standard and reference decoder behaviour.\n", name);
63  return AVERROR_INVALIDDATA;
64  } else {
65  if (get_bits_left(gbc) < zeroes) {
66  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
67  "%s: bitstream ended.\n", name);
68  return AVERROR_INVALIDDATA;
69  }
70 
71  bits_value = get_bits_long(gbc, zeroes);
72  value = bits_value + (UINT32_C(1) << zeroes) - 1;
73  }
74 
76 
77  if (value < range_min || value > range_max) {
78  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
79  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
80  name, value, range_min, range_max);
81  return AVERROR_INVALIDDATA;
82  }
83 
84  *write_to = value;
85  return 0;
86 }
87 
89  const char *name, uint32_t value,
90  uint32_t range_min, uint32_t range_max)
91 {
92  uint32_t v;
93  int zeroes;
94 
96 
97  if (value < range_min || value > range_max) {
98  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
99  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
100  name, value, range_min, range_max);
101  return AVERROR_INVALIDDATA;
102  }
103 
104  zeroes = av_log2(value + 1);
105  v = value - (1U << zeroes) + 1;
106 
107  if (put_bits_left(pbc) < 2 * zeroes + 1)
108  return AVERROR(ENOSPC);
109 
110  put_bits(pbc, zeroes, 0);
111  put_bits(pbc, 1, 1);
112  put_bits(pbc, zeroes, v);
113 
115 
116  return 0;
117 }
118 
120  const char *name, uint64_t *write_to)
121 {
122  uint64_t value;
123  uint32_t byte;
124  int i;
125 
127 
128  value = 0;
129  for (i = 0; i < 8; i++) {
130  if (get_bits_left(gbc) < 8) {
131  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid leb128 at "
132  "%s: bitstream ended.\n", name);
133  return AVERROR_INVALIDDATA;
134  }
135  byte = get_bits(gbc, 8);
136  value |= (uint64_t)(byte & 0x7f) << (i * 7);
137  if (!(byte & 0x80))
138  break;
139  }
140 
141  if (value > UINT32_MAX)
142  return AVERROR_INVALIDDATA;
143 
145 
146  *write_to = value;
147  return 0;
148 }
149 
151  const char *name, uint64_t value, int fixed_length)
152 {
153  int len, i;
154  uint8_t byte;
155 
157 
158  len = (av_log2(value) + 7) / 7;
159 
160  if (fixed_length) {
161  if (fixed_length < len) {
162  av_log(ctx->log_ctx, AV_LOG_ERROR, "OBU is too large for "
163  "fixed length size field (%d > %d).\n",
164  len, fixed_length);
165  return AVERROR(EINVAL);
166  }
167  len = fixed_length;
168  }
169 
170  for (i = 0; i < len; i++) {
171  if (put_bits_left(pbc) < 8)
172  return AVERROR(ENOSPC);
173 
174  byte = value >> (7 * i) & 0x7f;
175  if (i < len - 1)
176  byte |= 0x80;
177 
178  put_bits(pbc, 8, byte);
179  }
180 
182 
183  return 0;
184 }
185 
187  uint32_t n, const char *name,
188  const int *subscripts, uint32_t *write_to)
189 {
190  uint32_t m, v, extra_bit, value;
191  int w;
192 
194 
195  av_assert0(n > 0);
196 
197  w = av_log2(n) + 1;
198  m = (1 << w) - n;
199 
200  if (get_bits_left(gbc) < w) {
201  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid non-symmetric value at "
202  "%s: bitstream ended.\n", name);
203  return AVERROR_INVALIDDATA;
204  }
205 
206  if (w - 1 > 0)
207  v = get_bits(gbc, w - 1);
208  else
209  v = 0;
210 
211  if (v < m) {
212  value = v;
213  } else {
214  extra_bit = get_bits1(gbc);
215  value = (v << 1) - m + extra_bit;
216  }
217 
219 
220  *write_to = value;
221  return 0;
222 }
223 
225  uint32_t n, const char *name,
226  const int *subscripts, uint32_t value)
227 {
228  uint32_t w, m, v, extra_bit;
229 
231 
232  if (value > n) {
233  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
234  "%"PRIu32", but must be in [0,%"PRIu32"].\n",
235  name, value, n);
236  return AVERROR_INVALIDDATA;
237  }
238 
239  w = av_log2(n) + 1;
240  m = (1 << w) - n;
241 
242  if (put_bits_left(pbc) < w)
243  return AVERROR(ENOSPC);
244 
245  if (value < m) {
246  v = value;
247  put_bits(pbc, w - 1, v);
248  } else {
249  v = m + ((value - m) >> 1);
250  extra_bit = (value - m) & 1;
251  put_bits(pbc, w - 1, v);
252  put_bits(pbc, 1, extra_bit);
253  }
254 
256 
257  return 0;
258 }
259 
261  uint32_t range_min, uint32_t range_max,
262  const char *name, uint32_t *write_to)
263 {
264  uint32_t value;
265 
267 
268  av_assert0(range_min <= range_max && range_max - range_min < 32);
269 
270  for (value = range_min; value < range_max;) {
271  if (get_bits_left(gbc) < 1) {
272  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
273  "%s: bitstream ended.\n", name);
274  return AVERROR_INVALIDDATA;
275  }
276  if (get_bits1(gbc))
277  ++value;
278  else
279  break;
280  }
281 
283 
284  *write_to = value;
285  return 0;
286 }
287 
289  uint32_t range_min, uint32_t range_max,
290  const char *name, uint32_t value)
291 {
292  int len;
293 
295 
296  av_assert0(range_min <= range_max && range_max - range_min < 32);
297  if (value < range_min || value > range_max) {
298  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
299  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
300  name, value, range_min, range_max);
301  return AVERROR_INVALIDDATA;
302  }
303 
304  if (value == range_max)
305  len = range_max - range_min;
306  else
307  len = value - range_min + 1;
308  if (put_bits_left(pbc) < len)
309  return AVERROR(ENOSPC);
310 
311  if (len > 0)
312  put_bits(pbc, len, (1U << len) - 1 - (value != range_max));
313 
315 
316  return 0;
317 }
318 
320  uint32_t range_max, const char *name,
321  const int *subscripts, uint32_t *write_to)
322 {
323  uint32_t value, max_len, len, range_offset, range_bits;
324  int err;
325 
327 
328  av_assert0(range_max > 0);
329  max_len = av_log2(range_max - 1) - 3;
330 
331  err = cbs_av1_read_increment(ctx, gbc, 0, max_len,
332  "subexp_more_bits", &len);
333  if (err < 0)
334  return err;
335 
336  if (len) {
337  range_bits = 2 + len;
338  range_offset = 1 << range_bits;
339  } else {
340  range_bits = 3;
341  range_offset = 0;
342  }
343 
344  if (len < max_len) {
345  err = ff_cbs_read_simple_unsigned(ctx, gbc, range_bits,
346  "subexp_bits", &value);
347  if (err < 0)
348  return err;
349 
350  } else {
351  err = cbs_av1_read_ns(ctx, gbc, range_max - range_offset,
352  "subexp_final_bits", NULL, &value);
353  if (err < 0)
354  return err;
355  }
356  value += range_offset;
357 
359 
360  *write_to = value;
361  return err;
362 }
363 
365  uint32_t range_max, const char *name,
366  const int *subscripts, uint32_t value)
367 {
368  int err;
369  uint32_t max_len, len, range_offset, range_bits;
370 
372 
373  if (value > range_max) {
374  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
375  "%"PRIu32", but must be in [0,%"PRIu32"].\n",
376  name, value, range_max);
377  return AVERROR_INVALIDDATA;
378  }
379 
380  av_assert0(range_max > 0);
381  max_len = av_log2(range_max - 1) - 3;
382 
383  if (value < 8) {
384  range_bits = 3;
385  range_offset = 0;
386  len = 0;
387  } else {
388  range_bits = av_log2(value);
389  len = range_bits - 2;
390  if (len > max_len) {
391  // The top bin is combined with the one below it.
392  av_assert0(len == max_len + 1);
393  --range_bits;
394  len = max_len;
395  }
396  range_offset = 1 << range_bits;
397  }
398 
399  err = cbs_av1_write_increment(ctx, pbc, 0, max_len,
400  "subexp_more_bits", len);
401  if (err < 0)
402  return err;
403 
404  if (len < max_len) {
405  err = ff_cbs_write_simple_unsigned(ctx, pbc, range_bits,
406  "subexp_bits",
407  value - range_offset);
408  if (err < 0)
409  return err;
410 
411  } else {
412  err = cbs_av1_write_ns(ctx, pbc, range_max - range_offset,
413  "subexp_final_bits", NULL,
414  value - range_offset);
415  if (err < 0)
416  return err;
417  }
418 
420 
421  return err;
422 }
423 
424 
425 static int cbs_av1_tile_log2(int blksize, int target)
426 {
427  int k;
428  for (k = 0; (blksize << k) < target; k++);
429  return k;
430 }
431 
433  unsigned int a, unsigned int b)
434 {
435  unsigned int diff, m;
436  if (!seq->enable_order_hint)
437  return 0;
438  diff = a - b;
439  m = 1 << seq->order_hint_bits_minus_1;
440  diff = (diff & (m - 1)) - (diff & m);
441  return diff;
442 }
443 
445 {
446  GetBitContext tmp = *gbc;
447  size_t size = 0;
448  for (int i = 0; get_bits_left(&tmp) >= 8; i++) {
449  if (get_bits(&tmp, 8))
450  size = i;
451  }
452  return size;
453 }
454 
455 
456 #define HEADER(name) do { \
457  ff_cbs_trace_header(ctx, name); \
458  } while (0)
459 
460 #define CHECK(call) do { \
461  err = (call); \
462  if (err < 0) \
463  return err; \
464  } while (0)
465 
466 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
467 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
468 #define FUNC(name) FUNC_AV1(READWRITE, name)
469 
470 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
471 
472 #define fc(width, name, range_min, range_max) \
473  xf(width, name, current->name, range_min, range_max, 0, )
474 #define flag(name) fb(1, name)
475 #define su(width, name) \
476  xsu(width, name, current->name, 0, )
477 
478 #define fbs(width, name, subs, ...) \
479  xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
480 #define fcs(width, name, range_min, range_max, subs, ...) \
481  xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
482 #define flags(name, subs, ...) \
483  xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
484 #define sus(width, name, subs, ...) \
485  xsu(width, name, current->name, subs, __VA_ARGS__)
486 
487 #define fixed(width, name, value) do { \
488  av_unused uint32_t fixed_value = value; \
489  xf(width, name, fixed_value, value, value, 0, ); \
490  } while (0)
491 
492 
493 #define READ
494 #define READWRITE read
495 #define RWContext GetBitContext
496 
497 #define fb(width, name) do { \
498  uint32_t value; \
499  CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, \
500  #name, &value)); \
501  current->name = value; \
502  } while (0)
503 
504 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
505  uint32_t value; \
506  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
507  SUBSCRIPTS(subs, __VA_ARGS__), \
508  &value, range_min, range_max)); \
509  var = value; \
510  } while (0)
511 
512 #define xsu(width, name, var, subs, ...) do { \
513  int32_t value; \
514  CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
515  SUBSCRIPTS(subs, __VA_ARGS__), &value, \
516  MIN_INT_BITS(width), \
517  MAX_INT_BITS(width))); \
518  var = value; \
519  } while (0)
520 
521 #define uvlc(name, range_min, range_max) do { \
522  uint32_t value; \
523  CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
524  &value, range_min, range_max)); \
525  current->name = value; \
526  } while (0)
527 
528 #define ns(max_value, name, subs, ...) do { \
529  uint32_t value; \
530  CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
531  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
532  current->name = value; \
533  } while (0)
534 
535 #define increment(name, min, max) do { \
536  uint32_t value; \
537  CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
538  current->name = value; \
539  } while (0)
540 
541 #define subexp(name, max, subs, ...) do { \
542  uint32_t value; \
543  CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
544  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
545  current->name = value; \
546  } while (0)
547 
548 #define delta_q(name) do { \
549  uint8_t delta_coded; \
550  int8_t delta_q; \
551  xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
552  if (delta_coded) \
553  xsu(1 + 6, name.delta_q, delta_q, 0, ); \
554  else \
555  delta_q = 0; \
556  current->name = delta_q; \
557  } while (0)
558 
559 #define leb128(name) do { \
560  uint64_t value; \
561  CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
562  current->name = value; \
563  } while (0)
564 
565 #define infer(name, value) do { \
566  current->name = value; \
567  } while (0)
568 
569 #define byte_alignment(rw) (get_bits_count(rw) % 8)
570 
571 #include "cbs_av1_syntax_template.c"
572 
573 #undef READ
574 #undef READWRITE
575 #undef RWContext
576 #undef fb
577 #undef xf
578 #undef xsu
579 #undef uvlc
580 #undef ns
581 #undef increment
582 #undef subexp
583 #undef delta_q
584 #undef leb128
585 #undef infer
586 #undef byte_alignment
587 
588 
589 #define WRITE
590 #define READWRITE write
591 #define RWContext PutBitContext
592 
593 #define fb(width, name) do { \
594  CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
595  current->name)); \
596  } while (0)
597 
598 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
599  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
600  SUBSCRIPTS(subs, __VA_ARGS__), \
601  var, range_min, range_max)); \
602  } while (0)
603 
604 #define xsu(width, name, var, subs, ...) do { \
605  CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
606  SUBSCRIPTS(subs, __VA_ARGS__), var, \
607  MIN_INT_BITS(width), \
608  MAX_INT_BITS(width))); \
609  } while (0)
610 
611 #define uvlc(name, range_min, range_max) do { \
612  CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
613  range_min, range_max)); \
614  } while (0)
615 
616 #define ns(max_value, name, subs, ...) do { \
617  CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
618  SUBSCRIPTS(subs, __VA_ARGS__), \
619  current->name)); \
620  } while (0)
621 
622 #define increment(name, min, max) do { \
623  CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
624  current->name)); \
625  } while (0)
626 
627 #define subexp(name, max, subs, ...) do { \
628  CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
629  SUBSCRIPTS(subs, __VA_ARGS__), \
630  current->name)); \
631  } while (0)
632 
633 #define delta_q(name) do { \
634  xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
635  if (current->name) \
636  xsu(1 + 6, name.delta_q, current->name, 0, ); \
637  } while (0)
638 
639 #define leb128(name) do { \
640  CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name, 0)); \
641  } while (0)
642 
643 #define infer(name, value) do { \
644  if (current->name != (value)) { \
645  av_log(ctx->log_ctx, AV_LOG_ERROR, \
646  "%s does not match inferred value: " \
647  "%"PRId64", but should be %"PRId64".\n", \
648  #name, (int64_t)current->name, (int64_t)(value)); \
649  return AVERROR_INVALIDDATA; \
650  } \
651  } while (0)
652 
653 #define byte_alignment(rw) (put_bits_count(rw) % 8)
654 
655 #include "cbs_av1_syntax_template.c"
656 
657 #undef WRITE
658 #undef READWRITE
659 #undef RWContext
660 #undef fb
661 #undef xf
662 #undef xsu
663 #undef uvlc
664 #undef ns
665 #undef increment
666 #undef subexp
667 #undef delta_q
668 #undef leb128
669 #undef infer
670 #undef byte_alignment
671 
672 
675  int header)
676 {
677  GetBitContext gbc;
678  uint8_t *data;
679  size_t size;
680  uint64_t obu_length;
681  int pos, err, trace;
682 
683  // Don't include this parsing in trace output.
684  trace = ctx->trace_enable;
685  ctx->trace_enable = 0;
686 
687  data = frag->data;
688  size = frag->data_size;
689 
690  if (INT_MAX / 8 < size) {
691  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: "
692  "too large (%"SIZE_SPECIFIER" bytes).\n", size);
693  err = AVERROR_INVALIDDATA;
694  goto fail;
695  }
696 
697  if (header && size && data[0] & 0x80) {
698  // first bit is nonzero, the extradata does not consist purely of
699  // OBUs. Expect MP4/Matroska AV1CodecConfigurationRecord
700  int config_record_version = data[0] & 0x7f;
701 
702  if (config_record_version != 1) {
703  av_log(ctx->log_ctx, AV_LOG_ERROR,
704  "Unknown version %d of AV1CodecConfigurationRecord "
705  "found!\n",
706  config_record_version);
707  err = AVERROR_INVALIDDATA;
708  goto fail;
709  }
710 
711  if (size <= 4) {
712  if (size < 4) {
713  av_log(ctx->log_ctx, AV_LOG_WARNING,
714  "Undersized AV1CodecConfigurationRecord v%d found!\n",
715  config_record_version);
716  err = AVERROR_INVALIDDATA;
717  goto fail;
718  }
719 
720  goto success;
721  }
722 
723  // In AV1CodecConfigurationRecord v1, actual OBUs start after
724  // four bytes. Thus set the offset as required for properly
725  // parsing them.
726  data += 4;
727  size -= 4;
728  }
729 
730  while (size > 0) {
732  uint64_t obu_size;
733 
734  init_get_bits(&gbc, data, 8 * size);
735 
736  err = cbs_av1_read_obu_header(ctx, &gbc, &header);
737  if (err < 0)
738  goto fail;
739 
740  if (header.obu_has_size_field) {
741  if (get_bits_left(&gbc) < 8) {
742  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment "
743  "too short (%"SIZE_SPECIFIER" bytes).\n", size);
744  err = AVERROR_INVALIDDATA;
745  goto fail;
746  }
747  err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
748  if (err < 0)
749  goto fail;
750  } else
751  obu_size = size - 1 - header.obu_extension_flag;
752 
753  pos = get_bits_count(&gbc);
754  av_assert0(pos % 8 == 0 && pos / 8 <= size);
755 
756  obu_length = pos / 8 + obu_size;
757 
758  if (size < obu_length) {
759  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
760  "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
761  obu_length, size);
762  err = AVERROR_INVALIDDATA;
763  goto fail;
764  }
765 
766  err = ff_cbs_append_unit_data(frag, header.obu_type,
767  data, obu_length, frag->data_ref);
768  if (err < 0)
769  goto fail;
770 
771  data += obu_length;
772  size -= obu_length;
773  }
774 
775 success:
776  err = 0;
777 fail:
778  ctx->trace_enable = trace;
779  return err;
780 }
781 
783  CodedBitstreamUnit *unit,
784  GetBitContext *gbc,
785  AV1RawTileData *td)
786 {
787  int pos;
788 
789  pos = get_bits_count(gbc);
790  if (pos >= 8 * unit->data_size) {
791  av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before "
792  "any data in tile group (%d bits read).\n", pos);
793  return AVERROR_INVALIDDATA;
794  }
795  // Must be byte-aligned at this point.
796  av_assert0(pos % 8 == 0);
797 
798  td->data_ref = av_buffer_ref(unit->data_ref);
799  if (!td->data_ref)
800  return AVERROR(ENOMEM);
801 
802  td->data = unit->data + pos / 8;
803  td->data_size = unit->data_size - pos / 8;
804 
805  return 0;
806 }
807 
809  CodedBitstreamUnit *unit)
810 {
812  AV1RawOBU *obu;
813  GetBitContext gbc;
814  int err, start_pos, end_pos;
815 
816  err = ff_cbs_alloc_unit_content(ctx, unit);
817  if (err < 0)
818  return err;
819  obu = unit->content;
820 
821  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
822  if (err < 0)
823  return err;
824 
825  err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header);
826  if (err < 0)
827  return err;
828  av_assert0(obu->header.obu_type == unit->type);
829 
830  if (obu->header.obu_has_size_field) {
831  uint64_t obu_size;
832  err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
833  if (err < 0)
834  return err;
835  obu->obu_size = obu_size;
836  } else {
837  if (unit->data_size < 1 + obu->header.obu_extension_flag) {
838  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
839  "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size);
840  return AVERROR_INVALIDDATA;
841  }
842  obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
843  }
844 
845  start_pos = get_bits_count(&gbc);
846 
847  if (obu->header.obu_extension_flag) {
850  priv->operating_point_idc) {
851  int in_temporal_layer =
852  (priv->operating_point_idc >> priv->temporal_id ) & 1;
853  int in_spatial_layer =
854  (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1;
855  if (!in_temporal_layer || !in_spatial_layer) {
856  return AVERROR(EAGAIN); // drop_obu()
857  }
858  }
859  }
860 
861  switch (obu->header.obu_type) {
863  {
864  err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
865  &obu->obu.sequence_header);
866  if (err < 0)
867  return err;
868 
869  if (priv->operating_point >= 0) {
871 
872  if (priv->operating_point > sequence_header->operating_points_cnt_minus_1) {
873  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid Operating Point %d requested. "
874  "Must not be higher than %u.\n",
875  priv->operating_point, sequence_header->operating_points_cnt_minus_1);
876  return AVERROR(EINVAL);
877  }
878  priv->operating_point_idc = sequence_header->operating_point_idc[priv->operating_point];
879  }
880 
882  priv->sequence_header = &obu->obu.sequence_header;
883  }
884  break;
886  {
887  err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
888  if (err < 0)
889  return err;
890  }
891  break;
894  {
895  err = cbs_av1_read_frame_header_obu(ctx, &gbc,
896  &obu->obu.frame_header,
897  obu->header.obu_type ==
899  unit->data_ref);
900  if (err < 0)
901  return err;
902  }
903  break;
904  case AV1_OBU_TILE_GROUP:
905  {
906  err = cbs_av1_read_tile_group_obu(ctx, &gbc,
907  &obu->obu.tile_group);
908  if (err < 0)
909  return err;
910 
911  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
912  &obu->obu.tile_group.tile_data);
913  if (err < 0)
914  return err;
915  }
916  break;
917  case AV1_OBU_FRAME:
918  {
919  err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame,
920  unit->data_ref);
921  if (err < 0)
922  return err;
923 
924  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
925  &obu->obu.frame.tile_group.tile_data);
926  if (err < 0)
927  return err;
928  }
929  break;
930  case AV1_OBU_TILE_LIST:
931  {
932  err = cbs_av1_read_tile_list_obu(ctx, &gbc,
933  &obu->obu.tile_list);
934  if (err < 0)
935  return err;
936 
937  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
938  &obu->obu.tile_list.tile_data);
939  if (err < 0)
940  return err;
941  }
942  break;
943  case AV1_OBU_METADATA:
944  {
945  err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata);
946  if (err < 0)
947  return err;
948  }
949  break;
950  case AV1_OBU_PADDING:
951  {
952  err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->obu.padding);
953  if (err < 0)
954  return err;
955  }
956  break;
957  default:
958  return AVERROR(ENOSYS);
959  }
960 
961  end_pos = get_bits_count(&gbc);
962  av_assert0(end_pos <= unit->data_size * 8);
963 
964  if (obu->obu_size > 0 &&
967  obu->header.obu_type != AV1_OBU_FRAME) {
968  int nb_bits = obu->obu_size * 8 + start_pos - end_pos;
969 
970  if (nb_bits <= 0)
971  return AVERROR_INVALIDDATA;
972 
973  err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
974  if (err < 0)
975  return err;
976  }
977 
978  return 0;
979 }
980 
982  CodedBitstreamUnit *unit,
983  PutBitContext *pbc)
984 {
986  AV1RawOBU *obu = unit->content;
987  PutBitContext pbc_tmp;
988  AV1RawTileData *td;
989  size_t header_size;
990  int err, start_pos, end_pos, data_pos;
992 
993  // OBUs in the normal bitstream format must contain a size field
994  // in every OBU (in annex B it is optional, but we don't support
995  // writing that).
996  obu->header.obu_has_size_field = 1;
997  av1ctx = *priv;
998 
999  if (priv->sequence_header_ref) {
1001  }
1002 
1003  if (priv->frame_header_ref) {
1005  if (!av1ctx.frame_header_ref) {
1006  err = AVERROR(ENOMEM);
1007  goto error;
1008  }
1009  }
1010 
1011  err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
1012  if (err < 0)
1013  goto error;
1014 
1015  if (obu->header.obu_has_size_field) {
1016  pbc_tmp = *pbc;
1017  if (priv->fixed_obu_size_length) {
1018  for (int i = 0; i < priv->fixed_obu_size_length; i++)
1019  put_bits(pbc, 8, 0);
1020  } else {
1021  // Add space for the size field to fill later.
1022  put_bits32(pbc, 0);
1023  put_bits32(pbc, 0);
1024  }
1025  }
1026 
1027  td = NULL;
1028  start_pos = put_bits_count(pbc);
1029 
1030  switch (obu->header.obu_type) {
1032  {
1033  err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1034  &obu->obu.sequence_header);
1035  if (err < 0)
1036  goto error;
1037 
1039  priv->sequence_header = NULL;
1040 
1041  err = ff_cbs_make_unit_refcounted(ctx, unit);
1042  if (err < 0)
1043  goto error;
1044 
1046  priv->sequence_header = &obu->obu.sequence_header;
1047  }
1048  break;
1050  {
1051  err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1052  if (err < 0)
1053  goto error;
1054  }
1055  break;
1056  case AV1_OBU_FRAME_HEADER:
1058  {
1059  err = cbs_av1_write_frame_header_obu(ctx, pbc,
1060  &obu->obu.frame_header,
1061  obu->header.obu_type ==
1063  NULL);
1064  if (err < 0)
1065  goto error;
1066  }
1067  break;
1068  case AV1_OBU_TILE_GROUP:
1069  {
1070  err = cbs_av1_write_tile_group_obu(ctx, pbc,
1071  &obu->obu.tile_group);
1072  if (err < 0)
1073  goto error;
1074 
1075  td = &obu->obu.tile_group.tile_data;
1076  }
1077  break;
1078  case AV1_OBU_FRAME:
1079  {
1080  err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
1081  if (err < 0)
1082  goto error;
1083 
1084  td = &obu->obu.frame.tile_group.tile_data;
1085  }
1086  break;
1087  case AV1_OBU_TILE_LIST:
1088  {
1089  err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list);
1090  if (err < 0)
1091  goto error;
1092 
1093  td = &obu->obu.tile_list.tile_data;
1094  }
1095  break;
1096  case AV1_OBU_METADATA:
1097  {
1098  err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
1099  if (err < 0)
1100  goto error;
1101  }
1102  break;
1103  case AV1_OBU_PADDING:
1104  {
1105  err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding);
1106  if (err < 0)
1107  goto error;
1108  }
1109  break;
1110  default:
1111  err = AVERROR(ENOSYS);
1112  goto error;
1113  }
1114 
1115  end_pos = put_bits_count(pbc);
1116  header_size = (end_pos - start_pos + 7) / 8;
1117  if (td) {
1118  obu->obu_size = header_size + td->data_size;
1119  } else if (header_size > 0) {
1120  // Add trailing bits and recalculate.
1121  err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1122  if (err < 0)
1123  goto error;
1124  end_pos = put_bits_count(pbc);
1125  obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
1126  } else {
1127  // Empty OBU.
1128  obu->obu_size = 0;
1129  }
1130 
1131  end_pos = put_bits_count(pbc);
1132  // Must now be byte-aligned.
1133  av_assert0(end_pos % 8 == 0);
1134  flush_put_bits(pbc);
1135  start_pos /= 8;
1136  end_pos /= 8;
1137 
1138  *pbc = pbc_tmp;
1139  err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size,
1140  priv->fixed_obu_size_length);
1141  if (err < 0)
1142  goto error;
1143 
1144  data_pos = put_bits_count(pbc) / 8;
1145  flush_put_bits(pbc);
1146  av_assert0(data_pos <= start_pos);
1147 
1148  if (8 * obu->obu_size > put_bits_left(pbc)) {
1151  *priv = av1ctx;
1152 
1153  return AVERROR(ENOSPC);
1154  }
1155 
1156  if (obu->obu_size > 0) {
1157  if (!priv->fixed_obu_size_length) {
1158  memmove(pbc->buf + data_pos,
1159  pbc->buf + start_pos, header_size);
1160  } else {
1161  // The size was fixed so the following data was
1162  // already written in the correct place.
1163  }
1164  skip_put_bytes(pbc, header_size);
1165 
1166  if (td) {
1167  memcpy(pbc->buf + data_pos + header_size,
1168  td->data, td->data_size);
1169  skip_put_bytes(pbc, td->data_size);
1170  }
1171  }
1172 
1173  // OBU data must be byte-aligned.
1174  av_assert0(put_bits_count(pbc) % 8 == 0);
1175  err = 0;
1176 
1177 error:
1180 
1181  return err;
1182 }
1183 
1185  CodedBitstreamFragment *frag)
1186 {
1187  size_t size, pos;
1188  int i;
1189 
1190  size = 0;
1191  for (i = 0; i < frag->nb_units; i++)
1192  size += frag->units[i].data_size;
1193 
1195  if (!frag->data_ref)
1196  return AVERROR(ENOMEM);
1197  frag->data = frag->data_ref->data;
1198  memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1199 
1200  pos = 0;
1201  for (i = 0; i < frag->nb_units; i++) {
1202  memcpy(frag->data + pos, frag->units[i].data,
1203  frag->units[i].data_size);
1204  pos += frag->units[i].data_size;
1205  }
1206  av_assert0(pos == size);
1207  frag->data_size = size;
1208 
1209  return 0;
1210 }
1211 
1213 {
1215 
1217  priv->sequence_header = NULL;
1218  priv->frame_header = NULL;
1219 
1220  memset(priv->ref, 0, sizeof(priv->ref));
1221  priv->operating_point_idc = 0;
1222  priv->seen_frame_header = 0;
1223  priv->tile_num = 0;
1224 }
1225 
1227 {
1229 
1232 }
1233 
1234 static void cbs_av1_free_metadata(FFRefStructOpaque unused, void *content)
1235 {
1236  AV1RawOBU *obu = content;
1237  AV1RawMetadata *md;
1238 
1240  md = &obu->obu.metadata;
1241 
1242  switch (md->metadata_type) {
1247  break;
1249  av_buffer_unref(&md->metadata.itut_t35.payload_ref);
1250  break;
1251  default:
1252  av_buffer_unref(&md->metadata.unknown.payload_ref);
1253  }
1254 }
1255 
1261 
1263  obu.tile_group.tile_data.data),
1267  obu.tile_list.tile_data.data),
1269  obu.padding.payload),
1270 
1273 
1275 };
1276 
1277 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1278 static const AVOption cbs_av1_options[] = {
1279  { "operating_point", "Set operating point to select layers to parse from a scalable bitstream",
1280  OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AV1_MAX_OPERATING_POINTS - 1, 0 },
1281  { "fixed_obu_size_length", "Set fixed length of the obu_size field",
1282  OFFSET(fixed_obu_size_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, 0 },
1283  { NULL }
1284 };
1285 
1286 static const AVClass cbs_av1_class = {
1287  .class_name = "cbs_av1",
1288  .item_name = av_default_item_name,
1289  .option = cbs_av1_options,
1290  .version = LIBAVUTIL_VERSION_INT,
1291 };
1292 
1295 
1296  .priv_class = &cbs_av1_class,
1297  .priv_data_size = sizeof(CodedBitstreamAV1Context),
1298 
1300 
1305 
1306  .flush = &cbs_av1_flush,
1307  .close = &cbs_av1_close,
1308 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
CodedBitstreamUnit::content_ref
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition: cbs.h:112
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
CBS_UNIT_TYPE_COMPLEX
#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func)
Definition: cbs_internal.h:332
AV1_OBU_REDUNDANT_FRAME_HEADER
@ AV1_OBU_REDUNDANT_FRAME_HEADER
Definition: av1.h:36
cbs_av1_class
static const AVClass cbs_av1_class
Definition: cbs_av1.c:1286
cbs_av1_unit_types
static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[]
Definition: cbs_av1.c:1256
ff_refstruct_ref
void * ff_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:140
CodedBitstreamAV1Context::operating_point
int operating_point
Definition: cbs_av1.h:475
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
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
put_bits32
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:291
CodedBitstreamAV1Context::seen_frame_header
int seen_frame_header
Definition: cbs_av1.h:445
AV1RawSequenceHeader
Definition: cbs_av1.h:73
cbs_av1_write_increment
static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t range_min, uint32_t range_max, const char *name, uint32_t value)
Definition: cbs_av1.c:288
CodedBitstreamType::close
void(* close)(CodedBitstreamContext *ctx)
Definition: cbs_internal.h:152
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:107
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
md
#define md
Definition: vf_colormatrix.c:101
CodedBitstreamAV1Context::tile_num
int tile_num
Definition: cbs_av1.h:467
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
w
uint8_t w
Definition: llviddspenc.c:38
cbs_av1_read_increment
static int cbs_av1_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t range_min, uint32_t range_max, const char *name, uint32_t *write_to)
Definition: cbs_av1.c:260
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:219
CodedBitstreamAV1Context::frame_header_ref
AVBufferRef * frame_header_ref
Definition: cbs_av1.h:446
AVOption
AVOption.
Definition: opt.h:429
b
#define b
Definition: input.c:41
data
const char data[16]
Definition: mxf.c:149
cbs_av1_read_leb128
static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint64_t *write_to)
Definition: cbs_av1.c:119
cbs_av1_read_ns
static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t n, const char *name, const int *subscripts, uint32_t *write_to)
Definition: cbs_av1.c:186
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:74
cbs.h
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
AV1_METADATA_TYPE_ITUT_T35
@ AV1_METADATA_TYPE_ITUT_T35
Definition: av1.h:47
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AV1_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_TEMPORAL_DELIMITER
Definition: av1.h:31
AV1RawTileData::data
uint8_t * data
Definition: cbs_av1.h:292
AV1RawOBU::header
AV1RawOBUHeader header
Definition: cbs_av1.h:401
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:70
AV1RawTileData
Definition: cbs_av1.h:291
cbs_av1_syntax_template.c
cbs_av1_read_subexp
static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t *write_to)
Definition: cbs_av1.c:319
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
fail
#define fail()
Definition: checkasm.h:189
AV1_OBU_FRAME_HEADER
@ AV1_OBU_FRAME_HEADER
Definition: av1.h:32
CBS_UNIT_TYPE_INTERNAL_REF
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
Definition: cbs_internal.h:312
CBS_TRACE_WRITE_END_VALUE_ONLY
#define CBS_TRACE_WRITE_END_VALUE_ONLY()
Definition: cbs_internal.h:276
GetBitContext
Definition: get_bits.h:108
CodedBitstreamAV1Context::ref
AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES]
Definition: cbs_av1.h:472
put_bits_left
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:125
cbs_av1_close
static void cbs_av1_close(CodedBitstreamContext *ctx)
Definition: cbs_av1.c:1226
ff_cbs_type_av1
const CodedBitstreamType ff_cbs_type_av1
Definition: cbs_av1.c:1293
CodedBitstreamUnit::data
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:81
refstruct.h
cbs_av1_write_obu
static int cbs_av1_write_obu(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_av1.c:981
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:168
cbs_av1.h
avassert.h
CodedBitstreamUnitTypeDescriptor
Definition: cbs_internal.h:56
AV1RawOBUHeader::obu_extension_flag
uint8_t obu_extension_flag
Definition: cbs_av1.h:32
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
cbs_av1_read_uvlc
static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition: cbs_av1.c:30
AV1RawOBU::tile_list
AV1RawTileList tile_list
Definition: cbs_av1.h:410
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:135
cbs_av1_ref_tile_data
static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, GetBitContext *gbc, AV1RawTileData *td)
Definition: cbs_av1.c:782
cbs_av1_free_metadata
static void cbs_av1_free_metadata(FFRefStructOpaque unused, void *content)
Definition: cbs_av1.c:1234
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AV1_METADATA_TYPE_HDR_CLL
@ AV1_METADATA_TYPE_HDR_CLL
Definition: av1.h:44
cbs_av1_split_fragment
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_av1.c:673
ctx
AVFormatContext * ctx
Definition: movenc.c:49
cbs_av1_write_uvlc
static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition: cbs_av1.c:88
ff_cbs_append_unit_data
int ff_cbs_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:850
CodedBitstreamAV1Context::operating_point_idc
int operating_point_idc
Definition: cbs_av1.h:452
cbs_internal.h
CodedBitstreamType::codec_id
enum AVCodecID codec_id
Definition: cbs_internal.h:103
AV1RawOBU::obu_size
size_t obu_size
Definition: cbs_av1.h:403
PutBitContext
Definition: put_bits.h:50
CBS_TRACE_WRITE_END
#define CBS_TRACE_WRITE_END()
Definition: cbs_internal.h:254
CodedBitstreamType::read_unit
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:128
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV1RawMetadata
Definition: cbs_av1.h:381
AV1RawOBU
Definition: cbs_av1.h:400
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
PutBitContext::buf
uint8_t * buf
Definition: put_bits.h:53
NULL
#define NULL
Definition: coverity.c:32
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
cbs_av1_read_unit
static int cbs_av1_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_av1.c:808
cbs_av1_get_payload_bytes_left
static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
Definition: cbs_av1.c:444
cbs_av1_options
static const AVOption cbs_av1_options[]
Definition: cbs_av1.c:1278
AV1RawTileData::data_size
size_t data_size
Definition: cbs_av1.h:294
CBS_TRACE_READ_END_NO_SUBSCRIPTS
#define CBS_TRACE_READ_END_NO_SUBSCRIPTS()
Definition: cbs_internal.h:229
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
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:86
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:370
AV1_METADATA_TYPE_SCALABILITY
@ AV1_METADATA_TYPE_SCALABILITY
Definition: av1.h:46
cbs_av1_write_subexp
static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t range_max, const char *name, const int *subscripts, uint32_t value)
Definition: cbs_av1.c:364
AV1_OBU_TILE_GROUP
@ AV1_OBU_TILE_GROUP
Definition: av1.h:33
ff_cbs_read_simple_unsigned
int ff_cbs_read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
Definition: cbs.c:640
CBS_TRACE_READ_END_VALUE_ONLY
#define CBS_TRACE_READ_END_VALUE_ONLY()
Definition: cbs_internal.h:237
AV1_OBU_FRAME
@ AV1_OBU_FRAME
Definition: av1.h:35
AV1RawOBU::obu
union AV1RawOBU::@67 obu
AV1RawTileList::tile_data
AV1RawTileData tile_data
Definition: cbs_av1.h:315
cbs_av1_flush
static void cbs_av1_flush(CodedBitstreamContext *ctx)
Definition: cbs_av1.c:1212
byte
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:99
size
int size
Definition: twinvq_data.h:10344
AV1RawOBU::sequence_header
AV1RawSequenceHeader sequence_header
Definition: cbs_av1.h:406
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:128
CodedBitstreamAV1Context::frame_header
uint8_t * frame_header
Definition: cbs_av1.h:447
OFFSET
#define OFFSET(x)
Definition: cbs_av1.c:1277
AV1_OBU_SEQUENCE_HEADER
@ AV1_OBU_SEQUENCE_HEADER
Definition: av1.h:30
CodedBitstreamType::unit_types
const CodedBitstreamUnitTypeDescriptor * unit_types
Definition: cbs_internal.h:115
header
static const uint8_t header[24]
Definition: sdr2.c:68
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
AV1_METADATA_TYPE_HDR_MDCV
@ AV1_METADATA_TYPE_HDR_MDCV
Definition: av1.h:45
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
CBS_UNIT_TYPE_POD
#define CBS_UNIT_TYPE_POD(type_, structure)
Definition: cbs_internal.h:288
CodedBitstreamType
Definition: cbs_internal.h:102
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
AV1RawOBU::frame_header
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:407
cbs_av1_assemble_fragment
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_av1.c:1184
AV1RawOBU::metadata
AV1RawMetadata metadata
Definition: cbs_av1.h:411
cbs_av1_tile_log2
static int cbs_av1_tile_log2(int blksize, int target)
Definition: cbs_av1.c:425
CBS_TRACE_WRITE_END_NO_SUBSCRIPTS
#define CBS_TRACE_WRITE_END_NO_SUBSCRIPTS()
Definition: cbs_internal.h:266
CodedBitstreamAV1Context::spatial_id
int spatial_id
Definition: cbs_av1.h:451
AV1_OBU_PADDING
@ AV1_OBU_PADDING
Definition: av1.h:39
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:98
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
CodedBitstreamType::write_unit
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_internal.h:133
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:80
cbs_av1_write_leb128
static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint64_t value, int fixed_length)
Definition: cbs_av1.c:150
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
len
int len
Definition: vorbis_enc_data.h:426
ff_cbs_make_unit_refcounted
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
Definition: cbs.c:1035
AV1RawSequenceHeader::enable_order_hint
uint8_t enable_order_hint
Definition: cbs_av1.h:113
AV1RawOBU::tile_group
AV1RawTileGroup tile_group
Definition: cbs_av1.h:409
AV1RawTileGroup::tile_data
AV1RawTileData tile_data
Definition: cbs_av1.h:302
CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_END_OF_LIST
Definition: cbs_internal.h:335
pixfmt.h
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
AV1_OBU_TILE_LIST
@ AV1_OBU_TILE_LIST
Definition: av1.h:37
ff_refstruct_replace
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
pos
unsigned int pos
Definition: spdifenc.c:414
ff_cbs_alloc_unit_content
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition: cbs.c:923
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
cbs_av1_get_relative_dist
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
Definition: cbs_av1.c:432
U
#define U(x)
Definition: vpx_arith.h:37
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:129
AV1_METADATA_TYPE_TIMECODE
@ AV1_METADATA_TYPE_TIMECODE
Definition: av1.h:48
ff_cbs_write_simple_unsigned
int ff_cbs_write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
Definition: cbs.c:676
CodedBitstreamAV1Context::sequence_header
AV1RawSequenceHeader * sequence_header
Definition: cbs_av1.h:441
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
defs.h
skip_put_bytes
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:386
AV1RawSequenceHeader::order_hint_bits_minus_1
uint8_t order_hint_bits_minus_1
Definition: cbs_av1.h:122
AV1RawOBU::frame
AV1RawFrame frame
Definition: cbs_av1.h:408
AV1RawFrame::tile_group
AV1RawTileGroup tile_group
Definition: cbs_av1.h:307
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AV1_MAX_OPERATING_POINTS
@ AV1_MAX_OPERATING_POINTS
Definition: av1.h:74
AV1RawTileData::data_ref
AVBufferRef * data_ref
Definition: cbs_av1.h:293
cbs_av1_write_ns
static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc, uint32_t n, const char *name, const int *subscripts, uint32_t value)
Definition: cbs_av1.c:224
CodedBitstreamAV1Context::sequence_header_ref
AV1RawOBU * sequence_header_ref
A RefStruct reference backing sequence_header.
Definition: cbs_av1.h:443
sequence_header
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
Definition: cbs_mpeg2_syntax_template.c:19
AV1RawPadding::payload
uint8_t * payload
Definition: cbs_av1.h:394
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
CodedBitstreamType::assemble_fragment
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_internal.h:145
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:145
CodedBitstreamType::split_fragment
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_internal.h:122
AV1RawOBUHeader
Definition: cbs_av1.h:29
CodedBitstreamAV1Context::fixed_obu_size_length
int fixed_obu_size_length
Definition: cbs_av1.h:479
AV1RawOBUHeader::obu_has_size_field
uint8_t obu_has_size_field
Definition: cbs_av1.h:33
CBS_TRACE_READ_START
#define CBS_TRACE_READ_START()
Definition: cbs_internal.h:208
AV1RawOBU::padding
AV1RawPadding padding
Definition: cbs_av1.h:412
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
AV1_OBU_METADATA
@ AV1_OBU_METADATA
Definition: av1.h:34
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:153
CodedBitstreamAV1Context::temporal_id
int temporal_id
Definition: cbs_av1.h:450
CBS_TRACE_READ_END
#define CBS_TRACE_READ_END()
Definition: cbs_internal.h:216
CBS_TRACE_WRITE_START
#define CBS_TRACE_WRITE_START()
Definition: cbs_internal.h:246
CodedBitstreamAV1Context
Definition: cbs_av1.h:438
AV1RawOBUHeader::obu_type
uint8_t obu_type
Definition: cbs_av1.h:31