Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 /******************************************************************
     12 
     13 	iLBC Speech Coder ANSI-C Source Code
     14 
     15         iLBC_test.c
     16 
     17 ******************************************************************/
     18 
     19 #include <stdlib.h>
     20 #include <stdio.h>
     21 #include <string.h>
     22 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.h"
     23 
     24 /*---------------------------------------------------------------*
     25  *  Main program to test iLBC encoding and decoding
     26  *
     27  *  Usage:
     28  *	  exefile_name.exe <infile> <bytefile> <outfile> <channel>
     29  *
     30  *    <infile>   : Input file, speech for encoder (16-bit pcm file)
     31  *    <bytefile> : Bit stream output from the encoder
     32  *    <outfile>  : Output file, decoded speech (16-bit pcm file)
     33  *    <channel>  : Bit error file, optional (16-bit)
     34  *                     1 - Packet received correctly
     35  *                     0 - Packet Lost
     36  *
     37  *--------------------------------------------------------------*/
     38 
     39 #define BLOCKL_MAX			240
     40 #define ILBCNOOFWORDS_MAX	25
     41 
     42 
     43 int main(int argc, char* argv[])
     44 {
     45 
     46   FILE *ifileid,*efileid,*ofileid, *cfileid;
     47   int16_t data[BLOCKL_MAX];
     48   uint8_t encoded_data[2 * ILBCNOOFWORDS_MAX];
     49   int16_t decoded_data[BLOCKL_MAX];
     50   int len_int, mode;
     51   short pli;
     52   int blockcount = 0;
     53   int packetlosscount = 0;
     54   size_t frameLen, len, len_i16s;
     55   int16_t speechType;
     56   IlbcEncoderInstance *Enc_Inst;
     57   IlbcDecoderInstance *Dec_Inst;
     58 
     59 #ifdef __ILBC_WITH_40BITACC
     60   /* Doublecheck that long long exists */
     61   if (sizeof(long)>=sizeof(long long)) {
     62     fprintf(stderr, "40-bit simulation is not be supported on this platform\n");
     63     exit(0);
     64   }
     65 #endif
     66 
     67   /* get arguments and open files */
     68 
     69   if ((argc!=5) && (argc!=6)) {
     70     fprintf(stderr,
     71             "\n*-----------------------------------------------*\n");
     72     fprintf(stderr,
     73             "   %s <20,30> input encoded decoded (channel)\n\n",
     74             argv[0]);
     75     fprintf(stderr,
     76             "   mode    : Frame size for the encoding/decoding\n");
     77     fprintf(stderr,
     78             "                 20 - 20 ms\n");
     79     fprintf(stderr,
     80             "                 30 - 30 ms\n");
     81     fprintf(stderr,
     82             "   input   : Speech for encoder (16-bit pcm file)\n");
     83     fprintf(stderr,
     84             "   encoded : Encoded bit stream\n");
     85     fprintf(stderr,
     86             "   decoded : Decoded speech (16-bit pcm file)\n");
     87     fprintf(stderr,
     88             "   channel : Packet loss pattern, optional (16-bit)\n");
     89     fprintf(stderr,
     90             "                  1 - Packet received correctly\n");
     91     fprintf(stderr,
     92             "                  0 - Packet Lost\n");
     93     fprintf(stderr,
     94             "*-----------------------------------------------*\n\n");
     95     exit(1);
     96   }
     97   mode=atoi(argv[1]);
     98   if (mode != 20 && mode != 30) {
     99     fprintf(stderr,"Wrong mode %s, must be 20, or 30\n",
    100             argv[1]);
    101     exit(2);
    102   }
    103   if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
    104     fprintf(stderr,"Cannot open input file %s\n", argv[2]);
    105     exit(2);}
    106   if ( (efileid=fopen(argv[3],"wb")) == NULL) {
    107     fprintf(stderr, "Cannot open encoded file file %s\n",
    108             argv[3]); exit(1);}
    109   if ( (ofileid=fopen(argv[4],"wb")) == NULL) {
    110     fprintf(stderr, "Cannot open decoded file %s\n",
    111             argv[4]); exit(1);}
    112   if (argc==6) {
    113     if( (cfileid=fopen(argv[5],"rb")) == NULL) {
    114       fprintf(stderr, "Cannot open channel file %s\n",
    115               argv[5]);
    116       exit(1);
    117     }
    118   } else {
    119     cfileid=NULL;
    120   }
    121 
    122   /* print info */
    123 
    124   fprintf(stderr, "\n");
    125   fprintf(stderr,
    126           "*---------------------------------------------------*\n");
    127   fprintf(stderr,
    128           "*                                                   *\n");
    129   fprintf(stderr,
    130           "*      iLBC test program                            *\n");
    131   fprintf(stderr,
    132           "*                                                   *\n");
    133   fprintf(stderr,
    134           "*                                                   *\n");
    135   fprintf(stderr,
    136           "*---------------------------------------------------*\n");
    137   fprintf(stderr,"\nMode           : %2d ms\n", mode);
    138   fprintf(stderr,"Input file     : %s\n", argv[2]);
    139   fprintf(stderr,"Encoded file   : %s\n", argv[3]);
    140   fprintf(stderr,"Output file    : %s\n", argv[4]);
    141   if (argc==6) {
    142     fprintf(stderr,"Channel file   : %s\n", argv[5]);
    143   }
    144   fprintf(stderr,"\n");
    145 
    146   /* Create structs */
    147   WebRtcIlbcfix_EncoderCreate(&Enc_Inst);
    148   WebRtcIlbcfix_DecoderCreate(&Dec_Inst);
    149 
    150 
    151   /* Initialization */
    152 
    153   WebRtcIlbcfix_EncoderInit(Enc_Inst, mode);
    154   WebRtcIlbcfix_DecoderInit(Dec_Inst, mode);
    155   frameLen = (size_t)(mode*8);
    156 
    157   /* loop over input blocks */
    158 
    159   while (fread(data,sizeof(int16_t),frameLen,ifileid) == frameLen) {
    160 
    161     blockcount++;
    162 
    163     /* encoding */
    164 
    165     fprintf(stderr, "--- Encoding block %i --- ",blockcount);
    166     len_int = WebRtcIlbcfix_Encode(Enc_Inst, data, frameLen, encoded_data);
    167     if (len_int < 0) {
    168       fprintf(stderr, "Error encoding\n");
    169       exit(0);
    170     }
    171     len = (size_t)len_int;
    172     fprintf(stderr, "\r");
    173 
    174     /* write byte file */
    175 
    176     len_i16s = (len + 1) / sizeof(int16_t);
    177     if (fwrite(encoded_data, sizeof(int16_t), len_i16s, efileid) != len_i16s) {
    178       return -1;
    179     }
    180 
    181     /* get channel data if provided */
    182     if (argc==6) {
    183       if (fread(&pli, sizeof(int16_t), 1, cfileid)) {
    184         if ((pli!=0)&&(pli!=1)) {
    185           fprintf(stderr, "Error in channel file\n");
    186           exit(0);
    187         }
    188         if (pli==0) {
    189           /* Packet loss -> remove info from frame */
    190           memset(encoded_data, 0,
    191                  sizeof(int16_t)*ILBCNOOFWORDS_MAX);
    192           packetlosscount++;
    193         }
    194       } else {
    195         fprintf(stderr, "Error. Channel file too short\n");
    196         exit(0);
    197       }
    198     } else {
    199       pli=1;
    200     }
    201 
    202     /* decoding */
    203 
    204     fprintf(stderr, "--- Decoding block %i --- ",blockcount);
    205     if (pli==1) {
    206       len_int=WebRtcIlbcfix_Decode(Dec_Inst, encoded_data,
    207                                    len, decoded_data,&speechType);
    208       if (len_int < 0) {
    209         fprintf(stderr, "Error decoding\n");
    210         exit(0);
    211       }
    212       len = (size_t)len_int;
    213     } else {
    214       len=WebRtcIlbcfix_DecodePlc(Dec_Inst, decoded_data, 1);
    215     }
    216     fprintf(stderr, "\r");
    217 
    218     /* write output file */
    219 
    220     if (fwrite(decoded_data, sizeof(int16_t), len, ofileid) != len) {
    221       return -1;
    222     }
    223   }
    224 
    225   /* close files */
    226 
    227   fclose(ifileid);  fclose(efileid); fclose(ofileid);
    228   if (argc==6) {
    229     fclose(cfileid);
    230   }
    231 
    232   /* Free structs */
    233   WebRtcIlbcfix_EncoderFree(Enc_Inst);
    234   WebRtcIlbcfix_DecoderFree(Dec_Inst);
    235 
    236 
    237   printf("\nDone with simulation\n\n");
    238 
    239   return(0);
    240 }
    241