summaryrefslogtreecommitdiff
path: root/sys/src/games/mp3enc/mpglib_interface.c
blob: 3bcaba1d96e3a3826b219ba6db79a4ec4306973f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* $Id: mpglib_interface.c,v 1.18 2001/01/06 01:00:33 markt Exp $ */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_MPGLIB

#include <limits.h>
#include <stdlib.h>
#include <assert.h>

#include "interface.h"
#include "lame.h"

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif


MPSTR           mp;
plotting_data*  mpg123_pinfo = NULL;


int lame_decode_init( void )
{
    InitMP3 ( &mp );
    return 0;
}

/*
 * For lame_decode:  return code
 * -1     error
 *  0     ok, but need more data before outputing any samples
 *  n     number of samples output.  either 576 or 1152 depending on MP3 file.
 */
 
int lame_decode1_headers(
        unsigned char*   buffer,
        int              len,
        short            pcm_l [],
        short            pcm_r [],
        mp3data_struct*  mp3data )
{
    static const int smpls [2] [4] = {
    /* Layer   I    II   III */
        { 0, 384, 1152, 1152 }, /* MPEG-1     */
        { 0, 384, 1152,  576 }  /* MPEG-2(.5) */
    };
    static char        out  [8192];
    signed short int*  p = (signed short int*) out;
    int                processed_bytes;
    int                processed_samples;  // processed samples per channel
    int                ret;
    int                i;

    mp3data->header_parsed = 0;
  
    ret = decodeMP3 ( &mp, buffer, len, (char*)p, sizeof(out), &processed_bytes );

    if ( mp.header_parsed ) {
        mp3data->header_parsed = 1;
        mp3data->stereo        = mp.fr.stereo;
        mp3data->samplerate    = freqs [mp.fr.sampling_frequency];
        mp3data->mode          = mp.fr.mode;
        mp3data->mode_ext      = mp.fr.mode_ext;
        mp3data->framesize     = smpls [mp.fr.lsf] [mp.fr.lay];
	
        if (mp.fsizeold > 0)      /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
            mp3data->bitrate   = 8 * (4 + mp.fsizeold) * mp3data->samplerate /
	                         ( 1.e3 * mp3data->framesize ) + 0.5;
        else
            mp3data->bitrate   = tabsel_123 [mp.fr.lsf] [mp.fr.lay-1] [mp.fr.bitrate_index];

        if (mp.num_frames>0) {
	  /* Xing VBR header found and num_frames was set */
	  mp3data->totalframes = mp.num_frames;
          mp3data->nsamp=mp3data->framesize * mp.num_frames;  
	}
    }

    switch ( ret ) {
    case MP3_OK:
        switch ( mp.fr.stereo ) {
	case 1:
            processed_samples = processed_bytes >> 1;
            for ( i = 0; i < processed_samples; i++ ) 
	        pcm_l [i] = *p++;
	    break;
	case 2:
            processed_samples = processed_bytes >> 2;
            for ( i = 0; i < processed_samples; i++ ) {
	        pcm_l [i] = *p++;
		pcm_r [i] = *p++;
	    }
	    break;
	default:
            processed_samples = -1;
	    assert (0);
	    break;
        }    
	break;
	
    case MP3_NEED_MORE:
        processed_samples = 0;
	break;
	
    default:
        assert (0);
    case MP3_ERR: 
        processed_samples = -1;
	break;
	
    }
  
    //  printf ( "ok, more, err:  %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR );
    //  printf ( "ret = %i out=%i\n", ret, totsize );
    return processed_samples;
}


/*
 * For lame_decode:  return code
 *  -1     error
 *   0     ok, but need more data before outputing any samples
 *   n     number of samples output.  Will be at most one frame of
 *         MPEG data.  
 */
 
int lame_decode1( 
        unsigned char*  buffer,
	int    len,
	short  pcm_l [],
	short  pcm_r [] )
{
  mp3data_struct mp3data;
  
  return lame_decode1_headers ( buffer, len, pcm_l, pcm_r, &mp3data );
}


/*
 * For lame_decode:  return code
 *  -1     error
 *   0     ok, but need more data before outputing any samples
 *   n     number of samples output.  a multiple of 576 or 1152 depending on MP3 file.
 */
 
int lame_decode_headers( 
        unsigned char*   buffer,
	int              len,
	short            pcm_l [],
	short            pcm_r [],
        mp3data_struct*  mp3data )
{
    int  ret;
    int  totsize = 0;   // number of decoded samples per channel

    while (1) {
        switch ( ret = lame_decode1_headers ( buffer, len, pcm_l+totsize, pcm_r+totsize, mp3data ) ) {
        case -1: return ret;
        case  0: return totsize;
        default: totsize += ret;
                 len      = 0;  /* future calls to decodeMP3 are just to flush buffers */
                 break;
        }
    }
}


int lame_decode(
        unsigned char*  buffer,
	int    len,
	short  pcm_l [],
	short  pcm_r [] )
{
    mp3data_struct  mp3data;
    
    return lame_decode_headers ( buffer, len, pcm_l, pcm_r, &mp3data );
}


#endif

/* end of mpglib_interface.c */