Bug 797671: Import Webrtc.org code from stable branch 3.12 (rev 2820) rs=jesup
[gecko.git] / media / webrtc / trunk / src / modules / audio_coding / codecs / g722 / test / testG722.cc
blobd2fdca3a854a08b5d85c7b79c3d255efbc6b2f08
1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 */
12 * testG722.cpp : Defines the entry point for the console application.
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "typedefs.h"
20 /* include API */
21 #include "g722_interface.h"
23 /* Runtime statistics */
24 #include <time.h>
25 #define CLOCKS_PER_SEC_G722 100000
27 // Forward declaration
28 typedef struct WebRtcG722EncInst G722EncInst;
29 typedef struct WebRtcG722DecInst G722DecInst;
31 /* function for reading audio data from PCM file */
32 int readframe(WebRtc_Word16 *data, FILE *inp, int length)
34 short k, rlen, status = 0;
36 rlen = (short)fread(data, sizeof(WebRtc_Word16), length, inp);
37 if (rlen < length) {
38 for (k = rlen; k < length; k++)
39 data[k] = 0;
40 status = 1;
43 return status;
46 int main(int argc, char* argv[])
48 char inname[60], outbit[40], outname[40];
49 FILE *inp, *outbitp, *outp;
51 int framecnt, endfile;
52 WebRtc_Word16 framelength = 160;
53 G722EncInst *G722enc_inst;
54 G722DecInst *G722dec_inst;
55 int err;
57 /* Runtime statistics */
58 double starttime;
59 double runtime = 0;
60 double length_file;
62 WebRtc_Word16 stream_len = 0;
63 WebRtc_Word16 shortdata[960];
64 WebRtc_Word16 decoded[960];
65 WebRtc_Word16 streamdata[80*3];
66 WebRtc_Word16 speechType[1];
68 /* handling wrong input arguments in the command line */
69 if (argc!=5) {
70 printf("\n\nWrong number of arguments or flag values.\n\n");
72 printf("\n");
73 printf("Usage:\n\n");
74 printf("./testG722.exe framelength infile outbitfile outspeechfile \n\n");
75 printf("with:\n");
76 printf("framelength : Framelength in samples.\n\n");
77 printf("infile : Normal speech input file\n\n");
78 printf("outbitfile : Bitstream output file\n\n");
79 printf("outspeechfile: Speech output file\n\n");
80 exit(0);
84 /* Get frame length */
85 framelength = atoi(argv[1]);
87 /* Get Input and Output files */
88 sscanf(argv[2], "%s", inname);
89 sscanf(argv[3], "%s", outbit);
90 sscanf(argv[4], "%s", outname);
92 if ((inp = fopen(inname,"rb")) == NULL) {
93 printf(" G.722: Cannot read file %s.\n", inname);
94 exit(1);
96 if ((outbitp = fopen(outbit,"wb")) == NULL) {
97 printf(" G.722: Cannot write file %s.\n", outbit);
98 exit(1);
100 if ((outp = fopen(outname,"wb")) == NULL) {
101 printf(" G.722: Cannot write file %s.\n", outname);
102 exit(1);
104 printf("\nInput:%s\nOutput bitstream:%s\nOutput:%s\n", inname, outbit, outname);
106 /* Create and init */
107 WebRtcG722_CreateEncoder((G722EncInst **)&G722enc_inst);
108 WebRtcG722_CreateDecoder((G722DecInst **)&G722dec_inst);
109 WebRtcG722_EncoderInit((G722EncInst *)G722enc_inst);
110 WebRtcG722_DecoderInit((G722DecInst *)G722dec_inst);
113 /* Initialize encoder and decoder */
114 framecnt = 0;
115 endfile = 0;
116 while (endfile == 0) {
117 framecnt++;
119 /* Read speech block */
120 endfile = readframe(shortdata, inp, framelength);
122 /* Start clock before call to encoder and decoder */
123 starttime = clock()/(double)CLOCKS_PER_SEC_G722;
125 /* G.722 encoding + decoding */
126 stream_len = WebRtcG722_Encode((G722EncInst *)G722enc_inst, shortdata, framelength, streamdata);
127 err = WebRtcG722_Decode((G722DecInst *)G722dec_inst, streamdata, stream_len, decoded, speechType);
129 /* Stop clock after call to encoder and decoder */
130 runtime += (double)((clock()/(double)CLOCKS_PER_SEC_G722)-starttime);
132 if (stream_len < 0 || err < 0) {
133 /* exit if returned with error */
134 printf("Error in encoder/decoder\n");
135 } else {
136 /* Write coded bits to file */
137 if (fwrite(streamdata, sizeof(short), stream_len/2,
138 outbitp) != static_cast<size_t>(stream_len/2)) {
139 return -1;
141 /* Write coded speech to file */
142 if (fwrite(decoded, sizeof(short), framelength,
143 outp) != static_cast<size_t>(framelength)) {
144 return -1;
149 WebRtcG722_FreeEncoder((G722EncInst *)G722enc_inst);
150 WebRtcG722_FreeDecoder((G722DecInst *)G722dec_inst);
152 length_file = ((double)framecnt*(double)framelength/16000);
153 printf("\n\nLength of speech file: %.1f s\n", length_file);
154 printf("Time to run G.722: %.2f s (%.2f %% of realtime)\n\n", runtime, (100*runtime/length_file));
155 printf("---------------------END----------------------\n");
157 fclose(inp);
158 fclose(outbitp);
159 fclose(outp);
161 return 0;