|
Lines 28-33
Link Here
|
| 28 |
#include "log.h" |
28 |
#include "log.h" |
| 29 |
#include "io.h" |
29 |
#include "io.h" |
| 30 |
|
30 |
|
|
|
31 |
/* by LEGACY_FLAC we mean pre-1.1.3, before FLAC__SeekableStreamDecoder was merged into FLAC__StreamDecoder */ |
| 32 |
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT < 8 |
| 33 |
#define LEGACY_FLAC |
| 34 |
#else |
| 35 |
#undef LEGACY_FLAC |
| 36 |
#endif |
| 37 |
|
| 31 |
#define MAX_SUPPORTED_CHANNELS 2 |
38 |
#define MAX_SUPPORTED_CHANNELS 2 |
| 32 |
|
39 |
|
| 33 |
#define SAMPLES_PER_WRITE 512 |
40 |
#define SAMPLES_PER_WRITE 512 |
|
Lines 35-41
Link Here
|
| 35 |
|
42 |
|
| 36 |
struct flac_data |
43 |
struct flac_data |
| 37 |
{ |
44 |
{ |
|
|
45 |
#ifdef LEGACY_FLAC |
| 38 |
FLAC__SeekableStreamDecoder *decoder; |
46 |
FLAC__SeekableStreamDecoder *decoder; |
|
|
47 |
#else |
| 48 |
FLAC__StreamDecoder *decoder; |
| 49 |
#endif |
| 39 |
struct io_stream *stream; |
50 |
struct io_stream *stream; |
| 40 |
int bitrate; |
51 |
int bitrate; |
| 41 |
int abort; /* abort playing (due to an error) */ |
52 |
int abort; /* abort playing (due to an error) */ |
|
Lines 109-115
Link Here
|
| 109 |
} |
120 |
} |
| 110 |
|
121 |
|
| 111 |
static FLAC__StreamDecoderWriteStatus write_callback ( |
122 |
static FLAC__StreamDecoderWriteStatus write_callback ( |
|
|
123 |
#ifdef LEGACY_FLAC |
| 112 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
124 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
|
|
125 |
#else |
| 126 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 127 |
#endif |
| 113 |
const FLAC__Frame *frame, |
128 |
const FLAC__Frame *frame, |
| 114 |
const FLAC__int32 * const buffer[], void *client_data) |
129 |
const FLAC__int32 * const buffer[], void *client_data) |
| 115 |
{ |
130 |
{ |
|
Lines 127-133
Link Here
|
| 127 |
} |
142 |
} |
| 128 |
|
143 |
|
| 129 |
static void metadata_callback ( |
144 |
static void metadata_callback ( |
|
|
145 |
#ifdef LEGACY_FLAC |
| 130 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
146 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
|
|
147 |
#else |
| 148 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 149 |
#endif |
| 131 |
const FLAC__StreamMetadata *metadata, void *client_data) |
150 |
const FLAC__StreamMetadata *metadata, void *client_data) |
| 132 |
{ |
151 |
{ |
| 133 |
struct flac_data *data = (struct flac_data *)client_data; |
152 |
struct flac_data *data = (struct flac_data *)client_data; |
|
Lines 147-153
Link Here
|
| 147 |
} |
166 |
} |
| 148 |
|
167 |
|
| 149 |
static void error_callback ( |
168 |
static void error_callback ( |
|
|
169 |
#ifdef LEGACY_FLAC |
| 150 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
170 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
|
|
171 |
#else |
| 172 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 173 |
#endif |
| 151 |
FLAC__StreamDecoderErrorStatus status, void *client_data) |
174 |
FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 152 |
{ |
175 |
{ |
| 153 |
struct flac_data *data = (struct flac_data *)client_data; |
176 |
struct flac_data *data = (struct flac_data *)client_data; |
|
Lines 160-168
Link Here
|
| 160 |
decoder_error (&data->error, ERROR_FATAL, 0, "FLAC: lost sync"); |
183 |
decoder_error (&data->error, ERROR_FATAL, 0, "FLAC: lost sync"); |
| 161 |
} |
184 |
} |
| 162 |
|
185 |
|
|
|
186 |
#ifdef LEGACY_FLAC |
| 163 |
static FLAC__SeekableStreamDecoderReadStatus read_callback ( |
187 |
static FLAC__SeekableStreamDecoderReadStatus read_callback ( |
| 164 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
188 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
| 165 |
FLAC__byte buffer[], unsigned *bytes, void *client_data) |
189 |
FLAC__byte buffer[], unsigned *bytes, void *client_data) |
|
|
190 |
#else |
| 191 |
static FLAC__StreamDecoderReadStatus read_callback ( |
| 192 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 193 |
FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 194 |
#endif |
| 166 |
{ |
195 |
{ |
| 167 |
struct flac_data *data = (struct flac_data *)client_data; |
196 |
struct flac_data *data = (struct flac_data *)client_data; |
| 168 |
ssize_t res; |
197 |
ssize_t res; |
|
Lines 171-222
Link Here
|
| 171 |
|
200 |
|
| 172 |
if (res > 0) { |
201 |
if (res > 0) { |
| 173 |
*bytes = res; |
202 |
*bytes = res; |
|
|
203 |
#ifdef LEGACY_FLAC |
| 204 |
return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK; |
| 205 |
#else |
| 174 |
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
206 |
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
|
|
207 |
#endif |
| 175 |
} |
208 |
} |
| 176 |
|
209 |
|
| 177 |
if (res == 0) { |
210 |
if (res == 0) { |
| 178 |
*bytes = 0; |
211 |
*bytes = 0; |
|
|
212 |
/* not sure why this works, but if it ain't broke... */ |
| 179 |
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
213 |
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 180 |
} |
214 |
} |
| 181 |
|
215 |
|
| 182 |
error ("read error: %s", io_strerror(data->stream)); |
216 |
error ("read error: %s", io_strerror(data->stream)); |
| 183 |
|
217 |
|
|
|
218 |
#ifdef LEGACY_FLAC |
| 219 |
return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR; |
| 220 |
#else |
| 184 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
221 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
|
|
222 |
#endif |
| 185 |
} |
223 |
} |
| 186 |
|
224 |
|
|
|
225 |
#ifdef LEGACY_FLAC |
| 187 |
static FLAC__SeekableStreamDecoderSeekStatus seek_callback ( |
226 |
static FLAC__SeekableStreamDecoderSeekStatus seek_callback ( |
| 188 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
227 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
|
|
228 |
#else |
| 229 |
static FLAC__StreamDecoderSeekStatus seek_callback ( |
| 230 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 231 |
#endif |
| 189 |
FLAC__uint64 absolute_byte_offset, void *client_data) |
232 |
FLAC__uint64 absolute_byte_offset, void *client_data) |
| 190 |
{ |
233 |
{ |
| 191 |
struct flac_data *data = (struct flac_data *)client_data; |
234 |
struct flac_data *data = (struct flac_data *)client_data; |
| 192 |
|
235 |
|
|
|
236 |
#ifdef LEGACY_FLAC |
| 193 |
return io_seek(data->stream, absolute_byte_offset, SEEK_SET) >= 0 |
237 |
return io_seek(data->stream, absolute_byte_offset, SEEK_SET) >= 0 |
| 194 |
? FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK |
238 |
? FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK |
| 195 |
: FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR; |
239 |
: FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR; |
|
|
240 |
#else |
| 241 |
return io_seek(data->stream, absolute_byte_offset, SEEK_SET) >= 0 |
| 242 |
? FLAC__STREAM_DECODER_SEEK_STATUS_OK |
| 243 |
: FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; |
| 244 |
#endif |
| 196 |
} |
245 |
} |
| 197 |
|
246 |
|
|
|
247 |
#ifdef LEGACY_FLAC |
| 198 |
static FLAC__SeekableStreamDecoderTellStatus tell_callback ( |
248 |
static FLAC__SeekableStreamDecoderTellStatus tell_callback ( |
| 199 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
249 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
|
|
250 |
#else |
| 251 |
static FLAC__StreamDecoderTellStatus tell_callback ( |
| 252 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 253 |
#endif |
| 200 |
FLAC__uint64 *absolute_byte_offset, void *client_data) |
254 |
FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 201 |
{ |
255 |
{ |
| 202 |
struct flac_data *data = (struct flac_data *)client_data; |
256 |
struct flac_data *data = (struct flac_data *)client_data; |
| 203 |
|
257 |
|
| 204 |
*absolute_byte_offset = io_tell (data->stream); |
258 |
*absolute_byte_offset = io_tell (data->stream); |
|
|
259 |
#ifdef LEGACY_FLAC |
| 205 |
return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK; |
260 |
return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK; |
|
|
261 |
#else |
| 262 |
return FLAC__STREAM_DECODER_TELL_STATUS_OK; |
| 263 |
#endif |
| 206 |
} |
264 |
} |
| 207 |
|
265 |
|
|
|
266 |
#ifdef LEGACY_FLAC |
| 208 |
static FLAC__SeekableStreamDecoderLengthStatus length_callback ( |
267 |
static FLAC__SeekableStreamDecoderLengthStatus length_callback ( |
| 209 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
268 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
|
|
269 |
#else |
| 270 |
static FLAC__StreamDecoderLengthStatus length_callback ( |
| 271 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 272 |
#endif |
| 210 |
FLAC__uint64 *stream_length, void *client_data) |
273 |
FLAC__uint64 *stream_length, void *client_data) |
| 211 |
{ |
274 |
{ |
| 212 |
struct flac_data *data = (struct flac_data *)client_data; |
275 |
struct flac_data *data = (struct flac_data *)client_data; |
| 213 |
|
276 |
|
| 214 |
*stream_length = io_file_size (data->stream); |
277 |
*stream_length = io_file_size (data->stream); |
|
|
278 |
#ifdef LEGACY_FLAC |
| 215 |
return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK; |
279 |
return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK; |
|
|
280 |
#else |
| 281 |
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; |
| 282 |
#endif |
| 216 |
} |
283 |
} |
| 217 |
|
284 |
|
| 218 |
static FLAC__bool eof_callback ( |
285 |
static FLAC__bool eof_callback ( |
|
|
286 |
#ifdef LEGACY_FLAC |
| 219 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
287 |
const FLAC__SeekableStreamDecoder *decoder ATTR_UNUSED, |
|
|
288 |
#else |
| 289 |
const FLAC__StreamDecoder *decoder ATTR_UNUSED, |
| 290 |
#endif |
| 220 |
void *client_data) |
291 |
void *client_data) |
| 221 |
{ |
292 |
{ |
| 222 |
struct flac_data *data = (struct flac_data *)client_data; |
293 |
struct flac_data *data = (struct flac_data *)client_data; |
|
Lines 248-253
Link Here
|
| 248 |
|
319 |
|
| 249 |
data->ok = 1; |
320 |
data->ok = 1; |
| 250 |
|
321 |
|
|
|
322 |
#ifdef LEGACY_FLAC |
| 251 |
if (!(data->decoder = FLAC__seekable_stream_decoder_new())) { |
323 |
if (!(data->decoder = FLAC__seekable_stream_decoder_new())) { |
| 252 |
decoder_error (&data->error, ERROR_FATAL, 0, |
324 |
decoder_error (&data->error, ERROR_FATAL, 0, |
| 253 |
"FLAC__seekable_stream_decoder_new() failed"); |
325 |
"FLAC__seekable_stream_decoder_new() failed"); |
|
Lines 279-285
Link Here
|
| 279 |
eof_callback); |
351 |
eof_callback); |
| 280 |
|
352 |
|
| 281 |
if (FLAC__seekable_stream_decoder_init(data->decoder) |
353 |
if (FLAC__seekable_stream_decoder_init(data->decoder) |
| 282 |
!= FLAC__FILE_DECODER_OK) { |
354 |
!= FLAC__SEEKABLE_STREAM_DECODER_OK) { |
| 283 |
decoder_error (&data->error, ERROR_FATAL, 0, |
355 |
decoder_error (&data->error, ERROR_FATAL, 0, |
| 284 |
"FLAC__seekable_stream_decoder_init() failed"); |
356 |
"FLAC__seekable_stream_decoder_init() failed"); |
| 285 |
data->ok = 0; |
357 |
data->ok = 0; |
|
Lines 291-296
Link Here
|
| 291 |
decoder_error (&data->error, ERROR_FATAL, 0, |
363 |
decoder_error (&data->error, ERROR_FATAL, 0, |
| 292 |
"FLAC__seekable_stream_decoder_process_until_end_of_metadata()" |
364 |
"FLAC__seekable_stream_decoder_process_until_end_of_metadata()" |
| 293 |
" failed."); |
365 |
" failed."); |
|
|
366 |
#else |
| 367 |
if (!(data->decoder = FLAC__stream_decoder_new())) { |
| 368 |
decoder_error (&data->error, ERROR_FATAL, 0, |
| 369 |
"FLAC__stream_decoder_new() failed"); |
| 370 |
data->ok = 0; |
| 371 |
return data; |
| 372 |
} |
| 373 |
|
| 374 |
FLAC__stream_decoder_set_md5_checking (data->decoder, false); |
| 375 |
|
| 376 |
FLAC__stream_decoder_set_metadata_ignore_all (data->decoder); |
| 377 |
FLAC__stream_decoder_set_metadata_respond (data->decoder, |
| 378 |
FLAC__METADATA_TYPE_STREAMINFO); |
| 379 |
|
| 380 |
if (FLAC__stream_decoder_init_stream(data->decoder, read_callback, seek_callback, tell_callback, length_callback, eof_callback, write_callback, metadata_callback, error_callback, data) |
| 381 |
!= FLAC__STREAM_DECODER_INIT_STATUS_OK) { |
| 382 |
decoder_error (&data->error, ERROR_FATAL, 0, |
| 383 |
"FLAC__stream_decoder_init() failed"); |
| 384 |
data->ok = 0; |
| 385 |
return data; |
| 386 |
} |
| 387 |
|
| 388 |
if (!FLAC__stream_decoder_process_until_end_of_metadata(data->decoder)) { |
| 389 |
decoder_error (&data->error, ERROR_FATAL, 0, |
| 390 |
"FLAC__stream_decoder_process_until_end_of_metadata()" |
| 391 |
" failed."); |
| 392 |
#endif |
| 294 |
data->ok = 0; |
393 |
data->ok = 0; |
| 295 |
return data; |
394 |
return data; |
| 296 |
} |
395 |
} |
|
Lines 309-316
Link Here
|
| 309 |
|
408 |
|
| 310 |
if (data->ok) { |
409 |
if (data->ok) { |
| 311 |
if (data->decoder) { |
410 |
if (data->decoder) { |
|
|
411 |
#ifdef LEGACY_FLAC |
| 312 |
FLAC__seekable_stream_decoder_finish (data->decoder); |
412 |
FLAC__seekable_stream_decoder_finish (data->decoder); |
| 313 |
FLAC__seekable_stream_decoder_delete (data->decoder); |
413 |
FLAC__seekable_stream_decoder_delete (data->decoder); |
|
|
414 |
#else |
| 415 |
FLAC__stream_decoder_finish (data->decoder); |
| 416 |
FLAC__stream_decoder_delete (data->decoder); |
| 417 |
#endif |
| 314 |
} |
418 |
} |
| 315 |
io_close (data->stream); |
419 |
io_close (data->stream); |
| 316 |
} |
420 |
} |
|
Lines 430-440
Link Here
|
| 430 |
target_sample = (FLAC__uint64)((sec/(double)data->length) * |
534 |
target_sample = (FLAC__uint64)((sec/(double)data->length) * |
| 431 |
(double)data->total_samples); |
535 |
(double)data->total_samples); |
| 432 |
|
536 |
|
|
|
537 |
#ifdef LEGACY_FLAC |
| 433 |
if (FLAC__seekable_stream_decoder_seek_absolute(data->decoder, |
538 |
if (FLAC__seekable_stream_decoder_seek_absolute(data->decoder, |
| 434 |
target_sample)) |
539 |
target_sample)) |
|
|
540 |
#else |
| 541 |
if (FLAC__stream_decoder_seek_absolute(data->decoder, target_sample)) |
| 542 |
#endif |
| 435 |
return sec; |
543 |
return sec; |
| 436 |
else { |
544 |
else { |
|
|
545 |
#ifdef LEGACY_FLAC |
| 437 |
logit ("FLAC__seekable_stream_decoder_seek_absolute() failed."); |
546 |
logit ("FLAC__seekable_stream_decoder_seek_absolute() failed."); |
|
|
547 |
#else |
| 548 |
logit ("FLAC__stream_decoder_seek_absolute() failed."); |
| 549 |
#endif |
| 438 |
return -1; |
550 |
return -1; |
| 439 |
} |
551 |
} |
| 440 |
} |
552 |
} |
|
Lines 469-490
Link Here
|
| 469 |
if (!data->sample_buffer_fill) { |
581 |
if (!data->sample_buffer_fill) { |
| 470 |
debug ("decoding..."); |
582 |
debug ("decoding..."); |
| 471 |
|
583 |
|
|
|
584 |
#ifdef LEGACY_FLAC |
| 472 |
if (FLAC__seekable_stream_decoder_get_state(data->decoder) |
585 |
if (FLAC__seekable_stream_decoder_get_state(data->decoder) |
| 473 |
== FLAC__FILE_DECODER_END_OF_FILE) { |
586 |
== FLAC__FILE_DECODER_END_OF_FILE) { |
|
|
587 |
#else |
| 588 |
if (FLAC__stream_decoder_get_state(data->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM) { |
| 589 |
#endif |
| 474 |
logit ("EOF"); |
590 |
logit ("EOF"); |
| 475 |
return 0; |
591 |
return 0; |
| 476 |
} |
592 |
} |
| 477 |
|
593 |
|
|
|
594 |
#ifdef LEGACY_FLAC |
| 478 |
if (!FLAC__seekable_stream_decoder_process_single( |
595 |
if (!FLAC__seekable_stream_decoder_process_single( |
| 479 |
data->decoder)) { |
596 |
data->decoder)) { |
|
|
597 |
#else |
| 598 |
if (!FLAC__stream_decoder_process_single(data->decoder)) { |
| 599 |
#endif |
| 480 |
decoder_error (&data->error, ERROR_FATAL, 0, |
600 |
decoder_error (&data->error, ERROR_FATAL, 0, |
| 481 |
"Read error processing frame."); |
601 |
"Read error processing frame."); |
| 482 |
return 0; |
602 |
return 0; |
| 483 |
} |
603 |
} |
| 484 |
|
604 |
|
| 485 |
/* Count the bitrate */ |
605 |
/* Count the bitrate */ |
|
|
606 |
#ifdef LEGACY_FLAC |
| 486 |
if(!FLAC__seekable_stream_decoder_get_decode_position( |
607 |
if(!FLAC__seekable_stream_decoder_get_decode_position( |
| 487 |
data->decoder, &decode_position)) |
608 |
data->decoder, &decode_position)) |
|
|
609 |
#else |
| 610 |
if(!FLAC__stream_decoder_get_decode_position(data->decoder, &decode_position)) |
| 611 |
#endif |
| 488 |
decode_position = 0; |
612 |
decode_position = 0; |
| 489 |
if (decode_position > data->last_decode_position) { |
613 |
if (decode_position > data->last_decode_position) { |
| 490 |
int bytes_per_sec = bytes_per_sample * data->sample_rate |
614 |
int bytes_per_sec = bytes_per_sample * data->sample_rate |