niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | * 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 20 | #include "modules/audio_coding/codecs/g711/g711_interface.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | /* Runtime statistics */ |
| 23 | #include <time.h> |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 24 | #define CLOCKS_PER_SEC_G711 1000 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
| 26 | /* function for reading audio data from PCM file */ |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 27 | bool readframe(int16_t* data, FILE* inp, size_t length) { |
| 28 | size_t rlen = fread(data, sizeof(int16_t), length, inp); |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 29 | if (rlen >= length) |
| 30 | return false; |
| 31 | memset(data + rlen, 0, (length - rlen) * sizeof(int16_t)); |
| 32 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 35 | int main(int argc, char* argv[]) { |
| 36 | char inname[80], outname[40], bitname[40]; |
| 37 | FILE* inp; |
| 38 | FILE* outp; |
| 39 | FILE* bitp = NULL; |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 40 | int framecnt; |
| 41 | bool endfile; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 43 | size_t framelength = 80; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 45 | /* Runtime statistics */ |
| 46 | double starttime; |
| 47 | double runtime; |
| 48 | double length_file; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 50 | size_t stream_len = 0; |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 51 | int16_t shortdata[480]; |
| 52 | int16_t decoded[480]; |
kwiberg@webrtc.org | 1c6239a | 2015-02-09 12:55:48 +0000 | [diff] [blame] | 53 | uint8_t streamdata[1000]; |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 54 | int16_t speechType[1]; |
| 55 | char law[2]; |
| 56 | char versionNumber[40]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 58 | /* 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 62 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 73 | } |
| 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 Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 81 | 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 Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 85 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 86 | framelength = static_cast<size_t>(framelength_int); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 87 | |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | } |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 100 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 102 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 115 | starttime = clock() / (double) CLOCKS_PER_SEC_G711; /* Runtime statistics */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 117 | /* Initialize encoder and decoder */ |
| 118 | framecnt = 0; |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 119 | endfile = false; |
| 120 | while (!endfile) { |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 121 | 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.org | c78cf97 | 2014-11-04 13:23:36 +0000 | [diff] [blame] | 128 | stream_len = WebRtcG711_EncodeA(shortdata, framelength, streamdata); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 129 | if (argc == 6) { |
| 130 | /* Write bits to file */ |
| 131 | if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) != |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 132 | stream_len) { |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 133 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | } |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 135 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 136 | WebRtcG711_DecodeA(streamdata, stream_len, decoded, speechType); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 137 | } else if (!strcmp(law, "u")) { |
| 138 | /* u-law encoding */ |
kwiberg@webrtc.org | c78cf97 | 2014-11-04 13:23:36 +0000 | [diff] [blame] | 139 | stream_len = WebRtcG711_EncodeU(shortdata, framelength, streamdata); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 140 | if (argc == 6) { |
| 141 | /* Write bits to file */ |
| 142 | if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) != |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 143 | stream_len) { |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 144 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | } |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 146 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 147 | WebRtcG711_DecodeU(streamdata, stream_len, decoded, speechType); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 148 | } else { |
| 149 | printf("Wrong law mode\n"); |
| 150 | exit(1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 152 | /* Write coded speech to file */ |
| 153 | if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) { |
| 154 | return -1; |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 158 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 166 | fclose(inp); |
| 167 | fclose(outp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 169 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | } |