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