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