]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
. normalement on devrait se prendre 1 seul mail par commit gr�ce aux
[vlc] / src / input / input_netlist.c
1 /*****************************************************************************
2  * netlist.c: input thread
3  * Manages the TS and PES netlists (see netlist.h).
4  *****************************************************************************
5  * Copyright (C) 1998, 1999, 2000 VideoLAN
6  *
7  * Authors:
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public
20  * License along with this program; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
31 #include <sys/uio.h>                                            /* "input.h" */
32
33 #include <stdlib.h>                                                /* free() */
34 #include <string.h>                                            /* strerror() */
35 #include <errno.h>                                                  /* errno */
36
37 #include "config.h"
38 #include "common.h"
39 #include "threads.h"
40 #include "mtime.h"
41 #include "intf_msg.h"
42 #include "debug.h"
43 #include "input.h"
44 #include "input_netlist.h"
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49
50 /*****************************************************************************
51  * input_NetlistOpen: initialize the netlists buffers
52  *****************************************************************************/
53 int input_NetlistInit( input_thread_t *p_input )
54 {
55     int                 i_base, i_packets, i_iovec;
56
57     /* Initialize running indexes. */
58 #ifdef INPUT_LIFO_TS_NETLIST
59     p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
60 #else
61     p_input->netlist.i_ts_start = 0;
62     p_input->netlist.i_ts_end = 0;
63 #endif
64 #ifdef INPUT_LIFO_PES_NETLIST
65     p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
66 #else
67     p_input->netlist.i_pes_start = 0;
68     p_input->netlist.i_pes_end = 0;
69 #endif
70
71     /* Initialize all iovec from the TS netlist with the length of a packet */
72     for( i_iovec = 0; i_iovec < INPUT_MAX_TS + INPUT_TS_READ_ONCE; i_iovec++ )
73     {
74         p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
75     }
76
77     /* Allocate a big piece of memory to contain the INPUT_MAX_TS TS packets */
78     if( ( p_input->netlist.p_ts_packets = malloc( (INPUT_MAX_TS + 1)
79                                              * sizeof(ts_packet_t) ) ) == NULL )
80     {
81         intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
82                     strerror(errno) );
83         return( -1 );
84     }
85
86   /* Allocate a big piece of memory to contain the INPUT_MAX_PES PES packets */
87     if( !( p_input->netlist.p_pes_packets = malloc( (INPUT_MAX_PES + 1)
88                                              * sizeof(pes_packet_t) ) ) )
89     {
90         intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
91                     strerror(errno) );
92         free( p_input->netlist.p_ts_packets );
93         return( -1 );
94     }
95
96     /* Insert TS packets into the TS netlist */
97 #ifdef INPUT_LIFO_TS_NETLIST
98     i_base = p_input->netlist.i_ts_index;
99 #else
100     i_base = p_input->netlist.i_ts_start;
101 #endif
102     /* i_base is now the base address to locate free packets in the netlist */
103
104     for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
105     {
106         p_input->netlist.p_ts_free[i_base + i_packets].iov_base
107                           = (void *)(p_input->netlist.p_ts_packets + i_packets);
108         /* Initialize TS length. */
109         (p_input->netlist.p_ts_packets[i_packets]).i_payload_end = TS_PACKET_SIZE;
110     }
111
112     /* Insert PES packets into the netlist */
113 #ifdef INPUT_LIFO_PES_NETLIST
114     i_base = p_input->netlist.i_pes_index;
115 #else
116     i_base = p_input->netlist.i_pes_start;
117 #endif
118     /* i_base is now the base address to locate free packets in the netlist */
119
120     for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
121     {
122         p_input->netlist.p_pes_free[i_base + i_packets]
123                               = p_input->netlist.p_pes_packets + i_packets;
124     }
125
126     /* the p_pes_header_save buffer is allocated on the fly by the PES
127        demux if needed, and freed with the PES packet when the netlist
128        is destroyed. We initialise the field to NULL so that the demux
129        can determine if it has already allocated this buffer or not. */
130     for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
131     {
132       p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
133     }
134
135     return( 0 );
136 }
137
138 /*****************************************************************************
139  * input_NetlistClean: clean the netlists buffers
140  *****************************************************************************/
141 void input_NetlistEnd( input_thread_t *p_input )
142 {
143     int i;
144
145     /* free TS netlist */
146     free( p_input->netlist.p_ts_packets );
147
148     /* free the pes_buffer_save buffers of the PES packets if they have
149        been allocated */
150     for( i = 0; i < INPUT_MAX_PES + 1; i++ )
151     {
152         byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
153         if(p_buffer)
154             free(p_buffer);
155     }
156
157     /* free PES netlist */
158     free( p_input->netlist.p_pes_packets );
159 }
160