implement input abstraction for ogg and mp3

git-svn-id: https://svn.musicpd.org/mpd/trunk@904 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Warren Dukes 2004-05-04 19:49:29 +00:00
parent 638817fc5b
commit 659df58671
4 changed files with 52 additions and 27 deletions

View File

@ -23,7 +23,7 @@
#include <unistd.h>
#include <errno.h>
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;
}

View File

@ -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

View File

@ -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);

View File

@ -25,12 +25,14 @@
#include "audio.h"
#include "log.h"
#include "pcm_utils.h"
#include "inputStream.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <vorbis/vorbisfile.h>
#include <errno.h>
#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;
}