1 /* -*- mode: C; mode: fold -*- */
2 /*
3 * LAME MP3 encoding engine
4 *
5 * Copyright (c) 1999-2000 Mark Taylor
6 * Copyright (c) 2003 Olcios
7 * Copyright (c) 2008 Robert Hegemann
8 *
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.
13 *
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.
18 *
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.
23 */
24
25 /* $Id: mpglib_interface.c,v 1.44 2012/02/18 13:09:00 robert Exp $ */
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #ifdef HAVE_MPGLIB
32 #define hip_global_struct mpstr_tag
33 #include "lame.h"
34 #include "machine.h"
35 #include "encoder.h"
36 #include "interface.h"
37
38 #include "util.h"
39
40
41
42 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
43 /*
44 * OBSOLETE:
45 * - kept to let it link
46 * - forward declaration to silence compiler
47 */
48 int CDECL lame_decode_init(void);
49 int CDECL lame_decode(
50 unsigned char * mp3buf,
51 int len,
52 short pcm_l[],
53 short pcm_r[] );
54 int CDECL lame_decode_headers(
55 unsigned char* mp3buf,
56 int len,
57 short pcm_l[],
58 short pcm_r[],
59 mp3data_struct* mp3data );
60 int CDECL lame_decode1(
61 unsigned char* mp3buf,
62 int len,
63 short pcm_l[],
64 short pcm_r[] );
65 int CDECL lame_decode1_headers(
66 unsigned char* mp3buf,
67 int len,
68 short pcm_l[],
69 short pcm_r[],
70 mp3data_struct* mp3data );
71 int CDECL lame_decode1_headersB(
72 unsigned char* mp3buf,
73 int len,
74 short pcm_l[],
75 short pcm_r[],
76 mp3data_struct* mp3data,
77 int *enc_delay,
78 int *enc_padding );
79 int CDECL lame_decode_exit(void);
80 #endif
81
82
83 static MPSTR mp;
84
85 int
lame_decode_exit(void)86 lame_decode_exit(void)
87 {
88 ExitMP3(&mp);
89 return 0;
90 }
91
92
93 int
lame_decode_init(void)94 lame_decode_init(void)
95 {
96 (void) InitMP3(&mp);
97 return 0;
98 }
99
100
101
102
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++);
109
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++); \
117 }
118
119
120
121 /*
122 * For lame_decode: return code
123 * -1 error
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.
126 */
127
128 static int
decode1_headersB_clipchoice(PMPSTR pmp,unsigned char * buffer,size_t len,char pcm_l_raw[],char pcm_r_raw[],mp3data_struct * mp3data,int * enc_delay,int * enc_padding,char * p,size_t psize,int decoded_sample_size,int (* decodeMP3_ptr)(PMPSTR,unsigned char *,int,char *,int,int *))129 decode1_headersB_clipchoice(PMPSTR pmp, unsigned char *buffer, size_t 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,
134 int *))
135 {
136 static const int smpls[2][4] = {
137 /* Layer I II III */
138 {0, 384, 1152, 1152}, /* MPEG-1 */
139 {0, 384, 1152, 576} /* MPEG-2(.5) */
140 };
141
142 int processed_bytes;
143 int processed_samples; /* processed samples per channel */
144 int ret;
145 int i;
146 int const len_l = len < INT_MAX ? (int) len : INT_MAX;
147 int const psize_l = psize < INT_MAX ? (int) psize : INT_MAX;
148
149 mp3data->header_parsed = 0;
150 ret = (*decodeMP3_ptr) (pmp, buffer, len_l, p, psize_l, &processed_bytes);
151 /* three cases:
152 * 1. headers parsed, but data not complete
153 * pmp->header_parsed==1
154 * pmp->framesize=0
155 * pmp->fsizeold=size of last frame, or 0 if this is first frame
156 *
157 * 2. headers, data parsed, but ancillary data not complete
158 * pmp->header_parsed==1
159 * pmp->framesize=size of frame
160 * pmp->fsizeold=size of last frame, or 0 if this is first frame
161 *
162 * 3. frame fully decoded:
163 * pmp->header_parsed==0
164 * pmp->framesize=0
165 * pmp->fsizeold=size of frame (which is now the last frame)
166 *
167 */
168 if (pmp->header_parsed || pmp->fsizeold > 0 || pmp->framesize > 0) {
169 mp3data->header_parsed = 1;
170 mp3data->stereo = pmp->fr.stereo;
171 mp3data->samplerate = freqs[pmp->fr.sampling_frequency];
172 mp3data->mode = pmp->fr.mode;
173 mp3data->mode_ext = pmp->fr.mode_ext;
174 mp3data->framesize = smpls[pmp->fr.lsf][pmp->fr.lay];
175
176 /* free format, we need the entire frame before we can determine
177 * the bitrate. If we haven't gotten the entire frame, bitrate=0 */
178 if (pmp->fsizeold > 0) /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
179 mp3data->bitrate = 8 * (4 + pmp->fsizeold) * mp3data->samplerate /
180 (1.e3 * mp3data->framesize) + 0.5;
181 else if (pmp->framesize > 0)
182 mp3data->bitrate = 8 * (4 + pmp->framesize) * mp3data->samplerate /
183 (1.e3 * mp3data->framesize) + 0.5;
184 else
185 mp3data->bitrate = tabsel_123[pmp->fr.lsf][pmp->fr.lay - 1][pmp->fr.bitrate_index];
186
187
188
189 if (pmp->num_frames > 0) {
190 /* Xing VBR header found and num_frames was set */
191 mp3data->totalframes = pmp->num_frames;
192 mp3data->nsamp = mp3data->framesize * pmp->num_frames;
193 *enc_delay = pmp->enc_delay;
194 *enc_padding = pmp->enc_padding;
195 }
196 }
197
198 switch (ret) {
199 case MP3_OK:
200 switch (pmp->fr.stereo) {
201 case 1:
202 processed_samples = processed_bytes / decoded_sample_size;
203 if (decoded_sample_size == sizeof(short)) {
204 COPY_MONO(short, short)
205 }
206 else {
207 COPY_MONO(sample_t, FLOAT)
208 }
209 break;
210 case 2:
211 processed_samples = (processed_bytes / decoded_sample_size) >> 1;
212 if (decoded_sample_size == sizeof(short)) {
213 COPY_STEREO(short, short)
214 }
215 else {
216 COPY_STEREO(sample_t, FLOAT)
217 }
218 break;
219 default:
220 processed_samples = -1;
221 assert(0);
222 break;
223 }
224 break;
225
226 case MP3_NEED_MORE:
227 processed_samples = 0;
228 break;
229
230 case MP3_ERR:
231 processed_samples = -1;
232 break;
233
234 default:
235 processed_samples = -1;
236 assert(0);
237 break;
238 }
239
240 /*fprintf(stderr,"ok, more, err: %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR ); */
241 /*fprintf(stderr,"ret = %i out=%i\n", ret, processed_samples ); */
242 return processed_samples;
243 }
244
245
246 #define OUTSIZE_CLIPPED (4096*sizeof(short))
247
248 int
lame_decode1_headersB(unsigned char * buffer,int len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data,int * enc_delay,int * enc_padding)249 lame_decode1_headersB(unsigned char *buffer,
250 int len,
251 short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
252 int *enc_delay, int *enc_padding)
253 {
254 static char out[OUTSIZE_CLIPPED];
255
256 return decode1_headersB_clipchoice(&mp, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
257 enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
258 sizeof(short), decodeMP3);
259 }
260
261
262
263
264
265 /*
266 * For lame_decode: return code
267 * -1 error
268 * 0 ok, but need more data before outputing any samples
269 * n number of samples output. Will be at most one frame of
270 * MPEG data.
271 */
272
273 int
lame_decode1_headers(unsigned char * buffer,int len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)274 lame_decode1_headers(unsigned char *buffer,
275 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
276 {
277 int enc_delay, enc_padding;
278 return lame_decode1_headersB(buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
279 }
280
281
282 int
lame_decode1(unsigned char * buffer,int len,short pcm_l[],short pcm_r[])283 lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
284 {
285 mp3data_struct mp3data;
286
287 return lame_decode1_headers(buffer, len, pcm_l, pcm_r, &mp3data);
288 }
289
290
291 /*
292 * For lame_decode: return code
293 * -1 error
294 * 0 ok, but need more data before outputing any samples
295 * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
296 */
297
298 int
lame_decode_headers(unsigned char * buffer,int len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)299 lame_decode_headers(unsigned char *buffer,
300 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
301 {
302 int ret;
303 int totsize = 0; /* number of decoded samples per channel */
304
305 for (;;) {
306 switch (ret = lame_decode1_headers(buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
307 case -1:
308 return ret;
309 case 0:
310 return totsize;
311 default:
312 totsize += ret;
313 len = 0; /* future calls to decodeMP3 are just to flush buffers */
314 break;
315 }
316 }
317 }
318
319
320 int
lame_decode(unsigned char * buffer,int len,short pcm_l[],short pcm_r[])321 lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
322 {
323 mp3data_struct mp3data;
324
325 return lame_decode_headers(buffer, len, pcm_l, pcm_r, &mp3data);
326 }
327
328
329
330
hip_decode_init(void)331 hip_t hip_decode_init(void)
332 {
333 hip_t hip = lame_calloc(hip_global_flags, 1);
334 InitMP3(hip);
335 return hip;
336 }
337
338
hip_decode_exit(hip_t hip)339 int hip_decode_exit(hip_t hip)
340 {
341 if (hip) {
342 ExitMP3(hip);
343 free(hip);
344 }
345 return 0;
346 }
347
348
349 /* we forbid input with more than 1152 samples per channel for output in the unclipped mode */
350 #define OUTSIZE_UNCLIPPED (1152*2*sizeof(FLOAT))
351
352 int
hip_decode1_unclipped(hip_t hip,unsigned char * buffer,size_t len,sample_t pcm_l[],sample_t pcm_r[])353 hip_decode1_unclipped(hip_t hip, unsigned char *buffer, size_t len, sample_t pcm_l[], sample_t pcm_r[])
354 {
355 static char out[OUTSIZE_UNCLIPPED];
356 mp3data_struct mp3data;
357 int enc_delay, enc_padding;
358
359 if (hip) {
360 return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, &mp3data,
361 &enc_delay, &enc_padding, out, OUTSIZE_UNCLIPPED,
362 sizeof(FLOAT), decodeMP3_unclipped);
363 }
364 return 0;
365 }
366
367 /*
368 * For hip_decode: return code
369 * -1 error
370 * 0 ok, but need more data before outputing any samples
371 * n number of samples output. Will be at most one frame of
372 * MPEG data.
373 */
374
375 int
hip_decode1_headers(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)376 hip_decode1_headers(hip_t hip, unsigned char *buffer,
377 size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
378 {
379 int enc_delay, enc_padding;
380 return hip_decode1_headersB(hip, buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
381 }
382
383
384 int
hip_decode1(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[])385 hip_decode1(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
386 {
387 mp3data_struct mp3data;
388 return hip_decode1_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
389 }
390
391
392 /*
393 * For hip_decode: return code
394 * -1 error
395 * 0 ok, but need more data before outputing any samples
396 * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
397 */
398
399 int
hip_decode_headers(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)400 hip_decode_headers(hip_t hip, unsigned char *buffer,
401 size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
402 {
403 int ret;
404 int totsize = 0; /* number of decoded samples per channel */
405
406 for (;;) {
407 switch (ret = hip_decode1_headers(hip, buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
408 case -1:
409 return ret;
410 case 0:
411 return totsize;
412 default:
413 totsize += ret;
414 len = 0; /* future calls to decodeMP3 are just to flush buffers */
415 break;
416 }
417 }
418 }
419
420
421 int
hip_decode(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[])422 hip_decode(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
423 {
424 mp3data_struct mp3data;
425 return hip_decode_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
426 }
427
428
429 int
hip_decode1_headersB(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data,int * enc_delay,int * enc_padding)430 hip_decode1_headersB(hip_t hip, unsigned char *buffer,
431 size_t len,
432 short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
433 int *enc_delay, int *enc_padding)
434 {
435 static char out[OUTSIZE_CLIPPED];
436 if (hip) {
437 return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
438 enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
439 sizeof(short), decodeMP3);
440 }
441 return -1;
442 }
443
444
hip_set_pinfo(hip_t hip,plotting_data * pinfo)445 void hip_set_pinfo(hip_t hip, plotting_data* pinfo)
446 {
447 if (hip) {
448 hip->pinfo = pinfo;
449 }
450 }
451
452
453
hip_set_errorf(hip_t hip,lame_report_function func)454 void hip_set_errorf(hip_t hip, lame_report_function func)
455 {
456 if (hip) {
457 hip->report_err = func;
458 }
459 }
460
hip_set_debugf(hip_t hip,lame_report_function func)461 void hip_set_debugf(hip_t hip, lame_report_function func)
462 {
463 if (hip) {
464 hip->report_dbg = func;
465 }
466 }
467
hip_set_msgf(hip_t hip,lame_report_function func)468 void hip_set_msgf (hip_t hip, lame_report_function func)
469 {
470 if (hip) {
471 hip->report_msg = func;
472 }
473 }
474
475 #endif
476
477 /* end of mpglib_interface.c */
478