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