1 /*******************************************************************************
2 * netlist.c: input thread
4 *******************************************************************************
5 * Manages the TS and PES netlists (see netlist.h).
6 *******************************************************************************/
8 /*******************************************************************************
10 *******************************************************************************/
20 #include "vlc_thread.h"
24 #include "input_netlist.h"
26 /******************************************************************************
28 ******************************************************************************/
30 /******************************************************************************
31 * input_NetlistOpen: initialize the netlists buffers
32 ******************************************************************************/
33 int input_NetlistOpen( input_thread_t *p_input )
35 int i_base, i_packets, i_iovec;
37 /* Initialize running indexes. */
38 #ifdef INPUT_LIFO_TS_NETLIST
39 p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
41 p_input->netlist.i_ts_start = 0;
42 p_input->netlist.i_ts_end = 0;
44 #ifdef INPUT_LIFO_PES_NETLIST
45 p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
47 p_input->netlist.i_pes_start = 0;
48 p_input->netlist.i_pes_end = 0;
51 /* Initialize all iovec from the TS netlist with the length of a packet */
52 for( i_iovec = 0; i_iovec < INPUT_MAX_TS + INPUT_TS_READ_ONCE; i_iovec++ )
54 p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
57 /* Allocate a big piece of memory to contain the INPUT_MAX_TS TS packets */
58 if( ( p_input->netlist.p_ts_packets = malloc( (INPUT_MAX_TS + 1)
59 * sizeof(ts_packet_t) ) ) == NULL )
61 intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
66 /* Allocate a big piece of memory to contain the INPUT_MAX_PES PES packets */
67 if( !( p_input->netlist.p_pes_packets = malloc( (INPUT_MAX_PES + 1)
68 * sizeof(pes_packet_t) ) ) )
70 intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
72 free( p_input->netlist.p_ts_packets );
76 /* Insert TS packets into the TS netlist */
77 #ifdef INPUT_LIFO_TS_NETLIST
78 i_base = p_input->netlist.i_ts_index;
80 i_base = p_input->netlist.i_ts_start;
82 /* i_base is now the base address to locate free packets in the netlist */
84 for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
86 p_input->netlist.p_ts_free[i_base + i_packets].iov_base
87 = (p_input->netlist.p_ts_packets + i_packets);
88 /* Initialize TS length. */
89 (p_input->netlist.p_ts_packets[i_packets]).i_payload_end = TS_PACKET_SIZE;
92 /* Insert PES packets into the netlist */
93 #ifdef INPUT_LIFO_PES_NETLIST
94 i_base = p_input->netlist.i_pes_index;
96 i_base = p_input->netlist.i_pes_start;
98 /* i_base is now the base address to locate free packets in the netlist */
100 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
102 p_input->netlist.p_pes_free[i_base + i_packets]
103 = p_input->netlist.p_pes_packets + i_packets;
106 /* the p_pes_header_save buffer is allocated on the fly by the PES
107 demux if needed, and freed with the PES packet when the netlist
108 is destroyed. We initialise the field to NULL so that the demux
109 can determine if it has already allocated this buffer or not. */
110 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
112 p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
118 /******************************************************************************
119 * input_NetlistClean: clean the netlists buffers
120 ******************************************************************************/
121 void input_NetlistClean( input_thread_t *p_input )
125 /* free TS netlist */
126 free( p_input->netlist.p_ts_packets );
128 /* free the pes_buffer_save buffers of the PES packets if they have
130 for( i = 0; i < INPUT_MAX_PES + 1; i++ )
132 byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
137 /* free PES netlist */
138 free( p_input->netlist.p_pes_packets );