1 /*****************************************************************************
2 * input_es.c: Elementary Stream demux and packet management
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: input_es.c,v 1.12 2001/11/11 01:32:03 stef Exp $
7 * Author: 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 #define MODULE_NAME es
25 #include "modules_inner.h"
27 /*****************************************************************************
29 *****************************************************************************/
36 #include <sys/types.h>
46 # include <io.h> /* read() */
48 # include <sys/uio.h> /* struct iovec */
58 # include "input_iovec.h"
65 #include "stream_control.h"
66 #include "input_ext-intf.h"
67 #include "input_ext-dec.h"
68 #include "input_ext-plugins.h"
75 #include "modules_export.h"
77 /*****************************************************************************
79 *****************************************************************************/
80 static int ESProbe ( probedata_t * );
81 static int ESRead ( struct input_thread_s *,
82 data_packet_t * p_packets[INPUT_READ_ONCE] );
83 static void ESInit ( struct input_thread_s * );
84 static void ESEnd ( struct input_thread_s * );
85 static void ESSeek ( struct input_thread_s *, off_t );
86 static void ESDemux ( struct input_thread_s *, struct data_packet_s * );
87 static void ESNextDataPacket( struct bit_stream_s * );
88 static void ESInitBitstream( struct bit_stream_s *, struct decoder_fifo_s *,
89 void (* pf_bitstream_callback)( struct bit_stream_s *,
94 /*****************************************************************************
95 * Functions exported as capabilities. They are declared as static so that
96 * we don't pollute the namespace too much.
97 *****************************************************************************/
98 void _M( input_getfunctions )( function_list_t * p_function_list )
100 #define input p_function_list->functions.input
101 p_function_list->pf_probe = ESProbe;
102 input.pf_init = ESInit;
103 input.pf_open = NULL;
104 input.pf_close = NULL;
105 input.pf_end = ESEnd;
106 input.pf_init_bit_stream = ESInitBitstream;
107 input.pf_set_area = NULL;
108 input.pf_read = ESRead;
109 input.pf_demux = ESDemux;
110 input.pf_new_packet = input_NetlistNewPacket;
111 input.pf_new_pes = input_NetlistNewPES;
112 input.pf_delete_packet = input_NetlistDeletePacket;
113 input.pf_delete_pes = input_NetlistDeletePES;
114 input.pf_rewind = NULL;
115 input.pf_seek = ESSeek;
120 * Data reading functions
123 /*****************************************************************************
124 * ESProbe: verifies that the stream is a ES stream
125 *****************************************************************************/
126 static int ESProbe( probedata_t *p_data )
130 if( TestMethod( INPUT_METHOD_VAR, "es" ) )
138 /*****************************************************************************
139 * ESInit: initializes ES structures
140 *****************************************************************************/
141 static void ESInit( input_thread_t * p_input )
143 es_descriptor_t * p_es;
145 p_input->p_method_data = NULL;
147 /* Initialize netlist */
148 if( input_NetlistInit( p_input, NB_DATA, NB_DATA, NB_PES, ES_PACKET_SIZE,
151 intf_ErrMsg( "ES input : Could not initialize netlist" );
155 /* FIXME : detect if InitStream failed */
156 input_InitStream( p_input, 0 );
157 input_AddProgram( p_input, 0, 0 );
158 vlc_mutex_lock( &p_input->stream.stream_lock );
159 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xE0, 0 );
160 p_es->i_stream_id = 0xE0;
161 p_es->i_type = MPEG1_VIDEO_ES;
162 p_es->i_cat = VIDEO_ES;
163 input_SelectES( p_input, p_es );
164 p_input->stream.p_selected_area->i_tell = 0;
165 p_input->stream.pp_programs[0]->b_is_ok = 1;
166 vlc_mutex_unlock( &p_input->stream.stream_lock );
169 /*****************************************************************************
170 * ESEnd: frees unused data
171 *****************************************************************************/
172 static void ESEnd( input_thread_t * p_input )
176 /*****************************************************************************
177 * ESRead: reads data packets
178 *****************************************************************************
179 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
181 *****************************************************************************/
182 static int ESRead( input_thread_t * p_input,
183 data_packet_t * pp_packets[INPUT_READ_ONCE] )
186 struct iovec * p_iovec;
189 p_iovec = input_NetlistGetiovec( p_input->p_method_data );
191 if ( p_iovec == NULL )
193 return( -1 ); /* empty netlist */
196 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
198 i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
201 intf_ErrMsg( "input error: ES readv error" );
205 input_NetlistMviovec( p_input->p_method_data,
206 (int)(i_read/ES_PACKET_SIZE), pp_packets );
208 p_input->stream.p_selected_area->i_tell += i_read;
213 /*****************************************************************************
214 * ESSeek: changes the stream position indicator
215 *****************************************************************************/
216 static void ESSeek( input_thread_t * p_input, off_t i_position )
218 lseek( p_input->i_handle, i_position, SEEK_SET );
220 p_input->stream.p_selected_area->i_tell = i_position;
223 /*****************************************************************************
224 * ESDemux: fakes a demultiplexer
225 *****************************************************************************/
226 static void ESDemux( input_thread_t * p_input, data_packet_t * p_data )
228 pes_packet_t * p_pes = p_input->pf_new_pes( p_input->p_method_data );
229 decoder_fifo_t * p_fifo =
230 p_input->stream.pp_programs[0]->pp_es[0]->p_decoder_fifo;
234 intf_ErrMsg("Out of memory");
235 p_input->b_error = 1;
239 p_pes->i_rate = p_input->stream.control.i_rate;
240 p_pes->p_first = p_data;
242 if( (p_input->stream.pp_programs[0]->i_synchro_state == SYNCHRO_REINIT)
243 | (input_ClockManageControl( p_input, p_input->stream.pp_programs[0],
244 (mtime_t)0 ) == PAUSE_S) )
246 intf_WarnMsg( 2, "synchro reinit" );
247 p_pes->i_pts = mdate() + DEFAULT_PTS_DELAY;
248 p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_OK;
251 input_DecodePES( p_fifo, p_pes );
253 vlc_mutex_lock( &p_fifo->data_lock );
254 if( ( (DECODER_FIFO_END( *p_fifo ) - DECODER_FIFO_START( *p_fifo ))
255 & FIFO_SIZE ) >= MAX_PACKETS_IN_FIFO )
257 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
259 vlc_mutex_unlock( &p_fifo->data_lock );
262 /*****************************************************************************
263 * ESNextDataPacket: signals the input thread if there isn't enough packets
265 *****************************************************************************/
266 static void ESNextDataPacket( bit_stream_t * p_bit_stream )
268 decoder_fifo_t * p_fifo = p_bit_stream->p_decoder_fifo;
271 /* We are looking for the next data packet that contains real data,
272 * and not just a PES header */
275 /* We were reading the last data packet of this PES packet... It's
276 * time to jump to the next PES packet */
277 if( p_bit_stream->p_data->p_next == NULL )
279 /* We are going to read/write the start and end indexes of the
280 * decoder fifo and to use the fifo's conditional variable,
281 * that's why we need to take the lock before. */
282 vlc_mutex_lock( &p_fifo->data_lock );
284 /* Free the previous PES packet. */
285 p_fifo->pf_delete_pes( p_fifo->p_packets_mgt,
286 DECODER_FIFO_START( *p_fifo ) );
287 DECODER_FIFO_INCSTART( *p_fifo );
289 if( DECODER_FIFO_ISEMPTY( *p_fifo ) )
291 /* Signal the input thread we're waiting. */
292 vlc_cond_signal( &p_fifo->data_wait );
294 /* Wait for the input to tell us when we receive a packet. */
295 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
298 /* The next byte could be found in the next PES packet */
299 p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
301 vlc_mutex_unlock( &p_fifo->data_lock );
307 /* Perhaps the next data packet of the current PES packet contains
308 * real data (ie its payload's size is greater than 0). */
309 p_bit_stream->p_data = p_bit_stream->p_data->p_next;
313 } while ( p_bit_stream->p_data->p_payload_start
314 == p_bit_stream->p_data->p_payload_end );
316 /* We've found a data packet which contains interesting data... */
317 p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
318 p_bit_stream->p_end = p_bit_stream->p_data->p_payload_end;
320 /* Call back the decoder. */
321 if( p_bit_stream->pf_bitstream_callback != NULL )
323 p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
327 /*****************************************************************************
328 * ESInitBitstream: changes pf_next_data_packet
329 *****************************************************************************/
330 static void ESInitBitstream( bit_stream_t * p_bit_stream,
331 decoder_fifo_t * p_decoder_fifo,
332 void (* pf_bitstream_callback)( struct bit_stream_s *,
334 void * p_callback_arg )
336 InitBitstream( p_bit_stream, p_decoder_fifo, pf_bitstream_callback,
338 p_bit_stream->pf_next_data_packet = ESNextDataPacket;