test_opus_projection.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <assert.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include "float_cast.h"
  33. #include "opus.h"
  34. #include "test_opus_common.h"
  35. #include "opus_projection.h"
  36. #include "mathops.h"
  37. #include "../src/mapping_matrix.h"
  38. #include "mathops.h"
  39. #define BUFFER_SIZE 960
  40. #define MAX_DATA_BYTES 32768
  41. #define MAX_FRAME_SAMPLES 5760
  42. #define ERROR_TOLERANCE 1
  43. #define SIMPLE_MATRIX_SIZE 12
  44. #define SIMPLE_MATRIX_FRAME_SIZE 10
  45. #define SIMPLE_MATRIX_INPUT_SIZE 30
  46. #define SIMPLE_MATRIX_OUTPUT_SIZE 40
  47. int assert_is_equal(
  48. const opus_val16 *a, const opus_int16 *b, int size, opus_int16 tolerance)
  49. {
  50. int i;
  51. for (i = 0; i < size; i++)
  52. {
  53. #ifdef FIXED_POINT
  54. opus_int16 val = a[i];
  55. #else
  56. opus_int16 val = FLOAT2INT16(a[i]);
  57. #endif
  58. if (abs(val - b[i]) > tolerance)
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. int assert_is_equal_short(
  64. const opus_int16 *a, const opus_int16 *b, int size, opus_int16 tolerance)
  65. {
  66. int i;
  67. for (i = 0; i < size; i++)
  68. if (abs(a[i] - b[i]) > tolerance)
  69. return 1;
  70. return 0;
  71. }
  72. void test_simple_matrix(void)
  73. {
  74. const MappingMatrix simple_matrix_params = {4, 3, 0};
  75. const opus_int16 simple_matrix_data[SIMPLE_MATRIX_SIZE] = {0, 32767, 0, 0, 32767, 0, 0, 0, 0, 0, 0, 32767};
  76. const opus_int16 input_int16[SIMPLE_MATRIX_INPUT_SIZE] = {
  77. 32767, 0, -32768, 29491, -3277, -29491, 26214, -6554, -26214, 22938, -9830,
  78. -22938, 19661, -13107, -19661, 16384, -16384, -16384, 13107, -19661, -13107,
  79. 9830, -22938, -9830, 6554, -26214, -6554, 3277, -29491, -3277};
  80. const opus_int16 expected_output_int16[SIMPLE_MATRIX_OUTPUT_SIZE] = {
  81. 0, 32767, 0, -32768, -3277, 29491, 0, -29491, -6554, 26214, 0, -26214,
  82. -9830, 22938, 0, -22938, -13107, 19661, 0, -19661, -16384, 16384, 0, -16384,
  83. -19661, 13107, 0, -13107, -22938, 9830, 0, -9830, -26214, 6554, 0, -6554,
  84. -29491, 3277, 0, -3277};
  85. int i, ret;
  86. opus_int32 simple_matrix_size;
  87. opus_val16 *input_val16;
  88. opus_val16 *output_val16;
  89. opus_int16 *output_int16;
  90. MappingMatrix *simple_matrix;
  91. /* Allocate input/output buffers. */
  92. input_val16 = (opus_val16 *)opus_alloc(sizeof(opus_val16) * SIMPLE_MATRIX_INPUT_SIZE);
  93. output_int16 = (opus_int16 *)opus_alloc(sizeof(opus_int16) * SIMPLE_MATRIX_OUTPUT_SIZE);
  94. output_val16 = (opus_val16 *)opus_alloc(sizeof(opus_val16) * SIMPLE_MATRIX_OUTPUT_SIZE);
  95. /* Initialize matrix */
  96. simple_matrix_size = mapping_matrix_get_size(simple_matrix_params.rows,
  97. simple_matrix_params.cols);
  98. if (!simple_matrix_size)
  99. test_failed();
  100. simple_matrix = (MappingMatrix *)opus_alloc(simple_matrix_size);
  101. mapping_matrix_init(simple_matrix, simple_matrix_params.rows,
  102. simple_matrix_params.cols, simple_matrix_params.gain, simple_matrix_data,
  103. sizeof(simple_matrix_data));
  104. /* Copy inputs. */
  105. for (i = 0; i < SIMPLE_MATRIX_INPUT_SIZE; i++)
  106. {
  107. #ifdef FIXED_POINT
  108. input_val16[i] = input_int16[i];
  109. #else
  110. input_val16[i] = (1/32768.f)*input_int16[i];
  111. #endif
  112. }
  113. /* _in_short */
  114. for (i = 0; i < SIMPLE_MATRIX_OUTPUT_SIZE; i++)
  115. output_val16[i] = 0;
  116. for (i = 0; i < simple_matrix->rows; i++)
  117. {
  118. mapping_matrix_multiply_channel_in_short(simple_matrix,
  119. input_int16, simple_matrix->cols, &output_val16[i], i,
  120. simple_matrix->rows, SIMPLE_MATRIX_FRAME_SIZE);
  121. }
  122. ret = assert_is_equal(output_val16, expected_output_int16, SIMPLE_MATRIX_OUTPUT_SIZE, ERROR_TOLERANCE);
  123. if (ret)
  124. test_failed();
  125. /* _out_short */
  126. for (i = 0; i < SIMPLE_MATRIX_OUTPUT_SIZE; i++)
  127. output_int16[i] = 0;
  128. for (i = 0; i < simple_matrix->cols; i++)
  129. {
  130. mapping_matrix_multiply_channel_out_short(simple_matrix,
  131. &input_val16[i], i, simple_matrix->cols, output_int16,
  132. simple_matrix->rows, SIMPLE_MATRIX_FRAME_SIZE);
  133. }
  134. ret = assert_is_equal_short(output_int16, expected_output_int16, SIMPLE_MATRIX_OUTPUT_SIZE, ERROR_TOLERANCE);
  135. if (ret)
  136. test_failed();
  137. #if !defined(DISABLE_FLOAT_API) && !defined(FIXED_POINT)
  138. /* _in_float */
  139. for (i = 0; i < SIMPLE_MATRIX_OUTPUT_SIZE; i++)
  140. output_val16[i] = 0;
  141. for (i = 0; i < simple_matrix->rows; i++)
  142. {
  143. mapping_matrix_multiply_channel_in_float(simple_matrix,
  144. input_val16, simple_matrix->cols, &output_val16[i], i,
  145. simple_matrix->rows, SIMPLE_MATRIX_FRAME_SIZE);
  146. }
  147. ret = assert_is_equal(output_val16, expected_output_int16, SIMPLE_MATRIX_OUTPUT_SIZE, ERROR_TOLERANCE);
  148. if (ret)
  149. test_failed();
  150. /* _out_float */
  151. for (i = 0; i < SIMPLE_MATRIX_OUTPUT_SIZE; i++)
  152. output_val16[i] = 0;
  153. for (i = 0; i < simple_matrix->cols; i++)
  154. {
  155. mapping_matrix_multiply_channel_out_float(simple_matrix,
  156. &input_val16[i], i, simple_matrix->cols, output_val16,
  157. simple_matrix->rows, SIMPLE_MATRIX_FRAME_SIZE);
  158. }
  159. ret = assert_is_equal(output_val16, expected_output_int16, SIMPLE_MATRIX_OUTPUT_SIZE, ERROR_TOLERANCE);
  160. if (ret)
  161. test_failed();
  162. #endif
  163. opus_free(input_val16);
  164. opus_free(output_int16);
  165. opus_free(output_val16);
  166. opus_free(simple_matrix);
  167. }
  168. void test_creation_arguments(const int channels, const int mapping_family)
  169. {
  170. int streams;
  171. int coupled_streams;
  172. int enc_error;
  173. int dec_error;
  174. int ret;
  175. OpusProjectionEncoder *st_enc = NULL;
  176. OpusProjectionDecoder *st_dec = NULL;
  177. const opus_int32 Fs = 48000;
  178. const int application = OPUS_APPLICATION_AUDIO;
  179. int order_plus_one = (int)floor(sqrt((float)channels));
  180. int nondiegetic_channels = channels - order_plus_one * order_plus_one;
  181. int is_channels_valid = 0;
  182. int is_projection_valid = 0;
  183. st_enc = opus_projection_ambisonics_encoder_create(Fs, channels,
  184. mapping_family, &streams, &coupled_streams, application, &enc_error);
  185. if (st_enc != NULL)
  186. {
  187. opus_int32 matrix_size;
  188. unsigned char *matrix;
  189. ret = opus_projection_encoder_ctl(st_enc,
  190. OPUS_PROJECTION_GET_DEMIXING_MATRIX_SIZE_REQUEST, &matrix_size);
  191. if (ret != OPUS_OK || !matrix_size)
  192. test_failed();
  193. matrix = (unsigned char *)opus_alloc(matrix_size);
  194. ret = opus_projection_encoder_ctl(st_enc,
  195. OPUS_PROJECTION_GET_DEMIXING_MATRIX_REQUEST, matrix, matrix_size);
  196. opus_projection_encoder_destroy(st_enc);
  197. st_dec = opus_projection_decoder_create(Fs, channels, streams,
  198. coupled_streams, matrix, matrix_size, &dec_error);
  199. if (st_dec != NULL)
  200. {
  201. opus_projection_decoder_destroy(st_dec);
  202. }
  203. opus_free(matrix);
  204. }
  205. is_channels_valid = (order_plus_one >= 2 && order_plus_one <= 4) &&
  206. (nondiegetic_channels == 0 || nondiegetic_channels == 2);
  207. is_projection_valid = (enc_error == OPUS_OK && dec_error == OPUS_OK);
  208. if (is_channels_valid ^ is_projection_valid)
  209. {
  210. fprintf(stderr, "Channels: %d, Family: %d\n", channels, mapping_family);
  211. fprintf(stderr, "Order+1: %d, Non-diegetic Channels: %d\n",
  212. order_plus_one, nondiegetic_channels);
  213. fprintf(stderr, "Streams: %d, Coupled Streams: %d\n",
  214. streams, coupled_streams);
  215. test_failed();
  216. }
  217. }
  218. void generate_music(short *buf, opus_int32 len, opus_int32 channels)
  219. {
  220. opus_int32 i,j,k;
  221. opus_int32 *a,*b,*c,*d;
  222. a = (opus_int32 *)malloc(sizeof(opus_int32) * channels);
  223. b = (opus_int32 *)malloc(sizeof(opus_int32) * channels);
  224. c = (opus_int32 *)malloc(sizeof(opus_int32) * channels);
  225. d = (opus_int32 *)malloc(sizeof(opus_int32) * channels);
  226. memset(a, 0, sizeof(opus_int32) * channels);
  227. memset(b, 0, sizeof(opus_int32) * channels);
  228. memset(c, 0, sizeof(opus_int32) * channels);
  229. memset(d, 0, sizeof(opus_int32) * channels);
  230. j=0;
  231. for(i=0;i<len;i++)
  232. {
  233. for(k=0;k<channels;k++)
  234. {
  235. opus_uint32 r;
  236. opus_int32 v;
  237. v=(((j*((j>>12)^((j>>10|j>>12)&26&j>>7)))&128)+128)<<15;
  238. r=fast_rand();v+=r&65535;v-=r>>16;
  239. b[k]=v-a[k]+((b[k]*61+32)>>6);a[k]=v;
  240. c[k]=(30*(c[k]+b[k]+d[k])+32)>>6;d[k]=b[k];
  241. v=(c[k]+128)>>8;
  242. buf[i*channels+k]=v>32767?32767:(v<-32768?-32768:v);
  243. if(i%6==0)j++;
  244. }
  245. }
  246. free(a);
  247. free(b);
  248. free(c);
  249. free(d);
  250. }
  251. void test_encode_decode(opus_int32 bitrate, opus_int32 channels,
  252. const int mapping_family)
  253. {
  254. const opus_int32 Fs = 48000;
  255. const int application = OPUS_APPLICATION_AUDIO;
  256. OpusProjectionEncoder *st_enc;
  257. OpusProjectionDecoder *st_dec;
  258. int streams;
  259. int coupled;
  260. int error;
  261. short *buffer_in;
  262. short *buffer_out;
  263. unsigned char data[MAX_DATA_BYTES] = { 0 };
  264. int len;
  265. int out_samples;
  266. opus_int32 matrix_size = 0;
  267. unsigned char *matrix = NULL;
  268. buffer_in = (short *)malloc(sizeof(short) * BUFFER_SIZE * channels);
  269. buffer_out = (short *)malloc(sizeof(short) * BUFFER_SIZE * channels);
  270. st_enc = opus_projection_ambisonics_encoder_create(Fs, channels,
  271. mapping_family, &streams, &coupled, application, &error);
  272. if (error != OPUS_OK) {
  273. fprintf(stderr,
  274. "Couldn\'t create encoder with %d channels and mapping family %d.\n",
  275. channels, mapping_family);
  276. free(buffer_in);
  277. free(buffer_out);
  278. test_failed();
  279. }
  280. error = opus_projection_encoder_ctl(st_enc,
  281. OPUS_SET_BITRATE(bitrate * 1000 * (streams + coupled)));
  282. if (error != OPUS_OK)
  283. {
  284. goto bad_cleanup;
  285. }
  286. error = opus_projection_encoder_ctl(st_enc,
  287. OPUS_PROJECTION_GET_DEMIXING_MATRIX_SIZE_REQUEST, &matrix_size);
  288. if (error != OPUS_OK || !matrix_size)
  289. {
  290. goto bad_cleanup;
  291. }
  292. matrix = (unsigned char *)opus_alloc(matrix_size);
  293. error = opus_projection_encoder_ctl(st_enc,
  294. OPUS_PROJECTION_GET_DEMIXING_MATRIX_REQUEST, matrix, matrix_size);
  295. st_dec = opus_projection_decoder_create(Fs, channels, streams, coupled,
  296. matrix, matrix_size, &error);
  297. opus_free(matrix);
  298. if (error != OPUS_OK) {
  299. fprintf(stderr,
  300. "Couldn\'t create decoder with %d channels, %d streams "
  301. "and %d coupled streams.\n", channels, streams, coupled);
  302. goto bad_cleanup;
  303. }
  304. generate_music(buffer_in, BUFFER_SIZE, channels);
  305. len = opus_projection_encode(
  306. st_enc, buffer_in, BUFFER_SIZE, data, MAX_DATA_BYTES);
  307. if(len<0 || len>MAX_DATA_BYTES) {
  308. fprintf(stderr,"opus_encode() returned %d\n", len);
  309. goto bad_cleanup;
  310. }
  311. out_samples = opus_projection_decode(
  312. st_dec, data, len, buffer_out, MAX_FRAME_SAMPLES, 0);
  313. if(out_samples!=BUFFER_SIZE) {
  314. fprintf(stderr,"opus_decode() returned %d\n", out_samples);
  315. goto bad_cleanup;
  316. }
  317. opus_projection_decoder_destroy(st_dec);
  318. opus_projection_encoder_destroy(st_enc);
  319. free(buffer_in);
  320. free(buffer_out);
  321. return;
  322. bad_cleanup:
  323. free(buffer_in);
  324. free(buffer_out);
  325. test_failed();
  326. }
  327. int main(int _argc, char **_argv)
  328. {
  329. unsigned int i;
  330. (void)_argc;
  331. (void)_argv;
  332. /* Test simple matrix multiplication routines. */
  333. test_simple_matrix();
  334. /* Test full range of channels in creation arguments. */
  335. for (i = 0; i < 255; i++)
  336. test_creation_arguments(i, 3);
  337. /* Test encode/decode pipeline. */
  338. test_encode_decode(64 * 18, 18, 3);
  339. fprintf(stderr, "All projection tests passed.\n");
  340. return 0;
  341. }