clarify PicoDrive's license
[picodrive.git] / platform / common / mp3_helix.c
1 /*
2  * Some mp3 related code for Sega/Mega CD.
3  * Uses the Helix Fixed-point MP3 decoder
4  * (C) notaz, 2007-2009
5  *
6  * This work is licensed under the terms of MAME license.
7  * See COPYING file in the top-level directory.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <pico/pico_int.h>
14 #include <pico/sound/mix.h>
15 #include "helix/pub/mp3dec.h"
16 #include "mp3.h"
17 #include "lprintf.h"
18
19 static HMP3Decoder mp3dec = 0;
20 static FILE *mp3_current_file = NULL;
21 static int mp3_file_len = 0, mp3_file_pos = 0;
22 static int mp3_buffer_offs = 0;
23 static unsigned char mp3_input_buffer[2*1024];
24
25 #ifdef __GP2X__
26 #define mp3_update mp3_update_local
27 #define mp3_start_play mp3_start_play_local
28 #endif
29
30 static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
31 {
32         int ret, offs1, offs = 0;
33
34         while (1)
35         {
36                 offs1 = mp3_find_sync_word(buff + offs, 2048 - offs);
37                 if (offs1 < 0) return -2;
38                 offs += offs1;
39                 if (2048 - offs < 4) return -3;
40
41                 // printf("trying header %08x\n", *(int *)(buff + offs));
42
43                 ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
44                 if (ret == 0 && fi->bitrate != 0) break;
45                 offs++;
46         }
47
48         return ret;
49 }
50
51 int mp3_get_bitrate(void *f_, int len)
52 {
53         unsigned char buff[2048];
54         MP3FrameInfo fi;
55         FILE *f = f_;
56         int ret;
57
58         memset(buff, 0, sizeof(buff));
59
60         if (mp3dec)
61                 MP3FreeDecoder(mp3dec);
62         mp3dec = MP3InitDecoder();
63
64         fseek(f, 0, SEEK_SET);
65         ret = fread(buff, 1, sizeof(buff), f);
66         fseek(f, 0, SEEK_SET);
67         if (ret <= 0)
68                 return -1;
69
70         ret = try_get_header(buff, &fi);
71         if (ret != 0 || fi.bitrate == 0) {
72                 // try to read somewhere around the middle
73                 fseek(f, len>>1, SEEK_SET);
74                 fread(buff, 1, 2048, f);
75                 fseek(f, 0, SEEK_SET);
76                 ret = try_get_header(buff, &fi);
77         }
78         if (ret != 0)
79                 return ret;
80
81         // printf("bitrate: %i\n", fi.bitrate / 1000);
82
83         return fi.bitrate / 1000;
84 }
85
86 static int mp3_decode(void)
87 {
88         unsigned char *readPtr;
89         int bytesLeft;
90         int offset; // mp3 frame offset from readPtr
91         int had_err;
92         int err = 0;
93
94         do
95         {
96                 if (mp3_file_pos >= mp3_file_len)
97                         return 1; /* EOF, nothing to do */
98
99                 fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
100                 bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
101
102                 offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft);
103                 if (offset < 0) {
104                         lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
105                         mp3_file_pos = mp3_file_len;
106                         return 1; // EOF
107                 }
108                 readPtr = mp3_input_buffer + offset;
109                 bytesLeft -= offset;
110
111                 had_err = err;
112                 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
113                 if (err) {
114                         if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
115                                 // just need another frame
116                                 mp3_file_pos += readPtr - mp3_input_buffer;
117                                 continue;
118                         }
119                         if (err == ERR_MP3_INDATA_UNDERFLOW && !had_err) {
120                                 if (offset == 0)
121                                         // something's really wrong here, frame had to fit
122                                         mp3_file_pos = mp3_file_len;
123                                 else
124                                         mp3_file_pos += offset;
125                                 continue;
126                         }
127                         if (-12 <= err && err <= -6) {
128                                 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
129                                 // just try to skip the offending frame..
130                                 mp3_file_pos += offset + 1;
131                                 continue;
132                         }
133                         lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
134                         mp3_file_pos = mp3_file_len;
135                         return 1;
136                 }
137                 mp3_file_pos += readPtr - mp3_input_buffer;
138         }
139         while (0);
140
141         return 0;
142 }
143
144 void mp3_start_play(void *f_, int pos)
145 {
146         FILE *f = f_;
147
148         mp3_file_len = mp3_file_pos = 0;
149         mp3_current_file = NULL;
150         mp3_buffer_offs = 0;
151
152         if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
153                 return;
154
155         // must re-init decoder for new track
156         if (mp3dec)
157                 MP3FreeDecoder(mp3dec);
158         mp3dec = MP3InitDecoder();
159
160         mp3_current_file = f;
161         fseek(f, 0, SEEK_END);
162         mp3_file_len = ftell(f);
163
164         // search for first sync word, skipping stuff like ID3 tags
165         while (mp3_file_pos < 128*1024) {
166                 int offs, bytes;
167
168                 fseek(f, mp3_file_pos, SEEK_SET);
169                 bytes = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f);
170                 if (bytes < 4)
171                         break;
172                 offs = mp3_find_sync_word(mp3_input_buffer, bytes);
173                 if (offs >= 0) {
174                         mp3_file_pos += offs;
175                         break;
176                 }
177                 mp3_file_pos += bytes - 2;
178         }
179
180         // seek..
181         if (pos) {
182                 unsigned long long pos64 = mp3_file_len - mp3_file_pos;
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