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"
30 #include "libavutil/ffmath.h"
35 static const uint16_t opus_frame_duration[32] = {
48 * Read a 1- or 2-byte frame length
50 static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
55 return AVERROR_INVALIDDATA;
59 return AVERROR_INVALIDDATA;
66 * Read a multi-byte length (used for code 3 packet padding size)
68 static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
74 if (*ptr >= end || val > INT_MAX - 254)
75 return AVERROR_INVALIDDATA;
87 * Parse Opus packet info from raw packet data
89 int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
92 const uint8_t *ptr = buf;
93 const uint8_t *end = buf + buf_size;
102 pkt->code = (i ) & 0x3;
103 pkt->stereo = (i >> 2) & 0x1;
104 pkt->config = (i >> 3) & 0x1F;
106 /* code 2 and code 3 packets have at least 1 byte after the TOC */
107 if (pkt->code >= 2 && buf_size < 2)
113 pkt->frame_count = 1;
116 if (self_delimiting) {
117 int len = xiph_lacing_16bit(&ptr, end);
118 if (len < 0 || len > end - ptr)
121 buf_size = end - buf;
124 frame_bytes = end - ptr;
125 if (frame_bytes > MAX_FRAME_SIZE)
127 pkt->frame_offset[0] = ptr - buf;
128 pkt->frame_size[0] = frame_bytes;
131 /* 2 frames, equal size */
132 pkt->frame_count = 2;
135 if (self_delimiting) {
136 int len = xiph_lacing_16bit(&ptr, end);
137 if (len < 0 || 2 * len > end - ptr)
140 buf_size = end - buf;
143 frame_bytes = end - ptr;
144 if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
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;
152 /* 2 frames, different sizes */
153 pkt->frame_count = 2;
156 /* read 1st frame size */
157 frame_bytes = xiph_lacing_16bit(&ptr, end);
161 if (self_delimiting) {
162 int len = xiph_lacing_16bit(&ptr, end);
163 if (len < 0 || len + frame_bytes > end - ptr)
165 end = ptr + frame_bytes + len;
166 buf_size = end - buf;
169 pkt->frame_offset[0] = ptr - buf;
170 pkt->frame_size[0] = frame_bytes;
172 /* calculate 2nd frame size */
173 frame_bytes = end - ptr - pkt->frame_size[0];
174 if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
176 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
177 pkt->frame_size[1] = frame_bytes;
180 /* 1 to 48 frames, can be different sizes */
182 pkt->frame_count = (i ) & 0x3F;
183 padding = (i >> 6) & 0x01;
184 pkt->vbr = (i >> 7) & 0x01;
186 if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
189 /* read padding size */
191 padding = xiph_lacing_full(&ptr, end);
196 /* read frame sizes */
198 /* for VBR, all frames except the final one have their size coded
199 in the bitstream. the last frame size is implicit. */
201 for (i = 0; i < pkt->frame_count - 1; i++) {
202 frame_bytes = xiph_lacing_16bit(&ptr, end);
205 pkt->frame_size[i] = frame_bytes;
206 total_bytes += frame_bytes;
209 if (self_delimiting) {
210 int len = xiph_lacing_16bit(&ptr, end);
211 if (len < 0 || len + total_bytes + padding > end - ptr)
213 end = ptr + total_bytes + len + padding;
214 buf_size = end - buf;
217 frame_bytes = end - ptr - padding;
218 if (total_bytes > frame_bytes)
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;
225 /* for CBR, the remaining packet bytes are divided evenly between
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)
231 end = ptr + pkt->frame_count * frame_bytes + padding;
232 buf_size = end - buf;
234 frame_bytes = end - ptr - padding;
235 if (frame_bytes % pkt->frame_count ||
236 frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
238 frame_bytes /= pkt->frame_count;
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;
250 pkt->packet_size = buf_size;
251 pkt->data_size = pkt->packet_size - padding;
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)
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);
266 pkt->mode = OPUS_MODE_CELT;
267 pkt->bandwidth = (pkt->config - 16) >> 2;
268 /* skip medium band */
276 memset(pkt, 0, sizeof(*pkt));
277 return AVERROR_INVALIDDATA;
280 static int channel_reorder_vorbis(int nb_channels, int channel_idx)
282 return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
285 static int channel_reorder_unknown(int nb_channels, int channel_idx)
290 av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
293 static const uint8_t default_channel_map[2] = { 0, 1 };
295 int (*channel_reorder)(int, int) = channel_reorder_unknown;
297 const uint8_t *extradata, *channel_map;
299 int version, channels, map_type, streams, stereo_streams, i, j;
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);
308 extradata = opus_default_extradata;
309 extradata_size = sizeof(opus_default_extradata);
311 extradata = avctx->extradata;
312 extradata_size = avctx->extradata_size;
315 if (extradata_size < 19) {
316 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
318 return AVERROR_INVALIDDATA;
321 version = extradata[8];
323 avpriv_request_sample(avctx, "Extradata version %d", version);
324 return AVERROR_PATCHWELCOME;
327 avctx->delay = AV_RL16(extradata + 10);
329 channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
331 av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\n");
332 return AVERROR_INVALIDDATA;
335 s->gain_i = AV_RL16(extradata + 16);
337 s->gain = ff_exp10(s->gain_i / (20.0 * 256));
339 map_type = extradata[18];
342 av_log(avctx, AV_LOG_ERROR,
343 "Channel mapping 0 is only specified for up to 2 channels\n");
344 return AVERROR_INVALIDDATA;
346 layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
348 stereo_streams = channels - 1;
349 channel_map = default_channel_map;
350 } else if (map_type == 1 || map_type == 255) {
351 if (extradata_size < 21 + channels) {
352 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
354 return AVERROR_INVALIDDATA;
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;
368 av_log(avctx, AV_LOG_ERROR,
369 "Channel mapping 1 is only specified for up to 8 channels\n");
370 return AVERROR_INVALIDDATA;
372 layout = ff_vorbis_channel_layouts[channels - 1];
373 channel_reorder = channel_reorder_vorbis;
377 channel_map = extradata + 21;
379 avpriv_request_sample(avctx, "Mapping type %d", map_type);
380 return AVERROR_PATCHWELCOME;
383 s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));
384 if (!s->channel_maps)
385 return AVERROR(ENOMEM);
387 for (i = 0; i < channels; i++) {
388 ChannelMap *map = &s->channel_maps[i];
389 uint8_t idx = channel_map[channel_reorder(channels, i)];
394 } else if (idx >= streams + stereo_streams) {
395 av_log(avctx, AV_LOG_ERROR,
396 "Invalid channel map for output channel %d: %d\n", i, idx);
397 return AVERROR_INVALIDDATA;
400 /* check that we did not see this index yet */
402 for (j = 0; j < i; j++)
403 if (channel_map[channel_reorder(channels, j)] == idx) {
409 if (idx < 2 * stereo_streams) {
410 map->stream_idx = idx / 2;
411 map->channel_idx = idx & 1;
413 map->stream_idx = idx - stereo_streams;
414 map->channel_idx = 0;
418 avctx->channels = channels;
419 avctx->channel_layout = layout;
420 s->nb_streams = streams;
421 s->nb_stereo_streams = stereo_streams;