xref: /aosp_15_r20/external/libopus/doc/trivial_example.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
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, "rb");
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, "wb");
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       size_t samples;
117 
118       /* Read a 16 bits/sample audio frame. */
119       samples = fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
120 
121       /* For simplicity, only read whole frames. In a real application,
122        * we'd pad the final partial frame with zeroes, record the exact
123        * duration, and trim the decoded audio to match.
124        */
125       if (samples != FRAME_SIZE)
126       {
127          break;
128       }
129 
130       /* Convert from little-endian ordering. */
131       for (i=0;i<CHANNELS*FRAME_SIZE;i++)
132       {
133          in[i]=pcm_bytes[2*i+1]<<8|pcm_bytes[2*i];
134       }
135 
136       /* Encode the frame. */
137       nbBytes = opus_encode(encoder, in, FRAME_SIZE, cbits, MAX_PACKET_SIZE);
138       if (nbBytes<0)
139       {
140          fprintf(stderr, "encode failed: %s\n", opus_strerror(nbBytes));
141          return EXIT_FAILURE;
142       }
143 
144 
145       /* Decode the data. In this example, frame_size will be constant because
146          the encoder is using a constant frame size. However, that may not
147          be the case for all encoders, so the decoder must always check
148          the frame size returned. */
149       frame_size = opus_decode(decoder, cbits, nbBytes, out, MAX_FRAME_SIZE, 0);
150       if (frame_size<0)
151       {
152          fprintf(stderr, "decoder failed: %s\n", opus_strerror(frame_size));
153          return EXIT_FAILURE;
154       }
155 
156       /* Convert to little-endian ordering. */
157       for(i=0;i<CHANNELS*frame_size;i++)
158       {
159          pcm_bytes[2*i]=out[i]&0xFF;
160          pcm_bytes[2*i+1]=(out[i]>>8)&0xFF;
161       }
162       /* Write the decoded audio to file. */
163       fwrite(pcm_bytes, sizeof(short), frame_size*CHANNELS, fout);
164    }
165    /*Destroy the encoder state*/
166    opus_encoder_destroy(encoder);
167    opus_decoder_destroy(decoder);
168    fclose(fin);
169    fclose(fout);
170    return EXIT_SUCCESS;
171 }
172