opus_custom_demo.c 6.4 KB

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