1 /*****************************************************************************
2 * input_es.c: Elementary Stream demux and packet management
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: input_es.c,v 1.3 2001/05/30 17:03:12 sam Exp $
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>
51 #include "stream_control.h"
52 #include "input_ext-intf.h"
53 #include "input_ext-dec.h"
58 #include "mpeg_system.h"
63 #include "modules_export.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 static int ESProbe ( probedata_t * );
69 static int ESRead ( struct input_thread_s *,
70 data_packet_t * p_packets[INPUT_READ_ONCE] );
71 static void ESInit ( struct input_thread_s * );
72 static void ESEnd ( struct input_thread_s * );
73 static void ESSeek ( struct input_thread_s *, off_t );
74 static void ESDemux ( struct input_thread_s *, struct data_packet_s * );
75 static struct pes_packet_s * NewPES ( void * );
76 static struct data_packet_s * NewPacket ( void *, size_t );
77 static void DeletePacket( void *, struct data_packet_s * );
78 static void DeletePES ( void *, struct pes_packet_s * );
80 /*****************************************************************************
81 * Functions exported as capabilities. They are declared as static so that
82 * we don't pollute the namespace too much.
83 *****************************************************************************/
84 void _M( input_getfunctions )( function_list_t * p_function_list )
86 #define input p_function_list->functions.input
87 p_function_list->pf_probe = ESProbe;
88 input.pf_init = ESInit;
89 input.pf_open = NULL; /* Set in ESInit */
90 input.pf_close = NULL;
92 input.pf_set_area = NULL;
93 input.pf_read = ESRead;
94 input.pf_demux = ESDemux;
95 input.pf_new_packet = NewPacket;
96 input.pf_new_pes = NewPES;
97 input.pf_delete_packet = DeletePacket;
98 input.pf_delete_pes = DeletePES;
99 input.pf_rewind = NULL;
100 input.pf_seek = ESSeek;
105 * Data reading functions
108 /*****************************************************************************
109 * ESProbe: verifies that the stream is a ES stream
110 *****************************************************************************/
111 static int ESProbe( probedata_t *p_data )
113 input_thread_t * p_input = (input_thread_t *)p_data;
115 char * psz_name = p_input->p_source;
119 if( TestMethod( INPUT_METHOD_VAR, "es" ) )
124 i_handle = open( psz_name, 0 );
134 /*****************************************************************************
135 * ESInit: initializes ES structures
136 *****************************************************************************/
137 static void ESInit( input_thread_t * p_input )
139 thread_es_data_t * p_method;
142 (thread_es_data_t *)malloc( sizeof(thread_es_data_t) )) == NULL )
144 intf_ErrMsg( "Out of memory" );
145 p_input->b_error = 1;
148 p_input->p_plugin_data = (void *)p_method;
150 p_input->pf_open = p_input->pf_file_open;
151 p_input->pf_close = p_input->pf_file_close;
154 /*****************************************************************************
155 * ESEnd: frees unused data
156 *****************************************************************************/
157 static void ESEnd( input_thread_t * p_input )
161 free( p_input->p_plugin_data );
164 /*****************************************************************************
165 * SafeRead: reads a chunk of stream and correctly detects errors
166 *****************************************************************************/
167 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
170 thread_es_data_t * p_method;
173 p_method = (thread_es_data_t *)p_input->p_plugin_data;
174 while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
176 if( feof( p_method->stream ) )
181 if( (i_error = ferror( p_method->stream )) )
183 intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
187 vlc_mutex_lock( &p_input->stream.stream_lock );
188 p_input->stream.p_selected_area->i_tell += i_len;
189 vlc_mutex_unlock( &p_input->stream.stream_lock );
194 /*****************************************************************************
195 * ESRead: reads data packets
196 *****************************************************************************
197 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
199 *****************************************************************************/
200 static int ESRead( input_thread_t * p_input,
201 data_packet_t * pp_packets[INPUT_READ_ONCE] )
208 /*****************************************************************************
209 * ESSeek: changes the stream position indicator
210 *****************************************************************************/
211 static void ESSeek( input_thread_t * p_input, off_t i_position )
213 thread_es_data_t * p_method;
215 p_method = (thread_es_data_t *)p_input->p_plugin_data;
217 /* A little bourrin but should work for a while --Meuuh */
219 fseeko( p_method->stream, i_position, SEEK_SET );
221 fseek( p_method->stream, (long)i_position, SEEK_SET );
224 p_input->stream.p_selected_area->i_tell = i_position;
227 void ESDemux( input_thread_t * p_input, data_packet_t * p_data )
233 * Packet management utilities
236 /*****************************************************************************
237 * NewPacket: allocates a data packet
238 *****************************************************************************/
239 static struct data_packet_s * NewPacket( void * p_packet_cache,
248 /*****************************************************************************
249 * NewPES: allocates a pes packet
250 *****************************************************************************/
251 static pes_packet_t * NewPES( void * p_packet_cache )
258 /*****************************************************************************
259 * DeletePacket: deletes a data packet
260 *****************************************************************************/
261 static void DeletePacket( void * p_packet_cache,
262 data_packet_t * p_data )
267 /*****************************************************************************
268 * DeletePES: deletes a PES packet and associated data packets
269 *****************************************************************************/
270 static void DeletePES( void * p_packet_cache, pes_packet_t * p_pes )