FFmpeg
Loading...
Searching...
No Matches
parse.c
Go to the documentation of this file.
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
29#include "libavutil/error.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"
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 */
46static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
47{
48 int val;
49
50 if (*ptr >= end)
52 val = *(*ptr)++;
53 if (val >= 252) {
54 if (*ptr >= end)
56 val += 4 * *(*ptr)++;
57 }
58 return val;
59}
60
61/**
62 * Read a multi-byte length (used for code 3 packet padding size)
63 */
64static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
65{
66 int val = 0;
67 int next;
68
69 while (1) {
70 if (*ptr >= end || val > INT_MAX - 254)
72 next = *(*ptr)++;
73 val += next;
74 if (next < 255)
75 break;
76 else
77 val--;
78 }
79 return val;
80}
81
82/**
83 * Parse Opus packet info from raw packet data
84 */
85int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
86 int self_delimiting)
87{
88 const uint8_t *ptr = buf;
89 const uint8_t *end = buf + buf_size;
90 int padding = 0;
91 int frame_bytes, i;
92
93 if (buf_size < 1)
94 goto fail;
95
96 /* TOC byte */
97 i = *ptr++;
98 pkt->code = (i ) & 0x3;
99 pkt->stereo = (i >> 2) & 0x1;
100 pkt->config = (i >> 3) & 0x1F;
101
102 /* code 2 and code 3 packets have at least 1 byte after the TOC */
103 if (pkt->code >= 2 && buf_size < 2)
104 goto fail;
105
106 switch (pkt->code) {
107 case 0:
108 /* 1 frame */
109 pkt->frame_count = 1;
110 pkt->vbr = 0;
111
112 if (self_delimiting) {
113 int len = xiph_lacing_16bit(&ptr, end);
114 if (len < 0 || len > end - ptr)
115 goto fail;
116 end = ptr + len;
117 buf_size = end - buf;
118 }
119
120 frame_bytes = end - ptr;
121 if (frame_bytes > OPUS_MAX_FRAME_SIZE)
122 goto fail;
123 pkt->frame_offset[0] = ptr - buf;
124 pkt->frame_size[0] = frame_bytes;
125 break;
126 case 1:
127 /* 2 frames, equal size */
128 pkt->frame_count = 2;
129 pkt->vbr = 0;
130
131 if (self_delimiting) {
132 int len = xiph_lacing_16bit(&ptr, end);
133 if (len < 0 || 2 * len > end - ptr)
134 goto fail;
135 end = ptr + 2 * len;
136 buf_size = end - buf;
137 }
138
139 frame_bytes = end - ptr;
140 if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE)
141 goto fail;
142 pkt->frame_offset[0] = ptr - buf;
143 pkt->frame_size[0] = frame_bytes >> 1;
144 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
145 pkt->frame_size[1] = frame_bytes >> 1;
146 break;
147 case 2:
148 /* 2 frames, different sizes */
149 pkt->frame_count = 2;
150 pkt->vbr = 1;
151
152 /* read 1st frame size */
153 frame_bytes = xiph_lacing_16bit(&ptr, end);
154 if (frame_bytes < 0)
155 goto fail;
156
157 if (self_delimiting) {
158 int len = xiph_lacing_16bit(&ptr, end);
159 if (len < 0 || len + frame_bytes > end - ptr)
160 goto fail;
161 end = ptr + frame_bytes + len;
162 buf_size = end - buf;
163 }
164
165 pkt->frame_offset[0] = ptr - buf;
166 pkt->frame_size[0] = frame_bytes;
167
168 /* calculate 2nd frame size */
169 frame_bytes = end - ptr - pkt->frame_size[0];
170 if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE)
171 goto fail;
172 pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
173 pkt->frame_size[1] = frame_bytes;
174 break;
175 case 3:
176 /* 1 to 48 frames, can be different sizes */
177 i = *ptr++;
178 pkt->frame_count = (i ) & 0x3F;
179 padding = (i >> 6) & 0x01;
180 pkt->vbr = (i >> 7) & 0x01;
181
182 if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES)
183 goto fail;
184
185 /* read padding size */
186 if (padding) {
187 padding = xiph_lacing_full(&ptr, end);
188 if (padding < 0)
189 goto fail;
190 }
191
192 /* read frame sizes */
193 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 int total_bytes = 0;
197 for (i = 0; i < pkt->frame_count - 1; i++) {
198 frame_bytes = xiph_lacing_16bit(&ptr, end);
199 if (frame_bytes < 0)
200 goto fail;
201 pkt->frame_size[i] = frame_bytes;
202 total_bytes += frame_bytes;
203 }
204
205 if (self_delimiting) {
206 int len = xiph_lacing_16bit(&ptr, end);
208 goto fail;
209 end = ptr + total_bytes + len + padding;
210 buf_size = end - buf;
211 }
212
213 frame_bytes = end - ptr - padding;
214 if (total_bytes > frame_bytes)
215 goto fail;
216 pkt->frame_offset[0] = ptr - buf;
217 for (i = 1; i < pkt->frame_count; i++)
218 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
219 pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
220 } else {
221 /* for CBR, the remaining packet bytes are divided evenly between
222 the frames */
223 if (self_delimiting) {
224 frame_bytes = xiph_lacing_16bit(&ptr, end);
225 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
226 goto fail;
227 end = ptr + pkt->frame_count * frame_bytes + padding;
228 buf_size = end - buf;
229 } else {
230 frame_bytes = end - ptr - padding;
231 if (frame_bytes % pkt->frame_count ||
232 frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE)
233 goto fail;
234 frame_bytes /= pkt->frame_count;
235 }
236
237 pkt->frame_offset[0] = ptr - buf;
238 pkt->frame_size[0] = frame_bytes;
239 for (i = 1; i < pkt->frame_count; i++) {
240 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
241 pkt->frame_size[i] = frame_bytes;
242 }
243 }
244 }
245
246 pkt->packet_size = buf_size;
247 pkt->data_size = pkt->packet_size - padding;
248
249 /* total packet duration cannot be larger than 120ms */
250 pkt->frame_duration = ff_opus_frame_duration[pkt->config];
251 if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR)
252 goto fail;
253
254 /* set mode and bandwidth */
255 if (pkt->config < 12) {
256 pkt->mode = OPUS_MODE_SILK;
257 pkt->bandwidth = pkt->config >> 2;
258 } else if (pkt->config < 16) {
259 pkt->mode = OPUS_MODE_HYBRID;
260 pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
261 } else {
262 pkt->mode = OPUS_MODE_CELT;
263 pkt->bandwidth = (pkt->config - 16) >> 2;
264 /* skip medium band */
265 if (pkt->bandwidth)
266 pkt->bandwidth++;
267 }
268
269 return 0;
270
271fail:
272 memset(pkt, 0, sizeof(*pkt));
273 return AVERROR_INVALIDDATA;
274}
275
276static int channel_reorder_vorbis(int nb_channels, int channel_idx)
277{
278 return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
279}
280
281static int channel_reorder_unknown(int nb_channels, int channel_idx)
282{
283 return channel_idx;
284}
285
288{
289 static const uint8_t default_channel_map[2] = { 0, 1 };
290
291 int (*channel_reorder)(int, int) = channel_reorder_unknown;
292 int channels = avctx->ch_layout.nb_channels;
293
294 const uint8_t *extradata, *channel_map;
295 int extradata_size;
296 int version, map_type, streams, stereo_streams, i, j, ret;
297 AVChannelLayout layout = { 0 };
298
299 if (!avctx->extradata) {
300 if (channels > 2) {
301 av_log(avctx, AV_LOG_ERROR,
302 "Multichannel configuration without extradata.\n");
303 return AVERROR(EINVAL);
304 }
305 extradata = opus_default_extradata;
306 extradata_size = sizeof(opus_default_extradata);
307 } else {
308 extradata = avctx->extradata;
309 extradata_size = avctx->extradata_size;
310 }
311
312 if (extradata_size < 19) {
313 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
314 extradata_size);
315 return AVERROR_INVALIDDATA;
316 }
317
318 version = extradata[8];
319 if (version > 15) {
320 avpriv_request_sample(avctx, "Extradata version %d", version);
322 }
323
324 avctx->delay = AV_RL16(extradata + 10);
325
326 channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2;
327 if (!channels) {
328 av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
329 return AVERROR_INVALIDDATA;
330 }
331
332 s->gain_i = AV_RL16(extradata + 16);
333
334 map_type = extradata[18];
335 if (!map_type) {
336 if (channels > 2) {
337 av_log(avctx, AV_LOG_ERROR,
338 "Channel mapping 0 is only specified for up to 2 channels\n");
340 goto fail;
341 }
344 streams = 1;
345 stereo_streams = channels - 1;
346 channel_map = default_channel_map;
347 } else if (map_type == 1 || map_type == 2 || map_type == 255) {
348 if (extradata_size < 21 + channels) {
349 av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
350 extradata_size);
352 goto fail;
353 }
354
355 streams = extradata[19];
356 stereo_streams = extradata[20];
357 if (!streams || stereo_streams > streams ||
358 streams + stereo_streams > 255) {
359 av_log(avctx, AV_LOG_ERROR,
360 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
362 goto fail;
363 }
364
365 if (map_type == 1) {
366 if (channels > 8) {
367 av_log(avctx, AV_LOG_ERROR,
368 "Channel mapping 1 is only specified for up to 8 channels\n");
370 goto fail;
371 }
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");
383 goto fail;
384 }
385 if (channels > 227) {
386 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
388 goto fail;
389 }
390
392 layout.nb_channels = channels;
393 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)))
395 } else {
397 layout.nb_channels = channels;
398 }
399
400 channel_map = extradata + 21;
401 } else {
402 avpriv_request_sample(avctx, "Mapping type %d", map_type);
404 }
405
406 s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps));
407 if (!s->channel_maps) {
408 ret = AVERROR(ENOMEM);
409 goto fail;
410 }
411
412 for (i = 0; i < channels; i++) {
413 ChannelMap *map = &s->channel_maps[i];
414 uint8_t idx = channel_map[channel_reorder(channels, i)];
415
416 if (idx == 255) {
417 map->silence = 1;
418 continue;
419 } else if (idx >= streams + stereo_streams) {
420 av_log(avctx, AV_LOG_ERROR,
421 "Invalid channel map for output channel %d: %d\n", i, idx);
422 av_freep(&s->channel_maps);
424 goto fail;
425 }
426
427 /* check that we did not see this index yet */
428 map->copy = 0;
429 for (j = 0; j < i; j++)
430 if (channel_map[channel_reorder(channels, j)] == idx) {
431 map->copy = 1;
432 map->copy_idx = j;
433 break;
434 }
435
436 if (idx < 2 * stereo_streams) {
437 map->stream_idx = idx / 2;
438 map->channel_idx = idx & 1;
439 } else {
440 map->stream_idx = idx - stereo_streams;
441 map->channel_idx = 0;
442 }
443 }
444
445 ret = av_channel_layout_copy(&avctx->ch_layout, &layout);
446 if (ret < 0)
447 goto fail;
448
449 s->nb_streams = streams;
450 s->nb_stereo_streams = stereo_streams;
451
452 return 0;
453fail:
455 return ret;
456}
static double val(void *priv, double ch)
Definition aeval.c:77
channels
Definition aptx.h:31
static const uint8_t channel_map[8][8]
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
static AVPacket * pkt
error code definitions
const uint16_t ff_opus_frame_duration[32]
#define fail
Definition test.h:479
#define AV_CH_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
@ AV_CHANNEL_ORDER_AMBISONIC
The audio is represented as the decomposition of the sound field into spherical harmonics.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const VDPAUPixFmtMap * map
#define AV_RL16(p)
common internal api header.
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
version
Definition libkvazaar.c:313
#define ff_sqrt
Definition mathops.h:220
uint64_t layout
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
Definition parse.c:85
static int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
Read a multi-byte length (used for code 3 packet padding size)
Definition parse.c:64
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s)
Definition parse.c:286
static int channel_reorder_unknown(int nb_channels, int channel_idx)
Definition parse.c:281
static int channel_reorder_vorbis(int nb_channels, int channel_idx)
Definition parse.c:276
static int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
Read a 1- or 2-byte frame length.
Definition parse.c:46
@ OPUS_BANDWIDTH_SUPERWIDEBAND
Definition opus.h:53
static const uint8_t opus_default_extradata[30]
Definition opus.h:35
@ OPUS_MODE_SILK
Definition opus.h:42
@ OPUS_MODE_HYBRID
Definition opus.h:43
@ OPUS_MODE_CELT
Definition opus.h:44
#define OPUS_MAX_PACKET_DUR
Definition opus.h:30
#define OPUS_MAX_FRAME_SIZE
Definition opus.h:28
#define OPUS_MAX_FRAMES
Definition opus.h:29
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int delay
Codec delay.
Definition avcodec.h:587
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
const AVChannelLayout ff_vorbis_ch_layouts[9]
Definition vorbis_data.c:37
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition vorbis_data.c:26
int len