• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(HAVE_MPGLIB) || defined(HAVE_MPG123)
32 #define hip_global_struct mpstr_tag
33 
34 #ifdef HAVE_MPG123
35 
36 /* libmpg123 */
37 #include <mpg123.h>
38 #ifndef MPG123_API_VERSION
39 #error "Seems like you got the wrong mpg123 header. No MPG123_API_VERSION defined."
40 #endif
41 #if (MPG123_API_VERSION < 45)
42 #error "Need mpg123 API >= 45."
43 #endif
44 /* for mpstr_tag */
45 #include "mpglib/mpglib.h"
46 
47 #else
48 
49 /* mpglib */
50 #include "mpglib/interface.h"
51 
52 #endif
53 
54 #include "lame.h"
55 #include "machine.h"
56 #include "encoder.h"
57 
58 /* for plotting_data */
59 #ifndef NOANALYSIS
60 #include "lame-analysis.h"
61 #endif
62 
63 #include "util.h"
64 
65 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
66 /*
67  * OBSOLETE:
68  * - kept to let it link
69  * - forward declaration to silence compiler
70  */
71 int CDECL lame_decode_init(void);
72 int CDECL lame_decode(
73         unsigned char *  mp3buf,
74         int              len,
75         short            pcm_l[],
76         short            pcm_r[] );
77 int CDECL lame_decode_headers(
78         unsigned char*   mp3buf,
79         int              len,
80         short            pcm_l[],
81         short            pcm_r[],
82         mp3data_struct*  mp3data );
83 int CDECL lame_decode1(
84         unsigned char*  mp3buf,
85         int             len,
86         short           pcm_l[],
87         short           pcm_r[] );
88 int CDECL lame_decode1_headers(
89         unsigned char*   mp3buf,
90         int              len,
91         short            pcm_l[],
92         short            pcm_r[],
93         mp3data_struct*  mp3data );
94 int CDECL lame_decode1_headersB(
95         unsigned char*   mp3buf,
96         int              len,
97         short            pcm_l[],
98         short            pcm_r[],
99         mp3data_struct*  mp3data,
100         int              *enc_delay,
101         int              *enc_padding );
102 int CDECL lame_decode_exit(void);
103 #endif
104 
105 #ifdef HAVE_MPGLIB
106 static MPSTR   mp;
107 #endif
108 
109 int
lame_decode_exit(void)110 lame_decode_exit(void)
111 {
112 #ifdef HAVE_MPGLIB
113     ExitMP3(&mp);
114 #endif
115     return 0;
116 }
117 
118 
119 int
lame_decode_init(void)120 lame_decode_init(void)
121 {
122 #ifdef HAVE_MPGLIB
123     (void) InitMP3(&mp);
124 #endif
125     return 0;
126 }
127 
128 
129 
130 
131 /* copy mono samples */
132 #define COPY_MONO(DST_TYPE, SRC_TYPE)                                                           \
133     DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw;                                                    \
134     SRC_TYPE const *p_samples = (SRC_TYPE const *)p;                                            \
135     for (i = 0; i < processed_samples; i++)                                                     \
136       *pcm_l++ = (DST_TYPE)(*p_samples++);
137 
138 /* copy stereo samples */
139 #define COPY_STEREO(DST_TYPE, SRC_TYPE)                                                         \
140     DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw, *pcm_r = (DST_TYPE *)pcm_r_raw;                    \
141     SRC_TYPE const *p_samples = (SRC_TYPE const *)p;                                            \
142     for (i = 0; i < processed_samples; i++) {                                                   \
143       *pcm_l++ = (DST_TYPE)(*p_samples++);                                                      \
144       *pcm_r++ = (DST_TYPE)(*p_samples++);                                                      \
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.  either 576 or 1152 depending on MP3 file.
154  */
155 
156 #ifdef HAVE_MPGLIB
157 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 *))158 decode1_headersB_clipchoice(PMPSTR pmp, unsigned char *buffer, size_t len,
159                             char pcm_l_raw[], char pcm_r_raw[], mp3data_struct * mp3data,
160                             int *enc_delay, int *enc_padding,
161                             char *p, size_t psize, int decoded_sample_size,
162                             int (*decodeMP3_ptr) (PMPSTR, unsigned char *, int, char *, int,
163                             int *))
164 {
165     static const int smpls[2][4] = {
166         /* Layer   I    II   III */
167         {0, 384, 1152, 1152}, /* MPEG-1     */
168         {0, 384, 1152, 576} /* MPEG-2(.5) */
169     };
170 
171     int     processed_bytes;
172     int     processed_samples; /* processed samples per channel */
173     int     ret;
174     int     i;
175     int const len_l = len < INT_MAX ? (int) len : INT_MAX;
176     int const psize_l = psize < INT_MAX ? (int) psize : INT_MAX;
177 
178     mp3data->header_parsed = 0;
179     ret = (*decodeMP3_ptr) (pmp, buffer, len_l, p, psize_l, &processed_bytes);
180     /* three cases:
181      * 1. headers parsed, but data not complete
182      *       pmp->header_parsed==1
183      *       pmp->framesize=0
184      *       pmp->fsizeold=size of last frame, or 0 if this is first frame
185      *
186      * 2. headers, data parsed, but ancillary data not complete
187      *       pmp->header_parsed==1
188      *       pmp->framesize=size of frame
189      *       pmp->fsizeold=size of last frame, or 0 if this is first frame
190      *
191      * 3. frame fully decoded:
192      *       pmp->header_parsed==0
193      *       pmp->framesize=0
194      *       pmp->fsizeold=size of frame (which is now the last frame)
195      *
196      */
197     if (pmp->header_parsed || pmp->fsizeold > 0 || pmp->framesize > 0) {
198         mp3data->header_parsed = 1;
199         mp3data->stereo = pmp->fr.stereo;
200         mp3data->samplerate = freqs[pmp->fr.sampling_frequency];
201         mp3data->mode = pmp->fr.mode;
202         mp3data->mode_ext = pmp->fr.mode_ext;
203         mp3data->framesize = smpls[pmp->fr.lsf][pmp->fr.lay];
204 
205         /* free format, we need the entire frame before we can determine
206          * the bitrate.  If we haven't gotten the entire frame, bitrate=0 */
207         if (pmp->fsizeold > 0) /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
208             mp3data->bitrate = 8 * (4 + pmp->fsizeold) * mp3data->samplerate /
209                 (1.e3 * mp3data->framesize) + 0.5;
210         else if (pmp->framesize > 0)
211             mp3data->bitrate = 8 * (4 + pmp->framesize) * mp3data->samplerate /
212                 (1.e3 * mp3data->framesize) + 0.5;
213         else
214             mp3data->bitrate = tabsel_123[pmp->fr.lsf][pmp->fr.lay - 1][pmp->fr.bitrate_index];
215 
216 
217 
218         if (pmp->num_frames > 0) {
219             /* Xing VBR header found and num_frames was set */
220             mp3data->totalframes = pmp->num_frames;
221             mp3data->nsamp = mp3data->framesize * pmp->num_frames;
222             *enc_delay = pmp->enc_delay;
223             *enc_padding = pmp->enc_padding;
224         }
225     }
226 
227     switch (ret) {
228     case MP3_OK:
229         switch (pmp->fr.stereo) {
230         case 1:
231             processed_samples = processed_bytes / decoded_sample_size;
232             if (decoded_sample_size == sizeof(short)) {
233                 COPY_MONO(short, short)
234             }
235             else {
236                 COPY_MONO(sample_t, FLOAT)
237             }
238             break;
239         case 2:
240             processed_samples = (processed_bytes / decoded_sample_size) >> 1;
241             if (decoded_sample_size == sizeof(short)) {
242                 COPY_STEREO(short, short)
243             }
244             else {
245                 COPY_STEREO(sample_t, FLOAT)
246             }
247             break;
248         default:
249             processed_samples = -1;
250             assert(0);
251             break;
252         }
253         break;
254 
255     case MP3_NEED_MORE:
256         processed_samples = 0;
257         break;
258 
259     case MP3_ERR:
260         processed_samples = -1;
261         break;
262 
263     default:
264         processed_samples = -1;
265         assert(0);
266         break;
267     }
268 
269     /*fprintf(stderr,"ok, more, err:  %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR ); */
270     /*fprintf(stderr,"ret = %i out=%i\n", ret, processed_samples ); */
271     return processed_samples;
272 }
273 #endif
274 
275 
276 #define OUTSIZE_CLIPPED   (4096*sizeof(short))
277 
278 int
lame_decode1_headersB(unsigned char * buffer,int len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data,int * enc_delay,int * enc_padding)279 lame_decode1_headersB(unsigned char *buffer,
280                       int len,
281                       short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
282                       int *enc_delay, int *enc_padding)
283 {
284     static char out[OUTSIZE_CLIPPED];
285 #ifdef HAVE_MPGLIB
286     return decode1_headersB_clipchoice(&mp, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
287                                        enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
288                                        sizeof(short), decodeMP3);
289 #else
290     return -1;
291 #endif
292 }
293 
294 
295 
296 
297 
298 /*
299  * For lame_decode:  return code
300  *  -1     error
301  *   0     ok, but need more data before outputing any samples
302  *   n     number of samples output.  Will be at most one frame of
303  *         MPEG data.
304  */
305 
306 int
lame_decode1_headers(unsigned char * buffer,int len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)307 lame_decode1_headers(unsigned char *buffer,
308                      int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
309 {
310 #ifdef HAVE_MPGLIB
311     int     enc_delay, enc_padding;
312     return lame_decode1_headersB(buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
313 #else
314     return -1;
315 #endif
316 }
317 
318 
319 int
lame_decode1(unsigned char * buffer,int len,short pcm_l[],short pcm_r[])320 lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
321 {
322 #ifdef HAVE_MPGLIB
323     mp3data_struct mp3data;
324 
325     return lame_decode1_headers(buffer, len, pcm_l, pcm_r, &mp3data);
326 #else
327     return -1;
328 #endif
329 }
330 
331 
332 /*
333  * For lame_decode:  return code
334  *  -1     error
335  *   0     ok, but need more data before outputing any samples
336  *   n     number of samples output.  a multiple of 576 or 1152 depending on MP3 file.
337  */
338 
339 int
lame_decode_headers(unsigned char * buffer,int len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)340 lame_decode_headers(unsigned char *buffer,
341                     int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
342 {
343 #ifdef HAVE_MPGLIB
344     int     ret;
345     int     totsize = 0;     /* number of decoded samples per channel */
346 
347     for (;;) {
348         switch (ret = lame_decode1_headers(buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
349         case -1:
350             return ret;
351         case 0:
352             return totsize;
353         default:
354             totsize += ret;
355             len = 0;    /* future calls to decodeMP3 are just to flush buffers */
356             break;
357         }
358     }
359 #else
360     return -1;
361 #endif
362 }
363 
364 
365 int
lame_decode(unsigned char * buffer,int len,short pcm_l[],short pcm_r[])366 lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
367 {
368 #ifdef HAVE_MPGLIB
369     mp3data_struct mp3data;
370 
371     return lame_decode_headers(buffer, len, pcm_l, pcm_r, &mp3data);
372 #else
373     return -1;
374 #endif
375 }
376 
377 
378 
379 
hip_decode_init(void)380 hip_t hip_decode_init(void)
381 {
382     hip_t hip = lame_calloc(hip_global_flags, 1);
383     if(!hip)
384         return hip;
385 #ifdef HAVE_MPG123
386     mpg123_init();
387     hip->mh = mpg123_new(NULL, NULL);
388     /* Could allocate on demand only. */
389     memset(&hip->mi, 0, sizeof(hip->mi));
390     /* Since encoder delay/padding is communicated, I presume implicit
391        handling of gapless decoding is not expected. */
392     mpg123_param(hip->mh, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0.);
393     /* We are going to feed buffers. */
394     if(mpg123_open_feed(hip->mh) != MPG123_OK)
395     {
396         mpg123_delete(hip->mh);
397         free(hip);
398         hip = NULL;
399     }
400 #else
401     InitMP3(hip);
402 #endif
403     return hip;
404 }
405 
hip_decode_init_gapless(void)406 hip_t hip_decode_init_gapless(void)
407 {
408     hip_t hip = lame_calloc(hip_global_flags, 1);
409     if(!hip)
410         return hip;
411 #ifdef HAVE_MPG123
412     mpg123_init();
413     hip->mh = mpg123_new(NULL, NULL);
414     /* Could allocate on demand only. */
415     memset(&hip->mi, 0, sizeof(hip->mi));
416     /* Default on, but make it explicit. */
417     mpg123_param(hip->mh, MPG123_ADD_FLAGS, MPG123_GAPLESS, 0.);
418     /* We are going to feed buffers. */
419     if(mpg123_open_feed(hip->mh) != MPG123_OK)
420     {
421         mpg123_delete(hip->mh);
422         free(hip);
423         hip = NULL;
424     }
425 #else
426     hip = NULL;
427 #endif
428     return hip;
429 }
430 
431 
432 
hip_decode_exit(hip_t hip)433 int hip_decode_exit(hip_t hip)
434 {
435     if(hip) {
436 #ifdef HAVE_MPG123
437         mpg123_delete(hip->mh); /* Closes implicitly. */
438         /* No mpg123_exit(), will be deprecated anyway. */
439 #else
440         ExitMP3(hip);
441 #endif
442         free(hip);
443     }
444     return 0;
445 }
446 
447 #ifdef HAVE_MPG123
448 /* One decoding routine to cover all API cases. Any output pointer except pcm_l
449    and pcm_r, which are always required to be able to store full MPEG frame
450    (1152 samples), can be NULL if you are not really interested in it.
451    This always works on one whole MPEG frame, even if sample count can be
452    smaller after gapless handling. TODO: Optionally turn on gapless decoding?
453    If not, the decoder delay also needs to be communicated.
454    Or do we just assume 529 samples? */
455 
hip123_decode1(hip_t hip,unsigned char * buffer,size_t len,unsigned char * pcm_l,unsigned char * pcm_r,int * enc_delay,int * enc_padding,mp3data_struct * mp3data,int unclipped)456 int hip123_decode1( hip_t hip, unsigned char *buffer, size_t len,
457     unsigned char *pcm_l, unsigned char *pcm_r,
458     int *enc_delay, int *enc_padding,
459     mp3data_struct *mp3data,
460     int unclipped) /* If true, produce unclipped float (sample_t) output. */
461 {
462     int ret;
463     unsigned char *mpg123buf;
464     size_t mpg123fill;
465     long rate;
466     int channels;
467     int encoding;
468     int change_format;
469     int samples = 0;
470     int want_enc = unclipped ? MPG123_ENC_FLOAT_32 : MPG123_ENC_SIGNED_16;
471 
472     if(MPG123_OK != mpg123_feed(hip->mh, buffer, len))
473         return -1;
474     ret = mpg123_getformat(hip->mh, &rate, &channels, &encoding);
475     switch(ret) {
476         case MPG123_NEED_MORE:
477             return 0;
478         case MPG123_OK:
479             change_format = encoding != want_enc;
480         break;
481         default:
482             return -1;
483     }
484 
485     if(change_format)
486     {
487         mpg123_format_none(hip->mh);
488         mpg123_format2(hip->mh, 0, MPG123_MONO|MPG123_STEREO, want_enc);
489         /* This triggers renegotiation of output format on next decode. */
490         mpg123_decoder(hip->mh, NULL);
491     }
492 
493     /* Now decode for real. */
494     mpg123fill = 0; /* Still zero in case of error/need more. */
495     ret = mpg123_decode_frame(hip->mh, NULL, &mpg123buf, &mpg123fill);
496     /* A second time if we just got notified of new format. */
497     if(!mpg123fill && ret == MPG123_NEW_FORMAT)
498     {
499         mpg123_getformat(hip->mh, &rate, &channels, &encoding);
500         ret = mpg123_decode_frame(hip->mh, NULL, &mpg123buf, &mpg123fill);
501         /* True paranoia would check the encoding again. */
502     }
503     if(ret == MPG123_ERR)
504         return -1;
505     /* MPG123_NEED_MORE and MPG123_DONE (not happening here, though)
506         both result in mpg123fill==0, so return 0 here, which is what fits. */
507     samples = mpg123fill /
508         (unclipped ? sizeof(float) : sizeof(short)) / channels;
509     /* Now demultilex the data in mpg123buf into pcm_l and pcm_r. */
510     if(mpg123fill && mpg123buf)
511     {
512         if(unclipped)
513         {
514             /* Lame's sample_t could be wider than 32 bit, right? */
515             sample_t *spcm_l = (sample_t*)pcm_l;
516             sample_t *spcm_r = (sample_t*)pcm_r;
517             float    *srcbuf = (float*)mpg123buf;
518             int i;
519 
520             if(channels == 2) {
521                 for(i=0; i<samples; ++i) {
522                     spcm_l[i] = *srcbuf++;
523                     spcm_r[i] = *srcbuf++;
524                 }
525             }
526             else
527                 for(i=0; i<samples; ++i)
528                     spcm_l[i] = *srcbuf++;
529         }
530         else
531         {
532             /* It's all shorts. */
533             short *spcm_l = (short*)pcm_l;
534             short *spcm_r = (short*)pcm_r;
535             short *srcbuf = (short*)mpg123buf;
536             int i;
537 
538             if(channels == 2) {
539                 for(i=0; i<samples; ++i) {
540                     spcm_l[i] = *srcbuf++;
541                     spcm_r[i] = *srcbuf++;
542                 }
543             }
544             else
545                 memcpy(pcm_l, mpg123buf, sizeof(short)*samples);
546         }
547     }
548 
549     /* If we arrive here, there was some successful parsing of the stream at
550        least, so that meaningful info is available. */
551     if(mp3data) {
552         struct mpg123_frameinfo fi;
553         memset(mp3data, 0, sizeof(mp3data_struct));
554         /* Re-using last returns from getformat() before. */
555         if(MPG123_OK == mpg123_info(hip->mh, &fi)) {
556             mp3data->header_parsed = 1;
557             mp3data->stereo = channels; /* Channel count correct? Or is dual mono different? */
558             mp3data->samplerate = rate;
559             mp3data->mode = fi.mode;
560             mp3data->mode_ext = fi.mode_ext;
561             mp3data->framesize = mpg123_spf(hip->mh);
562             mp3data->bitrate = fi.bitrate;
563         }
564     }
565     if(enc_delay) {
566         long val;
567         mpg123_getstate(hip->mh, MPG123_ENC_DELAY, &val, NULL);
568         *enc_delay = val > INT_MAX ? -1 : val;
569     }
570     if(enc_padding) {
571         long val;
572         mpg123_getstate(hip->mh, MPG123_ENC_PADDING, &val, NULL);
573         *enc_padding = val > INT_MAX ? -1 : val;
574     }
575     if(hip->pinfo)
576         hip_finish_pinfo(hip);
577     return samples;
578 }
579 #endif
580 
581 
582 /* we forbid input with more than 1152 samples per channel for output in the unclipped mode */
583 #define OUTSIZE_UNCLIPPED (1152*2*sizeof(FLOAT))
584 
585 int
hip_decode1_unclipped(hip_t hip,unsigned char * buffer,size_t len,sample_t pcm_l[],sample_t pcm_r[])586 hip_decode1_unclipped(hip_t hip, unsigned char *buffer, size_t len, sample_t pcm_l[], sample_t pcm_r[])
587 {
588     if (hip) {
589 #ifdef HAVE_MPG123
590         return hip123_decode1( hip, buffer, len,
591             (unsigned char*)pcm_l, (unsigned char*)pcm_r,
592             NULL, NULL, NULL, 1 );
593 #else
594         mp3data_struct mp3data;
595         static char out[OUTSIZE_UNCLIPPED];
596         int     enc_delay, enc_padding;
597         return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, &mp3data,
598                                            &enc_delay, &enc_padding, out, OUTSIZE_UNCLIPPED,
599                                            sizeof(FLOAT), decodeMP3_unclipped);
600 #endif
601     }
602     return 0; /* not -1 ? */
603 }
604 
605 /*
606  * For hip_decode:  return code
607  *  -1     error
608  *   0     ok, but need more data before outputing any samples
609  *   n     number of samples output.  Will be at most one frame of
610  *         MPEG data.
611  */
612 
613 int
hip_decode1_headers(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)614 hip_decode1_headers(hip_t hip, unsigned char *buffer,
615                      size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
616 {
617 #ifdef HAVE_MPG123
618     return hip123_decode1( hip, buffer, len,
619         (unsigned char*)pcm_l, (unsigned char*)pcm_r,
620         NULL, NULL, mp3data, 0 );
621 #else
622     int     enc_delay, enc_padding;
623     return hip_decode1_headersB(hip, buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
624 #endif
625 }
626 
627 
628 int
hip_decode1(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[])629 hip_decode1(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
630 {
631 #ifdef HAVE_MPG123
632     return hip123_decode1( hip, buffer, len,
633         (unsigned char*)pcm_l, (unsigned char*)pcm_r,
634         NULL, NULL, NULL, 0 );
635 #else
636     mp3data_struct mp3data;
637     return hip_decode1_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
638 #endif
639 }
640 
641 
642 /*
643  * For hip_decode:  return code
644  *  -1     error
645  *   0     ok, but need more data before outputing any samples
646  *   n     number of samples output.  a multiple of 576 or 1152 depending on MP3 file.
647  */
648 
649 int
hip_decode_headers(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[],mp3data_struct * mp3data)650 hip_decode_headers(hip_t hip, unsigned char *buffer,
651                     size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
652 {
653     int     ret;
654     int     totsize = 0;     /* number of decoded samples per channel */
655 
656     for (;;) {
657         switch (ret = hip_decode1_headers(hip, buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
658         case -1:
659             return ret;
660         case 0:
661             return totsize;
662         default:
663             totsize += ret;
664             len = 0;    /* future calls to decodeMP3 are just to flush buffers */
665             break;
666         }
667     }
668 }
669 
670 
671 int
hip_decode(hip_t hip,unsigned char * buffer,size_t len,short pcm_l[],short pcm_r[])672 hip_decode(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
673 {
674     mp3data_struct mp3data;
675     return hip_decode_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
676 }
677 
678 
679 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)680 hip_decode1_headersB(hip_t hip, unsigned char *buffer,
681                       size_t len,
682                       short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
683                       int *enc_delay, int *enc_padding)
684 {
685     if (hip) {
686 #ifdef HAVE_MPG123
687         return hip123_decode1( hip, buffer, len,
688             (unsigned char*)pcm_l, (unsigned char*)pcm_r,
689             enc_delay, enc_padding, mp3data, 0);
690 #else
691         static char out[OUTSIZE_CLIPPED];
692         return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
693                                            enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
694                                            sizeof(short), decodeMP3);
695 #endif
696     }
697     return -1;
698 }
699 
700 
hip_set_pinfo(hip_t hip,plotting_data * pinfo)701 void hip_set_pinfo(hip_t hip, plotting_data* pinfo)
702 {
703     if (hip) {
704         hip->pinfo = pinfo;
705 #ifdef HAVE_MPG123
706         mpg123_set_moreinfo(hip->mh, &hip->mi);
707 #endif
708     }
709 }
710 
hip_finish_pinfo(hip_t hip)711 void hip_finish_pinfo(hip_t hip)
712 {
713 #ifndef NOANALYSIS
714 #ifdef HAVE_MPG123
715     struct mpg123_frameinfo fi;
716     long rate;
717     plotting_data *pinfo = hip->pinfo;
718     if(!hip || !hip->pinfo)
719         return;
720 
721     /* TODO: convert to pointers to avoid copies. Allocation should be
722        on mpg123 side (in form of the struct definition), as that is
723        the writing side. */
724     memcpy(pinfo->mpg123xr, hip->mi.xr, sizeof(pinfo->mpg123xr));
725     memcpy(pinfo->sfb, hip->mi.sfb, sizeof(pinfo->sfb));
726     memcpy(pinfo->sfb_s, hip->mi.sfb_s, sizeof(pinfo->sfb_s));
727     memcpy(pinfo->qss, hip->mi.qss, sizeof(pinfo->qss));
728     memcpy(pinfo->big_values, hip->mi.big_values, sizeof(pinfo->big_values));
729     memcpy(pinfo->sub_gain, hip->mi.sub_gain, sizeof(pinfo->sub_gain));
730     memcpy(pinfo->scalefac_scale, hip->mi.scalefac_scale, sizeof(pinfo->scalefac_scale));
731     memcpy(pinfo->preflag, hip->mi.preflag, sizeof(pinfo->preflag));
732     memcpy(pinfo->mpg123blocktype, hip->mi.blocktype, sizeof(pinfo->mpg123blocktype));
733     memcpy(pinfo->mixed, hip->mi.mixed, sizeof(pinfo->mixed));
734     memcpy(pinfo->mainbits, hip->mi.mainbits, sizeof(pinfo->mainbits));
735     memcpy(pinfo->sfbits, hip->mi.sfbits, sizeof(pinfo->sfbits));
736     memcpy(pinfo->scfsi, hip->mi.scfsi, sizeof(pinfo->scfsi));
737     pinfo->maindata = hip->mi.maindata;
738     pinfo->padding  = hip->mi.padding;
739     if(MPG123_OK == mpg123_info(hip->mh, &fi)) {
740         pinfo->js = (fi.mode == MPG123_M_JOINT);
741         pinfo->stereo = fi.mode == MPG123_M_MONO ? 1 : 2;
742         pinfo->crc = fi.flags & MPG123_CRC ? 1 : 0;
743         pinfo->emph = fi.emphasis;
744         pinfo->sampfreq = fi.rate;
745         pinfo->bitrate = fi.bitrate;
746         pinfo->ms_stereo = pinfo->js ? (fi.mode_ext & 0x2)>>1 : 0;
747         pinfo->i_stereo  = pinfo->js ? (fi.mode_ext & 0x1)    : 0;
748     }
749 #endif
750 #endif
751 }
752 
hip_set_errorf(hip_t hip,lame_report_function func)753 void hip_set_errorf(hip_t hip, lame_report_function func)
754 {
755 #ifdef HAVE_MPG123
756     /* TODO: implement something */
757 #else
758     if (hip) {
759         hip->report_err = func;
760     }
761 #endif
762 }
763 
hip_set_debugf(hip_t hip,lame_report_function func)764 void hip_set_debugf(hip_t hip, lame_report_function func)
765 {
766 #ifdef HAVE_MPG123
767     /* TODO: implement something */
768 #else
769     if (hip) {
770         hip->report_dbg = func;
771     }
772 #endif
773 }
774 
hip_set_msgf(hip_t hip,lame_report_function func)775 void hip_set_msgf  (hip_t hip, lame_report_function func)
776 {
777 #ifdef HAVE_MPG123
778     /* TODO: implement something */
779 #else
780     if (hip) {
781         hip->report_msg = func;
782     }
783 #endif
784 }
785 
786 #endif
787 
788 /* end of mpglib_interface.c */
789