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

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

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

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

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

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


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


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