FFmpeg  4.1.5
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  int width, const char *name,
212  const int *subscripts, int32_t *write_to)
213 {
214  int position;
215  int32_t value;
216 
217  if (ctx->trace_enable)
218  position = get_bits_count(gbc);
219 
220  if (get_bits_left(gbc) < width) {
221  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid signed value at "
222  "%s: bitstream ended.\n", name);
223  return AVERROR_INVALIDDATA;
224  }
225 
226  value = get_sbits(gbc, width);
227 
228  if (ctx->trace_enable) {
229  char bits[33];
230  int i;
231  for (i = 0; i < width; i++)
232  bits[i] = value & (1 << (width - i - 1)) ? '1' : '0';
233  bits[i] = 0;
234 
235  ff_cbs_trace_syntax_element(ctx, position,
236  name, subscripts, bits, value);
237  }
238 
239  *write_to = value;
240  return 0;
241 }
242 
244  int width, const char *name,
245  const int *subscripts, int32_t value)
246 {
247  if (put_bits_left(pbc) < width)
248  return AVERROR(ENOSPC);
249 
250  if (ctx->trace_enable) {
251  char bits[33];
252  int i;
253  for (i = 0; i < width; i++)
254  bits[i] = value & (1 << (width - i - 1)) ? '1' : '0';
255  bits[i] = 0;
256 
258  name, subscripts, bits, value);
259  }
260 
261  put_sbits(pbc, width, value);
262 
263  return 0;
264 }
265 
267  uint32_t n, const char *name,
268  const int *subscripts, uint32_t *write_to)
269 {
270  uint32_t w, m, v, extra_bit, value;
271  int position;
272 
273  av_assert0(n > 0);
274 
275  if (ctx->trace_enable)
276  position = get_bits_count(gbc);
277 
278  w = av_log2(n) + 1;
279  m = (1 << w) - n;
280 
281  if (get_bits_left(gbc) < w) {
282  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid non-symmetric value at "
283  "%s: bitstream ended.\n", name);
284  return AVERROR_INVALIDDATA;
285  }
286 
287  if (w - 1 > 0)
288  v = get_bits(gbc, w - 1);
289  else
290  v = 0;
291 
292  if (v < m) {
293  value = v;
294  } else {
295  extra_bit = get_bits1(gbc);
296  value = (v << 1) - m + extra_bit;
297  }
298 
299  if (ctx->trace_enable) {
300  char bits[33];
301  int i;
302  for (i = 0; i < w - 1; i++)
303  bits[i] = (v >> i & 1) ? '1' : '0';
304  if (v >= m)
305  bits[i++] = extra_bit ? '1' : '0';
306  bits[i] = 0;
307 
308  ff_cbs_trace_syntax_element(ctx, position,
309  name, subscripts, bits, value);
310  }
311 
312  *write_to = value;
313  return 0;
314 }
315 
317  uint32_t n, const char *name,
318  const int *subscripts, uint32_t value)
319 {
320  uint32_t w, m, v, extra_bit;
321  int position;
322 
323  if (value > n) {
324  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
325  "%"PRIu32", but must be in [0,%"PRIu32"].\n",
326  name, value, n);
327  return AVERROR_INVALIDDATA;
328  }
329 
330  if (ctx->trace_enable)
331  position = put_bits_count(pbc);
332 
333  w = av_log2(n) + 1;
334  m = (1 << w) - n;
335 
336  if (put_bits_left(pbc) < w)
337  return AVERROR(ENOSPC);
338 
339  if (value < m) {
340  v = value;
341  put_bits(pbc, w - 1, v);
342  } else {
343  v = m + ((value - m) >> 1);
344  extra_bit = (value - m) & 1;
345  put_bits(pbc, w - 1, v);
346  put_bits(pbc, 1, extra_bit);
347  }
348 
349  if (ctx->trace_enable) {
350  char bits[33];
351  int i;
352  for (i = 0; i < w - 1; i++)
353  bits[i] = (v >> i & 1) ? '1' : '0';
354  if (value >= m)
355  bits[i++] = extra_bit ? '1' : '0';
356  bits[i] = 0;
357 
358  ff_cbs_trace_syntax_element(ctx, position,
359  name, subscripts, bits, value);
360  }
361 
362  return 0;
363 }
364 
366  uint32_t range_min, uint32_t range_max,
367  const char *name, uint32_t *write_to)
368 {
369  uint32_t value;
370  int position, i;
371  char bits[33];
372 
373  av_assert0(range_min <= range_max && range_max - range_min < sizeof(bits) - 1);
374  if (ctx->trace_enable)
375  position = get_bits_count(gbc);
376 
377  for (i = 0, value = range_min; value < range_max;) {
378  if (get_bits_left(gbc) < 1) {
379  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
380  "%s: bitstream ended.\n", name);
381  return AVERROR_INVALIDDATA;
382  }
383  if (get_bits1(gbc)) {
384  bits[i++] = '1';
385  ++value;
386  } else {
387  bits[i++] = '0';
388  break;
389  }
390  }
391 
392  if (ctx->trace_enable) {
393  bits[i] = 0;
394  ff_cbs_trace_syntax_element(ctx, position,
395  name, NULL, bits, value);
396  }
397 
398  *write_to = value;
399  return 0;
400 }
401 
403  uint32_t range_min, uint32_t range_max,
404  const char *name, uint32_t value)
405 {
406  int len;
407 
408  av_assert0(range_min <= range_max && range_max - range_min < 32);
409  if (value < range_min || value > range_max) {
410  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
411  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
412  name, value, range_min, range_max);
413  return AVERROR_INVALIDDATA;
414  }
415 
416  if (value == range_max)
417  len = range_max - range_min;
418  else
419  len = value - range_min + 1;
420  if (put_bits_left(pbc) < len)
421  return AVERROR(ENOSPC);
422 
423  if (ctx->trace_enable) {
424  char bits[33];
425  int i;
426  for (i = 0; i < len; i++) {
427  if (range_min + i == value)
428  bits[i] = '0';
429  else
430  bits[i] = '1';
431  }
432  bits[i] = 0;
434  name, NULL, bits, value);
435  }
436 
437  if (len > 0)
438  put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
439 
440  return 0;
441 }
442 
444  uint32_t range_max, const char *name,
445  const int *subscripts, uint32_t *write_to)
446 {
447  uint32_t value;
448  int position, err;
449  uint32_t max_len, len, range_offset, range_bits;
450 
451  if (ctx->trace_enable)
452  position = get_bits_count(gbc);
453 
454  av_assert0(range_max > 0);
455  max_len = av_log2(range_max - 1) - 3;
456 
457  err = cbs_av1_read_increment(ctx, gbc, 0, max_len,
458  "subexp_more_bits", &len);
459  if (err < 0)
460  return err;
461 
462  if (len) {
463  range_bits = 2 + len;
464  range_offset = 1 << range_bits;
465  } else {
466  range_bits = 3;
467  range_offset = 0;
468  }
469 
470  if (len < max_len) {
471  err = ff_cbs_read_unsigned(ctx, gbc, range_bits,
472  "subexp_bits", NULL, &value,
473  0, MAX_UINT_BITS(range_bits));
474  if (err < 0)
475  return err;
476 
477  } else {
478  err = cbs_av1_read_ns(ctx, gbc, range_max - range_offset,
479  "subexp_final_bits", NULL, &value);
480  if (err < 0)
481  return err;
482  }
483  value += range_offset;
484 
485  if (ctx->trace_enable)
486  ff_cbs_trace_syntax_element(ctx, position,
487  name, subscripts, "", value);
488 
489  *write_to = value;
490  return err;
491 }
492 
494  uint32_t range_max, const char *name,
495  const int *subscripts, uint32_t value)
496 {
497  int position, err;
498  uint32_t max_len, len, range_offset, range_bits;
499 
500  if (value > range_max) {
501  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
502  "%"PRIu32", but must be in [0,%"PRIu32"].\n",
503  name, value, range_max);
504  return AVERROR_INVALIDDATA;
505  }
506 
507  if (ctx->trace_enable)
508  position = put_bits_count(pbc);
509 
510  av_assert0(range_max > 0);
511  max_len = av_log2(range_max - 1) - 3;
512 
513  if (value < 8) {
514  range_bits = 3;
515  range_offset = 0;
516  len = 0;
517  } else {
518  range_bits = av_log2(value);
519  len = range_bits - 2;
520  if (len > max_len) {
521  // The top bin is combined with the one below it.
522  av_assert0(len == max_len + 1);
523  --range_bits;
524  len = max_len;
525  }
526  range_offset = 1 << range_bits;
527  }
528 
529  err = cbs_av1_write_increment(ctx, pbc, 0, max_len,
530  "subexp_more_bits", len);
531  if (err < 0)
532  return err;
533 
534  if (len < max_len) {
535  err = ff_cbs_write_unsigned(ctx, pbc, range_bits,
536  "subexp_bits", NULL,
537  value - range_offset,
538  0, MAX_UINT_BITS(range_bits));
539  if (err < 0)
540  return err;
541 
542  } else {
543  err = cbs_av1_write_ns(ctx, pbc, range_max - range_offset,
544  "subexp_final_bits", NULL,
545  value - range_offset);
546  if (err < 0)
547  return err;
548  }
549 
550  if (ctx->trace_enable)
551  ff_cbs_trace_syntax_element(ctx, position,
552  name, subscripts, "", value);
553 
554  return err;
555 }
556 
557 
558 static int cbs_av1_tile_log2(int blksize, int target)
559 {
560  int k;
561  for (k = 0; (blksize << k) < target; k++);
562  return k;
563 }
564 
566  unsigned int a, unsigned int b)
567 {
568  unsigned int diff, m;
569  if (!seq->enable_order_hint)
570  return 0;
571  diff = a - b;
572  m = 1 << seq->order_hint_bits_minus_1;
573  diff = (diff & (m - 1)) - (diff & m);
574  return diff;
575 }
576 
577 
578 #define HEADER(name) do { \
579  ff_cbs_trace_header(ctx, name); \
580  } while (0)
581 
582 #define CHECK(call) do { \
583  err = (call); \
584  if (err < 0) \
585  return err; \
586  } while (0)
587 
588 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
589 #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
590 #define FUNC(name) FUNC_AV1(READWRITE, name)
591 
592 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
593 
594 #define fb(width, name) \
595  xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
596 #define fc(width, name, range_min, range_max) \
597  xf(width, name, current->name, range_min, range_max, 0)
598 #define flag(name) fb(1, name)
599 #define su(width, name) \
600  xsu(width, name, current->name, 0)
601 
602 #define fbs(width, name, subs, ...) \
603  xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
604 #define fcs(width, name, range_min, range_max, subs, ...) \
605  xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
606 #define flags(name, subs, ...) \
607  xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
608 #define sus(width, name, subs, ...) \
609  xsu(width, name, current->name, subs, __VA_ARGS__)
610 
611 #define fixed(width, name, value) do { \
612  av_unused uint32_t fixed_value = value; \
613  xf(width, name, fixed_value, value, value, 0); \
614  } while (0)
615 
616 
617 #define READ
618 #define READWRITE read
619 #define RWContext GetBitContext
620 
621 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
622  uint32_t value = range_min; \
623  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
624  SUBSCRIPTS(subs, __VA_ARGS__), \
625  &value, range_min, range_max)); \
626  var = value; \
627  } while (0)
628 
629 #define xsu(width, name, var, subs, ...) do { \
630  int32_t value = 0; \
631  CHECK(cbs_av1_read_su(ctx, rw, width, #name, \
632  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
633  var = value; \
634  } while (0)
635 
636 #define uvlc(name, range_min, range_max) do { \
637  uint32_t value = range_min; \
638  CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
639  &value, range_min, range_max)); \
640  current->name = value; \
641  } while (0)
642 
643 #define ns(max_value, name, subs, ...) do { \
644  uint32_t value = 0; \
645  CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
646  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
647  current->name = value; \
648  } while (0)
649 
650 #define increment(name, min, max) do { \
651  uint32_t value = 0; \
652  CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
653  current->name = value; \
654  } while (0)
655 
656 #define subexp(name, max, subs, ...) do { \
657  uint32_t value = 0; \
658  CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
659  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
660  current->name = value; \
661  } while (0)
662 
663 #define delta_q(name) do { \
664  uint8_t delta_coded; \
665  int8_t delta_q; \
666  xf(1, name.delta_coded, delta_coded, 0, 1, 0); \
667  if (delta_coded) \
668  xsu(1 + 6, name.delta_q, delta_q, 0); \
669  else \
670  delta_q = 0; \
671  current->name = delta_q; \
672  } while (0)
673 
674 #define leb128(name) do { \
675  uint64_t value = 0; \
676  CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
677  current->name = value; \
678  } while (0)
679 
680 #define infer(name, value) do { \
681  current->name = value; \
682  } while (0)
683 
684 #define byte_alignment(rw) (get_bits_count(rw) % 8)
685 
686 #include "cbs_av1_syntax_template.c"
687 
688 #undef READ
689 #undef READWRITE
690 #undef RWContext
691 #undef xf
692 #undef xsu
693 #undef uvlc
694 #undef leb128
695 #undef ns
696 #undef increment
697 #undef subexp
698 #undef delta_q
699 #undef leb128
700 #undef infer
701 #undef byte_alignment
702 
703 
704 #define WRITE
705 #define READWRITE write
706 #define RWContext PutBitContext
707 
708 #define xf(width, name, var, range_min, range_max, subs, ...) do { \
709  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
710  SUBSCRIPTS(subs, __VA_ARGS__), \
711  var, range_min, range_max)); \
712  } while (0)
713 
714 #define xsu(width, name, var, subs, ...) do { \
715  CHECK(cbs_av1_write_su(ctx, rw, width, #name, \
716  SUBSCRIPTS(subs, __VA_ARGS__), var)); \
717  } while (0)
718 
719 #define uvlc(name, range_min, range_max) do { \
720  CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
721  range_min, range_max)); \
722  } while (0)
723 
724 #define ns(max_value, name, subs, ...) do { \
725  CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
726  SUBSCRIPTS(subs, __VA_ARGS__), \
727  current->name)); \
728  } while (0)
729 
730 #define increment(name, min, max) do { \
731  CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
732  current->name)); \
733  } while (0)
734 
735 #define subexp(name, max, subs, ...) do { \
736  CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
737  SUBSCRIPTS(subs, __VA_ARGS__), \
738  current->name)); \
739  } while (0)
740 
741 #define delta_q(name) do { \
742  xf(1, name.delta_coded, current->name != 0, 0, 1, 0); \
743  if (current->name) \
744  xsu(1 + 6, name.delta_q, current->name, 0); \
745  } while (0)
746 
747 #define leb128(name) do { \
748  CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \
749  } while (0)
750 
751 #define infer(name, value) do { \
752  if (current->name != (value)) { \
753  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
754  "%s does not match inferred value: " \
755  "%"PRId64", but should be %"PRId64".\n", \
756  #name, (int64_t)current->name, (int64_t)(value)); \
757  } \
758  } while (0)
759 
760 #define byte_alignment(rw) (put_bits_count(rw) % 8)
761 
762 #include "cbs_av1_syntax_template.c"
763 
764 #undef READ
765 #undef READWRITE
766 #undef RWContext
767 #undef xf
768 #undef xsu
769 #undef uvlc
770 #undef leb128
771 #undef ns
772 #undef increment
773 #undef subexp
774 #undef delta_q
775 #undef infer
776 #undef byte_alignment
777 
778 
781  int header)
782 {
783  GetBitContext gbc;
784  uint8_t *data;
785  size_t size;
786  uint64_t obu_length;
787  int pos, err, trace;
788 
789  // Don't include this parsing in trace output.
790  trace = ctx->trace_enable;
791  ctx->trace_enable = 0;
792 
793  data = frag->data;
794  size = frag->data_size;
795 
796  if (INT_MAX / 8 < size) {
797  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: "
798  "too large (%"SIZE_SPECIFIER" bytes).\n", size);
799  err = AVERROR_INVALIDDATA;
800  goto fail;
801  }
802 
803  while (size > 0) {
805  uint64_t obu_size;
806 
807  init_get_bits(&gbc, data, 8 * size);
808 
809  err = cbs_av1_read_obu_header(ctx, &gbc, &header);
810  if (err < 0)
811  goto fail;
812 
813  if (!header.obu_has_size_field) {
814  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU for raw "
815  "stream: size field must be present.\n");
816  err = AVERROR_INVALIDDATA;
817  goto fail;
818  }
819 
820  if (get_bits_left(&gbc) < 8) {
821  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment "
822  "too short (%"SIZE_SPECIFIER" bytes).\n", size);
823  err = AVERROR_INVALIDDATA;
824  goto fail;
825  }
826 
827  err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
828  if (err < 0)
829  goto fail;
830 
831  pos = get_bits_count(&gbc);
832  av_assert0(pos % 8 == 0 && pos / 8 <= size);
833 
834  obu_length = pos / 8 + obu_size;
835 
836  if (size < obu_length) {
837  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
838  "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
839  obu_length, size);
840  err = AVERROR_INVALIDDATA;
841  goto fail;
842  }
843 
844  err = ff_cbs_insert_unit_data(ctx, frag, -1, header.obu_type,
845  data, obu_length, frag->data_ref);
846  if (err < 0)
847  goto fail;
848 
849  data += obu_length;
850  size -= obu_length;
851  }
852 
853  err = 0;
854 fail:
855  ctx->trace_enable = trace;
856  return err;
857 }
858 
860 {
862 }
863 
865 {
866  switch (md->metadata_type) {
869  break;
870  }
871 }
872 
873 static void cbs_av1_free_obu(void *unit, uint8_t *content)
874 {
875  AV1RawOBU *obu = (AV1RawOBU*)content;
876 
877  switch (obu->header.obu_type) {
878  case AV1_OBU_TILE_GROUP:
880  break;
881  case AV1_OBU_FRAME:
883  break;
884  case AV1_OBU_TILE_LIST:
886  break;
887  case AV1_OBU_METADATA:
889  break;
890  }
891 
892  av_freep(&obu);
893 }
894 
896  CodedBitstreamUnit *unit,
897  GetBitContext *gbc,
899 {
900  int pos;
901 
902  pos = get_bits_count(gbc);
903  if (pos >= 8 * unit->data_size) {
904  av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before "
905  "any data in tile group (%d bits read).\n", pos);
906  return AVERROR_INVALIDDATA;
907  }
908  // Must be byte-aligned at this point.
909  av_assert0(pos % 8 == 0);
910 
911  td->data_ref = av_buffer_ref(unit->data_ref);
912  if (!td->data_ref)
913  return AVERROR(ENOMEM);
914 
915  td->data = unit->data + pos / 8;
916  td->data_size = unit->data_size - pos / 8;
917 
918  return 0;
919 }
920 
922  CodedBitstreamUnit *unit)
923 {
924  CodedBitstreamAV1Context *priv = ctx->priv_data;
925  AV1RawOBU *obu;
926  GetBitContext gbc;
927  int err, start_pos, end_pos;
928 
929  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*obu),
931  if (err < 0)
932  return err;
933  obu = unit->content;
934 
935  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
936  if (err < 0)
937  return err;
938 
939  err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header);
940  if (err < 0)
941  return err;
942  av_assert0(obu->header.obu_type == unit->type);
943 
944  if (obu->header.obu_has_size_field) {
945  uint64_t obu_size;
946  err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
947  if (err < 0)
948  return err;
949  obu->obu_size = obu_size;
950  } else {
951  if (unit->data_size < 1 + obu->header.obu_extension_flag) {
952  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
953  "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size);
954  return AVERROR_INVALIDDATA;
955  }
956  obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
957  }
958 
959  start_pos = get_bits_count(&gbc);
960 
961  if (obu->header.obu_extension_flag) {
962  priv->temporal_id = obu->header.temporal_id;
963  priv->spatial_id = obu->header.spatial_id;
964 
967  priv->operating_point_idc) {
968  int in_temporal_layer =
969  (priv->operating_point_idc >> priv->temporal_id ) & 1;
970  int in_spatial_layer =
971  (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1;
972  if (!in_temporal_layer || !in_spatial_layer) {
973  // Decoding will drop this OBU at this operating point.
974  }
975  }
976  } else {
977  priv->temporal_id = 0;
978  priv->spatial_id = 0;
979  }
980 
981  priv->ref = (AV1ReferenceFrameState *)&priv->read_ref;
982 
983  switch (obu->header.obu_type) {
985  {
986  err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
987  &obu->obu.sequence_header);
988  if (err < 0)
989  return err;
990 
992  priv->sequence_header = NULL;
993 
995  if (!priv->sequence_header_ref)
996  return AVERROR(ENOMEM);
997  priv->sequence_header = &obu->obu.sequence_header;
998  }
999  break;
1001  {
1002  err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
1003  if (err < 0)
1004  return err;
1005  }
1006  break;
1007  case AV1_OBU_FRAME_HEADER:
1009  {
1010  err = cbs_av1_read_frame_header_obu(ctx, &gbc,
1011  &obu->obu.frame_header,
1012  obu->header.obu_type ==
1014  unit->data_ref);
1015  if (err < 0)
1016  return err;
1017  }
1018  break;
1019  case AV1_OBU_TILE_GROUP:
1020  {
1021  err = cbs_av1_read_tile_group_obu(ctx, &gbc,
1022  &obu->obu.tile_group);
1023  if (err < 0)
1024  return err;
1025 
1026  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1027  &obu->obu.tile_group.tile_data);
1028  if (err < 0)
1029  return err;
1030  }
1031  break;
1032  case AV1_OBU_FRAME:
1033  {
1034  err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame,
1035  unit->data_ref);
1036  if (err < 0)
1037  return err;
1038 
1039  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1040  &obu->obu.frame.tile_group.tile_data);
1041  if (err < 0)
1042  return err;
1043  }
1044  break;
1045  case AV1_OBU_TILE_LIST:
1046  {
1047  err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1048  &obu->obu.tile_list);
1049  if (err < 0)
1050  return err;
1051 
1052  err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1053  &obu->obu.tile_list.tile_data);
1054  if (err < 0)
1055  return err;
1056  }
1057  break;
1058  case AV1_OBU_METADATA:
1059  {
1060  err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata);
1061  if (err < 0)
1062  return err;
1063  }
1064  break;
1065  case AV1_OBU_PADDING:
1066  default:
1067  return AVERROR(ENOSYS);
1068  }
1069 
1070  end_pos = get_bits_count(&gbc);
1071  av_assert0(end_pos <= unit->data_size * 8);
1072 
1073  if (obu->obu_size > 0 &&
1075  obu->header.obu_type != AV1_OBU_TILE_LIST &&
1076  obu->header.obu_type != AV1_OBU_FRAME) {
1077  int nb_bits = obu->obu_size * 8 + start_pos - end_pos;
1078 
1079  if (nb_bits <= 0)
1080  return AVERROR_INVALIDDATA;
1081 
1082  err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
1083  if (err < 0)
1084  return err;
1085  }
1086 
1087  return 0;
1088 }
1089 
1091  CodedBitstreamUnit *unit,
1092  PutBitContext *pbc)
1093 {
1094  CodedBitstreamAV1Context *priv = ctx->priv_data;
1095  AV1RawOBU *obu = unit->content;
1096  PutBitContext pbc_tmp;
1097  AV1RawTileData *td;
1098  size_t header_size;
1099  int err, start_pos, end_pos, data_pos;
1100 
1101  // OBUs in the normal bitstream format must contain a size field
1102  // in every OBU (in annex B it is optional, but we don't support
1103  // writing that).
1104  obu->header.obu_has_size_field = 1;
1105 
1106  err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
1107  if (err < 0)
1108  return err;
1109 
1110  if (obu->header.obu_has_size_field) {
1111  pbc_tmp = *pbc;
1112  // Add space for the size field to fill later.
1113  put_bits32(pbc, 0);
1114  put_bits32(pbc, 0);
1115  }
1116 
1117  td = NULL;
1118  start_pos = put_bits_count(pbc);
1119 
1120  priv->ref = (AV1ReferenceFrameState *)&priv->write_ref;
1121 
1122  switch (obu->header.obu_type) {
1124  {
1125  err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1126  &obu->obu.sequence_header);
1127  if (err < 0)
1128  return err;
1129 
1131  priv->sequence_header = NULL;
1132 
1134  if (!priv->sequence_header_ref)
1135  return AVERROR(ENOMEM);
1136  priv->sequence_header = &obu->obu.sequence_header;
1137  }
1138  break;
1140  {
1141  err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1142  if (err < 0)
1143  return err;
1144  }
1145  break;
1146  case AV1_OBU_FRAME_HEADER:
1148  {
1149  err = cbs_av1_write_frame_header_obu(ctx, pbc,
1150  &obu->obu.frame_header,
1151  obu->header.obu_type ==
1153  NULL);
1154  if (err < 0)
1155  return err;
1156  }
1157  break;
1158  case AV1_OBU_TILE_GROUP:
1159  {
1160  err = cbs_av1_write_tile_group_obu(ctx, pbc,
1161  &obu->obu.tile_group);
1162  if (err < 0)
1163  return err;
1164 
1165  td = &obu->obu.tile_group.tile_data;
1166  }
1167  break;
1168  case AV1_OBU_FRAME:
1169  {
1170  err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
1171  if (err < 0)
1172  return err;
1173 
1174  td = &obu->obu.frame.tile_group.tile_data;
1175  }
1176  break;
1177  case AV1_OBU_TILE_LIST:
1178  {
1179  err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list);
1180  if (err < 0)
1181  return err;
1182 
1183  td = &obu->obu.tile_list.tile_data;
1184  }
1185  break;
1186  case AV1_OBU_METADATA:
1187  {
1188  err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
1189  if (err < 0)
1190  return err;
1191  }
1192  break;
1193  case AV1_OBU_PADDING:
1194  default:
1195  return AVERROR(ENOSYS);
1196  }
1197 
1198  end_pos = put_bits_count(pbc);
1199  header_size = (end_pos - start_pos + 7) / 8;
1200  if (td) {
1201  obu->obu_size = header_size + td->data_size;
1202  } else if (header_size > 0) {
1203  // Add trailing bits and recalculate.
1204  err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1205  if (err < 0)
1206  return err;
1207  end_pos = put_bits_count(pbc);
1208  obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
1209  } else {
1210  // Empty OBU.
1211  obu->obu_size = 0;
1212  }
1213 
1214  end_pos = put_bits_count(pbc);
1215  // Must now be byte-aligned.
1216  av_assert0(end_pos % 8 == 0);
1217  flush_put_bits(pbc);
1218  start_pos /= 8;
1219  end_pos /= 8;
1220 
1221  *pbc = pbc_tmp;
1222  err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size);
1223  if (err < 0)
1224  return err;
1225 
1226  data_pos = put_bits_count(pbc) / 8;
1227  flush_put_bits(pbc);
1228  av_assert0(data_pos <= start_pos);
1229 
1230  if (8 * obu->obu_size > put_bits_left(pbc))
1231  return AVERROR(ENOSPC);
1232 
1233  if (obu->obu_size > 0) {
1234  memmove(priv->write_buffer + data_pos,
1235  priv->write_buffer + start_pos, header_size);
1236  skip_put_bytes(pbc, header_size);
1237 
1238  if (td) {
1239  memcpy(priv->write_buffer + data_pos + header_size,
1240  td->data, td->data_size);
1241  skip_put_bytes(pbc, td->data_size);
1242  }
1243  }
1244 
1245  return 0;
1246 }
1247 
1249  CodedBitstreamUnit *unit)
1250 {
1251  CodedBitstreamAV1Context *priv = ctx->priv_data;
1252  PutBitContext pbc;
1253  int err;
1254 
1255  if (!priv->write_buffer) {
1256  // Initial write buffer size is 1MB.
1257  priv->write_buffer_size = 1024 * 1024;
1258 
1259  reallocate_and_try_again:
1260  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1261  if (err < 0) {
1262  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1263  "sufficiently large write buffer (last attempt "
1264  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
1265  return err;
1266  }
1267  }
1268 
1269  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1270 
1271  err = cbs_av1_write_obu(ctx, unit, &pbc);
1272  if (err == AVERROR(ENOSPC)) {
1273  // Overflow.
1274  priv->write_buffer_size *= 2;
1275  goto reallocate_and_try_again;
1276  }
1277  if (err < 0)
1278  return err;
1279 
1280  // Overflow but we didn't notice.
1281  av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1282 
1283  // OBU data must be byte-aligned.
1284  av_assert0(put_bits_count(&pbc) % 8 == 0);
1285 
1286  unit->data_size = put_bits_count(&pbc) / 8;
1287  flush_put_bits(&pbc);
1288 
1289  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
1290  if (err < 0)
1291  return err;
1292 
1293  memcpy(unit->data, priv->write_buffer, unit->data_size);
1294 
1295  return 0;
1296 }
1297 
1299  CodedBitstreamFragment *frag)
1300 {
1301  size_t size, pos;
1302  int i;
1303 
1304  size = 0;
1305  for (i = 0; i < frag->nb_units; i++)
1306  size += frag->units[i].data_size;
1307 
1309  if (!frag->data_ref)
1310  return AVERROR(ENOMEM);
1311  frag->data = frag->data_ref->data;
1312  memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1313 
1314  pos = 0;
1315  for (i = 0; i < frag->nb_units; i++) {
1316  memcpy(frag->data + pos, frag->units[i].data,
1317  frag->units[i].data_size);
1318  pos += frag->units[i].data_size;
1319  }
1320  av_assert0(pos == size);
1321  frag->data_size = size;
1322 
1323  return 0;
1324 }
1325 
1327 {
1328  CodedBitstreamAV1Context *priv = ctx->priv_data;
1329 
1332 
1333  av_freep(&priv->write_buffer);
1334 }
1335 
1338 
1339  .priv_data_size = sizeof(CodedBitstreamAV1Context),
1340 
1345 
1346  .close = &cbs_av1_close,
1347 };
const char * name
Definition: avisynth_c.h:775
AV1RawMetadata metadata
Definition: cbs_av1.h:379
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:873
AV1ReferenceFrameState write_ref[AV1_NUM_REF_FRAMES]
Definition: cbs_av1.h:426
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:371
AV1RawTileList tile_list
Definition: cbs_av1.h:378
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:240
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:381
AVBufferRef * payload_ref
Definition: cbs_av1.h:337
static void cbs_av1_free_metadata(AV1RawMetadata *md)
Definition: cbs_av1.c:864
static int cbs_av1_write_su(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, int32_t value)
Definition: cbs_av1.c:243
AVBufferRef * sequence_header_ref
Definition: cbs_av1.h:400
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:1326
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:585
uint8_t order_hint_bits_minus_1
Definition: cbs_av1.h:122
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:361
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:469
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:402
#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:197
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:921
uint8_t * write_buffer
Definition: cbs_av1.h:429
union AV1RawMetadata::@46 metadata
#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:426
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:606
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.
Definition: cbs.h:153
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:75
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:686
#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:266
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:814
static void cbs_av1_free_tile_data(AV1RawTileData *td)
Definition: cbs_av1.c:859
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#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:403
#define AVERROR(e)
Definition: error.h:43
AV1RawFrame frame
Definition: cbs_av1.h:376
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:443
simple assert() macros that are a bit more flexible than ISO C assert().
static int cbs_av1_read_su(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, int32_t *write_to)
Definition: cbs_av1.c:210
#define fail()
Definition: checkasm.h:117
static int cbs_av1_tile_log2(int blksize, int target)
Definition: cbs_av1.c:558
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:164
const CodedBitstreamType ff_cbs_type_av1
Definition: cbs_av1.c:1336
uint8_t obu_extension_flag
Definition: cbs_av1.h:32
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:1090
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:1298
#define width
AV1ReferenceFrameState * ref
Definition: cbs_av1.h:424
uint8_t w
Definition: llviddspenc.c:38
AV1RawOBUHeader header
Definition: cbs_av1.h:369
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_av1.c:779
int n
Definition: avisynth_c.h:684
static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, GetBitContext *gbc, AV1RawTileData *td)
Definition: cbs_av1.c:895
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
AV1ReferenceFrameState read_ref[AV1_NUM_REF_FRAMES]
Definition: cbs_av1.h:425
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
uint8_t * data
The data buffer.
Definition: buffer.h:89
uint8_t enable_order_hint
Definition: cbs_av1.h:113
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:487
double value
Definition: eval.c:98
Context structure for coded bitstream operations.
Definition: cbs.h:159
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:493
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:615
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:399
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:531
static int cbs_av1_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_av1.c:1248
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:316
AV1RawSequenceHeader sequence_header
Definition: cbs_av1.h:374
#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:180
AV1RawMetadataITUTT35 itut_t35
Definition: cbs_av1.h:362
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:782
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:357
int len
uint8_t obu_has_size_field
Definition: cbs_av1.h:33
AV1RawTileGroup tile_group
Definition: cbs_av1.h:377
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:365
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:375
#define av_freep(p)
union AV1RawOBU::@47 obu
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:565
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:376
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:80