]> git.sesse.net Git - vlc/blobdiff - src/input/input_ext-dec.c
* Coding style fixes here and there.
[vlc] / src / input / input_ext-dec.c
index bb29b1fec9951c4d26fe80f8b834c924101e9917..7be8abbed339eb52409ebf2baaa03235618e1f56 100644 (file)
@@ -2,6 +2,7 @@
  * input_ext-dec.c: services to the decoders
  *****************************************************************************
  * Copyright (C) 1998, 1999, 2000 VideoLAN
+ * $Id: input_ext-dec.c,v 1.14 2001/04/28 03:36:25 sam Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
@@ -25,6 +26,9 @@
  *****************************************************************************/
 #include "defs.h"
 
+#include <string.h>                                    /* memcpy(), memset() */
+#include <sys/types.h>                                              /* off_t */
+
 #include "config.h"
 #include "common.h"
 #include "threads.h"
 /*****************************************************************************
  * InitBitstream: initialize a bit_stream_t structure
  *****************************************************************************/
-void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo )
+void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo,
+                    void (* pf_bitstream_callback)( struct bit_stream_s *,
+                                                    boolean_t ),
+                    void * p_callback_arg )
 {
     p_bit_stream->p_decoder_fifo = p_fifo;
     p_bit_stream->pf_next_data_packet = NextDataPacket;
-    p_bit_stream->pf_bitstream_callback = NULL;
-    p_bit_stream->p_callback_arg = NULL;
+    p_bit_stream->pf_bitstream_callback = pf_bitstream_callback;
+    p_bit_stream->p_callback_arg = p_callback_arg;
 
     /* Get the first data packet. */
     vlc_mutex_lock( &p_fifo->data_lock );
@@ -66,6 +73,12 @@ void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo )
     p_bit_stream->fifo.i_available = 0;
     vlc_mutex_unlock( &p_fifo->data_lock );
 
+    /* Call back the decoder. */
+    if( p_bit_stream->pf_bitstream_callback != NULL )
+    {
+        p_bit_stream->pf_bitstream_callback( p_bit_stream, 1 );
+    }
+
     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
     {
         /* Get aligned on a word boundary.
@@ -156,10 +169,49 @@ u32 UnalignedShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
         else
         {
             p_bit_stream->pf_next_data_packet( p_bit_stream );
-            p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
-                                            << (8 * sizeof(WORD_TYPE) - 8
-                                            - p_bit_stream->fifo.i_available);
-            p_bit_stream->fifo.i_available += 8;
+
+            if( (ptrdiff_t)p_bit_stream->p_byte & (sizeof(WORD_TYPE) - 1) )
+            {
+                /* We are not aligned anymore. */
+                if( ((ptrdiff_t)p_bit_stream->p_byte
+                                    & (sizeof(WORD_TYPE) - 1)) * 8
+                        < p_bit_stream->fifo.i_available )
+                {
+                    /* We are not aligned, and won't be. Copy the first word
+                     * of the packet in a temporary buffer, and we'll see
+                     * later. */
+                    int     i;
+                    p_bit_stream->i_showbits_buffer = 0;
+
+                    for( i = 0; i < sizeof(WORD_TYPE) ; i++ )
+                    {
+                        if( p_bit_stream->p_byte >= p_bit_stream->p_end )
+                        {
+                            p_bit_stream->pf_next_data_packet( p_bit_stream );
+                        }
+                        ((byte_t *)&p_bit_stream->i_showbits_buffer)[i] =
+                            * p_bit_stream->p_byte;
+                        p_bit_stream->p_byte++;
+                    }
+
+                    /* This is kind of kludgy. */
+                    p_bit_stream->p_data->p_payload_start += sizeof(WORD_TYPE);
+                    p_bit_stream->p_byte =
+                        (byte_t *)&p_bit_stream->i_showbits_buffer;
+                    p_bit_stream->p_end =
+                        (byte_t *)&p_bit_stream->i_showbits_buffer
+                            + sizeof(WORD_TYPE);
+                    p_bit_stream->showbits_data.p_next = p_bit_stream->p_data;
+                    p_bit_stream->p_data = &p_bit_stream->showbits_data;
+                }
+                else
+                {
+                    /* We are not aligned, but we can be. */
+                    AlignWord( p_bit_stream );
+                }
+            }
+
+            return( ShowBits( p_bit_stream, i_bits ) );
         }
     }
     return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );