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