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); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* Get version and print */ |
| 75 | WebRtcG711_Version(versionNumber, 40); |
| 76 | |
| 77 | printf("-----------------------------------\n"); |
| 78 | printf("G.711 version: %s\n\n", versionNumber); |
| 79 | /* Get frame length */ |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 80 | int framelength_int = atoi(argv[1]); |
| 81 | if (framelength_int < 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 82 | printf(" G.722: Invalid framelength %d.\n", framelength_int); |
| 83 | exit(1); |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 84 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 85 | framelength = static_cast<size_t>(framelength_int); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 86 | |
| 87 | /* Get compression law */ |
| 88 | strcpy(law, argv[2]); |
| 89 | |
| 90 | /* Get Input and Output files */ |
| 91 | sscanf(argv[3], "%s", inname); |
| 92 | sscanf(argv[4], "%s", outname); |
| 93 | if (argc == 6) { |
| 94 | sscanf(argv[5], "%s", bitname); |
| 95 | if ((bitp = fopen(bitname, "wb")) == NULL) { |
| 96 | printf(" G.711: Cannot read file %s.\n", bitname); |
| 97 | exit(1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 99 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 101 | if ((inp = fopen(inname, "rb")) == NULL) { |
| 102 | printf(" G.711: Cannot read file %s.\n", inname); |
| 103 | exit(1); |
| 104 | } |
| 105 | if ((outp = fopen(outname, "wb")) == NULL) { |
| 106 | printf(" G.711: Cannot write file %s.\n", outname); |
| 107 | exit(1); |
| 108 | } |
| 109 | printf("\nInput: %s\nOutput: %s\n", inname, outname); |
| 110 | if (argc == 6) { |
| 111 | printf("\nBitfile: %s\n", bitname); |
| 112 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | starttime = clock() / (double)CLOCKS_PER_SEC_G711; /* Runtime statistics */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 116 | /* Initialize encoder and decoder */ |
| 117 | framecnt = 0; |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 118 | endfile = false; |
| 119 | while (!endfile) { |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 120 | framecnt++; |
| 121 | /* Read speech block */ |
| 122 | endfile = readframe(shortdata, inp, framelength); |
| 123 | |
| 124 | /* G.711 encoding */ |
| 125 | if (!strcmp(law, "A")) { |
| 126 | /* A-law encoding */ |
kwiberg@webrtc.org | c78cf97 | 2014-11-04 13:23:36 +0000 | [diff] [blame] | 127 | stream_len = WebRtcG711_EncodeA(shortdata, framelength, streamdata); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 128 | if (argc == 6) { |
| 129 | /* Write bits to file */ |
| 130 | if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) != |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 131 | stream_len) { |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 132 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 134 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 135 | WebRtcG711_DecodeA(streamdata, stream_len, decoded, speechType); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 136 | } else if (!strcmp(law, "u")) { |
| 137 | /* u-law encoding */ |
kwiberg@webrtc.org | c78cf97 | 2014-11-04 13:23:36 +0000 | [diff] [blame] | 138 | stream_len = WebRtcG711_EncodeU(shortdata, framelength, streamdata); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 139 | if (argc == 6) { |
| 140 | /* Write bits to file */ |
| 141 | if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) != |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 142 | stream_len) { |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 143 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | } |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 145 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 146 | WebRtcG711_DecodeU(streamdata, stream_len, decoded, speechType); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 147 | } else { |
| 148 | printf("Wrong law mode\n"); |
| 149 | exit(1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 151 | /* Write coded speech to file */ |
| 152 | if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) { |
| 153 | return -1; |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 157 | runtime = (double)(clock() / (double)CLOCKS_PER_SEC_G711 - starttime); |
| 158 | length_file = ((double)framecnt * (double)framelength / 8000); |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 159 | printf("\n\nLength of speech file: %.1f s\n", length_file); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 160 | printf("Time to run G.711: %.2f s (%.2f %% of realtime)\n\n", runtime, |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 161 | (100 * runtime / length_file)); |
| 162 | printf("---------------------END----------------------\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 164 | fclose(inp); |
| 165 | fclose(outp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | |
pbos@webrtc.org | ae4e2b3 | 2013-03-21 13:38:29 +0000 | [diff] [blame] | 167 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | } |