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