]> git.sesse.net Git - vlc/blob - modules/demux/gme.cpp
First rough demux2 implementation for game music files using Game_Music_Emu
[vlc] / modules / demux / gme.cpp
1 /*****************************************************************************
2  * gme.cpp: Game Music files demuxer (using Game_Music_Emu)
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean Sreng <fox@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "Nsf_Emu.h"
33 #include "Gbs_Emu.h"
34 #include "Vgm_Emu.h"
35 #include "Spc_Emu.h"
36 #include "Gym_Emu.h"
37
38 #ifdef HAVE_ZLIB_H
39     #include "zlib.h"
40 #endif
41
42 using namespace std;
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open    ( vlc_object_t * );
48 static void Close  ( vlc_object_t * );
49
50 vlc_module_begin();
51     set_shortname( "GME");
52     set_description( _("GME demuxer (Game_Music_Emu)" ) );
53     set_capability( "demux2", 10 );
54     set_category( CAT_INPUT );
55     set_subcategory( SUBCAT_INPUT_DEMUX );
56     set_callbacks( Open, Close );
57     add_shortcut( "gme" );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63
64 enum EmuType_e
65 {
66     EMU_NSF     = 0,
67     EMU_GBS     = 1,
68     EMU_VGM     = 2,
69     EMU_SPC     = 3,
70     EMU_GYM     = 4
71 };
72
73 static const char* type_str[] =
74 {
75     "NSF (Nes)", "GBS (Gameboy)", "VGM (Master System/Game Gear/Genesis)", "SPC (Super Nes)", "GYM (Genesis)"
76 };
77
78 struct demux_sys_t
79 {
80     es_format_t       fmt;
81     es_out_id_t      *es;
82
83     int64_t           i_time;
84     int64_t           i_length;
85
86     int               i_data;
87     uint8_t          *p_data;
88     int               i_type;
89     int               i_tracks;
90     Music_Emu        *p_musicemu;
91     Emu_Mem_Reader   *p_reader;
92     vlc_meta_t       *p_meta;
93 };
94
95 static int Demux  ( demux_t *p_demux );
96 static int Control( demux_t *p_demux, int i_query, va_list args );
97
98 #ifdef HAVE_ZLIB_H
99 static void inflate_gzbuf(uint8_t * p_buffer, size_t i_size, uint8_t ** pp_obuffer, size_t * pi_osize);
100 #endif
101
102 static const char* gme_ext[] =
103 {
104     "nsf", "nsfe", "gbs", "vgm", "vgz", "spc", "gym", NULL
105 };
106
107 /*****************************************************************************
108  * Open
109  *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
111 {
112     demux_t     *p_demux = (demux_t*)p_this;
113     demux_sys_t *p_sys;
114     char        *ext;
115     int         i;
116     vlc_value_t val;
117     
118     /* We accept file based on extention match */
119     if( strcasecmp( p_demux->psz_demux, "gme" ) )
120     {
121         if( ( ext = strrchr( p_demux->psz_path, '.' ) ) == NULL ||
122             stream_Size( p_demux->s ) == 0 ) return VLC_EGENERIC;
123
124         ext++;  /* skip . */
125         for( i = 0; gme_ext[i] != NULL; i++ )
126         {
127             if( !strcasecmp( ext, gme_ext[i] ) )
128             {
129                 break;
130             }
131         }
132         if( gme_ext[i] == NULL ) return VLC_EGENERIC;
133         msg_Dbg( p_demux, "running GME demuxer (ext=%s)", gme_ext[i] );
134     }
135
136 #ifndef HAVE_ZLIB_H
137     if (i == 4) /* gzipped vgm */
138     {
139         msg_Dbg( p_demux, "zlib unvailable, unable to read gzipped vgz file" );
140         return VLC_EGENERIC;
141     }
142 #endif
143
144     /* Fill p_demux field */
145     p_demux->pf_demux = Demux;
146     p_demux->pf_control = Control;
147     p_demux->p_sys = p_sys = (demux_sys_t *)malloc( sizeof( demux_sys_t ) );
148
149     msg_Dbg( p_demux, "loading complete file (could be long)" );
150     p_sys->i_data = stream_Size( p_demux->s );
151     p_sys->p_data = (uint8_t *)malloc( p_sys->i_data );
152     p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
153     if( p_sys->i_data <= 0 )
154     {
155         msg_Err( p_demux, "failed to read the complete file" );
156         free( p_sys->p_data );
157         free( p_sys );
158         return VLC_EGENERIC;
159     }
160
161     /* Prepare emulator */
162    
163 #ifdef HAVE_ZLIB_H
164     if (i == 4) /* gzipped vgm */
165     {
166         uint8_t * p_outbuffer;
167         size_t i_outsize;
168
169         inflate_gzbuf( p_sys->p_data, p_sys->i_data, &p_outbuffer, &i_outsize );
170         
171         if (p_outbuffer == NULL)
172         {
173             msg_Err( p_demux, "failed to understand the file : unable to inflate vgz file" );
174             /* we try to seek to recover for other plugin */
175             stream_Seek( p_demux->s, 0 );
176             free( p_sys->p_data );
177             free( p_sys );
178             return VLC_EGENERIC;
179         }
180
181         free(p_sys->p_data);
182
183         p_sys->p_data = p_outbuffer;
184         p_sys->i_data = i_outsize;
185     }    
186 #endif
187
188     p_sys->p_reader = new Emu_Mem_Reader( p_sys->p_data, p_sys->i_data ); 
189     
190     switch(i)
191     {
192         case 0:
193         case 1:
194             p_sys->i_type = EMU_NSF;
195             break;
196         case 2:
197             p_sys->i_type = EMU_GBS;
198             break;
199         case 3:
200         case 4:
201             p_sys->i_type = EMU_VGM;
202             break;
203         case 5:
204             p_sys->i_type = EMU_SPC;
205             break;
206         case 6:
207             p_sys->i_type = EMU_GYM;
208             break;
209     }
210
211     /* Emulator specific initialization */
212
213 #define INIT_EMU(type) \
214         type##_Emu::header_t header; \
215         type##_Emu * p_emu = new type##_Emu; \
216         p_emu->init( 44100 ); \
217         p_sys->p_musicemu = p_emu; \
218         p_sys->p_reader->read( &header, sizeof(header) ); \
219         p_error = p_emu->load( header, *(p_sys->p_reader) ); 
220
221     p_sys->p_meta = vlc_meta_New();
222
223     char psz_temp[512];
224 #define SET_META(vlc_meta, meta_info...) \
225     snprintf( psz_temp, 511, meta_info ); \
226     vlc_meta_Add( p_sys->p_meta, vlc_meta, psz_temp );
227
228     SET_META( VLC_META_CODEC_NAME, type_str[p_sys->i_type])
229     
230     const char * p_error;
231
232     switch(p_sys->i_type)
233     {
234         case EMU_NSF:
235         {
236             INIT_EMU(Nsf)
237             if (p_error == NULL)
238             {
239                 SET_META( VLC_META_TITLE, "%s", header.game )
240                 SET_META( VLC_META_AUTHOR, "%s", header.author )
241                 SET_META( VLC_META_COPYRIGHT, "%s", header.copyright )
242                 p_sys->i_tracks = p_emu->track_count();
243             }
244         }
245         break;
246         case EMU_GBS:
247         {
248             INIT_EMU(Gbs)
249             if (p_error == NULL)
250             {
251                 SET_META( VLC_META_TITLE, "%s", header.game )
252                 SET_META( VLC_META_AUTHOR, "%s", header.author )
253                 SET_META( VLC_META_COPYRIGHT, "%s", header.copyright )
254                 p_sys->i_tracks = p_emu->track_count();
255             }
256         }
257         break;
258         case EMU_VGM:
259         {
260             INIT_EMU(Vgm)
261             if (p_error == NULL)
262             {
263                 p_sys->i_tracks = p_emu->track_count();
264             }
265        }
266         break;
267         case EMU_SPC:
268         {
269             INIT_EMU(Spc)
270             if (p_error == NULL)
271             {
272                 SET_META( VLC_META_TITLE, "%s (%s)", header.song, header.game )
273                 SET_META( VLC_META_AUTHOR, "%s", header.author )
274                 p_sys->i_tracks = p_emu->track_count();
275             }
276       }
277         break;
278         case EMU_GYM:
279         {
280             INIT_EMU(Gym)
281             if (p_error == NULL)
282             {
283                 SET_META( VLC_META_TITLE, "%s (%s)", header.song, header.game )
284                 SET_META( VLC_META_COPYRIGHT, "%s", header.copyright )
285                 p_sys->i_tracks = p_emu->track_count();
286             }
287      }
288         break;
289     } 
290
291     if( p_error != NULL )
292     {
293         msg_Err( p_demux, "failed to understand the file : %s", p_error );
294         /* we try to seek to recover for other plugin */
295         stream_Seek( p_demux->s, 0 );
296         free( p_sys->p_data );
297         free( p_sys );
298         return VLC_EGENERIC;
299     }
300
301    /* init time */
302     p_sys->i_time  = 1;
303     p_sys->i_length = 314 * (int64_t)1000;
304
305     msg_Dbg( p_demux, "GME loaded type=%s title=%s tracks=%i", type_str[p_sys->i_type],
306               vlc_meta_GetValue( p_sys->p_meta, VLC_META_TITLE ), p_sys->i_tracks );
307
308     p_sys->p_musicemu->start_track( 0 );
309
310 #ifdef WORDS_BIGENDIAN
311     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
312 #else
313     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
314 #endif
315     p_sys->fmt.audio.i_rate = 44100;
316     p_sys->fmt.audio.i_channels = 2;
317     p_sys->fmt.audio.i_bitspersample = 16;
318     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
319     
320     return VLC_SUCCESS;
321 }
322
323 /*****************************************************************************
324  * Close
325  *****************************************************************************/
326 static void Close( vlc_object_t *p_this )
327 {
328     demux_t     *p_demux = (demux_t*)p_this;
329     demux_sys_t *p_sys = p_demux->p_sys;
330
331     delete p_sys->p_musicemu;
332     delete p_sys->p_reader;
333
334     free( p_sys->p_data );
335     free( p_sys );
336 }
337
338
339 /*****************************************************************************
340  * Demux:
341  *****************************************************************************/
342 static int Demux( demux_t *p_demux )
343 {
344     demux_sys_t *p_sys = p_demux->p_sys;
345     block_t     *p_frame;
346     int         i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
347                        p_sys->fmt.audio.i_channels;
348     const unsigned int i_buf = p_sys->fmt.audio.i_rate / 10 * i_bk;
349     const unsigned int i_emubuf = i_buf / sizeof(Music_Emu::sample_t);
350     const char * p_error;
351     Music_Emu::sample_t p_emubuf [i_emubuf];
352
353     p_frame = block_New( p_demux, i_buf );
354
355     p_sys->p_musicemu->play( i_emubuf, p_emubuf );
356     
357     /*
358     if( p_error != NULL )
359     {
360         msg_Dbg( p_demux, "stop playing : %s", p_error );
361         block_Release( p_frame );
362         return 0;
363     }
364     */
365
366     /* Copy emulator output to frame */
367     for (int i = 0; i<i_buf; i++) p_frame->p_buffer[i] = ((uint8_t *)p_emubuf)[i];
368
369     /* Set PCR */
370     es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
371
372     /* We should use p_frame->i_buffer */
373     p_sys->i_time += (int64_t)1000000 * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
374
375     /* Send data */
376     p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
377     es_out_Send( p_demux->out, p_sys->es, p_frame );
378
379     return 1;
380 }
381
382 /*****************************************************************************
383  * Control:
384  *****************************************************************************/
385 static int Control( demux_t *p_demux, int i_query, va_list args )
386 {
387     demux_sys_t *p_sys = p_demux->p_sys;
388     double f, *pf;
389     int64_t i64, *pi64;
390     int i_idx;
391     vlc_meta_t **pp_meta;
392
393 switch( i_query )
394     {
395         case DEMUX_GET_META:
396             pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
397             if( p_sys->p_meta )
398                 *pp_meta = vlc_meta_Duplicate( p_sys->p_meta );
399             else
400                 *pp_meta = NULL;
401             return VLC_SUCCESS;
402
403         case DEMUX_GET_POSITION:
404             pf = (double*) va_arg( args, double* );
405             if( p_sys->i_length > 0 )
406             {
407                 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
408                 return VLC_SUCCESS;
409             }
410             return VLC_EGENERIC;
411 /*
412         case DEMUX_SET_POSITION:
413             f = (double) va_arg( args, double );
414
415             i64 = f * p_sys->i_length;
416             if( i64 >= 0 && i64 <= p_sys->i_length )
417             {
418                 ModPlug_Seek( p_sys->f, i64 / 1000 );
419                 p_sys->i_time = i64 + 1;
420                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
421
422                 return VLC_SUCCESS;
423             }
424             return VLC_EGENERIC;
425 */
426         case DEMUX_GET_TIME:
427             pi64 = (int64_t*)va_arg( args, int64_t * );
428             *pi64 = p_sys->i_time;
429             return VLC_SUCCESS;
430
431         case DEMUX_GET_LENGTH:
432             pi64 = (int64_t*)va_arg( args, int64_t * );
433             *pi64 = p_sys->i_length;
434             return VLC_SUCCESS;
435 /*
436         case DEMUX_SET_TIME:
437             i64 = (int64_t)va_arg( args, int64_t );
438
439             if( i64 >= 0 && i64 <= p_sys->i_length )
440             {
441                 ModPlug_Seek( p_sys->f, i64 / 1000 );
442                 p_sys->i_time = i64 + 1;
443                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
444
445                 return VLC_SUCCESS;
446             }
447             return VLC_EGENERIC;
448 */
449         case DEMUX_GET_TITLE_INFO:
450             if( p_sys->i_tracks > 1 )
451             {
452                 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
453                 int *pi_int    = (int*)va_arg( args, int* );
454
455                 *pi_int = p_sys->i_tracks;
456                 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->i_tracks );
457
458                 for( int i = 0; i < p_sys->i_tracks; i++ )
459                 {
460                     char psz_temp[16];
461                     snprintf(psz_temp, 15, "Track %i", i);
462                     (*ppp_title)[i] = vlc_input_title_New();
463                     (*ppp_title)[i]->psz_name = strdup(psz_temp);
464                 }
465
466                 return VLC_SUCCESS;
467             }
468             return VLC_EGENERIC;
469
470
471         case DEMUX_SET_TITLE:
472             i_idx = (int)va_arg( args, int );
473             p_sys->p_musicemu->start_track( i_idx );    
474             p_demux->info.i_title = i_idx;
475             p_demux->info.i_update = INPUT_UPDATE_TITLE;
476             msg_Dbg( p_demux, "set title %i", i_idx);
477             return VLC_SUCCESS;
478
479         case DEMUX_GET_FPS: /* meaningless */
480         default:
481             return VLC_EGENERIC;
482     }
483
484 }
485
486 #ifdef HAVE_ZLIB_H
487 static void inflate_gzbuf(uint8_t * p_buffer, size_t i_size, uint8_t ** pp_obuffer, size_t * pi_osize)
488 {
489     z_stream z_str;
490     int err;
491     size_t offset, out_size;
492     uint8_t * out_buffer;
493
494     (*pp_obuffer) = NULL;
495     (*pi_osize) = 0;
496
497     memset(&z_str, 0, sizeof(z_str));
498
499     out_size = i_size * 2;
500     out_buffer = (uint8_t*)malloc(out_size);
501
502     z_str.next_in   = (unsigned char*)p_buffer;
503     z_str.avail_in  = i_size;
504     z_str.next_out  = out_buffer;
505     z_str.avail_out = out_size;
506
507     if ((err = inflateInit2(&z_str, 31)) != Z_OK) /* gzip format */
508     {
509         free(out_buffer);
510         return;
511     }
512
513     while ((err = inflate(&z_str, Z_FINISH)) != Z_STREAM_END)
514     {
515         switch(err)
516         {
517         case Z_OK:
518             break;
519         case Z_BUF_ERROR:
520             offset = z_str.next_out - out_buffer;
521             out_size *= 2;
522             out_buffer = (uint8_t *)realloc(out_buffer, out_size);
523             z_str.next_out  = out_buffer + offset;
524             z_str.avail_out = out_size - offset;
525             break;
526         default:
527             inflateEnd(&z_str);
528             free(out_buffer);
529             return;
530         }
531     }
532
533     (*pi_osize) = out_size - z_str.avail_out;
534
535     inflateEnd(&z_str);
536     
537     out_buffer = (uint8_t *)realloc(out_buffer, *pi_osize); 
538     (*pp_obuffer) = out_buffer;
539 }
540 #endif