blob: 9f2155d0f7f9a75ef286d8709506723089d38eee [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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>
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19/* include API */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/codecs/g722/g722_interface.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22/* Runtime statistics */
23#include <time.h>
Yves Gerey665174f2018-06-19 15:03:05 +020024#define CLOCKS_PER_SEC_G722 100000
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26// Forward declaration
Yves Gerey665174f2018-06-19 15:03:05 +020027typedef struct WebRtcG722EncInst G722EncInst;
28typedef struct WebRtcG722DecInst G722DecInst;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
30/* function for reading audio data from PCM file */
Yves Gerey665174f2018-06-19 15:03:05 +020031bool 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;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
Yves Gerey665174f2018-06-19 15:03:05 +020039int main(int argc, char* argv[]) {
40 char inname[60], outbit[40], outname[40];
41 FILE *inp, *outbitp, *outp;
niklase@google.com470e71d2011-07-07 08:21:25 +000042
Yves Gerey665174f2018-06-19 15:03:05 +020043 int framecnt;
44 bool endfile;
45 size_t framelength = 160;
46 G722EncInst* G722enc_inst;
47 G722DecInst* G722dec_inst;
niklase@google.com470e71d2011-07-07 08:21:25 +000048
Yves Gerey665174f2018-06-19 15:03:05 +020049 /* Runtime statistics */
50 double starttime;
51 double runtime = 0;
52 double length_file;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
Yves Gerey665174f2018-06-19 15:03:05 +020054 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];
niklase@google.com470e71d2011-07-07 08:21:25 +000059
Yves Gerey665174f2018-06-19 15:03:05 +020060 /* handling wrong input arguments in the command line */
61 if (argc != 5) {
62 printf("\n\nWrong number of arguments or flag values.\n\n");
niklase@google.com470e71d2011-07-07 08:21:25 +000063
Yves Gerey665174f2018-06-19 15:03:05 +020064 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +000074
Yves Gerey665174f2018-06-19 15:03:05 +020075 /* 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;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 }
Yves Gerey665174f2018-06-19 15:03:05 +0200135 /* Write coded speech to file */
136 if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) {
137 return -1;
Peter Kastingf045e4d2015-06-10 21:15:38 -0700138 }
Yves Gerey665174f2018-06-19 15:03:05 +0200139 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
Yves Gerey665174f2018-06-19 15:03:05 +0200141 WebRtcG722_FreeEncoder((G722EncInst*)G722enc_inst);
142 WebRtcG722_FreeDecoder((G722DecInst*)G722dec_inst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
Yves Gerey665174f2018-06-19 15:03:05 +0200144 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");
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
Yves Gerey665174f2018-06-19 15:03:05 +0200150 fclose(inp);
151 fclose(outbitp);
152 fclose(outp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
Yves Gerey665174f2018-06-19 15:03:05 +0200154 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}