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