annotate src/opus-1.3/celt/opus_custom_demo.c @ 76:f3731af47c4b

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