FFmpeg  4.2.1
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/pixfmt.h"
21 
22 #include "cbs.h"
23 #include "cbs_internal.h"
24 #include "cbs_av1.h"
25 #include "internal.h"
26 
27 
29  const char *name, uint32_t *write_to,
30  uint32_t range_min, uint32_t range_max)
31 {
32  uint32_t zeroes, bits_value, value;
33  int position;
34 
35  if (ctx->trace_enable)
36  position = get_bits_count(gbc);
37 
38  zeroes = 0;
39  while (1) {
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  value = MAX_UINT_BITS(32);
53  } else {
54  if (get_bits_left(gbc) < zeroes) {
55  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
56  "%s: bitstream ended.\n", name);
57  return AVERROR_INVALIDDATA;
58  }
59 
60  bits_value = get_bits_long(gbc, zeroes);
61  value = bits_value + (UINT32_C(1) << zeroes) - 1;
62  }
63 
64  if (ctx->trace_enable) {
65  char bits[65];
66  int i, j, k;
67 
68  if (zeroes >= 32) {
69  while (zeroes > 32) {
70  k = FFMIN(zeroes - 32, 32);
71  for (i = 0; i < k; i++)
72  bits[i] = '0';
73  bits[i] = 0;
74  ff_cbs_trace_syntax_element(ctx, position, name,
75  NULL, bits, 0);
76  zeroes -= k;
77  position += k;
78  }
79  }
80 
81  for (i = 0; i < zeroes; i++)
82  bits[i] = '0';
83  bits[i++] = '1';
84 
85  if (zeroes < 32) {
86  for (j = 0; j < zeroes; j++)
87  bits[i++] = (bits_value >> (zeroes - j - 1) & 1) ? '1' : '0';
88  }
89 
90  bits[i] = 0;
91  ff_cbs_trace_syntax_element(ctx, position, name,
92  NULL, bits, value);
93  }
94 
95  if (value < range_min || value > range_max) {
96  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
97  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
98  name, value, range_min, range_max);
99  return AVERROR_INVALIDDATA;
100  }
101 
102  *write_to = value;
103  return 0;
104 }
105 
107  const char *name, uint32_t value,
108  uint32_t range_min, uint32_t range_max)
109 {
110  uint32_t v;
111  int position, zeroes;
112 
113  if (value < range_min || value > range_max) {
114  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
115  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
116  name, value, range_min, range_max);
117  return AVERROR_INVALIDDATA;
118  }
119 
120  if (ctx->trace_enable)
121  position = put_bits_count(pbc);
122 
123  if (value == 0) {
124  zeroes = 0;
125  put_bits(pbc, 1, 1);
126  } else {
127  zeroes = av_log2(value + 1);
128  v = value - (1 << zeroes) + 1;
129  put_bits(pbc, zeroes + 1, 1);
130  put_bits(pbc, zeroes, v);
131  }
132 
133  if (ctx->trace_enable) {
134  char bits[65];
135  int i, j;
136  i = 0;
137  for (j = 0; j < zeroes; j++)
138  bits[i++] = '0';
139  bits[i++] = '1';
140  for (j = 0; j < zeroes; j++)
141  bits[i++] = (v >> (zeroes - j - 1) & 1) ? '1' : '0';
142  bits[i++] = 0;
143  ff_cbs_trace_syntax_element(ctx, position, name, NULL,
144  bits, value);
145  }
146 
147  return 0;
148 }
149 
151  const char *name, uint64_t *write_to)
152 {
153  uint64_t value;
154  int position, err, i;
155 
156  if (ctx->trace_enable)
157  position = get_bits_count(gbc);
158 
159  value = 0;
160  for (i = 0; i < 8; i++) {
161  int subscript[2] = { 1, i };
162  uint32_t byte;
163  err = ff_cbs_read_unsigned(ctx, gbc, 8, "leb128_byte[i]", subscript,
164  &byte, 0x00, 0xff);
165  if (err < 0)
166  return err;
167 
168  value |= (uint64_t)(byte & 0x7f) << (i * 7);
169  if (!(byte & 0x80))
170  break;
171  }
172 
173  if (ctx->trace_enable)
174  ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
175 
176  *write_to = value;
177  return 0;
178 }
179 
181  const char *name, uint64_t value)
182 {
183  int position, err, len, i;
184  uint8_t byte;
185 
186  len = (av_log2(value) + 7) / 7;
187 
188  if (ctx->trace_enable)
189  position = put_bits_count(pbc);
190 
191  for (i = 0; i < len; i++) {
192  int subscript[2] = { 1, i };
193 
194  byte = value >> (7 * i) & 0x7f;
195  if (i < len - 1)
196  byte |= 0x80;
197 
198  err = ff_cbs_write_unsigned(ctx, pbc, 8, "leb128_byte[i]", subscript,
199  byte, 0x00, 0xff);
200  if (err < 0)
201  return err;
202  }
203 
204  if (ctx->trace_enable)
205  ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
206 
207  return 0;
208 }
209 
211  uint32_t n, const char *name,
212  const int *subscripts, uint32_t *write_to)
213 {
214  uint32_t w, m, v, extra_bit, value;
215  int position;
216 
217  av_assert0(n > 0);
218 
219  if (ctx->trace_enable)
220  position = get_bits_count(gbc);
221 
222  w = av_log2(n) + 1;
223  m = (1 << w) - n;
224 
225  if (get_bits_left(gbc) < w) {
226  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid non-symmetric value at "
227  "%s: bitstream ended.\n", name);
228  return AVERROR_INVALIDDATA;
229  }
230 
231  if (w - 1 > 0)
232  v = get_bits(gbc, w - 1);
233  else
234  v = 0;
235 
236  if (v < m) {
237  value = v;
238  } else {
239  extra_bit = get_bits1(gbc);
240  value = (v << 1) - m + extra_bit;
241  }
242 
243  if (ctx->trace_enable) {
244  char bits[33];
245  int i;
246  for (i = 0; i < w - 1; i++)
247  bits[i] = (v >> i & 1) ? '1' : '0';
248  if (v >= m)
249  bits[i++] = extra_bit ? '1' : '0';
250  bits[i] = 0;
251 
252  ff_cbs_trace_syntax_element(ctx, position,
253  name, subscripts, bits, value);
254  }
255 
256  *write_to = value;
257  return 0;
258 }
259 
261  uint32_t n, const char *name,
262  const int *subscripts, uint32_t value)
263 {
264  uint32_t w, m, v, extra_bit;
265  int position;
266 
267  if (value > n) {
268  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
269  "%"PRIu32", but must be in [0,%"PRIu32"].\n",
270  name, value, n);
271  return AVERROR_INVALIDDATA;
272  }
273 
274  if (ctx->trace_enable)
275  position = put_bits_count(pbc);
276 
277  w = av_log2(n) + 1;
278  m = (1 << w) - n;
279 
280  if (put_bits_left(pbc) < w)
281  return AVERROR(ENOSPC);
282 
283  if (value < m) {
284  v = value;
285  put_bits(pbc, w - 1, v);
286  } else {
287  v = m + ((value - m) >> 1);
288  extra_bit = (value - m) & 1;
289  put_bits(pbc, w - 1, v);
290  put_bits(pbc, 1, extra_bit);
291  }
292 
293  if (ctx->trace_enable) {
294  char bits[33];
295  int i;
296  for (i = 0; i < w - 1; i++)
297  bits[i] = (v >> i & 1) ? '1' : '0';
298  if (value >= m)
299  bits[i++] = extra_bit ? '1' : '0';
300  bits[i] = 0;
301 
302  ff_cbs_trace_syntax_element(ctx, position,
303  name, subscripts, bits, value);
304  }
305 
306  return 0;
307 }
308 
310  uint32_t range_min, uint32_t range_max,
311  const char *name, uint32_t *write_to)
312 {
313  uint32_t value;
314  int position, i;
315  char bits[33];
316 
317  av_assert0(range_min <= range_max && range_max - range_min < sizeof(bits) - 1);
318  if (ctx->trace_enable)
319  position = get_bits_count(gbc);
320 
321  for (i = 0, value = range_min; value < range_max;) {
322  if (get_bits_left(gbc) < 1) {
323  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
324  "%s: bitstream ended.\n", name);
325  return AVERROR_INVALIDDATA;
326  }
327  if (get_bits1(gbc)) {
328  bits[i++] = '1';
329  ++value;
330  } else {
331  bits[i++] = '0';
332  break;
333  }
334  }
335 
336  if (ctx->trace_enable) {
337  bits[i] = 0;
338  ff_cbs_trace_syntax_element(ctx, position,
339  name, NULL, bits, value);
340  }
341 
342  *write_to = value;
343  return 0;
344 }
345 
347  uint32_t range_min, uint32_t range_max,
348  const char *name, uint32_t value)
349 {
350  int len;
351 
352  av_assert0(range_min <= range_max && range_max - range_min < 32);
353  if (value < range_min || value > range_max) {
354  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
355  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
356  name, value, range_min, range_max);
357  return AVERROR_INVALIDDATA;
358  }
359 
360  if (value == range_max)
361  len = range_max - range_min;
362  else
363  len = value - range_min + 1;
364  if (put_bits_left(pbc) < len)
365  return AVERROR(ENOSPC);
366 
367  if (ctx->trace_enable) {
368  char bits[33];
369  int i;
370  for (i = 0; i < len; i++) {
371  if (range_min + i == value)
372  bits[i] = '0';
373  else
374  bits[i] = '1';
375  }
376  bits[i] = 0;
378  name, NULL, bits, value);
379  }
380 
381  if (len > 0)
382  put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
383 
384  return 0;
385 }
386 
388  uint32_t range_max, const char *name,
389  const int *subscripts, uint32_t *write_to)
390 {
391  uint32_t value;
392  int position, err;
393  uint32_t max_len, len, range_offset, range_bits;
394 
395  if (ctx->trace_enable)
396  position = get_bits_count(gbc);
397 
398  av_assert0(range_max > 0);
399  max_len = av_log2(range_max - 1) - 3;
400 
401  err = cbs_av1_read_increment(ctx, gbc, 0, max_len,
402  "subexp_more_bits", &len);
403  if (err < 0)
404  return err;
405 
406  if (len) {
407  range_bits = 2 + len;
408  range_offset = 1 << range_bits;
409  } else {
410  range_bits = 3;
411  range_offset = 0;
412  }
413 
414  if (len < max_len) {
415  err = ff_cbs_read_unsigned(ctx, gbc, range_bits,
416  "subexp_bits", NULL, &value,
417  0, MAX_UINT_BITS(range_bits));
418  if (err < 0)
419  return err;
420 
421  } else {
422  err = cbs_av1_read_ns(ctx, gbc, range_max - range_offset,
423  "subexp_final_bits", NULL, &value);
424  if (err < 0)
425  return err;
426  }
427  value += range_offset;
428 
429  if (ctx->trace_enable)
430  ff_cbs_trace_syntax_element(ctx, position,
431  name, subscripts, "", value);
432 
433  *write_to = value;
434  return err;
435 }
436 
438  uint32_t range_max, const char *name,
439  const int *subscripts, uint32_t value)
440 {
441  int position, err;
442  uint32_t max_len, len, range_offset, range_bits;
443 
444  if (value > range_max) {
445  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
446  "%"PRIu32", but must be in [0,%"PRIu32"].\n",
447  name, value, range_max);
448  return AVERROR_INVALIDDATA;
449  }
450 
451  if (ctx->trace_enable)
452  position = put_bits_count(pbc);
453 
454  av_assert0(range_max > 0);
455  max_len = av_log2(range_max - 1) - 3;
456 
457  if (value < 8) {
458  range_bits = 3;
459  range_offset = 0;
460  len = 0;
461  } else {
462  range_bits = av_log2(value);
463  len = range_bits - 2;
464  if (len > max_len) {
465  // The top bin is combined with the one below it.
466  av_assert0(len == max_len + 1);
467  --range_bits;
468  len = max_len;
469  }
470  range_offset = 1 << range_bits;
471  }
472 
473  err = cbs_av1_write_increment(ctx, pbc, 0, max_len,
474  "subexp_more_bits", len);
475  if (err < 0)
476  return err;
477 
478  if (len < max_len) {
479  err = ff_cbs_write_unsigned(ctx, pbc, range_bits,
480  "subexp_bits", NULL,
481  value - range_offset,
482  0, MAX_UINT_BITS(range_bits));
483  if (err < 0)
484  return err;
485 
486  } else {
487  err = cbs_av1_write_ns(ctx, pbc, range_max - range_offset,
488  "subexp_final_bits", NULL,
489  value - range_offset);
490  if (err < 0)
491  return err;
492  }
493 
494  if (ctx->trace_enable)
495  ff_cbs_trace_syntax_element(ctx, position,
496  name, subscripts, "", value);
497 
498  return err;
499 }
500 
501 
502 static int cbs_av1_tile_log2(int blksize, int target)
503 {
504  int k;
505  for (k = 0; (blksize << k) < target; k++);
506  return k;
507 }
508 
510  unsigned int a, unsigned int b)
511 {
512  unsigned int diff, m;
513  if (!seq->enable_order_hint)
514  return 0;
515  diff = a - b;
516  m = 1 << seq->order_hint_bits_minus_1;
517  diff = (diff & (m - 1)) - (diff & m);
518  return diff;
519 }
520 
522 {
523  GetBitContext tmp = *gbc;
524  size_t size = 0;
525  for (int i = 0; get_bits_left(&tmp) >= 8; i++) {
526  if (get_bits(&tmp, 8))
527  size = i;
528  }
529  return size;
530 }
531 
532 
533 #define HEADER(name) do { \
534  ff_cbs_trace_header(ctx, name); \
535  } while (0)
536 
537 #define CHECK(call) do { \
538  err = (call); \
539  if (err < 0) \
540  return err; \
541  } while (0)
542 
543 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
544 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
545 #define FUNC(name) FUNC_AV1(READWRITE, name)
546 
547 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
548 
549 #define fb(width, name) \
550  xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
551 #define fc(width, name, range_min, range_max) \
552  xf(width, name, current->name, range_min, range_max, 0)
553 #define flag(name) fb(1, name)
554 #define su(width, name) \
555  xsu(width, name, current->name, 0)
556 
557 #define fbs(width, name, subs, ...) \
558  xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
559 #define fcs(width, name, range_min, range_max, subs, ...) \
560  xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
561 #define flags(name, subs, ...) \
562  xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
563 #define sus(width, name, subs, ...) \
564  xsu(width, name, current->name, subs, __VA_ARGS__)
565 
566 #define fixed(width, name, value) do { \
567  av_unused uint32_t fixed_value = value; \
568  xf(width, name, fixed_value, value, value, 0); \
569  } while (0)
570 
571 
572 #define READ
573 #define READWRITE read
574 #define RWContext GetBitContext
575 
576 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
577  uint32_t value = range_min; \
578  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
579  SUBSCRIPTS(subs, __VA_ARGS__), \
580  &value, range_min, range_max)); \
581  var = value; \
582  } while (0)
583 
584 #define xsu(width, name, var, subs, ...) do { \
585  int32_t value = 0; \
586  CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
587  SUBSCRIPTS(subs, __VA_ARGS__), &value, \
588  MIN_INT_BITS(width), \
589  MAX_INT_BITS(width))); \
590  var = value; \
591  } while (0)
592 
593 #define uvlc(name, range_min, range_max) do { \
594  uint32_t value = range_min; \
595  CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
596  &value, range_min, range_max)); \
597  current->name = value; \
598  } while (0)
599 
600 #define ns(max_value, name, subs, ...) do { \
601  uint32_t value = 0; \
602  CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
603  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
604  current->name = value; \
605  } while (0)
606 
607 #define increment(name, min, max) do { \
608  uint32_t value = 0; \
609  CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
610  current->name = value; \
611  } while (0)
612 
613 #define subexp(name, max, subs, ...) do { \
614  uint32_t value = 0; \
615  CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
616  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
617  current->name = value; \
618  } while (0)
619 
620 #define delta_q(name) do { \
621  uint8_t delta_coded; \
622  int8_t delta_q; \
623  xf(1, name.delta_coded, delta_coded, 0, 1, 0); \
624  if (delta_coded) \
625  xsu(1 + 6, name.delta_q, delta_q, 0); \
626  else \
627  delta_q = 0; \
628  current->name = delta_q; \
629  } while (0)
630 
631 #define leb128(name) do { \
632  uint64_t value = 0; \
633  CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
634  current->name = value; \
635  } while (0)
636 
637 #define infer(name, value) do { \
638  current->name = value; \
639  } while (0)
640 
641 #define byte_alignment(rw) (get_bits_count(rw) % 8)
642 
643 #include "cbs_av1_syntax_template.c"
644 
645 #undef READ
646 #undef READWRITE
647 #undef RWContext
648 #undef xf
649 #undef xsu
650 #undef uvlc
651 #undef ns
652 #undef increment
653 #undef subexp
654 #undef delta_q
655 #undef leb128
656 #undef infer
657 #undef byte_alignment
658 
659 
660 #define WRITE
661 #define READWRITE write
662 #define RWContext PutBitContext
663 
664 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
665  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
666  SUBSCRIPTS(subs, __VA_ARGS__), \
667  var, range_min, range_max)); \
668  } while (0)
669 
670 #define xsu(width, name, var, subs, ...) do { \
671  CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
672  SUBSCRIPTS(subs, __VA_ARGS__), var, \
673  MIN_INT_BITS(width), \
674  MAX_INT_BITS(width))); \
675  } while (0)
676 
677 #define uvlc(name, range_min, range_max) do { \
678  CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
679  range_min, range_max)); \
680  } while (0)
681 
682 #define ns(max_value, name, subs, ...) do { \
683  CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
684  SUBSCRIPTS(subs, __VA_ARGS__), \
685  current->name)); \
686  } while (0)
687 
688 #define increment(name, min, max) do { \
689  CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
690  current->name)); \
691  } while (0)
692 
693 #define subexp(name, max, subs, ...) do { \
694  CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
695  SUBSCRIPTS(subs, __VA_ARGS__), \
696  current->name)); \
697  } while (0)
698 
699 #define delta_q(name) do { \
700  xf(1, name.delta_coded, current->name != 0, 0, 1, 0); \
701  if (current->name) \
702  xsu(1 + 6, name.delta_q, current->name, 0); \
703  } while (0)
704 
705 #define leb128(name) do { \
706  CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \
707  } while (0)
708 
709 #define infer(name, value) do { \
710  if (current->name != (value)) { \
711  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
712  "%s does not match inferred value: " \
713  "%"PRId64", but should be %"PRId64".\n", \
714  #name, (int64_t)current->name, (int64_t)(value)); \
715  } \
716  } while (0)
717 
718 #define byte_alignment(rw) (put_bits_count(rw) % 8)
719 
720 #include "cbs_av1_syntax_template.c"
721 
722 #undef WRITE
723 #undef READWRITE
724 #undef RWContext
725 #undef xf
726 #undef xsu
727 #undef uvlc
728 #undef ns
729 #undef increment
730 #undef subexp
731 #undef delta_q
732 #undef leb128
733 #undef infer
734 #undef byte_alignment
735 
736 
739  int header)
740 {
741  GetBitContext gbc;
742  uint8_t *data;
743  size_t size;
744  uint64_t obu_length;
745  int pos, err, trace;
746 
747  // Don't include this parsing in trace output.
748  trace = ctx->trace_enable;
749  ctx->trace_enable = 0;
750 
751  data = frag->data;
752  size = frag->data_size;
753 
754  if (INT_MAX / 8 < size) {
755  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: "
756  "too large (%"SIZE_SPECIFIER" bytes).\n", size);
757  err = AVERROR_INVALIDDATA;
758  goto fail;
759  }
760 
761  while (size > 0) {
763  uint64_t obu_size;
764 
765  init_get_bits(&gbc, data, 8 * size);
766 
767  err = cbs_av1_read_obu_header(ctx, &gbc, &header);
768  if (err < 0)
769  goto fail;
770 
771  if (get_bits_left(&gbc) < 8) {
772  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment "
773  "too short (%"SIZE_SPECIFIER" bytes).\n", size);
774  err = AVERROR_INVALIDDATA;
775  goto fail;
776  }
777 
778  if (header.obu_has_size_field) {
779  err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
780  if (err < 0)
781  goto fail;
782  } else
783  obu_size = size - 1 - header.obu_extension_flag;
784 
785  pos = get_bits_count(&gbc);
786  av_assert0(pos % 8 == 0 && pos / 8 <= size);
787 
788  obu_length = pos / 8 + obu_size;
789 
790  if (size < obu_length) {
791  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
792  "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
793  obu_length, size);
794  err = AVERROR_INVALIDDATA;
795  goto fail;
796  }
797 
798  err = ff_cbs_insert_unit_data(ctx, frag, -1, header.obu_type,
799  data, obu_length, frag->data_ref);
800  if (err < 0)
801  goto fail;
802 
803  data += obu_length;
804  size -= obu_length;
805  }
806 
807  err = 0;
808 fail:
809  ctx->trace_enable = trace;
810  return err;
811 }
812 
814 {
816 }
817 
819 {
821 }
822 
824 {
825  switch (md->metadata_type) {
828  break;
829  }
830 }
831 
832 static void cbs_av1_free_obu(void *unit, uint8_t *content)
833 {
834  AV1RawOBU *obu = (AV1RawOBU*)content;
835 
836  switch (obu->header.obu_type) {
837  case AV1_OBU_TILE_GROUP:
839  break;
840  case AV1_OBU_FRAME:
842  break;
843  case AV1_OBU_TILE_LIST:
845  break;
846  case AV1_OBU_METADATA:
848  break;
849  case AV1_OBU_PADDING:
851  break;
852  }
853 
854  av_freep(&obu);
855 }
856 
858  CodedBitstreamUnit *unit,
859  GetBitContext *gbc,
861 {
862  int pos;
863 
864  pos = get_bits_count(gbc);
865  if (pos >= 8 * unit->data_size) {
866  av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before "
867  "any data in tile group (%d bits read).\n", pos);
868  return AVERROR_INVALIDDATA;
869  }
870  // Must be byte-aligned at this point.
871  av_assert0(pos % 8 == 0);
872 
873  td->data_ref = av_buffer_ref(unit->data_ref);
874  if (!td->data_ref)
875  return AVERROR(ENOMEM);
876 
877  td->data = unit->data + pos / 8;
878  td->data_size = unit->data_size - pos / 8;
879 
880  return 0;
881 }
882 
884  CodedBitstreamUnit *unit)
885 {
886  CodedBitstreamAV1Context *priv = ctx->priv_data;
887  AV1RawOBU *obu;
888  GetBitContext gbc;
889  int err, start_pos, end_pos;
890 
891  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*obu),
893  if (err < 0)
894  return err;
895  obu = unit->content;
896 
897  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
898  if (err < 0)
899  return err;
900 
901  err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header);
902  if (err < 0)
903  return err;
904  av_assert0(obu->header.obu_type == unit->type);
905 
906  if (obu->header.obu_has_size_field) {
907  uint64_t obu_size;
908  err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
909  if (err < 0)
910  return err;
911  obu->obu_size = obu_size;
912  } else {
913  if (unit->data_size < 1 + obu->header.obu_extension_flag) {
914  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
915  "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size);
916  return AVERROR_INVALIDDATA;
917  }
918  obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
919  }
920 
921  start_pos = get_bits_count(&gbc);
922 
923  if (obu->header.obu_extension_flag) {
924  priv->temporal_id = obu->header.temporal_id;
925  priv->spatial_id = obu->header.spatial_id;
926 
929  priv->operating_point_idc) {
930  int in_temporal_layer =
931  (priv->operating_point_idc >> priv->temporal_id ) & 1;
932  int in_spatial_layer =
933  (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1;
934  if (!in_temporal_layer || !in_spatial_layer) {
935  // Decoding will drop this OBU at this operating point.
936  }
937  }
938  } else {
939  priv->temporal_id = 0;
940  priv->spatial_id = 0;
941  }
942 
943  switch (obu->header.obu_type) {
945  {
946  err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
947  &obu->obu.sequence_header);
948  if (err < 0)
949  return err;
950 
952  priv->sequence_header = NULL;
953 
955  if (!priv->sequence_header_ref)
956  return AVERROR(ENOMEM);
957  priv->sequence_header = &obu->obu.sequence_header;
958  }
959  break;
961  {
962  err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
963  if (err < 0)
964  return err;
965  }
966  break;
969  {
970  err = cbs_av1_read_frame_header_obu(ctx, &gbc,
971  &obu->obu.frame_header,
972  obu->header.obu_type ==
974  unit->data_ref);
975  if (err < 0)
976  return err;
977  }
978  break;
979  case AV1_OBU_TILE_GROUP:
980  {
981  err = cbs_av1_read_tile_group_obu(ctx, &gbc,
982  &obu->obu.tile_group);
983  if (err < 0)
984  return err;
985 
986  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
987  &obu->obu.tile_group.tile_data);
988  if (err < 0)
989  return err;
990  }
991  break;
992  case AV1_OBU_FRAME:
993  {
994  err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame,
995  unit->data_ref);
996  if (err < 0)
997  return err;
998 
999  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1000  &obu->obu.frame.tile_group.tile_data);
1001  if (err < 0)
1002  return err;
1003  }
1004  break;
1005  case AV1_OBU_TILE_LIST:
1006  {
1007  err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1008  &obu->obu.tile_list);
1009  if (err < 0)
1010  return err;
1011 
1012  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1013  &obu->obu.tile_list.tile_data);
1014  if (err < 0)
1015  return err;
1016  }
1017  break;
1018  case AV1_OBU_METADATA:
1019  {
1020  err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata);
1021  if (err < 0)
1022  return err;
1023  }
1024  break;
1025  case AV1_OBU_PADDING:
1026  {
1027  err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->obu.padding);
1028  if (err < 0)
1029  return err;
1030  }
1031  break;
1032  default:
1033  return AVERROR(ENOSYS);
1034  }
1035 
1036  end_pos = get_bits_count(&gbc);
1037  av_assert0(end_pos <= unit->data_size * 8);
1038 
1039  if (obu->obu_size > 0 &&
1041  obu->header.obu_type != AV1_OBU_FRAME) {
1042  int nb_bits = obu->obu_size * 8 + start_pos - end_pos;
1043 
1044  if (nb_bits <= 0)
1045  return AVERROR_INVALIDDATA;
1046 
1047  err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
1048  if (err < 0)
1049  return err;
1050  }
1051 
1052  return 0;
1053 }
1054 
1056  CodedBitstreamUnit *unit,
1057  PutBitContext *pbc)
1058 {
1059  CodedBitstreamAV1Context *priv = ctx->priv_data;
1060  AV1RawOBU *obu = unit->content;
1061  PutBitContext pbc_tmp;
1062  AV1RawTileData *td;
1063  size_t header_size;
1064  int err, start_pos, end_pos, data_pos;
1065 
1066  // OBUs in the normal bitstream format must contain a size field
1067  // in every OBU (in annex B it is optional, but we don't support
1068  // writing that).
1069  obu->header.obu_has_size_field = 1;
1070 
1071  err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
1072  if (err < 0)
1073  return err;
1074 
1075  if (obu->header.obu_has_size_field) {
1076  pbc_tmp = *pbc;
1077  // Add space for the size field to fill later.
1078  put_bits32(pbc, 0);
1079  put_bits32(pbc, 0);
1080  }
1081 
1082  td = NULL;
1083  start_pos = put_bits_count(pbc);
1084 
1085  switch (obu->header.obu_type) {
1087  {
1088  err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1089  &obu->obu.sequence_header);
1090  if (err < 0)
1091  return err;
1092 
1094  priv->sequence_header = NULL;
1095 
1097  if (!priv->sequence_header_ref)
1098  return AVERROR(ENOMEM);
1099  priv->sequence_header = &obu->obu.sequence_header;
1100  }
1101  break;
1103  {
1104  err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1105  if (err < 0)
1106  return err;
1107  }
1108  break;
1109  case AV1_OBU_FRAME_HEADER:
1111  {
1112  err = cbs_av1_write_frame_header_obu(ctx, pbc,
1113  &obu->obu.frame_header,
1114  obu->header.obu_type ==
1116  NULL);
1117  if (err < 0)
1118  return err;
1119  }
1120  break;
1121  case AV1_OBU_TILE_GROUP:
1122  {
1123  err = cbs_av1_write_tile_group_obu(ctx, pbc,
1124  &obu->obu.tile_group);
1125  if (err < 0)
1126  return err;
1127 
1128  td = &obu->obu.tile_group.tile_data;
1129  }
1130  break;
1131  case AV1_OBU_FRAME:
1132  {
1133  err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
1134  if (err < 0)
1135  return err;
1136 
1137  td = &obu->obu.frame.tile_group.tile_data;
1138  }
1139  break;
1140  case AV1_OBU_TILE_LIST:
1141  {
1142  err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list);
1143  if (err < 0)
1144  return err;
1145 
1146  td = &obu->obu.tile_list.tile_data;
1147  }
1148  break;
1149  case AV1_OBU_METADATA:
1150  {
1151  err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
1152  if (err < 0)
1153  return err;
1154  }
1155  break;
1156  case AV1_OBU_PADDING:
1157  {
1158  err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding);
1159  if (err < 0)
1160  return err;
1161  }
1162  break;
1163  default:
1164  return AVERROR(ENOSYS);
1165  }
1166 
1167  end_pos = put_bits_count(pbc);
1168  header_size = (end_pos - start_pos + 7) / 8;
1169  if (td) {
1170  obu->obu_size = header_size + td->data_size;
1171  } else if (header_size > 0) {
1172  // Add trailing bits and recalculate.
1173  err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1174  if (err < 0)
1175  return err;
1176  end_pos = put_bits_count(pbc);
1177  obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
1178  } else {
1179  // Empty OBU.
1180  obu->obu_size = 0;
1181  }
1182 
1183  end_pos = put_bits_count(pbc);
1184  // Must now be byte-aligned.
1185  av_assert0(end_pos % 8 == 0);
1186  flush_put_bits(pbc);
1187  start_pos /= 8;
1188  end_pos /= 8;
1189 
1190  *pbc = pbc_tmp;
1191  err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size);
1192  if (err < 0)
1193  return err;
1194 
1195  data_pos = put_bits_count(pbc) / 8;
1196  flush_put_bits(pbc);
1197  av_assert0(data_pos <= start_pos);
1198 
1199  if (8 * obu->obu_size > put_bits_left(pbc))
1200  return AVERROR(ENOSPC);
1201 
1202  if (obu->obu_size > 0) {
1203  memmove(priv->write_buffer + data_pos,
1204  priv->write_buffer + start_pos, header_size);
1205  skip_put_bytes(pbc, header_size);
1206 
1207  if (td) {
1208  memcpy(priv->write_buffer + data_pos + header_size,
1209  td->data, td->data_size);
1210  skip_put_bytes(pbc, td->data_size);
1211  }
1212  }
1213 
1214  return 0;
1215 }
1216 
1218  CodedBitstreamUnit *unit)
1219 {
1220  CodedBitstreamAV1Context *priv = ctx->priv_data;
1221  PutBitContext pbc;
1222  int err;
1223 
1224  if (!priv->write_buffer) {
1225  // Initial write buffer size is 1MB.
1226  priv->write_buffer_size = 1024 * 1024;
1227 
1228  reallocate_and_try_again:
1229  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1230  if (err < 0) {
1231  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1232  "sufficiently large write buffer (last attempt "
1233  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1234  return err;
1235  }
1236  }
1237 
1238  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1239 
1240  err = cbs_av1_write_obu(ctx, unit, &pbc);
1241  if (err == AVERROR(ENOSPC)) {
1242  // Overflow.
1243  priv->write_buffer_size *= 2;
1244  goto reallocate_and_try_again;
1245  }
1246  if (err < 0)
1247  return err;
1248 
1249  // Overflow but we didn't notice.
1250  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1251 
1252  // OBU data must be byte-aligned.
1253  av_assert0(put_bits_count(&pbc) % 8 == 0);
1254 
1255  unit->data_size = put_bits_count(&pbc) / 8;
1256  flush_put_bits(&pbc);
1257 
1258  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1259  if (err < 0)
1260  return err;
1261 
1262  memcpy(unit->data, priv->write_buffer, unit->data_size);
1263 
1264  return 0;
1265 }
1266 
1268  CodedBitstreamFragment *frag)
1269 {
1270  size_t size, pos;
1271  int i;
1272 
1273  size = 0;
1274  for (i = 0; i < frag->nb_units; i++)
1275  size += frag->units[i].data_size;
1276 
1278  if (!frag->data_ref)
1279  return AVERROR(ENOMEM);
1280  frag->data = frag->data_ref->data;
1281  memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1282 
1283  pos = 0;
1284  for (i = 0; i < frag->nb_units; i++) {
1285  memcpy(frag->data + pos, frag->units[i].data,
1286  frag->units[i].data_size);
1287  pos += frag->units[i].data_size;
1288  }
1289  av_assert0(pos == size);
1290  frag->data_size = size;
1291 
1292  return 0;
1293 }
1294 
1296 {
1297  CodedBitstreamAV1Context *priv = ctx->priv_data;
1298 
1301 
1302  av_freep(&priv->write_buffer);
1303 }
1304 
1307 
1308  .priv_data_size = sizeof(CodedBitstreamAV1Context),
1309 
1314 
1315  .close = &cbs_av1_close,
1316 };
const char * name
Definition: avisynth_c.h:867
AV1RawMetadata metadata
Definition: cbs_av1.h:398
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:250
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void cbs_av1_free_obu(void *unit, uint8_t *content)
Definition: cbs_av1.c:832
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
int size
size_t obu_size
Definition: cbs_av1.h:390
AV1RawTileList tile_list
Definition: cbs_av1.h:397
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
AVBufferRef * payload_ref
Definition: cbs_av1.h:350
static void cbs_av1_free_metadata(AV1RawMetadata *md)
Definition: cbs_av1.c:823
AVBufferRef * sequence_header_ref
Definition: cbs_av1.h:420
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
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:28
static void cbs_av1_close(CodedBitstreamContext *ctx)
Definition: cbs_av1.c:1295
const char * b
Definition: vf_curves.c:116
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
Definition: cbs.c:588
uint8_t order_hint_bits_minus_1
Definition: cbs_av1.h:122
int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition: cbs.c:472
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:346
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:206
AV1RawTileData tile_data
Definition: cbs_av1.h:309
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:106
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_internal.h:53
uint8_t
uint8_t * data
Definition: cbs_av1.h:286
static int cbs_av1_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_av1.c:883
uint8_t * write_buffer
Definition: cbs_av1.h:447
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:96
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition: cbs.c:429
const char data[16]
Definition: mxf.c:91
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:609
Coded bitstream unit structure.
Definition: cbs.h:64
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:75
static void cbs_av1_free_padding(AV1RawPadding *pd)
Definition: cbs_av1.c:818
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:703
#define av_log(a,...)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
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:210
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static void cbs_av1_free_tile_data(AV1RawTileData *td)
Definition: cbs_av1.c:813
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define td
Definition: regdef.h:70
void(* close)(CodedBitstreamContext *ctx)
Definition: cbs_internal.h:57
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
AV1RawTileData tile_data
Definition: cbs_av1.h:296
uint8_t temporal_id
Definition: cbs_av1.h:36
AVBufferRef * frame_header_ref
Definition: cbs_av1.h:423
#define AVERROR(e)
Definition: error.h:43
union AV1RawOBU::@48 obu
AV1RawFrame frame
Definition: cbs_av1.h:395
union AV1RawMetadata::@47 metadata
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:387
simple assert() macros that are a bit more flexible than ISO C assert().
uint8_t bits
Definition: vp3data.h:202
#define fail()
Definition: checkasm.h:120
static int cbs_av1_tile_log2(int blksize, int target)
Definition: cbs_av1.c:502
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:173
const CodedBitstreamType ff_cbs_type_av1
Definition: cbs_av1.c:1305
uint8_t obu_extension_flag
Definition: cbs_av1.h:32
AVBufferRef * payload_ref
Definition: cbs_av1.h:383
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:48
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:333
#define md
#define FFMIN(a, b)
Definition: common.h:96
static int cbs_av1_write_obu(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_av1.c:1055
static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, uint64_t value)
Definition: cbs_av1.c:180
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_av1.c:1267
uint8_t w
Definition: llviddspenc.c:38
AV1RawOBUHeader header
Definition: cbs_av1.h:388
AVFormatContext * ctx
Definition: movenc.c:48
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_av1.c:737
int n
Definition: avisynth_c.h:760
static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, GetBitContext *gbc, AV1RawTileData *td)
Definition: cbs_av1.c:857
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
size_t data_size
Definition: cbs_av1.h:287
#define av_log2
Definition: intmath.h:83
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
uint8_t * data
The data buffer.
Definition: buffer.h:89
static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
Definition: cbs_av1.c:521
uint8_t enable_order_hint
Definition: cbs_av1.h:113
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
double value
Definition: eval.c:98
Context structure for coded bitstream operations.
Definition: cbs.h:168
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:437
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:106
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
AV1RawTileGroup tile_group
Definition: cbs_av1.h:301
uint8_t obu_type
Definition: cbs_av1.h:31
enum AVCodecID codec_id
Definition: cbs_internal.h:29
AV1RawSequenceHeader * sequence_header
Definition: cbs_av1.h:419
uint8_t spatial_id
Definition: cbs_av1.h:37
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:44
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
static int cbs_av1_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_av1.c:1217
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:260
AV1RawSequenceHeader sequence_header
Definition: cbs_av1.h:393
#define SIZE_SPECIFIER
Definition: internal.h:262
AVBufferRef * data_ref
Definition: cbs_av1.h:288
void * priv_data
Internal codec-specific data.
Definition: cbs.h:189
AV1RawMetadataITUTT35 itut_t35
Definition: cbs_av1.h:375
AV1RawPadding padding
Definition: cbs_av1.h:399
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_internal.h:38
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:92
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:790
static av_always_inline int diff(const uint32_t a, const uint32_t b)
pixel format definitions
uint64_t metadata_type
Definition: cbs_av1.h:370
int len
uint8_t obu_has_size_field
Definition: cbs_av1.h:33
AV1RawTileGroup tile_group
Definition: cbs_av1.h:396
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:309
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:394
#define av_freep(p)
static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, uint64_t *write_to)
Definition: cbs_av1.c:150
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
Definition: cbs_av1.c:509
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:139
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *str, const int *subscripts, const char *bits, int64_t value)
Definition: cbs.c:379
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:80
static uint8_t tmp[11]
Definition: aes_ctr.c:26