Chris@69: /* Copyright (c) 2007-2008 CSIRO Chris@69: Copyright (c) 2007-2009 Xiph.Org Foundation Chris@69: Written by Jean-Marc Valin */ Chris@69: /* Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: Chris@69: - Redistributions of source code must retain the above copyright Chris@69: notice, this list of conditions and the following disclaimer. Chris@69: Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@69: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@69: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@69: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER Chris@69: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@69: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@69: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Chris@69: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF Chris@69: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING Chris@69: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@69: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@69: */ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "opus_custom.h" Chris@69: #include "arch.h" Chris@69: #include Chris@69: #include Chris@69: #include Chris@69: #include Chris@69: Chris@69: #define MAX_PACKET 1275 Chris@69: Chris@69: int main(int argc, char *argv[]) Chris@69: { Chris@69: int err; Chris@69: char *inFile, *outFile; Chris@69: FILE *fin, *fout; Chris@69: OpusCustomMode *mode=NULL; Chris@69: OpusCustomEncoder *enc; Chris@69: OpusCustomDecoder *dec; Chris@69: int len; Chris@69: opus_int32 frame_size, channels, rate; Chris@69: int bytes_per_packet; Chris@69: unsigned char data[MAX_PACKET]; Chris@69: int complexity; Chris@69: #if !(defined (FIXED_POINT) && !defined(CUSTOM_MODES)) && defined(RESYNTH) Chris@69: int i; Chris@69: double rmsd = 0; Chris@69: #endif Chris@69: int count = 0; Chris@69: opus_int32 skip; Chris@69: opus_int16 *in, *out; Chris@69: if (argc != 9 && argc != 8 && argc != 7) Chris@69: { Chris@69: fprintf (stderr, "Usage: test_opus_custom " Chris@69: " [ [packet loss rate]] " Chris@69: " \n"); Chris@69: return 1; Chris@69: } Chris@69: Chris@69: rate = (opus_int32)atol(argv[1]); Chris@69: channels = atoi(argv[2]); Chris@69: frame_size = atoi(argv[3]); Chris@69: mode = opus_custom_mode_create(rate, frame_size, NULL); Chris@69: if (mode == NULL) Chris@69: { Chris@69: fprintf(stderr, "failed to create a mode\n"); Chris@69: return 1; Chris@69: } Chris@69: Chris@69: bytes_per_packet = atoi(argv[4]); Chris@69: if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET) Chris@69: { Chris@69: fprintf (stderr, "bytes per packet must be between 0 and %d\n", Chris@69: MAX_PACKET); Chris@69: return 1; Chris@69: } Chris@69: Chris@69: inFile = argv[argc-2]; Chris@69: fin = fopen(inFile, "rb"); Chris@69: if (!fin) Chris@69: { Chris@69: fprintf (stderr, "Could not open input file %s\n", argv[argc-2]); Chris@69: return 1; Chris@69: } Chris@69: outFile = argv[argc-1]; Chris@69: fout = fopen(outFile, "wb+"); Chris@69: if (!fout) Chris@69: { Chris@69: fprintf (stderr, "Could not open output file %s\n", argv[argc-1]); Chris@69: fclose(fin); Chris@69: return 1; Chris@69: } Chris@69: Chris@69: enc = opus_custom_encoder_create(mode, channels, &err); Chris@69: if (err != 0) Chris@69: { Chris@69: fprintf(stderr, "Failed to create the encoder: %s\n", opus_strerror(err)); Chris@69: fclose(fin); Chris@69: fclose(fout); Chris@69: return 1; Chris@69: } Chris@69: dec = opus_custom_decoder_create(mode, channels, &err); Chris@69: if (err != 0) Chris@69: { Chris@69: fprintf(stderr, "Failed to create the decoder: %s\n", opus_strerror(err)); Chris@69: fclose(fin); Chris@69: fclose(fout); Chris@69: return 1; Chris@69: } Chris@69: opus_custom_decoder_ctl(dec, OPUS_GET_LOOKAHEAD(&skip)); Chris@69: Chris@69: if (argc>7) Chris@69: { Chris@69: complexity=atoi(argv[5]); Chris@69: opus_custom_encoder_ctl(enc,OPUS_SET_COMPLEXITY(complexity)); Chris@69: } Chris@69: Chris@69: in = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16)); Chris@69: out = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16)); Chris@69: Chris@69: while (!feof(fin)) Chris@69: { Chris@69: int ret; Chris@69: err = fread(in, sizeof(short), frame_size*channels, fin); Chris@69: if (feof(fin)) Chris@69: break; Chris@69: len = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet); Chris@69: if (len <= 0) Chris@69: fprintf (stderr, "opus_custom_encode() failed: %s\n", opus_strerror(len)); Chris@69: Chris@69: /* This is for simulating bit errors */ Chris@69: #if 0 Chris@69: int errors = 0; Chris@69: int eid = 0; Chris@69: /* This simulates random bit error */ Chris@69: for (i=0;i 0) Chris@69: { Chris@69: rmsd = sqrt(rmsd/(1.0*frame_size*channels*count)); Chris@69: fprintf (stderr, "Error: encoder doesn't match decoder\n"); Chris@69: fprintf (stderr, "RMS mismatch is %f\n", rmsd); Chris@69: return 1; Chris@69: } else { Chris@69: fprintf (stderr, "Encoder matches decoder!!\n"); Chris@69: } Chris@69: #endif Chris@69: return 0; Chris@69: } Chris@69: