FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_av1.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 362 546 66.3%
Functions: 17 21 81.0%
Branches: 186 359 51.8%

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