2003-05-12 10:55:19 +00:00
|
|
|
/*
|
|
|
|
* Faad decoder
|
|
|
|
* Copyright (c) 2003 Zdenek Kabelac.
|
2004-04-09 14:53:41 +00:00
|
|
|
* Copyright (c) 2004 Thomas Raivio.
|
2003-05-12 10:55:19 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-01-12 22:43:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-05-12 10:55:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file faad.c
|
|
|
|
* AAC decoder.
|
|
|
|
*
|
|
|
|
* still a bit unfinished - but it plays something
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "faad.h"
|
|
|
|
|
2004-06-06 03:52:42 +00:00
|
|
|
#ifndef FAADAPI
|
|
|
|
#define FAADAPI
|
|
|
|
#endif
|
|
|
|
|
2003-05-12 10:55:19 +00:00
|
|
|
/*
|
|
|
|
* when CONFIG_FAADBIN is defined the libfaad will be opened at runtime
|
|
|
|
*/
|
|
|
|
//#undef CONFIG_FAADBIN
|
|
|
|
//#define CONFIG_FAADBIN
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAADBIN
|
|
|
|
#include <dlfcn.h>
|
|
|
|
static const char* libfaadname = "libfaad.so.0";
|
|
|
|
#else
|
|
|
|
#define dlopen(a)
|
|
|
|
#define dlclose(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
2005-12-22 01:10:11 +00:00
|
|
|
void* handle; /* dlopen handle */
|
|
|
|
void* faac_handle; /* FAAD library handle */
|
2003-05-12 10:55:19 +00:00
|
|
|
int sample_size;
|
2006-02-08 00:30:58 +00:00
|
|
|
int init;
|
2003-05-12 10:55:19 +00:00
|
|
|
|
|
|
|
/* faad calls */
|
|
|
|
faacDecHandle FAADAPI (*faacDecOpen)(void);
|
|
|
|
faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifndef FAAD2_VERSION
|
2005-12-22 01:10:11 +00:00
|
|
|
int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
|
2004-04-09 14:53:41 +00:00
|
|
|
faacDecConfigurationPtr config);
|
2005-12-22 01:10:11 +00:00
|
|
|
int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
|
|
|
|
unsigned char *buffer,
|
|
|
|
unsigned long *samplerate,
|
|
|
|
unsigned long *channels);
|
|
|
|
int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
|
2004-04-09 14:53:41 +00:00
|
|
|
unsigned long SizeOfDecoderSpecificInfo,
|
|
|
|
unsigned long *samplerate, unsigned long *channels);
|
2005-12-22 01:10:11 +00:00
|
|
|
int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
|
|
|
|
unsigned char *buffer,
|
|
|
|
unsigned long *bytesconsumed,
|
|
|
|
short *sample_buffer,
|
2004-04-09 14:53:41 +00:00
|
|
|
unsigned long *samples);
|
|
|
|
#else
|
2005-12-22 01:10:11 +00:00
|
|
|
unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
|
2004-04-09 14:53:41 +00:00
|
|
|
faacDecConfigurationPtr config);
|
2005-12-22 01:10:11 +00:00
|
|
|
long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
|
|
|
|
unsigned char *buffer,
|
|
|
|
unsigned long buffer_size,
|
|
|
|
unsigned long *samplerate,
|
|
|
|
unsigned char *channels);
|
|
|
|
char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
|
2003-05-12 10:55:19 +00:00
|
|
|
unsigned long SizeOfDecoderSpecificInfo,
|
|
|
|
unsigned long *samplerate, unsigned char *channels);
|
2005-12-22 01:10:11 +00:00
|
|
|
void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
|
|
|
|
faacDecFrameInfo *hInfo,
|
|
|
|
unsigned char *buffer,
|
|
|
|
unsigned long buffer_size);
|
|
|
|
char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
|
2004-04-09 14:53:41 +00:00
|
|
|
#endif
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-12 10:55:19 +00:00
|
|
|
void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
|
2005-12-17 18:14:38 +00:00
|
|
|
|
|
|
|
|
2003-05-12 10:55:19 +00:00
|
|
|
} FAACContext;
|
|
|
|
|
|
|
|
static const unsigned long faac_srates[] =
|
|
|
|
{
|
|
|
|
96000, 88200, 64000, 48000, 44100, 32000,
|
|
|
|
24000, 22050, 16000, 12000, 11025, 8000
|
|
|
|
};
|
|
|
|
|
|
|
|
static int faac_init_mp4(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
FAACContext *s = (FAACContext *) avctx->priv_data;
|
|
|
|
unsigned long samplerate;
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifndef FAAD2_VERSION
|
|
|
|
unsigned long channels;
|
|
|
|
#else
|
2003-05-12 10:55:19 +00:00
|
|
|
unsigned char channels;
|
2004-04-09 14:53:41 +00:00
|
|
|
#endif
|
2003-05-12 10:55:19 +00:00
|
|
|
int r = 0;
|
|
|
|
|
2006-02-08 00:30:58 +00:00
|
|
|
if (avctx->extradata){
|
2005-12-22 01:10:11 +00:00
|
|
|
r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
|
|
|
|
avctx->extradata_size,
|
|
|
|
&samplerate, &channels);
|
2006-02-08 00:30:58 +00:00
|
|
|
if (r < 0){
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
|
|
|
|
r, samplerate, (long)channels, avctx->extradata_size);
|
|
|
|
} else {
|
|
|
|
avctx->sample_rate = samplerate;
|
|
|
|
avctx->channels = channels;
|
|
|
|
s->init = 1;
|
|
|
|
}
|
|
|
|
}
|
2004-06-05 22:36:43 +00:00
|
|
|
|
2003-05-12 10:55:19 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int faac_decode_frame(AVCodecContext *avctx,
|
|
|
|
void *data, int *data_size,
|
|
|
|
uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
FAACContext *s = (FAACContext *) avctx->priv_data;
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifndef FAAD2_VERSION
|
|
|
|
unsigned long bytesconsumed;
|
|
|
|
short *sample_buffer = NULL;
|
|
|
|
unsigned long samples;
|
|
|
|
int out;
|
|
|
|
#else
|
2003-05-12 10:55:19 +00:00
|
|
|
faacDecFrameInfo frame_info;
|
2004-04-09 14:53:41 +00:00
|
|
|
void *out;
|
|
|
|
#endif
|
|
|
|
if(buf_size == 0)
|
2005-12-22 01:10:11 +00:00
|
|
|
return 0;
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifndef FAAD2_VERSION
|
2005-12-17 18:14:38 +00:00
|
|
|
out = s->faacDecDecode(s->faac_handle,
|
|
|
|
(unsigned char*)buf,
|
|
|
|
&bytesconsumed,
|
|
|
|
data,
|
2004-04-09 14:53:41 +00:00
|
|
|
&samples);
|
|
|
|
samples *= s->sample_size;
|
|
|
|
if (data_size)
|
2005-12-22 01:10:11 +00:00
|
|
|
*data_size = samples;
|
2004-04-09 14:53:41 +00:00
|
|
|
return (buf_size < (int)bytesconsumed)
|
2005-12-22 01:10:11 +00:00
|
|
|
? buf_size : (int)bytesconsumed;
|
2004-04-09 14:53:41 +00:00
|
|
|
#else
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2006-02-08 00:30:58 +00:00
|
|
|
if(!s->init){
|
|
|
|
unsigned long srate;
|
|
|
|
unsigned char channels;
|
2006-03-01 23:19:04 +00:00
|
|
|
int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
|
2006-02-08 00:30:58 +00:00
|
|
|
if(r < 0){
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
|
|
|
|
s->faacDecGetErrorMessage(frame_info.error));
|
2006-03-05 12:32:24 +00:00
|
|
|
return -1;
|
2006-02-08 00:30:58 +00:00
|
|
|
}
|
|
|
|
avctx->sample_rate = srate;
|
|
|
|
avctx->channels = channels;
|
|
|
|
s->init = 1;
|
|
|
|
}
|
|
|
|
|
2004-04-09 14:53:41 +00:00
|
|
|
out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
|
2003-05-12 10:55:19 +00:00
|
|
|
|
|
|
|
if (frame_info.error > 0) {
|
2005-12-22 01:10:11 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
|
|
|
|
s->faacDecGetErrorMessage(frame_info.error));
|
2006-03-05 12:32:24 +00:00
|
|
|
return -1;
|
2003-05-12 10:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frame_info.samples *= s->sample_size;
|
|
|
|
memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
|
|
|
|
|
|
|
|
if (data_size)
|
2005-12-22 01:10:11 +00:00
|
|
|
*data_size = frame_info.samples;
|
2003-05-12 10:55:19 +00:00
|
|
|
|
|
|
|
return (buf_size < (int)frame_info.bytesconsumed)
|
2005-12-22 01:10:11 +00:00
|
|
|
? buf_size : (int)frame_info.bytesconsumed;
|
2004-04-09 14:53:41 +00:00
|
|
|
#endif
|
2003-05-12 10:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int faac_decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
FAACContext *s = (FAACContext *) avctx->priv_data;
|
|
|
|
|
|
|
|
if (s->faacDecClose)
|
|
|
|
s->faacDecClose(s->faac_handle);
|
|
|
|
|
|
|
|
dlclose(s->handle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int faac_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
FAACContext *s = (FAACContext *) avctx->priv_data;
|
|
|
|
faacDecConfigurationPtr faac_cfg;
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAADBIN
|
|
|
|
const char* err = 0;
|
|
|
|
|
|
|
|
s->handle = dlopen(libfaadname, RTLD_LAZY);
|
|
|
|
if (!s->handle)
|
|
|
|
{
|
2005-12-22 01:10:11 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
|
|
|
|
libfaadname, dlerror());
|
2003-05-12 10:55:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#define dfaac(a, b) \
|
|
|
|
do { static const char* n = "faacDec" #a; \
|
|
|
|
if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
|
|
|
|
for(;;) {
|
|
|
|
#else /* !CONFIG_FAADBIN */
|
|
|
|
#define dfaac(a, b) s->faacDec ## a = faacDec ## a
|
|
|
|
#endif /* CONFIG_FAADBIN */
|
|
|
|
|
|
|
|
// resolve all needed function calls
|
2005-12-22 01:10:11 +00:00
|
|
|
dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
|
|
|
|
dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
|
|
|
|
FAADAPI (*)(faacDecHandle)));
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifndef FAAD2_VERSION
|
2005-12-22 01:10:11 +00:00
|
|
|
dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
|
|
|
|
faacDecConfigurationPtr)));
|
2003-05-12 10:55:19 +00:00
|
|
|
|
2005-12-22 01:10:11 +00:00
|
|
|
dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
|
|
|
|
unsigned long*, unsigned long*)));
|
2004-04-09 14:53:41 +00:00
|
|
|
dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
|
2005-12-22 01:10:11 +00:00
|
|
|
unsigned long, unsigned long*,
|
|
|
|
unsigned long*)));
|
2004-04-09 14:53:41 +00:00
|
|
|
dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
|
2005-12-22 01:10:11 +00:00
|
|
|
dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
|
|
|
|
unsigned long*, short*, unsigned long*)));
|
2004-04-09 14:53:41 +00:00
|
|
|
#else
|
2005-12-22 01:10:11 +00:00
|
|
|
dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
|
|
|
|
faacDecConfigurationPtr)));
|
|
|
|
dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
|
|
|
|
unsigned long, unsigned long*, unsigned char*)));
|
|
|
|
dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
|
|
|
|
unsigned long, unsigned long*,
|
|
|
|
unsigned char*)));
|
|
|
|
dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
|
|
|
|
unsigned char*, unsigned long)));
|
|
|
|
dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
|
2004-04-09 14:53:41 +00:00
|
|
|
#endif
|
2003-05-12 10:55:19 +00:00
|
|
|
#undef dfacc
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAADBIN
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
dlclose(s->handle);
|
2005-12-22 01:10:11 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
|
|
|
|
err, libfaadname);
|
2003-05-12 10:55:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s->faac_handle = s->faacDecOpen();
|
|
|
|
if (!s->faac_handle) {
|
2003-11-03 18:06:54 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
|
2003-05-12 10:55:19 +00:00
|
|
|
faac_decode_end(avctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
|
|
|
|
|
|
|
|
if (faac_cfg) {
|
2005-12-22 01:10:11 +00:00
|
|
|
switch (avctx->bits_per_sample) {
|
|
|
|
case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
|
|
|
|
default:
|
|
|
|
case 16:
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifdef FAAD2_VERSION
|
2005-12-22 01:10:11 +00:00
|
|
|
faac_cfg->outputFormat = FAAD_FMT_16BIT;
|
2004-04-09 14:53:41 +00:00
|
|
|
#endif
|
2005-12-22 01:10:11 +00:00
|
|
|
s->sample_size = 2;
|
|
|
|
break;
|
|
|
|
case 24:
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifdef FAAD2_VERSION
|
2005-12-22 01:10:11 +00:00
|
|
|
faac_cfg->outputFormat = FAAD_FMT_24BIT;
|
2004-04-09 14:53:41 +00:00
|
|
|
#endif
|
2005-12-22 01:10:11 +00:00
|
|
|
s->sample_size = 3;
|
|
|
|
break;
|
|
|
|
case 32:
|
2004-04-09 14:53:41 +00:00
|
|
|
#ifdef FAAD2_VERSION
|
2005-12-22 01:10:11 +00:00
|
|
|
faac_cfg->outputFormat = FAAD_FMT_32BIT;
|
2004-04-09 14:53:41 +00:00
|
|
|
#endif
|
2005-12-22 01:10:11 +00:00
|
|
|
s->sample_size = 4;
|
|
|
|
break;
|
|
|
|
}
|
2003-05-12 10:55:19 +00:00
|
|
|
|
2005-12-22 01:10:11 +00:00
|
|
|
faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
|
|
|
|
faac_cfg->defObjectType = LC;
|
2003-05-12 10:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
|
|
|
|
|
|
|
|
faac_init_mp4(avctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define AAC_CODEC(id, name) \
|
|
|
|
AVCodec name ## _decoder = { \
|
|
|
|
#name, \
|
|
|
|
CODEC_TYPE_AUDIO, \
|
|
|
|
id, \
|
|
|
|
sizeof(FAACContext), \
|
|
|
|
faac_decode_init, \
|
|
|
|
NULL, \
|
|
|
|
faac_decode_end, \
|
|
|
|
faac_decode_frame, \
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME - raw AAC files - maybe just one entry will be enough
|
|
|
|
AAC_CODEC(CODEC_ID_AAC, aac);
|
|
|
|
// If it's mp4 file - usually embeded into Qt Mov
|
|
|
|
AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
|
|
|
|
|
|
|
|
#undef AAC_CODEC
|