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 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #ifdef HAVE_MPGLIB
00031
00032 #include "lame.h"
00033 #include "machine.h"
00034 #include "encoder.h"
00035 #include "interface.h"
00036
00037
00038
00039 MPSTR mp;
00040 plotting_data *mpg123_pinfo = NULL;
00041
00042 int
00043 lame_decode_exit(void)
00044 {
00045 ExitMP3(&mp);
00046 return 0;
00047 }
00048
00049
00050 int
00051 lame_decode_init(void)
00052 {
00053 (void) InitMP3(&mp);
00054 return 0;
00055 }
00056
00057
00058
00059
00060
00061 #define COPY_MONO(DST_TYPE, SRC_TYPE) \
00062 DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw; \
00063 SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
00064 for (i = 0; i < processed_samples; i++) \
00065 *pcm_l++ = (DST_TYPE)(*p_samples++);
00066
00067
00068 #define COPY_STEREO(DST_TYPE, SRC_TYPE) \
00069 DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw, *pcm_r = (DST_TYPE *)pcm_r_raw; \
00070 SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
00071 for (i = 0; i < processed_samples; i++) { \
00072 *pcm_l++ = (DST_TYPE)(*p_samples++); \
00073 *pcm_r++ = (DST_TYPE)(*p_samples++); \
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 int
00086 lame_decode1_headersB_clipchoice(unsigned char *buffer, int len,
00087 char pcm_l_raw[], char pcm_r_raw[], mp3data_struct * mp3data,
00088 int *enc_delay, int *enc_padding,
00089 char *p, size_t psize, int decoded_sample_size,
00090 int (*decodeMP3_ptr) (PMPSTR, unsigned char *, int, char *, int,
00091 int *))
00092 {
00093 static const int smpls[2][4] = {
00094
00095 {0, 384, 1152, 1152},
00096 {0, 384, 1152, 576}
00097 };
00098
00099 int processed_bytes;
00100 int processed_samples;
00101 int ret;
00102 int i;
00103
00104 mp3data->header_parsed = 0;
00105
00106 ret = (*decodeMP3_ptr) (&mp, buffer, len, p, psize, &processed_bytes);
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 if (mp.header_parsed || mp.fsizeold > 0 || mp.framesize > 0) {
00125 mp3data->header_parsed = 1;
00126 mp3data->stereo = mp.fr.stereo;
00127 mp3data->samplerate = freqs[mp.fr.sampling_frequency];
00128 mp3data->mode = mp.fr.mode;
00129 mp3data->mode_ext = mp.fr.mode_ext;
00130 mp3data->framesize = smpls[mp.fr.lsf][mp.fr.lay];
00131
00132
00133
00134 if (mp.fsizeold > 0)
00135 mp3data->bitrate = 8 * (4 + mp.fsizeold) * mp3data->samplerate /
00136 (1.e3 * mp3data->framesize) + 0.5;
00137 else if (mp.framesize > 0)
00138 mp3data->bitrate = 8 * (4 + mp.framesize) * mp3data->samplerate /
00139 (1.e3 * mp3data->framesize) + 0.5;
00140 else
00141 mp3data->bitrate = tabsel_123[mp.fr.lsf][mp.fr.lay - 1][mp.fr.bitrate_index];
00142
00143
00144
00145 if (mp.num_frames > 0) {
00146
00147 mp3data->totalframes = mp.num_frames;
00148 mp3data->nsamp = mp3data->framesize * mp.num_frames;
00149 *enc_delay = mp.enc_delay;
00150 *enc_padding = mp.enc_padding;
00151 }
00152 }
00153
00154 switch (ret) {
00155 case MP3_OK:
00156 switch (mp.fr.stereo) {
00157 case 1:
00158 processed_samples = processed_bytes / decoded_sample_size;
00159 if (decoded_sample_size == sizeof(short)) {
00160 COPY_MONO(short, short)
00161 }
00162 else {
00163 COPY_MONO(sample_t, FLOAT)
00164 }
00165 break;
00166 case 2:
00167 processed_samples = (processed_bytes / decoded_sample_size) >> 1;
00168 if (decoded_sample_size == sizeof(short)) {
00169 COPY_STEREO(short, short)
00170 }
00171 else {
00172 COPY_STEREO(sample_t, FLOAT)
00173 }
00174 break;
00175 default:
00176 processed_samples = -1;
00177 assert(0);
00178 break;
00179 }
00180 break;
00181
00182 case MP3_NEED_MORE:
00183 processed_samples = 0;
00184 break;
00185
00186 case MP3_ERR:
00187 processed_samples = -1;
00188 break;
00189
00190 default:
00191 processed_samples = -1;
00192 assert(0);
00193 break;
00194 }
00195
00196
00197
00198 return processed_samples;
00199 }
00200
00201
00202 #define OUTSIZE_CLIPPED (4096*sizeof(short))
00203
00204 int
00205 lame_decode1_headersB(unsigned char *buffer,
00206 int len,
00207 short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
00208 int *enc_delay, int *enc_padding)
00209 {
00210 static char out[OUTSIZE_CLIPPED];
00211
00212 return lame_decode1_headersB_clipchoice(buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
00213 enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
00214 sizeof(short), decodeMP3);
00215 }
00216
00217
00218
00219 #define OUTSIZE_UNCLIPPED (1152*2*sizeof(FLOAT))
00220
00221 int
00222 lame_decode1_unclipped(unsigned char *buffer, int len, sample_t pcm_l[], sample_t pcm_r[])
00223 {
00224 static char out[OUTSIZE_UNCLIPPED];
00225 mp3data_struct mp3data;
00226 int enc_delay, enc_padding;
00227
00228 return lame_decode1_headersB_clipchoice(buffer, len, (char *) pcm_l, (char *) pcm_r, &mp3data,
00229 &enc_delay, &enc_padding, out, OUTSIZE_UNCLIPPED,
00230 sizeof(FLOAT), decodeMP3_unclipped);
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 int
00244 lame_decode1_headers(unsigned char *buffer,
00245 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
00246 {
00247 int enc_delay, enc_padding;
00248 return lame_decode1_headersB(buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
00249 }
00250
00251
00252 int
00253 lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
00254 {
00255 mp3data_struct mp3data;
00256
00257 return lame_decode1_headers(buffer, len, pcm_l, pcm_r, &mp3data);
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 int
00269 lame_decode_headers(unsigned char *buffer,
00270 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
00271 {
00272 int ret;
00273 int totsize = 0;
00274
00275 while (1) {
00276 switch (ret = lame_decode1_headers(buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
00277 case -1:
00278 return ret;
00279 case 0:
00280 return totsize;
00281 default:
00282 totsize += ret;
00283 len = 0;
00284 break;
00285 }
00286 }
00287 }
00288
00289
00290 int
00291 lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
00292 {
00293 mp3data_struct mp3data;
00294
00295 return lame_decode_headers(buffer, len, pcm_l, pcm_r, &mp3data);
00296 }
00297
00298
00299 #endif
00300
00301