]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
Changement de chaine. Delicat encore, mais il marche. Quelques corrections
[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/types.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 "vlc_thread.h"
22 #include "intf_msg.h"
23 #include "debug.h"
24 #include "input.h"
25 #include "input_netlist.h"
26
27 /******************************************************************************
28  * Local prototypes
29  ******************************************************************************/
30
31 /******************************************************************************
32  * input_NetlistOpen: initialize the netlists buffers
33  ******************************************************************************/
34 int input_NetlistInit( input_thread_t *p_input )
35 {
36     int                 i_base, i_packets, i_iovec;
37
38     /* Initialize running indexes. */
39 #ifdef INPUT_LIFO_TS_NETLIST
40     p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
41 #else
42     p_input->netlist.i_ts_start = 0;
43     p_input->netlist.i_ts_end = 0;
44 #endif
45 #ifdef INPUT_LIFO_PES_NETLIST
46     p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
47 #else
48     p_input->netlist.i_pes_start = 0;
49     p_input->netlist.i_pes_end = 0;
50 #endif
51
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++ )
54     {
55         p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
56     }
57
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 )
61     {
62         intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
63                     strerror(errno) );
64         return( -1 );
65     }
66
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) ) ) )
70     {
71         intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
72                     strerror(errno) );
73         free( p_input->netlist.p_ts_packets );
74         return( -1 );
75     }
76
77     /* Insert TS packets into the TS netlist */
78 #ifdef INPUT_LIFO_TS_NETLIST
79     i_base = p_input->netlist.i_ts_index;
80 #else
81     i_base = p_input->netlist.i_ts_start;
82 #endif
83     /* i_base is now the base address to locate free packets in the netlist */
84
85     for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
86     {
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;
91     }
92
93     /* Insert PES packets into the netlist */
94 #ifdef INPUT_LIFO_PES_NETLIST
95     i_base = p_input->netlist.i_pes_index;
96 #else
97     i_base = p_input->netlist.i_pes_start;
98 #endif
99     /* i_base is now the base address to locate free packets in the netlist */
100
101     for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
102     {
103         p_input->netlist.p_pes_free[i_base + i_packets]
104                               = p_input->netlist.p_pes_packets + i_packets;
105     }
106
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++ )
112     {
113       p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
114     }
115
116     return( 0 );
117 }
118
119 /******************************************************************************
120  * input_NetlistClean: clean the netlists buffers
121  ******************************************************************************/
122 void input_NetlistEnd( input_thread_t *p_input )
123 {
124     int i;
125
126     /* free TS netlist */
127     free( p_input->netlist.p_ts_packets );
128
129     /* free the pes_buffer_save buffers of the PES packets if they have
130        been allocated */
131     for( i = 0; i < INPUT_MAX_PES + 1; i++ )
132     {
133       byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
134       if(p_buffer)
135         free(p_buffer);
136     }
137
138     /* free PES netlist */
139     free( p_input->netlist.p_pes_packets );
140 }
141