2003-05-12 13:55:19 +03:00
|
|
|
/*
|
|
|
|
* Faad decoder
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2003 Zdenek Kabelac
|
|
|
|
* Copyright (c) 2004 Thomas Raivio
|
2003-05-12 13:55:19 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-05-12 13:55:19 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-05-12 13:55:19 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-05-12 13:55:19 +03:00
|
|
|
* 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
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-05-12 13:55:19 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-02-01 04:00:19 +02:00
|
|
|
* @file libavcodec/libfaad.c
|
2003-05-12 13:55:19 +03:00
|
|
|
* AAC decoder.
|
|
|
|
*
|
|
|
|
* still a bit unfinished - but it plays something
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "faad.h"
|
|
|
|
|
2004-06-06 06:52:42 +03:00
|
|
|
#ifndef FAADAPI
|
|
|
|
#define FAADAPI
|
|
|
|
#endif
|
|
|
|
|
2003-05-12 13:55:19 +03:00
|
|
|
/*
|
2009-01-14 01:44:16 +02:00
|
|
|
* when CONFIG_LIBFAADBIN is true libfaad will be opened at runtime
|
2003-05-12 13:55:19 +03:00
|
|
|
*/
|
2007-01-07 01:54:01 +02:00
|
|
|
//#undef CONFIG_LIBFAADBIN
|
2009-01-14 01:44:16 +02:00
|
|
|
//#define CONFIG_LIBFAADBIN 0
|
|
|
|
//#define CONFIG_LIBFAADBIN 1
|
2003-05-12 13:55:19 +03:00
|
|
|
|
2009-01-14 01:44:16 +02:00
|
|
|
#if CONFIG_LIBFAADBIN
|
2003-05-12 13:55:19 +03:00
|
|
|
#include <dlfcn.h>
|
2008-10-02 18:52:04 +03:00
|
|
|
static const char* const libfaadname = "libfaad.so";
|
2003-05-12 13:55:19 +03:00
|
|
|
#else
|
|
|
|
#define dlopen(a)
|
|
|
|
#define dlclose(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
2005-12-22 03:10:11 +02:00
|
|
|
void* handle; /* dlopen handle */
|
|
|
|
void* faac_handle; /* FAAD library handle */
|
2003-05-12 13:55:19 +03:00
|
|
|
int sample_size;
|
2006-02-08 02:30:58 +02:00
|
|
|
int init;
|
2003-05-12 13:55:19 +03:00
|
|
|
|
|
|
|
/* faad calls */
|
|
|
|
faacDecHandle FAADAPI (*faacDecOpen)(void);
|
|
|
|
faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
|
2004-04-09 17:53:41 +03:00
|
|
|
#ifndef FAAD2_VERSION
|
2008-01-24 21:51:31 +02:00
|
|
|
int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
|
2004-04-09 17:53:41 +03:00
|
|
|
faacDecConfigurationPtr config);
|
2008-01-24 21:51:31 +02: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 17:53:41 +03:00
|
|
|
unsigned long SizeOfDecoderSpecificInfo,
|
|
|
|
unsigned long *samplerate, unsigned long *channels);
|
2008-01-24 21:51:31 +02:00
|
|
|
int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
|
|
|
|
unsigned char *buffer,
|
|
|
|
unsigned long *bytesconsumed,
|
|
|
|
short *sample_buffer,
|
|
|
|
unsigned long *samples);
|
2004-04-09 17:53:41 +03:00
|
|
|
#else
|
2008-01-24 21:51:31 +02:00
|
|
|
unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
|
2004-04-09 17:53:41 +03:00
|
|
|
faacDecConfigurationPtr config);
|
2008-01-24 21:51:31 +02: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 13:55:19 +03:00
|
|
|
unsigned long SizeOfDecoderSpecificInfo,
|
|
|
|
unsigned long *samplerate, unsigned char *channels);
|
2008-01-24 21:51:31 +02:00
|
|
|
void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
|
|
|
|
faacDecFrameInfo *hInfo,
|
|
|
|
unsigned char *buffer,
|
|
|
|
unsigned long buffer_size);
|
|
|
|
char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
|
2004-04-09 17:53:41 +03:00
|
|
|
#endif
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-05-12 13:55:19 +03:00
|
|
|
void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
|
|
|
|
2003-05-12 13:55:19 +03:00
|
|
|
} FAACContext;
|
|
|
|
|
|
|
|
static const unsigned long faac_srates[] =
|
|
|
|
{
|
|
|
|
96000, 88200, 64000, 48000, 44100, 32000,
|
|
|
|
24000, 22050, 16000, 12000, 11025, 8000
|
|
|
|
};
|
|
|
|
|
2008-01-05 23:08:20 +02:00
|
|
|
static void channel_setup(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
#ifdef FAAD2_VERSION
|
|
|
|
FAACContext *s = avctx->priv_data;
|
|
|
|
if (avctx->request_channels > 0 && avctx->request_channels == 2 &&
|
2008-01-24 21:51:31 +02:00
|
|
|
avctx->request_channels < avctx->channels) {
|
2008-01-05 23:08:20 +02:00
|
|
|
faacDecConfigurationPtr faac_cfg;
|
|
|
|
avctx->channels = 2;
|
|
|
|
faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
|
|
|
|
faac_cfg->downMatrix = 1;
|
|
|
|
s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:48:55 +02:00
|
|
|
static av_cold int faac_init_mp4(AVCodecContext *avctx)
|
2003-05-12 13:55:19 +03:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
FAACContext *s = avctx->priv_data;
|
2003-05-12 13:55:19 +03:00
|
|
|
unsigned long samplerate;
|
2004-04-09 17:53:41 +03:00
|
|
|
#ifndef FAAD2_VERSION
|
|
|
|
unsigned long channels;
|
|
|
|
#else
|
2003-05-12 13:55:19 +03:00
|
|
|
unsigned char channels;
|
2004-04-09 17:53:41 +03:00
|
|
|
#endif
|
2003-05-12 13:55:19 +03:00
|
|
|
int r = 0;
|
|
|
|
|
2006-02-08 02:30:58 +02:00
|
|
|
if (avctx->extradata){
|
2005-12-22 03:10:11 +02:00
|
|
|
r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
|
|
|
|
avctx->extradata_size,
|
|
|
|
&samplerate, &channels);
|
2006-02-08 02:30:58 +02: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;
|
2008-01-05 23:08:20 +02:00
|
|
|
channel_setup(avctx);
|
2006-02-08 02:30:58 +02:00
|
|
|
s->init = 1;
|
|
|
|
}
|
|
|
|
}
|
2004-06-06 01:36:43 +03:00
|
|
|
|
2003-05-12 13:55:19 +03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int faac_decode_frame(AVCodecContext *avctx,
|
|
|
|
void *data, int *data_size,
|
2009-04-07 18:59:50 +03:00
|
|
|
AVPacket *avpkt)
|
2003-05-12 13:55:19 +03:00
|
|
|
{
|
2009-04-07 18:59:50 +03:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2007-04-08 23:24:16 +03:00
|
|
|
FAACContext *s = avctx->priv_data;
|
2004-04-09 17:53:41 +03:00
|
|
|
#ifndef FAAD2_VERSION
|
|
|
|
unsigned long bytesconsumed;
|
|
|
|
short *sample_buffer = NULL;
|
|
|
|
unsigned long samples;
|
|
|
|
int out;
|
|
|
|
#else
|
2003-05-12 13:55:19 +03:00
|
|
|
faacDecFrameInfo frame_info;
|
2004-04-09 17:53:41 +03:00
|
|
|
void *out;
|
|
|
|
#endif
|
|
|
|
if(buf_size == 0)
|
2005-12-22 03:10:11 +02:00
|
|
|
return 0;
|
2004-04-09 17:53:41 +03:00
|
|
|
#ifndef FAAD2_VERSION
|
2005-12-17 20:14:38 +02:00
|
|
|
out = s->faacDecDecode(s->faac_handle,
|
|
|
|
(unsigned char*)buf,
|
|
|
|
&bytesconsumed,
|
|
|
|
data,
|
2004-04-09 17:53:41 +03:00
|
|
|
&samples);
|
|
|
|
samples *= s->sample_size;
|
|
|
|
if (data_size)
|
2005-12-22 03:10:11 +02:00
|
|
|
*data_size = samples;
|
2004-04-09 17:53:41 +03:00
|
|
|
return (buf_size < (int)bytesconsumed)
|
2005-12-22 03:10:11 +02:00
|
|
|
? buf_size : (int)bytesconsumed;
|
2004-04-09 17:53:41 +03:00
|
|
|
#else
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2006-02-08 02:30:58 +02:00
|
|
|
if(!s->init){
|
|
|
|
unsigned long srate;
|
|
|
|
unsigned char channels;
|
2006-03-02 01:19:04 +02:00
|
|
|
int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
|
2006-02-08 02:30:58 +02:00
|
|
|
if(r < 0){
|
2009-12-11 00:29:21 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "libfaad: codec init failed.\n");
|
2006-03-05 14:32:24 +02:00
|
|
|
return -1;
|
2006-02-08 02:30:58 +02:00
|
|
|
}
|
|
|
|
avctx->sample_rate = srate;
|
|
|
|
avctx->channels = channels;
|
2008-01-05 23:08:20 +02:00
|
|
|
channel_setup(avctx);
|
2006-02-08 02:30:58 +02:00
|
|
|
s->init = 1;
|
|
|
|
}
|
|
|
|
|
2004-04-09 17:53:41 +03:00
|
|
|
out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
|
2003-05-12 13:55:19 +03:00
|
|
|
|
|
|
|
if (frame_info.error > 0) {
|
2009-12-11 00:29:21 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "libfaad: frame decoding failed: %s\n",
|
2008-01-24 21:51:31 +02:00
|
|
|
s->faacDecGetErrorMessage(frame_info.error));
|
2006-03-05 14:32:24 +02:00
|
|
|
return -1;
|
2003-05-12 13:55:19 +03:00
|
|
|
}
|
2008-06-28 22:41:18 +03:00
|
|
|
if (!avctx->frame_size)
|
|
|
|
avctx->frame_size = frame_info.samples/avctx->channels;
|
2003-05-12 13:55:19 +03: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 03:10:11 +02:00
|
|
|
*data_size = frame_info.samples;
|
2003-05-12 13:55:19 +03:00
|
|
|
|
|
|
|
return (buf_size < (int)frame_info.bytesconsumed)
|
2005-12-22 03:10:11 +02:00
|
|
|
? buf_size : (int)frame_info.bytesconsumed;
|
2004-04-09 17:53:41 +03:00
|
|
|
#endif
|
2003-05-12 13:55:19 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int faac_decode_end(AVCodecContext *avctx)
|
2003-05-12 13:55:19 +03:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
FAACContext *s = avctx->priv_data;
|
2003-05-12 13:55:19 +03:00
|
|
|
|
2007-03-31 19:47:39 +03:00
|
|
|
s->faacDecClose(s->faac_handle);
|
2003-05-12 13:55:19 +03:00
|
|
|
|
|
|
|
dlclose(s->handle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int faac_decode_init(AVCodecContext *avctx)
|
2003-05-12 13:55:19 +03:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
FAACContext *s = avctx->priv_data;
|
2003-05-12 13:55:19 +03:00
|
|
|
faacDecConfigurationPtr faac_cfg;
|
|
|
|
|
2009-01-14 01:44:16 +02:00
|
|
|
#if CONFIG_LIBFAADBIN
|
2003-05-12 13:55:19 +03:00
|
|
|
const char* err = 0;
|
|
|
|
|
|
|
|
s->handle = dlopen(libfaadname, RTLD_LAZY);
|
|
|
|
if (!s->handle)
|
|
|
|
{
|
2005-12-22 03:10:11 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
|
2008-01-24 21:51:31 +02:00
|
|
|
libfaadname, dlerror());
|
2003-05-12 13:55:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2008-01-25 23:14:17 +02:00
|
|
|
|
|
|
|
#define dfaac(a) do { \
|
|
|
|
const char* n = AV_STRINGIFY(faacDec ## a); \
|
|
|
|
if (!err && !(s->faacDec ## a = dlsym(s->handle, n))) { \
|
|
|
|
err = n; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2007-01-07 01:54:01 +02:00
|
|
|
#else /* !CONFIG_LIBFAADBIN */
|
2008-01-25 23:14:17 +02:00
|
|
|
#define dfaac(a) s->faacDec ## a = faacDec ## a
|
2007-01-07 01:54:01 +02:00
|
|
|
#endif /* CONFIG_LIBFAADBIN */
|
2003-05-12 13:55:19 +03:00
|
|
|
|
2008-01-25 23:14:17 +02:00
|
|
|
// resolve all needed function calls
|
|
|
|
dfaac(Open);
|
|
|
|
dfaac(Close);
|
|
|
|
dfaac(GetCurrentConfiguration);
|
|
|
|
dfaac(SetConfiguration);
|
|
|
|
dfaac(Init);
|
|
|
|
dfaac(Init2);
|
|
|
|
dfaac(Decode);
|
|
|
|
#ifdef FAAD2_VERSION
|
|
|
|
dfaac(GetErrorMessage);
|
2004-04-09 17:53:41 +03:00
|
|
|
#endif
|
2008-01-25 23:14:17 +02:00
|
|
|
|
|
|
|
#undef dfaac
|
2003-05-12 13:55:19 +03:00
|
|
|
|
2009-01-14 01:44:16 +02:00
|
|
|
#if CONFIG_LIBFAADBIN
|
2003-05-12 13:55:19 +03:00
|
|
|
if (err) {
|
|
|
|
dlclose(s->handle);
|
2005-12-22 03:10:11 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
|
2008-01-24 21:51:31 +02:00
|
|
|
err, libfaadname);
|
2003-05-12 13:55:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s->faac_handle = s->faacDecOpen();
|
|
|
|
if (!s->faac_handle) {
|
2003-11-03 20:06:54 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
|
2003-05-12 13:55:19 +03:00
|
|
|
faac_decode_end(avctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
|
|
|
|
|
|
|
|
if (faac_cfg) {
|
2008-09-08 17:24:59 +03:00
|
|
|
switch (avctx->bits_per_coded_sample) {
|
|
|
|
case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_coded_sample); break;
|
2005-12-22 03:10:11 +02:00
|
|
|
default:
|
|
|
|
case 16:
|
2004-04-09 17:53:41 +03:00
|
|
|
#ifdef FAAD2_VERSION
|
2005-12-22 03:10:11 +02:00
|
|
|
faac_cfg->outputFormat = FAAD_FMT_16BIT;
|
2004-04-09 17:53:41 +03:00
|
|
|
#endif
|
2005-12-22 03:10:11 +02:00
|
|
|
s->sample_size = 2;
|
|
|
|
break;
|
|
|
|
case 24:
|
2004-04-09 17:53:41 +03:00
|
|
|
#ifdef FAAD2_VERSION
|
2005-12-22 03:10:11 +02:00
|
|
|
faac_cfg->outputFormat = FAAD_FMT_24BIT;
|
2004-04-09 17:53:41 +03:00
|
|
|
#endif
|
2005-12-22 03:10:11 +02:00
|
|
|
s->sample_size = 3;
|
|
|
|
break;
|
|
|
|
case 32:
|
2004-04-09 17:53:41 +03:00
|
|
|
#ifdef FAAD2_VERSION
|
2005-12-22 03:10:11 +02:00
|
|
|
faac_cfg->outputFormat = FAAD_FMT_32BIT;
|
2004-04-09 17:53:41 +03:00
|
|
|
#endif
|
2005-12-22 03:10:11 +02:00
|
|
|
s->sample_size = 4;
|
|
|
|
break;
|
|
|
|
}
|
2003-05-12 13:55:19 +03:00
|
|
|
|
2005-12-22 03:10:11 +02:00
|
|
|
faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
|
|
|
|
faac_cfg->defObjectType = LC;
|
2003-05-12 13:55:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
|
|
|
|
|
|
|
|
faac_init_mp4(avctx);
|
|
|
|
|
2008-01-05 23:08:20 +02:00
|
|
|
if(!s->init && avctx->channels > 0)
|
|
|
|
channel_setup(avctx);
|
|
|
|
|
2008-07-31 13:47:31 +03:00
|
|
|
avctx->sample_fmt = SAMPLE_FMT_S16;
|
2003-05-12 13:55:19 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-03 17:48:52 +02:00
|
|
|
AVCodec libfaad_decoder = {
|
2009-03-04 01:52:31 +02:00
|
|
|
"libfaad",
|
2010-03-31 02:30:55 +03:00
|
|
|
AVMEDIA_TYPE_AUDIO,
|
2009-03-03 17:48:52 +02:00
|
|
|
CODEC_ID_AAC,
|
|
|
|
sizeof(FAACContext),
|
|
|
|
faac_decode_init,
|
|
|
|
NULL,
|
|
|
|
faac_decode_end,
|
|
|
|
faac_decode_frame,
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libfaad AAC (Advanced Audio Codec)"),
|
2009-03-04 01:52:31 +02:00
|
|
|
};
|