comparison src/flac113/libflac/stream_decoder.c @ 721:574de61036a3 trunk

[svn] More VFS stuff.
author js
date Sat, 24 Feb 2007 11:40:20 -0800
parents a9b178bc4ae4
children
comparison
equal deleted inserted replaced
720:0357863b3631 721:574de61036a3
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32 #define USE_VFS 1
33 #include <audacious/vfs.h>
34
32 #if HAVE_CONFIG_H 35 #if HAVE_CONFIG_H
33 # include <config.h> 36 # include <config.h>
34 #endif 37 #endif
35 38
36 #if defined _MSC_VER || defined __MINGW32__ 39 #if defined _MSC_VER || defined __MINGW32__
44 #include <stdio.h> 47 #include <stdio.h>
45 #include <stdlib.h> /* for malloc() */ 48 #include <stdlib.h> /* for malloc() */
46 #include <string.h> /* for memset/memcpy() */ 49 #include <string.h> /* for memset/memcpy() */
47 #include <sys/stat.h> /* for stat() */ 50 #include <sys/stat.h> /* for stat() */
48 #include <sys/types.h> /* for off_t */ 51 #include <sys/types.h> /* for off_t */
49 #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ 52 #if defined USE_VFS || defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
50 #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */ 53 #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
51 #define fseeko fseek 54 #define fseeko fseek
52 #define ftello ftell 55 #define ftello ftell
53 #endif 56 #endif
54 #endif 57 #endif
100 * Private class method prototypes 103 * Private class method prototypes
101 * 104 *
102 ***********************************************************************/ 105 ***********************************************************************/
103 106
104 static void set_defaults_(FLAC__StreamDecoder *decoder); 107 static void set_defaults_(FLAC__StreamDecoder *decoder);
108 #ifndef USE_VFS
105 static FILE *get_binary_stdin_(void); 109 static FILE *get_binary_stdin_(void);
110 #endif
106 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels); 111 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
107 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); 112 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
108 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); 113 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
109 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); 114 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
110 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); 115 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
165 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */ 170 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
166 void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); 171 void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
167 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */ 172 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
168 void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); 173 void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
169 void *client_data; 174 void *client_data;
170 FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */ 175 VFSFile *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
171 FLAC__BitReader *input; 176 FLAC__BitReader *input;
172 FLAC__int32 *output[FLAC__MAX_CHANNELS]; 177 FLAC__int32 *output[FLAC__MAX_CHANNELS];
173 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */ 178 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
174 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; 179 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
175 unsigned output_capacity, output_channels; 180 unsigned output_capacity, output_channels;
530 ); 535 );
531 } 536 }
532 537
533 static FLAC__StreamDecoderInitStatus init_FILE_internal_( 538 static FLAC__StreamDecoderInitStatus init_FILE_internal_(
534 FLAC__StreamDecoder *decoder, 539 FLAC__StreamDecoder *decoder,
535 FILE *file, 540 VFSFile *file,
536 FLAC__StreamDecoderWriteCallback write_callback, 541 FLAC__StreamDecoderWriteCallback write_callback,
537 FLAC__StreamDecoderMetadataCallback metadata_callback, 542 FLAC__StreamDecoderMetadataCallback metadata_callback,
538 FLAC__StreamDecoderErrorCallback error_callback, 543 FLAC__StreamDecoderErrorCallback error_callback,
539 void *client_data, 544 void *client_data,
540 FLAC__bool is_ogg 545 FLAC__bool is_ogg
552 /* 557 /*
553 * To make sure that our file does not go unclosed after an error, we 558 * To make sure that our file does not go unclosed after an error, we
554 * must assign the FILE pointer before any further error can occur in 559 * must assign the FILE pointer before any further error can occur in
555 * this routine. 560 * this routine.
556 */ 561 */
562 #ifndef USE_VFS
557 if(file == stdin) 563 if(file == stdin)
558 file = get_binary_stdin_(); /* just to be safe */ 564 file = get_binary_stdin_(); /* just to be safe */
565 #endif
559 566
560 decoder->private_->file = file; 567 decoder->private_->file = file;
561 568
562 return init_stream_internal_( 569 return init_stream_internal_(
563 decoder, 570 decoder,
564 file_read_callback_, 571 file_read_callback_,
572 #ifdef USE_VFS
573 file_seek_callback_,
574 file_tell_callback_,
575 file_length_callback_,
576 #else
565 decoder->private_->file == stdin? 0: file_seek_callback_, 577 decoder->private_->file == stdin? 0: file_seek_callback_,
566 decoder->private_->file == stdin? 0: file_tell_callback_, 578 decoder->private_->file == stdin? 0: file_tell_callback_,
567 decoder->private_->file == stdin? 0: file_length_callback_, 579 decoder->private_->file == stdin? 0: file_length_callback_,
580 #endif
568 file_eof_callback_, 581 file_eof_callback_,
569 write_callback, 582 write_callback,
570 metadata_callback, 583 metadata_callback,
571 error_callback, 584 error_callback,
572 client_data, 585 client_data,
574 ); 587 );
575 } 588 }
576 589
577 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE( 590 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
578 FLAC__StreamDecoder *decoder, 591 FLAC__StreamDecoder *decoder,
579 FILE *file, 592 VFSFile *file,
580 FLAC__StreamDecoderWriteCallback write_callback, 593 FLAC__StreamDecoderWriteCallback write_callback,
581 FLAC__StreamDecoderMetadataCallback metadata_callback, 594 FLAC__StreamDecoderMetadataCallback metadata_callback,
582 FLAC__StreamDecoderErrorCallback error_callback, 595 FLAC__StreamDecoderErrorCallback error_callback,
583 void *client_data 596 void *client_data
584 ) 597 )
586 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); 599 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
587 } 600 }
588 601
589 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE( 602 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
590 FLAC__StreamDecoder *decoder, 603 FLAC__StreamDecoder *decoder,
591 FILE *file, 604 VFSFile *file,
592 FLAC__StreamDecoderWriteCallback write_callback, 605 FLAC__StreamDecoderWriteCallback write_callback,
593 FLAC__StreamDecoderMetadataCallback metadata_callback, 606 FLAC__StreamDecoderMetadataCallback metadata_callback,
594 FLAC__StreamDecoderErrorCallback error_callback, 607 FLAC__StreamDecoderErrorCallback error_callback,
595 void *client_data 608 void *client_data
596 ) 609 )
606 FLAC__StreamDecoderErrorCallback error_callback, 619 FLAC__StreamDecoderErrorCallback error_callback,
607 void *client_data, 620 void *client_data,
608 FLAC__bool is_ogg 621 FLAC__bool is_ogg
609 ) 622 )
610 { 623 {
611 FILE *file; 624 VFSFile *file;
612 625
613 FLAC__ASSERT(0 != decoder); 626 FLAC__ASSERT(0 != decoder);
614 627
615 /* 628 /*
616 * To make sure that our file does not go unclosed after an error, we 629 * To make sure that our file does not go unclosed after an error, we
621 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; 634 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
622 635
623 if(0 == write_callback || 0 == error_callback) 636 if(0 == write_callback || 0 == error_callback)
624 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; 637 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
625 638
639 #ifdef USE_VFS
640 file = vfs_fopen(filename, "rb");
641 #else
626 file = filename? fopen(filename, "rb") : stdin; 642 file = filename? fopen(filename, "rb") : stdin;
643 #endif
627 644
628 if(0 == file) 645 if(0 == file)
629 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE; 646 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
630 647
631 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg); 648 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
701 if(decoder->private_->is_ogg) 718 if(decoder->private_->is_ogg)
702 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect); 719 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
703 #endif 720 #endif
704 721
705 if(0 != decoder->private_->file) { 722 if(0 != decoder->private_->file) {
723 #ifndef USE_VFS
706 if(decoder->private_->file != stdin) 724 if(decoder->private_->file != stdin)
707 fclose(decoder->private_->file); 725 fclose(decoder->private_->file);
726 #endif
708 decoder->private_->file = 0; 727 decoder->private_->file = 0;
709 } 728 }
710 729
711 if(decoder->private_->do_md5_checking) { 730 if(decoder->private_->do_md5_checking) {
712 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16)) 731 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
993 * (internal_reset_hack) don't try to rewind since we are already at 1012 * (internal_reset_hack) don't try to rewind since we are already at
994 * the beginning of the stream and don't want to fail if the input is 1013 * the beginning of the stream and don't want to fail if the input is
995 * not seekable. 1014 * not seekable.
996 */ 1015 */
997 if(!decoder->private_->internal_reset_hack) { 1016 if(!decoder->private_->internal_reset_hack) {
1017 #ifndef USE_VFS
998 if(decoder->private_->file == stdin) 1018 if(decoder->private_->file == stdin)
999 return false; /* can't rewind stdin, reset fails */ 1019 return false; /* can't rewind stdin, reset fails */
1020 #endif
1000 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) 1021 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
1001 return false; /* seekable and seek fails, reset fails */ 1022 return false; /* seekable and seek fails, reset fails */
1002 } 1023 }
1003 else 1024 else
1004 decoder->private_->internal_reset_hack = false; 1025 decoder->private_->internal_reset_hack = false;
1270 } 1291 }
1271 1292
1272 /* 1293 /*
1273 * This will forcibly set stdin to binary mode (for OSes that require it) 1294 * This will forcibly set stdin to binary mode (for OSes that require it)
1274 */ 1295 */
1296 #ifndef USE_VFS
1275 FILE *get_binary_stdin_(void) 1297 FILE *get_binary_stdin_(void)
1276 { 1298 {
1277 /* if something breaks here it is probably due to the presence or 1299 /* if something breaks here it is probably due to the presence or
1278 * absence of an underscore before the identifiers 'setmode', 1300 * absence of an underscore before the identifiers 'setmode',
1279 * 'fileno', and/or 'O_BINARY'; check your system header files. 1301 * 'fileno', and/or 'O_BINARY'; check your system header files.
1285 setmode(_fileno(stdin), _O_BINARY); 1307 setmode(_fileno(stdin), _O_BINARY);
1286 #endif 1308 #endif
1287 1309
1288 return stdin; 1310 return stdin;
1289 } 1311 }
1312 #endif
1290 1313
1291 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels) 1314 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
1292 { 1315 {
1293 unsigned i; 1316 unsigned i;
1294 FLAC__int32 *tmp; 1317 FLAC__int32 *tmp;
3249 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) 3272 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3250 { 3273 {
3251 (void)client_data; 3274 (void)client_data;
3252 3275
3253 if(*bytes > 0) { 3276 if(*bytes > 0) {
3254 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file); 3277 *bytes = vfs_fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3278 #ifndef USE_VFS
3255 if(ferror(decoder->private_->file)) 3279 if(ferror(decoder->private_->file))
3256 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; 3280 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3257 else if(*bytes == 0) 3281 else
3282 #endif
3283 if(*bytes == 0)
3258 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; 3284 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3259 else 3285 else
3260 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; 3286 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3261 } 3287 }
3262 else 3288 else
3265 3291
3266 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) 3292 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3267 { 3293 {
3268 (void)client_data; 3294 (void)client_data;
3269 3295
3296 #ifndef USE_VFS
3270 if(decoder->private_->file == stdin) 3297 if(decoder->private_->file == stdin)
3271 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; 3298 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
3272 else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0) 3299 else
3300 #endif
3301 if(vfs_fseek(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3273 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; 3302 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
3274 else 3303 else
3275 return FLAC__STREAM_DECODER_SEEK_STATUS_OK; 3304 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
3276 } 3305 }
3277 3306
3278 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) 3307 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3279 { 3308 {
3280 off_t pos; 3309 off_t pos;
3281 (void)client_data; 3310 (void)client_data;
3282 3311
3312 #ifndef USE_VFS
3283 if(decoder->private_->file == stdin) 3313 if(decoder->private_->file == stdin)
3284 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; 3314 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
3285 else if((pos = ftello(decoder->private_->file)) < 0) 3315 else
3316 #endif
3317 if((pos = vfs_ftell(decoder->private_->file)) < 0)
3286 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; 3318 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
3287 else { 3319 else {
3288 *absolute_byte_offset = (FLAC__uint64)pos; 3320 *absolute_byte_offset = (FLAC__uint64)pos;
3289 return FLAC__STREAM_DECODER_TELL_STATUS_OK; 3321 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
3290 } 3322 }
3293 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) 3325 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3294 { 3326 {
3295 struct stat filestats; 3327 struct stat filestats;
3296 (void)client_data; 3328 (void)client_data;
3297 3329
3330 #ifndef USE_VFS
3298 if(decoder->private_->file == stdin) 3331 if(decoder->private_->file == stdin)
3299 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; 3332 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
3300 else if(fstat(fileno(decoder->private_->file), &filestats) != 0) 3333 else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
3301 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; 3334 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
3302 else { 3335 else {
3336 #endif
3303 *stream_length = (FLAC__uint64)filestats.st_size; 3337 *stream_length = (FLAC__uint64)filestats.st_size;
3304 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; 3338 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
3305 } 3339 #ifndef USE_VFS
3340 }
3341 #endif
3306 } 3342 }
3307 3343
3308 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data) 3344 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3309 { 3345 {
3310 (void)client_data; 3346 (void)client_data;
3311 3347
3312 return feof(decoder->private_->file)? true : false; 3348 return vfs_feof(decoder->private_->file)? true : false;
3313 } 3349 }