]> git.sesse.net Git - vlc/blob - modules/access/imem.c
l10n: Khmer update
[vlc] / modules / access / imem.c
1 /*****************************************************************************
2  * imem.c : Memory input for VLC
3  *****************************************************************************
4  * Copyright (C) 2009-2010 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_access.h>
37 #include <vlc_demux.h>
38 #include <vlc_charset.h>
39
40 /*****************************************************************************
41  * Module descriptior
42  *****************************************************************************/
43 static int  OpenAccess (vlc_object_t *);
44 static void CloseAccess(vlc_object_t *);
45
46 static int  OpenDemux (vlc_object_t *);
47 static void CloseDemux(vlc_object_t *);
48
49 #define ID_TEXT N_("ID")
50 #define ID_LONGTEXT N_(\
51     "Set the ID of the elementary stream")
52
53 #define GROUP_TEXT N_("Group")
54 #define GROUP_LONGTEXT N_(\
55     "Set the group of the elementary stream")
56
57 #define CAT_TEXT N_("Category")
58 #define CAT_LONGTEXT N_(\
59     "Set the category of the elementary stream")
60 static const int cat_values[] = {
61     0, 1, 2, 3, 4,
62 };
63 static const char *cat_texts[] = {
64     N_("Unknown"), N_("Audio"), N_("Video"), N_("Subtitle"), N_("Data")
65 };
66
67 #define CODEC_TEXT N_("Codec")
68 #define CODEC_LONGTEXT N_(\
69     "Set the codec of the elementary stream")
70
71 #define LANGUAGE_TEXT N_("Language")
72 #define LANGUAGE_LONGTEXT N_(\
73     "Language of the elementary stream as described by ISO639")
74
75 #define SAMPLERATE_TEXT N_("Sample rate")
76 #define SAMPLERATE_LONGTEXT N_(\
77     "Sample rate of an audio elementary stream")
78
79 #define CHANNELS_TEXT N_("Channels count")
80 #define CHANNELS_LONGTEXT N_(\
81     "Channels count of an audio elementary stream")
82
83 #define WIDTH_TEXT N_("Width")
84 #define WIDTH_LONGTEXT N_("Width of video or subtitle elementary streams")
85
86 #define HEIGHT_TEXT N_("Height")
87 #define HEIGHT_LONGTEXT N_("Height of video or subtitle elementary streams")
88
89 #define DAR_TEXT N_("Display aspect ratio")
90 #define DAR_LONGTEXT N_(\
91     "Display aspect ratio of a video elementary stream")
92
93 #define FPS_TEXT N_("Frame rate")
94 #define FPS_LONGTEXT N_(\
95     "Frame rate of a video elementary stream")
96
97 #define COOKIE_TEXT N_("Callback cookie string")
98 #define COOKIE_LONGTEXT N_(\
99     "Text identifier for the callback functions")
100
101 #define DATA_TEXT N_("Callback data")
102 #define DATA_LONGTEXT N_(\
103     "Data for the get and release functions")
104
105 #define GET_TEXT N_("Get function")
106 #define GET_LONGTEXT N_(\
107     "Address of the get callback function")
108
109 #define RELEASE_TEXT N_("Release function")
110 #define RELEASE_LONGTEXT N_(\
111     "Address of the release callback function")
112
113 #define SIZE_TEXT N_("Size")
114 #define SIZE_LONGTEXT N_(\
115     "Size of stream in bytes")
116
117 vlc_module_begin()
118     set_shortname(N_("Memory input"))
119     set_description(N_("Memory input"))
120     set_category(CAT_INPUT)
121     set_subcategory(SUBCAT_INPUT_ACCESS)
122
123     add_string ("imem-get", "0", GET_TEXT, GET_LONGTEXT, true)
124         change_volatile()
125     add_string ("imem-release", "0", RELEASE_TEXT, RELEASE_LONGTEXT, true)
126         change_volatile()
127     add_string ("imem-cookie", NULL, COOKIE_TEXT, COOKIE_LONGTEXT, true)
128         change_volatile()
129         change_safe()
130     add_string ("imem-data", "0", DATA_TEXT, DATA_LONGTEXT, true)
131         change_volatile()
132
133     add_integer("imem-id", -1, ID_TEXT, ID_LONGTEXT, true)
134         change_private()
135         change_safe()
136     add_integer("imem-group", 0, GROUP_TEXT, GROUP_LONGTEXT, true)
137         change_private()
138         change_safe()
139     add_integer("imem-cat", 0, CAT_TEXT, CAT_LONGTEXT, true)
140         change_integer_list(cat_values, cat_texts)
141         change_private()
142         change_safe()
143     add_string ("imem-codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true)
144         change_private()
145         change_safe()
146     add_string( "imem-language", NULL, LANGUAGE_TEXT, LANGUAGE_LONGTEXT, false)
147         change_private()
148         change_safe()
149
150     add_integer("imem-samplerate", 0, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, true)
151         change_private()
152         change_safe()
153     add_integer("imem-channels", 0, CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
154         change_private()
155         change_safe()
156
157     add_integer("imem-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, true)
158         change_private()
159         change_safe()
160     add_integer("imem-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
161         change_private()
162         change_safe()
163     add_string ("imem-dar", NULL, DAR_TEXT, DAR_LONGTEXT, true)
164         change_private()
165         change_safe()
166     add_string ("imem-fps", NULL, FPS_TEXT, FPS_LONGTEXT, true)
167         change_private()
168         change_safe()
169
170     add_integer ("imem-size", 0, SIZE_TEXT, SIZE_LONGTEXT, true)
171         change_private()
172         change_safe()
173
174     add_shortcut("imem")
175     set_capability("access_demux", 0)
176     set_callbacks(OpenDemux, CloseDemux)
177
178     add_submodule()
179         add_shortcut("imem")
180         set_capability("access", 0)
181         set_callbacks(OpenAccess, CloseAccess)
182 vlc_module_end()
183
184 /*****************************************************************************
185  * Exported API
186  *****************************************************************************/
187
188 /* The clock origin for the DTS and PTS is assumed to be 0.
189  * A negative value means unknown.
190  *
191  * TODO define flags
192  */
193 typedef int  (*imem_get_t)(void *data, const char *cookie,
194                            int64_t *dts, int64_t *pts, unsigned *flags,
195                            size_t *, void **);
196 typedef void (*imem_release_t)(void *data, const char *cookie, size_t, void *);
197
198 /*****************************************************************************
199  * Local prototypes
200  *****************************************************************************/
201
202 /* */
203 static block_t *Block(access_t *);
204 static int ControlAccess(access_t *, int, va_list);
205
206 static int Demux(demux_t *);
207 static int ControlDemux(demux_t *, int, va_list);
208
209 /* */
210 typedef struct {
211     struct {
212         imem_get_t      get;
213         imem_release_t  release;
214         void           *data;
215         char           *cookie;
216     } source;
217
218     es_out_id_t  *es;
219
220     mtime_t      dts;
221
222     mtime_t      deadline;
223 } imem_sys_t;
224
225 static void ParseMRL(vlc_object_t *, const char *);
226
227 /**
228  * It closes the common part of the access and access_demux
229  */
230 static void CloseCommon(imem_sys_t *sys)
231 {
232     free(sys->source.cookie);
233     free(sys);
234 }
235
236 /**
237  * It initializes the common part for imem access/access_demux.
238  */
239 static int OpenCommon(vlc_object_t *object, imem_sys_t **sys_ptr, const char *psz_path)
240 {
241     char *tmp;
242
243     /* */
244     imem_sys_t *sys = calloc(1, sizeof(*sys));
245     if (!sys)
246         return VLC_ENOMEM;
247
248     /* Read the user functions */
249     tmp = var_InheritString(object, "imem-get");
250     if (tmp)
251         sys->source.get = (imem_get_t)(intptr_t)strtoll(tmp, NULL, 0);
252     free(tmp);
253
254     tmp = var_InheritString(object, "imem-release");
255     if (tmp)
256         sys->source.release = (imem_release_t)(intptr_t)strtoll(tmp, NULL, 0);
257     free(tmp);
258
259     if (!sys->source.get || !sys->source.release) {
260         msg_Err(object, "Invalid get/release function pointers");
261         free(sys);
262         return VLC_EGENERIC;
263     }
264
265     tmp = var_InheritString(object, "imem-data");
266     if (tmp)
267         sys->source.data = (void *)(uintptr_t)strtoull(tmp, NULL, 0);
268     free(tmp);
269
270     /* Now we can parse the MRL (get/release must not be parsed to avoid
271      * security risks) */
272     if (*psz_path)
273         ParseMRL(object, psz_path);
274
275     sys->source.cookie = var_InheritString(object, "imem-cookie");
276
277     msg_Dbg(object, "Using get(%p), release(%p), data(%p), cookie(%s)",
278             sys->source.get, sys->source.release, sys->source.data,
279             sys->source.cookie ? sys->source.cookie : "(null)");
280
281     /* */
282     sys->dts       = 0;
283     sys->deadline  = VLC_TS_INVALID;
284
285     *sys_ptr = sys;
286     return VLC_SUCCESS;
287 }
288
289 /**
290  * It opens an imem access.
291  */
292 static int OpenAccess(vlc_object_t *object)
293 {
294     access_t   *access = (access_t *)object;
295     imem_sys_t *sys;
296
297     if (OpenCommon(object, &sys, access->psz_location))
298         return VLC_EGENERIC;
299
300     if (var_InheritInteger(object, "imem-cat") != 4) {
301         CloseCommon(sys);
302         return VLC_EGENERIC;
303     }
304
305     /* */
306     access_InitFields(access);
307     access->pf_control = ControlAccess;
308     access->pf_read    = NULL;
309     access->pf_block   = Block;
310     access->pf_seek    = NULL;
311     access->p_sys      = (access_sys_t*)sys;
312     access->info.i_size = var_InheritInteger(object, "imem-size");
313
314     return VLC_SUCCESS;
315 }
316
317 /**
318  * It closes an imem access
319  */
320 static void CloseAccess(vlc_object_t *object)
321 {
322     access_t *access = (access_t *)object;
323
324     CloseCommon((imem_sys_t*)access->p_sys);
325 }
326
327 /**
328  * It controls an imem access
329  */
330 static int ControlAccess(access_t *access, int i_query, va_list args)
331 {
332     (void) access;
333     switch (i_query)
334     {
335     case ACCESS_CAN_SEEK:
336     case ACCESS_CAN_FASTSEEK: {
337         bool *b = va_arg( args, bool* );
338         *b = false;
339         return VLC_SUCCESS;
340     }
341     case ACCESS_CAN_PAUSE:
342     case ACCESS_CAN_CONTROL_PACE: {
343         bool *b = va_arg( args, bool* );
344         *b = true;
345         return VLC_SUCCESS;
346     }
347     case ACCESS_GET_PTS_DELAY: {
348         int64_t *delay = va_arg(args, int64_t *);
349         *delay = DEFAULT_PTS_DELAY; /* FIXME? */
350         return VLC_SUCCESS;
351     }
352     case ACCESS_SET_PAUSE_STATE:
353         return VLC_SUCCESS;
354
355     case ACCESS_GET_TITLE_INFO:
356     case ACCESS_SET_TITLE:
357     case ACCESS_SET_SEEKPOINT:
358     case ACCESS_SET_PRIVATE_ID_STATE:
359     case ACCESS_GET_META:
360     case ACCESS_GET_PRIVATE_ID_STATE:
361     case ACCESS_GET_CONTENT_TYPE:
362     default:
363         return VLC_EGENERIC;
364     }
365 }
366
367 /**
368  * It retreives data using the get() callback, copies them,
369  * and then release them using the release() callback.
370  */
371 static block_t *Block(access_t *access)
372 {
373     imem_sys_t *sys = (imem_sys_t*)access->p_sys;
374
375     unsigned flags;
376     size_t buffer_size;
377     void   *buffer;
378
379     if (sys->source.get(sys->source.data, sys->source.cookie,
380                         NULL, NULL, &flags, &buffer_size, &buffer)) {
381         access->info.b_eof = true;
382         return NULL;
383     }
384
385     block_t *block = NULL;
386     if (buffer_size > 0) {
387         block = block_New(access, buffer_size);
388         if (block)
389             memcpy(block->p_buffer, buffer, buffer_size);
390     }
391
392     sys->source.release(sys->source.data, sys->source.cookie,
393                         buffer_size, buffer);
394     return block;
395 }
396
397 /**
398  * It opens an imem access_demux.
399  */
400 static int OpenDemux(vlc_object_t *object)
401 {
402     demux_t    *demux = (demux_t *)object;
403     imem_sys_t *sys;
404
405     if (OpenCommon(object, &sys, demux->psz_location))
406         return VLC_EGENERIC;
407
408     /* ES format */
409     es_format_t fmt;
410     es_format_Init(&fmt, UNKNOWN_ES, 0);
411
412     fmt.i_id = var_InheritInteger(object, "imem-id");
413     fmt.i_group = var_InheritInteger(object, "imem-group");
414
415     char *tmp = var_InheritString(object, "imem-codec");
416     if (tmp)
417         fmt.i_codec = vlc_fourcc_GetCodecFromString(UNKNOWN_ES, tmp);
418     free(tmp);
419
420     const int cat = var_InheritInteger(object, "imem-cat");
421     switch (cat) {
422     case 1: {
423         fmt.i_cat = AUDIO_ES;
424         fmt.audio.i_channels = var_InheritInteger(object, "imem-channels");
425         fmt.audio.i_rate = var_InheritInteger(object, "imem-samplerate");
426
427         msg_Dbg(object, "Audio %4.4s %d channels %d Hz",
428                 (const char *)&fmt.i_codec,
429                 fmt.audio.i_channels, fmt.audio.i_rate);
430         break;
431     }
432     case 2: {
433         fmt.i_cat = VIDEO_ES;
434         fmt.video.i_width  = var_InheritInteger(object, "imem-width");
435         fmt.video.i_height = var_InheritInteger(object, "imem-height");
436         unsigned num, den;
437         if (!var_InheritURational(object, &num, &den, "imem-dar") && num > 0 && den > 0) {
438             if (fmt.video.i_width > 0 && fmt.video.i_height > 0) {
439                 fmt.video.i_sar_num = num * fmt.video.i_height;
440                 fmt.video.i_sar_den = den * fmt.video.i_width;
441             }
442         }
443         if (!var_InheritURational(object, &num, &den, "imem-fps") && num > 0 && den > 0) {
444             fmt.video.i_frame_rate      = num;
445             fmt.video.i_frame_rate_base = den;
446         }
447
448         msg_Dbg(object, "Video %4.4s %dx%d  SAR %d:%d frame rate %u/%u",
449                 (const char *)&fmt.i_codec,
450                 fmt.video.i_width, fmt.video.i_height,
451                 fmt.video.i_sar_num, fmt.video.i_sar_den,
452                 fmt.video.i_frame_rate, fmt.video.i_frame_rate_base);
453         break;
454     }
455     case 3: {
456         fmt.i_cat = SPU_ES;
457         fmt.subs.spu.i_original_frame_width =
458             var_InheritInteger(object, "imem-width");
459         fmt.subs.spu.i_original_frame_height =
460             var_InheritInteger(object, "imem-height");
461
462         msg_Dbg(object, "Subtitle %4.4s",
463                 (const char *)&fmt.i_codec);
464         break;
465     }
466     default:
467         if (cat != 4)
468             msg_Err(object, "Invalid ES category");
469         es_format_Clean(&fmt);
470         CloseCommon(sys);
471         return VLC_EGENERIC;
472     }
473
474     fmt.psz_language = var_InheritString(object, "imem-language");
475
476     sys->es = es_out_Add(demux->out, &fmt);
477     es_format_Clean(&fmt);
478
479     if (!sys->es) {
480         CloseCommon(sys);
481         return VLC_EGENERIC;
482     }
483
484     /* */
485     demux->pf_control = ControlDemux;
486     demux->pf_demux   = Demux;
487     demux->p_sys      = (demux_sys_t*)sys;
488
489     demux->info.i_update = 0;
490     demux->info.i_title = 0;
491     demux->info.i_seekpoint = 0;
492     return VLC_SUCCESS;
493 }
494
495 /**
496  * It closes an imem access_demux
497  */
498 static void CloseDemux(vlc_object_t *object)
499 {
500     demux_t *demux = (demux_t *)object;
501
502     CloseCommon((imem_sys_t*)demux->p_sys);
503 }
504
505 /**
506  * It controls an imem access_demux
507  */
508 static int ControlDemux(demux_t *demux, int i_query, va_list args)
509 {
510     imem_sys_t *sys = (imem_sys_t*)demux->p_sys;
511
512     switch (i_query)
513     {
514     case DEMUX_CAN_PAUSE:
515     case DEMUX_CAN_CONTROL_PACE: {
516         bool *b = va_arg(args, bool *);
517         *b = true;
518         return VLC_SUCCESS;
519     }
520     case DEMUX_SET_PAUSE_STATE:
521         return VLC_SUCCESS;
522
523     case DEMUX_GET_PTS_DELAY: {
524         int64_t *delay = va_arg(args, int64_t *);
525         *delay = DEFAULT_PTS_DELAY; /* FIXME? */
526         return VLC_SUCCESS;
527     }
528     case DEMUX_GET_POSITION: {
529         double *position = va_arg(args, double *);
530         *position = 0.0;
531         return VLC_SUCCESS;
532     }
533     case DEMUX_GET_TIME: {
534         int64_t *t = va_arg(args, int64_t *);
535         *t = sys->dts;
536         return VLC_SUCCESS;
537     }
538     case DEMUX_GET_LENGTH: {
539         int64_t *l = va_arg(args, int64_t *);
540         *l = 0;
541         return VLC_SUCCESS;
542     }
543     case DEMUX_SET_NEXT_DEMUX_TIME:
544         sys->deadline = va_arg(args, int64_t);
545         return VLC_SUCCESS;
546
547     /* */
548     case DEMUX_CAN_SEEK:
549     case DEMUX_SET_POSITION:
550     case DEMUX_SET_TIME:
551     default:
552         return VLC_EGENERIC;
553     }
554
555     return VLC_EGENERIC;
556 }
557
558 /**
559  * It retreives data using the get() callback, sends them to es_out
560  * and the release it using the release() callback.
561  */
562 static int Demux(demux_t *demux)
563 {
564     imem_sys_t *sys = (imem_sys_t*)demux->p_sys;
565
566     if (sys->deadline == VLC_TS_INVALID)
567         sys->deadline = sys->dts + 1;
568
569     for (;;) {
570         if (sys->deadline <= sys->dts)
571             break;
572
573         /* */
574         int64_t dts, pts;
575         unsigned flags;
576         size_t buffer_size;
577         void   *buffer;
578
579         if (sys->source.get(sys->source.data, sys->source.cookie,
580                             &dts, &pts, &flags, &buffer_size, &buffer))
581             return 0;
582
583         if (dts < 0)
584             dts = pts;
585
586         if (buffer_size > 0) {
587             block_t *block = block_New(demux, buffer_size);
588             if (block) {
589                 block->i_dts = dts >= 0 ? (1 + dts) : VLC_TS_INVALID;
590                 block->i_pts = pts >= 0 ? (1 + pts) : VLC_TS_INVALID;
591                 memcpy(block->p_buffer, buffer, buffer_size);
592
593                 es_out_Control(demux->out, ES_OUT_SET_PCR, block->i_dts);
594                 es_out_Send(demux->out, sys->es, block);
595             }
596         }
597
598         sys->dts = dts;
599
600         sys->source.release(sys->source.data, sys->source.cookie,
601                             buffer_size, buffer);
602     }
603     sys->deadline = VLC_TS_INVALID;
604     return 1;
605 }
606
607 /**
608  * Parse the MRL and extract configuration from it.
609  *
610  * Syntax: option1=value1[:option2=value2[...]]
611  *
612  * XXX get and release are not supported on purpose.
613  */
614 static void ParseMRL(vlc_object_t *object, const char *psz_path)
615 {
616     static const struct {
617         const char *name;
618         int        type;
619     } options[] = {
620         { "id",         VLC_VAR_INTEGER },
621         { "group",      VLC_VAR_INTEGER },
622         { "cat",        VLC_VAR_INTEGER },
623         { "samplerate", VLC_VAR_INTEGER },
624         { "channels",   VLC_VAR_INTEGER },
625         { "width",      VLC_VAR_INTEGER },
626         { "height",     VLC_VAR_INTEGER },
627         { "cookie",     VLC_VAR_STRING },
628         { "codec",      VLC_VAR_STRING },
629         { "language",   VLC_VAR_STRING },
630         { "dar",        VLC_VAR_STRING },
631         { "fps",        VLC_VAR_STRING },
632         { NULL, -1 }
633     };
634
635     char *dup = strdup(psz_path);
636     if (!dup)
637         return;
638     char *current = dup;
639
640     while (current) {
641         char *next = strchr(current, ':');
642         if (next)
643             *next++ = '\0';
644
645         char *option = current;
646         char *value = strchr(current, '=');
647         if (value) {
648             *value++ = '\0';
649             msg_Dbg(object, "option '%s' value '%s'", option, value);
650         } else {
651             msg_Dbg(object, "option '%s' without value (unsupported)", option);
652         }
653
654         char *name;
655         if (asprintf(&name, "imem-%s", option) < 0)
656             name = NULL;
657         for (unsigned i = 0; name && options[i].name; i++) {
658             if (strcmp(options[i].name, option))
659                 continue;
660             /* */
661             var_Create(object, name, options[i].type | VLC_VAR_DOINHERIT);
662             if (options[i].type == VLC_VAR_INTEGER && value) {
663                 var_SetInteger(object, name, strtol(value, NULL, 0));
664             } else if (options[i].type == VLC_VAR_STRING && value) {
665                 var_SetString(object, name, value);
666             }
667             break;
668         }
669         free(name);
670
671         /* */
672         current = next;
673     }
674     free(dup);
675 }