FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus/parse.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 171 264 64.8%
Functions: 6 6 100.0%
Branches: 88 163 54.0%

Line Branch Exec 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 21825 static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
47 {
48 int val;
49
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21825 times.
21825 if (*ptr >= end)
51 return AVERROR_INVALIDDATA;
52 21825 val = *(*ptr)++;
53
2/2
✓ Branch 0 taken 898 times.
✓ Branch 1 taken 20927 times.
21825 if (val >= 252) {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 898 times.
898 if (*ptr >= end)
55 return AVERROR_INVALIDDATA;
56 898 val += 4 * *(*ptr)++;
57 }
58 21825 return val;
59 }
60
61 /**
62 * Read a multi-byte length (used for code 3 packet padding size)
63 */
64 1320 static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
65 {
66 1320 int val = 0;
67 int next;
68
69 while (1) {
70
2/4
✓ Branch 0 taken 1776 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1776 times.
1776 if (*ptr >= end || val > INT_MAX - 254)
71 return AVERROR_INVALIDDATA;
72 1776 next = *(*ptr)++;
73 1776 val += next;
74
2/2
✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 456 times.
1776 if (next < 255)
75 1320 break;
76 else
77 456 val--;
78 }
79 1320 return val;
80 }
81
82 /**
83 * Parse Opus packet info from raw packet data
84 */
85 49171 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
86 int self_delimiting)
87 {
88 49171 const uint8_t *ptr = buf;
89 49171 const uint8_t *end = buf + buf_size;
90 49171 int padding = 0;
91 int frame_bytes, i;
92
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49171 times.
49171 if (buf_size < 1)
94 goto fail;
95
96 /* TOC byte */
97 49171 i = *ptr++;
98 49171 pkt->code = (i ) & 0x3;
99 49171 pkt->stereo = (i >> 2) & 0x1;
100 49171 pkt->config = (i >> 3) & 0x1F;
101
102 /* code 2 and code 3 packets have at least 1 byte after the TOC */
103
3/4
✓ Branch 0 taken 8200 times.
✓ Branch 1 taken 40971 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8200 times.
49171 if (pkt->code >= 2 && buf_size < 2)
104 goto fail;
105
106
4/5
✓ Branch 0 taken 40331 times.
✓ Branch 1 taken 640 times.
✓ Branch 2 taken 1998 times.
✓ Branch 3 taken 6202 times.
✗ Branch 4 not taken.
49171 switch (pkt->code) {
107 40331 case 0:
108 /* 1 frame */
109 40331 pkt->frame_count = 1;
110 40331 pkt->vbr = 0;
111
112
2/2
✓ Branch 0 taken 6901 times.
✓ Branch 1 taken 33430 times.
40331 if (self_delimiting) {
113 6901 int len = xiph_lacing_16bit(&ptr, end);
114
2/4
✓ Branch 0 taken 6901 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6901 times.
6901 if (len < 0 || len > end - ptr)
115 goto fail;
116 6901 end = ptr + len;
117 6901 buf_size = end - buf;
118 }
119
120 40331 frame_bytes = end - ptr;
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40331 times.
40331 if (frame_bytes > OPUS_MAX_FRAME_SIZE)
122 goto fail;
123 40331 pkt->frame_offset[0] = ptr - buf;
124 40331 pkt->frame_size[0] = frame_bytes;
125 40331 break;
126 640 case 1:
127 /* 2 frames, equal size */
128 640 pkt->frame_count = 2;
129 640 pkt->vbr = 0;
130
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 640 times.
640 if (self_delimiting) {
132 int len = xiph_lacing_16bit(&ptr, end);
133 if (len < 0 || 2 * len > end - ptr)
134 goto fail;
135 end = ptr + 2 * len;
136 buf_size = end - buf;
137 }
138
139 640 frame_bytes = end - ptr;
140
2/4
✓ Branch 0 taken 640 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 640 times.
640 if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE)
141 goto fail;
142 640 pkt->frame_offset[0] = ptr - buf;
143 640 pkt->frame_size[0] = frame_bytes >> 1;
144 640 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
145 640 pkt->frame_size[1] = frame_bytes >> 1;
146 640 break;
147 1998 case 2:
148 /* 2 frames, different sizes */
149 1998 pkt->frame_count = 2;
150 1998 pkt->vbr = 1;
151
152 /* read 1st frame size */
153 1998 frame_bytes = xiph_lacing_16bit(&ptr, end);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1998 times.
1998 if (frame_bytes < 0)
155 goto fail;
156
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1998 times.
1998 if (self_delimiting) {
158 int len = xiph_lacing_16bit(&ptr, end);
159 if (len < 0 || len + frame_bytes > end - ptr)
160 goto fail;
161 end = ptr + frame_bytes + len;
162 buf_size = end - buf;
163 }
164
165 1998 pkt->frame_offset[0] = ptr - buf;
166 1998 pkt->frame_size[0] = frame_bytes;
167
168 /* calculate 2nd frame size */
169 1998 frame_bytes = end - ptr - pkt->frame_size[0];
170
2/4
✓ Branch 0 taken 1998 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1998 times.
1998 if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE)
171 goto fail;
172 1998 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
173 1998 pkt->frame_size[1] = frame_bytes;
174 1998 break;
175 6202 case 3:
176 /* 1 to 48 frames, can be different sizes */
177 6202 i = *ptr++;
178 6202 pkt->frame_count = (i ) & 0x3F;
179 6202 padding = (i >> 6) & 0x01;
180 6202 pkt->vbr = (i >> 7) & 0x01;
181
182
2/4
✓ Branch 0 taken 6202 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6202 times.
6202 if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES)
183 goto fail;
184
185 /* read padding size */
186
2/2
✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 4882 times.
6202 if (padding) {
187 1320 padding = xiph_lacing_full(&ptr, end);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
1320 if (padding < 0)
189 goto fail;
190 }
191
192 /* read frame sizes */
193
2/2
✓ Branch 0 taken 4352 times.
✓ Branch 1 taken 1850 times.
6202 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 4352 int total_bytes = 0;
197
2/2
✓ Branch 0 taken 12926 times.
✓ Branch 1 taken 4352 times.
17278 for (i = 0; i < pkt->frame_count - 1; i++) {
198 12926 frame_bytes = xiph_lacing_16bit(&ptr, end);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12926 times.
12926 if (frame_bytes < 0)
200 goto fail;
201 12926 pkt->frame_size[i] = frame_bytes;
202 12926 total_bytes += frame_bytes;
203 }
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4352 times.
4352 if (self_delimiting) {
206 int len = xiph_lacing_16bit(&ptr, end);
207 if (len < 0 || len + total_bytes + padding > end - ptr)
208 goto fail;
209 end = ptr + total_bytes + len + padding;
210 buf_size = end - buf;
211 }
212
213 4352 frame_bytes = end - ptr - padding;
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4352 times.
4352 if (total_bytes > frame_bytes)
215 goto fail;
216 4352 pkt->frame_offset[0] = ptr - buf;
217
2/2
✓ Branch 0 taken 12926 times.
✓ Branch 1 taken 4352 times.
17278 for (i = 1; i < pkt->frame_count; i++)
218 12926 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
219 4352 pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
220 } else {
221 /* for CBR, the remaining packet bytes are divided evenly between
222 the frames */
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1850 times.
1850 if (self_delimiting) {
224 frame_bytes = xiph_lacing_16bit(&ptr, end);
225 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
226 goto fail;
227 end = ptr + pkt->frame_count * frame_bytes + padding;
228 buf_size = end - buf;
229 } else {
230 1850 frame_bytes = end - ptr - padding;
231
1/2
✓ Branch 0 taken 1850 times.
✗ Branch 1 not taken.
1850 if (frame_bytes % pkt->frame_count ||
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1850 times.
1850 frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE)
233 goto fail;
234 1850 frame_bytes /= pkt->frame_count;
235 }
236
237 1850 pkt->frame_offset[0] = ptr - buf;
238 1850 pkt->frame_size[0] = frame_bytes;
239
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 1850 times.
2630 for (i = 1; i < pkt->frame_count; i++) {
240 780 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
241 780 pkt->frame_size[i] = frame_bytes;
242 }
243 }
244 }
245
246 49171 pkt->packet_size = buf_size;
247 49171 pkt->data_size = pkt->packet_size - padding;
248
249 /* total packet duration cannot be larger than 120ms */
250 49171 pkt->frame_duration = ff_opus_frame_duration[pkt->config];
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49171 times.
49171 if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR)
252 goto fail;
253
254 /* set mode and bandwidth */
255
2/2
✓ Branch 0 taken 9060 times.
✓ Branch 1 taken 40111 times.
49171 if (pkt->config < 12) {
256 9060 pkt->mode = OPUS_MODE_SILK;
257 9060 pkt->bandwidth = pkt->config >> 2;
258
2/2
✓ Branch 0 taken 9851 times.
✓ Branch 1 taken 30260 times.
40111 } else if (pkt->config < 16) {
259 9851 pkt->mode = OPUS_MODE_HYBRID;
260
2/2
✓ Branch 0 taken 5147 times.
✓ Branch 1 taken 4704 times.
9851 pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
261 } else {
262 30260 pkt->mode = OPUS_MODE_CELT;
263 30260 pkt->bandwidth = (pkt->config - 16) >> 2;
264 /* skip medium band */
265
2/2
✓ Branch 0 taken 26416 times.
✓ Branch 1 taken 3844 times.
30260 if (pkt->bandwidth)
266 26416 pkt->bandwidth++;
267 }
268
269 49171 return 0;
270
271 fail:
272 memset(pkt, 0, sizeof(*pkt));
273 return AVERROR_INVALIDDATA;
274 }
275
276 291 static int channel_reorder_vorbis(int nb_channels, int channel_idx)
277 {
278 291 return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
279 }
280
281 270 static int channel_reorder_unknown(int nb_channels, int channel_idx)
282 {
283 270 return channel_idx;
284 }
285
286 157 av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
287 OpusParseContext *s)
288 {
289 static const uint8_t default_channel_map[2] = { 0, 1 };
290
291 157 int (*channel_reorder)(int, int) = channel_reorder_unknown;
292 157 int channels = avctx->ch_layout.nb_channels;
293
294 const uint8_t *extradata, *channel_map;
295 int extradata_size;
296 int version, map_type, streams, stereo_streams, i, j, ret;
297 157 AVChannelLayout layout = { 0 };
298
299
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 156 times.
157 if (!avctx->extradata) {
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (channels > 2) {
301 av_log(avctx, AV_LOG_ERROR,
302 "Multichannel configuration without extradata.\n");
303 return AVERROR(EINVAL);
304 }
305 1 extradata = opus_default_extradata;
306 1 extradata_size = sizeof(opus_default_extradata);
307 } else {
308 156 extradata = avctx->extradata;
309 156 extradata_size = avctx->extradata_size;
310 }
311
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (extradata_size < 19) {
313 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
314 extradata_size);
315 return AVERROR_INVALIDDATA;
316 }
317
318 157 version = extradata[8];
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (version > 15) {
320 avpriv_request_sample(avctx, "Extradata version %d", version);
321 return AVERROR_PATCHWELCOME;
322 }
323
324 157 avctx->delay = AV_RL16(extradata + 10);
325
326
3/4
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
157 channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2;
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (!channels) {
328 av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
329 return AVERROR_INVALIDDATA;
330 }
331
332 157 s->gain_i = AV_RL16(extradata + 16);
333
334 157 map_type = extradata[18];
335
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 11 times.
157 if (!map_type) {
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (channels > 2) {
337 av_log(avctx, AV_LOG_ERROR,
338 "Channel mapping 0 is only specified for up to 2 channels\n");
339 ret = AVERROR_INVALIDDATA;
340 goto fail;
341 }
342
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 62 times.
146 layout = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
343 (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
344 146 streams = 1;
345 146 stereo_streams = channels - 1;
346 146 channel_map = default_channel_map;
347
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
11 } else if (map_type == 1 || map_type == 2 || map_type == 255) {
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (extradata_size < 21 + channels) {
349 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
350 extradata_size);
351 ret = AVERROR_INVALIDDATA;
352 goto fail;
353 }
354
355 11 streams = extradata[19];
356 11 stereo_streams = extradata[20];
357
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 if (!streams || stereo_streams > streams ||
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 streams + stereo_streams > 255) {
359 av_log(avctx, AV_LOG_ERROR,
360 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
361 ret = AVERROR_INVALIDDATA;
362 goto fail;
363 }
364
365
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (map_type == 1) {
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (channels > 8) {
367 av_log(avctx, AV_LOG_ERROR,
368 "Channel mapping 1 is only specified for up to 8 channels\n");
369 ret = AVERROR_INVALIDDATA;
370 goto fail;
371 }
372 11 av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]);
373 11 channel_reorder = channel_reorder_vorbis;
374 } else if (map_type == 2) {
375 int ambisonic_order = ff_sqrt(channels) - 1;
376 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
377 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
378 av_log(avctx, AV_LOG_ERROR,
379 "Channel mapping 2 is only specified for channel counts"
380 " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
381 " for nonnegative integer n\n");
382 ret = AVERROR_INVALIDDATA;
383 goto fail;
384 }
385 if (channels > 227) {
386 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
387 ret = AVERROR_INVALIDDATA;
388 goto fail;
389 }
390
391 layout.order = AV_CHANNEL_ORDER_AMBISONIC;
392 layout.nb_channels = channels;
393 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)))
394 layout.u.mask = AV_CH_LAYOUT_STEREO;
395 } else {
396 layout.order = AV_CHANNEL_ORDER_UNSPEC;
397 layout.nb_channels = channels;
398 }
399
400 11 channel_map = extradata + 21;
401 } else {
402 avpriv_request_sample(avctx, "Mapping type %d", map_type);
403 return AVERROR_PATCHWELCOME;
404 }
405
406 157 s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (!s->channel_maps) {
408 ret = AVERROR(ENOMEM);
409 goto fail;
410 }
411
412
2/2
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 157 times.
439 for (i = 0; i < channels; i++) {
413 282 ChannelMap *map = &s->channel_maps[i];
414 282 uint8_t idx = channel_map[channel_reorder(channels, i)];
415
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 282 times.
282 if (idx == 255) {
417 map->silence = 1;
418 continue;
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 282 times.
282 } else if (idx >= streams + stereo_streams) {
420 av_log(avctx, AV_LOG_ERROR,
421 "Invalid channel map for output channel %d: %d\n", i, idx);
422 av_freep(&s->channel_maps);
423 ret = AVERROR_INVALIDDATA;
424 goto fail;
425 }
426
427 /* check that we did not see this index yet */
428 282 map->copy = 0;
429
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 282 times.
561 for (j = 0; j < i; j++)
430
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
279 if (channel_map[channel_reorder(channels, j)] == idx) {
431 map->copy = 1;
432 map->copy_idx = j;
433 break;
434 }
435
436
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 106 times.
282 if (idx < 2 * stereo_streams) {
437 176 map->stream_idx = idx / 2;
438 176 map->channel_idx = idx & 1;
439 } else {
440 106 map->stream_idx = idx - stereo_streams;
441 106 map->channel_idx = 0;
442 }
443 }
444
445 157 ret = av_channel_layout_copy(&avctx->ch_layout, &layout);
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (ret < 0)
447 goto fail;
448
449 157 s->nb_streams = streams;
450 157 s->nb_stereo_streams = stereo_streams;
451
452 157 return 0;
453 fail:
454 av_channel_layout_uninit(&layout);
455 return ret;
456 }
457