00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00030 #include "avcodec.h"
00031 #include "faad.h"
00032
00033 #ifndef FAADAPI
00034 #define FAADAPI
00035 #endif
00036
00037
00038
00039
00040
00041
00042
00043 #ifdef CONFIG_LIBFAADBIN
00044 #include <dlfcn.h>
00045 static const char* libfaadname = "libfaad.so";
00046 #else
00047 #define dlopen(a)
00048 #define dlclose(a)
00049 #endif
00050
00051 typedef struct {
00052 void* handle;
00053 void* faac_handle;
00054 int sample_size;
00055 int init;
00056
00057
00058 faacDecHandle FAADAPI (*faacDecOpen)(void);
00059 faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
00060 #ifndef FAAD2_VERSION
00061 int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
00062 faacDecConfigurationPtr config);
00063 int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
00064 unsigned char *buffer,
00065 unsigned long *samplerate,
00066 unsigned long *channels);
00067 int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
00068 unsigned long SizeOfDecoderSpecificInfo,
00069 unsigned long *samplerate, unsigned long *channels);
00070 int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
00071 unsigned char *buffer,
00072 unsigned long *bytesconsumed,
00073 short *sample_buffer,
00074 unsigned long *samples);
00075 #else
00076 unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
00077 faacDecConfigurationPtr config);
00078 long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
00079 unsigned char *buffer,
00080 unsigned long buffer_size,
00081 unsigned long *samplerate,
00082 unsigned char *channels);
00083 char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
00084 unsigned long SizeOfDecoderSpecificInfo,
00085 unsigned long *samplerate, unsigned char *channels);
00086 void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
00087 faacDecFrameInfo *hInfo,
00088 unsigned char *buffer,
00089 unsigned long buffer_size);
00090 char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
00091 #endif
00092
00093 void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
00094
00095
00096 } FAACContext;
00097
00098 static const unsigned long faac_srates[] =
00099 {
00100 96000, 88200, 64000, 48000, 44100, 32000,
00101 24000, 22050, 16000, 12000, 11025, 8000
00102 };
00103
00104 static void channel_setup(AVCodecContext *avctx)
00105 {
00106 #ifdef FAAD2_VERSION
00107 FAACContext *s = avctx->priv_data;
00108 if (avctx->request_channels > 0 && avctx->request_channels == 2 &&
00109 avctx->request_channels < avctx->channels) {
00110 faacDecConfigurationPtr faac_cfg;
00111 avctx->channels = 2;
00112 faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
00113 faac_cfg->downMatrix = 1;
00114 s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
00115 }
00116 #endif
00117 }
00118
00119 static int faac_init_mp4(AVCodecContext *avctx)
00120 {
00121 FAACContext *s = avctx->priv_data;
00122 unsigned long samplerate;
00123 #ifndef FAAD2_VERSION
00124 unsigned long channels;
00125 #else
00126 unsigned char channels;
00127 #endif
00128 int r = 0;
00129
00130 if (avctx->extradata){
00131 r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
00132 avctx->extradata_size,
00133 &samplerate, &channels);
00134 if (r < 0){
00135 av_log(avctx, AV_LOG_ERROR,
00136 "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
00137 r, samplerate, (long)channels, avctx->extradata_size);
00138 } else {
00139 avctx->sample_rate = samplerate;
00140 avctx->channels = channels;
00141 channel_setup(avctx);
00142 s->init = 1;
00143 }
00144 }
00145
00146 return r;
00147 }
00148
00149 static int faac_decode_frame(AVCodecContext *avctx,
00150 void *data, int *data_size,
00151 uint8_t *buf, int buf_size)
00152 {
00153 FAACContext *s = avctx->priv_data;
00154 #ifndef FAAD2_VERSION
00155 unsigned long bytesconsumed;
00156 short *sample_buffer = NULL;
00157 unsigned long samples;
00158 int out;
00159 #else
00160 faacDecFrameInfo frame_info;
00161 void *out;
00162 #endif
00163 if(buf_size == 0)
00164 return 0;
00165 #ifndef FAAD2_VERSION
00166 out = s->faacDecDecode(s->faac_handle,
00167 (unsigned char*)buf,
00168 &bytesconsumed,
00169 data,
00170 &samples);
00171 samples *= s->sample_size;
00172 if (data_size)
00173 *data_size = samples;
00174 return (buf_size < (int)bytesconsumed)
00175 ? buf_size : (int)bytesconsumed;
00176 #else
00177
00178 if(!s->init){
00179 unsigned long srate;
00180 unsigned char channels;
00181 int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
00182 if(r < 0){
00183 av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
00184 s->faacDecGetErrorMessage(frame_info.error));
00185 return -1;
00186 }
00187 avctx->sample_rate = srate;
00188 avctx->channels = channels;
00189 channel_setup(avctx);
00190 s->init = 1;
00191 }
00192
00193 out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
00194
00195 if (frame_info.error > 0) {
00196 av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
00197 s->faacDecGetErrorMessage(frame_info.error));
00198 return -1;
00199 }
00200
00201 frame_info.samples *= s->sample_size;
00202 memcpy(data, out, frame_info.samples);
00203
00204 if (data_size)
00205 *data_size = frame_info.samples;
00206
00207 return (buf_size < (int)frame_info.bytesconsumed)
00208 ? buf_size : (int)frame_info.bytesconsumed;
00209 #endif
00210 }
00211
00212 static int faac_decode_end(AVCodecContext *avctx)
00213 {
00214 FAACContext *s = avctx->priv_data;
00215
00216 s->faacDecClose(s->faac_handle);
00217
00218 dlclose(s->handle);
00219 return 0;
00220 }
00221
00222 static int faac_decode_init(AVCodecContext *avctx)
00223 {
00224 FAACContext *s = avctx->priv_data;
00225 faacDecConfigurationPtr faac_cfg;
00226
00227 #ifdef CONFIG_LIBFAADBIN
00228 const char* err = 0;
00229
00230 s->handle = dlopen(libfaadname, RTLD_LAZY);
00231 if (!s->handle)
00232 {
00233 av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
00234 libfaadname, dlerror());
00235 return -1;
00236 }
00237
00238 #define dfaac(a) do { \
00239 const char* n = AV_STRINGIFY(faacDec ## a); \
00240 if (!err && !(s->faacDec ## a = dlsym(s->handle, n))) { \
00241 err = n; \
00242 } \
00243 } while(0)
00244 #else
00245 #define dfaac(a) s->faacDec ## a = faacDec ## a
00246 #endif
00247
00248
00249 dfaac(Open);
00250 dfaac(Close);
00251 dfaac(GetCurrentConfiguration);
00252 dfaac(SetConfiguration);
00253 dfaac(Init);
00254 dfaac(Init2);
00255 dfaac(Decode);
00256 #ifdef FAAD2_VERSION
00257 dfaac(GetErrorMessage);
00258 #endif
00259
00260 #undef dfaac
00261
00262 #ifdef CONFIG_LIBFAADBIN
00263 if (err) {
00264 dlclose(s->handle);
00265 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
00266 err, libfaadname);
00267 return -1;
00268 }
00269 #endif
00270
00271 s->faac_handle = s->faacDecOpen();
00272 if (!s->faac_handle) {
00273 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
00274 faac_decode_end(avctx);
00275 return -1;
00276 }
00277
00278
00279 faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
00280
00281 if (faac_cfg) {
00282 switch (avctx->bits_per_sample) {
00283 case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
00284 default:
00285 case 16:
00286 #ifdef FAAD2_VERSION
00287 faac_cfg->outputFormat = FAAD_FMT_16BIT;
00288 #endif
00289 s->sample_size = 2;
00290 break;
00291 case 24:
00292 #ifdef FAAD2_VERSION
00293 faac_cfg->outputFormat = FAAD_FMT_24BIT;
00294 #endif
00295 s->sample_size = 3;
00296 break;
00297 case 32:
00298 #ifdef FAAD2_VERSION
00299 faac_cfg->outputFormat = FAAD_FMT_32BIT;
00300 #endif
00301 s->sample_size = 4;
00302 break;
00303 }
00304
00305 faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
00306 faac_cfg->defObjectType = LC;
00307 }
00308
00309 s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
00310
00311 faac_init_mp4(avctx);
00312
00313 if(!s->init && avctx->channels > 0)
00314 channel_setup(avctx);
00315
00316 return 0;
00317 }
00318
00319 #define AAC_CODEC(id, name) \
00320 AVCodec name ## _decoder = { \
00321 #name, \
00322 CODEC_TYPE_AUDIO, \
00323 id, \
00324 sizeof(FAACContext), \
00325 faac_decode_init, \
00326 NULL, \
00327 faac_decode_end, \
00328 faac_decode_frame, \
00329 }
00330
00331
00332 AAC_CODEC(CODEC_ID_AAC, libfaad);
00333 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
00334
00335 AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
00336 #endif
00337
00338 #undef AAC_CODEC