11f4e722 |
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 | |
9615b3df |
9 | #include <pico/pico_int.h> |
10 | #include <pico/sound/mix.h> |
11f4e722 |
11 | #include "helix/pub/mp3dec.h" |
9615b3df |
12 | #include "mp3.h" |
11f4e722 |
13 | #include "lprintf.h" |
14 | |
11f4e722 |
15 | static HMP3Decoder mp3dec = 0; |
9615b3df |
16 | static FILE *mp3_current_file = NULL; |
17 | static int mp3_file_len = 0, mp3_file_pos = 0; |
11f4e722 |
18 | static int mp3_buffer_offs = 0; |
9615b3df |
19 | static unsigned char mp3_input_buffer[2*1024]; |
11f4e722 |
20 | |
9615b3df |
21 | #ifdef __GP2X__ |
22 | #define mp3_update mp3_update_local |
23 | #define mp3_start_play mp3_start_play_local |
24 | #endif |
11f4e722 |
25 | |
26 | static int try_get_header(unsigned char *buff, MP3FrameInfo *fi) |
27 | { |
28 | int ret, offs1, offs = 0; |
29 | |
30 | while (1) |
31 | { |
69dfeea1 |
32 | offs1 = mp3_find_sync_word(buff + offs, 2048 - offs); |
11f4e722 |
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 | |
69dfeea1 |
47 | int mp3_get_bitrate(void *f_, int len) |
11f4e722 |
48 | { |
49 | unsigned char buff[2048]; |
50 | MP3FrameInfo fi; |
69dfeea1 |
51 | FILE *f = f_; |
11f4e722 |
52 | int ret; |
53 | |
69dfeea1 |
54 | memset(buff, 0, sizeof(buff)); |
11f4e722 |
55 | |
69dfeea1 |
56 | if (mp3dec) |
57 | MP3FreeDecoder(mp3dec); |
6e507f76 |
58 | mp3dec = MP3InitDecoder(); |
11f4e722 |
59 | |
60 | fseek(f, 0, SEEK_SET); |
69dfeea1 |
61 | ret = fread(buff, 1, sizeof(buff), f); |
11f4e722 |
62 | fseek(f, 0, SEEK_SET); |
69dfeea1 |
63 | if (ret <= 0) |
64 | return -1; |
11f4e722 |
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 | } |
69dfeea1 |
74 | if (ret != 0) |
75 | return ret; |
11f4e722 |
76 | |
77 | // printf("bitrate: %i\n", fi.bitrate / 1000); |
78 | |
79 | return fi.bitrate / 1000; |
80 | } |
81 | |
11f4e722 |
82 | static int mp3_decode(void) |
83 | { |
84 | unsigned char *readPtr; |
85 | int bytesLeft; |
86 | int offset; // mp3 frame offset from readPtr |
9615b3df |
87 | int had_err; |
88 | int err = 0; |
11f4e722 |
89 | |
90 | do |
91 | { |
9615b3df |
92 | if (mp3_file_pos >= mp3_file_len) |
93 | return 1; /* EOF, nothing to do */ |
11f4e722 |
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 | |
69dfeea1 |
98 | offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft); |
11f4e722 |
99 | if (offset < 0) { |
9615b3df |
100 | lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset); |
11f4e722 |
101 | mp3_file_pos = mp3_file_len; |
102 | return 1; // EOF |
103 | } |
104 | readPtr = mp3_input_buffer + offset; |
105 | bytesLeft -= offset; |
106 | |
9615b3df |
107 | had_err = err; |
c985b45a |
108 | err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0); |
11f4e722 |
109 | if (err) { |
9615b3df |
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) { |
11f4e722 |
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; |
9615b3df |
122 | } |
123 | if (-12 <= err && err <= -6) { |
11f4e722 |
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 | } |
9615b3df |
129 | lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err); |
11f4e722 |
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 | |
69dfeea1 |
140 | void mp3_start_play(void *f_, int pos) |
11f4e722 |
141 | { |
69dfeea1 |
142 | FILE *f = f_; |
143 | |
11f4e722 |
144 | mp3_file_len = mp3_file_pos = 0; |
145 | mp3_current_file = NULL; |
146 | mp3_buffer_offs = 0; |
147 | |
9615b3df |
148 | if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file? |
11f4e722 |
149 | return; |
150 | |
9615b3df |
151 | // must re-init decoder for new track |
152 | if (mp3dec) |
153 | MP3FreeDecoder(mp3dec); |
154 | mp3dec = MP3InitDecoder(); |
11f4e722 |
155 | |
156 | mp3_current_file = f; |
157 | fseek(f, 0, SEEK_END); |
158 | mp3_file_len = ftell(f); |
159 | |
69dfeea1 |
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 | |
11f4e722 |
176 | // seek.. |
177 | if (pos) { |
69dfeea1 |
178 | unsigned long long pos64 = mp3_file_len - mp3_file_pos; |
9615b3df |
179 | pos64 *= pos; |
69dfeea1 |
180 | mp3_file_pos += pos64 >> 10; |
11f4e722 |
181 | } |
182 | |
183 | mp3_decode(); |
184 | } |
185 | |
11f4e722 |
186 | void 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 | |
9615b3df |
191 | if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len) |
192 | return; /* no file / EOF */ |
11f4e722 |
193 | |
194 | length_mp3 = length; |
f9dd0a63 |
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 | } |
11f4e722 |
203 | |
204 | if (1152 - mp3_buffer_offs >= length_mp3) { |
c985b45a |
205 | mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1); |
11f4e722 |
206 | |
207 | mp3_buffer_offs += length_mp3; |
208 | } else { |
209 | int ret, left = 1152 - mp3_buffer_offs; |
210 | |
c985b45a |
211 | mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1); |
11f4e722 |
212 | ret = mp3_decode(); |
213 | if (ret == 0) { |
214 | mp3_buffer_offs = length_mp3 - left; |
c985b45a |
215 | mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1); |
11f4e722 |
216 | } else |
217 | mp3_buffer_offs = 0; |
218 | } |
219 | } |
220 | |