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