2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
5 * This file is part of FFmpeg.
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.
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.
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
24 * Opus decoder/parser shared code
29 #include "libavutil/error.h"
34 static const uint16_t opus_frame_duration[32] = {
47 * Read a 1- or 2-byte frame length
49 static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
54 return AVERROR_INVALIDDATA;
58 return AVERROR_INVALIDDATA;
65 * Read a multi-byte length (used for code 3 packet padding size)
67 static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
73 if (*ptr >= end || val > INT_MAX - 254)
74 return AVERROR_INVALIDDATA;
86 * Parse Opus packet info from raw packet data
88 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
91 const uint8_t *ptr = buf;
92 const uint8_t *end = buf + buf_size;
101 pkt->code = (i ) & 0x3;
102 pkt->stereo = (i >> 2) & 0x1;
103 pkt->config = (i >> 3) & 0x1F;
105 /* code 2 and code 3 packets have at least 1 byte after the TOC */
106 if (pkt->code >= 2 && buf_size < 2)
112 pkt->frame_count = 1;
115 if (self_delimiting) {
116 int len = xiph_lacing_16bit(&ptr, end);
117 if (len < 0 || len > end - ptr)
120 buf_size = end - buf;
123 frame_bytes = end - ptr;
124 if (frame_bytes > MAX_FRAME_SIZE)
126 pkt->frame_offset[0] = ptr - buf;
127 pkt->frame_size[0] = frame_bytes;
130 /* 2 frames, equal size */
131 pkt->frame_count = 2;
134 if (self_delimiting) {
135 int len = xiph_lacing_16bit(&ptr, end);
136 if (len < 0 || 2 * len > end - ptr)
139 buf_size = end - buf;
142 frame_bytes = end - ptr;
143 if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
145 pkt->frame_offset[0] = ptr - buf;
146 pkt->frame_size[0] = frame_bytes >> 1;
147 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
148 pkt->frame_size[1] = frame_bytes >> 1;
151 /* 2 frames, different sizes */
152 pkt->frame_count = 2;
155 /* read 1st frame size */
156 frame_bytes = xiph_lacing_16bit(&ptr, end);
160 if (self_delimiting) {
161 int len = xiph_lacing_16bit(&ptr, end);
162 if (len < 0 || len + frame_bytes > end - ptr)
164 end = ptr + frame_bytes + len;
165 buf_size = end - buf;
168 pkt->frame_offset[0] = ptr - buf;
169 pkt->frame_size[0] = frame_bytes;
171 /* calculate 2nd frame size */
172 frame_bytes = end - ptr - pkt->frame_size[0];
173 if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
175 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
176 pkt->frame_size[1] = frame_bytes;
179 /* 1 to 48 frames, can be different sizes */
181 pkt->frame_count = (i ) & 0x3F;
182 padding = (i >> 6) & 0x01;
183 pkt->vbr = (i >> 7) & 0x01;
185 if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
188 /* read padding size */
190 padding = xiph_lacing_full(&ptr, end);
195 /* read frame sizes */
197 /* for VBR, all frames except the final one have their size coded
198 in the bitstream. the last frame size is implicit. */
200 for (i = 0; i < pkt->frame_count - 1; i++) {
201 frame_bytes = xiph_lacing_16bit(&ptr, end);
204 pkt->frame_size[i] = frame_bytes;
205 total_bytes += frame_bytes;
208 if (self_delimiting) {
209 int len = xiph_lacing_16bit(&ptr, end);
210 if (len < 0 || len + total_bytes + padding > end - ptr)
212 end = ptr + total_bytes + len + padding;
213 buf_size = end - buf;
216 frame_bytes = end - ptr - padding;
217 if (total_bytes > frame_bytes)
219 pkt->frame_offset[0] = ptr - buf;
220 for (i = 1; i < pkt->frame_count; i++)
221 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
222 pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
224 /* for CBR, the remaining packet bytes are divided evenly between
226 if (self_delimiting) {
227 frame_bytes = xiph_lacing_16bit(&ptr, end);
228 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
230 end = ptr + pkt->frame_count * frame_bytes + padding;
231 buf_size = end - buf;
233 frame_bytes = end - ptr - padding;
234 if (frame_bytes % pkt->frame_count ||
235 frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
237 frame_bytes /= pkt->frame_count;
240 pkt->frame_offset[0] = ptr - buf;
241 pkt->frame_size[0] = frame_bytes;
242 for (i = 1; i < pkt->frame_count; i++) {
243 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
244 pkt->frame_size[i] = frame_bytes;
249 pkt->packet_size = buf_size;
250 pkt->data_size = pkt->packet_size - padding;
252 /* total packet duration cannot be larger than 120ms */
253 pkt->frame_duration = opus_frame_duration[pkt->config];
254 if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR)
257 /* set mode and bandwidth */
258 if (pkt->config < 12) {
259 pkt->mode = OPUS_MODE_SILK;
260 pkt->bandwidth = pkt->config >> 2;
261 } else if (pkt->config < 16) {
262 pkt->mode = OPUS_MODE_HYBRID;
263 pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
265 pkt->mode = OPUS_MODE_CELT;
266 pkt->bandwidth = (pkt->config - 16) >> 2;
267 /* skip mediumband */
275 memset(pkt, 0, sizeof(*pkt));
276 return AVERROR_INVALIDDATA;
279 static int channel_reorder_vorbis(int nb_channels, int channel_idx)
281 return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
284 static int channel_reorder_unknown(int nb_channels, int channel_idx)
289 av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
292 static const uint8_t default_channel_map[2] = { 0, 1 };
294 int (*channel_reorder)(int, int) = channel_reorder_unknown;
296 const uint8_t *extradata, *channel_map;
298 int version, channels, map_type, streams, stereo_streams, i, j;
301 if (!avctx->extradata) {
302 if (avctx->channels > 2) {
303 av_log(avctx, AV_LOG_ERROR,
304 "Multichannel configuration without extradata.\n");
305 return AVERROR(EINVAL);
307 extradata = opus_default_extradata;
308 extradata_size = sizeof(opus_default_extradata);
310 extradata = avctx->extradata;
311 extradata_size = avctx->extradata_size;
314 if (extradata_size < 19) {
315 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
317 return AVERROR_INVALIDDATA;
320 version = extradata[8];
322 avpriv_request_sample(avctx, "Extradata version %d", version);
323 return AVERROR_PATCHWELCOME;
326 avctx->delay = AV_RL16(extradata + 10);
328 channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
330 av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\n");
331 return AVERROR_INVALIDDATA;
334 s->gain_i = AV_RL16(extradata + 16);
336 s->gain = pow(10, s->gain_i / (20.0 * 256));
338 map_type = extradata[18];
341 av_log(avctx, AV_LOG_ERROR,
342 "Channel mapping 0 is only specified for up to 2 channels\n");
343 return AVERROR_INVALIDDATA;
345 layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
347 stereo_streams = channels - 1;
348 channel_map = default_channel_map;
349 } else if (map_type == 1 || map_type == 255) {
350 if (extradata_size < 21 + channels) {
351 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
353 return AVERROR_INVALIDDATA;
356 streams = extradata[19];
357 stereo_streams = extradata[20];
358 if (!streams || stereo_streams > streams ||
359 streams + stereo_streams > 255) {
360 av_log(avctx, AV_LOG_ERROR,
361 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
362 return AVERROR_INVALIDDATA;
367 av_log(avctx, AV_LOG_ERROR,
368 "Channel mapping 1 is only specified for up to 8 channels\n");
369 return AVERROR_INVALIDDATA;
371 layout = ff_vorbis_channel_layouts[channels - 1];
372 channel_reorder = channel_reorder_vorbis;
376 channel_map = extradata + 21;
378 avpriv_request_sample(avctx, "Mapping type %d", map_type);
379 return AVERROR_PATCHWELCOME;
382 s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));
383 if (!s->channel_maps)
384 return AVERROR(ENOMEM);
386 for (i = 0; i < channels; i++) {
387 ChannelMap *map = &s->channel_maps[i];
388 uint8_t idx = channel_map[channel_reorder(channels, i)];
393 } else if (idx >= streams + stereo_streams) {
394 av_log(avctx, AV_LOG_ERROR,
395 "Invalid channel map for output channel %d: %d\n", i, idx);
396 return AVERROR_INVALIDDATA;
399 /* check that we din't see this index yet */
401 for (j = 0; j < i; j++)
402 if (channel_map[channel_reorder(channels, j)] == idx) {
408 if (idx < 2 * stereo_streams) {
409 map->stream_idx = idx / 2;
410 map->channel_idx = idx & 1;
412 map->stream_idx = idx - stereo_streams;
413 map->channel_idx = 0;
417 avctx->channels = channels;
418 avctx->channel_layout = layout;
419 s->nb_streams = streams;
420 s->nb_stereo_streams = stereo_streams;