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 static bool SkipID3Tag( demux_t * );
34 static bool SkipAPETag( demux_t *p_demux );
36 /*****************************************************************************
38 * if s is NULL then load a access_demux
39 *****************************************************************************/
40 demux_t *__demux_New( vlc_object_t *p_obj,
41 const char *psz_access, const char *psz_demux,
43 stream_t *s, es_out_t *out, bool b_quick )
45 static const char typename[] = "demux";
46 demux_t *p_demux = vlc_custom_create( p_obj, sizeof( *p_demux ),
47 VLC_OBJECT_GENERIC, typename );
48 const char *psz_module;
50 if( p_demux == NULL ) return NULL;
53 p_demux->psz_access = strdup( psz_access );
54 p_demux->psz_demux = strdup( psz_demux );
55 p_demux->psz_path = strdup( psz_path );
57 /* Take into account "demux" to be able to do :demux=dump */
58 if( p_demux->psz_demux && *p_demux->psz_demux == '\0' )
60 free( p_demux->psz_demux );
61 p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
62 if( p_demux->psz_demux == NULL )
63 p_demux->psz_demux = strdup( "" );
68 msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
69 p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
75 p_demux->pf_demux = NULL;
76 p_demux->pf_control = NULL;
77 p_demux->p_sys = NULL;
78 p_demux->info.i_update = 0;
79 p_demux->info.i_title = 0;
80 p_demux->info.i_seekpoint = 0;
82 if( s ) psz_module = p_demux->psz_demux;
83 else psz_module = p_demux->psz_access;
85 if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
87 /* XXX: add only file without any problem here and with strong detection.
88 * - no .mp3, .a52, ... (aac is added as it works only by file ext
90 * - wav can't be added 'cause of a52 and dts in them as raw audio
92 static const struct { char ext[5]; char demux[9]; } exttodemux[] =
96 { "asf", "asf" }, { "wmv", "asf" }, { "wma", "asf" },
101 { "m3u", "playlist" },
102 { "mkv", "mkv" }, { "mka", "mkv" }, { "mks", "mkv" },
103 { "mp4", "mp4" }, { "m4a", "mp4" }, { "mov", "mp4" }, { "moov", "mp4" },
104 { "mod", "mod" }, { "it", "mod" }, { "s3m", "mod" }, { "xm", "mod" },
106 { "ogg", "ogg" }, { "ogm", "ogg" }, /* legacy Ogg */
107 { "oga", "ogg" }, { "spx", "ogg" }, { "ogv", "ogg" },
108 { "ogx", "ogg" }, /*RFC5334*/
114 { "mid", "smf" }, { "rmi", "smf" },
117 /* Here, we don't mind if it does not work, it must be quick */
118 static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
126 const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
131 for( i = 0; exttodemux[i].ext[0]; i++ )
133 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
135 psz_module = exttodemux[i].demux;
142 for( i = 0; exttodemux_quick[i].ext[0]; i++ )
144 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
146 psz_module = exttodemux_quick[i].demux;
154 /* Before module_need (for var_Create...) */
155 vlc_object_attach( p_demux, p_obj );
159 /* ID3/APE tags will mess-up demuxer probing so we skip it here.
160 * ID3/APE parsers will called later on in the demuxer to access the
162 if( !SkipID3Tag( p_demux ) )
163 SkipAPETag( p_demux );
166 module_need( p_demux, "demux", psz_module,
167 !strcmp( psz_module, p_demux->psz_demux ) ?
173 module_need( p_demux, "access_demux", psz_module,
174 !strcmp( psz_module, p_demux->psz_access ) ?
178 if( p_demux->p_module == NULL )
180 vlc_object_detach( p_demux );
181 free( p_demux->psz_path );
182 free( p_demux->psz_demux );
183 free( p_demux->psz_access );
184 vlc_object_release( p_demux );
191 /*****************************************************************************
193 *****************************************************************************/
194 void demux_Delete( demux_t *p_demux )
196 module_unneed( p_demux, p_demux->p_module );
197 vlc_object_detach( p_demux );
199 free( p_demux->psz_path );
200 free( p_demux->psz_demux );
201 free( p_demux->psz_access );
203 vlc_object_release( p_demux );
206 /*****************************************************************************
207 * demux_vaControlHelper:
208 *****************************************************************************/
209 int demux_vaControlHelper( stream_t *s,
210 int64_t i_start, int64_t i_end,
211 int64_t i_bitrate, int i_align,
212 int i_query, va_list args )
218 if( i_end < 0 ) i_end = stream_Size( s );
219 if( i_start < 0 ) i_start = 0;
220 if( i_align <= 0 ) i_align = 1;
221 i_tell = stream_Tell( s );
225 case DEMUX_GET_LENGTH:
226 pi64 = (int64_t*)va_arg( args, int64_t * );
227 if( i_bitrate > 0 && i_end > i_start )
229 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
235 pi64 = (int64_t*)va_arg( args, int64_t * );
236 if( i_bitrate > 0 && i_end > i_start )
238 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
243 case DEMUX_GET_POSITION:
244 pf = (double*)va_arg( args, double * );
245 if( i_start < i_end )
247 *pf = (double)( i_tell - i_start ) /
248 (double)( i_end - i_start );
254 case DEMUX_SET_POSITION:
255 f = (double)va_arg( args, double );
256 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
258 int64_t i_block = (f * ( i_end - i_start )) / i_align;
260 if( stream_Seek( s, i_start + i_block * i_align ) )
269 i64 = (int64_t)va_arg( args, int64_t );
270 if( i_bitrate > 0 && i64 >= 0 )
272 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
273 if( stream_Seek( s, i_start + i_block * i_align ) )
283 case DEMUX_HAS_UNSUPPORTED_META:
284 case DEMUX_SET_NEXT_DEMUX_TIME:
285 case DEMUX_GET_TITLE_INFO:
286 case DEMUX_SET_GROUP:
287 case DEMUX_GET_ATTACHMENTS:
288 case DEMUX_CAN_RECORD:
289 case DEMUX_SET_RECORD_STATE:
293 msg_Err( s, "unknown query in demux_vaControlDefault" );
298 /****************************************************************************
300 ****************************************************************************/
301 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
303 decoder_t *p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );
307 es_format_Clean( p_fmt );
311 p_packetizer->pf_decode_audio = NULL;
312 p_packetizer->pf_decode_video = NULL;
313 p_packetizer->pf_decode_sub = NULL;
314 p_packetizer->pf_packetize = NULL;
316 p_packetizer->fmt_in = *p_fmt;
317 es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
319 p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
320 if( !p_packetizer->p_module )
322 es_format_Clean( p_fmt );
323 vlc_object_release( p_packetizer );
324 msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
330 void demux_PacketizerDestroy( decoder_t *p_packetizer )
332 if( p_packetizer->p_module )
333 module_unneed( p_packetizer, p_packetizer->p_module );
334 es_format_Clean( &p_packetizer->fmt_in );
335 if( p_packetizer->p_description )
336 vlc_meta_Delete( p_packetizer->p_description );
337 vlc_object_release( p_packetizer );
340 static bool SkipID3Tag( demux_t *p_demux )
342 const uint8_t *p_peek;
343 uint8_t version, revision;
350 /* Get 10 byte id3 header */
351 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
354 if( memcmp( p_peek, "ID3", 3 ) )
358 revision = p_peek[4];
359 b_footer = p_peek[5] & 0x10;
360 i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
362 if( b_footer ) i_size += 10;
365 /* Skip the entire tag */
366 stream_Read( p_demux->s, NULL, i_size );
368 msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
369 version, revision, i_size );
372 static bool SkipAPETag( demux_t *p_demux )
374 const uint8_t *p_peek;
382 /* Get 32 byte ape header */
383 if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
386 if( memcmp( p_peek, "APETAGEX", 8 ) )
389 i_version = GetDWLE( &p_peek[8] );
390 flags = GetDWLE( &p_peek[8+4+4] );
391 if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
394 i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
396 /* Skip the entire tag */
397 stream_Read( p_demux->s, NULL, i_size );
399 msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
400 i_version/1000, i_size );