• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * testG722.cpp : Defines the entry point for the console application.
13  */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 /* include API */
20 #include "modules/audio_coding/codecs/g722/g722_interface.h"
21 
22 /* Runtime statistics */
23 #include <time.h>
24 #define CLOCKS_PER_SEC_G722 100000
25 
26 // Forward declaration
27 typedef struct WebRtcG722EncInst G722EncInst;
28 typedef struct WebRtcG722DecInst G722DecInst;
29 
30 /* function for reading audio data from PCM file */
readframe(int16_t * data,FILE * inp,size_t length)31 bool readframe(int16_t* data, FILE* inp, size_t length) {
32   size_t rlen = fread(data, sizeof(int16_t), length, inp);
33   if (rlen >= length)
34     return false;
35   memset(data + rlen, 0, (length - rlen) * sizeof(int16_t));
36   return true;
37 }
38 
main(int argc,char * argv[])39 int main(int argc, char* argv[]) {
40   char inname[60], outbit[40], outname[40];
41   FILE *inp, *outbitp, *outp;
42 
43   int framecnt;
44   bool endfile;
45   size_t framelength = 160;
46   G722EncInst* G722enc_inst;
47   G722DecInst* G722dec_inst;
48 
49   /* Runtime statistics */
50   double starttime;
51   double runtime = 0;
52   double length_file;
53 
54   size_t stream_len = 0;
55   int16_t shortdata[960];
56   int16_t decoded[960];
57   uint8_t streamdata[80 * 6];
58   int16_t speechType[1];
59 
60   /* handling wrong input arguments in the command line */
61   if (argc != 5) {
62     printf("\n\nWrong number of arguments or flag values.\n\n");
63 
64     printf("\n");
65     printf("Usage:\n\n");
66     printf("./testG722.exe framelength infile outbitfile outspeechfile \n\n");
67     printf("with:\n");
68     printf("framelength  :    Framelength in samples.\n\n");
69     printf("infile       :    Normal speech input file\n\n");
70     printf("outbitfile   :    Bitstream output file\n\n");
71     printf("outspeechfile:    Speech output file\n\n");
72     exit(0);
73   }
74 
75   /* Get frame length */
76   int framelength_int = atoi(argv[1]);
77   if (framelength_int < 0) {
78     printf("  G.722: Invalid framelength %d.\n", framelength_int);
79     exit(1);
80   }
81   framelength = static_cast<size_t>(framelength_int);
82 
83   /* Get Input and Output files */
84   sscanf(argv[2], "%s", inname);
85   sscanf(argv[3], "%s", outbit);
86   sscanf(argv[4], "%s", outname);
87 
88   if ((inp = fopen(inname, "rb")) == NULL) {
89     printf("  G.722: Cannot read file %s.\n", inname);
90     exit(1);
91   }
92   if ((outbitp = fopen(outbit, "wb")) == NULL) {
93     printf("  G.722: Cannot write file %s.\n", outbit);
94     exit(1);
95   }
96   if ((outp = fopen(outname, "wb")) == NULL) {
97     printf("  G.722: Cannot write file %s.\n", outname);
98     exit(1);
99   }
100   printf("\nInput:%s\nOutput bitstream:%s\nOutput:%s\n", inname, outbit,
101          outname);
102 
103   /* Create and init */
104   WebRtcG722_CreateEncoder((G722EncInst**)&G722enc_inst);
105   WebRtcG722_CreateDecoder((G722DecInst**)&G722dec_inst);
106   WebRtcG722_EncoderInit((G722EncInst*)G722enc_inst);
107   WebRtcG722_DecoderInit((G722DecInst*)G722dec_inst);
108 
109   /* Initialize encoder and decoder */
110   framecnt = 0;
111   endfile = false;
112   while (!endfile) {
113     framecnt++;
114 
115     /* Read speech block */
116     endfile = readframe(shortdata, inp, framelength);
117 
118     /* Start clock before call to encoder and decoder */
119     starttime = clock() / (double)CLOCKS_PER_SEC_G722;
120 
121     /* G.722 encoding + decoding */
122     stream_len = WebRtcG722_Encode((G722EncInst*)G722enc_inst, shortdata,
123                                    framelength, streamdata);
124     WebRtcG722_Decode(G722dec_inst, streamdata, stream_len, decoded,
125                       speechType);
126 
127     /* Stop clock after call to encoder and decoder */
128     runtime += (double)((clock() / (double)CLOCKS_PER_SEC_G722) - starttime);
129 
130     /* Write coded bits to file */
131     if (fwrite(streamdata, sizeof(short), stream_len / 2, outbitp) !=
132         stream_len / 2) {
133       return -1;
134     }
135     /* Write coded speech to file */
136     if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) {
137       return -1;
138     }
139   }
140 
141   WebRtcG722_FreeEncoder((G722EncInst*)G722enc_inst);
142   WebRtcG722_FreeDecoder((G722DecInst*)G722dec_inst);
143 
144   length_file = ((double)framecnt * (double)framelength / 16000);
145   printf("\n\nLength of speech file: %.1f s\n", length_file);
146   printf("Time to run G.722:      %.2f s (%.2f %% of realtime)\n\n", runtime,
147          (100 * runtime / length_file));
148   printf("---------------------END----------------------\n");
149 
150   fclose(inp);
151   fclose(outbitp);
152   fclose(outp);
153 
154   return 0;
155 }
156