]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_es.c
* Applied old FreeBSD patch for dvd input by German Tischler.
[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.6 2001/06/07 15:27:44 sam Exp $
6  *
7  * Authors: 
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 #elif defined( _MSC_VER ) && defined( _WIN32 )
42 #   include <io.h>
43 #endif
44
45 #include <fcntl.h>
46
47 #include "config.h"
48 #include "common.h"
49 #include "threads.h"
50 #include "mtime.h"
51 #include "tests.h"
52
53 #include "intf_msg.h"
54
55 #include "main.h"
56
57 #include "stream_control.h"
58 #include "input_ext-intf.h"
59 #include "input_ext-dec.h"
60
61 #include "input.h"
62
63 #include "input_es.h"
64 #include "mpeg_system.h"
65
66 #include "debug.h"
67
68 #include "modules.h"
69 #include "modules_export.h"
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74 static int  ESProbe     ( probedata_t * );
75 static int  ESRead      ( struct input_thread_s *,
76                           data_packet_t * p_packets[INPUT_READ_ONCE] );
77 static void ESInit      ( struct input_thread_s * );
78 static void ESEnd       ( struct input_thread_s * );
79 static void ESSeek      ( struct input_thread_s *, off_t );
80 static void ESDemux     ( struct input_thread_s *, struct data_packet_s * );
81 static struct pes_packet_s *  NewPES    ( void * );
82 static struct data_packet_s * NewPacket ( void *, size_t );
83 static void DeletePacket( void *, struct data_packet_s * );
84 static void DeletePES   ( void *, struct pes_packet_s * );
85
86 /*****************************************************************************
87  * Functions exported as capabilities. They are declared as static so that
88  * we don't pollute the namespace too much.
89  *****************************************************************************/
90 void _M( input_getfunctions )( function_list_t * p_function_list )
91 {
92 #define input p_function_list->functions.input
93     p_function_list->pf_probe = ESProbe;
94     input.pf_init             = ESInit;
95     input.pf_open             = NULL; /* Set in ESInit */
96     input.pf_close            = NULL;
97     input.pf_end              = ESEnd;
98     input.pf_set_area         = NULL;
99     input.pf_read             = ESRead;
100     input.pf_demux            = ESDemux;
101     input.pf_new_packet       = NewPacket;
102     input.pf_new_pes          = NewPES;
103     input.pf_delete_packet    = DeletePacket;
104     input.pf_delete_pes       = DeletePES;
105     input.pf_rewind           = NULL;
106     input.pf_seek             = ESSeek;
107 #undef input
108 }
109
110 /*
111  * Data reading functions
112  */
113
114 /*****************************************************************************
115  * ESProbe: verifies that the stream is a ES stream
116  *****************************************************************************/
117 static int ESProbe( probedata_t *p_data )
118 {
119     input_thread_t * p_input = (input_thread_t *)p_data;
120
121     char * psz_name = p_input->p_source;
122     int i_handle;
123     int i_score = 5;
124
125     if( TestMethod( INPUT_METHOD_VAR, "es" ) )
126     {
127         return( 999 );
128     }
129
130     i_handle = open( psz_name, 0 );
131     if( i_handle == -1 )
132     {
133         return( 0 );
134     }
135     close( i_handle );
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     thread_es_data_t *  p_method;
146
147     if( (p_method =
148          (thread_es_data_t *)malloc( sizeof(thread_es_data_t) )) == NULL )
149     {
150         intf_ErrMsg( "Out of memory" );
151         p_input->b_error = 1;
152         return;
153     }
154     p_input->p_plugin_data = (void *)p_method;
155
156     p_input->pf_open  = p_input->pf_file_open;
157     p_input->pf_close = p_input->pf_file_close;
158 }
159
160 /*****************************************************************************
161  * ESEnd: frees unused data
162  *****************************************************************************/
163 static void ESEnd( input_thread_t * p_input )
164 {
165     /* XXX */
166
167     free( p_input->p_plugin_data );
168 }
169
170 /*****************************************************************************
171  * SafeRead: reads a chunk of stream and correctly detects errors
172  *****************************************************************************/
173 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
174                                 size_t i_len )
175 {
176     thread_es_data_t *  p_method;
177     int                 i_error;
178
179     p_method = (thread_es_data_t *)p_input->p_plugin_data;
180     while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
181     {
182         if( feof( p_method->stream ) )
183         {
184             return( 1 );
185         }
186
187         if( (i_error = ferror( p_method->stream )) )
188         {
189             intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
190             return( -1 );
191         }
192     }
193     vlc_mutex_lock( &p_input->stream.stream_lock );
194     p_input->stream.p_selected_area->i_tell += i_len;
195     vlc_mutex_unlock( &p_input->stream.stream_lock );
196
197     return( 0 );
198 }
199
200 /*****************************************************************************
201  * ESRead: reads data packets
202  *****************************************************************************
203  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
204  * EOF.
205  *****************************************************************************/
206 static int ESRead( input_thread_t * p_input,
207                    data_packet_t * pp_packets[INPUT_READ_ONCE] )
208 {
209     /* XXX */
210
211     return( 0 );
212 }
213
214 /*****************************************************************************
215  * ESSeek: changes the stream position indicator
216  *****************************************************************************/
217 static void ESSeek( input_thread_t * p_input, off_t i_position )
218 {
219     thread_es_data_t *  p_method;
220
221     p_method = (thread_es_data_t *)p_input->p_plugin_data;
222
223     /* A little bourrin but should work for a while --Meuuh */
224 #ifndef WIN32
225     fseeko( p_method->stream, i_position, SEEK_SET );
226 #else
227     fseek( p_method->stream, (long)i_position, SEEK_SET );
228 #endif
229
230     p_input->stream.p_selected_area->i_tell = i_position;
231 }
232
233 void ESDemux( input_thread_t * p_input, data_packet_t * p_data )
234 {
235     /* XXX */
236 }
237
238 /*
239  * Packet management utilities
240  */
241
242 /*****************************************************************************
243  * NewPacket: allocates a data packet
244  *****************************************************************************/
245 static struct data_packet_s * NewPacket( void * p_packet_cache,
246                                          size_t l_size )
247
248     /* XXX */
249
250     return NULL;
251 }
252
253
254 /*****************************************************************************
255  * NewPES: allocates a pes packet
256  *****************************************************************************/
257 static pes_packet_t * NewPES( void * p_packet_cache )
258 {
259     /* XXX */
260
261     return NULL;
262 }
263
264 /*****************************************************************************
265  * DeletePacket: deletes a data packet
266  *****************************************************************************/
267 static void DeletePacket( void * p_packet_cache,
268                           data_packet_t * p_data )
269 {
270     /* XXX */
271 }
272
273 /*****************************************************************************
274  * DeletePES: deletes a PES packet and associated data packets
275  *****************************************************************************/
276 static void DeletePES( void * p_packet_cache, pes_packet_t * p_pes )
277 {
278     /* XXX */
279 }
280