trivial_example.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (c) 2013 Jean-Marc Valin */
  2. /*
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  15. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /* This is meant to be a simple example of encoding and decoding audio
  24. using Opus. It should make it easy to understand how the Opus API
  25. works. For more information, see the full API documentation at:
  26. https://www.opus-codec.org/docs/ */
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <opus.h>
  31. #include <stdio.h>
  32. /*The frame size is hardcoded for this sample code but it doesn't have to be*/
  33. #define FRAME_SIZE 960
  34. #define SAMPLE_RATE 48000
  35. #define CHANNELS 2
  36. #define APPLICATION OPUS_APPLICATION_AUDIO
  37. #define BITRATE 64000
  38. #define MAX_FRAME_SIZE 6*960
  39. #define MAX_PACKET_SIZE (3*1276)
  40. int main(int argc, char **argv)
  41. {
  42. char *inFile;
  43. FILE *fin;
  44. char *outFile;
  45. FILE *fout;
  46. opus_int16 in[FRAME_SIZE*CHANNELS];
  47. opus_int16 out[MAX_FRAME_SIZE*CHANNELS];
  48. unsigned char cbits[MAX_PACKET_SIZE];
  49. int nbBytes;
  50. /*Holds the state of the encoder and decoder */
  51. OpusEncoder *encoder;
  52. OpusDecoder *decoder;
  53. int err;
  54. if (argc != 3)
  55. {
  56. fprintf(stderr, "usage: trivial_example input.pcm output.pcm\n");
  57. fprintf(stderr, "input and output are 16-bit little-endian raw files\n");
  58. return EXIT_FAILURE;
  59. }
  60. /*Create a new encoder state */
  61. encoder = opus_encoder_create(SAMPLE_RATE, CHANNELS, APPLICATION, &err);
  62. if (err<0)
  63. {
  64. fprintf(stderr, "failed to create an encoder: %s\n", opus_strerror(err));
  65. return EXIT_FAILURE;
  66. }
  67. /* Set the desired bit-rate. You can also set other parameters if needed.
  68. The Opus library is designed to have good defaults, so only set
  69. parameters you know you need. Doing otherwise is likely to result
  70. in worse quality, but better. */
  71. err = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE));
  72. if (err<0)
  73. {
  74. fprintf(stderr, "failed to set bitrate: %s\n", opus_strerror(err));
  75. return EXIT_FAILURE;
  76. }
  77. inFile = argv[1];
  78. fin = fopen(inFile, "rb");
  79. if (fin==NULL)
  80. {
  81. fprintf(stderr, "failed to open input file: %s\n", strerror(errno));
  82. return EXIT_FAILURE;
  83. }
  84. /* Create a new decoder state. */
  85. decoder = opus_decoder_create(SAMPLE_RATE, CHANNELS, &err);
  86. if (err<0)
  87. {
  88. fprintf(stderr, "failed to create decoder: %s\n", opus_strerror(err));
  89. return EXIT_FAILURE;
  90. }
  91. outFile = argv[2];
  92. fout = fopen(outFile, "wb");
  93. if (fout==NULL)
  94. {
  95. fprintf(stderr, "failed to open output file: %s\n", strerror(errno));
  96. return EXIT_FAILURE;
  97. }
  98. while (1)
  99. {
  100. int i;
  101. unsigned char pcm_bytes[MAX_FRAME_SIZE*CHANNELS*2];
  102. int frame_size;
  103. size_t samples;
  104. /* Read a 16 bits/sample audio frame. */
  105. samples = fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
  106. /* For simplicity, only read whole frames. In a real application,
  107. * we'd pad the final partial frame with zeroes, record the exact
  108. * duration, and trim the decoded audio to match.
  109. */
  110. if (samples != FRAME_SIZE)
  111. {
  112. break;
  113. }
  114. /* Convert from little-endian ordering. */
  115. for (i=0;i<CHANNELS*FRAME_SIZE;i++)
  116. {
  117. in[i]=pcm_bytes[2*i+1]<<8|pcm_bytes[2*i];
  118. }
  119. /* Encode the frame. */
  120. nbBytes = opus_encode(encoder, in, FRAME_SIZE, cbits, MAX_PACKET_SIZE);
  121. if (nbBytes<0)
  122. {
  123. fprintf(stderr, "encode failed: %s\n", opus_strerror(nbBytes));
  124. return EXIT_FAILURE;
  125. }
  126. /* Decode the data. In this example, frame_size will be constant because
  127. the encoder is using a constant frame size. However, that may not
  128. be the case for all encoders, so the decoder must always check
  129. the frame size returned. */
  130. frame_size = opus_decode(decoder, cbits, nbBytes, out, MAX_FRAME_SIZE, 0);
  131. if (frame_size<0)
  132. {
  133. fprintf(stderr, "decoder failed: %s\n", opus_strerror(frame_size));
  134. return EXIT_FAILURE;
  135. }
  136. /* Convert to little-endian ordering. */
  137. for(i=0;i<CHANNELS*frame_size;i++)
  138. {
  139. pcm_bytes[2*i]=out[i]&0xFF;
  140. pcm_bytes[2*i+1]=(out[i]>>8)&0xFF;
  141. }
  142. /* Write the decoded audio to file. */
  143. fwrite(pcm_bytes, sizeof(short), frame_size*CHANNELS, fout);
  144. }
  145. /*Destroy the encoder state*/
  146. opus_encoder_destroy(encoder);
  147. opus_decoder_destroy(decoder);
  148. fclose(fin);
  149. fclose(fout);
  150. return EXIT_SUCCESS;
  151. }