]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
Initial revision
[vlc] / src / input / input_netlist.c
1 /*******************************************************************************
2  * netlist.c: input thread 
3  * (c)1998 VideoLAN
4  *******************************************************************************
5  * Manages the TS and PES netlists (see netlist.h).
6  *******************************************************************************/
7
8 /*******************************************************************************
9  * Preamble
10  *******************************************************************************/
11 #include <pthread.h>
12 #include <sys/uio.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include "common.h"
19 #include "config.h"
20 #include "mtime.h"
21 #include "intf_msg.h"
22 #include "debug.h"
23 #include "input.h"
24 #include "input_netlist.h"
25
26 /******************************************************************************
27  * Local prototypes
28  ******************************************************************************/
29
30 /******************************************************************************
31  * input_NetlistOpen: initialize the netlists buffers
32  ******************************************************************************/
33 int input_NetlistOpen( input_thread_t *p_input )
34 {
35     int                 i_base, i_packets, i_iovec;
36
37     /* Initialize running indexes. */
38 #ifdef INPUT_LIFO_TS_NETLIST
39     p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
40 #else
41     p_input->netlist.i_ts_start = 0;
42     p_input->netlist.i_ts_end = 0;
43 #endif
44 #ifdef INPUT_LIFO_PES_NETLIST
45     p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
46 #else
47     p_input->netlist.i_pes_start = 0;
48     p_input->netlist.i_pes_end = 0;
49 #endif
50
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++ )
53     {
54         p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
55     }
56
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 )
60     {
61         intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
62                     strerror(errno) );
63         return( -1 );
64     }
65
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) ) ) )
69     {
70         intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
71                     strerror(errno) );
72         free( p_input->netlist.p_ts_packets );
73         return( -1 );
74     }
75
76     /* Insert TS packets into the TS netlist */
77 #ifdef INPUT_LIFO_TS_NETLIST
78     i_base = p_input->netlist.i_ts_index;
79 #else
80     i_base = p_input->netlist.i_ts_start;
81 #endif
82     /* i_base is now the base address to locate free packets in the netlist */
83
84     for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
85     {
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;
90     }
91
92     /* Insert PES packets into the netlist */
93 #ifdef INPUT_LIFO_PES_NETLIST
94     i_base = p_input->netlist.i_pes_index;
95 #else
96     i_base = p_input->netlist.i_pes_start;
97 #endif
98     /* i_base is now the base address to locate free packets in the netlist */
99
100     for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
101     {
102         p_input->netlist.p_pes_free[i_base + i_packets]
103                               = p_input->netlist.p_pes_packets + i_packets;
104     }
105
106     return( 0 );
107 }
108
109 /******************************************************************************
110  * input_NetlistClean: clean the netlists buffers
111  ******************************************************************************/
112 void input_NetlistClean( input_thread_t *p_input )
113 {
114     free( p_input->netlist.p_ts_packets );                 /* free TS netlist */
115     free( p_input->netlist.p_pes_packets );               /* free PES netlist */
116 }
117