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> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | /* include API */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_coding/codecs/g722/g722_interface.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | /* Runtime statistics */ |
| 23 | #include <time.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | #define CLOCKS_PER_SEC_G722 100000 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
| 26 | // Forward declaration |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | typedef struct WebRtcG722EncInst G722EncInst; |
| 28 | typedef struct WebRtcG722DecInst G722DecInst; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
| 30 | /* function for reading audio data from PCM file */ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | bool 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | int main(int argc, char* argv[]) { |
| 40 | char inname[60], outbit[40], outname[40]; |
| 41 | FILE *inp, *outbitp, *outp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 43 | int framecnt; |
| 44 | bool endfile; |
| 45 | size_t framelength = 160; |
| 46 | G722EncInst* G722enc_inst; |
| 47 | G722DecInst* G722dec_inst; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | /* Runtime statistics */ |
| 50 | double starttime; |
| 51 | double runtime = 0; |
| 52 | double length_file; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 54 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | /* 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 75 | /* 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 135 | /* Write coded speech to file */ |
| 136 | if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) { |
| 137 | return -1; |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 138 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 139 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 141 | WebRtcG722_FreeEncoder((G722EncInst*)G722enc_inst); |
| 142 | WebRtcG722_FreeDecoder((G722DecInst*)G722dec_inst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 144 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 150 | fclose(inp); |
| 151 | fclose(outbitp); |
| 152 | fclose(outp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 154 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | } |