1 /*****************************************************************************
2 * mpeg_es.c : Elementary Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mpeg_es.c,v 1.13 2002/07/31 20:56:52 sam Exp $
7 * Authors: Christophe Massiot <massiot@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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
32 #include <vlc/input.h>
34 #include <sys/types.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 #define ES_PACKET_SIZE 65536
40 #define MAX_PACKETS_IN_FIFO 3
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Activate ( vlc_object_t * );
46 static int Demux ( input_thread_t * );
48 /*****************************************************************************
50 *****************************************************************************/
52 set_description( _("ISO 13818-1 MPEG Elementary Stream input") );
53 set_capability( "demux", 150 );
54 set_callbacks( Activate, NULL );
59 * Data reading functions
62 /*****************************************************************************
63 * Activate: initializes ES structures
64 *****************************************************************************/
65 static int Activate( vlc_object_t * p_this )
67 input_thread_t * p_input = (input_thread_t *)p_this;
68 es_descriptor_t * p_es;
71 /* Set the demux function */
72 p_input->pf_demux = Demux;
74 /* Initialize access plug-in structures. */
75 if( p_input->i_mtu == 0 )
78 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
81 /* Have a peep at the show. */
82 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
84 /* Stream shorter than 4 bytes... */
85 msg_Err( p_input, "cannot peek()" );
89 if( *p_peek || *(p_peek + 1) || *(p_peek + 2) != 1 )
91 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "es", 3 ) )
94 msg_Err( p_input, "this doesn't look like an MPEG ES stream, continuing" );
98 msg_Warn( p_input, "ES module discarded (no startcode)" );
102 else if( *(p_peek + 3) > 0xb9 )
104 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "es", 3 ) )
107 msg_Err( p_input, "this seems to be a system stream (PS plug-in ?), but continuing" );
111 msg_Warn( p_input, "ES module discarded (system startcode)" );
116 if( input_InitStream( p_input, 0 ) == -1 )
120 input_AddProgram( p_input, 0, 0 );
121 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
122 vlc_mutex_lock( &p_input->stream.stream_lock );
123 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xE0, 0 );
124 p_es->i_stream_id = 0xE0;
125 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
126 p_es->i_cat = VIDEO_ES;
127 input_SelectES( p_input, p_es );
128 p_input->stream.p_selected_area->i_tell = 0;
129 p_input->stream.p_selected_program->b_is_ok = 1;
130 vlc_mutex_unlock( &p_input->stream.stream_lock );
135 /*****************************************************************************
136 * Demux: reads and demuxes data packets
137 *****************************************************************************
138 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
139 *****************************************************************************/
140 static int Demux( input_thread_t * p_input )
143 decoder_fifo_t * p_fifo =
144 p_input->stream.p_selected_program->pp_es[0]->p_decoder_fifo;
145 pes_packet_t * p_pes;
146 data_packet_t * p_data;
153 i_read = input_SplitBuffer( p_input, &p_data, ES_PACKET_SIZE );
160 p_pes = input_NewPES( p_input->p_method_data );
164 msg_Err( p_input, "out of memory" );
165 input_DeletePacket( p_input->p_method_data, p_data );
169 p_pes->i_rate = p_input->stream.control.i_rate;
170 p_pes->p_first = p_pes->p_last = p_data;
171 p_pes->i_nb_data = 1;
173 vlc_mutex_lock( &p_fifo->data_lock );
175 if( p_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
177 /* Wait for the decoder. */
178 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
180 vlc_mutex_unlock( &p_fifo->data_lock );
182 if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
183 | (input_ClockManageControl( p_input,
184 p_input->stream.p_selected_program,
185 (mtime_t)0 ) == PAUSE_S) )
187 msg_Warn( p_input, "synchro reinit" );
188 p_pes->i_pts = mdate() + DEFAULT_PTS_DELAY;
189 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
192 input_DecodePES( p_fifo, p_pes );