]> git.sesse.net Git - vlc/blob - src/input/demux.c
Merge branch '1.0'
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "demux.h"
29 #include <libvlc.h>
30 #include <vlc_codec.h>
31 #include <vlc_meta.h>
32
33 static bool SkipID3Tag( demux_t * );
34 static bool SkipAPETag( demux_t *p_demux );
35
36 /*****************************************************************************
37  * demux_New:
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,
42                        const char *psz_path,
43                        stream_t *s, es_out_t *out, bool b_quick )
44 {
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;
49
50     if( p_demux == NULL ) return NULL;
51
52     /* Parse URL */
53     p_demux->psz_access = strdup( psz_access );
54     p_demux->psz_demux  = strdup( psz_demux );
55     p_demux->psz_path   = strdup( psz_path );
56
57     /* Take into account "demux" to be able to do :demux=dump */
58     if( p_demux->psz_demux && *p_demux->psz_demux == '\0' )
59     {
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( "" );
64     }
65
66     if( !b_quick )
67     {
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 );
70     }
71
72     p_demux->s          = s;
73     p_demux->out        = out;
74
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;
81
82     if( s ) psz_module = p_demux->psz_demux;
83     else psz_module = p_demux->psz_access;
84
85     if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
86     {
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
89         *     anyway
90         *  - wav can't be added 'cause of a52 and dts in them as raw audio
91          */
92          static const struct { char ext[5]; char demux[9]; } exttodemux[] =
93          {
94             { "aac",  "aac" },
95             { "aiff", "aiff" },
96             { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
97             { "avi",  "avi" },
98             { "au",   "au" },
99             { "flac", "flac" },
100             { "dv",   "dv" },
101             { "drc",  "dirac" },
102             { "m3u",  "playlist" },
103             { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
104             { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
105             { "mod",  "mod" }, { "it",  "mod" }, { "s3m",  "mod" }, { "xm",   "mod" },
106             { "nsv",  "nsv" },
107             { "ogg",  "ogg" }, { "ogm",  "ogg" }, /* legacy Ogg */
108             { "oga",  "ogg" }, { "spx",  "ogg" }, { "ogv", "ogg" },
109             { "ogx",  "ogg" }, /*RFC5334*/
110             { "pva",  "pva" },
111             { "rm",   "rm" },
112             { "m4v",  "m4v" },
113             { "h264", "h264" },
114             { "voc",  "voc" },
115             { "mid",  "smf" }, { "rmi",  "smf" },
116             { "",  "" },
117         };
118         /* Here, we don't mind if it does not work, it must be quick */
119         static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
120         {
121             { "mp3", "mpga" },
122             { "ogg", "ogg" },
123             { "wma", "asf" },
124             { "", "" }
125         };
126
127         const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
128         int  i;
129
130         if( !b_quick )
131         {
132             for( i = 0; exttodemux[i].ext[0]; i++ )
133             {
134                 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
135                 {
136                     psz_module = exttodemux[i].demux;
137                     break;
138                 }
139             }
140         }
141         else
142         {
143             for( i = 0; exttodemux_quick[i].ext[0]; i++ )
144             {
145                 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
146                 {
147                     psz_module = exttodemux_quick[i].demux;
148                     break;
149                 }
150             }
151
152         }
153     }
154
155     /* Before module_need (for var_Create...) */
156     vlc_object_attach( p_demux, p_obj );
157
158     if( s )
159     {
160         /* ID3/APE tags will mess-up demuxer probing so we skip it here.
161          * ID3/APE parsers will called later on in the demuxer to access the
162          * skipped info. */
163         if( !SkipID3Tag( p_demux ) )
164             SkipAPETag( p_demux );
165
166         p_demux->p_module =
167             module_need( p_demux, "demux", psz_module,
168                          !strcmp( psz_module, p_demux->psz_demux ) ?
169                          true : false );
170     }
171     else
172     {
173         p_demux->p_module =
174             module_need( p_demux, "access_demux", psz_module,
175                          !strcmp( psz_module, p_demux->psz_access ) ?
176                          true : false );
177     }
178
179     if( p_demux->p_module == NULL )
180     {
181         vlc_object_detach( p_demux );
182         free( p_demux->psz_path );
183         free( p_demux->psz_demux );
184         free( p_demux->psz_access );
185         vlc_object_release( p_demux );
186         return NULL;
187     }
188
189     return p_demux;
190 }
191
192 /*****************************************************************************
193  * demux_Delete:
194  *****************************************************************************/
195 void demux_Delete( demux_t *p_demux )
196 {
197     module_unneed( p_demux, p_demux->p_module );
198     vlc_object_detach( p_demux );
199
200     free( p_demux->psz_path );
201     free( p_demux->psz_demux );
202     free( p_demux->psz_access );
203
204     vlc_object_release( p_demux );
205 }
206
207 /*****************************************************************************
208  * demux_vaControlHelper:
209  *****************************************************************************/
210 int demux_vaControlHelper( stream_t *s,
211                             int64_t i_start, int64_t i_end,
212                             int64_t i_bitrate, int i_align,
213                             int i_query, va_list args )
214 {
215     int64_t i_tell;
216     double  f, *pf;
217     int64_t i64, *pi64;
218
219     if( i_end < 0 )    i_end   = stream_Size( s );
220     if( i_start < 0 )  i_start = 0;
221     if( i_align <= 0 ) i_align = 1;
222     i_tell = stream_Tell( s );
223
224     switch( i_query )
225     {
226         case DEMUX_GET_LENGTH:
227             pi64 = (int64_t*)va_arg( args, int64_t * );
228             if( i_bitrate > 0 && i_end > i_start )
229             {
230                 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
231                 return VLC_SUCCESS;
232             }
233             return VLC_EGENERIC;
234
235         case DEMUX_GET_TIME:
236             pi64 = (int64_t*)va_arg( args, int64_t * );
237             if( i_bitrate > 0 && i_tell >= i_start )
238             {
239                 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
240                 return VLC_SUCCESS;
241             }
242             return VLC_EGENERIC;
243
244         case DEMUX_GET_POSITION:
245             pf = (double*)va_arg( args, double * );
246             if( i_start < i_end )
247             {
248                 *pf = (double)( i_tell - i_start ) /
249                       (double)( i_end  - i_start );
250                 return VLC_SUCCESS;
251             }
252             return VLC_EGENERIC;
253
254
255         case DEMUX_SET_POSITION:
256             f = (double)va_arg( args, double );
257             if( i_start < i_end && f >= 0.0 && f <= 1.0 )
258             {
259                 int64_t i_block = (f * ( i_end - i_start )) / i_align;
260
261                 if( stream_Seek( s, i_start + i_block * i_align ) )
262                 {
263                     return VLC_EGENERIC;
264                 }
265                 return VLC_SUCCESS;
266             }
267             return VLC_EGENERIC;
268
269         case DEMUX_SET_TIME:
270             i64 = (int64_t)va_arg( args, int64_t );
271             if( i_bitrate > 0 && i64 >= 0 )
272             {
273                 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
274                 if( stream_Seek( s, i_start + i_block * i_align ) )
275                 {
276                     return VLC_EGENERIC;
277                 }
278                 return VLC_SUCCESS;
279             }
280             return VLC_EGENERIC;
281
282         case DEMUX_GET_FPS:
283         case DEMUX_GET_META:
284         case DEMUX_HAS_UNSUPPORTED_META:
285         case DEMUX_SET_NEXT_DEMUX_TIME:
286         case DEMUX_GET_TITLE_INFO:
287         case DEMUX_SET_GROUP:
288         case DEMUX_GET_ATTACHMENTS:
289         case DEMUX_CAN_RECORD:
290         case DEMUX_SET_RECORD_STATE:
291             return VLC_EGENERIC;
292
293         default:
294             msg_Err( s, "unknown query in demux_vaControlDefault" );
295             return VLC_EGENERIC;
296     }
297 }
298
299 /****************************************************************************
300  * Utility functions
301  ****************************************************************************/
302 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
303 {
304     decoder_t *p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_DECODER );
305
306     if( !p_packetizer )
307     {
308         es_format_Clean( p_fmt );
309         return NULL;
310     }
311     p_fmt->b_packetized = false;
312
313     p_packetizer->pf_decode_audio = NULL;
314     p_packetizer->pf_decode_video = NULL;
315     p_packetizer->pf_decode_sub = NULL;
316     p_packetizer->pf_packetize = NULL;
317
318     p_packetizer->fmt_in = *p_fmt;
319     es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
320
321     p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
322     if( !p_packetizer->p_module )
323     {
324         es_format_Clean( p_fmt );
325         vlc_object_release( p_packetizer );
326         msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
327         return NULL;
328     }
329
330     return p_packetizer;
331 }
332
333 void demux_PacketizerDestroy( decoder_t *p_packetizer )
334 {
335     if( p_packetizer->p_module )
336         module_unneed( p_packetizer, p_packetizer->p_module );
337     es_format_Clean( &p_packetizer->fmt_in );
338     if( p_packetizer->p_description )
339         vlc_meta_Delete( p_packetizer->p_description );
340     vlc_object_release( p_packetizer );
341 }
342
343 static bool SkipID3Tag( demux_t *p_demux )
344 {
345     const uint8_t *p_peek;
346     uint8_t version, revision;
347     int i_size;
348     int b_footer;
349
350     if( !p_demux->s )
351         return false;
352
353     /* Get 10 byte id3 header */
354     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
355         return false;
356
357     if( memcmp( p_peek, "ID3", 3 ) )
358         return false;
359
360     version = p_peek[3];
361     revision = p_peek[4];
362     b_footer = p_peek[5] & 0x10;
363     i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
364
365     if( b_footer ) i_size += 10;
366     i_size += 10;
367
368     /* Skip the entire tag */
369     stream_Read( p_demux->s, NULL, i_size );
370
371     msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
372              version, revision, i_size );
373     return true;
374 }
375 static bool SkipAPETag( demux_t *p_demux )
376 {
377     const uint8_t *p_peek;
378     int i_version;
379     int i_size;
380     uint32_t flags;
381
382     if( !p_demux->s )
383         return false;
384
385     /* Get 32 byte ape header */
386     if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
387         return false;
388
389     if( memcmp( p_peek, "APETAGEX", 8 ) )
390         return false;
391
392     i_version = GetDWLE( &p_peek[8] );
393     flags = GetDWLE( &p_peek[8+4+4] );
394     if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
395         return false;
396
397     i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
398
399     /* Skip the entire tag */
400     stream_Read( p_demux->s, NULL, i_size );
401
402     msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
403              i_version/1000, i_size );
404     return true;
405 }
406