1 /* -*- mode: C; mode: fold -*- */
3 * LAME MP3 encoding engine
5 * Copyright (c) 1999-2000 Mark Taylor
6 * Copyright (c) 2003 Olcios
7 * Copyright (c) 2008 Robert Hegemann
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 /* $Id: mpglib_interface.c,v 1.42 2011/05/07 16:05:17 rbrito Exp $ */
32 #define hip_global_struct mpstr_tag
36 #include "interface.h"
42 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
45 * - kept to let it link
46 * - forward declaration to silence compiler
48 int CDECL lame_decode_init(void);
49 int CDECL lame_decode(
50 unsigned char * mp3buf,
54 int CDECL lame_decode_headers(
55 unsigned char* mp3buf,
59 mp3data_struct* mp3data );
60 int CDECL lame_decode1(
61 unsigned char* mp3buf,
65 int CDECL lame_decode1_headers(
66 unsigned char* mp3buf,
70 mp3data_struct* mp3data );
71 int CDECL lame_decode1_headersB(
72 unsigned char* mp3buf,
76 mp3data_struct* mp3data,
79 int CDECL lame_decode_exit(void);
86 lame_decode_exit(void)
94 lame_decode_init(void)
103 /* copy mono samples */
104 #define COPY_MONO(DST_TYPE, SRC_TYPE) \
105 DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw; \
106 SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
107 for (i = 0; i < processed_samples; i++) \
108 *pcm_l++ = (DST_TYPE)(*p_samples++);
110 /* copy stereo samples */
111 #define COPY_STEREO(DST_TYPE, SRC_TYPE) \
112 DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw, *pcm_r = (DST_TYPE *)pcm_r_raw; \
113 SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
114 for (i = 0; i < processed_samples; i++) { \
115 *pcm_l++ = (DST_TYPE)(*p_samples++); \
116 *pcm_r++ = (DST_TYPE)(*p_samples++); \
122 * For lame_decode: return code
124 * 0 ok, but need more data before outputing any samples
125 * n number of samples output. either 576 or 1152 depending on MP3 file.
129 decode1_headersB_clipchoice(PMPSTR pmp, unsigned char *buffer, int len,
130 char pcm_l_raw[], char pcm_r_raw[], mp3data_struct * mp3data,
131 int *enc_delay, int *enc_padding,
132 char *p, size_t psize, int decoded_sample_size,
133 int (*decodeMP3_ptr) (PMPSTR, unsigned char *, int, char *, int,
136 static const int smpls[2][4] = {
138 {0, 384, 1152, 1152}, /* MPEG-1 */
139 {0, 384, 1152, 576} /* MPEG-2(.5) */
143 int processed_samples; /* processed samples per channel */
147 mp3data->header_parsed = 0;
149 ret = (*decodeMP3_ptr) (pmp, buffer, len, p, (int) psize, &processed_bytes);
151 * 1. headers parsed, but data not complete
152 * pmp->header_parsed==1
154 * pmp->fsizeold=size of last frame, or 0 if this is first frame
156 * 2. headers, data parsed, but ancillary data not complete
157 * pmp->header_parsed==1
158 * pmp->framesize=size of frame
159 * pmp->fsizeold=size of last frame, or 0 if this is first frame
161 * 3. frame fully decoded:
162 * pmp->header_parsed==0
164 * pmp->fsizeold=size of frame (which is now the last frame)
167 if (pmp->header_parsed || pmp->fsizeold > 0 || pmp->framesize > 0) {
168 mp3data->header_parsed = 1;
169 mp3data->stereo = pmp->fr.stereo;
170 mp3data->samplerate = freqs[pmp->fr.sampling_frequency];
171 mp3data->mode = pmp->fr.mode;
172 mp3data->mode_ext = pmp->fr.mode_ext;
173 mp3data->framesize = smpls[pmp->fr.lsf][pmp->fr.lay];
175 /* free format, we need the entire frame before we can determine
176 * the bitrate. If we haven't gotten the entire frame, bitrate=0 */
177 if (pmp->fsizeold > 0) /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
178 mp3data->bitrate = 8 * (4 + pmp->fsizeold) * mp3data->samplerate /
179 (1.e3 * mp3data->framesize) + 0.5;
180 else if (pmp->framesize > 0)
181 mp3data->bitrate = 8 * (4 + pmp->framesize) * mp3data->samplerate /
182 (1.e3 * mp3data->framesize) + 0.5;
184 mp3data->bitrate = tabsel_123[pmp->fr.lsf][pmp->fr.lay - 1][pmp->fr.bitrate_index];
188 if (pmp->num_frames > 0) {
189 /* Xing VBR header found and num_frames was set */
190 mp3data->totalframes = pmp->num_frames;
191 mp3data->nsamp = mp3data->framesize * pmp->num_frames;
192 *enc_delay = pmp->enc_delay;
193 *enc_padding = pmp->enc_padding;
199 switch (pmp->fr.stereo) {
201 processed_samples = processed_bytes / decoded_sample_size;
202 if (decoded_sample_size == sizeof(short)) {
203 COPY_MONO(short, short)
206 COPY_MONO(sample_t, FLOAT)
210 processed_samples = (processed_bytes / decoded_sample_size) >> 1;
211 if (decoded_sample_size == sizeof(short)) {
212 COPY_STEREO(short, short)
215 COPY_STEREO(sample_t, FLOAT)
219 processed_samples = -1;
226 processed_samples = 0;
230 processed_samples = -1;
234 processed_samples = -1;
239 /*fprintf(stderr,"ok, more, err: %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR ); */
240 /*fprintf(stderr,"ret = %i out=%i\n", ret, processed_samples ); */
241 return processed_samples;
245 #define OUTSIZE_CLIPPED (4096*sizeof(short))
248 lame_decode1_headersB(unsigned char *buffer,
250 short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
251 int *enc_delay, int *enc_padding)
253 static char out[OUTSIZE_CLIPPED];
255 return decode1_headersB_clipchoice(&mp, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
256 enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
257 sizeof(short), decodeMP3);
265 * For lame_decode: return code
267 * 0 ok, but need more data before outputing any samples
268 * n number of samples output. Will be at most one frame of
273 lame_decode1_headers(unsigned char *buffer,
274 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
276 int enc_delay, enc_padding;
277 return lame_decode1_headersB(buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
282 lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
284 mp3data_struct mp3data;
286 return lame_decode1_headers(buffer, len, pcm_l, pcm_r, &mp3data);
291 * For lame_decode: return code
293 * 0 ok, but need more data before outputing any samples
294 * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
298 lame_decode_headers(unsigned char *buffer,
299 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
302 int totsize = 0; /* number of decoded samples per channel */
305 switch (ret = lame_decode1_headers(buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
312 len = 0; /* future calls to decodeMP3 are just to flush buffers */
320 lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
322 mp3data_struct mp3data;
324 return lame_decode_headers(buffer, len, pcm_l, pcm_r, &mp3data);
330 hip_t hip_decode_init(void)
332 hip_t hip = calloc(1, sizeof(hip_global_flags));
338 int hip_decode_exit(hip_t hip)
348 /* we forbid input with more than 1152 samples per channel for output in the unclipped mode */
349 #define OUTSIZE_UNCLIPPED (1152*2*sizeof(FLOAT))
352 hip_decode1_unclipped(hip_t hip, unsigned char *buffer, size_t len, sample_t pcm_l[], sample_t pcm_r[])
354 static char out[OUTSIZE_UNCLIPPED];
355 mp3data_struct mp3data;
356 int enc_delay, enc_padding;
359 return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, &mp3data,
360 &enc_delay, &enc_padding, out, OUTSIZE_UNCLIPPED,
361 sizeof(FLOAT), decodeMP3_unclipped);
367 * For hip_decode: return code
369 * 0 ok, but need more data before outputing any samples
370 * n number of samples output. Will be at most one frame of
375 hip_decode1_headers(hip_t hip, unsigned char *buffer,
376 size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
378 int enc_delay, enc_padding;
379 return hip_decode1_headersB(hip, buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
384 hip_decode1(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
386 mp3data_struct mp3data;
387 return hip_decode1_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
392 * For hip_decode: return code
394 * 0 ok, but need more data before outputing any samples
395 * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
399 hip_decode_headers(hip_t hip, unsigned char *buffer,
400 size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
403 int totsize = 0; /* number of decoded samples per channel */
406 switch (ret = hip_decode1_headers(hip, buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
413 len = 0; /* future calls to decodeMP3 are just to flush buffers */
421 hip_decode(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
423 mp3data_struct mp3data;
424 return hip_decode_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
429 hip_decode1_headersB(hip_t hip, unsigned char *buffer,
431 short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
432 int *enc_delay, int *enc_padding)
434 static char out[OUTSIZE_CLIPPED];
436 return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
437 enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
438 sizeof(short), decodeMP3);
444 void hip_set_pinfo(hip_t hip, plotting_data* pinfo)
453 void hip_set_errorf(hip_t hip, lame_report_function func)
456 hip->report_err = func;
460 void hip_set_debugf(hip_t hip, lame_report_function func)
463 hip->report_dbg = func;
467 void hip_set_msgf (hip_t hip, lame_report_function func)
470 hip->report_msg = func;
476 /* end of mpglib_interface.c */