]> git.sesse.net Git - vlc/blob - modules/demux/gme.c
Qt4: add ATSC and CQAM to open dialog
[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_input.h>
33 #include <vlc_demux.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     unsigned     track_id;
56
57     es_out_id_t *es;
58     date_t       pts;
59
60     input_title_t **titlev;
61     unsigned        titlec;
62 };
63
64
65 static int Demux (demux_t *);
66 static int Control (demux_t *, int, va_list);
67 static gme_err_t Reader (void *, void *, int);
68
69 static int Open (vlc_object_t *obj)
70 {
71     demux_t *demux = (demux_t *)obj;
72
73     int64_t size = stream_Size (demux->s);
74     if (size < 4 /* GME needs to know the file size */
75      || size > LONG_MAX /* too big for GME */)
76         return VLC_EGENERIC;
77
78     /* Auto detection */
79     const uint8_t *peek;
80     if (stream_Peek (demux->s, &peek, 4) < 4)
81         return VLC_EGENERIC;
82
83     const char *type = gme_identify_header (peek);
84     if (!*type)
85         return VLC_EGENERIC;
86     msg_Dbg (obj, "detected file type %s", type);
87
88     /* Initialization */
89     demux_sys_t *sys = malloc (sizeof (*sys));
90     if (unlikely(sys == NULL))
91         return VLC_ENOMEM;
92
93     sys->emu = gme_new_emu (gme_identify_extension (type), RATE);
94     if (sys->emu == NULL)
95     {
96         free (sys);
97         return VLC_ENOMEM;
98     }
99     gme_load_custom (sys->emu, Reader, size, demux->s);
100     gme_start_track (sys->emu, sys->track_id = 0);
101
102     es_format_t fmt;
103     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16N);
104     fmt.audio.i_rate = RATE;
105     fmt.audio.i_bytes_per_frame = 4;
106     fmt.audio.i_frame_length = 4;
107     fmt.audio.i_channels = 2;
108     fmt.audio.i_blockalign = 4;
109     fmt.audio.i_bitspersample = 16;
110     fmt.i_bitrate = RATE * 4;
111
112     sys->es = es_out_Add (demux->out, &fmt);
113     date_Init (&sys->pts, RATE, 1);
114     date_Set (&sys->pts, 0);
115
116     /* Titles */
117     unsigned n = gme_track_count (sys->emu);
118     sys->titlev = malloc (n * sizeof (*sys->titlev));
119     if (unlikely(sys->titlev == NULL))
120         n = 0;
121     sys->titlec = n;
122     for (unsigned i = 0; i < n; i++)
123     {
124          input_title_t *title = vlc_input_title_New ();
125          sys->titlev[i] = title;
126          if (unlikely(title == NULL))
127              continue;
128
129          gme_info_t *infos;
130          if (gme_track_info (sys->emu, &infos, i))
131              continue;
132          msg_Dbg (obj, "track %u: %s %d ms", i, infos->song, infos->length);
133          if (infos->length != -1)
134              title->i_length = infos->length * INT64_C(1000);
135          if (infos->song[0])
136              title->psz_name = strdup (infos->song);
137          gme_free_info (infos);
138     }
139
140     /* Callbacks */
141     demux->pf_demux = Demux;
142     demux->pf_control = Control;
143     demux->p_sys = sys;
144     return VLC_SUCCESS;
145 }
146
147
148 static void Close (vlc_object_t *obj)
149 {
150     demux_t *demux = (demux_t *)obj;
151     demux_sys_t *sys = demux->p_sys;
152
153     for (unsigned i = 0, n = sys->titlec; i < n; i++)
154         vlc_input_title_Delete (sys->titlev[i]);
155     free (sys->titlev);
156     gme_delete (sys->emu);
157     free (sys);
158 }
159
160
161 static gme_err_t Reader (void *data, void *buf, int length)
162 {
163     stream_t *s = data;
164
165     if (stream_Read (s, buf, length) < length)
166         return "short read";
167     return NULL;
168 }
169
170
171 #define SAMPLES (RATE / 10)
172
173 static int Demux (demux_t *demux)
174 {
175     demux_sys_t *sys = demux->p_sys;
176
177     /* Next track */
178     if (gme_track_ended (sys->emu))
179     {
180         msg_Dbg (demux, "track %u ended", sys->track_id);
181         if (++sys->track_id >= (unsigned)gme_track_count (sys->emu))
182             return 0;
183
184         demux->info.i_update |= INPUT_UPDATE_TITLE;
185         demux->info.i_title = sys->track_id;
186         gme_start_track (sys->emu, sys->track_id);
187     }
188
189
190     block_t *block = block_Alloc (2 * 2 * SAMPLES);
191     if (unlikely(block == NULL))
192         return 0;
193
194     gme_err_t ret = gme_play (sys->emu, 2 * SAMPLES, (void *)block->p_buffer);
195     if (ret != NULL)
196     {
197         block_Release (block);
198         msg_Err (demux, "%s", ret);
199         return 0;
200     }
201
202     block->i_pts = block->i_dts = VLC_TS_0 + date_Get (&sys->pts);
203     es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
204     es_out_Send (demux->out, sys->es, block);
205     date_Increment (&sys->pts, SAMPLES);
206     return 1;
207 }
208
209
210 static int Control (demux_t *demux, int query, va_list args)
211 {
212     demux_sys_t *sys = demux->p_sys;
213
214     switch (query)
215     {
216         case DEMUX_GET_POSITION:
217         {
218             double *pos = va_arg (args, double *);
219
220             if (unlikely(sys->track_id >= sys->titlec)
221              || (sys->titlev[sys->track_id]->i_length == 0))
222                 *pos = 0.;
223             else
224                 *pos = (double)(gme_tell (sys->emu))
225                     / (double)(sys->titlev[sys->track_id]->i_length / 1000);
226             return VLC_SUCCESS;
227         }
228
229         case DEMUX_SET_POSITION:
230         {
231             double pos = va_arg (args, double);
232
233             if (unlikely(sys->track_id >= sys->titlec)
234              || (sys->titlev[sys->track_id]->i_length == 0))
235                 break;
236
237             int seek = (sys->titlev[sys->track_id]->i_length / 1000) * pos;
238             if (seek > INT_MAX || gme_seek (sys->emu, seek))
239                 break;
240             return VLC_SUCCESS;
241         }
242
243         case DEMUX_GET_LENGTH:
244         {
245             int64_t *v = va_arg (args, int64_t *);
246
247             if (unlikely(sys->track_id >= sys->titlec)
248              || (sys->titlev[sys->track_id]->i_length == 0))
249                 break;
250             *v = sys->titlev[sys->track_id]->i_length;
251             return VLC_SUCCESS;
252         }
253
254         case DEMUX_GET_TIME:
255         {
256             int64_t *v = va_arg (args, int64_t *);
257             *v = gme_tell (sys->emu) * INT64_C(1000);
258             return VLC_SUCCESS;
259         }
260
261         case DEMUX_SET_TIME:
262         {
263             int64_t v = va_arg (args, int64_t) / 1000;
264             if (v > INT_MAX || gme_seek (sys->emu, v))
265                 break;
266             return VLC_SUCCESS;
267         }
268
269         case DEMUX_GET_TITLE_INFO:
270         {
271             input_title_t ***titlev = va_arg (args, input_title_t ***);
272             int *titlec = va_arg (args, int *);
273             *(va_arg (args, int *)) = 0; /* Title offset */
274             *(va_arg (args, int *)) = 0; /* Chapter offset */
275
276             unsigned n = sys->titlec;
277             *titlev = malloc (sizeof (**titlev) * n);
278             if (unlikely(titlev == NULL))
279                 n = 0;
280             *titlec = n;
281             for (unsigned i = 0; i < n; i++)
282                 (*titlev)[i] = vlc_input_title_Duplicate (sys->titlev[i]);
283             return VLC_SUCCESS;
284         }
285
286         case DEMUX_SET_TITLE:
287         {
288             int track_id = va_arg (args, int);
289             if (track_id >= gme_track_count (sys->emu))
290                 break;
291             gme_start_track (sys->emu, track_id);
292             demux->info.i_update |= INPUT_UPDATE_TITLE;
293             demux->info.i_title = track_id;
294             sys->track_id = track_id;
295             return VLC_SUCCESS;
296         }
297     }
298
299     return VLC_EGENERIC;
300 }