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