]> git.sesse.net Git - vlc/blob - src/input/demux.c
Fixed a (probable) typo in aout_ChangeFilterString().
[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 #include <vlc_url.h>
33
34 static bool SkipID3Tag( demux_t * );
35 static bool SkipAPETag( demux_t *p_demux );
36
37 /* Decode URL (which has had its scheme stripped earlier) to a file path. */
38 /* XXX: evil code duplication from access.c */
39 static char *get_path(const char *location)
40 {
41     char *url, *path;
42
43     /* Prepending "file://" is a bit hackish. But then again, we do not want
44      * to hard-code the list of schemes that use file paths in make_path().
45      */
46     if (asprintf(&url, "file://%s", location) == -1)
47         return NULL;
48
49     path = make_path (url);
50     free (url);
51     return path;
52 }
53
54
55 /*****************************************************************************
56  * demux_New:
57  *  if s is NULL then load a access_demux
58  *****************************************************************************/
59 demux_t *__demux_New( vlc_object_t *p_obj, input_thread_t *p_parent_input,
60                        const char *psz_access, const char *psz_demux,
61                        const char *psz_location,
62                        stream_t *s, es_out_t *out, bool b_quick )
63 {
64     static const char typename[] = "demux";
65     demux_t *p_demux = vlc_custom_create( p_obj, sizeof( *p_demux ),
66                                           VLC_OBJECT_GENERIC, typename );
67     const char *psz_module;
68
69     if( p_demux == NULL ) return NULL;
70
71     p_demux->p_input = p_parent_input;
72
73     /* Parse URL */
74     p_demux->psz_access = strdup( psz_access );
75     p_demux->psz_demux  = strdup( psz_demux );
76     p_demux->psz_location = strdup( psz_location );
77     p_demux->psz_file = get_path( psz_location );
78
79     /* Take into account "demux" to be able to do :demux=dump */
80     if( p_demux->psz_demux && *p_demux->psz_demux == '\0' )
81     {
82         free( p_demux->psz_demux );
83         p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
84         if( p_demux->psz_demux == NULL )
85             p_demux->psz_demux = strdup( "" );
86     }
87
88     if( !b_quick )
89         msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' "
90                  "location='%s' file='%s'",
91                  p_demux->psz_access, p_demux->psz_demux,
92                  p_demux->psz_location, p_demux->psz_file );
93
94     p_demux->s          = s;
95     p_demux->out        = out;
96
97     p_demux->pf_demux   = NULL;
98     p_demux->pf_control = NULL;
99     p_demux->p_sys      = NULL;
100     p_demux->info.i_update = 0;
101     p_demux->info.i_title  = 0;
102     p_demux->info.i_seekpoint = 0;
103
104     if( s ) psz_module = p_demux->psz_demux;
105     else psz_module = p_demux->psz_access;
106
107     const char *psz_ext;
108
109     if( s && *psz_module == '\0'
110      && p_demux->psz_file != NULL
111      && (psz_ext = strrchr( p_demux->psz_file, '.' )) )
112     {
113        /* XXX: add only file without any problem here and with strong detection.
114         *  - no .mp3, .a52, ... (aac is added as it works only by file ext
115         *     anyway
116         *  - wav can't be added 'cause of a52 and dts in them as raw audio
117          */
118          static const struct { char ext[5]; char demux[9]; } exttodemux[] =
119          {
120             { "aac",  "aac" },
121             { "aiff", "aiff" },
122             { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
123             { "avi",  "avi" },
124             { "au",   "au" },
125             { "flac", "flac" },
126             { "dv",   "dv" },
127             { "drc",  "dirac" },
128             { "m3u",  "m3u" },
129             { "m3u8", "m3u8" },
130             { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
131             { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
132             { "nsv",  "nsv" },
133             { "ogg",  "ogg" }, { "ogm",  "ogg" }, /* legacy Ogg */
134             { "oga",  "ogg" }, { "spx",  "ogg" }, { "ogv", "ogg" },
135             { "ogx",  "ogg" }, /*RFC5334*/
136             { "pva",  "pva" },
137             { "rm",   "rm" },
138             { "m4v",  "m4v" },
139             { "h264", "h264" },
140             { "voc",  "voc" },
141             { "mid",  "smf" }, { "rmi",  "smf" },
142             { "",  "" },
143         };
144         /* Here, we don't mind if it does not work, it must be quick */
145         static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
146         {
147             { "mp3", "mpga" },
148             { "ogg", "ogg" },
149             { "wma", "asf" },
150             { "", "" }
151         };
152
153         psz_ext++; // skip '.'
154
155         if( !b_quick )
156         {
157             for( unsigned i = 0; exttodemux[i].ext[0]; i++ )
158             {
159                 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
160                 {
161                     psz_module = exttodemux[i].demux;
162                     break;
163                 }
164             }
165         }
166         else
167         {
168             for( unsigned i = 0; exttodemux_quick[i].ext[0]; i++ )
169             {
170                 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
171                 {
172                     psz_module = exttodemux_quick[i].demux;
173                     break;
174                 }
175             }
176
177         }
178     }
179
180     /* Before module_need (for var_Create...) */
181     vlc_object_attach( p_demux, p_obj );
182
183     if( s )
184     {
185         /* ID3/APE tags will mess-up demuxer probing so we skip it here.
186          * ID3/APE parsers will called later on in the demuxer to access the
187          * skipped info. */
188         if( !SkipID3Tag( p_demux ) )
189             SkipAPETag( p_demux );
190
191         p_demux->p_module =
192             module_need( p_demux, "demux", psz_module,
193                          !strcmp( psz_module, p_demux->psz_demux ) );
194     }
195     else
196     {
197         p_demux->p_module =
198             module_need( p_demux, "access_demux", psz_module,
199                          !strcmp( psz_module, p_demux->psz_access ) );
200     }
201
202     if( p_demux->p_module == NULL )
203     {
204         free( p_demux->psz_file );
205         free( p_demux->psz_location );
206         free( p_demux->psz_demux );
207         free( p_demux->psz_access );
208         vlc_object_release( p_demux );
209         return NULL;
210     }
211
212     return p_demux;
213 }
214
215 /*****************************************************************************
216  * demux_Delete:
217  *****************************************************************************/
218 void demux_Delete( demux_t *p_demux )
219 {
220     module_unneed( p_demux, p_demux->p_module );
221
222     free( p_demux->psz_file );
223     free( p_demux->psz_location );
224     free( p_demux->psz_demux );
225     free( p_demux->psz_access );
226
227     vlc_object_release( p_demux );
228 }
229
230 /*****************************************************************************
231  * demux_GetParentInput:
232  *****************************************************************************/
233 input_thread_t * demux_GetParentInput( demux_t *p_demux )
234 {
235     return p_demux->p_input ? vlc_object_hold((vlc_object_t*)p_demux->p_input) : NULL;
236 }
237
238
239 /*****************************************************************************
240  * demux_vaControlHelper:
241  *****************************************************************************/
242 int demux_vaControlHelper( stream_t *s,
243                             int64_t i_start, int64_t i_end,
244                             int64_t i_bitrate, int i_align,
245                             int i_query, va_list args )
246 {
247     int64_t i_tell;
248     double  f, *pf;
249     int64_t i64, *pi64;
250
251     if( i_end < 0 )    i_end   = stream_Size( s );
252     if( i_start < 0 )  i_start = 0;
253     if( i_align <= 0 ) i_align = 1;
254     i_tell = stream_Tell( s );
255
256     switch( i_query )
257     {
258         case DEMUX_GET_LENGTH:
259             pi64 = (int64_t*)va_arg( args, int64_t * );
260             if( i_bitrate > 0 && i_end > i_start )
261             {
262                 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
263                 return VLC_SUCCESS;
264             }
265             return VLC_EGENERIC;
266
267         case DEMUX_GET_TIME:
268             pi64 = (int64_t*)va_arg( args, int64_t * );
269             if( i_bitrate > 0 && i_tell >= i_start )
270             {
271                 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
272                 return VLC_SUCCESS;
273             }
274             return VLC_EGENERIC;
275
276         case DEMUX_GET_POSITION:
277             pf = (double*)va_arg( args, double * );
278             if( i_start < i_end )
279             {
280                 *pf = (double)( i_tell - i_start ) /
281                       (double)( i_end  - i_start );
282                 return VLC_SUCCESS;
283             }
284             return VLC_EGENERIC;
285
286
287         case DEMUX_SET_POSITION:
288             f = (double)va_arg( args, double );
289             if( i_start < i_end && f >= 0.0 && f <= 1.0 )
290             {
291                 int64_t i_block = (f * ( i_end - i_start )) / i_align;
292
293                 if( stream_Seek( s, i_start + i_block * i_align ) )
294                 {
295                     return VLC_EGENERIC;
296                 }
297                 return VLC_SUCCESS;
298             }
299             return VLC_EGENERIC;
300
301         case DEMUX_SET_TIME:
302             i64 = (int64_t)va_arg( args, int64_t );
303             if( i_bitrate > 0 && i64 >= 0 )
304             {
305                 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
306                 if( stream_Seek( s, i_start + i_block * i_align ) )
307                 {
308                     return VLC_EGENERIC;
309                 }
310                 return VLC_SUCCESS;
311             }
312             return VLC_EGENERIC;
313
314         case DEMUX_GET_FPS:
315         case DEMUX_GET_META:
316         case DEMUX_HAS_UNSUPPORTED_META:
317         case DEMUX_SET_NEXT_DEMUX_TIME:
318         case DEMUX_GET_TITLE_INFO:
319         case DEMUX_SET_GROUP:
320         case DEMUX_GET_ATTACHMENTS:
321         case DEMUX_CAN_RECORD:
322         case DEMUX_SET_RECORD_STATE:
323             return VLC_EGENERIC;
324
325         default:
326             msg_Err( s, "unknown query in demux_vaControlDefault" );
327             return VLC_EGENERIC;
328     }
329 }
330
331 /****************************************************************************
332  * Utility functions
333  ****************************************************************************/
334 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
335 {
336     decoder_t *p_packetizer;
337     p_packetizer = vlc_custom_create( p_demux, sizeof( *p_packetizer ),
338                                       VLC_OBJECT_GENERIC, "demux packetizer" );
339     if( !p_packetizer )
340     {
341         es_format_Clean( p_fmt );
342         return NULL;
343     }
344     p_fmt->b_packetized = false;
345
346     p_packetizer->pf_decode_audio = NULL;
347     p_packetizer->pf_decode_video = NULL;
348     p_packetizer->pf_decode_sub = NULL;
349     p_packetizer->pf_packetize = NULL;
350
351     p_packetizer->fmt_in = *p_fmt;
352     es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
353
354     vlc_object_attach( p_packetizer, p_demux );
355     p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
356     if( !p_packetizer->p_module )
357     {
358         es_format_Clean( p_fmt );
359         vlc_object_release( p_packetizer );
360         msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
361         return NULL;
362     }
363
364     return p_packetizer;
365 }
366
367 void demux_PacketizerDestroy( decoder_t *p_packetizer )
368 {
369     if( p_packetizer->p_module )
370         module_unneed( p_packetizer, p_packetizer->p_module );
371     es_format_Clean( &p_packetizer->fmt_in );
372     if( p_packetizer->p_description )
373         vlc_meta_Delete( p_packetizer->p_description );
374     vlc_object_release( p_packetizer );
375 }
376
377 static bool SkipID3Tag( demux_t *p_demux )
378 {
379     const uint8_t *p_peek;
380     uint8_t version, revision;
381     int i_size;
382     int b_footer;
383
384     if( !p_demux->s )
385         return false;
386
387     /* Get 10 byte id3 header */
388     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
389         return false;
390
391     if( memcmp( p_peek, "ID3", 3 ) )
392         return false;
393
394     version = p_peek[3];
395     revision = p_peek[4];
396     b_footer = p_peek[5] & 0x10;
397     i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
398
399     if( b_footer ) i_size += 10;
400     i_size += 10;
401
402     /* Skip the entire tag */
403     stream_Read( p_demux->s, NULL, i_size );
404
405     msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
406              version, revision, i_size );
407     return true;
408 }
409 static bool SkipAPETag( demux_t *p_demux )
410 {
411     const uint8_t *p_peek;
412     int i_version;
413     int i_size;
414     uint32_t flags;
415
416     if( !p_demux->s )
417         return false;
418
419     /* Get 32 byte ape header */
420     if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
421         return false;
422
423     if( memcmp( p_peek, "APETAGEX", 8 ) )
424         return false;
425
426     i_version = GetDWLE( &p_peek[8] );
427     flags = GetDWLE( &p_peek[8+4+4] );
428     if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
429         return false;
430
431     i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
432
433     /* Skip the entire tag */
434     stream_Read( p_demux->s, NULL, i_size );
435
436     msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
437              i_version/1000, i_size );
438     return true;
439 }
440