]> git.sesse.net Git - vlc/blob - src/input/demux.c
Add image types.
[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             { "m3u",  "playlist" },
102             { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
103             { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
104             { "mod",  "mod" }, { "it",  "mod" }, { "s3m",  "mod" }, { "xm",   "mod" },
105             { "nsv",  "nsv" },
106             { "ogg",  "ogg" }, { "ogm",  "ogg" }, /* legacy Ogg */
107             { "oga",  "ogg" }, { "spx",  "ogg" }, { "ogv", "ogg" },
108             { "ogx",  "ogg" }, /*RFC5334*/
109             { "pva",  "pva" },
110             { "rm",   "rm" },
111             { "m4v",  "m4v" },
112             { "h264", "h264" },
113             { "voc",  "voc" },
114             { "mid",  "smf" }, { "rmi",  "smf" },
115
116             { "jpeg", "image" },
117             { "jpg",  "image" },
118             { "ljpg", "image" },
119             { "png",  "image" },
120             { "pgm",  "image" },
121             { "pgmyuv", "image" },
122             { "pbm",  "image" },
123             { "pam",  "image" },
124             { "tga",  "image" },
125             { "bmp",  "image" },
126             { "pnm",  "image" },
127             { "xpm",  "image" },
128             { "xcf",  "image" },
129             { "pcx",  "image" },
130             { "gif",  "image" },
131             { "tif",  "image" },
132             { "tiff", "image" },
133             { "lbm",  "image" },
134
135             { "",  "" },
136         };
137         /* Here, we don't mind if it does not work, it must be quick */
138         static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
139         {
140             { "mp3", "mpga" },
141             { "ogg", "ogg" },
142             { "wma", "asf" },
143             { "", "" }
144         };
145
146         const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
147         int  i;
148
149         if( !b_quick )
150         {
151             for( i = 0; exttodemux[i].ext[0]; i++ )
152             {
153                 if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
154                 {
155                     psz_module = exttodemux[i].demux;
156                     break;
157                 }
158             }
159         }
160         else
161         {
162             for( i = 0; exttodemux_quick[i].ext[0]; i++ )
163             {
164                 if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
165                 {
166                     psz_module = exttodemux_quick[i].demux;
167                     break;
168                 }
169             }
170
171         }
172     }
173
174     /* Before module_need (for var_Create...) */
175     vlc_object_attach( p_demux, p_obj );
176
177     if( s )
178     {
179         /* ID3/APE tags will mess-up demuxer probing so we skip it here.
180          * ID3/APE parsers will called later on in the demuxer to access the
181          * skipped info. */
182         if( !SkipID3Tag( p_demux ) )
183             SkipAPETag( p_demux );
184
185         p_demux->p_module =
186             module_need( p_demux, "demux", psz_module,
187                          !strcmp( psz_module, p_demux->psz_demux ) ?
188                          true : false );
189     }
190     else
191     {
192         p_demux->p_module =
193             module_need( p_demux, "access_demux", psz_module,
194                          !strcmp( psz_module, p_demux->psz_access ) ?
195                          true : false );
196     }
197
198     if( p_demux->p_module == NULL )
199     {
200         vlc_object_detach( p_demux );
201         free( p_demux->psz_path );
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     vlc_object_detach( p_demux );
218
219     free( p_demux->psz_path );
220     free( p_demux->psz_demux );
221     free( p_demux->psz_access );
222
223     vlc_object_release( p_demux );
224 }
225
226 /*****************************************************************************
227  * demux_vaControlHelper:
228  *****************************************************************************/
229 int demux_vaControlHelper( stream_t *s,
230                             int64_t i_start, int64_t i_end,
231                             int64_t i_bitrate, int i_align,
232                             int i_query, va_list args )
233 {
234     int64_t i_tell;
235     double  f, *pf;
236     int64_t i64, *pi64;
237
238     if( i_end < 0 )    i_end   = stream_Size( s );
239     if( i_start < 0 )  i_start = 0;
240     if( i_align <= 0 ) i_align = 1;
241     i_tell = stream_Tell( s );
242
243     switch( i_query )
244     {
245         case DEMUX_GET_LENGTH:
246             pi64 = (int64_t*)va_arg( args, int64_t * );
247             if( i_bitrate > 0 && i_end > i_start )
248             {
249                 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
250                 return VLC_SUCCESS;
251             }
252             return VLC_EGENERIC;
253
254         case DEMUX_GET_TIME:
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_tell - i_start) / i_bitrate;
259                 return VLC_SUCCESS;
260             }
261             return VLC_EGENERIC;
262
263         case DEMUX_GET_POSITION:
264             pf = (double*)va_arg( args, double * );
265             if( i_start < i_end )
266             {
267                 *pf = (double)( i_tell - i_start ) /
268                       (double)( i_end  - i_start );
269                 return VLC_SUCCESS;
270             }
271             return VLC_EGENERIC;
272
273
274         case DEMUX_SET_POSITION:
275             f = (double)va_arg( args, double );
276             if( i_start < i_end && f >= 0.0 && f <= 1.0 )
277             {
278                 int64_t i_block = (f * ( i_end - i_start )) / i_align;
279
280                 if( stream_Seek( s, i_start + i_block * i_align ) )
281                 {
282                     return VLC_EGENERIC;
283                 }
284                 return VLC_SUCCESS;
285             }
286             return VLC_EGENERIC;
287
288         case DEMUX_SET_TIME:
289             i64 = (int64_t)va_arg( args, int64_t );
290             if( i_bitrate > 0 && i64 >= 0 )
291             {
292                 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
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_GET_FPS:
302         case DEMUX_GET_META:
303         case DEMUX_HAS_UNSUPPORTED_META:
304         case DEMUX_SET_NEXT_DEMUX_TIME:
305         case DEMUX_GET_TITLE_INFO:
306         case DEMUX_SET_GROUP:
307         case DEMUX_GET_ATTACHMENTS:
308         case DEMUX_CAN_RECORD:
309         case DEMUX_SET_RECORD_STATE:
310             return VLC_EGENERIC;
311
312         default:
313             msg_Err( s, "unknown query in demux_vaControlDefault" );
314             return VLC_EGENERIC;
315     }
316 }
317
318 /****************************************************************************
319  * Utility functions
320  ****************************************************************************/
321 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
322 {
323     decoder_t *p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );
324
325     if( !p_packetizer )
326     {
327         es_format_Clean( p_fmt );
328         return NULL;
329     }
330
331     p_packetizer->pf_decode_audio = NULL;
332     p_packetizer->pf_decode_video = NULL;
333     p_packetizer->pf_decode_sub = NULL;
334     p_packetizer->pf_packetize = NULL;
335
336     p_packetizer->fmt_in = *p_fmt;
337     es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
338
339     p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
340     if( !p_packetizer->p_module )
341     {
342         es_format_Clean( p_fmt );
343         vlc_object_release( p_packetizer );
344         msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
345         return NULL;
346     }
347
348     return p_packetizer;
349 }
350 void demux_PacketizerDestroy( decoder_t *p_packetizer )
351 {
352     if( p_packetizer->p_module )
353         module_unneed( p_packetizer, p_packetizer->p_module );
354     es_format_Clean( &p_packetizer->fmt_in );
355     if( p_packetizer->p_description )
356         vlc_meta_Delete( p_packetizer->p_description );
357     vlc_object_release( p_packetizer );
358 }
359
360 static bool SkipID3Tag( demux_t *p_demux )
361 {
362     const uint8_t *p_peek;
363     uint8_t version, revision;
364     int i_size;
365     int b_footer;
366
367     if( !p_demux->s )
368         return false;
369
370     /* Get 10 byte id3 header */
371     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
372         return false;
373
374     if( memcmp( p_peek, "ID3", 3 ) )
375         return false;
376
377     version = p_peek[3];
378     revision = p_peek[4];
379     b_footer = p_peek[5] & 0x10;
380     i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];
381
382     if( b_footer ) i_size += 10;
383     i_size += 10;
384
385     /* Skip the entire tag */
386     stream_Read( p_demux->s, NULL, i_size );
387
388     msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
389              version, revision, i_size );
390     return true;
391 }
392 static bool SkipAPETag( demux_t *p_demux )
393 {
394     const uint8_t *p_peek;
395     int i_version;
396     int i_size;
397     uint32_t flags;
398
399     if( !p_demux->s )
400         return false;
401
402     /* Get 32 byte ape header */
403     if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
404         return false;
405
406     if( memcmp( p_peek, "APETAGEX", 8 ) )
407         return false;
408
409     i_version = GetDWLE( &p_peek[8] );
410     flags = GetDWLE( &p_peek[8+4+4] );
411     if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
412         return false;
413
414     i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );
415
416     /* Skip the entire tag */
417     stream_Read( p_demux->s, NULL, i_size );
418
419     msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
420              i_version/1000, i_size );
421     return true;
422 }
423