-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.c
197 lines (171 loc) · 4.94 KB
/
decoder.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
/*
* ===================================================================
* 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
* ===================================================================
*
*/
#include <stdlib.h>
#include <stdio.h>
#include "interf_dec.h"
#include "sp_dec.h"
#include "typedef.h"
#include "board.h"
#ifndef ETSI
#ifndef IF2
#include <string.h>
#define AMR_MAGIC_NUMBER "#!AMR\n"
#endif
#endif
#define PATH_STRING_MAX_LEN 256
char file_speech_path[PATH_STRING_MAX_LEN];
char file_analysis_path[PATH_STRING_MAX_LEN];
static void DecoderUsage(void)
{
rt_kprintf("Usage: decoder [analysis_file] [speech_file]\n");
rt_kprintf(" example : decoder out.amr out.wav\n");
}
void DeocoderCopyright(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 decoder main program
*
* Usage: decoder bitstream_file synthesis_file
*
* 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.
*
* Format for synthesis_file:
* Speech is written to a 16 bit 8kHz file.
*
* ETSI bitstream file format is defined using ETSI as preprocessor
* definition
* Returns:
* 0
*/
static void decoder_thread(void *parameter)
{
FILE *file_speech, *file_analysis;
short synth[160];
int frames = 0;
int *destate;
int read_size;
#ifndef ETSI
unsigned char analysis[32];
enum Mode dec_mode;
#ifdef IF2
short block_size[16] =
{12, 13, 15, 17, 18, 20, 25, 30, 5, 0, 0, 0, 0, 0, 0, 0};
#else
char magic[8];
short block_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
#endif
#else
short analysis[250];
#endif
/* Process command line options */
rt_tick_t tick = rt_tick_get_millisecond();
// open speech file
file_speech = fopen(file_speech_path, "wb");
if (file_speech == NULL)
{
rt_kprintf("open speech file '%s' error !\n", file_speech_path);
return;
}
// open analysis file
file_analysis = fopen(file_analysis_path, "rb");
if (file_analysis == NULL)
{
rt_kprintf("open analysis file '%s' error !\n", file_analysis_path);
fclose(file_speech);
return;
}
/* init decoder */
destate = Decoder_Interface_init();
DeocoderCopyright();
#ifndef ETSI
#ifndef IF2
/* read and verify magic number */
fread(magic, sizeof(char), strlen(AMR_MAGIC_NUMBER), file_analysis);
if (strncmp(magic, AMR_MAGIC_NUMBER, strlen(AMR_MAGIC_NUMBER)))
{
rt_kprintf("%s%s\n", "Invalid magic number: ", magic);
fclose(file_speech);
fclose(file_analysis);
return;
}
#endif
#endif
#ifndef ETSI
/* find mode, read file */
while (fread(analysis, sizeof(unsigned char), 1, file_analysis) > 0)
{
#ifdef IF2
dec_mode = analysis[0] & 0x000F;
#else
dec_mode = (analysis[0] >> 3) & 0x000F;
#endif
read_size = block_size[dec_mode];
fread(&analysis[1], sizeof(char), read_size, file_analysis);
#else
read_size = 250;
/* read file */
while (fread(analysis, sizeof(short), read_size, file_analysis) > 0)
{
#endif
frames++;
/* call decoder */
Decoder_Interface_Decode(destate, analysis, synth, 0);
fwrite(synth, sizeof(short), 160, file_speech);
}
Decoder_Interface_exit(destate);
fclose(file_speech);
fclose(file_analysis);
rt_kprintf("%s%i%s\n", "Decoded ", frames, " frames.");
rt_kprintf("decoder used %d.%03d s\n", (rt_tick_get_millisecond() - tick) / 1000, (rt_tick_get_millisecond() - tick) % 1000);
return;
}
static void decoder(int argc, char *argv[])
{
if (argc != 3)
{
DecoderUsage();
return;
}
// set file_analysis_path
memset(file_analysis_path, 0, PATH_STRING_MAX_LEN);
memcpy(file_analysis_path, argv[1], strlen(argv[1]));
// set file_speech_path
memset(file_speech_path, 0, PATH_STRING_MAX_LEN);
memcpy(file_speech_path, argv[2], strlen(argv[2]));
// create thread
rt_thread_t thread = rt_thread_create("decoder", decoder_thread, RT_NULL, 4 * 1024, 21, 10);
if (thread != RT_NULL)
rt_thread_startup(thread);
return;
}
#ifdef AMRNB_USING_CMD
MSH_CMD_EXPORT_ALIAS(decoder, amr_decoder, AMR - NB decoder cmd);
#endif