1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 *****************************************************************************/
33 #include <vlc_codec.h>
36 #include <vlc_modules.h>
37 #include <vlc_strings.h>
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
,
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 */
109 { "ogx", "ogg" }, /*RFC5334*/
110 { "opus", "ogg" }, /*draft-terriberry-oggopus-01*/
112 { "rm", "avformat" },
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 */
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 /*****************************************************************************
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
157 void (*destroy
)(demux_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
);
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
185 if (vlc_stream_Tell(demux
->s
) != 0 && vlc_stream_Seek(demux
->s
, 0))
187 msg_Err(demux
, "seek failure before probing");
191 return probe(VLC_OBJECT(demux
));
194 /*****************************************************************************
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
))
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
);
215 psz_demux
= demux_NameFromMimeType( type
);
220 p_demux
->p_input
= p_parent_input
;
221 p_demux
->psz_access
= strdup( psz_access
);
222 p_demux
->psz_demux
= strdup( psz_demux
);
223 p_demux
->psz_location
= strdup( psz_location
);
224 p_demux
->psz_file
= get_path( psz_location
); /* parse URL */
226 if( unlikely(p_demux
->psz_access
== NULL
227 || p_demux
->psz_demux
== NULL
228 || p_demux
->psz_location
== NULL
) )
232 msg_Dbg( p_obj
, "creating demux: access='%s' demux='%s' "
233 "location='%s' file='%s'",
234 p_demux
->psz_access
, p_demux
->psz_demux
,
235 p_demux
->psz_location
, p_demux
->psz_file
);
239 p_demux
->b_preparsing
= b_preparsing
;
241 p_demux
->pf_demux
= NULL
;
242 p_demux
->pf_control
= NULL
;
243 p_demux
->p_sys
= NULL
;
244 p_demux
->info
.i_update
= 0;
245 p_demux
->info
.i_title
= 0;
246 p_demux
->info
.i_seekpoint
= 0;
247 priv
->destroy
= s
? demux_DestroyDemux
: demux_DestroyAccessDemux
;
251 const char *psz_module
= NULL
;
253 if( !strcmp( p_demux
->psz_demux
, "any" ) && p_demux
->psz_file
)
255 char const* psz_ext
= strrchr( p_demux
->psz_file
, '.' );
258 psz_module
= DemuxNameFromExtension( psz_ext
+ 1, b_preparsing
);
261 if( psz_module
== NULL
)
262 psz_module
= p_demux
->psz_demux
;
264 p_demux
->p_module
= vlc_module_load(p_demux
, "demux", psz_module
,
265 !strcmp(psz_module
, p_demux
->psz_demux
), demux_Probe
, p_demux
);
270 module_need( p_demux
, "access_demux", p_demux
->psz_access
, true );
273 if( p_demux
->p_module
== NULL
)
278 free( p_demux
->psz_file
);
279 free( p_demux
->psz_location
);
280 free( p_demux
->psz_demux
);
281 free( p_demux
->psz_access
);
282 vlc_object_release( p_demux
);
286 /*****************************************************************************
288 *****************************************************************************/
289 void demux_Delete( demux_t
*p_demux
)
291 demux_priv_t
*priv
= (demux_priv_t
*)p_demux
;
293 module_unneed( p_demux
, p_demux
->p_module
);
295 priv
->destroy(p_demux
);
296 free( p_demux
->psz_file
);
297 free( p_demux
->psz_location
);
298 free( p_demux
->psz_demux
);
299 free( p_demux
->psz_access
);
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
, ... )
311 va_start( ap
, query
);
312 ret
= demux
->pf_control( demux
, query
, ap
);
317 int demux_vaControl( demux_t
*demux
, int query
, va_list args
)
319 if( demux
->s
!= NULL
)
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
:
331 ret
= demux
->pf_control( demux
, query
, args
);
332 if( ret
!= VLC_SUCCESS
)
333 ret
= vlc_stream_vaControl( demux
->s
, query
, ap
);
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
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
:
355 if( demux_ControlInternal( demux
, DEMUX_CAN_PAUSE
,
357 return vlc_stream_vaControl( demux
->s
, query
, args
);
359 /* The caller shall not pause if pause is unsupported. */
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
)
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
);
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
) )
404 case DEMUX_CAN_PAUSE
:
405 case DEMUX_CAN_CONTROL_PACE
:
406 case DEMUX_GET_PTS_DELAY
:
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
;
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
;
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
);
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
) )
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
) )
468 case DEMUX_IS_PLAYLIST
:
469 *va_arg( args
, bool * ) = false;
473 case DEMUX_HAS_UNSUPPORTED_META
:
474 case DEMUX_SET_NEXT_DEMUX_TIME
:
475 case DEMUX_GET_TITLE_INFO
:
476 case DEMUX_SET_GROUP
:
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
:
487 case DEMUX_NAV_RIGHT
:
488 case DEMUX_NAV_POPUP
:
492 case DEMUX_SET_TITLE
:
493 case DEMUX_SET_SEEKPOINT
:
494 case DEMUX_SET_RECORD_STATE
:
497 msg_Err( s
, "unknown query 0x%x in %s", i_query
, __func__
);
503 /****************************************************************************
505 ****************************************************************************/
506 decoder_t
*demux_PacketizerNew( demux_t
*p_demux
, es_format_t
*p_fmt
, const char *psz_msg
)
508 decoder_t
*p_packetizer
;
509 p_packetizer
= vlc_custom_create( p_demux
, sizeof( *p_packetizer
),
510 "demux packetizer" );
513 es_format_Clean( p_fmt
);
516 p_fmt
->b_packetized
= false;
518 p_packetizer
->pf_decode
= NULL
;
519 p_packetizer
->pf_packetize
= NULL
;
521 p_packetizer
->fmt_in
= *p_fmt
;
522 es_format_Init( &p_packetizer
->fmt_out
, p_fmt
->i_cat
, 0 );
524 p_packetizer
->p_module
= module_need( p_packetizer
, "packetizer", NULL
, false );
525 if( !p_packetizer
->p_module
)
527 es_format_Clean( p_fmt
);
528 vlc_object_release( p_packetizer
);
529 msg_Err( p_demux
, "cannot find packetizer for %s", psz_msg
);
536 void demux_PacketizerDestroy( decoder_t
*p_packetizer
)
538 if( p_packetizer
->p_module
)
539 module_unneed( p_packetizer
, p_packetizer
->p_module
);
540 es_format_Clean( &p_packetizer
->fmt_in
);
541 es_format_Clean( &p_packetizer
->fmt_out
);
542 if( p_packetizer
->p_description
)
543 vlc_meta_Delete( p_packetizer
->p_description
);
544 vlc_object_release( p_packetizer
);
547 unsigned demux_TestAndClearFlags( demux_t
*p_demux
, unsigned flags
)
549 unsigned update
= flags
;
551 if ( demux_Control( p_demux
, DEMUX_TEST_AND_CLEAR_FLAGS
, &update
) == VLC_SUCCESS
)
554 update
= p_demux
->info
.i_update
& flags
;
555 p_demux
->info
.i_update
&= ~flags
;
559 int demux_GetTitle( demux_t
*p_demux
)
562 if ( demux_Control( p_demux
, DEMUX_GET_TITLE
, &i_title
) == VLC_SUCCESS
)
564 return p_demux
->info
.i_title
;
567 int demux_GetSeekpoint( demux_t
*p_demux
)
570 if ( demux_Control( p_demux
, DEMUX_GET_SEEKPOINT
, &i_seekpoint
) == VLC_SUCCESS
)
572 return p_demux
->info
.i_seekpoint
;
575 static demux_t
*demux_FilterNew( demux_t
*p_next
, const char *p_name
)
577 demux_priv_t
*priv
= vlc_custom_create(p_next
, sizeof (*priv
), "demux_filter");
578 if (unlikely(priv
== NULL
))
581 demux_t
*p_demux
= &priv
->demux
;
583 p_demux
->p_next
= p_next
;
584 p_demux
->p_input
= NULL
;
585 p_demux
->p_sys
= NULL
;
586 p_demux
->psz_access
= NULL
;
587 p_demux
->psz_demux
= NULL
;
588 p_demux
->psz_location
= NULL
;
589 p_demux
->psz_file
= NULL
;
591 priv
->destroy
= demux_DestroyDemuxFilter
;
593 module_need( p_demux
, "demux_filter", p_name
, p_name
!= NULL
);
595 if( p_demux
->p_module
== NULL
)
600 vlc_object_release( p_demux
);
604 demux_t
*demux_FilterChainNew( demux_t
*p_demux
, const char *psz_chain
)
606 if( !psz_chain
|| !*psz_chain
)
609 char *psz_parser
= strdup(psz_chain
);
614 vlc_array_init(&name
);
619 config_chain_t
*p_cfg
;
621 char *psz_rest_chain
= config_ChainCreate( &psz_name
, &p_cfg
, psz_parser
);
623 psz_parser
= psz_rest_chain
;
625 vlc_array_append(&name
, psz_name
);
626 config_ChainDestroy(p_cfg
);
629 size_t i
= vlc_array_count(&name
);
632 char *p_name
= vlc_array_item_at_index(&name
, i
);
633 demux_t
*p_next
= demux_FilterNew( p_demux
, p_name
);
643 vlc_array_clear(&name
);
647 demux_Delete(p_demux
);
650 free(vlc_array_item_at_index(&name
, i
));
651 vlc_array_clear(&name
);