aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_coding/codecs/ilbc/test/iLBC_testLib.c
blob: f14192c2ae2cb1f82ba6cecd6a34164825b08c78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

/******************************************************************

iLBC Speech Coder ANSI-C Source Code

iLBC_test.c

******************************************************************/

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "ilbc.h"

//#define JUNK_DATA
#ifdef JUNK_DATA
#define SEED_FILE "randseed.txt"
#endif


/*----------------------------------------------------------------*
*  Main program to test iLBC encoding and decoding
*
*  Usage:
*		exefile_name.exe <infile> <bytefile> <outfile>
*
*---------------------------------------------------------------*/

int main(int argc, char* argv[])
{
  FILE *ifileid,*efileid,*ofileid, *chfileid;
  short encoded_data[55], data[240], speechType;
  int len_int, mode;
  short pli;
  size_t len, readlen;
  int blockcount = 0;

  IlbcEncoderInstance *Enc_Inst;
  IlbcDecoderInstance *Dec_Inst;
#ifdef JUNK_DATA
  size_t i;
  FILE *seedfile;
  unsigned int random_seed = (unsigned int) time(NULL);//1196764538
#endif

  /* Create structs */
  WebRtcIlbcfix_EncoderCreate(&Enc_Inst);
  WebRtcIlbcfix_DecoderCreate(&Dec_Inst);

  /* get arguments and open files */

  if (argc != 6 ) {
    fprintf(stderr, "%s mode inputfile bytefile outputfile channelfile\n",
            argv[0]);
    fprintf(stderr, "Example:\n");
    fprintf(stderr, "%s <30,20> in.pcm byte.dat out.pcm T30.0.dat\n", argv[0]);
    exit(1);
  }
  mode=atoi(argv[1]);
  if (mode != 20 && mode != 30) {
    fprintf(stderr,"Wrong mode %s, must be 20, or 30\n", argv[1]);
    exit(2);
  }
  if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
    fprintf(stderr,"Cannot open input file %s\n", argv[2]);
    exit(2);}
  if ( (efileid=fopen(argv[3],"wb")) == NULL) {
    fprintf(stderr, "Cannot open channelfile file %s\n",
            argv[3]); exit(3);}
  if( (ofileid=fopen(argv[4],"wb")) == NULL) {
    fprintf(stderr, "Cannot open output file %s\n",
            argv[4]); exit(3);}
  if ( (chfileid=fopen(argv[5],"rb")) == NULL) {
    fprintf(stderr,"Cannot open channel file file %s\n", argv[5]);
    exit(2);
  }
  /* print info */
  fprintf(stderr, "\n");
  fprintf(stderr,
          "*---------------------------------------------------*\n");
  fprintf(stderr,
          "*                                                   *\n");
  fprintf(stderr,
          "*      iLBCtest                                     *\n");
  fprintf(stderr,
          "*                                                   *\n");
  fprintf(stderr,
          "*                                                   *\n");
  fprintf(stderr,
		"*---------------------------------------------------*\n");
#ifdef SPLIT_10MS
  fprintf(stderr,"\n10ms split with raw mode: %2d ms\n", mode);
#else
  fprintf(stderr,"\nMode          : %2d ms\n", mode);
#endif
  fprintf(stderr,"\nInput file    : %s\n", argv[2]);
  fprintf(stderr,"Coded file    : %s\n", argv[3]);
  fprintf(stderr,"Output file   : %s\n\n", argv[4]);
  fprintf(stderr,"Channel file  : %s\n\n", argv[5]);

#ifdef JUNK_DATA
  srand(random_seed);

  if ( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) {
    fprintf(stderr, "Error: Could not open file %s\n", SEED_FILE);
  }
  else {
    fprintf(seedfile, "%u\n", random_seed);
    fclose(seedfile);
  }
#endif

  /* Initialization */
  WebRtcIlbcfix_EncoderInit(Enc_Inst, mode);
  WebRtcIlbcfix_DecoderInit(Dec_Inst, mode);

  /* loop over input blocks */
#ifdef SPLIT_10MS
  readlen = 80;
#else
  readlen = (size_t)(mode << 3);
#endif
  while(fread(data, sizeof(short), readlen, ifileid) == readlen) {
    blockcount++;

    /* encoding */
    fprintf(stderr, "--- Encoding block %i --- ",blockcount);
    len_int=WebRtcIlbcfix_Encode(Enc_Inst, data, readlen, encoded_data);
    if (len_int < 0) {
      fprintf(stderr, "Error encoding\n");
      exit(0);
    }
    len = (size_t)len_int;
    fprintf(stderr, "\r");

#ifdef JUNK_DATA
    for ( i = 0; i < len; i++) {
      encoded_data[i] = (short) (encoded_data[i] + (short) rand());
    }
#endif
    /* write byte file */
    if(len != 0){ //len may be 0 in 10ms split case
      fwrite(encoded_data,1,len,efileid);

      /* get channel data if provided */
      if (argc==6) {
        if (fread(&pli, sizeof(int16_t), 1, chfileid)) {
          if ((pli!=0)&&(pli!=1)) {
            fprintf(stderr, "Error in channel file\n");
            exit(0);
          }
          if (pli==0) {
            /* Packet loss -> remove info from frame */
            memset(encoded_data, 0, sizeof(int16_t)*25);
          }
        } else {
          fprintf(stderr, "Error. Channel file too short\n");
          exit(0);
        }
      } else {
        pli=1;
      }

      /* decoding */
      fprintf(stderr, "--- Decoding block %i --- ",blockcount);
      if (pli==1) {
        len_int = WebRtcIlbcfix_Decode(Dec_Inst, encoded_data, len, data,
                                       &speechType);
        if (len_int < 0) {
          fprintf(stderr, "Error decoding\n");
          exit(0);
        }
        len = (size_t)len_int;
      } else {
        len=WebRtcIlbcfix_DecodePlc(Dec_Inst, data, 1);
      }
      fprintf(stderr, "\r");

      /* write output file */
      fwrite(data,sizeof(short),len,ofileid);
    }
  }

#ifdef JUNK_DATA
  if ( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) {
    fprintf(stderr, "Error: Could not open file %s\n", SEED_FILE);
  }
  else {
    fprintf(seedfile, "ok\n\n");
    fclose(seedfile);
  }
#endif

  /* free structs */
  WebRtcIlbcfix_EncoderFree(Enc_Inst);
  WebRtcIlbcfix_DecoderFree(Dec_Inst);

  /* close files */
  fclose(ifileid);
  fclose(efileid);
  fclose(ofileid);

  return 0;
}