UPnP: split sat>ip parsing function
[vlc.git] / modules / demux / gme.c
blob6fc9018603f19be73fdf9d696fdde1cf8acd1998
1 /**
2 * @file gme.c
3 * @brief Game Music Emu demux module for VLC media player
4 */
5 /*****************************************************************************
6 * Copyright © 2010 Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <stdarg.h>
28 #include <limits.h>
29 #include <assert.h>
31 #include <vlc_common.h>
32 #include <vlc_input.h>
33 #include <vlc_demux.h>
34 #include <vlc_plugin.h>
36 #include <gme/gme.h>
38 static int Open (vlc_object_t *);
39 static void Close (vlc_object_t *);
41 vlc_module_begin ()
42 set_shortname ("GME")
43 set_description ("Game Music Emu")
44 set_category (CAT_INPUT)
45 set_subcategory (SUBCAT_INPUT_DEMUX)
46 set_capability ("demux", 10)
47 set_callbacks (Open, Close)
48 vlc_module_end ()
50 #define RATE 48000
52 typedef struct
54 Music_Emu *emu;
55 unsigned track_id;
57 es_out_id_t *es;
58 date_t pts;
60 input_title_t **titlev;
61 unsigned titlec;
62 bool title_changed;
63 } demux_sys_t;
66 static int Demux (demux_t *);
67 static int Control (demux_t *, int, va_list);
68 static gme_err_t ReaderStream (void *, void *, int);
69 static gme_err_t ReaderBlock (void *, void *, int);
71 static int Open (vlc_object_t *obj)
73 demux_t *demux = (demux_t *)obj;
74 uint64_t size;
76 if (vlc_stream_GetSize(demux->s, &size))
77 return VLC_EGENERIC;
78 if (size > LONG_MAX /* too big for GME */)
79 return VLC_EGENERIC;
81 /* Auto detection */
82 const uint8_t *peek;
83 if (vlc_stream_Peek (demux->s, &peek, 4) < 4)
84 return VLC_EGENERIC;
86 const char *type = gme_identify_header (peek);
87 if (!*type)
88 return VLC_EGENERIC;
89 msg_Dbg (obj, "detected file type %s", type);
91 block_t *data = NULL;
92 if (size <= 0)
94 data = vlc_stream_Block (demux->s, 1 << 24);
95 if (data == NULL)
96 return VLC_EGENERIC;
99 /* Initialization */
100 demux_sys_t *sys = malloc (sizeof (*sys));
101 if (unlikely(sys == NULL))
102 return VLC_ENOMEM;
104 sys->emu = gme_new_emu (gme_identify_extension (type), RATE);
105 if (sys->emu == NULL)
107 free (sys);
108 return VLC_ENOMEM;
110 if (data)
112 gme_load_custom (sys->emu, ReaderBlock, data->i_buffer, data);
113 block_Release(data);
115 else
117 gme_load_custom (sys->emu, ReaderStream, size, demux->s);
119 gme_start_track (sys->emu, sys->track_id = 0);
121 es_format_t fmt;
122 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16N);
123 fmt.audio.i_rate = RATE;
124 fmt.audio.i_bytes_per_frame = 4;
125 fmt.audio.i_frame_length = 4;
126 fmt.audio.i_channels = 2;
127 fmt.audio.i_blockalign = 4;
128 fmt.audio.i_bitspersample = 16;
129 fmt.i_bitrate = RATE * 4;
131 sys->es = es_out_Add (demux->out, &fmt);
132 date_Init (&sys->pts, RATE, 1);
133 date_Set(&sys->pts, VLC_TICK_0);
135 /* Titles */
136 unsigned n = gme_track_count (sys->emu);
137 sys->titlev = vlc_alloc (n, sizeof (*sys->titlev));
138 if (unlikely(sys->titlev == NULL))
139 n = 0;
140 sys->titlec = n;
141 for (unsigned i = 0; i < n; i++)
143 input_title_t *title = vlc_input_title_New ();
144 sys->titlev[i] = title;
145 if (unlikely(title == NULL))
146 continue;
148 gme_info_t *infos;
149 if (gme_track_info (sys->emu, &infos, i))
150 continue;
151 msg_Dbg (obj, "track %u: %s %d ms", i, infos->song, infos->length);
152 if (infos->length != -1)
153 title->i_length = VLC_TICK_FROM_MS(infos->length);
154 if (infos->song[0])
155 title->psz_name = strdup (infos->song);
156 gme_free_info (infos);
158 sys->title_changed = false;
160 /* Callbacks */
161 demux->pf_demux = Demux;
162 demux->pf_control = Control;
163 demux->p_sys = sys;
164 return VLC_SUCCESS;
168 static void Close (vlc_object_t *obj)
170 demux_t *demux = (demux_t *)obj;
171 demux_sys_t *sys = demux->p_sys;
173 for (unsigned i = 0, n = sys->titlec; i < n; i++)
174 vlc_input_title_Delete (sys->titlev[i]);
175 free (sys->titlev);
176 gme_delete (sys->emu);
177 free (sys);
181 static gme_err_t ReaderStream (void *data, void *buf, int length)
183 stream_t *s = data;
185 if (vlc_stream_Read (s, buf, length) < length)
186 return "short read";
187 return NULL;
189 static gme_err_t ReaderBlock (void *data, void *buf, int length)
191 block_t *block = data;
193 int max = __MIN (length, (int)block->i_buffer);
194 memcpy (buf, block->p_buffer, max);
195 block->i_buffer -= max;
196 block->p_buffer += max;
197 if (max != length)
198 return "short read";
199 return NULL;
202 #define SAMPLES (RATE / 10)
204 static int Demux (demux_t *demux)
206 demux_sys_t *sys = demux->p_sys;
208 /* Next track */
209 if (gme_track_ended (sys->emu))
211 msg_Dbg (demux, "track %u ended", sys->track_id);
212 if (++sys->track_id >= (unsigned)gme_track_count (sys->emu))
213 return VLC_DEMUXER_EOF;
215 sys->title_changed = true;
216 gme_start_track (sys->emu, sys->track_id);
220 block_t *block = block_Alloc (2 * 2 * SAMPLES);
221 if (unlikely(block == NULL))
222 return VLC_DEMUXER_EOF;
224 gme_err_t ret = gme_play (sys->emu, 2 * SAMPLES, (void *)block->p_buffer);
225 if (ret != NULL)
227 block_Release (block);
228 msg_Err (demux, "%s", ret);
229 return VLC_DEMUXER_EOF;
232 block->i_pts = block->i_dts = date_Get (&sys->pts);
233 es_out_SetPCR (demux->out, block->i_pts);
234 es_out_Send (demux->out, sys->es, block);
235 date_Increment (&sys->pts, SAMPLES);
236 return VLC_DEMUXER_SUCCESS;
240 static int Control (demux_t *demux, int query, va_list args)
242 demux_sys_t *sys = demux->p_sys;
244 switch (query)
246 case DEMUX_CAN_SEEK:
247 *va_arg (args, bool *) = true;
248 return VLC_SUCCESS;
250 case DEMUX_GET_POSITION:
252 double *pos = va_arg (args, double *);
254 if (unlikely(sys->track_id >= sys->titlec)
255 || (sys->titlev[sys->track_id]->i_length == 0))
256 *pos = 0.;
257 else
259 int offset = gme_tell (sys->emu);
261 *pos = (double)offset
262 / (double)(sys->titlev[sys->track_id]->i_length / 1000);
264 return VLC_SUCCESS;
267 case DEMUX_SET_POSITION:
269 double pos = va_arg (args, double);
271 if (unlikely(sys->track_id >= sys->titlec)
272 || (sys->titlev[sys->track_id]->i_length == 0))
273 break;
275 int seek = MS_FROM_VLC_TICK(sys->titlev[sys->track_id]->i_length) * pos;
276 if (gme_seek (sys->emu, seek))
277 break;
278 return VLC_SUCCESS;
281 case DEMUX_GET_LENGTH:
283 int64_t *v = va_arg (args, int64_t *);
285 if (unlikely(sys->track_id >= sys->titlec)
286 || (sys->titlev[sys->track_id]->i_length == 0))
287 break;
288 *v = sys->titlev[sys->track_id]->i_length;
289 return VLC_SUCCESS;
292 case DEMUX_GET_TIME:
294 int64_t *v = va_arg (args, int64_t *);
295 *v = gme_tell (sys->emu) * INT64_C(1000);
296 return VLC_SUCCESS;
299 case DEMUX_SET_TIME:
301 int64_t v = va_arg (args, int64_t) / 1000;
302 if (v > INT_MAX || gme_seek (sys->emu, v))
303 break;
304 return VLC_SUCCESS;
307 case DEMUX_GET_TITLE_INFO:
309 input_title_t ***titlev = va_arg (args, input_title_t ***);
310 int *titlec = va_arg (args, int *);
311 *(va_arg (args, int *)) = 0; /* Title offset */
312 *(va_arg (args, int *)) = 0; /* Chapter offset */
314 unsigned n = sys->titlec;
315 *titlev = vlc_alloc (n, sizeof (**titlev));
316 if (unlikely(*titlev == NULL))
317 n = 0;
318 *titlec = n;
319 for (unsigned i = 0; i < n; i++)
320 (*titlev)[i] = vlc_input_title_Duplicate (sys->titlev[i]);
321 return VLC_SUCCESS;
324 case DEMUX_SET_TITLE:
326 int track_id = va_arg (args, int);
327 if (track_id >= gme_track_count (sys->emu))
328 break;
329 gme_start_track (sys->emu, track_id);
330 sys->title_changed = true;
331 sys->track_id = track_id;
332 return VLC_SUCCESS;
335 case DEMUX_TEST_AND_CLEAR_FLAGS:
337 unsigned *restrict flags = va_arg(args, unsigned *);
339 if ((*flags & INPUT_UPDATE_TITLE) && sys->title_changed) {
340 *flags = INPUT_UPDATE_TITLE;
341 sys->title_changed = false;
342 } else
343 *flags = 0;
344 return VLC_SUCCESS;
347 case DEMUX_GET_TITLE:
348 *va_arg(args, int *) = sys->track_id;
349 return VLC_SUCCESS;
351 case DEMUX_CAN_PAUSE:
352 case DEMUX_SET_PAUSE_STATE:
353 case DEMUX_CAN_CONTROL_PACE:
354 case DEMUX_GET_PTS_DELAY:
355 return demux_vaControlHelper( demux->s, 0, -1, 0, 1, query, args );
357 default:
358 return VLC_EGENERIC;
361 return VLC_EGENERIC;