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