]> git.sesse.net Git - vlc/blobdiff - modules/codec/lpcm.c
* spelling error in description string
[vlc] / modules / codec / lpcm.c
index 8f3da92ee0cf1acf128d4956751476eae95b4c89..a73f1d2449802c21575eb319b0f711e9e849016a 100644 (file)
@@ -2,7 +2,7 @@
  * lpcm.c: lpcm decoder module
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: lpcm.c,v 1.12 2003/03/03 14:19:09 massiot Exp $
+ * $Id: lpcm.c,v 1.15 2003/03/31 22:39:28 massiot Exp $
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *          Henri Fallon <henri@videolan.org>
 #   include <unistd.h>                                           /* getpid() */
 #endif
 
-/* DVD PES size (2048) - 40 bytes (headers) */
-#define LPCM_FRAME_LENGTH 2008
-
 /*****************************************************************************
  * dec_thread_t : lpcm decoder thread descriptor
  *****************************************************************************/
 typedef struct dec_thread_t
 {
-    /*
-     * Thread properties
-     */
-    vlc_thread_t        thread_id;                /* id for thread functions */
-
     /*
      * Input properties
      */
     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
-    bit_stream_t        bit_stream;
-    int                 sync_ptr;         /* sync ptr from lpcm magic header */
 
     /*
      * Output properties
@@ -82,6 +72,8 @@ typedef struct dec_thread_t
  * - start code (8 bits) == 0x80
  */
 
+#define LPCM_HEADER_LEN 6
+
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -125,7 +117,7 @@ static int RunDecoder( decoder_fifo_t * p_fifo )
     dec_thread_t *   p_dec;
 
     /* Allocate the memory needed to store the thread's structure */
-    if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
+    if( (p_dec = (dec_thread_t *)mallocsizeof(dec_thread_t)) )
             == NULL) 
     {
         msg_Err( p_fifo, "out of memory" );
@@ -136,16 +128,6 @@ static int RunDecoder( decoder_fifo_t * p_fifo )
     /* Initialize the thread properties */
     p_dec->p_fifo = p_fifo;
 
-    /* Init the bitstream */
-    if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
-                       NULL, NULL ) != VLC_SUCCESS )
-    {
-        msg_Err( p_fifo, "cannot initialize bitstream" );
-        DecoderError( p_fifo );
-        EndThread( p_dec );
-        return -1;
-    }
-   
     p_dec->output_format.i_format = VLC_FOURCC('s','1','6','b');
     p_dec->p_aout = NULL;
     p_dec->p_aout_input = NULL;
@@ -173,30 +155,61 @@ static int RunDecoder( decoder_fifo_t * p_fifo )
  *****************************************************************************/
 static void DecodeFrame( dec_thread_t * p_dec )
 {
+    pes_packet_t *     p_pes;
+    data_packet_t *    p_data;
     aout_buffer_t *    p_buffer;
+    void *             p_dest;
     mtime_t            i_pts;
     uint8_t            i_header;
-    unsigned int       i_rate, i_original_channels;
+    unsigned int       i_rate = 0, i_original_channels = 0, i_size;
+    int                i;
 
-    /* Look for sync word - should be 0xXX80 */
-    RealignBits( &p_dec->bit_stream );
-    while ( (ShowBits( &p_dec->bit_stream, 16 ) & 0xc8ff) != 0x0080 && 
-             (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
+    input_ExtractPES( p_dec->p_fifo, &p_pes );
+    if ( !p_pes )
     {
-        RemoveBits( &p_dec->bit_stream, 8 );
+        p_dec->p_fifo->b_error = 1;
+        return;
+    }
+
+    /* Compute the size of the PES - i_pes_size includes the PES header. */
+    p_data = p_pes->p_first;
+    i_size = 0;
+    while ( p_data != NULL )
+    {
+        i_size += p_data->p_payload_end - p_data->p_payload_start;
+        p_data = p_data->p_next;
+    }
+    if ( i_size < LPCM_HEADER_LEN )
+    {
+        msg_Err(p_dec->p_fifo, "PES packet is too short");
+        input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
+        return;
     }
-    if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) return;
 
-    NextPTS( &p_dec->bit_stream, &i_pts, NULL );
+    i_pts = p_pes->i_pts;
     if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
     {
         aout_DateSet( &p_dec->end_date, i_pts );
     }
-    
+
+    p_data = p_pes->p_first;
+    /* It necessarily contains one byte. */
     /* Get LPCM header. */
-    i_header = GetBits( &p_dec->bit_stream, 16 ) >> 8;
 
-    switch ( i_header >> 4 )
+    /* Drop the first four bytes. */
+    for ( i = 0; i < 4; i++ )
+    {
+        if ( p_data->p_payload_end == p_data->p_payload_start )
+        {
+            p_data = p_data->p_next;
+        }
+        p_data->p_payload_start++;
+    }
+
+    i_header = p_data->p_payload_start[0];
+    p_data->p_payload_start++;
+
+    switch ( (i_header >> 4) & 0x3 )
     {
     case 0:
         i_rate = 48000;
@@ -253,6 +266,19 @@ static void DecodeFrame( dec_thread_t * p_dec )
         break;
     }
 
+    /* Check frame sync and drop it. */
+    if ( p_data->p_payload_end == p_data->p_payload_start )
+    {
+        p_data = p_data->p_next;
+    }
+    if ( p_data->p_payload_start[0] != 0x80 )
+    {
+        msg_Warn(p_dec->p_fifo, "no frame sync");
+        input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
+        return;
+    }
+    p_data->p_payload_start++;
+
     if( (p_dec->p_aout_input != NULL) &&
         ( (p_dec->output_format.i_rate != i_rate)
             || (p_dec->output_format.i_original_channels
@@ -271,6 +297,7 @@ static void DecodeFrame( dec_thread_t * p_dec )
         p_dec->output_format.i_physical_channels
                    = i_original_channels & AOUT_CHAN_PHYSMASK;
         aout_DateInit( &p_dec->end_date, i_rate );
+        aout_DateSet( &p_dec->end_date, i_pts );
         p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
                                            &p_dec->p_aout,
                                            &p_dec->output_format );
@@ -278,41 +305,44 @@ static void DecodeFrame( dec_thread_t * p_dec )
         if ( p_dec->p_aout_input == NULL )
         {
             p_dec->p_fifo->b_error = 1;
+            input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
             return;
         }
     }
 
     if ( !aout_DateGet( &p_dec->end_date ) )
     {
-        byte_t p_junk[LPCM_FRAME_LENGTH];
-
         /* We've just started the stream, wait for the first PTS. */
-        GetChunk( &p_dec->bit_stream, p_junk, LPCM_FRAME_LENGTH );
+        input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
         return;
     }
 
     p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
-            LPCM_FRAME_LENGTH / p_dec->output_format.i_bytes_per_frame );
+            (i_size - LPCM_HEADER_LEN)
+                / p_dec->output_format.i_bytes_per_frame );
     
     if( p_buffer == NULL )
     {
         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
         p_dec->p_fifo->b_error = 1;
+        input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
         return;
     }
     p_buffer->start_date = aout_DateGet( &p_dec->end_date );
     p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
-            LPCM_FRAME_LENGTH / p_dec->output_format.i_bytes_per_frame );
+            (i_size - LPCM_HEADER_LEN)
+                / p_dec->output_format.i_bytes_per_frame );
 
     /* Get the whole frame. */
-    GetChunk( &p_dec->bit_stream, p_buffer->p_buffer, 
-              LPCM_FRAME_LENGTH);
-    if( p_dec->p_fifo->b_die )
+    p_dest = p_buffer->p_buffer;
+    while ( p_data != NULL )
     {
-        aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
-                              p_buffer );
-        return;
+        p_dec->p_fifo->p_vlc->pf_memcpy( p_dest, p_data->p_payload_start,
+                p_data->p_payload_end - p_data->p_payload_start );
+        p_dest += p_data->p_payload_end - p_data->p_payload_start;
+        p_data = p_data->p_next;
     }
+    input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
 
     /* Send the buffer to the aout core. */
     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
@@ -328,6 +358,5 @@ static void EndThread( dec_thread_t * p_dec )
         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
     }
 
-    CloseBitstream( &p_dec->bit_stream );
     free( p_dec );
 }