aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/cbs_vp8.c
blob: 1f80f34fafee88d120090e79aa23fcfd60864cdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/avassert.h"

#include "cbs.h"
#include "cbs_internal.h"
#include "cbs_vp8.h"

#include <stdbool.h>

#define DEFAULT_PROB 0x80

// The probability table is defined in 'vp8data.c'.
extern const uint8_t ff_vp8_token_update_probs[4][8][3][11];

// Implements VP8 boolean decoder using GetBitContext to read the bitstream.
typedef struct CBSVP8BoolDecoder {
    GetBitContext *gbc;

    uint8_t value;
    uint8_t range;

    uint8_t count; // Store the number of bits in the `value` buffer.

} CBSVP8BoolDecoder;

static int cbs_vp8_bool_decoder_init(CBSVP8BoolDecoder *decoder, GetBitContext *gbc)
{
    av_assert0(decoder);
    av_assert0(gbc);

    decoder->gbc = gbc;
    decoder->value = 0;
    decoder->range = 255;

    decoder->count = 0;

    return 0;
}

static bool cbs_vp8_bool_decoder_fill_value(CBSVP8BoolDecoder *decoder)
{
    int bits = 8 - decoder->count;

    av_assert0(decoder->count <= 8);
    if (decoder->count == 8) {
      return true;
    }

    if (get_bits_left(decoder->gbc) >= bits) {
        decoder->value |= get_bits(decoder->gbc, bits);
        decoder->count += bits;
    }

    return (decoder->count == 8);
}

static int cbs_vp8_bool_decoder_read_bool(CBSVP8BoolDecoder *decoder,
                                          const uint8_t prob, uint8_t *output)
{
    uint8_t split = 1 + (((decoder->range - 1) * prob) >> 8);

    if (!cbs_vp8_bool_decoder_fill_value(decoder)) {
        return AVERROR_INVALIDDATA;
    }

    av_assert0(decoder->count == 8);
    if (decoder->value >= split) {
        *output = 1;
        decoder->range -= split;
        decoder->value -= split;
    } else {
        *output = 0;
        decoder->range = split;
    }

    while (decoder->range < 128) {
        decoder->value <<= 1;
        decoder->range <<= 1;
        --decoder->count;
    }

    return 0;
}

static int cbs_vp8_bool_decoder_read_literal(CBSVP8BoolDecoder *decoder,
                                             const uint8_t prob,
                                             uint32_t num_bits,
                                             uint32_t *output)
{
    int ret = 0;

    av_assert0(num_bits <= 32);

    *output = 0;
    for (; num_bits > 0; --num_bits) {
        uint8_t bit_output = 0;
        if ((ret = cbs_vp8_bool_decoder_read_bool(decoder, prob,
                                                  &bit_output)) != 0) {
            return ret;
        }

        *output = (*output << 1) | bit_output;
    }

    return 0;
}

static int cbs_vp8_bool_decoder_read_unsigned(
    CodedBitstreamContext *ctx, CBSVP8BoolDecoder *bool_decoder, int width,
    uint8_t prob, const char *name, const int *subscripts, uint32_t *write_to,
    bool trace_enable)
{
    int ret = 0;
    GetBitContext *gbc = bool_decoder->gbc;
    uint32_t value;

    CBS_TRACE_READ_START();

    av_assert0(width >= 0 && width <= 8);

    ret = cbs_vp8_bool_decoder_read_literal(bool_decoder, prob, width, &value);
    if (ret != 0) {
        return ret;
    }

    if (trace_enable) {
      CBS_TRACE_READ_END();
    }

    *write_to = value;
    return 0;
}

static int cbs_vp8_bool_decoder_read_signed(
    CodedBitstreamContext *ctx, CBSVP8BoolDecoder *bool_decoder, int width,
    uint8_t prob, const char *name, const int *subscripts, int32_t *write_to)
{
    int ret = 0;
    GetBitContext *gbc = bool_decoder->gbc;
    int32_t value;
    uint8_t sign = 0;

    CBS_TRACE_READ_START();

    av_assert0(width >= 0 && width <= 8);

    ret = cbs_vp8_bool_decoder_read_literal(bool_decoder, prob, width, &value);
    if (ret != 0) {
        return ret;
    }

    ret = cbs_vp8_bool_decoder_read_bool(bool_decoder, prob, &sign);
    if (ret != 0) {
        return ret;
    }

    if (sign) {
        value = -value;
    }

    CBS_TRACE_READ_END();

    *write_to = value;
    return 0;
}

static int cbs_vp8_read_unsigned_le(CodedBitstreamContext *ctx,
                                    GetBitContext *gbc, int width,
                                    const char *name, const int *subscripts,
                                    uint32_t *write_to, uint32_t range_min,
                                    uint32_t range_max)
{
    int32_t value;

    CBS_TRACE_READ_START();

    av_assert0(width > 0 && width <= 24);

    if (get_bits_left(gbc) < width) {
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value: bitstream ended.\n");
        return AVERROR_INVALIDDATA;
    }

    value = get_bits_le(gbc, width);

    CBS_TRACE_READ_END();

    if (value < range_min || value > range_max) {
        av_log(ctx->log_ctx, AV_LOG_ERROR,
               "%s out of range: "
               "%" PRIu32 ", but must be in [%" PRIu32 ",%" PRIu32 "].\n",
               name, value, range_min, range_max);
        return AVERROR_INVALIDDATA;
    }

    *write_to = value;
    return 0;
}

#define HEADER(name) \
    do { \
        ff_cbs_trace_header(ctx, name); \
    } while (0)

#define CHECK(call) \
    do { \
        int err = (call); \
        if (err < 0) \
            return err; \
    } while (0)

#define FUNC_NAME(rw, codec, name) cbs_##codec##_##rw##_##name
#define FUNC_VP8(rw, name) FUNC_NAME(rw, vp8, name)
#define FUNC(name) FUNC_VP8(READWRITE, name)

#define SUBSCRIPTS(subs, ...) \
    (subs > 0 ? ((int[subs + 1]){subs, __VA_ARGS__}) : NULL)

#define f(width, name) xf(width, name, 0, )

// bool [de|en]coder methods.
#define bc_f(width, name) bc_unsigned_subs(width, DEFAULT_PROB, true, name, 0, )
#define bc_s(width, name) bc_signed_subs(width, DEFAULT_PROB, name, 0, )
#define bc_fs(width, name, subs, ...) \
    bc_unsigned_subs(width, DEFAULT_PROB, true, name, subs, __VA_ARGS__)
#define bc_ss(width, name, subs, ...) \
    bc_signed_subs(width, DEFAULT_PROB, name, subs, __VA_ARGS__)

// bool [de|en]coder methods for boolean value and disable tracing.
#define bc_b(name) bc_unsigned_subs(1, DEFAULT_PROB, false, name, 0, )
#define bc_b_prob(prob, name) bc_unsigned_subs(1, prob, false, name, 0, )

#define READ
#define READWRITE read
#define RWContext GetBitContext
#define CBSVP8BoolCodingRW CBSVP8BoolDecoder

#define xf(width, name, subs, ...) \
    do { \
        uint32_t value; \
        CHECK(cbs_vp8_read_unsigned_le(ctx, rw, width, #name, \
                                       SUBSCRIPTS(subs, __VA_ARGS__), &value, \
                                       0, MAX_UINT_BITS(width))); \
        current->name = value; \
    } while (0)

#define fixed(width, name, value) \
    do { \
        uint32_t fixed_value; \
        CHECK(cbs_vp8_read_unsigned_le(ctx, rw, width, #name, 0, &fixed_value, \
                                       value, value)); \
    } while (0)

#define bc_unsigned_subs(width, prob, enable_trace, name, subs, ...) \
    do { \
        uint32_t value; \
        CHECK(cbs_vp8_bool_decoder_read_unsigned( \
            ctx, bool_coding_rw, width, prob, #name, \
            SUBSCRIPTS(subs, __VA_ARGS__), &value, enable_trace)); \
        current->name = value; \
    } while (0)

#define bc_signed_subs(width, prob, name, subs, ...) \
    do { \
        int32_t value; \
        CHECK(cbs_vp8_bool_decoder_read_signed( \
            ctx, bool_coding_rw, width, prob, #name, \
            SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
        current->name = value; \
    } while (0)

#include "cbs_vp8_syntax_template.c"

static int cbs_vp8_split_fragment(CodedBitstreamContext *ctx,
                                  CodedBitstreamFragment *frag, int header)
{
    int err;

    if (frag->data_size == 0)
        return AVERROR_INVALIDDATA;

    err = ff_cbs_append_unit_data(frag, 0, frag->data, frag->data_size,
                                  frag->data_ref);
    if (err < 0)
        return err;

    return 0;
}

static int cbs_vp8_read_unit(CodedBitstreamContext *ctx,
                             CodedBitstreamUnit *unit)
{
    VP8RawFrame *frame;
    GetBitContext gbc;
    CBSVP8BoolDecoder bool_decoder;
    int err, pos;

    err = ff_cbs_alloc_unit_content(ctx, unit);
    if (err < 0)
        return err;
    frame = unit->content;

    // Create GetBitContext for uncompressed header.
    err = init_get_bits8_le(&gbc, unit->data, unit->data_size);
    if (err < 0)
        return err;

    err = cbs_vp8_read_uncompressed_header(ctx, &gbc, frame);
    if (err < 0)
        return err;

    pos = get_bits_count(&gbc);
    av_assert0(pos % 8 == 0);

    // Create boolean decoder for compressed header.
    err = cbs_vp8_bool_decoder_init(&bool_decoder, &gbc);
    if (err < 0)
        return err;

    err = cbs_vp8_read_compressed_header(ctx, &bool_decoder, frame);
    if (err < 0)
        return err;

    pos = get_bits_count(&gbc);
    // Position may not be byte-aligned after compressed header; Round up byte
    // count for accurate data positioning.
    pos = (pos + 7) / 8;
    av_assert0(pos <= unit->data_size);

    frame->data_ref = av_buffer_ref(unit->data_ref);
    if (!frame->data_ref)
        return AVERROR(ENOMEM);

    frame->data = unit->data + pos;
    frame->data_size = unit->data_size - pos;

    return 0;
}

static int cbs_vp8_write_unit(CodedBitstreamContext *ctx,
                              CodedBitstreamUnit *unit, PutBitContext *pbc)
{
    return AVERROR_PATCHWELCOME;
}

static int cbs_vp8_assemble_fragment(CodedBitstreamContext *ctx,
                                     CodedBitstreamFragment *frag)
{
    return AVERROR_PATCHWELCOME;
}

static const CodedBitstreamUnitTypeDescriptor cbs_vp8_unit_types[] = {
    CBS_UNIT_TYPE_INTERNAL_REF(0, VP8RawFrame, data),
    CBS_UNIT_TYPE_END_OF_LIST,
};

const CodedBitstreamType ff_cbs_type_vp8 = {
    .codec_id          = AV_CODEC_ID_VP8,

    .priv_data_size    = 0,

    .unit_types        = cbs_vp8_unit_types,

    .split_fragment    = &cbs_vp8_split_fragment,
    .read_unit         = &cbs_vp8_read_unit,
    .write_unit        = &cbs_vp8_write_unit,

    .assemble_fragment = &cbs_vp8_assemble_fragment,
};