1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Author: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
30 #include "input_internal.h"
32 static bool SkipID3Tag( demux_t * );
33 static bool SkipAPETag( demux_t *p_demux );
35 /*****************************************************************************
37 * if s is NULL then load a access_demux
38 *****************************************************************************/
39 demux_t *__demux_New( vlc_object_t *p_obj,
40 const char *psz_access, const char *psz_demux,
42 stream_t *s, es_out_t *out, bool b_quick )
44 static const char typename[] = "demux";
45 demux_t *p_demux = vlc_custom_create( p_obj, sizeof( *p_demux ),
46 VLC_OBJECT_GENERIC, typename );
47 const char *psz_module;
49 if( p_demux == NULL ) return NULL;
52 p_demux->psz_access = strdup( psz_access );
53 p_demux->psz_demux = strdup( psz_demux );
54 p_demux->psz_path = strdup( psz_path );
56 /* Take into account "demux" to be able to do :demux=dump */
57 if( p_demux->psz_demux && *p_demux->psz_demux == '\0' )
59 free( p_demux->psz_demux );
60 p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
61 if( p_demux->psz_demux == NULL )
62 p_demux->psz_demux = strdup( "" );
67 msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
68 p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
74 p_demux->pf_demux = NULL;
75 p_demux->pf_control = NULL;
76 p_demux->p_sys = NULL;
77 p_demux->info.i_update = 0;
78 p_demux->info.i_title = 0;
79 p_demux->info.i_seekpoint = 0;
81 if( s ) psz_module = p_demux->psz_demux;
82 else psz_module = p_demux->psz_access;
84 if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
86 /* XXX: add only file without any problem here and with strong detection.
87 * - no .mp3, .a52, ... (aac is added as it works only by file ext
89 * - wav can't be added 'cause of a52 and dts in them as raw audio
91 static const struct { char ext[5]; char demux[9]; } exttodemux[] =
95 { "asf", "asf" }, { "wmv", "asf" }, { "wma", "asf" },
100 { "m3u", "playlist" },
101 { "mkv", "mkv" }, { "mka", "mkv" }, { "mks", "mkv" },
102 { "mp4", "mp4" }, { "m4a", "mp4" }, { "mov", "mp4" }, { "moov", "mp4" },
103 { "mod", "mod" }, { "xm", "mod" },
105 { "ogg", "ogg" }, { "ogm", "ogg" },
112 /* Here, we don't mind if it does not work, it must be quick */
113 static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
121 const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
126 for( i = 0; exttodemux[i].ext[0]; i++ )
128 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
130 psz_module = exttodemux[i].demux;
137 for( i = 0; exttodemux_quick[i].ext[0]; i++ )
139 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
141 psz_module = exttodemux_quick[i].demux;
149 /* Before module_Need (for var_Create...) */
150 vlc_object_attach( p_demux, p_obj );
154 /* ID3/APE tags will mess-up demuxer probing so we skip it here.
155 * ID3/APE parsers will called later on in the demuxer to access the
157 if( !SkipID3Tag( p_demux ) )
158 SkipAPETag( p_demux );
161 module_Need( p_demux, "demux", psz_module,
162 !strcmp( psz_module, p_demux->psz_demux ) ?
168 module_Need( p_demux, "access_demux", psz_module,
169 !strcmp( psz_module, p_demux->psz_access ) ?
173 if( p_demux->p_module == NULL )
175 vlc_object_detach( p_demux );
176 free( p_demux->psz_path );
177 free( p_demux->psz_demux );
178 free( p_demux->psz_access );
179 vlc_object_release( p_demux );
186 /*****************************************************************************
188 *****************************************************************************/
189 void demux_Delete( demux_t *p_demux )
191 module_Unneed( p_demux, p_demux->p_module );
192 vlc_object_detach( p_demux );
194 free( p_demux->psz_path );
195 free( p_demux->psz_demux );
196 free( p_demux->psz_access );
198 vlc_object_release( p_demux );
201 /*****************************************************************************
202 * demux_vaControlHelper:
203 *****************************************************************************/
204 int demux_vaControlHelper( stream_t *s,
205 int64_t i_start, int64_t i_end,
206 int i_bitrate, int i_align,
207 int i_query, va_list args )
213 if( i_end < 0 ) i_end = stream_Size( s );
214 if( i_start < 0 ) i_start = 0;
215 if( i_align <= 0 ) i_align = 1;
216 i_tell = stream_Tell( s );
220 case DEMUX_GET_LENGTH:
221 pi64 = (int64_t*)va_arg( args, int64_t * );
222 if( i_bitrate > 0 && i_end > i_start )
224 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
230 pi64 = (int64_t*)va_arg( args, int64_t * );
231 if( i_bitrate > 0 && i_end > i_start )
233 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
238 case DEMUX_GET_POSITION:
239 pf = (double*)va_arg( args, double * );
240 if( i_start < i_end )
242 *pf = (double)( i_tell - i_start ) /
243 (double)( i_end - i_start );
249 case DEMUX_SET_POSITION:
250 f = (double)va_arg( args, double );
251 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
253 int64_t i_block = (f * ( i_end - i_start )) / i_align;
255 if( stream_Seek( s, i_start + i_block * i_align ) )
264 i64 = (int64_t)va_arg( args, int64_t );
265 if( i_bitrate > 0 && i64 >= 0 )
267 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
268 if( stream_Seek( s, i_start + i_block * i_align ) )
278 case DEMUX_HAS_UNSUPPORTED_META:
279 case DEMUX_SET_NEXT_DEMUX_TIME:
280 case DEMUX_GET_TITLE_INFO:
281 case DEMUX_SET_GROUP:
282 case DEMUX_GET_ATTACHMENTS:
286 msg_Err( s, "unknown query in demux_vaControlDefault" );
291 /****************************************************************************
292 * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
293 ****************************************************************************/
297 block_fifo_t *p_fifo;
309 static int DStreamRead ( stream_t *, void *p_read, int i_read );
310 static int DStreamPeek ( stream_t *, const uint8_t **pp_peek, int i_peek );
311 static int DStreamControl( stream_t *, int i_query, va_list );
312 static int DStreamThread ( stream_t * );
315 stream_t *__stream_DemuxNew( vlc_object_t *p_obj, const char *psz_demux,
318 /* We create a stream reader, and launch a thread */
320 d_stream_sys_t *p_sys;
322 if( psz_demux == NULL || *psz_demux == '\0' ) return NULL;
324 s = vlc_stream_create( p_obj );
327 s->pf_read = DStreamRead;
328 s->pf_peek = DStreamPeek;
329 s->pf_control= DStreamControl;
332 s->b_little_endian = false;
334 s->p_sys = malloc( sizeof( d_stream_sys_t) );
335 if( s->p_sys == NULL )
337 vlc_object_release( s );
340 p_sys = (d_stream_sys_t*)s->p_sys;
344 p_sys->p_demux = NULL;
345 p_sys->p_block = NULL;
346 p_sys->psz_name = strdup( psz_demux );
349 if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
351 vlc_object_release( s );
352 free( p_sys->psz_name );
357 if( vlc_thread_create( s, "stream out", DStreamThread,
358 VLC_THREAD_PRIORITY_INPUT, false ) )
360 vlc_object_release( s );
361 free( p_sys->psz_name );
369 void stream_DemuxSend( stream_t *s, block_t *p_block )
371 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
372 if( p_block ) block_FifoPut( p_sys->p_fifo, p_block );
375 void stream_DemuxDelete( stream_t *s )
377 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
380 vlc_object_kill( s );
382 vlc_object_kill( p_sys->p_demux );
383 p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
384 block_FifoPut( p_sys->p_fifo, p_empty );
385 vlc_thread_join( s );
387 if( p_sys->p_demux ) demux_Delete( p_sys->p_demux );
388 if( p_sys->p_block ) block_Release( p_sys->p_block );
390 block_FifoRelease( p_sys->p_fifo );
391 free( p_sys->psz_name );
394 vlc_object_release( s );
398 static int DStreamRead( stream_t *s, void *p_read, int i_read )
400 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
401 uint8_t *p_out = p_read;
404 //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
406 while( !s->b_die && !s->b_error && i_read )
408 block_t *p_block = p_sys->p_block;
413 p_block = block_FifoGet( p_sys->p_fifo );
414 if( !p_block ) s->b_error = 1;
415 p_sys->p_block = p_block;
418 if( p_block && i_read )
420 i_copy = __MIN( i_read, p_block->i_buffer );
421 if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
424 p_block->i_buffer -= i_copy;
425 p_block->p_buffer += i_copy;
427 if( !p_block->i_buffer )
429 block_Release( p_block );
430 p_sys->p_block = NULL;
435 p_sys->i_pos += i_out;
439 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, int i_peek )
441 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
442 block_t **pp_block = &p_sys->p_block;
446 //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
448 while( !s->b_die && !s->b_error && i_peek )
454 *pp_block = block_FifoGet( p_sys->p_fifo );
455 if( !*pp_block ) s->b_error = 1;
458 if( *pp_block && i_peek )
460 i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
464 if( i_peek ) pp_block = &(*pp_block)->p_next;
470 p_sys->p_block = block_ChainGather( p_sys->p_block );
471 *pp_peek = p_sys->p_block->p_buffer;
477 static int DStreamControl( stream_t *s, int i_query, va_list args )
479 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
486 case STREAM_GET_SIZE:
487 p_i64 = (int64_t*) va_arg( args, int64_t * );
491 case STREAM_CAN_SEEK:
492 p_b = (bool*) va_arg( args, bool * );
496 case STREAM_CAN_FASTSEEK:
497 p_b = (bool*) va_arg( args, bool * );
501 case STREAM_GET_POSITION:
502 p_i64 = (int64_t*) va_arg( args, int64_t * );
503 *p_i64 = p_sys->i_pos;
506 case STREAM_SET_POSITION:
508 int64_t i64 = (int64_t)va_arg( args, int64_t );
510 if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
511 i_skip = i64 - p_sys->i_pos;
515 int i_read = DStreamRead( s, NULL, i_skip );
516 if( i_read <= 0 ) return VLC_EGENERIC;
523 p_int = (int*) va_arg( args, int * );
527 case STREAM_CONTROL_ACCESS:
528 case STREAM_GET_CONTENT_TYPE:
532 msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
537 static int DStreamThread( stream_t *s )
539 d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
542 /* Create the demuxer */
543 if( !(p_demux = demux_New( s, "", p_sys->psz_name, "", s, p_sys->out,
549 p_sys->p_demux = p_demux;
552 while( !s->b_die && !p_demux->b_die )
554 if( p_demux->pf_demux( p_demux ) <= 0 ) break;
557 vlc_object_kill( p_demux );
561 /****************************************************************************
563 ****************************************************************************/
564 static bool SkipID3Tag( demux_t *p_demux )
566 const uint8_t *p_peek;
567 uint8_t version, revision;
574 /* Get 10 byte id3 header */
575 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
578 if( memcmp( p_peek, "ID3", 3 ) )
582 revision = p_peek[4];
583 b_footer = p_peek[5] & 0x10;
584 i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
586 if( b_footer ) i_size += 10;
589 /* Skip the entire tag */
590 stream_Read( p_demux->s, NULL, i_size );
592 msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
593 version, revision, i_size );
596 static bool SkipAPETag( demux_t *p_demux )
598 const uint8_t *p_peek;
606 /* Get 32 byte ape header */
607 if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
610 if( memcmp( p_peek, "APETAGEX", 8 ) )
613 i_version = GetDWLE( &p_peek[8] );
614 flags = GetDWLE( &p_peek[8+4+4] );
615 if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
618 i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
620 /* Skip the entire tag */
621 stream_Read( p_demux->s, NULL, i_size );
623 msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
624 i_version/1000, i_size );