FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_av1.c
Date: 2021-09-22 23:55:11
Exec Total Coverage
Lines: 355 581 61.1%
Branches: 170 364 46.7%

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 "internal.h"
27
28
29 static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc,
30 const char *name, uint32_t *write_to,
31 uint32_t range_min, uint32_t range_max)
32 {
33 uint32_t zeroes, bits_value, value;
34 int position;
35
36 if (ctx->trace_enable)
37 position = get_bits_count(gbc);
38
39 zeroes = 0;
40 while (1) {
41 if (get_bits_left(gbc) < 1) {
42 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
43 "%s: bitstream ended.\n", name);
44 return AVERROR_INVALIDDATA;
45 }
46
47 if (get_bits1(gbc))
48 break;
49 ++zeroes;
50 }
51
52 if (zeroes >= 32) {
53 value = MAX_UINT_BITS(32);
54 } else {
55 if (get_bits_left(gbc) < zeroes) {
56 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
57 "%s: bitstream ended.\n", name);
58 return AVERROR_INVALIDDATA;
59 }
60
61 bits_value = get_bits_long(gbc, zeroes);
62 value = bits_value + (UINT32_C(1) << zeroes) - 1;
63 }
64
65 if (ctx->trace_enable) {
66 char bits[65];
67 int i, j, k;
68
69 if (zeroes >= 32) {
70 while (zeroes > 32) {
71 k = FFMIN(zeroes - 32, 32);
72 for (i = 0; i < k; i++)
73 bits[i] = '0';
74 bits[i] = 0;
75 ff_cbs_trace_syntax_element(ctx, position, name,
76 NULL, bits, 0);
77 zeroes -= k;
78 position += k;
79 }
80 }
81
82 for (i = 0; i < zeroes; i++)
83 bits[i] = '0';
84 bits[i++] = '1';
85
86 if (zeroes < 32) {
87 for (j = 0; j < zeroes; j++)
88 bits[i++] = (bits_value >> (zeroes - j - 1) & 1) ? '1' : '0';
89 }
90
91 bits[i] = 0;
92 ff_cbs_trace_syntax_element(ctx, position, name,
93 NULL, bits, value);
94 }
95
96 if (value < range_min || value > range_max) {
97 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
98 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
99 name, value, range_min, range_max);
100 return AVERROR_INVALIDDATA;
101 }
102
103 *write_to = value;
104 return 0;
105 }
106
107 static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc,
108 const char *name, uint32_t value,
109 uint32_t range_min, uint32_t range_max)
110 {
111 uint32_t v;
112 int position, zeroes;
113
114 if (value < range_min || value > range_max) {
115 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
116 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
117 name, value, range_min, range_max);
118 return AVERROR_INVALIDDATA;
119 }
120
121 if (ctx->trace_enable)
122 position = put_bits_count(pbc);
123
124 zeroes = av_log2(value + 1);
125 v = value - (1U << zeroes) + 1;
126 put_bits(pbc, zeroes, 0);
127 put_bits(pbc, 1, 1);
128 put_bits(pbc, zeroes, v);
129
130 if (ctx->trace_enable) {
131 char bits[65];
132 int i, j;
133 i = 0;
134 for (j = 0; j < zeroes; j++)
135 bits[i++] = '0';
136 bits[i++] = '1';
137 for (j = 0; j < zeroes; j++)
138 bits[i++] = (v >> (zeroes - j - 1) & 1) ? '1' : '0';
139 bits[i++] = 0;
140 ff_cbs_trace_syntax_element(ctx, position, name, NULL,
141 bits, value);
142 }
143
144 return 0;
145 }
146
147 6034 static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc,
148 const char *name, uint64_t *write_to)
149 {
150 uint64_t value;
151 int position, err, i;
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (ctx->trace_enable)
154 position = get_bits_count(gbc);
155
156 6034 value = 0;
157
1/2
✓ Branch 0 taken 8538 times.
✗ Branch 1 not taken.
8538 for (i = 0; i < 8; i++) {
158 8538 int subscript[2] = { 1, i };
159 uint32_t byte;
160 8538 err = ff_cbs_read_unsigned(ctx, gbc, 8, "leb128_byte[i]", subscript,
161 &byte, 0x00, 0xff);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8538 times.
8538 if (err < 0)
163 return err;
164
165 8538 value |= (uint64_t)(byte & 0x7f) << (i * 7);
166
2/2
✓ Branch 0 taken 6034 times.
✓ Branch 1 taken 2504 times.
8538 if (!(byte & 0x80))
167 6034 break;
168 }
169
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (value > UINT32_MAX)
171 return AVERROR_INVALIDDATA;
172
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (ctx->trace_enable)
174 ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
175
176 6034 *write_to = value;
177 6034 return 0;
178 }
179
180 1029 static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc,
181 const char *name, uint64_t value)
182 {
183 int position, err, len, i;
184 uint8_t byte;
185
186 1029 len = (av_log2(value) + 7) / 7;
187
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (ctx->trace_enable)
189 position = put_bits_count(pbc);
190
191
2/2
✓ Branch 0 taken 1448 times.
✓ Branch 1 taken 1029 times.
2477 for (i = 0; i < len; i++) {
192 1448 int subscript[2] = { 1, i };
193
194 1448 byte = value >> (7 * i) & 0x7f;
195
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 1029 times.
1448 if (i < len - 1)
196 419 byte |= 0x80;
197
198 1448 err = ff_cbs_write_unsigned(ctx, pbc, 8, "leb128_byte[i]", subscript,
199 byte, 0x00, 0xff);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1448 times.
1448 if (err < 0)
201 return err;
202 }
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (ctx->trace_enable)
205 ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
206
207 1029 return 0;
208 }
209
210 390 static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc,
211 uint32_t n, const char *name,
212 const int *subscripts, uint32_t *write_to)
213 {
214 uint32_t m, v, extra_bit, value;
215 int position, w;
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 390 times.
390 av_assert0(n > 0);
218
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 390 times.
390 if (ctx->trace_enable)
220 position = get_bits_count(gbc);
221
222 390 w = av_log2(n) + 1;
223 390 m = (1 << w) - n;
224
225
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 390 times.
390 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
1/2
✓ Branch 0 taken 390 times.
✗ Branch 1 not taken.
390 if (w - 1 > 0)
232 390 v = get_bits(gbc, w - 1);
233 else
234 v = 0;
235
236
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 78 times.
390 if (v < m) {
237 312 value = v;
238 } else {
239 78 extra_bit = get_bits1(gbc);
240 78 value = (v << 1) - m + extra_bit;
241 }
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 390 times.
390 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 390 *write_to = value;
257 390 return 0;
258 }
259
260 130 static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc,
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (ctx->trace_enable)
275 position = put_bits_count(pbc);
276
277 130 w = av_log2(n) + 1;
278 130 m = (1 << w) - n;
279
280
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 130 times.
130 if (put_bits_left(pbc) < w)
281 return AVERROR(ENOSPC);
282
283
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 26 times.
130 if (value < m) {
284 104 v = value;
285 104 put_bits(pbc, w - 1, v);
286 } else {
287 26 v = m + ((value - m) >> 1);
288 26 extra_bit = (value - m) & 1;
289 26 put_bits(pbc, w - 1, v);
290 26 put_bits(pbc, 1, extra_bit);
291 }
292
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 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 130 return 0;
307 }
308
309 4117 static int cbs_av1_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc,
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
2/4
✓ Branch 0 taken 4117 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4117 times.
4117 av_assert0(range_min <= range_max && range_max - range_min < sizeof(bits) - 1);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4117 times.
4117 if (ctx->trace_enable)
319 position = get_bits_count(gbc);
320
321
2/2
✓ Branch 0 taken 6322 times.
✓ Branch 1 taken 619 times.
6941 for (i = 0, value = range_min; value < range_max;) {
322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6322 times.
6322 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
2/2
✓ Branch 1 taken 2824 times.
✓ Branch 2 taken 3498 times.
6322 if (get_bits1(gbc)) {
328 2824 bits[i++] = '1';
329 2824 ++value;
330 } else {
331 3498 bits[i++] = '0';
332 3498 break;
333 }
334 }
335
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4117 times.
4117 if (ctx->trace_enable) {
337 bits[i] = 0;
338 ff_cbs_trace_syntax_element(ctx, position,
339 name, NULL, bits, value);
340 }
341
342 4117 *write_to = value;
343 4117 return 0;
344 }
345
346 1317 static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc,
347 uint32_t range_min, uint32_t range_max,
348 const char *name, uint32_t value)
349 {
350 int len;
351
352
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);
353
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) {
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
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 1114 times.
1317 if (value == range_max)
361 203 len = range_max - range_min;
362 else
363 1114 len = value - range_min + 1;
364
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1317 times.
1317 if (put_bits_left(pbc) < len)
365 return AVERROR(ENOSPC);
366
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1317 times.
1317 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;
377 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
378 name, NULL, bits, value);
379 }
380
381
1/2
✓ Branch 0 taken 1317 times.
✗ Branch 1 not taken.
1317 if (len > 0)
382 1317 put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
383
384 1317 return 0;
385 }
386
387 308 static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc,
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (ctx->trace_enable)
396 position = get_bits_count(gbc);
397
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 av_assert0(range_max > 0);
399 308 max_len = av_log2(range_max - 1) - 3;
400
401 308 err = cbs_av1_read_increment(ctx, gbc, 0, max_len,
402 "subexp_more_bits", &len);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (err < 0)
404 return err;
405
406
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 15 times.
308 if (len) {
407 293 range_bits = 2 + len;
408 293 range_offset = 1 << range_bits;
409 } else {
410 15 range_bits = 3;
411 15 range_offset = 0;
412 }
413
414
1/2
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
308 if (len < max_len) {
415 308 err = ff_cbs_read_unsigned(ctx, gbc, range_bits,
416 "subexp_bits", NULL, &value,
417 308 0, MAX_UINT_BITS(range_bits));
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 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 308 value += range_offset;
428
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (ctx->trace_enable)
430 ff_cbs_trace_syntax_element(ctx, position,
431 name, subscripts, "", value);
432
433 308 *write_to = value;
434 308 return err;
435 }
436
437 60 static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc,
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (ctx->trace_enable)
452 position = put_bits_count(pbc);
453
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 av_assert0(range_max > 0);
455 60 max_len = av_log2(range_max - 1) - 3;
456
457
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 55 times.
60 if (value < 8) {
458 5 range_bits = 3;
459 5 range_offset = 0;
460 5 len = 0;
461 } else {
462 55 range_bits = av_log2(value);
463 55 len = range_bits - 2;
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 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 55 range_offset = 1 << range_bits;
471 }
472
473 60 err = cbs_av1_write_increment(ctx, pbc, 0, max_len,
474 "subexp_more_bits", len);
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (err < 0)
476 return err;
477
478
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (len < max_len) {
479 60 err = ff_cbs_write_unsigned(ctx, pbc, range_bits,
480 "subexp_bits", NULL,
481 value - range_offset,
482 60 0, MAX_UINT_BITS(range_bits));
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (ctx->trace_enable)
495 ff_cbs_trace_syntax_element(ctx, position,
496 name, subscripts, "", value);
497
498 60 return err;
499 }
500
501
502 8565 static int cbs_av1_tile_log2(int blksize, int target)
503 {
504 int k;
505
2/2
✓ Branch 0 taken 8866 times.
✓ Branch 1 taken 8565 times.
17431 for (k = 0; (blksize << k) < target; k++);
506 8565 return k;
507 }
508
509 12634 static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq,
510 unsigned int a, unsigned int b)
511 {
512 unsigned int diff, m;
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12634 times.
12634 if (!seq->enable_order_hint)
514 return 0;
515 12634 diff = a - b;
516 12634 m = 1 << seq->order_hint_bits_minus_1;
517 12634 diff = (diff & (m - 1)) - (diff & m);
518 12634 return diff;
519 }
520
521 static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
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; \
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; \
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; \
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; \
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; \
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; \
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; \
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_ERROR, \
712 "%s does not match inferred value: " \
713 "%"PRId64", but should be %"PRId64".\n", \
714 #name, (int64_t)current->name, (int64_t)(value)); \
715 return AVERROR_INVALIDDATA; \
716 } \
717 } while (0)
718
719 #define byte_alignment(rw) (put_bits_count(rw) % 8)
720
721 #include "cbs_av1_syntax_template.c"
722
723 #undef WRITE
724 #undef READWRITE
725 #undef RWContext
726 #undef xf
727 #undef xsu
728 #undef uvlc
729 #undef ns
730 #undef increment
731 #undef subexp
732 #undef delta_q
733 #undef leb128
734 #undef infer
735 #undef byte_alignment
736
737
738 1094 static int cbs_av1_split_fragment(CodedBitstreamContext *ctx,
739 CodedBitstreamFragment *frag,
740 int header)
741 {
742 GetBitContext gbc;
743 uint8_t *data;
744 size_t size;
745 uint64_t obu_length;
746 int pos, err, trace;
747
748 // Don't include this parsing in trace output.
749 1094 trace = ctx->trace_enable;
750 1094 ctx->trace_enable = 0;
751
752 1094 data = frag->data;
753 1094 size = frag->data_size;
754
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1094 times.
1094 if (INT_MAX / 8 < size) {
756 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: "
757 "too large (%"SIZE_SPECIFIER" bytes).\n", size);
758 err = AVERROR_INVALIDDATA;
759 goto fail;
760 }
761
762
5/6
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1054 times.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 36 times.
1094 if (header && size && data[0] & 0x80) {
763 // first bit is nonzero, the extradata does not consist purely of
764 // OBUs. Expect MP4/Matroska AV1CodecConfigurationRecord
765 4 int config_record_version = data[0] & 0x7f;
766
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (config_record_version != 1) {
768 av_log(ctx->log_ctx, AV_LOG_ERROR,
769 "Unknown version %d of AV1CodecConfigurationRecord "
770 "found!\n",
771 config_record_version);
772 err = AVERROR_INVALIDDATA;
773 goto fail;
774 }
775
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (size <= 4) {
777 if (size < 4) {
778 av_log(ctx->log_ctx, AV_LOG_WARNING,
779 "Undersized AV1CodecConfigurationRecord v%d found!\n",
780 config_record_version);
781 err = AVERROR_INVALIDDATA;
782 goto fail;
783 }
784
785 goto success;
786 }
787
788 // In AV1CodecConfigurationRecord v1, actual OBUs start after
789 // four bytes. Thus set the offset as required for properly
790 // parsing them.
791 4 data += 4;
792 4 size -= 4;
793 }
794
795
2/2
✓ Branch 0 taken 3035 times.
✓ Branch 1 taken 1094 times.
4129 while (size > 0) {
796 AV1RawOBUHeader header;
797 uint64_t obu_size;
798
799 3035 init_get_bits(&gbc, data, 8 * size);
800
801 3035 err = cbs_av1_read_obu_header(ctx, &gbc, &header);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3035 times.
3035 if (err < 0)
803 goto fail;
804
805
2/2
✓ Branch 0 taken 3013 times.
✓ Branch 1 taken 22 times.
3035 if (header.obu_has_size_field) {
806
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3013 times.
3013 if (get_bits_left(&gbc) < 8) {
807 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment "
808 "too short (%"SIZE_SPECIFIER" bytes).\n", size);
809 err = AVERROR_INVALIDDATA;
810 goto fail;
811 }
812 3013 err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3013 times.
3013 if (err < 0)
814 goto fail;
815 } else
816 22 obu_size = size - 1 - header.obu_extension_flag;
817
818 3035 pos = get_bits_count(&gbc);
819
2/4
✓ Branch 0 taken 3035 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3035 times.
3035 av_assert0(pos % 8 == 0 && pos / 8 <= size);
820
821 3035 obu_length = pos / 8 + obu_size;
822
823
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3035 times.
3035 if (size < obu_length) {
824 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
825 "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
826 obu_length, size);
827 err = AVERROR_INVALIDDATA;
828 goto fail;
829 }
830
831 3035 err = ff_cbs_insert_unit_data(frag, -1, header.obu_type,
832 data, obu_length, frag->data_ref);
833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3035 times.
3035 if (err < 0)
834 goto fail;
835
836 3035 data += obu_length;
837 3035 size -= obu_length;
838 }
839
840 1094 success:
841 1094 err = 0;
842 1094 fail:
843 1094 ctx->trace_enable = trace;
844 1094 return err;
845 }
846
847 1323 static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx,
848 CodedBitstreamUnit *unit,
849 GetBitContext *gbc,
850 AV1RawTileData *td)
851 {
852 int pos;
853
854 1323 pos = get_bits_count(gbc);
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1323 times.
1323 if (pos >= 8 * unit->data_size) {
856 av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before "
857 "any data in tile group (%d bits read).\n", pos);
858 return AVERROR_INVALIDDATA;
859 }
860 // Must be byte-aligned at this point.
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1323 times.
1323 av_assert0(pos % 8 == 0);
862
863 1323 td->data_ref = av_buffer_ref(unit->data_ref);
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1323 times.
1323 if (!td->data_ref)
865 return AVERROR(ENOMEM);
866
867 1323 td->data = unit->data + pos / 8;
868 1323 td->data_size = unit->data_size - pos / 8;
869
870 1323 return 0;
871 }
872
873 3023 static int cbs_av1_read_unit(CodedBitstreamContext *ctx,
874 CodedBitstreamUnit *unit)
875 {
876 3023 CodedBitstreamAV1Context *priv = ctx->priv_data;
877 AV1RawOBU *obu;
878 GetBitContext gbc;
879 int err, start_pos, end_pos;
880
881 3023 err = ff_cbs_alloc_unit_content2(ctx, unit);
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3023 times.
3023 if (err < 0)
883 return err;
884 3023 obu = unit->content;
885
886 3023 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3023 times.
3023 if (err < 0)
888 return err;
889
890 3023 err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3023 times.
3023 if (err < 0)
892 return err;
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3023 times.
3023 av_assert0(obu->header.obu_type == unit->type);
894
895
2/2
✓ Branch 0 taken 3001 times.
✓ Branch 1 taken 22 times.
3023 if (obu->header.obu_has_size_field) {
896 uint64_t obu_size;
897 3001 err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3001 times.
3001 if (err < 0)
899 return err;
900 3001 obu->obu_size = obu_size;
901 } else {
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (unit->data_size < 1 + obu->header.obu_extension_flag) {
903 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
904 "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size);
905 return AVERROR_INVALIDDATA;
906 }
907 22 obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
908 }
909
910 3023 start_pos = get_bits_count(&gbc);
911
912
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 2783 times.
3023 if (obu->header.obu_extension_flag) {
913
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
240 if (obu->header.obu_type != AV1_OBU_SEQUENCE_HEADER &&
914
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
240 obu->header.obu_type != AV1_OBU_TEMPORAL_DELIMITER &&
915
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
240 priv->operating_point_idc) {
916 80 int in_temporal_layer =
917 80 (priv->operating_point_idc >> priv->temporal_id ) & 1;
918 80 int in_spatial_layer =
919 80 (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1;
920
2/4
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
80 if (!in_temporal_layer || !in_spatial_layer) {
921 return AVERROR(EAGAIN); // drop_obu()
922 }
923 }
924 }
925
926
6/9
✓ Branch 0 taken 407 times.
✓ Branch 1 taken 1004 times.
✓ Branch 2 taken 269 times.
✓ Branch 3 taken 312 times.
✓ Branch 4 taken 1011 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
3023 switch (obu->header.obu_type) {
927 407 case AV1_OBU_SEQUENCE_HEADER:
928 {
929 407 err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
930 &obu->obu.sequence_header);
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
407 if (err < 0)
932 return err;
933
934
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 296 times.
407 if (priv->operating_point >= 0) {
935 111 AV1RawSequenceHeader *sequence_header = &obu->obu.sequence_header;
936
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111 times.
111 if (priv->operating_point > sequence_header->operating_points_cnt_minus_1) {
938 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid Operating Point %d requested. "
939 "Must not be higher than %u.\n",
940 priv->operating_point, sequence_header->operating_points_cnt_minus_1);
941 return AVERROR(EINVAL);
942 }
943 111 priv->operating_point_idc = sequence_header->operating_point_idc[priv->operating_point];
944 }
945
946 407 av_buffer_unref(&priv->sequence_header_ref);
947 407 priv->sequence_header = NULL;
948
949 407 priv->sequence_header_ref = av_buffer_ref(unit->content_ref);
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
407 if (!priv->sequence_header_ref)
951 return AVERROR(ENOMEM);
952 407 priv->sequence_header = &obu->obu.sequence_header;
953 }
954 407 break;
955 1004 case AV1_OBU_TEMPORAL_DELIMITER:
956 {
957 1004 err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1004 times.
1004 if (err < 0)
959 return err;
960 }
961 1004 break;
962 269 case AV1_OBU_FRAME_HEADER:
963 case AV1_OBU_REDUNDANT_FRAME_HEADER:
964 {
965 269 err = cbs_av1_read_frame_header_obu(ctx, &gbc,
966 &obu->obu.frame_header,
967 269 obu->header.obu_type ==
968 AV1_OBU_REDUNDANT_FRAME_HEADER,
969 unit->data_ref);
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (err < 0)
971 return err;
972 }
973 269 break;
974 312 case AV1_OBU_TILE_GROUP:
975 {
976 312 err = cbs_av1_read_tile_group_obu(ctx, &gbc,
977 &obu->obu.tile_group);
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 if (err < 0)
979 return err;
980
981 312 err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
982 &obu->obu.tile_group.tile_data);
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 if (err < 0)
984 return err;
985 }
986 312 break;
987 1011 case AV1_OBU_FRAME:
988 {
989 1011 err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame,
990 unit->data_ref);
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1011 times.
1011 if (err < 0)
992 return err;
993
994 1011 err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
995 &obu->obu.frame.tile_group.tile_data);
996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1011 times.
1011 if (err < 0)
997 return err;
998 }
999 1011 break;
1000 case AV1_OBU_TILE_LIST:
1001 {
1002 err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1003 &obu->obu.tile_list);
1004 if (err < 0)
1005 return err;
1006
1007 err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1008 &obu->obu.tile_list.tile_data);
1009 if (err < 0)
1010 return err;
1011 }
1012 break;
1013 20 case AV1_OBU_METADATA:
1014 {
1015 20 err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata);
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (err < 0)
1017 return err;
1018 }
1019 20 break;
1020 case AV1_OBU_PADDING:
1021 {
1022 err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->obu.padding);
1023 if (err < 0)
1024 return err;
1025 }
1026 break;
1027 default:
1028 return AVERROR(ENOSYS);
1029 }
1030
1031 3023 end_pos = get_bits_count(&gbc);
1032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3023 times.
3023 av_assert0(end_pos <= unit->data_size * 8);
1033
1034
2/2
✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 1004 times.
3023 if (obu->obu_size > 0 &&
1035
2/2
✓ Branch 0 taken 1707 times.
✓ Branch 1 taken 312 times.
2019 obu->header.obu_type != AV1_OBU_TILE_GROUP &&
1036
1/2
✓ Branch 0 taken 1707 times.
✗ Branch 1 not taken.
1707 obu->header.obu_type != AV1_OBU_TILE_LIST &&
1037
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 1011 times.
1707 obu->header.obu_type != AV1_OBU_FRAME) {
1038 696 int nb_bits = obu->obu_size * 8 + start_pos - end_pos;
1039
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
696 if (nb_bits <= 0)
1041 return AVERROR_INVALIDDATA;
1042
1043 696 err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
1044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
696 if (err < 0)
1045 return err;
1046 }
1047
1048 3023 return 0;
1049 }
1050
1051 1017 static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
1052 CodedBitstreamUnit *unit,
1053 PutBitContext *pbc)
1054 {
1055 1017 CodedBitstreamAV1Context *priv = ctx->priv_data;
1056 1017 AV1RawOBU *obu = unit->content;
1057 PutBitContext pbc_tmp;
1058 AV1RawTileData *td;
1059 size_t header_size;
1060 int err, start_pos, end_pos, data_pos;
1061
1062 // OBUs in the normal bitstream format must contain a size field
1063 // in every OBU (in annex B it is optional, but we don't support
1064 // writing that).
1065 1017 obu->header.obu_has_size_field = 1;
1066
1067 1017 err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
1068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1017 times.
1017 if (err < 0)
1069 return err;
1070
1071
1/2
✓ Branch 0 taken 1017 times.
✗ Branch 1 not taken.
1017 if (obu->header.obu_has_size_field) {
1072 1017 pbc_tmp = *pbc;
1073 // Add space for the size field to fill later.
1074 1017 put_bits32(pbc, 0);
1075 1017 put_bits32(pbc, 0);
1076 }
1077
1078 1017 td = NULL;
1079 1017 start_pos = put_bits_count(pbc);
1080
1081
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) {
1082 143 case AV1_OBU_SEQUENCE_HEADER:
1083 {
1084 143 err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1085 &obu->obu.sequence_header);
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 if (err < 0)
1087 return err;
1088
1089 143 av_buffer_unref(&priv->sequence_header_ref);
1090 143 priv->sequence_header = NULL;
1091
1092 143 err = ff_cbs_make_unit_refcounted(ctx, unit);
1093
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 if (err < 0)
1094 return err;
1095
1096 143 priv->sequence_header_ref = av_buffer_ref(unit->content_ref);
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 if (!priv->sequence_header_ref)
1098 return AVERROR(ENOMEM);
1099 143 priv->sequence_header = &obu->obu.sequence_header;
1100 }
1101 143 break;
1102 337 case AV1_OBU_TEMPORAL_DELIMITER:
1103 {
1104 337 err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (err < 0)
1106 return err;
1107 }
1108 337 break;
1109 87 case AV1_OBU_FRAME_HEADER:
1110 case AV1_OBU_REDUNDANT_FRAME_HEADER:
1111 {
1112 87 err = cbs_av1_write_frame_header_obu(ctx, pbc,
1113 &obu->obu.frame_header,
1114 87 obu->header.obu_type ==
1115 AV1_OBU_REDUNDANT_FRAME_HEADER,
1116 NULL);
1117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (err < 0)
1118 return err;
1119 }
1120 87 break;
1121 104 case AV1_OBU_TILE_GROUP:
1122 {
1123 104 err = cbs_av1_write_tile_group_obu(ctx, pbc,
1124 &obu->obu.tile_group);
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (err < 0)
1126 return err;
1127
1128 104 td = &obu->obu.tile_group.tile_data;
1129 }
1130 104 break;
1131 334 case AV1_OBU_FRAME:
1132 {
1133 334 err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
1134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 334 times.
334 if (err < 0)
1135 return err;
1136
1137 334 td = &obu->obu.frame.tile_group.tile_data;
1138 }
1139 334 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 12 case AV1_OBU_METADATA:
1150 {
1151 12 err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (err < 0)
1153 return err;
1154 }
1155 12 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 1017 end_pos = put_bits_count(pbc);
1168 1017 header_size = (end_pos - start_pos + 7) / 8;
1169
2/2
✓ Branch 0 taken 438 times.
✓ Branch 1 taken 579 times.
1017 if (td) {
1170 438 obu->obu_size = header_size + td->data_size;
1171
2/2
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 337 times.
579 } else if (header_size > 0) {
1172 // Add trailing bits and recalculate.
1173 242 err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242 times.
242 if (err < 0)
1175 return err;
1176 242 end_pos = put_bits_count(pbc);
1177 242 obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
1178 } else {
1179 // Empty OBU.
1180 337 obu->obu_size = 0;
1181 }
1182
1183 1017 end_pos = put_bits_count(pbc);
1184 // Must now be byte-aligned.
1185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1017 times.
1017 av_assert0(end_pos % 8 == 0);
1186 1017 flush_put_bits(pbc);
1187 1017 start_pos /= 8;
1188 1017 end_pos /= 8;
1189
1190 1017 *pbc = pbc_tmp;
1191 1017 err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size);
1192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1017 times.
1017 if (err < 0)
1193 return err;
1194
1195 1017 data_pos = put_bits_count(pbc) / 8;
1196 1017 flush_put_bits(pbc);
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1017 times.
1017 av_assert0(data_pos <= start_pos);
1198
1199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1017 times.
1017 if (8 * obu->obu_size > put_bits_left(pbc))
1200 return AVERROR(ENOSPC);
1201
1202
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 337 times.
1017 if (obu->obu_size > 0) {
1203 680 memmove(pbc->buf + data_pos,
1204 680 pbc->buf + start_pos, header_size);
1205 680 skip_put_bytes(pbc, header_size);
1206
1207
2/2
✓ Branch 0 taken 438 times.
✓ Branch 1 taken 242 times.
680 if (td) {
1208 438 memcpy(pbc->buf + data_pos + header_size,
1209 438 td->data, td->data_size);
1210 438 skip_put_bytes(pbc, td->data_size);
1211 }
1212 }
1213
1214 // OBU data must be byte-aligned.
1215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1017 times.
1017 av_assert0(put_bits_count(pbc) % 8 == 0);
1216
1217 1017 return 0;
1218 }
1219
1220 353 static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx,
1221 CodedBitstreamFragment *frag)
1222 {
1223 size_t size, pos;
1224 int i;
1225
1226 353 size = 0;
1227
2/2
✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 353 times.
1370 for (i = 0; i < frag->nb_units; i++)
1228 1017 size += frag->units[i].data_size;
1229
1230 353 frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
1231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353 times.
353 if (!frag->data_ref)
1232 return AVERROR(ENOMEM);
1233 353 frag->data = frag->data_ref->data;
1234 353 memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1235
1236 353 pos = 0;
1237
2/2
✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 353 times.
1370 for (i = 0; i < frag->nb_units; i++) {
1238 1017 memcpy(frag->data + pos, frag->units[i].data,
1239 1017 frag->units[i].data_size);
1240 1017 pos += frag->units[i].data_size;
1241 }
1242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353 times.
353 av_assert0(pos == size);
1243 353 frag->data_size = size;
1244
1245 353 return 0;
1246 }
1247
1248 static void cbs_av1_flush(CodedBitstreamContext *ctx)
1249 {
1250 CodedBitstreamAV1Context *priv = ctx->priv_data;
1251
1252 av_buffer_unref(&priv->frame_header_ref);
1253 priv->sequence_header = NULL;
1254 priv->frame_header = NULL;
1255
1256 memset(priv->ref, 0, sizeof(priv->ref));
1257 priv->operating_point_idc = 0;
1258 priv->seen_frame_header = 0;
1259 priv->tile_num = 0;
1260 }
1261
1262 77 static void cbs_av1_close(CodedBitstreamContext *ctx)
1263 {
1264 77 CodedBitstreamAV1Context *priv = ctx->priv_data;
1265
1266 77 av_buffer_unref(&priv->sequence_header_ref);
1267 77 av_buffer_unref(&priv->frame_header_ref);
1268 77 }
1269
1270 20 static void cbs_av1_free_metadata(void *unit, uint8_t *content)
1271 {
1272 20 AV1RawOBU *obu = (AV1RawOBU*)content;
1273 AV1RawMetadata *md;
1274
1275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 av_assert0(obu->header.obu_type == AV1_OBU_METADATA);
1276 20 md = &obu->obu.metadata;
1277
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 switch (md->metadata_type) {
1279 case AV1_METADATA_TYPE_ITUT_T35:
1280 av_buffer_unref(&md->metadata.itut_t35.payload_ref);
1281 break;
1282 }
1283 20 av_free(content);
1284 20 }
1285
1286 static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[] = {
1287 CBS_UNIT_TYPE_POD(AV1_OBU_SEQUENCE_HEADER, AV1RawOBU),
1288 CBS_UNIT_TYPE_POD(AV1_OBU_TEMPORAL_DELIMITER, AV1RawOBU),
1289 CBS_UNIT_TYPE_POD(AV1_OBU_FRAME_HEADER, AV1RawOBU),
1290 CBS_UNIT_TYPE_POD(AV1_OBU_REDUNDANT_FRAME_HEADER, AV1RawOBU),
1291
1292 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_TILE_GROUP, AV1RawOBU,
1293 obu.tile_group.tile_data.data),
1294 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_FRAME, AV1RawOBU,
1295 obu.frame.tile_group.tile_data.data),
1296 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_TILE_LIST, AV1RawOBU,
1297 obu.tile_list.tile_data.data),
1298 CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_PADDING, AV1RawOBU,
1299 obu.padding.payload),
1300
1301 CBS_UNIT_TYPE_COMPLEX(AV1_OBU_METADATA, AV1RawOBU,
1302 &cbs_av1_free_metadata),
1303
1304 CBS_UNIT_TYPE_END_OF_LIST
1305 };
1306
1307 #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
1308 static const AVOption cbs_av1_options[] = {
1309 { "operating_point", "Set operating point to select layers to parse from a scalable bitstream",
1310 OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AV1_MAX_OPERATING_POINTS - 1, 0 },
1311 { NULL }
1312 };
1313
1314 static const AVClass cbs_av1_class = {
1315 .class_name = "cbs_av1",
1316 .item_name = av_default_item_name,
1317 .option = cbs_av1_options,
1318 .version = LIBAVUTIL_VERSION_INT,
1319 };
1320
1321 const CodedBitstreamType ff_cbs_type_av1 = {
1322 .codec_id = AV_CODEC_ID_AV1,
1323
1324 .priv_class = &cbs_av1_class,
1325 .priv_data_size = sizeof(CodedBitstreamAV1Context),
1326
1327 .unit_types = cbs_av1_unit_types,
1328
1329 .split_fragment = &cbs_av1_split_fragment,
1330 .read_unit = &cbs_av1_read_unit,
1331 .write_unit = &cbs_av1_write_obu,
1332 .assemble_fragment = &cbs_av1_assemble_fragment,
1333
1334 .flush = &cbs_av1_flush,
1335 .close = &cbs_av1_close,
1336 };
1337