Dircache: Fix memory leak (and recently panic).
[maemo-rb.git] / apps / codecs / vgm.c
blob9c8619c7b84e34f9bdd4a08f1357070be2bb161a
2 /* Ripped off from Game_Music_Emu 0.5.2. http://www.slack.net/~ant/ */
3 /* Inflate code taken from WikiViewer plugin by Adam Gashlin */
5 #include <codecs/lib/codeclib.h>
7 #include "libgme/blargg_endian.h"
8 #include "libgme/vgm_emu.h"
9 #include "libgme/inflate/mallocer.h"
10 #include "libgme/inflate/inflate.h"
12 CODEC_HEADER
14 /* Maximum number of bytes to process in one iteration */
15 #define CHUNK_SIZE (1024*4)
16 #define MAINMEMBUF 0
18 static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
19 static struct Vgm_Emu vgm_emu;
21 static void *inflatebuf; /* heap for gunzip */
22 static char *songbuf; /* destination for uncompressed song */
23 static uint32_t songbuflen=0; /* size of the song buffer */
24 static uint32_t songlen=0; /* used size of the song buffer */
26 /****************** rockbox interface ******************/
28 /* this is the codec entry point */
29 enum codec_status codec_main(enum codec_entry_call_reason reason)
31 if (reason == CODEC_LOAD) {
32 /* we only render 16 bits */
33 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
35 /* 32 Khz, Interleaved stereo */
36 ci->configure(DSP_SET_FREQUENCY, 44100);
37 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
39 Vgm_init(&vgm_emu);
40 Vgm_set_sample_rate(&vgm_emu, 44100);
43 return CODEC_OK;
46 /* this is called for each file to process */
47 enum codec_status codec_run(void)
49 blargg_err_t err;
50 uint8_t *buf;
51 size_t n;
52 intptr_t param;
54 uint32_t elapsed_time = 0;
56 DEBUGF("VGM: next_track\n");
57 if (codec_init()) {
58 return CODEC_ERROR;
61 codec_set_replaygain(ci->id3);
63 /* Read the entire file */
64 DEBUGF("VGM: request file\n");
65 ci->seek_buffer(0);
66 buf = ci->request_buffer(&n, ci->filesize);
67 if (!buf) {
68 DEBUGF("VGM: file load failed\n");
69 return CODEC_ERROR;
72 /* If couldn't get the whole buffer
73 will trim file and put and 'end_command'
74 at the end*/
75 if (n < (size_t)ci->filesize) {
76 DEBUGF("VGM: file was trimmed\n");
79 /* If is gzipped decompress it */
80 if ( get_le16( buf ) == 0x8b1f ) {
81 wpw_init_mempool(MAINMEMBUF);
82 inflatebuf=wpw_malloc(MAINMEMBUF,0x13500);
84 /* Will use available remaining memory
85 as output buffer */
86 songbuflen=wpw_available(MAINMEMBUF);
87 songbuf=wpw_malloc(MAINMEMBUF,songbuflen);
89 songlen=decompress(buf,n,songbuf,songbuflen,0,inflatebuf);
91 if ((err = Vgm_load_mem(&vgm_emu, songbuf, songlen, true))) {
92 DEBUGF("VGM: Vgm_load_mem failed (%s)\n", err);
93 return CODEC_ERROR;
96 /* Since metadata parser doesn't support VGZ
97 will set song length here */
98 ci->id3->length = Track_get_length( &vgm_emu );
100 else if ((err = Vgm_load_mem(&vgm_emu, buf, n, false))) {
101 DEBUGF("VGM: Vgm_load failed_mem (%s)\n", err);
102 return CODEC_ERROR;
105 Vgm_start_track(&vgm_emu);
107 /* for REPEAT_ONE we disable track limits */
108 if (ci->global_settings->repeat_mode != REPEAT_ONE) {
109 Track_set_fade(&vgm_emu, ci->id3->length - 4000, 4000);
112 ci->set_elapsed(0);
114 /* The main decoder loop */
115 while (1) {
116 enum codec_command_action action = ci->get_command(&param);
118 if (action == CODEC_ACTION_HALT)
119 break;
121 if (action == CODEC_ACTION_SEEK_TIME) {
122 ci->set_elapsed(param);
123 elapsed_time = param;
124 Track_seek(&vgm_emu, param);
125 ci->seek_complete();
127 /* Set fade again in case we seek to start of song */
128 Track_set_fade(&vgm_emu, ci->id3->length - 4000, 4000);
131 /* Generate audio buffer */
132 err = Vgm_play(&vgm_emu, CHUNK_SIZE, samples);
133 if (err || vgm_emu.track_ended) break;
135 ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1);
137 elapsed_time += (CHUNK_SIZE / 2) / 44.1;
138 ci->set_elapsed(elapsed_time);
141 return CODEC_OK;