mp3_helix.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Some mp3 related code for Sega/Mega CD.
  2. // Uses the Helix Fixed-point MP3 decoder
  3. // (c) Copyright 2007, Grazvydas "notaz" Ignotas
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "../../Pico/PicoInt.h"
  7. #include "../../Pico/sound/mix.h"
  8. #include "helix/pub/mp3dec.h"
  9. #include "lprintf.h"
  10. static HMP3Decoder mp3dec = 0;
  11. static int mp3_buffer_offs = 0;
  12. static int find_sync_word(unsigned char *buf, int nBytes)
  13. {
  14. unsigned char *p, *pe;
  15. /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */
  16. for (p = buf, pe = buf + nBytes - 4; p < pe; p++)
  17. {
  18. int pn;
  19. if (p[0] != 0xff) continue;
  20. pn = p[1];
  21. if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1
  22. (pn & 6) == 0) { // invalid layer
  23. p++; continue;
  24. }
  25. pn = p[2];
  26. if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates
  27. (pn & 0x0c) != 0) { // not 44kHz
  28. continue;
  29. }
  30. return p - buf;
  31. }
  32. return -1;
  33. }
  34. static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
  35. {
  36. int ret, offs1, offs = 0;
  37. while (1)
  38. {
  39. offs1 = find_sync_word(buff + offs, 2048 - offs);
  40. if (offs1 < 0) return -2;
  41. offs += offs1;
  42. if (2048 - offs < 4) return -3;
  43. // printf("trying header %08x\n", *(int *)(buff + offs));
  44. ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
  45. if (ret == 0 && fi->bitrate != 0) break;
  46. offs++;
  47. }
  48. return ret;
  49. }
  50. int mp3_get_bitrate(FILE *f, int len)
  51. {
  52. unsigned char buff[2048];
  53. MP3FrameInfo fi;
  54. int ret;
  55. memset(buff, 0, 2048);
  56. if (mp3dec) MP3FreeDecoder(mp3dec);
  57. mp3dec = MP3InitDecoder();
  58. fseek(f, 0, SEEK_SET);
  59. ret = fread(buff, 1, 2048, f);
  60. fseek(f, 0, SEEK_SET);
  61. if (ret <= 0) return -1;
  62. ret = try_get_header(buff, &fi);
  63. if (ret != 0 || fi.bitrate == 0) {
  64. // try to read somewhere around the middle
  65. fseek(f, len>>1, SEEK_SET);
  66. fread(buff, 1, 2048, f);
  67. fseek(f, 0, SEEK_SET);
  68. ret = try_get_header(buff, &fi);
  69. }
  70. if (ret != 0) return ret;
  71. // printf("bitrate: %i\n", fi.bitrate / 1000);
  72. return fi.bitrate / 1000;
  73. }
  74. #ifdef __GP2X__
  75. #include "../gp2x/code940/940shared.h"
  76. extern _940_ctl_t *shared_ctl;
  77. extern unsigned char *mp3_mem;
  78. static int mp3_decode(void)
  79. {
  80. // tried copying this to cached mem, no improvement noticed
  81. int mp3_offs = shared_ctl->mp3_offs;
  82. unsigned char *readPtr = mp3_mem + mp3_offs;
  83. int bytesLeft = shared_ctl->mp3_len - mp3_offs;
  84. int offset; // frame offset from readPtr
  85. int retries = 0, err;
  86. if (bytesLeft <= 0) return 1; // EOF, nothing to do
  87. retry:
  88. offset = find_sync_word(readPtr, bytesLeft);
  89. if (offset < 0) {
  90. shared_ctl->mp3_offs = shared_ctl->mp3_len;
  91. return 1; // EOF
  92. }
  93. readPtr += offset;
  94. bytesLeft -= offset;
  95. err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
  96. if (err) {
  97. if (err == ERR_MP3_INDATA_UNDERFLOW) {
  98. shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
  99. return 1;
  100. } else if (err <= -6 && err >= -12) {
  101. // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
  102. // just try to skip the offending frame..
  103. readPtr++;
  104. bytesLeft--;
  105. if (retries++ < 2) goto retry;
  106. else lprintf("mp3 decode failed with %i after %i retries\n", err, retries);
  107. }
  108. shared_ctl->mp3_errors++;
  109. shared_ctl->mp3_lasterr = err;
  110. }
  111. shared_ctl->mp3_offs = readPtr - mp3_mem;
  112. return 0;
  113. }
  114. void mp3_start_local(void)
  115. {
  116. // must re-init decoder for new track
  117. if (mp3dec) MP3FreeDecoder(mp3dec);
  118. mp3dec = MP3InitDecoder();
  119. mp3_buffer_offs = 0;
  120. mp3_decode();
  121. }
  122. #define mp3_update mp3_update_local
  123. #else // !__GP2X__
  124. static FILE *mp3_current_file = NULL;
  125. static int mp3_file_len = 0, mp3_file_pos = 0;
  126. static unsigned char mp3_input_buffer[2*1024];
  127. static int mp3_decode(void)
  128. {
  129. unsigned char *readPtr;
  130. int bytesLeft;
  131. int offset; // mp3 frame offset from readPtr
  132. int err;
  133. do
  134. {
  135. if (mp3_file_pos >= mp3_file_len) return 1; // EOF, nothing to do
  136. fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
  137. bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
  138. offset = find_sync_word(mp3_input_buffer, bytesLeft);
  139. if (offset < 0) {
  140. //lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
  141. mp3_file_pos = mp3_file_len;
  142. return 1; // EOF
  143. }
  144. readPtr = mp3_input_buffer + offset;
  145. bytesLeft -= offset;
  146. err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
  147. if (err) {
  148. //lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
  149. if (err == ERR_MP3_INDATA_UNDERFLOW) {
  150. if (offset == 0)
  151. // something's really wrong here, frame had to fit
  152. mp3_file_pos = mp3_file_len;
  153. else
  154. mp3_file_pos += offset;
  155. continue;
  156. } else if (err <= -6 && err >= -12) {
  157. // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
  158. // just try to skip the offending frame..
  159. mp3_file_pos += offset + 1;
  160. continue;
  161. }
  162. mp3_file_pos = mp3_file_len;
  163. return 1;
  164. }
  165. mp3_file_pos += readPtr - mp3_input_buffer;
  166. }
  167. while (0);
  168. return 0;
  169. }
  170. void mp3_start_play(FILE *f, int pos)
  171. {
  172. mp3_file_len = mp3_file_pos = 0;
  173. mp3_current_file = NULL;
  174. mp3_buffer_offs = 0;
  175. // must re-init decoder for new track
  176. if (mp3dec) MP3FreeDecoder(mp3dec);
  177. mp3dec = MP3InitDecoder();
  178. if (!(PicoOpt&POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
  179. return;
  180. //lprintf("mp3_start_play %p %i\n", f, pos);
  181. mp3_current_file = f;
  182. fseek(f, 0, SEEK_END);
  183. mp3_file_len = ftell(f);
  184. // seek..
  185. if (pos) {
  186. mp3_file_pos = (mp3_file_len << 6) >> 10;
  187. mp3_file_pos *= pos;
  188. mp3_file_pos >>= 6;
  189. }
  190. mp3_decode();
  191. }
  192. int mp3_get_offset(void)
  193. {
  194. unsigned int offs1024 = 0;
  195. int cdda_on;
  196. cdda_on = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_CDDA) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
  197. (Pico_mcd->scd.Status_CDC & 1) && mp3_current_file != NULL;
  198. if (cdda_on) {
  199. offs1024 = mp3_file_pos << 7;
  200. offs1024 /= mp3_file_len >> 3;
  201. }
  202. //lprintf("mp3_get_offset offs1024=%u (%i/%i)\n", offs1024, mp3_file_pos, mp3_file_len);
  203. return offs1024;
  204. }
  205. #endif // ifndef __GP2X__
  206. void mp3_update(int *buffer, int length, int stereo)
  207. {
  208. int length_mp3, shr = 0;
  209. void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
  210. #ifndef __GP2X__
  211. if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len) return; // no file / EOF
  212. #endif
  213. length_mp3 = length;
  214. if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
  215. else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; }
  216. if (1152 - mp3_buffer_offs >= length_mp3) {
  217. mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
  218. mp3_buffer_offs += length_mp3;
  219. } else {
  220. int ret, left = 1152 - mp3_buffer_offs;
  221. mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
  222. ret = mp3_decode();
  223. if (ret == 0) {
  224. mp3_buffer_offs = length_mp3 - left;
  225. mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);
  226. } else
  227. mp3_buffer_offs = 0;
  228. }
  229. }