1 /*****************************************************************************
2 * netlist.c: input thread
3 * Manages the TS and PES netlists (see netlist.h).
4 *****************************************************************************
5 * Copyright (C) 1998, 1999, 2000 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <sys/types.h> /* on BSD, uio.h needs types.h */
30 #include <sys/uio.h> /* "input.h" */
32 #include <stdlib.h> /* free() */
33 #include <string.h> /* strerror() */
34 #include <errno.h> /* errno */
43 #include "input_netlist.h"
45 /*****************************************************************************
47 *****************************************************************************/
49 /*****************************************************************************
50 * input_NetlistOpen: initialize the netlists buffers
51 *****************************************************************************/
52 int input_NetlistInit( input_thread_t *p_input )
54 int i_base, i_packets, i_iovec;
56 /* Initialize running indexes. */
57 #ifdef INPUT_LIFO_TS_NETLIST
58 p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
60 p_input->netlist.i_ts_start = 0;
61 p_input->netlist.i_ts_end = 0;
63 #ifdef INPUT_LIFO_PES_NETLIST
64 p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
66 p_input->netlist.i_pes_start = 0;
67 p_input->netlist.i_pes_end = 0;
70 /* Initialize all iovec from the TS netlist with the length of a packet */
71 for( i_iovec = 0; i_iovec < INPUT_MAX_TS + INPUT_TS_READ_ONCE; i_iovec++ )
73 p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
76 /* Allocate a big piece of memory to contain the INPUT_MAX_TS TS packets */
77 if( ( p_input->netlist.p_ts_packets = malloc( (INPUT_MAX_TS + 1)
78 * sizeof(ts_packet_t) ) ) == NULL )
80 intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
85 /* Allocate a big piece of memory to contain the INPUT_MAX_PES PES packets */
86 if( !( p_input->netlist.p_pes_packets = malloc( (INPUT_MAX_PES + 1)
87 * sizeof(pes_packet_t) ) ) )
89 intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
91 free( p_input->netlist.p_ts_packets );
95 /* Insert TS packets into the TS netlist */
96 #ifdef INPUT_LIFO_TS_NETLIST
97 i_base = p_input->netlist.i_ts_index;
99 i_base = p_input->netlist.i_ts_start;
101 /* i_base is now the base address to locate free packets in the netlist */
103 for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
105 p_input->netlist.p_ts_free[i_base + i_packets].iov_base
106 = (void *)(p_input->netlist.p_ts_packets + i_packets);
107 /* Initialize TS length. */
108 (p_input->netlist.p_ts_packets[i_packets]).i_payload_end = TS_PACKET_SIZE;
111 /* Insert PES packets into the netlist */
112 #ifdef INPUT_LIFO_PES_NETLIST
113 i_base = p_input->netlist.i_pes_index;
115 i_base = p_input->netlist.i_pes_start;
117 /* i_base is now the base address to locate free packets in the netlist */
119 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
121 p_input->netlist.p_pes_free[i_base + i_packets]
122 = p_input->netlist.p_pes_packets + i_packets;
125 /* the p_pes_header_save buffer is allocated on the fly by the PES
126 demux if needed, and freed with the PES packet when the netlist
127 is destroyed. We initialise the field to NULL so that the demux
128 can determine if it has already allocated this buffer or not. */
129 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
131 p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
137 /*****************************************************************************
138 * input_NetlistClean: clean the netlists buffers
139 *****************************************************************************/
140 void input_NetlistEnd( input_thread_t *p_input )
144 /* free TS netlist */
145 free( p_input->netlist.p_ts_packets );
147 /* free the pes_buffer_save buffers of the PES packets if they have
149 for( i = 0; i < INPUT_MAX_PES + 1; i++ )
151 byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
156 /* free PES netlist */
157 free( p_input->netlist.p_pes_packets );