]> git.sesse.net Git - vlc/blob - src/input/demux.c
all: added support for demuxer attachment (Patch by Bernie Purcell)
[vlc] / src / input / demux.c
1 /*****************************************************************************
2  * demux.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26
27 #include "input_internal.h"
28
29 static void SkipID3Tag( demux_t * );
30
31 /*****************************************************************************
32  * demux2_New:
33  *  if s is NULL then load a access_demux
34  *****************************************************************************/
35 demux_t *__demux2_New( vlc_object_t *p_obj,
36                        const char *psz_access, const char *psz_demux,
37                        const char *psz_path,
38                        stream_t *s, es_out_t *out, vlc_bool_t b_quick )
39 {
40     demux_t *p_demux = vlc_object_create( p_obj, VLC_OBJECT_DEMUX );
41     const char *psz_module;
42
43     if( p_demux == NULL ) return NULL;
44
45     /* Parse URL */
46     p_demux->psz_access = strdup( psz_access );
47     p_demux->psz_demux  = strdup( psz_demux );
48     p_demux->psz_path   = strdup( psz_path );
49
50     /* Take into account "demux" to be able to do :demux=dump */
51     if( *p_demux->psz_demux == '\0' )
52     {
53         free( p_demux->psz_demux );
54         p_demux->psz_demux = var_GetString( p_obj, "demux" );
55     }
56
57     if( !b_quick )
58     {
59         msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
60                  p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
61     }
62
63     p_demux->s          = s;
64     p_demux->out        = out;
65
66     p_demux->pf_demux   = NULL;
67     p_demux->pf_control = NULL;
68     p_demux->p_sys      = NULL;
69     p_demux->info.i_update = 0;
70     p_demux->info.i_title  = 0;
71     p_demux->info.i_seekpoint = 0;
72
73     if( s ) psz_module = p_demux->psz_demux;
74     else psz_module = p_demux->psz_access;
75
76     if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
77     {
78        /* XXX: add only file without any problem here and with strong detection.
79         *  - no .mp3, .a52, ... (aac is added as it works only by file ext
80         *     anyway
81         *  - wav can't be added 'cause of a52 and dts in them as raw audio
82          */
83          static const struct { char ext[5]; char demux[9]; } exttodemux[] =
84          {
85             { "aac",  "aac" },
86             { "aiff", "aiff" },
87             { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
88             { "avi",  "avi" },
89             { "au",   "au" },
90             { "flac", "flac" },
91             { "dv",   "dv" },
92             { "m3u",  "playlist" },
93             { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
94             { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
95             { "mod",  "mod" }, { "xm",   "mod" },
96             { "nsv",  "nsv" },
97             { "ogg",  "ogg" }, { "ogm",  "ogg" },
98             { "pva",  "pva" },
99             { "rm",   "rm" },
100             { "m4v",  "m4v" },
101             { "h264",  "h264" },
102             { "",  "" },
103         };
104         /* Here, we don't mind if it does not work, it must be quick */
105         static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
106         {
107             { "mp3", "mpga" },
108             { "ogg", "ogg" },
109             { "wma", "asf" },
110             { "", "" }
111         };
112
113         const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
114         int  i;
115
116         if( !b_quick )
117         {
118             for( i = 0; exttodemux[i].ext[0]; i++ )
119             {
120                 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
121                 {
122                     psz_module = exttodemux[i].demux;
123                     break;
124                 }
125             }
126         }
127         else
128         {
129             for( i = 0; exttodemux_quick[i].ext[0]; i++ )
130             {
131                 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
132                 {
133                     psz_module = exttodemux_quick[i].demux;
134                     break;
135                 }
136             }
137
138         }
139     }
140
141     /* Before module_Need (for var_Create...) */
142     vlc_object_attach( p_demux, p_obj );
143
144     if( s )
145     {
146         /* ID3 tags will mess-up demuxer probing so we skip it here.
147          * ID3 parsers will called later on in the demuxer to access the
148          * skipped info. */
149         SkipID3Tag( p_demux );
150
151         p_demux->p_module =
152             module_Need( p_demux, "demux2", psz_module,
153                          !strcmp( psz_module, p_demux->psz_demux ) ?
154                          VLC_TRUE : VLC_FALSE );
155     }
156     else
157     {
158         p_demux->p_module =
159             module_Need( p_demux, "access_demux", psz_module,
160                          !strcmp( psz_module, p_demux->psz_access ) ?
161                          VLC_TRUE : VLC_FALSE );
162     }
163
164     if( p_demux->p_module == NULL )
165     {
166         vlc_object_detach( p_demux );
167         free( p_demux->psz_path );
168         free( p_demux->psz_demux );
169         free( p_demux->psz_access );
170         vlc_object_destroy( p_demux );
171         return NULL;
172     }
173
174     return p_demux;
175 }
176
177 /*****************************************************************************
178  * demux2_Delete:
179  *****************************************************************************/
180 void demux2_Delete( demux_t *p_demux )
181 {
182     module_Unneed( p_demux, p_demux->p_module );
183     vlc_object_detach( p_demux );
184
185     free( p_demux->psz_path );
186     free( p_demux->psz_demux );
187     free( p_demux->psz_access );
188
189     vlc_object_destroy( p_demux );
190 }
191
192 /*****************************************************************************
193  * demux2_vaControlHelper:
194  *****************************************************************************/
195 int demux2_vaControlHelper( stream_t *s,
196                             int64_t i_start, int64_t i_end,
197                             int i_bitrate, int i_align,
198                             int i_query, va_list args )
199 {
200     int64_t i_tell;
201     double  f, *pf;
202     int64_t i64, *pi64;
203
204     if( i_end < 0 )    i_end   = stream_Size( s );
205     if( i_start < 0 )  i_start = 0;
206     if( i_align <= 0 ) i_align = 1;
207     i_tell = stream_Tell( s );
208
209     switch( i_query )
210     {
211         case DEMUX_GET_LENGTH:
212             pi64 = (int64_t*)va_arg( args, int64_t * );
213             if( i_bitrate > 0 && i_end > i_start )
214             {
215                 *pi64 = I64C(8000000) * (i_end - i_start) / i_bitrate;
216                 return VLC_SUCCESS;
217             }
218             return VLC_EGENERIC;
219
220         case DEMUX_GET_TIME:
221             pi64 = (int64_t*)va_arg( args, int64_t * );
222             if( i_bitrate > 0 && i_end > i_start )
223             {
224                 *pi64 = I64C(8000000) * (i_tell - i_start) / i_bitrate;
225                 return VLC_SUCCESS;
226             }
227             return VLC_EGENERIC;
228
229         case DEMUX_GET_POSITION:
230             pf = (double*)va_arg( args, double * );
231             if( i_start < i_end )
232             {
233                 *pf = (double)( i_tell - i_start ) /
234                       (double)( i_end  - i_start );
235                 return VLC_SUCCESS;
236             }
237             return VLC_EGENERIC;
238
239
240         case DEMUX_SET_POSITION:
241             f = (double)va_arg( args, double );
242             if( i_start < i_end && f >= 0.0 && f <= 1.0 )
243             {
244                 int64_t i_block = (f * ( i_end - i_start )) / i_align;
245
246                 if( stream_Seek( s, i_start + i_block * i_align ) )
247                 {
248                     return VLC_EGENERIC;
249                 }
250                 return VLC_SUCCESS;
251             }
252             return VLC_EGENERIC;
253
254         case DEMUX_SET_TIME:
255             i64 = (int64_t)va_arg( args, int64_t );
256             if( i_bitrate > 0 && i64 >= 0 )
257             {
258                 int64_t i_block = i64 * i_bitrate / I64C(8000000) / i_align;
259                 if( stream_Seek( s, i_start + i_block * i_align ) )
260                 {
261                     return VLC_EGENERIC;
262                 }
263                 return VLC_SUCCESS;
264             }
265             return VLC_EGENERIC;
266
267         case DEMUX_GET_FPS:
268         case DEMUX_GET_META:
269         case DEMUX_SET_NEXT_DEMUX_TIME:
270         case DEMUX_GET_TITLE_INFO:
271         case DEMUX_SET_GROUP:
272         case DEMUX_GET_ATTACHMENTS:
273             return VLC_EGENERIC;
274
275         default:
276             msg_Err( s, "unknown query in demux_vaControlDefault" );
277             return VLC_EGENERIC;
278     }
279 }
280
281 /****************************************************************************
282  * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
283  ****************************************************************************/
284 typedef struct
285 {
286     /* Data buffer */
287     block_fifo_t *p_fifo;
288     block_t      *p_block;
289
290     int64_t     i_pos;
291
292     /* Demuxer */
293     char        *psz_name;
294     es_out_t    *out;
295     demux_t     *p_demux;
296
297 } d_stream_sys_t;
298
299 static int DStreamRead   ( stream_t *, void *p_read, int i_read );
300 static int DStreamPeek   ( stream_t *, uint8_t **pp_peek, int i_peek );
301 static int DStreamControl( stream_t *, int i_query, va_list );
302 static int DStreamThread ( stream_t * );
303
304
305 stream_t *__stream_DemuxNew( vlc_object_t *p_obj, const char *psz_demux,
306                              es_out_t *out )
307 {
308     /* We create a stream reader, and launch a thread */
309     stream_t       *s;
310     d_stream_sys_t *p_sys;
311
312     if( psz_demux == NULL || *psz_demux == '\0' ) return NULL;
313
314     s = vlc_stream_create( p_obj );
315     s->pf_block  = NULL;
316     s->pf_read   = DStreamRead;
317     s->pf_peek   = DStreamPeek;
318     s->pf_control= DStreamControl;
319
320     s->i_char_width = 1;
321     s->b_little_endian = VLC_FALSE;
322
323     s->p_sys = malloc( sizeof( d_stream_sys_t) );
324     p_sys = (d_stream_sys_t*)s->p_sys;
325
326     p_sys->i_pos = 0;
327     p_sys->out = out;
328     p_sys->p_demux = NULL;
329     p_sys->p_block = NULL;
330     p_sys->psz_name = strdup( psz_demux );
331
332     /* decoder fifo */
333     if( ( p_sys->p_fifo = block_FifoNew( s ) ) == NULL )
334     {
335         msg_Err( s, "out of memory" );
336         vlc_object_destroy( s );
337         free( p_sys );
338         return NULL;
339     }
340
341     if( vlc_thread_create( s, "stream out", DStreamThread,
342                            VLC_THREAD_PRIORITY_INPUT, VLC_FALSE ) )
343     {
344         vlc_object_destroy( s );
345         free( p_sys );
346         return NULL;
347     }
348
349     return s;
350 }
351
352 void stream_DemuxSend( stream_t *s, block_t *p_block )
353 {
354     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
355     if( p_block ) block_FifoPut( p_sys->p_fifo, p_block );
356 }
357
358 void stream_DemuxDelete( stream_t *s )
359 {
360     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
361     block_t *p_empty;
362
363     vlc_object_kill( s );
364     if( p_sys->p_demux )
365         vlc_object_kill( p_sys->p_demux );
366     p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
367     block_FifoPut( p_sys->p_fifo, p_empty );
368     vlc_thread_join( s );
369
370     if( p_sys->p_demux ) demux2_Delete( p_sys->p_demux );
371     if( p_sys->p_block ) block_Release( p_sys->p_block );
372
373     block_FifoRelease( p_sys->p_fifo );
374     free( p_sys->psz_name );
375     free( p_sys );
376
377     vlc_object_destroy( s );
378 }
379
380
381 static int DStreamRead( stream_t *s, void *p_read, int i_read )
382 {
383     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
384     uint8_t *p_out = p_read;
385     int i_out = 0;
386
387     //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
388
389     while( !s->b_die && !s->b_error && i_read )
390     {
391         block_t *p_block = p_sys->p_block;
392         int i_copy;
393
394         if( !p_block )
395         {
396             p_block = block_FifoGet( p_sys->p_fifo );
397             if( !p_block ) s->b_error = 1;
398             p_sys->p_block = p_block;
399         }
400
401         if( p_block && i_read )
402         {
403             i_copy = __MIN( i_read, p_block->i_buffer );
404             if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
405             i_read -= i_copy;
406             i_out += i_copy;
407             p_block->i_buffer -= i_copy;
408             p_block->p_buffer += i_copy;
409
410             if( !p_block->i_buffer )
411             {
412                 block_Release( p_block );
413                 p_sys->p_block = NULL;
414             }
415         }
416     }
417
418     p_sys->i_pos += i_out;
419     return i_out;
420 }
421
422 static int DStreamPeek( stream_t *s, uint8_t **pp_peek, int i_peek )
423 {
424     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
425     block_t **pp_block = &p_sys->p_block;
426     int i_out = 0;
427     *pp_peek = 0;
428
429     //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
430
431     while( !s->b_die && !s->b_error && i_peek )
432     {
433         int i_copy;
434
435         if( !*pp_block )
436         {
437             *pp_block = block_FifoGet( p_sys->p_fifo );
438             if( !*pp_block ) s->b_error = 1;
439         }
440
441         if( *pp_block && i_peek )
442         {
443             i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
444             i_peek -= i_copy;
445             i_out += i_copy;
446
447             if( i_peek ) pp_block = &(*pp_block)->p_next;
448         }
449     }
450
451     if( p_sys->p_block )
452     {
453         p_sys->p_block = block_ChainGather( p_sys->p_block );
454         *pp_peek = p_sys->p_block->p_buffer;
455     }
456
457     return i_out;
458 }
459
460 static int DStreamControl( stream_t *s, int i_query, va_list args )
461 {
462     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
463     int64_t    *p_i64;
464     vlc_bool_t *p_b;
465     int        *p_int;
466
467     switch( i_query )
468     {
469         case STREAM_GET_SIZE:
470             p_i64 = (int64_t*) va_arg( args, int64_t * );
471             *p_i64 = 0;
472             return VLC_SUCCESS;
473
474         case STREAM_CAN_SEEK:
475             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
476             *p_b = VLC_FALSE;
477             return VLC_SUCCESS;
478
479         case STREAM_CAN_FASTSEEK:
480             p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
481             *p_b = VLC_FALSE;
482             return VLC_SUCCESS;
483
484         case STREAM_GET_POSITION:
485             p_i64 = (int64_t*) va_arg( args, int64_t * );
486             *p_i64 = p_sys->i_pos;
487             return VLC_SUCCESS;
488
489         case STREAM_SET_POSITION:
490         {
491             int64_t i64 = (int64_t)va_arg( args, int64_t );
492             int i_skip;
493             if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
494             i_skip = i64 - p_sys->i_pos;
495
496             while( i_skip > 0 )
497             {
498                 int i_read = DStreamRead( s, NULL, i_skip );
499                 if( i_read <= 0 ) return VLC_EGENERIC;
500                 i_skip -= i_read;
501             }
502             return VLC_SUCCESS;
503         }
504
505         case STREAM_GET_MTU:
506             p_int = (int*) va_arg( args, int * );
507             *p_int = 0;
508             return VLC_SUCCESS;
509
510         case STREAM_CONTROL_ACCESS:
511             return VLC_EGENERIC;
512
513         default:
514             msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
515             return VLC_EGENERIC;
516     }
517 }
518
519 static int DStreamThread( stream_t *s )
520 {
521     d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
522     demux_t *p_demux;
523
524     /* Create the demuxer */
525     if( !(p_demux = demux2_New( s, "", p_sys->psz_name, "", s, p_sys->out,
526                                VLC_FALSE )) )
527     {
528         return VLC_EGENERIC;
529     }
530
531     p_sys->p_demux = p_demux;
532
533     /* Main loop */
534     while( !s->b_die && !p_demux->b_die )
535     {
536         if( p_demux->pf_demux( p_demux ) <= 0 ) break;
537     }
538
539     vlc_object_kill( p_demux );
540     return VLC_SUCCESS;
541 }
542
543 /****************************************************************************
544  * Utility functions
545  ****************************************************************************/
546 static void SkipID3Tag( demux_t *p_demux )
547 {
548     uint8_t *p_peek;
549     uint8_t version, revision;
550     int i_size;
551     int b_footer;
552
553     if( !p_demux->s ) return;
554
555     /* Get 10 byte id3 header */
556     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return;
557
558     if( p_peek[0] != 'I' || p_peek[1] != 'D' || p_peek[2] != '3' ) return;
559
560     version = p_peek[3];
561     revision = p_peek[4];
562     b_footer = p_peek[5] & 0x10;
563     i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
564
565     if( b_footer ) i_size += 10;
566     i_size += 10;
567
568     /* Skip the entire tag */
569     stream_Read( p_demux->s, NULL, i_size );
570
571     msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
572              version, revision, i_size );
573
574     return;
575 }