menu: generalize credits code to show any message
[picodrive.git] / platform / common / mp3_helix.c
CommitLineData
da42200b 1// Some mp3 related code for Sega/Mega CD.
2// Uses the Helix Fixed-point MP3 decoder
3
4// (c) Copyright 2007, Grazvydas "notaz" Ignotas
5
6#include <stdio.h>
7#include <string.h>
8
7c34867a 9#include <pico/pico_int.h>
10#include <pico/sound/mix.h>
da42200b 11#include "helix/pub/mp3dec.h"
7c34867a 12#include "mp3.h"
da42200b 13#include "lprintf.h"
14
da42200b 15static HMP3Decoder mp3dec = 0;
7c34867a 16static FILE *mp3_current_file = NULL;
17static int mp3_file_len = 0, mp3_file_pos = 0;
da42200b 18static int mp3_buffer_offs = 0;
7c34867a 19static unsigned char mp3_input_buffer[2*1024];
da42200b 20
7c34867a 21#ifdef __GP2X__
22#define mp3_update mp3_update_local
23#define mp3_start_play mp3_start_play_local
24#endif
da42200b 25
26static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
27{
28 int ret, offs1, offs = 0;
29
30 while (1)
31 {
7c18e34a 32 offs1 = mp3_find_sync_word(buff + offs, 2048 - offs);
da42200b 33 if (offs1 < 0) return -2;
34 offs += offs1;
35 if (2048 - offs < 4) return -3;
36
37 // printf("trying header %08x\n", *(int *)(buff + offs));
38
39 ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
40 if (ret == 0 && fi->bitrate != 0) break;
41 offs++;
42 }
43
44 return ret;
45}
46
7c18e34a 47int mp3_get_bitrate(void *f_, int len)
da42200b 48{
49 unsigned char buff[2048];
50 MP3FrameInfo fi;
7c18e34a 51 FILE *f = f_;
da42200b 52 int ret;
53
7c18e34a 54 memset(buff, 0, sizeof(buff));
da42200b 55
7c18e34a 56 if (mp3dec)
57 MP3FreeDecoder(mp3dec);
091facb8 58 mp3dec = MP3InitDecoder();
da42200b 59
60 fseek(f, 0, SEEK_SET);
7c18e34a 61 ret = fread(buff, 1, sizeof(buff), f);
da42200b 62 fseek(f, 0, SEEK_SET);
7c18e34a 63 if (ret <= 0)
64 return -1;
da42200b 65
66 ret = try_get_header(buff, &fi);
67 if (ret != 0 || fi.bitrate == 0) {
68 // try to read somewhere around the middle
69 fseek(f, len>>1, SEEK_SET);
70 fread(buff, 1, 2048, f);
71 fseek(f, 0, SEEK_SET);
72 ret = try_get_header(buff, &fi);
73 }
7c18e34a 74 if (ret != 0)
75 return ret;
da42200b 76
77 // printf("bitrate: %i\n", fi.bitrate / 1000);
78
79 return fi.bitrate / 1000;
80}
81
da42200b 82static int mp3_decode(void)
83{
84 unsigned char *readPtr;
85 int bytesLeft;
86 int offset; // mp3 frame offset from readPtr
7c34867a 87 int had_err;
88 int err = 0;
da42200b 89
90 do
91 {
7c34867a 92 if (mp3_file_pos >= mp3_file_len)
93 return 1; /* EOF, nothing to do */
da42200b 94
95 fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
96 bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
97
7c18e34a 98 offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft);
da42200b 99 if (offset < 0) {
7c34867a 100 lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
da42200b 101 mp3_file_pos = mp3_file_len;
102 return 1; // EOF
103 }
104 readPtr = mp3_input_buffer + offset;
105 bytesLeft -= offset;
106
7c34867a 107 had_err = err;
c9e1affc 108 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
da42200b 109 if (err) {
7c34867a 110 if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
111 // just need another frame
112 mp3_file_pos += readPtr - mp3_input_buffer;
113 continue;
114 }
115 if (err == ERR_MP3_INDATA_UNDERFLOW && !had_err) {
da42200b 116 if (offset == 0)
117 // something's really wrong here, frame had to fit
118 mp3_file_pos = mp3_file_len;
119 else
120 mp3_file_pos += offset;
121 continue;
7c34867a 122 }
123 if (-12 <= err && err <= -6) {
da42200b 124 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
125 // just try to skip the offending frame..
126 mp3_file_pos += offset + 1;
127 continue;
128 }
7c34867a 129 lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
da42200b 130 mp3_file_pos = mp3_file_len;
131 return 1;
132 }
133 mp3_file_pos += readPtr - mp3_input_buffer;
134 }
135 while (0);
136
137 return 0;
138}
139
7c18e34a 140void mp3_start_play(void *f_, int pos)
da42200b 141{
7c18e34a 142 FILE *f = f_;
143
da42200b 144 mp3_file_len = mp3_file_pos = 0;
145 mp3_current_file = NULL;
146 mp3_buffer_offs = 0;
147
7c34867a 148 if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
da42200b 149 return;
150
7c34867a 151 // must re-init decoder for new track
152 if (mp3dec)
153 MP3FreeDecoder(mp3dec);
154 mp3dec = MP3InitDecoder();
da42200b 155
156 mp3_current_file = f;
157 fseek(f, 0, SEEK_END);
158 mp3_file_len = ftell(f);
159
7c18e34a 160 // search for first sync word, skipping stuff like ID3 tags
161 while (mp3_file_pos < 128*1024) {
162 int offs, bytes;
163
164 fseek(f, mp3_file_pos, SEEK_SET);
165 bytes = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f);
166 if (bytes < 4)
167 break;
168 offs = mp3_find_sync_word(mp3_input_buffer, bytes);
169 if (offs >= 0) {
170 mp3_file_pos += offs;
171 break;
172 }
173 mp3_file_pos += bytes - 2;
174 }
175
da42200b 176 // seek..
177 if (pos) {
7c18e34a 178 unsigned long long pos64 = mp3_file_len - mp3_file_pos;
7c34867a 179 pos64 *= pos;
7c18e34a 180 mp3_file_pos += pos64 >> 10;
da42200b 181 }
182
183 mp3_decode();
184}
185
da42200b 186void mp3_update(int *buffer, int length, int stereo)
187{
188 int length_mp3, shr = 0;
189 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
190
7c34867a 191 if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len)
192 return; /* no file / EOF */
da42200b 193
194 length_mp3 = length;
02da059d 195 if (PsndRate <= 11025 + 100) {
196 mix_samples = mix_16h_to_32_s2;
197 length_mp3 <<= 2; shr = 2;
198 }
199 else if (PsndRate <= 22050 + 100) {
200 mix_samples = mix_16h_to_32_s1;
201 length_mp3 <<= 1; shr = 1;
202 }
da42200b 203
204 if (1152 - mp3_buffer_offs >= length_mp3) {
c9e1affc 205 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
da42200b 206
207 mp3_buffer_offs += length_mp3;
208 } else {
209 int ret, left = 1152 - mp3_buffer_offs;
210
c9e1affc 211 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
da42200b 212 ret = mp3_decode();
213 if (ret == 0) {
214 mp3_buffer_offs = length_mp3 - left;
c9e1affc 215 mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);
da42200b 216 } else
217 mp3_buffer_offs = 0;
218 }
219}
220