3 * @brief Game Music Emu demux module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2010 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This library 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 General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
34 #include <vlc_plugin.h>
38 static int Open (vlc_object_t *);
39 static void Close (vlc_object_t *);
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)
60 static int Demux (demux_t *);
61 static int Control (demux_t *, int, va_list);
62 static gme_err_t Reader (void *, void *, int);
64 static int Open (vlc_object_t *obj)
66 demux_t *demux = (demux_t *)obj;
68 int64_t size = stream_Size (demux->s);
69 if (size < 4 /* GME needs to know the file size */
70 || size > LONG_MAX /* too big for GME */)
74 if (stream_Peek (demux->s, &peek, 4) < 4)
77 const char *type = gme_identify_header (peek);
80 msg_Dbg (obj, "detected file type %s", type);
82 demux_sys_t *sys = malloc (sizeof (*sys));
83 if (unlikely(sys == NULL))
86 sys->emu = gme_new_emu (gme_identify_extension (type), RATE);
92 gme_load_custom (sys->emu, Reader, size, demux->s);
93 gme_start_track (sys->emu, 0);
96 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16N);
97 fmt.audio.i_rate = RATE;
98 fmt.audio.i_bytes_per_frame = 4;
99 fmt.audio.i_frame_length = 4;
100 fmt.audio.i_channels = 2;
101 fmt.audio.i_blockalign = 4;
102 fmt.audio.i_bitspersample = 16;
103 fmt.i_bitrate = RATE * 4;
105 sys->es = es_out_Add (demux->out, &fmt);
106 date_Init (&sys->pts, RATE, 1);
107 date_Set (&sys->pts, 0);
109 demux->pf_demux = Demux;
110 demux->pf_control = Control;
116 static void Close (vlc_object_t *obj)
118 demux_t *demux = (demux_t *)obj;
119 demux_sys_t *sys = demux->p_sys;
121 gme_delete (sys->emu);
126 static gme_err_t Reader (void *data, void *buf, int length)
130 if (stream_Read (s, buf, length) < length)
136 #define SAMPLES (RATE / 10)
138 static int Demux (demux_t *demux)
140 demux_sys_t *sys = demux->p_sys;
142 if (gme_track_ended (sys->emu))
144 msg_Dbg (demux, "track ended");
148 block_t *block = block_Alloc (2 * 2 * SAMPLES);
149 if (unlikely(block == NULL))
152 gme_err_t ret = gme_play (sys->emu, 2 * SAMPLES, (void *)block->p_buffer);
155 block_Release (block);
156 msg_Err (demux, "%s", ret);
160 block->i_pts = block->i_dts = VLC_TS_0 + date_Get (&sys->pts);
161 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
162 es_out_Send (demux->out, sys->es, block);
163 date_Increment (&sys->pts, SAMPLES);
168 static int Control (demux_t *demux, int query, va_list args)
170 demux_sys_t *sys = demux->p_sys;
174 case DEMUX_GET_POSITION: // TODO
176 double *pos = va_arg (args, double *);
181 //case DEMUX_SET_POSITION: TODO
182 //case DEMUX_GET_LENGTH: TODO
185 int64_t *v = va_arg (args, int64_t *);
186 *v = gme_tell (sys->emu) * 1000;
192 int64_t v = va_arg (args, int64_t);
193 if (v > INT_MAX || gme_seek (sys->emu, v / 1000))