forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemux.c
524 lines (442 loc) · 15.3 KB
/
demux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*****************************************************************************
* demux.c
*****************************************************************************
* Copyright (C) 1999-2004 VLC authors and VideoLAN
*
* Author: Laurent Aimar <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <limits.h>
#include "demux.h"
#include <libvlc.h>
#include <vlc_codec.h>
#include <vlc_meta.h>
#include <vlc_url.h>
#include <vlc_modules.h>
#include <vlc_strings.h>
#include "input_internal.h"
typedef const struct
{
char const key[20];
char const name[8];
} demux_mapping;
static int demux_mapping_cmp( const void *k, const void *v )
{
demux_mapping* entry = v;
return vlc_ascii_strcasecmp( k, entry->key );
}
static const char *demux_NameFromMimeType(const char *mime)
{
static demux_mapping types[] =
{ /* Must be sorted in ascending ASCII order */
{ "audio/aac", "m4a" },
{ "audio/aacp", "m4a" },
{ "audio/mpeg", "mp3" },
//{ "video/MP1S", "es,mpgv" }, !b_force
{ "video/dv", "rawdv" },
{ "video/MP2P", "ps" },
{ "video/MP2T", "ts" },
{ "video/nsa", "nsv" },
{ "video/nsv", "nsv" },
};
demux_mapping *type = bsearch(mime, types, ARRAY_SIZE(types),
sizeof (*types), demux_mapping_cmp);
return (type != NULL) ? type->name : "any";
}
demux_t *demux_New( vlc_object_t *p_obj, const char *psz_name,
stream_t *s, es_out_t *out )
{
assert(s != NULL );
return demux_NewAdvanced( p_obj, NULL, psz_name, "", s, out, false );
}
struct vlc_demux_private
{
module_t *module;
};
static void demux_DestroyDemux(demux_t *demux)
{
struct vlc_demux_private *priv = vlc_stream_Private(demux);
module_unneed(demux, priv->module);
free(demux->psz_filepath);
free(demux->psz_name);
assert(demux->s != NULL);
vlc_stream_Delete(demux->s);
}
static int demux_Probe(void *func, bool forced, va_list ap)
{
int (*probe)(vlc_object_t *) = func;
demux_t *demux = va_arg(ap, demux_t *);
/* Restore input stream offset (in case previous probed demux failed to
* to do so). */
if (vlc_stream_Tell(demux->s) != 0 && vlc_stream_Seek(demux->s, 0))
{
msg_Err(demux, "seek failure before probing");
return VLC_EGENERIC;
}
demux->obj.force = forced;
int ret = probe(VLC_OBJECT(demux));
if (ret)
vlc_objres_clear(VLC_OBJECT(demux));
return ret;
}
demux_t *demux_NewAdvanced( vlc_object_t *p_obj, input_thread_t *p_input,
const char *module, const char *url,
stream_t *s, es_out_t *out, bool b_preparsing )
{
struct vlc_demux_private *priv;
demux_t *p_demux = vlc_stream_CustomNew(p_obj, demux_DestroyDemux,
sizeof (*priv), "demux");
if (unlikely(p_demux == NULL))
return NULL;
assert(s != NULL);
priv = vlc_stream_Private(p_demux);
p_demux->p_input_item = p_input ? input_GetItem(p_input) : NULL;
p_demux->psz_name = strdup(module);
if (unlikely(p_demux->psz_name == NULL))
goto error;
p_demux->psz_url = strdup(url);
if (unlikely(p_demux->psz_url == NULL))
goto error;
const char *p = strstr(p_demux->psz_url, "://");
p_demux->psz_location = (p != NULL) ? (p + 3) : "";
p_demux->psz_filepath = get_path(p_demux->psz_location); /* parse URL */
if( !b_preparsing )
msg_Dbg( p_obj, "creating demux \"%s\", URL: %s, path: %s",
module, url, p_demux->psz_filepath );
p_demux->s = s;
p_demux->out = out;
p_demux->b_preparsing = b_preparsing;
p_demux->pf_readdir = NULL;
p_demux->pf_demux = NULL;
p_demux->pf_control = NULL;
p_demux->p_sys = NULL;
char *modbuf = NULL;
bool strict = true;
if (!strcasecmp(module, "any" ) || module[0] == '\0') {
/* Look up demux by content type for hard to detect formats */
char *type = stream_MimeType(s);
if (type != NULL) {
module = demux_NameFromMimeType(type);
free(type);
}
strict = false;
}
if (strcasecmp(module, "any") == 0 && p_demux->psz_filepath != NULL)
{
const char *ext = strrchr(p_demux->psz_filepath, '.');
if (ext != NULL) {
if (b_preparsing && !vlc_ascii_strcasecmp(ext, ".mp3"))
module = "mpga";
else
if (likely(asprintf(&modbuf, "ext-%s", ext + 1) >= 0))
module = modbuf;
}
strict = false;
}
priv->module = vlc_module_load(p_demux, "demux", module, strict,
demux_Probe, p_demux);
free(modbuf);
if (priv->module == NULL)
{
free( p_demux->psz_filepath );
goto error;
}
return p_demux;
error:
free( p_demux->psz_name );
stream_CommonDelete( p_demux );
return NULL;
}
int demux_Demux(demux_t *demux)
{
if (demux->pf_demux != NULL)
return demux->pf_demux(demux);
if (demux->pf_readdir != NULL && demux->p_input_item != NULL) {
input_item_node_t *node = input_item_node_Create(demux->p_input_item);
if (unlikely(node == NULL))
return VLC_DEMUXER_EGENERIC;
if (vlc_stream_ReadDir(demux, node)) {
input_item_node_Delete(node);
return VLC_DEMUXER_EGENERIC;
}
if (es_out_Control(demux->out, ES_OUT_POST_SUBNODE, node))
input_item_node_Delete(node);
return VLC_DEMUXER_EOF;
}
return VLC_DEMUXER_SUCCESS;
}
#define static_control_match(foo) \
static_assert((unsigned) DEMUX_##foo == STREAM_##foo, "Mismatch")
int demux_vaControl( demux_t *demux, int query, va_list args )
{
return demux->pf_control( demux, query, args );
}
/*****************************************************************************
* demux_vaControlHelper:
*****************************************************************************/
int demux_vaControlHelper( stream_t *s,
int64_t i_start, int64_t i_end,
int64_t i_bitrate, int i_align,
int i_query, va_list args )
{
int64_t i_tell;
double f, *pf;
vlc_tick_t i64;
if( i_end < 0 ) i_end = stream_Size( s );
if( i_start < 0 ) i_start = 0;
if( i_align <= 0 ) i_align = 1;
i_tell = vlc_stream_Tell( s );
static_control_match(CAN_PAUSE);
static_control_match(CAN_CONTROL_PACE);
static_control_match(GET_PTS_DELAY);
static_control_match(GET_META);
static_control_match(GET_SIGNAL);
static_control_match(SET_PAUSE_STATE);
switch( i_query )
{
case DEMUX_CAN_SEEK:
{
bool *b = va_arg( args, bool * );
if( (i_bitrate <= 0 && i_start >= i_end)
|| vlc_stream_Control( s, STREAM_CAN_SEEK, b ) )
*b = false;
break;
}
case DEMUX_CAN_PAUSE:
case DEMUX_CAN_CONTROL_PACE:
case DEMUX_GET_PTS_DELAY:
case DEMUX_GET_META:
case DEMUX_GET_SIGNAL:
case DEMUX_GET_TYPE:
case DEMUX_SET_PAUSE_STATE:
return vlc_stream_vaControl( s, i_query, args );
case DEMUX_GET_LENGTH:
if( i_bitrate > 0 && i_end > i_start )
{
*va_arg( args, vlc_tick_t * ) = vlc_tick_from_samples((i_end - i_start) * 8, i_bitrate);
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_GET_TIME:
if( i_bitrate > 0 && i_tell >= i_start )
{
*va_arg( args, vlc_tick_t * ) = vlc_tick_from_samples((i_tell - i_start) * 8, i_bitrate);
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_GET_POSITION:
pf = va_arg( args, double * );
if( i_start < i_end )
{
*pf = (double)( i_tell - i_start ) /
(double)( i_end - i_start );
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_GET_NORMAL_TIME:
return VLC_EGENERIC;
case DEMUX_SET_POSITION:
f = va_arg( args, double );
if( i_start < i_end && f >= 0.0 && f <= 1.0 )
{
int64_t i_block = (f * ( i_end - i_start )) / i_align;
if( vlc_stream_Seek( s, i_start + i_block * i_align ) )
{
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_SET_TIME:
i64 = va_arg( args, vlc_tick_t );
if( i_bitrate > 0 && i64 >= 0 )
{
int64_t i_block = samples_from_vlc_tick( i64, i_bitrate ) / (8 * i_align);
if( vlc_stream_Seek( s, i_start + i_block * i_align ) )
{
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_GET_FPS:
case DEMUX_HAS_UNSUPPORTED_META:
case DEMUX_SET_NEXT_DEMUX_TIME:
case DEMUX_GET_TITLE_INFO:
case DEMUX_SET_GROUP_DEFAULT:
case DEMUX_SET_GROUP_ALL:
case DEMUX_SET_GROUP_LIST:
case DEMUX_SET_ES:
case DEMUX_SET_ES_LIST:
case DEMUX_GET_ATTACHMENTS:
case DEMUX_CAN_RECORD:
case DEMUX_TEST_AND_CLEAR_FLAGS:
case DEMUX_GET_TITLE:
case DEMUX_GET_SEEKPOINT:
case DEMUX_NAV_ACTIVATE:
case DEMUX_NAV_UP:
case DEMUX_NAV_DOWN:
case DEMUX_NAV_LEFT:
case DEMUX_NAV_RIGHT:
case DEMUX_NAV_POPUP:
case DEMUX_NAV_MENU:
case DEMUX_FILTER_ENABLE:
case DEMUX_FILTER_DISABLE:
return VLC_EGENERIC;
case DEMUX_SET_TITLE:
case DEMUX_SET_SEEKPOINT:
case DEMUX_SET_RECORD_STATE:
assert(0);
default:
msg_Err( s, "unknown query 0x%x in %s", i_query, __func__ );
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
/****************************************************************************
* Utility functions
****************************************************************************/
decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
{
decoder_t *p_packetizer;
p_packetizer = vlc_custom_create( p_demux, sizeof( *p_packetizer ),
"demux packetizer" );
if( !p_packetizer )
{
es_format_Clean( p_fmt );
return NULL;
}
p_fmt->b_packetized = false;
p_packetizer->pf_decode = NULL;
p_packetizer->pf_packetize = NULL;
p_packetizer->fmt_in = *p_fmt;
es_format_Init( &p_packetizer->fmt_out, p_fmt->i_cat, 0 );
p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, false );
if( !p_packetizer->p_module )
{
es_format_Clean( p_fmt );
vlc_object_delete(p_packetizer);
msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
return NULL;
}
return p_packetizer;
}
void demux_PacketizerDestroy( decoder_t *p_packetizer )
{
if( p_packetizer->p_module )
module_unneed( p_packetizer, p_packetizer->p_module );
es_format_Clean( &p_packetizer->fmt_in );
es_format_Clean( &p_packetizer->fmt_out );
if( p_packetizer->p_description )
vlc_meta_Delete( p_packetizer->p_description );
vlc_object_delete(p_packetizer);
}
unsigned demux_TestAndClearFlags( demux_t *p_demux, unsigned flags )
{
unsigned update = flags;
if (demux_Control( p_demux, DEMUX_TEST_AND_CLEAR_FLAGS, &update))
return 0;
return update;
}
int demux_GetTitle( demux_t *p_demux )
{
int title;
if (demux_Control(p_demux, DEMUX_GET_TITLE, &title))
title = 0;
return title;
}
int demux_GetSeekpoint( demux_t *p_demux )
{
int seekpoint;
if (demux_Control(p_demux, DEMUX_GET_SEEKPOINT, &seekpoint))
seekpoint = 0;
return seekpoint;
}
static demux_t *demux_FilterNew( demux_t *p_next, const char *p_name )
{
struct vlc_demux_private *priv;
demux_t *p_demux = vlc_stream_CustomNew(VLC_OBJECT(p_next),
demux_DestroyDemux, sizeof (*priv),
"demux filter");
if (unlikely(p_demux == NULL))
return NULL;
priv = vlc_stream_Private(p_demux);
p_demux->s = p_next;
p_demux->p_input_item = NULL;
p_demux->p_sys = NULL;
p_demux->psz_name = NULL;
p_demux->psz_url = NULL;
p_demux->psz_location = NULL;
p_demux->psz_filepath = NULL;
p_demux->out = NULL;
priv->module = module_need(p_demux, "demux_filter", p_name,
p_name != NULL);
if (priv->module == NULL)
goto error;
return p_demux;
error:
stream_CommonDelete( p_demux );
return NULL;
}
demux_t *demux_FilterChainNew( demux_t *p_demux, const char *psz_chain )
{
if( !psz_chain || !*psz_chain )
return NULL;
char *psz_parser = strdup(psz_chain);
if(!psz_parser)
return NULL;
/* parse chain */
while(psz_parser)
{
config_chain_t *p_cfg;
char *psz_name;
char *psz_rest_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
free( psz_parser );
psz_parser = psz_rest_chain;
demux_t *filter = demux_FilterNew(p_demux, psz_name);
if (filter != NULL)
p_demux = filter;
free(psz_name);
config_ChainDestroy(p_cfg);
}
return p_demux;
}
static bool demux_filter_enable_disable(demux_t *p_demux,
const char *psz_demux, bool b_enable)
{
struct vlc_demux_private *priv = vlc_stream_Private(p_demux);
if ( psz_demux &&
(strcmp(module_get_name(priv->module, false), psz_demux) == 0
|| strcmp(module_get_name(priv->module, true), psz_demux) == 0) )
{
demux_Control( p_demux,
b_enable ? DEMUX_FILTER_ENABLE : DEMUX_FILTER_DISABLE );
return true;
}
return false;
}
bool demux_FilterEnable( demux_t *p_demux_chain, const char* psz_demux )
{
return demux_filter_enable_disable( p_demux_chain, psz_demux, true );
}
bool demux_FilterDisable( demux_t *p_demux_chain, const char* psz_demux )
{
return demux_filter_enable_disable( p_demux_chain, psz_demux, false );
}