]> git.sesse.net Git - vlc/blob - modules/access/imem.c
imem: typo
[vlc] / modules / access / imem.c
1 /*****************************************************************************
2  * imem.c : Memory input for VLC
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir _AT_ videolan _DOT org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <limits.h>
32 #include <math.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
38
39 /*****************************************************************************
40  * Module descriptior
41  *****************************************************************************/
42 static int  Open (vlc_object_t *);
43 static void Close(vlc_object_t *);
44
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.")
49
50 #define ID_TEXT N_("ID")
51 #define ID_LONGTEXT N_(\
52     "Set the ID of the elementary stream")
53
54 #define GROUP_TEXT N_("Group")
55 #define GROUP_LONGTEXT N_(\
56     "Set the group of the elementary stream")
57
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[] = {
62     0, 1, 2, 3
63 };
64 static const char *cat_texts[] = {
65     N_("Unknown"), N_("Audio"), N_("Video"), N_("Subtitle")
66 };
67
68 #define CODEC_TEXT N_("Codec")
69 #define CODEC_LONGTEXT N_(\
70     "Set the codec of the elementary stream")
71
72 #define LANGUAGE_TEXT N_("Language")
73 #define LANGUAGE_LONGTEXT N_(\
74     "Language of the elementary stream as described by ISO639")
75
76 #define SAMPLERATE_TEXT N_("Sample rate")
77 #define SAMPLERATE_LONGTEXT N_(\
78     "Sample rate of an audio elementary stream")
79
80 #define CHANNELS_TEXT N_("Channels count")
81 #define CHANNELS_LONGTEXT N_(\
82     "Channels count of an audio elementary stream")
83
84 #define WIDTH_TEXT N_("Width")
85 #define WIDTH_LONGTEXT N_("Width of video or subtitle elementary streams")
86
87 #define HEIGHT_TEXT N_("Height")
88 #define HEIGHT_LONGTEXT N_("Height of video or subtitle elementary streams")
89
90 #define DAR_TEXT N_("Display aspect ratio")
91 #define DAR_LONGTEXT N_(\
92     "Display aspect ratio of a video elementary stream")
93
94 #define FPS_TEXT N_("Frame rate")
95 #define FPS_LONGTEXT N_(\
96     "Frame rate of a video elementary stream")
97
98 #define COOKIE_TEXT N_("Callback cookie string")
99 #define COOKIE_LONGTEXT N_(\
100     "Text identifier for the callback functions")
101
102 #define DATA_TEXT N_("Callback data")
103 #define DATA_LONGTEXT N_(\
104     "Data for the get and release functions")
105
106 #define GET_TEXT N_("Get function")
107 #define GET_LONGTEXT N_(\
108     "Address of the get callback function")
109
110 #define RELEASE_TEXT N_("Release function")
111 #define RELEASE_LONGTEXT N_(\
112     "Address of the release callback function")
113
114 vlc_module_begin()
115     set_shortname(N_("Memory input"))
116     set_description(N_("Memory input"))
117     set_category(CAT_INPUT)
118     set_subcategory(SUBCAT_INPUT_ACCESS)
119
120     add_integer("imem-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true)
121         change_private()
122     add_string ("imem-get", "0", NULL, GET_TEXT, GET_LONGTEXT, true)
123         change_volatile()
124     add_string ("imem-release", "0", NULL, RELEASE_TEXT, RELEASE_LONGTEXT, true)
125         change_volatile()
126     add_string ("imem-cookie", NULL, NULL, COOKIE_TEXT, COOKIE_LONGTEXT, true)
127         change_volatile()
128     add_string ("imem-data", "0", NULL, DATA_TEXT, DATA_LONGTEXT, true)
129         change_volatile()
130
131     add_integer("imem-id", -1, NULL, ID_TEXT, ID_LONGTEXT, true)
132         change_private()
133     add_integer("imem-group", 0, NULL, GROUP_TEXT, GROUP_LONGTEXT, true)
134         change_private()
135     add_integer("imem-cat", 0, NULL, CAT_TEXT, CAT_LONGTEXT, true)
136         change_integer_list(cat_values, cat_texts, NULL)
137         change_private()
138     add_string ("imem-codec", NULL, NULL, CODEC_TEXT, CODEC_LONGTEXT, true)
139         change_private()
140     add_string( "imem-language", NULL, NULL, LANGUAGE_TEXT, LANGUAGE_LONGTEXT, false)
141         change_private()
142
143     add_integer("imem-samplerate", 0, NULL, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, true)
144         change_private()
145     add_integer("imem-channels", 0, NULL, CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
146         change_private()
147
148     add_integer("imem-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true)
149         change_private()
150     add_integer("imem-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
151         change_private()
152     add_string ("imem-dar", NULL, NULL, DAR_TEXT, DAR_LONGTEXT, true)
153         change_private()
154     add_string ("imem-fps", NULL, NULL, FPS_TEXT, FPS_LONGTEXT, true)
155         change_private()
156
157     add_shortcut("imem")
158     set_capability("access_demux", 0)
159     set_callbacks(Open, Close)
160 vlc_module_end()
161
162 /*****************************************************************************
163  * Exported API
164  *****************************************************************************/
165
166 /* The clock origin for the DTS and PTS is assumed to be 0.
167  * A negative value means unknown.
168  *
169  * TODO define flags
170  */
171 typedef int  (*imem_get_t)(void *data, const char *cookie,
172                            int64_t *dts, int64_t *pts, unsigned *flags,
173                            size_t *, void **);
174 typedef void (*imem_release_t)(void *data, const char *cookie, size_t, void *);
175
176 /*****************************************************************************
177  * Local prototypes
178  *****************************************************************************/
179
180 /* */
181 static int Demux(demux_t *);
182 static int Control(demux_t *, int, va_list);
183
184 /* */
185 struct demux_sys_t {
186     struct {
187         imem_get_t      get;
188         imem_release_t  release;
189         void           *data;
190         char           *cookie;
191     } source;
192
193     es_out_id_t  *es;
194
195     mtime_t      pts_delay;
196
197     mtime_t      dts;
198
199     mtime_t      deadline;
200 };
201
202 static void ParseMRL(demux_t *);
203 static int var_CreateGetRational(demux_t *,
204                                  unsigned *num, unsigned *den,
205                                  const char *var);
206
207 /**
208  * It opens an imem access_demux
209  */
210 static int Open(vlc_object_t *object)
211 {
212     demux_t     *demux = (demux_t*)object;
213     char *tmp;
214
215         /* */
216     demux_sys_t *sys = calloc(1, sizeof(*sys));
217         if (!sys)
218                 return VLC_ENOMEM;
219
220     /* Read the user functions */
221     tmp = var_CreateGetString(demux, "imem-get");
222     if (tmp)
223         sys->source.get = (imem_get_t)(intptr_t)strtoll(tmp, NULL, 0);
224     free(tmp);
225
226     tmp = var_CreateGetString(demux, "imem-release");
227     if (tmp)
228         sys->source.release = (imem_release_t)(intptr_t)strtoll(tmp, NULL, 0);
229     free(tmp);
230
231     if (!sys->source.get || !sys->source.release) {
232         msg_Err(demux, "Invalid get/release function pointers");
233         free(sys);
234         return VLC_EGENERIC;
235     }
236
237     tmp = var_CreateGetString(demux, "imem-data");
238     if (tmp)
239         sys->source.data = (void *)(uintptr_t)strtoull(tmp, NULL, 0);
240     free(tmp);
241
242     /* Now we can parse the MRL (get/release must not be parsed to avoid
243      * security risks) */
244     if (*demux->psz_path)
245         ParseMRL(demux);
246
247     /* Now we can parse the MRL (get/release must not be parsed to avoid
248      * security risks) */
249     if (*demux->psz_path)
250         ParseMRL(demux);
251
252     sys->source.cookie = var_InheritString(demux, "imem-cookie");
253
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)");
257
258         /* ES format */
259     es_format_t fmt;
260         es_format_Init(&fmt, UNKNOWN_ES, 0);
261
262     fmt.i_id = var_CreateGetInteger(demux, "imem-id");
263     fmt.i_group = var_CreateGetInteger(demux, "imem-group");
264
265     tmp = var_CreateGetString(demux, "imem-codec");
266     if (tmp)
267         fmt.i_codec = vlc_fourcc_GetCodecFromString(UNKNOWN_ES, tmp);
268     free(tmp);
269
270     switch (var_CreateGetInteger(demux, "imem-cat")) {
271     case 1: {
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");
275
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);
279         break;
280     }
281     case 2: {
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");
285         unsigned num, den;
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;
290             }
291         }
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;
295         }
296
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);
302         break;
303     }
304     case 3: {
305         fmt.i_cat = SPU_ES;
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");
310
311         msg_Dbg(demux, "Subtitle %4.4s",
312                 (const char *)&fmt.i_codec);
313         break;
314     }
315     default:
316         msg_Err(demux, "Invalid ES category");
317         es_format_Clean(&fmt);
318         free(sys);
319         return VLC_EGENERIC;
320     }
321
322     fmt.psz_language = var_CreateGetString(demux, "imem-language");
323
324     /* */
325         sys->es = es_out_Add(demux->out, &fmt);
326     es_format_Clean(&fmt);
327
328     if (!sys->es) {
329         free(sys->source.data);
330         free(sys);
331         return VLC_EGENERIC;
332     }
333
334     /* */
335     sys->pts_delay = var_CreateGetInteger(demux, "imem-caching") * INT64_C(1000);
336     sys->dts       = 0;
337     sys->deadline  = VLC_TS_INVALID;
338
339     /* Set up demux */
340     demux->pf_control = Control;
341     demux->pf_demux   = Demux;
342     demux->p_sys      = sys;
343
344     demux->info.i_update = 0;
345     demux->info.i_title = 0;
346     demux->info.i_seekpoint = 0;
347     return VLC_SUCCESS;
348 }
349
350 /**
351  * It closes an imem access_demux
352  */
353 static void Close(vlc_object_t *object)
354 {
355     demux_t     *demux = (demux_t *)object;
356     demux_sys_t *sys = demux->p_sys;
357
358     free(sys->source.cookie);
359     free(sys);
360 }
361
362 /**
363  * It controls imem
364  */
365 static int Control(demux_t *demux, int i_query, va_list args)
366 {
367     demux_sys_t *sys = demux->p_sys;
368
369     switch (i_query)
370     {
371     case DEMUX_CAN_PAUSE:
372     case DEMUX_CAN_CONTROL_PACE: {
373         bool *b = va_arg(args, bool *);
374         *b = true;
375         return VLC_SUCCESS;
376     }
377     case DEMUX_SET_PAUSE_STATE:
378         return VLC_SUCCESS;
379
380     case DEMUX_GET_PTS_DELAY: {
381         int64_t *delay = va_arg(args, int64_t *);
382         *delay = sys->pts_delay;
383         return VLC_SUCCESS;
384     }
385     case DEMUX_GET_POSITION: {
386         double *position = va_arg(args, double *);
387         *position = 0.0;
388         return VLC_SUCCESS;
389     }
390     case DEMUX_GET_TIME: {
391         int64_t *t = va_arg(args, int64_t *);
392         *t = sys->dts;
393         return VLC_SUCCESS;
394     }
395     case DEMUX_GET_LENGTH: {
396         int64_t *l = va_arg(args, int64_t *);
397         *l = 0;
398         return VLC_SUCCESS;
399     }
400     case DEMUX_SET_NEXT_DEMUX_TIME:
401         sys->deadline = va_arg(args, int64_t);
402         return VLC_SUCCESS;
403
404     /* */
405     case DEMUX_CAN_SEEK:
406     case DEMUX_SET_POSITION:
407     case DEMUX_SET_TIME:
408     default:
409         return VLC_EGENERIC;
410     }
411
412     return VLC_EGENERIC;
413 }
414
415 /**
416  * It retreives data using the get() callback, sends them to es_out
417  * and the release it using the release() callback.
418  */
419 static int Demux(demux_t *demux)
420 {
421     demux_sys_t *sys = demux->p_sys;
422
423     if (sys->deadline == VLC_TS_INVALID)
424         sys->deadline = sys->dts + 1;
425
426     for (;;) {
427         if (sys->deadline <= sys->dts)
428             break;
429
430         /* */
431         int64_t dts, pts;
432         unsigned flags;
433         size_t buffer_size;
434         void   *buffer;
435
436         if (sys->source.get(sys->source.data, sys->source.cookie,
437                             &dts, &pts, &flags, &buffer_size, &buffer))
438             return 0;
439
440         if (dts < 0)
441             dts = pts;
442
443         if (buffer_size > 0) {
444             block_t *block = block_New(demux, buffer_size);
445             if (block) {
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);
449
450                 es_out_Control(demux->out, ES_OUT_SET_PCR, block->i_dts);
451                 es_out_Send(demux->out, sys->es, block);
452             }
453         }
454
455         sys->dts = dts;
456
457         sys->source.release(sys->source.data, sys->source.cookie,
458                             buffer_size, buffer);
459     }
460     sys->deadline = VLC_TS_INVALID;
461     return 1;
462 }
463
464 /**
465  * It parses a rational number (it also accepts basic float number).
466  *
467  * It returns an error if the rational number cannot be parsed (0/0 is valid).
468  */
469 static int var_CreateGetRational(demux_t *demux,
470                                  unsigned *num, unsigned *den,
471                                  const char *var)
472 {
473     /* */
474     *num = 0;
475     *den = 0;
476
477     /* */
478     char *tmp = var_CreateGetString(demux, var);
479     if (!tmp)
480         goto error;
481
482     char *next;
483     unsigned n = strtol(tmp,  &next, 0);
484     unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
485
486     if (*next == '.') {
487         /* Interpret as a float number */
488         double r = us_atof(tmp);
489         double c = ceil(r);
490         if (c >= UINT_MAX)
491             goto error;
492         unsigned m = c;
493         if (m > 0) {
494             d = UINT_MAX / m;
495             n = r * d;
496         } else {
497             n = 0;
498             d = 0;
499         }
500     }
501
502     if (n > 0 && d > 0)
503         vlc_ureduce(num, den, n, d, 0);
504
505     free(tmp);
506     return VLC_SUCCESS;
507
508 error:
509     free(tmp);
510     return VLC_EGENERIC;
511 }
512
513 /**
514  * Parse the MRL and extract configuration from it.
515  *
516  * Syntax: option1=value1[:option2=value2[...]]
517  *
518  * XXX get and release are not supported on purpose.
519  */
520 static void ParseMRL(demux_t *demux)
521 {
522     static const struct {
523         const char *name;
524         int        type;
525     } options[] = {
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 },
539         { NULL, -1 }
540     };
541
542     char *dup = strdup(demux->psz_path);
543     if (!dup)
544         return;
545     char *current = dup;
546
547     while (current) {
548         char *next = strchr(current, ':');
549         if (next)
550             *next++ = '\0';
551
552         char *option = current;
553         char *value = strchr(current, '=');
554         if (value) {
555             *value++ = '\0';
556             msg_Dbg(demux, "option '%s' value '%s'", option, value);
557         } else {
558             msg_Dbg(demux, "option '%s' without value (unsupported)", option);
559         }
560
561         char *name;
562         if (asprintf(&name, "imem-%s", option) < 0)
563             name = NULL;
564         for (unsigned i = 0; name && options[i].name; i++) {
565             if (strcmp(options[i].name, option))
566                 continue;
567             /* */
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);
573             }
574             break;
575         }
576         free(name);
577
578         /* */
579         current = next;
580     }
581     free(dup);
582 }