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