added "iv.dynstring"
[iv.d.git] / clibcelt / src / custom_demo.c
blob0a1f94244e34f03ab3ad371c5b2c77d652cf496d
1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
4 /*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include "libcelt/opus_custom.h"
34 #include "libcelt/arch.h"
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <math.h>
38 #include <string.h>
41 int main (int argc, char *argv[]) {
42 int err;
43 char *inFile, *outFile;
44 FILE *fin, *fout;
45 OpusCustomMode *mode=NULL;
46 OpusCustomEncoder *enc;
47 OpusCustomDecoder *dec;
48 int len;
49 opus_int32 frame_size, channels, rate;
50 int bytes_per_packet;
51 unsigned char data[OPUS_CUSTOM_MAX_PACKET];
52 int complexity;
53 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
54 int i;
55 double rmsd = 0;
56 #endif
57 int count = 0;
58 opus_int32 skip;
59 opus_int16 *in, *out;
61 if (argc != 9 && argc != 8 && argc != 7) {
62 fprintf (stderr, "Usage: test_opus_custom <rate> <channels> <frame size> "
63 " <bytes per packet> [<complexity> [packet loss rate]] "
64 "<input> <output>\n");
65 return 1;
68 rate = (opus_int32)atol(argv[1]);
69 channels = atoi(argv[2]);
70 frame_size = atoi(argv[3]);
71 mode = opus_custom_mode_create(rate, frame_size, NULL);
72 if (mode == NULL)
74 fprintf(stderr, "failed to create a mode\n");
75 return 1;
78 bytes_per_packet = atoi(argv[4]);
79 if (bytes_per_packet < 0 || bytes_per_packet > OPUS_CUSTOM_MAX_PACKET)
81 fprintf (stderr, "bytes per packet must be between 0 and %d\n",
82 OPUS_CUSTOM_MAX_PACKET);
83 return 1;
86 inFile = argv[argc-2];
87 fin = fopen(inFile, "rb");
88 if (!fin)
90 fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
91 return 1;
93 outFile = argv[argc-1];
94 fout = fopen(outFile, "wb+");
95 if (!fout)
97 fprintf (stderr, "Could not open output file %s\n", argv[argc-1]);
98 fclose(fin);
99 return 1;
102 enc = opus_custom_encoder_create(mode, channels, &err);
103 if (err != 0)
105 fprintf(stderr, "Failed to create the encoder: %s\n", opus_strerror(err));
106 fclose(fin);
107 fclose(fout);
108 return 1;
110 dec = opus_custom_decoder_create(mode, channels, &err);
111 if (err != 0)
113 fprintf(stderr, "Failed to create the decoder: %s\n", opus_strerror(err));
114 fclose(fin);
115 fclose(fout);
116 return 1;
118 opus_custom_decoder_ctl(dec, OPUS_GET_LOOKAHEAD(&skip));
120 if (argc>7)
122 complexity=atoi(argv[5]);
123 opus_custom_encoder_ctl(enc,OPUS_SET_COMPLEXITY(complexity));
126 in = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
127 out = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));
129 while (!feof(fin))
131 int ret;
132 err = fread(in, sizeof(short), frame_size*channels, fin);
133 if (feof(fin))
134 break;
135 len = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);
136 if (len <= 0)
137 fprintf (stderr, "opus_custom_encode() failed: %s\n", opus_strerror(len));
140 unsigned char ll;
141 ll = len&0xff;
142 fwrite(&ll, 1, 1, fout);
143 ll = (len>>8)&0xff;
144 fwrite(&ll, 1, 1, fout);
146 fwrite(data, 1, len, fout);
147 #if 0
148 /* This is for simulating bit errors */
149 #if 0
150 int errors = 0;
151 int eid = 0;
152 /* This simulates random bit error */
153 for (i=0;i<len*8;i++)
155 if (rand()%atoi(argv[8])==0)
157 if (i<64)
159 errors++;
160 eid = i;
162 data[i/8] ^= 1<<(7-(i%8));
165 if (errors == 1)
166 data[eid/8] ^= 1<<(7-(eid%8));
167 else if (errors%2 == 1)
168 data[rand()%8] ^= 1<<rand()%8;
169 #endif
171 #if 1 /* Set to zero to use the encoder's output instead */
172 /* This is to simulate packet loss */
173 if (argc==9 && rand()%1000<atoi(argv[argc-3]))
174 /*if (errors && (errors%2==0))*/
175 ret = opus_custom_decode(dec, NULL, len, out, frame_size);
176 else
177 ret = opus_custom_decode(dec, data, len, out, frame_size);
178 if (ret < 0)
179 fprintf(stderr, "opus_custom_decode() failed: %s\n", opus_strerror(ret));
180 #else
181 for (i=0;i<ret*channels;i++)
182 out[i] = in[i];
183 #endif
184 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
185 for (i=0;i<ret*channels;i++)
187 rmsd += (in[i]-out[i])*1.0*(in[i]-out[i]);
188 /*out[i] -= in[i];*/
190 #endif
191 #endif
192 count++;
193 //fwrite(out+skip*channels, sizeof(short), (ret-skip)*channels, fout);
194 skip = 0;
196 PRINT_MIPS(stderr);
198 opus_custom_encoder_destroy(enc);
199 opus_custom_decoder_destroy(dec);
200 fclose(fin);
201 fclose(fout);
202 opus_custom_mode_destroy(mode);
203 free(in);
204 free(out);
205 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
206 if (rmsd > 0)
208 rmsd = sqrt(rmsd/(1.0*frame_size*channels*count));
209 fprintf (stderr, "Error: encoder doesn't match decoder\n");
210 fprintf (stderr, "RMS mismatch is %f\n", rmsd);
211 return 1;
212 } else {
213 fprintf (stderr, "Encoder matches decoder!!\n");
215 #endif
216 return 0;