]> git.sesse.net Git - vlc/blobdiff - src/input/input_ext-dec.c
* Speed optimization in the handling of the unusual ephemer DVD subtitles.
[vlc] / src / input / input_ext-dec.c
index ced9bdd5b9676cb8c9a3ec26197576cfbd1c3863..006be270006b55bbdd18f9d44e4919c5d95d39f8 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.16 2001/05/08 00:43:57 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 );
@@ -65,6 +72,21 @@ void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo )
     p_bit_stream->fifo.buffer = 0;
     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.
+         * NB : we _will_ get aligned, because we have at most 
+         * sizeof(WORD_TYPE) - 1 bytes to store, and at least
+         * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
+        AlignWord( p_bit_stream );
+    }
 }
 
 /*****************************************************************************
@@ -147,10 +169,56 @@ 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;
+
+                    /* sizeof(WORD_TYPE) - number of bytes to trash
+                     * from the last payload */
+                    int     j;
+
+                    p_bit_stream->i_showbits_buffer = 0;
+
+                    for( j = i = 0 ; i < sizeof(WORD_TYPE) ; i++ )
+                    {
+                        if( p_bit_stream->p_byte >= p_bit_stream->p_end )
+                        {
+                            j = i;
+                            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) - j;
+                    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) );