vout/macosx: fix Control not working with libvlc
[vlc.git] / modules / demux / gme.c
blobb144ca6c056a110d2350a56d9a35584ca603532f
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 struct demux_sys_t
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;
65 static int Demux (demux_t *);
66 static int Control (demux_t *, int, va_list);
67 static gme_err_t ReaderStream (void *, void *, int);
68 static gme_err_t ReaderBlock (void *, void *, int);
70 static int Open (vlc_object_t *obj)
72 demux_t *demux = (demux_t *)obj;
73 uint64_t size;
75 if (vlc_stream_GetSize(demux->s, &size))
76 return VLC_EGENERIC;
77 if (size > LONG_MAX /* too big for GME */)
78 return VLC_EGENERIC;
80 /* Auto detection */
81 const uint8_t *peek;
82 if (vlc_stream_Peek (demux->s, &peek, 4) < 4)
83 return VLC_EGENERIC;
85 const char *type = gme_identify_header (peek);
86 if (!*type)
87 return VLC_EGENERIC;
88 msg_Dbg (obj, "detected file type %s", type);
90 block_t *data = NULL;
91 if (size <= 0)
93 data = vlc_stream_Block (demux->s, 1 << 24);
94 if (data == NULL)
95 return VLC_EGENERIC;
98 /* Initialization */
99 demux_sys_t *sys = malloc (sizeof (*sys));
100 if (unlikely(sys == NULL))
101 return VLC_ENOMEM;
103 sys->emu = gme_new_emu (gme_identify_extension (type), RATE);
104 if (sys->emu == NULL)
106 free (sys);
107 return VLC_ENOMEM;
109 if (data)
111 gme_load_custom (sys->emu, ReaderBlock, data->i_buffer, data);
112 block_Release(data);
114 else
116 gme_load_custom (sys->emu, ReaderStream, size, demux->s);
118 gme_start_track (sys->emu, sys->track_id = 0);
120 es_format_t fmt;
121 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16N);
122 fmt.audio.i_rate = RATE;
123 fmt.audio.i_bytes_per_frame = 4;
124 fmt.audio.i_frame_length = 4;
125 fmt.audio.i_channels = 2;
126 fmt.audio.i_blockalign = 4;
127 fmt.audio.i_bitspersample = 16;
128 fmt.i_bitrate = RATE * 4;
130 sys->es = es_out_Add (demux->out, &fmt);
131 date_Init (&sys->pts, RATE, 1);
132 date_Set (&sys->pts, 0);
134 /* Titles */
135 unsigned n = gme_track_count (sys->emu);
136 sys->titlev = vlc_alloc (n, sizeof (*sys->titlev));
137 if (unlikely(sys->titlev == NULL))
138 n = 0;
139 sys->titlec = n;
140 for (unsigned i = 0; i < n; i++)
142 input_title_t *title = vlc_input_title_New ();
143 sys->titlev[i] = title;
144 if (unlikely(title == NULL))
145 continue;
147 gme_info_t *infos;
148 if (gme_track_info (sys->emu, &infos, i))
149 continue;
150 msg_Dbg (obj, "track %u: %s %d ms", i, infos->song, infos->length);
151 if (infos->length != -1)
152 title->i_length = infos->length * INT64_C(1000);
153 if (infos->song[0])
154 title->psz_name = strdup (infos->song);
155 gme_free_info (infos);
158 /* Callbacks */
159 demux->pf_demux = Demux;
160 demux->pf_control = Control;
161 demux->p_sys = sys;
162 return VLC_SUCCESS;
166 static void Close (vlc_object_t *obj)
168 demux_t *demux = (demux_t *)obj;
169 demux_sys_t *sys = demux->p_sys;
171 for (unsigned i = 0, n = sys->titlec; i < n; i++)
172 vlc_input_title_Delete (sys->titlev[i]);
173 free (sys->titlev);
174 gme_delete (sys->emu);
175 free (sys);
179 static gme_err_t ReaderStream (void *data, void *buf, int length)
181 stream_t *s = data;
183 if (vlc_stream_Read (s, buf, length) < length)
184 return "short read";
185 return NULL;
187 static gme_err_t ReaderBlock (void *data, void *buf, int length)
189 block_t *block = data;
191 int max = __MIN (length, (int)block->i_buffer);
192 memcpy (buf, block->p_buffer, max);
193 block->i_buffer -= max;
194 block->p_buffer += max;
195 if (max != length)
196 return "short read";
197 return NULL;
200 #define SAMPLES (RATE / 10)
202 static int Demux (demux_t *demux)
204 demux_sys_t *sys = demux->p_sys;
206 /* Next track */
207 if (gme_track_ended (sys->emu))
209 msg_Dbg (demux, "track %u ended", sys->track_id);
210 if (++sys->track_id >= (unsigned)gme_track_count (sys->emu))
211 return 0;
213 demux->info.i_update |= INPUT_UPDATE_TITLE;
214 demux->info.i_title = sys->track_id;
215 gme_start_track (sys->emu, sys->track_id);
219 block_t *block = block_Alloc (2 * 2 * SAMPLES);
220 if (unlikely(block == NULL))
221 return 0;
223 gme_err_t ret = gme_play (sys->emu, 2 * SAMPLES, (void *)block->p_buffer);
224 if (ret != NULL)
226 block_Release (block);
227 msg_Err (demux, "%s", ret);
228 return 0;
231 block->i_pts = block->i_dts = VLC_TS_0 + date_Get (&sys->pts);
232 es_out_SetPCR (demux->out, block->i_pts);
233 es_out_Send (demux->out, sys->es, block);
234 date_Increment (&sys->pts, SAMPLES);
235 return 1;
239 static int Control (demux_t *demux, int query, va_list args)
241 demux_sys_t *sys = demux->p_sys;
243 switch (query)
245 case DEMUX_CAN_SEEK:
246 *va_arg (args, bool *) = true;
247 return VLC_SUCCESS;
249 case DEMUX_GET_POSITION:
251 double *pos = va_arg (args, double *);
253 if (unlikely(sys->track_id >= sys->titlec)
254 || (sys->titlev[sys->track_id]->i_length == 0))
255 *pos = 0.;
256 else
258 int offset = gme_tell (sys->emu);
260 *pos = (double)offset
261 / (double)(sys->titlev[sys->track_id]->i_length / 1000);
263 return VLC_SUCCESS;
266 case DEMUX_SET_POSITION:
268 double pos = va_arg (args, double);
270 if (unlikely(sys->track_id >= sys->titlec)
271 || (sys->titlev[sys->track_id]->i_length == 0))
272 break;
274 int seek = (sys->titlev[sys->track_id]->i_length / 1000) * pos;
275 if (gme_seek (sys->emu, seek))
276 break;
277 return VLC_SUCCESS;
280 case DEMUX_GET_LENGTH:
282 int64_t *v = va_arg (args, int64_t *);
284 if (unlikely(sys->track_id >= sys->titlec)
285 || (sys->titlev[sys->track_id]->i_length == 0))
286 break;
287 *v = sys->titlev[sys->track_id]->i_length;
288 return VLC_SUCCESS;
291 case DEMUX_GET_TIME:
293 int64_t *v = va_arg (args, int64_t *);
294 *v = gme_tell (sys->emu) * INT64_C(1000);
295 return VLC_SUCCESS;
298 case DEMUX_SET_TIME:
300 int64_t v = va_arg (args, int64_t) / 1000;
301 if (v > INT_MAX || gme_seek (sys->emu, v))
302 break;
303 return VLC_SUCCESS;
306 case DEMUX_GET_TITLE_INFO:
308 input_title_t ***titlev = va_arg (args, input_title_t ***);
309 int *titlec = va_arg (args, int *);
310 *(va_arg (args, int *)) = 0; /* Title offset */
311 *(va_arg (args, int *)) = 0; /* Chapter offset */
313 unsigned n = sys->titlec;
314 *titlev = vlc_alloc (n, sizeof (**titlev));
315 if (unlikely(*titlev == NULL))
316 n = 0;
317 *titlec = n;
318 for (unsigned i = 0; i < n; i++)
319 (*titlev)[i] = vlc_input_title_Duplicate (sys->titlev[i]);
320 return VLC_SUCCESS;
323 case DEMUX_SET_TITLE:
325 int track_id = va_arg (args, int);
326 if (track_id >= gme_track_count (sys->emu))
327 break;
328 gme_start_track (sys->emu, track_id);
329 demux->info.i_update |= INPUT_UPDATE_TITLE;
330 demux->info.i_title = track_id;
331 sys->track_id = track_id;
332 return VLC_SUCCESS;
336 return VLC_EGENERIC;