blob: a126b16fccb361c15f5ee5f4f8c350888b27bc6e [file] [log] [blame]
Alexandre Lision744f7422013-09-25 11:39:37 -04001/* Copyright (c) 2011 Xiph.Org Foundation
2 Written by Jean-Marc Valin */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
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.
14
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 COPYRIGHT OWNER
19 OR 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.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "opus_multistream.h"
33#include "opus.h"
34#include "opus_private.h"
35#include "stack_alloc.h"
36#include <stdarg.h>
37#include "float_cast.h"
38#include "os_support.h"
39
40typedef struct ChannelLayout {
41 int nb_channels;
42 int nb_streams;
43 int nb_coupled_streams;
44 unsigned char mapping[256];
45} ChannelLayout;
46
47typedef struct {
48 int nb_streams;
49 int nb_coupled_streams;
50 unsigned char mapping[8];
51} VorbisLayout;
52
53/* Index is nb_channel-1*/
54static const VorbisLayout vorbis_mappings[8] = {
55 {1, 0, {0}}, /* 1: mono */
56 {1, 1, {0, 1}}, /* 2: stereo */
57 {2, 1, {0, 2, 1}}, /* 3: 1-d surround */
58 {2, 2, {0, 1, 2, 3}}, /* 4: quadraphonic surround */
59 {3, 2, {0, 4, 1, 2, 3}}, /* 5: 5-channel surround */
60 {4, 2, {0, 4, 1, 2, 3, 5}}, /* 6: 5.1 surround */
61 {4, 3, {0, 4, 1, 2, 3, 5, 6}}, /* 7: 6.1 surround */
62 {5, 3, {0, 6, 1, 2, 3, 4, 5, 7}}, /* 8: 7.1 surround */
63};
64
65struct OpusMSEncoder {
66 ChannelLayout layout;
67 opus_int32 bitrate_bps;
68 int surround;
69 int lfe_stream;
70 /* Encoder states go here */
71};
72
73struct OpusMSDecoder {
74 ChannelLayout layout;
75 /* Decoder states go here */
76};
77
78#ifdef FIXED_POINT
79#define opus_encode_native opus_encode
80#else
81#define opus_encode_native opus_encode_float
82#endif
83
84static int validate_layout(const ChannelLayout *layout)
85{
86 int i, max_channel;
87
88 max_channel = layout->nb_streams+layout->nb_coupled_streams;
89 if (max_channel>255)
90 return 0;
91 for (i=0;i<layout->nb_channels;i++)
92 {
93 if (layout->mapping[i] >= max_channel && layout->mapping[i] != 255)
94 return 0;
95 }
96 return 1;
97}
98
99
100static int get_left_channel(const ChannelLayout *layout, int stream_id, int prev)
101{
102 int i;
103 i = (prev<0) ? 0 : prev+1;
104 for (;i<layout->nb_channels;i++)
105 {
106 if (layout->mapping[i]==stream_id*2)
107 return i;
108 }
109 return -1;
110}
111
112static int get_right_channel(const ChannelLayout *layout, int stream_id, int prev)
113{
114 int i;
115 i = (prev<0) ? 0 : prev+1;
116 for (;i<layout->nb_channels;i++)
117 {
118 if (layout->mapping[i]==stream_id*2+1)
119 return i;
120 }
121 return -1;
122}
123
124static int get_mono_channel(const ChannelLayout *layout, int stream_id, int prev)
125{
126 int i;
127 i = (prev<0) ? 0 : prev+1;
128 for (;i<layout->nb_channels;i++)
129 {
130 if (layout->mapping[i]==stream_id+layout->nb_coupled_streams)
131 return i;
132 }
133 return -1;
134}
135
136static int validate_encoder_layout(const ChannelLayout *layout)
137{
138 int s;
139 for (s=0;s<layout->nb_streams;s++)
140 {
141 if (s < layout->nb_coupled_streams)
142 {
143 if (get_left_channel(layout, s, -1)==-1)
144 return 0;
145 if (get_right_channel(layout, s, -1)==-1)
146 return 0;
147 } else {
148 if (get_mono_channel(layout, s, -1)==-1)
149 return 0;
150 }
151 }
152 return 1;
153}
154
155opus_int32 opus_multistream_encoder_get_size(int nb_streams, int nb_coupled_streams)
156{
157 int coupled_size;
158 int mono_size;
159
160 if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0;
161 coupled_size = opus_encoder_get_size(2);
162 mono_size = opus_encoder_get_size(1);
163 return align(sizeof(OpusMSEncoder))
164 + nb_coupled_streams * align(coupled_size)
165 + (nb_streams-nb_coupled_streams) * align(mono_size);
166}
167
168opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_family)
169{
170 int nb_streams;
171 int nb_coupled_streams;
172 opus_int32 size;
173
174 if (mapping_family==0)
175 {
176 if (channels==1)
177 {
178 nb_streams=1;
179 nb_coupled_streams=0;
180 } else if (channels==2)
181 {
182 nb_streams=1;
183 nb_coupled_streams=1;
184 } else
185 return 0;
186 } else if (mapping_family==1 && channels<=8 && channels>=1)
187 {
188 nb_streams=vorbis_mappings[channels-1].nb_streams;
189 nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams;
190 } else if (mapping_family==255)
191 {
192 nb_streams=channels;
193 nb_coupled_streams=0;
194 } else
195 return 0;
196 size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams);
197 return size;
198}
199
200
201static int opus_multistream_encoder_init_impl(
202 OpusMSEncoder *st,
203 opus_int32 Fs,
204 int channels,
205 int streams,
206 int coupled_streams,
207 const unsigned char *mapping,
208 int application,
209 int surround
210)
211{
212 int coupled_size;
213 int mono_size;
214 int i, ret;
215 char *ptr;
216
217 if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
218 (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0))
219 return OPUS_BAD_ARG;
220
221 st->layout.nb_channels = channels;
222 st->layout.nb_streams = streams;
223 st->layout.nb_coupled_streams = coupled_streams;
224 if (!surround)
225 st->lfe_stream = -1;
226 st->bitrate_bps = OPUS_AUTO;
227 for (i=0;i<st->layout.nb_channels;i++)
228 st->layout.mapping[i] = mapping[i];
229 if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout))
230 return OPUS_BAD_ARG;
231 ptr = (char*)st + align(sizeof(OpusMSEncoder));
232 coupled_size = opus_encoder_get_size(2);
233 mono_size = opus_encoder_get_size(1);
234
235 for (i=0;i<st->layout.nb_coupled_streams;i++)
236 {
237 ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application);
238 if(ret!=OPUS_OK)return ret;
239 ptr += align(coupled_size);
240 }
241 for (;i<st->layout.nb_streams;i++)
242 {
243 ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 1, application);
244 if(ret!=OPUS_OK)return ret;
245 ptr += align(mono_size);
246 }
247 st->surround = surround;
248 return OPUS_OK;
249}
250
251int opus_multistream_encoder_init(
252 OpusMSEncoder *st,
253 opus_int32 Fs,
254 int channels,
255 int streams,
256 int coupled_streams,
257 const unsigned char *mapping,
258 int application
259)
260{
261 return opus_multistream_encoder_init_impl(st, Fs, channels, streams, coupled_streams, mapping, application, 0);
262}
263
264int opus_multistream_surround_encoder_init(
265 OpusMSEncoder *st,
266 opus_int32 Fs,
267 int channels,
268 int mapping_family,
269 int *streams,
270 int *coupled_streams,
271 unsigned char *mapping,
272 int application
273)
274{
275 if ((channels>255) || (channels<1))
276 return OPUS_BAD_ARG;
277 st->lfe_stream = -1;
278 if (mapping_family==0)
279 {
280 if (channels==1)
281 {
282 *streams=1;
283 *coupled_streams=0;
284 mapping[0]=0;
285 } else if (channels==2)
286 {
287 *streams=1;
288 *coupled_streams=1;
289 mapping[0]=0;
290 mapping[1]=1;
291 } else
292 return OPUS_UNIMPLEMENTED;
293 } else if (mapping_family==1 && channels<=8 && channels>=1)
294 {
295 int i;
296 *streams=vorbis_mappings[channels-1].nb_streams;
297 *coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams;
298 for (i=0;i<channels;i++)
299 mapping[i] = vorbis_mappings[channels-1].mapping[i];
300 if (channels>=6)
301 st->lfe_stream = *streams-1;
302 } else if (mapping_family==255)
303 {
304 int i;
305 *streams=channels;
306 *coupled_streams=0;
307 for(i=0;i<channels;i++)
308 mapping[i] = i;
309 } else
310 return OPUS_UNIMPLEMENTED;
311 return opus_multistream_encoder_init_impl(st, Fs, channels, *streams, *coupled_streams,
312 mapping, application, channels>2&&mapping_family==1);
313}
314
315OpusMSEncoder *opus_multistream_encoder_create(
316 opus_int32 Fs,
317 int channels,
318 int streams,
319 int coupled_streams,
320 const unsigned char *mapping,
321 int application,
322 int *error
323)
324{
325 int ret;
326 OpusMSEncoder *st;
327 if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
328 (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0))
329 {
330 if (error)
331 *error = OPUS_BAD_ARG;
332 return NULL;
333 }
334 st = (OpusMSEncoder *)opus_alloc(opus_multistream_encoder_get_size(streams, coupled_streams));
335 if (st==NULL)
336 {
337 if (error)
338 *error = OPUS_ALLOC_FAIL;
339 return NULL;
340 }
341 ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application);
342 if (ret != OPUS_OK)
343 {
344 opus_free(st);
345 st = NULL;
346 }
347 if (error)
348 *error = ret;
349 return st;
350}
351
352OpusMSEncoder *opus_multistream_surround_encoder_create(
353 opus_int32 Fs,
354 int channels,
355 int mapping_family,
356 int *streams,
357 int *coupled_streams,
358 unsigned char *mapping,
359 int application,
360 int *error
361)
362{
363 int ret;
364 OpusMSEncoder *st;
365 if ((channels>255) || (channels<1))
366 {
367 if (error)
368 *error = OPUS_BAD_ARG;
369 return NULL;
370 }
371 st = (OpusMSEncoder *)opus_alloc(opus_multistream_surround_encoder_get_size(channels, mapping_family));
372 if (st==NULL)
373 {
374 if (error)
375 *error = OPUS_ALLOC_FAIL;
376 return NULL;
377 }
378 ret = opus_multistream_surround_encoder_init(st, Fs, channels, mapping_family, streams, coupled_streams, mapping, application);
379 if (ret != OPUS_OK)
380 {
381 opus_free(st);
382 st = NULL;
383 }
384 if (error)
385 *error = ret;
386 return st;
387}
388
389typedef void (*opus_copy_channel_in_func)(
390 opus_val16 *dst,
391 int dst_stride,
392 const void *src,
393 int src_stride,
394 int src_channel,
395 int frame_size
396);
397
398static void surround_rate_allocation(
399 OpusMSEncoder *st,
400 opus_int32 *rate,
401 int frame_size
402 )
403{
404 int i;
405 opus_int32 channel_rate;
406 opus_int32 Fs;
407 char *ptr;
408 int stream_offset;
409 int lfe_offset;
410 int coupled_ratio; /* Q8 */
411 int lfe_ratio; /* Q8 */
412
413 ptr = (char*)st + align(sizeof(OpusMSEncoder));
414 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs));
415
416 /* We start by giving each stream (coupled or uncoupled) the same bitrate.
417 This models the main saving of coupled channels over uncoupled. */
418 stream_offset = 20000;
419 /* The LFE stream is an exception to the above and gets fewer bits. */
420 lfe_offset = 7000;
421 /* Coupled streams get twice the mono rate after the first 20 kb/s. */
422 coupled_ratio = 512;
423 /* Should depend on the bitrate, for now we assume LFE gets 3/8 the bits of mono */
424 lfe_ratio = 96;
425
426 /* Compute bitrate allocation between streams */
427 if (st->bitrate_bps==OPUS_AUTO)
428 {
429 channel_rate = Fs+60*Fs/frame_size;
430 } else if (st->bitrate_bps==OPUS_BITRATE_MAX)
431 {
432 channel_rate = 300000;
433 } else {
434 int nb_lfe;
435 int nb_uncoupled;
436 int nb_coupled;
437 int total;
438 nb_lfe = (st->lfe_stream!=-1);
439 nb_coupled = st->layout.nb_coupled_streams;
440 nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe;
441 total = (nb_uncoupled<<8) /* mono */
442 + coupled_ratio*nb_coupled /* stereo */
443 + nb_lfe*lfe_ratio;
444 channel_rate = 256*(st->bitrate_bps-lfe_offset*nb_lfe-stream_offset*(nb_coupled+nb_uncoupled))/total;
445 }
446
447 for (i=0;i<st->layout.nb_streams;i++)
448 {
449 if (i<st->layout.nb_coupled_streams)
450 rate[i] = stream_offset+(channel_rate*coupled_ratio>>8);
451 else if (i!=st->lfe_stream)
452 rate[i] = stream_offset+channel_rate;
453 else
454 rate[i] = lfe_offset+(channel_rate*lfe_ratio>>8);
455 }
456}
457
458/* Max size in case the encoder decides to return three frames */
459#define MS_FRAME_TMP (3*1275+7)
460static int opus_multistream_encode_native
461(
462 OpusMSEncoder *st,
463 opus_copy_channel_in_func copy_channel_in,
464 const void *pcm,
465 int frame_size,
466 unsigned char *data,
467 opus_int32 max_data_bytes
468)
469{
470 opus_int32 Fs;
471 int coupled_size;
472 int mono_size;
473 int s;
474 char *ptr;
475 int tot_size;
476 VARDECL(opus_val16, buf);
477 unsigned char tmp_data[MS_FRAME_TMP];
478 OpusRepacketizer rp;
479 opus_int32 bitrates[256];
480 ALLOC_STACK;
481
482 ptr = (char*)st + align(sizeof(OpusMSEncoder));
483 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs));
484
485 if (400*frame_size < Fs)
486 {
487 RESTORE_STACK;
488 return OPUS_BAD_ARG;
489 }
490 /* Validate frame_size before using it to allocate stack space.
491 This mirrors the checks in opus_encode[_float](). */
492 if (400*frame_size != Fs && 200*frame_size != Fs &&
493 100*frame_size != Fs && 50*frame_size != Fs &&
494 25*frame_size != Fs && 50*frame_size != 3*Fs)
495 {
496 RESTORE_STACK;
497 return OPUS_BAD_ARG;
498 }
499 ALLOC(buf, 2*frame_size, opus_val16);
500 coupled_size = opus_encoder_get_size(2);
501 mono_size = opus_encoder_get_size(1);
502
503 if (max_data_bytes < 4*st->layout.nb_streams-1)
504 {
505 RESTORE_STACK;
506 return OPUS_BUFFER_TOO_SMALL;
507 }
508 /* Compute bitrate allocation between streams (this could be a lot better) */
509 surround_rate_allocation(st, bitrates, frame_size);
510
511 ptr = (char*)st + align(sizeof(OpusMSEncoder));
512 for (s=0;s<st->layout.nb_streams;s++)
513 {
514 OpusEncoder *enc;
515 enc = (OpusEncoder*)ptr;
516 if (s < st->layout.nb_coupled_streams)
517 ptr += align(coupled_size);
518 else
519 ptr += align(mono_size);
520 opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrates[s]));
521 if (st->surround)
522 {
523 opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY));
524 opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND));
525 if (s < st->layout.nb_coupled_streams)
526 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(2));
527 }
528 }
529
530 ptr = (char*)st + align(sizeof(OpusMSEncoder));
531 /* Counting ToC */
532 tot_size = 0;
533 for (s=0;s<st->layout.nb_streams;s++)
534 {
535 OpusEncoder *enc;
536 int len;
537 int curr_max;
538
539 opus_repacketizer_init(&rp);
540 enc = (OpusEncoder*)ptr;
541 if (s < st->layout.nb_coupled_streams)
542 {
543 int left, right;
544 left = get_left_channel(&st->layout, s, -1);
545 right = get_right_channel(&st->layout, s, -1);
546 (*copy_channel_in)(buf, 2,
547 pcm, st->layout.nb_channels, left, frame_size);
548 (*copy_channel_in)(buf+1, 2,
549 pcm, st->layout.nb_channels, right, frame_size);
550 ptr += align(coupled_size);
551 } else {
552 int chan = get_mono_channel(&st->layout, s, -1);
553 (*copy_channel_in)(buf, 1,
554 pcm, st->layout.nb_channels, chan, frame_size);
555 ptr += align(mono_size);
556 }
557 /* number of bytes left (+Toc) */
558 curr_max = max_data_bytes - tot_size;
559 /* Reserve three bytes for the last stream and four for the others */
560 curr_max -= IMAX(0,4*(st->layout.nb_streams-s-1)-1);
561 curr_max = IMIN(curr_max,MS_FRAME_TMP);
562 len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max);
563 if (len<0)
564 {
565 RESTORE_STACK;
566 return len;
567 }
568 /* We need to use the repacketizer to add the self-delimiting lengths
569 while taking into account the fact that the encoder can now return
570 more than one frame at a time (e.g. 60 ms CELT-only) */
571 opus_repacketizer_cat(&rp, tmp_data, len);
572 len = opus_repacketizer_out_range_impl(&rp, 0, opus_repacketizer_get_nb_frames(&rp), data, max_data_bytes-tot_size, s != st->layout.nb_streams-1);
573 data += len;
574 tot_size += len;
575 }
576 RESTORE_STACK;
577 return tot_size;
578
579}
580
581#if !defined(DISABLE_FLOAT_API)
582static void opus_copy_channel_in_float(
583 opus_val16 *dst,
584 int dst_stride,
585 const void *src,
586 int src_stride,
587 int src_channel,
588 int frame_size
589)
590{
591 const float *float_src;
592 int i;
593 float_src = (const float *)src;
594 for (i=0;i<frame_size;i++)
595#if defined(FIXED_POINT)
596 dst[i*dst_stride] = FLOAT2INT16(float_src[i*src_stride+src_channel]);
597#else
598 dst[i*dst_stride] = float_src[i*src_stride+src_channel];
599#endif
600}
601#endif
602
603static void opus_copy_channel_in_short(
604 opus_val16 *dst,
605 int dst_stride,
606 const void *src,
607 int src_stride,
608 int src_channel,
609 int frame_size
610)
611{
612 const opus_int16 *short_src;
613 int i;
614 short_src = (const opus_int16 *)src;
615 for (i=0;i<frame_size;i++)
616#if defined(FIXED_POINT)
617 dst[i*dst_stride] = short_src[i*src_stride+src_channel];
618#else
619 dst[i*dst_stride] = (1/32768.f)*short_src[i*src_stride+src_channel];
620#endif
621}
622
623#ifdef FIXED_POINT
624int opus_multistream_encode(
625 OpusMSEncoder *st,
626 const opus_val16 *pcm,
627 int frame_size,
628 unsigned char *data,
629 opus_int32 max_data_bytes
630)
631{
632 return opus_multistream_encode_native(st, opus_copy_channel_in_short,
633 pcm, frame_size, data, max_data_bytes);
634}
635
636#ifndef DISABLE_FLOAT_API
637int opus_multistream_encode_float(
638 OpusMSEncoder *st,
639 const float *pcm,
640 int frame_size,
641 unsigned char *data,
642 opus_int32 max_data_bytes
643)
644{
645 return opus_multistream_encode_native(st, opus_copy_channel_in_float,
646 pcm, frame_size, data, max_data_bytes);
647}
648#endif
649
650#else
651
652int opus_multistream_encode_float
653(
654 OpusMSEncoder *st,
655 const opus_val16 *pcm,
656 int frame_size,
657 unsigned char *data,
658 opus_int32 max_data_bytes
659)
660{
661 return opus_multistream_encode_native(st, opus_copy_channel_in_float,
662 pcm, frame_size, data, max_data_bytes);
663}
664
665int opus_multistream_encode(
666 OpusMSEncoder *st,
667 const opus_int16 *pcm,
668 int frame_size,
669 unsigned char *data,
670 opus_int32 max_data_bytes
671)
672{
673 return opus_multistream_encode_native(st, opus_copy_channel_in_short,
674 pcm, frame_size, data, max_data_bytes);
675}
676#endif
677
678int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...)
679{
680 va_list ap;
681 int coupled_size, mono_size;
682 char *ptr;
683 int ret = OPUS_OK;
684
685 va_start(ap, request);
686
687 coupled_size = opus_encoder_get_size(2);
688 mono_size = opus_encoder_get_size(1);
689 ptr = (char*)st + align(sizeof(OpusMSEncoder));
690 switch (request)
691 {
692 case OPUS_SET_BITRATE_REQUEST:
693 {
694 opus_int32 value = va_arg(ap, opus_int32);
695 if (value<0 && value!=OPUS_AUTO && value!=OPUS_BITRATE_MAX)
696 goto bad_arg;
697 st->bitrate_bps = value;
698 }
699 break;
700 case OPUS_GET_BITRATE_REQUEST:
701 {
702 int s;
703 opus_int32 *value = va_arg(ap, opus_int32*);
704 *value = 0;
705 for (s=0;s<st->layout.nb_streams;s++)
706 {
707 opus_int32 rate;
708 OpusEncoder *enc;
709 enc = (OpusEncoder*)ptr;
710 if (s < st->layout.nb_coupled_streams)
711 ptr += align(coupled_size);
712 else
713 ptr += align(mono_size);
714 opus_encoder_ctl(enc, request, &rate);
715 *value += rate;
716 }
717 }
718 break;
719 case OPUS_GET_LSB_DEPTH_REQUEST:
720 case OPUS_GET_VBR_REQUEST:
721 case OPUS_GET_APPLICATION_REQUEST:
722 case OPUS_GET_BANDWIDTH_REQUEST:
723 case OPUS_GET_COMPLEXITY_REQUEST:
724 case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
725 case OPUS_GET_DTX_REQUEST:
726 case OPUS_GET_VOICE_RATIO_REQUEST:
727 case OPUS_GET_VBR_CONSTRAINT_REQUEST:
728 case OPUS_GET_SIGNAL_REQUEST:
729 case OPUS_GET_LOOKAHEAD_REQUEST:
730 case OPUS_GET_SAMPLE_RATE_REQUEST:
731 case OPUS_GET_INBAND_FEC_REQUEST:
732 case OPUS_GET_FORCE_CHANNELS_REQUEST:
733 {
734 OpusEncoder *enc;
735 /* For int32* GET params, just query the first stream */
736 opus_int32 *value = va_arg(ap, opus_int32*);
737 enc = (OpusEncoder*)ptr;
738 ret = opus_encoder_ctl(enc, request, value);
739 }
740 break;
741 case OPUS_GET_FINAL_RANGE_REQUEST:
742 {
743 int s;
744 opus_uint32 *value = va_arg(ap, opus_uint32*);
745 opus_uint32 tmp;
746 *value=0;
747 for (s=0;s<st->layout.nb_streams;s++)
748 {
749 OpusEncoder *enc;
750 enc = (OpusEncoder*)ptr;
751 if (s < st->layout.nb_coupled_streams)
752 ptr += align(coupled_size);
753 else
754 ptr += align(mono_size);
755 ret = opus_encoder_ctl(enc, request, &tmp);
756 if (ret != OPUS_OK) break;
757 *value ^= tmp;
758 }
759 }
760 break;
761 case OPUS_SET_LSB_DEPTH_REQUEST:
762 case OPUS_SET_COMPLEXITY_REQUEST:
763 case OPUS_SET_VBR_REQUEST:
764 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
765 case OPUS_SET_BANDWIDTH_REQUEST:
766 case OPUS_SET_SIGNAL_REQUEST:
767 case OPUS_SET_APPLICATION_REQUEST:
768 case OPUS_SET_INBAND_FEC_REQUEST:
769 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
770 case OPUS_SET_DTX_REQUEST:
771 case OPUS_SET_FORCE_MODE_REQUEST:
772 case OPUS_SET_FORCE_CHANNELS_REQUEST:
773 {
774 int s;
775 /* This works for int32 params */
776 opus_int32 value = va_arg(ap, opus_int32);
777 for (s=0;s<st->layout.nb_streams;s++)
778 {
779 OpusEncoder *enc;
780
781 enc = (OpusEncoder*)ptr;
782 if (s < st->layout.nb_coupled_streams)
783 ptr += align(coupled_size);
784 else
785 ptr += align(mono_size);
786 ret = opus_encoder_ctl(enc, request, value);
787 if (ret != OPUS_OK)
788 break;
789 }
790 }
791 break;
792 case OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST:
793 {
794 int s;
795 opus_int32 stream_id;
796 OpusEncoder **value;
797 stream_id = va_arg(ap, opus_int32);
798 if (stream_id<0 || stream_id >= st->layout.nb_streams)
799 ret = OPUS_BAD_ARG;
800 value = va_arg(ap, OpusEncoder**);
801 for (s=0;s<stream_id;s++)
802 {
803 if (s < st->layout.nb_coupled_streams)
804 ptr += align(coupled_size);
805 else
806 ptr += align(mono_size);
807 }
808 *value = (OpusEncoder*)ptr;
809 }
810 break;
811 default:
812 ret = OPUS_UNIMPLEMENTED;
813 break;
814 }
815
816 va_end(ap);
817 return ret;
818bad_arg:
819 va_end(ap);
820 return OPUS_BAD_ARG;
821}
822
823void opus_multistream_encoder_destroy(OpusMSEncoder *st)
824{
825 opus_free(st);
826}
827
828
829/* DECODER */
830
831opus_int32 opus_multistream_decoder_get_size(int nb_streams, int nb_coupled_streams)
832{
833 int coupled_size;
834 int mono_size;
835
836 if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0;
837 coupled_size = opus_decoder_get_size(2);
838 mono_size = opus_decoder_get_size(1);
839 return align(sizeof(OpusMSDecoder))
840 + nb_coupled_streams * align(coupled_size)
841 + (nb_streams-nb_coupled_streams) * align(mono_size);
842}
843
844int opus_multistream_decoder_init(
845 OpusMSDecoder *st,
846 opus_int32 Fs,
847 int channels,
848 int streams,
849 int coupled_streams,
850 const unsigned char *mapping
851)
852{
853 int coupled_size;
854 int mono_size;
855 int i, ret;
856 char *ptr;
857
858 if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
859 (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0))
860 return OPUS_BAD_ARG;
861
862 st->layout.nb_channels = channels;
863 st->layout.nb_streams = streams;
864 st->layout.nb_coupled_streams = coupled_streams;
865
866 for (i=0;i<st->layout.nb_channels;i++)
867 st->layout.mapping[i] = mapping[i];
868 if (!validate_layout(&st->layout))
869 return OPUS_BAD_ARG;
870
871 ptr = (char*)st + align(sizeof(OpusMSDecoder));
872 coupled_size = opus_decoder_get_size(2);
873 mono_size = opus_decoder_get_size(1);
874
875 for (i=0;i<st->layout.nb_coupled_streams;i++)
876 {
877 ret=opus_decoder_init((OpusDecoder*)ptr, Fs, 2);
878 if(ret!=OPUS_OK)return ret;
879 ptr += align(coupled_size);
880 }
881 for (;i<st->layout.nb_streams;i++)
882 {
883 ret=opus_decoder_init((OpusDecoder*)ptr, Fs, 1);
884 if(ret!=OPUS_OK)return ret;
885 ptr += align(mono_size);
886 }
887 return OPUS_OK;
888}
889
890
891OpusMSDecoder *opus_multistream_decoder_create(
892 opus_int32 Fs,
893 int channels,
894 int streams,
895 int coupled_streams,
896 const unsigned char *mapping,
897 int *error
898)
899{
900 int ret;
901 OpusMSDecoder *st;
902 if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
903 (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0))
904 {
905 if (error)
906 *error = OPUS_BAD_ARG;
907 return NULL;
908 }
909 st = (OpusMSDecoder *)opus_alloc(opus_multistream_decoder_get_size(streams, coupled_streams));
910 if (st==NULL)
911 {
912 if (error)
913 *error = OPUS_ALLOC_FAIL;
914 return NULL;
915 }
916 ret = opus_multistream_decoder_init(st, Fs, channels, streams, coupled_streams, mapping);
917 if (error)
918 *error = ret;
919 if (ret != OPUS_OK)
920 {
921 opus_free(st);
922 st = NULL;
923 }
924 return st;
925}
926
927typedef void (*opus_copy_channel_out_func)(
928 void *dst,
929 int dst_stride,
930 int dst_channel,
931 const opus_val16 *src,
932 int src_stride,
933 int frame_size
934);
935
936static int opus_multistream_decode_native(
937 OpusMSDecoder *st,
938 const unsigned char *data,
939 opus_int32 len,
940 void *pcm,
941 opus_copy_channel_out_func copy_channel_out,
942 int frame_size,
943 int decode_fec
944)
945{
946 opus_int32 Fs;
947 int coupled_size;
948 int mono_size;
949 int s, c;
950 char *ptr;
951 int do_plc=0;
952 VARDECL(opus_val16, buf);
953 ALLOC_STACK;
954
955 /* Limit frame_size to avoid excessive stack allocations. */
956 opus_multistream_decoder_ctl(st, OPUS_GET_SAMPLE_RATE(&Fs));
957 frame_size = IMIN(frame_size, Fs/25*3);
958 ALLOC(buf, 2*frame_size, opus_val16);
959 ptr = (char*)st + align(sizeof(OpusMSDecoder));
960 coupled_size = opus_decoder_get_size(2);
961 mono_size = opus_decoder_get_size(1);
962
963 if (len==0)
964 do_plc = 1;
965 if (len < 0)
966 return OPUS_BAD_ARG;
967 if (!do_plc && len < 2*st->layout.nb_streams-1)
968 return OPUS_INVALID_PACKET;
969 for (s=0;s<st->layout.nb_streams;s++)
970 {
971 OpusDecoder *dec;
972 int packet_offset, ret;
973
974 dec = (OpusDecoder*)ptr;
975 ptr += (s < st->layout.nb_coupled_streams) ? align(coupled_size) : align(mono_size);
976
977 if (!do_plc && len<=0)
978 {
979 RESTORE_STACK;
980 return OPUS_INVALID_PACKET;
981 }
982 packet_offset = 0;
983 ret = opus_decode_native(dec, data, len, buf, frame_size, decode_fec, s!=st->layout.nb_streams-1, &packet_offset);
984 data += packet_offset;
985 len -= packet_offset;
986 if (ret > frame_size)
987 {
988 RESTORE_STACK;
989 return OPUS_BUFFER_TOO_SMALL;
990 }
991 if (s>0 && ret != frame_size)
992 {
993 RESTORE_STACK;
994 return OPUS_INVALID_PACKET;
995 }
996 if (ret <= 0)
997 {
998 RESTORE_STACK;
999 return ret;
1000 }
1001 frame_size = ret;
1002 if (s < st->layout.nb_coupled_streams)
1003 {
1004 int chan, prev;
1005 prev = -1;
1006 /* Copy "left" audio to the channel(s) where it belongs */
1007 while ( (chan = get_left_channel(&st->layout, s, prev)) != -1)
1008 {
1009 (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
1010 buf, 2, frame_size);
1011 prev = chan;
1012 }
1013 prev = -1;
1014 /* Copy "right" audio to the channel(s) where it belongs */
1015 while ( (chan = get_right_channel(&st->layout, s, prev)) != -1)
1016 {
1017 (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
1018 buf+1, 2, frame_size);
1019 prev = chan;
1020 }
1021 } else {
1022 int chan, prev;
1023 prev = -1;
1024 /* Copy audio to the channel(s) where it belongs */
1025 while ( (chan = get_mono_channel(&st->layout, s, prev)) != -1)
1026 {
1027 (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
1028 buf, 1, frame_size);
1029 prev = chan;
1030 }
1031 }
1032 }
1033 /* Handle muted channels */
1034 for (c=0;c<st->layout.nb_channels;c++)
1035 {
1036 if (st->layout.mapping[c] == 255)
1037 {
1038 (*copy_channel_out)(pcm, st->layout.nb_channels, c,
1039 NULL, 0, frame_size);
1040 }
1041 }
1042 RESTORE_STACK;
1043 return frame_size;
1044}
1045
1046#if !defined(DISABLE_FLOAT_API)
1047static void opus_copy_channel_out_float(
1048 void *dst,
1049 int dst_stride,
1050 int dst_channel,
1051 const opus_val16 *src,
1052 int src_stride,
1053 int frame_size
1054)
1055{
1056 float *float_dst;
1057 int i;
1058 float_dst = (float*)dst;
1059 if (src != NULL)
1060 {
1061 for (i=0;i<frame_size;i++)
1062#if defined(FIXED_POINT)
1063 float_dst[i*dst_stride+dst_channel] = (1/32768.f)*src[i*src_stride];
1064#else
1065 float_dst[i*dst_stride+dst_channel] = src[i*src_stride];
1066#endif
1067 }
1068 else
1069 {
1070 for (i=0;i<frame_size;i++)
1071 float_dst[i*dst_stride+dst_channel] = 0;
1072 }
1073}
1074#endif
1075
1076static void opus_copy_channel_out_short(
1077 void *dst,
1078 int dst_stride,
1079 int dst_channel,
1080 const opus_val16 *src,
1081 int src_stride,
1082 int frame_size
1083)
1084{
1085 opus_int16 *short_dst;
1086 int i;
1087 short_dst = (opus_int16*)dst;
1088 if (src != NULL)
1089 {
1090 for (i=0;i<frame_size;i++)
1091#if defined(FIXED_POINT)
1092 short_dst[i*dst_stride+dst_channel] = src[i*src_stride];
1093#else
1094 short_dst[i*dst_stride+dst_channel] = FLOAT2INT16(src[i*src_stride]);
1095#endif
1096 }
1097 else
1098 {
1099 for (i=0;i<frame_size;i++)
1100 short_dst[i*dst_stride+dst_channel] = 0;
1101 }
1102}
1103
1104
1105
1106#ifdef FIXED_POINT
1107int opus_multistream_decode(
1108 OpusMSDecoder *st,
1109 const unsigned char *data,
1110 opus_int32 len,
1111 opus_int16 *pcm,
1112 int frame_size,
1113 int decode_fec
1114)
1115{
1116 return opus_multistream_decode_native(st, data, len,
1117 pcm, opus_copy_channel_out_short, frame_size, decode_fec);
1118}
1119
1120#ifndef DISABLE_FLOAT_API
1121int opus_multistream_decode_float(OpusMSDecoder *st, const unsigned char *data,
1122 opus_int32 len, float *pcm, int frame_size, int decode_fec)
1123{
1124 return opus_multistream_decode_native(st, data, len,
1125 pcm, opus_copy_channel_out_float, frame_size, decode_fec);
1126}
1127#endif
1128
1129#else
1130
1131int opus_multistream_decode(OpusMSDecoder *st, const unsigned char *data,
1132 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
1133{
1134 return opus_multistream_decode_native(st, data, len,
1135 pcm, opus_copy_channel_out_short, frame_size, decode_fec);
1136}
1137
1138int opus_multistream_decode_float(
1139 OpusMSDecoder *st,
1140 const unsigned char *data,
1141 opus_int32 len,
1142 float *pcm,
1143 int frame_size,
1144 int decode_fec
1145)
1146{
1147 return opus_multistream_decode_native(st, data, len,
1148 pcm, opus_copy_channel_out_float, frame_size, decode_fec);
1149}
1150#endif
1151
1152int opus_multistream_decoder_ctl(OpusMSDecoder *st, int request, ...)
1153{
1154 va_list ap;
1155 int coupled_size, mono_size;
1156 char *ptr;
1157 int ret = OPUS_OK;
1158
1159 va_start(ap, request);
1160
1161 coupled_size = opus_decoder_get_size(2);
1162 mono_size = opus_decoder_get_size(1);
1163 ptr = (char*)st + align(sizeof(OpusMSDecoder));
1164 switch (request)
1165 {
1166 case OPUS_GET_BANDWIDTH_REQUEST:
1167 case OPUS_GET_SAMPLE_RATE_REQUEST:
1168 {
1169 OpusDecoder *dec;
1170 /* For int32* GET params, just query the first stream */
1171 opus_int32 *value = va_arg(ap, opus_int32*);
1172 dec = (OpusDecoder*)ptr;
1173 ret = opus_decoder_ctl(dec, request, value);
1174 }
1175 break;
1176 case OPUS_GET_FINAL_RANGE_REQUEST:
1177 {
1178 int s;
1179 opus_uint32 *value = va_arg(ap, opus_uint32*);
1180 opus_uint32 tmp;
1181 *value = 0;
1182 for (s=0;s<st->layout.nb_streams;s++)
1183 {
1184 OpusDecoder *dec;
1185 dec = (OpusDecoder*)ptr;
1186 if (s < st->layout.nb_coupled_streams)
1187 ptr += align(coupled_size);
1188 else
1189 ptr += align(mono_size);
1190 ret = opus_decoder_ctl(dec, request, &tmp);
1191 if (ret != OPUS_OK) break;
1192 *value ^= tmp;
1193 }
1194 }
1195 break;
1196 case OPUS_RESET_STATE:
1197 {
1198 int s;
1199 for (s=0;s<st->layout.nb_streams;s++)
1200 {
1201 OpusDecoder *dec;
1202
1203 dec = (OpusDecoder*)ptr;
1204 if (s < st->layout.nb_coupled_streams)
1205 ptr += align(coupled_size);
1206 else
1207 ptr += align(mono_size);
1208 ret = opus_decoder_ctl(dec, OPUS_RESET_STATE);
1209 if (ret != OPUS_OK)
1210 break;
1211 }
1212 }
1213 break;
1214 case OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST:
1215 {
1216 int s;
1217 opus_int32 stream_id;
1218 OpusDecoder **value;
1219 stream_id = va_arg(ap, opus_int32);
1220 if (stream_id<0 || stream_id >= st->layout.nb_streams)
1221 ret = OPUS_BAD_ARG;
1222 value = va_arg(ap, OpusDecoder**);
1223 for (s=0;s<stream_id;s++)
1224 {
1225 if (s < st->layout.nb_coupled_streams)
1226 ptr += align(coupled_size);
1227 else
1228 ptr += align(mono_size);
1229 }
1230 *value = (OpusDecoder*)ptr;
1231 }
1232 break;
1233 case OPUS_SET_GAIN_REQUEST:
1234 {
1235 int s;
1236 /* This works for int32 params */
1237 opus_int32 value = va_arg(ap, opus_int32);
1238 for (s=0;s<st->layout.nb_streams;s++)
1239 {
1240 OpusDecoder *dec;
1241
1242 dec = (OpusDecoder*)ptr;
1243 if (s < st->layout.nb_coupled_streams)
1244 ptr += align(coupled_size);
1245 else
1246 ptr += align(mono_size);
1247 ret = opus_decoder_ctl(dec, request, value);
1248 if (ret != OPUS_OK)
1249 break;
1250 }
1251 }
1252 break;
1253 default:
1254 ret = OPUS_UNIMPLEMENTED;
1255 break;
1256 }
1257
1258 va_end(ap);
1259 return ret;
1260}
1261
1262
1263void opus_multistream_decoder_destroy(OpusMSDecoder *st)
1264{
1265 opus_free(st);
1266}