Debian Codegraph @

libmp3lame/mpglib_interface.c

raw
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$ */
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #define hip_global_struct mpstr_tag
32
33 #ifdef HAVE_MPG123
34 /* libmpg123 */
35 #include <mpg123.h>
36 #ifndef MPG123_API_VERSION
37 #error "Seems like you got the wrong mpg123 header. No MPG123_API_VERSION defined."
38 #endif
39 #if (MPG123_API_VERSION < 45)
40 #error "Need mpg123 API >= 45."
41 #endif
42
43 #endif /* HAVE_MPG123 */
44
45 /* for mpstr_tag */
46 #include "mpglib/mpglib.h"
47
48 #include "lame.h"
49 #include "machine.h"
50 #include "encoder.h"
51
52 /* for plotting_data */
53 #ifndef NOANALYSIS
54 #include "lame-analysis.h"
55 #endif
56
57 #include "util.h"
58
59 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
60 /*
61 * OBSOLETE:
62 * - kept to let it link
63 * - forward declaration to silence compiler
64 */
65 int CDECL lame_decode_init(void);
66 int CDECL lame_decode(
67 unsigned char * mp3buf,
68 int len,
69 short pcm_l[],
70 short pcm_r[] );
71 int CDECL lame_decode_headers(
72 unsigned char* mp3buf,
73 int len,
74 short pcm_l[],
75 short pcm_r[],
76 mp3data_struct* mp3data );
77 int CDECL lame_decode1(
78 unsigned char* mp3buf,
79 int len,
80 short pcm_l[],
81 short pcm_r[] );
82 int CDECL lame_decode1_headers(
83 unsigned char* mp3buf,
84 int len,
85 short pcm_l[],
86 short pcm_r[],
87 mp3data_struct* mp3data );
88 int CDECL lame_decode1_headersB(
89 unsigned char* mp3buf,
90 int len,
91 short pcm_l[],
92 short pcm_r[],
93 mp3data_struct* mp3data,
94 int *enc_delay,
95 int *enc_padding );
96 int CDECL lame_decode_exit(void);
97 #endif
98
99 int
100 lame_decode_exit(void)
101 {
102 return 0;
103 }
104
105
106 int
107 lame_decode_init(void)
108 {
109 return 0;
110 }
111
112
113
114
115 /* copy mono samples */
116 #define COPY_MONO(DST_TYPE, SRC_TYPE) \
117 DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw; \
118 SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
119 for (i = 0; i < processed_samples; i++) \
120 *pcm_l++ = (DST_TYPE)(*p_samples++);
121
122 /* copy stereo samples */
123 #define COPY_STEREO(DST_TYPE, SRC_TYPE) \
124 DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw, *pcm_r = (DST_TYPE *)pcm_r_raw; \
125 SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
126 for (i = 0; i < processed_samples; i++) { \
127 *pcm_l++ = (DST_TYPE)(*p_samples++); \
128 *pcm_r++ = (DST_TYPE)(*p_samples++); \
129 }
130
131
132
133
134 #define OUTSIZE_CLIPPED (4096*sizeof(short))
135
136 int
137 lame_decode1_headersB(unsigned char *buffer,
138 int len,
139 short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
140 int *enc_delay, int *enc_padding)
141 {
142 return -1;
143 }
144
145
146
147
148
149 /*
150 * For lame_decode: return code
151 * -1 error
152 * 0 ok, but need more data before outputing any samples
153 * n number of samples output. Will be at most one frame of
154 * MPEG data.
155 */
156
157 int
158 lame_decode1_headers(unsigned char *buffer,
159 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
160 {
161 return -1;
162 }
163
164
165 int
166 lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
167 {
168 return -1;
169 }
170
171
172 /*
173 * For lame_decode: return code
174 * -1 error
175 * 0 ok, but need more data before outputing any samples
176 * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
177 */
178
179 int
180 lame_decode_headers(unsigned char *buffer,
181 int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
182 {
183 return -1;
184 }
185
186
187 int
188 lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
189 {
190 return -1;
191 }
192
193
194
195
196 hip_t hip_decode_init(void)
197 {
198 hip_t hip = lame_calloc(hip_global_flags, 1);
199 if(!hip)
200 return hip;
201 #ifdef HAVE_MPG123
202 mpg123_init();
203 hip->mh = mpg123_new(NULL, NULL);
204 /* Could allocate on demand only. */
205 memset(&hip->mi, 0, sizeof(hip->mi));
206 /* Since encoder delay/padding is communicated, I presume implicit
207 handling of gapless decoding is not expected. */
208 mpg123_param(hip->mh, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0.);
209 /* We are going to feed buffers. */
210 if(mpg123_open_feed(hip->mh) != MPG123_OK)
211 {
212 mpg123_delete(hip->mh);
213 free(hip);
214 hip = NULL;
215 }
216 #endif
217 return hip;
218 }
219
220 hip_t hip_decode_init_gapless(void)
221 {
222 hip_t hip = lame_calloc(hip_global_flags, 1);
223 if(!hip)
224 return hip;
225 #ifdef HAVE_MPG123
226 mpg123_init();
227 hip->mh = mpg123_new(NULL, NULL);
228 /* Could allocate on demand only. */
229 memset(&hip->mi, 0, sizeof(hip->mi));
230 /* Default on, but make it explicit. */
231 mpg123_param(hip->mh, MPG123_ADD_FLAGS, MPG123_GAPLESS, 0.);
232 /* We are going to feed buffers. */
233 if(mpg123_open_feed(hip->mh) != MPG123_OK)
234 {
235 mpg123_delete(hip->mh);
236 free(hip);
237 hip = NULL;
238 }
239 #else
240 hip = NULL;
241 #endif
242 return hip;
243 }
244
245
246
247 int hip_decode_exit(hip_t hip)
248 {
249 if(hip) {
250 #ifdef HAVE_MPG123
251 mpg123_delete(hip->mh); /* Closes implicitly. */
252 /* No mpg123_exit(), will be deprecated anyway. */
253 #endif
254 free(hip);
255 }
256 return 0;
257 }
258
259 #ifdef HAVE_MPG123
260 /* One decoding routine to cover all API cases. Any output pointer except pcm_l
261 and pcm_r, which are always required to be able to store full MPEG frame
262 (1152 samples), can be NULL if you are not really interested in it.
263 This always works on one whole MPEG frame, even if sample count can be
264 smaller after gapless handling. TODO: Optionally turn on gapless decoding?
265 If not, the decoder delay also needs to be communicated.
266 Or do we just assume 529 samples? */
267
268 int hip123_decode1( hip_t hip, unsigned char *buffer, size_t len,
269 unsigned char *pcm_l, unsigned char *pcm_r,
270 int *enc_delay, int *enc_padding,
271 mp3data_struct *mp3data,
272 int unclipped) /* If true, produce unclipped float (sample_t) output. */
273 {
274 int ret;
275 unsigned char *mpg123buf;
276 size_t mpg123fill;
277 long rate;
278 int channels;
279 int encoding;
280 int change_format;
281 int samples = 0;
282 int want_enc = unclipped ? MPG123_ENC_FLOAT_32 : MPG123_ENC_SIGNED_16;
283
284 if(MPG123_OK != mpg123_feed(hip->mh, buffer, len))
285 return -1;
286 ret = mpg123_getformat(hip->mh, &rate, &channels, &encoding);
287 switch(ret) {
288 case MPG123_NEED_MORE:
289 return 0;
290 case MPG123_OK:
291 change_format = encoding != want_enc;
292 break;
293 default:
294 return -1;
295 }
296
297 if(change_format)
298 {
299 mpg123_format_none(hip->mh);
300 mpg123_format2(hip->mh, 0, MPG123_MONO|MPG123_STEREO, want_enc);
301 /* This triggers renegotiation of output format on next decode. */
302 mpg123_decoder(hip->mh, NULL);
303 }
304
305 /* Now decode for real. */
306 mpg123fill = 0; /* Still zero in case of error/need more. */
307 ret = mpg123_decode_frame(hip->mh, NULL, &mpg123buf, &mpg123fill);
308 /* A second time if we just got notified of new format. */
309 if(!mpg123fill && ret == MPG123_NEW_FORMAT)
310 {
311 mpg123_getformat(hip->mh, &rate, &channels, &encoding);
312 ret = mpg123_decode_frame(hip->mh, NULL, &mpg123buf, &mpg123fill);
313 /* True paranoia would check the encoding again. */
314 }
315 if(ret == MPG123_ERR)
316 return -1;
317 /* MPG123_NEED_MORE and MPG123_DONE (not happening here, though)
318 both result in mpg123fill==0, so return 0 here, which is what fits. */
319 samples = mpg123fill /
320 (unclipped ? sizeof(float) : sizeof(short)) / channels;
321 /* Now demultilex the data in mpg123buf into pcm_l and pcm_r. */
322 if(mpg123fill && mpg123buf)
323 {
324 if(unclipped)
325 {
326 /* Lame's sample_t could be wider than 32 bit, right? */
327 sample_t *spcm_l = (sample_t*)pcm_l;
328 sample_t *spcm_r = (sample_t*)pcm_r;
329 float *srcbuf = (float*)mpg123buf;
330 int i;
331
332 if(channels == 2) {
333 for(i=0; i<samples; ++i) {
334 spcm_l[i] = *srcbuf++;
335 spcm_r[i] = *srcbuf++;
336 }
337 }
338 else
339 for(i=0; i<samples; ++i)
340 spcm_l[i] = *srcbuf++;
341 }
342 else
343 {
344 /* It's all shorts. */
345 short *spcm_l = (short*)pcm_l;
346 short *spcm_r = (short*)pcm_r;
347 short *srcbuf = (short*)mpg123buf;
348 int i;
349
350 if(channels == 2) {
351 for(i=0; i<samples; ++i) {
352 spcm_l[i] = *srcbuf++;
353 spcm_r[i] = *srcbuf++;
354 }
355 }
356 else
357 memcpy(pcm_l, mpg123buf, sizeof(short)*samples);
358 }
359 }
360
361 /* If we arrive here, there was some successful parsing of the stream at
362 least, so that meaningful info is available. */
363 if(mp3data) {
364 struct mpg123_frameinfo fi;
365 memset(mp3data, 0, sizeof(mp3data_struct));
366 /* Re-using last returns from getformat() before. */
367 if(MPG123_OK == mpg123_info(hip->mh, &fi)) {
368 mp3data->header_parsed = 1;
369 mp3data->stereo = channels; /* Channel count correct? Or is dual mono different? */
370 mp3data->samplerate = rate;
371 mp3data->mode = fi.mode;
372 mp3data->mode_ext = fi.mode_ext;
373 mp3data->framesize = mpg123_spf(hip->mh);
374 mp3data->bitrate = fi.bitrate;
375 }
376 }
377 if(enc_delay) {
378 long val;
379 mpg123_getstate(hip->mh, MPG123_ENC_DELAY, &val, NULL);
380 *enc_delay = val > INT_MAX ? -1 : val;
381 }
382 if(enc_padding) {
383 long val;
384 mpg123_getstate(hip->mh, MPG123_ENC_PADDING, &val, NULL);
385 *enc_padding = val > INT_MAX ? -1 : val;
386 }
387 if(hip->pinfo)
388 hip_finish_pinfo(hip);
389 return samples;
390 }
391 #endif
392
393
394 /* we forbid input with more than 1152 samples per channel for output in the unclipped mode */
395 #define OUTSIZE_UNCLIPPED (1152*2*sizeof(FLOAT))
396
397 int
398 hip_decode1_unclipped(hip_t hip, unsigned char *buffer, size_t len, sample_t pcm_l[], sample_t pcm_r[])
399 {
400 if (hip) {
401 #ifdef HAVE_MPG123
402 return hip123_decode1( hip, buffer, len,
403 (unsigned char*)pcm_l, (unsigned char*)pcm_r,
404 NULL, NULL, NULL, 1 );
405 #endif
406 }
407 return 0; /* not -1 ? */
408 }
409
410 /*
411 * For hip_decode: return code
412 * -1 error
413 * 0 ok, but need more data before outputing any samples
414 * n number of samples output. Will be at most one frame of
415 * MPEG data.
416 */
417
418 int
419 hip_decode1_headers(hip_t hip, unsigned char *buffer,
420 size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
421 {
422 #ifdef HAVE_MPG123
423 return hip123_decode1( hip, buffer, len,
424 (unsigned char*)pcm_l, (unsigned char*)pcm_r,
425 NULL, NULL, mp3data, 0 );
426 #else
427 int enc_delay, enc_padding;
428 return hip_decode1_headersB(hip, buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
429 #endif
430 }
431
432
433 int
434 hip_decode1(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
435 {
436 #ifdef HAVE_MPG123
437 return hip123_decode1( hip, buffer, len,
438 (unsigned char*)pcm_l, (unsigned char*)pcm_r,
439 NULL, NULL, NULL, 0 );
440 #else
441 mp3data_struct mp3data;
442 return hip_decode1_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
443 #endif
444 }
445
446
447 /*
448 * For hip_decode: return code
449 * -1 error
450 * 0 ok, but need more data before outputing any samples
451 * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
452 */
453
454 int
455 hip_decode_headers(hip_t hip, unsigned char *buffer,
456 size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
457 {
458 int ret;
459 int totsize = 0; /* number of decoded samples per channel */
460
461 for (;;) {
462 switch (ret = hip_decode1_headers(hip, buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
463 case -1:
464 return ret;
465 case 0:
466 return totsize;
467 default:
468 totsize += ret;
469 len = 0; /* future calls to decodeMP3 are just to flush buffers */
470 break;
471 }
472 }
473 }
474
475
476 int
477 hip_decode(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
478 {
479 mp3data_struct mp3data;
480 return hip_decode_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
481 }
482
483
484 int
485 hip_decode1_headersB(hip_t hip, unsigned char *buffer,
486 size_t len,
487 short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
488 int *enc_delay, int *enc_padding)
489 {
490 if (hip) {
491 #ifdef HAVE_MPG123
492 return hip123_decode1( hip, buffer, len,
493 (unsigned char*)pcm_l, (unsigned char*)pcm_r,
494 enc_delay, enc_padding, mp3data, 0);
495 #endif
496 }
497 return -1;
498 }
499
500
501 void hip_set_pinfo(hip_t hip, plotting_data* pinfo)
502 {
503 if (hip) {
504 hip->pinfo = pinfo;
505 #ifdef HAVE_MPG123
506 mpg123_set_moreinfo(hip->mh, &hip->mi);
507 #endif
508 }
509 }
510
511 void hip_finish_pinfo(hip_t hip)
512 {
513 #ifndef NOANALYSIS
514 #ifdef HAVE_MPG123
515 struct mpg123_frameinfo fi;
516 long rate;
517 plotting_data *pinfo = hip->pinfo;
518 if(!hip || !hip->pinfo)
519 return;
520
521 /* TODO: convert to pointers to avoid copies. Allocation should be
522 on mpg123 side (in form of the struct definition), as that is
523 the writing side. */
524 memcpy(pinfo->mpg123xr, hip->mi.xr, sizeof(pinfo->mpg123xr));
525 memcpy(pinfo->sfb, hip->mi.sfb, sizeof(pinfo->sfb));
526 memcpy(pinfo->sfb_s, hip->mi.sfb_s, sizeof(pinfo->sfb_s));
527 memcpy(pinfo->qss, hip->mi.qss, sizeof(pinfo->qss));
528 memcpy(pinfo->big_values, hip->mi.big_values, sizeof(pinfo->big_values));
529 memcpy(pinfo->sub_gain, hip->mi.sub_gain, sizeof(pinfo->sub_gain));
530 memcpy(pinfo->scalefac_scale, hip->mi.scalefac_scale, sizeof(pinfo->scalefac_scale));
531 memcpy(pinfo->preflag, hip->mi.preflag, sizeof(pinfo->preflag));
532 memcpy(pinfo->mpg123blocktype, hip->mi.blocktype, sizeof(pinfo->mpg123blocktype));
533 memcpy(pinfo->mixed, hip->mi.mixed, sizeof(pinfo->mixed));
534 memcpy(pinfo->mainbits, hip->mi.mainbits, sizeof(pinfo->mainbits));
535 memcpy(pinfo->sfbits, hip->mi.sfbits, sizeof(pinfo->sfbits));
536 memcpy(pinfo->scfsi, hip->mi.scfsi, sizeof(pinfo->scfsi));
537 pinfo->maindata = hip->mi.maindata;
538 pinfo->padding = hip->mi.padding;
539 if(MPG123_OK == mpg123_info(hip->mh, &fi)) {
540 pinfo->js = (fi.mode == MPG123_M_JOINT);
541 pinfo->stereo = fi.mode == MPG123_M_MONO ? 1 : 2;
542 pinfo->crc = fi.flags & MPG123_CRC ? 1 : 0;
543 pinfo->emph = fi.emphasis;
544 pinfo->sampfreq = fi.rate;
545 pinfo->bitrate = fi.bitrate;
546 pinfo->ms_stereo = pinfo->js ? (fi.mode_ext & 0x2)>>1 : 0;
547 pinfo->i_stereo = pinfo->js ? (fi.mode_ext & 0x1) : 0;
548 }
549 #endif
550 #endif
551 }
552
553 void hip_set_errorf(hip_t hip, lame_report_function func)
554 {
555 #ifdef HAVE_MPG123
556 /* TODO: implement something */
557 #endif
558 }
559
560 void hip_set_debugf(hip_t hip, lame_report_function func)
561 {
562 #ifdef HAVE_MPG123
563 /* TODO: implement something */
564 #endif
565 }
566
567 void hip_set_msgf (hip_t hip, lame_report_function func)
568 {
569 #ifdef HAVE_MPG123
570 /* TODO: implement something */
571 #endif
572 }
573
574 /* end of mpglib_interface.c */