forked from aikiriao/IMA-ADPCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
328 lines (281 loc) · 10.2 KB
/
main.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details. */
#include "ima_adpcm.h"
#include "wav.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
/* バージョン文字列 */
#define IMAADPCMCUI_VERSION_STRING "1.1.1"
/* ブロックサイズ 今の所1024で固定 */
#define IMAADPCMCUI_BLOCK_SIZE 1024
/* デコード処理 */
static int do_decode(const char *adpcm_filename, const char *decoded_filename)
{
FILE *fp;
struct stat fstat;
uint8_t *buffer;
uint32_t buffer_size;
struct IMAADPCMWAVDecoder *decoder;
struct IMAADPCMWAVHeaderInfo header;
struct WAVFile *wav;
struct WAVFileFormat wavformat;
int16_t *output[IMAADPCM_MAX_NUM_CHANNELS];
uint32_t ch, smpl;
IMAADPCMApiResult ret;
/* ファイルオープン */
fp = fopen(adpcm_filename, "rb");
if (fp == NULL) {
fprintf(stderr, "Failed to open %s. \n", adpcm_filename);
return 1;
}
/* 入力ファイルのサイズ取得 / バッファ領域割り当て */
stat(adpcm_filename, &fstat);
buffer_size = (uint32_t)fstat.st_size;
buffer = (uint8_t *)malloc(buffer_size);
/* バッファ領域にデータをロード */
fread(buffer, sizeof(uint8_t), buffer_size, fp);
fclose(fp);
/* デコーダ作成 */
decoder = IMAADPCMWAVDecoder_Create(NULL, 0);
/* ヘッダ読み取り */
if ((ret = IMAADPCMWAVDecoder_DecodeHeader(buffer, buffer_size, &header))
!= IMAADPCM_APIRESULT_OK) {
fprintf(stderr, "Failed to read header. API result: %d \n", ret);
return 1;
}
/* 出力バッファ領域確保 */
for (ch = 0; ch < header.num_channels; ch++) {
output[ch] = malloc(sizeof(int16_t) * header.num_samples);
}
/* 全データをデコード */
if ((ret = IMAADPCMWAVDecoder_DecodeWhole(decoder,
buffer, buffer_size, output,
header.num_channels, header.num_samples)) != IMAADPCM_APIRESULT_OK) {
fprintf(stderr, "Failed to decode. API result: %d \n", ret);
return 1;
}
/* 出力ファイルを作成 */
wavformat.data_format = WAV_DATA_FORMAT_PCM;
wavformat.num_channels = header.num_channels;
wavformat.sampling_rate = header.sampling_rate;
wavformat.bits_per_sample = 16;
wavformat.num_samples = header.num_samples;
wav = WAV_Create(&wavformat);
/* PCM書き出し */
for (ch = 0; ch < header.num_channels; ch++) {
for (smpl = 0; smpl < header.num_samples; smpl++) {
WAVFile_PCM(wav, smpl, ch) = (output[ch][smpl] << 16);
}
}
WAV_WriteToFile(decoded_filename, wav);
IMAADPCMWAVDecoder_Destroy(decoder);
for (ch = 0; ch < header.num_channels; ch++) {
free(output[ch]);
}
WAV_Destroy(wav);
free(buffer);
return 0;
}
/* エンコード処理 */
static int do_encode(const char *wav_file, const char *encoded_filename)
{
FILE *fp;
struct WAVFile *wavfile;
struct stat fstat;
int16_t *input[IMAADPCM_MAX_NUM_CHANNELS];
uint32_t ch, smpl, buffer_size, output_size;
uint32_t num_channels, num_samples;
uint8_t *buffer;
struct IMAADPCMWAVEncodeParameter enc_param;
struct IMAADPCMWAVEncoder *encoder;
IMAADPCMApiResult api_result;
/* 入力wav取得 */
wavfile = WAV_CreateFromFile(wav_file);
if (wavfile == NULL) {
fprintf(stderr, "Failed to open %s. \n", wav_file);
return 1;
}
num_channels = wavfile->format.num_channels;
num_samples = wavfile->format.num_samples;
/* 出力データの領域割当て */
for (ch = 0; ch < num_channels; ch++) {
input[ch] = malloc(sizeof(int16_t) * num_samples);
}
/* 入力wavと同じサイズの出力領域を確保(増えることはないと期待) */
stat(wav_file, &fstat);
buffer_size = (uint32_t)fstat.st_size;
buffer = malloc(buffer_size);
/* 16bit幅でデータ取得 */
for (ch = 0; ch < num_channels; ch++) {
for (smpl = 0; smpl < num_samples; smpl++) {
input[ch][smpl] = (int16_t)(WAVFile_PCM(wavfile, smpl, ch) >> 16);
}
}
/* ハンドル作成 */
encoder = IMAADPCMWAVEncoder_Create(NULL, 0);
/* エンコードパラメータをセット */
enc_param.num_channels = (uint16_t)num_channels;
enc_param.sampling_rate = wavfile->format.sampling_rate;
enc_param.bits_per_sample = 4;
enc_param.block_size = IMAADPCMCUI_BLOCK_SIZE;
if ((api_result = IMAADPCMWAVEncoder_SetEncodeParameter(encoder, &enc_param))
!= IMAADPCM_APIRESULT_OK) {
fprintf(stderr, "Failed to set encode parameter. API result:%d \n", api_result);
return 1;
}
/* エンコード */
if ((api_result = IMAADPCMWAVEncoder_EncodeWhole(
encoder, (const int16_t *const *)input, num_samples,
buffer, buffer_size, &output_size)) != IMAADPCM_APIRESULT_OK) {
fprintf(stderr, "Failed to encode. API result:%d \n", api_result);
return 1;
}
/* ファイル書き出し */
fp = fopen(encoded_filename, "wb");
if (fp == NULL) {
fprintf(stderr, "Failed to open output file %s \n", encoded_filename);
return 1;
}
if (fwrite(buffer, sizeof(uint8_t), output_size, fp) < output_size) {
fprintf(stderr, "Warning: failed to write encoded data \n");
return 1;
}
fclose(fp);
/* 領域開放 */
IMAADPCMWAVEncoder_Destroy(encoder);
free(buffer);
for (ch = 0; ch < num_channels; ch++) {
free(input[ch]);
}
WAV_Destroy(wavfile);
return 0;
}
/* 残差出力処理 */
static int do_residual_output(const char *wav_file, const char *residual_filename)
{
struct WAVFile *wavfile;
struct stat fstat;
int16_t *pcmdata[IMAADPCM_MAX_NUM_CHANNELS];
uint32_t ch, smpl, buffer_size, output_size;
uint32_t num_channels, num_samples;
uint8_t *buffer;
struct IMAADPCMWAVEncodeParameter enc_param;
struct IMAADPCMWAVEncoder *encoder;
struct IMAADPCMWAVDecoder *decoder;
IMAADPCMApiResult api_result;
/* 入力wav取得 */
wavfile = WAV_CreateFromFile(wav_file);
if (wavfile == NULL) {
fprintf(stderr, "Failed to open %s. \n", wav_file);
return 1;
}
num_channels = wavfile->format.num_channels;
num_samples = wavfile->format.num_samples;
/* 出力データの領域割当て */
for (ch = 0; ch < num_channels; ch++) {
pcmdata[ch] = malloc(sizeof(int16_t) * num_samples);
}
/* 入力wavと同じサイズの出力領域を確保(増えることはないと期待) */
stat(wav_file, &fstat);
buffer_size = (uint32_t)fstat.st_size;
buffer = malloc(buffer_size);
/* 16bit幅でデータ取得 */
for (ch = 0; ch < num_channels; ch++) {
for (smpl = 0; smpl < num_samples; smpl++) {
pcmdata[ch][smpl] = (int16_t)(WAVFile_PCM(wavfile, smpl, ch) >> 16);
}
}
/* ハンドル作成 */
encoder = IMAADPCMWAVEncoder_Create(NULL, 0);
decoder = IMAADPCMWAVDecoder_Create(NULL, 0);
/* エンコードパラメータをセット */
enc_param.num_channels = (uint16_t)num_channels;
enc_param.sampling_rate = wavfile->format.sampling_rate;
enc_param.bits_per_sample = 4;
enc_param.block_size = IMAADPCMCUI_BLOCK_SIZE;
if ((api_result = IMAADPCMWAVEncoder_SetEncodeParameter(encoder, &enc_param))
!= IMAADPCM_APIRESULT_OK) {
fprintf(stderr, "Failed to set encode parameter. API result:%d \n", api_result);
return 1;
}
/* エンコード */
if ((api_result = IMAADPCMWAVEncoder_EncodeWhole(
encoder, (const int16_t *const *)pcmdata, num_samples,
buffer, buffer_size, &output_size)) != IMAADPCM_APIRESULT_OK) {
fprintf(stderr, "Failed to encode. API result:%d \n", api_result);
return 1;
}
/* そのままデコード */
if ((api_result = IMAADPCMWAVDecoder_DecodeWhole(decoder,
buffer, output_size, pcmdata, num_channels, num_samples)) != IMAADPCM_APIRESULT_OK) {
fprintf(stderr, "Failed to decode. API result: %d \n", api_result);
return 1;
}
/* 残差(量子化誤差)計算 */
for (ch = 0; ch < num_channels; ch++) {
for (smpl = 0; smpl < num_samples; smpl++) {
int32_t decoded = pcmdata[ch][smpl] << 16;
WAVFile_PCM(wavfile, smpl, ch) -= decoded;
}
}
/* 残差をファイルに書き出し */
WAV_WriteToFile(residual_filename, wavfile);
/* 領域開放 */
IMAADPCMWAVEncoder_Destroy(encoder);
IMAADPCMWAVDecoder_Destroy(decoder);
free(buffer);
for (ch = 0; ch < num_channels; ch++) {
free(pcmdata[ch]);
}
WAV_Destroy(wavfile);
return 0;
}
/* 使用法の印字 */
static void print_usage(const char* program_name)
{
printf(
"IMA-ADPCM encoder/decoder Version." IMAADPCMCUI_VERSION_STRING "\n" \
"Usage: %s -[edr] INPUT.wav OUTPUT.wav \n" \
"-e: encode mode (PCM wav -> IMA-ADPCM wav)\n" \
"-d: decode mode (IMA-ADPCM wav -> PCM wav)\n" \
"-r: output residual (PCM wav -> Residual PCM wav)\n",
program_name);
}
/* メインエントリ */
int main(int argc, char **argv)
{
int ret;
const char *option;
const char *in_filename, *out_filename;
/* 引数の数が想定外 */
if (argc != 4) {
print_usage(argv[0]);
return 1;
}
/* オプション文字列の取得 */
option = argv[1];
in_filename = argv[2];
out_filename = argv[3];
/* 引数が無効 */
if ((option == NULL) || (in_filename == NULL) || (out_filename == NULL)) {
print_usage(argv[0]);
return 1;
}
/* エンコード/デコード呼び分け */
if (strncmp(option, "-e", 2) == 0) {
ret = do_encode(in_filename, out_filename);
} else if (strncmp(option, "-d", 2) == 0) {
ret = do_decode(in_filename, out_filename);
} else if (strncmp(option, "-r", 2) == 0) {
ret = do_residual_output(in_filename, out_filename);
} else {
print_usage(argv[0]);
return 1;
}
return ret;
}