]> git.sesse.net Git - vlc/blobdiff - src/input/input_netlist.c
* Fixed a few warnings with gcc 3.0.
[vlc] / src / input / input_netlist.c
index 1cf6a64b59a753dcf183ffd5419fb78d161fbc5e..930e121927c532dbe387c9027091acdebd4775b8 100644 (file)
@@ -2,7 +2,7 @@
  * input_netlist.c: netlist management
  *****************************************************************************
  * Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: input_netlist.c,v 1.21 2000/12/22 13:04:45 sam Exp $
+ * $Id: input_netlist.c,v 1.36 2001/05/06 04:32:02 sam Exp $
  *
  * Authors: Henri Fallon <henri@videolan.org>
  *
 #include "defs.h"
 
 #include <stdlib.h>
-#include <sys/uio.h>                                         /* struct iovec */
+#include <string.h>                                    /* memcpy(), memset() */
+#include <sys/types.h>
+#include <unistd.h>
+
+#ifndef WIN32 
+#   include <sys/uio.h>                                      /* struct iovec */
+#else
+    struct iovec
+    {
+        void *iov_base; /* Pointer to data.  */
+        size_t iov_len; /* Length of data.  */
+    };
+#endif
 
 #include "config.h"
 #include "common.h"
 #include "input_ext-intf.h"
 #include "input_ext-dec.h"
 
-#include "input_netlist.h"
 #include "input.h"
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
+#include "input_netlist.h"
 
 /*****************************************************************************
  * input_NetlistInit: allocates netlist buffers and init indexes
  *****************************************************************************/
 int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
-                       size_t i_buffer_size )
+                       size_t i_buffer_size, int i_read_once )
 {
     unsigned int i_loop;
     netlist_t * p_netlist;
@@ -65,6 +73,25 @@ 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 );
@@ -105,7 +132,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)");
@@ -119,12 +146,10 @@ 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;
-        
-        //peut-être pas nécessaire ici vu qu'on le fera à chaque fois
-        //dans NewPacket et Getiovec
+      
         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;
     }
@@ -143,7 +168,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);
     
@@ -151,15 +176,9 @@ int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
     p_netlist->i_data_start = 0;
     p_netlist->i_data_end = i_nb_data - 1;
 
-    //INPUT_READ_ONCE en trop
     p_netlist->i_pes_start = 0;
-    p_netlist->i_pes_end = i_nb_pes + INPUT_READ_ONCE - 1;
+    p_netlist->i_pes_end = i_nb_pes - 1;
 
-    //inutiles, en fait toujours strictement == à i_data_start (les deux
-    //pointent vers le même buffer donc il faut garder une synchronisation)
-    p_netlist->i_iovec_start = 0;
-    p_netlist->i_iovec_end = i_nb_data - 1;
-    
     p_netlist->i_nb_data = i_nb_data;
     p_netlist->i_nb_pes = i_nb_pes;
     p_netlist->i_buffer_size = i_buffer_size;
@@ -169,6 +188,10 @@ int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
 
 /*****************************************************************************
  * 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 )
 {
@@ -178,50 +201,72 @@ struct iovec * input_NetlistGetiovec( void * p_method_data )
     p_netlist = ( netlist_t * ) p_method_data;
     
     /* check */
-    //vérifier que -truc % bidule fait bien ce qu'il faut (me souviens plus
-    //de la définition de % sur -N)
-    if ( 
-     (p_netlist->i_iovec_end - p_netlist->i_iovec_start)%p_netlist->i_nb_data 
-     < INPUT_READ_ONCE )
+    if( 
+     ( (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);
     }
 
-    /* readv only takes contiguous buffers */
-    if( p_netlist->i_nb_data - p_netlist->i_iovec_start < INPUT_READ_ONCE )
+    /* readv only takes contiguous buffers 
+     * so, as a solution, we chose to have a FIFO a bit longer
+     * than i_nb_data, and copy the begining of the FIFO to its end
+     * if the readv needs to go after the end */
+    if( p_netlist->i_nb_data - p_netlist->i_data_start <
+                                                    p_netlist->i_read_once )
+    {
         memcpy( &p_netlist->p_free_iovec[p_netlist->i_nb_data], 
                 p_netlist->p_free_iovec, 
-                INPUT_READ_ONCE-(p_netlist->i_nb_data-p_netlist->i_iovec_start)
+                (p_netlist->i_read_once-
+                    (p_netlist->i_nb_data-p_netlist->i_data_start))
+                    * sizeof(struct iovec)
               );
-    //manque un sizeof() dans le memcpy
-
-    //pas tout de suite (->dans Mviovec), parce que readv ne va pas
-    //_nécessairement_ prendre tous les iovec disponibles (cf. man readv)
-    p_netlist->i_iovec_start += INPUT_READ_ONCE;
-    p_netlist->i_iovec_start %= p_netlist->i_nb_data;
 
-    //il faudrait aussi initialiser les data_packet_t correspondants,
-    //comme dans NewPacket
+    }
+    return &p_netlist->p_free_iovec[p_netlist->i_data_start];
 
-    return &p_netlist->p_free_iovec[p_netlist->i_iovec_start];
 }
 
 /*****************************************************************************
  * 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;
     
-    //remplacer i_iovec_start par i_data_start, en fait c'est la même
-    //chose.
-    //il manque un lock
-    p_netlist->i_iovec_start += i_nb_iovec;
-    p_netlist->i_iovec_start %= p_netlist->i_nb_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 - 1 );
+
+    /* unlock */
+    vlc_mutex_unlock (&p_netlist->lock);
+    
 }
 
 /*****************************************************************************
@@ -255,28 +300,21 @@ 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;
-
-    //on vire aussi, forcément
-    p_netlist->i_iovec_start++; 
-    p_netlist->i_iovec_start %= p_netlist->i_nb_data;
+    p_netlist->i_data_start &= ( p_netlist->i_nb_data - 1 );
 
     /* unlock */
     vlc_mutex_unlock (&p_netlist->lock);
 
-    if (i_buffer_size < p_netlist->i_buffer_size) 
-    {
-        p_return->p_payload_end = p_return->p_payload_start + i_buffer_size;
-    }
    
     /* initialize data */
     p_return->p_next = NULL;
     p_return->b_discard_payload = 0;
-    //p_payload_start = ..., p_payload_end = ... (risque d'être modifié
-    //à tout moment par l'input et les décodeurs, donc on ne peut rien
-    //supposer...)
+    
+    p_return->p_payload_start = p_return->p_buffer;
+    p_return->p_payload_end = p_return->p_payload_start + i_buffer_size;
+    
     return ( p_return );
 }
 
@@ -304,16 +342,15 @@ 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);
     
     /* initialize PES */
-    p_return->b_messed_up = 
-        p_return->b_data_alignment = 
+    p_return->b_data_alignment = 
         p_return->b_discontinuity = 
-        p_return->b_has_pts = 0;
+        p_return->i_pts = p_return->i_dts = 0;
     p_return->i_pes_size = 0;
     p_return->p_first = NULL;
    
@@ -333,19 +370,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;
-    
-    /* Delete the corresponding iovec */
-    p_netlist->i_iovec_end++; 
-    p_netlist->i_iovec_end %= p_netlist->i_nb_data;
-    p_netlist->p_free_iovec[p_netlist->i_iovec_end].iov_base = 
-        p_data->p_buffer;
+    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 );
 }
 
 /*****************************************************************************
@@ -354,7 +392,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;
@@ -369,24 +407,29 @@ 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->i_iovec_end++; 
-        p_netlist->i_iovec_end %= p_netlist->i_nb_data;
-        p_netlist->p_free_iovec[p_netlist->i_iovec_end].iov_base = 
-            p_netlist->p_data->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 );
+
 }
 
 /*****************************************************************************
@@ -399,13 +442,17 @@ void input_NetlistEnd( input_thread_t * p_input)
     /* cast */
     p_netlist = ( netlist_t * ) p_input->p_method_data;
     
+    /* destroy the mutex lock */
+    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);
-    //et p_buffers il pue ?
+    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 );
 }
+