]> git.sesse.net Git - vlc/blob - include/input_netlist.h
07929e32f59898c52679b89b037dddf1e3e92fee
[vlc] / include / input_netlist.h
1 /*****************************************************************************
2  * input_netlist.h: netlist interface
3  * The netlists are an essential part of the input structure. We maintain a
4  * list of free TS packets and free PES packets to avoid continuous malloc
5  * and free.
6  *****************************************************************************
7  * Copyright (C) 1998, 1999, 2000 VideoLAN
8  *
9  * Authors:
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this program; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *****************************************************************************/
26
27 #include "intf_msg.h"
28
29 /*****************************************************************************
30  * Prototypes
31  *****************************************************************************/
32 int     input_NetlistInit       ( input_thread_t *p_input );
33 void    input_NetlistEnd        ( input_thread_t *p_input );
34
35 static __inline__ void input_NetlistFreePES( input_thread_t *p_input, pes_packet_t *p_pes_packet );
36 static __inline__ void input_NetlistFreeTS( input_thread_t *p_input, ts_packet_t *p_ts_packet );
37 static __inline__ pes_packet_t* input_NetlistGetPES( input_thread_t *p_input );
38
39 /*****************************************************************************
40  * input_NetlistFreePES: add a PES packet to the netlist
41  *****************************************************************************
42  * Add a PES packet to the PES netlist, so that the packet can immediately be
43  * reused by the demultiplexer. We put this function directly in the .h file,
44  * because it is very frequently called.
45  *****************************************************************************/
46 static __inline__ void input_NetlistFreePES( input_thread_t *p_input,
47                                   pes_packet_t *p_pes_packet )
48 {
49     int             i_dummy;
50     ts_packet_t *   p_ts_packet;
51
52     ASSERT(p_pes_packet);
53
54     /* We will be playing with indexes, so we take a lock. */
55     vlc_mutex_lock( &p_input->netlist.lock );
56
57     /* Free all TS packets in this PES structure. */
58     p_ts_packet = p_pes_packet->p_first_ts;
59     for( i_dummy = 0; i_dummy < p_pes_packet->i_ts_packets; i_dummy++ )
60     {
61         ASSERT(p_ts_packet);
62
63 #ifdef INPUT_LIFO_TS_NETLIST
64         p_input->netlist.i_ts_index--;
65         p_input->netlist.p_ts_free[p_input->netlist.i_ts_index].iov_base
66                              = p_ts_packet;
67 #else /* FIFO */
68         p_input->netlist.p_ts_free[p_input->netlist.i_ts_end].iov_base
69                              = p_ts_packet;
70         p_input->netlist.i_ts_end++;
71         p_input->netlist.i_ts_end &= INPUT_MAX_TS; /* loop */
72 #endif
73         p_ts_packet = p_ts_packet->p_next_ts;
74     }
75
76     /* Free the PES structure. */
77 #ifdef INPUT_LIFO_PES_NETLIST
78     p_input->netlist.i_pes_index--;
79     p_input->netlist.p_pes_free[p_input->netlist.i_pes_index] = p_pes_packet;
80 #else /* FIFO */
81     p_input->netlist.p_pes_free[p_input->netlist.i_pes_end] = p_pes_packet;
82     p_input->netlist.i_pes_end++;
83     p_input->netlist.i_pes_end &= INPUT_MAX_PES; /* loop */
84 #endif
85
86     vlc_mutex_unlock( &p_input->netlist.lock );
87 }
88
89 /*****************************************************************************
90  * input_NetlistFreeTS: add a TS packet to the netlist
91  *****************************************************************************
92  * Add a TS packet to the TS netlist, so that the packet can immediately be
93  * reused by the demultiplexer. Shouldn't be called by other threads (they
94  * should only use input_FreePES.
95  *****************************************************************************/
96 static __inline__ void input_NetlistFreeTS( input_thread_t *p_input,
97                                             ts_packet_t *p_ts_packet )
98 {
99     ASSERT(p_ts_packet);
100
101     /* We will be playing with indexes, so we take a lock. */
102     vlc_mutex_lock( &p_input->netlist.lock );
103
104     /* Free the TS structure. */
105 #ifdef INPUT_LIFO_TS_NETLIST
106     p_input->netlist.i_ts_index--;
107     p_input->netlist.p_ts_free[p_input->netlist.i_ts_index].iov_base = p_ts_packet;
108 #else /* FIFO */
109     p_input->netlist.p_ts_free[p_input->netlist.i_ts_end].iov_base = p_ts_packet;
110     p_input->netlist.i_ts_end++;
111     p_input->netlist.i_ts_end &= INPUT_MAX_TS; /* loop */
112 #endif
113
114     vlc_mutex_unlock( &p_input->netlist.lock );
115 }
116
117 /*****************************************************************************
118  * input_NetlistGetPES: remove a PES packet from the netlist
119  *****************************************************************************
120  * Add a TS packet to the TS netlist, so that the packet can immediately be
121  * reused by the demultiplexer. Shouldn't be called by other threads (they
122  * should only use input_FreePES.
123  *****************************************************************************/
124 static __inline__ pes_packet_t* input_NetlistGetPES( input_thread_t *p_input )
125 {
126     pes_packet_t *          p_pes_packet;
127
128 #ifdef INPUT_LIFO_PES_NETLIST
129     /* i_pes_index might be accessed by a decoder thread to give back a
130      * packet. */
131     vlc_mutex_lock( &p_input->netlist.lock );
132
133     /* Verify that we still have PES packet in the netlist */
134     if( (INPUT_MAX_PES - p_input->netlist.i_pes_index ) <= 1 )
135     {
136         intf_ErrMsg("input error: PES netlist is empty !\n");
137         return( NULL );
138     }
139
140     /* Fetch a new PES packet */
141     p_pes_packet = p_input->netlist.p_pes_free[p_input->netlist.i_pes_index];
142     p_input->netlist.i_pes_index++;
143     vlc_mutex_unlock( &p_input->netlist.lock );
144
145 #else /* FIFO */
146     /* No need to lock, since we are the only ones accessing i_pes_start. */
147
148     /* Verify that we still have PES packet in the netlist */
149     if( ((p_input->netlist.i_pes_end -1 - p_input->netlist.i_pes_start) & INPUT_MAX_PES) <= 1 )
150     {
151         intf_ErrMsg("input error: PES netlist is empty !\n");
152         return( NULL );
153     }
154
155     p_pes_packet = p_input->netlist.p_pes_free[p_input->netlist.i_pes_start];
156     p_input->netlist.i_pes_start++;
157     p_input->netlist.i_pes_start &= INPUT_MAX_PES; /* loop */
158 #endif /* netlist type */
159
160     /* Initialize PES flags. */
161     p_pes_packet->b_data_loss = 0;
162     p_pes_packet->b_data_alignment = 0;
163     p_pes_packet->b_has_pts = 0;
164     p_pes_packet->b_random_access = 0;
165     p_pes_packet->b_discard_payload = 0;
166     p_pes_packet->i_pes_size = 0;
167     p_pes_packet->i_ts_packets = 0;
168     p_pes_packet->p_first_ts = NULL;
169     p_pes_packet->p_last_ts = NULL;
170
171     return( p_pes_packet );
172 }