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