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
6 *****************************************************************************
7 * Copyright (C) 1998, 1999, 2000 VideoLAN
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.
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
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 int input_NetlistInit ( input_thread_t *p_input );
32 void input_NetlistEnd ( input_thread_t *p_input );
34 static __inline__ void input_NetlistFreePES( input_thread_t *p_input, pes_packet_t *p_pes_packet );
35 static __inline__ void input_NetlistFreeTS( input_thread_t *p_input, ts_packet_t *p_ts_packet );
36 static __inline__ pes_packet_t* input_NetlistGetPES( input_thread_t *p_input );
38 /*****************************************************************************
39 * input_NetlistFreePES: add a PES packet to the netlist
40 *****************************************************************************
41 * Add a PES packet to the PES netlist, so that the packet can immediately be
42 * reused by the demultiplexer. We put this function directly in the .h file,
43 * because it is very frequently called.
44 *****************************************************************************/
45 static __inline__ void input_NetlistFreePES( input_thread_t *p_input,
46 pes_packet_t *p_pes_packet )
49 ts_packet_t * p_ts_packet;
53 /* We will be playing with indexes, so we take a lock. */
54 vlc_mutex_lock( &p_input->netlist.lock );
56 /* Free all TS packets in this PES structure. */
57 p_ts_packet = p_pes_packet->p_first_ts;
58 for( i_dummy = 0; i_dummy < p_pes_packet->i_ts_packets; i_dummy++ )
62 #ifdef INPUT_LIFO_TS_NETLIST
63 p_input->netlist.i_ts_index--;
64 p_input->netlist.p_ts_free[p_input->netlist.i_ts_index].iov_base
65 = (void *)p_ts_packet;
67 p_input->netlist.p_ts_free[p_input->netlist.i_ts_end].iov_base
68 = (void *)p_ts_packet;
69 p_input->netlist.i_ts_end++;
70 p_input->netlist.i_ts_end &= INPUT_MAX_TS; /* loop */
72 p_ts_packet = p_ts_packet->p_next_ts;
75 /* Free the PES structure. */
76 #ifdef INPUT_LIFO_PES_NETLIST
77 p_input->netlist.i_pes_index--;
78 p_input->netlist.p_pes_free[p_input->netlist.i_pes_index] = p_pes_packet;
80 p_input->netlist.p_pes_free[p_input->netlist.i_pes_end] = p_pes_packet;
81 p_input->netlist.i_pes_end++;
82 p_input->netlist.i_pes_end &= INPUT_MAX_PES; /* loop */
85 vlc_mutex_unlock( &p_input->netlist.lock );
88 /*****************************************************************************
89 * input_NetlistFreeTS: add a TS packet to the netlist
90 *****************************************************************************
91 * Add a TS packet to the TS netlist, so that the packet can immediately be
92 * reused by the demultiplexer. Shouldn't be called by other threads (they
93 * should only use input_FreePES.
94 *****************************************************************************/
95 static __inline__ void input_NetlistFreeTS( input_thread_t *p_input,
96 ts_packet_t *p_ts_packet )
100 /* We will be playing with indexes, so we take a lock. */
101 vlc_mutex_lock( &p_input->netlist.lock );
103 /* Free the TS structure. */
104 #ifdef INPUT_LIFO_TS_NETLIST
105 p_input->netlist.i_ts_index--;
106 p_input->netlist.p_ts_free[p_input->netlist.i_ts_index].iov_base
107 = (void *)p_ts_packet;
109 p_input->netlist.p_ts_free[p_input->netlist.i_ts_end].iov_base
110 = (void *)p_ts_packet;
111 p_input->netlist.i_ts_end++;
112 p_input->netlist.i_ts_end &= INPUT_MAX_TS; /* loop */
115 vlc_mutex_unlock( &p_input->netlist.lock );
118 /*****************************************************************************
119 * input_NetlistGetPES: remove a PES packet from the netlist
120 *****************************************************************************
121 * Add a TS packet to the TS netlist, so that the packet can immediately be
122 * reused by the demultiplexer. Shouldn't be called by other threads (they
123 * should only use input_FreePES.
124 *****************************************************************************/
125 static __inline__ pes_packet_t* input_NetlistGetPES( input_thread_t *p_input )
127 pes_packet_t * p_pes_packet;
129 #ifdef INPUT_LIFO_PES_NETLIST
130 /* i_pes_index might be accessed by a decoder thread to give back a
132 vlc_mutex_lock( &p_input->netlist.lock );
134 /* Verify that we still have PES packet in the netlist */
135 if( (INPUT_MAX_PES - p_input->netlist.i_pes_index ) <= 1 )
137 intf_ErrMsg("input error: PES netlist is empty !\n");
141 /* Fetch a new PES packet */
142 p_pes_packet = p_input->netlist.p_pes_free[p_input->netlist.i_pes_index];
143 p_input->netlist.i_pes_index++;
144 vlc_mutex_unlock( &p_input->netlist.lock );
147 /* No need to lock, since we are the only ones accessing i_pes_start. */
149 /* Verify that we still have PES packet in the netlist */
150 if( ((p_input->netlist.i_pes_end -1 - p_input->netlist.i_pes_start) & INPUT_MAX_PES) <= 1 )
152 intf_ErrMsg("input error: PES netlist is empty !\n");
156 p_pes_packet = p_input->netlist.p_pes_free[p_input->netlist.i_pes_start];
157 p_input->netlist.i_pes_start++;
158 p_input->netlist.i_pes_start &= INPUT_MAX_PES; /* loop */
159 #endif /* netlist type */
161 /* Initialize PES flags. */
162 p_pes_packet->b_data_loss = 0;
163 p_pes_packet->b_data_alignment = 0;
164 p_pes_packet->b_has_pts = 0;
165 p_pes_packet->b_random_access = 0;
166 p_pes_packet->b_discard_payload = 0;
167 p_pes_packet->i_pes_size = 0;
168 p_pes_packet->i_pes_real_size = 0;
169 p_pes_packet->i_ts_packets = 0;
170 p_pes_packet->p_first_ts = NULL;
171 p_pes_packet->p_last_ts = NULL;
173 return( p_pes_packet );