]> git.sesse.net Git - vlc/blobdiff - src/ac3_decoder/ac3_decoder.c
* Fixed a few warnings with gcc 3.0.
[vlc] / src / ac3_decoder / ac3_decoder.c
index c80e80208ade135858070e8b2561910e639dfe83..8659165496019b86f1c11b0131c3f40efef27794 100644 (file)
 /*****************************************************************************
- * ac3_decoder.c: ac3 decoder thread
- * (c)1999 VideoLAN
+ * ac3_decoder.c: core ac3 decoder
+ *****************************************************************************
+ * Copyright (C) 1999, 2000 VideoLAN
+ * $Id: ac3_decoder.c,v 1.31 2001/05/06 04:32:02 sam Exp $
+ *
+ * Authors: Michel Kaempf <maxx@via.ecp.fr>
+ *          Michel Lespinasse <walken@zoy.org>
+ *          Aaron Holtzman <aholtzma@engr.uvic.ca>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-/*****************************************************************************
- * Preamble
- *****************************************************************************/
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#include <unistd.h>                                              /* getpid() */
+#include "defs.h"
 
-#include <stdio.h>                                           /* "intf_msg.h" */
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <sys/soundcard.h>                               /* "audio_output.h" */
-#include <sys/uio.h>                                            /* "input.h" */
+#include <string.h>                                              /* memcpy() */
 
-#include "common.h"
 #include "config.h"
+#include "common.h"
+#include "threads.h"
 #include "mtime.h"
-#include "vlc_thread.h"
-#include "debug.h"                                      /* "input_netlist.h" */
 
 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
 
-#include "input.h"                                           /* pes_packet_t */
-#include "input_netlist.h"                         /* input_NetlistFreePES() */
-#include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
+#include "stream_control.h"
+#include "input_ext-dec.h"
 
 #include "audio_output.h"
 
 #include "ac3_decoder.h"
-#include "ac3_parse.h"
-#include "ac3_exponent.h"
-#include "ac3_bit_allocate.h"
-#include "ac3_mantissa.h"
-#include "ac3_rematrix.h"
-#include "ac3_imdct.h"
-#include "ac3_downmix.h"
-
-/*****************************************************************************
- * 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 byte_t   GetByte                 ( bit_stream_t * p_bit_stream );
-//static void     NeedBits                ( bit_stream_t * p_bit_stream, int i_bits );
-//static void     DumpBits                ( bit_stream_t * p_bit_stream, int i_bits );
-//static int      FindHeader              ( adec_thread_t * p_adec );
+#include "ac3_decoder_thread.h"
+#include "ac3_internal.h"
 
-/*****************************************************************************
- * ac3dec_CreateThread: creates an ac3 decoder thread
- *****************************************************************************/
-ac3dec_thread_t * ac3dec_CreateThread( input_thread_t * p_input )
-{
-    ac3dec_thread_t *   p_ac3dec;
+#include <stdio.h>
 
-    intf_DbgMsg("ac3dec debug: creating ac3 decoder thread\n");
+void imdct_init (imdct_t * p_imdct);
+void downmix_init (downmix_t * p_downmix);
 
-    /* Allocate the memory needed to store the thread's structure */
-    if ( (p_ac3dec = (ac3dec_thread_t *)malloc( sizeof(ac3dec_thread_t) )) == NULL )
-    {
-        intf_ErrMsg("ac3dec error: not enough memory for ac3dec_CreateThread() to create the new thread\n");
-        return( NULL );
-    }
-
-    /*
-     * Initialize the thread properties
-     */
-    p_ac3dec->b_die = 0;
-    p_ac3dec->b_error = 0;
-
-    /*
-     * Initialize the input properties
-     */
-    /* Initialize the decoder fifo's data lock and conditional variable and set
-     * its buffer as empty */
-    vlc_mutex_init( &p_ac3dec->fifo.data_lock );
-    vlc_cond_init( &p_ac3dec->fifo.data_wait );
-    p_ac3dec->fifo.i_start = 0;
-    p_ac3dec->fifo.i_end = 0;
-    /* Initialize the bit stream structure */
-    p_ac3dec->bit_stream.p_input = p_input;
-    p_ac3dec->bit_stream.p_decoder_fifo = &p_ac3dec->fifo;
-    p_ac3dec->bit_stream.fifo.buffer = 0;
-    p_ac3dec->bit_stream.fifo.i_available = 0;
-
-    /*
-     * Initialize the output properties
-     */
-    p_ac3dec->p_aout = p_input->p_aout;
-    p_ac3dec->p_aout_fifo = NULL;
-
-    imdct_init();
-
-    /* Spawn the ac3 decoder thread */
-    if ( vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec) )
-    {
-        intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
-        free( p_ac3dec );
-        return( NULL );
-    }
-
-    intf_DbgMsg( "ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec );
-    return( p_ac3dec );
-}
+static float cmixlev_lut[4] = { 0.707, 0.595, 0.500, 0.707 };
+static float smixlev_lut[4] = { 0.707, 0.500, 0.0  , 0.500 };
 
-/*****************************************************************************
- * ac3dec_DestroyThread: destroys an ac3 decoder thread
- *****************************************************************************/
-void ac3dec_DestroyThread( ac3dec_thread_t * p_ac3dec )
-{
-    intf_DbgMsg( "ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec );
-
-    /* Ask thread to kill itself */
-    p_ac3dec->b_die = 1;
-    /* Make sure the decoder thread leaves the GetByte() function */
-    vlc_mutex_lock( &(p_ac3dec->fifo.data_lock) );
-    vlc_cond_signal( &(p_ac3dec->fifo.data_wait) );
-    vlc_mutex_unlock( &(p_ac3dec->fifo.data_lock) );
-
-    /* Waiting for the decoder thread to exit */
-    /* Remove this as soon as the "status" flag is implemented */
-    vlc_thread_join( p_ac3dec->thread_id );
-}
-
-/* Following functions are local */
-
-/*****************************************************************************
- * decode_find_sync()
- *****************************************************************************/
-static __inline__ int decode_find_sync( ac3dec_thread_t * p_ac3dec )
-{
-    while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
-    {
-        NeedBits( &(p_ac3dec->bit_stream), 16 );
-        if ( (p_ac3dec->bit_stream.fifo.buffer >> (32 - 16)) == 0x0b77 )
-        {
-            DumpBits( &(p_ac3dec->bit_stream), 16 );
-            p_ac3dec->total_bits_read = 16;
-            return( 0 );
-        }
-        DumpBits( &(p_ac3dec->bit_stream), 8 );
-    }
-    return( -1 );
-}
-
-/*****************************************************************************
- * InitThread : initialize an ac3 decoder thread
- *****************************************************************************/
-static int InitThread( ac3dec_thread_t * p_ac3dec )
+int ac3_init (ac3dec_t * p_ac3dec)
 {
-    aout_fifo_t         aout_fifo;
-
-    intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );
-
-    /* Our first job is to initialize the bit stream structure with the
-     * beginning of the input stream */
-    vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
-    while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
-    {
-        vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
-    }
-    p_ac3dec->bit_stream.p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
-    p_ac3dec->bit_stream.i_byte = p_ac3dec->bit_stream.p_ts->i_payload_start;
-    vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
-
-    aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
-    aout_fifo.b_stereo = 1;
-
-    aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
-
-    /* Creating the audio output fifo */
-    if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
-    {
-        return( -1 );
-    }
-
-    intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
-    return( 0 );
+//    p_ac3dec->bit_stream.buffer = 0;
+//    p_ac3dec->bit_stream.i_available = 0;
+    p_ac3dec->mantissa.lfsr_state = 1;       /* dither_gen initialization */
+    imdct_init(&p_ac3dec->imdct);
+    downmix_init(&p_ac3dec->downmix);
+    
+    return 0;
 }
 
-/*****************************************************************************
- * RunThread : ac3 decoder thread
- *****************************************************************************/
-static void RunThread( ac3dec_thread_t * p_ac3dec )
+int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer)
 {
-    intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );
-
-    /* Initializing the ac3 decoder thread */
-    if ( InitThread(p_ac3dec) )
+    int i;
+    ac3dec_thread_t * p_ac3dec_t = (ac3dec_thread_t *) p_ac3dec->bit_stream.p_callback_arg;
+    
+    if (parse_bsi (p_ac3dec))
     {
-        p_ac3dec->b_error = 1;
+        intf_WarnMsg (1,"Error during ac3parsing");
+        parse_auxdata (p_ac3dec);
+        return 1;
     }
-
-    /* ac3 decoder thread's main loop */
-    while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
-    {
-       decode_find_sync( p_ac3dec );
-
-       if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
-       {
-               p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
-               DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
-       }
-       else
-       {
-               p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
-       }
-
-       parse_syncinfo( p_ac3dec );
-/*
-        switch ( p_ac3dec->syncinfo.fscod )
+    
+       /* compute downmix parameters
+        * downmix to tow channels for now */
+       p_ac3dec->dm_par.clev = 0.0;
+    p_ac3dec->dm_par.slev = 0.0; 
+    p_ac3dec->dm_par.unit = 1.0;
+       if (p_ac3dec->bsi.acmod & 0x1)  /* have center */
+           p_ac3dec->dm_par.clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
+
+       if (p_ac3dec->bsi.acmod & 0x4)  /* have surround channels */
+               p_ac3dec->dm_par.slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
+
+    p_ac3dec->dm_par.unit /= 1.0 + p_ac3dec->dm_par.clev + p_ac3dec->dm_par.slev;
+       p_ac3dec->dm_par.clev *= p_ac3dec->dm_par.unit;
+       p_ac3dec->dm_par.slev *= p_ac3dec->dm_par.unit;
+
+    for (i = 0; i < 6; i++) {
+        /* Initialize freq/time sample storage */
+        memset(p_ac3dec->samples, 0, sizeof(float) * 256 * 
+                (p_ac3dec->bsi.nfchans + p_ac3dec->bsi.lfeon));
+
+
+        if ((p_ac3dec_t->p_fifo->b_die) && (p_ac3dec_t->p_fifo->b_error))
+        {        
+            return 1;
+        }
+        if (parse_audblk (p_ac3dec, i))
         {
-            case 0:
-                p_ac3dec->p_aout_fifo->l_rate = 48000;
-                break;
-
-            case 1:
-                p_ac3dec->p_aout_fifo->l_rate = 44100;
-                break;
-
-            case 2:
-                p_ac3dec->p_aout_fifo->l_rate = 32000;
-                break;
-
-            default:
-                fprintf( stderr, "ac3dec debug: fscod == `11' (reserved)\n" );
-                break;
+            intf_WarnMsg (1,"Error during ac3audioblock");
+            parse_auxdata (p_ac3dec);
+            return 1;
         }
-*/
-       parse_bsi( p_ac3dec );
-
-       /* frame 1 */
-       parse_audblk( p_ac3dec );
-       exponent_unpack( p_ac3dec );
-       bit_allocate( p_ac3dec );
-       mantissa_unpack( p_ac3dec );
-       if ( p_ac3dec->bsi.acmod == 0x2 )
-       {
-               rematrix( p_ac3dec );
-       }
-       imdct( p_ac3dec );
-       downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
-       vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
-       p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-       vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
-
-       /* frame 2 */
-       parse_audblk( p_ac3dec );
-       exponent_unpack( p_ac3dec );
-       bit_allocate( p_ac3dec );
-       mantissa_unpack( p_ac3dec );
-       if ( p_ac3dec->bsi.acmod == 0x2 )
-       {
-               rematrix( p_ac3dec );
-       }
-       imdct( p_ac3dec );
-       downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
-       p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
-       vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
-       p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-       vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
-
-       /* frame 3 */
-       parse_audblk( p_ac3dec );
-       exponent_unpack( p_ac3dec );
-       bit_allocate( p_ac3dec );
-       mantissa_unpack( p_ac3dec );
-       if ( p_ac3dec->bsi.acmod == 0x2 )
-       {
-               rematrix( p_ac3dec );
-       }
-       imdct( p_ac3dec );
-       downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
-       p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
-       vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
-       p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-       vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
-
-       /* frame 4 */
-       parse_audblk( p_ac3dec );
-       exponent_unpack( p_ac3dec );
-       bit_allocate( p_ac3dec );
-       mantissa_unpack( p_ac3dec );
-       if ( p_ac3dec->bsi.acmod == 0x2 )
-       {
-               rematrix( p_ac3dec );
-       }
-       imdct( p_ac3dec );
-       downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
-       p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
-       vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
-       p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-       vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
-
-       /* frame 5 */
-       parse_audblk( p_ac3dec );
-       exponent_unpack( p_ac3dec );
-       bit_allocate( p_ac3dec );
-       mantissa_unpack( p_ac3dec );
-       if ( p_ac3dec->bsi.acmod == 0x2 )
-       {
-               rematrix( p_ac3dec );
-       }
-       imdct( p_ac3dec );
-       downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
-       p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
-       vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
-       p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-       vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
 
-       /* frame 6 */
-       parse_audblk( p_ac3dec );
-       exponent_unpack( p_ac3dec );
-       bit_allocate( p_ac3dec );
-       mantissa_unpack( p_ac3dec );
-       if ( p_ac3dec->bsi.acmod == 0x2 )
-       {
-               rematrix( p_ac3dec );
-       }
-       imdct( p_ac3dec );
-       downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
-       p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
-       vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
-       p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-       vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
-
-       parse_auxdata( p_ac3dec );
-    }
-
-    /* If b_error is set, the ac3 decoder thread enters the error loop */
-    if ( p_ac3dec->b_error )
-    {
-        ErrorThread( p_ac3dec );
-    }
-
-    /* End of the ac3 decoder thread */
-    EndThread( p_ac3dec );
-}
-
-/*****************************************************************************
- * ErrorThread : ac3 decoder's RunThread() error loop
- *****************************************************************************/
-static void ErrorThread( ac3dec_thread_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_ac3dec->fifo.data_lock );
+        if ((p_ac3dec_t->p_fifo->b_die) && (p_ac3dec_t->p_fifo->b_error))
+        {        
+            return 1;
+        }
 
-    /* Wait until a `die' order is sent */
-    while( !p_ac3dec->b_die )
-    {
-        /* Trash all received PES packets */
-        while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
+        if (exponent_unpack (p_ac3dec))
         {
-            input_NetlistFreePES( p_ac3dec->bit_stream.p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
-           DECODER_FIFO_INCSTART( p_ac3dec->fifo );
+            intf_WarnMsg (1,"Error during ac3unpack");
+            parse_auxdata (p_ac3dec);
+            return 1;
         }
+        bit_allocate (p_ac3dec);
+        mantissa_unpack (p_ac3dec);
 
-        /* Waiting for the input thread to put new PES packets in the fifo */
-        vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
-    }
-
-    /* We can release the lock before leaving */
-    vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
-}
-
-/*****************************************************************************
- * EndThread : ac3 decoder thread destruction
- *****************************************************************************/
-static void EndThread( ac3dec_thread_t * p_ac3dec )
-{
-    intf_DbgMsg( "ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec );
+        if ((p_ac3dec_t->p_fifo->b_die) && (p_ac3dec_t->p_fifo->b_error))
+        {        
+            return 1;
+        }
+        
+        if  (p_ac3dec->bsi.acmod == 0x2)
+            rematrix (p_ac3dec);
+        imdct (p_ac3dec, buffer);
 
-    /* If the audio output fifo was created, we destroy it */
-    if ( p_ac3dec->p_aout_fifo != NULL )
-    {
-        aout_DestroyFifo( p_ac3dec->p_aout_fifo );
+        buffer += 2*256;
     }
 
-    /* Destroy descriptor */
-    free( p_ac3dec );
+    parse_auxdata (p_ac3dec);
 
-    intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec );
+    return 0;
 }