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 | * testG722.cpp : Defines the entry point for the console application. |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 18 | #include "typedefs.h" // NOLINT(build/include) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | /* include API */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/audio_coding/codecs/g722/g722_interface.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
| 23 | /* Runtime statistics */ |
| 24 | #include <time.h> |
| 25 | #define CLOCKS_PER_SEC_G722 100000 |
| 26 | |
| 27 | // Forward declaration |
| 28 | typedef struct WebRtcG722EncInst G722EncInst; |
| 29 | typedef struct WebRtcG722DecInst G722DecInst; |
| 30 | |
| 31 | /* function for reading audio data from PCM file */ |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 32 | bool readframe(int16_t *data, FILE *inp, size_t length) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 34 | size_t rlen = fread(data, sizeof(int16_t), length, inp); |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 35 | if (rlen >= length) |
| 36 | return false; |
| 37 | memset(data + rlen, 0, (length - rlen) * sizeof(int16_t)); |
| 38 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | int main(int argc, char* argv[]) |
| 42 | { |
| 43 | char inname[60], outbit[40], outname[40]; |
| 44 | FILE *inp, *outbitp, *outp; |
| 45 | |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 46 | int framecnt; |
| 47 | bool endfile; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 48 | size_t framelength = 160; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | G722EncInst *G722enc_inst; |
| 50 | G722DecInst *G722dec_inst; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
| 52 | /* Runtime statistics */ |
| 53 | double starttime; |
kjellander@webrtc.org | 543c3ea | 2011-11-23 12:20:35 +0000 | [diff] [blame] | 54 | double runtime = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | double length_file; |
| 56 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 57 | size_t stream_len = 0; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 58 | int16_t shortdata[960]; |
| 59 | int16_t decoded[960]; |
kwiberg@webrtc.org | 0cd5558 | 2014-12-02 11:45:51 +0000 | [diff] [blame] | 60 | uint8_t streamdata[80 * 6]; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 61 | int16_t speechType[1]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
| 63 | /* handling wrong input arguments in the command line */ |
| 64 | if (argc!=5) { |
| 65 | printf("\n\nWrong number of arguments or flag values.\n\n"); |
| 66 | |
| 67 | printf("\n"); |
| 68 | printf("Usage:\n\n"); |
| 69 | printf("./testG722.exe framelength infile outbitfile outspeechfile \n\n"); |
| 70 | printf("with:\n"); |
| 71 | printf("framelength : Framelength in samples.\n\n"); |
| 72 | printf("infile : Normal speech input file\n\n"); |
| 73 | printf("outbitfile : Bitstream output file\n\n"); |
| 74 | printf("outspeechfile: Speech output file\n\n"); |
| 75 | exit(0); |
| 76 | |
| 77 | } |
| 78 | |
| 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) { |
| 82 | printf(" G.722: Invalid framelength %d.\n", framelength_int); |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 83 | exit(1); |
| 84 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 85 | framelength = static_cast<size_t>(framelength_int); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
| 87 | /* Get Input and Output files */ |
| 88 | sscanf(argv[2], "%s", inname); |
| 89 | sscanf(argv[3], "%s", outbit); |
| 90 | sscanf(argv[4], "%s", outname); |
| 91 | |
| 92 | if ((inp = fopen(inname,"rb")) == NULL) { |
| 93 | printf(" G.722: Cannot read file %s.\n", inname); |
| 94 | exit(1); |
| 95 | } |
| 96 | if ((outbitp = fopen(outbit,"wb")) == NULL) { |
| 97 | printf(" G.722: Cannot write file %s.\n", outbit); |
| 98 | exit(1); |
| 99 | } |
| 100 | if ((outp = fopen(outname,"wb")) == NULL) { |
| 101 | printf(" G.722: Cannot write file %s.\n", outname); |
| 102 | exit(1); |
| 103 | } |
| 104 | printf("\nInput:%s\nOutput bitstream:%s\nOutput:%s\n", inname, outbit, outname); |
| 105 | |
| 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); |
| 111 | |
| 112 | |
| 113 | /* Initialize encoder and decoder */ |
| 114 | framecnt = 0; |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 115 | endfile = false; |
| 116 | while (!endfile) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | framecnt++; |
| 118 | |
| 119 | /* Read speech block */ |
| 120 | endfile = readframe(shortdata, inp, framelength); |
| 121 | |
| 122 | /* Start clock before call to encoder and decoder */ |
| 123 | starttime = clock()/(double)CLOCKS_PER_SEC_G722; |
| 124 | |
| 125 | /* G.722 encoding + decoding */ |
| 126 | stream_len = WebRtcG722_Encode((G722EncInst *)G722enc_inst, shortdata, framelength, streamdata); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 127 | WebRtcG722_Decode(G722dec_inst, streamdata, stream_len, decoded, |
| 128 | speechType); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | |
| 130 | /* Stop clock after call to encoder and decoder */ |
| 131 | runtime += (double)((clock()/(double)CLOCKS_PER_SEC_G722)-starttime); |
| 132 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 133 | /* Write coded bits to file */ |
| 134 | if (fwrite(streamdata, sizeof(short), stream_len / 2, outbitp) != |
| 135 | stream_len / 2) { |
| 136 | return -1; |
| 137 | } |
| 138 | /* Write coded speech to file */ |
| 139 | if (fwrite(decoded, sizeof(short), framelength, outp) != |
| 140 | framelength) { |
| 141 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | WebRtcG722_FreeEncoder((G722EncInst *)G722enc_inst); |
| 146 | WebRtcG722_FreeDecoder((G722DecInst *)G722dec_inst); |
| 147 | |
| 148 | length_file = ((double)framecnt*(double)framelength/16000); |
| 149 | printf("\n\nLength of speech file: %.1f s\n", length_file); |
| 150 | printf("Time to run G.722: %.2f s (%.2f %% of realtime)\n\n", runtime, (100*runtime/length_file)); |
| 151 | printf("---------------------END----------------------\n"); |
| 152 | |
| 153 | fclose(inp); |
| 154 | fclose(outbitp); |
| 155 | fclose(outp); |
| 156 | |
| 157 | return 0; |
| 158 | } |