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