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