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