OSDN Git Service

added a bunch of things~ and midi stuff~
[proj16/16.git] / src / lib / doslib / ext / lame / mpglib_interface.c
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.42 2011/05/07 16:05:17 rbrito 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
86 lame_decode_exit(void)
87 {
88     ExitMP3(&mp);
89     return 0;
90 }
91
92
93 int
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
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,
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
147     mp3data->header_parsed = 0;
148
149     ret = (*decodeMP3_ptr) (pmp, buffer, len, p, (int) psize, &processed_bytes);
150     /* three cases:  
151      * 1. headers parsed, but data not complete
152      *       pmp->header_parsed==1 
153      *       pmp->framesize=0           
154      *       pmp->fsizeold=size of last frame, or 0 if this is first frame
155      *
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
160      *
161      * 3. frame fully decoded:  
162      *       pmp->header_parsed==0 
163      *       pmp->framesize=0           
164      *       pmp->fsizeold=size of frame (which is now the last frame)
165      *
166      */
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];
174
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;
183         else
184             mp3data->bitrate = tabsel_123[pmp->fr.lsf][pmp->fr.lay - 1][pmp->fr.bitrate_index];
185
186
187
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;
194         }
195     }
196
197     switch (ret) {
198     case MP3_OK:
199         switch (pmp->fr.stereo) {
200         case 1:
201             processed_samples = processed_bytes / decoded_sample_size;
202             if (decoded_sample_size == sizeof(short)) {
203                 COPY_MONO(short, short)
204             }
205             else {
206                 COPY_MONO(sample_t, FLOAT)
207             }
208             break;
209         case 2:
210             processed_samples = (processed_bytes / decoded_sample_size) >> 1;
211             if (decoded_sample_size == sizeof(short)) {
212                 COPY_STEREO(short, short)
213             }
214             else {
215                 COPY_STEREO(sample_t, FLOAT)
216             }
217             break;
218         default:
219             processed_samples = -1;
220             assert(0);
221             break;
222         }
223         break;
224
225     case MP3_NEED_MORE:
226         processed_samples = 0;
227         break;
228
229     case MP3_ERR:
230         processed_samples = -1;
231         break;
232
233     default:
234         processed_samples = -1;
235         assert(0);
236         break;
237     }
238
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;
242 }
243
244
245 #define OUTSIZE_CLIPPED   (4096*sizeof(short))
246
247 int
248 lame_decode1_headersB(unsigned char *buffer,
249                       int len,
250                       short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
251                       int *enc_delay, int *enc_padding)
252 {
253     static char out[OUTSIZE_CLIPPED];
254
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);
258 }
259
260
261
262
263
264 /*
265  * For lame_decode:  return code
266  *  -1     error
267  *   0     ok, but need more data before outputing any samples
268  *   n     number of samples output.  Will be at most one frame of
269  *         MPEG data.  
270  */
271
272 int
273 lame_decode1_headers(unsigned char *buffer,
274                      int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
275 {
276     int     enc_delay, enc_padding;
277     return lame_decode1_headersB(buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
278 }
279
280
281 int
282 lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
283 {
284     mp3data_struct mp3data;
285
286     return lame_decode1_headers(buffer, len, pcm_l, pcm_r, &mp3data);
287 }
288
289
290 /*
291  * For lame_decode:  return code
292  *  -1     error
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.
295  */
296
297 int
298 lame_decode_headers(unsigned char *buffer,
299                     int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
300 {
301     int     ret;
302     int     totsize = 0;     /* number of decoded samples per channel */
303
304     for (;;) {
305         switch (ret = lame_decode1_headers(buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
306         case -1:
307             return ret;
308         case 0:
309             return totsize;
310         default:
311             totsize += ret;
312             len = 0;    /* future calls to decodeMP3 are just to flush buffers */
313             break;
314         }
315     }
316 }
317
318
319 int
320 lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
321 {
322     mp3data_struct mp3data;
323
324     return lame_decode_headers(buffer, len, pcm_l, pcm_r, &mp3data);
325 }
326
327
328
329
330 hip_t hip_decode_init(void)
331 {
332     hip_t hip = calloc(1, sizeof(hip_global_flags));
333     InitMP3(hip);
334     return hip;
335 }
336
337
338 int hip_decode_exit(hip_t hip)
339 {
340     if (hip) {
341         ExitMP3(hip);
342         free(hip);
343     }
344     return 0;
345 }
346
347
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))
350
351 int
352 hip_decode1_unclipped(hip_t hip, unsigned char *buffer, size_t len, sample_t pcm_l[], sample_t pcm_r[])
353 {
354     static char out[OUTSIZE_UNCLIPPED];
355     mp3data_struct mp3data;
356     int     enc_delay, enc_padding;
357
358     if (hip) {
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);
362     }
363     return 0;
364 }
365
366 /*
367  * For hip_decode:  return code
368  *  -1     error
369  *   0     ok, but need more data before outputing any samples
370  *   n     number of samples output.  Will be at most one frame of
371  *         MPEG data.  
372  */
373
374 int
375 hip_decode1_headers(hip_t hip, unsigned char *buffer,
376                      size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
377 {
378     int     enc_delay, enc_padding;
379     return hip_decode1_headersB(hip, buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
380 }
381
382
383 int
384 hip_decode1(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
385 {
386     mp3data_struct mp3data;
387     return hip_decode1_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
388 }
389
390
391 /*
392  * For hip_decode:  return code
393  *  -1     error
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.
396  */
397
398 int
399 hip_decode_headers(hip_t hip, unsigned char *buffer,
400                     size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
401 {
402     int     ret;
403     int     totsize = 0;     /* number of decoded samples per channel */
404
405     for (;;) {
406         switch (ret = hip_decode1_headers(hip, buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
407         case -1:
408             return ret;
409         case 0:
410             return totsize;
411         default:
412             totsize += ret;
413             len = 0;    /* future calls to decodeMP3 are just to flush buffers */
414             break;
415         }
416     }
417 }
418
419
420 int
421 hip_decode(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
422 {
423     mp3data_struct mp3data;
424     return hip_decode_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
425 }
426
427
428 int
429 hip_decode1_headersB(hip_t hip, unsigned char *buffer,
430                       size_t len,
431                       short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
432                       int *enc_delay, int *enc_padding)
433 {
434     static char out[OUTSIZE_CLIPPED];
435     if (hip) {
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);
439     }
440     return -1;
441 }
442
443
444 void hip_set_pinfo(hip_t hip, plotting_data* pinfo)
445 {
446     if (hip) {
447         hip->pinfo = pinfo;
448     }
449 }
450
451
452
453 void hip_set_errorf(hip_t hip, lame_report_function func)
454 {
455     if (hip) {
456         hip->report_err = func;
457     }
458 }
459
460 void hip_set_debugf(hip_t hip, lame_report_function func)
461 {
462     if (hip) {
463         hip->report_dbg = func;
464     }
465 }
466
467 void hip_set_msgf  (hip_t hip, lame_report_function func)
468 {
469     if (hip) {
470         hip->report_msg = func;
471     }
472 }
473
474 #endif
475
476 /* end of mpglib_interface.c */