opus_multistream_decoder.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* 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. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "opus_multistream.h"
  28. #include "opus.h"
  29. #include "opus_private.h"
  30. #include "stack_alloc.h"
  31. #include <stdarg.h>
  32. #include "float_cast.h"
  33. #include "os_support.h"
  34. struct OpusMSDecoder {
  35. ChannelLayout layout;
  36. /* Decoder states go here */
  37. };
  38. /* DECODER */
  39. opus_int32 opus_multistream_decoder_get_size(int nb_streams, int nb_coupled_streams)
  40. {
  41. int coupled_size;
  42. int mono_size;
  43. if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0;
  44. coupled_size = opus_decoder_get_size(2);
  45. mono_size = opus_decoder_get_size(1);
  46. return align(sizeof(OpusMSDecoder))
  47. + nb_coupled_streams * align(coupled_size)
  48. + (nb_streams-nb_coupled_streams) * align(mono_size);
  49. }
  50. int opus_multistream_decoder_init(
  51. OpusMSDecoder *st,
  52. opus_int32 Fs,
  53. int channels,
  54. int streams,
  55. int coupled_streams,
  56. const unsigned char *mapping
  57. )
  58. {
  59. int coupled_size;
  60. int mono_size;
  61. int i, ret;
  62. char *ptr;
  63. if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
  64. (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams))
  65. return OPUS_BAD_ARG;
  66. st->layout.nb_channels = channels;
  67. st->layout.nb_streams = streams;
  68. st->layout.nb_coupled_streams = coupled_streams;
  69. for (i=0;i<st->layout.nb_channels;i++)
  70. st->layout.mapping[i] = mapping[i];
  71. if (!validate_layout(&st->layout))
  72. return OPUS_BAD_ARG;
  73. ptr = (char*)st + align(sizeof(OpusMSDecoder));
  74. coupled_size = opus_decoder_get_size(2);
  75. mono_size = opus_decoder_get_size(1);
  76. for (i=0;i<st->layout.nb_coupled_streams;i++)
  77. {
  78. ret=opus_decoder_init((OpusDecoder*)ptr, Fs, 2);
  79. if(ret!=OPUS_OK)return ret;
  80. ptr += align(coupled_size);
  81. }
  82. for (;i<st->layout.nb_streams;i++)
  83. {
  84. ret=opus_decoder_init((OpusDecoder*)ptr, Fs, 1);
  85. if(ret!=OPUS_OK)return ret;
  86. ptr += align(mono_size);
  87. }
  88. return OPUS_OK;
  89. }
  90. OpusMSDecoder *opus_multistream_decoder_create(
  91. opus_int32 Fs,
  92. int channels,
  93. int streams,
  94. int coupled_streams,
  95. const unsigned char *mapping,
  96. int *error
  97. )
  98. {
  99. int ret;
  100. OpusMSDecoder *st;
  101. if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
  102. (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams))
  103. {
  104. if (error)
  105. *error = OPUS_BAD_ARG;
  106. return NULL;
  107. }
  108. st = (OpusMSDecoder *)opus_alloc(opus_multistream_decoder_get_size(streams, coupled_streams));
  109. if (st==NULL)
  110. {
  111. if (error)
  112. *error = OPUS_ALLOC_FAIL;
  113. return NULL;
  114. }
  115. ret = opus_multistream_decoder_init(st, Fs, channels, streams, coupled_streams, mapping);
  116. if (error)
  117. *error = ret;
  118. if (ret != OPUS_OK)
  119. {
  120. opus_free(st);
  121. st = NULL;
  122. }
  123. return st;
  124. }
  125. typedef void (*opus_copy_channel_out_func)(
  126. void *dst,
  127. int dst_stride,
  128. int dst_channel,
  129. const opus_val16 *src,
  130. int src_stride,
  131. int frame_size
  132. );
  133. static int opus_multistream_packet_validate(const unsigned char *data,
  134. opus_int32 len, int nb_streams, opus_int32 Fs)
  135. {
  136. int s;
  137. int count;
  138. unsigned char toc;
  139. opus_int16 size[48];
  140. int samples=0;
  141. opus_int32 packet_offset;
  142. for (s=0;s<nb_streams;s++)
  143. {
  144. int tmp_samples;
  145. if (len<=0)
  146. return OPUS_INVALID_PACKET;
  147. count = opus_packet_parse_impl(data, len, s!=nb_streams-1, &toc, NULL,
  148. size, NULL, &packet_offset);
  149. if (count<0)
  150. return count;
  151. tmp_samples = opus_packet_get_nb_samples(data, packet_offset, Fs);
  152. if (s!=0 && samples != tmp_samples)
  153. return OPUS_INVALID_PACKET;
  154. samples = tmp_samples;
  155. data += packet_offset;
  156. len -= packet_offset;
  157. }
  158. return samples;
  159. }
  160. static int opus_multistream_decode_native(
  161. OpusMSDecoder *st,
  162. const unsigned char *data,
  163. opus_int32 len,
  164. void *pcm,
  165. opus_copy_channel_out_func copy_channel_out,
  166. int frame_size,
  167. int decode_fec,
  168. int soft_clip
  169. )
  170. {
  171. opus_int32 Fs;
  172. int coupled_size;
  173. int mono_size;
  174. int s, c;
  175. char *ptr;
  176. int do_plc=0;
  177. VARDECL(opus_val16, buf);
  178. ALLOC_STACK;
  179. /* Limit frame_size to avoid excessive stack allocations. */
  180. opus_multistream_decoder_ctl(st, OPUS_GET_SAMPLE_RATE(&Fs));
  181. frame_size = IMIN(frame_size, Fs/25*3);
  182. ALLOC(buf, 2*frame_size, opus_val16);
  183. ptr = (char*)st + align(sizeof(OpusMSDecoder));
  184. coupled_size = opus_decoder_get_size(2);
  185. mono_size = opus_decoder_get_size(1);
  186. if (len==0)
  187. do_plc = 1;
  188. if (len < 0)
  189. {
  190. RESTORE_STACK;
  191. return OPUS_BAD_ARG;
  192. }
  193. if (!do_plc && len < 2*st->layout.nb_streams-1)
  194. {
  195. RESTORE_STACK;
  196. return OPUS_INVALID_PACKET;
  197. }
  198. if (!do_plc)
  199. {
  200. int ret = opus_multistream_packet_validate(data, len, st->layout.nb_streams, Fs);
  201. if (ret < 0)
  202. {
  203. RESTORE_STACK;
  204. return ret;
  205. } else if (ret > frame_size)
  206. {
  207. RESTORE_STACK;
  208. return OPUS_BUFFER_TOO_SMALL;
  209. }
  210. }
  211. for (s=0;s<st->layout.nb_streams;s++)
  212. {
  213. OpusDecoder *dec;
  214. int packet_offset, ret;
  215. dec = (OpusDecoder*)ptr;
  216. ptr += (s < st->layout.nb_coupled_streams) ? align(coupled_size) : align(mono_size);
  217. if (!do_plc && len<=0)
  218. {
  219. RESTORE_STACK;
  220. return OPUS_INTERNAL_ERROR;
  221. }
  222. packet_offset = 0;
  223. ret = opus_decode_native(dec, data, len, buf, frame_size, decode_fec, s!=st->layout.nb_streams-1, &packet_offset, soft_clip);
  224. data += packet_offset;
  225. len -= packet_offset;
  226. if (ret <= 0)
  227. {
  228. RESTORE_STACK;
  229. return ret;
  230. }
  231. frame_size = ret;
  232. if (s < st->layout.nb_coupled_streams)
  233. {
  234. int chan, prev;
  235. prev = -1;
  236. /* Copy "left" audio to the channel(s) where it belongs */
  237. while ( (chan = get_left_channel(&st->layout, s, prev)) != -1)
  238. {
  239. (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
  240. buf, 2, frame_size);
  241. prev = chan;
  242. }
  243. prev = -1;
  244. /* Copy "right" audio to the channel(s) where it belongs */
  245. while ( (chan = get_right_channel(&st->layout, s, prev)) != -1)
  246. {
  247. (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
  248. buf+1, 2, frame_size);
  249. prev = chan;
  250. }
  251. } else {
  252. int chan, prev;
  253. prev = -1;
  254. /* Copy audio to the channel(s) where it belongs */
  255. while ( (chan = get_mono_channel(&st->layout, s, prev)) != -1)
  256. {
  257. (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
  258. buf, 1, frame_size);
  259. prev = chan;
  260. }
  261. }
  262. }
  263. /* Handle muted channels */
  264. for (c=0;c<st->layout.nb_channels;c++)
  265. {
  266. if (st->layout.mapping[c] == 255)
  267. {
  268. (*copy_channel_out)(pcm, st->layout.nb_channels, c,
  269. NULL, 0, frame_size);
  270. }
  271. }
  272. RESTORE_STACK;
  273. return frame_size;
  274. }
  275. #if !defined(DISABLE_FLOAT_API)
  276. static void opus_copy_channel_out_float(
  277. void *dst,
  278. int dst_stride,
  279. int dst_channel,
  280. const opus_val16 *src,
  281. int src_stride,
  282. int frame_size
  283. )
  284. {
  285. float *float_dst;
  286. opus_int32 i;
  287. float_dst = (float*)dst;
  288. if (src != NULL)
  289. {
  290. for (i=0;i<frame_size;i++)
  291. #if defined(FIXED_POINT)
  292. float_dst[i*dst_stride+dst_channel] = (1/32768.f)*src[i*src_stride];
  293. #else
  294. float_dst[i*dst_stride+dst_channel] = src[i*src_stride];
  295. #endif
  296. }
  297. else
  298. {
  299. for (i=0;i<frame_size;i++)
  300. float_dst[i*dst_stride+dst_channel] = 0;
  301. }
  302. }
  303. #endif
  304. static void opus_copy_channel_out_short(
  305. void *dst,
  306. int dst_stride,
  307. int dst_channel,
  308. const opus_val16 *src,
  309. int src_stride,
  310. int frame_size
  311. )
  312. {
  313. opus_int16 *short_dst;
  314. opus_int32 i;
  315. short_dst = (opus_int16*)dst;
  316. if (src != NULL)
  317. {
  318. for (i=0;i<frame_size;i++)
  319. #if defined(FIXED_POINT)
  320. short_dst[i*dst_stride+dst_channel] = src[i*src_stride];
  321. #else
  322. short_dst[i*dst_stride+dst_channel] = FLOAT2INT16(src[i*src_stride]);
  323. #endif
  324. }
  325. else
  326. {
  327. for (i=0;i<frame_size;i++)
  328. short_dst[i*dst_stride+dst_channel] = 0;
  329. }
  330. }
  331. #ifdef FIXED_POINT
  332. int opus_multistream_decode(
  333. OpusMSDecoder *st,
  334. const unsigned char *data,
  335. opus_int32 len,
  336. opus_int16 *pcm,
  337. int frame_size,
  338. int decode_fec
  339. )
  340. {
  341. return opus_multistream_decode_native(st, data, len,
  342. pcm, opus_copy_channel_out_short, frame_size, decode_fec, 0);
  343. }
  344. #ifndef DISABLE_FLOAT_API
  345. int opus_multistream_decode_float(OpusMSDecoder *st, const unsigned char *data,
  346. opus_int32 len, float *pcm, int frame_size, int decode_fec)
  347. {
  348. return opus_multistream_decode_native(st, data, len,
  349. pcm, opus_copy_channel_out_float, frame_size, decode_fec, 0);
  350. }
  351. #endif
  352. #else
  353. int opus_multistream_decode(OpusMSDecoder *st, const unsigned char *data,
  354. opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
  355. {
  356. return opus_multistream_decode_native(st, data, len,
  357. pcm, opus_copy_channel_out_short, frame_size, decode_fec, 1);
  358. }
  359. int opus_multistream_decode_float(
  360. OpusMSDecoder *st,
  361. const unsigned char *data,
  362. opus_int32 len,
  363. float *pcm,
  364. int frame_size,
  365. int decode_fec
  366. )
  367. {
  368. return opus_multistream_decode_native(st, data, len,
  369. pcm, opus_copy_channel_out_float, frame_size, decode_fec, 0);
  370. }
  371. #endif
  372. int opus_multistream_decoder_ctl(OpusMSDecoder *st, int request, ...)
  373. {
  374. va_list ap;
  375. int coupled_size, mono_size;
  376. char *ptr;
  377. int ret = OPUS_OK;
  378. va_start(ap, request);
  379. coupled_size = opus_decoder_get_size(2);
  380. mono_size = opus_decoder_get_size(1);
  381. ptr = (char*)st + align(sizeof(OpusMSDecoder));
  382. switch (request)
  383. {
  384. case OPUS_GET_BANDWIDTH_REQUEST:
  385. case OPUS_GET_SAMPLE_RATE_REQUEST:
  386. case OPUS_GET_GAIN_REQUEST:
  387. case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
  388. {
  389. OpusDecoder *dec;
  390. /* For int32* GET params, just query the first stream */
  391. opus_int32 *value = va_arg(ap, opus_int32*);
  392. dec = (OpusDecoder*)ptr;
  393. ret = opus_decoder_ctl(dec, request, value);
  394. }
  395. break;
  396. case OPUS_GET_FINAL_RANGE_REQUEST:
  397. {
  398. int s;
  399. opus_uint32 *value = va_arg(ap, opus_uint32*);
  400. opus_uint32 tmp;
  401. if (!value)
  402. {
  403. goto bad_arg;
  404. }
  405. *value = 0;
  406. for (s=0;s<st->layout.nb_streams;s++)
  407. {
  408. OpusDecoder *dec;
  409. dec = (OpusDecoder*)ptr;
  410. if (s < st->layout.nb_coupled_streams)
  411. ptr += align(coupled_size);
  412. else
  413. ptr += align(mono_size);
  414. ret = opus_decoder_ctl(dec, request, &tmp);
  415. if (ret != OPUS_OK) break;
  416. *value ^= tmp;
  417. }
  418. }
  419. break;
  420. case OPUS_RESET_STATE:
  421. {
  422. int s;
  423. for (s=0;s<st->layout.nb_streams;s++)
  424. {
  425. OpusDecoder *dec;
  426. dec = (OpusDecoder*)ptr;
  427. if (s < st->layout.nb_coupled_streams)
  428. ptr += align(coupled_size);
  429. else
  430. ptr += align(mono_size);
  431. ret = opus_decoder_ctl(dec, OPUS_RESET_STATE);
  432. if (ret != OPUS_OK)
  433. break;
  434. }
  435. }
  436. break;
  437. case OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST:
  438. {
  439. int s;
  440. opus_int32 stream_id;
  441. OpusDecoder **value;
  442. stream_id = va_arg(ap, opus_int32);
  443. if (stream_id<0 || stream_id >= st->layout.nb_streams)
  444. ret = OPUS_BAD_ARG;
  445. value = va_arg(ap, OpusDecoder**);
  446. if (!value)
  447. {
  448. goto bad_arg;
  449. }
  450. for (s=0;s<stream_id;s++)
  451. {
  452. if (s < st->layout.nb_coupled_streams)
  453. ptr += align(coupled_size);
  454. else
  455. ptr += align(mono_size);
  456. }
  457. *value = (OpusDecoder*)ptr;
  458. }
  459. break;
  460. case OPUS_SET_GAIN_REQUEST:
  461. {
  462. int s;
  463. /* This works for int32 params */
  464. opus_int32 value = va_arg(ap, opus_int32);
  465. for (s=0;s<st->layout.nb_streams;s++)
  466. {
  467. OpusDecoder *dec;
  468. dec = (OpusDecoder*)ptr;
  469. if (s < st->layout.nb_coupled_streams)
  470. ptr += align(coupled_size);
  471. else
  472. ptr += align(mono_size);
  473. ret = opus_decoder_ctl(dec, request, value);
  474. if (ret != OPUS_OK)
  475. break;
  476. }
  477. }
  478. break;
  479. default:
  480. ret = OPUS_UNIMPLEMENTED;
  481. break;
  482. }
  483. va_end(ap);
  484. return ret;
  485. bad_arg:
  486. va_end(ap);
  487. return OPUS_BAD_ARG;
  488. }
  489. void opus_multistream_decoder_destroy(OpusMSDecoder *st)
  490. {
  491. opus_free(st);
  492. }