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