1/*
2 *  Copyright (c) 2011 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#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <ctype.h>
15
16//#include "isac_codec.h"
17//#include "isac_structs.h"
18#include "isacfix.h"
19
20
21#define NUM_CODECS 1
22
23int main(int argc, char* argv[])
24{
25    FILE *inFileList;
26    FILE *audioFile;
27    FILE *outFile;
28    char audioFileName[501];
29    short audioBuff[960];
30    short encoded[600];
31    short startAudio;
32    short encodedLen;
33    ISACFIX_MainStruct *isac_struct;
34    unsigned long int hist[601];
35
36    // reset the histogram
37    for(short n=0; n < 601; n++)
38    {
39        hist[n] = 0;
40    }
41
42
43    inFileList = fopen(argv[1], "r");
44    if(inFileList == NULL)
45    {
46        printf("Could not open the input file.\n");
47        getchar();
48        exit(-1);
49    }
50    outFile = fopen(argv[2], "w");
51    if(outFile == NULL)
52    {
53        printf("Could not open the histogram file.\n");
54        getchar();
55        exit(-1);
56    }
57
58    short frameSizeMsec = 30;
59    if(argc > 3)
60    {
61        frameSizeMsec = atoi(argv[3]);
62    }
63
64    short audioOffset = 0;
65    if(argc > 4)
66    {
67        audioOffset = atoi(argv[4]);
68    }
69    int ok;
70    ok = WebRtcIsacfix_Create(&isac_struct);
71    // instantaneous mode
72    ok |= WebRtcIsacfix_EncoderInit(isac_struct, 1);
73    // is not used but initialize
74    ok |= WebRtcIsacfix_DecoderInit(isac_struct);
75    ok |= WebRtcIsacfix_Control(isac_struct, 32000, frameSizeMsec);
76
77    if(ok != 0)
78    {
79        printf("\nProblem in seting up iSAC\n");
80        exit(-1);
81    }
82
83    while( fgets(audioFileName, 500, inFileList) != NULL )
84    {
85        // remove trailing white-spaces and any Cntrl character
86        if(strlen(audioFileName) == 0)
87        {
88            continue;
89        }
90        short n = strlen(audioFileName) - 1;
91        while(isspace(audioFileName[n]) || iscntrl(audioFileName[n]))
92        {
93            audioFileName[n] = '\0';
94            n--;
95            if(n < 0)
96            {
97                break;
98            }
99        }
100
101        // remove leading spaces
102        if(strlen(audioFileName) == 0)
103        {
104            continue;
105        }
106        n = 0;
107        while((isspace(audioFileName[n]) || iscntrl(audioFileName[n])) &&
108            (audioFileName[n] != '\0'))
109        {
110            n++;
111        }
112        memmove(audioFileName, &audioFileName[n], 500 - n);
113        if(strlen(audioFileName) == 0)
114        {
115            continue;
116        }
117        audioFile = fopen(audioFileName, "rb");
118        if(audioFile == NULL)
119        {
120            printf("\nCannot open %s!!!!!\n", audioFileName);
121            exit(0);
122        }
123
124        if(audioOffset > 0)
125        {
126            fseek(audioFile, (audioOffset<<1), SEEK_SET);
127        }
128
129        while(fread(audioBuff, sizeof(short), (480*frameSizeMsec/30), audioFile) >= (480*frameSizeMsec/30))
130        {
131            startAudio = 0;
132            do
133            {
134                encodedLen = WebRtcIsacfix_Encode(isac_struct,
135                                                  &audioBuff[startAudio], encoded);
136                startAudio += 160;
137            } while(encodedLen == 0);
138
139            if(encodedLen < 0)
140            {
141                printf("\nEncoding Error!!!\n");
142                exit(0);
143            }
144            hist[encodedLen]++;
145        }
146        fclose(audioFile);
147    }
148    fclose(inFileList);
149    unsigned long totalFrames = 0;
150    for(short n=0; n < 601; n++)
151    {
152        totalFrames += hist[n];
153        fprintf(outFile, "%10lu\n", hist[n]);
154    }
155    fclose(outFile);
156
157    short topTenCntr = 0;
158    printf("\nTotal number of Frames %lu\n\n", totalFrames);
159    printf("Payload Len    # occurences\n");
160    printf("===========    ============\n");
161
162    for(short n = 600; (n >= 0) && (topTenCntr < 10); n--)
163    {
164        if(hist[n] > 0)
165        {
166            topTenCntr++;
167            printf("    %3d            %3d\n", n, hist[n]);
168        }
169    }
170    WebRtcIsacfix_Free(isac_struct);
171    return 0;
172}
173
174