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