blob: 98f3925faa54e03aa4818f20f0ff5360360eb201 [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 * testG711.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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/codecs/g711/g711_interface.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22/* Runtime statistics */
23#include <time.h>
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000024#define CLOCKS_PER_SEC_G711 1000
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26/* function for reading audio data from PCM file */
Peter Kastingdce40cf2015-08-24 14:52:23 -070027bool readframe(int16_t* data, FILE* inp, size_t length) {
28 size_t rlen = fread(data, sizeof(int16_t), length, inp);
Peter Kasting728d9032015-06-11 14:31:38 -070029 if (rlen >= length)
30 return false;
31 memset(data + rlen, 0, (length - rlen) * sizeof(int16_t));
32 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000035int main(int argc, char* argv[]) {
36 char inname[80], outname[40], bitname[40];
37 FILE* inp;
38 FILE* outp;
39 FILE* bitp = NULL;
Peter Kasting728d9032015-06-11 14:31:38 -070040 int framecnt;
41 bool endfile;
niklase@google.com470e71d2011-07-07 08:21:25 +000042
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 size_t framelength = 80;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000045 /* Runtime statistics */
46 double starttime;
47 double runtime;
48 double length_file;
niklase@google.com470e71d2011-07-07 08:21:25 +000049
Peter Kastingdce40cf2015-08-24 14:52:23 -070050 size_t stream_len = 0;
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000051 int16_t shortdata[480];
52 int16_t decoded[480];
kwiberg@webrtc.org1c6239a2015-02-09 12:55:48 +000053 uint8_t streamdata[1000];
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000054 int16_t speechType[1];
55 char law[2];
56 char versionNumber[40];
niklase@google.com470e71d2011-07-07 08:21:25 +000057
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000058 /* handling wrong input arguments in the command line */
59 if ((argc != 5) && (argc != 6)) {
60 printf("\n\nWrong number of arguments or flag values.\n\n");
niklase@google.com470e71d2011-07-07 08:21:25 +000061
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000062 printf("\n");
63 printf("\nG.711 test application\n\n");
64 printf("Usage:\n\n");
65 printf("./testG711.exe framelength law infile outfile \n\n");
66 printf("framelength: Framelength in samples.\n");
67 printf("law : Coding law, A och u.\n");
68 printf("infile : Normal speech input file\n");
69 printf("outfile : Speech output file\n\n");
70 printf("outbits : Output bitstream file [optional]\n\n");
71 exit(0);
niklase@google.com470e71d2011-07-07 08:21:25 +000072
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000073 }
74
75 /* Get version and print */
76 WebRtcG711_Version(versionNumber, 40);
77
78 printf("-----------------------------------\n");
79 printf("G.711 version: %s\n\n", versionNumber);
80 /* Get frame length */
Peter Kastingdce40cf2015-08-24 14:52:23 -070081 int framelength_int = atoi(argv[1]);
82 if (framelength_int < 0) {
83 printf(" G.722: Invalid framelength %d.\n", framelength_int);
84 exit(1);
Peter Kastingf045e4d2015-06-10 21:15:38 -070085 }
Peter Kastingdce40cf2015-08-24 14:52:23 -070086 framelength = static_cast<size_t>(framelength_int);
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +000087
88 /* Get compression law */
89 strcpy(law, argv[2]);
90
91 /* Get Input and Output files */
92 sscanf(argv[3], "%s", inname);
93 sscanf(argv[4], "%s", outname);
94 if (argc == 6) {
95 sscanf(argv[5], "%s", bitname);
96 if ((bitp = fopen(bitname, "wb")) == NULL) {
97 printf(" G.711: Cannot read file %s.\n", bitname);
98 exit(1);
niklase@google.com470e71d2011-07-07 08:21:25 +000099 }
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000100 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000102 if ((inp = fopen(inname, "rb")) == NULL) {
103 printf(" G.711: Cannot read file %s.\n", inname);
104 exit(1);
105 }
106 if ((outp = fopen(outname, "wb")) == NULL) {
107 printf(" G.711: Cannot write file %s.\n", outname);
108 exit(1);
109 }
110 printf("\nInput: %s\nOutput: %s\n", inname, outname);
111 if (argc == 6) {
112 printf("\nBitfile: %s\n", bitname);
113 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000115 starttime = clock() / (double) CLOCKS_PER_SEC_G711; /* Runtime statistics */
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000117 /* Initialize encoder and decoder */
118 framecnt = 0;
Peter Kasting728d9032015-06-11 14:31:38 -0700119 endfile = false;
120 while (!endfile) {
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000121 framecnt++;
122 /* Read speech block */
123 endfile = readframe(shortdata, inp, framelength);
124
125 /* G.711 encoding */
126 if (!strcmp(law, "A")) {
127 /* A-law encoding */
kwiberg@webrtc.orgc78cf972014-11-04 13:23:36 +0000128 stream_len = WebRtcG711_EncodeA(shortdata, framelength, streamdata);
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000129 if (argc == 6) {
130 /* Write bits to file */
131 if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
Peter Kastingdce40cf2015-08-24 14:52:23 -0700132 stream_len) {
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000133 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 }
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000135 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 WebRtcG711_DecodeA(streamdata, stream_len, decoded, speechType);
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000137 } else if (!strcmp(law, "u")) {
138 /* u-law encoding */
kwiberg@webrtc.orgc78cf972014-11-04 13:23:36 +0000139 stream_len = WebRtcG711_EncodeU(shortdata, framelength, streamdata);
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000140 if (argc == 6) {
141 /* Write bits to file */
142 if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 stream_len) {
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000144 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 }
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000146 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700147 WebRtcG711_DecodeU(streamdata, stream_len, decoded, speechType);
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000148 } else {
149 printf("Wrong law mode\n");
150 exit(1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 /* Write coded speech to file */
153 if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) {
154 return -1;
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000155 }
156 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000158 runtime = (double)(clock() / (double) CLOCKS_PER_SEC_G711 - starttime);
159 length_file = ((double) framecnt * (double) framelength / 8000);
160 printf("\n\nLength of speech file: %.1f s\n", length_file);
161 printf("Time to run G.711: %.2f s (%.2f %% of realtime)\n\n",
162 runtime,
163 (100 * runtime / length_file));
164 printf("---------------------END----------------------\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000166 fclose(inp);
167 fclose(outp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
pbos@webrtc.orgae4e2b32013-03-21 13:38:29 +0000169 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000170}