-
Notifications
You must be signed in to change notification settings - Fork 8
/
flac.c
executable file
·305 lines (259 loc) · 8.92 KB
/
flac.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
/*
* MDFourier
* A Fourier Transform analysis tool to compare game console audio
* http://junkerhq.net/MDFourier/
*
* Copyright (C)2019-2021 Artemio Urbina
*
* This file is part of the 240p Test Suite
*
* You can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Requires the FFTW library:
* http://www.fftw.org/
*
*/
/*
* This implements libFLAC to decode a FLAC file to a data structure in RAM
* It only supports 16/24-bit mono/stereo files.
*
* Complete API documentation can be found at:
* http://xiph.org/flac/api/
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "flac.h"
#include "log.h"
#include "freq.h"
#include "FLAC/stream_decoder.h"
#include <ctype.h>
#define FLAC_ERR_STR 1024
int flacInternalMDFErrors = 0;
char flacInternalErrorStr[FLAC_ERR_STR];
extern char *getFilenameExtension(char *filename);
extern int getExtensionLength(char *filename);
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
char* getflacErrorStr(void)
{
if(flacInternalErrorStr[0] != 0)
return(flacInternalErrorStr);
else
return NULL;
}
int flacErrorReported(void)
{
return(flacInternalMDFErrors);
}
char *strtoupper(char *str)
{
unsigned char *p = (unsigned char *)str;
while (*p) {
*p = (unsigned char)toupper((unsigned char)*p);
p++;
}
return str;
}
int IsFlac(char *name)
{
int len = 0;
const char *ext = NULL;
ext = getFilenameExtension(name);
if(ext)
{
char extHC[10];
len = strlen(ext);
if(len != 4)
return 0;
memset(extHC, 0, sizeof(char)*10);
strncpy(extHC, ext, len);
if(strncmp(strtoupper(extHC), "FLAC", 4) == 0)
return 1;
}
return 0;
}
// Fill header data for MDWave/SaveWAVEChunk
int FillRIFFHeader(wav_hdr *header)
{
if(!header)
{
logmsg("ERROR: Received NULL RIFF header\n");
return 0;
}
// RIFF
memcpy(header->riff.RIFF, "RIFF", sizeof(char)*4);
header->riff.ChunkSize = header->data.DataSize+36;
memcpy(header->riff.WAVE, "WAVE", sizeof(char)*4);
// fmt
memcpy(header->fmt.fmt, "fmt ", sizeof(char)*4);
header->fmt.Subchunk1Size = 16;
// data
memcpy(header->data.DataID, "data", sizeof(char)*4);
return 1;
}
int FLACtoSignal(char *input, AudioSignal *Signal)
{
FLAC__bool ok = true;
FLAC__StreamDecoder *decoder = 0;
FLAC__StreamDecoderInitStatus init_status;
memset(flacInternalErrorStr, 0, FLAC_ERR_STR);
if(!Signal) {
logmsg("ERROR: opening empty Data Structure\n");
return 0;
}
if((decoder = FLAC__stream_decoder_new()) == NULL) {
logmsg("ERROR: allocating decoder\n");
return 0;
}
(void)FLAC__stream_decoder_set_md5_checking(decoder, true);
init_status = FLAC__stream_decoder_init_file(decoder, input, write_callback, metadata_callback, error_callback, /*client_data=*/Signal);
if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
logmsg("ERROR: Initializing FLAC decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
ok = false;
}
if(ok) {
ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
if(!ok)
{
if(!flacInternalMDFErrors)
logmsg("ERROR: (FLAC) %s\n", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
Signal->errorFLAC++;
}
}
FLAC__stream_decoder_delete(decoder);
if(Signal->header.data.DataSize != (uint32_t)Signal->samplesPosFLAC*Signal->bytesPerSample)
{
if((uint32_t)Signal->samplesPosFLAC > Signal->header.data.DataSize) // Buffer overflow!!!
{
logmsg("ERROR: FLAC decoder had a buffer overflow\n Got%lu bytes and expected %lu bytes\n",
Signal->samplesPosFLAC, Signal->header.data.DataSize);
return 0;
}
//if(config->verbose)
if(!flacInternalMDFErrors)
logmsg(" - WARNING: FLAC decoder got %lu bytes and expected %lu bytes (fixed internally)\n",
Signal->samplesPosFLAC*Signal->bytesPerSample, Signal->header.data.DataSize);
Signal->header.data.DataSize = Signal->samplesPosFLAC;
}
if(!FillRIFFHeader(&Signal->header))
return 0;
if(Signal->errorFLAC)
return 0;
return ok ? 1 : 0;
}
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
AudioSignal *Signal = (AudioSignal*)client_data;
long int pos = 0;
size_t i;
(void)decoder;
if(!Signal) {
logmsg("ERROR: Got empty Signal structure for FLAC decoding\n");
flacInternalMDFErrors = 1;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if(Signal->header.fmt.NumOfChan != frame->header.channels) {
logmsg("ERROR: FLAC Channel definition discrepancy %d vs %d\n", Signal->header.fmt.NumOfChan, frame->header.channels);
flacInternalMDFErrors = 1;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if(buffer[0] == NULL) {
logmsg("ERROR: FLAC buffer[0] is NULL\n");
flacInternalMDFErrors = 1;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if(Signal->header.fmt.NumOfChan == 2 && buffer[1] == NULL) {
logmsg("ERROR: FLAC buffer[1] is NULL\n");
flacInternalMDFErrors = 1;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
/* write header data before we write the first frame */
if(frame->header.number.sample_number == 0)
{
if(Signal->header.data.DataSize == 0) {
logmsg("ERROR: MDFourier only works for FLAC files that have total_samples count in STREAMINFO\n");
flacInternalMDFErrors = 1;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if(Signal->header.fmt.bitsPerSample != 16 && Signal->header.fmt.bitsPerSample != 24) {
logmsg("ERROR: Only 16/24 bit flac supported.\n\tPlease convert file to 16/24 bit flac.\n");
flacInternalMDFErrors = 1;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if(Signal->header.fmt.NumOfChan != 2 && Signal->header.fmt.NumOfChan != 1) {
logmsg("ERROR: Only Mono and Stereo files are supported.\n");
flacInternalMDFErrors = 1;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
Signal->Samples = (double*)malloc(sizeof(double)*Signal->numSamples*Signal->header.fmt.NumOfChan);
if(!Signal->Samples)
{
logmsg("\tERROR: FLAC data chunks malloc failed!\n");
flacInternalMDFErrors = 1;
return(FLAC__STREAM_DECODER_WRITE_STATUS_ABORT);
}
memset(Signal->Samples, 0, sizeof(double)*Signal->numSamples*Signal->header.fmt.NumOfChan);
}
/* save decoded PCM samples */
pos = Signal->samplesPosFLAC;
for(i = 0; i < frame->header.blocksize; i++)
{
Signal->Samples[pos++] = (double)(FLAC__int32)buffer[0][i];
if(Signal->header.fmt.NumOfChan == 2)
Signal->Samples[pos++] = (double)(FLAC__int32)buffer[1][i];
}
Signal->samplesPosFLAC = pos;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
AudioSignal *Signal = (AudioSignal*)client_data;
(void)decoder;
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
{
uint64_t total_samples = 0;
unsigned sample_rate = 0;
unsigned channels = 0;
unsigned bps = 0;
total_samples = metadata->data.stream_info.total_samples;
sample_rate = metadata->data.stream_info.sample_rate;
channels = metadata->data.stream_info.channels;
bps = metadata->data.stream_info.bits_per_sample;
Signal->header.fmt.AudioFormat = WAVE_FORMAT_PCM;
Signal->header.fmt.NumOfChan = channels;
Signal->header.fmt.SamplesPerSec = sample_rate;
Signal->header.fmt.bytesPerSec = sample_rate * channels * (bps/8);
Signal->header.fmt.blockAlign = (FLAC__uint16)(channels * (bps/8));
Signal->header.fmt.bitsPerSample = bps;
Signal->header.data.DataSize = (FLAC__uint32)(total_samples * channels * (bps/8));
Signal->numSamples = total_samples*channels;
Signal->bytesPerSample = bps/8;
Signal->SamplesStart = 0;
Signal->samplesPosFLAC = 0;
}
}
void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
AudioSignal *Signal = (AudioSignal*)client_data;
(void)decoder;
strncpy(flacInternalErrorStr, FLAC__StreamDecoderErrorStatusString[status], FLAC_ERR_STR - 1);
logmsgFileOnly("Got error while decoding FLAC: %s\n", flacInternalErrorStr);
if(Signal)
Signal->errorFLAC ++;
}