GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_mpeg2.c Lines: 110 129 85.3 %
Date: 2019-11-18 18:00:01 Branches: 57 90 63.3 %

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_mpeg2.h"
24
#include "internal.h"
25
26
27
#define HEADER(name) do { \
28
        ff_cbs_trace_header(ctx, name); \
29
    } while (0)
30
31
#define CHECK(call) do { \
32
        err = (call); \
33
        if (err < 0) \
34
            return err; \
35
    } while (0)
36
37
#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
38
#define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
39
#define FUNC(name) FUNC_MPEG2(READWRITE, name)
40
41
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
42
43
#define ui(width, name) \
44
        xui(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
45
#define uir(width, name) \
46
        xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0)
47
#define uis(width, name, subs, ...) \
48
        xui(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
49
#define uirs(width, name, subs, ...) \
50
        xui(width, name, current->name, 1, MAX_UINT_BITS(width), subs, __VA_ARGS__)
51
#define xui(width, name, var, range_min, range_max, subs, ...) \
52
        xuia(width, #name, var, range_min, range_max, subs, __VA_ARGS__)
53
#define sis(width, name, subs, ...) \
54
        xsi(width, name, current->name, subs, __VA_ARGS__)
55
56
#define marker_bit() \
57
        bit("marker_bit", 1)
58
#define bit(string, value) do { \
59
        av_unused uint32_t bit = value; \
60
        xuia(1, string, bit, value, value, 0); \
61
    } while (0)
62
63
64
#define READ
65
#define READWRITE read
66
#define RWContext GetBitContext
67
68
#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
69
        uint32_t value; \
70
        CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
71
                                   SUBSCRIPTS(subs, __VA_ARGS__), \
72
                                   &value, range_min, range_max)); \
73
        var = value; \
74
    } while (0)
75
76
#define xsi(width, name, var, subs, ...) do { \
77
        int32_t value; \
78
        CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
79
                                 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
80
                                 MIN_INT_BITS(width), \
81
                                 MAX_INT_BITS(width))); \
82
        var = value; \
83
    } while (0)
84
85
#define nextbits(width, compare, var) \
86
    (get_bits_left(rw) >= width && \
87
     (var = show_bits(rw, width)) == (compare))
88
89
#define infer(name, value) do { \
90
        current->name = value; \
91
    } while (0)
92
93
#include "cbs_mpeg2_syntax_template.c"
94
95
#undef READ
96
#undef READWRITE
97
#undef RWContext
98
#undef xuia
99
#undef xsi
100
#undef nextbits
101
#undef infer
102
103
104
#define WRITE
105
#define READWRITE write
106
#define RWContext PutBitContext
107
108
#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
109
        CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
110
                                    SUBSCRIPTS(subs, __VA_ARGS__), \
111
                                    var, range_min, range_max)); \
112
    } while (0)
113
114
#define xsi(width, name, var, subs, ...) do { \
115
        CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
116
                                  SUBSCRIPTS(subs, __VA_ARGS__), var, \
117
                                  MIN_INT_BITS(width), \
118
                                  MAX_INT_BITS(width))); \
119
    } while (0)
120
121
#define nextbits(width, compare, var) (var)
122
123
#define infer(name, value) do { \
124
        if (current->name != (value)) { \
125
            av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
126
                   "%s does not match inferred value: " \
127
                   "%"PRId64", but should be %"PRId64".\n", \
128
                   #name, (int64_t)current->name, (int64_t)(value)); \
129
        } \
130
    } while (0)
131
132
#include "cbs_mpeg2_syntax_template.c"
133
134
#undef WRITE
135
#undef READWRITE
136
#undef RWContext
137
#undef xuia
138
#undef xsi
139
#undef nextbits
140
#undef infer
141
142
143
19
static void cbs_mpeg2_free_picture_header(void *opaque, uint8_t *content)
144
{
145
19
    MPEG2RawPictureHeader *picture = (MPEG2RawPictureHeader*)content;
146
19
    av_buffer_unref(&picture->extra_information_picture.extra_information_ref);
147
19
    av_freep(&content);
148
19
}
149
150
14
static void cbs_mpeg2_free_user_data(void *opaque, uint8_t *content)
151
{
152
14
    MPEG2RawUserData *user = (MPEG2RawUserData*)content;
153
14
    av_buffer_unref(&user->user_data_ref);
154
14
    av_freep(&content);
155
14
}
156
157
8746
static void cbs_mpeg2_free_slice(void *opaque, uint8_t *content)
158
{
159
8746
    MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
160
8746
    av_buffer_unref(&slice->header.extra_information_slice.extra_information_ref);
161
8746
    av_buffer_unref(&slice->data_ref);
162
8746
    av_freep(&content);
163
8746
}
164
165
18
static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
166
                                    CodedBitstreamFragment *frag,
167
                                    int header)
168
{
169
    const uint8_t *start, *end;
170
    CodedBitstreamUnitType unit_type;
171
18
    uint32_t start_code = -1;
172
    size_t unit_size;
173
18
    int err, i, final = 0;
174
175
18
    start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
176
                                   &start_code);
177
18
    if (start_code >> 8 != 0x000001) {
178
        // No start code found.
179
        return AVERROR_INVALIDDATA;
180
    }
181
182
18
    for (i = 0;; i++) {
183
8843
        unit_type = start_code & 0xff;
184
185
8843
        if (start == frag->data + frag->data_size) {
186
            // The last four bytes form a start code which constitutes
187
            // a unit of its own.  In this situation avpriv_find_start_code
188
            // won't modify start_code at all so modify start_code so that
189
            // the next unit will be treated as the last unit.
190
3
            start_code = 0;
191
        }
192
193
8843
        end = avpriv_find_start_code(start--, frag->data + frag->data_size,
194
                                     &start_code);
195
196
        // start points to the byte containing the start_code_identifier
197
        // (may be the last byte of fragment->data); end points to the byte
198
        // following the byte containing the start code identifier (or to
199
        // the end of fragment->data).
200
8843
        if (start_code >> 8 == 0x000001) {
201
            // Unit runs from start to the beginning of the start code
202
            // pointed to by end (including any padding zeroes).
203
8825
            unit_size = (end - 4) - start;
204
        } else {
205
           // We didn't find a start code, so this is the final unit.
206
18
           unit_size = end - start;
207
18
           final     = 1;
208
        }
209
210
8843
        err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type, (uint8_t*)start,
211
                                      unit_size, frag->data_ref);
212
8843
        if (err < 0)
213
            return err;
214
215
8843
        if (final)
216
18
            break;
217
218
8825
        start = end;
219
    }
220
221
18
    return 0;
222
}
223
224
8843
static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
225
                               CodedBitstreamUnit *unit)
226
{
227
    GetBitContext gbc;
228
    int err;
229
230
8843
    err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
231
8843
    if (err < 0)
232
        return err;
233
234

8843
    if (MPEG2_START_IS_SLICE(unit->type)) {
235
        MPEG2RawSlice *slice;
236
        int pos, len;
237
238
8746
        err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
239
                                        &cbs_mpeg2_free_slice);
240
8746
        if (err < 0)
241
            return err;
242
8746
        slice = unit->content;
243
244
8746
        err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
245
8746
        if (err < 0)
246
            return err;
247
248
8746
        pos = get_bits_count(&gbc);
249
8746
        len = unit->data_size;
250
251
8746
        slice->data_size = len - pos / 8;
252
8746
        slice->data_ref  = av_buffer_ref(unit->data_ref);
253
8746
        if (!slice->data_ref)
254
            return AVERROR(ENOMEM);
255
8746
        slice->data = unit->data + pos / 8;
256
257
8746
        slice->data_bit_start = pos % 8;
258
259
    } else {
260

97
        switch (unit->type) {
261
#define START(start_code, type, read_func, free_func) \
262
        case start_code: \
263
            { \
264
                type *header; \
265
                err = ff_cbs_alloc_unit_content(ctx, unit, \
266
                                                sizeof(*header), free_func); \
267
                if (err < 0) \
268
                    return err; \
269
                header = unit->content; \
270
                err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
271
                if (err < 0) \
272
                    return err; \
273
            } \
274
            break;
275

19
            START(MPEG2_START_PICTURE,   MPEG2RawPictureHeader,
276
                  picture_header,           &cbs_mpeg2_free_picture_header);
277

14
            START(MPEG2_START_USER_DATA, MPEG2RawUserData,
278
                  user_data,                &cbs_mpeg2_free_user_data);
279

6
            START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader,
280
                  sequence_header,          NULL);
281

53
            START(MPEG2_START_EXTENSION, MPEG2RawExtensionData,
282
                  extension_data,           NULL);
283

2
            START(MPEG2_START_GROUP,     MPEG2RawGroupOfPicturesHeader,
284
                  group_of_pictures_header, NULL);
285

3
            START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd,
286
                  sequence_end,             NULL);
287
#undef START
288
        default:
289
            return AVERROR(ENOSYS);
290
        }
291
    }
292
293
8843
    return 0;
294
}
295
296
97
static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
297
                                  CodedBitstreamUnit *unit,
298
                                  PutBitContext *pbc)
299
{
300
    int err;
301
302

97
    switch (unit->type) {
303
#define START(start_code, type, func) \
304
    case start_code: \
305
        err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
306
        break;
307
19
        START(MPEG2_START_PICTURE,         MPEG2RawPictureHeader,  picture_header);
308
14
        START(MPEG2_START_USER_DATA,       MPEG2RawUserData,       user_data);
309
6
        START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader, sequence_header);
310
53
        START(MPEG2_START_EXTENSION,       MPEG2RawExtensionData,  extension_data);
311
2
        START(MPEG2_START_GROUP,           MPEG2RawGroupOfPicturesHeader,
312
                                                         group_of_pictures_header);
313
3
        START(MPEG2_START_SEQUENCE_END,    MPEG2RawSequenceEnd,    sequence_end);
314
#undef START
315
    default:
316
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
317
               "code %02"PRIx32".\n", unit->type);
318
        return AVERROR_PATCHWELCOME;
319
    }
320
321
97
    return err;
322
}
323
324
8746
static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
325
                                 CodedBitstreamUnit *unit,
326
                                 PutBitContext *pbc)
327
{
328
8746
    MPEG2RawSlice *slice = unit->content;
329
    int err;
330
331
8746
    err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
332
8746
    if (err < 0)
333
        return err;
334
335
8746
    if (slice->data) {
336
8746
        size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
337
8746
        uint8_t *pos = slice->data + slice->data_bit_start / 8;
338
339

8746
        av_assert0(slice->data_bit_start >= 0 &&
340
                   slice->data_size > slice->data_bit_start / 8);
341
342
8746
        if (slice->data_size * 8 + 8 > put_bits_left(pbc))
343
            return AVERROR(ENOSPC);
344
345
        // First copy the remaining bits of the first byte
346
8746
        if (slice->data_bit_start % 8)
347
8746
            put_bits(pbc, 8 - slice->data_bit_start % 8,
348
8746
                     *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
349
350
8746
        if (put_bits_count(pbc) % 8 == 0) {
351
            // If the writer is aligned at this point,
352
            // memcpy can be used to improve performance.
353
            // This is the normal case.
354
8746
            flush_put_bits(pbc);
355
8746
            memcpy(put_bits_ptr(pbc), pos, rest);
356
8746
            skip_put_bytes(pbc, rest);
357
        } else {
358
            // If not, we have to copy manually:
359
            for (; rest > 3; rest -= 4, pos += 4)
360
                put_bits32(pbc, AV_RB32(pos));
361
362
            for (; rest; rest--, pos++)
363
                put_bits(pbc, 8, *pos);
364
365
            // Align with zeros
366
            put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
367
        }
368
    }
369
370
8746
    return 0;
371
}
372
373
8843
static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
374
                                CodedBitstreamUnit *unit,
375
                                PutBitContext *pbc)
376
{
377

8843
    if (MPEG2_START_IS_SLICE(unit->type))
378
8746
        return cbs_mpeg2_write_slice (ctx, unit, pbc);
379
    else
380
97
        return cbs_mpeg2_write_header(ctx, unit, pbc);
381
}
382
383
18
static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
384
                                       CodedBitstreamFragment *frag)
385
{
386
    uint8_t *data;
387
    size_t size, dp;
388
    int i;
389
390
18
    size = 0;
391
8861
    for (i = 0; i < frag->nb_units; i++)
392
8843
        size += 3 + frag->units[i].data_size;
393
394
18
    frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
395
18
    if (!frag->data_ref)
396
        return AVERROR(ENOMEM);
397
18
    data = frag->data_ref->data;
398
399
18
    dp = 0;
400
8861
    for (i = 0; i < frag->nb_units; i++) {
401
8843
        CodedBitstreamUnit *unit = &frag->units[i];
402
403
8843
        data[dp++] = 0;
404
8843
        data[dp++] = 0;
405
8843
        data[dp++] = 1;
406
407
8843
        memcpy(data + dp, unit->data, unit->data_size);
408
8843
        dp += unit->data_size;
409
    }
410
411
18
    av_assert0(dp == size);
412
413
18
    memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
414
18
    frag->data      = data;
415
18
    frag->data_size = size;
416
417
18
    return 0;
418
}
419
420
const CodedBitstreamType ff_cbs_type_mpeg2 = {
421
    .codec_id          = AV_CODEC_ID_MPEG2VIDEO,
422
423
    .priv_data_size    = sizeof(CodedBitstreamMPEG2Context),
424
425
    .split_fragment    = &cbs_mpeg2_split_fragment,
426
    .read_unit         = &cbs_mpeg2_read_unit,
427
    .write_unit        = &cbs_mpeg2_write_unit,
428
    .assemble_fragment = &cbs_mpeg2_assemble_fragment,
429
};