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