1 /*****************************************************************************
2 * netlist.c: input thread
3 * Manages the TS and PES netlists (see netlist.h).
4 *****************************************************************************
5 * Copyright (C) 1998, 1999, 2000 VideoLAN
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 GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <sys/types.h> /* on BSD, uio.h needs types.h */
29 #include <sys/uio.h> /* "input.h" */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
33 #include <errno.h> /* errno */
42 #include "input_netlist.h"
44 /*****************************************************************************
46 *****************************************************************************/
48 /*****************************************************************************
49 * input_NetlistOpen: initialize the netlists buffers
50 *****************************************************************************/
51 int input_NetlistInit( input_thread_t *p_input )
53 int i_base, i_packets, i_iovec;
55 /* Initialize running indexes. */
56 #ifdef INPUT_LIFO_TS_NETLIST
57 p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
59 p_input->netlist.i_ts_start = 0;
60 p_input->netlist.i_ts_end = 0;
62 #ifdef INPUT_LIFO_PES_NETLIST
63 p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
65 p_input->netlist.i_pes_start = 0;
66 p_input->netlist.i_pes_end = 0;
69 /* Initialize all iovec from the TS netlist with the length of a packet */
70 for( i_iovec = 0; i_iovec < INPUT_MAX_TS + INPUT_TS_READ_ONCE; i_iovec++ )
72 p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
75 /* Allocate a big piece of memory to contain the INPUT_MAX_TS TS packets */
76 if( ( p_input->netlist.p_ts_packets = malloc( (INPUT_MAX_TS + 1)
77 * sizeof(ts_packet_t) ) ) == NULL )
79 intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
84 /* Allocate a big piece of memory to contain the INPUT_MAX_PES PES packets */
85 if( !( p_input->netlist.p_pes_packets = malloc( (INPUT_MAX_PES + 1)
86 * sizeof(pes_packet_t) ) ) )
88 intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
90 free( p_input->netlist.p_ts_packets );
94 /* Insert TS packets into the TS netlist */
95 #ifdef INPUT_LIFO_TS_NETLIST
96 i_base = p_input->netlist.i_ts_index;
98 i_base = p_input->netlist.i_ts_start;
100 /* i_base is now the base address to locate free packets in the netlist */
102 for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
104 p_input->netlist.p_ts_free[i_base + i_packets].iov_base
105 = (void *)(p_input->netlist.p_ts_packets + i_packets);
106 /* Initialize TS length. */
107 (p_input->netlist.p_ts_packets[i_packets]).i_payload_end = TS_PACKET_SIZE;
110 /* Insert PES packets into the netlist */
111 #ifdef INPUT_LIFO_PES_NETLIST
112 i_base = p_input->netlist.i_pes_index;
114 i_base = p_input->netlist.i_pes_start;
116 /* i_base is now the base address to locate free packets in the netlist */
118 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
120 p_input->netlist.p_pes_free[i_base + i_packets]
121 = p_input->netlist.p_pes_packets + i_packets;
124 /* the p_pes_header_save buffer is allocated on the fly by the PES
125 demux if needed, and freed with the PES packet when the netlist
126 is destroyed. We initialise the field to NULL so that the demux
127 can determine if it has already allocated this buffer or not. */
128 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
130 p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
136 /*****************************************************************************
137 * input_NetlistClean: clean the netlists buffers
138 *****************************************************************************/
139 void input_NetlistEnd( input_thread_t *p_input )
143 /* free TS netlist */
144 free( p_input->netlist.p_ts_packets );
146 /* free the pes_buffer_save buffers of the PES packets if they have
148 for( i = 0; i < INPUT_MAX_PES + 1; i++ )
150 byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
155 /* free PES netlist */
156 free( p_input->netlist.p_pes_packets );