]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_es.c
* added functions in inputs to change the current program (most of
[vlc] / plugins / mpeg / input_es.c
1 /*****************************************************************************
2  * input_es.c: Elementary Stream demux and packet management
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: input_es.c,v 1.15 2001/12/07 16:47:47 jobi Exp $
6  *
7  * Author: Christophe Massiot <massiot@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 #define MODULE_NAME es
25 #include "modules_inner.h"
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #include <fcntl.h>
44
45 #if defined( WIN32 )
46 #   include <io.h>                                                 /* read() */
47 #else
48 #   include <sys/uio.h>                                      /* struct iovec */
49 #endif
50
51 #include "config.h"
52 #include "common.h"
53 #include "intf_msg.h"
54 #include "threads.h"
55 #include "mtime.h"
56 #include "tests.h"
57
58 #if defined( WIN32 )
59 #   include "input_iovec.h"
60 #endif
61
62 #include "main.h"
63
64 #include "modules.h"
65 #include "modules_export.h"
66
67 #include "stream_control.h"
68 #include "input_ext-intf.h"
69 #include "input_ext-dec.h"
70 #include "input_ext-plugins.h"
71
72 #include "input_es.h"
73
74 #include "debug.h"
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 static int  ESProbe     ( probedata_t * );
80 static int  ESRead      ( struct input_thread_s *,
81                           data_packet_t * p_packets[INPUT_READ_ONCE] );
82 static void ESInit          ( struct input_thread_s * );
83 static void ESEnd           ( struct input_thread_s * );
84 static void ESSeek          ( struct input_thread_s *, off_t );
85 static int  ESSetProgram    ( struct input_thread_s *, pgrm_descriptor_t * );
86 static void ESDemux         ( struct input_thread_s *, 
87                                 struct data_packet_s * );
88 static void ESNextDataPacket( struct bit_stream_s * );
89 static void ESInitBitstream( struct bit_stream_s *, struct decoder_fifo_s *,
90                         void (* pf_bitstream_callback)( struct bit_stream_s *,
91                                                         boolean_t ),
92                         void * );
93
94
95 /*****************************************************************************
96  * Functions exported as capabilities. They are declared as static so that
97  * we don't pollute the namespace too much.
98  *****************************************************************************/
99 void _M( input_getfunctions )( function_list_t * p_function_list )
100 {
101 #define input p_function_list->functions.input
102     p_function_list->pf_probe = ESProbe;
103     input.pf_init             = ESInit;
104     input.pf_open             = NULL;
105     input.pf_close            = NULL;
106     input.pf_end              = ESEnd;
107     input.pf_init_bit_stream  = ESInitBitstream;
108     input.pf_set_area         = NULL;
109     input.pf_set_program      = ESSetProgram;
110     input.pf_read             = ESRead;
111     input.pf_demux            = ESDemux;
112     input.pf_new_packet       = input_NetlistNewPacket;
113     input.pf_new_pes          = input_NetlistNewPES;
114     input.pf_delete_packet    = input_NetlistDeletePacket;
115     input.pf_delete_pes       = input_NetlistDeletePES;
116     input.pf_rewind           = NULL;
117     input.pf_seek             = ESSeek;
118 #undef input
119 }
120
121 /*
122  * Data reading functions
123  */
124
125 /*****************************************************************************
126  * ESProbe: verifies that the stream is a ES stream
127  *****************************************************************************/
128 static int ESProbe( probedata_t *p_data )
129 {
130     int i_score = 5;
131
132     if( TestMethod( INPUT_METHOD_VAR, "es" ) )
133     {
134         return( 999 );
135     }
136
137     return( i_score );
138 }
139
140 /*****************************************************************************
141  * ESInit: initializes ES structures
142  *****************************************************************************/
143 static void ESInit( input_thread_t * p_input )
144 {
145     es_descriptor_t *   p_es;
146
147     p_input->p_method_data = NULL;
148
149     /* Initialize netlist */
150     if( input_NetlistInit( p_input, NB_DATA, NB_DATA, NB_PES, ES_PACKET_SIZE,
151                            INPUT_READ_ONCE ) )
152     {
153         intf_ErrMsg( "ES input : Could not initialize netlist" );
154         return;
155     }
156
157     /* FIXME : detect if InitStream failed */
158     input_InitStream( p_input, 0 );
159     input_AddProgram( p_input, 0, 0 );
160     vlc_mutex_lock( &p_input->stream.stream_lock );
161     p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xE0, 0 );
162     p_es->i_stream_id = 0xE0;
163     p_es->i_type = MPEG1_VIDEO_ES;
164     p_es->i_cat = VIDEO_ES;
165     input_SelectES( p_input, p_es );
166     p_input->stream.p_selected_area->i_tell = 0;
167     p_input->stream.pp_programs[0]->b_is_ok = 1;
168     vlc_mutex_unlock( &p_input->stream.stream_lock );
169 }
170
171 /*****************************************************************************
172  * ESEnd: frees unused data
173  *****************************************************************************/
174 static void ESEnd( input_thread_t * p_input )
175 {
176 }
177
178 /*****************************************************************************
179  * ESRead: reads data packets
180  *****************************************************************************
181  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
182  * EOF.
183  *****************************************************************************/
184 static int ESRead( input_thread_t * p_input,
185                    data_packet_t * pp_packets[INPUT_READ_ONCE] )
186 {
187     int             i_read;
188     struct iovec  * p_iovec;
189
190     /* Get iovecs */
191     p_iovec = input_NetlistGetiovec( p_input->p_method_data );
192
193     if ( p_iovec == NULL )
194     {
195         return( -1 ); /* empty netlist */
196     }
197
198     memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
199
200     i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
201     if( i_read == -1 )
202     {
203         intf_ErrMsg( "input error: ES readv error" );
204         return( -1 );
205     }
206
207     input_NetlistMviovec( p_input->p_method_data,
208              (int)(i_read/ES_PACKET_SIZE), pp_packets );
209
210     p_input->stream.p_selected_area->i_tell += i_read;
211
212     return( 0 );
213 }
214
215 /*****************************************************************************
216  * ESSeek: changes the stream position indicator
217  *****************************************************************************/
218 static void ESSeek( input_thread_t * p_input, off_t i_position )
219 {
220     lseek( p_input->i_handle, i_position, SEEK_SET );
221
222     p_input->stream.p_selected_area->i_tell = i_position;
223 }
224
225
226 /*****************************************************************************
227  * ESSetProgram: Does nothing
228  *****************************************************************************/
229 static int ESSetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
230 {
231     return( 0 );
232 }
233
234 /*****************************************************************************
235  * ESDemux: fakes a demultiplexer
236  *****************************************************************************/
237 static void ESDemux( input_thread_t * p_input, data_packet_t * p_data )
238 {
239     pes_packet_t *  p_pes = p_input->pf_new_pes( p_input->p_method_data );
240     decoder_fifo_t * p_fifo =
241         p_input->stream.pp_programs[0]->pp_es[0]->p_decoder_fifo;
242
243     if( p_pes == NULL )
244     {
245         intf_ErrMsg("Out of memory");
246         p_input->b_error = 1;
247         return;
248     }
249
250     p_pes->i_rate = p_input->stream.control.i_rate;
251     p_pes->p_first = p_data;
252
253     if( (p_input->stream.pp_programs[0]->i_synchro_state == SYNCHRO_REINIT)
254          | (input_ClockManageControl( p_input, p_input->stream.pp_programs[0],
255                                   (mtime_t)0 ) == PAUSE_S) )
256     {
257         intf_WarnMsg( 2, "synchro reinit" );
258         p_pes->i_pts = mdate() + DEFAULT_PTS_DELAY;
259         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_OK;
260     }
261
262     input_DecodePES( p_fifo, p_pes );
263
264     vlc_mutex_lock( &p_fifo->data_lock );
265     if( ( (DECODER_FIFO_END( *p_fifo ) - DECODER_FIFO_START( *p_fifo ))
266             & FIFO_SIZE ) >= MAX_PACKETS_IN_FIFO )
267     {
268         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
269     }
270     vlc_mutex_unlock( &p_fifo->data_lock );
271 }
272
273 /*****************************************************************************
274  * ESNextDataPacket: signals the input thread if there isn't enough packets
275  * available
276  *****************************************************************************/
277 static void ESNextDataPacket( bit_stream_t * p_bit_stream )
278 {
279     decoder_fifo_t *    p_fifo = p_bit_stream->p_decoder_fifo;
280     boolean_t           b_new_pes;
281
282     /* We are looking for the next data packet that contains real data,
283      * and not just a PES header */
284     do
285     {
286         /* We were reading the last data packet of this PES packet... It's
287          * time to jump to the next PES packet */
288         if( p_bit_stream->p_data->p_next == NULL )
289         {
290             /* We are going to read/write the start and end indexes of the
291              * decoder fifo and to use the fifo's conditional variable,
292              * that's why we need to take the lock before. */
293             vlc_mutex_lock( &p_fifo->data_lock );
294
295             /* Free the previous PES packet. */
296             p_fifo->pf_delete_pes( p_fifo->p_packets_mgt,
297                                    DECODER_FIFO_START( *p_fifo ) );
298             DECODER_FIFO_INCSTART( *p_fifo );
299
300             if( DECODER_FIFO_ISEMPTY( *p_fifo ) )
301             {
302                 /* Signal the input thread we're waiting. */
303                 vlc_cond_signal( &p_fifo->data_wait );
304
305                 /* Wait for the input to tell us when we receive a packet. */
306                 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
307             }
308
309             /* The next byte could be found in the next PES packet */
310             p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
311
312             vlc_mutex_unlock( &p_fifo->data_lock );
313
314             b_new_pes = 1;
315         }
316         else
317         {
318             /* Perhaps the next data packet of the current PES packet contains
319              * real data (ie its payload's size is greater than 0). */
320             p_bit_stream->p_data = p_bit_stream->p_data->p_next;
321
322             b_new_pes = 0;
323         }
324     } while ( p_bit_stream->p_data->p_payload_start
325                == p_bit_stream->p_data->p_payload_end );
326
327     /* We've found a data packet which contains interesting data... */
328     p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
329     p_bit_stream->p_end  = p_bit_stream->p_data->p_payload_end;
330
331     /* Call back the decoder. */
332     if( p_bit_stream->pf_bitstream_callback != NULL )
333     {
334         p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
335     }
336 }
337
338 /*****************************************************************************
339  * ESInitBitstream: changes pf_next_data_packet
340  *****************************************************************************/
341 static void ESInitBitstream( bit_stream_t * p_bit_stream,
342                              decoder_fifo_t * p_decoder_fifo,
343                         void (* pf_bitstream_callback)( struct bit_stream_s *,
344                                                         boolean_t ),
345                             void * p_callback_arg )
346 {
347     InitBitstream( p_bit_stream, p_decoder_fifo, pf_bitstream_callback,
348                    p_callback_arg );
349     p_bit_stream->pf_next_data_packet = ESNextDataPacket;
350 }