Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/opus/parse.c
Line
Count
Source
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 "libavcodec/avcodec.h"
35
#include "libavcodec/internal.h"
36
#include "libavcodec/mathops.h"
37
#include "libavcodec/vorbis_data.h"
38
39
#include "opus.h"
40
#include "parse.h"
41
#include "tab.h"
42
43
/**
44
 * Read a 1- or 2-byte frame length
45
 */
46
static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
47
500k
{
48
500k
    int val;
49
50
500k
    if (*ptr >= end)
51
24.9k
        return AVERROR_INVALIDDATA;
52
475k
    val = *(*ptr)++;
53
475k
    if (val >= 252) {
54
32.1k
        if (*ptr >= end)
55
6.81k
            return AVERROR_INVALIDDATA;
56
25.3k
        val += 4 * *(*ptr)++;
57
25.3k
    }
58
469k
    return val;
59
475k
}
60
61
/**
62
 * Read a multi-byte length (used for code 3 packet padding size)
63
 */
64
static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
65
36.9k
{
66
36.9k
    int val = 0;
67
36.9k
    int next;
68
69
836k
    while (1) {
70
836k
        if (*ptr >= end || val > INT_MAX - 254)
71
8.50k
            return AVERROR_INVALIDDATA;
72
828k
        next = *(*ptr)++;
73
828k
        val += next;
74
828k
        if (next < 255)
75
28.4k
            break;
76
800k
        else
77
800k
            val--;
78
828k
    }
79
28.4k
    return val;
80
36.9k
}
81
82
/**
83
 * Parse Opus packet info from raw packet data
84
 */
85
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
86
                         int self_delimiting)
87
802k
{
88
802k
    const uint8_t *ptr = buf;
89
802k
    const uint8_t *end = buf + buf_size;
90
802k
    int padding = 0;
91
802k
    int frame_bytes, i;
92
93
802k
    if (buf_size < 1)
94
1.70k
        goto fail;
95
96
    /* TOC byte */
97
801k
    i = *ptr++;
98
801k
    pkt->code   = (i     ) & 0x3;
99
801k
    pkt->stereo = (i >> 2) & 0x1;
100
801k
    pkt->config = (i >> 3) & 0x1F;
101
102
    /* code 2 and code 3 packets have at least 1 byte after the TOC */
103
801k
    if (pkt->code >= 2 && buf_size < 2)
104
63.9k
        goto fail;
105
106
737k
    switch (pkt->code) {
107
369k
    case 0:
108
        /* 1 frame */
109
369k
        pkt->frame_count = 1;
110
369k
        pkt->vbr         = 0;
111
112
369k
        if (self_delimiting) {
113
113k
            int len = xiph_lacing_16bit(&ptr, end);
114
113k
            if (len < 0 || len > end - ptr)
115
67.2k
                goto fail;
116
46.7k
            end      = ptr + len;
117
46.7k
            buf_size = end - buf;
118
46.7k
        }
119
120
302k
        frame_bytes = end - ptr;
121
302k
        if (frame_bytes > OPUS_MAX_FRAME_SIZE)
122
848
            goto fail;
123
301k
        pkt->frame_offset[0] = ptr - buf;
124
301k
        pkt->frame_size[0]   = frame_bytes;
125
301k
        break;
126
188k
    case 1:
127
        /* 2 frames, equal size */
128
188k
        pkt->frame_count = 2;
129
188k
        pkt->vbr         = 0;
130
131
188k
        if (self_delimiting) {
132
42.9k
            int len = xiph_lacing_16bit(&ptr, end);
133
42.9k
            if (len < 0 || 2 * len > end - ptr)
134
37.5k
                goto fail;
135
5.47k
            end      = ptr + 2 * len;
136
5.47k
            buf_size = end - buf;
137
5.47k
        }
138
139
150k
        frame_bytes = end - ptr;
140
150k
        if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE)
141
24.4k
            goto fail;
142
126k
        pkt->frame_offset[0] = ptr - buf;
143
126k
        pkt->frame_size[0]   = frame_bytes >> 1;
144
126k
        pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
145
126k
        pkt->frame_size[1]   = frame_bytes >> 1;
146
126k
        break;
147
78.5k
    case 2:
148
        /* 2 frames, different sizes */
149
78.5k
        pkt->frame_count = 2;
150
78.5k
        pkt->vbr         = 1;
151
152
        /* read 1st frame size */
153
78.5k
        frame_bytes = xiph_lacing_16bit(&ptr, end);
154
78.5k
        if (frame_bytes < 0)
155
1.67k
            goto fail;
156
157
76.8k
        if (self_delimiting) {
158
29.2k
            int len = xiph_lacing_16bit(&ptr, end);
159
29.2k
            if (len < 0 || len + frame_bytes > end - ptr)
160
25.6k
                goto fail;
161
3.63k
            end      = ptr + frame_bytes + len;
162
3.63k
            buf_size = end - buf;
163
3.63k
        }
164
165
51.2k
        pkt->frame_offset[0] = ptr - buf;
166
51.2k
        pkt->frame_size[0]   = frame_bytes;
167
168
        /* calculate 2nd frame size */
169
51.2k
        frame_bytes = end - ptr - pkt->frame_size[0];
170
51.2k
        if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE)
171
16.4k
            goto fail;
172
34.7k
        pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
173
34.7k
        pkt->frame_size[1]   = frame_bytes;
174
34.7k
        break;
175
101k
    case 3:
176
        /* 1 to 48 frames, can be different sizes */
177
101k
        i = *ptr++;
178
101k
        pkt->frame_count = (i     ) & 0x3F;
179
101k
        padding          = (i >> 6) & 0x01;
180
101k
        pkt->vbr         = (i >> 7) & 0x01;
181
182
101k
        if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES)
183
27.6k
            goto fail;
184
185
        /* read padding size */
186
73.5k
        if (padding) {
187
36.9k
            padding = xiph_lacing_full(&ptr, end);
188
36.9k
            if (padding < 0)
189
8.50k
                goto fail;
190
36.9k
        }
191
192
        /* read frame sizes */
193
65.0k
        if (pkt->vbr) {
194
            /* for VBR, all frames except the final one have their size coded
195
               in the bitstream. the last frame size is implicit. */
196
30.6k
            int total_bytes = 0;
197
240k
            for (i = 0; i < pkt->frame_count - 1; i++) {
198
217k
                frame_bytes = xiph_lacing_16bit(&ptr, end);
199
217k
                if (frame_bytes < 0)
200
7.98k
                    goto fail;
201
209k
                pkt->frame_size[i] = frame_bytes;
202
209k
                total_bytes += frame_bytes;
203
209k
            }
204
205
22.6k
            if (self_delimiting) {
206
6.03k
                int len = xiph_lacing_16bit(&ptr, end);
207
6.03k
                if (len < 0 || len + total_bytes + padding > end - ptr)
208
3.88k
                    goto fail;
209
2.15k
                end      = ptr + total_bytes + len + padding;
210
2.15k
                buf_size = end - buf;
211
2.15k
            }
212
213
18.8k
            frame_bytes = end - ptr - padding;
214
18.8k
            if (total_bytes > frame_bytes)
215
7.17k
                goto fail;
216
11.6k
            pkt->frame_offset[0] = ptr - buf;
217
67.1k
            for (i = 1; i < pkt->frame_count; i++)
218
55.4k
                pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
219
11.6k
            pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
220
34.3k
        } else {
221
            /* for CBR, the remaining packet bytes are divided evenly between
222
               the frames */
223
34.3k
            if (self_delimiting) {
224
12.8k
                frame_bytes = xiph_lacing_16bit(&ptr, end);
225
12.8k
                if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
226
10.0k
                    goto fail;
227
2.82k
                end      = ptr + pkt->frame_count * frame_bytes + padding;
228
2.82k
                buf_size = end - buf;
229
21.5k
            } else {
230
21.5k
                frame_bytes = end - ptr - padding;
231
21.5k
                if (frame_bytes % pkt->frame_count ||
232
11.2k
                    frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE)
233
10.7k
                    goto fail;
234
10.8k
                frame_bytes /= pkt->frame_count;
235
10.8k
            }
236
237
13.6k
            pkt->frame_offset[0] = ptr - buf;
238
13.6k
            pkt->frame_size[0]   = frame_bytes;
239
92.2k
            for (i = 1; i < pkt->frame_count; i++) {
240
78.5k
                pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
241
78.5k
                pkt->frame_size[i]   = frame_bytes;
242
78.5k
            }
243
13.6k
        }
244
737k
    }
245
246
487k
    pkt->packet_size = buf_size;
247
487k
    pkt->data_size   = pkt->packet_size - padding;
248
249
    /* total packet duration cannot be larger than 120ms */
250
487k
    pkt->frame_duration = ff_opus_frame_duration[pkt->config];
251
487k
    if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR)
252
2.53k
        goto fail;
253
254
    /* set mode and bandwidth */
255
484k
    if (pkt->config < 12) {
256
385k
        pkt->mode = OPUS_MODE_SILK;
257
385k
        pkt->bandwidth = pkt->config >> 2;
258
385k
    } else if (pkt->config < 16) {
259
33.8k
        pkt->mode = OPUS_MODE_HYBRID;
260
33.8k
        pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
261
65.4k
    } else {
262
65.4k
        pkt->mode = OPUS_MODE_CELT;
263
65.4k
        pkt->bandwidth = (pkt->config - 16) >> 2;
264
        /* skip medium band */
265
65.4k
        if (pkt->bandwidth)
266
52.1k
            pkt->bandwidth++;
267
65.4k
    }
268
269
484k
    return 0;
270
271
318k
fail:
272
318k
    memset(pkt, 0, sizeof(*pkt));
273
318k
    return AVERROR_INVALIDDATA;
274
487k
}
275
276
static int channel_reorder_vorbis(int nb_channels, int channel_idx)
277
6.98k
{
278
6.98k
    return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
279
6.98k
}
280
281
static int channel_reorder_unknown(int nb_channels, int channel_idx)
282
452k
{
283
452k
    return channel_idx;
284
452k
}
285
286
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
287
                                    OpusParseContext *s)
288
27.7k
{
289
27.7k
    static const uint8_t default_channel_map[2] = { 0, 1 };
290
291
27.7k
    int (*channel_reorder)(int, int) = channel_reorder_unknown;
292
27.7k
    int channels = avctx->ch_layout.nb_channels;
293
294
27.7k
    const uint8_t *extradata, *channel_map;
295
27.7k
    int extradata_size;
296
27.7k
    int version, map_type, streams, stereo_streams, i, j, ret;
297
27.7k
    AVChannelLayout layout = { 0 };
298
299
27.7k
    if (!avctx->extradata) {
300
4.49k
        if (channels > 2) {
301
59
            av_log(avctx, AV_LOG_ERROR,
302
59
                   "Multichannel configuration without extradata.\n");
303
59
            return AVERROR(EINVAL);
304
59
        }
305
4.43k
        extradata      = opus_default_extradata;
306
4.43k
        extradata_size = sizeof(opus_default_extradata);
307
23.2k
    } else {
308
23.2k
        extradata = avctx->extradata;
309
23.2k
        extradata_size = avctx->extradata_size;
310
23.2k
    }
311
312
27.6k
    if (extradata_size < 19) {
313
8.24k
        av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
314
8.24k
               extradata_size);
315
8.24k
        return AVERROR_INVALIDDATA;
316
8.24k
    }
317
318
19.4k
    version = extradata[8];
319
19.4k
    if (version > 15) {
320
4.88k
        avpriv_request_sample(avctx, "Extradata version %d", version);
321
4.88k
        return AVERROR_PATCHWELCOME;
322
4.88k
    }
323
324
14.5k
    avctx->delay = AV_RL16(extradata + 10);
325
326
14.5k
    channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2;
327
14.5k
    if (!channels) {
328
1.31k
        av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
329
1.31k
        return AVERROR_INVALIDDATA;
330
1.31k
    }
331
332
13.2k
    s->gain_i = AV_RL16(extradata + 16);
333
334
13.2k
    map_type = extradata[18];
335
13.2k
    if (!map_type) {
336
4.99k
        if (channels > 2) {
337
356
            av_log(avctx, AV_LOG_ERROR,
338
356
                   "Channel mapping 0 is only specified for up to 2 channels\n");
339
356
            ret = AVERROR_INVALIDDATA;
340
356
            goto fail;
341
356
        }
342
4.63k
        layout         = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
343
4.63k
                                           (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
344
4.63k
        streams        = 1;
345
4.63k
        stereo_streams = channels - 1;
346
4.63k
        channel_map    = default_channel_map;
347
8.22k
    } else if (map_type == 1 || map_type == 2 || map_type == 255) {
348
7.11k
        if (extradata_size < 21 + channels) {
349
1.31k
            av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
350
1.31k
                   extradata_size);
351
1.31k
            ret = AVERROR_INVALIDDATA;
352
1.31k
            goto fail;
353
1.31k
        }
354
355
5.79k
        streams        = extradata[19];
356
5.79k
        stereo_streams = extradata[20];
357
5.79k
        if (!streams || stereo_streams > streams ||
358
3.00k
            streams + stereo_streams > 255) {
359
2.79k
            av_log(avctx, AV_LOG_ERROR,
360
2.79k
                   "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
361
2.79k
            ret = AVERROR_INVALIDDATA;
362
2.79k
            goto fail;
363
2.79k
        }
364
365
3.00k
        if (map_type == 1) {
366
931
            if (channels > 8) {
367
225
                av_log(avctx, AV_LOG_ERROR,
368
225
                       "Channel mapping 1 is only specified for up to 8 channels\n");
369
225
                ret = AVERROR_INVALIDDATA;
370
225
                goto fail;
371
225
            }
372
706
            av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]);
373
706
            channel_reorder = channel_reorder_vorbis;
374
2.07k
        } else if (map_type == 2) {
375
1.48k
            int ambisonic_order = ff_sqrt(channels) - 1;
376
1.48k
            if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
377
1.46k
                channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
378
1.44k
                av_log(avctx, AV_LOG_ERROR,
379
1.44k
                       "Channel mapping 2 is only specified for channel counts"
380
1.44k
                       " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
381
1.44k
                       " for nonnegative integer n\n");
382
1.44k
                ret = AVERROR_INVALIDDATA;
383
1.44k
                goto fail;
384
1.44k
            }
385
44
            if (channels > 227) {
386
0
                av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
387
0
                ret = AVERROR_INVALIDDATA;
388
0
                goto fail;
389
0
            }
390
391
44
            layout.order = AV_CHANNEL_ORDER_AMBISONIC;
392
44
            layout.nb_channels = channels;
393
44
            if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)))
394
21
                layout.u.mask = AV_CH_LAYOUT_STEREO;
395
584
        } else {
396
584
            layout.order       = AV_CHANNEL_ORDER_UNSPEC;
397
584
            layout.nb_channels = channels;
398
584
        }
399
400
1.33k
        channel_map = extradata + 21;
401
1.33k
    } else {
402
1.11k
        avpriv_request_sample(avctx, "Mapping type %d", map_type);
403
1.11k
        return AVERROR_PATCHWELCOME;
404
1.11k
    }
405
406
5.96k
    s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
407
5.96k
    if (!s->channel_maps) {
408
0
        ret = AVERROR(ENOMEM);
409
0
        goto fail;
410
0
    }
411
412
39.4k
    for (i = 0; i < channels; i++) {
413
33.4k
        ChannelMap *map = &s->channel_maps[i];
414
33.4k
        uint8_t     idx = channel_map[channel_reorder(channels, i)];
415
416
33.4k
        if (idx == 255) {
417
1.78k
            map->silence = 1;
418
1.78k
            continue;
419
31.6k
        } else if (idx >= streams + stereo_streams) {
420
13
            av_log(avctx, AV_LOG_ERROR,
421
13
                   "Invalid channel map for output channel %d: %d\n", i, idx);
422
13
            av_freep(&s->channel_maps);
423
13
            ret = AVERROR_INVALIDDATA;
424
13
            goto fail;
425
13
        }
426
427
        /* check that we did not see this index yet */
428
31.6k
        map->copy = 0;
429
439k
        for (j = 0; j < i; j++)
430
425k
            if (channel_map[channel_reorder(channels, j)] == idx) {
431
17.4k
                map->copy     = 1;
432
17.4k
                map->copy_idx = j;
433
17.4k
                break;
434
17.4k
            }
435
436
31.6k
        if (idx < 2 * stereo_streams) {
437
15.8k
            map->stream_idx  = idx / 2;
438
15.8k
            map->channel_idx = idx & 1;
439
15.8k
        } else {
440
15.8k
            map->stream_idx  = idx - stereo_streams;
441
15.8k
            map->channel_idx = 0;
442
15.8k
        }
443
31.6k
    }
444
445
5.95k
    ret = av_channel_layout_copy(&avctx->ch_layout, &layout);
446
5.95k
    if (ret < 0)
447
0
        goto fail;
448
449
5.95k
    s->nb_streams         = streams;
450
5.95k
    s->nb_stereo_streams  = stereo_streams;
451
452
5.95k
    return 0;
453
6.14k
fail:
454
6.14k
    av_channel_layout_uninit(&layout);
455
6.14k
    return ret;
456
5.95k
}