vsync bugfix + refactoring
[libpicofe.git] / common / mp3_helix.c
CommitLineData
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 15static HMP3Decoder mp3dec = 0;
9615b3df 16static FILE *mp3_current_file = NULL;
17static int mp3_file_len = 0, mp3_file_pos = 0;
11f4e722 18static int mp3_buffer_offs = 0;
9615b3df 19static 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
6e507f76 26static 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
11f4e722 52static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
53{
54 int ret, offs1, offs = 0;
55
56 while (1)
57 {
6e507f76 58 offs1 = find_sync_word(buff + offs, 2048 - offs);
11f4e722 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
73int 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
6e507f76 81 if (mp3dec) MP3FreeDecoder(mp3dec);
82 mp3dec = MP3InitDecoder();
11f4e722 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
11f4e722 104static int mp3_decode(void)
105{
106 unsigned char *readPtr;
107 int bytesLeft;
108 int offset; // mp3 frame offset from readPtr
9615b3df 109 int had_err;
110 int err = 0;
11f4e722 111
112 do
113 {
9615b3df 114 if (mp3_file_pos >= mp3_file_len)
115 return 1; /* EOF, nothing to do */
11f4e722 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
6e507f76 120 offset = find_sync_word(mp3_input_buffer, bytesLeft);
11f4e722 121 if (offset < 0) {
9615b3df 122 lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
11f4e722 123 mp3_file_pos = mp3_file_len;
124 return 1; // EOF
125 }
126 readPtr = mp3_input_buffer + offset;
127 bytesLeft -= offset;
128
9615b3df 129 had_err = err;
c985b45a 130 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
11f4e722 131 if (err) {
9615b3df 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) {
11f4e722 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;
9615b3df 144 }
145 if (-12 <= err && err <= -6) {
11f4e722 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 }
9615b3df 151 lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
11f4e722 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
162void 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
9615b3df 168 if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
11f4e722 169 return;
170
9615b3df 171 // must re-init decoder for new track
172 if (mp3dec)
173 MP3FreeDecoder(mp3dec);
174 mp3dec = MP3InitDecoder();
11f4e722 175
176 mp3_current_file = f;
177 fseek(f, 0, SEEK_END);
178 mp3_file_len = ftell(f);
179
180 // seek..
181 if (pos) {
9615b3df 182 unsigned long long pos64 = mp3_file_len;
183 pos64 *= pos;
184 mp3_file_pos = pos64 >> 10;
11f4e722 185 }
186
187 mp3_decode();
188}
189
11f4e722 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
9615b3df 195 if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len)
196 return; /* no file / EOF */
11f4e722 197
198 length_mp3 = length;
199 if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
200 else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; }
201
202 if (1152 - mp3_buffer_offs >= length_mp3) {
c985b45a 203 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
11f4e722 204
205 mp3_buffer_offs += length_mp3;
206 } else {
207 int ret, left = 1152 - mp3_buffer_offs;
208
c985b45a 209 mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
11f4e722 210 ret = mp3_decode();
211 if (ret == 0) {
212 mp3_buffer_offs = length_mp3 - left;
c985b45a 213 mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);
11f4e722 214 } else
215 mp3_buffer_offs = 0;
216 }
217}
218