1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "opus_custom.h"
40 #define MAX_PACKET 1275
42 int main(int argc, char *argv[])
45 char *inFile, *outFile;
51 opus_int32 frame_size, channels;
53 unsigned char data[MAX_PACKET];
56 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
63 if (argc != 9 && argc != 8 && argc != 7)
65 fprintf (stderr, "Usage: testcelt <rate> <channels> <frame size> "
66 " <bytes per packet> [<complexity> [packet loss rate]] "
67 "<input> <output>\n");
72 channels = atoi(argv[2]);
73 frame_size = atoi(argv[3]);
74 mode = opus_custom_mode_create(rate, frame_size, NULL);
77 fprintf(stderr, "failed to create a mode\n");
81 bytes_per_packet = atoi(argv[4]);
82 if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)
84 fprintf (stderr, "bytes per packet must be between 0 and %d\n",
89 inFile = argv[argc-2];
90 fin = fopen(inFile, "rb");
93 fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
96 outFile = argv[argc-1];
97 fout = fopen(outFile, "wb+");
100 fprintf (stderr, "Could not open output file %s\n", argv[argc-1]);
104 enc = opus_custom_encoder_create(mode, channels, &err);
107 fprintf(stderr, "Failed to create the encoder: %s\n", opus_strerror(err));
110 dec = opus_custom_decoder_create(mode, channels, &err);
113 fprintf(stderr, "Failed to create the decoder: %s\n", opus_strerror(err));
116 opus_custom_decoder_ctl(dec, CELT_GET_LOOKAHEAD(&skip));
120 complexity=atoi(argv[5]);
121 opus_custom_encoder_ctl(enc,CELT_SET_COMPLEXITY(complexity));
124 in = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
125 out = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
130 err = fread(in, sizeof(short), frame_size*channels, fin);
133 len = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);
135 fprintf (stderr, "opus_custom_encode() failed: %s\n", opus_strerror(len));
137 /* This is for simulating bit errors */
141 /* This simulates random bit error */
142 for (i=0;i<len*8;i++)
144 if (rand()%atoi(argv[8])==0)
151 data[i/8] ^= 1<<(7-(i%8));
155 data[eid/8] ^= 1<<(7-(eid%8));
156 else if (errors%2 == 1)
157 data[rand()%8] ^= 1<<rand()%8;
160 #if 1 /* Set to zero to use the encoder's output instead */
161 /* This is to simulate packet loss */
162 if (argc==9 && rand()%1000<atoi(argv[argc-3]))
163 /*if (errors && (errors%2==0))*/
164 ret = opus_custom_decode(dec, NULL, len, out, frame_size);
166 ret = opus_custom_decode(dec, data, len, out, frame_size);
168 fprintf(stderr, "opus_custom_decode() failed: %s\n", opus_strerror(ret));
170 for (i=0;i<ret*channels;i++)
173 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
174 for (i=0;i<ret*channels;i++)
176 rmsd += (in[i]-out[i])*1.0*(in[i]-out[i]);
181 fwrite(out+skip*channels, sizeof(short), (ret-skip)*channels, fout);
186 opus_custom_encoder_destroy(enc);
187 opus_custom_decoder_destroy(dec);
190 opus_custom_mode_destroy(mode);
193 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
196 rmsd = sqrt(rmsd/(1.0*frame_size*channels*count));
197 fprintf (stderr, "Error: encoder doesn't match decoder\n");
198 fprintf (stderr, "RMS mismatch is %f\n", rmsd);
201 fprintf (stderr, "Encoder matches decoder!!\n");