]> git.sesse.net Git - vlc/blobdiff - plugins/ac3_adec/ac3_adec.c
All decoders (audio, video, subtitles) are now modules.
[vlc] / plugins / ac3_adec / ac3_adec.c
similarity index 77%
rename from src/ac3_decoder/ac3_decoder_thread.c
rename to plugins/ac3_adec/ac3_adec.c
index 667d83ed6cba5763ba9c3badc3f7e32193ce2788..1559b04e3a8e0a6946acf857dc81b63596b57483 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
- * ac3_decoder_thread.c: ac3 decoder thread
+ * ac3_adec.c: ac3 decoder module main file
  *****************************************************************************
  * Copyright (C) 1999, 2000 VideoLAN
- * $Id: ac3_decoder_thread.c,v 1.39 2001/10/31 11:55:53 reno Exp $
+ * $Id: ac3_adec.c,v 1.1 2001/11/13 12:09:17 henri Exp $
  *
  * Authors: Michel Lespinasse <walken@zoy.org>
  *
@@ -29,6 +29,8 @@
  * - vlc_cond_signal() / vlc_cond_wait()
  *
  */
+#define MODULE_NAME ac3_adec
+#include "modules_inner.h"
 
 /*****************************************************************************
  * Preamble
 #include "common.h"
 #include "threads.h"
 #include "mtime.h"
-#include "modules.h"
-
 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
 
+#include "audio_output.h"
+
+#include "modules.h"
+#include "modules_export.h"
+
 #include "stream_control.h"
 #include "input_ext-dec.h"
-
-#include "audio_output.h"
+#include "input_ext-intf.h"                                /* MPEG?_AUDIO_ES */
 
 #include "ac3_imdct.h"
 #include "ac3_downmix.h"
 #include "ac3_decoder.h"
-#include "ac3_decoder_thread.h"
+#include "ac3_adec.h"
 
 #define AC3DEC_FRAME_SIZE (2*1536) 
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int      InitThread              (ac3dec_thread_t * p_adec);
-static void     RunThread               (ac3dec_thread_t * p_adec);
-static void     ErrorThread             (ac3dec_thread_t * p_adec);
-static void     EndThread               (ac3dec_thread_t * p_adec);
-static void     BitstreamCallback       ( bit_stream_t *p_bit_stream,
+static int      ac3_adec_Probe      ( probedata_t * );
+static int      ac3_adec_Run         ( decoder_config_t * );
+static int      ac3_adec_Init        (ac3dec_thread_t * p_adec);
+static void     ac3_adec_ErrorThread (ac3dec_thread_t * p_adec);
+static void     ac3_adec_EndThread   (ac3dec_thread_t * p_adec);
+static void     BitstreamCallback    ( bit_stream_t *p_bit_stream,
                                               boolean_t b_new_pes );
 
 /*****************************************************************************
- * ac3dec_CreateThread: creates an ac3 decoder thread
+ * Capabilities
+ *****************************************************************************/
+void _M( adec_getfunctions )( function_list_t * p_function_list )
+{
+    p_function_list->pf_probe = ac3_adec_Probe;
+    p_function_list->functions.dec.pf_RunThread = ac3_adec_Run;
+}
+
+/*****************************************************************************
+ * Build configuration tree.
+ *****************************************************************************/
+MODULE_CONFIG_START
+ADD_WINDOW( "Configuration for ac3 decoder module" )
+    ADD_COMMENT( "Nothing to configure" )
+MODULE_CONFIG_STOP
+
+MODULE_INIT_START
+    p_module->i_capabilities = MODULE_CAPABILITY_DEC;
+    p_module->psz_longname = "Ac3 sofware decoder";
+MODULE_INIT_STOP
+
+MODULE_ACTIVATE_START
+    _M( adec_getfunctions )( &p_module->p_functions->dec );
+MODULE_ACTIVATE_STOP
+
+MODULE_DEACTIVATE_START
+MODULE_DEACTIVATE_STOP
+
+
+/*****************************************************************************
+ * ac3_adec_Probe: probe the decoder and return score
+ *****************************************************************************
+ * Tries to launch a decoder and return score so that the interface is able 
+ * to chose.
+ *****************************************************************************/
+static int ac3_adec_Probe( probedata_t *p_data )
+{
+    if( p_data->i_type == AC3_AUDIO_ES )
+        return( 50 );
+    else
+        return( 0 );
+}
+
+/*****************************************************************************
+ * ac3_adec_Run: this function is called just after the thread is created
  *****************************************************************************/
-vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
+static int ac3_adec_Run ( decoder_config_t * p_config )
 {
     ac3dec_thread_t *   p_ac3thread;
+    int sync;
 
-    intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
+    intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
 
     /* Allocate the memory needed to store the thread's structure */
     p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
@@ -87,15 +137,110 @@ vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
     if(p_ac3thread == NULL)
     {
         intf_ErrMsg ( "ac3dec error: not enough memory "
-                      "for ac3dec_CreateThread() to create the new thread");
-        return 0;
+                      "for ac3_adec_Run() to allocate p_ac3thread" );
+        return( -1 );
     }
    
     /*
      * Initialize the thread properties
      */
     p_ac3thread->p_config = p_config;
-    p_ac3thread->p_fifo = p_config->decoder_config.p_decoder_fifo;
+    if( ac3_adec_Init( p_ac3thread ) )
+    {
+        intf_ErrMsg( "ac3_adec error : could not initialize thread" );
+        return( -1 );
+    }
+
+    sync = 0;
+    p_ac3thread->sync_ptr = 0;
+
+    /* ac3 decoder thread's main loop */
+    /* FIXME : do we have enough room to store the decoded frames ?? */
+    while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
+    {
+        s16 * buffer;
+        ac3_sync_info_t sync_info;
+        int ptr;
+
+        if (!sync) {
+            do {
+                GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
+            } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
+                    && (!p_ac3thread->p_fifo->b_error));
+            
+            ptr = p_ac3thread->sync_ptr;
+
+            while(ptr-- && (!p_ac3thread->p_fifo->b_die)
+                && (!p_ac3thread->p_fifo->b_error))
+            {
+                p_ac3thread->ac3_decoder->bit_stream.p_byte++;
+            }
+                        
+            /* we are in sync now */
+            sync = 1;
+        }
+
+        if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
+        {
+            p_ac3thread->p_aout_fifo->date[
+                p_ac3thread->p_aout_fifo->l_end_frame] =
+                DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
+            DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
+        } else {
+            p_ac3thread->p_aout_fifo->date[
+                p_ac3thread->p_aout_fifo->l_end_frame] =
+                LAST_MDATE;
+        }
+    
+        if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
+        {
+            sync = 0;
+            goto bad_frame;
+        }
+
+        p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
+
+        buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
+            (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
+
+        if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
+        {
+            sync = 0;
+            goto bad_frame;
+        }
+        
+        vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
+        p_ac3thread->p_aout_fifo->l_end_frame = 
+            (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
+        vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
+        vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
+
+        bad_frame:
+            RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
+    }
+
+    /* If b_error is set, the ac3 decoder thread enters the error loop */
+    if (p_ac3thread->p_fifo->b_error)
+    {
+        ac3_adec_ErrorThread (p_ac3thread);
+    }
+
+    /* End of the ac3 decoder thread */
+    ac3_adec_EndThread (p_ac3thread);
+    
+    return( 0 );
+}
+
+
+/*****************************************************************************
+ * ac3_adec_Init: initialize data before entering main loop
+ *****************************************************************************/
+static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
+{
+    /*
+     * Thread properties 
+     */
+    p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
     p_ac3thread->ac3_decoder = memalign(16, sizeof(ac3dec_t));
 
     /*
@@ -109,7 +254,7 @@ vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
         free( p_ac3thread->ac3_decoder );
         free( p_ac3thread );
-        return( 0 );
+        return( -1 );
     }
 
 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
@@ -138,7 +283,7 @@ vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
         free( p_ac3thread->ac3_decoder->imdct );
         free( p_ac3thread->ac3_decoder );
         free( p_ac3thread );
-        return( 0 );
+        return( -1 );
     }
 
 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
@@ -152,10 +297,12 @@ vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
 
     /* Initialize the ac3 decoder structures */
 #if defined( __MINGW32__ )
-    p_ac3thread->ac3_decoder->samples_back = memalign(16, 6 * 256 * sizeof(float) + 15);
-    p_ac3thread->ac3_decoder->samples = (float *) (((unsigned long) p_ac3thread->ac3_decoder->samples_back+15) & ~0xFUL);
+    p_ac3thread->ac3_decoder->samples_back = memalign(16, 6 * 256 *
+        sizeof(float) + 15);
+    p_ac3thread->ac3_decoder->samples = (float *) (((unsigned long)
+        p_ac3thread->ac3_decoder->samples_back+15) & ~0xFUL);
 #else
-    p_ac3thread->ac3_decoder->samples = memalign(16, 6 * 256 * sizeof(float));
+     p_ac3thread->ac3_decoder->samples = memalign(16, 6 * 256 * sizeof(float));
 #endif
     p_ac3thread->ac3_decoder->imdct->buf = memalign(16, N/4 * sizeof(complex_t));
     p_ac3thread->ac3_decoder->imdct->delay = memalign(16, 6 * 256 * sizeof(float));
@@ -180,164 +327,35 @@ vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
      */
     p_ac3thread->p_aout_fifo = NULL;
 
-    /* Spawn the ac3 decoder thread */
-    if (vlc_thread_create(&p_ac3thread->thread_id, "ac3 decoder", 
-                (vlc_thread_func_t)RunThread, (void *)p_ac3thread))
-    {
-        intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread" );
-        module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
-        module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
-        free( p_ac3thread->ac3_decoder->imdct->w_1 );
-        free( p_ac3thread->ac3_decoder->imdct->w_64 );
-        free( p_ac3thread->ac3_decoder->imdct->w_32 );
-        free( p_ac3thread->ac3_decoder->imdct->w_16 );
-        free( p_ac3thread->ac3_decoder->imdct->w_8 );
-        free( p_ac3thread->ac3_decoder->imdct->w_4 );
-        free( p_ac3thread->ac3_decoder->imdct->w_2 );
-        free( p_ac3thread->ac3_decoder->imdct->xcos_sin_sse );
-        free( p_ac3thread->ac3_decoder->imdct->xsin2 );
-        free( p_ac3thread->ac3_decoder->imdct->xcos2 );
-        free( p_ac3thread->ac3_decoder->imdct->xsin1 );
-        free( p_ac3thread->ac3_decoder->imdct->xcos1 );
-        free( p_ac3thread->ac3_decoder->imdct->delay1 );
-        free( p_ac3thread->ac3_decoder->imdct->delay );
-        free( p_ac3thread->ac3_decoder->imdct->buf );
-#if defined( __MINGW32__ )
-        free( p_ac3thread->ac3_decoder->samples_back );
-#else
-        free( p_ac3thread->ac3_decoder->samples );
-#endif
-        free( p_ac3thread->ac3_decoder->imdct );
-        free( p_ac3thread->ac3_decoder );
-        free( p_ac3thread );
-        return 0;
-    }
+    intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized", 
+                  p_ac3thread );
 
-    intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3thread);
-    return p_ac3thread->thread_id;
-}
-
-/* Following functions are local */
-
-/*****************************************************************************
- * InitThread : initialize an ac3 decoder thread
- *****************************************************************************/
-static int InitThread (ac3dec_thread_t * p_ac3thread)
-{
-    intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3thread);
-
-    p_ac3thread->p_config->decoder_config.pf_init_bit_stream(
+    /*
+     * Bit stream
+     */
+    p_ac3thread->p_config->pf_init_bit_stream(
             &p_ac3thread->ac3_decoder->bit_stream,
-            p_ac3thread->p_config->decoder_config.p_decoder_fifo,
+            p_ac3thread->p_config->p_decoder_fifo,
             BitstreamCallback, (void *) p_ac3thread );
-
+    
     /* Creating the audio output fifo */
     p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
                                                AC3DEC_FRAME_SIZE, NULL  );
     if ( p_ac3thread->p_aout_fifo == NULL )
     {
-        return -1;
+        return( -1 );
     }
 
     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
-    return 0;
-}
-
-/*****************************************************************************
- * RunThread : ac3 decoder thread
- *****************************************************************************/
-static void RunThread (ac3dec_thread_t * p_ac3thread)
-{
-    int sync;
-
-    intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3thread, getpid());
-
-    /* Initializing the ac3 decoder thread */
-    if (InitThread (p_ac3thread)) /* XXX?? */
-    {
-        p_ac3thread->p_fifo->b_error = 1;
-    }
-
-    sync = 0;
-    p_ac3thread->sync_ptr = 0;
-
-    /* ac3 decoder thread's main loop */
-    /* FIXME : do we have enough room to store the decoded frames ?? */
-    while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
-    {
-        s16 * buffer;
-        ac3_sync_info_t sync_info;
-        int ptr;
-
-        if (!sync) {
-            do {
-                GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
-            } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
-                    && (!p_ac3thread->p_fifo->b_error));
-            
-            ptr = p_ac3thread->sync_ptr;
-
-            while(ptr-- && (!p_ac3thread->p_fifo->b_die)
-                && (!p_ac3thread->p_fifo->b_error))
-            {
-                p_ac3thread->ac3_decoder->bit_stream.p_byte++;
-            }
-                        
-            /* we are in sync now */
-            sync = 1;
-        }
-
-        if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
-        {
-            p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame] =
-                DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
-            DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
-        } else {
-            p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame] =
-                LAST_MDATE;
-        }
     
-        if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
-        {
-            sync = 0;
-            goto bad_frame;
-        }
-
-        p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
-
-        buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
-            (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
-
-        if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
-        {
-            sync = 0;
-            goto bad_frame;
-        }
-        
-        vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
-        p_ac3thread->p_aout_fifo->l_end_frame = 
-            (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-        vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
-        vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
-
-        bad_frame:
-            RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
-    }
-
-    /* If b_error is set, the ac3 decoder thread enters the error loop */
-    if (p_ac3thread->p_fifo->b_error)
-    {
-        ErrorThread (p_ac3thread);
-    }
-
-    /* End of the ac3 decoder thread */
-    EndThread (p_ac3thread);
+    return( 0 );
 }
 
+
 /*****************************************************************************
- * ErrorThread : ac3 decoder's RunThread() error loop
+ * ac3_adec_ErrorThread : ac3 decoder's RunThread() error loop
  *****************************************************************************/
-static void ErrorThread (ac3dec_thread_t * p_ac3thread)
+static void ac3_adec_ErrorThread (ac3dec_thread_t * p_ac3thread)
 {
     /* We take the lock, because we are going to read/write the start/end
      * indexes of the decoder fifo */
@@ -349,7 +367,8 @@ static void ErrorThread (ac3dec_thread_t * p_ac3thread)
         /* Trash all received PES packets */
         while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
         {
-            p_ac3thread->p_fifo->pf_delete_pes(p_ac3thread->p_fifo->p_packets_mgt,
+            p_ac3thread->p_fifo->pf_delete_pes(
+                    p_ac3thread->p_fifo->p_packets_mgt,
                     DECODER_FIFO_START(*p_ac3thread->p_fifo));
             DECODER_FIFO_INCSTART (*p_ac3thread->p_fifo);
         }
@@ -364,9 +383,9 @@ static void ErrorThread (ac3dec_thread_t * p_ac3thread)
 }
 
 /*****************************************************************************
- * EndThread : ac3 decoder thread destruction
+ * ac3_adec_EndThread : ac3 decoder thread destruction
  *****************************************************************************/
-static void EndThread (ac3dec_thread_t * p_ac3thread)
+static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
 {
     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);