1 /*****************************************************************************
2 * imem.c : Memory input for VLC
3 *****************************************************************************
4 * Copyright (C) 2009-2010 Laurent Aimar
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int OpenDemux (vlc_object_t *);
43 static void CloseDemux(vlc_object_t *);
45 #define CACHING_TEXT N_("Caching value in ms")
46 #define CACHING_LONGTEXT N_(\
47 "Caching value for imem streams. This " \
48 "value should be set in milliseconds.")
50 #define ID_TEXT N_("ID")
51 #define ID_LONGTEXT N_(\
52 "Set the ID of the elementary stream")
54 #define GROUP_TEXT N_("Group")
55 #define GROUP_LONGTEXT N_(\
56 "Set the group of the elementary stream")
58 #define CAT_TEXT N_("Category")
59 #define CAT_LONGTEXT N_(\
60 "Set the category of the elementary stream")
61 static const int cat_values[] = {
64 static const char *cat_texts[] = {
65 N_("Unknown"), N_("Audio"), N_("Video"), N_("Subtitle")
68 #define CODEC_TEXT N_("Codec")
69 #define CODEC_LONGTEXT N_(\
70 "Set the codec of the elementary stream")
72 #define LANGUAGE_TEXT N_("Language")
73 #define LANGUAGE_LONGTEXT N_(\
74 "Language of the elementary stream as described by ISO639")
76 #define SAMPLERATE_TEXT N_("Sample rate")
77 #define SAMPLERATE_LONGTEXT N_(\
78 "Sample rate of an audio elementary stream")
80 #define CHANNELS_TEXT N_("Channels count")
81 #define CHANNELS_LONGTEXT N_(\
82 "Channels count of an audio elementary stream")
84 #define WIDTH_TEXT N_("Width")
85 #define WIDTH_LONGTEXT N_("Width of video or subtitle elementary streams")
87 #define HEIGHT_TEXT N_("Height")
88 #define HEIGHT_LONGTEXT N_("Height of video or subtitle elementary streams")
90 #define DAR_TEXT N_("Display aspect ratio")
91 #define DAR_LONGTEXT N_(\
92 "Display aspect ratio of a video elementary stream")
94 #define FPS_TEXT N_("Frame rate")
95 #define FPS_LONGTEXT N_(\
96 "Frame rate of a video elementary stream")
98 #define COOKIE_TEXT N_("Callback cookie string")
99 #define COOKIE_LONGTEXT N_(\
100 "Text identifier for the callback functions")
102 #define DATA_TEXT N_("Callback data")
103 #define DATA_LONGTEXT N_(\
104 "Data for the get and release functions")
106 #define GET_TEXT N_("Get function")
107 #define GET_LONGTEXT N_(\
108 "Address of the get callback function")
110 #define RELEASE_TEXT N_("Release function")
111 #define RELEASE_LONGTEXT N_(\
112 "Address of the release callback function")
115 set_shortname(N_("Memory input"))
116 set_description(N_("Memory input"))
117 set_category(CAT_INPUT)
118 set_subcategory(SUBCAT_INPUT_ACCESS)
120 add_integer("imem-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true)
122 add_string ("imem-get", "0", NULL, GET_TEXT, GET_LONGTEXT, true)
124 add_string ("imem-release", "0", NULL, RELEASE_TEXT, RELEASE_LONGTEXT, true)
126 add_string ("imem-cookie", NULL, NULL, COOKIE_TEXT, COOKIE_LONGTEXT, true)
129 add_string ("imem-data", "0", NULL, DATA_TEXT, DATA_LONGTEXT, true)
132 add_integer("imem-id", -1, NULL, ID_TEXT, ID_LONGTEXT, true)
135 add_integer("imem-group", 0, NULL, GROUP_TEXT, GROUP_LONGTEXT, true)
138 add_integer("imem-cat", 0, NULL, CAT_TEXT, CAT_LONGTEXT, true)
139 change_integer_list(cat_values, cat_texts, NULL)
142 add_string ("imem-codec", NULL, NULL, CODEC_TEXT, CODEC_LONGTEXT, true)
145 add_string( "imem-language", NULL, NULL, LANGUAGE_TEXT, LANGUAGE_LONGTEXT, false)
149 add_integer("imem-samplerate", 0, NULL, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, true)
152 add_integer("imem-channels", 0, NULL, CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
156 add_integer("imem-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true)
159 add_integer("imem-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
162 add_string ("imem-dar", NULL, NULL, DAR_TEXT, DAR_LONGTEXT, true)
165 add_string ("imem-fps", NULL, NULL, FPS_TEXT, FPS_LONGTEXT, true)
170 set_capability("access_demux", 0)
171 set_callbacks(OpenDemux, CloseDemux)
174 /*****************************************************************************
176 *****************************************************************************/
178 /* The clock origin for the DTS and PTS is assumed to be 0.
179 * A negative value means unknown.
183 typedef int (*imem_get_t)(void *data, const char *cookie,
184 int64_t *dts, int64_t *pts, unsigned *flags,
186 typedef void (*imem_release_t)(void *data, const char *cookie, size_t, void *);
188 /*****************************************************************************
190 *****************************************************************************/
193 static int Demux(demux_t *);
194 static int ControlDemux(demux_t *, int, va_list);
200 imem_release_t release;
214 static void ParseMRL(vlc_object_t *, const char *);
215 #define var_CreateGetRational(a,b,c,d) var_CreateGetRational(VLC_OBJECT(a),b,c,d)
216 static int (var_CreateGetRational)(vlc_object_t *,
217 unsigned *num, unsigned *den,
221 * It closes the common part of the access and access_demux
223 static void CloseCommon(imem_sys_t *sys)
225 free(sys->source.cookie);
230 * It initializes the common part for imem access/access_demux.
232 static int OpenCommon(vlc_object_t *object, imem_sys_t **sys_ptr, const char *psz_path)
237 imem_sys_t *sys = calloc(1, sizeof(*sys));
241 /* Read the user functions */
242 tmp = var_CreateGetString(object, "imem-get");
244 sys->source.get = (imem_get_t)(intptr_t)strtoll(tmp, NULL, 0);
247 tmp = var_CreateGetString(object, "imem-release");
249 sys->source.release = (imem_release_t)(intptr_t)strtoll(tmp, NULL, 0);
252 if (!sys->source.get || !sys->source.release) {
253 msg_Err(object, "Invalid get/release function pointers");
258 tmp = var_CreateGetString(object, "imem-data");
260 sys->source.data = (void *)(uintptr_t)strtoull(tmp, NULL, 0);
263 /* Now we can parse the MRL (get/release must not be parsed to avoid
266 ParseMRL(object, psz_path);
268 sys->source.cookie = var_InheritString(object, "imem-cookie");
270 msg_Dbg(object, "Using get(%p), release(%p), data(%p), cookie(%s)",
271 sys->source.get, sys->source.release, sys->source.data,
272 sys->source.cookie ? sys->source.cookie : "(null)");
275 sys->pts_delay = var_CreateGetInteger(object, "imem-caching") * INT64_C(1000);
277 sys->deadline = VLC_TS_INVALID;
284 * It opens an imem access_demux.
286 static int OpenDemux(vlc_object_t *object)
288 demux_t *demux = (demux_t *)object;
291 if (OpenCommon(VLC_OBJECT(demux), &sys, demux->psz_path))
296 es_format_Init(&fmt, UNKNOWN_ES, 0);
298 fmt.i_id = var_CreateGetInteger(object, "imem-id");
299 fmt.i_group = var_CreateGetInteger(object, "imem-group");
301 char *tmp = var_CreateGetString(object, "imem-codec");
303 fmt.i_codec = vlc_fourcc_GetCodecFromString(UNKNOWN_ES, tmp);
306 switch (var_CreateGetInteger(object, "imem-cat")) {
308 fmt.i_cat = AUDIO_ES;
309 fmt.audio.i_channels = var_CreateGetInteger(object, "imem-channels");
310 fmt.audio.i_rate = var_CreateGetInteger(object, "imem-samplerate");
312 msg_Dbg(object, "Audio %4.4s %d channels %d Hz",
313 (const char *)&fmt.i_codec,
314 fmt.audio.i_channels, fmt.audio.i_rate);
318 fmt.i_cat = VIDEO_ES;
319 fmt.video.i_width = var_CreateGetInteger(object, "imem-width");
320 fmt.video.i_height = var_CreateGetInteger(object, "imem-height");
322 if (!var_CreateGetRational(object, &num, &den, "imem-dar") && num > 0 && den > 0) {
323 if (fmt.video.i_width > 0 && fmt.video.i_height > 0) {
324 fmt.video.i_sar_num = num * fmt.video.i_height;
325 fmt.video.i_sar_den = den * fmt.video.i_width;
328 if (!var_CreateGetRational(object, &num, &den, "imem-fps") && num > 0 && den > 0) {
329 fmt.video.i_frame_rate = num;
330 fmt.video.i_frame_rate_base = den;
333 msg_Dbg(object, "Video %4.4s %dx%d SAR %d:%d frame rate %u/%u",
334 (const char *)&fmt.i_codec,
335 fmt.video.i_width, fmt.video.i_height,
336 fmt.video.i_sar_num, fmt.video.i_sar_den,
337 fmt.video.i_frame_rate, fmt.video.i_frame_rate_base);
342 fmt.subs.spu.i_original_frame_width =
343 var_CreateGetInteger(object, "imem-width");
344 fmt.subs.spu.i_original_frame_height =
345 var_CreateGetInteger(object, "imem-height");
347 msg_Dbg(object, "Subtitle %4.4s",
348 (const char *)&fmt.i_codec);
352 msg_Err(object, "Invalid ES category");
353 es_format_Clean(&fmt);
358 fmt.psz_language = var_CreateGetString(object, "imem-language");
360 sys->es = es_out_Add(demux->out, &fmt);
361 es_format_Clean(&fmt);
369 demux->pf_control = ControlDemux;
370 demux->pf_demux = Demux;
371 demux->p_sys = (demux_sys_t*)sys;
373 demux->info.i_update = 0;
374 demux->info.i_title = 0;
375 demux->info.i_seekpoint = 0;
380 * It closes an imem access_demux
382 static void CloseDemux(vlc_object_t *object)
384 demux_t *demux = (demux_t *)object;
386 CloseCommon((imem_sys_t*)demux->p_sys);
392 static int ControlDemux(demux_t *demux, int i_query, va_list args)
394 imem_sys_t *sys = (imem_sys_t*)demux->p_sys;
398 case DEMUX_CAN_PAUSE:
399 case DEMUX_CAN_CONTROL_PACE: {
400 bool *b = va_arg(args, bool *);
404 case DEMUX_SET_PAUSE_STATE:
407 case DEMUX_GET_PTS_DELAY: {
408 int64_t *delay = va_arg(args, int64_t *);
409 *delay = sys->pts_delay;
412 case DEMUX_GET_POSITION: {
413 double *position = va_arg(args, double *);
417 case DEMUX_GET_TIME: {
418 int64_t *t = va_arg(args, int64_t *);
422 case DEMUX_GET_LENGTH: {
423 int64_t *l = va_arg(args, int64_t *);
427 case DEMUX_SET_NEXT_DEMUX_TIME:
428 sys->deadline = va_arg(args, int64_t);
433 case DEMUX_SET_POSITION:
443 * It retreives data using the get() callback, sends them to es_out
444 * and the release it using the release() callback.
446 static int Demux(demux_t *demux)
448 imem_sys_t *sys = (imem_sys_t*)demux->p_sys;
450 if (sys->deadline == VLC_TS_INVALID)
451 sys->deadline = sys->dts + 1;
454 if (sys->deadline <= sys->dts)
463 if (sys->source.get(sys->source.data, sys->source.cookie,
464 &dts, &pts, &flags, &buffer_size, &buffer))
470 if (buffer_size > 0) {
471 block_t *block = block_New(demux, buffer_size);
473 block->i_dts = dts >= 0 ? (1 + dts) : VLC_TS_INVALID;
474 block->i_pts = pts >= 0 ? (1 + pts) : VLC_TS_INVALID;
475 memcpy(block->p_buffer, buffer, buffer_size);
477 es_out_Control(demux->out, ES_OUT_SET_PCR, block->i_dts);
478 es_out_Send(demux->out, sys->es, block);
484 sys->source.release(sys->source.data, sys->source.cookie,
485 buffer_size, buffer);
487 sys->deadline = VLC_TS_INVALID;
492 * It parses a rational number (it also accepts basic float number).
494 * It returns an error if the rational number cannot be parsed (0/0 is valid).
496 static int (var_CreateGetRational)(vlc_object_t *object,
497 unsigned *num, unsigned *den,
505 char *tmp = var_CreateGetString(object, var);
510 unsigned n = strtol(tmp, &next, 0);
511 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
514 /* Interpret as a float number */
515 double r = us_atof(tmp);
530 vlc_ureduce(num, den, n, d, 0);
541 * Parse the MRL and extract configuration from it.
543 * Syntax: option1=value1[:option2=value2[...]]
545 * XXX get and release are not supported on purpose.
547 static void ParseMRL(vlc_object_t *object, const char *psz_path)
549 static const struct {
553 { "caching", VLC_VAR_INTEGER },
554 { "id", VLC_VAR_INTEGER },
555 { "group", VLC_VAR_INTEGER },
556 { "cat", VLC_VAR_INTEGER },
557 { "samplerate", VLC_VAR_INTEGER },
558 { "channels", VLC_VAR_INTEGER },
559 { "width", VLC_VAR_INTEGER },
560 { "height", VLC_VAR_INTEGER },
561 { "cookie", VLC_VAR_STRING },
562 { "codec", VLC_VAR_STRING },
563 { "language", VLC_VAR_STRING },
564 { "dar", VLC_VAR_STRING },
565 { "fps", VLC_VAR_STRING },
569 char *dup = strdup(psz_path);
575 char *next = strchr(current, ':');
579 char *option = current;
580 char *value = strchr(current, '=');
583 msg_Dbg(object, "option '%s' value '%s'", option, value);
585 msg_Dbg(object, "option '%s' without value (unsupported)", option);
589 if (asprintf(&name, "imem-%s", option) < 0)
591 for (unsigned i = 0; name && options[i].name; i++) {
592 if (strcmp(options[i].name, option))
595 var_Create(object, name, options[i].type | VLC_VAR_DOINHERIT);
596 if (options[i].type == VLC_VAR_INTEGER && value) {
597 var_SetInteger(object, name, strtol(value, NULL, 0));
598 } else if (options[i].type == VLC_VAR_STRING && value) {
599 var_SetString(object, name, value);