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