1 /* Copyright (C)2012 Xiph.Org Foundation
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "opus_header.h"
37 - "OpusHead" (64 bits)
38 - version number (8 bits)
41 - Sampling rate (32 bits)
42 - Gain in dB (16 bits, S7.8)
43 - Mapping (8 bits, 0=single stream (mono/stereo) 1=Vorbis mapping,
44 2..254: reserved, 255: multistream with no mapping)
47 - N = totel number of streams (8 bits)
48 - M = number of paired streams (8 bits)
49 - C times channel origin
67 const unsigned char *data;
72 static int write_uint32(Packet *p, ogg_uint32_t val)
74 if (p->pos>p->maxlen-4)
76 p->data[p->pos ] = (val ) & 0xFF;
77 p->data[p->pos+1] = (val>> 8) & 0xFF;
78 p->data[p->pos+2] = (val>>16) & 0xFF;
79 p->data[p->pos+3] = (val>>24) & 0xFF;
84 static int write_uint16(Packet *p, ogg_uint16_t val)
86 if (p->pos>p->maxlen-2)
88 p->data[p->pos ] = (val ) & 0xFF;
89 p->data[p->pos+1] = (val>> 8) & 0xFF;
94 static int write_chars(Packet *p, const unsigned char *str, int nb_chars)
96 if (p->pos>p->maxlen-nb_chars)
98 for (int i=0;i<nb_chars;i++)
99 p->data[p->pos++] = str[i];
103 static int read_uint32(ROPacket *p, ogg_uint32_t *val)
105 if (p->pos>p->maxlen-4)
107 *val = (ogg_uint32_t)p->data[p->pos ];
108 *val |= (ogg_uint32_t)p->data[p->pos+1]<< 8;
109 *val |= (ogg_uint32_t)p->data[p->pos+2]<<16;
110 *val |= (ogg_uint32_t)p->data[p->pos+3]<<24;
115 static int read_uint16(ROPacket *p, ogg_uint16_t *val)
117 if (p->pos>p->maxlen-2)
119 *val = (ogg_uint16_t)p->data[p->pos ];
120 *val |= (ogg_uint16_t)p->data[p->pos+1]<<8;
125 static int read_chars(ROPacket *p, unsigned char *str, int nb_chars)
127 if (p->pos>p->maxlen-nb_chars)
129 for (int i=0;i<nb_chars;i++)
130 str[i] = p->data[p->pos++];
134 int opus_header_parse(const unsigned char *packet, int len, OpusHeader *h)
139 ogg_uint16_t shortval;
146 read_chars(&p, (unsigned char*)str, 8);
147 if (memcmp(str, "OpusHead", 8)!=0)
150 if (!read_chars(&p, &ch, 1))
153 if((h->version&240) != 0) /* Only major version 0 supported. */
156 if (!read_chars(&p, &ch, 1))
159 if (h->channels == 0)
162 if (!read_uint16(&p, &shortval))
164 h->preskip = shortval;
166 if (!read_uint32(&p, &h->input_sample_rate))
169 if (!read_uint16(&p, &shortval))
171 h->gain = (short)shortval;
173 if (!read_chars(&p, &ch, 1))
175 h->channel_mapping = ch;
177 if (h->channel_mapping != 0)
179 if (!read_chars(&p, &ch, 1))
186 if (!read_chars(&p, &ch, 1))
189 if (ch>h->nb_streams || (ch+h->nb_streams)>255)
193 /* Multi-stream support */
194 for (int i=0;i<h->channels;i++)
196 if (!read_chars(&p, &h->stream_map[i], 1))
198 if (h->stream_map[i]>(h->nb_streams+h->nb_coupled) && h->stream_map[i]!=255)
205 h->nb_coupled = h->channels>1;
209 /*For version 0/1 we know there won't be any more data
210 so reject any that have data past the end.*/
211 if ((h->version==0 || h->version==1) && p.pos != len)
216 int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len)
225 if (!write_chars(&p, (const unsigned char*)"OpusHead", 8))
229 if (!write_chars(&p, &ch, 1))
233 if (!write_chars(&p, &ch, 1))
236 if (!write_uint16(&p, h->preskip))
239 if (!write_uint32(&p, h->input_sample_rate))
242 if (!write_uint16(&p, h->gain))
245 ch = h->channel_mapping;
246 if (!write_chars(&p, &ch, 1))
249 if (h->channel_mapping != 0)
252 if (!write_chars(&p, &ch, 1))
256 if (!write_chars(&p, &ch, 1))
259 /* Multi-stream support */
260 for (int i=0;i<h->channels;i++)
262 if (!write_chars(&p, &h->stream_map[i], 1))