-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder.c
262 lines (231 loc) · 6.77 KB
/
encoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* ===================================================================
* TS 26.104
* REL-5 V5.4.0 2004-03
* REL-6 V6.1.0 2004-03
* REL-15 V15.1.0 2018-07
* 3GPP AMR Floating-point Speech Codec
* ===================================================================
*
*/
/*
* encoder.c
*
*
* Project:
* AMR Floating-Point Codec
*
* Contains:
* Speech encoder main program
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "typedef.h"
#include "interf_enc.h"
#include "board.h"
#ifndef ETSI
#ifndef IF2
#define AMR_MAGIC_NUMBER "#!AMR\n"
#endif
#endif
#define PATH_STRING_MAX_LEN 256
#define MODE_MAX_LEN 10
static char file_speech_path[PATH_STRING_MAX_LEN];
static char file_encoded_path[PATH_STRING_MAX_LEN];
static char file_mode_path[MODE_MAX_LEN];
static const short modeConv[] = {475, 515, 59, 67, 74, 795, 102, 122};
static void EncoderUsage(void)
{
rt_kprintf("Usage: encoder -dtx [mode] [speech_file] [bitstream_file]\n");
rt_kprintf(" mode : MR475, MR515, MR59, MR67, MR74, MR795, MR102, MR122\n");
rt_kprintf(" example : encoder -dtx MR102 test.wav out.amr\n");
}
static void EncoderCopyright(void)
{
rt_kprintf("Copyright:\n");
rt_kprintf("========================================\n");
rt_kprintf(" TS 26.104 \n");
rt_kprintf(" REL-16 V16.0.0 2018-09 \n");
rt_kprintf(" 3GPP AMR Floating-point Speech Encoder \n");
rt_kprintf("========================================\n");
}
/*
* main
*
*
* Function:
* Speech encoder main program
*
* Usage: encoder speech_file bitstream_file mode dtx mode_file
*
* Format for speech_file:
* Speech is read from a binary file of 16 bits data.
*
* Format for ETSI bitstream file:
* 1 word (2-byte) for the TX frame type
* 244 words (2-byte) containing 244 bits.
* Bit 0 = 0x0000 and Bit 1 = 0x0001
* 1 word (2-byte) for the mode indication
* 4 words for future use, currently written as zero
*
* Format for 3GPP bitstream file:
* Holds mode information and bits packed to octets.
* Size is from 1 byte to 31 bytes.
*
* ETSI bitstream file format is defined using ETSI as preprocessor
* definition
*
* mode : MR475, MR515, MR59, MR67, MR74, MR795, MR102, MR122
* mode_file : reads mode information from a file
* Returns:
* 0
*/
static void encoder_thread(void *parameter)
{
/* file strucrures */
FILE *file_speech = NULL;
FILE *file_encoded = NULL;
FILE *file_mode = NULL;
/* input speech vector */
short speech[160];
/* counters */
int byte_counter, frames = 0, bytes = 0;
/* pointer to encoder state structure */
int *enstate;
/* requested mode */
enum Mode req_mode = MR122;
int dtx = 0;
/* temporary variables */
char mode_string[9];
long mode_tmp;
/* bitstream filetype */
#ifndef ETSI
unsigned char serial_data[32];
#else
short serial_data[250] =
{0};
#endif
/* Process command line options */
rt_tick_t tick = rt_tick_get_millisecond();
// open encoded file
file_encoded = fopen(file_encoded_path, "wb");
if (file_encoded == NULL)
{
rt_kprintf("open encoded file '%s' error !\n", file_encoded_path);
return;
}
// open speech file
file_speech = fopen(file_speech_path, "rb");
if (file_speech == NULL)
{
rt_kprintf("open speech file '%s' error !\n", file_speech_path);
fclose(file_encoded);
return;
}
// set code rate
mode_tmp = strtol(&file_mode_path[2], NULL, 0);
for (req_mode = 0; req_mode < 8; req_mode++)
{
if (mode_tmp == modeConv[req_mode])
break;
}
if (req_mode == 8)
{
rt_kprintf("mode error !\n");
fclose(file_speech);
fclose(file_encoded);
if (file_mode != NULL)
fclose(file_mode);
return;
}
dtx = 1;
enstate = Encoder_Interface_init(dtx);
EncoderCopyright();
#ifndef VAD2
rt_kprintf("%s\n", "Code compiled with VAD option: VAD1");
#else
rt_kprintf("%s\n", "Code compiled with VAD option: VAD2");
#endif
#ifndef ETSI
#ifndef IF2
/* write magic number to indicate single channel AMR file storage format */
bytes = fwrite(AMR_MAGIC_NUMBER, sizeof(char), strlen(AMR_MAGIC_NUMBER), file_encoded);
#endif
#endif
/* read file */
while (fread(speech, sizeof(Word16), 160, file_speech) > 0)
{
/* read mode */
if (file_mode != NULL)
{
req_mode = 8;
if (fscanf(file_mode, "%9s\n", mode_string) != EOF)
{
mode_tmp = strtol(&mode_string[2], NULL, 0);
for (req_mode = 0; req_mode < 8; req_mode++)
{
if (mode_tmp == modeConv[req_mode])
{
break;
}
}
}
if (req_mode == 8)
{
break;
}
}
frames++;
/* call encoder */
byte_counter = Encoder_Interface_Encode(enstate, req_mode, speech, serial_data, 0);
if (frames % 64 == 0)
rt_kprintf(".");
bytes += byte_counter;
fwrite(serial_data, sizeof(UWord8), byte_counter, file_encoded);
fflush(file_encoded);
}
Encoder_Interface_exit(enstate);
rt_kprintf("\n");
#ifndef ETSI
#ifdef IF2
rt_kprintf("%s%i%s%i%s\n", "Frame structure AMR IF2: ", frames, " frames, ", bytes, " bytes.");
#else
rt_kprintf("%s%i%s%i%s\n", "Frame structure AMR MIME file storage format: ", frames, " frames, ", bytes,
" bytes.");
#endif
#else
rt_kprintf("%s%i%s\n", "Frame structure AMR ETSI: ", frames, " frames. ");
#endif
fclose(file_speech);
fclose(file_encoded);
if (file_mode != NULL)
fclose(file_mode);
rt_kprintf("encoder used %d.%03d s\n", (rt_tick_get_millisecond() - tick) / 1000, (rt_tick_get_millisecond() - tick) % 1000);
}
static void encoder(int argc, char *argv[])
{
if (argc != 5)
{
EncoderUsage();
return;
}
// set file_mode_path
memset(file_mode_path, 0, MODE_MAX_LEN);
memcpy(file_mode_path, argv[2], strlen(argv[2]));
// set file_speech_path
memset(file_speech_path, 0, PATH_STRING_MAX_LEN);
memcpy(file_speech_path, argv[3], strlen(argv[3]));
// set file_encoded_path
memset(file_encoded_path, 0, PATH_STRING_MAX_LEN);
memcpy(file_encoded_path, argv[4], strlen(argv[4]));
// create thread
rt_thread_t thread = rt_thread_create("encoder", encoder_thread, RT_NULL, 24 * 1024, 21, 10);
if (thread != RT_NULL)
rt_thread_startup(thread);
return;
}
#ifdef AMRNB_USING_CMD
MSH_CMD_EXPORT_ALIAS(encoder, amr_encoder, AMR - NB encoder cmd);
#endif