]> git.sesse.net Git - vlc/blob - include/input_netlist.h
Encapsulation des pthread qui sont maintenant remplac�es par les vlc_thread.
[vlc] / include / input_netlist.h
1 /*******************************************************************************
2  * input_netlist.h: netlist interface
3  * (c)1998 VideoLAN
4  *******************************************************************************
5  * The netlists are an essential part of the input structure. We maintain a
6  * list of free TS packets and free PES packets to avoid continuous malloc
7  * and free.
8  *******************************************************************************/
9
10 /******************************************************************************
11  * Prototypes
12  ******************************************************************************/
13 int input_NetlistOpen( input_thread_t *p_input );
14 void input_NetlistClean( input_thread_t *p_input );
15 /* ?? implement also a "normal" (non inline, non static) function in input_netlist.c,
16    which will be used when inline is disabled */
17 /* ?? test */ static __inline__ void input_NetlistFreePES( input_thread_t *p_input,
18                                   pes_packet_t *p_pes_packet );
19 static __inline__ void input_NetlistFreeTS( input_thread_t *p_input,
20                                  ts_packet_t *p_ts_packet );
21 static __inline__ pes_packet_t* input_NetlistGetPES( input_thread_t *p_input );
22
23 /*******************************************************************************
24  * input_NetlistFreePES: add a PES packet to the netlist
25  *******************************************************************************
26  * Add a PES packet to the PES netlist, so that the packet can immediately be
27  * reused by the demultiplexer. We put this function directly in the .h file,
28  * because it is very frequently called.
29  *******************************************************************************/
30 static __inline__ void input_NetlistFreePES( input_thread_t *p_input,
31                                   pes_packet_t *p_pes_packet )
32 {
33     int             i_dummy;
34     ts_packet_t *   p_ts_packet;
35
36     ASSERT(p_pes_packet);
37
38     /* We will be playing with indexes, so we take a lock. */
39     vlc_mutex_lock( &p_input->netlist.lock );
40
41     /* Free all TS packets in this PES structure. */
42     p_ts_packet = p_pes_packet->p_first_ts;
43     for( i_dummy = 0; i_dummy < p_pes_packet->i_ts_packets; i_dummy++ )
44     {
45         ASSERT(p_ts_packet);
46
47 #ifdef INPUT_LIFO_TS_NETLIST
48         p_input->netlist.i_ts_index--;
49         p_input->netlist.p_ts_free[p_input->netlist.i_ts_index].iov_base
50                              = p_ts_packet;
51 #else /* FIFO */
52         p_input->netlist.p_ts_free[p_input->netlist.i_ts_end].iov_base
53                              = p_ts_packet;
54         p_input->netlist.i_ts_end++;
55         p_input->netlist.i_ts_end &= INPUT_MAX_TS; /* loop */
56 #endif
57         p_ts_packet = p_ts_packet->p_next_ts;
58     }
59     
60     /* Free the PES structure. */
61 #ifdef INPUT_LIFO_PES_NETLIST
62     p_input->netlist.i_pes_index--;
63     p_input->netlist.p_pes_free[p_input->netlist.i_pes_index] = p_pes_packet;
64 #else /* FIFO */
65     p_input->netlist.p_pes_free[p_input->netlist.i_pes_end] = p_pes_packet;
66     p_input->netlist.i_pes_end++;
67     p_input->netlist.i_pes_end &= INPUT_MAX_PES; /* loop */
68 #endif
69
70     vlc_mutex_unlock( &p_input->netlist.lock );
71 }
72
73 /*******************************************************************************
74  * input_NetlistFreeTS: add a TS packet to the netlist
75  *******************************************************************************
76  * Add a TS packet to the TS netlist, so that the packet can immediately be
77  * reused by the demultiplexer. Shouldn't be called by other threads (they
78  * should only use input_FreePES.
79  *******************************************************************************/
80 static __inline__ void input_NetlistFreeTS( input_thread_t *p_input,
81                                             ts_packet_t *p_ts_packet )
82 {
83     ASSERT(p_ts_packet);
84
85     /* We will be playing with indexes, so we take a lock. */
86     vlc_mutex_lock( &p_input->netlist.lock );
87
88     /* Free the TS structure. */
89 #ifdef INPUT_LIFO_TS_NETLIST
90     p_input->netlist.i_ts_index--;
91     p_input->netlist.p_ts_free[p_input->netlist.i_ts_index].iov_base = p_ts_packet;
92 #else /* FIFO */
93     p_input->netlist.p_ts_free[p_input->netlist.i_ts_end].iov_base = p_ts_packet;
94     p_input->netlist.i_ts_end++;
95     p_input->netlist.i_ts_end &= INPUT_MAX_TS; /* loop */
96 #endif
97
98     vlc_mutex_unlock( &p_input->netlist.lock );
99 }
100
101 /*******************************************************************************
102  * input_NetlistGetPES: remove a PES packet from the netlist
103  *******************************************************************************
104  * Add a TS packet to the TS netlist, so that the packet can immediately be
105  * reused by the demultiplexer. Shouldn't be called by other threads (they
106  * should only use input_FreePES.
107  *******************************************************************************/
108 static __inline__ pes_packet_t* input_NetlistGetPES( input_thread_t *p_input )
109 {
110     pes_packet_t *          p_pes_packet;
111
112 #ifdef INPUT_LIFO_PES_NETLIST
113     /* i_pes_index might be accessed by a decoder thread to give back a 
114      * packet. */
115     vlc_mutex_lock( &p_input->netlist.lock );
116
117     /* Verify that we still have PES packet in the netlist */
118     if( (INPUT_MAX_PES - p_input->netlist.i_pes_index ) <= 1 )
119     {
120         intf_ErrMsg("input error: PES netlist is empty !\n");
121         return( NULL );
122     }
123
124     /* Fetch a new PES packet */
125     p_pes_packet = p_input->netlist.p_pes_free[p_input->netlist.i_pes_index];
126     p_input->netlist.i_pes_index++;
127     vlc_mutex_unlock( &p_input->netlist.lock );
128
129 #else /* FIFO */
130     /* No need to lock, since we are the only ones accessing i_pes_start. */
131
132     /* Verify that we still have PES packet in the netlist */
133     if( ((p_input->netlist.i_pes_end -1 - p_input->netlist.i_pes_start) & INPUT_MAX_PES) <= 1 )
134     {
135         intf_ErrMsg("input error: PES netlist is empty !\n");
136         return( NULL );
137     }
138
139     p_pes_packet = p_input->netlist.p_pes_free[p_input->netlist.i_pes_start];
140     p_input->netlist.i_pes_start++;
141     p_input->netlist.i_pes_start &= INPUT_MAX_PES; /* loop */
142 #endif /* netlist type */
143
144     /* Initialize PES flags. */
145     p_pes_packet->b_data_loss = 0;
146     p_pes_packet->b_data_alignment = 0;
147     p_pes_packet->b_has_pts = 0;
148     p_pes_packet->b_random_access = 0;
149     p_pes_packet->b_discard_payload = 0;
150     p_pes_packet->i_pes_size = 0;
151     p_pes_packet->i_ts_packets = 0;
152     p_pes_packet->p_first_ts = NULL;
153     p_pes_packet->p_last_ts = NULL;
154
155     return( p_pes_packet );
156 }