mp4/aac now use InputStream

git-svn-id: https://svn.musicpd.org/mpd/trunk@925 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Warren Dukes 2004-05-06 18:49:04 +00:00
parent 9ca135d723
commit ef9f832fb3
4 changed files with 58 additions and 55 deletions

View File

@ -26,6 +26,7 @@
#include "utils.h" #include "utils.h"
#include "audio.h" #include "audio.h"
#include "log.h" #include "log.h"
#include "inputStream.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -35,12 +36,12 @@
/* all code here is either based on or copied from FAAD2's frontend code */ /* all code here is either based on or copied from FAAD2's frontend code */
typedef struct { typedef struct {
InputStream * inStream;
long bytesIntoBuffer; long bytesIntoBuffer;
long bytesConsumed; long bytesConsumed;
long fileOffset; long fileOffset;
unsigned char *buffer; unsigned char *buffer;
int atEof; int atEof;
FILE *infile;
} AacBuffer; } AacBuffer;
void fillAacBuffer(AacBuffer *b) { void fillAacBuffer(AacBuffer *b) {
@ -53,8 +54,9 @@ void fillAacBuffer(AacBuffer *b) {
} }
if(!b->atEof) { if(!b->atEof) {
bread = fread((void *)(b->buffer+b->bytesIntoBuffer),1, bread = readFromInputStream(b->inStream,
b->bytesConsumed,b->infile); (void *)(b->buffer+b->bytesIntoBuffer),
1,b->bytesConsumed);
if(bread!=b->bytesConsumed) b->atEof = 1; if(bread!=b->bytesConsumed) b->atEof = 1;
b->bytesIntoBuffer+=bread; b->bytesIntoBuffer+=bread;
} }
@ -132,7 +134,7 @@ int adtsParse(AacBuffer * b, float * length) {
return 1; 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 * retFileread, size_t * retTagsize)
{ {
size_t fileread; size_t fileread;
@ -143,17 +145,15 @@ void initAacBuffer(FILE * fp, AacBuffer * b, float * length,
memset(b,0,sizeof(AacBuffer)); memset(b,0,sizeof(AacBuffer));
b->infile = fp; b->inStream = inStream;
fseek(b->infile,0,SEEK_END); fileread = inStream->size;
fileread = ftell(b->infile);
fseek(b->infile,0,SEEK_SET);
b->buffer = malloc(FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS); b->buffer = malloc(FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS);
memset(b->buffer,0,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, bread = readFromInputStream(inStream,b->buffer,1,
b->infile); FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS);
b->bytesIntoBuffer = bread; b->bytesIntoBuffer = bread;
b->bytesConsumed = 0; b->bytesConsumed = 0;
b->fileOffset = 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)) { if((b->buffer[0] == 0xFF) && ((b->buffer[1] & 0xF6) == 0xF0)) {
adtsParse(b, length); adtsParse(b, length);
fseek(b->infile, tagsize, SEEK_SET); seekInputStream(b->inStream, tagsize, SEEK_SET);
bread = fread(b->buffer, 1, bread = readFromInputStream(b->inStream, b->buffer, 1,
FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS, FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS);
b->infile);
if(bread != FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS) b->atEof = 1; if(bread != FAAD_MIN_STREAMSIZE*AAC_MAX_CHANNELS) b->atEof = 1;
else b->atEof = 0; else b->atEof = 0;
b->bytesIntoBuffer = bread; b->bytesIntoBuffer = bread;
@ -209,12 +208,12 @@ float getAacFloatTotalTime(char * file) {
faacDecConfigurationPtr config; faacDecConfigurationPtr config;
unsigned long sampleRate; unsigned long sampleRate;
unsigned char channels; unsigned char channels;
FILE * fp = fopen(file,"r"); InputStream inStream;
size_t bread; 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) { if(length < 0) {
decoder = faacDecOpen(); decoder = faacDecOpen();
@ -236,7 +235,7 @@ float getAacFloatTotalTime(char * file) {
} }
if(b.buffer) free(b.buffer); if(b.buffer) free(b.buffer);
fclose(b.infile); closeInputStream(&inStream);
return length; return length;
} }
@ -270,15 +269,13 @@ int aac_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
int seekPositionFound = 0;*/ int seekPositionFound = 0;*/
mpd_uint16 bitRate = 0; mpd_uint16 bitRate = 0;
AacBuffer b; AacBuffer b;
FILE * fp; InputStream inStream;
if((totalTime = getAacFloatTotalTime(dc->file)) < 0) return -1; 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(&inStream,&b,NULL,NULL,NULL);
initAacBuffer(fp,&b,NULL,NULL,NULL);
decoder = faacDecOpen(); decoder = faacDecOpen();
@ -303,7 +300,7 @@ int aac_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
if(bread < 0) { if(bread < 0) {
ERROR("Error not a AAC stream.\n"); ERROR("Error not a AAC stream.\n");
faacDecClose(decoder); faacDecClose(decoder);
fclose(b.infile); closeInputStream(b.inStream);
if(b.buffer) free(b.buffer); if(b.buffer) free(b.buffer);
return -1; return -1;
} }
@ -404,7 +401,7 @@ int aac_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
} while (!eof); } while (!eof);
faacDecClose(decoder); faacDecClose(decoder);
fclose(b.infile); closeInputStream(b.inStream);
if(b.buffer) free(b.buffer); if(b.buffer) free(b.buffer);
if(dc->start) return -1; if(dc->start) return -1;

View File

@ -25,6 +25,7 @@
#include "audio.h" #include "audio.h"
#include "log.h" #include "log.h"
#include "pcm_utils.h" #include "pcm_utils.h"
#include "inputStream.h"
#include "mp4ff/mp4ff.h" #include "mp4ff/mp4ff.h"
@ -72,17 +73,18 @@ int mp4_getAACTrack(mp4ff_t *infile) {
return -1; return -1;
} }
uint32_t mp4_readCallback(void *user_data, void *buffer, uint32_t length) { uint32_t mp4_inputStreamReadCallback(void *inStream, void *buffer,
return fread(buffer, 1, length, (FILE*)user_data); uint32_t length)
{
return readFromInputStream((InputStream*) inStream, buffer, 1, length);
} }
uint32_t mp4_seekCallback(void *user_data, uint64_t position) { uint32_t mp4_inputStreamSeekCallback(void *inStream, uint64_t position) {
return fseek((FILE*)user_data, position, SEEK_SET); return seekInputStream((InputStream *) inStream, position, SEEK_SET);
} }
int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) { int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
FILE * fh;
mp4ff_t * mp4fh; mp4ff_t * mp4fh;
mp4ff_callback_t * mp4cb; mp4ff_callback_t * mp4cb;
int32_t track; int32_t track;
@ -109,23 +111,23 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
int seekPositionFound = 0; int seekPositionFound = 0;
long offset; long offset;
mpd_uint16 bitRate = 0; mpd_uint16 bitRate = 0;
InputStream inStream;
fh = fopen(dc->file,"r"); if(openInputStreamFromFile(&inStream,dc->file) < 0) {
if(!fh) {
ERROR("failed to open %s\n",dc->file); ERROR("failed to open %s\n",dc->file);
return -1; return -1;
} }
mp4cb = malloc(sizeof(mp4ff_callback_t)); mp4cb = malloc(sizeof(mp4ff_callback_t));
mp4cb->read = mp4_readCallback; mp4cb->read = mp4_inputStreamReadCallback;
mp4cb->seek = mp4_seekCallback; mp4cb->seek = mp4_inputStreamSeekCallback;
mp4cb->user_data = fh; mp4cb->user_data = &inStream;
mp4fh = mp4ff_open_read(mp4cb); mp4fh = mp4ff_open_read(mp4cb);
if(!mp4fh) { if(!mp4fh) {
ERROR("Input does not appear to be a mp4 stream.\n"); ERROR("Input does not appear to be a mp4 stream.\n");
free(mp4cb); free(mp4cb);
fclose(fh); closeInputStream(&inStream);
return -1; return -1;
} }
@ -133,7 +135,7 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
if(track < 0) { if(track < 0) {
ERROR("No AAC track found in mp4 stream.\n"); ERROR("No AAC track found in mp4 stream.\n");
mp4ff_close(mp4fh); mp4ff_close(mp4fh);
fclose(fh); closeInputStream(&inStream);
free(mp4cb); free(mp4cb);
return -1; return -1;
} }
@ -163,7 +165,7 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
faacDecClose(decoder); faacDecClose(decoder);
mp4ff_close(mp4fh); mp4ff_close(mp4fh);
free(mp4cb); free(mp4cb);
fclose(fh); closeInputStream(&inStream);
return -1; 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"); ERROR("Error getting audio format of mp4 AAC track.\n");
faacDecClose(decoder); faacDecClose(decoder);
mp4ff_close(mp4fh); mp4ff_close(mp4fh);
fclose(fh); closeInputStream(&inStream);
free(mp4cb); free(mp4cb);
return -1; return -1;
} }
@ -316,7 +318,7 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc) {
free(seekTable); free(seekTable);
faacDecClose(decoder); faacDecClose(decoder);
mp4ff_close(mp4fh); mp4ff_close(mp4fh);
fclose(fh); closeInputStream(&inStream);
free(mp4cb); free(mp4cb);
if(dc->start) return -1; if(dc->start) return -1;

View File

@ -29,12 +29,14 @@
int mp4_getAACTrack(mp4ff_t *infile); 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); 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 /* HAVE_FAAD */
#endif #endif

View File

@ -26,6 +26,7 @@
#include "utils.h" #include "utils.h"
#include "utf8.h" #include "utf8.h"
#include "log.h" #include "log.h"
#include "inputStream.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.h> #include <stdlib.h>
@ -209,7 +210,7 @@ MpdTag * aacTagDup(char * utf8file) {
MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) { MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) {
MpdTag * ret = NULL; MpdTag * ret = NULL;
FILE * fh; InputStream inStream;
mp4ff_t * mp4fh; mp4ff_t * mp4fh;
mp4ff_callback_t * cb; mp4ff_callback_t * cb;
int32_t track; int32_t track;
@ -218,27 +219,28 @@ MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) {
*mp4MetadataFound = 0; *mp4MetadataFound = 0;
fh = fopen(rmp2amp(utf8ToFsCharset(utf8file)),"r"); if(openInputStreamFromFile(&inStream,rmp2amp(utf8ToFsCharset(utf8file)))
if(!fh) { < 0)
{
return NULL; return NULL;
} }
cb = malloc(sizeof(mp4ff_callback_t)); cb = malloc(sizeof(mp4ff_callback_t));
cb->read = mp4_readCallback; cb->read = mp4_inputStreamReadCallback;
cb->seek = mp4_seekCallback; cb->seek = mp4_inputStreamSeekCallback;
cb->user_data = fh; cb->user_data = &inStream;
mp4fh = mp4ff_open_read(cb); mp4fh = mp4ff_open_read(cb);
if(!mp4fh) { if(!mp4fh) {
free(cb); free(cb);
fclose(fh); closeInputStream(&inStream);
return NULL; return NULL;
} }
track = mp4_getAACTrack(mp4fh); track = mp4_getAACTrack(mp4fh);
if(track < 0) { if(track < 0) {
mp4ff_close(mp4fh); mp4ff_close(mp4fh);
fclose(fh); closeInputStream(&inStream);
free(cb); free(cb);
return NULL; return NULL;
} }
@ -248,7 +250,7 @@ MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) {
scale = mp4ff_time_scale(mp4fh,track); scale = mp4ff_time_scale(mp4fh,track);
if(scale < 0) { if(scale < 0) {
mp4ff_close(mp4fh); mp4ff_close(mp4fh);
fclose(fh); closeInputStream(&inStream);
free(cb); free(cb);
freeMpdTag(ret); freeMpdTag(ret);
return NULL; return NULL;
@ -272,7 +274,7 @@ MpdTag * mp4DataDup(char * utf8file, int * mp4MetadataFound) {
} }
mp4ff_close(mp4fh); mp4ff_close(mp4fh);
fclose(fh); closeInputStream(&inStream);
free(cb); free(cb);
return ret; return ret;