opus_projection_decoder.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright (c) 2017 Google Inc.
  2. Written by Andrew Allen */
  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 "mathops.h"
  28. #include "os_support.h"
  29. #include "opus_private.h"
  30. #include "opus_defines.h"
  31. #include "opus_projection.h"
  32. #include "opus_multistream.h"
  33. #include "mapping_matrix.h"
  34. #include "stack_alloc.h"
  35. struct OpusProjectionDecoder
  36. {
  37. opus_int32 demixing_matrix_size_in_bytes;
  38. /* Encoder states go here */
  39. };
  40. #if !defined(DISABLE_FLOAT_API)
  41. static void opus_projection_copy_channel_out_float(
  42. void *dst,
  43. int dst_stride,
  44. int dst_channel,
  45. const opus_val16 *src,
  46. int src_stride,
  47. int frame_size,
  48. void *user_data)
  49. {
  50. float *float_dst;
  51. const MappingMatrix *matrix;
  52. float_dst = (float *)dst;
  53. matrix = (const MappingMatrix *)user_data;
  54. if (dst_channel == 0)
  55. OPUS_CLEAR(float_dst, frame_size * dst_stride);
  56. if (src != NULL)
  57. mapping_matrix_multiply_channel_out_float(matrix, src, dst_channel,
  58. src_stride, float_dst, dst_stride, frame_size);
  59. }
  60. #endif
  61. static void opus_projection_copy_channel_out_short(
  62. void *dst,
  63. int dst_stride,
  64. int dst_channel,
  65. const opus_val16 *src,
  66. int src_stride,
  67. int frame_size,
  68. void *user_data)
  69. {
  70. opus_int16 *short_dst;
  71. const MappingMatrix *matrix;
  72. short_dst = (opus_int16 *)dst;
  73. matrix = (const MappingMatrix *)user_data;
  74. if (dst_channel == 0)
  75. OPUS_CLEAR(short_dst, frame_size * dst_stride);
  76. if (src != NULL)
  77. mapping_matrix_multiply_channel_out_short(matrix, src, dst_channel,
  78. src_stride, short_dst, dst_stride, frame_size);
  79. }
  80. static MappingMatrix *get_dec_demixing_matrix(OpusProjectionDecoder *st)
  81. {
  82. /* void* cast avoids clang -Wcast-align warning */
  83. return (MappingMatrix*)(void*)((char*)st +
  84. align(sizeof(OpusProjectionDecoder)));
  85. }
  86. static OpusMSDecoder *get_multistream_decoder(OpusProjectionDecoder *st)
  87. {
  88. /* void* cast avoids clang -Wcast-align warning */
  89. return (OpusMSDecoder*)(void*)((char*)st +
  90. align(sizeof(OpusProjectionDecoder) +
  91. st->demixing_matrix_size_in_bytes));
  92. }
  93. opus_int32 opus_projection_decoder_get_size(int channels, int streams,
  94. int coupled_streams)
  95. {
  96. opus_int32 matrix_size;
  97. opus_int32 decoder_size;
  98. matrix_size =
  99. mapping_matrix_get_size(streams + coupled_streams, channels);
  100. if (!matrix_size)
  101. return 0;
  102. decoder_size = opus_multistream_decoder_get_size(streams, coupled_streams);
  103. if (!decoder_size)
  104. return 0;
  105. return align(sizeof(OpusProjectionDecoder)) + matrix_size + decoder_size;
  106. }
  107. int opus_projection_decoder_init(OpusProjectionDecoder *st, opus_int32 Fs,
  108. int channels, int streams, int coupled_streams,
  109. unsigned char *demixing_matrix, opus_int32 demixing_matrix_size)
  110. {
  111. int nb_input_streams;
  112. opus_int32 expected_matrix_size;
  113. int i, ret;
  114. unsigned char mapping[255];
  115. VARDECL(opus_int16, buf);
  116. ALLOC_STACK;
  117. /* Verify supplied matrix size. */
  118. nb_input_streams = streams + coupled_streams;
  119. expected_matrix_size = nb_input_streams * channels * sizeof(opus_int16);
  120. if (expected_matrix_size != demixing_matrix_size)
  121. {
  122. RESTORE_STACK;
  123. return OPUS_BAD_ARG;
  124. }
  125. /* Convert demixing matrix input into internal format. */
  126. ALLOC(buf, nb_input_streams * channels, opus_int16);
  127. for (i = 0; i < nb_input_streams * channels; i++)
  128. {
  129. int s = demixing_matrix[2*i + 1] << 8 | demixing_matrix[2*i];
  130. s = ((s & 0xFFFF) ^ 0x8000) - 0x8000;
  131. buf[i] = (opus_int16)s;
  132. }
  133. /* Assign demixing matrix. */
  134. st->demixing_matrix_size_in_bytes =
  135. mapping_matrix_get_size(channels, nb_input_streams);
  136. if (!st->demixing_matrix_size_in_bytes)
  137. {
  138. RESTORE_STACK;
  139. return OPUS_BAD_ARG;
  140. }
  141. mapping_matrix_init(get_dec_demixing_matrix(st), channels, nb_input_streams, 0,
  142. buf, demixing_matrix_size);
  143. /* Set trivial mapping so each input channel pairs with a matrix column. */
  144. for (i = 0; i < channels; i++)
  145. mapping[i] = i;
  146. ret = opus_multistream_decoder_init(
  147. get_multistream_decoder(st), Fs, channels, streams, coupled_streams, mapping);
  148. RESTORE_STACK;
  149. return ret;
  150. }
  151. OpusProjectionDecoder *opus_projection_decoder_create(
  152. opus_int32 Fs, int channels, int streams, int coupled_streams,
  153. unsigned char *demixing_matrix, opus_int32 demixing_matrix_size, int *error)
  154. {
  155. int size;
  156. int ret;
  157. OpusProjectionDecoder *st;
  158. /* Allocate space for the projection decoder. */
  159. size = opus_projection_decoder_get_size(channels, streams, coupled_streams);
  160. if (!size) {
  161. if (error)
  162. *error = OPUS_ALLOC_FAIL;
  163. return NULL;
  164. }
  165. st = (OpusProjectionDecoder *)opus_alloc(size);
  166. if (!st)
  167. {
  168. if (error)
  169. *error = OPUS_ALLOC_FAIL;
  170. return NULL;
  171. }
  172. /* Initialize projection decoder with provided settings. */
  173. ret = opus_projection_decoder_init(st, Fs, channels, streams, coupled_streams,
  174. demixing_matrix, demixing_matrix_size);
  175. if (ret != OPUS_OK)
  176. {
  177. opus_free(st);
  178. st = NULL;
  179. }
  180. if (error)
  181. *error = ret;
  182. return st;
  183. }
  184. #ifdef FIXED_POINT
  185. int opus_projection_decode(OpusProjectionDecoder *st, const unsigned char *data,
  186. opus_int32 len, opus_int16 *pcm, int frame_size,
  187. int decode_fec)
  188. {
  189. return opus_multistream_decode_native(get_multistream_decoder(st), data, len,
  190. pcm, opus_projection_copy_channel_out_short, frame_size, decode_fec, 0,
  191. get_dec_demixing_matrix(st));
  192. }
  193. #else
  194. int opus_projection_decode(OpusProjectionDecoder *st, const unsigned char *data,
  195. opus_int32 len, opus_int16 *pcm, int frame_size,
  196. int decode_fec)
  197. {
  198. return opus_multistream_decode_native(get_multistream_decoder(st), data, len,
  199. pcm, opus_projection_copy_channel_out_short, frame_size, decode_fec, 1,
  200. get_dec_demixing_matrix(st));
  201. }
  202. #endif
  203. #ifndef DISABLE_FLOAT_API
  204. int opus_projection_decode_float(OpusProjectionDecoder *st, const unsigned char *data,
  205. opus_int32 len, float *pcm, int frame_size, int decode_fec)
  206. {
  207. return opus_multistream_decode_native(get_multistream_decoder(st), data, len,
  208. pcm, opus_projection_copy_channel_out_float, frame_size, decode_fec, 0,
  209. get_dec_demixing_matrix(st));
  210. }
  211. #endif
  212. int opus_projection_decoder_ctl(OpusProjectionDecoder *st, int request, ...)
  213. {
  214. va_list ap;
  215. int ret = OPUS_OK;
  216. va_start(ap, request);
  217. ret = opus_multistream_decoder_ctl_va_list(get_multistream_decoder(st),
  218. request, ap);
  219. va_end(ap);
  220. return ret;
  221. }
  222. void opus_projection_decoder_destroy(OpusProjectionDecoder *st)
  223. {
  224. opus_free(st);
  225. }