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 *****************************************************************************/
30 #include <vlc_codec.h>
33 #include <vlc_modules.h>
35 static bool SkipID3Tag( demux_t * );
36 static bool SkipAPETag( demux_t *p_demux );
38 /* Decode URL (which has had its scheme stripped earlier) to a file path. */
39 /* XXX: evil code duplication from access.c */
40 static char *get_path(const char *location)
44 /* Prepending "file://" is a bit hackish. But then again, we do not want
45 * to hard-code the list of schemes that use file paths in make_path().
47 if (asprintf(&url, "file://%s", location) == -1)
50 path = make_path (url);
56 /*****************************************************************************
58 * if s is NULL then load a access_demux
59 *****************************************************************************/
60 demux_t *demux_New( vlc_object_t *p_obj, input_thread_t *p_parent_input,
61 const char *psz_access, const char *psz_demux,
62 const char *psz_location,
63 stream_t *s, es_out_t *out, bool b_quick )
65 demux_t *p_demux = vlc_custom_create( p_obj, sizeof( *p_demux ), "demux" );
66 const char *psz_module;
68 if( p_demux == NULL ) return NULL;
70 p_demux->p_input = p_parent_input;
73 p_demux->psz_access = strdup( psz_access );
74 p_demux->psz_demux = strdup( psz_demux );
75 p_demux->psz_location = strdup( psz_location );
76 p_demux->psz_file = get_path( psz_location );
78 /* Take into account "demux" to be able to do :demux=dump */
79 if( *p_demux->psz_demux == '\0' )
81 free( p_demux->psz_demux );
82 p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
83 if( p_demux->psz_demux == NULL )
84 p_demux->psz_demux = strdup( "" );
88 msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' "
89 "location='%s' file='%s'",
90 p_demux->psz_access, p_demux->psz_demux,
91 p_demux->psz_location, p_demux->psz_file );
96 p_demux->pf_demux = NULL;
97 p_demux->pf_control = NULL;
98 p_demux->p_sys = NULL;
99 p_demux->info.i_update = 0;
100 p_demux->info.i_title = 0;
101 p_demux->info.i_seekpoint = 0;
103 if( s ) psz_module = p_demux->psz_demux;
104 else psz_module = p_demux->psz_access;
108 if( s && *psz_module == '\0'
109 && p_demux->psz_file != NULL
110 && (psz_ext = strrchr( p_demux->psz_file, '.' )) )
112 /* XXX: add only file without any problem here and with strong detection.
113 * - no .mp3, .a52, ... (aac is added as it works only by file ext
115 * - wav can't be added 'cause of a52 and dts in them as raw audio
117 static const struct { char ext[5]; char demux[9]; } exttodemux[] =
121 { "asf", "asf" }, { "wmv", "asf" }, { "wma", "asf" },
129 { "mkv", "mkv" }, { "mka", "mkv" }, { "mks", "mkv" },
130 { "mp4", "mp4" }, { "m4a", "mp4" }, { "mov", "mp4" }, { "moov", "mp4" },
132 { "ogg", "ogg" }, { "ogm", "ogg" }, /* legacy Ogg */
133 { "oga", "ogg" }, { "spx", "ogg" }, { "ogv", "ogg" },
134 { "ogx", "ogg" }, /*RFC5334*/
136 { "rm", "avformat" },
140 { "mid", "smf" }, { "rmi", "smf" },
143 /* Here, we don't mind if it does not work, it must be quick */
144 static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
152 psz_ext++; // skip '.'
156 for( unsigned i = 0; exttodemux[i].ext[0]; i++ )
158 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
160 psz_module = exttodemux[i].demux;
167 for( unsigned i = 0; exttodemux_quick[i].ext[0]; i++ )
169 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
171 psz_module = exttodemux_quick[i].demux;
181 /* ID3/APE tags will mess-up demuxer probing so we skip it here.
182 * ID3/APE parsers will called later on in the demuxer to access the
184 while (SkipID3Tag( p_demux ))
186 SkipAPETag( p_demux );
189 module_need( p_demux, "demux", psz_module,
190 !strcmp( psz_module, p_demux->psz_demux ) );
195 module_need( p_demux, "access_demux", psz_module,
196 !strcmp( psz_module, p_demux->psz_access ) );
199 if( p_demux->p_module == NULL )
201 free( p_demux->psz_file );
202 free( p_demux->psz_location );
203 free( p_demux->psz_demux );
204 free( p_demux->psz_access );
205 vlc_object_release( p_demux );
212 /*****************************************************************************
214 *****************************************************************************/
215 void demux_Delete( demux_t *p_demux )
217 module_unneed( p_demux, p_demux->p_module );
219 free( p_demux->psz_file );
220 free( p_demux->psz_location );
221 free( p_demux->psz_demux );
222 free( p_demux->psz_access );
224 vlc_object_release( p_demux );
227 /*****************************************************************************
228 * demux_GetParentInput:
229 *****************************************************************************/
230 input_thread_t * demux_GetParentInput( demux_t *p_demux )
232 return p_demux->p_input ? vlc_object_hold((vlc_object_t*)p_demux->p_input) : NULL;
236 /*****************************************************************************
237 * demux_vaControlHelper:
238 *****************************************************************************/
239 int demux_vaControlHelper( stream_t *s,
240 int64_t i_start, int64_t i_end,
241 int64_t i_bitrate, int i_align,
242 int i_query, va_list args )
248 if( i_end < 0 ) i_end = stream_Size( s );
249 if( i_start < 0 ) i_start = 0;
250 if( i_align <= 0 ) i_align = 1;
251 i_tell = stream_Tell( s );
255 case DEMUX_GET_LENGTH:
256 pi64 = (int64_t*)va_arg( args, int64_t * );
257 if( i_bitrate > 0 && i_end > i_start )
259 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
265 pi64 = (int64_t*)va_arg( args, int64_t * );
266 if( i_bitrate > 0 && i_tell >= i_start )
268 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
273 case DEMUX_GET_POSITION:
274 pf = (double*)va_arg( args, double * );
275 if( i_start < i_end )
277 *pf = (double)( i_tell - i_start ) /
278 (double)( i_end - i_start );
284 case DEMUX_SET_POSITION:
285 f = (double)va_arg( args, double );
286 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
288 int64_t i_block = (f * ( i_end - i_start )) / i_align;
290 if( stream_Seek( s, i_start + i_block * i_align ) )
299 i64 = (int64_t)va_arg( args, int64_t );
300 if( i_bitrate > 0 && i64 >= 0 )
302 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
303 if( stream_Seek( s, i_start + i_block * i_align ) )
311 case DEMUX_GET_PTS_DELAY:
314 case DEMUX_HAS_UNSUPPORTED_META:
315 case DEMUX_SET_NEXT_DEMUX_TIME:
316 case DEMUX_GET_TITLE_INFO:
317 case DEMUX_SET_GROUP:
318 case DEMUX_GET_ATTACHMENTS:
319 case DEMUX_CAN_RECORD:
320 case DEMUX_SET_RECORD_STATE:
324 msg_Err( s, "unknown query in demux_vaControlDefault" );
329 /****************************************************************************
331 ****************************************************************************/
332 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
334 decoder_t *p_packetizer;
335 p_packetizer = vlc_custom_create( p_demux, sizeof( *p_packetizer ),
336 "demux packetizer" );
339 es_format_Clean( p_fmt );
342 p_fmt->b_packetized = false;
344 p_packetizer->pf_decode_audio = NULL;
345 p_packetizer->pf_decode_video = NULL;
346 p_packetizer->pf_decode_sub = NULL;
347 p_packetizer->pf_packetize = NULL;
349 p_packetizer->fmt_in = *p_fmt;
350 es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
352 p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
353 if( !p_packetizer->p_module )
355 es_format_Clean( p_fmt );
356 vlc_object_release( p_packetizer );
357 msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
364 void demux_PacketizerDestroy( decoder_t *p_packetizer )
366 if( p_packetizer->p_module )
367 module_unneed( p_packetizer, p_packetizer->p_module );
368 es_format_Clean( &p_packetizer->fmt_in );
369 if( p_packetizer->p_description )
370 vlc_meta_Delete( p_packetizer->p_description );
371 vlc_object_release( p_packetizer );
374 static bool SkipID3Tag( demux_t *p_demux )
376 const uint8_t *p_peek;
377 uint8_t version, revision;
384 /* Get 10 byte id3 header */
385 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
388 if( memcmp( p_peek, "ID3", 3 ) )
392 revision = p_peek[4];
393 b_footer = p_peek[5] & 0x10;
394 i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
396 if( b_footer ) i_size += 10;
399 /* Skip the entire tag */
400 stream_Read( p_demux->s, NULL, i_size );
402 msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
403 version, revision, i_size );
406 static bool SkipAPETag( demux_t *p_demux )
408 const uint8_t *p_peek;
416 /* Get 32 byte ape header */
417 if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
420 if( memcmp( p_peek, "APETAGEX", 8 ) )
423 i_version = GetDWLE( &p_peek[8] );
424 flags = GetDWLE( &p_peek[8+4+4] );
425 if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
428 i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
430 /* Skip the entire tag */
431 stream_Read( p_demux->s, NULL, i_size );
433 msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
434 i_version/1000, i_size );