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, input_thread_t *p_parent_input,
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;
52 p_demux->p_input = p_parent_input;
55 p_demux->psz_access = strdup( psz_access );
56 p_demux->psz_demux = strdup( psz_demux );
57 p_demux->psz_path = strdup( psz_path );
59 /* Take into account "demux" to be able to do :demux=dump */
60 if( p_demux->psz_demux && *p_demux->psz_demux == '\0' )
62 free( p_demux->psz_demux );
63 p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
64 if( p_demux->psz_demux == NULL )
65 p_demux->psz_demux = strdup( "" );
70 msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
71 p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
77 p_demux->pf_demux = NULL;
78 p_demux->pf_control = NULL;
79 p_demux->p_sys = NULL;
80 p_demux->info.i_update = 0;
81 p_demux->info.i_title = 0;
82 p_demux->info.i_seekpoint = 0;
84 if( s ) psz_module = p_demux->psz_demux;
85 else psz_module = p_demux->psz_access;
87 if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
89 /* XXX: add only file without any problem here and with strong detection.
90 * - no .mp3, .a52, ... (aac is added as it works only by file ext
92 * - wav can't be added 'cause of a52 and dts in them as raw audio
94 static const struct { char ext[5]; char demux[9]; } exttodemux[] =
98 { "asf", "asf" }, { "wmv", "asf" }, { "wma", "asf" },
106 { "mkv", "mkv" }, { "mka", "mkv" }, { "mks", "mkv" },
107 { "mp4", "mp4" }, { "m4a", "mp4" }, { "mov", "mp4" }, { "moov", "mp4" },
109 { "ogg", "ogg" }, { "ogm", "ogg" }, /* legacy Ogg */
110 { "oga", "ogg" }, { "spx", "ogg" }, { "ogv", "ogg" },
111 { "ogx", "ogg" }, /*RFC5334*/
117 { "mid", "smf" }, { "rmi", "smf" },
120 /* Here, we don't mind if it does not work, it must be quick */
121 static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
129 const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
134 for( i = 0; exttodemux[i].ext[0]; i++ )
136 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
138 psz_module = exttodemux[i].demux;
145 for( i = 0; exttodemux_quick[i].ext[0]; i++ )
147 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
149 psz_module = exttodemux_quick[i].demux;
157 /* Before module_need (for var_Create...) */
158 vlc_object_attach( p_demux, p_obj );
162 /* ID3/APE tags will mess-up demuxer probing so we skip it here.
163 * ID3/APE parsers will called later on in the demuxer to access the
165 if( !SkipID3Tag( p_demux ) )
166 SkipAPETag( p_demux );
169 module_need( p_demux, "demux", psz_module,
170 !strcmp( psz_module, p_demux->psz_demux ) );
175 module_need( p_demux, "access_demux", psz_module,
176 !strcmp( psz_module, p_demux->psz_access ) );
179 if( p_demux->p_module == NULL )
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 );
198 free( p_demux->psz_path );
199 free( p_demux->psz_demux );
200 free( p_demux->psz_access );
202 vlc_object_release( p_demux );
205 /*****************************************************************************
206 * demux_GetParentInput:
207 *****************************************************************************/
208 input_thread_t * demux_GetParentInput( demux_t *p_demux )
210 return p_demux->p_input ? vlc_object_hold((vlc_object_t*)p_demux->p_input) : NULL;
214 /*****************************************************************************
215 * demux_vaControlHelper:
216 *****************************************************************************/
217 int demux_vaControlHelper( stream_t *s,
218 int64_t i_start, int64_t i_end,
219 int64_t i_bitrate, int i_align,
220 int i_query, va_list args )
226 if( i_end < 0 ) i_end = stream_Size( s );
227 if( i_start < 0 ) i_start = 0;
228 if( i_align <= 0 ) i_align = 1;
229 i_tell = stream_Tell( s );
233 case DEMUX_GET_LENGTH:
234 pi64 = (int64_t*)va_arg( args, int64_t * );
235 if( i_bitrate > 0 && i_end > i_start )
237 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
243 pi64 = (int64_t*)va_arg( args, int64_t * );
244 if( i_bitrate > 0 && i_tell >= i_start )
246 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
251 case DEMUX_GET_POSITION:
252 pf = (double*)va_arg( args, double * );
253 if( i_start < i_end )
255 *pf = (double)( i_tell - i_start ) /
256 (double)( i_end - i_start );
262 case DEMUX_SET_POSITION:
263 f = (double)va_arg( args, double );
264 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
266 int64_t i_block = (f * ( i_end - i_start )) / i_align;
268 if( stream_Seek( s, i_start + i_block * i_align ) )
277 i64 = (int64_t)va_arg( args, int64_t );
278 if( i_bitrate > 0 && i64 >= 0 )
280 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
281 if( stream_Seek( s, i_start + i_block * i_align ) )
291 case DEMUX_HAS_UNSUPPORTED_META:
292 case DEMUX_SET_NEXT_DEMUX_TIME:
293 case DEMUX_GET_TITLE_INFO:
294 case DEMUX_SET_GROUP:
295 case DEMUX_GET_ATTACHMENTS:
296 case DEMUX_CAN_RECORD:
297 case DEMUX_SET_RECORD_STATE:
301 msg_Err( s, "unknown query in demux_vaControlDefault" );
306 /****************************************************************************
308 ****************************************************************************/
309 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
311 decoder_t *p_packetizer;
312 p_packetizer = vlc_custom_create( p_demux, sizeof( *p_packetizer ),
313 VLC_OBJECT_GENERIC, "demux packetizer" );
316 es_format_Clean( p_fmt );
319 p_fmt->b_packetized = false;
321 p_packetizer->pf_decode_audio = NULL;
322 p_packetizer->pf_decode_video = NULL;
323 p_packetizer->pf_decode_sub = NULL;
324 p_packetizer->pf_packetize = NULL;
326 p_packetizer->fmt_in = *p_fmt;
327 es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
329 p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
330 if( !p_packetizer->p_module )
332 es_format_Clean( p_fmt );
333 vlc_object_release( p_packetizer );
334 msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
341 void demux_PacketizerDestroy( decoder_t *p_packetizer )
343 if( p_packetizer->p_module )
344 module_unneed( p_packetizer, p_packetizer->p_module );
345 es_format_Clean( &p_packetizer->fmt_in );
346 if( p_packetizer->p_description )
347 vlc_meta_Delete( p_packetizer->p_description );
348 vlc_object_release( p_packetizer );
351 static bool SkipID3Tag( demux_t *p_demux )
353 const uint8_t *p_peek;
354 uint8_t version, revision;
361 /* Get 10 byte id3 header */
362 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
365 if( memcmp( p_peek, "ID3", 3 ) )
369 revision = p_peek[4];
370 b_footer = p_peek[5] & 0x10;
371 i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
373 if( b_footer ) i_size += 10;
376 /* Skip the entire tag */
377 stream_Read( p_demux->s, NULL, i_size );
379 msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
380 version, revision, i_size );
383 static bool SkipAPETag( demux_t *p_demux )
385 const uint8_t *p_peek;
393 /* Get 32 byte ape header */
394 if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
397 if( memcmp( p_peek, "APETAGEX", 8 ) )
400 i_version = GetDWLE( &p_peek[8] );
401 flags = GetDWLE( &p_peek[8+4+4] );
402 if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
405 i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
407 /* Skip the entire tag */
408 stream_Read( p_demux->s, NULL, i_size );
410 msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
411 i_version/1000, i_size );