GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_av1.c Lines: 356 572 62.2 %
Date: 2020-07-13 04:22:34 Branches: 159 346 46.0 %

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/pixfmt.h"
21
22
#include "cbs.h"
23
#include "cbs_internal.h"
24
#include "cbs_av1.h"
25
#include "internal.h"
26
27
28
static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc,
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
106
static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc,
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
    zeroes = av_log2(value + 1);
124
    v = value - (1U << zeroes) + 1;
125
    put_bits(pbc, zeroes, 0);
126
    put_bits(pbc, 1, 1);
127
    put_bits(pbc, zeroes, v);
128
129
    if (ctx->trace_enable) {
130
        char bits[65];
131
        int i, j;
132
        i = 0;
133
        for (j = 0; j < zeroes; j++)
134
            bits[i++] = '0';
135
        bits[i++] = '1';
136
        for (j = 0; j < zeroes; j++)
137
            bits[i++] = (v >> (zeroes - j - 1) & 1) ? '1' : '0';
138
        bits[i++] = 0;
139
        ff_cbs_trace_syntax_element(ctx, position, name, NULL,
140
                                    bits, value);
141
    }
142
143
    return 0;
144
}
145
146
4086
static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc,
147
                               const char *name, uint64_t *write_to)
148
{
149
    uint64_t value;
150
    int position, err, i;
151
152
4086
    if (ctx->trace_enable)
153
        position = get_bits_count(gbc);
154
155
4086
    value = 0;
156
5778
    for (i = 0; i < 8; i++) {
157
5778
        int subscript[2] = { 1, i };
158
        uint32_t byte;
159
5778
        err = ff_cbs_read_unsigned(ctx, gbc, 8, "leb128_byte[i]", subscript,
160
                                   &byte, 0x00, 0xff);
161
5778
        if (err < 0)
162
            return err;
163
164
5778
        value |= (uint64_t)(byte & 0x7f) << (i * 7);
165
5778
        if (!(byte & 0x80))
166
4086
            break;
167
    }
168
169
4086
    if (value > UINT32_MAX)
170
        return AVERROR_INVALIDDATA;
171
172
4086
    if (ctx->trace_enable)
173
        ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
174
175
4086
    *write_to = value;
176
4086
    return 0;
177
}
178
179
1029
static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc,
180
                                const char *name, uint64_t value)
181
{
182
    int position, err, len, i;
183
    uint8_t byte;
184
185
1029
    len = (av_log2(value) + 7) / 7;
186
187
1029
    if (ctx->trace_enable)
188
        position = put_bits_count(pbc);
189
190
2477
    for (i = 0; i < len; i++) {
191
1448
        int subscript[2] = { 1, i };
192
193
1448
        byte = value >> (7 * i) & 0x7f;
194
1448
        if (i < len - 1)
195
419
            byte |= 0x80;
196
197
1448
        err = ff_cbs_write_unsigned(ctx, pbc, 8, "leb128_byte[i]", subscript,
198
                                    byte, 0x00, 0xff);
199
1448
        if (err < 0)
200
            return err;
201
    }
202
203
1029
    if (ctx->trace_enable)
204
        ff_cbs_trace_syntax_element(ctx, position, name, NULL, "", value);
205
206
1029
    return 0;
207
}
208
209
260
static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc,
210
                           uint32_t n, const char *name,
211
                           const int *subscripts, uint32_t *write_to)
212
{
213
    uint32_t m, v, extra_bit, value;
214
    int position, w;
215
216
260
    av_assert0(n > 0);
217
218
260
    if (ctx->trace_enable)
219
        position = get_bits_count(gbc);
220
221
260
    w = av_log2(n) + 1;
222
260
    m = (1 << w) - n;
223
224
260
    if (get_bits_left(gbc) < w) {
225
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid non-symmetric value at "
226
               "%s: bitstream ended.\n", name);
227
        return AVERROR_INVALIDDATA;
228
    }
229
230
260
    if (w - 1 > 0)
231
260
        v = get_bits(gbc, w - 1);
232
    else
233
        v = 0;
234
235
260
    if (v < m) {
236
208
        value = v;
237
    } else {
238
52
        extra_bit = get_bits1(gbc);
239
52
        value = (v << 1) - m + extra_bit;
240
    }
241
242
260
    if (ctx->trace_enable) {
243
        char bits[33];
244
        int i;
245
        for (i = 0; i < w - 1; i++)
246
            bits[i] = (v >> i & 1) ? '1' : '0';
247
        if (v >= m)
248
            bits[i++] = extra_bit ? '1' : '0';
249
        bits[i] = 0;
250
251
        ff_cbs_trace_syntax_element(ctx, position,
252
                                    name, subscripts, bits, value);
253
    }
254
255
260
    *write_to = value;
256
260
    return 0;
257
}
258
259
130
static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc,
260
                            uint32_t n, const char *name,
261
                            const int *subscripts, uint32_t value)
262
{
263
    uint32_t w, m, v, extra_bit;
264
    int position;
265
266
130
    if (value > n) {
267
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
268
               "%"PRIu32", but must be in [0,%"PRIu32"].\n",
269
               name, value, n);
270
        return AVERROR_INVALIDDATA;
271
    }
272
273
130
    if (ctx->trace_enable)
274
        position = put_bits_count(pbc);
275
276
130
    w = av_log2(n) + 1;
277
130
    m = (1 << w) - n;
278
279
130
    if (put_bits_left(pbc) < w)
280
        return AVERROR(ENOSPC);
281
282
130
    if (value < m) {
283
104
        v = value;
284
104
        put_bits(pbc, w - 1, v);
285
    } else {
286
26
        v = m + ((value - m) >> 1);
287
26
        extra_bit = (value - m) & 1;
288
26
        put_bits(pbc, w - 1, v);
289
26
        put_bits(pbc, 1, extra_bit);
290
    }
291
292
130
    if (ctx->trace_enable) {
293
        char bits[33];
294
        int i;
295
        for (i = 0; i < w - 1; i++)
296
            bits[i] = (v >> i & 1) ? '1' : '0';
297
        if (value >= m)
298
            bits[i++] = extra_bit ? '1' : '0';
299
        bits[i] = 0;
300
301
        ff_cbs_trace_syntax_element(ctx, position,
302
                                    name, subscripts, bits, value);
303
    }
304
305
130
    return 0;
306
}
307
308
2727
static int cbs_av1_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc,
309
                                  uint32_t range_min, uint32_t range_max,
310
                                  const char *name, uint32_t *write_to)
311
{
312
    uint32_t value;
313
    int position, i;
314
    char bits[33];
315
316

2727
    av_assert0(range_min <= range_max && range_max - range_min < sizeof(bits) - 1);
317
2727
    if (ctx->trace_enable)
318
        position = get_bits_count(gbc);
319
320
4396
    for (i = 0, value = range_min; value < range_max;) {
321
3978
        if (get_bits_left(gbc) < 1) {
322
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
323
                   "%s: bitstream ended.\n", name);
324
            return AVERROR_INVALIDDATA;
325
        }
326
3978
        if (get_bits1(gbc)) {
327
1669
            bits[i++] = '1';
328
1669
            ++value;
329
        } else {
330
2309
            bits[i++] = '0';
331
2309
            break;
332
        }
333
    }
334
335
2727
    if (ctx->trace_enable) {
336
        bits[i] = 0;
337
        ff_cbs_trace_syntax_element(ctx, position,
338
                                    name, NULL, bits, value);
339
    }
340
341
2727
    *write_to = value;
342
2727
    return 0;
343
}
344
345
1317
static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc,
346
                                   uint32_t range_min, uint32_t range_max,
347
                                   const char *name, uint32_t value)
348
{
349
    int len;
350
351

1317
    av_assert0(range_min <= range_max && range_max - range_min < 32);
352

1317
    if (value < range_min || value > range_max) {
353
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
354
               "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
355
               name, value, range_min, range_max);
356
        return AVERROR_INVALIDDATA;
357
    }
358
359
1317
    if (value == range_max)
360
203
        len = range_max - range_min;
361
    else
362
1114
        len = value - range_min + 1;
363
1317
    if (put_bits_left(pbc) < len)
364
        return AVERROR(ENOSPC);
365
366
1317
    if (ctx->trace_enable) {
367
        char bits[33];
368
        int i;
369
        for (i = 0; i < len; i++) {
370
            if (range_min + i == value)
371
                bits[i] = '0';
372
            else
373
                bits[i] = '1';
374
        }
375
        bits[i] = 0;
376
        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
377
                                    name, NULL, bits, value);
378
    }
379
380
1317
    if (len > 0)
381
1317
        put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
382
383
1317
    return 0;
384
}
385
386
168
static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc,
387
                               uint32_t range_max, const char *name,
388
                               const int *subscripts, uint32_t *write_to)
389
{
390
    uint32_t value;
391
    int position, err;
392
    uint32_t max_len, len, range_offset, range_bits;
393
394
168
    if (ctx->trace_enable)
395
        position = get_bits_count(gbc);
396
397
168
    av_assert0(range_max > 0);
398
168
    max_len = av_log2(range_max - 1) - 3;
399
400
168
    err = cbs_av1_read_increment(ctx, gbc, 0, max_len,
401
                                 "subexp_more_bits", &len);
402
168
    if (err < 0)
403
        return err;
404
405
168
    if (len) {
406
158
        range_bits   = 2 + len;
407
158
        range_offset = 1 << range_bits;
408
    } else {
409
10
        range_bits   = 3;
410
10
        range_offset = 0;
411
    }
412
413
168
    if (len < max_len) {
414
168
        err = ff_cbs_read_unsigned(ctx, gbc, range_bits,
415
                                   "subexp_bits", NULL, &value,
416
168
                                   0, MAX_UINT_BITS(range_bits));
417
168
        if (err < 0)
418
            return err;
419
420
    } else {
421
        err = cbs_av1_read_ns(ctx, gbc, range_max - range_offset,
422
                              "subexp_final_bits", NULL, &value);
423
        if (err < 0)
424
            return err;
425
    }
426
168
    value += range_offset;
427
428
168
    if (ctx->trace_enable)
429
        ff_cbs_trace_syntax_element(ctx, position,
430
                                    name, subscripts, "", value);
431
432
168
    *write_to = value;
433
168
    return err;
434
}
435
436
60
static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc,
437
                                uint32_t range_max, const char *name,
438
                                const int *subscripts, uint32_t value)
439
{
440
    int position, err;
441
    uint32_t max_len, len, range_offset, range_bits;
442
443
60
    if (value > range_max) {
444
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
445
               "%"PRIu32", but must be in [0,%"PRIu32"].\n",
446
               name, value, range_max);
447
        return AVERROR_INVALIDDATA;
448
    }
449
450
60
    if (ctx->trace_enable)
451
        position = put_bits_count(pbc);
452
453
60
    av_assert0(range_max > 0);
454
60
    max_len = av_log2(range_max - 1) - 3;
455
456
60
    if (value < 8) {
457
5
        range_bits   = 3;
458
5
        range_offset = 0;
459
5
        len = 0;
460
    } else {
461
55
        range_bits = av_log2(value);
462
55
        len = range_bits - 2;
463
55
        if (len > max_len) {
464
            // The top bin is combined with the one below it.
465
            av_assert0(len == max_len + 1);
466
            --range_bits;
467
            len = max_len;
468
        }
469
55
        range_offset = 1 << range_bits;
470
    }
471
472
60
    err = cbs_av1_write_increment(ctx, pbc, 0, max_len,
473
                                  "subexp_more_bits", len);
474
60
    if (err < 0)
475
        return err;
476
477
60
    if (len < max_len) {
478
60
        err = ff_cbs_write_unsigned(ctx, pbc, range_bits,
479
                                    "subexp_bits", NULL,
480
                                    value - range_offset,
481
60
                                    0, MAX_UINT_BITS(range_bits));
482
60
        if (err < 0)
483
            return err;
484
485
    } else {
486
        err = cbs_av1_write_ns(ctx, pbc, range_max - range_offset,
487
                               "subexp_final_bits", NULL,
488
                               value - range_offset);
489
        if (err < 0)
490
            return err;
491
    }
492
493
60
    if (ctx->trace_enable)
494
        ff_cbs_trace_syntax_element(ctx, position,
495
                                    name, subscripts, "", value);
496
497
60
    return err;
498
}
499
500
501
6450
static int cbs_av1_tile_log2(int blksize, int target)
502
{
503
    int k;
504
13122
    for (k = 0; (blksize << k) < target; k++);
505
6450
    return k;
506
}
507
508
9421
static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq,
509
                                     unsigned int a, unsigned int b)
510
{
511
    unsigned int diff, m;
512
9421
    if (!seq->enable_order_hint)
513
        return 0;
514
9421
    diff = a - b;
515
9421
    m = 1 << seq->order_hint_bits_minus_1;
516
9421
    diff = (diff & (m - 1)) - (diff & m);
517
9421
    return diff;
518
}
519
520
static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
521
{
522
    GetBitContext tmp = *gbc;
523
    size_t size = 0;
524
    for (int i = 0; get_bits_left(&tmp) >= 8; i++) {
525
        if (get_bits(&tmp, 8))
526
            size = i;
527
    }
528
    return size;
529
}
530
531
532
#define HEADER(name) do { \
533
        ff_cbs_trace_header(ctx, name); \
534
    } while (0)
535
536
#define CHECK(call) do { \
537
        err = (call); \
538
        if (err < 0) \
539
            return err; \
540
    } while (0)
541
542
#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
543
#define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
544
#define FUNC(name) FUNC_AV1(READWRITE, name)
545
546
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
547
548
#define fb(width, name) \
549
        xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
550
#define fc(width, name, range_min, range_max) \
551
        xf(width, name, current->name, range_min, range_max, 0, )
552
#define flag(name) fb(1, name)
553
#define su(width, name) \
554
        xsu(width, name, current->name, 0, )
555
556
#define fbs(width, name, subs, ...) \
557
        xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
558
#define fcs(width, name, range_min, range_max, subs, ...) \
559
        xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
560
#define flags(name, subs, ...) \
561
        xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
562
#define sus(width, name, subs, ...) \
563
        xsu(width, name, current->name, subs, __VA_ARGS__)
564
565
#define fixed(width, name, value) do { \
566
        av_unused uint32_t fixed_value = value; \
567
        xf(width, name, fixed_value, value, value, 0, ); \
568
    } while (0)
569
570
571
#define READ
572
#define READWRITE read
573
#define RWContext GetBitContext
574
575
#define xf(width, name, var, range_min, range_max, subs, ...) do { \
576
        uint32_t value; \
577
        CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
578
                                   SUBSCRIPTS(subs, __VA_ARGS__), \
579
                                   &value, range_min, range_max)); \
580
        var = value; \
581
    } while (0)
582
583
#define xsu(width, name, var, subs, ...) do { \
584
        int32_t value; \
585
        CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
586
                                 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
587
                                 MIN_INT_BITS(width), \
588
                                 MAX_INT_BITS(width))); \
589
        var = value; \
590
    } while (0)
591
592
#define uvlc(name, range_min, range_max) do { \
593
        uint32_t value; \
594
        CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
595
                                &value, range_min, range_max)); \
596
        current->name = value; \
597
    } while (0)
598
599
#define ns(max_value, name, subs, ...) do { \
600
        uint32_t value; \
601
        CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
602
                              SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
603
        current->name = value; \
604
    } while (0)
605
606
#define increment(name, min, max) do { \
607
        uint32_t value; \
608
        CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
609
        current->name = value; \
610
    } while (0)
611
612
#define subexp(name, max, subs, ...) do { \
613
        uint32_t value; \
614
        CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
615
                                  SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
616
        current->name = value; \
617
    } while (0)
618
619
#define delta_q(name) do { \
620
        uint8_t delta_coded; \
621
        int8_t delta_q; \
622
        xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
623
        if (delta_coded) \
624
            xsu(1 + 6, name.delta_q, delta_q, 0, ); \
625
        else \
626
            delta_q = 0; \
627
        current->name = delta_q; \
628
    } while (0)
629
630
#define leb128(name) do { \
631
        uint64_t value; \
632
        CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
633
        current->name = value; \
634
    } while (0)
635
636
#define infer(name, value) do { \
637
        current->name = value; \
638
    } while (0)
639
640
#define byte_alignment(rw) (get_bits_count(rw) % 8)
641
642
#include "cbs_av1_syntax_template.c"
643
644
#undef READ
645
#undef READWRITE
646
#undef RWContext
647
#undef xf
648
#undef xsu
649
#undef uvlc
650
#undef ns
651
#undef increment
652
#undef subexp
653
#undef delta_q
654
#undef leb128
655
#undef infer
656
#undef byte_alignment
657
658
659
#define WRITE
660
#define READWRITE write
661
#define RWContext PutBitContext
662
663
#define xf(width, name, var, range_min, range_max, subs, ...) do { \
664
        CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
665
                                    SUBSCRIPTS(subs, __VA_ARGS__), \
666
                                    var, range_min, range_max)); \
667
    } while (0)
668
669
#define xsu(width, name, var, subs, ...) do { \
670
        CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
671
                                  SUBSCRIPTS(subs, __VA_ARGS__), var, \
672
                                  MIN_INT_BITS(width), \
673
                                  MAX_INT_BITS(width))); \
674
    } while (0)
675
676
#define uvlc(name, range_min, range_max) do { \
677
        CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
678
                                 range_min, range_max)); \
679
    } while (0)
680
681
#define ns(max_value, name, subs, ...) do { \
682
        CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
683
                               SUBSCRIPTS(subs, __VA_ARGS__), \
684
                               current->name)); \
685
    } while (0)
686
687
#define increment(name, min, max) do { \
688
        CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
689
                                      current->name)); \
690
    } while (0)
691
692
#define subexp(name, max, subs, ...) do { \
693
        CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
694
                                   SUBSCRIPTS(subs, __VA_ARGS__), \
695
                                   current->name)); \
696
    } while (0)
697
698
#define delta_q(name) do { \
699
        xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
700
        if (current->name) \
701
            xsu(1 + 6, name.delta_q, current->name, 0, ); \
702
    } while (0)
703
704
#define leb128(name) do { \
705
        CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name)); \
706
    } while (0)
707
708
#define infer(name, value) do { \
709
        if (current->name != (value)) { \
710
            av_log(ctx->log_ctx, AV_LOG_ERROR, \
711
                   "%s does not match inferred value: " \
712
                   "%"PRId64", but should be %"PRId64".\n", \
713
                   #name, (int64_t)current->name, (int64_t)(value)); \
714
            return AVERROR_INVALIDDATA; \
715
        } \
716
    } while (0)
717
718
#define byte_alignment(rw) (put_bits_count(rw) % 8)
719
720
#include "cbs_av1_syntax_template.c"
721
722
#undef WRITE
723
#undef READWRITE
724
#undef RWContext
725
#undef xf
726
#undef xsu
727
#undef uvlc
728
#undef ns
729
#undef increment
730
#undef subexp
731
#undef delta_q
732
#undef leb128
733
#undef infer
734
#undef byte_alignment
735
736
737
756
static int cbs_av1_split_fragment(CodedBitstreamContext *ctx,
738
                                  CodedBitstreamFragment *frag,
739
                                  int header)
740
{
741
    GetBitContext gbc;
742
    uint8_t *data;
743
    size_t size;
744
    uint64_t obu_length;
745
    int pos, err, trace;
746
747
    // Don't include this parsing in trace output.
748
756
    trace = ctx->trace_enable;
749
756
    ctx->trace_enable = 0;
750
751
756
    data = frag->data;
752
756
    size = frag->data_size;
753
754
756
    if (INT_MAX / 8 < size) {
755
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: "
756
               "too large (%"SIZE_SPECIFIER" bytes).\n", size);
757
        err = AVERROR_INVALIDDATA;
758
        goto fail;
759
    }
760
761
2821
    while (size > 0) {
762
        AV1RawOBUHeader header;
763
        uint64_t obu_size;
764
765
2065
        init_get_bits(&gbc, data, 8 * size);
766
767
2065
        err = cbs_av1_read_obu_header(ctx, &gbc, &header);
768
2065
        if (err < 0)
769
            goto fail;
770
771
2065
        if (header.obu_has_size_field) {
772
2043
            if (get_bits_left(&gbc) < 8) {
773
                av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment "
774
                       "too short (%"SIZE_SPECIFIER" bytes).\n", size);
775
                err = AVERROR_INVALIDDATA;
776
                goto fail;
777
            }
778
2043
            err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
779
2043
            if (err < 0)
780
                goto fail;
781
        } else
782
22
            obu_size = size - 1 - header.obu_extension_flag;
783
784
2065
        pos = get_bits_count(&gbc);
785

2065
        av_assert0(pos % 8 == 0 && pos / 8 <= size);
786
787
2065
        obu_length = pos / 8 + obu_size;
788
789
2065
        if (size < obu_length) {
790
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
791
                   "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
792
                   obu_length, size);
793
            err = AVERROR_INVALIDDATA;
794
            goto fail;
795
        }
796
797
2065
        err = ff_cbs_insert_unit_data(frag, -1, header.obu_type,
798
                                      data, obu_length, frag->data_ref);
799
2065
        if (err < 0)
800
            goto fail;
801
802
2065
        data += obu_length;
803
2065
        size -= obu_length;
804
    }
805
806
756
    err = 0;
807
756
fail:
808
756
    ctx->trace_enable = trace;
809
756
    return err;
810
}
811
812
888
static void cbs_av1_free_tile_data(AV1RawTileData *td)
813
{
814
888
    av_buffer_unref(&td->data_ref);
815
888
}
816
817
static void cbs_av1_free_padding(AV1RawPadding *pd)
818
{
819
    av_buffer_unref(&pd->payload_ref);
820
}
821
822
12
static void cbs_av1_free_metadata(AV1RawMetadata *md)
823
{
824
12
    switch (md->metadata_type) {
825
    case AV1_METADATA_TYPE_ITUT_T35:
826
        av_buffer_unref(&md->metadata.itut_t35.payload_ref);
827
        break;
828
    }
829
12
}
830
831
2053
static void cbs_av1_free_obu(void *opaque, uint8_t *content)
832
{
833
2053
    AV1RawOBU *obu = (AV1RawOBU*)content;
834
835

2053
    switch (obu->header.obu_type) {
836
208
    case AV1_OBU_TILE_GROUP:
837
208
        cbs_av1_free_tile_data(&obu->obu.tile_group.tile_data);
838
208
        break;
839
680
    case AV1_OBU_FRAME:
840
680
        cbs_av1_free_tile_data(&obu->obu.frame.tile_group.tile_data);
841
680
        break;
842
    case AV1_OBU_TILE_LIST:
843
        cbs_av1_free_tile_data(&obu->obu.tile_list.tile_data);
844
        break;
845
12
    case AV1_OBU_METADATA:
846
12
        cbs_av1_free_metadata(&obu->obu.metadata);
847
12
        break;
848
    case AV1_OBU_PADDING:
849
        cbs_av1_free_padding(&obu->obu.padding);
850
        break;
851
    }
852
853
2053
    av_freep(&obu);
854
2053
}
855
856
888
static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx,
857
                                 CodedBitstreamUnit *unit,
858
                                 GetBitContext *gbc,
859
                                 AV1RawTileData *td)
860
{
861
    int pos;
862
863
888
    pos = get_bits_count(gbc);
864
888
    if (pos >= 8 * unit->data_size) {
865
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before "
866
               "any data in tile group (%d bits read).\n", pos);
867
        return AVERROR_INVALIDDATA;
868
    }
869
    // Must be byte-aligned at this point.
870
888
    av_assert0(pos % 8 == 0);
871
872
888
    td->data_ref = av_buffer_ref(unit->data_ref);
873
888
    if (!td->data_ref)
874
        return AVERROR(ENOMEM);
875
876
888
    td->data      = unit->data      + pos / 8;
877
888
    td->data_size = unit->data_size - pos / 8;
878
879
888
    return 0;
880
}
881
882
2053
static int cbs_av1_read_unit(CodedBitstreamContext *ctx,
883
                             CodedBitstreamUnit *unit)
884
{
885
2053
    CodedBitstreamAV1Context *priv = ctx->priv_data;
886
    AV1RawOBU *obu;
887
    GetBitContext gbc;
888
    int err, start_pos, end_pos;
889
890
2053
    err = ff_cbs_alloc_unit_content(unit, sizeof(*obu),
891
                                    &cbs_av1_free_obu);
892
2053
    if (err < 0)
893
        return err;
894
2053
    obu = unit->content;
895
896
2053
    err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
897
2053
    if (err < 0)
898
        return err;
899
900
2053
    err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header);
901
2053
    if (err < 0)
902
        return err;
903
2053
    av_assert0(obu->header.obu_type == unit->type);
904
905
2053
    if (obu->header.obu_has_size_field) {
906
        uint64_t obu_size;
907
2031
        err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
908
2031
        if (err < 0)
909
            return err;
910
2031
        obu->obu_size = obu_size;
911
    } else {
912
22
        if (unit->data_size < 1 + obu->header.obu_extension_flag) {
913
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
914
                   "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size);
915
            return AVERROR_INVALIDDATA;
916
        }
917
22
        obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
918
    }
919
920
2053
    start_pos = get_bits_count(&gbc);
921
922
2053
    if (obu->header.obu_extension_flag) {
923
160
        priv->temporal_id = obu->header.temporal_id;
924
160
        priv->spatial_id  = obu->header.spatial_id;
925
926
160
        if (obu->header.obu_type != AV1_OBU_SEQUENCE_HEADER &&
927
160
            obu->header.obu_type != AV1_OBU_TEMPORAL_DELIMITER &&
928
160
            priv->operating_point_idc) {
929
            int in_temporal_layer =
930
                (priv->operating_point_idc >>  priv->temporal_id    ) & 1;
931
            int in_spatial_layer  =
932
                (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1;
933
            if (!in_temporal_layer || !in_spatial_layer) {
934
                // Decoding will drop this OBU at this operating point.
935
            }
936
        }
937
    } else {
938
1893
        priv->temporal_id = 0;
939
1893
        priv->spatial_id  = 0;
940
    }
941
942
2053
    priv->ref = (AV1ReferenceFrameState *)&priv->read_ref;
943
944


2053
    switch (obu->header.obu_type) {
945
294
    case AV1_OBU_SEQUENCE_HEADER:
946
        {
947
294
            err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
948
                                                   &obu->obu.sequence_header);
949
294
            if (err < 0)
950
                return err;
951
952
294
            av_buffer_unref(&priv->sequence_header_ref);
953
294
            priv->sequence_header = NULL;
954
955
294
            priv->sequence_header_ref = av_buffer_ref(unit->content_ref);
956
294
            if (!priv->sequence_header_ref)
957
                return AVERROR(ENOMEM);
958
294
            priv->sequence_header = &obu->obu.sequence_header;
959
        }
960
294
        break;
961
682
    case AV1_OBU_TEMPORAL_DELIMITER:
962
        {
963
682
            err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
964
682
            if (err < 0)
965
                return err;
966
        }
967
682
        break;
968
177
    case AV1_OBU_FRAME_HEADER:
969
    case AV1_OBU_REDUNDANT_FRAME_HEADER:
970
        {
971
177
            err = cbs_av1_read_frame_header_obu(ctx, &gbc,
972
                                                &obu->obu.frame_header,
973
177
                                                obu->header.obu_type ==
974
                                                AV1_OBU_REDUNDANT_FRAME_HEADER,
975
                                                unit->data_ref);
976
177
            if (err < 0)
977
                return err;
978
        }
979
177
        break;
980
208
    case AV1_OBU_TILE_GROUP:
981
        {
982
208
            err = cbs_av1_read_tile_group_obu(ctx, &gbc,
983
                                              &obu->obu.tile_group);
984
208
            if (err < 0)
985
                return err;
986
987
208
            err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
988
                                        &obu->obu.tile_group.tile_data);
989
208
            if (err < 0)
990
                return err;
991
        }
992
208
        break;
993
680
    case AV1_OBU_FRAME:
994
        {
995
680
            err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame,
996
                                         unit->data_ref);
997
680
            if (err < 0)
998
                return err;
999
1000
680
            err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1001
                                        &obu->obu.frame.tile_group.tile_data);
1002
680
            if (err < 0)
1003
                return err;
1004
        }
1005
680
        break;
1006
    case AV1_OBU_TILE_LIST:
1007
        {
1008
            err = cbs_av1_read_tile_list_obu(ctx, &gbc,
1009
                                             &obu->obu.tile_list);
1010
            if (err < 0)
1011
                return err;
1012
1013
            err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
1014
                                        &obu->obu.tile_list.tile_data);
1015
            if (err < 0)
1016
                return err;
1017
        }
1018
        break;
1019
12
    case AV1_OBU_METADATA:
1020
        {
1021
12
            err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata);
1022
12
            if (err < 0)
1023
                return err;
1024
        }
1025
12
        break;
1026
    case AV1_OBU_PADDING:
1027
        {
1028
            err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->obu.padding);
1029
            if (err < 0)
1030
                return err;
1031
        }
1032
        break;
1033
    default:
1034
        return AVERROR(ENOSYS);
1035
    }
1036
1037
2053
    end_pos = get_bits_count(&gbc);
1038
2053
    av_assert0(end_pos <= unit->data_size * 8);
1039
1040
2053
    if (obu->obu_size > 0 &&
1041
1371
        obu->header.obu_type != AV1_OBU_TILE_GROUP &&
1042
1163
        obu->header.obu_type != AV1_OBU_TILE_LIST &&
1043
1163
        obu->header.obu_type != AV1_OBU_FRAME) {
1044
483
        int nb_bits = obu->obu_size * 8 + start_pos - end_pos;
1045
1046
483
        if (nb_bits <= 0)
1047
            return AVERROR_INVALIDDATA;
1048
1049
483
        err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
1050
483
        if (err < 0)
1051
            return err;
1052
    }
1053
1054
2053
    return 0;
1055
}
1056
1057
1017
static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
1058
                             CodedBitstreamUnit *unit,
1059
                             PutBitContext *pbc)
1060
{
1061
1017
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1062
1017
    AV1RawOBU *obu = unit->content;
1063
    PutBitContext pbc_tmp;
1064
    AV1RawTileData *td;
1065
    size_t header_size;
1066
    int err, start_pos, end_pos, data_pos;
1067
1068
    // OBUs in the normal bitstream format must contain a size field
1069
    // in every OBU (in annex B it is optional, but we don't support
1070
    // writing that).
1071
1017
    obu->header.obu_has_size_field = 1;
1072
1073
1017
    err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
1074
1017
    if (err < 0)
1075
        return err;
1076
1077
1017
    if (obu->header.obu_has_size_field) {
1078
1017
        pbc_tmp = *pbc;
1079
        // Add space for the size field to fill later.
1080
1017
        put_bits32(pbc, 0);
1081
1017
        put_bits32(pbc, 0);
1082
    }
1083
1084
1017
    td = NULL;
1085
1017
    start_pos = put_bits_count(pbc);
1086
1087
1017
    priv->ref = (AV1ReferenceFrameState *)&priv->write_ref;
1088
1089


1017
    switch (obu->header.obu_type) {
1090
143
    case AV1_OBU_SEQUENCE_HEADER:
1091
        {
1092
143
            err = cbs_av1_write_sequence_header_obu(ctx, pbc,
1093
                                                    &obu->obu.sequence_header);
1094
143
            if (err < 0)
1095
                return err;
1096
1097
143
            av_buffer_unref(&priv->sequence_header_ref);
1098
143
            priv->sequence_header = NULL;
1099
1100
143
            priv->sequence_header_ref = av_buffer_ref(unit->content_ref);
1101
143
            if (!priv->sequence_header_ref)
1102
                return AVERROR(ENOMEM);
1103
143
            priv->sequence_header = &obu->obu.sequence_header;
1104
        }
1105
143
        break;
1106
337
    case AV1_OBU_TEMPORAL_DELIMITER:
1107
        {
1108
337
            err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
1109
337
            if (err < 0)
1110
                return err;
1111
        }
1112
337
        break;
1113
87
    case AV1_OBU_FRAME_HEADER:
1114
    case AV1_OBU_REDUNDANT_FRAME_HEADER:
1115
        {
1116
87
            err = cbs_av1_write_frame_header_obu(ctx, pbc,
1117
                                                 &obu->obu.frame_header,
1118
87
                                                 obu->header.obu_type ==
1119
                                                 AV1_OBU_REDUNDANT_FRAME_HEADER,
1120
                                                 NULL);
1121
87
            if (err < 0)
1122
                return err;
1123
        }
1124
87
        break;
1125
104
    case AV1_OBU_TILE_GROUP:
1126
        {
1127
104
            err = cbs_av1_write_tile_group_obu(ctx, pbc,
1128
                                               &obu->obu.tile_group);
1129
104
            if (err < 0)
1130
                return err;
1131
1132
104
            td = &obu->obu.tile_group.tile_data;
1133
        }
1134
104
        break;
1135
334
    case AV1_OBU_FRAME:
1136
        {
1137
334
            err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
1138
334
            if (err < 0)
1139
                return err;
1140
1141
334
            td = &obu->obu.frame.tile_group.tile_data;
1142
        }
1143
334
        break;
1144
    case AV1_OBU_TILE_LIST:
1145
        {
1146
            err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list);
1147
            if (err < 0)
1148
                return err;
1149
1150
            td = &obu->obu.tile_list.tile_data;
1151
        }
1152
        break;
1153
12
    case AV1_OBU_METADATA:
1154
        {
1155
12
            err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
1156
12
            if (err < 0)
1157
                return err;
1158
        }
1159
12
        break;
1160
    case AV1_OBU_PADDING:
1161
        {
1162
            err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding);
1163
            if (err < 0)
1164
                return err;
1165
        }
1166
        break;
1167
    default:
1168
        return AVERROR(ENOSYS);
1169
    }
1170
1171
1017
    end_pos = put_bits_count(pbc);
1172
1017
    header_size = (end_pos - start_pos + 7) / 8;
1173
1017
    if (td) {
1174
438
        obu->obu_size = header_size + td->data_size;
1175
579
    } else if (header_size > 0) {
1176
        // Add trailing bits and recalculate.
1177
242
        err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
1178
242
        if (err < 0)
1179
            return err;
1180
242
        end_pos = put_bits_count(pbc);
1181
242
        obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
1182
    } else {
1183
        // Empty OBU.
1184
337
        obu->obu_size = 0;
1185
    }
1186
1187
1017
    end_pos = put_bits_count(pbc);
1188
    // Must now be byte-aligned.
1189
1017
    av_assert0(end_pos % 8 == 0);
1190
1017
    flush_put_bits(pbc);
1191
1017
    start_pos /= 8;
1192
1017
    end_pos   /= 8;
1193
1194
1017
    *pbc = pbc_tmp;
1195
1017
    err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size);
1196
1017
    if (err < 0)
1197
        return err;
1198
1199
1017
    data_pos = put_bits_count(pbc) / 8;
1200
1017
    flush_put_bits(pbc);
1201
1017
    av_assert0(data_pos <= start_pos);
1202
1203
1017
    if (8 * obu->obu_size > put_bits_left(pbc))
1204
        return AVERROR(ENOSPC);
1205
1206
1017
    if (obu->obu_size > 0) {
1207
680
        memmove(pbc->buf + data_pos,
1208
680
                pbc->buf + start_pos, header_size);
1209
680
        skip_put_bytes(pbc, header_size);
1210
1211
680
        if (td) {
1212
438
            memcpy(pbc->buf + data_pos + header_size,
1213
438
                   td->data, td->data_size);
1214
438
            skip_put_bytes(pbc, td->data_size);
1215
        }
1216
    }
1217
1218
    // OBU data must be byte-aligned.
1219
1017
    av_assert0(put_bits_count(pbc) % 8 == 0);
1220
1221
1017
    return 0;
1222
}
1223
1224
353
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx,
1225
                                     CodedBitstreamFragment *frag)
1226
{
1227
    size_t size, pos;
1228
    int i;
1229
1230
353
    size = 0;
1231
1370
    for (i = 0; i < frag->nb_units; i++)
1232
1017
        size += frag->units[i].data_size;
1233
1234
353
    frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
1235
353
    if (!frag->data_ref)
1236
        return AVERROR(ENOMEM);
1237
353
    frag->data = frag->data_ref->data;
1238
353
    memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1239
1240
353
    pos = 0;
1241
1370
    for (i = 0; i < frag->nb_units; i++) {
1242
1017
        memcpy(frag->data + pos, frag->units[i].data,
1243
1017
               frag->units[i].data_size);
1244
1017
        pos += frag->units[i].data_size;
1245
    }
1246
353
    av_assert0(pos == size);
1247
353
    frag->data_size = size;
1248
1249
353
    return 0;
1250
}
1251
1252
39
static void cbs_av1_close(CodedBitstreamContext *ctx)
1253
{
1254
39
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1255
1256
39
    av_buffer_unref(&priv->sequence_header_ref);
1257
39
    av_buffer_unref(&priv->frame_header_ref);
1258
39
}
1259
1260
const CodedBitstreamType ff_cbs_type_av1 = {
1261
    .codec_id          = AV_CODEC_ID_AV1,
1262
1263
    .priv_data_size    = sizeof(CodedBitstreamAV1Context),
1264
1265
    .split_fragment    = &cbs_av1_split_fragment,
1266
    .read_unit         = &cbs_av1_read_unit,
1267
    .write_unit        = &cbs_av1_write_obu,
1268
    .assemble_fragment = &cbs_av1_assemble_fragment,
1269
1270
    .close             = &cbs_av1_close,
1271
};