/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 | 516k | { |
48 | 516k | int val; |
49 | | |
50 | 516k | if (*ptr >= end) |
51 | 28.0k | return AVERROR_INVALIDDATA; |
52 | 488k | val = *(*ptr)++; |
53 | 488k | if (val >= 252) { |
54 | 33.5k | if (*ptr >= end) |
55 | 6.79k | return AVERROR_INVALIDDATA; |
56 | 26.7k | val += 4 * *(*ptr)++; |
57 | 26.7k | } |
58 | 481k | return val; |
59 | 488k | } |
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 | 34.9k | { |
66 | 34.9k | int val = 0; |
67 | 34.9k | int next; |
68 | | |
69 | 363k | while (1) { |
70 | 363k | if (*ptr >= end || val > INT_MAX - 254) |
71 | 6.92k | return AVERROR_INVALIDDATA; |
72 | 356k | next = *(*ptr)++; |
73 | 356k | val += next; |
74 | 356k | if (next < 255) |
75 | 28.0k | break; |
76 | 328k | else |
77 | 328k | val--; |
78 | 356k | } |
79 | 28.0k | return val; |
80 | 34.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 | 840k | { |
88 | 840k | const uint8_t *ptr = buf; |
89 | 840k | const uint8_t *end = buf + buf_size; |
90 | 840k | int padding = 0; |
91 | 840k | int frame_bytes, i; |
92 | | |
93 | 840k | if (buf_size < 1) |
94 | 1.66k | goto fail; |
95 | | |
96 | | /* TOC byte */ |
97 | 839k | i = *ptr++; |
98 | 839k | pkt->code = (i ) & 0x3; |
99 | 839k | pkt->stereo = (i >> 2) & 0x1; |
100 | 839k | pkt->config = (i >> 3) & 0x1F; |
101 | | |
102 | | /* code 2 and code 3 packets have at least 1 byte after the TOC */ |
103 | 839k | if (pkt->code >= 2 && buf_size < 2) |
104 | 72.7k | goto fail; |
105 | | |
106 | 766k | switch (pkt->code) { |
107 | 393k | case 0: |
108 | | /* 1 frame */ |
109 | 393k | pkt->frame_count = 1; |
110 | 393k | pkt->vbr = 0; |
111 | | |
112 | 393k | if (self_delimiting) { |
113 | 115k | int len = xiph_lacing_16bit(&ptr, end); |
114 | 115k | if (len < 0 || len > end - ptr) |
115 | 70.1k | goto fail; |
116 | 45.6k | end = ptr + len; |
117 | 45.6k | buf_size = end - buf; |
118 | 45.6k | } |
119 | | |
120 | 322k | frame_bytes = end - ptr; |
121 | 322k | if (frame_bytes > OPUS_MAX_FRAME_SIZE) |
122 | 837 | goto fail; |
123 | 322k | pkt->frame_offset[0] = ptr - buf; |
124 | 322k | pkt->frame_size[0] = frame_bytes; |
125 | 322k | break; |
126 | 191k | case 1: |
127 | | /* 2 frames, equal size */ |
128 | 191k | pkt->frame_count = 2; |
129 | 191k | pkt->vbr = 0; |
130 | | |
131 | 191k | if (self_delimiting) { |
132 | 43.6k | int len = xiph_lacing_16bit(&ptr, end); |
133 | 43.6k | if (len < 0 || 2 * len > end - ptr) |
134 | 38.1k | goto fail; |
135 | 5.48k | end = ptr + 2 * len; |
136 | 5.48k | buf_size = end - buf; |
137 | 5.48k | } |
138 | | |
139 | 153k | frame_bytes = end - ptr; |
140 | 153k | if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE) |
141 | 25.9k | goto fail; |
142 | 127k | pkt->frame_offset[0] = ptr - buf; |
143 | 127k | pkt->frame_size[0] = frame_bytes >> 1; |
144 | 127k | pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; |
145 | 127k | pkt->frame_size[1] = frame_bytes >> 1; |
146 | 127k | break; |
147 | 81.9k | case 2: |
148 | | /* 2 frames, different sizes */ |
149 | 81.9k | pkt->frame_count = 2; |
150 | 81.9k | pkt->vbr = 1; |
151 | | |
152 | | /* read 1st frame size */ |
153 | 81.9k | frame_bytes = xiph_lacing_16bit(&ptr, end); |
154 | 81.9k | if (frame_bytes < 0) |
155 | 1.40k | goto fail; |
156 | | |
157 | 80.5k | if (self_delimiting) { |
158 | 31.6k | int len = xiph_lacing_16bit(&ptr, end); |
159 | 31.6k | if (len < 0 || len + frame_bytes > end - ptr) |
160 | 27.3k | goto fail; |
161 | 4.26k | end = ptr + frame_bytes + len; |
162 | 4.26k | buf_size = end - buf; |
163 | 4.26k | } |
164 | | |
165 | 53.1k | pkt->frame_offset[0] = ptr - buf; |
166 | 53.1k | pkt->frame_size[0] = frame_bytes; |
167 | | |
168 | | /* calculate 2nd frame size */ |
169 | 53.1k | frame_bytes = end - ptr - pkt->frame_size[0]; |
170 | 53.1k | if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE) |
171 | 17.5k | goto fail; |
172 | 35.6k | pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; |
173 | 35.6k | pkt->frame_size[1] = frame_bytes; |
174 | 35.6k | break; |
175 | 99.6k | case 3: |
176 | | /* 1 to 48 frames, can be different sizes */ |
177 | 99.6k | i = *ptr++; |
178 | 99.6k | pkt->frame_count = (i ) & 0x3F; |
179 | 99.6k | padding = (i >> 6) & 0x01; |
180 | 99.6k | pkt->vbr = (i >> 7) & 0x01; |
181 | | |
182 | 99.6k | if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES) |
183 | 28.2k | goto fail; |
184 | | |
185 | | /* read padding size */ |
186 | 71.3k | if (padding) { |
187 | 34.9k | padding = xiph_lacing_full(&ptr, end); |
188 | 34.9k | if (padding < 0) |
189 | 6.92k | goto fail; |
190 | 34.9k | } |
191 | | |
192 | | /* read frame sizes */ |
193 | 64.4k | 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.1k | int total_bytes = 0; |
197 | 245k | for (i = 0; i < pkt->frame_count - 1; i++) { |
198 | 223k | frame_bytes = xiph_lacing_16bit(&ptr, end); |
199 | 223k | if (frame_bytes < 0) |
200 | 8.45k | goto fail; |
201 | 215k | pkt->frame_size[i] = frame_bytes; |
202 | 215k | total_bytes += frame_bytes; |
203 | 215k | } |
204 | | |
205 | 21.7k | if (self_delimiting) { |
206 | 6.60k | int len = xiph_lacing_16bit(&ptr, end); |
207 | 6.60k | if (len < 0 || len + total_bytes + padding > end - ptr) |
208 | 3.91k | goto fail; |
209 | 2.69k | end = ptr + total_bytes + len + padding; |
210 | 2.69k | buf_size = end - buf; |
211 | 2.69k | } |
212 | | |
213 | 17.8k | frame_bytes = end - ptr - padding; |
214 | 17.8k | if (total_bytes > frame_bytes) |
215 | 5.80k | goto fail; |
216 | 12.0k | pkt->frame_offset[0] = ptr - buf; |
217 | 69.9k | for (i = 1; i < pkt->frame_count; i++) |
218 | 57.9k | pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; |
219 | 12.0k | pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes; |
220 | 34.2k | } else { |
221 | | /* for CBR, the remaining packet bytes are divided evenly between |
222 | | the frames */ |
223 | 34.2k | if (self_delimiting) { |
224 | 13.0k | frame_bytes = xiph_lacing_16bit(&ptr, end); |
225 | 13.0k | if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr) |
226 | 10.1k | goto fail; |
227 | 2.89k | end = ptr + pkt->frame_count * frame_bytes + padding; |
228 | 2.89k | buf_size = end - buf; |
229 | 21.2k | } else { |
230 | 21.2k | frame_bytes = end - ptr - padding; |
231 | 21.2k | if (frame_bytes % pkt->frame_count || |
232 | 11.3k | frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE) |
233 | 10.4k | goto fail; |
234 | 10.7k | frame_bytes /= pkt->frame_count; |
235 | 10.7k | } |
236 | | |
237 | 13.6k | pkt->frame_offset[0] = ptr - buf; |
238 | 13.6k | pkt->frame_size[0] = frame_bytes; |
239 | 110k | for (i = 1; i < pkt->frame_count; i++) { |
240 | 96.7k | pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; |
241 | 96.7k | pkt->frame_size[i] = frame_bytes; |
242 | 96.7k | } |
243 | 13.6k | } |
244 | 766k | } |
245 | | |
246 | 510k | pkt->packet_size = buf_size; |
247 | 510k | pkt->data_size = pkt->packet_size - padding; |
248 | | |
249 | | /* total packet duration cannot be larger than 120ms */ |
250 | 510k | pkt->frame_duration = ff_opus_frame_duration[pkt->config]; |
251 | 510k | if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR) |
252 | 2.57k | goto fail; |
253 | | |
254 | | /* set mode and bandwidth */ |
255 | 508k | if (pkt->config < 12) { |
256 | 408k | pkt->mode = OPUS_MODE_SILK; |
257 | 408k | pkt->bandwidth = pkt->config >> 2; |
258 | 408k | } else if (pkt->config < 16) { |
259 | 30.2k | pkt->mode = OPUS_MODE_HYBRID; |
260 | 30.2k | pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14); |
261 | 69.5k | } else { |
262 | 69.5k | pkt->mode = OPUS_MODE_CELT; |
263 | 69.5k | pkt->bandwidth = (pkt->config - 16) >> 2; |
264 | | /* skip medium band */ |
265 | 69.5k | if (pkt->bandwidth) |
266 | 56.1k | pkt->bandwidth++; |
267 | 69.5k | } |
268 | | |
269 | 508k | return 0; |
270 | | |
271 | 332k | fail: |
272 | 332k | memset(pkt, 0, sizeof(*pkt)); |
273 | 332k | return AVERROR_INVALIDDATA; |
274 | 510k | } |
275 | | |
276 | | static int channel_reorder_vorbis(int nb_channels, int channel_idx) |
277 | 8.32k | { |
278 | 8.32k | return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx]; |
279 | 8.32k | } |
280 | | |
281 | | static int channel_reorder_unknown(int nb_channels, int channel_idx) |
282 | 383k | { |
283 | 383k | return channel_idx; |
284 | 383k | } |
285 | | |
286 | | av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, |
287 | | OpusParseContext *s) |
288 | 37.7k | { |
289 | 37.7k | static const uint8_t default_channel_map[2] = { 0, 1 }; |
290 | | |
291 | 37.7k | int (*channel_reorder)(int, int) = channel_reorder_unknown; |
292 | 37.7k | int channels = avctx->ch_layout.nb_channels; |
293 | | |
294 | 37.7k | const uint8_t *extradata, *channel_map; |
295 | 37.7k | int extradata_size; |
296 | 37.7k | int version, map_type, streams, stereo_streams, i, j, ret; |
297 | 37.7k | AVChannelLayout layout = { 0 }; |
298 | | |
299 | 37.7k | if (!avctx->extradata) { |
300 | 4.56k | if (channels > 2) { |
301 | 57 | av_log(avctx, AV_LOG_ERROR, |
302 | 57 | "Multichannel configuration without extradata.\n"); |
303 | 57 | return AVERROR(EINVAL); |
304 | 57 | } |
305 | 4.51k | extradata = opus_default_extradata; |
306 | 4.51k | extradata_size = sizeof(opus_default_extradata); |
307 | 33.1k | } else { |
308 | 33.1k | extradata = avctx->extradata; |
309 | 33.1k | extradata_size = avctx->extradata_size; |
310 | 33.1k | } |
311 | | |
312 | 37.6k | if (extradata_size < 19) { |
313 | 3.95k | av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", |
314 | 3.95k | extradata_size); |
315 | 3.95k | return AVERROR_INVALIDDATA; |
316 | 3.95k | } |
317 | | |
318 | 33.7k | version = extradata[8]; |
319 | 33.7k | if (version > 15) { |
320 | 10.5k | avpriv_request_sample(avctx, "Extradata version %d", version); |
321 | 10.5k | return AVERROR_PATCHWELCOME; |
322 | 10.5k | } |
323 | | |
324 | 23.1k | avctx->delay = AV_RL16(extradata + 10); |
325 | | |
326 | 23.1k | channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2; |
327 | 23.1k | if (!channels) { |
328 | 4.05k | av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n"); |
329 | 4.05k | return AVERROR_INVALIDDATA; |
330 | 4.05k | } |
331 | | |
332 | 19.1k | s->gain_i = AV_RL16(extradata + 16); |
333 | | |
334 | 19.1k | map_type = extradata[18]; |
335 | 19.1k | if (!map_type) { |
336 | 5.06k | if (channels > 2) { |
337 | 346 | av_log(avctx, AV_LOG_ERROR, |
338 | 346 | "Channel mapping 0 is only specified for up to 2 channels\n"); |
339 | 346 | ret = AVERROR_INVALIDDATA; |
340 | 346 | goto fail; |
341 | 346 | } |
342 | 4.71k | layout = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO : |
343 | 4.71k | (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; |
344 | 4.71k | streams = 1; |
345 | 4.71k | stereo_streams = channels - 1; |
346 | 4.71k | channel_map = default_channel_map; |
347 | 14.0k | } else if (map_type == 1 || map_type == 2 || map_type == 255) { |
348 | 12.8k | 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 | 11.5k | streams = extradata[19]; |
356 | 11.5k | stereo_streams = extradata[20]; |
357 | 11.5k | if (!streams || stereo_streams > streams || |
358 | 8.75k | 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 | 8.74k | if (map_type == 1) { |
366 | 6.68k | if (channels > 8) { |
367 | 5.92k | av_log(avctx, AV_LOG_ERROR, |
368 | 5.92k | "Channel mapping 1 is only specified for up to 8 channels\n"); |
369 | 5.92k | ret = AVERROR_INVALIDDATA; |
370 | 5.92k | goto fail; |
371 | 5.92k | } |
372 | 758 | av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]); |
373 | 758 | channel_reorder = channel_reorder_vorbis; |
374 | 2.06k | } else if (map_type == 2) { |
375 | 1.52k | int ambisonic_order = ff_sqrt(channels) - 1; |
376 | 1.52k | if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) && |
377 | 1.49k | channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) { |
378 | 1.47k | av_log(avctx, AV_LOG_ERROR, |
379 | 1.47k | "Channel mapping 2 is only specified for channel counts" |
380 | 1.47k | " which can be written as (n + 1)^2 or (n + 1)^2 + 2" |
381 | 1.47k | " for nonnegative integer n\n"); |
382 | 1.47k | ret = AVERROR_INVALIDDATA; |
383 | 1.47k | goto fail; |
384 | 1.47k | } |
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 | 20 | layout.u.mask = AV_CH_LAYOUT_STEREO; |
395 | 543 | } else { |
396 | 543 | layout.order = AV_CHANNEL_ORDER_UNSPEC; |
397 | 543 | layout.nb_channels = channels; |
398 | 543 | } |
399 | | |
400 | 1.34k | channel_map = extradata + 21; |
401 | 1.34k | } else { |
402 | 1.18k | avpriv_request_sample(avctx, "Mapping type %d", map_type); |
403 | 1.18k | return AVERROR_PATCHWELCOME; |
404 | 1.18k | } |
405 | | |
406 | 6.06k | s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps)); |
407 | 6.06k | if (!s->channel_maps) { |
408 | 0 | ret = AVERROR(ENOMEM); |
409 | 0 | goto fail; |
410 | 0 | } |
411 | | |
412 | 35.8k | for (i = 0; i < channels; i++) { |
413 | 29.7k | ChannelMap *map = &s->channel_maps[i]; |
414 | 29.7k | uint8_t idx = channel_map[channel_reorder(channels, i)]; |
415 | | |
416 | 29.7k | if (idx == 255) { |
417 | 1.56k | map->silence = 1; |
418 | 1.56k | continue; |
419 | 28.1k | } else if (idx >= streams + stereo_streams) { |
420 | 11 | av_log(avctx, AV_LOG_ERROR, |
421 | 11 | "Invalid channel map for output channel %d: %d\n", i, idx); |
422 | 11 | av_freep(&s->channel_maps); |
423 | 11 | ret = AVERROR_INVALIDDATA; |
424 | 11 | goto fail; |
425 | 11 | } |
426 | | |
427 | | /* check that we did not see this index yet */ |
428 | 28.1k | map->copy = 0; |
429 | 376k | for (j = 0; j < i; j++) |
430 | 361k | if (channel_map[channel_reorder(channels, j)] == idx) { |
431 | 14.0k | map->copy = 1; |
432 | 14.0k | map->copy_idx = j; |
433 | 14.0k | break; |
434 | 14.0k | } |
435 | | |
436 | 28.1k | if (idx < 2 * stereo_streams) { |
437 | 15.7k | map->stream_idx = idx / 2; |
438 | 15.7k | map->channel_idx = idx & 1; |
439 | 15.7k | } else { |
440 | 12.3k | map->stream_idx = idx - stereo_streams; |
441 | 12.3k | map->channel_idx = 0; |
442 | 12.3k | } |
443 | 28.1k | } |
444 | | |
445 | 6.05k | ret = av_channel_layout_copy(&avctx->ch_layout, &layout); |
446 | 6.05k | if (ret < 0) |
447 | 0 | goto fail; |
448 | | |
449 | 6.05k | s->nb_streams = streams; |
450 | 6.05k | s->nb_stereo_streams = stereo_streams; |
451 | | |
452 | 6.05k | return 0; |
453 | 11.8k | fail: |
454 | 11.8k | av_channel_layout_uninit(&layout); |
455 | 11.8k | return ret; |
456 | 6.05k | } |