diff --git a/src/aac_decode.c b/src/aac_decode.c index be0f332ec..9d56e3392 100644 --- a/src/aac_decode.c +++ b/src/aac_decode.c @@ -26,6 +26,7 @@ #include "utils.h" #include "audio.h" #include "log.h" +#include "inputStream.h" #include #include @@ -35,12 +36,12 @@ /* all code here is either based on or copied from FAAD2's frontend code */ typedef struct { + InputStream * inStream; long bytesIntoBuffer; long bytesConsumed; long fileOffset; unsigned char *buffer; int atEof; - FILE *infile; } AacBuffer; void fillAacBuffer(AacBuffer *b) { @@ -53,8 +54,9 @@ void fillAacBuffer(AacBuffer *b) { } if(!b->atEof) { - bread = fread((void *)(b->buffer+b->bytesIntoBuffer),1, - b->bytesConsumed,b->infile); + bread = readFromInputStream(b->inStream, + (void *)(b->buffer+b->bytesIntoBuffer), + 1,b->bytesConsumed); if(bread!=b->bytesConsumed) b->atEof = 1; b->bytesIntoBuffer+=bread; } @@ -132,7 +134,7 @@ int adtsParse(AacBuffer * b, float * length) { return 1; } -void initAacBuffer(FILE * fp, AacBuffer * b, float * length, +void initAacBuffer(InputStream * inStream, AacBuffer * b, float * length, size_t * retFileread, size_t * retTagsize) { size_t fileread; @@ -143,17 +145,15 @@ void initAacBuffer(FILE * fp, AacBuffer * b, float * length, memset(b,0,sizeof(AacBuffer)); - b->infile = fp; + b->inStream = inStream; - fseek(b->infile,0,SEEK_END); - fileread = ftell(b->infile); - fseek(b->infile,0,SEEK_SET); + fileread = inStream->size; b->buffer = malloc(FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS); memset(b->buffer,0,FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS); - bread = fread(b->buffer,1,FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS, - b->infile); + bread = readFromInputStream(inStream,b->buffer,1, + FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS); b->bytesIntoBuffer = bread; b->bytesConsumed = 0; b->fileOffset = 0; @@ -177,11 +177,10 @@ void initAacBuffer(FILE * fp, AacBuffer * b, float * length, if((b->buffer[0] == 0xFF) && ((b->buffer[1] & 0xF6) == 0xF0)) { adtsParse(b, length); - fseek(b->infile, tagsize, SEEK_SET); + seekInputStream(b->inStream, tagsize, SEEK_SET); - bread = fread(b->buffer, 1, - FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS, - b->infile); + bread = readFromInputStream(b->inStream, b->buffer, 1, + FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS); if(bread != FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS) b->atEof = 1; else b->atEof = 0; b->bytesIntoBuffer = bread; @@ -209,12 +208,12 @@ float getAacFloatTotalTime(char * file) { faacDecConfigurationPtr config; unsigned long sampleRate; unsigned char channels; - FILE * fp = fopen(file,"r"); + InputStream inStream; size_t bread; - if(fp==NULL) return -1; + if(openInputStreamFromFile(&inStream,file) < 0) return -1; - initAacBuffer(fp,&b,&length,&fileread,&tagsize); + initAacBuffer(&inStream,&b,&length,&fileread,&tagsize); if(length < 0) { decoder = faacDecOpen(); @@ -236,7 +235,7 @@ float getAacFloatTotalTime(char * file) { } if(b.buffer) free(b.buffer); - fclose(b.infile); + closeInputStream(&inStream); return length; } @@ -270,15 +269,13 @@ int aac_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { int seekPositionFound = 0;*/ mpd_uint16 bitRate = 0; AacBuffer b; - FILE * fp; + InputStream inStream; if((totalTime = getAacFloatTotalTime(dc->file)) < 0) return -1; - fp = fopen(dc->file,"r"); + if(openInputStreamFromFile(&inStream,dc->file) < 0) return -1; - if(fp==NULL) return -1; - - initAacBuffer(fp,&b,NULL,NULL,NULL); + initAacBuffer(&inStream,&b,NULL,NULL,NULL); decoder = faacDecOpen(); @@ -303,7 +300,7 @@ int aac_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { if(bread < 0) { ERROR("Error not a AAC stream.\n"); faacDecClose(decoder); - fclose(b.infile); + closeInputStream(b.inStream); if(b.buffer) free(b.buffer); return -1; } @@ -404,7 +401,7 @@ int aac_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { } while (!eof); faacDecClose(decoder); - fclose(b.infile); + closeInputStream(b.inStream); if(b.buffer) free(b.buffer); if(dc->start) return -1; diff --git a/src/mp4_decode.c b/src/mp4_decode.c index 2e64b6a96..b6cea1d62 100644 --- a/src/mp4_decode.c +++ b/src/mp4_decode.c @@ -25,6 +25,7 @@ #include "audio.h" #include "log.h" #include "pcm_utils.h" +#include "inputStream.h" #include "mp4ff/mp4ff.h" @@ -72,17 +73,18 @@ int mp4_getAACTrack(mp4ff_t *infile) { return -1; } -uint32_t mp4_readCallback(void *user_data, void *buffer, uint32_t length) { - return fread(buffer, 1, length, (FILE*)user_data); +uint32_t mp4_inputStreamReadCallback(void *inStream, void *buffer, + uint32_t length) +{ + return readFromInputStream((InputStream*) inStream, buffer, 1, length); } -uint32_t mp4_seekCallback(void *user_data, uint64_t position) { - return fseek((FILE*)user_data, position, SEEK_SET); +uint32_t mp4_inputStreamSeekCallback(void *inStream, uint64_t position) { + return seekInputStream((InputStream *) inStream, position, SEEK_SET); } int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { - FILE * fh; mp4ff_t * mp4fh; mp4ff_callback_t * mp4cb; int32_t track; @@ -109,23 +111,23 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { int seekPositionFound = 0; long offset; mpd_uint16 bitRate = 0; + InputStream inStream; - fh = fopen(dc->file,"r"); - if(!fh) { + if(openInputStreamFromFile(&inStream,dc->file) < 0) { ERROR("failed to open %s\n",dc->file); return -1; } mp4cb = malloc(sizeof(mp4ff_callback_t)); - mp4cb->read = mp4_readCallback; - mp4cb->seek = mp4_seekCallback; - mp4cb->user_data = fh; + mp4cb->read = mp4_inputStreamReadCallback; + mp4cb->seek = mp4_inputStreamSeekCallback; + mp4cb->user_data = &inStream; mp4fh = mp4ff_open_read(mp4cb); if(!mp4fh) { ERROR("Input does not appear to be a mp4 stream.\n"); free(mp4cb); - fclose(fh); + closeInputStream(&inStream); return -1; } @@ -133,7 +135,7 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { if(track < 0) { ERROR("No AAC track found in mp4 stream.\n"); mp4ff_close(mp4fh); - fclose(fh); + closeInputStream(&inStream); free(mp4cb); return -1; } @@ -163,7 +165,7 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { faacDecClose(decoder); mp4ff_close(mp4fh); free(mp4cb); - fclose(fh); + closeInputStream(&inStream); return -1; } @@ -178,7 +180,7 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { ERROR("Error getting audio format of mp4 AAC track.\n"); faacDecClose(decoder); mp4ff_close(mp4fh); - fclose(fh); + closeInputStream(&inStream); free(mp4cb); return -1; } @@ -316,7 +318,7 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { free(seekTable); faacDecClose(decoder); mp4ff_close(mp4fh); - fclose(fh); + closeInputStream(&inStream); free(mp4cb); if(dc->start) return -1; diff --git a/src/mp4_decode.h b/src/mp4_decode.h index 4028db453..0904dcc28 100644 --- a/src/mp4_decode.h +++ b/src/mp4_decode.h @@ -29,12 +29,14 @@ int mp4_getAACTrack(mp4ff_t *infile); -uint32_t mp4_readCallback(void *user_data, void *buffer, uint32_t length); - -uint32_t mp4_seekCallback(void *user_data, uint64_t position); - int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc); +uint32_t mp4_inputStreamReadCallback(void *inStream, void *buffer, + uint32_t length); + +uint32_t mp4_inputStreamSeekCallback(void *inStream, uint64_t position); + + #endif /* HAVE_FAAD */ #endif diff --git a/src/tag.c b/src/tag.c index 42124adfd..ccd7b421e 100644 --- a/src/tag.c +++ b/src/tag.c @@ -26,6 +26,7 @@ #include "utils.h" #include "utf8.h" #include "log.h" +#include "inputStream.h" #include #include @@ -209,7 +210,7 @@ MpdTag * aacTagDup(char * utf8file) { MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) { MpdTag * ret = NULL; - FILE * fh; + InputStream inStream; mp4ff_t * mp4fh; mp4ff_callback_t * cb; int32_t track; @@ -217,28 +218,29 @@ MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) { int32_t scale; *mp4MetadataFound = 0; - - fh = fopen(rmp2amp(utf8ToFsCharset(utf8file)),"r"); - if(!fh) { + + if(openInputStreamFromFile(&inStream,rmp2amp(utf8ToFsCharset(utf8file))) + < 0) + { return NULL; } cb = malloc(sizeof(mp4ff_callback_t)); - cb->read = mp4_readCallback; - cb->seek = mp4_seekCallback; - cb->user_data = fh; + cb->read = mp4_inputStreamReadCallback; + cb->seek = mp4_inputStreamSeekCallback; + cb->user_data = &inStream; mp4fh = mp4ff_open_read(cb); if(!mp4fh) { free(cb); - fclose(fh); + closeInputStream(&inStream); return NULL; } track = mp4_getAACTrack(mp4fh); if(track < 0) { mp4ff_close(mp4fh); - fclose(fh); + closeInputStream(&inStream); free(cb); return NULL; } @@ -248,7 +250,7 @@ MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) { scale = mp4ff_time_scale(mp4fh,track); if(scale < 0) { mp4ff_close(mp4fh); - fclose(fh); + closeInputStream(&inStream); free(cb); freeMpdTag(ret); return NULL; @@ -272,7 +274,7 @@ MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) { } mp4ff_close(mp4fh); - fclose(fh); + closeInputStream(&inStream); free(cb); return ret;