cannam@154
|
1 /* Copyright (c) 2007-2008 CSIRO
|
cannam@154
|
2 Copyright (c) 2007-2009 Xiph.Org Foundation
|
cannam@154
|
3 Written by Jean-Marc Valin */
|
cannam@154
|
4 /*
|
cannam@154
|
5 Redistribution and use in source and binary forms, with or without
|
cannam@154
|
6 modification, are permitted provided that the following conditions
|
cannam@154
|
7 are met:
|
cannam@154
|
8
|
cannam@154
|
9 - Redistributions of source code must retain the above copyright
|
cannam@154
|
10 notice, this list of conditions and the following disclaimer.
|
cannam@154
|
11
|
cannam@154
|
12 - Redistributions in binary form must reproduce the above copyright
|
cannam@154
|
13 notice, this list of conditions and the following disclaimer in the
|
cannam@154
|
14 documentation and/or other materials provided with the distribution.
|
cannam@154
|
15
|
cannam@154
|
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
cannam@154
|
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
cannam@154
|
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
cannam@154
|
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
cannam@154
|
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
cannam@154
|
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
cannam@154
|
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
cannam@154
|
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
cannam@154
|
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
cannam@154
|
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
cannam@154
|
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
cannam@154
|
27 */
|
cannam@154
|
28
|
cannam@154
|
29 #ifdef HAVE_CONFIG_H
|
cannam@154
|
30 #include "config.h"
|
cannam@154
|
31 #endif
|
cannam@154
|
32
|
cannam@154
|
33 #include "opus_custom.h"
|
cannam@154
|
34 #include "arch.h"
|
cannam@154
|
35 #include <stdio.h>
|
cannam@154
|
36 #include <stdlib.h>
|
cannam@154
|
37 #include <math.h>
|
cannam@154
|
38 #include <string.h>
|
cannam@154
|
39
|
cannam@154
|
40 #define MAX_PACKET 1275
|
cannam@154
|
41
|
cannam@154
|
42 int main(int argc, char *argv[])
|
cannam@154
|
43 {
|
cannam@154
|
44 int err;
|
cannam@154
|
45 char *inFile, *outFile;
|
cannam@154
|
46 FILE *fin, *fout;
|
cannam@154
|
47 OpusCustomMode *mode=NULL;
|
cannam@154
|
48 OpusCustomEncoder *enc;
|
cannam@154
|
49 OpusCustomDecoder *dec;
|
cannam@154
|
50 int len;
|
cannam@154
|
51 opus_int32 frame_size, channels, rate;
|
cannam@154
|
52 int bytes_per_packet;
|
cannam@154
|
53 unsigned char data[MAX_PACKET];
|
cannam@154
|
54 int complexity;
|
cannam@154
|
55 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
|
cannam@154
|
56 int i;
|
cannam@154
|
57 double rmsd = 0;
|
cannam@154
|
58 #endif
|
cannam@154
|
59 int count = 0;
|
cannam@154
|
60 opus_int32 skip;
|
cannam@154
|
61 opus_int16 *in, *out;
|
cannam@154
|
62 if (argc != 9 && argc != 8 && argc != 7)
|
cannam@154
|
63 {
|
cannam@154
|
64 fprintf (stderr, "Usage: test_opus_custom <rate> <channels> <frame size> "
|
cannam@154
|
65 " <bytes per packet> [<complexity> [packet loss rate]] "
|
cannam@154
|
66 "<input> <output>\n");
|
cannam@154
|
67 return 1;
|
cannam@154
|
68 }
|
cannam@154
|
69
|
cannam@154
|
70 rate = (opus_int32)atol(argv[1]);
|
cannam@154
|
71 channels = atoi(argv[2]);
|
cannam@154
|
72 frame_size = atoi(argv[3]);
|
cannam@154
|
73 mode = opus_custom_mode_create(rate, frame_size, NULL);
|
cannam@154
|
74 if (mode == NULL)
|
cannam@154
|
75 {
|
cannam@154
|
76 fprintf(stderr, "failed to create a mode\n");
|
cannam@154
|
77 return 1;
|
cannam@154
|
78 }
|
cannam@154
|
79
|
cannam@154
|
80 bytes_per_packet = atoi(argv[4]);
|
cannam@154
|
81 if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)
|
cannam@154
|
82 {
|
cannam@154
|
83 fprintf (stderr, "bytes per packet must be between 0 and %d\n",
|
cannam@154
|
84 MAX_PACKET);
|
cannam@154
|
85 return 1;
|
cannam@154
|
86 }
|
cannam@154
|
87
|
cannam@154
|
88 inFile = argv[argc-2];
|
cannam@154
|
89 fin = fopen(inFile, "rb");
|
cannam@154
|
90 if (!fin)
|
cannam@154
|
91 {
|
cannam@154
|
92 fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
|
cannam@154
|
93 return 1;
|
cannam@154
|
94 }
|
cannam@154
|
95 outFile = argv[argc-1];
|
cannam@154
|
96 fout = fopen(outFile, "wb+");
|
cannam@154
|
97 if (!fout)
|
cannam@154
|
98 {
|
cannam@154
|
99 fprintf (stderr, "Could not open output file %s\n", argv[argc-1]);
|
cannam@154
|
100 fclose(fin);
|
cannam@154
|
101 return 1;
|
cannam@154
|
102 }
|
cannam@154
|
103
|
cannam@154
|
104 enc = opus_custom_encoder_create(mode, channels, &err);
|
cannam@154
|
105 if (err != 0)
|
cannam@154
|
106 {
|
cannam@154
|
107 fprintf(stderr, "Failed to create the encoder: %s\n", opus_strerror(err));
|
cannam@154
|
108 fclose(fin);
|
cannam@154
|
109 fclose(fout);
|
cannam@154
|
110 return 1;
|
cannam@154
|
111 }
|
cannam@154
|
112 dec = opus_custom_decoder_create(mode, channels, &err);
|
cannam@154
|
113 if (err != 0)
|
cannam@154
|
114 {
|
cannam@154
|
115 fprintf(stderr, "Failed to create the decoder: %s\n", opus_strerror(err));
|
cannam@154
|
116 fclose(fin);
|
cannam@154
|
117 fclose(fout);
|
cannam@154
|
118 return 1;
|
cannam@154
|
119 }
|
cannam@154
|
120 opus_custom_decoder_ctl(dec, OPUS_GET_LOOKAHEAD(&skip));
|
cannam@154
|
121
|
cannam@154
|
122 if (argc>7)
|
cannam@154
|
123 {
|
cannam@154
|
124 complexity=atoi(argv[5]);
|
cannam@154
|
125 opus_custom_encoder_ctl(enc,OPUS_SET_COMPLEXITY(complexity));
|
cannam@154
|
126 }
|
cannam@154
|
127
|
cannam@154
|
128 in = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
|
cannam@154
|
129 out = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
|
cannam@154
|
130
|
cannam@154
|
131 while (!feof(fin))
|
cannam@154
|
132 {
|
cannam@154
|
133 int ret;
|
cannam@154
|
134 err = fread(in, sizeof(short), frame_size*channels, fin);
|
cannam@154
|
135 if (feof(fin))
|
cannam@154
|
136 break;
|
cannam@154
|
137 len = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);
|
cannam@154
|
138 if (len <= 0)
|
cannam@154
|
139 fprintf (stderr, "opus_custom_encode() failed: %s\n", opus_strerror(len));
|
cannam@154
|
140
|
cannam@154
|
141 /* This is for simulating bit errors */
|
cannam@154
|
142 #if 0
|
cannam@154
|
143 int errors = 0;
|
cannam@154
|
144 int eid = 0;
|
cannam@154
|
145 /* This simulates random bit error */
|
cannam@154
|
146 for (i=0;i<len*8;i++)
|
cannam@154
|
147 {
|
cannam@154
|
148 if (rand()%atoi(argv[8])==0)
|
cannam@154
|
149 {
|
cannam@154
|
150 if (i<64)
|
cannam@154
|
151 {
|
cannam@154
|
152 errors++;
|
cannam@154
|
153 eid = i;
|
cannam@154
|
154 }
|
cannam@154
|
155 data[i/8] ^= 1<<(7-(i%8));
|
cannam@154
|
156 }
|
cannam@154
|
157 }
|
cannam@154
|
158 if (errors == 1)
|
cannam@154
|
159 data[eid/8] ^= 1<<(7-(eid%8));
|
cannam@154
|
160 else if (errors%2 == 1)
|
cannam@154
|
161 data[rand()%8] ^= 1<<rand()%8;
|
cannam@154
|
162 #endif
|
cannam@154
|
163
|
cannam@154
|
164 #if 1 /* Set to zero to use the encoder's output instead */
|
cannam@154
|
165 /* This is to simulate packet loss */
|
cannam@154
|
166 if (argc==9 && rand()%1000<atoi(argv[argc-3]))
|
cannam@154
|
167 /*if (errors && (errors%2==0))*/
|
cannam@154
|
168 ret = opus_custom_decode(dec, NULL, len, out, frame_size);
|
cannam@154
|
169 else
|
cannam@154
|
170 ret = opus_custom_decode(dec, data, len, out, frame_size);
|
cannam@154
|
171 if (ret < 0)
|
cannam@154
|
172 fprintf(stderr, "opus_custom_decode() failed: %s\n", opus_strerror(ret));
|
cannam@154
|
173 #else
|
cannam@154
|
174 for (i=0;i<ret*channels;i++)
|
cannam@154
|
175 out[i] = in[i];
|
cannam@154
|
176 #endif
|
cannam@154
|
177 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
|
cannam@154
|
178 for (i=0;i<ret*channels;i++)
|
cannam@154
|
179 {
|
cannam@154
|
180 rmsd += (in[i]-out[i])*1.0*(in[i]-out[i]);
|
cannam@154
|
181 /*out[i] -= in[i];*/
|
cannam@154
|
182 }
|
cannam@154
|
183 #endif
|
cannam@154
|
184 count++;
|
cannam@154
|
185 fwrite(out+skip*channels, sizeof(short), (ret-skip)*channels, fout);
|
cannam@154
|
186 skip = 0;
|
cannam@154
|
187 }
|
cannam@154
|
188 PRINT_MIPS(stderr);
|
cannam@154
|
189
|
cannam@154
|
190 opus_custom_encoder_destroy(enc);
|
cannam@154
|
191 opus_custom_decoder_destroy(dec);
|
cannam@154
|
192 fclose(fin);
|
cannam@154
|
193 fclose(fout);
|
cannam@154
|
194 opus_custom_mode_destroy(mode);
|
cannam@154
|
195 free(in);
|
cannam@154
|
196 free(out);
|
cannam@154
|
197 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
|
cannam@154
|
198 if (rmsd > 0)
|
cannam@154
|
199 {
|
cannam@154
|
200 rmsd = sqrt(rmsd/(1.0*frame_size*channels*count));
|
cannam@154
|
201 fprintf (stderr, "Error: encoder doesn't match decoder\n");
|
cannam@154
|
202 fprintf (stderr, "RMS mismatch is %f\n", rmsd);
|
cannam@154
|
203 return 1;
|
cannam@154
|
204 } else {
|
cannam@154
|
205 fprintf (stderr, "Encoder matches decoder!!\n");
|
cannam@154
|
206 }
|
cannam@154
|
207 #endif
|
cannam@154
|
208 return 0;
|
cannam@154
|
209 }
|
cannam@154
|
210
|