mp4/aac cleanups
git-svn-id: https://svn.musicpd.org/mpd/trunk@276 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
parent
f409d85bbd
commit
ad94c1dcf3
|
@ -38,6 +38,9 @@
|
||||||
#ifdef HAVE_AUDIOFILE
|
#ifdef HAVE_AUDIOFILE
|
||||||
#include "audiofile_decode.h"
|
#include "audiofile_decode.h"
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef HAVE_FAAD
|
||||||
|
#include "mp4_decode.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
@ -213,6 +216,11 @@ int decoderInit(PlayerControl * pc, Buffer * cb, AudioFormat *af,
|
||||||
dc->error = mp3_decode(cb,af,dc);
|
dc->error = mp3_decode(cb,af,dc);
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef HAVE_FAAD
|
||||||
|
case DECODE_TYPE_MP4:
|
||||||
|
dc->error = mp4_decode(cb,af,dc);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
#ifdef HAVE_OGG
|
#ifdef HAVE_OGG
|
||||||
case DECODE_TYPE_OGG:
|
case DECODE_TYPE_OGG:
|
||||||
dc->error = ogg_decode(cb,af,dc);
|
dc->error = ogg_decode(cb,af,dc);
|
||||||
|
|
|
@ -72,6 +72,8 @@
|
||||||
/* Define to empty if `const' does not conform to ANSI C. */
|
/* Define to empty if `const' does not conform to ANSI C. */
|
||||||
#undef const
|
#undef const
|
||||||
|
|
||||||
/* Define as `__inline' if that's what the C compiler calls it, or to nothing
|
/* Define to `__inline__' or `__inline' if that's what the C compiler
|
||||||
if it is not supported. */
|
calls it, or to nothing if 'inline' is not supported under any name. */
|
||||||
|
#ifndef __cplusplus
|
||||||
#undef inline
|
#undef inline
|
||||||
|
#endif
|
||||||
|
|
|
@ -125,9 +125,11 @@
|
||||||
/* Define to empty if `const' does not conform to ANSI C. */
|
/* Define to empty if `const' does not conform to ANSI C. */
|
||||||
#undef const
|
#undef const
|
||||||
|
|
||||||
/* Define as `__inline' if that's what the C compiler calls it, or to nothing
|
/* Define to `__inline__' or `__inline' if that's what the C compiler
|
||||||
if it is not supported. */
|
calls it, or to nothing if 'inline' is not supported under any name. */
|
||||||
|
#ifndef __cplusplus
|
||||||
#undef inline
|
#undef inline
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Define to `int' if <sys/types.h> does not define. */
|
/* Define to `int' if <sys/types.h> does not define. */
|
||||||
#undef pid_t
|
#undef pid_t
|
||||||
|
|
169
src/mp4_decode.c
169
src/mp4_decode.c
|
@ -36,6 +36,8 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <faad.h>
|
#include <faad.h>
|
||||||
|
|
||||||
|
/* all code here is either based on or copied from FAAD2's frontend code */
|
||||||
|
|
||||||
int mp4_getAACTrack(mp4ff_t *infile) {
|
int mp4_getAACTrack(mp4ff_t *infile) {
|
||||||
/* find AAC track */
|
/* find AAC track */
|
||||||
int i, rc;
|
int i, rc;
|
||||||
|
@ -75,20 +77,25 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc)
|
||||||
mp4ff_t * mp4fh;
|
mp4ff_t * mp4fh;
|
||||||
mp4ff_callback_t * mp4cb;
|
mp4ff_callback_t * mp4cb;
|
||||||
int32_t track;
|
int32_t track;
|
||||||
int32_t time;
|
float time;
|
||||||
int32_t scale;
|
int32_t scale;
|
||||||
faacDecHandle decoder;
|
faacDecHandle decoder;
|
||||||
faacDecFrameInfo frameInfo;
|
faacDecFrameInfo frameInfo;
|
||||||
faacDecConfigurationPtr config;
|
faacDecConfigurationPtr config;
|
||||||
mp4AudioSpecificConfig mp4ASC;
|
|
||||||
unsigned char * mp4Buffer;
|
unsigned char * mp4Buffer;
|
||||||
int mp4BufferSize;
|
int mp4BufferSize;
|
||||||
unsigned int frameSize;
|
|
||||||
unsigned int useAacLength;
|
|
||||||
unsigned long sampleRate;
|
unsigned long sampleRate;
|
||||||
unsigned char channels;
|
unsigned char channels;
|
||||||
long sampleId;
|
long sampleId;
|
||||||
long numSamples;
|
long numSamples;
|
||||||
|
int eof = 0;
|
||||||
|
int rc;
|
||||||
|
long dur;
|
||||||
|
unsigned int sampleCount;
|
||||||
|
char * sampleBuffer;
|
||||||
|
unsigned int initial = 1;
|
||||||
|
size_t sampleBufferLen;
|
||||||
|
|
||||||
|
|
||||||
fh = fopen(dc->file,"r");
|
fh = fopen(dc->file,"r");
|
||||||
if(!fh) {
|
if(!fh) {
|
||||||
|
@ -147,16 +154,8 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc)
|
||||||
af->channels = channels;
|
af->channels = channels;
|
||||||
time = mp4ff_get_track_duration_use_offsets(mp4fh,track);
|
time = mp4ff_get_track_duration_use_offsets(mp4fh,track);
|
||||||
scale = mp4ff_time_scale(mp4fh,track);
|
scale = mp4ff_time_scale(mp4fh,track);
|
||||||
frameSize = 1024;
|
|
||||||
useAacLength = 0;
|
|
||||||
|
|
||||||
if(mp4Buffer) {
|
if(mp4Buffer) free(mp4Buffer);
|
||||||
if(AudioSpecificConfig(mp4Buffer,mp4BufferSize,&mp4ASC) >= 0) {
|
|
||||||
if(mp4ASC.frameLengthFlag==1) frameSize = 960;
|
|
||||||
if(mp4ASC.sbr_present_flag==1) frameSize*= 2;
|
|
||||||
}
|
|
||||||
free(mp4Buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(scale < 0) {
|
if(scale < 0) {
|
||||||
ERROR("Error getting audio format of mp4 AAC track.\n");
|
ERROR("Error getting audio format of mp4 AAC track.\n");
|
||||||
|
@ -172,111 +171,75 @@ int mp4_decode(Buffer * cb, AudioFormat * af, DecoderControl * dc)
|
||||||
|
|
||||||
dc->state = DECODE_STATE_DECODE;
|
dc->state = DECODE_STATE_DECODE;
|
||||||
dc->start = 0;
|
dc->start = 0;
|
||||||
{
|
time = 0.0;
|
||||||
int eof = 0;
|
|
||||||
int rc;
|
|
||||||
long dur;
|
|
||||||
unsigned int sampleCount;
|
|
||||||
unsigned int delay = 0;
|
|
||||||
char * sampleBuffer;
|
|
||||||
unsigned int initial = 1;
|
|
||||||
size_t sampleBufferLen;
|
|
||||||
|
|
||||||
for(sampleId=0; sampleId<numSamples && !eof; sampleId++) {
|
for(sampleId=0; sampleId<numSamples && !eof; sampleId++) {
|
||||||
if(dc->seek) {
|
if(dc->seek) {
|
||||||
cb->end = 0;
|
cb->end = 0;
|
||||||
cb->wrap = 0;
|
cb->wrap = 0;
|
||||||
//#warning implement seeking here!
|
#warning implement seeking here!
|
||||||
dc->seek = 0;
|
dc->seek = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
dur = mp4ff_get_sample_duration(mp4fh,track,sampleId);
|
dur = mp4ff_get_sample_duration(mp4fh,track,sampleId);
|
||||||
rc = mp4ff_read_sample(mp4fh,track,sampleId,&mp4Buffer,
|
rc = mp4ff_read_sample(mp4fh,track,sampleId,&mp4Buffer,
|
||||||
&mp4BufferSize);
|
&mp4BufferSize);
|
||||||
|
|
||||||
if(rc==0) eof = 1;
|
if(rc==0) {
|
||||||
else {
|
eof = 1;
|
||||||
sampleBuffer = faacDecDecode(decoder,
|
break;
|
||||||
&frameInfo,
|
}
|
||||||
mp4Buffer,
|
|
||||||
mp4BufferSize);
|
|
||||||
if(mp4Buffer) free(mp4Buffer);
|
|
||||||
if(sampleId==0) dur = 0;
|
|
||||||
if(useAacLength || scale!=sampleRate) {
|
|
||||||
sampleCount = frameInfo.samples;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sampleCount = (unsigned long)(dur *
|
|
||||||
frameInfo.channels);
|
|
||||||
if(!useAacLength && !initial &&
|
|
||||||
(sampleId < numSamples/2) &&
|
|
||||||
(sampleCount!=
|
|
||||||
frameInfo.samples))
|
|
||||||
{
|
|
||||||
useAacLength = 1;
|
|
||||||
sampleCount = frameInfo.samples;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(initial && (sampleCount < frameSize*
|
|
||||||
frameInfo.channels) &&
|
|
||||||
(frameInfo.samples >
|
|
||||||
sampleCount))
|
|
||||||
{
|
|
||||||
delay = frameInfo.samples -
|
|
||||||
sampleCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
sampleBuffer = faacDecDecode(decoder,&frameInfo,mp4Buffer,
|
||||||
|
mp4BufferSize);
|
||||||
|
if(mp4Buffer) free(mp4Buffer);
|
||||||
|
if(sampleId==0) dur = 0;
|
||||||
|
time+=((float)dur)/scale;
|
||||||
|
sampleCount = (unsigned long)(dur*channels);
|
||||||
|
|
||||||
if(sampleCount>0) initial =0;
|
if(sampleCount>0) initial =0;
|
||||||
sampleBufferLen = sampleCount*2;
|
sampleBufferLen = sampleCount*2;
|
||||||
sampleBuffer+=delay*2;
|
while(sampleBufferLen > 0) {
|
||||||
while(sampleBufferLen > 0) {
|
size_t size = sampleBufferLen>CHUNK_SIZE ? CHUNK_SIZE:
|
||||||
size_t size = sampleBufferLen>
|
|
||||||
CHUNK_SIZE?
|
|
||||||
CHUNK_SIZE:
|
|
||||||
sampleBufferLen;
|
sampleBufferLen;
|
||||||
while(cb->begin==cb->end && cb->wrap &&
|
while(cb->begin==cb->end && cb->wrap &&
|
||||||
!dc->stop && !dc->seek)
|
!dc->stop && !dc->seek)
|
||||||
{
|
{
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
}
|
}
|
||||||
if(dc->stop) {
|
if(dc->stop) {
|
||||||
eof = 1;
|
eof = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if(dc->seek) break;
|
else if(dc->seek) break;
|
||||||
|
|
||||||
#ifdef WORDS_BIGENDIAN
|
#ifdef WORDS_BIGENDIAN
|
||||||
pcm_changeBufferEndianness(sampleBuffer,
|
pcm_changeBufferEndianness(sampleBuffer,size,af->bits);
|
||||||
size,af->bits);
|
|
||||||
#endif
|
#endif
|
||||||
memcpy(cb->chunks+cb->end*CHUNK_SIZE,
|
sampleBufferLen-=size;
|
||||||
sampleBuffer,size);
|
memcpy(cb->chunks+cb->end*CHUNK_SIZE,sampleBuffer,size);
|
||||||
cb->chunkSize[cb->end] = size;
|
cb->chunkSize[cb->end] = size;
|
||||||
|
sampleBuffer+=size;
|
||||||
|
|
||||||
//#warning implement time for AAC
|
cb->times[cb->end] = time;
|
||||||
cb->times[cb->end] = 0;
|
|
||||||
|
|
||||||
++cb->end;
|
++cb->end;
|
||||||
|
|
||||||
if(cb->end>=buffered_chunks) {
|
if(cb->end>=buffered_chunks) {
|
||||||
cb->end = 0;
|
cb->end = 0;
|
||||||
cb->wrap = 1;
|
cb->wrap = 1;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dc->seek) dc->seek = 0;
|
|
||||||
|
|
||||||
if(dc->stop) {
|
|
||||||
dc->state = DECODE_STATE_STOP;
|
|
||||||
dc->stop = 0;
|
|
||||||
}
|
|
||||||
else dc->state = DECODE_STATE_STOP;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(dc->seek) dc->seek = 0;
|
||||||
|
|
||||||
|
if(dc->stop) {
|
||||||
|
dc->state = DECODE_STATE_STOP;
|
||||||
|
dc->stop = 0;
|
||||||
|
}
|
||||||
|
else dc->state = DECODE_STATE_STOP;
|
||||||
|
|
||||||
faacDecClose(decoder);
|
faacDecClose(decoder);
|
||||||
mp4ff_close(mp4fh);
|
mp4ff_close(mp4fh);
|
||||||
fclose(fh);
|
fclose(fh);
|
||||||
|
|
Loading…
Reference in New Issue