mpglib_interface.c

Go to the documentation of this file.
00001 /* -*- mode: C; mode: fold -*- */
00002 /*
00003  *      LAME MP3 encoding engine
00004  *
00005  *      Copyright (c) 1999-2000 Mark Taylor
00006  *      Copyright (c) 2003 Olcios
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021  * Boston, MA 02111-1307, USA.
00022  */
00023 
00024 /* $Id: mpglib_interface.c,v 1.32 2007/07/24 17:46:10 bouvigne Exp $ */
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 /* copy mono samples */
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 /* copy stereo samples */
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  * For lame_decode:  return code
00080  * -1     error
00081  *  0     ok, but need more data before outputing any samples
00082  *  n     number of samples output.  either 576 or 1152 depending on MP3 file.
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         /* Layer   I    II   III */
00095         {0, 384, 1152, 1152}, /* MPEG-1     */
00096         {0, 384, 1152, 576} /* MPEG-2(.5) */
00097     };
00098 
00099     int     processed_bytes;
00100     int     processed_samples; /* processed samples per channel */
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     /* three cases:  
00108      * 1. headers parsed, but data not complete
00109      *       mp.header_parsed==1 
00110      *       mp.framesize=0           
00111      *       mp.fsizeold=size of last frame, or 0 if this is first frame
00112      *
00113      * 2. headers, data parsed, but ancillary data not complete
00114      *       mp.header_parsed==1 
00115      *       mp.framesize=size of frame           
00116      *       mp.fsizeold=size of last frame, or 0 if this is first frame
00117      *
00118      * 3. frame fully decoded:  
00119      *       mp.header_parsed==0 
00120      *       mp.framesize=0           
00121      *       mp.fsizeold=size of frame (which is now the last frame)
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         /* free format, we need the entire frame before we can determine
00133          * the bitrate.  If we haven't gotten the entire frame, bitrate=0 */
00134         if (mp.fsizeold > 0) /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
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             /* Xing VBR header found and num_frames was set */
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     /*fprintf(stderr,"ok, more, err:  %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR ); */
00197     /*fprintf(stderr,"ret = %i out=%i\n", ret, processed_samples ); */
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 /* we forbid input with more than 1152 samples per channel for output in the unclipped mode */
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  * For lame_decode:  return code
00237  *  -1     error
00238  *   0     ok, but need more data before outputing any samples
00239  *   n     number of samples output.  Will be at most one frame of
00240  *         MPEG data.  
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  * For lame_decode:  return code
00263  *  -1     error
00264  *   0     ok, but need more data before outputing any samples
00265  *   n     number of samples output.  a multiple of 576 or 1152 depending on MP3 file.
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;     /* number of decoded samples per channel */
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;    /* future calls to decodeMP3 are just to flush buffers */
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 /* end of mpglib_interface.c */

Generated on Sun Dec 2 11:34:19 2007 for LAME by  doxygen 1.5.2