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 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #define MAX_PACKET 1275
46 int main(int argc, char *argv[])
49 char *inFile, *outFile;
55 celt_int32 frame_size, channels;
57 unsigned char data[MAX_PACKET];
60 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES))
67 if (argc != 9 && argc != 8 && argc != 7)
69 fprintf (stderr, "Usage: testcelt <rate> <channels> <frame size> "
70 " <bytes per packet> [<complexity> [packet loss rate]] "
71 "<input> <output>\n");
76 channels = atoi(argv[2]);
77 frame_size = atoi(argv[3]);
78 mode = celt_mode_create(rate, frame_size, NULL);
79 celt_mode_info(mode, CELT_GET_LOOKAHEAD, &skip);
83 fprintf(stderr, "failed to create a mode\n");
87 bytes_per_packet = atoi(argv[4]);
88 if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)
90 fprintf (stderr, "bytes per packet must be between 0 and %d\n",
95 inFile = argv[argc-2];
96 fin = fopen(inFile, "rb");
99 fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
102 outFile = argv[argc-1];
103 fout = fopen(outFile, "wb+");
106 fprintf (stderr, "Could not open output file %s\n", argv[argc-1]);
110 enc = celt_encoder_create_custom(mode, channels, &err);
113 fprintf(stderr, "Failed to create the encoder: %s\n", celt_strerror(err));
116 dec = celt_decoder_create_custom(mode, channels, &err);
119 fprintf(stderr, "Failed to create the decoder: %s\n", celt_strerror(err));
125 complexity=atoi(argv[5]);
126 celt_encoder_ctl(enc,CELT_SET_COMPLEXITY(complexity));
129 in = (celt_int16*)malloc(frame_size*channels*sizeof(celt_int16));
130 out = (celt_int16*)malloc(frame_size*channels*sizeof(celt_int16));
134 err = fread(in, sizeof(short), frame_size*channels, fin);
137 len = celt_encode(enc, in, frame_size, data, bytes_per_packet);
139 fprintf (stderr, "celt_encode() failed: %s\n", celt_strerror(len));
141 /* This is for simulating bit errors */
145 /* This simulates random bit error */
146 for (i=0;i<len*8;i++)
148 if (rand()%atoi(argv[8])==0)
155 data[i/8] ^= 1<<(7-(i%8));
159 data[eid/8] ^= 1<<(7-(eid%8));
160 else if (errors%2 == 1)
161 data[rand()%8] ^= 1<<rand()%8;
164 #if 1 /* Set to zero to use the encoder's output instead */
165 /* This is to simulate packet loss */
166 if (argc==9 && rand()%1000<atoi(argv[argc-3]))
167 /*if (errors && (errors%2==0))*/
168 err = celt_decode(dec, NULL, len, out, frame_size);
170 err = celt_decode(dec, data, len, out, frame_size);
172 fprintf(stderr, "celt_decode() failed: %s\n", celt_strerror(err));
174 for (i=0;i<frame_size*channels;i++)
177 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES))
178 for (i=0;i<frame_size*channels;i++)
180 rmsd += (in[i]-out[i])*1.0*(in[i]-out[i]);
185 fwrite(out+skip*channels, sizeof(short), (frame_size-skip)*channels, fout);
190 celt_encoder_destroy(enc);
191 celt_decoder_destroy(dec);
194 celt_mode_destroy(mode);
200 rmsd = sqrt(rmsd/(1.0*frame_size*channels*count));
201 fprintf (stderr, "Error: encoder doesn't match decoder\n");
202 fprintf (stderr, "RMS mismatch is %f\n", rmsd);
205 fprintf (stderr, "Encoder matches decoder!!\n");