From 659df586719c727204513c97c243c9f14dbfa5d9 Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Tue, 4 May 2004 19:49:29 +0000 Subject: implement input abstraction for ogg and mp3 git-svn-id: https://svn.musicpd.org/mpd/trunk@904 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/inputStream.c | 21 ++++++++------------- src/inputStream.h | 10 +++++----- src/mp3_decode.c | 9 ++++----- src/ogg_decode.c | 39 +++++++++++++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/src/inputStream.c b/src/inputStream.c index 0856dd125..9168e6aeb 100644 --- a/src/inputStream.c +++ b/src/inputStream.c @@ -23,7 +23,7 @@ #include #include -int initInputStreamFromFile(InputStream * inStream, char * filename) { +int openInputStreamFromFile(InputStream * inStream, char * filename) { inStream->fp = fopen(filename,"r"); if(!inStream->fp) { inStream->error = errno; @@ -39,14 +39,9 @@ int initInputStreamFromFile(InputStream * inStream, char * filename) { return 0; } -int seekInputStream(InputStream * inStream, long offset) { - if(offset<0) { - inStream->error = EINVAL; - return -1; - } - - if(fseek(inStream->fp,offset,SEEK_SET)==0) { - inStream->offset = offset; +int seekInputStream(InputStream * inStream, long offset, int whence) { + if(fseek(inStream->fp,offset,whence)==0) { + inStream->offset = ftell(inStream->fp); } else { inStream->error = errno; @@ -56,19 +51,19 @@ int seekInputStream(InputStream * inStream, long offset) { return 0; } -size_t fillBufferFromInputStream(InputStream * inStream, char * buffer, - size_t buflen) +size_t readFromInputStream(InputStream * inStream, void * ptr, size_t size, + size_t nmemb) { size_t readSize; - readSize = fread(buffer,1,buflen,inStream->fp); + readSize = fread(ptr,size,nmemb,inStream->fp); if(readSize>0) inStream->offset+=readSize; return readSize; } -int finishInputStream(InputStream * inStream) { +int closeInputStream(InputStream * inStream) { if(fclose(inStream->fp)<0) { inStream->error = errno; } diff --git a/src/inputStream.h b/src/inputStream.h index 2f11da044..8998e3ac1 100644 --- a/src/inputStream.h +++ b/src/inputStream.h @@ -31,11 +31,11 @@ typedef struct _InputStream { /* if an error occurs for these 3 functions, then -1 is returned and errno for the input stream is set */ -int initInputStreamFromFile(InputStream * inStream, char * filename); -int seekInputStream(InputStream * inStream, long offset); -int finishInputStream(InputStream * inStream); +int openInputStreamFromFile(InputStream * inStream, char * filename); +int seekInputStream(InputStream * inStream, long offset, int whence); +int closeInputStream(InputStream * inStream); -size_t fillBufferFromInputStream(InputStream * inStream, char * buffer, - size_t buflen); +size_t readFromInputStream(InputStream * inStream, void * ptr, size_t size, + size_t nmemb); #endif diff --git a/src/mp3_decode.c b/src/mp3_decode.c index 58cd62a52..cf1c9b54e 100644 --- a/src/mp3_decode.c +++ b/src/mp3_decode.c @@ -136,7 +136,7 @@ typedef struct _mp3DecodeData { int initMp3DecodeData(mp3DecodeData * data, char * file) { int ret; - while(((ret = initInputStreamFromFile(&(data->inStream),file))<0) && + while(((ret = openInputStreamFromFile(&(data->inStream),file))<0) && data->inStream.error==EINTR); if(ret<0) return -1; @@ -164,7 +164,7 @@ int fillMp3InputBuffer(mp3DecodeData * data, long offset) { unsigned char * readStart; if(offset>=0) { - seekInputStream(&(data->inStream),offset); + seekInputStream(&(data->inStream),offset,SEEK_SET); } if(offset==-1 && (data->stream).next_frame!=NULL) { @@ -179,8 +179,7 @@ int fillMp3InputBuffer(mp3DecodeData * data, long offset) { remaining = 0; } - readSize = fillBufferFromInputStream(&(data->inStream),readStart, - readSize); + readSize = readFromInputStream(&(data->inStream),readStart,1,readSize); if(readSize<=0) return -1; mad_stream_buffer(&data->stream,data->readBuffer,readSize+remaining); @@ -375,7 +374,7 @@ void mp3DecodeDataFinalize(mp3DecodeData * data) { mad_frame_finish(&data->frame); mad_stream_finish(&data->stream); - while(finishInputStream(&(data->inStream))<0 && + while(closeInputStream(&(data->inStream))<0 && data->inStream.error==EINTR); if(data->frameOffset) free(data->frameOffset); if(data->times) free(data->times); diff --git a/src/ogg_decode.c b/src/ogg_decode.c index 7d673d462..e37dae2a2 100644 --- a/src/ogg_decode.c +++ b/src/ogg_decode.c @@ -25,12 +25,14 @@ #include "audio.h" #include "log.h" #include "pcm_utils.h" +#include "inputStream.h" #include #include #include #include #include +#include #ifdef WORDS_BIGENDIAN #define OGG_DECODE_USE_BIGENDIAN 1 @@ -38,6 +40,7 @@ #define OGG_DECODE_USE_BIGENDIAN 0 #endif +/* this is just for tag parsing for db import! */ int getOggTotalTime(char * file) { OggVorbis_File vf; FILE * oggfp; @@ -57,19 +60,47 @@ int getOggTotalTime(char * file) { return totalTime; } +size_t ogg_read_cb(void * ptr, size_t size, size_t nmemb, void * inStream) +{ + size_t ret; + ret = readFromInputStream((InputStream *)inStream,ptr,size,nmemb); + + if(ret<0) errno = ((InputStream *)inStream)->error; + + return ret; +} + +int ogg_seek_cb(void * inStream, ogg_int64_t offset, int whence) { + return seekInputStream((InputStream *)inStream,offset,whence); +} + +int ogg_close_cb(void * inStream) { + return closeInputStream((InputStream *)inStream); +} + +long ogg_tell_cb(void * inStream) { + return ((InputStream *)inStream)->offset; +} + int ogg_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { OggVorbis_File vf; - FILE * oggfp; + ov_callbacks callbacks; + InputStream inStream; + + callbacks.read_func = ogg_read_cb; + callbacks.seek_func = ogg_seek_cb; + callbacks.close_func = ogg_close_cb; + callbacks.tell_func = ogg_tell_cb; - if(!(oggfp = fopen(dc->file,"r"))) { + if(openInputStreamFromFile(&inStream,dc->file)<0) { ERROR("failed to open ogg\n"); return -1; } - if(ov_open(oggfp, &vf, NULL, 0) < 0) { + if(ov_open_callbacks(&inStream, &vf, NULL, 0, callbacks) < 0) { ERROR("Input does not appear to be an Ogg bit stream.\n"); - fclose(oggfp); + closeInputStream(&inStream); return -1; } -- cgit v1.2.3