00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "mp3_utils.h"
00029
00031 void splt_mp3_init_stream_frame(splt_mp3_state *mp3state)
00032 {
00033 mad_stream_init(&mp3state->stream);
00034 mad_frame_init(&mp3state->frame);
00035 }
00036
00038 void splt_mp3_finish_stream_frame(splt_mp3_state *mp3state)
00039 {
00040 mad_stream_finish(&mp3state->stream);
00041 mad_frame_finish(&mp3state->frame);
00042 }
00043
00048 void splt_mp3_checksync(splt_mp3_state *mp3state)
00049 {
00050
00051
00052
00053
00054
00055
00056
00057 mp3state->syncdetect = 0;
00058
00059
00060 }
00061
00063 int splt_mp3_c_bitrate(unsigned long head)
00064 {
00065 if ((head & 0xffe00000) != 0xffe00000) return 0;
00066 if (!((head>>17)&3)) return 0;
00067 if (((head>>12)&0xf) == 0xf) return 0;
00068 if (!((head >> 12) & 0xf)) return 0;
00069 if (((head>>10)&0x3) == 0x3 ) return 0;
00070 if (((head >> 19) & 1)==1 && ((head>>17)&3)==3 &&
00071 ((head>>16)&1)==1) return 0;
00072 if ((head & 0xffff0000) == 0xfffe0000) return 0;
00073
00074 return ((head>>12)&0xf);
00075 }
00076
00078 struct splt_header splt_mp3_makehead(unsigned long headword,
00079 struct splt_mp3 mp3f, struct splt_header head, off_t ptr)
00080 {
00081 head.ptr = ptr;
00082 head.bitrate = splt_mp3_tabsel_123[1 - mp3f.mpgid][mp3f.layer-1][splt_mp3_c_bitrate(headword)];
00083 head.padding = ((headword>>9)&0x1);
00084 head.framesize = (head.bitrate*144000)/
00085 (mp3f.freq<<(1 - mp3f.mpgid)) + head.padding;
00086
00087 return head;
00088 }
00089
00091 off_t splt_mp3_findhead(splt_mp3_state *mp3state, off_t start)
00092 {
00093 if (splt_io_get_word(mp3state->file_input,
00094 start, SEEK_SET, &mp3state->headw) == -1)
00095 {
00096 return -1;
00097 }
00098 if (feof(mp3state->file_input))
00099 {
00100 return -1;
00101 }
00102 while (!(splt_mp3_c_bitrate(mp3state->headw)))
00103 {
00104 if (feof(mp3state->file_input))
00105 {
00106 return -1;
00107 }
00108 mp3state->headw <<= 8;
00109 mp3state->headw |= fgetc(mp3state->file_input);
00110 start++;
00111 }
00112
00113 return start;
00114 }
00115
00117 off_t splt_mp3_findvalidhead(splt_mp3_state *mp3state, off_t start)
00118 {
00119 off_t begin;
00120 struct splt_header h;
00121
00122 begin = splt_mp3_findhead(mp3state, start);
00123
00124 do {
00125 start = begin;
00126 if (start == -1)
00127 {
00128 break;
00129 }
00130 h = splt_mp3_makehead (mp3state->headw, mp3state->mp3file, h, start);
00131 begin = splt_mp3_findhead(mp3state, (start + 1));
00132 } while (begin!=(start + h.framesize));
00133
00134 return start;
00135 }
00136
00138 int splt_mp3_xing_info_off(splt_mp3_state *mp3state)
00139 {
00140 unsigned long headw = 0;
00141 int i;
00142
00143 for (i=0; i<mp3state->mp3file.xing; i++)
00144 {
00145 if ((headw == SPLT_MP3_XING_MAGIC) ||
00146 (headw == SPLT_MP3_INFO_MAGIC))
00147 {
00148 return i;
00149 }
00150 headw <<= 8;
00151 headw |= mp3state->mp3file.xingbuffer[i];
00152 }
00153
00154 return 0;
00155 }
00156
00161 int splt_mp3_get_frame(splt_mp3_state *mp3state)
00162 {
00163 if(mp3state->stream.buffer==NULL ||
00164 mp3state->stream.error==MAD_ERROR_BUFLEN)
00165 {
00166 size_t readSize, remaining;
00167 unsigned char *readStart;
00168
00169 if (feof(mp3state->file_input))
00170 {
00171 return -2;
00172 }
00173
00174 if(mp3state->stream.next_frame!=NULL)
00175 {
00176 remaining = mp3state->stream.bufend - mp3state->stream.next_frame;
00177 memmove(mp3state->inputBuffer, mp3state->stream.next_frame, remaining);
00178 readStart = mp3state->inputBuffer + remaining;
00179 readSize = SPLT_MAD_BSIZE - remaining;
00180 }
00181 else
00182 {
00183 readSize = SPLT_MAD_BSIZE;
00184 readStart=mp3state->inputBuffer;
00185 remaining=0;
00186 }
00187
00188 readSize=fread(readStart, 1, readSize, mp3state->file_input);
00189 if (readSize <= 0)
00190 {
00191 return -2;
00192 }
00193
00194 mp3state->buf_len = readSize + remaining;
00195 mp3state->bytes += readSize;
00196
00197 mad_stream_buffer(&mp3state->stream, mp3state->inputBuffer,
00198 readSize+remaining);
00199 mp3state->stream.error = MAD_ERROR_NONE;
00200 }
00201
00202
00203 return mad_frame_decode(&mp3state->frame,&mp3state->stream);
00204 }
00205
00223 int splt_mp3_get_valid_frame(splt_state *state, int *error)
00224 {
00225 splt_mp3_state *mp3state = state->codec;
00226 int ok = 0;
00227 do
00228 {
00229 int ret = splt_mp3_get_frame(mp3state);
00230 if(ret != 0)
00231 {
00232 if (ret == -2)
00233 {
00234 return -1;
00235 }
00236 if (mp3state->stream.error == MAD_ERROR_LOSTSYNC)
00237 {
00238
00239 state->syncerrors++;
00240 if ((mp3state->syncdetect) && (state->syncerrors>SPLT_MAXSYNC))
00241 {
00242 splt_mp3_checksync(mp3state);
00243 }
00244 }
00245 if(MAD_RECOVERABLE(mp3state->stream.error))
00246 {
00247 continue;
00248 }
00249 else
00250 {
00251 if(mp3state->stream.error==MAD_ERROR_BUFLEN)
00252 {
00253 continue;
00254 }
00255 else
00256 {
00257 splt_e_set_error_data(state, mad_stream_errorstr(&mp3state->stream));
00258 *error = SPLT_ERROR_PLUGIN_ERROR;
00259 return -3;
00260 }
00261 }
00262 }
00263 else
00264 {
00265
00266 mp3state->data_ptr = (unsigned char *) mp3state->stream.this_frame;
00267 if (mp3state->stream.next_frame!=NULL)
00268 {
00269 mp3state->data_len = (long) (mp3state->stream.next_frame - mp3state->stream.this_frame);
00270 }
00271 ok = 1;
00272 }
00273
00274 } while (!ok);
00275
00276 return ok;
00277 }
00278