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 $
7 * Authors: Henri Fallon <henri@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <videolan/vlc.h>
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36 #include "input_ext-plugins.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 #define TS_READ_ONCE 200
42 #define TS_PACKET_SIZE 188
43 #define TS_SYNC_CODE 0x47
45 /*****************************************************************************
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 * );
53 /*****************************************************************************
54 * Build configuration tree.
55 *****************************************************************************/
60 SET_DESCRIPTION( "ISO 13818-1 MPEG Transport Stream input" )
61 ADD_CAPABILITY( DEMUX, 160 )
66 input_getfunctions( &p_module->p_functions->demux );
69 MODULE_DEACTIVATE_START
70 MODULE_DEACTIVATE_STOP
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 )
78 #define input p_function_list->functions.demux
79 input.pf_init = TSInit;
81 input.pf_demux = TSDemux;
82 input.pf_rewind = NULL;
86 /*****************************************************************************
87 * TSInit: initializes TS structures
88 *****************************************************************************/
89 static int TSInit( input_thread_t * p_input )
91 es_descriptor_t * p_pat_es;
92 es_ts_data_t * p_demux_data;
93 stream_ts_data_t * p_stream_data;
96 /* Initialize access plug-in structures. */
97 if( p_input->i_mtu == 0 )
100 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
103 /* Have a peep at the show. */
104 if( input_Peek( p_input, &p_peek, 1 ) < 1 )
106 intf_ErrMsg( "input error: cannot peek() (mpeg_ts)" );
110 if( *p_peek != TS_SYNC_CODE )
112 if( p_input->psz_demux && strncmp( p_input->psz_demux, "ts", 3 ) )
115 intf_ErrMsg( "input error: this doesn't seem like a TS stream, continuing" );
119 intf_WarnMsg( 2, "input: TS plug-in discarded (no sync)" );
124 /* Adapt the bufsize for our only use. */
125 if( p_input->i_mtu != 0 )
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;
131 if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
136 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
137 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
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;
153 /*****************************************************************************
154 * TSEnd: frees unused data
155 *****************************************************************************/
156 static void TSEnd( input_thread_t * p_input )
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
165 *****************************************************************************/
166 #define PEEK( SIZE ) \
167 i_error = input_Peek( p_input, &p_peek, SIZE ); \
168 if( i_error == -1 ) \
172 else if( i_error < SIZE ) \
178 static int TSDemux( input_thread_t * p_input )
180 int i_read_once = (p_input->i_mtu ?
181 p_input->i_bufsize / TS_PACKET_SIZE :
185 for( i = 0; i < i_read_once; i++ )
187 data_packet_t * p_data;
188 ssize_t i_read, i_error;
193 if( *p_peek != TS_SYNC_CODE )
195 intf_WarnMsg( 3, "input warning: garbage at input (%x)", *p_peek );
199 /* Try to resync on next packet. */
200 PEEK( TS_PACKET_SIZE );
201 p_input->p_current_data += TS_PACKET_SIZE;
205 /* Move forward until we find 0x47 (and hope it's the good
207 while( *p_peek != TS_SYNC_CODE )
209 p_input->p_current_data++;
215 i_read = input_SplitBuffer( p_input, &p_data, TS_PACKET_SIZE );
222 input_DemuxTS( p_input, p_data );
225 return( i_read_once );