1 /*******************************************************************************
2 * netlist.c: input thread
4 *******************************************************************************
5 * Manages the TS and PES netlists (see netlist.h).
6 *******************************************************************************/
8 /*******************************************************************************
10 *******************************************************************************/
11 #include <sys/types.h>
21 #include "vlc_thread.h"
25 #include "input_netlist.h"
27 /******************************************************************************
29 ******************************************************************************/
31 /******************************************************************************
32 * input_NetlistOpen: initialize the netlists buffers
33 ******************************************************************************/
34 int input_NetlistOpen( input_thread_t *p_input )
36 int i_base, i_packets, i_iovec;
38 /* Initialize running indexes. */
39 #ifdef INPUT_LIFO_TS_NETLIST
40 p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
42 p_input->netlist.i_ts_start = 0;
43 p_input->netlist.i_ts_end = 0;
45 #ifdef INPUT_LIFO_PES_NETLIST
46 p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
48 p_input->netlist.i_pes_start = 0;
49 p_input->netlist.i_pes_end = 0;
52 /* Initialize all iovec from the TS netlist with the length of a packet */
53 for( i_iovec = 0; i_iovec < INPUT_MAX_TS + INPUT_TS_READ_ONCE; i_iovec++ )
55 p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
58 /* Allocate a big piece of memory to contain the INPUT_MAX_TS TS packets */
59 if( ( p_input->netlist.p_ts_packets = malloc( (INPUT_MAX_TS + 1)
60 * sizeof(ts_packet_t) ) ) == NULL )
62 intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
67 /* Allocate a big piece of memory to contain the INPUT_MAX_PES PES packets */
68 if( !( p_input->netlist.p_pes_packets = malloc( (INPUT_MAX_PES + 1)
69 * sizeof(pes_packet_t) ) ) )
71 intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
73 free( p_input->netlist.p_ts_packets );
77 /* Insert TS packets into the TS netlist */
78 #ifdef INPUT_LIFO_TS_NETLIST
79 i_base = p_input->netlist.i_ts_index;
81 i_base = p_input->netlist.i_ts_start;
83 /* i_base is now the base address to locate free packets in the netlist */
85 for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
87 p_input->netlist.p_ts_free[i_base + i_packets].iov_base
88 = (p_input->netlist.p_ts_packets + i_packets);
89 /* Initialize TS length. */
90 (p_input->netlist.p_ts_packets[i_packets]).i_payload_end = TS_PACKET_SIZE;
93 /* Insert PES packets into the netlist */
94 #ifdef INPUT_LIFO_PES_NETLIST
95 i_base = p_input->netlist.i_pes_index;
97 i_base = p_input->netlist.i_pes_start;
99 /* i_base is now the base address to locate free packets in the netlist */
101 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
103 p_input->netlist.p_pes_free[i_base + i_packets]
104 = p_input->netlist.p_pes_packets + i_packets;
107 /* the p_pes_header_save buffer is allocated on the fly by the PES
108 demux if needed, and freed with the PES packet when the netlist
109 is destroyed. We initialise the field to NULL so that the demux
110 can determine if it has already allocated this buffer or not. */
111 for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
113 p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
119 /******************************************************************************
120 * input_NetlistClean: clean the netlists buffers
121 ******************************************************************************/
122 void input_NetlistClean( input_thread_t *p_input )
126 /* free TS netlist */
127 free( p_input->netlist.p_ts_packets );
129 /* free the pes_buffer_save buffers of the PES packets if they have
131 for( i = 0; i < INPUT_MAX_PES + 1; i++ )
133 byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
138 /* free PES netlist */
139 free( p_input->netlist.p_pes_packets );