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 demux_t
*demux_New( vlc_object_t
*p_obj
, const char *psz_name
,
142 stream_t
*s
, es_out_t
*out
)
145 return demux_NewAdvanced( p_obj
, NULL
, psz_name
, "", s
, out
, false );
148 struct vlc_demux_private
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
172 if (vlc_stream_Tell(demux
->s
) != 0 && vlc_stream_Seek(demux
->s
, 0))
174 msg_Err(demux
, "seek failure before probing");
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
))
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
);
200 psz_demux
= demux_NameFromMimeType( 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
))
210 p_demux
->psz_url
= strdup(url
);
211 if (unlikely(p_demux
->psz_url
== NULL
))
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 */
219 msg_Dbg( p_obj
, "creating demux \"%s\", URL: %s, path: %s",
220 psz_demux
, url
, p_demux
->psz_filepath
);
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
, '.' );
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
);
254 free( p_demux
->psz_name
);
255 stream_CommonDelete( p_demux
);
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
)
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
);
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
) )
303 case DEMUX_CAN_PAUSE
:
304 case DEMUX_CAN_CONTROL_PACE
:
305 case DEMUX_GET_PTS_DELAY
:
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
;
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
;
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
);
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
) )
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
) )
367 case DEMUX_IS_PLAYLIST
:
368 *va_arg( args
, bool * ) = false;
372 case DEMUX_HAS_UNSUPPORTED_META
:
373 case DEMUX_SET_NEXT_DEMUX_TIME
:
374 case DEMUX_GET_TITLE_INFO
:
375 case DEMUX_SET_GROUP
:
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
:
386 case DEMUX_NAV_RIGHT
:
387 case DEMUX_NAV_POPUP
:
389 case DEMUX_FILTER_ENABLE
:
390 case DEMUX_FILTER_DISABLE
:
393 case DEMUX_SET_TITLE
:
394 case DEMUX_SET_SEEKPOINT
:
395 case DEMUX_SET_RECORD_STATE
:
398 msg_Err( s
, "unknown query 0x%x in %s", i_query
, __func__
);
404 /****************************************************************************
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" );
414 es_format_Clean( p_fmt
);
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
);
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
))
457 int demux_GetTitle( demux_t
*p_demux
)
461 if (demux_Control(p_demux
, DEMUX_GET_TITLE
, &title
))
466 int demux_GetSeekpoint( demux_t
*p_demux
)
470 if (demux_Control(p_demux
, DEMUX_GET_SEEKPOINT
, &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
),
481 if (unlikely(p_demux
== 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
;
494 priv
->module
= module_need(p_demux
, "demux_filter", p_name
,
496 if (priv
->module
== NULL
)
501 stream_CommonDelete( p_demux
);
505 demux_t
*demux_FilterChainNew( demux_t
*p_demux
, const char *psz_chain
)
507 if( !psz_chain
|| !*psz_chain
)
510 char *psz_parser
= strdup(psz_chain
);
517 config_chain_t
*p_cfg
;
519 char *psz_rest_chain
= config_ChainCreate( &psz_name
, &p_cfg
, psz_parser
);
521 psz_parser
= psz_rest_chain
;
523 demux_t
*filter
= demux_FilterNew(p_demux
, psz_name
);
528 config_ChainDestroy(p_cfg
);
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
);
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 );