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