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