opus_multistream_decoder.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. /* DECODER */
  35. #if defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS)
  36. static void validate_ms_decoder(OpusMSDecoder *st)
  37. {
  38. validate_layout(&st->layout);
  39. }
  40. #define VALIDATE_MS_DECODER(st) validate_ms_decoder(st)
  41. #else
  42. #define VALIDATE_MS_DECODER(st)
  43. #endif
  44. opus_int32 opus_multistream_decoder_get_size(int nb_streams, int nb_coupled_streams)
  45. {
  46. int coupled_size;
  47. int mono_size;
  48. if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0;
  49. coupled_size = opus_decoder_get_size(2);
  50. mono_size = opus_decoder_get_size(1);
  51. return align(sizeof(OpusMSDecoder))
  52. + nb_coupled_streams * align(coupled_size)
  53. + (nb_streams-nb_coupled_streams) * align(mono_size);
  54. }
  55. int opus_multistream_decoder_init(
  56. OpusMSDecoder *st,
  57. opus_int32 Fs,
  58. int channels,
  59. int streams,
  60. int coupled_streams,
  61. const unsigned char *mapping
  62. )
  63. {
  64. int coupled_size;
  65. int mono_size;
  66. int i, ret;
  67. char *ptr;
  68. if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
  69. (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams))
  70. return OPUS_BAD_ARG;
  71. st->layout.nb_channels = channels;
  72. st->layout.nb_streams = streams;
  73. st->layout.nb_coupled_streams = coupled_streams;
  74. for (i=0;i<st->layout.nb_channels;i++)
  75. st->layout.mapping[i] = mapping[i];
  76. if (!validate_layout(&st->layout))
  77. return OPUS_BAD_ARG;
  78. ptr = (char*)st + align(sizeof(OpusMSDecoder));
  79. coupled_size = opus_decoder_get_size(2);
  80. mono_size = opus_decoder_get_size(1);
  81. for (i=0;i<st->layout.nb_coupled_streams;i++)
  82. {
  83. ret=opus_decoder_init((OpusDecoder*)ptr, Fs, 2);
  84. if(ret!=OPUS_OK)return ret;
  85. ptr += align(coupled_size);
  86. }
  87. for (;i<st->layout.nb_streams;i++)
  88. {
  89. ret=opus_decoder_init((OpusDecoder*)ptr, Fs, 1);
  90. if(ret!=OPUS_OK)return ret;
  91. ptr += align(mono_size);
  92. }
  93. return OPUS_OK;
  94. }
  95. OpusMSDecoder *opus_multistream_decoder_create(
  96. opus_int32 Fs,
  97. int channels,
  98. int streams,
  99. int coupled_streams,
  100. const unsigned char *mapping,
  101. int *error
  102. )
  103. {
  104. int ret;
  105. OpusMSDecoder *st;
  106. if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
  107. (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams))
  108. {
  109. if (error)
  110. *error = OPUS_BAD_ARG;
  111. return NULL;
  112. }
  113. st = (OpusMSDecoder *)opus_alloc(opus_multistream_decoder_get_size(streams, coupled_streams));
  114. if (st==NULL)
  115. {
  116. if (error)
  117. *error = OPUS_ALLOC_FAIL;
  118. return NULL;
  119. }
  120. ret = opus_multistream_decoder_init(st, Fs, channels, streams, coupled_streams, mapping);
  121. if (error)
  122. *error = ret;
  123. if (ret != OPUS_OK)
  124. {
  125. opus_free(st);
  126. st = NULL;
  127. }
  128. return st;
  129. }
  130. static int opus_multistream_packet_validate(const unsigned char *data,
  131. opus_int32 len, int nb_streams, opus_int32 Fs)
  132. {
  133. int s;
  134. int count;
  135. unsigned char toc;
  136. opus_int16 size[48];
  137. int samples=0;
  138. opus_int32 packet_offset;
  139. for (s=0;s<nb_streams;s++)
  140. {
  141. int tmp_samples;
  142. if (len<=0)
  143. return OPUS_INVALID_PACKET;
  144. count = opus_packet_parse_impl(data, len, s!=nb_streams-1, &toc, NULL,
  145. size, NULL, &packet_offset);
  146. if (count<0)
  147. return count;
  148. tmp_samples = opus_packet_get_nb_samples(data, packet_offset, Fs);
  149. if (s!=0 && samples != tmp_samples)
  150. return OPUS_INVALID_PACKET;
  151. samples = tmp_samples;
  152. data += packet_offset;
  153. len -= packet_offset;
  154. }
  155. return samples;
  156. }
  157. int opus_multistream_decode_native(
  158. OpusMSDecoder *st,
  159. const unsigned char *data,
  160. opus_int32 len,
  161. void *pcm,
  162. opus_copy_channel_out_func copy_channel_out,
  163. int frame_size,
  164. int decode_fec,
  165. int soft_clip,
  166. void *user_data
  167. )
  168. {
  169. opus_int32 Fs;
  170. int coupled_size;
  171. int mono_size;
  172. int s, c;
  173. char *ptr;
  174. int do_plc=0;
  175. VARDECL(opus_val16, buf);
  176. ALLOC_STACK;
  177. VALIDATE_MS_DECODER(st);
  178. if (frame_size <= 0)
  179. {
  180. RESTORE_STACK;
  181. return OPUS_BAD_ARG;
  182. }
  183. /* Limit frame_size to avoid excessive stack allocations. */
  184. MUST_SUCCEED(opus_multistream_decoder_ctl(st, OPUS_GET_SAMPLE_RATE(&Fs)));
  185. frame_size = IMIN(frame_size, Fs/25*3);
  186. ALLOC(buf, 2*frame_size, opus_val16);
  187. ptr = (char*)st + align(sizeof(OpusMSDecoder));
  188. coupled_size = opus_decoder_get_size(2);
  189. mono_size = opus_decoder_get_size(1);
  190. if (len==0)
  191. do_plc = 1;
  192. if (len < 0)
  193. {
  194. RESTORE_STACK;
  195. return OPUS_BAD_ARG;
  196. }
  197. if (!do_plc && len < 2*st->layout.nb_streams-1)
  198. {
  199. RESTORE_STACK;
  200. return OPUS_INVALID_PACKET;
  201. }
  202. if (!do_plc)
  203. {
  204. int ret = opus_multistream_packet_validate(data, len, st->layout.nb_streams, Fs);
  205. if (ret < 0)
  206. {
  207. RESTORE_STACK;
  208. return ret;
  209. } else if (ret > frame_size)
  210. {
  211. RESTORE_STACK;
  212. return OPUS_BUFFER_TOO_SMALL;
  213. }
  214. }
  215. for (s=0;s<st->layout.nb_streams;s++)
  216. {
  217. OpusDecoder *dec;
  218. opus_int32 packet_offset;
  219. int ret;
  220. dec = (OpusDecoder*)ptr;
  221. ptr += (s < st->layout.nb_coupled_streams) ? align(coupled_size) : align(mono_size);
  222. if (!do_plc && len<=0)
  223. {
  224. RESTORE_STACK;
  225. return OPUS_INTERNAL_ERROR;
  226. }
  227. packet_offset = 0;
  228. ret = opus_decode_native(dec, data, len, buf, frame_size, decode_fec, s!=st->layout.nb_streams-1, &packet_offset, soft_clip);
  229. if (!do_plc)
  230. {
  231. data += packet_offset;
  232. len -= packet_offset;
  233. }
  234. if (ret <= 0)
  235. {
  236. RESTORE_STACK;
  237. return ret;
  238. }
  239. frame_size = ret;
  240. if (s < st->layout.nb_coupled_streams)
  241. {
  242. int chan, prev;
  243. prev = -1;
  244. /* Copy "left" audio to the channel(s) where it belongs */
  245. while ( (chan = get_left_channel(&st->layout, s, prev)) != -1)
  246. {
  247. (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
  248. buf, 2, frame_size, user_data);
  249. prev = chan;
  250. }
  251. prev = -1;
  252. /* Copy "right" audio to the channel(s) where it belongs */
  253. while ( (chan = get_right_channel(&st->layout, s, prev)) != -1)
  254. {
  255. (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
  256. buf+1, 2, frame_size, user_data);
  257. prev = chan;
  258. }
  259. } else {
  260. int chan, prev;
  261. prev = -1;
  262. /* Copy audio to the channel(s) where it belongs */
  263. while ( (chan = get_mono_channel(&st->layout, s, prev)) != -1)
  264. {
  265. (*copy_channel_out)(pcm, st->layout.nb_channels, chan,
  266. buf, 1, frame_size, user_data);
  267. prev = chan;
  268. }
  269. }
  270. }
  271. /* Handle muted channels */
  272. for (c=0;c<st->layout.nb_channels;c++)
  273. {
  274. if (st->layout.mapping[c] == 255)
  275. {
  276. (*copy_channel_out)(pcm, st->layout.nb_channels, c,
  277. NULL, 0, frame_size, user_data);
  278. }
  279. }
  280. RESTORE_STACK;
  281. return frame_size;
  282. }
  283. #if !defined(DISABLE_FLOAT_API)
  284. static void opus_copy_channel_out_float(
  285. void *dst,
  286. int dst_stride,
  287. int dst_channel,
  288. const opus_val16 *src,
  289. int src_stride,
  290. int frame_size,
  291. void *user_data
  292. )
  293. {
  294. float *float_dst;
  295. opus_int32 i;
  296. (void)user_data;
  297. float_dst = (float*)dst;
  298. if (src != NULL)
  299. {
  300. for (i=0;i<frame_size;i++)
  301. #if defined(FIXED_POINT)
  302. float_dst[i*dst_stride+dst_channel] = (1/32768.f)*src[i*src_stride];
  303. #else
  304. float_dst[i*dst_stride+dst_channel] = src[i*src_stride];
  305. #endif
  306. }
  307. else
  308. {
  309. for (i=0;i<frame_size;i++)
  310. float_dst[i*dst_stride+dst_channel] = 0;
  311. }
  312. }
  313. #endif
  314. static void opus_copy_channel_out_short(
  315. void *dst,
  316. int dst_stride,
  317. int dst_channel,
  318. const opus_val16 *src,
  319. int src_stride,
  320. int frame_size,
  321. void *user_data
  322. )
  323. {
  324. opus_int16 *short_dst;
  325. opus_int32 i;
  326. (void)user_data;
  327. short_dst = (opus_int16*)dst;
  328. if (src != NULL)
  329. {
  330. for (i=0;i<frame_size;i++)
  331. #if defined(FIXED_POINT)
  332. short_dst[i*dst_stride+dst_channel] = src[i*src_stride];
  333. #else
  334. short_dst[i*dst_stride+dst_channel] = FLOAT2INT16(src[i*src_stride]);
  335. #endif
  336. }
  337. else
  338. {
  339. for (i=0;i<frame_size;i++)
  340. short_dst[i*dst_stride+dst_channel] = 0;
  341. }
  342. }
  343. #ifdef FIXED_POINT
  344. int opus_multistream_decode(
  345. OpusMSDecoder *st,
  346. const unsigned char *data,
  347. opus_int32 len,
  348. opus_int16 *pcm,
  349. int frame_size,
  350. int decode_fec
  351. )
  352. {
  353. return opus_multistream_decode_native(st, data, len,
  354. pcm, opus_copy_channel_out_short, frame_size, decode_fec, 0, NULL);
  355. }
  356. #ifndef DISABLE_FLOAT_API
  357. int opus_multistream_decode_float(OpusMSDecoder *st, const unsigned char *data,
  358. opus_int32 len, float *pcm, int frame_size, int decode_fec)
  359. {
  360. return opus_multistream_decode_native(st, data, len,
  361. pcm, opus_copy_channel_out_float, frame_size, decode_fec, 0, NULL);
  362. }
  363. #endif
  364. #else
  365. int opus_multistream_decode(OpusMSDecoder *st, const unsigned char *data,
  366. opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
  367. {
  368. return opus_multistream_decode_native(st, data, len,
  369. pcm, opus_copy_channel_out_short, frame_size, decode_fec, 1, NULL);
  370. }
  371. int opus_multistream_decode_float(
  372. OpusMSDecoder *st,
  373. const unsigned char *data,
  374. opus_int32 len,
  375. opus_val16 *pcm,
  376. int frame_size,
  377. int decode_fec
  378. )
  379. {
  380. return opus_multistream_decode_native(st, data, len,
  381. pcm, opus_copy_channel_out_float, frame_size, decode_fec, 0, NULL);
  382. }
  383. #endif
  384. int opus_multistream_decoder_ctl_va_list(OpusMSDecoder *st, int request,
  385. va_list ap)
  386. {
  387. int coupled_size, mono_size;
  388. char *ptr;
  389. int ret = OPUS_OK;
  390. coupled_size = opus_decoder_get_size(2);
  391. mono_size = opus_decoder_get_size(1);
  392. ptr = (char*)st + align(sizeof(OpusMSDecoder));
  393. switch (request)
  394. {
  395. case OPUS_GET_BANDWIDTH_REQUEST:
  396. case OPUS_GET_SAMPLE_RATE_REQUEST:
  397. case OPUS_GET_GAIN_REQUEST:
  398. case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
  399. case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
  400. {
  401. OpusDecoder *dec;
  402. /* For int32* GET params, just query the first stream */
  403. opus_int32 *value = va_arg(ap, opus_int32*);
  404. dec = (OpusDecoder*)ptr;
  405. ret = opus_decoder_ctl(dec, request, value);
  406. }
  407. break;
  408. case OPUS_GET_FINAL_RANGE_REQUEST:
  409. {
  410. int s;
  411. opus_uint32 *value = va_arg(ap, opus_uint32*);
  412. opus_uint32 tmp;
  413. if (!value)
  414. {
  415. goto bad_arg;
  416. }
  417. *value = 0;
  418. for (s=0;s<st->layout.nb_streams;s++)
  419. {
  420. OpusDecoder *dec;
  421. dec = (OpusDecoder*)ptr;
  422. if (s < st->layout.nb_coupled_streams)
  423. ptr += align(coupled_size);
  424. else
  425. ptr += align(mono_size);
  426. ret = opus_decoder_ctl(dec, request, &tmp);
  427. if (ret != OPUS_OK) break;
  428. *value ^= tmp;
  429. }
  430. }
  431. break;
  432. case OPUS_RESET_STATE:
  433. {
  434. int s;
  435. for (s=0;s<st->layout.nb_streams;s++)
  436. {
  437. OpusDecoder *dec;
  438. dec = (OpusDecoder*)ptr;
  439. if (s < st->layout.nb_coupled_streams)
  440. ptr += align(coupled_size);
  441. else
  442. ptr += align(mono_size);
  443. ret = opus_decoder_ctl(dec, OPUS_RESET_STATE);
  444. if (ret != OPUS_OK)
  445. break;
  446. }
  447. }
  448. break;
  449. case OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST:
  450. {
  451. int s;
  452. opus_int32 stream_id;
  453. OpusDecoder **value;
  454. stream_id = va_arg(ap, opus_int32);
  455. if (stream_id<0 || stream_id >= st->layout.nb_streams)
  456. goto bad_arg;
  457. value = va_arg(ap, OpusDecoder**);
  458. if (!value)
  459. {
  460. goto bad_arg;
  461. }
  462. for (s=0;s<stream_id;s++)
  463. {
  464. if (s < st->layout.nb_coupled_streams)
  465. ptr += align(coupled_size);
  466. else
  467. ptr += align(mono_size);
  468. }
  469. *value = (OpusDecoder*)ptr;
  470. }
  471. break;
  472. case OPUS_SET_GAIN_REQUEST:
  473. case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
  474. {
  475. int s;
  476. /* This works for int32 params */
  477. opus_int32 value = va_arg(ap, opus_int32);
  478. for (s=0;s<st->layout.nb_streams;s++)
  479. {
  480. OpusDecoder *dec;
  481. dec = (OpusDecoder*)ptr;
  482. if (s < st->layout.nb_coupled_streams)
  483. ptr += align(coupled_size);
  484. else
  485. ptr += align(mono_size);
  486. ret = opus_decoder_ctl(dec, request, value);
  487. if (ret != OPUS_OK)
  488. break;
  489. }
  490. }
  491. break;
  492. default:
  493. ret = OPUS_UNIMPLEMENTED;
  494. break;
  495. }
  496. return ret;
  497. bad_arg:
  498. return OPUS_BAD_ARG;
  499. }
  500. int opus_multistream_decoder_ctl(OpusMSDecoder *st, int request, ...)
  501. {
  502. int ret;
  503. va_list ap;
  504. va_start(ap, request);
  505. ret = opus_multistream_decoder_ctl_va_list(st, request, ap);
  506. va_end(ap);
  507. return ret;
  508. }
  509. void opus_multistream_decoder_destroy(OpusMSDecoder *st)
  510. {
  511. opus_free(st);
  512. }