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