]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
- �a compile sous FreeBSD (mais �a ne tourne pas)
[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 <sys/types.h>                        /* on BSD, uio.h needs types.h */
29 #include <sys/uio.h>                                            /* "input.h" */
30
31 #include <stdlib.h>                                                /* free() */
32 #include <string.h>                                            /* strerror() */
33 #include <errno.h>                                                  /* errno */
34
35 #include "common.h"
36 #include "config.h"
37 #include "mtime.h"
38 #include "threads.h"
39 #include "intf_msg.h"
40 #include "debug.h"
41 #include "input.h"
42 #include "input_netlist.h"
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47
48 /*****************************************************************************
49  * input_NetlistOpen: initialize the netlists buffers
50  *****************************************************************************/
51 int input_NetlistInit( input_thread_t *p_input )
52 {
53     int                 i_base, i_packets, i_iovec;
54
55     /* Initialize running indexes. */
56 #ifdef INPUT_LIFO_TS_NETLIST
57     p_input->netlist.i_ts_index = INPUT_TS_READ_ONCE;
58 #else
59     p_input->netlist.i_ts_start = 0;
60     p_input->netlist.i_ts_end = 0;
61 #endif
62 #ifdef INPUT_LIFO_PES_NETLIST
63     p_input->netlist.i_pes_index = 1; /* We allocate one PES at a time */
64 #else
65     p_input->netlist.i_pes_start = 0;
66     p_input->netlist.i_pes_end = 0;
67 #endif
68
69     /* Initialize all iovec from the TS netlist with the length of a packet */
70     for( i_iovec = 0; i_iovec < INPUT_MAX_TS + INPUT_TS_READ_ONCE; i_iovec++ )
71     {
72         p_input->netlist.p_ts_free[i_iovec].iov_len = TS_PACKET_SIZE;
73     }
74
75     /* Allocate a big piece of memory to contain the INPUT_MAX_TS TS packets */
76     if( ( p_input->netlist.p_ts_packets = malloc( (INPUT_MAX_TS + 1)
77                                              * sizeof(ts_packet_t) ) ) == NULL )
78     {
79         intf_ErrMsg("input error: can't allocate TS netlist buffer (%s)\n",
80                     strerror(errno) );
81         return( -1 );
82     }
83
84   /* Allocate a big piece of memory to contain the INPUT_MAX_PES PES packets */
85     if( !( p_input->netlist.p_pes_packets = malloc( (INPUT_MAX_PES + 1)
86                                              * sizeof(pes_packet_t) ) ) )
87     {
88         intf_ErrMsg("input error: can't allocate PES netlist buffer (%s)\n",
89                     strerror(errno) );
90         free( p_input->netlist.p_ts_packets );
91         return( -1 );
92     }
93
94     /* Insert TS packets into the TS netlist */
95 #ifdef INPUT_LIFO_TS_NETLIST
96     i_base = p_input->netlist.i_ts_index;
97 #else
98     i_base = p_input->netlist.i_ts_start;
99 #endif
100     /* i_base is now the base address to locate free packets in the netlist */
101
102     for( i_packets = 0; i_packets < INPUT_MAX_TS + 1; i_packets++ )
103     {
104         p_input->netlist.p_ts_free[i_base + i_packets].iov_base
105                           = (void *)(p_input->netlist.p_ts_packets + i_packets);
106         /* Initialize TS length. */
107         (p_input->netlist.p_ts_packets[i_packets]).i_payload_end = TS_PACKET_SIZE;
108     }
109
110     /* Insert PES packets into the netlist */
111 #ifdef INPUT_LIFO_PES_NETLIST
112     i_base = p_input->netlist.i_pes_index;
113 #else
114     i_base = p_input->netlist.i_pes_start;
115 #endif
116     /* i_base is now the base address to locate free packets in the netlist */
117
118     for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
119     {
120         p_input->netlist.p_pes_free[i_base + i_packets]
121                               = p_input->netlist.p_pes_packets + i_packets;
122     }
123
124     /* the p_pes_header_save buffer is allocated on the fly by the PES
125        demux if needed, and freed with the PES packet when the netlist
126        is destroyed. We initialise the field to NULL so that the demux
127        can determine if it has already allocated this buffer or not. */
128     for( i_packets = 0; i_packets < INPUT_MAX_PES + 1; i_packets++ )
129     {
130       p_input->netlist.p_pes_packets[i_packets].p_pes_header_save = NULL;
131     }
132
133     return( 0 );
134 }
135
136 /*****************************************************************************
137  * input_NetlistClean: clean the netlists buffers
138  *****************************************************************************/
139 void input_NetlistEnd( input_thread_t *p_input )
140 {
141     int i;
142
143     /* free TS netlist */
144     free( p_input->netlist.p_ts_packets );
145
146     /* free the pes_buffer_save buffers of the PES packets if they have
147        been allocated */
148     for( i = 0; i < INPUT_MAX_PES + 1; i++ )
149     {
150         byte_t* p_buffer = p_input->netlist.p_pes_packets[i].p_pes_header_save;
151         if(p_buffer)
152             free(p_buffer);
153     }
154
155     /* free PES netlist */
156     free( p_input->netlist.p_pes_packets );
157 }
158