mp3_helix.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <pico/pico_int.h>
  12. #include <pico/sound/mix.h>
  13. #include "helix/pub/mp3dec.h"
  14. #include "mp3.h"
  15. #include "lprintf.h"
  16. static HMP3Decoder mp3dec = 0;
  17. static FILE *mp3_current_file = NULL;
  18. static int mp3_file_len = 0, mp3_file_pos = 0;
  19. static int mp3_buffer_offs = 0;
  20. static unsigned char mp3_input_buffer[2*1024];
  21. #ifdef __GP2X__
  22. #define mp3_update mp3_update_local
  23. #define mp3_start_play mp3_start_play_local
  24. #endif
  25. static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
  26. {
  27. int ret, offs1, offs = 0;
  28. while (1)
  29. {
  30. offs1 = mp3_find_sync_word(buff + offs, 2048 - offs);
  31. if (offs1 < 0) return -2;
  32. offs += offs1;
  33. if (2048 - offs < 4) return -3;
  34. // printf("trying header %08x\n", *(int *)(buff + offs));
  35. ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
  36. if (ret == 0 && fi->bitrate != 0) break;
  37. offs++;
  38. }
  39. return ret;
  40. }
  41. int mp3_get_bitrate(void *f_, int len)
  42. {
  43. unsigned char buff[2048];
  44. MP3FrameInfo fi;
  45. FILE *f = f_;
  46. int ret;
  47. memset(buff, 0, sizeof(buff));
  48. if (mp3dec)
  49. MP3FreeDecoder(mp3dec);
  50. mp3dec = MP3InitDecoder();
  51. fseek(f, 0, SEEK_SET);
  52. ret = fread(buff, 1, sizeof(buff), f);
  53. fseek(f, 0, SEEK_SET);
  54. if (ret <= 0)
  55. return -1;
  56. ret = try_get_header(buff, &fi);
  57. if (ret != 0 || fi.bitrate == 0) {
  58. // try to read somewhere around the middle
  59. fseek(f, len>>1, SEEK_SET);
  60. fread(buff, 1, 2048, f);
  61. fseek(f, 0, SEEK_SET);
  62. ret = try_get_header(buff, &fi);
  63. }
  64. if (ret != 0)
  65. return ret;
  66. // printf("bitrate: %i\n", fi.bitrate / 1000);
  67. return fi.bitrate / 1000;
  68. }
  69. static int mp3_decode(void)
  70. {
  71. unsigned char *readPtr;
  72. int bytesLeft;
  73. int offset; // mp3 frame offset from readPtr
  74. int had_err;
  75. int err = 0;
  76. do
  77. {
  78. if (mp3_file_pos >= mp3_file_len)
  79. return 1; /* EOF, nothing to do */
  80. fseek(mp3_current_file, mp3_file_pos, SEEK_SET);
  81. bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file);
  82. offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft);
  83. if (offset < 0) {
  84. lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset);
  85. mp3_file_pos = mp3_file_len;
  86. return 1; // EOF
  87. }
  88. readPtr = mp3_input_buffer + offset;
  89. bytesLeft -= offset;
  90. had_err = err;
  91. err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
  92. if (err) {
  93. if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
  94. // just need another frame
  95. mp3_file_pos += readPtr - mp3_input_buffer;
  96. continue;
  97. }
  98. if (err == ERR_MP3_INDATA_UNDERFLOW && !had_err) {
  99. if (offset == 0)
  100. // something's really wrong here, frame had to fit
  101. mp3_file_pos = mp3_file_len;
  102. else
  103. mp3_file_pos += offset;
  104. continue;
  105. }
  106. if (-12 <= err && err <= -6) {
  107. // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
  108. // just try to skip the offending frame..
  109. mp3_file_pos += offset + 1;
  110. continue;
  111. }
  112. lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
  113. mp3_file_pos = mp3_file_len;
  114. return 1;
  115. }
  116. mp3_file_pos += readPtr - mp3_input_buffer;
  117. }
  118. while (0);
  119. return 0;
  120. }
  121. void mp3_start_play(void *f_, int pos)
  122. {
  123. FILE *f = f_;
  124. mp3_file_len = mp3_file_pos = 0;
  125. mp3_current_file = NULL;
  126. mp3_buffer_offs = 0;
  127. if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file?
  128. return;
  129. // must re-init decoder for new track
  130. if (mp3dec)
  131. MP3FreeDecoder(mp3dec);
  132. mp3dec = MP3InitDecoder();
  133. mp3_current_file = f;
  134. fseek(f, 0, SEEK_END);
  135. mp3_file_len = ftell(f);
  136. // search for first sync word, skipping stuff like ID3 tags
  137. while (mp3_file_pos < 128*1024) {
  138. int offs, bytes;
  139. fseek(f, mp3_file_pos, SEEK_SET);
  140. bytes = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f);
  141. if (bytes < 4)
  142. break;
  143. offs = mp3_find_sync_word(mp3_input_buffer, bytes);
  144. if (offs >= 0) {
  145. mp3_file_pos += offs;
  146. break;
  147. }
  148. mp3_file_pos += bytes - 2;
  149. }
  150. // seek..
  151. if (pos) {
  152. unsigned long long pos64 = mp3_file_len - mp3_file_pos;
  153. pos64 *= pos;
  154. mp3_file_pos += pos64 >> 10;
  155. }
  156. mp3_decode();
  157. }
  158. void mp3_update(int *buffer, int length, int stereo)
  159. {
  160. int length_mp3, shr = 0;
  161. void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
  162. if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len)
  163. return; /* no file / EOF */
  164. length_mp3 = length;
  165. if (PsndRate <= 11025 + 100) {
  166. mix_samples = mix_16h_to_32_s2;
  167. length_mp3 <<= 2; shr = 2;
  168. }
  169. else if (PsndRate <= 22050 + 100) {
  170. mix_samples = mix_16h_to_32_s1;
  171. length_mp3 <<= 1; shr = 1;
  172. }
  173. if (1152 - mp3_buffer_offs >= length_mp3) {
  174. mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1);
  175. mp3_buffer_offs += length_mp3;
  176. } else {
  177. int ret, left = 1152 - mp3_buffer_offs;
  178. mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
  179. ret = mp3_decode();
  180. if (ret == 0) {
  181. mp3_buffer_offs = length_mp3 - left;
  182. mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1);
  183. } else
  184. mp3_buffer_offs = 0;
  185. }
  186. }