summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/opus_header.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/opus_header.c')
-rw-r--r--lib/rbcodec/codecs/libopus/opus_header.c117
1 files changed, 20 insertions, 97 deletions
diff --git a/lib/rbcodec/codecs/libopus/opus_header.c b/lib/rbcodec/codecs/libopus/opus_header.c
index d00d4fba23..5fe5a7a4f9 100644
--- a/lib/rbcodec/codecs/libopus/opus_header.c
+++ b/lib/rbcodec/codecs/libopus/opus_header.c
@@ -44,7 +44,7 @@
44 2..254: reserved, 255: multistream with no mapping) 44 2..254: reserved, 255: multistream with no mapping)
45 45
46 - if (mapping != 0) 46 - if (mapping != 0)
47 - N = totel number of streams (8 bits) 47 - N = total number of streams (8 bits)
48 - M = number of paired streams (8 bits) 48 - M = number of paired streams (8 bits)
49 - C times channel origin 49 - C times channel origin
50 - if (C<2*M) 50 - if (C<2*M)
@@ -58,49 +58,11 @@
58*/ 58*/
59 59
60typedef struct { 60typedef struct {
61 unsigned char *data;
62 int maxlen;
63 int pos;
64} Packet;
65
66typedef struct {
67 const unsigned char *data; 61 const unsigned char *data;
68 int maxlen; 62 int maxlen;
69 int pos; 63 int pos;
70} ROPacket; 64} ROPacket;
71 65
72static int write_uint32(Packet *p, ogg_uint32_t val)
73{
74 if (p->pos>p->maxlen-4)
75 return 0;
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;
80 p->pos += 4;
81 return 1;
82}
83
84static int write_uint16(Packet *p, ogg_uint16_t val)
85{
86 if (p->pos>p->maxlen-2)
87 return 0;
88 p->data[p->pos ] = (val ) & 0xFF;
89 p->data[p->pos+1] = (val>> 8) & 0xFF;
90 p->pos += 2;
91 return 1;
92}
93
94static int write_chars(Packet *p, const unsigned char *str, int nb_chars)
95{
96 int i;
97 if (p->pos>p->maxlen-nb_chars)
98 return 0;
99 for (i=0;i<nb_chars;i++)
100 p->data[p->pos++] = str[i];
101 return 1;
102}
103
104static int read_uint32(ROPacket *p, ogg_uint32_t *val) 66static int read_uint32(ROPacket *p, ogg_uint32_t *val)
105{ 67{
106 if (p->pos>p->maxlen-4) 68 if (p->pos>p->maxlen-4)
@@ -194,12 +156,28 @@ int opus_header_parse(const unsigned char *packet, int len, OpusHeader *h)
194 h->nb_coupled = ch; 156 h->nb_coupled = ch;
195 157
196 /* Multi-stream support */ 158 /* Multi-stream support */
197 for (i=0;i<h->channels;i++) 159 if (h->channel_mapping == 3)
198 { 160 {
199 if (!read_chars(&p, &h->stream_map[i], 1)) 161 int dmatrix_size = h->channels * (h->nb_streams + h->nb_coupled) * 2;
162 if (dmatrix_size > len - p.pos)
200 return 0; 163 return 0;
201 if (h->stream_map[i]>(h->nb_streams+h->nb_coupled) && h->stream_map[i]!=255) 164 if (dmatrix_size > OPUS_DEMIXING_MATRIX_SIZE_MAX)
165 p.pos += dmatrix_size;
166 else if (!read_chars(&p, h->dmatrix, dmatrix_size))
202 return 0; 167 return 0;
168 for (i=0;i<h->channels;i++) {
169 h->stream_map[i] = i;
170 }
171 }
172 else
173 {
174 for (i=0;i<h->channels;i++)
175 {
176 if (!read_chars(&p, &h->stream_map[i], 1))
177 return 0;
178 if (h->stream_map[i]>(h->nb_streams+h->nb_coupled) && h->stream_map[i]!=255)
179 return 0;
180 }
203 } 181 }
204 } else { 182 } else {
205 if(h->channels>2) 183 if(h->channels>2)
@@ -216,61 +194,6 @@ int opus_header_parse(const unsigned char *packet, int len, OpusHeader *h)
216 return 1; 194 return 1;
217} 195}
218 196
219int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len)
220{
221 int i;
222 Packet p;
223 unsigned char ch;
224
225 p.data = packet;
226 p.maxlen = len;
227 p.pos = 0;
228 if (len<19)return 0;
229 if (!write_chars(&p, (const unsigned char*)"OpusHead", 8))
230 return 0;
231 /* Version is 1 */
232 ch = 1;
233 if (!write_chars(&p, &ch, 1))
234 return 0;
235
236 ch = h->channels;
237 if (!write_chars(&p, &ch, 1))
238 return 0;
239
240 if (!write_uint16(&p, h->preskip))
241 return 0;
242
243 if (!write_uint32(&p, h->input_sample_rate))
244 return 0;
245
246 if (!write_uint16(&p, h->gain))
247 return 0;
248
249 ch = h->channel_mapping;
250 if (!write_chars(&p, &ch, 1))
251 return 0;
252
253 if (h->channel_mapping != 0)
254 {
255 ch = h->nb_streams;
256 if (!write_chars(&p, &ch, 1))
257 return 0;
258
259 ch = h->nb_coupled;
260 if (!write_chars(&p, &ch, 1))
261 return 0;
262
263 /* Multi-stream support */
264 for (i=0;i<h->channels;i++)
265 {
266 if (!write_chars(&p, &h->stream_map[i], 1))
267 return 0;
268 }
269 }
270
271 return p.pos;
272}
273
274/* This is just here because it's a convenient file linked by both opusenc and 197/* This is just here because it's a convenient file linked by both opusenc and
275 opusdec (to guarantee this maps stays in sync). */ 198 opusdec (to guarantee this maps stays in sync). */
276const int wav_permute_matrix[8][8] = 199const int wav_permute_matrix[8][8] =