]> git.sesse.net Git - vlc/blobdiff - plugins/lpcm_adec/lpcm_adec.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[vlc] / plugins / lpcm_adec / lpcm_adec.c
index e91961bfda865c0031e0c2214c823a01e7f9f676..0fc0d3134f0c83d06164f62a83b8c424ababb30e 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
- * lpcm_decoder_thread.c: lpcm decoder thread
+ * lpcm_adec.c: lpcm decoder thread
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: lpcm_adec.c,v 1.10 2002/01/21 23:57:46 massiot Exp $
+ * $Id: lpcm_adec.c,v 1.20 2002/07/31 20:56:51 sam Exp $
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *          Henri Fallon <henri@videolan.org>
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdio.h>                                           /* "intf_msg.h" */
 #include <string.h>                                    /* memcpy(), memset() */
 #include <stdlib.h>                                      /* malloc(), free() */
 
-#include <videolan/vlc.h>
+#include <vlc/vlc.h>
+#include <vlc/aout.h>
+#include <vlc/decoder.h>
 
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>                                           /* getpid() */
 #endif
 
-#include "audio_output.h"
-
-#include "stream_control.h"
-#include "input_ext-dec.h"
-
 #include "lpcm_adec.h"
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int  decoder_Probe  ( probedata_t * );
-static int  decoder_Run    ( decoder_config_t * );
+static int  OpenDecoder    ( vlc_object_t * );
+static int  RunDecoder     ( decoder_fifo_t * );
+
        void DecodeFrame    ( lpcmdec_thread_t * );
 static int  InitThread     ( lpcmdec_thread_t * );
 static void EndThread      ( lpcmdec_thread_t * );
 
-
 /*****************************************************************************
- * Capabilities
+ * Module descriptor
  *****************************************************************************/
-void _M( adec_getfunctions )( function_list_t * p_function_list )
-{
-    p_function_list->pf_probe = decoder_Probe;
-    p_function_list->functions.dec.pf_run = decoder_Run;
-}
+vlc_module_begin();
+    set_description( _("linear PCM audio decoder") );
+    set_capability( "decoder", 100 );
+    set_callbacks( OpenDecoder, NULL );
+vlc_module_end();
 
 /*****************************************************************************
- * Build configuration tree.
+ * OpenDecoder: probe the decoder and return score
  *****************************************************************************/
-MODULE_CONFIG_START
-MODULE_CONFIG_STOP
-
-MODULE_INIT_START
-    SET_DESCRIPTION( "Linear PCM audio decoder" )
-    ADD_CAPABILITY( DECODER, 100 )
-MODULE_INIT_STOP
-
-MODULE_ACTIVATE_START
-    _M( adec_getfunctions )( &p_module->p_functions->dec );
-MODULE_ACTIVATE_STOP
-
-MODULE_DEACTIVATE_START
-MODULE_DEACTIVATE_STOP
-
-/*****************************************************************************
- * decoder_Probe: probe the decoder and return score
- *****************************************************************************/
-static int decoder_Probe( probedata_t *p_data )
+static int OpenDecoder( vlc_object_t *p_this )
 {
-    return ( p_data->i_type == LPCM_AUDIO_ES ) ? 100 : 0;
+    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
+
+    if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m') )
+    {   
+        return VLC_EGENERIC;
+    }
+    
+    p_fifo->pf_run = RunDecoder;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * decoder_Run: the lpcm decoder
+ * RunDecoder: the lpcm decoder
  *****************************************************************************/
-static int decoder_Run( decoder_config_t * p_config )
+static int RunDecoder( decoder_fifo_t * p_fifo )
 {
     lpcmdec_thread_t *   p_lpcmdec;
 
-    intf_DbgMsg("lpcm_adec debug: thread launched, initializing.");
-    
     /* Allocate the memory needed to store the thread's structure */
     if( (p_lpcmdec = (lpcmdec_thread_t *)malloc (sizeof(lpcmdec_thread_t)) )
             == NULL) 
     {
-        intf_ErrMsg( "LPCM : error : cannot create lpcmdec_thread_t" );
-        DecoderError( p_config->p_decoder_fifo );
+        msg_Err( p_fifo, "out of memory" );
+        DecoderError( p_fifo );
         return( -1 );
     }
 
     /*
      * Initialize the thread properties
      */
-    p_lpcmdec->p_config = p_config;
-    p_lpcmdec->p_fifo = p_config->p_decoder_fifo;
+    p_lpcmdec->p_fifo = p_fifo;
 
     if( InitThread( p_lpcmdec ) )
     {
-        DecoderError( p_config->p_decoder_fifo );
+        DecoderError( p_fifo );
         free( p_lpcmdec );
         return( -1 );
     }
 
-    intf_DbgMsg( "LPCM Debug: lpcm decoder thread %p initialized\n", 
-                 p_lpcmdec );
-    
     /* lpcm decoder thread's main loop */
     while ((!p_lpcmdec->p_fifo->b_die) && (!p_lpcmdec->p_fifo->b_error))
     {
@@ -146,12 +126,13 @@ static int InitThread (lpcmdec_thread_t * p_lpcmdec)
 {
 
     /* Init the BitStream */
-    InitBitstream( &p_lpcmdec->bit_stream, p_lpcmdec->p_config->p_decoder_fifo,
+    InitBitstream( &p_lpcmdec->bit_stream, p_lpcmdec->p_fifo,
                    NULL, NULL);
 
     /* Creating the audio output fifo */
-    p_lpcmdec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 48000,
-                                            0, LPCMDEC_FRAME_SIZE/2, NULL  );
+    p_lpcmdec->p_aout_fifo =
+                aout_CreateFifo( p_lpcmdec->p_fifo, AOUT_FIFO_PCM,
+                                 2, 48000, LPCMDEC_FRAME_SIZE / 2, NULL  );
     if ( p_lpcmdec->p_aout_fifo == NULL )
     {
         return( -1 );
@@ -164,23 +145,24 @@ static int InitThread (lpcmdec_thread_t * p_lpcmdec)
  *****************************************************************************/
 void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
 {
-    byte_t * buffer,p_temp[LPCMDEC_FRAME_SIZE];
-    int i_loop;
-    byte_t byte1, byte2;
+    byte_t *    buffer,p_temp[LPCMDEC_FRAME_SIZE];
+    vlc_bool_t  b_sync;
+    int         i_loop;
 
     CurrentPTS( &p_lpcmdec->bit_stream,
-        &p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->l_end_frame],
+        &p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame],
         NULL );
-    if( !p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->l_end_frame] )
+    if( !p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] )
     {
-        p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->l_end_frame] =
+        p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] =
             LAST_MDATE;
     }
 
     buffer = ((byte_t *)p_lpcmdec->p_aout_fifo->buffer) + 
-              (p_lpcmdec->p_aout_fifo->l_end_frame * LPCMDEC_FRAME_SIZE);
+              (p_lpcmdec->p_aout_fifo->i_end_frame * LPCMDEC_FRAME_SIZE);
 
     RemoveBits32(&p_lpcmdec->bit_stream);
+#if 0
     byte1 = GetBits(&p_lpcmdec->bit_stream, 8) ;
     byte2 = GetBits(&p_lpcmdec->bit_stream, 8) ;
     
@@ -194,9 +176,23 @@ void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
         byte1 = byte2;
         byte2 = GetBits(&p_lpcmdec->bit_stream, 8);
     }
+#else
+    b_sync = 0;
+    while( ( !p_lpcmdec->p_fifo->b_die ) &&
+           ( !p_lpcmdec->p_fifo->b_error ) &&
+           ( !b_sync ) )
+    {
+        while( ( !p_lpcmdec->p_fifo->b_die ) &&
+               ( !p_lpcmdec->p_fifo->b_error ) &&
+               ( GetBits( &p_lpcmdec->bit_stream, 8 ) != 0x01 ) );
+        b_sync = ( ShowBits( &p_lpcmdec->bit_stream, 8 ) == 0x80 );
+    }
+    RemoveBits( &p_lpcmdec->bit_stream, 8 );
+#endif
     
     GetChunk( &p_lpcmdec->bit_stream, p_temp, LPCMDEC_FRAME_SIZE);
-    
+    if( p_lpcmdec->p_fifo->b_die || p_lpcmdec->p_fifo->b_error ) return;
+
     for( i_loop = 0; i_loop < LPCMDEC_FRAME_SIZE/2; i_loop++ )
     {
         buffer[2*i_loop]=p_temp[2*i_loop+1];
@@ -204,13 +200,10 @@ void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
     }
     
     vlc_mutex_lock (&p_lpcmdec->p_aout_fifo->data_lock);
-    p_lpcmdec->p_aout_fifo->l_end_frame = 
-        (p_lpcmdec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
+    p_lpcmdec->p_aout_fifo->i_end_frame = 
+        (p_lpcmdec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
     vlc_cond_signal (&p_lpcmdec->p_aout_fifo->data_wait);
     vlc_mutex_unlock (&p_lpcmdec->p_aout_fifo->data_lock);
-    
-    intf_DbgMsg( "LPCM Debug: %x", *buffer );
-
 }
 
 /*****************************************************************************
@@ -218,8 +211,6 @@ void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
  *****************************************************************************/
 static void EndThread( lpcmdec_thread_t * p_lpcmdec )
 {
-    intf_DbgMsg( "LPCM Debug: destroying lpcm decoder thread %p", p_lpcmdec );
-
     /* If the audio output fifo was created, we destroy it */
     if( p_lpcmdec->p_aout_fifo != NULL ) 
     {
@@ -233,6 +224,4 @@ static void EndThread( lpcmdec_thread_t * p_lpcmdec )
 
     /* Destroy descriptor */
     free( p_lpcmdec );
-
-    intf_DbgMsg( "LPCM Debug: lpcm decoder thread %p destroyed", p_lpcmdec );
 }