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