Add 8088flex TMV video decoder.
[ffmpeg.git] / libavcodec / libfaad.c
index 6de8084..953c2a1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Faad decoder
- * Copyright (c) 2003 Zdenek Kabelac.
- * Copyright (c) 2004 Thomas Raivio.
+ * Copyright (c) 2003 Zdenek Kabelac
+ * Copyright (c) 2004 Thomas Raivio
  *
  * This file is part of FFmpeg.
  *
@@ -21,7 +21,7 @@
  */
 
 /**
- * @file faad.c
+ * @file libavcodec/libfaad.c
  * AAC decoder.
  *
  * still a bit unfinished - but it plays something
 #endif
 
 /*
- * when CONFIG_LIBFAADBIN is defined the libfaad will be opened at runtime
+ * when CONFIG_LIBFAADBIN is true libfaad will be opened at runtime
  */
 //#undef CONFIG_LIBFAADBIN
-//#define CONFIG_LIBFAADBIN
+//#define CONFIG_LIBFAADBIN 0
+//#define CONFIG_LIBFAADBIN 1
 
-#ifdef CONFIG_LIBFAADBIN
+#if CONFIG_LIBFAADBIN
 #include <dlfcn.h>
-static const char* libfaadname = "libfaad.so.0";
+static const char* const libfaadname = "libfaad.so";
 #else
 #define dlopen(a)
 #define dlclose(a)
@@ -58,36 +59,36 @@ typedef struct {
     faacDecHandle FAADAPI (*faacDecOpen)(void);
     faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
 #ifndef FAAD2_VERSION
-        int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
+    int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
                                            faacDecConfigurationPtr config);
-        int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
-                                unsigned char *buffer,
-                                unsigned long *samplerate,
-                                unsigned long *channels);
-        int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
+    int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
+                               unsigned char *buffer,
+                               unsigned long *samplerate,
+                               unsigned long *channels);
+    int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
                                 unsigned long SizeOfDecoderSpecificInfo,
                                 unsigned long *samplerate, unsigned long *channels);
-        int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
-                                unsigned char *buffer,
-                                unsigned long *bytesconsumed,
-                                short *sample_buffer,
-                                unsigned long *samples);
+    int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
+                                 unsigned char *buffer,
+                                 unsigned long *bytesconsumed,
+                                 short *sample_buffer,
+                                 unsigned long *samples);
 #else
-        unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
+    unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
                                                      faacDecConfigurationPtr config);
-        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,
+    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,
                                  unsigned long SizeOfDecoderSpecificInfo,
                                  unsigned long *samplerate, unsigned char *channels);
-        void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
-                                         faacDecFrameInfo *hInfo,
-                                         unsigned char *buffer,
-                                                                 unsigned long buffer_size);
-        char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
+    void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
+                                   faacDecFrameInfo *hInfo,
+                                   unsigned char *buffer,
+                                   unsigned long buffer_size);
+    char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
 #endif
 
     void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
@@ -106,7 +107,7 @@ static void channel_setup(AVCodecContext *avctx)
 #ifdef FAAD2_VERSION
     FAACContext *s = avctx->priv_data;
     if (avctx->request_channels > 0 && avctx->request_channels == 2 &&
-            avctx->request_channels < avctx->channels) {
+        avctx->request_channels < avctx->channels) {
         faacDecConfigurationPtr faac_cfg;
         avctx->channels = 2;
         faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
@@ -116,7 +117,7 @@ static void channel_setup(AVCodecContext *avctx)
 #endif
 }
 
-static int faac_init_mp4(AVCodecContext *avctx)
+static av_cold int faac_init_mp4(AVCodecContext *avctx)
 {
     FAACContext *s = avctx->priv_data;
     unsigned long samplerate;
@@ -148,8 +149,10 @@ static int faac_init_mp4(AVCodecContext *avctx)
 
 static int faac_decode_frame(AVCodecContext *avctx,
                              void *data, int *data_size,
-                             uint8_t *buf, int buf_size)
+                             AVPacket *avpkt)
 {
+    const uint8_t *buf = avpkt->data;
+    int buf_size = avpkt->size;
     FAACContext *s = avctx->priv_data;
 #ifndef FAAD2_VERSION
     unsigned long bytesconsumed;
@@ -180,8 +183,7 @@ static int faac_decode_frame(AVCodecContext *avctx,
         unsigned char channels;
         int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
         if(r < 0){
-            av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
-                   s->faacDecGetErrorMessage(frame_info.error));
+            av_log(avctx, AV_LOG_ERROR, "faac: codec init failed.\n");
             return -1;
         }
         avctx->sample_rate = srate;
@@ -194,10 +196,11 @@ static int faac_decode_frame(AVCodecContext *avctx,
 
     if (frame_info.error > 0) {
         av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
-                s->faacDecGetErrorMessage(frame_info.error));
+               s->faacDecGetErrorMessage(frame_info.error));
         return -1;
     }
-
+    if (!avctx->frame_size)
+        avctx->frame_size = frame_info.samples/avctx->channels;
     frame_info.samples *= s->sample_size;
     memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
 
@@ -209,7 +212,7 @@ static int faac_decode_frame(AVCodecContext *avctx,
 #endif
 }
 
-static int faac_decode_end(AVCodecContext *avctx)
+static av_cold int faac_decode_end(AVCodecContext *avctx)
 {
     FAACContext *s = avctx->priv_data;
 
@@ -219,66 +222,51 @@ static int faac_decode_end(AVCodecContext *avctx)
     return 0;
 }
 
-static int faac_decode_init(AVCodecContext *avctx)
+static av_cold int faac_decode_init(AVCodecContext *avctx)
 {
     FAACContext *s = avctx->priv_data;
     faacDecConfigurationPtr faac_cfg;
 
-#ifdef CONFIG_LIBFAADBIN
+#if CONFIG_LIBFAADBIN
     const char* err = 0;
 
     s->handle = dlopen(libfaadname, RTLD_LAZY);
     if (!s->handle)
     {
         av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
-                libfaadname, dlerror());
+               libfaadname, dlerror());
         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(;;) {
+
+#define dfaac(a) do {                                                   \
+        const char* n = AV_STRINGIFY(faacDec ## a);                     \
+        if (!err && !(s->faacDec ## a = dlsym(s->handle, n))) {         \
+            err = n;                                                    \
+        }                                                               \
+    } while(0)
 #else  /* !CONFIG_LIBFAADBIN */
-#define dfaac(a, b)     s->faacDec ## a = faacDec ## a
+#define dfaac(a)     s->faacDec ## a = faacDec ## a
 #endif /* CONFIG_LIBFAADBIN */
 
-        // resolve all needed function calls
-        dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
-        dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
-        dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
-                                        FAADAPI (*)(faacDecHandle)));
-#ifndef FAAD2_VERSION
-        dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
-                                                           faacDecConfigurationPtr)));
-
-        dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
-                                     unsigned long*, unsigned long*)));
-    dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
-                                       unsigned long, unsigned long*,
-                                       unsigned long*)));
-        dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
-                             unsigned long*, short*, unsigned long*)));
-#else
-        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)));
+    // resolve all needed function calls
+    dfaac(Open);
+    dfaac(Close);
+    dfaac(GetCurrentConfiguration);
+    dfaac(SetConfiguration);
+    dfaac(Init);
+    dfaac(Init2);
+    dfaac(Decode);
+#ifdef FAAD2_VERSION
+    dfaac(GetErrorMessage);
 #endif
-#undef dfacc
 
-#ifdef CONFIG_LIBFAADBIN
-        break;
-    }
+#undef dfaac
+
+#if CONFIG_LIBFAADBIN
     if (err) {
         dlclose(s->handle);
         av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
-                err, libfaadname);
+               err, libfaadname);
         return -1;
     }
 #endif
@@ -294,8 +282,8 @@ static int faac_decode_init(AVCodecContext *avctx)
     faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
 
     if (faac_cfg) {
-        switch (avctx->bits_per_sample) {
-        case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
+        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;
         default:
         case 16:
 #ifdef FAAD2_VERSION
@@ -328,26 +316,18 @@ static int faac_decode_init(AVCodecContext *avctx)
     if(!s->init && avctx->channels > 0)
         channel_setup(avctx);
 
+    avctx->sample_fmt = SAMPLE_FMT_S16;
     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, libfaad);
-#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
-// If it's mp4 file - usually embeded into Qt Mov
-AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
-#endif
-
-#undef AAC_CODEC
+AVCodec libfaad_decoder = {
+    "libfaad",
+    CODEC_TYPE_AUDIO,
+    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)"),
+};