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 "libcelt/opus_custom.h"
34 #include "libcelt/arch.h"
41 int main (int argc
, char *argv
[]) {
43 char *inFile
, *outFile
;
45 OpusCustomMode
*mode
=NULL
;
46 OpusCustomEncoder
*enc
;
47 OpusCustomDecoder
*dec
;
49 opus_int32 frame_size
, channels
, rate
;
51 unsigned char data
[OPUS_CUSTOM_MAX_PACKET
];
53 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
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");
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
);
74 fprintf(stderr
, "failed to create a mode\n");
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
);
86 inFile
= argv
[argc
-2];
87 fin
= fopen(inFile
, "rb");
90 fprintf (stderr
, "Could not open input file %s\n", argv
[argc
-2]);
93 outFile
= argv
[argc
-1];
94 fout
= fopen(outFile
, "wb+");
97 fprintf (stderr
, "Could not open output file %s\n", argv
[argc
-1]);
102 enc
= opus_custom_encoder_create(mode
, channels
, &err
);
105 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
));
118 opus_custom_decoder_ctl(dec
, OPUS_GET_LOOKAHEAD(&skip
));
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
));
132 err
= fread(in
, sizeof(short), frame_size
*channels
, fin
);
135 len
= opus_custom_encode(enc
, in
, frame_size
, data
, bytes_per_packet
);
137 fprintf (stderr
, "opus_custom_encode() failed: %s\n", opus_strerror(len
));
142 fwrite(&ll
, 1, 1, fout
);
144 fwrite(&ll
, 1, 1, fout
);
146 fwrite(data
, 1, len
, fout
);
148 /* This is for simulating bit errors */
152 /* This simulates random bit error */
153 for (i
=0;i
<len
*8;i
++)
155 if (rand()%atoi(argv
[8])==0)
162 data
[i
/8] ^= 1<<(7-(i
%8));
166 data
[eid
/8] ^= 1<<(7-(eid
%8));
167 else if (errors
%2 == 1)
168 data
[rand()%8] ^= 1<<rand()%8;
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
);
177 ret
= opus_custom_decode(dec
, data
, len
, out
, frame_size
);
179 fprintf(stderr
, "opus_custom_decode() failed: %s\n", opus_strerror(ret
));
181 for (i
=0;i
<ret
*channels
;i
++)
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
]);
193 //fwrite(out+skip*channels, sizeof(short), (ret-skip)*channels, fout);
198 opus_custom_encoder_destroy(enc
);
199 opus_custom_decoder_destroy(dec
);
202 opus_custom_mode_destroy(mode
);
205 #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH)
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
);
213 fprintf (stderr
, "Encoder matches decoder!!\n");