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.0";
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 int faac_init_mp4(AVCodecContext *avctx)
00105 {
00106 FAACContext *s = avctx->priv_data;
00107 unsigned long samplerate;
00108 #ifndef FAAD2_VERSION
00109 unsigned long channels;
00110 #else
00111 unsigned char channels;
00112 #endif
00113 int r = 0;
00114
00115 if (avctx->extradata){
00116 r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
00117 avctx->extradata_size,
00118 &samplerate, &channels);
00119 if (r < 0){
00120 av_log(avctx, AV_LOG_ERROR,
00121 "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
00122 r, samplerate, (long)channels, avctx->extradata_size);
00123 } else {
00124 avctx->sample_rate = samplerate;
00125 avctx->channels = channels;
00126 s->init = 1;
00127 }
00128 }
00129
00130 return r;
00131 }
00132
00133 static int faac_decode_frame(AVCodecContext *avctx,
00134 void *data, int *data_size,
00135 uint8_t *buf, int buf_size)
00136 {
00137 FAACContext *s = avctx->priv_data;
00138 #ifndef FAAD2_VERSION
00139 unsigned long bytesconsumed;
00140 short *sample_buffer = NULL;
00141 unsigned long samples;
00142 int out;
00143 #else
00144 faacDecFrameInfo frame_info;
00145 void *out;
00146 #endif
00147 if(buf_size == 0)
00148 return 0;
00149 #ifndef FAAD2_VERSION
00150 out = s->faacDecDecode(s->faac_handle,
00151 (unsigned char*)buf,
00152 &bytesconsumed,
00153 data,
00154 &samples);
00155 samples *= s->sample_size;
00156 if (data_size)
00157 *data_size = samples;
00158 return (buf_size < (int)bytesconsumed)
00159 ? buf_size : (int)bytesconsumed;
00160 #else
00161
00162 if(!s->init){
00163 unsigned long srate;
00164 unsigned char channels;
00165 int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
00166 if(r < 0){
00167 av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
00168 s->faacDecGetErrorMessage(frame_info.error));
00169 return -1;
00170 }
00171 avctx->sample_rate = srate;
00172 avctx->channels = channels;
00173 s->init = 1;
00174 }
00175
00176 out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
00177
00178 if (frame_info.error > 0) {
00179 av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
00180 s->faacDecGetErrorMessage(frame_info.error));
00181 return -1;
00182 }
00183
00184 frame_info.samples *= s->sample_size;
00185 memcpy(data, out, frame_info.samples);
00186
00187 if (data_size)
00188 *data_size = frame_info.samples;
00189
00190 return (buf_size < (int)frame_info.bytesconsumed)
00191 ? buf_size : (int)frame_info.bytesconsumed;
00192 #endif
00193 }
00194
00195 static int faac_decode_end(AVCodecContext *avctx)
00196 {
00197 FAACContext *s = avctx->priv_data;
00198
00199 s->faacDecClose(s->faac_handle);
00200
00201 dlclose(s->handle);
00202 return 0;
00203 }
00204
00205 static int faac_decode_init(AVCodecContext *avctx)
00206 {
00207 FAACContext *s = avctx->priv_data;
00208 faacDecConfigurationPtr faac_cfg;
00209
00210 #ifdef CONFIG_LIBFAADBIN
00211 const char* err = 0;
00212
00213 s->handle = dlopen(libfaadname, RTLD_LAZY);
00214 if (!s->handle)
00215 {
00216 av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
00217 libfaadname, dlerror());
00218 return -1;
00219 }
00220 #define dfaac(a, b) \
00221 do { static const char* n = "faacDec" #a; \
00222 if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
00223 for(;;) {
00224 #else
00225 #define dfaac(a, b) s->faacDec ## a = faacDec ## a
00226 #endif
00227
00228
00229 dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
00230 dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
00231 dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
00232 FAADAPI (*)(faacDecHandle)));
00233 #ifndef FAAD2_VERSION
00234 dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
00235 faacDecConfigurationPtr)));
00236
00237 dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
00238 unsigned long*, unsigned long*)));
00239 dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
00240 unsigned long, unsigned long*,
00241 unsigned long*)));
00242 dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
00243 unsigned long*, short*, unsigned long*)));
00244 #else
00245 dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
00246 faacDecConfigurationPtr)));
00247 dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
00248 unsigned long, unsigned long*, unsigned char*)));
00249 dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
00250 unsigned long, unsigned long*,
00251 unsigned char*)));
00252 dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
00253 unsigned char*, unsigned long)));
00254 dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
00255 #endif
00256 #undef dfacc
00257
00258 #ifdef CONFIG_LIBFAADBIN
00259 break;
00260 }
00261 if (err) {
00262 dlclose(s->handle);
00263 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
00264 err, libfaadname);
00265 return -1;
00266 }
00267 #endif
00268
00269 s->faac_handle = s->faacDecOpen();
00270 if (!s->faac_handle) {
00271 av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
00272 faac_decode_end(avctx);
00273 return -1;
00274 }
00275
00276
00277 faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
00278
00279 if (faac_cfg) {
00280 switch (avctx->bits_per_sample) {
00281 case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
00282 default:
00283 case 16:
00284 #ifdef FAAD2_VERSION
00285 faac_cfg->outputFormat = FAAD_FMT_16BIT;
00286 #endif
00287 s->sample_size = 2;
00288 break;
00289 case 24:
00290 #ifdef FAAD2_VERSION
00291 faac_cfg->outputFormat = FAAD_FMT_24BIT;
00292 #endif
00293 s->sample_size = 3;
00294 break;
00295 case 32:
00296 #ifdef FAAD2_VERSION
00297 faac_cfg->outputFormat = FAAD_FMT_32BIT;
00298 #endif
00299 s->sample_size = 4;
00300 break;
00301 }
00302
00303 faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
00304 faac_cfg->defObjectType = LC;
00305 }
00306
00307 s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
00308
00309 faac_init_mp4(avctx);
00310
00311 return 0;
00312 }
00313
00314 #define AAC_CODEC(id, name) \
00315 AVCodec name ## _decoder = { \
00316 #name, \
00317 CODEC_TYPE_AUDIO, \
00318 id, \
00319 sizeof(FAACContext), \
00320 faac_decode_init, \
00321 NULL, \
00322 faac_decode_end, \
00323 faac_decode_frame, \
00324 }
00325
00326
00327 AAC_CODEC(CODEC_ID_AAC, libfaad);
00328 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
00329
00330 AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
00331 #endif
00332
00333 #undef AAC_CODEC