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 };
293 uint8_t default_extradata
[19] = {
294 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
295 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
298 int (*channel_reorder
)(int, int) = channel_reorder_unknown
;
300 const uint8_t *extradata
, *channel_map
;
302 int version
, channels
, map_type
, streams
, stereo_streams
, i
, j
;
305 if (!avctx
->extradata
) {
306 if (avctx
->channels
> 2) {
307 av_log(avctx
, AV_LOG_ERROR
,
308 "Multichannel configuration without extradata.\n");
309 return AVERROR(EINVAL
);
311 default_extradata
[9] = (avctx
->channels
== 1) ? 1 : 2;
312 extradata
= default_extradata
;
313 extradata_size
= sizeof(default_extradata
);
315 extradata
= avctx
->extradata
;
316 extradata_size
= avctx
->extradata_size
;
319 if (extradata_size
< 19) {
320 av_log(avctx
, AV_LOG_ERROR
, "Invalid extradata size: %d\n",
322 return AVERROR_INVALIDDATA
;
325 version
= extradata
[8];
327 avpriv_request_sample(avctx
, "Extradata version %d", version
);
328 return AVERROR_PATCHWELCOME
;
331 avctx
->delay
= AV_RL16(extradata
+ 10);
333 channels
= extradata
[9];
335 av_log(avctx
, AV_LOG_ERROR
, "Zero channel count specified in the extadata\n");
336 return AVERROR_INVALIDDATA
;
339 s
->gain_i
= AV_RL16(extradata
+ 16);
341 s
->gain
= pow(10, s
->gain_i
/ (20.0 * 256));
343 map_type
= extradata
[18];
346 av_log(avctx
, AV_LOG_ERROR
,
347 "Channel mapping 0 is only specified for up to 2 channels\n");
348 return AVERROR_INVALIDDATA
;
350 layout
= (channels
== 1) ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO
;
352 stereo_streams
= channels
- 1;
353 channel_map
= default_channel_map
;
354 } else if (map_type
== 1 || map_type
== 255) {
355 if (extradata_size
< 21 + channels
) {
356 av_log(avctx
, AV_LOG_ERROR
, "Invalid extradata size: %d\n",
358 return AVERROR_INVALIDDATA
;
361 streams
= extradata
[19];
362 stereo_streams
= extradata
[20];
363 if (!streams
|| stereo_streams
> streams
||
364 streams
+ stereo_streams
> 255) {
365 av_log(avctx
, AV_LOG_ERROR
,
366 "Invalid stream/stereo stream count: %d/%d\n", streams
, stereo_streams
);
367 return AVERROR_INVALIDDATA
;
372 av_log(avctx
, AV_LOG_ERROR
,
373 "Channel mapping 1 is only specified for up to 8 channels\n");
374 return AVERROR_INVALIDDATA
;
376 layout
= ff_vorbis_channel_layouts
[channels
- 1];
377 channel_reorder
= channel_reorder_vorbis
;
381 channel_map
= extradata
+ 21;
383 avpriv_request_sample(avctx
, "Mapping type %d", map_type
);
384 return AVERROR_PATCHWELCOME
;
387 s
->channel_maps
= av_mallocz_array(channels
, sizeof(*s
->channel_maps
));
388 if (!s
->channel_maps
)
389 return AVERROR(ENOMEM
);
391 for (i
= 0; i
< channels
; i
++) {
392 ChannelMap
*map
= &s
->channel_maps
[i
];
393 uint8_t idx
= channel_map
[channel_reorder(channels
, i
)];
398 } else if (idx
>= streams
+ stereo_streams
) {
399 av_log(avctx
, AV_LOG_ERROR
,
400 "Invalid channel map for output channel %d: %d\n", i
, idx
);
401 return AVERROR_INVALIDDATA
;
404 /* check that we din't see this index yet */
406 for (j
= 0; j
< i
; j
++)
407 if (channel_map
[channel_reorder(channels
, j
)] == idx
) {
413 if (idx
< 2 * stereo_streams
) {
414 map
->stream_idx
= idx
/ 2;
415 map
->channel_idx
= idx
& 1;
417 map
->stream_idx
= idx
- stereo_streams
;
418 map
->channel_idx
= 0;
422 avctx
->channels
= channels
;
423 avctx
->channel_layout
= layout
;
424 s
->nb_streams
= streams
;
425 s
->nb_stereo_streams
= stereo_streams
;