]> git.sesse.net Git - vlc/blobdiff - plugins/mpeg/input_ps.c
* Fixed a total breakage of decoder plugins introduced by fast_memcpy.
[vlc] / plugins / mpeg / input_ps.c
index fb5a841a9e9ff42f9a44fbe7a6753189bce6a934..7124580032363db7904bad40569fea1926fe289b 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * input_ps.c: PS demux and packet management
  *****************************************************************************
- * Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: input_ps.c,v 1.3 2001/02/08 13:52:35 massiot Exp $
+ * Copyright (C) 1998-2001 VideoLAN
+ * $Id: input_ps.c,v 1.44 2001/12/07 18:33:07 sam Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
+ *          Cyril Deguet <asmax@via.ecp.fr>
  *
  * 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
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
+#define MODULE_NAME ps
+#include "modules_inner.h"
+
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
 #include "defs.h"
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <errno.h>
 
-#include "config.h"
+#ifdef STRNCASECMP_IN_STRINGS_H
+#   include <strings.h>
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>
+#elif defined( _MSC_VER ) && defined( _WIN32 )
+#   include <io.h>
+#endif
+
+#include <fcntl.h>
+
 #include "common.h"
+#include "intf_msg.h"
 #include "threads.h"
 #include "mtime.h"
 #include "tests.h"
 
-#include "intf_msg.h"
-
-#include "main.h"
+#include "modules.h"
+#include "modules_export.h"
 
 #include "stream_control.h"
 #include "input_ext-intf.h"
 #include "input_ext-dec.h"
-
-#include "input.h"
+#include "input_ext-plugins.h"
 
 #include "input_ps.h"
-#include "mpeg_system.h"
 
 #include "debug.h"
 
-#include "modules.h"
+/*****************************************************************************
+ * fseeko: fseeko replacement for BSDI.
+ *****************************************************************************/
+#ifdef __bsdi__
+int    __sfseek __P(( FILE *, fpos_t, int ));
+fpos_t __sftell __P(( FILE * ));
+
+static __inline__ off_t fseeko( FILE *p_file, off_t i_offset, int i_pos )
+{
+    return __sfseek( p_file, i_offset, i_pos );
+}
+#endif
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int  PSProbe     ( probedata_t * );
-static int  PSRead      ( struct input_thread_s *,
-                          data_packet_t * p_packets[INPUT_READ_ONCE] );
-static void PSInit      ( struct input_thread_s * );
-static void PSEnd       ( struct input_thread_s * );
-static void PSSeek      ( struct input_thread_s *, off_t );
+static int  PSProbe         ( probedata_t * );
+static int  PSRead          ( struct input_thread_s *,
+                             data_packet_t * p_packets[INPUT_READ_ONCE] );
+static void PSInit          ( struct input_thread_s * );
+static void PSEnd           ( struct input_thread_s * );
+static int  PSSetProgram    ( struct input_thread_s * , pgrm_descriptor_t * );
+static void PSSeek          ( struct input_thread_s *, off_t );
 static struct pes_packet_s *  NewPES    ( void * );
 static struct data_packet_s * NewPacket ( void *, size_t );
-static void DeletePacket( void *, struct data_packet_s * );
-static void DeletePES   ( void *, struct pes_packet_s * );
+static void DeletePacket    ( void *, struct data_packet_s * );
+static void DeletePES       ( void *, struct pes_packet_s * );
 
 /*****************************************************************************
  * Functions exported as capabilities. They are declared as static so that
  * we don't pollute the namespace too much.
  *****************************************************************************/
-void input_getfunctions( function_list_t * p_function_list )
+void _M( input_getfunctions )( function_list_t * p_function_list )
 {
 #define input p_function_list->functions.input
     p_function_list->pf_probe = PSProbe;
     input.pf_init             = PSInit;
-    input.pf_open             = input_FileOpen;
-    input.pf_close            = input_FileClose;
+    input.pf_open             = NULL;
+    input.pf_close            = NULL;
     input.pf_end              = PSEnd;
+    input.pf_init_bit_stream  = InitBitstream;
+    input.pf_set_area         = NULL;
+    input.pf_set_program      = PSSetProgram;
     input.pf_read             = PSRead;
     input.pf_demux            = input_DemuxPS;
     input.pf_new_packet       = NewPacket;
@@ -99,12 +131,26 @@ void input_getfunctions( function_list_t * p_function_list )
  *****************************************************************************/
 static int PSProbe( probedata_t *p_data )
 {
+    input_thread_t * p_input = (input_thread_t *)p_data;
+
+    char * psz_name = p_input->p_source;
+    int i_score = 10;
+
     if( TestMethod( INPUT_METHOD_VAR, "ps" ) )
     {
         return( 999 );
     }
 
-    return 10;
+    if( ( strlen(psz_name) > 5 ) && (!strncasecmp( psz_name, "file:", 5 )
+                                      || !strncasecmp( psz_name, "http:", 5 )) )
+    {
+        /* If the user specified "file:" or "http:" then it's probably a
+         * PS file */
+        i_score = 100;
+        psz_name += 5;
+    }
+
+    return( i_score );
 }
 
 /*****************************************************************************
@@ -112,37 +158,94 @@ static int PSProbe( probedata_t *p_data )
  *****************************************************************************/
 static void PSInit( input_thread_t * p_input )
 {
-    thread_ps_data_t *  p_method;
+    packet_cache_t *    p_packet_cache;
 
-    if( (p_method =
-         (thread_ps_data_t *)malloc( sizeof(thread_ps_data_t) )) == NULL )
+    /* creates the packet cache structure */
+    p_packet_cache = malloc( sizeof(packet_cache_t) );
+    if ( p_packet_cache == NULL )
     {
         intf_ErrMsg( "Out of memory" );
         p_input->b_error = 1;
         return;
     }
-
-    p_input->p_plugin_data = (void *)p_method;
-    p_input->p_method_data = NULL;
-
-    /* Re-open the socket as a buffered FILE stream */
-    if( (p_method->stream = fdopen( p_input->i_handle, "r" )) == NULL )
+    p_input->p_method_data = (void *)p_packet_cache;
+
+    /* Initialize packet cache mutex */
+    vlc_mutex_init( &p_packet_cache->lock );
+    
+    /* allocates the data cache */
+    p_packet_cache->data.p_stack = malloc( DATA_CACHE_SIZE * 
+        sizeof(data_packet_t*) );
+    if ( p_packet_cache->data.p_stack == NULL )
     {
-        intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
+        intf_ErrMsg( "Out of memory" );
         p_input->b_error = 1;
         return;
     }
-    fseek( p_method->stream, 0, SEEK_SET );
+    p_packet_cache->data.l_index = 0;
+    
+    /* allocates the PES cache */
+    p_packet_cache->pes.p_stack = malloc( PES_CACHE_SIZE * 
+        sizeof(pes_packet_t*) );
+    if ( p_packet_cache->pes.p_stack == NULL )
+    {
+        intf_ErrMsg( "Out of memory" );
+        p_input->b_error = 1;
+        return;
+    }
+    p_packet_cache->pes.l_index = 0;
+    
+    /* allocates the small buffer cache */
+    p_packet_cache->smallbuffer.p_stack = malloc( SMALL_CACHE_SIZE * 
+        sizeof(packet_buffer_t) );
+    if ( p_packet_cache->smallbuffer.p_stack == NULL )
+    {
+        intf_ErrMsg( "Out of memory" );
+        p_input->b_error = 1;
+        return;
+    }
+    p_packet_cache->smallbuffer.l_index = 0;
+    
+    /* allocates the large buffer cache */
+    p_packet_cache->largebuffer.p_stack = malloc( LARGE_CACHE_SIZE * 
+        sizeof(packet_buffer_t) );
+    if ( p_packet_cache->largebuffer.p_stack == NULL )
+    {
+        intf_ErrMsg( "Out of memory" );
+        p_input->b_error = 1;
+        return;
+    }
+    p_packet_cache->largebuffer.l_index = 0;
+    
+    if( p_input->p_stream == NULL )
+    {
+        /* Re-open the socket as a buffered FILE stream */
+        p_input->p_stream = fdopen( p_input->i_handle, "r" );
+
+        if( p_input->p_stream == NULL )
+        {
+            intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
+            p_input->b_error = 1;
+            return;
+        }
+    }
 
     /* FIXME : detect if InitStream failed */
     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
-
+    
+    p_input->stream.p_selected_program = 
+            p_input->stream.pp_programs[0] ;
+    p_input->stream.p_new_program = 
+            p_input->stream.pp_programs[0] ;
+    
     if( p_input->stream.b_seekable )
     {
         stream_ps_data_t * p_demux_data =
              (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
 
+        rewind( p_input->p_stream );
+
         /* Pre-parse the stream to gather stream_descriptor_t. */
         p_input->stream.pp_programs[0]->b_is_ok = 0;
         p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
@@ -176,14 +279,17 @@ static void PSInit( input_thread_t * p_input )
             }
 
             /* File too big. */
-            if( p_input->stream.i_tell > INPUT_PREPARSE_LENGTH )
+            if( p_input->stream.p_selected_area->i_tell >
+                                                    INPUT_PREPARSE_LENGTH )
             {
                 break;
             }
         }
-        fseek( p_method->stream, 0, SEEK_SET );
+        rewind( p_input->p_stream );
         vlc_mutex_lock( &p_input->stream.stream_lock );
-        p_input->stream.i_tell = 0;
+
+        p_input->stream.p_selected_area->i_tell = 0;
+
         if( p_demux_data->b_has_PSM )
         {
             /* (The PSM decoder will care about spawning the decoders) */
@@ -211,12 +317,12 @@ static void PSInit( input_thread_t * p_input )
 
                     case MPEG1_AUDIO_ES:
                     case MPEG2_AUDIO_ES:
-                        if( main_GetIntVariable( INPUT_DVD_CHANNEL_VAR, 0 )
+                        if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
                                 == (p_es->i_id & 0x1F) )
-                        switch( main_GetIntVariable( INPUT_DVD_AUDIO_VAR, 0 ) )
+                        switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
                         {
                         case 0:
-                            main_PutIntVariable( INPUT_DVD_CHANNEL_VAR,
+                            main_PutIntVariable( INPUT_AUDIO_VAR,
                                                  REQUESTED_MPEG );
                         case REQUESTED_MPEG:
                             input_SelectES( p_input, p_es );
@@ -224,12 +330,12 @@ static void PSInit( input_thread_t * p_input )
                         break;
 
                     case AC3_AUDIO_ES:
-                        if( main_GetIntVariable( INPUT_DVD_CHANNEL_VAR, 0 )
+                        if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
                                 == ((p_es->i_id & 0xF00) >> 8) )
-                        switch( main_GetIntVariable( INPUT_DVD_AUDIO_VAR, 0 ) )
+                        switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
                         {
                         case 0:
-                            main_PutIntVariable( INPUT_DVD_CHANNEL_VAR,
+                            main_PutIntVariable( INPUT_AUDIO_VAR,
                                                  REQUESTED_AC3 );
                         case REQUESTED_AC3:
                             input_SelectES( p_input, p_es );
@@ -237,7 +343,7 @@ static void PSInit( input_thread_t * p_input )
                         break;
 
                     case DVD_SPU_ES:
-                        if( main_GetIntVariable( INPUT_DVD_SUBTITLE_VAR, -1 )
+                        if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
                                 == ((p_es->i_id & 0x1F00) >> 8) )
                         {
                             input_SelectES( p_input, p_es );
@@ -245,22 +351,33 @@ static void PSInit( input_thread_t * p_input )
                         break;
 
                     case LPCM_AUDIO_ES:
-                        /* FIXME ! */
+                        if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
+                                == ((p_es->i_id & 0x1F00) >> 8) )
+                        switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
+                        {
+                        case 0:
+                            main_PutIntVariable( INPUT_AUDIO_VAR,
+                                                 REQUESTED_LPCM );
+                        case REQUESTED_LPCM:
+                            input_SelectES( p_input, p_es );
+                        }
                         break;
                 }
             }
                     
         }
 #endif
-#ifdef STATS
-        input_DumpStream( p_input );
-#endif
+        if( p_main->b_stats )
+        {
+            input_DumpStream( p_input );
+        }
         vlc_mutex_unlock( &p_input->stream.stream_lock );
     }
     else
     {
         /* The programs will be added when we read them. */
         vlc_mutex_lock( &p_input->stream.stream_lock );
+        p_input->stream.i_method = INPUT_METHOD_FILE;
         p_input->stream.pp_programs[0]->b_is_ok = 0;
         vlc_mutex_unlock( &p_input->stream.stream_lock );
     }
@@ -271,8 +388,22 @@ static void PSInit( input_thread_t * p_input )
  *****************************************************************************/
 static void PSEnd( input_thread_t * p_input )
 {
-    free( p_input->stream.p_demux_data );
-    free( p_input->p_plugin_data );
+#define p_packet_cache ((packet_cache_t *)p_input->p_method_data)
+
+    vlc_mutex_destroy( &p_packet_cache->lock );
+
+    if( p_packet_cache->data.p_stack )
+        free( p_packet_cache->data.p_stack );
+    if( p_packet_cache->pes.p_stack )
+        free( p_packet_cache->pes.p_stack );
+    if( p_packet_cache->smallbuffer.p_stack )
+        free( p_packet_cache->smallbuffer.p_stack );
+    if( p_packet_cache->largebuffer.p_stack )
+        free( p_packet_cache->largebuffer.p_stack );
+
+#undef p_packet_cache
+
+    free( p_input->p_method_data );
 }
 
 /*****************************************************************************
@@ -281,25 +412,23 @@ static void PSEnd( input_thread_t * p_input )
 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
                                 size_t i_len )
 {
-    thread_ps_data_t *  p_method;
     int                 i_error;
 
-    p_method = (thread_ps_data_t *)p_input->p_plugin_data;
-    while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
+    while( fread( p_buffer, i_len, 1, p_input->p_stream ) != 1 )
     {
-        if( feof( p_method->stream ) )
+        if( feof( p_input->p_stream ) )
         {
             return( 1 );
         }
 
-        if( (i_error = ferror( p_method->stream )) )
+        if( (i_error = ferror( p_input->p_stream )) )
         {
             intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
             return( -1 );
         }
     }
     vlc_mutex_lock( &p_input->stream.stream_lock );
-    p_input->stream.i_tell += i_len;
+    p_input->stream.p_selected_area->i_tell += i_len;
     vlc_mutex_unlock( &p_input->stream.stream_lock );
     return( 0 );
 }
@@ -317,15 +446,12 @@ static int PSRead( input_thread_t * p_input,
     data_packet_t *     p_data;
     size_t              i_packet_size;
     int                 i_packet, i_error;
-    thread_ps_data_t *  p_method;
-
-    p_method = (thread_ps_data_t *)p_input->p_plugin_data;
 
     memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
     for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
     {
         /* Read what we believe to be a packet header. */
-        if( (i_error = SafeRead( p_input, p_header, 6 )) )
+        if( (i_error = SafeRead( p_input, p_header, 4 )) )
         {
             return( i_error );
         }
@@ -342,13 +468,13 @@ static int PSRead( input_thread_t * p_input,
                 /* It is common for MPEG-1 streams to pad with zeros
                  * (although it is forbidden by the recommendation), so
                  * don't bother everybody in this case. */
-                intf_WarnMsg( 1, "Garbage at input (%x)", i_startcode );
+                intf_WarnMsg( 3, "Garbage at input (%.8x)", i_startcode );
             }
 
             while( (i_startcode & 0xFFFFFF00) != 0x100L )
             {
                 i_startcode <<= 8;
-                if( (i_dummy = getc( p_method->stream )) != EOF )
+                if( (i_dummy = getc( p_input->p_stream )) != EOF )
                 {
                     i_startcode |= i_dummy;
                 }
@@ -359,68 +485,88 @@ static int PSRead( input_thread_t * p_input,
             }
             /* Packet found. */
             *(u32 *)p_header = U32_AT(&i_startcode);
-            if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
-            {
-                return( i_error );
-            }
         }
 
-        if( U32_AT(p_header) != 0x1BA )
+        /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
+        if( U32_AT(p_header) != 0x1B9 )
         {
-            /* That's the case for all packets, except pack header. */
-            i_packet_size = U16_AT(&p_header[4]);
-        }
-        else
-        {
-            /* Pack header. */
-            if( (p_header[4] & 0xC0) == 0x40 )
+            /* The packet is at least 6 bytes long. */
+            if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
             {
-                /* MPEG-2 */
-                i_packet_size = 8;
+                return( i_error );
             }
-            else if( (p_header[4] & 0xF0) == 0x20 )
+
+            if( U32_AT(p_header) != 0x1BA )
             {
-                /* MPEG-1 */
-                i_packet_size = 6;
+                /* That's the case for all packets, except pack header. */
+                i_packet_size = U16_AT(&p_header[4]);
             }
             else
             {
-                intf_ErrMsg( "Unable to determine stream type" );
-                return( -1 );
+                /* Pack header. */
+                if( (p_header[4] & 0xC0) == 0x40 )
+                {
+                    /* MPEG-2 */
+                    i_packet_size = 8;
+                }
+                else if( (p_header[4] & 0xF0) == 0x20 )
+                {
+                    /* MPEG-1 */
+                    i_packet_size = 6;
+                }
+                else
+                {
+                    intf_ErrMsg( "Unable to determine stream type" );
+                    return( -1 );
+                }
             }
         }
+        else
+        {
+            /* System End Code */
+            i_packet_size = -2;
+        }
 
         /* Fetch a packet of the appropriate size. */
-        if( (p_data = NewPacket( p_input, i_packet_size + 6 )) == NULL )
+        p_data = NewPacket( p_input->p_method_data, i_packet_size + 6 );
+        if( p_data == NULL )
         {
             intf_ErrMsg( "Out of memory" );
             return( -1 );
         }
 
-        /* Copy the header we already read. */
-        memcpy( p_data->p_buffer, p_header, 6 );
-
-        /* Read the remaining of the packet. */
-        if( i_packet_size && (i_error =
-                SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
+        if( U32_AT(p_header) != 0x1B9 )
         {
-            return( i_error );
-        }
+            /* Copy the header we already read. */
+            memcpy( p_data->p_buffer, p_header, 6 );
 
-        /* In MPEG-2 pack headers we still have to read stuffing bytes. */
-        if( U32_AT(p_header) == 0x1BA )
-        {
-            if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
+            /* Read the remaining of the packet. */
+            if( i_packet_size && (i_error =
+                    SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
             {
-                /* MPEG-2 stuffing bytes */
-                byte_t      p_garbage[8];
-                if( (i_error = SafeRead( p_input, p_garbage,
-                                         p_data->p_buffer[13] & 0x7)) )
+                return( i_error );
+            }
+
+            /* In MPEG-2 pack headers we still have to read stuffing bytes. */
+            if( U32_AT(p_header) == 0x1BA )
+            {
+                if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
                 {
-                    return( i_error );
+                    /* MPEG-2 stuffing bytes */
+                    byte_t      p_garbage[8];
+                    if( (i_error = SafeRead( p_input, p_garbage,
+                                             p_data->p_buffer[13] & 0x7)) )
+                    {
+                        return( i_error );
+                    }
                 }
             }
         }
+        else
+        {
+            /* Copy the small header. */
+            memcpy( p_data->p_buffer, p_header, 4 );
+        }
 
         /* Give the packet to the other input stages. */
         pp_packets[i_packet] = p_data;
@@ -429,115 +575,381 @@ static int PSRead( input_thread_t * p_input,
     return( 0 );
 }
 
+/*****************************************************************************
+ * PSSetProgram: Does nothing since a PS Stream is mono-program
+ *****************************************************************************/
+static int PSSetProgram( input_thread_t * p_input, 
+            pgrm_descriptor_t * p_program)
+{
+    return( 0 );
+}
 /*****************************************************************************
  * PSSeek: changes the stream position indicator
  *****************************************************************************/
 static void PSSeek( input_thread_t * p_input, off_t i_position )
 {
-    thread_ps_data_t *  p_method;
-
-    p_method = (thread_ps_data_t *)p_input->p_plugin_data;
-
     /* A little bourrin but should work for a while --Meuuh */
-    fseek( p_method->stream, i_position, SEEK_SET );
+#if defined( WIN32 ) || defined( SYS_GNU0_2 )
+    fseek( p_input->p_stream, (long)i_position, SEEK_SET );
+#else
+    fseeko( p_input->p_stream, i_position, SEEK_SET );
+#endif
 
-    p_input->stream.i_tell = i_position;
+    p_input->stream.p_selected_area->i_tell = i_position;
 }
 
 /*
  * Packet management utilities
  */
 
+
 /*****************************************************************************
  * NewPacket: allocates a data packet
  *****************************************************************************/
-static struct data_packet_s * NewPacket( void * p_garbage,
-                                         size_t i_size )
-{
-    data_packet_t * p_data;
+static struct data_packet_s * NewPacket( void * p_packet_cache,
+                                         size_t l_size )
+{ 
+    packet_cache_t *   p_cache;
+    data_packet_t *    p_data;
+    long               l_index;
 
-    /* Safety check */
-    if( i_size > INPUT_MAX_PACKET_SIZE )
+    p_cache = (packet_cache_t *)p_packet_cache;
+
+#ifdef DEBUG
+    if ( p_cache == NULL )
     {
-        intf_ErrMsg( "Packet too big (%d)", i_size );
+        intf_ErrMsg( "PPacket cache not initialized" );
         return NULL;
     }
+#endif
 
-    if( (p_data = (data_packet_t *)malloc( sizeof(data_packet_t) )) == NULL )
+    /* Safety check */
+    if( l_size > INPUT_MAX_PACKET_SIZE )
     {
-        intf_DbgMsg( "Out of memory" );
+        intf_ErrMsg( "Packet too big (%d)", l_size );
         return NULL;
     }
 
-    if( (p_data->p_buffer = (byte_t *)malloc( i_size )) == NULL )
+    vlc_mutex_lock( &p_cache->lock );
+
+    /* Checks whether the data cache is empty */
+    if( p_cache->data.l_index == 0 )
     {
-        intf_DbgMsg( "Out of memory" );
-        free( p_data );
-        return NULL;
+        /* Allocates a new packet */
+        p_data = malloc( sizeof(data_packet_t) );
+        if( p_data == NULL )
+        {
+            intf_ErrMsg( "Out of memory" );
+            vlc_mutex_unlock( &p_cache->lock );
+            return NULL;
+        }
+#ifdef TRACE_INPUT
+        intf_DbgMsg( "PS input: data packet allocated" );
+#endif
+    }
+    else
+    {
+        /* Takes the packet out from the cache */
+        if( (p_data = p_cache->data.p_stack[ -- p_cache->data.l_index ]) 
+            == NULL )
+        {
+            intf_ErrMsg( "NULL packet in the data cache" );
+            vlc_mutex_unlock( &p_cache->lock );
+            return NULL;
+        }
     }
+    
+    if( l_size < MAX_SMALL_SIZE )
+    {
+        /* Small buffer */  
+   
+        /* Checks whether the buffer cache is empty */
+        if( p_cache->smallbuffer.l_index == 0 )
+        {
+            /* Allocates a new packet */
+            p_data->p_buffer = malloc( l_size );
+            if( p_data->p_buffer == NULL )
+            {
+                intf_DbgMsg( "Out of memory" );
+                free( p_data );
+                vlc_mutex_unlock( &p_cache->lock );
+                return NULL;
+            }
+#ifdef TRACE_INPUT
+            intf_DbgMsg( "PS input: small buffer allocated" );
+#endif
+            p_data->l_size = l_size;
+        }
+        else
+        {
+            /* Takes the packet out from the cache */
+            l_index = -- p_cache->smallbuffer.l_index;    
+            if( (p_data->p_buffer = p_cache->smallbuffer.p_stack[l_index].p_data)
+                == NULL )
+            {
+                intf_ErrMsg( "NULL packet in the small buffer cache" );
+                free( p_data );
+                vlc_mutex_unlock( &p_cache->lock );
+                return NULL;
+            }
+            /* Reallocates the packet if it is too small or too large */
+            if( p_cache->smallbuffer.p_stack[l_index].l_size < l_size ||
+                p_cache->smallbuffer.p_stack[l_index].l_size > 2*l_size )
+            {
+                p_data->p_buffer = realloc( p_data->p_buffer, l_size );
+                p_data->l_size = l_size;
+            }
+            else
+            {
+                p_data->l_size = p_cache->smallbuffer.p_stack[l_index].l_size;
+            }
+        }
+    }
+    else
+    {
+        /* Large buffer */  
+   
+        /* Checks whether the buffer cache is empty */
+        if( p_cache->largebuffer.l_index == 0 )
+        {
+            /* Allocates a new packet */
+            p_data->p_buffer = malloc( l_size );
+            if ( p_data->p_buffer == NULL )
+            {
+                intf_ErrMsg( "Out of memory" );
+                free( p_data );
+                vlc_mutex_unlock( &p_cache->lock );
+                return NULL;
+            }
+#ifdef TRACE_INPUT
+            intf_DbgMsg( "PS input: large buffer allocated" );
+#endif
+            p_data->l_size = l_size;
+        }
+        else
+        {
+            /* Takes the packet out from the cache */
+            l_index = -- p_cache->largebuffer.l_index;    
+            p_data->p_buffer = p_cache->largebuffer.p_stack[l_index].p_data;
+            if( p_data->p_buffer == NULL )
+            {
+                intf_ErrMsg( "NULL packet in the small buffer cache" );
+                free( p_data );
+                vlc_mutex_unlock( &p_cache->lock );
+                return NULL;
+            }
+            /* Reallocates the packet if it is too small or too large */
+            if( p_cache->largebuffer.p_stack[l_index].l_size < l_size ||
+                p_cache->largebuffer.p_stack[l_index].l_size > 2*l_size )
+            {
+                p_data->p_buffer = realloc( p_data->p_buffer, l_size );
+                p_data->l_size = l_size;
+            }
+            else
+            {
+                p_data->l_size = p_cache->largebuffer.p_stack[l_index].l_size;
+            }
+        }
+    }
+
+    vlc_mutex_unlock( &p_cache->lock );
 
     /* Initialize data */
     p_data->p_next = NULL;
     p_data->b_discard_payload = 0;
-
     p_data->p_payload_start = p_data->p_buffer;
-    p_data->p_payload_end = p_data->p_buffer + i_size;
+    p_data->p_payload_end = p_data->p_buffer + l_size;
 
     return( p_data );
+
 }
 
+
 /*****************************************************************************
  * NewPES: allocates a pes packet
  *****************************************************************************/
-static pes_packet_t * NewPES( void * p_garbage )
+static pes_packet_t * NewPES( void * p_packet_cache )
 {
-    pes_packet_t * p_pes;
+    packet_cache_t *   p_cache;
+    pes_packet_t *     p_pes;
+
+    p_cache = (packet_cache_t *)p_packet_cache;
 
-    if( (p_pes = (pes_packet_t *)malloc( sizeof(pes_packet_t) )) == NULL )
+#ifdef DEBUG
+    if ( p_cache == NULL )
     {
-        intf_DbgMsg( "Out of memory" );
+        intf_ErrMsg( "Packet cache not initialized" );
         return NULL;
     }
+#endif
+
+    vlc_mutex_lock( &p_cache->lock );
+
+    /* Checks whether the PES cache is empty */
+    if( p_cache->pes.l_index == 0 )
+    {
+        /* Allocates a new packet */
+        p_pes = malloc( sizeof(pes_packet_t) );
+        if( p_pes == NULL )
+        {
+            intf_DbgMsg( "Out of memory" );
+            vlc_mutex_unlock( &p_cache->lock );
+            return NULL;
+        }
+#ifdef TRACE_INPUT
+        intf_DbgMsg( "PS input: PES packet allocated" );
+#endif
+    }
+    else
+    {
+        /* Takes the packet out from the cache */
+        p_pes = p_cache->pes.p_stack[ -- p_cache->pes.l_index ];
+        if( p_pes == NULL )
+        {
+            intf_ErrMsg( "NULL packet in the data cache" );
+            vlc_mutex_unlock( &p_cache->lock );
+            return NULL;
+        }
+    }
+
+    vlc_mutex_unlock( &p_cache->lock );
 
-    p_pes->b_messed_up = p_pes->b_data_alignment = p_pes->b_discontinuity =
+    p_pes->b_data_alignment = p_pes->b_discontinuity =
         p_pes->i_pts = p_pes->i_dts = 0;
     p_pes->i_pes_size = 0;
     p_pes->p_first = NULL;
 
     return( p_pes );
+    
 }
 
 /*****************************************************************************
  * DeletePacket: deletes a data packet
  *****************************************************************************/
-static void DeletePacket( void * p_garbage,
+static void DeletePacket( void * p_packet_cache,
                           data_packet_t * p_data )
 {
-    ASSERT(p_data);
-    ASSERT(p_data->p_buffer);
-    free( p_data->p_buffer );
-    free( p_data );
+    packet_cache_t *   p_cache;
+
+    p_cache = (packet_cache_t *)p_packet_cache;
+
+#ifdef DEBUG
+    if ( p_cache == NULL )
+    {
+        intf_ErrMsg( "Packet cache not initialized" );
+        return;
+    }
+#endif
+
+    ASSERT( p_data );
+
+    vlc_mutex_lock( &p_cache->lock );
+
+    /* Checks whether the data cache is full */
+    if ( p_cache->data.l_index < DATA_CACHE_SIZE )
+    {
+        /* Cache not full: store the packet in it */
+        p_cache->data.p_stack[ p_cache->data.l_index ++ ] = p_data;
+        /* Small buffer or large buffer? */
+        if ( p_data->l_size < MAX_SMALL_SIZE )
+        {
+            /* Checks whether the small buffer cache is full */
+            if ( p_cache->smallbuffer.l_index < SMALL_CACHE_SIZE )
+            {
+                p_cache->smallbuffer.p_stack[
+                    p_cache->smallbuffer.l_index ].l_size = p_data->l_size;
+                p_cache->smallbuffer.p_stack[
+                    p_cache->smallbuffer.l_index++ ].p_data = p_data->p_buffer;
+            }
+            else
+            {
+                ASSERT( p_data->p_buffer );
+                free( p_data->p_buffer );
+#ifdef TRACE_INPUT
+                intf_DbgMsg( "PS input: small buffer freed" );
+#endif
+            }
+        }
+        else
+        {
+            /* Checks whether the large buffer cache is full */
+            if ( p_cache->largebuffer.l_index < LARGE_CACHE_SIZE )
+            {
+                p_cache->largebuffer.p_stack[
+                    p_cache->largebuffer.l_index ].l_size = p_data->l_size;
+                p_cache->largebuffer.p_stack[
+                    p_cache->largebuffer.l_index++ ].p_data = p_data->p_buffer;
+            }
+            else
+            {
+                ASSERT( p_data->p_buffer );
+                free( p_data->p_buffer );
+#ifdef TRACE_INPUT
+                intf_DbgMsg( "PS input: large buffer freed" );
+#endif
+            }
+        }
+    }
+    else
+    {
+        /* Cache full: the packet must be freed */
+        free( p_data->p_buffer );
+        free( p_data );
+#ifdef TRACE_INPUT
+        intf_DbgMsg( "PS input: data packet freed" );
+#endif
+    }
+
+    vlc_mutex_unlock( &p_cache->lock );
 }
 
 /*****************************************************************************
  * DeletePES: deletes a PES packet and associated data packets
  *****************************************************************************/
-static void DeletePES( void * p_garbage, pes_packet_t * p_pes )
+static void DeletePES( void * p_packet_cache, pes_packet_t * p_pes )
 {
+    packet_cache_t *    p_cache;
     data_packet_t *     p_data;
     data_packet_t *     p_next;
 
+    p_cache = (packet_cache_t *)p_packet_cache;
+
+#ifdef DEBUG
+    if ( p_cache == NULL )
+    {
+        intf_ErrMsg( "Packet cache not initialized" );
+        return;
+    }
+#endif
+
+    ASSERT( p_pes);
+
     p_data = p_pes->p_first;
 
     while( p_data != NULL )
     {
         p_next = p_data->p_next;
-        free( p_data->p_buffer );
-        free( p_data );
+        DeletePacket( p_cache, p_data );
         p_data = p_next;
     }
 
-    free( p_pes );
-}
+    vlc_mutex_lock( &p_cache->lock );
+
+    /* Checks whether the PES cache is full */
+    if ( p_cache->pes.l_index < PES_CACHE_SIZE )
+    {
+        /* Cache not full: store the packet in it */
+        p_cache->pes.p_stack[ p_cache->pes.l_index ++ ] = p_pes;
+    }
+    else
+    {
+        /* Cache full: the packet must be freed */
+        free( p_pes );
+#ifdef TRACE_INPUT
+        intf_DbgMsg( "PS input: PES packet freed" );
+#endif
+    }
 
+    vlc_mutex_unlock( &p_cache->lock );
+}