]> git.sesse.net Git - vlc/blob - modules/demux/gme.c
GME: new plugin based on the official C API
[vlc] / modules / demux / gme.c
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
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdarg.h>
28 #include <limits.h>
29 #include <assert.h>
30
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
33 #include <vlc_aout.h>
34 #include <vlc_plugin.h>
35
36 #include <gme/gme.h>
37
38 static int  Open (vlc_object_t *);
39 static void Close (vlc_object_t *);
40
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 ()
49
50 #define RATE 48000
51
52 struct demux_sys_t
53 {
54     Music_Emu   *emu;
55     es_out_id_t *es;
56     date_t       pts;
57 };
58
59
60 static int Demux (demux_t *);
61 static int Control (demux_t *, int, va_list);
62 static gme_err_t Reader (void *, void *, int);
63
64 static int Open (vlc_object_t *obj)
65 {
66     demux_t *demux = (demux_t *)obj;
67
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 */)
71         return VLC_EGENERIC;
72
73     const uint8_t *peek;
74     if (stream_Peek (demux->s, &peek, 4) < 4)
75         return VLC_EGENERIC;
76
77     const char *type = gme_identify_header (peek);
78     if (!*type)
79         return VLC_EGENERIC;
80     msg_Dbg (obj, "detected file type %s", type);
81
82     demux_sys_t *sys = malloc (sizeof (*sys));
83     if (unlikely(sys == NULL))
84         return VLC_ENOMEM;
85
86     sys->emu = gme_new_emu (gme_identify_extension (type), RATE);
87     if (sys->emu == NULL)
88     {
89         free (sys);
90         return VLC_ENOMEM;
91     }
92     gme_load_custom (sys->emu, Reader, size, demux->s);
93     gme_start_track (sys->emu, 0);
94
95     es_format_t fmt;
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;
104
105     sys->es = es_out_Add (demux->out, &fmt);
106     date_Init (&sys->pts, RATE, 1);
107     date_Set (&sys->pts, 0);
108
109     demux->pf_demux = Demux;
110     demux->pf_control = Control;
111     demux->p_sys = sys;
112     return VLC_SUCCESS;
113 }
114
115
116 static void Close (vlc_object_t *obj)
117 {
118     demux_t *demux = (demux_t *)obj;
119     demux_sys_t *sys = demux->p_sys;
120
121     gme_delete (sys->emu);
122     free (sys);
123 }
124
125
126 static gme_err_t Reader (void *data, void *buf, int length)
127 {
128     stream_t *s = data;
129
130     if (stream_Read (s, buf, length) < length)
131         return "short read";
132     return NULL;
133 }
134
135
136 #define SAMPLES (RATE / 10)
137
138 static int Demux (demux_t *demux)
139 {
140     demux_sys_t *sys = demux->p_sys;
141
142     if (gme_track_ended (sys->emu))
143     {
144         msg_Dbg (demux, "track ended");
145         return 0;
146     }
147
148     block_t *block = block_Alloc (2 * 2 * SAMPLES);
149     if (unlikely(block == NULL))
150         return 0;
151
152     gme_err_t ret = gme_play (sys->emu, 2 * SAMPLES, (void *)block->p_buffer);
153     if (ret != NULL)
154     {
155         block_Release (block);
156         msg_Err (demux, "%s", ret);
157         return 0;
158     }
159
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);
164     return 1;
165 }
166
167
168 static int Control (demux_t *demux, int query, va_list args)
169 {
170     demux_sys_t *sys = demux->p_sys;
171
172     switch (query)
173     {
174         case DEMUX_GET_POSITION: // TODO
175         {
176             double *pos = va_arg (args, double *);
177             *pos = 0.;
178             return VLC_SUCCESS;
179         }
180
181         //case DEMUX_SET_POSITION: TODO
182         //case DEMUX_GET_LENGTH: TODO
183         case DEMUX_GET_TIME:
184         {
185             int64_t *v = va_arg (args, int64_t *);
186             *v = gme_tell (sys->emu) * 1000;
187             return VLC_SUCCESS;
188         }
189
190         case DEMUX_SET_TIME:
191         {
192             int64_t v = va_arg (args, int64_t);
193             if (v > INT_MAX || gme_seek (sys->emu, v / 1000))
194                 return VLC_EGENERIC;
195             return VLC_SUCCESS;
196         }
197     }
198
199     return VLC_EGENERIC;
200 }