mp3.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "../../Pico/sound/mix.h"
  4. #include "code940/940shared.h"
  5. #include "helix/pub/mp3dec.h"
  6. static short mp3_out_buffer[2*1152];
  7. static HMP3Decoder mp3dec = 0;
  8. static int mp3_buffer_offs = 0;
  9. extern _940_ctl_t *shared_ctl;
  10. extern unsigned char *mp3_mem;
  11. extern int PsndRate;
  12. static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
  13. {
  14. int ret, offs1, offs = 0;
  15. while (1)
  16. {
  17. offs1 = MP3FindSyncWord(buff + offs, 2048 - offs);
  18. if (offs1 < 0) return -2;
  19. offs += offs1;
  20. if (2048 - offs < 4) return -3;
  21. // printf("trying header %08x\n", *(int *)(buff + offs));
  22. ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
  23. if (ret == 0 && fi->bitrate != 0) break;
  24. offs++;
  25. }
  26. return ret;
  27. }
  28. int mp3_get_bitrate(FILE *f, int len)
  29. {
  30. unsigned char buff[2048];
  31. MP3FrameInfo fi;
  32. int ret;
  33. memset(buff, 0, 2048);
  34. if (!mp3dec) mp3dec = MP3InitDecoder();
  35. fseek(f, 0, SEEK_SET);
  36. ret = fread(buff, 1, 2048, f);
  37. fseek(f, 0, SEEK_SET);
  38. if (ret <= 0) return -1;
  39. ret = try_get_header(buff, &fi);
  40. if (ret != 0 || fi.bitrate == 0) {
  41. // try to read somewhere around the middle
  42. fseek(f, len>>1, SEEK_SET);
  43. fread(buff, 1, 2048, f);
  44. fseek(f, 0, SEEK_SET);
  45. ret = try_get_header(buff, &fi);
  46. }
  47. if (ret != 0) return ret;
  48. // printf("bitrate: %i\n", fi.bitrate / 1000);
  49. return fi.bitrate / 1000;
  50. }
  51. static void mp3_decode(void)
  52. {
  53. // tried copying this to cached mem, no improvement noticed
  54. int mp3_offs = shared_ctl->mp3_offs;
  55. unsigned char *readPtr = mp3_mem + mp3_offs;
  56. int bytesLeft = shared_ctl->mp3_len - mp3_offs;
  57. int offset; // frame offset from readPtr
  58. int err;
  59. if (bytesLeft <= 0) return; // EOF, nothing to do
  60. offset = MP3FindSyncWord(readPtr, bytesLeft);
  61. if (offset < 0) {
  62. shared_ctl->mp3_offs = shared_ctl->mp3_len;
  63. return; // EOF
  64. }
  65. readPtr += offset;
  66. bytesLeft -= offset;
  67. err = MP3Decode(mp3dec, &readPtr, &bytesLeft, mp3_out_buffer, 0);
  68. if (err) {
  69. if (err == ERR_MP3_INDATA_UNDERFLOW) {
  70. shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
  71. return;
  72. } else if (err <= -6 && err >= -12) {
  73. // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
  74. // just try to skip the offending frame..
  75. readPtr++;
  76. }
  77. shared_ctl->mp3_errors++;
  78. shared_ctl->mp3_lasterr = err;
  79. }
  80. shared_ctl->mp3_offs = readPtr - mp3_mem;
  81. }
  82. void mp3_update_local(int *buffer, int length, int stereo)
  83. {
  84. int length_mp3, shr = 0;
  85. void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
  86. length_mp3 = length;
  87. if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
  88. else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; }
  89. if (1152 - mp3_buffer_offs >= length_mp3) {
  90. mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, length<<1);
  91. mp3_buffer_offs += length_mp3;
  92. } else {
  93. int left = 1152 - mp3_buffer_offs;
  94. mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
  95. mp3_decode();
  96. mp3_buffer_offs = length_mp3 - left;
  97. mix_samples(buffer + ((left>>shr)<<1), mp3_out_buffer, (mp3_buffer_offs>>shr)<<1);
  98. }
  99. }
  100. void mp3_start_local(void)
  101. {
  102. mp3_buffer_offs = 0;
  103. mp3_decode();
  104. }