]> git.sesse.net Git - vlc/blobdiff - plugins/ac3_adec/ac3_adec.c
* ALL: got rid of p_object->p_this which is now useless.
[vlc] / plugins / ac3_adec / ac3_adec.c
index 5692e35ee4745538b2a2ec5f44433c1058569e60..36535073fb95b28a94041c3e748b9a4bdd92eeae 100644 (file)
@@ -2,7 +2,7 @@
  * ac3_adec.c: ac3 decoder module main file
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: ac3_adec.c,v 1.8 2001/12/16 16:18:36 sam Exp $
+ * $Id: ac3_adec.c,v 1.33 2002/06/01 18:04:48 sam Exp $
  *
  * Authors: Michel Lespinasse <walken@zoy.org>
  *
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#define MODULE_NAME ac3_adec
-#include "modules_inner.h"
-
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>                                              /* getpid() */
-#endif
-
 #include <stdlib.h>                                      /* malloc(), free() */
 #include <string.h>                                              /* memset() */
 
-#include "common.h"
-#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
-#include "threads.h"
-#include "mtime.h"
+#include <vlc/vlc.h>
+#include <vlc/aout.h>
+#include <vlc/decoder.h>
 
-#include "audio_output.h"
-
-#include "stream_control.h"
-#include "input_ext-dec.h"
-#include "input_ext-intf.h"                                /* MPEG?_AUDIO_ES */
-
-#include "modules.h"
-#include "modules_export.h"
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>                                           /* getpid() */
+#endif
 
 #include "ac3_imdct.h"
 #include "ac3_downmix.h"
-#include "ac3_decoder.h"
 #include "ac3_adec.h"
 
 #define AC3DEC_FRAME_SIZE (2*1536) 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-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 );
+static int  decoder_Probe     ( u8 * );
+static int  decoder_Run       ( decoder_fifo_t * );
+static int  InitThread        ( ac3dec_t * p_adec );
+static void EndThread         ( ac3dec_t * p_adec );
+static void BitstreamCallback ( bit_stream_t *p_bit_stream,
+                                vlc_bool_t b_new_pes );
 
 /*****************************************************************************
  * 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_run = ac3_adec_Run;
+    p_function_list->functions.dec.pf_probe = decoder_Probe;
+    p_function_list->functions.dec.pf_run   = decoder_Run;
 }
 
 /*****************************************************************************
  * Build configuration tree.
  *****************************************************************************/
+/* Variable containing the AC3 downmix method */
+#define DOWNMIX_METHOD_VAR              "ac3-downmix"
+/* Variable containing the AC3 IMDCT method */
+#define IMDCT_METHOD_VAR                "ac3-imdct"
+
 MODULE_CONFIG_START
-ADD_WINDOW( "Configuration for AC3 decoder module" )
-    ADD_COMMENT( "Nothing to configure" )
+ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL)
+ADD_MODULE  ( DOWNMIX_METHOD_VAR, MODULE_CAPABILITY_DOWNMIX, NULL, NULL,
+              N_("AC3 downmix module"), NULL )
+ADD_MODULE  ( IMDCT_METHOD_VAR, MODULE_CAPABILITY_IMDCT, NULL, NULL,
+              N_("AC3 IMDCT module"), NULL )
 MODULE_CONFIG_STOP
 
 MODULE_INIT_START
-    p_module->i_capabilities = MODULE_CAPABILITY_DEC;
-    p_module->psz_longname = "AC3 sofware decoder";
+    SET_DESCRIPTION( _("software AC3 decoder") )
+    ADD_CAPABILITY( DECODER, 50 )
+    ADD_SHORTCUT( "ac3" )
 MODULE_INIT_STOP
 
 MODULE_ACTIVATE_START
@@ -99,151 +91,170 @@ MODULE_DEACTIVATE_STOP
 
 
 /*****************************************************************************
- * ac3_adec_Probe: probe the decoder and return score
+ * decoder_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 )
+static int decoder_Probe( u8 *pi_type )
 {
-    return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
+    return ( *pi_type == AC3_AUDIO_ES ) ? 0 : -1;
 }
 
 /*****************************************************************************
- * ac3_adec_Run: this function is called just after the thread is created
+ * decoder_Run: this function is called just after the thread is created
  *****************************************************************************/
-static int ac3_adec_Run ( decoder_config_t * p_config )
+static int decoder_Run ( decoder_fifo_t * p_fifo )
 {
-    ac3dec_thread_t *   p_ac3thread;
-    int sync;
-
-    intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
+    ac3dec_t *   p_ac3dec;
+    void *       p_orig;                          /* pointer before memalign */
+    vlc_bool_t   b_sync = 0;
 
     /* Allocate the memory needed to store the thread's structure */
-    p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
+    p_ac3dec = (ac3dec_t *)vlc_memalign( &p_orig, 16, sizeof(ac3dec_t) );
+    memset( p_ac3dec, 0, sizeof( ac3dec_t ) );
 
-    if( p_ac3thread == NULL )
+    if( p_ac3dec == NULL )
     {
-        intf_ErrMsg ( "ac3_adec error: not enough memory "
-                      "for ac3_adec_Run() to allocate p_ac3thread" );
+        msg_Err( p_fifo, "out of memory" );
+        DecoderError( p_fifo );
         return( -1 );
     }
-   
+
     /*
      * Initialize the thread properties
      */
-    p_ac3thread->p_config = p_config;
-    if( ac3_adec_Init( p_ac3thread ) )
+    p_ac3dec->p_fifo = p_fifo;
+    if( InitThread( p_ac3dec ) )
     {
-        intf_ErrMsg( "ac3_adec error: could not initialize thread" );
-        free( p_ac3thread );
+        msg_Err( p_fifo, "could not initialize thread" );
+        DecoderError( p_fifo );
+        free( p_orig );
         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))
+    while ((!p_ac3dec->p_fifo->b_die) && (!p_ac3dec->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( !b_sync )
+        {
+             int i_sync_ptr;
+#define p_bit_stream (&p_ac3dec->bit_stream)
+
+             /* Go to the next PES packet and jump to sync_ptr */
+             do {
+                BitstreamNextDataPacket( p_bit_stream );
+             } while( !p_bit_stream->p_decoder_fifo->b_die
+                       && !p_bit_stream->p_decoder_fifo->b_error
+                       && p_bit_stream->p_data !=
+                          p_bit_stream->p_decoder_fifo->p_first->p_first );
+             i_sync_ptr = *(p_bit_stream->p_byte - 2) << 8
+                            | *(p_bit_stream->p_byte - 1);
+             p_bit_stream->p_byte += i_sync_ptr;
+
+             /* Empty the bit FIFO and realign the bit stream */
+             p_bit_stream->fifo.buffer = 0;
+             p_bit_stream->fifo.i_available = 0;
+             AlignWord( p_bit_stream );
+             b_sync = 1;
+#undef p_bit_stream
         }
 
-        if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
+        if (ac3_sync_frame (p_ac3dec, &sync_info))
         {
-            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;
+            b_sync = 0;
+            continue;
         }
-    
-        if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
+
+        if( ( p_ac3dec->p_aout_fifo != NULL ) &&
+            ( p_ac3dec->p_aout_fifo->i_rate != sync_info.sample_rate ) )
         {
-            sync = 0;
-            goto bad_frame;
+            /* Make sure the output thread leaves the NextFrame() function */
+            vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
+            aout_DestroyFifo (p_ac3dec->p_aout_fifo);
+            vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
+            vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
+
+            p_ac3dec->p_aout_fifo = NULL;
         }
 
-        p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
+        /* Creating the audio output fifo if not created yet */
+        if (p_ac3dec->p_aout_fifo == NULL ) {
+            p_ac3dec->p_aout_fifo =
+                aout_CreateFifo( p_ac3dec->p_fifo, AOUT_FIFO_PCM, 2,
+                         sync_info.sample_rate, AC3DEC_FRAME_SIZE, NULL  );
+            if ( p_ac3dec->p_aout_fifo == NULL )
+            {
+                p_ac3dec->p_fifo->b_error = 1;
+                break;
+            }
+        }
 
-        buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
-            (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
+        CurrentPTS( &p_ac3dec->bit_stream,
+            &p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->i_end_frame],
+            NULL );
+        if( !p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->i_end_frame] )
+        {
+            p_ac3dec->p_aout_fifo->date[
+                p_ac3dec->p_aout_fifo->i_end_frame] =
+                LAST_MDATE;
+        }
+    
+        buffer = ((s16 *)p_ac3dec->p_aout_fifo->buffer) + 
+            (p_ac3dec->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE);
 
-        if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
+        if (ac3_decode_frame (p_ac3dec, buffer))
         {
-            sync = 0;
-            goto bad_frame;
+            b_sync = 0;
+            continue;
         }
         
-        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);
+        vlc_mutex_lock (&p_ac3dec->p_aout_fifo->data_lock);
+        p_ac3dec->p_aout_fifo->i_end_frame = 
+            (p_ac3dec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
+        vlc_cond_signal (&p_ac3dec->p_aout_fifo->data_wait);
+        vlc_mutex_unlock (&p_ac3dec->p_aout_fifo->data_lock);
+
+        RealignBits(&p_ac3dec->bit_stream);
     }
 
     /* If b_error is set, the ac3 decoder thread enters the error loop */
-    if (p_ac3thread->p_fifo->b_error)
+    if (p_ac3dec->p_fifo->b_error)
     {
-        ac3_adec_ErrorThread (p_ac3thread);
+        DecoderError( p_ac3dec->p_fifo );
     }
 
     /* End of the ac3 decoder thread */
-    ac3_adec_EndThread (p_ac3thread);
+    EndThread (p_ac3dec);
 
-    free( p_ac3thread );
+    free( p_orig );
 
     return( 0 );
 }
 
-
 /*****************************************************************************
- * ac3_adec_Init: initialize data before entering main loop
+ * InitThread: initialize data before entering main loop
  *****************************************************************************/
-static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
+static int InitThread( ac3dec_t * p_ac3dec )
 {
-    /*
-     * Thread properties 
-     */
-    p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
-    p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
+    char *psz_name;
 
     /*
      * Choose the best downmix module
      */
-#define DOWNMIX p_ac3thread->ac3_decoder->downmix
-    DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
+#define DOWNMIX p_ac3dec->downmix
+    psz_name = config_GetPsz( p_ac3dec->p_fifo, DOWNMIX_METHOD_VAR );
+    DOWNMIX.p_module = module_Need( p_ac3dec->p_fifo,
+                                    MODULE_CAPABILITY_DOWNMIX, psz_name, NULL );
+    if( psz_name ) free( psz_name );
 
     if( DOWNMIX.p_module == NULL )
     {
-        intf_ErrMsg( "ac3dec error: no suitable downmix module" );
-        free( p_ac3thread->ac3_decoder );
+        msg_Err( p_ac3dec->p_fifo, "no suitable downmix module" );
         return( -1 );
     }
 
@@ -261,17 +272,20 @@ static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
     /*
      * Choose the best IMDCT module
      */
-    p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
+    p_ac3dec->imdct = vlc_memalign( &p_ac3dec->imdct_orig,
+                                    16, sizeof(imdct_t) );
     
-#define IMDCT p_ac3thread->ac3_decoder->imdct
-    IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
+#define IMDCT p_ac3dec->imdct
+    psz_name = config_GetPsz( p_ac3dec->p_fifo, IMDCT_METHOD_VAR );
+    IMDCT->p_module = module_Need( p_ac3dec->p_fifo,
+                                   MODULE_CAPABILITY_IMDCT, psz_name, NULL );
+    if( psz_name ) free( psz_name );
 
     if( IMDCT->p_module == NULL )
     {
-        intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
-        module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
-        free( p_ac3thread->ac3_decoder->imdct );
-        free( p_ac3thread->ac3_decoder );
+        msg_Err( p_ac3dec->p_fifo, "no suitable IMDCT module" );
+        module_Unneed( p_ac3dec->downmix.p_module );
+        free( p_ac3dec->imdct_orig );
         return( -1 );
     }
 
@@ -284,175 +298,100 @@ static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
 #undef F
 
     /* Initialize the ac3 decoder structures */
-#define p_dec p_ac3thread->ac3_decoder
-#if defined( __MINGW32__ )
-    p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
-    p_dec->samples = (float *)
-                     (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
-#else
-    p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
-#endif
-#undef p_dec
-
-    IMDCT->buf    = memalign( 16, N/4 * sizeof(complex_t) );
-    IMDCT->delay  = memalign( 16, 6 * 256 * sizeof(float) );
-    IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
-    IMDCT->xcos1  = memalign( 16, N/4 * sizeof(float) );
-    IMDCT->xsin1  = memalign( 16, N/4 * sizeof(float) );
-    IMDCT->xcos2  = memalign( 16, N/8 * sizeof(float) );
-    IMDCT->xsin2  = memalign( 16, N/8 * sizeof(float) );
-    IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
-    IMDCT->w_1    = memalign( 16, 1  * sizeof(complex_t) );
-    IMDCT->w_2    = memalign( 16, 2  * sizeof(complex_t) );
-    IMDCT->w_4    = memalign( 16, 4  * sizeof(complex_t) );
-    IMDCT->w_8    = memalign( 16, 8  * sizeof(complex_t) );
-    IMDCT->w_16   = memalign( 16, 16 * sizeof(complex_t) );
-    IMDCT->w_32   = memalign( 16, 32 * sizeof(complex_t) );
-    IMDCT->w_64   = memalign( 16, 64 * sizeof(complex_t) );
-
-    ac3_init( p_ac3thread->ac3_decoder );
+    p_ac3dec->samples = vlc_memalign( &p_ac3dec->samples_orig,
+                                      16, 6 * 256 * sizeof(float) );
+
+    IMDCT->buf    = vlc_memalign( &IMDCT->buf_orig,
+                                  16, N/4 * sizeof(complex_t) );
+    IMDCT->delay  = vlc_memalign( &IMDCT->delay_orig,
+                                  16, 6 * 256 * sizeof(float) );
+    IMDCT->delay1 = vlc_memalign( &IMDCT->delay1_orig,
+                                  16, 6 * 256 * sizeof(float) );
+    IMDCT->xcos1  = vlc_memalign( &IMDCT->xcos1_orig,
+                                  16, N/4 * sizeof(float) );
+    IMDCT->xsin1  = vlc_memalign( &IMDCT->xsin1_orig,
+                                  16, N/4 * sizeof(float) );
+    IMDCT->xcos2  = vlc_memalign( &IMDCT->xcos2_orig,
+                                  16, N/8 * sizeof(float) );
+    IMDCT->xsin2  = vlc_memalign( &IMDCT->xsin2_orig,
+                                  16, N/8 * sizeof(float) );
+    IMDCT->xcos_sin_sse = vlc_memalign( &IMDCT->xcos_sin_sse_orig,
+                                        16, 128 * 4 * sizeof(float) );
+    IMDCT->w_1    = vlc_memalign( &IMDCT->w_1_orig,
+                                  16, 1  * sizeof(complex_t) );
+    IMDCT->w_2    = vlc_memalign( &IMDCT->w_2_orig,
+                                  16, 2  * sizeof(complex_t) );
+    IMDCT->w_4    = vlc_memalign( &IMDCT->w_4_orig,
+                                  16, 4  * sizeof(complex_t) );
+    IMDCT->w_8    = vlc_memalign( &IMDCT->w_8_orig,
+                                  16, 8  * sizeof(complex_t) );
+    IMDCT->w_16   = vlc_memalign( &IMDCT->w_16_orig,
+                                  16, 16 * sizeof(complex_t) );
+    IMDCT->w_32   = vlc_memalign( &IMDCT->w_32_orig,
+                                  16, 32 * sizeof(complex_t) );
+    IMDCT->w_64   = vlc_memalign( &IMDCT->w_64_orig,
+                                  16, 64 * sizeof(complex_t) );
+#undef IMDCT
+
+    _M( ac3_init )( p_ac3dec );
 
     /*
      * Initialize the output properties
      */
-    p_ac3thread->p_aout_fifo = NULL;
-
-    intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized", 
-                  p_ac3thread );
+    p_ac3dec->p_aout_fifo = NULL;
 
     /*
      * Bit stream
      */
-    p_ac3thread->p_config->pf_init_bit_stream(
-            &p_ac3thread->ac3_decoder->bit_stream,
-            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 )
-    {
-        free( IMDCT->w_1 );
-        free( IMDCT->w_64 );
-        free( IMDCT->w_32 );
-        free( IMDCT->w_16 );
-        free( IMDCT->w_8 );
-        free( IMDCT->w_4 );
-        free( IMDCT->w_2 );
-        free( IMDCT->xcos_sin_sse );
-        free( IMDCT->xsin2 );
-        free( IMDCT->xcos2 );
-        free( IMDCT->xsin1 );
-        free( IMDCT->xcos1 );
-        free( IMDCT->delay1 );
-        free( IMDCT->delay );
-        free( IMDCT->buf );
-#undef IMDCT
-
-#if defined( __MINGW32__ )
-        free( p_ac3thread->ac3_decoder->samples_back );
-#else
-        free( p_ac3thread->ac3_decoder->samples );
-#endif
-
-        module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
-        module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
-
-        free( p_ac3thread->ac3_decoder->imdct );
-        free( p_ac3thread->ac3_decoder );
-
-        return( -1 );
-    }
-
-    intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
+    InitBitstream( &p_ac3dec->bit_stream, p_ac3dec->p_fifo,
+                   BitstreamCallback, (void *) p_ac3dec );
     
     return( 0 );
 }
 
-
 /*****************************************************************************
- * ac3_adec_ErrorThread : ac3 decoder's RunThread() error loop
+ * EndThread : ac3 decoder thread destruction
  *****************************************************************************/
-static void ac3_adec_ErrorThread (ac3dec_thread_t * p_ac3thread)
+static void EndThread (ac3dec_t * p_ac3dec)
 {
-    /* We take the lock, because we are going to read/write the start/end
-     * indexes of the decoder fifo */
-    vlc_mutex_lock (&p_ac3thread->p_fifo->data_lock);
-
-    /* Wait until a `die' order is sent */
-    while (!p_ac3thread->p_fifo->b_die)
-    {
-        /* 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,
-                    DECODER_FIFO_START(*p_ac3thread->p_fifo));
-            DECODER_FIFO_INCSTART (*p_ac3thread->p_fifo);
-        }
-
-        /* Waiting for the input thread to put new PES packets in the fifo */
-        vlc_cond_wait (&p_ac3thread->p_fifo->data_wait,
-                       &p_ac3thread->p_fifo->data_lock);
-    }
-
-    /* We can release the lock before leaving */
-    vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
-}
-
-/*****************************************************************************
- * ac3_adec_EndThread : ac3 decoder thread destruction
- *****************************************************************************/
-static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
-{
-    intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
-
     /* If the audio output fifo was created, we destroy it */
-    if (p_ac3thread->p_aout_fifo != NULL)
+    if (p_ac3dec->p_aout_fifo != NULL)
     {
-        aout_DestroyFifo (p_ac3thread->p_aout_fifo);
+        aout_DestroyFifo (p_ac3dec->p_aout_fifo);
 
         /* Make sure the output thread leaves the NextFrame() function */
-        vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
-        vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
-        vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
+        vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
+        vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
+        vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
     }
 
     /* Free allocated structures */
-#define IMDCT p_ac3thread->ac3_decoder->imdct
-    free( IMDCT->w_1 );
-    free( IMDCT->w_64 );
-    free( IMDCT->w_32 );
-    free( IMDCT->w_16 );
-    free( IMDCT->w_8 );
-    free( IMDCT->w_4 );
-    free( IMDCT->w_2 );
-    free( IMDCT->xcos_sin_sse );
-    free( IMDCT->xsin2 );
-    free( IMDCT->xcos2 );
-    free( IMDCT->xsin1 );
-    free( IMDCT->xcos1 );
-    free( IMDCT->delay1 );
-    free( IMDCT->delay );
-    free( IMDCT->buf );
+#define IMDCT p_ac3dec->imdct
+    free( IMDCT->w_1_orig );
+    free( IMDCT->w_64_orig );
+    free( IMDCT->w_32_orig );
+    free( IMDCT->w_16_orig );
+    free( IMDCT->w_8_orig );
+    free( IMDCT->w_4_orig );
+    free( IMDCT->w_2_orig );
+    free( IMDCT->xcos_sin_sse_orig );
+    free( IMDCT->xsin2_orig );
+    free( IMDCT->xcos2_orig );
+    free( IMDCT->xsin1_orig );
+    free( IMDCT->xcos1_orig );
+    free( IMDCT->delay1_orig );
+    free( IMDCT->delay_orig );
+    free( IMDCT->buf_orig );
 #undef IMDCT
 
-#if defined( __MINGW32__ )
-    free( p_ac3thread->ac3_decoder->samples_back );
-#else
-    free( p_ac3thread->ac3_decoder->samples );
-#endif
+    free( p_ac3dec->samples_orig );
 
     /* Unlock the modules */
-    module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
-    module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
+    module_Unneed( p_ac3dec->downmix.p_module );
+    module_Unneed( p_ac3dec->imdct->p_module );
 
     /* Free what's left of the decoder */
-    free( p_ac3thread->ac3_decoder->imdct );
-    free( p_ac3thread->ac3_decoder );
-
-    intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
+    free( p_ac3dec->imdct_orig );
 }
 
 /*****************************************************************************
@@ -461,20 +400,12 @@ static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
  * This function is called by input's NextDataPacket.
  *****************************************************************************/
 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
-                                        boolean_t b_new_pes)
+                                vlc_bool_t b_new_pes )
 {
-
-    ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
-
     if( b_new_pes )
     {
-        int ptr;
-        
-        ptr = *(p_bit_stream->p_byte + 1);
-        ptr <<= 8;
-        ptr |= *(p_bit_stream->p_byte + 2);
-        p_ac3thread->sync_ptr = ptr;
-        p_bit_stream->p_byte += 3;                                                            
+        /* Drop special AC3 header */
+/*        p_bit_stream->p_byte += 3; */
     }
 }