]> git.sesse.net Git - vlc/blob - plugins/mpeg_system/mpeg_ts.c
Input III (Episode 1).
[vlc] / plugins / mpeg_system / mpeg_ts.c
1 /*****************************************************************************
2  * mpeg_ts.c : Transport Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: mpeg_ts.c,v 1.4 2002/03/01 00:33:18 massiot Exp $
6  *
7  * Authors: Henri Fallon <henri@via.ecp.fr>
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
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <videolan/vlc.h>
32
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36 #include "input_ext-plugins.h"
37
38 /*****************************************************************************
39  * Constants
40  *****************************************************************************/
41 #define TS_READ_ONCE 200
42 #define TS_PACKET_SIZE 188
43 #define TS_SYNC_CODE 0x47
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static void input_getfunctions( function_list_t * p_function_list );
49 static int  TSInit      ( struct input_thread_s * );
50 static void TSEnd       ( struct input_thread_s * );
51 static int  TSDemux     ( struct input_thread_s * );
52
53 /*****************************************************************************
54  * Build configuration tree.
55  *****************************************************************************/
56 MODULE_CONFIG_START
57 MODULE_CONFIG_STOP
58
59 MODULE_INIT_START
60     SET_DESCRIPTION( "ISO 13818-1 MPEG Transport Stream input" )
61     ADD_CAPABILITY( DEMUX, 160 )
62     ADD_SHORTCUT( "ts" )
63 MODULE_INIT_STOP
64
65 MODULE_ACTIVATE_START
66     input_getfunctions( &p_module->p_functions->demux );
67 MODULE_ACTIVATE_STOP
68
69 MODULE_DEACTIVATE_START
70 MODULE_DEACTIVATE_STOP
71
72 /*****************************************************************************
73  * Functions exported as capabilities. They are declared as static so that
74  * we don't pollute the namespace too much.
75  *****************************************************************************/
76 static void input_getfunctions( function_list_t * p_function_list )
77 {
78 #define input p_function_list->functions.demux
79     input.pf_init             = TSInit;
80     input.pf_end              = TSEnd;
81     input.pf_demux            = TSDemux;
82     input.pf_rewind           = NULL;
83 #undef input
84 }
85
86 /*****************************************************************************
87  * TSInit: initializes TS structures
88  *****************************************************************************/
89 static int TSInit( input_thread_t * p_input )
90 {
91     es_descriptor_t     * p_pat_es;
92     es_ts_data_t        * p_demux_data;
93     stream_ts_data_t    * p_stream_data;
94     byte_t              * p_peek;
95
96     /* Initialize access plug-in structures. */
97     if( p_input->i_mtu == 0 )
98     {
99         /* Improve speed. */
100         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
101     }
102
103     /* Have a peep at the show. */
104     if( input_Peek( p_input, &p_peek, 1 ) < 1 )
105     {
106         intf_ErrMsg( "input error: cannot peek() (mpeg_ts)" );
107         return( -1 );
108     }
109
110     if( *p_peek != TS_SYNC_CODE )
111     {
112         if( p_input->psz_demux && strncmp( p_input->psz_demux, "ts", 3 ) )
113         {
114             /* User forced */
115             intf_ErrMsg( "input error: this doesn't seem like a TS stream, continuing" );
116         }
117         else
118         {
119             intf_WarnMsg( 2, "input: TS plug-in discarded (no sync)" );
120             return( -1 );
121         }
122     }
123
124     /* Adapt the bufsize for our only use. */
125     if( p_input->i_mtu != 0 )
126     {
127         /* Have minimum granularity to avoid bottlenecks at the input level. */
128         p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
129     }
130
131     if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
132     {
133         return( -1 );
134     }
135
136     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
137     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
138
139     /* We'll have to catch the PAT in order to continue
140      * Then the input will catch the PMT and then the others ES
141      * The PAT es is indepedent of any program. */
142     p_pat_es = input_AddES( p_input, NULL,
143                             0x00, sizeof( es_ts_data_t ) );
144     p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
145     p_demux_data->b_psi = 1;
146     p_demux_data->i_psi_type = PSI_IS_PAT;
147     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
148     p_demux_data->p_psi_section->b_is_complete = 1;
149
150     return( 0 );
151 }
152
153 /*****************************************************************************
154  * TSEnd: frees unused data
155  *****************************************************************************/
156 static void TSEnd( input_thread_t * p_input )
157 {
158 }
159
160 /*****************************************************************************
161  * TSDemux: reads and demuxes data packets
162  *****************************************************************************
163  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
164  * packets.
165  *****************************************************************************/
166 #define PEEK( SIZE )                                                        \
167     i_error = input_Peek( p_input, &p_peek, SIZE );                         \
168     if( i_error == -1 )                                                     \
169     {                                                                       \
170         return( -1 );                                                       \
171     }                                                                       \
172     else if( i_error < SIZE )                                               \
173     {                                                                       \
174         /* EOF */                                                           \
175         return( 0 );                                                        \
176     }
177
178 static int TSDemux( input_thread_t * p_input )
179 {
180     int             i_read_once = (p_input->i_mtu ?
181                                    p_input->i_bufsize / TS_PACKET_SIZE :
182                                    TS_READ_ONCE);
183     int             i;
184
185     for( i = 0; i < i_read_once; i++ )
186     {
187         data_packet_t *     p_data;
188         ssize_t             i_read, i_error;
189         byte_t *            p_peek;
190
191         PEEK( 1 );
192
193         if( *p_peek != TS_SYNC_CODE )
194         {
195             intf_WarnMsg( 3, "input warning: garbage at input (%x)", *p_peek );
196
197             if( p_input->i_mtu )
198             {
199                 /* Try to resync on next packet. */
200                 PEEK( TS_PACKET_SIZE );
201                 p_input->p_current_data += TS_PACKET_SIZE;
202             }
203             else
204             {
205                 /* Move forward until we find 0x47 (and hope it's the good
206                  * one... FIXME) */
207                 while( *p_peek != TS_SYNC_CODE )
208                 {
209                     p_input->p_current_data++;
210                     PEEK( 1 );
211                 }
212             }
213         }
214
215         i_read = input_SplitBuffer( p_input, &p_data, TS_PACKET_SIZE );
216
217         if( i_read <= 0 )
218         {
219             return( i_read );
220         }
221
222         input_DemuxTS( p_input, p_data );
223     }
224
225     return( i_read_once );
226 }
227