clarify PicoDrive's license
[picodrive.git] / platform / common / mp3_helix.c
CommitLineData
cff531af 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 */
da42200b 9
10#include <stdio.h>
11#include <string.h>
12
7c34867a 13#include <pico/pico_int.h>
14#include <pico/sound/mix.h>
da42200b 15#include "helix/pub/mp3dec.h"
7c34867a 16#include "mp3.h"
da42200b 17#include "lprintf.h"
18
da42200b 19static HMP3Decoder mp3dec = 0;
7c34867a 20static FILE *mp3_current_file = NULL;
21static int mp3_file_len = 0, mp3_file_pos = 0;
da42200b 22static int mp3_buffer_offs = 0;
7c34867a 23static unsigned char mp3_input_buffer[2*1024];
da42200b 24
7c34867a 25#ifdef __GP2X__
26#define mp3_update mp3_update_local
27#define mp3_start_play mp3_start_play_local
28#endif
da42200b 29
30static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
31{
32 int ret, offs1, offs = 0;
33
34 while (1)
35 {
7c18e34a 36 offs1 = mp3_find_sync_word(buff + offs, 2048 - offs);
da42200b 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
7c18e34a 51int mp3_get_bitrate(void *f_, int len)
da42200b 52{
53 unsigned char buff[2048];
54 MP3FrameInfo fi;
7c18e34a 55 FILE *f = f_;
da42200b 56 int ret;
57
7c18e34a 58 memset(buff, 0, sizeof(buff));
da42200b 59
7c18e34a 60 if (mp3dec)
61 MP3FreeDecoder(mp3dec);
091facb8 62 mp3dec = MP3InitDecoder();
da42200b 63
64 fseek(f, 0, SEEK_SET);
7c18e34a 65 ret = fread(buff, 1, sizeof(buff), f);
da42200b 66 fseek(f, 0, SEEK_SET);
7c18e34a 67 if (ret <= 0)
68 return -1;
da42200b 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 }
7c18e34a 78 if (ret != 0)
79 return ret;
da42200b 80
81 // printf("bitrate: %i\n", fi.bitrate / 1000);
82
83 return fi.bitrate / 1000;
84}
85
da42200b 86static int mp3_decode(void)
87{
88 unsigned char *readPtr;
89 int bytesLeft;
90 int offset; // mp3 frame offset from readPtr
7c34867a 91 int had_err;
92 int err = 0;
da42200b 93
94 do
95 {
7c34867a 96 if (mp3_file_pos >= mp3_file_len)
97 return 1; /* EOF, nothing to do */
da42200b 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
7c18e34a 102 offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft);
da42200b 103 if (offset < 0) {
7c34867a 104 lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
da42200b 105 mp3_file_pos = mp3_file_len;
106 return 1; // EOF
107 }
108 readPtr = mp3_input_buffer + offset;
109 bytesLeft -= offset;
110
7c34867a 111 had_err = err;
c9e1affc 112 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
da42200b 113 if (err) {
7c34867a 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) {
da42200b 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;
7c34867a 126 }
127 if (-12 <= err && err <= -6) {
da42200b 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 }
7c34867a 133 lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
da42200b 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
7c18e34a 144void mp3_start_play(void *f_, int pos)
da42200b 145{
7c18e34a 146 FILE *f = f_;
147
da42200b 148 mp3_file_len = mp3_file_pos = 0;
149 mp3_current_file = NULL;
150 mp3_buffer_offs = 0;
151
7c34867a 152 if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
da42200b 153 return;
154
7c34867a 155 // must re-init decoder for new track
156 if (mp3dec)
157 MP3FreeDecoder(mp3dec);
158 mp3dec = MP3InitDecoder();
da42200b 159
160 mp3_current_file = f;
161 fseek(f, 0, SEEK_END);
162 mp3_file_len = ftell(f);
163
7c18e34a 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
da42200b 180 // seek..
181 if (pos) {
7c18e34a 182 unsigned long long pos64 = mp3_file_len - mp3_file_pos;
7c34867a 183 pos64 *= pos;
7c18e34a 184 mp3_file_pos += pos64 >> 10;
da42200b 185 }
186
187 mp3_decode();
188}
189
da42200b 190void 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
7c34867a 195 if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len)
196 return; /* no file / EOF */
da42200b 197
198 length_mp3 = length;
02da059d 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 }
da42200b 207
208 if (1152 - mp3_buffer_offs >= length_mp3) {
c9e1affc 209 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
da42200b 210
211 mp3_buffer_offs += length_mp3;
212 } else {
213 int ret, left = 1152 - mp3_buffer_offs;
214
c9e1affc 215 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
da42200b 216 ret = mp3_decode();
217 if (ret == 0) {
218 mp3_buffer_offs = length_mp3 - left;
c9e1affc 219 mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);
da42200b 220 } else
221 mp3_buffer_offs = 0;
222 }
223}
224