]> git.sesse.net Git - vlc/blobdiff - src/input/input_netlist.c
A BIG kludge for the calculation of mux_rate, so that times displayed in
[vlc] / src / input / input_netlist.c
index b3593dc112e2eaed76744b16c2bcdd341b8fdcaa..d48e52ec2a01979632b3aff1cbcd706766fd35f6 100644 (file)
@@ -2,7 +2,7 @@
  * input_netlist.c: netlist management
  *****************************************************************************
  * Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: input_netlist.c,v 1.30 2001/02/08 17:44:12 massiot Exp $
+ * $Id: input_netlist.c,v 1.40 2001/07/17 09:48:08 massiot Exp $
  *
  * Authors: Henri Fallon <henri@videolan.org>
  *
 #include "defs.h"
 
 #include <stdlib.h>
+#include <string.h>                                    /* memcpy(), memset() */
 #include <sys/types.h>
-#include <sys/uio.h>                                         /* struct iovec */
-#include <unistd.h>
+
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>
+#endif
+
+#if !defined( WIN32 )
+#   include <sys/uio.h>                                      /* struct iovec */
+#else
+#   include <io.h>
+#   include "input_iovec.h"
+#endif
 
 #include "config.h"
 #include "common.h"
 #include "stream_control.h"
 #include "input_ext-intf.h"
 #include "input_ext-dec.h"
-
-#include "input_netlist.h"
-#include "input.h"
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
+#include "input_ext-plugins.h"
 
 /*****************************************************************************
  * input_NetlistInit: allocates netlist buffers and init indexes
@@ -67,6 +71,31 @@ int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
     
     p_netlist = (netlist_t *) p_input->p_method_data;
     
+    p_netlist->i_read_once = i_read_once;
+    
+    /* In order to optimize netlist, we are taking i_nb_data a 2^i 
+     * so that modulo is an "&".
+     * This is not changing i_nb data outside this function except in 
+     * the netlist_t struct */
+    /* As i_loop is unsigned int, and i_ns_data int, this shouldn't be a 
+     * problem */
+    for( i_loop = 1; i_loop < i_nb_data; i_loop *= 2 )
+    {
+        ;
+    }
+
+    intf_DbgMsg( "Netlist : Required %i byte, got %u",i_nb_data,i_loop );
+    i_nb_data = i_loop;
+
+    /* Same thing for i_nb_pes */
+    for( i_loop = 1; i_loop < i_nb_data; i_loop *= 2 )
+    {
+        ;
+    }
+
+    intf_DbgMsg( "Netlist : Required %i byte, got %u",i_nb_data,i_loop );
+    i_nb_data = i_loop;
+    
     /* allocate the buffers */ 
     p_netlist->p_buffers = 
         (byte_t *) malloc(i_buffer_size* i_nb_data );
@@ -107,7 +136,7 @@ int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
     }
     
     p_netlist->p_free_iovec = ( struct iovec * )
-        malloc( (i_nb_data + INPUT_READ_ONCE) * sizeof(struct iovec) );
+        malloc( (i_nb_data + p_netlist->i_read_once) * sizeof(struct iovec) );
     if ( p_netlist->p_free_iovec == NULL )
     {
         intf_ErrMsg ("Unable to malloc in netlist initialization (6)");
@@ -121,6 +150,9 @@ int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
 
         p_netlist->pp_free_data[i_loop]->p_buffer = 
             p_netlist->p_buffers + i_loop * i_buffer_size;
+      
+        p_netlist->pp_free_data[i_loop]->p_payload_start = 
+            p_netlist->pp_free_data[i_loop]->p_buffer;
         
         p_netlist->pp_free_data[i_loop]->p_payload_end =
             p_netlist->pp_free_data[i_loop]->p_buffer + i_buffer_size;
@@ -140,7 +172,7 @@ int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
    
         p_netlist->p_free_iovec[i_loop].iov_len = i_buffer_size;
     }
-    
+   
     /* vlc_mutex_init */
     vlc_mutex_init (&p_netlist->lock);
     
@@ -155,16 +187,17 @@ int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
     p_netlist->i_nb_pes = i_nb_pes;
     p_netlist->i_buffer_size = i_buffer_size;
 
-    p_netlist->i_read_once = i_read_once;
-
     return (0); /* Everything went all right */
 }
 
 /*****************************************************************************
  * input_NetlistGetiovec: returns an iovec pointer for a readv() operation
+ *****************************************************************************
+ * We return an iovec vector, so that readv can read many packets at a time,
+ * and we set pp_data to direct to the fifo pointer, which will allow us
+ * to get the corresponding data_packet.
  *****************************************************************************/
-struct iovec * input_NetlistGetiovec( void * p_method_data,
-                                      struct data_packet_s ** pp_data )
+struct iovec * input_NetlistGetiovec( void * p_method_data )
 {
     netlist_t * p_netlist;
 
@@ -173,8 +206,8 @@ struct iovec * input_NetlistGetiovec( void * p_method_data,
     
     /* check */
     if( 
-     (p_netlist->i_data_end - p_netlist->i_data_start + p_netlist->i_nb_data)
-     %p_netlist->i_nb_data < p_netlist->i_read_once )
+     ( (p_netlist->i_data_end - p_netlist->i_data_start + p_netlist->i_nb_data)
+     & ( p_netlist->i_nb_data -1 ) ) < p_netlist->i_read_once )
     {
         intf_ErrMsg("Empty iovec FIFO. Unable to allocate memory");
         return (NULL);
@@ -191,22 +224,11 @@ struct iovec * input_NetlistGetiovec( void * p_method_data,
                 p_netlist->p_free_iovec, 
                 (p_netlist->i_read_once-
                     (p_netlist->i_nb_data-p_netlist->i_data_start))
-                    * sizeof(struct iovec*)
+                    * sizeof(struct iovec)
               );
 
     }
  
-    /* Gives a pointer to the data_packet struct associated with io_vec */
-    *pp_data = p_netlist->pp_free_data[p_netlist->i_data_start];
-
-    /* Initialize payload start and end */
-    p_netlist->pp_free_data[p_netlist->i_data_start]->p_payload_start 
-        = p_netlist->pp_free_data[p_netlist->i_data_start]->p_buffer;
-    p_netlist->pp_free_data[p_netlist->i_data_start]->p_payload_end 
-        = p_netlist->pp_free_data[p_netlist->i_data_start]->p_payload_start
-        + p_netlist->i_buffer_size;
-
     return &p_netlist->p_free_iovec[p_netlist->i_data_start];
 
 }
@@ -214,18 +236,37 @@ struct iovec * input_NetlistGetiovec( void * p_method_data,
 /*****************************************************************************
  * input_NetlistMviovec: move the iovec pointer after a readv() operation
  *****************************************************************************/
-void input_NetlistMviovec( void * p_method_data, size_t i_nb_iovec )
+void input_NetlistMviovec( void * p_method_data, size_t i_nb_iovec,
+                           struct data_packet_s * pp_packets[INPUT_READ_ONCE] )
 {
     netlist_t * p_netlist;
+    unsigned int i_loop = 0;
+    unsigned int i_current;
 
     /* cast */
     p_netlist = (netlist_t *) p_method_data;
     
     /* lock */
     vlc_mutex_lock ( &p_netlist->lock );
-    
+
+    i_current = p_netlist->i_data_start;
+
+
+    /* Fills a table of pointers to packets associated with the io_vec's */
+    while (i_loop < i_nb_iovec )
+    {
+        if( i_current >= p_netlist->i_nb_data ) 
+            i_current-=p_netlist->i_nb_data;
+        
+        pp_packets[i_loop] = p_netlist->pp_free_data[i_current];
+        pp_packets[i_loop]->b_discard_payload = 0;
+
+        i_loop ++;
+        i_current ++;
+    }
+
     p_netlist->i_data_start += i_nb_iovec;
-    p_netlist->i_data_start %= p_netlist->i_nb_data;
+    p_netlist->i_data_start &= ( p_netlist->i_nb_data - 1 );
 
     /* unlock */
     vlc_mutex_unlock (&p_netlist->lock);
@@ -263,9 +304,9 @@ struct data_packet_s * input_NetlistNewPacket( void * p_method_data,
         return ( NULL );
     }
     
-    p_return = (p_netlist->pp_free_data[p_netlist->i_data_start]);
+    p_return = p_netlist->pp_free_data[p_netlist->i_data_start];
     p_netlist->i_data_start++;
-    p_netlist->i_data_start %= p_netlist->i_nb_data;
+    p_netlist->i_data_start &= ( p_netlist->i_nb_data - 1 );
 
     /* unlock */
     vlc_mutex_unlock (&p_netlist->lock);
@@ -305,7 +346,7 @@ struct pes_packet_s * input_NetlistNewPES( void * p_method_data )
     /* allocate */
     p_return = p_netlist->pp_free_pes[p_netlist->i_pes_start];
     p_netlist->i_pes_start++;
-    p_netlist->i_pes_start %= p_netlist->i_nb_pes
+    p_netlist->i_pes_start &= ( p_netlist->i_nb_pes - 1 )
    
     /* unlock */
     vlc_mutex_unlock (&p_netlist->lock);
@@ -333,15 +374,20 @@ void input_NetlistDeletePacket( void * p_method_data, data_packet_t * p_data )
     /* lock */
     vlc_mutex_lock ( &p_netlist->lock );
 
-    /* Delete data_packet */
+
+   /* Delete data_packet */
     p_netlist->i_data_end ++;
-    p_netlist->i_data_end %= p_netlist->i_nb_data;
-    p_netlist->pp_free_data[p_netlist->i_data_end] = p_data;
+    p_netlist->i_data_end &= ( p_netlist->i_nb_data - 1 );
     
+    p_netlist->pp_free_data[p_netlist->i_data_end] = p_data;
     p_netlist->p_free_iovec[p_netlist->i_data_end].iov_base = p_data->p_buffer;
-    
+
+    /* re initialize for next time */
+    p_data->p_payload_start = p_data->p_buffer;
+    p_data->p_next = NULL;
     /* unlock */
-    vlc_mutex_unlock (&p_netlist->lock);
+    vlc_mutex_unlock( &p_netlist->lock );
 }
 
 /*****************************************************************************
@@ -350,7 +396,7 @@ void input_NetlistDeletePacket( void * p_method_data, data_packet_t * p_data )
 void input_NetlistDeletePES( void * p_method_data, pes_packet_t * p_pes )
 {
     netlist_t * p_netlist; 
-    data_packet_t * p_current_packet;
+    data_packet_t * p_current_packet,* p_next_packet;
     
     /* cast */
     p_netlist = (netlist_t *)p_method_data;
@@ -365,22 +411,28 @@ void input_NetlistDeletePES( void * p_method_data, pes_packet_t * p_pes )
         /* copy of NetListDeletePacket, duplicate code avoid many locks */
 
         p_netlist->i_data_end ++;
-        p_netlist->i_data_end %= p_netlist->i_nb_data;
-        p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
+        p_netlist->i_data_end &= ( p_netlist->i_nb_data - 1 );
+
+        /* re initialize*/
+        p_current_packet->p_payload_start = p_current_packet->p_buffer;
         
+        p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
+
         p_netlist->p_free_iovec[p_netlist->i_data_end].iov_base 
             = p_current_packet->p_buffer;
     
-        p_current_packet = p_current_packet->p_next;
+        p_next_packet = p_current_packet->p_next;
+        p_current_packet->p_next = NULL;
+        p_current_packet = p_next_packet;
     }
  
     /* delete our current PES packet */
     p_netlist->i_pes_end ++;
-    p_netlist->i_pes_end %= p_netlist->i_nb_pes;
+    p_netlist->i_pes_end &= ( p_netlist->i_nb_pes - 1 );
     p_netlist->pp_free_pes[p_netlist->i_pes_end] = p_pes;
     
     /* unlock */
-    vlc_mutex_unlock (&p_netlist->lock);
+    vlc_mutex_unlock( &p_netlist->lock );
 
 }
 
@@ -398,12 +450,13 @@ void input_NetlistEnd( input_thread_t * p_input)
     vlc_mutex_destroy (&p_netlist->lock);
     
     /* free the FIFO, the buffer, and the netlist structure */
-    free (p_netlist->pp_free_data);
-    free (p_netlist->pp_free_pes);
-    free (p_netlist->p_pes);
-    free (p_netlist->p_data);
-    free (p_netlist->p_buffers);
+    free( p_netlist->pp_free_data );
+    free( p_netlist->pp_free_pes );
+    free( p_netlist->p_pes );
+    free( p_netlist->p_data );
+    free( p_netlist->p_buffers );
 
     /* free the netlist */
-    free (p_netlist);
+    free( p_netlist );
 }
+