1 /*****************************************************************************
2 * imem.c : Memory input for VLC
3 *****************************************************************************
4 * Copyright (C) 2009 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 Open (vlc_object_t *);
43 static void Close(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)
128 add_string ("imem-data", "0", NULL, DATA_TEXT, DATA_LONGTEXT, true)
131 add_integer("imem-id", -1, NULL, ID_TEXT, ID_LONGTEXT, true)
133 add_integer("imem-group", 0, NULL, GROUP_TEXT, GROUP_LONGTEXT, true)
135 add_integer("imem-cat", 0, NULL, CAT_TEXT, CAT_LONGTEXT, true)
136 change_integer_list(cat_values, cat_texts, NULL)
138 add_string ("imeme-codec", NULL, NULL, CODEC_TEXT, CODEC_LONGTEXT, true)
140 add_string( "imem-language", NULL, NULL, LANGUAGE_TEXT, LANGUAGE_LONGTEXT, false)
143 add_integer("imem-samplerate", 0, NULL, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, true)
145 add_integer("imem-channels", 0, NULL, CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
148 add_integer("imem-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true)
150 add_integer("imem-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
152 add_string ("imem-dar", NULL, NULL, DAR_TEXT, DAR_LONGTEXT, true)
154 add_string ("imem-fps", NULL, NULL, FPS_TEXT, FPS_LONGTEXT, true)
158 set_capability("access_demux", 0)
159 set_callbacks(Open, Close)
162 /*****************************************************************************
164 *****************************************************************************/
166 /* The clock origin for the DTS and PTS is assumed to be 0.
167 * A negative value means unknown.
171 typedef int (*imem_get_t)(void *data, const char *cookie,
172 int64_t *dts, int64_t *pts, unsigned *flags,
174 typedef void (*imem_release_t)(void *data, const char *cookie, size_t, void *);
176 /*****************************************************************************
178 *****************************************************************************/
181 static int Demux(demux_t *);
182 static int Control(demux_t *, int, va_list);
188 imem_release_t release;
202 static void ParseMRL(demux_t *);
203 static int var_CreateGetRational(demux_t *,
204 unsigned *num, unsigned *den,
208 * It opens an imem access_demux
210 static int Open(vlc_object_t *object)
212 demux_t *demux = (demux_t*)object;
216 demux_sys_t *sys = calloc(1, sizeof(*sys));
220 /* Read the user functions */
221 tmp = var_CreateGetString(demux, "imem-get");
223 sys->source.get = (imem_get_t)(intptr_t)strtoll(tmp, NULL, 0);
226 tmp = var_CreateGetString(demux, "imem-release");
228 sys->source.release = (imem_release_t)(intptr_t)strtoll(tmp, NULL, 0);
231 if (!sys->source.get || !sys->source.release) {
232 msg_Err(demux, "Invalid get/release function pointers");
237 tmp = var_CreateGetString(demux, "imem-data");
239 sys->source.data = (void *)(uintptr_t)strtoull(tmp, NULL, 0);
242 /* Now we can parse the MRL (get/release must not be parsed to avoid
244 if (*demux->psz_path)
247 /* Now we can parse the MRL (get/release must not be parsed to avoid
249 if (*demux->psz_path)
252 sys->source.cookie = var_InheritString(demux, "imem-cookie");
254 msg_Dbg(demux, "Using get(%p), release(%p), data(%p), cookie(%s)",
255 sys->source.get, sys->source.release, sys->source.data,
256 sys->source.cookie ? sys->source.cookie : "(null)");
260 es_format_Init(&fmt, UNKNOWN_ES, 0);
262 fmt.i_id = var_CreateGetInteger(demux, "imem-id");
263 fmt.i_group = var_CreateGetInteger(demux, "imem-group");
265 tmp = var_CreateGetString(demux, "imem-codec");
267 fmt.i_codec = vlc_fourcc_GetCodecFromString(UNKNOWN_ES, tmp);
270 switch (var_CreateGetInteger(demux, "imem-cat")) {
272 fmt.i_cat = AUDIO_ES;
273 fmt.audio.i_channels = var_CreateGetInteger(demux, "imem-channels");
274 fmt.audio.i_rate = var_CreateGetInteger(demux, "imem-samplerate");
276 msg_Dbg(demux, "Audio %4.4s %d channels %d Hz",
277 (const char *)&fmt.i_codec,
278 fmt.audio.i_channels, fmt.audio.i_rate);
282 fmt.i_cat = VIDEO_ES;
283 fmt.video.i_width = var_CreateGetInteger(demux, "imem-width");
284 fmt.video.i_height = var_CreateGetInteger(demux, "imem-height");
286 if (!var_CreateGetRational(demux, &num, &den, "imem-dar") && num > 0 && den > 0) {
287 if (fmt.video.i_width > 0 && fmt.video.i_height > 0) {
288 fmt.video.i_sar_num = num * fmt.video.i_height;
289 fmt.video.i_sar_den = den * fmt.video.i_width;
292 if (!var_CreateGetRational(demux, &num, &den, "imem-fps") && num > 0 && den > 0) {
293 fmt.video.i_frame_rate = num;
294 fmt.video.i_frame_rate_base = den;
297 msg_Dbg(demux, "Video %4.4s %dx%d SAR %d:%d frame rate %u/%u",
298 (const char *)&fmt.i_codec,
299 fmt.video.i_width, fmt.video.i_height,
300 fmt.video.i_sar_num, fmt.video.i_sar_den,
301 fmt.video.i_frame_rate, fmt.video.i_frame_rate_base);
306 fmt.subs.spu.i_original_frame_width =
307 var_CreateGetInteger(demux, "imem-width");
308 fmt.subs.spu.i_original_frame_height =
309 var_CreateGetInteger(demux, "imem-height");
311 msg_Dbg(demux, "Subtitle %4.4s",
312 (const char *)&fmt.i_codec);
316 msg_Err(demux, "Invalid ES category");
317 es_format_Clean(&fmt);
322 fmt.psz_language = var_CreateGetString(demux, "imem-language");
325 sys->es = es_out_Add(demux->out, &fmt);
326 es_format_Clean(&fmt);
329 free(sys->source.data);
335 sys->pts_delay = var_CreateGetInteger(demux, "imem-caching") * INT64_C(1000);
337 sys->deadline = VLC_TS_INVALID;
340 demux->pf_control = Control;
341 demux->pf_demux = Demux;
344 demux->info.i_update = 0;
345 demux->info.i_title = 0;
346 demux->info.i_seekpoint = 0;
351 * It closes an imem access_demux
353 static void Close(vlc_object_t *object)
355 demux_t *demux = (demux_t *)object;
356 demux_sys_t *sys = demux->p_sys;
358 free(sys->source.cookie);
365 static int Control(demux_t *demux, int i_query, va_list args)
367 demux_sys_t *sys = demux->p_sys;
371 case DEMUX_CAN_PAUSE:
372 case DEMUX_CAN_CONTROL_PACE: {
373 bool *b = va_arg(args, bool *);
377 case DEMUX_SET_PAUSE_STATE:
380 case DEMUX_GET_PTS_DELAY: {
381 int64_t *delay = va_arg(args, int64_t *);
382 *delay = sys->pts_delay;
385 case DEMUX_GET_POSITION: {
386 double *position = va_arg(args, double *);
390 case DEMUX_GET_TIME: {
391 int64_t *t = va_arg(args, int64_t *);
395 case DEMUX_GET_LENGTH: {
396 int64_t *l = va_arg(args, int64_t *);
400 case DEMUX_SET_NEXT_DEMUX_TIME:
401 sys->deadline = va_arg(args, int64_t);
406 case DEMUX_SET_POSITION:
416 * It retreives data using the get() callback, sends them to es_out
417 * and the release it using the release() callback.
419 static int Demux(demux_t *demux)
421 demux_sys_t *sys = demux->p_sys;
423 if (sys->deadline == VLC_TS_INVALID)
424 sys->deadline = sys->dts + 1;
427 if (sys->deadline <= sys->dts)
436 if (sys->source.get(sys->source.data, sys->source.cookie,
437 &dts, &pts, &flags, &buffer_size, &buffer))
443 if (buffer_size > 0) {
444 block_t *block = block_New(demux, buffer_size);
446 block->i_dts = dts >= 0 ? (1 + dts) : VLC_TS_INVALID;
447 block->i_pts = pts >= 0 ? (1 + pts) : VLC_TS_INVALID;
448 memcpy(block->p_buffer, buffer, buffer_size);
450 es_out_Control(demux->out, ES_OUT_SET_PCR, block->i_dts);
451 es_out_Send(demux->out, sys->es, block);
457 sys->source.release(sys->source.data, sys->source.cookie,
458 buffer_size, buffer);
460 sys->deadline = VLC_TS_INVALID;
465 * It parses a rational number (it also accepts basic float number).
467 * It returns an error if the rational number cannot be parsed (0/0 is valid).
469 static int var_CreateGetRational(demux_t *demux,
470 unsigned *num, unsigned *den,
478 char *tmp = var_CreateGetString(demux, var);
483 unsigned n = strtol(tmp, &next, 0);
484 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
487 /* Interpret as a float number */
488 double r = us_atof(tmp);
503 vlc_ureduce(num, den, n, d, 0);
514 * Parse the MRL and extract configuration from it.
516 * Syntax: option1=value1[:option2=value2[...]]
518 * XXX get and release are not supported on purpose.
520 static void ParseMRL(demux_t *demux)
522 static const struct {
526 { "caching", VLC_VAR_INTEGER },
527 { "id", VLC_VAR_INTEGER },
528 { "group", VLC_VAR_INTEGER },
529 { "cat", VLC_VAR_INTEGER },
530 { "samplerate", VLC_VAR_INTEGER },
531 { "channels", VLC_VAR_INTEGER },
532 { "width", VLC_VAR_INTEGER },
533 { "height", VLC_VAR_INTEGER },
534 { "cookie", VLC_VAR_STRING },
535 { "codec", VLC_VAR_STRING },
536 { "language", VLC_VAR_STRING },
537 { "dar", VLC_VAR_STRING },
538 { "fps", VLC_VAR_STRING },
542 char *dup = strdup(demux->psz_path);
548 char *next = strchr(current, ':');
552 char *option = current;
553 char *value = strchr(current, '=');
556 msg_Dbg(demux, "option '%s' value '%s'", option, value);
558 msg_Dbg(demux, "option '%s' without value (unsupported)", option);
562 if (asprintf(&name, "imem-%s", option) < 0)
564 for (unsigned i = 0; name && options[i].name; i++) {
565 if (strcmp(options[i].name, option))
568 var_Create(demux, name, options[i].type | VLC_VAR_DOINHERIT);
569 if (options[i].type == VLC_VAR_INTEGER && value) {
570 var_SetInteger(demux, name, strtol(value, NULL, 0));
571 } else if (options[i].type == VLC_VAR_STRING && value) {
572 var_SetString(demux, name, value);