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 *****************************************************************************/
30 #include <sys/types.h> /* on BSD, uio.h needs types.h */
31 #include <sys/uio.h> /* "input.h" */
33 #include <stdlib.h> /* free() */
34 #include <string.h> /* strerror() */
35 #include <errno.h> /* errno */
44 #include "input_netlist.h"
46 /*****************************************************************************
48 *****************************************************************************/
50 /*****************************************************************************
51 * input_NetlistOpen: initialize the netlists buffers
52 *****************************************************************************/
53 int input_NetlistInit( input_thread_t *p_input )
55 int i_base, i_packets, i_iovec;
57 /* Initialize running indexes. */
58 #ifdef INPUT_LIFO_TS_NETLIST
59 p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
61 p_input->netlist.i_ts_start = 0;
62 p_input->netlist.i_ts_end = 0;
64 #ifdef INPUT_LIFO_PES_NETLIST
65 p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
67 p_input->netlist.i_pes_start = 0;
68 p_input->netlist.i_pes_end = 0;
71 /* Initialize all iovec from the TS netlist with the length of a packet */
72 for( i_iovec = 0; i_iovec < INPUT_MAX_TS + INPUT_TS_READ_ONCE; i_iovec++ )
74 p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
77 /* Allocate a big piece of memory to contain the INPUT_MAX_TS TS packets */
78 if( ( p_input->netlist.p_ts_packets = malloc( (INPUT_MAX_TS + 1)
79 * sizeof(ts_packet_t) ) ) == NULL )
81 intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
86 /* Allocate a big piece of memory to contain the INPUT_MAX_PES PES packets */
87 if( !( p_input->netlist.p_pes_packets = malloc( (INPUT_MAX_PES + 1)
88 * sizeof(pes_packet_t) ) ) )
90 intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
92 free( p_input->netlist.p_ts_packets );
96 /* Insert TS packets into the TS netlist */
97 #ifdef INPUT_LIFO_TS_NETLIST
98 i_base = p_input->netlist.i_ts_index;
100 i_base = p_input->netlist.i_ts_start;
102 /* i_base is now the base address to locate free packets in the netlist */
104 for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
106 p_input->netlist.p_ts_free[i_base + i_packets].iov_base
107 = (void *)(p_input->netlist.p_ts_packets + i_packets);
108 /* Initialize TS length. */
109 (p_input->netlist.p_ts_packets[i_packets]).i_payload_end = TS_PACKET_SIZE;
112 /* Insert PES packets into the netlist */
113 #ifdef INPUT_LIFO_PES_NETLIST
114 i_base = p_input->netlist.i_pes_index;
116 i_base = p_input->netlist.i_pes_start;
118 /* i_base is now the base address to locate free packets in the netlist */
120 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
122 p_input->netlist.p_pes_free[i_base + i_packets]
123 = p_input->netlist.p_pes_packets + i_packets;
126 /* the p_pes_header_save buffer is allocated on the fly by the PES
127 demux if needed, and freed with the PES packet when the netlist
128 is destroyed. We initialise the field to NULL so that the demux
129 can determine if it has already allocated this buffer or not. */
130 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
132 p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
138 /*****************************************************************************
139 * input_NetlistClean: clean the netlists buffers
140 *****************************************************************************/
141 void input_NetlistEnd( input_thread_t *p_input )
145 /* free TS netlist */
146 free( p_input->netlist.p_ts_packets );
148 /* free the pes_buffer_save buffers of the PES packets if they have
150 for( i = 0; i < INPUT_MAX_PES + 1; i++ )
152 byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
157 /* free PES netlist */
158 free( p_input->netlist.p_pes_packets );