]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
Fin du remplacement des pthread + ajout du frame rate dans display.c.
[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 <sys/uio.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include "common.h"
18 #include "config.h"
19 #include "mtime.h"
20 #include "vlc_thread.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     /* 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++ )
111     {
112       p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
113     }
114
115     return( 0 );
116 }
117
118 /******************************************************************************
119  * input_NetlistClean: clean the netlists buffers
120  ******************************************************************************/
121 void input_NetlistClean( input_thread_t *p_input )
122 {
123     int i;
124
125     /* free TS netlist */
126     free( p_input->netlist.p_ts_packets );
127
128     /* free the pes_buffer_save buffers of the PES packets if they have
129        been allocated */
130     for( i = 0; i < INPUT_MAX_PES + 1; i++ )
131     {
132       byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
133       if(p_buffer)
134         free(p_buffer);
135     }
136
137     /* free PES netlist */
138     free( p_input->netlist.p_pes_packets );
139 }
140