cff531af |
1 | /* |
2 | * PicoDrive |
fc11dd05 |
3 | * (C) notaz, 2010,2013 |
cff531af |
4 | * |
5 | * This work is licensed under the terms of MAME license. |
6 | * See COPYING file in the top-level directory. |
7 | */ |
fc11dd05 |
8 | #include <stdio.h> |
9 | #include <string.h> |
10 | |
11 | #include <pico/pico_int.h> |
12 | #include <pico/sound/mix.h> |
7c18e34a |
13 | #include "mp3.h" |
14 | |
fc11dd05 |
15 | static FILE *mp3_current_file; |
16 | static int mp3_file_len, mp3_file_pos; |
17 | static int cdda_out_pos; |
18 | static int decoder_active; |
19 | |
20 | unsigned short mpeg1_l3_bitrates[16] = { |
21 | 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 |
22 | }; |
23 | |
7c18e34a |
24 | int mp3_find_sync_word(const unsigned char *buf, int size) |
25 | { |
26 | const unsigned char *p, *pe; |
27 | |
28 | /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */ |
29 | for (p = buf, pe = buf + size - 3; p <= pe; p++) |
30 | { |
31 | int pn; |
32 | if (p[0] != 0xff) |
33 | continue; |
34 | pn = p[1]; |
35 | if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1 |
36 | (pn & 6) == 0) { // invalid layer |
37 | p++; continue; |
38 | } |
39 | pn = p[2]; |
40 | if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates |
41 | (pn & 0x0c) != 0) { // not 44kHz |
42 | continue; |
43 | } |
44 | |
45 | return p - buf; |
46 | } |
47 | |
48 | return -1; |
49 | } |
50 | |
fc11dd05 |
51 | static int try_get_bitrate(unsigned char *buf, int buf_size) |
52 | { |
53 | int offs1, offs = 0; |
54 | int ret; |
55 | |
56 | while (1) |
57 | { |
58 | offs1 = mp3_find_sync_word(buf + offs, buf_size - offs); |
59 | if (offs1 < 0) |
60 | return -2; |
61 | offs += offs1; |
62 | if (buf_size - offs < 4) |
63 | return -3; |
64 | |
65 | // printf("trying header %08x\n", *(int *)(buf + offs)); |
66 | |
67 | ret = mpeg1_l3_bitrates[buf[offs + 2] >> 4]; |
68 | if (ret > 0) |
69 | return ret; |
70 | } |
71 | |
72 | return -2; |
73 | } |
74 | |
75 | int mp3_get_bitrate(void *f_, int len) |
76 | { |
77 | unsigned char buf[2048]; |
78 | FILE *f = f_; |
79 | int retval = -1; |
80 | int ret; |
81 | |
82 | memset(buf, 0, sizeof(buf)); |
83 | |
84 | fseek(f, 0, SEEK_SET); |
85 | ret = fread(buf, 1, sizeof(buf), f); |
86 | if (ret != sizeof(buf)) |
87 | goto out; |
88 | |
89 | ret = try_get_bitrate(buf, sizeof(buf)); |
90 | if (ret <= 0) { |
91 | // try to read somewhere around the middle |
92 | fseek(f, len / 2, SEEK_SET); |
93 | fread(buf, 1, sizeof(buf), f); |
94 | ret = try_get_bitrate(buf, sizeof(buf)); |
95 | } |
96 | if (ret > 0) |
97 | retval = ret; |
98 | |
99 | //printf("bitrate: %i\n", retval); |
100 | |
101 | out: |
102 | fseek(f, 0, SEEK_SET); |
103 | return retval; |
104 | } |
105 | |
75a30842 |
106 | void mp3_start_play(void *f_, int pos1024) |
fc11dd05 |
107 | { |
108 | unsigned char buf[2048]; |
109 | FILE *f = f_; |
110 | int ret; |
111 | |
112 | mp3_file_len = mp3_file_pos = 0; |
113 | mp3_current_file = NULL; |
114 | cdda_out_pos = 0; |
115 | decoder_active = 0; |
116 | |
117 | if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file? |
118 | return; |
119 | |
fc11dd05 |
120 | fseek(f, 0, SEEK_END); |
121 | mp3_file_len = ftell(f); |
122 | |
123 | // search for first sync word, skipping stuff like ID3 tags |
124 | while (mp3_file_pos < 128*1024) { |
125 | int offs, bytes; |
126 | |
127 | fseek(f, mp3_file_pos, SEEK_SET); |
128 | bytes = fread(buf, 1, sizeof(buf), f); |
129 | if (bytes < 4) |
130 | break; |
131 | offs = mp3_find_sync_word(buf, bytes); |
132 | if (offs >= 0) { |
133 | mp3_file_pos += offs; |
134 | break; |
135 | } |
136 | mp3_file_pos += bytes - 3; |
137 | } |
138 | |
139 | // seek.. |
75a30842 |
140 | if (pos1024 != 0) { |
fc11dd05 |
141 | unsigned long long pos64 = mp3_file_len - mp3_file_pos; |
75a30842 |
142 | pos64 *= pos1024; |
fc11dd05 |
143 | mp3_file_pos += pos64 >> 10; |
144 | } |
145 | |
75a30842 |
146 | ret = mp3dec_start(f, mp3_file_pos); |
147 | if (ret != 0) { |
148 | return; |
149 | } |
150 | |
151 | mp3_current_file = f; |
152 | decoder_active = 1; |
153 | |
fc11dd05 |
154 | mp3dec_decode(mp3_current_file, &mp3_file_pos, mp3_file_len); |
155 | } |
156 | |
157 | void mp3_update(int *buffer, int length, int stereo) |
158 | { |
159 | int length_mp3, shr = 0; |
160 | void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32; |
161 | |
162 | if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len) |
163 | return; /* no file / EOF */ |
164 | |
165 | if (!decoder_active) |
166 | return; |
167 | |
168 | length_mp3 = length; |
169 | if (PsndRate <= 11025 + 100) { |
170 | mix_samples = mix_16h_to_32_s2; |
171 | length_mp3 <<= 2; shr = 2; |
172 | } |
173 | else if (PsndRate <= 22050 + 100) { |
174 | mix_samples = mix_16h_to_32_s1; |
175 | length_mp3 <<= 1; shr = 1; |
176 | } |
177 | |
178 | if (1152 - cdda_out_pos >= length_mp3) { |
179 | mix_samples(buffer, cdda_out_buffer + cdda_out_pos * 2, |
180 | length * 2); |
181 | |
182 | cdda_out_pos += length_mp3; |
183 | } else { |
184 | int ret, left = 1152 - cdda_out_pos; |
185 | |
75a30842 |
186 | if (left > 0) |
187 | mix_samples(buffer, cdda_out_buffer + cdda_out_pos * 2, |
188 | (left >> shr) * 2); |
fc11dd05 |
189 | |
190 | ret = mp3dec_decode(mp3_current_file, &mp3_file_pos, |
191 | mp3_file_len); |
192 | if (ret == 0) { |
193 | cdda_out_pos = length_mp3 - left; |
194 | mix_samples(buffer + (left >> shr) * 2, |
195 | cdda_out_buffer, |
196 | (cdda_out_pos >> shr) * 2); |
197 | } else |
198 | cdda_out_pos = 0; |
199 | } |
200 | } |
201 | |