Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/opus/parse.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2012 Andrew D'Addesio
3
 * Copyright (c) 2013-2014 Mozilla Corporation
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23
 * @file
24
 * Opus decoder/parser shared code
25
 */
26
27
#include "libavutil/attributes.h"
28
#include "libavutil/channel_layout.h"
29
#include "libavutil/error.h"
30
#include "libavutil/intreadwrite.h"
31
#include "libavutil/log.h"
32
#include "libavutil/mem.h"
33
34
#include "avcodec.h"
35
#include "internal.h"
36
#include "mathops.h"
37
#include "opus.h"
38
#include "parse.h"
39
#include "vorbis_data.h"
40
41
static const uint16_t opus_frame_duration[32] = {
42
    480, 960, 1920, 2880,
43
    480, 960, 1920, 2880,
44
    480, 960, 1920, 2880,
45
    480, 960,
46
    480, 960,
47
    120, 240,  480,  960,
48
    120, 240,  480,  960,
49
    120, 240,  480,  960,
50
    120, 240,  480,  960,
51
};
52
53
/**
54
 * Read a 1- or 2-byte frame length
55
 */
56
static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
57
37.4k
{
58
37.4k
    int val;
59
60
37.4k
    if (*ptr >= end)
61
572
        return AVERROR_INVALIDDATA;
62
36.8k
    val = *(*ptr)++;
63
36.8k
    if (val >= 252) {
64
1.39k
        if (*ptr >= end)
65
139
            return AVERROR_INVALIDDATA;
66
1.25k
        val += 4 * *(*ptr)++;
67
1.25k
    }
68
36.7k
    return val;
69
36.8k
}
70
71
/**
72
 * Read a multi-byte length (used for code 3 packet padding size)
73
 */
74
static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
75
2.03k
{
76
2.03k
    int val = 0;
77
2.03k
    int next;
78
79
2.30k
    while (1) {
80
2.30k
        if (*ptr >= end || val > INT_MAX - 254)
81
93
            return AVERROR_INVALIDDATA;
82
2.20k
        next = *(*ptr)++;
83
2.20k
        val += next;
84
2.20k
        if (next < 255)
85
1.94k
            break;
86
266
        else
87
266
            val--;
88
2.20k
    }
89
1.94k
    return val;
90
2.03k
}
91
92
/**
93
 * Parse Opus packet info from raw packet data
94
 */
95
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
96
                         int self_delimiting)
97
44.8k
{
98
44.8k
    const uint8_t *ptr = buf;
99
44.8k
    const uint8_t *end = buf + buf_size;
100
44.8k
    int padding = 0;
101
44.8k
    int frame_bytes, i;
102
103
44.8k
    if (buf_size < 1)
104
333
        goto fail;
105
106
    /* TOC byte */
107
44.5k
    i = *ptr++;
108
44.5k
    pkt->code   = (i     ) & 0x3;
109
44.5k
    pkt->stereo = (i >> 2) & 0x1;
110
44.5k
    pkt->config = (i >> 3) & 0x1F;
111
112
    /* code 2 and code 3 packets have at least 1 byte after the TOC */
113
44.5k
    if (pkt->code >= 2 && buf_size < 2)
114
661
        goto fail;
115
116
43.8k
    switch (pkt->code) {
117
23.2k
    case 0:
118
        /* 1 frame */
119
23.2k
        pkt->frame_count = 1;
120
23.2k
        pkt->vbr         = 0;
121
122
23.2k
        if (self_delimiting) {
123
7.60k
            int len = xiph_lacing_16bit(&ptr, end);
124
7.60k
            if (len < 0 || len > end - ptr)
125
3.53k
                goto fail;
126
4.07k
            end      = ptr + len;
127
4.07k
            buf_size = end - buf;
128
4.07k
        }
129
130
19.7k
        frame_bytes = end - ptr;
131
19.7k
        if (frame_bytes > OPUS_MAX_FRAME_SIZE)
132
95
            goto fail;
133
19.6k
        pkt->frame_offset[0] = ptr - buf;
134
19.6k
        pkt->frame_size[0]   = frame_bytes;
135
19.6k
        break;
136
6.21k
    case 1:
137
        /* 2 frames, equal size */
138
6.21k
        pkt->frame_count = 2;
139
6.21k
        pkt->vbr         = 0;
140
141
6.21k
        if (self_delimiting) {
142
1.07k
            int len = xiph_lacing_16bit(&ptr, end);
143
1.07k
            if (len < 0 || 2 * len > end - ptr)
144
1.06k
                goto fail;
145
9
            end      = ptr + 2 * len;
146
9
            buf_size = end - buf;
147
9
        }
148
149
5.14k
        frame_bytes = end - ptr;
150
5.14k
        if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE)
151
2.99k
            goto fail;
152
2.14k
        pkt->frame_offset[0] = ptr - buf;
153
2.14k
        pkt->frame_size[0]   = frame_bytes >> 1;
154
2.14k
        pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
155
2.14k
        pkt->frame_size[1]   = frame_bytes >> 1;
156
2.14k
        break;
157
6.04k
    case 2:
158
        /* 2 frames, different sizes */
159
6.04k
        pkt->frame_count = 2;
160
6.04k
        pkt->vbr         = 1;
161
162
        /* read 1st frame size */
163
6.04k
        frame_bytes = xiph_lacing_16bit(&ptr, end);
164
6.04k
        if (frame_bytes < 0)
165
15
            goto fail;
166
167
6.03k
        if (self_delimiting) {
168
1.49k
            int len = xiph_lacing_16bit(&ptr, end);
169
1.49k
            if (len < 0 || len + frame_bytes > end - ptr)
170
1.46k
                goto fail;
171
22
            end      = ptr + frame_bytes + len;
172
22
            buf_size = end - buf;
173
22
        }
174
175
4.56k
        pkt->frame_offset[0] = ptr - buf;
176
4.56k
        pkt->frame_size[0]   = frame_bytes;
177
178
        /* calculate 2nd frame size */
179
4.56k
        frame_bytes = end - ptr - pkt->frame_size[0];
180
4.56k
        if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE)
181
1.90k
            goto fail;
182
2.66k
        pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
183
2.66k
        pkt->frame_size[1]   = frame_bytes;
184
2.66k
        break;
185
8.35k
    case 3:
186
        /* 1 to 48 frames, can be different sizes */
187
8.35k
        i = *ptr++;
188
8.35k
        pkt->frame_count = (i     ) & 0x3F;
189
8.35k
        padding          = (i >> 6) & 0x01;
190
8.35k
        pkt->vbr         = (i >> 7) & 0x01;
191
192
8.35k
        if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES)
193
2.69k
            goto fail;
194
195
        /* read padding size */
196
5.65k
        if (padding) {
197
2.03k
            padding = xiph_lacing_full(&ptr, end);
198
2.03k
            if (padding < 0)
199
93
                goto fail;
200
2.03k
        }
201
202
        /* read frame sizes */
203
5.56k
        if (pkt->vbr) {
204
            /* for VBR, all frames except the final one have their size coded
205
               in the bitstream. the last frame size is implicit. */
206
2.41k
            int total_bytes = 0;
207
22.6k
            for (i = 0; i < pkt->frame_count - 1; i++) {
208
20.5k
                frame_bytes = xiph_lacing_16bit(&ptr, end);
209
20.5k
                if (frame_bytes < 0)
210
364
                    goto fail;
211
20.2k
                pkt->frame_size[i] = frame_bytes;
212
20.2k
                total_bytes += frame_bytes;
213
20.2k
            }
214
215
2.04k
            if (self_delimiting) {
216
34
                int len = xiph_lacing_16bit(&ptr, end);
217
34
                if (len < 0 || len + total_bytes + padding > end - ptr)
218
34
                    goto fail;
219
0
                end      = ptr + total_bytes + len + padding;
220
0
                buf_size = end - buf;
221
0
            }
222
223
2.01k
            frame_bytes = end - ptr - padding;
224
2.01k
            if (total_bytes > frame_bytes)
225
622
                goto fail;
226
1.39k
            pkt->frame_offset[0] = ptr - buf;
227
5.60k
            for (i = 1; i < pkt->frame_count; i++)
228
4.21k
                pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
229
1.39k
            pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
230
3.15k
        } else {
231
            /* for CBR, the remaining packet bytes are divided evenly between
232
               the frames */
233
3.15k
            if (self_delimiting) {
234
618
                frame_bytes = xiph_lacing_16bit(&ptr, end);
235
618
                if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
236
398
                    goto fail;
237
220
                end      = ptr + pkt->frame_count * frame_bytes + padding;
238
220
                buf_size = end - buf;
239
2.53k
            } else {
240
2.53k
                frame_bytes = end - ptr - padding;
241
2.53k
                if (frame_bytes % pkt->frame_count ||
242
2.53k
                    frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE)
243
1.27k
                    goto fail;
244
1.26k
                frame_bytes /= pkt->frame_count;
245
1.26k
            }
246
247
1.48k
            pkt->frame_offset[0] = ptr - buf;
248
1.48k
            pkt->frame_size[0]   = frame_bytes;
249
6.80k
            for (i = 1; i < pkt->frame_count; i++) {
250
5.32k
                pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
251
5.32k
                pkt->frame_size[i]   = frame_bytes;
252
5.32k
            }
253
1.48k
        }
254
43.8k
    }
255
256
27.3k
    pkt->packet_size = buf_size;
257
27.3k
    pkt->data_size   = pkt->packet_size - padding;
258
259
    /* total packet duration cannot be larger than 120ms */
260
27.3k
    pkt->frame_duration = opus_frame_duration[pkt->config];
261
27.3k
    if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR)
262
291
        goto fail;
263
264
    /* set mode and bandwidth */
265
27.0k
    if (pkt->config < 12) {
266
14.4k
        pkt->mode = OPUS_MODE_SILK;
267
14.4k
        pkt->bandwidth = pkt->config >> 2;
268
14.4k
    } else if (pkt->config < 16) {
269
2.89k
        pkt->mode = OPUS_MODE_HYBRID;
270
2.89k
        pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
271
9.65k
    } else {
272
9.65k
        pkt->mode = OPUS_MODE_CELT;
273
9.65k
        pkt->bandwidth = (pkt->config - 16) >> 2;
274
        /* skip medium band */
275
9.65k
        if (pkt->bandwidth)
276
8.54k
            pkt->bandwidth++;
277
9.65k
    }
278
279
27.0k
    return 0;
280
281
17.8k
fail:
282
17.8k
    memset(pkt, 0, sizeof(*pkt));
283
17.8k
    return AVERROR_INVALIDDATA;
284
27.3k
}
285
286
static int channel_reorder_vorbis(int nb_channels, int channel_idx)
287
657
{
288
657
    return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
289
657
}
290
291
static int channel_reorder_unknown(int nb_channels, int channel_idx)
292
25
{
293
25
    return channel_idx;
294
25
}
295
296
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
297
                                    OpusParseContext *s)
298
153
{
299
153
    static const uint8_t default_channel_map[2] = { 0, 1 };
300
301
153
    int (*channel_reorder)(int, int) = channel_reorder_unknown;
302
153
    int channels = avctx->ch_layout.nb_channels;
303
304
153
    const uint8_t *extradata, *channel_map;
305
153
    int extradata_size;
306
153
    int version, map_type, streams, stereo_streams, i, j, ret;
307
153
    AVChannelLayout layout = { 0 };
308
309
153
    if (!avctx->extradata) {
310
0
        if (channels > 2) {
311
0
            av_log(avctx, AV_LOG_ERROR,
312
0
                   "Multichannel configuration without extradata.\n");
313
0
            return AVERROR(EINVAL);
314
0
        }
315
0
        extradata      = opus_default_extradata;
316
0
        extradata_size = sizeof(opus_default_extradata);
317
153
    } else {
318
153
        extradata = avctx->extradata;
319
153
        extradata_size = avctx->extradata_size;
320
153
    }
321
322
153
    if (extradata_size < 19) {
323
0
        av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
324
0
               extradata_size);
325
0
        return AVERROR_INVALIDDATA;
326
0
    }
327
328
153
    version = extradata[8];
329
153
    if (version > 15) {
330
93
        avpriv_request_sample(avctx, "Extradata version %d", version);
331
93
        return AVERROR_PATCHWELCOME;
332
93
    }
333
334
60
    avctx->delay = AV_RL16(extradata + 10);
335
60
    if (avctx->internal)
336
0
        avctx->internal->skip_samples = avctx->delay;
337
338
60
    channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2;
339
60
    if (!channels) {
340
0
        av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
341
0
        return AVERROR_INVALIDDATA;
342
0
    }
343
344
60
    s->gain_i = AV_RL16(extradata + 16);
345
346
60
    map_type = extradata[18];
347
60
    if (!map_type) {
348
13
        if (channels > 2) {
349
0
            av_log(avctx, AV_LOG_ERROR,
350
0
                   "Channel mapping 0 is only specified for up to 2 channels\n");
351
0
            ret = AVERROR_INVALIDDATA;
352
0
            goto fail;
353
0
        }
354
13
        layout         = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
355
13
                                           (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
356
13
        streams        = 1;
357
13
        stereo_streams = channels - 1;
358
13
        channel_map    = default_channel_map;
359
47
    } else if (map_type == 1 || map_type == 2 || map_type == 255) {
360
27
        if (extradata_size < 21 + channels) {
361
5
            av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
362
5
                   extradata_size);
363
5
            ret = AVERROR_INVALIDDATA;
364
5
            goto fail;
365
5
        }
366
367
22
        streams        = extradata[19];
368
22
        stereo_streams = extradata[20];
369
22
        if (!streams || stereo_streams > streams ||
370
22
            streams + stereo_streams > 255) {
371
0
            av_log(avctx, AV_LOG_ERROR,
372
0
                   "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
373
0
            ret = AVERROR_INVALIDDATA;
374
0
            goto fail;
375
0
        }
376
377
22
        if (map_type == 1) {
378
22
            if (channels > 8) {
379
0
                av_log(avctx, AV_LOG_ERROR,
380
0
                       "Channel mapping 1 is only specified for up to 8 channels\n");
381
0
                ret = AVERROR_INVALIDDATA;
382
0
                goto fail;
383
0
            }
384
22
            av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]);
385
22
            channel_reorder = channel_reorder_vorbis;
386
22
        } else if (map_type == 2) {
387
0
            int ambisonic_order = ff_sqrt(channels) - 1;
388
0
            if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
389
0
                channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
390
0
                av_log(avctx, AV_LOG_ERROR,
391
0
                       "Channel mapping 2 is only specified for channel counts"
392
0
                       " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
393
0
                       " for nonnegative integer n\n");
394
0
                ret = AVERROR_INVALIDDATA;
395
0
                goto fail;
396
0
            }
397
0
            if (channels > 227) {
398
0
                av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
399
0
                ret = AVERROR_INVALIDDATA;
400
0
                goto fail;
401
0
            }
402
403
0
            layout.order = AV_CHANNEL_ORDER_AMBISONIC;
404
0
            layout.nb_channels = channels;
405
0
            if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)))
406
0
                layout.u.mask = AV_CH_LAYOUT_STEREO;
407
0
        } else {
408
0
            layout.order       = AV_CHANNEL_ORDER_UNSPEC;
409
0
            layout.nb_channels = channels;
410
0
        }
411
412
22
        channel_map = extradata + 21;
413
22
    } else {
414
20
        avpriv_request_sample(avctx, "Mapping type %d", map_type);
415
20
        return AVERROR_PATCHWELCOME;
416
20
    }
417
418
35
    s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
419
35
    if (!s->channel_maps) {
420
0
        ret = AVERROR(ENOMEM);
421
0
        goto fail;
422
0
    }
423
424
212
    for (i = 0; i < channels; i++) {
425
177
        ChannelMap *map = &s->channel_maps[i];
426
177
        uint8_t     idx = channel_map[channel_reorder(channels, i)];
427
428
177
        if (idx == 255) {
429
12
            map->silence = 1;
430
12
            continue;
431
165
        } else if (idx >= streams + stereo_streams) {
432
0
            av_log(avctx, AV_LOG_ERROR,
433
0
                   "Invalid channel map for output channel %d: %d\n", i, idx);
434
0
            av_freep(&s->channel_maps);
435
0
            ret = AVERROR_INVALIDDATA;
436
0
            goto fail;
437
0
        }
438
439
        /* check that we did not see this index yet */
440
165
        map->copy = 0;
441
670
        for (j = 0; j < i; j++)
442
505
            if (channel_map[channel_reorder(channels, j)] == idx) {
443
0
                map->copy     = 1;
444
0
                map->copy_idx = j;
445
0
                break;
446
0
            }
447
448
165
        if (idx < 2 * stereo_streams) {
449
118
            map->stream_idx  = idx / 2;
450
118
            map->channel_idx = idx & 1;
451
118
        } else {
452
47
            map->stream_idx  = idx - stereo_streams;
453
47
            map->channel_idx = 0;
454
47
        }
455
165
    }
456
457
35
    ret = av_channel_layout_copy(&avctx->ch_layout, &layout);
458
35
    if (ret < 0)
459
0
        goto fail;
460
461
35
    s->nb_streams         = streams;
462
35
    s->nb_stereo_streams  = stereo_streams;
463
464
35
    return 0;
465
5
fail:
466
5
    av_channel_layout_uninit(&layout);
467
5
    return ret;
468
35
}
469