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 COPYRIGHT OWNER
20 OR 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;
47 OpusCustomMode *mode=NULL;
48 OpusCustomEncoder *enc;
49 OpusCustomDecoder *dec;
51 opus_int32 frame_size, channels, rate;
53 unsigned char data[MAX_PACKET];
55 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
62 if (argc != 9 && argc != 8 && argc != 7)
64 fprintf (stderr, "Usage: test_opus_custom <rate> <channels> <frame size> "
65 " <bytes per packet> [<complexity> [packet loss rate]] "
66 "<input> <output>\n");
70 rate = (opus_int32)atol(argv[1]);
71 channels = atoi(argv[2]);
72 frame_size = atoi(argv[3]);
73 mode = opus_custom_mode_create(rate, frame_size, NULL);
76 fprintf(stderr, "failed to create a mode\n");
80 bytes_per_packet = atoi(argv[4]);
81 if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)
83 fprintf (stderr, "bytes per packet must be between 0 and %d\n",
88 inFile = argv[argc-2];
89 fin = fopen(inFile, "rb");
92 fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
95 outFile = argv[argc-1];
96 fout = fopen(outFile, "wb+");
99 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));
112 dec = opus_custom_decoder_create(mode, channels, &err);
115 fprintf(stderr, "Failed to create the decoder: %s\n", opus_strerror(err));
120 opus_custom_decoder_ctl(dec, OPUS_GET_LOOKAHEAD(&skip));
124 complexity=atoi(argv[5]);
125 opus_custom_encoder_ctl(enc,OPUS_SET_COMPLEXITY(complexity));
128 in = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
129 out = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
134 err = fread(in, sizeof(short), frame_size*channels, fin);
137 len = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);
139 fprintf (stderr, "opus_custom_encode() failed: %s\n", opus_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 ret = opus_custom_decode(dec, NULL, len, out, frame_size);
170 ret = opus_custom_decode(dec, data, len, out, frame_size);
172 fprintf(stderr, "opus_custom_decode() failed: %s\n", opus_strerror(ret));
174 for (i=0;i<ret*channels;i++)
177 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
178 for (i=0;i<ret*channels;i++)
180 rmsd += (in[i]-out[i])*1.0*(in[i]-out[i]);
185 fwrite(out+skip*channels, sizeof(short), (ret-skip)*channels, fout);
190 opus_custom_encoder_destroy(enc);
191 opus_custom_decoder_destroy(dec);
194 opus_custom_mode_destroy(mode);
197 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
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");