-
Notifications
You must be signed in to change notification settings - Fork 98
/
flac.c
387 lines (332 loc) · 11.9 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Squeezelite - lightweight headless squeezeplay emulator for linux
*
* (c) Adrian Smith 2012, triode1@btinternet.com
* Ralph Irving 2015-2024, ralph_irving@hotmail.com
*
* This program is free software: 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "squeezelite.h"
#include <FLAC/stream_decoder.h>
#if BYTES_PER_FRAME == 4
#define ALIGN8(n) (n << 8)
#define ALIGN16(n) (n)
#define ALIGN24(n) (n >> 8)
#define ALIGN32(n) (n >> 16)
#else
#define ALIGN8(n) (n << 24)
#define ALIGN16(n) (n << 16)
#define ALIGN24(n) (n << 8)
#define ALIGN32(n) (n)
#endif
struct flac {
FLAC__StreamDecoder *decoder;
u8_t container;
#if !LINKALL
// FLAC symbols to be dynamically loaded
const char **FLAC__StreamDecoderErrorStatusString;
const char **FLAC__StreamDecoderStateString;
FLAC__StreamDecoder * (* FLAC__stream_decoder_new)(void);
FLAC__bool (* FLAC__stream_decoder_reset)(FLAC__StreamDecoder *decoder);
void (* FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder);
FLAC__StreamDecoderInitStatus (* FLAC__stream_decoder_init_stream)(
FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderReadCallback read_callback,
FLAC__StreamDecoderSeekCallback seek_callback,
FLAC__StreamDecoderTellCallback tell_callback,
FLAC__StreamDecoderLengthCallback length_callback,
FLAC__StreamDecoderEofCallback eof_callback,
FLAC__StreamDecoderWriteCallback write_callback,
FLAC__StreamDecoderMetadataCallback metadata_callback,
FLAC__StreamDecoderErrorCallback error_callback,
void *client_data
);
FLAC__StreamDecoderInitStatus (* FLAC__stream_decoder_init_ogg_stream)(
FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderReadCallback read_callback,
FLAC__StreamDecoderSeekCallback seek_callback,
FLAC__StreamDecoderTellCallback tell_callback,
FLAC__StreamDecoderLengthCallback length_callback,
FLAC__StreamDecoderEofCallback eof_callback,
FLAC__StreamDecoderWriteCallback write_callback,
FLAC__StreamDecoderMetadataCallback metadata_callback,
FLAC__StreamDecoderErrorCallback error_callback,
void *client_data
);
FLAC__bool (* FLAC__stream_decoder_process_single)(FLAC__StreamDecoder *decoder);
FLAC__StreamDecoderState (* FLAC__stream_decoder_get_state)(const FLAC__StreamDecoder *decoder);
void (*FLAC__stream_decoder_set_metadata_respond)(FLAC__StreamDecoder* decoder, FLAC__MetadataType type);
FLAC__bool (*FLAC__stream_decoder_set_ogg_chaining)(FLAC__StreamDecoder* decoder, FLAC__bool allow);
#endif
};
static struct flac *f;
extern log_level loglevel;
extern struct buffer *streambuf;
extern struct buffer *outputbuf;
extern struct streamstate stream;
extern struct outputstate output;
extern struct decodestate decode;
extern struct processstate process;
#define LOCK_S mutex_lock(streambuf->mutex)
#define UNLOCK_S mutex_unlock(streambuf->mutex)
#define LOCK_O mutex_lock(outputbuf->mutex)
#define UNLOCK_O mutex_unlock(outputbuf->mutex)
#if PROCESS
#define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
#define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
#define IF_DIRECT(x) if (decode.direct) { x }
#define IF_PROCESS(x) if (!decode.direct) { x }
#else
#define LOCK_O_direct mutex_lock(outputbuf->mutex)
#define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
#define IF_DIRECT(x) { x }
#define IF_PROCESS(x)
#endif
#if LINKALL
#define FLAC(h, fn, ...) (FLAC__ ## fn)(__VA_ARGS__)
#define FLAC_A(h, a) (FLAC__ ## a)
#else
#define FLAC(h, fn, ...) (h)->FLAC__##fn(__VA_ARGS__)
#define FLAC_A(h, a) (h)->FLAC__ ## a
#endif
static void metadata_cb(const FLAC__StreamDecoder* decoder, const FLAC__StreamMetadata* metadata, void* client_data) {
switch (metadata->type) {
case FLAC__METADATA_TYPE_STREAMINFO:
LOG_INFO("stream parameters rate:%d, channels:%d, size:%d", metadata->data.stream_info.sample_rate,
metadata->data.stream_info.channels, metadata->data.stream_info.bits_per_sample);
break;
case FLAC__METADATA_TYPE_VORBIS_COMMENT: {
FLAC__StreamMetadata_VorbisComment_Entry* comment = metadata->data.vorbis_comment.comments;
for (int i = 0; i < metadata->data.vorbis_comment.num_comments; i++, comment++) {
LOG_INFO("stream metadata %*s", comment->length, comment->entry);
}
}
default:
break;
}
}
static FLAC__StreamDecoderReadStatus read_cb(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *want, void *client_data) {
size_t bytes;
bool end;
LOCK_S;
bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
bytes = min(bytes, *want);
end = (stream.state <= DISCONNECT && bytes == 0);
memcpy(buffer, streambuf->readp, bytes);
_buf_inc_readp(streambuf, bytes);
UNLOCK_S;
*want = bytes;
// if there's nothing in the stream buffer, libFLAC will continuously call this function as quickly as possible. slow it down.
if (!bytes && !end)
usleep(1000);
return end ? FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM : FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
static FLAC__StreamDecoderWriteStatus write_cb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
const FLAC__int32 *const buffer[], void *client_data) {
size_t frames = frame->header.blocksize;
unsigned bits_per_sample = frame->header.bits_per_sample;
unsigned channels = frame->header.channels;
FLAC__int32 *lptr = (FLAC__int32 *)buffer[0];
FLAC__int32 *rptr = (FLAC__int32 *)buffer[channels > 1 ? 1 : 0];
if (decode.new_stream) {
LOCK_O;
LOG_INFO("setting track_start");
output.track_start = outputbuf->writep;
decode.new_stream = false;
#if DSD
#if SL_LITTLE_ENDIAN
#define MARKER_OFFSET 2
#else
#define MARKER_OFFSET 1
#endif
if (bits_per_sample == 24 && is_stream_dop(((u8_t *)lptr) + MARKER_OFFSET, ((u8_t *)rptr) + MARKER_OFFSET, 4, frames)) {
LOG_INFO("file contains DOP");
if (output.dsdfmt == DOP_S24_LE || output.dsdfmt == DOP_S24_3LE)
output.next_fmt = output.dsdfmt;
else
output.next_fmt = DOP;
output.next_sample_rate = frame->header.sample_rate;
output.fade = FADE_INACTIVE;
} else {
output.next_sample_rate = decode_newstream(frame->header.sample_rate, output.supported_rates);
output.next_fmt = PCM;
if (output.fade_mode) _checkfade(true);
}
#else
output.next_sample_rate = decode_newstream(frame->header.sample_rate, output.supported_rates);
if (output.fade_mode) _checkfade(true);
#endif
UNLOCK_O;
}
LOCK_O_direct;
while (frames > 0) {
frames_t f;
frames_t count;
ISAMPLE_T *optr;
IF_DIRECT(
optr = (ISAMPLE_T *)outputbuf->writep;
f = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
);
IF_PROCESS(
optr = (ISAMPLE_T *)process.inbuf;
f = process.max_in_frames;
);
f = min(f, frames);
count = f;
if (bits_per_sample == 8) {
while (count--) {
*optr++ = ALIGN8(*lptr++);
*optr++ = ALIGN8(*rptr++);
}
} else if (bits_per_sample == 16) {
while (count--) {
*optr++ = ALIGN16(*lptr++);
*optr++ = ALIGN16(*rptr++);
}
} else if (bits_per_sample == 24) {
while (count--) {
*optr++ = ALIGN24(*lptr++);
*optr++ = ALIGN24(*rptr++);
}
} else if (bits_per_sample == 32) {
while (count--) {
*optr++ = ALIGN32(*lptr++);
*optr++ = ALIGN32(*rptr++);
}
} else {
LOG_ERROR("unsupported bits per sample: %u", bits_per_sample);
}
frames -= f;
IF_DIRECT(
_buf_inc_writep(outputbuf, f * BYTES_PER_FRAME);
);
IF_PROCESS(
process.in_frames = f;
if (frames) LOG_ERROR("unhandled case");
);
}
UNLOCK_O_direct;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
static void error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderErrorStatusString)[status]);
}
static void flac_close(void) {
FLAC(f, stream_decoder_delete, f->decoder);
f->decoder = NULL;
}
static void flac_open(u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness) {
if ( f->decoder && f->container != sample_size ) {
flac_close();
}
f->container = sample_size;
if (f->decoder) {
FLAC(f, stream_decoder_reset, f->decoder);
} else {
f->decoder = FLAC(f, stream_decoder_new);
}
if ( f->container == 'o' ) {
LOG_INFO("ogg/flac container - using init_ogg_stream");
#if LINKALL
#ifdef FLAC__OGG_CHAINING
FLAC__stream_decoder_set_ogg_chaining(f->decoder, true);
#else
#pragma message ("OggFlac library does not support chaining")
#endif
#else
if (f->FLAC__stream_decoder_set_ogg_chaining) {
f->FLAC__stream_decoder_set_ogg_chaining(f->decoder, true);
}
#endif
FLAC(f, stream_decoder_set_metadata_respond, f->decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
FLAC(f, stream_decoder_init_ogg_stream, f->decoder, &read_cb, NULL, NULL, NULL, NULL, &write_cb, &metadata_cb, &error_cb, NULL);
} else {
FLAC(f, stream_decoder_init_stream, f->decoder, &read_cb, NULL, NULL, NULL, NULL, &write_cb, NULL, &error_cb, NULL);
}
}
static decode_state flac_decode(void) {
bool ok = FLAC(f, stream_decoder_process_single, f->decoder);
FLAC__StreamDecoderState state = FLAC(f, stream_decoder_get_state, f->decoder);
if (!ok && state != FLAC__STREAM_DECODER_END_OF_STREAM) {
LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderStateString)[state]);
};
if (state == FLAC__STREAM_DECODER_END_OF_STREAM) {
return DECODE_COMPLETE;
} else if (state > FLAC__STREAM_DECODER_END_OF_STREAM) {
return DECODE_ERROR;
} else {
return DECODE_RUNNING;
}
}
static bool load_flac() {
#if !LINKALL
void *handle = NULL;
char name[30];
char *err;
sprintf(name, LIBFLAC, FLAC_API_VERSION_CURRENT < 12 ? 8 : 12);
handle = dlopen(name, RTLD_NOW);
if (!handle) {
LOG_INFO("dlerror: %s", dlerror());
return false;
}
f->FLAC__StreamDecoderErrorStatusString = dlsym(handle, "FLAC__StreamDecoderErrorStatusString");
f->FLAC__StreamDecoderStateString = dlsym(handle, "FLAC__StreamDecoderStateString");
f->FLAC__stream_decoder_new = dlsym(handle, "FLAC__stream_decoder_new");
f->FLAC__stream_decoder_reset = dlsym(handle, "FLAC__stream_decoder_reset");
f->FLAC__stream_decoder_delete = dlsym(handle, "FLAC__stream_decoder_delete");
f->FLAC__stream_decoder_init_stream = dlsym(handle, "FLAC__stream_decoder_init_stream");
f->FLAC__stream_decoder_init_ogg_stream = dlsym(handle, "FLAC__stream_decoder_init_ogg_stream");
f->FLAC__stream_decoder_process_single = dlsym(handle, "FLAC__stream_decoder_process_single");
f->FLAC__stream_decoder_get_state = dlsym(handle, "FLAC__stream_decoder_get_state");
f->FLAC__stream_decoder_set_metadata_respond = dlsym(handle, "FLAC__stream_decoder_set_metadata_respond");
if ((err = dlerror()) != NULL) {
LOG_INFO("dlerror: %s", err);
return false;
}
// ignore error for this new API
f->FLAC__stream_decoder_set_ogg_chaining = dlsym(handle, "FLAC__stream_decoder_set_ogg_chaining");
if (!f->FLAC__stream_decoder_set_ogg_chaining) {
if ((err = dlerror()) != NULL) {
LOG_INFO("dlerror: %s", err);
}
LOG_INFO("OggFlac chaining disabled");
}
LOG_INFO("loaded %s", name);
#elif !defined(FLAC__OGG_CHAINING)
LOG_INFO("OggFlac chaining disabled");
#endif
return true;
}
struct codec *register_flac(void) {
static struct codec ret = {
'f', // id
"ogf,flc", // types
16384, // min read
204800, // min space
flac_open, // open
flac_close, // close
flac_decode, // decode
};
f = malloc(sizeof(struct flac));
if (!f) {
return NULL;
}
f->decoder = NULL;
if (!load_flac()) {
return NULL;
}
LOG_INFO("using flac to decode ogf,flc");
return &ret;
}