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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
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" );
60 /*****************************************************************************
62 *****************************************************************************/
73 static const char* type_str[] =
75 "NSF (Nes)", "GBS (Gameboy)", "VGM (Master System/Game Gear/Genesis)", "SPC (Super Nes)", "GYM (Genesis)"
90 Music_Emu *p_musicemu;
91 Emu_Mem_Reader *p_reader;
95 static int Demux ( demux_t *p_demux );
96 static int Control( demux_t *p_demux, int i_query, va_list args );
99 static void inflate_gzbuf(uint8_t * p_buffer, size_t i_size, uint8_t ** pp_obuffer, size_t * pi_osize);
102 static const char* gme_ext[] =
104 "nsf", "nsfe", "gbs", "vgm", "vgz", "spc", "gym", NULL
107 /*****************************************************************************
109 *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
112 demux_t *p_demux = (demux_t*)p_this;
118 /* We accept file based on extention match */
119 if( strcasecmp( p_demux->psz_demux, "gme" ) )
121 if( ( ext = strrchr( p_demux->psz_path, '.' ) ) == NULL ||
122 stream_Size( p_demux->s ) == 0 ) return VLC_EGENERIC;
125 for( i = 0; gme_ext[i] != NULL; i++ )
127 if( !strcasecmp( ext, gme_ext[i] ) )
132 if( gme_ext[i] == NULL ) return VLC_EGENERIC;
133 msg_Dbg( p_demux, "running GME demuxer (ext=%s)", gme_ext[i] );
137 if (i == 4) /* gzipped vgm */
139 msg_Dbg( p_demux, "zlib unvailable, unable to read gzipped vgz file" );
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 ) );
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 )
155 msg_Err( p_demux, "failed to read the complete file" );
156 free( p_sys->p_data );
161 /* Prepare emulator */
164 if (i == 4) /* gzipped vgm */
166 uint8_t * p_outbuffer;
169 inflate_gzbuf( p_sys->p_data, p_sys->i_data, &p_outbuffer, &i_outsize );
171 if (p_outbuffer == NULL)
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 );
183 p_sys->p_data = p_outbuffer;
184 p_sys->i_data = i_outsize;
188 p_sys->p_reader = new Emu_Mem_Reader( p_sys->p_data, p_sys->i_data );
194 p_sys->i_type = EMU_NSF;
197 p_sys->i_type = EMU_GBS;
201 p_sys->i_type = EMU_VGM;
204 p_sys->i_type = EMU_SPC;
207 p_sys->i_type = EMU_GYM;
211 /* Emulator specific initialization */
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) );
221 p_sys->p_meta = vlc_meta_New();
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 );
228 SET_META( VLC_META_CODEC_NAME, type_str[p_sys->i_type])
230 const char * p_error;
232 switch(p_sys->i_type)
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();
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();
263 p_sys->i_tracks = p_emu->track_count();
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();
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();
291 if( p_error != NULL )
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 );
303 p_sys->i_length = 314 * (int64_t)1000;
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 );
308 p_sys->p_musicemu->start_track( 0 );
310 #ifdef WORDS_BIGENDIAN
311 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
313 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
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 );
323 /*****************************************************************************
325 *****************************************************************************/
326 static void Close( vlc_object_t *p_this )
328 demux_t *p_demux = (demux_t*)p_this;
329 demux_sys_t *p_sys = p_demux->p_sys;
331 delete p_sys->p_musicemu;
332 delete p_sys->p_reader;
334 free( p_sys->p_data );
339 /*****************************************************************************
341 *****************************************************************************/
342 static int Demux( demux_t *p_demux )
344 demux_sys_t *p_sys = p_demux->p_sys;
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];
353 p_frame = block_New( p_demux, i_buf );
355 p_sys->p_musicemu->play( i_emubuf, p_emubuf );
358 if( p_error != NULL )
360 msg_Dbg( p_demux, "stop playing : %s", p_error );
361 block_Release( p_frame );
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];
370 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
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;
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 );
382 /*****************************************************************************
384 *****************************************************************************/
385 static int Control( demux_t *p_demux, int i_query, va_list args )
387 demux_sys_t *p_sys = p_demux->p_sys;
391 vlc_meta_t **pp_meta;
396 pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
398 *pp_meta = vlc_meta_Duplicate( p_sys->p_meta );
403 case DEMUX_GET_POSITION:
404 pf = (double*) va_arg( args, double* );
405 if( p_sys->i_length > 0 )
407 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
412 case DEMUX_SET_POSITION:
413 f = (double) va_arg( args, double );
415 i64 = f * p_sys->i_length;
416 if( i64 >= 0 && i64 <= p_sys->i_length )
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 );
427 pi64 = (int64_t*)va_arg( args, int64_t * );
428 *pi64 = p_sys->i_time;
431 case DEMUX_GET_LENGTH:
432 pi64 = (int64_t*)va_arg( args, int64_t * );
433 *pi64 = p_sys->i_length;
437 i64 = (int64_t)va_arg( args, int64_t );
439 if( i64 >= 0 && i64 <= p_sys->i_length )
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 );
449 case DEMUX_GET_TITLE_INFO:
450 if( p_sys->i_tracks > 1 )
452 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
453 int *pi_int = (int*)va_arg( args, int* );
455 *pi_int = p_sys->i_tracks;
456 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->i_tracks );
458 for( int i = 0; i < p_sys->i_tracks; i++ )
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);
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);
479 case DEMUX_GET_FPS: /* meaningless */
487 static void inflate_gzbuf(uint8_t * p_buffer, size_t i_size, uint8_t ** pp_obuffer, size_t * pi_osize)
491 size_t offset, out_size;
492 uint8_t * out_buffer;
494 (*pp_obuffer) = NULL;
497 memset(&z_str, 0, sizeof(z_str));
499 out_size = i_size * 2;
500 out_buffer = (uint8_t*)malloc(out_size);
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;
507 if ((err = inflateInit2(&z_str, 31)) != Z_OK) /* gzip format */
513 while ((err = inflate(&z_str, Z_FINISH)) != Z_STREAM_END)
520 offset = z_str.next_out - out_buffer;
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;
533 (*pi_osize) = out_size - z_str.avail_out;
537 out_buffer = (uint8_t *)realloc(out_buffer, *pi_osize);
538 (*pp_obuffer) = out_buffer;