demux: avi: PTSToByte remove useless casts and change type
[vlc.git] / src / input / demux.c
blob18b64b5f31f4d309669a8d0eea87f4e97ada03b2
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 /*****************************************************************************
142 * demux_New:
143 * if s is NULL then load a access_demux
144 *****************************************************************************/
145 demux_t *demux_New( vlc_object_t *p_obj, const char *psz_name,
146 const char *psz_location, stream_t *s, es_out_t *out )
148 return demux_NewAdvanced( p_obj, NULL,
149 (s == NULL) ? psz_name : "",
150 (s != NULL) ? psz_name : "",
151 psz_location, s, out, false );
154 typedef struct demux_priv_t
156 demux_t demux;
157 void (*destroy)(demux_t *);
158 } demux_priv_t;
160 static void demux_DestroyDemux(demux_t *demux)
162 assert(demux->s != NULL);
163 vlc_stream_Delete(demux->s);
166 static void demux_DestroyAccessDemux(demux_t *demux)
168 assert(demux->s == NULL);
169 (void) demux;
172 static void demux_DestroyDemuxFilter(demux_t *demux)
174 assert(demux->p_next != NULL);
175 demux_Delete(demux->p_next);
178 static int demux_Probe(void *func, va_list ap)
180 int (*probe)(vlc_object_t *) = func;
181 demux_t *demux = va_arg(ap, demux_t *);
183 /* Restore input stream offset (in case previous probed demux failed to
184 * to do so). */
185 if (vlc_stream_Tell(demux->s) != 0 && vlc_stream_Seek(demux->s, 0))
187 msg_Err(demux, "seek failure before probing");
188 return VLC_EGENERIC;
191 return probe(VLC_OBJECT(demux));
194 /*****************************************************************************
195 * demux_NewAdvanced:
196 * if s is NULL then load a access_demux
197 *****************************************************************************/
198 #undef demux_NewAdvanced
199 demux_t *demux_NewAdvanced( vlc_object_t *p_obj, input_thread_t *p_parent_input,
200 const char *psz_access, const char *psz_demux,
201 const char *psz_location,
202 stream_t *s, es_out_t *out, bool b_preparsing )
204 demux_priv_t *priv = vlc_custom_create(p_obj, sizeof (*priv), "demux");
205 if (unlikely(priv == NULL))
206 return NULL;
208 demux_t *p_demux = &priv->demux;
210 if( s != NULL && (!strcasecmp( psz_demux, "any" ) || !psz_demux[0]) )
211 { /* Look up demux by mime-type for hard to detect formats */
212 char *type = stream_MimeType( s );
213 if( type != NULL )
215 psz_demux = demux_NameFromMimeType( type );
216 free( type );
220 size_t schemelen = strlen(psz_access);
222 p_demux->p_input = p_parent_input;
223 p_demux->psz_name = strdup( psz_demux );
224 if (unlikely(p_demux->psz_name == NULL))
225 goto error;
227 if (unlikely(asprintf(&p_demux->psz_url, "%s://%s", psz_access,
228 psz_location) == -1))
229 goto error;
231 p_demux->psz_location = p_demux->psz_url + schemelen + 3;
232 p_demux->psz_filepath = get_path( psz_location ); /* parse URL */
234 if( !b_preparsing )
235 msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' "
236 "location='%s' file='%s'", psz_access, psz_demux,
237 p_demux->psz_location, p_demux->psz_filepath );
239 p_demux->s = s;
240 p_demux->out = out;
241 p_demux->b_preparsing = b_preparsing;
243 p_demux->pf_demux = NULL;
244 p_demux->pf_control = NULL;
245 p_demux->p_sys = NULL;
246 p_demux->info.i_update = 0;
247 priv->destroy = s ? demux_DestroyDemux : demux_DestroyAccessDemux;
249 if( s != NULL )
251 const char *psz_module = NULL;
253 if( !strcmp( p_demux->psz_name, "any" ) && p_demux->psz_filepath )
255 char const* psz_ext = strrchr( p_demux->psz_filepath, '.' );
257 if( psz_ext )
258 psz_module = DemuxNameFromExtension( psz_ext + 1, b_preparsing );
261 if( psz_module == NULL )
262 psz_module = p_demux->psz_name;
264 p_demux->p_module = vlc_module_load(p_demux, "demux", psz_module,
265 !strcmp(psz_module, p_demux->psz_name), demux_Probe, p_demux);
267 else
269 p_demux->p_module =
270 module_need( p_demux, "access_demux", psz_access, true );
273 if( p_demux->p_module == NULL )
275 free( p_demux->psz_filepath );
276 free( p_demux->psz_url );
277 goto error;
280 return p_demux;
281 error:
282 free( p_demux->psz_name );
283 vlc_object_release( p_demux );
284 return NULL;
287 /*****************************************************************************
288 * demux_Delete:
289 *****************************************************************************/
290 void demux_Delete( demux_t *p_demux )
292 demux_priv_t *priv = (demux_priv_t *)p_demux;
294 module_unneed( p_demux, p_demux->p_module );
296 priv->destroy(p_demux);
297 free( p_demux->psz_filepath );
298 free( p_demux->psz_url );
299 free( p_demux->psz_name );
300 vlc_object_release( p_demux );
303 #define static_control_match(foo) \
304 static_assert((unsigned) DEMUX_##foo == STREAM_##foo, "Mismatch")
306 static int demux_ControlInternal( demux_t *demux, int query, ... )
308 int ret;
309 va_list ap;
311 va_start( ap, query );
312 ret = demux->pf_control( demux, query, ap );
313 va_end( ap );
314 return ret;
317 int demux_vaControl( demux_t *demux, int query, va_list args )
319 if( demux->s != NULL )
320 switch( query )
322 /* Legacy fallback for missing getters in synchronous demuxers */
323 case DEMUX_CAN_PAUSE:
324 case DEMUX_CAN_CONTROL_PACE:
325 case DEMUX_GET_PTS_DELAY:
327 int ret;
328 va_list ap;
330 va_copy( ap, args );
331 ret = demux->pf_control( demux, query, args );
332 if( ret != VLC_SUCCESS )
333 ret = vlc_stream_vaControl( demux->s, query, ap );
334 va_end( ap );
335 return ret;
338 /* Some demuxers need to control pause directly (e.g. adaptive),
339 * but many legacy demuxers do not understand pause at all.
340 * If DEMUX_CAN_PAUSE is not implemented, bypass the demuxer and
341 * byte stream. If DEMUX_CAN_PAUSE is implemented and pause is
342 * supported, pause the demuxer normally. Else, something went very
343 * wrong.
345 * Note that this requires asynchronous/threaded demuxers to
346 * always return VLC_SUCCESS for DEMUX_CAN_PAUSE, so that they are
347 * never bypassed. Otherwise, we would reenter demux->s callbacks
348 * and break thread safety. At the time of writing, asynchronous or
349 * threaded *non-access* demuxers do not exist and are not fully
350 * supported by the input thread, so this is theoretical. */
351 case DEMUX_SET_PAUSE_STATE:
353 bool can_pause;
355 if( demux_ControlInternal( demux, DEMUX_CAN_PAUSE,
356 &can_pause ) )
357 return vlc_stream_vaControl( demux->s, query, args );
359 /* The caller shall not pause if pause is unsupported. */
360 assert( can_pause );
361 break;
365 return demux->pf_control( demux, query, args );
368 /*****************************************************************************
369 * demux_vaControlHelper:
370 *****************************************************************************/
371 int demux_vaControlHelper( stream_t *s,
372 int64_t i_start, int64_t i_end,
373 int64_t i_bitrate, int i_align,
374 int i_query, va_list args )
376 int64_t i_tell;
377 double f, *pf;
378 int64_t i64, *pi64;
380 if( i_end < 0 ) i_end = stream_Size( s );
381 if( i_start < 0 ) i_start = 0;
382 if( i_align <= 0 ) i_align = 1;
383 i_tell = vlc_stream_Tell( s );
385 static_control_match(CAN_PAUSE);
386 static_control_match(CAN_CONTROL_PACE);
387 static_control_match(GET_PTS_DELAY);
388 static_control_match(GET_META);
389 static_control_match(GET_SIGNAL);
390 static_control_match(SET_PAUSE_STATE);
392 switch( i_query )
394 case DEMUX_CAN_SEEK:
396 bool *b = va_arg( args, bool * );
398 if( (i_bitrate <= 0 && i_start >= i_end)
399 || vlc_stream_Control( s, STREAM_CAN_SEEK, b ) )
400 *b = false;
401 break;
404 case DEMUX_CAN_PAUSE:
405 case DEMUX_CAN_CONTROL_PACE:
406 case DEMUX_GET_PTS_DELAY:
407 case DEMUX_GET_META:
408 case DEMUX_GET_SIGNAL:
409 case DEMUX_SET_PAUSE_STATE:
410 return vlc_stream_vaControl( s, i_query, args );
412 case DEMUX_GET_LENGTH:
413 pi64 = (int64_t*)va_arg( args, int64_t * );
414 if( i_bitrate > 0 && i_end > i_start )
416 *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
417 return VLC_SUCCESS;
419 return VLC_EGENERIC;
421 case DEMUX_GET_TIME:
422 pi64 = (int64_t*)va_arg( args, int64_t * );
423 if( i_bitrate > 0 && i_tell >= i_start )
425 *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
426 return VLC_SUCCESS;
428 return VLC_EGENERIC;
430 case DEMUX_GET_POSITION:
431 pf = (double*)va_arg( args, double * );
432 if( i_start < i_end )
434 *pf = (double)( i_tell - i_start ) /
435 (double)( i_end - i_start );
436 return VLC_SUCCESS;
438 return VLC_EGENERIC;
441 case DEMUX_SET_POSITION:
442 f = (double)va_arg( args, double );
443 if( i_start < i_end && f >= 0.0 && f <= 1.0 )
445 int64_t i_block = (f * ( i_end - i_start )) / i_align;
447 if( vlc_stream_Seek( s, i_start + i_block * i_align ) )
449 return VLC_EGENERIC;
451 return VLC_SUCCESS;
453 return VLC_EGENERIC;
455 case DEMUX_SET_TIME:
456 i64 = (int64_t)va_arg( args, int64_t );
457 if( i_bitrate > 0 && i64 >= 0 )
459 int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
460 if( vlc_stream_Seek( s, i_start + i_block * i_align ) )
462 return VLC_EGENERIC;
464 return VLC_SUCCESS;
466 return VLC_EGENERIC;
468 case DEMUX_IS_PLAYLIST:
469 *va_arg( args, bool * ) = false;
470 return VLC_SUCCESS;
472 case DEMUX_GET_FPS:
473 case DEMUX_HAS_UNSUPPORTED_META:
474 case DEMUX_SET_NEXT_DEMUX_TIME:
475 case DEMUX_GET_TITLE_INFO:
476 case DEMUX_SET_GROUP:
477 case DEMUX_SET_ES:
478 case DEMUX_GET_ATTACHMENTS:
479 case DEMUX_CAN_RECORD:
480 case DEMUX_TEST_AND_CLEAR_FLAGS:
481 case DEMUX_GET_TITLE:
482 case DEMUX_GET_SEEKPOINT:
483 case DEMUX_NAV_ACTIVATE:
484 case DEMUX_NAV_UP:
485 case DEMUX_NAV_DOWN:
486 case DEMUX_NAV_LEFT:
487 case DEMUX_NAV_RIGHT:
488 case DEMUX_NAV_POPUP:
489 case DEMUX_NAV_MENU:
490 case DEMUX_FILTER_ENABLE:
491 case DEMUX_FILTER_DISABLE:
492 return VLC_EGENERIC;
494 case DEMUX_SET_TITLE:
495 case DEMUX_SET_SEEKPOINT:
496 case DEMUX_SET_RECORD_STATE:
497 assert(0);
498 default:
499 msg_Err( s, "unknown query 0x%x in %s", i_query, __func__ );
500 return VLC_EGENERIC;
502 return VLC_SUCCESS;
505 /****************************************************************************
506 * Utility functions
507 ****************************************************************************/
508 decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
510 decoder_t *p_packetizer;
511 p_packetizer = vlc_custom_create( p_demux, sizeof( *p_packetizer ),
512 "demux packetizer" );
513 if( !p_packetizer )
515 es_format_Clean( p_fmt );
516 return NULL;
518 p_fmt->b_packetized = false;
520 p_packetizer->pf_decode = NULL;
521 p_packetizer->pf_packetize = NULL;
523 p_packetizer->fmt_in = *p_fmt;
524 es_format_Init( &p_packetizer->fmt_out, p_fmt->i_cat, 0 );
526 p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
527 if( !p_packetizer->p_module )
529 es_format_Clean( p_fmt );
530 vlc_object_release( p_packetizer );
531 msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
532 return NULL;
535 return p_packetizer;
538 void demux_PacketizerDestroy( decoder_t *p_packetizer )
540 if( p_packetizer->p_module )
541 module_unneed( p_packetizer, p_packetizer->p_module );
542 es_format_Clean( &p_packetizer->fmt_in );
543 es_format_Clean( &p_packetizer->fmt_out );
544 if( p_packetizer->p_description )
545 vlc_meta_Delete( p_packetizer->p_description );
546 vlc_object_release( p_packetizer );
549 unsigned demux_TestAndClearFlags( demux_t *p_demux, unsigned flags )
551 unsigned update = flags;
553 if ( demux_Control( p_demux, DEMUX_TEST_AND_CLEAR_FLAGS, &update ) == VLC_SUCCESS )
554 return update;
556 update = p_demux->info.i_update & flags;
557 p_demux->info.i_update &= ~flags;
558 return update;
561 int demux_GetTitle( demux_t *p_demux )
563 int title;
565 if (demux_Control(p_demux, DEMUX_GET_TITLE, &title))
566 title = 0;
567 return title;
570 int demux_GetSeekpoint( demux_t *p_demux )
572 int seekpoint;
574 if (demux_Control(p_demux, DEMUX_GET_SEEKPOINT, &seekpoint))
575 seekpoint = 0;
576 return seekpoint;
579 static demux_t *demux_FilterNew( demux_t *p_next, const char *p_name )
581 demux_priv_t *priv = vlc_custom_create(p_next, sizeof (*priv), "demux_filter");
582 if (unlikely(priv == NULL))
583 return NULL;
585 demux_t *p_demux = &priv->demux;
587 p_demux->p_next = p_next;
588 p_demux->p_input = NULL;
589 p_demux->p_sys = NULL;
590 p_demux->psz_name = NULL;
591 p_demux->psz_url = NULL;
592 p_demux->psz_location = NULL;
593 p_demux->psz_filepath = NULL;
594 p_demux->out = NULL;
595 priv->destroy = demux_DestroyDemuxFilter;
596 p_demux->p_module =
597 module_need( p_demux, "demux_filter", p_name, p_name != NULL );
599 if( p_demux->p_module == NULL )
600 goto error;
602 return p_demux;
603 error:
604 vlc_object_release( p_demux );
605 return NULL;
608 demux_t *demux_FilterChainNew( demux_t *p_demux, const char *psz_chain )
610 if( !psz_chain || !*psz_chain )
611 return NULL;
613 char *psz_parser = strdup(psz_chain);
614 if(!psz_parser)
615 return NULL;
617 /* parse chain */
618 while(psz_parser)
620 config_chain_t *p_cfg;
621 char *psz_name;
622 char *psz_rest_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
623 free( psz_parser );
624 psz_parser = psz_rest_chain;
626 demux_t *filter = demux_FilterNew(p_demux, psz_name);
627 if (filter != NULL)
628 p_demux = filter;
630 free(psz_name);
631 config_ChainDestroy(p_cfg);
634 return p_demux;
637 static bool demux_filter_enable_disable( demux_t *p_demux_chain,
638 const char* psz_demux, bool b_enable )
640 demux_t *p_demux = p_demux_chain;
642 if( strcmp( module_get_name( p_demux->p_module, false ), psz_demux) == 0 ||
643 strcmp( module_get_name( p_demux->p_module, true ), psz_demux ) == 0 )
645 demux_Control( p_demux,
646 b_enable ? DEMUX_FILTER_ENABLE : DEMUX_FILTER_DISABLE );
647 return true;
649 return false;
652 bool demux_FilterEnable( demux_t *p_demux_chain, const char* psz_demux )
654 return demux_filter_enable_disable( p_demux_chain, psz_demux, true );
657 bool demux_FilterDisable( demux_t *p_demux_chain, const char* psz_demux )
659 return demux_filter_enable_disable( p_demux_chain, psz_demux, false );