1 /*****************************************************************************
2 * gme.cpp: Game Music files demuxer (using Game_Music_Emu)
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Jean Sreng <fox@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
49 set_shortname( "GME");
50 set_description( _("GME demuxer (Game_Music_Emu)" ) );
51 set_capability( "demux2", 10 );
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_DEMUX );
54 set_callbacks( Open, Close );
55 add_shortcut( "gme" );
58 /*****************************************************************************
60 *****************************************************************************/
71 static const char* type_str[] =
73 "NSF (Nes)", "GBS (Gameboy)", "VGM (Master System/Game Gear/Genesis)", "SPC (Super Nes)", "GYM (Genesis)"
88 Music_Emu *p_musicemu;
89 Emu_Mem_Reader *p_reader;
93 static int Demux ( demux_t *p_demux );
94 static int Control( demux_t *p_demux, int i_query, va_list args );
97 static void inflate_gzbuf(uint8_t * p_buffer, size_t i_size, uint8_t ** pp_obuffer, size_t * pi_osize);
100 static const char* gme_ext[] =
102 "nsf", "nsfe", "gbs", "vgm", "vgz", "spc", "gym", NULL
105 /*****************************************************************************
107 *****************************************************************************/
108 static int Open( vlc_object_t *p_this )
110 demux_t *p_demux = (demux_t*)p_this;
116 /* We accept file based on extention match */
117 if( strcasecmp( p_demux->psz_demux, "gme" ) )
119 if( ( ext = strrchr( p_demux->psz_path, '.' ) ) == NULL ||
120 stream_Size( p_demux->s ) == 0 ) return VLC_EGENERIC;
123 for( i = 0; gme_ext[i] != NULL; i++ )
125 if( !strcasecmp( ext, gme_ext[i] ) )
130 if( gme_ext[i] == NULL ) return VLC_EGENERIC;
131 msg_Dbg( p_demux, "running GME demuxer (ext=%s)", gme_ext[i] );
135 if (i == 4) /* gzipped vgm */
137 msg_Dbg( p_demux, "zlib unvailable, unable to read gzipped vgz file" );
142 /* Fill p_demux field */
143 p_demux->pf_demux = Demux;
144 p_demux->pf_control = Control;
145 p_demux->p_sys = p_sys = (demux_sys_t *)malloc( sizeof( demux_sys_t ) );
147 msg_Dbg( p_demux, "loading complete file (could be long)" );
148 p_sys->i_data = stream_Size( p_demux->s );
149 p_sys->p_data = (uint8_t *)malloc( p_sys->i_data );
150 p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
151 if( p_sys->i_data <= 0 )
153 msg_Err( p_demux, "failed to read the complete file" );
154 free( p_sys->p_data );
159 /* Prepare emulator */
162 if (i == 4) /* gzipped vgm */
164 uint8_t * p_outbuffer;
167 inflate_gzbuf( p_sys->p_data, p_sys->i_data, &p_outbuffer, &i_outsize );
169 if (p_outbuffer == NULL)
171 msg_Err( p_demux, "failed to understand the file : unable to inflate vgz file" );
172 /* we try to seek to recover for other plugin */
173 stream_Seek( p_demux->s, 0 );
174 free( p_sys->p_data );
181 p_sys->p_data = p_outbuffer;
182 p_sys->i_data = i_outsize;
186 p_sys->p_reader = new Emu_Mem_Reader( p_sys->p_data, p_sys->i_data );
192 p_sys->i_type = EMU_NSF;
195 p_sys->i_type = EMU_GBS;
199 p_sys->i_type = EMU_VGM;
202 p_sys->i_type = EMU_SPC;
205 p_sys->i_type = EMU_GYM;
209 /* Emulator specific initialization */
211 #define INIT_EMU(type) \
212 type##_Emu::header_t header; \
213 type##_Emu * p_emu = new type##_Emu; \
214 p_emu->init( 44100 ); \
215 p_sys->p_musicemu = p_emu; \
216 p_sys->p_reader->read( &header, sizeof(header) ); \
217 p_error = p_emu->load( header, *(p_sys->p_reader) );
219 p_sys->p_meta = vlc_meta_New();
223 /// \todo Reinstate meta codec name
224 //SET_META( VLC_META_CODEC_NAME, type_str[p_sys->i_type])
226 const char * p_error;
228 switch(p_sys->i_type)
235 vlc_meta_SetTitle( p_meta, header.game );
236 vlc_meta_SetArtist( p_meta, header.author );
237 vlc_meta_SetCopyright( p_meta, header.copyright );
238 p_sys->i_tracks = p_emu->track_count();
247 vlc_meta_SetTitle( p_meta, header.game );
248 vlc_meta_SetArtist( p_meta, header.author );
249 vlc_meta_SetCopyright( p_meta, header.copyright );
250 p_sys->i_tracks = p_emu->track_count();
259 p_sys->i_tracks = p_emu->track_count();
268 snprintf( psz_temp, 511, "%s (%s)", header.song, header.game );
269 vlc_meta_SetTitle( p_meta, psz_temp );
270 vlc_meta_SetArtist( p_meta, header.author );
271 p_sys->i_tracks = p_emu->track_count();
280 snprintf( psz_temp, 511, "%s (%s)", header.song, header.game );
281 vlc_meta_SetTitle( p_meta, psz_temp );
282 vlc_meta_SetCopyright( p_meta, header.copyright );
283 p_sys->i_tracks = p_emu->track_count();
289 if( p_error != NULL )
291 msg_Err( p_demux, "failed to understand the file : %s", p_error );
292 /* we try to seek to recover for other plugin */
293 stream_Seek( p_demux->s, 0 );
294 free( p_sys->p_data );
301 p_sys->i_length = 314 * (int64_t)1000;
303 msg_Dbg( p_demux, "GME loaded type=%s title=%s tracks=%i", type_str[p_sys->i_type],
304 vlc_meta_GetValue( p_sys->p_meta, VLC_META_TITLE ), p_sys->i_tracks );
306 p_sys->p_musicemu->start_track( 0 );
308 #ifdef WORDS_BIGENDIAN
309 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
311 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
313 p_sys->fmt.audio.i_rate = 44100;
314 p_sys->fmt.audio.i_channels = 2;
315 p_sys->fmt.audio.i_bitspersample = 16;
316 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
321 /*****************************************************************************
323 *****************************************************************************/
324 static void Close( vlc_object_t *p_this )
326 demux_t *p_demux = (demux_t*)p_this;
327 demux_sys_t *p_sys = p_demux->p_sys;
329 delete p_sys->p_musicemu;
330 delete p_sys->p_reader;
332 free( p_sys->p_data );
337 /*****************************************************************************
339 *****************************************************************************/
340 static int Demux( demux_t *p_demux )
342 demux_sys_t *p_sys = p_demux->p_sys;
344 int i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
345 p_sys->fmt.audio.i_channels;
346 const unsigned int i_buf = p_sys->fmt.audio.i_rate / 10 * i_bk;
347 const unsigned int i_emubuf = i_buf / sizeof(Music_Emu::sample_t);
348 const char * p_error;
349 Music_Emu::sample_t p_emubuf [i_emubuf];
351 p_frame = block_New( p_demux, i_buf );
353 p_sys->p_musicemu->play( i_emubuf, p_emubuf );
356 if( p_error != NULL )
358 msg_Dbg( p_demux, "stop playing : %s", p_error );
359 block_Release( p_frame );
364 /* Copy emulator output to frame */
365 for (int i = 0; i<i_buf; i++) p_frame->p_buffer[i] = ((uint8_t *)p_emubuf)[i];
368 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
370 /* We should use p_frame->i_buffer */
371 p_sys->i_time += (int64_t)1000000 * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
374 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
375 es_out_Send( p_demux->out, p_sys->es, p_frame );
380 /*****************************************************************************
382 *****************************************************************************/
383 static int Control( demux_t *p_demux, int i_query, va_list args )
385 demux_sys_t *p_sys = p_demux->p_sys;
389 vlc_meta_t **pp_meta;
394 pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
396 *pp_meta = vlc_meta_Duplicate( p_sys->p_meta );
401 case DEMUX_GET_POSITION:
402 pf = (double*) va_arg( args, double* );
403 if( p_sys->i_length > 0 )
405 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
410 case DEMUX_SET_POSITION:
411 f = (double) va_arg( args, double );
413 i64 = f * p_sys->i_length;
414 if( i64 >= 0 && i64 <= p_sys->i_length )
416 ModPlug_Seek( p_sys->f, i64 / 1000 );
417 p_sys->i_time = i64 + 1;
418 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
425 pi64 = (int64_t*)va_arg( args, int64_t * );
426 *pi64 = p_sys->i_time;
429 case DEMUX_GET_LENGTH:
430 pi64 = (int64_t*)va_arg( args, int64_t * );
431 *pi64 = p_sys->i_length;
435 i64 = (int64_t)va_arg( args, int64_t );
437 if( i64 >= 0 && i64 <= p_sys->i_length )
439 ModPlug_Seek( p_sys->f, i64 / 1000 );
440 p_sys->i_time = i64 + 1;
441 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
447 case DEMUX_GET_TITLE_INFO:
448 if( p_sys->i_tracks > 1 )
450 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
451 int *pi_int = (int*)va_arg( args, int* );
453 *pi_int = p_sys->i_tracks;
454 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->i_tracks );
456 for( int i = 0; i < p_sys->i_tracks; i++ )
459 snprintf(psz_temp, 15, "Track %i", i);
460 (*ppp_title)[i] = vlc_input_title_New();
461 (*ppp_title)[i]->psz_name = strdup(psz_temp);
469 case DEMUX_SET_TITLE:
470 i_idx = (int)va_arg( args, int );
471 p_sys->p_musicemu->start_track( i_idx );
472 p_demux->info.i_title = i_idx;
473 p_demux->info.i_update = INPUT_UPDATE_TITLE;
474 msg_Dbg( p_demux, "set title %i", i_idx);
477 case DEMUX_GET_FPS: /* meaningless */
485 static void inflate_gzbuf(uint8_t * p_buffer, size_t i_size, uint8_t ** pp_obuffer, size_t * pi_osize)
489 size_t offset, out_size;
490 uint8_t * out_buffer;
492 (*pp_obuffer) = NULL;
495 memset(&z_str, 0, sizeof(z_str));
497 out_size = i_size * 2;
498 out_buffer = (uint8_t*)malloc(out_size);
500 z_str.next_in = (unsigned char*)p_buffer;
501 z_str.avail_in = i_size;
502 z_str.next_out = out_buffer;
503 z_str.avail_out = out_size;
505 if ((err = inflateInit2(&z_str, 31)) != Z_OK) /* gzip format */
511 while ((err = inflate(&z_str, Z_FINISH)) != Z_STREAM_END)
518 offset = z_str.next_out - out_buffer;
520 out_buffer = (uint8_t *)realloc(out_buffer, out_size);
521 z_str.next_out = out_buffer + offset;
522 z_str.avail_out = out_size - offset;
531 (*pi_osize) = out_size - z_str.avail_out;
535 out_buffer = (uint8_t *)realloc(out_buffer, *pi_osize);
536 (*pp_obuffer) = out_buffer;