GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_vp9.c Lines: 156 220 70.9 %
Date: 2019-11-22 03:34:36 Branches: 81 156 51.9 %

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
21
#include "cbs.h"
22
#include "cbs_internal.h"
23
#include "cbs_vp9.h"
24
#include "internal.h"
25
26
27
50
static int cbs_vp9_read_s(CodedBitstreamContext *ctx, GetBitContext *gbc,
28
                          int width, const char *name,
29
                          const int *subscripts, int32_t *write_to)
30
{
31
    uint32_t magnitude;
32
    int position, sign;
33
    int32_t value;
34
35
50
    if (ctx->trace_enable)
36
        position = get_bits_count(gbc);
37
38
50
    if (get_bits_left(gbc) < width + 1) {
39
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid signed value at "
40
               "%s: bitstream ended.\n", name);
41
        return AVERROR_INVALIDDATA;
42
    }
43
44
50
    magnitude = get_bits(gbc, width);
45
50
    sign      = get_bits1(gbc);
46
50
    value     = sign ? -(int32_t)magnitude : magnitude;
47
48
50
    if (ctx->trace_enable) {
49
        char bits[33];
50
        int i;
51
        for (i = 0; i < width; i++)
52
            bits[i] = magnitude >> (width - i - 1) & 1 ? '1' : '0';
53
        bits[i] = sign ? '1' : '0';
54
        bits[i + 1] = 0;
55
56
        ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
57
                                    bits, value);
58
    }
59
60
50
    *write_to = value;
61
50
    return 0;
62
}
63
64
50
static int cbs_vp9_write_s(CodedBitstreamContext *ctx, PutBitContext *pbc,
65
                           int width, const char *name,
66
                           const int *subscripts, int32_t value)
67
{
68
    uint32_t magnitude;
69
    int sign;
70
71
50
    if (put_bits_left(pbc) < width + 1)
72
        return AVERROR(ENOSPC);
73
74
50
    sign      = value < 0;
75
50
    magnitude = sign ? -value : value;
76
77
50
    if (ctx->trace_enable) {
78
        char bits[33];
79
        int i;
80
        for (i = 0; i < width; i++)
81
            bits[i] = magnitude >> (width - i - 1) & 1 ? '1' : '0';
82
        bits[i] = sign ? '1' : '0';
83
        bits[i + 1] = 0;
84
85
        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
86
                                    name, subscripts, bits, value);
87
    }
88
89
50
    put_bits(pbc, width, magnitude);
90
50
    put_bits(pbc, 1, sign);
91
92
50
    return 0;
93
}
94
95
362
static int cbs_vp9_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc,
96
                                  uint32_t range_min, uint32_t range_max,
97
                                  const char *name, uint32_t *write_to)
98
{
99
    uint32_t value;
100
    int position, i;
101
    char bits[8];
102
103

362
    av_assert0(range_min <= range_max && range_max - range_min < sizeof(bits) - 1);
104
362
    if (ctx->trace_enable)
105
        position = get_bits_count(gbc);
106
107
366
    for (i = 0, value = range_min; value < range_max;) {
108
187
        if (get_bits_left(gbc) < 1) {
109
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
110
                   "%s: bitstream ended.\n", name);
111
            return AVERROR_INVALIDDATA;
112
        }
113
187
        if (get_bits1(gbc)) {
114
4
            bits[i++] = '1';
115
4
            ++value;
116
        } else {
117
183
            bits[i++] = '0';
118
183
            break;
119
        }
120
    }
121
122
362
    if (ctx->trace_enable) {
123
        bits[i] = 0;
124
        ff_cbs_trace_syntax_element(ctx, position, name, NULL, bits, value);
125
    }
126
127
362
    *write_to = value;
128
362
    return 0;
129
}
130
131
362
static int cbs_vp9_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc,
132
                                   uint32_t range_min, uint32_t range_max,
133
                                   const char *name, uint32_t value)
134
{
135
    int len;
136
137

362
    av_assert0(range_min <= range_max && range_max - range_min < 8);
138

362
    if (value < range_min || value > range_max) {
139
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
140
               "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
141
               name, value, range_min, range_max);
142
        return AVERROR_INVALIDDATA;
143
    }
144
145
362
    if (value == range_max)
146
179
        len = range_max - range_min;
147
    else
148
183
        len = value - range_min + 1;
149
362
    if (put_bits_left(pbc) < len)
150
        return AVERROR(ENOSPC);
151
152
362
    if (ctx->trace_enable) {
153
        char bits[8];
154
        int i;
155
        for (i = 0; i < len; i++) {
156
            if (range_min + i == value)
157
                bits[i] = '0';
158
            else
159
                bits[i] = '1';
160
        }
161
        bits[i] = 0;
162
        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
163
                                    name, NULL, bits, value);
164
    }
165
166
362
    if (len > 0)
167
183
        put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
168
169
362
    return 0;
170
}
171
172
27
static int cbs_vp9_read_le(CodedBitstreamContext *ctx, GetBitContext *gbc,
173
                           int width, const char *name,
174
                           const int *subscripts, uint32_t *write_to)
175
{
176
    uint32_t value;
177
    int position, b;
178
179
27
    av_assert0(width % 8 == 0);
180
181
27
    if (ctx->trace_enable)
182
        position = get_bits_count(gbc);
183
184
27
    if (get_bits_left(gbc) < width) {
185
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid le value at "
186
               "%s: bitstream ended.\n", name);
187
        return AVERROR_INVALIDDATA;
188
    }
189
190
27
    value = 0;
191
81
    for (b = 0; b < width; b += 8)
192
54
        value |= get_bits(gbc, 8) << b;
193
194
27
    if (ctx->trace_enable) {
195
        char bits[33];
196
        int i;
197
        for (b = 0; b < width; b += 8)
198
            for (i = 0; i < 8; i++)
199
                bits[b + i] = value >> (b + i) & 1 ? '1' : '0';
200
        bits[b] = 0;
201
202
        ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
203
                                    bits, value);
204
    }
205
206
27
    *write_to = value;
207
27
    return 0;
208
}
209
210
27
static int cbs_vp9_write_le(CodedBitstreamContext *ctx, PutBitContext *pbc,
211
                            int width, const char *name,
212
                            const int *subscripts, uint32_t value)
213
{
214
    int b;
215
216
27
    av_assert0(width % 8 == 0);
217
218
27
    if (put_bits_left(pbc) < width)
219
        return AVERROR(ENOSPC);
220
221
27
    if (ctx->trace_enable) {
222
        char bits[33];
223
        int i;
224
        for (b = 0; b < width; b += 8)
225
            for (i = 0; i < 8; i++)
226
                bits[b + i] = value >> (b + i) & 1 ? '1' : '0';
227
        bits[b] = 0;
228
229
        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
230
                                    name, subscripts, bits, value);
231
    }
232
233
81
    for (b = 0; b < width; b += 8)
234
54
        put_bits(pbc, 8, value >> b & 0xff);
235
236
27
    return 0;
237
}
238
239
#define HEADER(name) do { \
240
        ff_cbs_trace_header(ctx, name); \
241
    } while (0)
242
243
#define CHECK(call) do { \
244
        err = (call); \
245
        if (err < 0) \
246
            return err; \
247
    } while (0)
248
249
#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
250
#define FUNC_VP9(rw, name) FUNC_NAME(rw, vp9, name)
251
#define FUNC(name) FUNC_VP9(READWRITE, name)
252
253
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
254
255
#define f(width, name) \
256
        xf(width, name, current->name, 0)
257
#define s(width, name) \
258
        xs(width, name, current->name, 0)
259
#define fs(width, name, subs, ...) \
260
        xf(width, name, current->name, subs, __VA_ARGS__)
261
#define ss(width, name, subs, ...) \
262
        xs(width, name, current->name, subs, __VA_ARGS__)
263
264
265
#define READ
266
#define READWRITE read
267
#define RWContext GetBitContext
268
269
#define xf(width, name, var, subs, ...) do { \
270
        uint32_t value; \
271
        CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
272
                                   SUBSCRIPTS(subs, __VA_ARGS__), \
273
                                   &value, 0, (1 << width) - 1)); \
274
        var = value; \
275
    } while (0)
276
#define xs(width, name, var, subs, ...) do { \
277
        int32_t value; \
278
        CHECK(cbs_vp9_read_s(ctx, rw, width, #name, \
279
                             SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
280
        var = value; \
281
    } while (0)
282
283
284
#define increment(name, min, max) do { \
285
        uint32_t value; \
286
        CHECK(cbs_vp9_read_increment(ctx, rw, min, max, #name, &value)); \
287
        current->name = value; \
288
    } while (0)
289
290
#define fle(width, name, subs, ...) do { \
291
        CHECK(cbs_vp9_read_le(ctx, rw, width, #name, \
292
                              SUBSCRIPTS(subs, __VA_ARGS__), &current->name)); \
293
    } while (0)
294
295
#define delta_q(name) do { \
296
        uint8_t delta_coded; \
297
        int8_t delta_q; \
298
        xf(1, name.delta_coded, delta_coded, 0); \
299
        if (delta_coded) \
300
            xs(4, name.delta_q, delta_q, 0); \
301
        else \
302
            delta_q = 0; \
303
        current->name = delta_q; \
304
    } while (0)
305
306
#define prob(name, subs, ...) do { \
307
        uint8_t prob_coded; \
308
        uint8_t prob; \
309
        xf(1, name.prob_coded, prob_coded, subs, __VA_ARGS__); \
310
        if (prob_coded) \
311
            xf(8, name.prob, prob, subs, __VA_ARGS__); \
312
        else \
313
            prob = 255; \
314
        current->name = prob; \
315
    } while (0)
316
317
#define fixed(width, name, value) do { \
318
        av_unused uint32_t fixed_value; \
319
        CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
320
                                   0, &fixed_value, value, value)); \
321
    } while (0)
322
323
#define infer(name, value) do { \
324
        current->name = value; \
325
    } while (0)
326
327
#define byte_alignment(rw) (get_bits_count(rw) % 8)
328
329
#include "cbs_vp9_syntax_template.c"
330
331
#undef READ
332
#undef READWRITE
333
#undef RWContext
334
#undef xf
335
#undef xs
336
#undef increment
337
#undef fle
338
#undef delta_q
339
#undef prob
340
#undef fixed
341
#undef infer
342
#undef byte_alignment
343
344
345
#define WRITE
346
#define READWRITE write
347
#define RWContext PutBitContext
348
349
#define xf(width, name, var, subs, ...) do { \
350
        CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
351
                                    SUBSCRIPTS(subs, __VA_ARGS__), \
352
                                    var, 0, (1 << width) - 1)); \
353
    } while (0)
354
#define xs(width, name, var, subs, ...) do { \
355
        CHECK(cbs_vp9_write_s(ctx, rw, width, #name, \
356
                              SUBSCRIPTS(subs, __VA_ARGS__), var)); \
357
    } while (0)
358
359
#define increment(name, min, max) do { \
360
        CHECK(cbs_vp9_write_increment(ctx, rw, min, max, #name, current->name)); \
361
    } while (0)
362
363
#define fle(width, name, subs, ...) do { \
364
        CHECK(cbs_vp9_write_le(ctx, rw, width, #name, \
365
                               SUBSCRIPTS(subs, __VA_ARGS__), current->name)); \
366
    } while (0)
367
368
#define delta_q(name) do { \
369
        xf(1, name.delta_coded, !!current->name, 0); \
370
        if (current->name) \
371
            xs(4, name.delta_q, current->name, 0); \
372
    } while (0)
373
374
#define prob(name, subs, ...) do { \
375
        xf(1, name.prob_coded, current->name != 255, subs, __VA_ARGS__); \
376
        if (current->name != 255) \
377
            xf(8, name.prob, current->name, subs, __VA_ARGS__); \
378
    } while (0)
379
380
#define fixed(width, name, value) do { \
381
        CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
382
                                    0, value, value, value)); \
383
    } while (0)
384
385
#define infer(name, value) do { \
386
        if (current->name != (value)) { \
387
            av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
388
                   "%s does not match inferred value: " \
389
                   "%"PRId64", but should be %"PRId64".\n", \
390
                   #name, (int64_t)current->name, (int64_t)(value)); \
391
        } \
392
    } while (0)
393
394
#define byte_alignment(rw) (put_bits_count(rw) % 8)
395
396
#include "cbs_vp9_syntax_template.c"
397
398
#undef WRITE
399
#undef READWRITE
400
#undef RWContext
401
#undef xf
402
#undef xs
403
#undef increment
404
#undef fle
405
#undef delta_q
406
#undef prob
407
#undef fixed
408
#undef infer
409
#undef byte_alignment
410
411
412
183
static int cbs_vp9_split_fragment(CodedBitstreamContext *ctx,
413
                                  CodedBitstreamFragment *frag,
414
                                  int header)
415
{
416
    uint8_t superframe_header;
417
    int err;
418
419
    // Last byte in the packet.
420
183
    superframe_header = frag->data[frag->data_size - 1];
421
422
183
    if ((superframe_header & 0xe0) == 0xc0) {
423
        VP9RawSuperframeIndex sfi;
424
        GetBitContext gbc;
425
        size_t index_size, pos;
426
        int i;
427
428
13
        index_size = 2 + (((superframe_header & 0x18) >> 3) + 1) *
429
13
                          ((superframe_header & 0x07) + 1);
430
431
13
        err = init_get_bits(&gbc, frag->data + frag->data_size - index_size,
432
                            8 * index_size);
433
13
        if (err < 0)
434
            return err;
435
436
13
        err = cbs_vp9_read_superframe_index(ctx, &gbc, &sfi);
437
13
        if (err < 0)
438
            return err;
439
440
13
        pos = 0;
441
40
        for (i = 0; i <= sfi.frames_in_superframe_minus_1; i++) {
442
27
            if (pos + sfi.frame_sizes[i] + index_size > frag->data_size) {
443
                av_log(ctx->log_ctx, AV_LOG_ERROR, "Frame %d too large "
444
                       "in superframe: %"PRIu32" bytes.\n",
445
                       i, sfi.frame_sizes[i]);
446
                return AVERROR_INVALIDDATA;
447
            }
448
449
27
            err = ff_cbs_insert_unit_data(ctx, frag, -1, 0,
450
27
                                          frag->data + pos,
451
27
                                          sfi.frame_sizes[i],
452
                                          frag->data_ref);
453
27
            if (err < 0)
454
                return err;
455
456
27
            pos += sfi.frame_sizes[i];
457
        }
458
13
        if (pos + index_size != frag->data_size) {
459
            av_log(ctx->log_ctx, AV_LOG_WARNING, "Extra padding at "
460
                   "end of superframe: %"SIZE_SPECIFIER" bytes.\n",
461
                   frag->data_size - (pos + index_size));
462
        }
463
464
13
        return 0;
465
466
    } else {
467
170
        err = ff_cbs_insert_unit_data(ctx, frag, -1, 0,
468
                                      frag->data, frag->data_size,
469
                                      frag->data_ref);
470
170
        if (err < 0)
471
            return err;
472
    }
473
474
170
    return 0;
475
}
476
477
197
static void cbs_vp9_free_frame(void *opaque, uint8_t *content)
478
{
479
197
    VP9RawFrame *frame = (VP9RawFrame*)content;
480
197
    av_buffer_unref(&frame->data_ref);
481
197
    av_freep(&frame);
482
197
}
483
484
197
static int cbs_vp9_read_unit(CodedBitstreamContext *ctx,
485
                             CodedBitstreamUnit *unit)
486
{
487
    VP9RawFrame *frame;
488
    GetBitContext gbc;
489
    int err, pos;
490
491
197
    err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
492
197
    if (err < 0)
493
        return err;
494
495
197
    err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*frame),
496
                                    &cbs_vp9_free_frame);
497
197
    if (err < 0)
498
        return err;
499
197
    frame = unit->content;
500
501
197
    err = cbs_vp9_read_frame(ctx, &gbc, frame);
502
197
    if (err < 0)
503
        return err;
504
505
197
    pos = get_bits_count(&gbc);
506
197
    av_assert0(pos % 8 == 0);
507
197
    pos /= 8;
508
197
    av_assert0(pos <= unit->data_size);
509
510
197
    if (pos == unit->data_size) {
511
        // No data (e.g. a show-existing-frame frame).
512
    } else {
513
181
        frame->data_ref = av_buffer_ref(unit->data_ref);
514
181
        if (!frame->data_ref)
515
            return AVERROR(ENOMEM);
516
517
181
        frame->data      = unit->data      + pos;
518
181
        frame->data_size = unit->data_size - pos;
519
    }
520
521
197
    return 0;
522
}
523
524
197
static int cbs_vp9_write_unit(CodedBitstreamContext *ctx,
525
                              CodedBitstreamUnit *unit,
526
                              PutBitContext *pbc)
527
{
528
197
    VP9RawFrame *frame = unit->content;
529
    int err;
530
531
197
    err = cbs_vp9_write_frame(ctx, pbc, frame);
532
197
    if (err < 0)
533
        return err;
534
535
    // Frame must be byte-aligned.
536
197
    av_assert0(put_bits_count(pbc) % 8 == 0);
537
538
197
    if (frame->data) {
539
181
        if (frame->data_size > put_bits_left(pbc) / 8)
540
            return AVERROR(ENOSPC);
541
542
181
        flush_put_bits(pbc);
543
181
        memcpy(put_bits_ptr(pbc), frame->data, frame->data_size);
544
181
        skip_put_bytes(pbc, frame->data_size);
545
    }
546
547
197
    return 0;
548
}
549
550
183
static int cbs_vp9_assemble_fragment(CodedBitstreamContext *ctx,
551
                                     CodedBitstreamFragment *frag)
552
{
553
    int err;
554
555
183
    if (frag->nb_units == 1) {
556
        // Output is just the content of the single frame.
557
558
170
        CodedBitstreamUnit *frame = &frag->units[0];
559
560
170
        frag->data_ref = av_buffer_ref(frame->data_ref);
561
170
        if (!frag->data_ref)
562
            return AVERROR(ENOMEM);
563
564
170
        frag->data      = frame->data;
565
170
        frag->data_size = frame->data_size;
566
567
    } else {
568
        // Build superframe out of frames.
569
570
        VP9RawSuperframeIndex sfi;
571
        PutBitContext pbc;
572
        AVBufferRef *ref;
573
        uint8_t *data;
574
        size_t size, max, pos;
575
        int i, size_len;
576
577
13
        if (frag->nb_units > 8) {
578
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many frames to "
579
                   "make superframe: %d.\n", frag->nb_units);
580
            return AVERROR(EINVAL);
581
        }
582
583
13
        max = 0;
584
40
        for (i = 0; i < frag->nb_units; i++)
585
27
            if (max < frag->units[i].data_size)
586
14
                max = frag->units[i].data_size;
587
588
13
        if (max < 2)
589
            size_len = 1;
590
        else
591
13
            size_len = av_log2(max) / 8 + 1;
592
13
        av_assert0(size_len <= 4);
593
594
13
        sfi.superframe_marker            = VP9_SUPERFRAME_MARKER;
595
13
        sfi.bytes_per_framesize_minus_1  = size_len - 1;
596
13
        sfi.frames_in_superframe_minus_1 = frag->nb_units - 1;
597
598
13
        size = 2;
599
40
        for (i = 0; i < frag->nb_units; i++) {
600
27
            size += size_len + frag->units[i].data_size;
601
27
            sfi.frame_sizes[i] = frag->units[i].data_size;
602
        }
603
604
13
        ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
605
13
        if (!ref)
606
            return AVERROR(ENOMEM);
607
13
        data = ref->data;
608
13
        memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
609
610
13
        pos = 0;
611
40
        for (i = 0; i < frag->nb_units; i++) {
612
27
            av_assert0(size - pos > frag->units[i].data_size);
613
27
            memcpy(data + pos, frag->units[i].data,
614
27
                   frag->units[i].data_size);
615
27
            pos += frag->units[i].data_size;
616
        }
617
13
        av_assert0(size - pos == 2 + frag->nb_units * size_len);
618
619
13
        init_put_bits(&pbc, data + pos, size - pos);
620
621
13
        err = cbs_vp9_write_superframe_index(ctx, &pbc, &sfi);
622
13
        if (err < 0) {
623
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write "
624
                   "superframe index.\n");
625
            av_buffer_unref(&ref);
626
            return err;
627
        }
628
629
13
        av_assert0(put_bits_left(&pbc) == 0);
630
13
        flush_put_bits(&pbc);
631
632
13
        frag->data_ref  = ref;
633
13
        frag->data      = data;
634
13
        frag->data_size = size;
635
    }
636
637
183
    return 0;
638
}
639
640
const CodedBitstreamType ff_cbs_type_vp9 = {
641
    .codec_id          = AV_CODEC_ID_VP9,
642
643
    .priv_data_size    = sizeof(CodedBitstreamVP9Context),
644
645
    .split_fragment    = &cbs_vp9_split_fragment,
646
    .read_unit         = &cbs_vp9_read_unit,
647
    .write_unit        = &cbs_vp9_write_unit,
648
    .assemble_fragment = &cbs_vp9_assemble_fragment,
649
};