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