opus_custom_demo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Written by Jean-Marc Valin */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "opus_custom.h"
  29. #include "arch.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <math.h>
  33. #include <string.h>
  34. #define MAX_PACKET 1275
  35. int main(int argc, char *argv[])
  36. {
  37. int err;
  38. char *inFile, *outFile;
  39. FILE *fin, *fout;
  40. OpusCustomMode *mode=NULL;
  41. OpusCustomEncoder *enc;
  42. OpusCustomDecoder *dec;
  43. int len;
  44. opus_int32 frame_size, channels, rate;
  45. int bytes_per_packet;
  46. unsigned char data[MAX_PACKET];
  47. int complexity;
  48. #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
  49. int i;
  50. double rmsd = 0;
  51. #endif
  52. int count = 0;
  53. opus_int32 skip;
  54. opus_int16 *in, *out;
  55. if (argc != 9 && argc != 8 && argc != 7)
  56. {
  57. fprintf (stderr, "Usage: test_opus_custom <rate> <channels> <frame size> "
  58. " <bytes per packet> [<complexity> [packet loss rate]] "
  59. "<input> <output>\n");
  60. return 1;
  61. }
  62. rate = (opus_int32)atol(argv[1]);
  63. channels = atoi(argv[2]);
  64. frame_size = atoi(argv[3]);
  65. mode = opus_custom_mode_create(rate, frame_size, NULL);
  66. if (mode == NULL)
  67. {
  68. fprintf(stderr, "failed to create a mode\n");
  69. return 1;
  70. }
  71. bytes_per_packet = atoi(argv[4]);
  72. if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)
  73. {
  74. fprintf (stderr, "bytes per packet must be between 0 and %d\n",
  75. MAX_PACKET);
  76. return 1;
  77. }
  78. inFile = argv[argc-2];
  79. fin = fopen(inFile, "rb");
  80. if (!fin)
  81. {
  82. fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
  83. return 1;
  84. }
  85. outFile = argv[argc-1];
  86. fout = fopen(outFile, "wb+");
  87. if (!fout)
  88. {
  89. fprintf (stderr, "Could not open output file %s\n", argv[argc-1]);
  90. fclose(fin);
  91. return 1;
  92. }
  93. enc = opus_custom_encoder_create(mode, channels, &err);
  94. if (err != 0)
  95. {
  96. fprintf(stderr, "Failed to create the encoder: %s\n", opus_strerror(err));
  97. fclose(fin);
  98. fclose(fout);
  99. return 1;
  100. }
  101. dec = opus_custom_decoder_create(mode, channels, &err);
  102. if (err != 0)
  103. {
  104. fprintf(stderr, "Failed to create the decoder: %s\n", opus_strerror(err));
  105. fclose(fin);
  106. fclose(fout);
  107. return 1;
  108. }
  109. opus_custom_decoder_ctl(dec, OPUS_GET_LOOKAHEAD(&skip));
  110. if (argc>7)
  111. {
  112. complexity=atoi(argv[5]);
  113. opus_custom_encoder_ctl(enc,OPUS_SET_COMPLEXITY(complexity));
  114. }
  115. in = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
  116. out = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
  117. while (!feof(fin))
  118. {
  119. int ret;
  120. err = fread(in, sizeof(short), frame_size*channels, fin);
  121. if (feof(fin))
  122. break;
  123. len = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);
  124. if (len <= 0)
  125. fprintf (stderr, "opus_custom_encode() failed: %s\n", opus_strerror(len));
  126. /* This is for simulating bit errors */
  127. #if 0
  128. int errors = 0;
  129. int eid = 0;
  130. /* This simulates random bit error */
  131. for (i=0;i<len*8;i++)
  132. {
  133. if (rand()%atoi(argv[8])==0)
  134. {
  135. if (i<64)
  136. {
  137. errors++;
  138. eid = i;
  139. }
  140. data[i/8] ^= 1<<(7-(i%8));
  141. }
  142. }
  143. if (errors == 1)
  144. data[eid/8] ^= 1<<(7-(eid%8));
  145. else if (errors%2 == 1)
  146. data[rand()%8] ^= 1<<rand()%8;
  147. #endif
  148. #if 1 /* Set to zero to use the encoder's output instead */
  149. /* This is to simulate packet loss */
  150. if (argc==9 && rand()%1000<atoi(argv[argc-3]))
  151. /*if (errors && (errors%2==0))*/
  152. ret = opus_custom_decode(dec, NULL, len, out, frame_size);
  153. else
  154. ret = opus_custom_decode(dec, data, len, out, frame_size);
  155. if (ret < 0)
  156. fprintf(stderr, "opus_custom_decode() failed: %s\n", opus_strerror(ret));
  157. #else
  158. for (i=0;i<ret*channels;i++)
  159. out[i] = in[i];
  160. #endif
  161. #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
  162. for (i=0;i<ret*channels;i++)
  163. {
  164. rmsd += (in[i]-out[i])*1.0*(in[i]-out[i]);
  165. /*out[i] -= in[i];*/
  166. }
  167. #endif
  168. count++;
  169. fwrite(out+skip*channels, sizeof(short), (ret-skip)*channels, fout);
  170. skip = 0;
  171. }
  172. PRINT_MIPS(stderr);
  173. opus_custom_encoder_destroy(enc);
  174. opus_custom_decoder_destroy(dec);
  175. fclose(fin);
  176. fclose(fout);
  177. opus_custom_mode_destroy(mode);
  178. free(in);
  179. free(out);
  180. #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
  181. if (rmsd > 0)
  182. {
  183. rmsd = sqrt(rmsd/(1.0*frame_size*channels*count));
  184. fprintf (stderr, "Error: encoder doesn't match decoder\n");
  185. fprintf (stderr, "RMS mismatch is %f\n", rmsd);
  186. return 1;
  187. } else {
  188. fprintf (stderr, "Encoder matches decoder!!\n");
  189. }
  190. #endif
  191. return 0;
  192. }