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