summaryrefslogtreecommitdiff
path: root/libavcodec/opus.c (plain)
blob: 5847e88e11d3e01bc5f860615564b128686327e3
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 <stdint.h>
28
29#include "libavutil/error.h"
30#include "libavutil/ffmath.h"
31
32#include "opus.h"
33#include "vorbis.h"
34
35static const uint16_t opus_frame_duration[32] = {
36 480, 960, 1920, 2880,
37 480, 960, 1920, 2880,
38 480, 960, 1920, 2880,
39 480, 960,
40 480, 960,
41 120, 240, 480, 960,
42 120, 240, 480, 960,
43 120, 240, 480, 960,
44 120, 240, 480, 960,
45};
46
47/**
48 * Read a 1- or 2-byte frame length
49 */
50static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
51{
52 int val;
53
54 if (*ptr >= end)
55 return AVERROR_INVALIDDATA;
56 val = *(*ptr)++;
57 if (val >= 252) {
58 if (*ptr >= end)
59 return AVERROR_INVALIDDATA;
60 val += 4 * *(*ptr)++;
61 }
62 return val;
63}
64
65/**
66 * Read a multi-byte length (used for code 3 packet padding size)
67 */
68static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
69{
70 int val = 0;
71 int next;
72
73 while (1) {
74 if (*ptr >= end || val > INT_MAX - 254)
75 return AVERROR_INVALIDDATA;
76 next = *(*ptr)++;
77 val += next;
78 if (next < 255)
79 break;
80 else
81 val--;
82 }
83 return val;
84}
85
86/**
87 * Parse Opus packet info from raw packet data
88 */
89int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
90 int self_delimiting)
91{
92 const uint8_t *ptr = buf;
93 const uint8_t *end = buf + buf_size;
94 int padding = 0;
95 int frame_bytes, i;
96
97 if (buf_size < 1)
98 goto fail;
99
100 /* TOC byte */
101 i = *ptr++;
102 pkt->code = (i ) & 0x3;
103 pkt->stereo = (i >> 2) & 0x1;
104 pkt->config = (i >> 3) & 0x1F;
105
106 /* code 2 and code 3 packets have at least 1 byte after the TOC */
107 if (pkt->code >= 2 && buf_size < 2)
108 goto fail;
109
110 switch (pkt->code) {
111 case 0:
112 /* 1 frame */
113 pkt->frame_count = 1;
114 pkt->vbr = 0;
115
116 if (self_delimiting) {
117 int len = xiph_lacing_16bit(&ptr, end);
118 if (len < 0 || len > end - ptr)
119 goto fail;
120 end = ptr + len;
121 buf_size = end - buf;
122 }
123
124 frame_bytes = end - ptr;
125 if (frame_bytes > MAX_FRAME_SIZE)
126 goto fail;
127 pkt->frame_offset[0] = ptr - buf;
128 pkt->frame_size[0] = frame_bytes;
129 break;
130 case 1:
131 /* 2 frames, equal size */
132 pkt->frame_count = 2;
133 pkt->vbr = 0;
134
135 if (self_delimiting) {
136 int len = xiph_lacing_16bit(&ptr, end);
137 if (len < 0 || 2 * len > end - ptr)
138 goto fail;
139 end = ptr + 2 * len;
140 buf_size = end - buf;
141 }
142
143 frame_bytes = end - ptr;
144 if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
145 goto fail;
146 pkt->frame_offset[0] = ptr - buf;
147 pkt->frame_size[0] = frame_bytes >> 1;
148 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
149 pkt->frame_size[1] = frame_bytes >> 1;
150 break;
151 case 2:
152 /* 2 frames, different sizes */
153 pkt->frame_count = 2;
154 pkt->vbr = 1;
155
156 /* read 1st frame size */
157 frame_bytes = xiph_lacing_16bit(&ptr, end);
158 if (frame_bytes < 0)
159 goto fail;
160
161 if (self_delimiting) {
162 int len = xiph_lacing_16bit(&ptr, end);
163 if (len < 0 || len + frame_bytes > end - ptr)
164 goto fail;
165 end = ptr + frame_bytes + len;
166 buf_size = end - buf;
167 }
168
169 pkt->frame_offset[0] = ptr - buf;
170 pkt->frame_size[0] = frame_bytes;
171
172 /* calculate 2nd frame size */
173 frame_bytes = end - ptr - pkt->frame_size[0];
174 if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
175 goto fail;
176 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
177 pkt->frame_size[1] = frame_bytes;
178 break;
179 case 3:
180 /* 1 to 48 frames, can be different sizes */
181 i = *ptr++;
182 pkt->frame_count = (i ) & 0x3F;
183 padding = (i >> 6) & 0x01;
184 pkt->vbr = (i >> 7) & 0x01;
185
186 if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
187 goto fail;
188
189 /* read padding size */
190 if (padding) {
191 padding = xiph_lacing_full(&ptr, end);
192 if (padding < 0)
193 goto fail;
194 }
195
196 /* read frame sizes */
197 if (pkt->vbr) {
198 /* for VBR, all frames except the final one have their size coded
199 in the bitstream. the last frame size is implicit. */
200 int total_bytes = 0;
201 for (i = 0; i < pkt->frame_count - 1; i++) {
202 frame_bytes = xiph_lacing_16bit(&ptr, end);
203 if (frame_bytes < 0)
204 goto fail;
205 pkt->frame_size[i] = frame_bytes;
206 total_bytes += frame_bytes;
207 }
208
209 if (self_delimiting) {
210 int len = xiph_lacing_16bit(&ptr, end);
211 if (len < 0 || len + total_bytes + padding > end - ptr)
212 goto fail;
213 end = ptr + total_bytes + len + padding;
214 buf_size = end - buf;
215 }
216
217 frame_bytes = end - ptr - padding;
218 if (total_bytes > frame_bytes)
219 goto fail;
220 pkt->frame_offset[0] = ptr - buf;
221 for (i = 1; i < pkt->frame_count; i++)
222 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
223 pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
224 } else {
225 /* for CBR, the remaining packet bytes are divided evenly between
226 the frames */
227 if (self_delimiting) {
228 frame_bytes = xiph_lacing_16bit(&ptr, end);
229 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
230 goto fail;
231 end = ptr + pkt->frame_count * frame_bytes + padding;
232 buf_size = end - buf;
233 } else {
234 frame_bytes = end - ptr - padding;
235 if (frame_bytes % pkt->frame_count ||
236 frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
237 goto fail;
238 frame_bytes /= pkt->frame_count;
239 }
240
241 pkt->frame_offset[0] = ptr - buf;
242 pkt->frame_size[0] = frame_bytes;
243 for (i = 1; i < pkt->frame_count; i++) {
244 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
245 pkt->frame_size[i] = frame_bytes;
246 }
247 }
248 }
249
250 pkt->packet_size = buf_size;
251 pkt->data_size = pkt->packet_size - padding;
252
253 /* total packet duration cannot be larger than 120ms */
254 pkt->frame_duration = opus_frame_duration[pkt->config];
255 if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR)
256 goto fail;
257
258 /* set mode and bandwidth */
259 if (pkt->config < 12) {
260 pkt->mode = OPUS_MODE_SILK;
261 pkt->bandwidth = pkt->config >> 2;
262 } else if (pkt->config < 16) {
263 pkt->mode = OPUS_MODE_HYBRID;
264 pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
265 } else {
266 pkt->mode = OPUS_MODE_CELT;
267 pkt->bandwidth = (pkt->config - 16) >> 2;
268 /* skip medium band */
269 if (pkt->bandwidth)
270 pkt->bandwidth++;
271 }
272
273 return 0;
274
275fail:
276 memset(pkt, 0, sizeof(*pkt));
277 return AVERROR_INVALIDDATA;
278}
279
280static int channel_reorder_vorbis(int nb_channels, int channel_idx)
281{
282 return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
283}
284
285static int channel_reorder_unknown(int nb_channels, int channel_idx)
286{
287 return channel_idx;
288}
289
290av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
291 OpusContext *s)
292{
293 static const uint8_t default_channel_map[2] = { 0, 1 };
294
295 int (*channel_reorder)(int, int) = channel_reorder_unknown;
296
297 const uint8_t *extradata, *channel_map;
298 int extradata_size;
299 int version, channels, map_type, streams, stereo_streams, i, j;
300 uint64_t layout;
301
302 if (!avctx->extradata) {
303 if (avctx->channels > 2) {
304 av_log(avctx, AV_LOG_ERROR,
305 "Multichannel configuration without extradata.\n");
306 return AVERROR(EINVAL);
307 }
308 extradata = opus_default_extradata;
309 extradata_size = sizeof(opus_default_extradata);
310 } else {
311 extradata = avctx->extradata;
312 extradata_size = avctx->extradata_size;
313 }
314
315 if (extradata_size < 19) {
316 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
317 extradata_size);
318 return AVERROR_INVALIDDATA;
319 }
320
321 version = extradata[8];
322 if (version > 15) {
323 avpriv_request_sample(avctx, "Extradata version %d", version);
324 return AVERROR_PATCHWELCOME;
325 }
326
327 avctx->delay = AV_RL16(extradata + 10);
328
329 channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
330 if (!channels) {
331 av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
332 return AVERROR_INVALIDDATA;
333 }
334
335 s->gain_i = AV_RL16(extradata + 16);
336 if (s->gain_i)
337 s->gain = ff_exp10(s->gain_i / (20.0 * 256));
338
339 map_type = extradata[18];
340 if (!map_type) {
341 if (channels > 2) {
342 av_log(avctx, AV_LOG_ERROR,
343 "Channel mapping 0 is only specified for up to 2 channels\n");
344 return AVERROR_INVALIDDATA;
345 }
346 layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
347 streams = 1;
348 stereo_streams = channels - 1;
349 channel_map = default_channel_map;
350 } else if (map_type == 1 || map_type == 2 || map_type == 255) {
351 if (extradata_size < 21 + channels) {
352 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
353 extradata_size);
354 return AVERROR_INVALIDDATA;
355 }
356
357 streams = extradata[19];
358 stereo_streams = extradata[20];
359 if (!streams || stereo_streams > streams ||
360 streams + stereo_streams > 255) {
361 av_log(avctx, AV_LOG_ERROR,
362 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
363 return AVERROR_INVALIDDATA;
364 }
365
366 if (map_type == 1) {
367 if (channels > 8) {
368 av_log(avctx, AV_LOG_ERROR,
369 "Channel mapping 1 is only specified for up to 8 channels\n");
370 return AVERROR_INVALIDDATA;
371 }
372 layout = ff_vorbis_channel_layouts[channels - 1];
373 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 return AVERROR_INVALIDDATA;
383 }
384 if (channels > 227) {
385 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
386 return AVERROR_INVALIDDATA;
387 }
388 layout = 0;
389 } else
390 layout = 0;
391
392 channel_map = extradata + 21;
393 } else {
394 avpriv_request_sample(avctx, "Mapping type %d", map_type);
395 return AVERROR_PATCHWELCOME;
396 }
397
398 s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));
399 if (!s->channel_maps)
400 return AVERROR(ENOMEM);
401
402 for (i = 0; i < channels; i++) {
403 ChannelMap *map = &s->channel_maps[i];
404 uint8_t idx = channel_map[channel_reorder(channels, i)];
405
406 if (idx == 255) {
407 map->silence = 1;
408 continue;
409 } else if (idx >= streams + stereo_streams) {
410 av_log(avctx, AV_LOG_ERROR,
411 "Invalid channel map for output channel %d: %d\n", i, idx);
412 av_freep(&s->channel_maps);
413 return AVERROR_INVALIDDATA;
414 }
415
416 /* check that we did not see this index yet */
417 map->copy = 0;
418 for (j = 0; j < i; j++)
419 if (channel_map[channel_reorder(channels, j)] == idx) {
420 map->copy = 1;
421 map->copy_idx = j;
422 break;
423 }
424
425 if (idx < 2 * stereo_streams) {
426 map->stream_idx = idx / 2;
427 map->channel_idx = idx & 1;
428 } else {
429 map->stream_idx = idx - stereo_streams;
430 map->channel_idx = 0;
431 }
432 }
433
434 avctx->channels = channels;
435 avctx->channel_layout = layout;
436 s->nb_streams = streams;
437 s->nb_stereo_streams = stereo_streams;
438
439 return 0;
440}
441