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