1 /* Copyright (C)2012 Xiph.Org Foundation
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
7 * - Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * \file opus_common.c Common functions of the opus decoder and audio format
34 #include "opus_common.h"
35 #include "portable_io.h"
43 static int read_chars(struct packet *p, unsigned char *str, int nb_chars)
47 if (p->pos > p->maxlen - nb_chars)
49 for (i = 0; i < nb_chars; i++)
50 str[i] = p->data[p->pos++];
54 static int read_uint32(struct packet *p, uint32_t *val)
56 if (p->pos > p->maxlen - 4)
58 *val = read_u32(p->data + p->pos);
63 static int read_uint16(struct packet *p, uint16_t *val)
65 if (p->pos > p->maxlen - 2)
67 *val = read_u16(p->data + p->pos);
73 * Get metadata of an opus stream.
75 * This is called from both the audio format handler (which passes ogg packet
76 * 0) and from the decoder.
78 * \param packet Start of the packet.
79 * \param len Number of bytes.
84 int opus_parse_header(const char *packet, int len, struct opus_header *h)
89 unsigned char ch, channel_mapping;
96 return -E_OPUS_HEADER;
97 read_chars(&p, (unsigned char*)str, 8);
98 if (memcmp(str, "OpusHead", 8) != 0)
99 return -E_OPUS_HEADER;
101 if (!read_chars(&p, &ch, 1))
102 return -E_OPUS_HEADER;
104 if ((h->version & 240) != 0) /* Only major version 0 supported. */
105 return -E_OPUS_HEADER;
107 if (!read_chars(&p, &ch, 1))
108 return -E_OPUS_HEADER;
110 if (h->channels == 0)
111 return -E_OPUS_HEADER;
113 if (!read_uint16(&p, &h->preskip))
114 return -E_OPUS_HEADER;
116 if (!read_uint32(&p, &h->input_sample_rate))
117 return -E_OPUS_HEADER;
119 if (!read_uint16(&p, &h->gain))
120 return -E_OPUS_HEADER;
122 if (!read_chars(&p, &ch, 1))
123 return -E_OPUS_HEADER;
124 channel_mapping = ch;
126 if (channel_mapping != 0) {
127 if (!read_chars(&p, &ch, 1))
128 return -E_OPUS_HEADER;
131 return -E_OPUS_HEADER;
134 if (!read_chars(&p, &ch, 1))
135 return -E_OPUS_HEADER;
137 if (ch > h->nb_streams || (ch + h->nb_streams) > 255)
138 return -E_OPUS_HEADER;
141 /* Multi-stream support */
142 for (i = 0; i < h->channels; i++) {
143 if (!read_chars(&p, &h->stream_map[i], 1))
144 return -E_OPUS_HEADER;
145 if (h->stream_map[i] > (h->nb_streams + h->nb_coupled)
146 && h->stream_map[i] != 255)
147 return -E_OPUS_HEADER;
151 return -E_OPUS_HEADER;
153 h->nb_coupled = h->channels > 1;
154 h->stream_map[0] = 0;
155 h->stream_map[1] = 1;
158 * For version 0/1 we know there won't be any more data so reject any
159 * that have data past the end.
161 if ((h->version == 0 || h->version == 1) && p.pos != len)
162 return -E_OPUS_HEADER;