dec: access vout/aout only if the category matches
[vlc.git] / src / input / demux.c
blob8d3d33c02eb8691746d3d737a956f689d77ae895
1 /*****************************************************************************
2 * demux.c
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 * $Id$
7 * Author: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <limits.h>
31 #include "demux.h"
32 #include <libvlc.h>
33 #include <vlc_codec.h>
34 #include <vlc_meta.h>
35 #include <vlc_url.h>
36 #include <vlc_modules.h>
37 #include <vlc_strings.h>
39 typedef const struct
41 char const key[20];
42 char const name[8];
44 } demux_mapping;
46 static int demux_mapping_cmp( const void *k, const void *v )
48 demux_mapping* entry = v;
49 return vlc_ascii_strcasecmp( k, entry->key );
52 static demux_mapping* demux_lookup( char const* key,
53 demux_mapping* data, size_t size )
55 return bsearch( key, data, size, sizeof( *data ), demux_mapping_cmp );
58 static const char *demux_NameFromMimeType(const char *mime)
60 static demux_mapping types[] =
61 { /* Must be sorted in ascending ASCII order */
62 { "audio/aac", "m4a" },
63 { "audio/aacp", "m4a" },
64 { "audio/mpeg", "mp3" },
65 //{ "video/MP1S", "es,mpgv" }, !b_force
66 { "video/dv", "rawdv" },
67 { "video/MP2P", "ps" },
68 { "video/MP2T", "ts" },
69 { "video/nsa", "nsv" },
70 { "video/nsv", "nsv" },
72 demux_mapping *type = demux_lookup( mime, types, ARRAY_SIZE( types ) );
73 return (type != NULL) ? type->name : "any";
76 static const char* DemuxNameFromExtension( char const* ext,
77 bool b_preparsing )
79 /* NOTE: Add only file without any problems here and with strong detection:
80 * - no .mp3, .a52, ...
81 * - wav can't be added 'cause of a52 and dts in them as raw audio
83 static demux_mapping strong[] =
84 { /* NOTE: must be sorted in asc order */
85 { "aiff", "aiff" },
86 { "asf", "asf" },
87 { "au", "au" },
88 { "avi", "avi" },
89 { "drc", "dirac" },
90 { "dv", "dv" },
91 { "flac", "flac" },
92 { "h264", "h264" },
93 { "kar", "smf" },
94 { "m3u", "m3u" },
95 { "m4a", "mp4" },
96 { "m4v", "m4v" },
97 { "mid", "smf" },
98 { "mka", "mkv" },
99 { "mks", "mkv" },
100 { "mkv", "mkv" },
101 { "moov", "mp4" },
102 { "mov", "mp4" },
103 { "mp4", "mp4" },
104 { "nsv", "nsv" },
105 { "oga", "ogg" },
106 { "ogg", "ogg" },
107 { "ogm", "ogg" },
108 { "ogv", "ogg" },
109 { "ogx", "ogg" }, /*RFC5334*/
110 { "opus", "ogg" }, /*draft-terriberry-oggopus-01*/
111 { "pva", "pva" },
112 { "rm", "avformat" },
113 { "rmi", "smf" },
114 { "spx", "ogg" },
115 { "voc", "voc" },
116 { "wma", "asf" },
117 { "wmv", "asf" },
120 /* Here, we don't mind if it does not work, it must be quick */
121 static demux_mapping quick[] =
122 { /* NOTE: shall be sorted in asc order */
123 { "mp3", "mpga" },
124 { "ogg", "ogg" },
125 { "wma", "asf" },
128 struct {
129 demux_mapping* data;
130 size_t size;
132 } lookup = {
133 .data = b_preparsing ? quick : strong,
134 .size = b_preparsing ? ARRAY_SIZE( quick ) : ARRAY_SIZE( strong )
137 demux_mapping* result = demux_lookup( ext, lookup.data, lookup.size );
138 return result ? result->name : NULL;
141 demux_t *demux_New( vlc_object_t *p_obj, const char *psz_name,
142 stream_t *s, es_out_t *out )
144 assert(s != NULL );
145 return demux_NewAdvanced( p_obj, NULL, psz_name, "", s, out, false );
148 struct vlc_demux_private
150 module_t *module;
153 static void demux_DestroyDemux(demux_t *demux)
155 struct vlc_demux_private *priv = vlc_stream_Private(demux);
157 module_unneed(demux, priv->module);
158 free(demux->psz_filepath);
159 free(demux->psz_name);
161 assert(demux->s != NULL);
162 vlc_stream_Delete(demux->s);
165 static int demux_Probe(void *func, va_list ap)
167 int (*probe)(vlc_object_t *) = func;
168 demux_t *demux = va_arg(ap, demux_t *);
170 /* Restore input stream offset (in case previous probed demux failed to
171 * to do so). */
172 if (vlc_stream_Tell(demux->s) != 0 && vlc_stream_Seek(demux->s, 0))
174 msg_Err(demux, "seek failure before probing");
175 return VLC_EGENERIC;
178 return probe(VLC_OBJECT(demux));
181 demux_t *demux_NewAdvanced( vlc_object_t *p_obj, input_thread_t *p_parent_input,
182 const char *psz_demux, const char *url,
183 stream_t *s, es_out_t *out, bool b_preparsing )
185 struct vlc_demux_private *priv;
186 demux_t *p_demux = vlc_stream_CustomNew(p_obj, demux_DestroyDemux,
187 sizeof (*priv), "demux");
189 if (unlikely(p_demux == NULL))
190 return NULL;
192 assert(s != NULL);
193 priv = vlc_stream_Private(p_demux);
195 if (!strcasecmp( psz_demux, "any" ) || !psz_demux[0])
196 { /* Look up demux by mime-type for hard to detect formats */
197 char *type = stream_MimeType( s );
198 if( type != NULL )
200 psz_demux = demux_NameFromMimeType( type );
201 free( type );
205 p_demux->p_input = p_parent_input;
206 p_demux->psz_name = strdup( psz_demux );
207 if (unlikely(p_demux->psz_name == NULL))
208 goto error;
210 p_demux->psz_url = strdup(url);
211 if (unlikely(p_demux->psz_url == NULL))
212 goto error;
214 const char *p = strstr(p_demux->psz_url, "://");
215 p_demux->psz_location = (p != NULL) ? (p + 3) : "";
216 p_demux->psz_filepath = get_path(p_demux->psz_location); /* parse URL */
218 if( !b_preparsing )
219 msg_Dbg( p_obj, "creating demux \"%s\", URL: %s, path: %s",
220 psz_demux, url, p_demux->psz_filepath );
222 p_demux->s = s;
223 p_demux->out = out;
224 p_demux->b_preparsing = b_preparsing;
226 p_demux->pf_demux = NULL;
227 p_demux->pf_control = NULL;
228 p_demux->p_sys = NULL;
230 const char *psz_module = NULL;
232 if( !strcmp( p_demux->psz_name, "any" ) && p_demux->psz_filepath )
234 char const* psz_ext = strrchr( p_demux->psz_filepath, '.' );
236 if( psz_ext )
237 psz_module = DemuxNameFromExtension( psz_ext + 1, b_preparsing );
240 if( psz_module == NULL )
241 psz_module = p_demux->psz_name;
243 priv->module = vlc_module_load(p_demux, "demux", psz_module,
244 !strcmp(psz_module, p_demux->psz_name), demux_Probe, p_demux);
246 if (priv->module == NULL)
248 free( p_demux->psz_filepath );
249 goto error;
252 return p_demux;
253 error:
254 free( p_demux->psz_name );
255 stream_CommonDelete( p_demux );
256 return NULL;
259 #define static_control_match(foo) \
260 static_assert((unsigned) DEMUX_##foo == STREAM_##foo, "Mismatch")
262 int demux_vaControl( demux_t *demux, int query, va_list args )
264 return demux->pf_control( demux, query, args );
267 /*****************************************************************************
268 * demux_vaControlHelper:
269 *****************************************************************************/
270 int demux_vaControlHelper( stream_t *s,
271 int64_t i_start, int64_t i_end,
272 int64_t i_bitrate, int i_align,
273 int i_query, va_list args )
275 int64_t i_tell;
276 double f, *pf;
277 int64_t i64, *pi64;
279 if( i_end < 0 ) i_end = stream_Size( s );
280 if( i_start < 0 ) i_start = 0;
281 if( i_align <= 0 ) i_align = 1;
282 i_tell = vlc_stream_Tell( s );
284 static_control_match(CAN_PAUSE);
285 static_control_match(CAN_CONTROL_PACE);
286 static_control_match(GET_PTS_DELAY);
287 static_control_match(GET_META);
288 static_control_match(GET_SIGNAL);
289 static_control_match(SET_PAUSE_STATE);
291 switch( i_query )
293 case DEMUX_CAN_SEEK:
295 bool *b = va_arg( args, bool * );
297 if( (i_bitrate <= 0 && i_start >= i_end)
298 || vlc_stream_Control( s, STREAM_CAN_SEEK, b ) )
299 *b = false;
300 break;
303 case DEMUX_CAN_PAUSE:
304 case DEMUX_CAN_CONTROL_PACE:
305 case DEMUX_GET_PTS_DELAY:
306 case DEMUX_GET_META:
307 case DEMUX_GET_SIGNAL:
308 case DEMUX_SET_PAUSE_STATE:
309 return vlc_stream_vaControl( s, i_query, args );
311 case DEMUX_GET_LENGTH:
312 pi64 = (int64_t*)va_arg( args, int64_t * );
313 if( i_bitrate > 0 && i_end > i_start )
315 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
316 return VLC_SUCCESS;
318 return VLC_EGENERIC;
320 case DEMUX_GET_TIME:
321 pi64 = (int64_t*)va_arg( args, int64_t * );
322 if( i_bitrate > 0 && i_tell >= i_start )
324 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
325 return VLC_SUCCESS;
327 return VLC_EGENERIC;
329 case DEMUX_GET_POSITION:
330 pf = (double*)va_arg( args, double * );
331 if( i_start < i_end )
333 *pf = (double)( i_tell - i_start ) /
334 (double)( i_end - i_start );
335 return VLC_SUCCESS;
337 return VLC_EGENERIC;
340 case DEMUX_SET_POSITION:
341 f = (double)va_arg( args, double );
342 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
344 int64_t i_block = (f * ( i_end - i_start )) / i_align;
346 if( vlc_stream_Seek( s, i_start + i_block * i_align ) )
348 return VLC_EGENERIC;
350 return VLC_SUCCESS;
352 return VLC_EGENERIC;
354 case DEMUX_SET_TIME:
355 i64 = (int64_t)va_arg( args, int64_t );
356 if( i_bitrate > 0 && i64 >= 0 )
358 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
359 if( vlc_stream_Seek( s, i_start + i_block * i_align ) )
361 return VLC_EGENERIC;
363 return VLC_SUCCESS;
365 return VLC_EGENERIC;
367 case DEMUX_IS_PLAYLIST:
368 *va_arg( args, bool * ) = false;
369 return VLC_SUCCESS;
371 case DEMUX_GET_FPS:
372 case DEMUX_HAS_UNSUPPORTED_META:
373 case DEMUX_SET_NEXT_DEMUX_TIME:
374 case DEMUX_GET_TITLE_INFO:
375 case DEMUX_SET_GROUP:
376 case DEMUX_SET_ES:
377 case DEMUX_GET_ATTACHMENTS:
378 case DEMUX_CAN_RECORD:
379 case DEMUX_TEST_AND_CLEAR_FLAGS:
380 case DEMUX_GET_TITLE:
381 case DEMUX_GET_SEEKPOINT:
382 case DEMUX_NAV_ACTIVATE:
383 case DEMUX_NAV_UP:
384 case DEMUX_NAV_DOWN:
385 case DEMUX_NAV_LEFT:
386 case DEMUX_NAV_RIGHT:
387 case DEMUX_NAV_POPUP:
388 case DEMUX_NAV_MENU:
389 case DEMUX_FILTER_ENABLE:
390 case DEMUX_FILTER_DISABLE:
391 return VLC_EGENERIC;
393 case DEMUX_SET_TITLE:
394 case DEMUX_SET_SEEKPOINT:
395 case DEMUX_SET_RECORD_STATE:
396 assert(0);
397 default:
398 msg_Err( s, "unknown query 0x%x in %s", i_query, __func__ );
399 return VLC_EGENERIC;
401 return VLC_SUCCESS;
404 /****************************************************************************
405 * Utility functions
406 ****************************************************************************/
407 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
409 decoder_t *p_packetizer;
410 p_packetizer = vlc_custom_create( p_demux, sizeof( *p_packetizer ),
411 "demux packetizer" );
412 if( !p_packetizer )
414 es_format_Clean( p_fmt );
415 return NULL;
417 p_fmt->b_packetized = false;
419 p_packetizer->pf_decode = NULL;
420 p_packetizer->pf_packetize = NULL;
422 p_packetizer->fmt_in = *p_fmt;
423 es_format_Init( &p_packetizer->fmt_out, p_fmt->i_cat, 0 );
425 p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
426 if( !p_packetizer->p_module )
428 es_format_Clean( p_fmt );
429 vlc_object_release( p_packetizer );
430 msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
431 return NULL;
434 return p_packetizer;
437 void demux_PacketizerDestroy( decoder_t *p_packetizer )
439 if( p_packetizer->p_module )
440 module_unneed( p_packetizer, p_packetizer->p_module );
441 es_format_Clean( &p_packetizer->fmt_in );
442 es_format_Clean( &p_packetizer->fmt_out );
443 if( p_packetizer->p_description )
444 vlc_meta_Delete( p_packetizer->p_description );
445 vlc_object_release( p_packetizer );
448 unsigned demux_TestAndClearFlags( demux_t *p_demux, unsigned flags )
450 unsigned update = flags;
452 if (demux_Control( p_demux, DEMUX_TEST_AND_CLEAR_FLAGS, &update))
453 return 0;
454 return update;
457 int demux_GetTitle( demux_t *p_demux )
459 int title;
461 if (demux_Control(p_demux, DEMUX_GET_TITLE, &title))
462 title = 0;
463 return title;
466 int demux_GetSeekpoint( demux_t *p_demux )
468 int seekpoint;
470 if (demux_Control(p_demux, DEMUX_GET_SEEKPOINT, &seekpoint))
471 seekpoint = 0;
472 return seekpoint;
475 static demux_t *demux_FilterNew( demux_t *p_next, const char *p_name )
477 struct vlc_demux_private *priv;
478 demux_t *p_demux = vlc_stream_CustomNew(VLC_OBJECT(p_next),
479 demux_DestroyDemux, sizeof (*priv),
480 "demux filter");
481 if (unlikely(p_demux == NULL))
482 return NULL;
484 priv = vlc_stream_Private(p_demux);
485 p_demux->p_next = p_next;
486 p_demux->p_input = NULL;
487 p_demux->p_sys = NULL;
488 p_demux->psz_name = NULL;
489 p_demux->psz_url = NULL;
490 p_demux->psz_location = NULL;
491 p_demux->psz_filepath = NULL;
492 p_demux->out = NULL;
494 priv->module = module_need(p_demux, "demux_filter", p_name,
495 p_name != NULL);
496 if (priv->module == NULL)
497 goto error;
499 return p_demux;
500 error:
501 stream_CommonDelete( p_demux );
502 return NULL;
505 demux_t *demux_FilterChainNew( demux_t *p_demux, const char *psz_chain )
507 if( !psz_chain || !*psz_chain )
508 return NULL;
510 char *psz_parser = strdup(psz_chain);
511 if(!psz_parser)
512 return NULL;
514 /* parse chain */
515 while(psz_parser)
517 config_chain_t *p_cfg;
518 char *psz_name;
519 char *psz_rest_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
520 free( psz_parser );
521 psz_parser = psz_rest_chain;
523 demux_t *filter = demux_FilterNew(p_demux, psz_name);
524 if (filter != NULL)
525 p_demux = filter;
527 free(psz_name);
528 config_ChainDestroy(p_cfg);
531 return p_demux;
534 static bool demux_filter_enable_disable(demux_t *p_demux,
535 const char *psz_demux, bool b_enable)
537 struct vlc_demux_private *priv = vlc_stream_Private(p_demux);
539 if (strcmp(module_get_name(priv->module, false), psz_demux) == 0
540 || strcmp(module_get_name(priv->module, true), psz_demux) == 0)
542 demux_Control( p_demux,
543 b_enable ? DEMUX_FILTER_ENABLE : DEMUX_FILTER_DISABLE );
544 return true;
546 return false;
549 bool demux_FilterEnable( demux_t *p_demux_chain, const char* psz_demux )
551 return demux_filter_enable_disable( p_demux_chain, psz_demux, true );
554 bool demux_FilterDisable( demux_t *p_demux_chain, const char* psz_demux )
556 return demux_filter_enable_disable( p_demux_chain, psz_demux, false );