Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ d771bcae

History | View | Annotate | Download (8 KB)

1
/*
2
 * MPEG Audio decoder
3
 * Copyright (c) 2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include <stdlib.h>
20
#include <stdio.h>
21
#include <string.h>
22
#include "avcodec.h"
23
#include "mpglib/mpg123.h"
24

    
25
/*
26
 * TODO: 
27
 *  - add free format
28
 *  - do not rely anymore on mpglib (first step: implement dct64 and decoding filter)
29
 */
30

    
31
#define HEADER_SIZE 4
32
#define BACKSTEP_SIZE 512
33

    
34
typedef struct MPADecodeContext {
35
    struct mpstr mpstr;
36
    UINT8 inbuf1[2][MAXFRAMESIZE + BACKSTEP_SIZE];        /* input buffer */
37
    int inbuf_index;
38
    UINT8 *inbuf_ptr, *inbuf;
39
    int frame_size;
40
    int error_protection;
41
    int layer;
42
    int sample_rate;
43
    int bit_rate;
44
    int old_frame_size;
45
    GetBitContext gb;
46
} MPADecodeContext;
47

    
48
/* XXX: suppress that mess */
49
struct mpstr *gmp;
50
GetBitContext *gmp_gb;
51
static MPADecodeContext *gmp_s;
52

    
53
/* XXX: merge constants with encoder */
54
static const unsigned short mp_bitrate_tab[2][3][15] = {
55
    { {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 },
56
      {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 },
57
      {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 } },
58
    { {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
59
      {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
60
      {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
61
    }
62
};
63

    
64
static unsigned short mp_freq_tab[3] = { 44100, 48000, 32000 };
65

    
66
static int decode_init(AVCodecContext * avctx)
67
{
68
    MPADecodeContext *s = avctx->priv_data;
69
    struct mpstr *mp = &s->mpstr;
70
    static int init;
71

    
72
    mp->fr.single = -1;
73
    mp->synth_bo = 1;
74

    
75
    if(!init) {
76
        init = 1;
77
        make_decode_tables(32767);
78
        init_layer2();
79
        init_layer3(SBLIMIT);
80
    }
81

    
82
    s->inbuf_index = 0;
83
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
84
    s->inbuf_ptr = s->inbuf;
85
    
86
    return 0;
87
}
88

    
89
/* fast header check for resync */
90
static int check_header(UINT32 header)
91
{
92
    /* header */
93
    if ((header & 0xffe00000) != 0xffe00000)
94
        return -1;
95
    /* layer check */
96
    if (((header >> 17) & 3) == 0)
97
        return -1;
98
    /* bit rate : currently no free format supported */
99
    if (((header >> 12) & 0xf) == 0xf ||
100
        ((header >> 12) & 0xf) == 0x0)
101
        return -1;
102
    /* frequency */
103
    if (((header >> 10) & 3) == 3)
104
        return -1;
105
    return 0;
106
}
107

    
108
/* header decoding. MUST check the header before because no
109
   consistency check is done there */
110
static void decode_header(MPADecodeContext *s, UINT32 header)
111
{
112
    struct frame *fr = &s->mpstr.fr;
113
    int sample_rate, frame_size;
114

    
115
    if (header & (1<<20)) {
116
        fr->lsf = (header & (1<<19)) ? 0 : 1;
117
        fr->mpeg25 = 0;
118
    } else {
119
        fr->lsf = 1;
120
        fr->mpeg25 = 1;
121
    }
122
    
123
    s->layer = 4 - ((header >> 17) & 3);
124
    /* extract frequency */
125
    fr->sampling_frequency = ((header >> 10) & 3);
126
    sample_rate = mp_freq_tab[fr->sampling_frequency] >> (fr->lsf + fr->mpeg25);
127
    fr->sampling_frequency += 3 * (fr->lsf + fr->mpeg25);
128

    
129
    s->error_protection = ((header>>16) & 1) ^ 1;
130

    
131
    fr->bitrate_index = ((header>>12)&0xf);
132
    fr->padding   = ((header>>9)&0x1);
133
    fr->extension = ((header>>8)&0x1);
134
    fr->mode      = ((header>>6)&0x3);
135
    fr->mode_ext  = ((header>>4)&0x3);
136
    fr->copyright = ((header>>3)&0x1);
137
    fr->original  = ((header>>2)&0x1);
138
    fr->emphasis  = header & 0x3;
139

    
140
    fr->stereo    = (fr->mode == MPG_MD_MONO) ? 1 : 2;
141

    
142
    
143
    frame_size = mp_bitrate_tab[fr->lsf][s->layer - 1][fr->bitrate_index];
144
    s->bit_rate = frame_size * 1000;
145
    switch(s->layer) {
146
    case 1:
147
        frame_size = (frame_size * 12000) / sample_rate;
148
        frame_size = ((frame_size + fr->padding) << 2);
149
        break;
150
    case 2:
151
        frame_size = (frame_size * 144000) / sample_rate;
152
        frame_size += fr->padding;
153
        break;
154
    case 3:
155
        frame_size = (frame_size * 144000) / (sample_rate << fr->lsf);
156
        frame_size += fr->padding;
157
        break;
158
    }
159
    s->frame_size = frame_size;
160
    s->sample_rate = sample_rate;
161

    
162
#if 0
163
    printf("layer%d, %d Hz, %d kbits/s, %s\n",
164
           s->layer, s->sample_rate, s->bit_rate, fr->stereo ? "stereo" : "mono");
165
#endif
166
}
167

    
168
static int mp_decode_frame(MPADecodeContext *s, 
169
                           short *samples)
170
{
171
    int nb_bytes;
172
    
173
    init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, s->inbuf_ptr - s->inbuf - HEADER_SIZE);
174
    
175
    /* skip error protection field */
176
    if (s->error_protection)
177
        get_bits(&s->gb, 16);
178

    
179
    /* XXX: horrible: global! */
180
    gmp = &s->mpstr;
181
    gmp_s = s;
182
    gmp_gb = &s->gb;
183

    
184
    nb_bytes = 0;
185
    switch(s->layer) {
186
    case 1:
187
        do_layer1(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
188
        break;
189
    case 2:
190
        do_layer2(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
191
        break;
192
    case 3:
193
        do_layer3(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
194
        s->inbuf_index ^= 1;
195
        s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
196
        s->old_frame_size = s->frame_size;
197
        break;
198
    default:
199
        break;
200
    }
201
    return nb_bytes;
202
}
203

    
204
/*
205
 * seek back in the stream for backstep bytes (at most 511 bytes, and
206
 * at most in last frame). Note that this is slightly incorrect (data
207
 * can span more than one block!)  
208
 */
209
int set_pointer(long backstep)
210
{
211
    UINT8 *ptr;
212

    
213
    /* compute current position in stream */
214
    ptr = gmp_gb->buf_ptr - (gmp_gb->bit_cnt >> 3);
215
    /* copy old data before current one */
216
    ptr -= backstep;
217
    memcpy(ptr, gmp_s->inbuf1[gmp_s->inbuf_index ^ 1] + 
218
           BACKSTEP_SIZE + gmp_s->old_frame_size - backstep, backstep);
219
    /* init get bits again */
220
    init_get_bits(gmp_gb, ptr, gmp_s->frame_size + backstep);
221

    
222
    return 0;
223
}
224

    
225
static int decode_frame(AVCodecContext * avctx,
226
                        void *data, int *data_size,
227
                        UINT8 * buf, int buf_size)
228
{
229
    MPADecodeContext *s = avctx->priv_data;
230
    UINT32 header;
231
    UINT8 *buf_ptr;
232
    int len, out_size;
233
    short *out_samples = data;
234

    
235
    *data_size = 0;
236
    buf_ptr = buf;
237
    while (buf_size > 0) {
238
        len = s->inbuf_ptr - s->inbuf;
239
        if (s->frame_size == 0) {
240
            /* no header seen : find one. We need at least 7 bytes to parse it */
241
            len = HEADER_SIZE - len;
242
            if (len > buf_size)
243
                len = buf_size;
244
            memcpy(s->inbuf_ptr, buf_ptr, len);
245
            buf_ptr += len;
246
            s->inbuf_ptr += len;
247
            buf_size -= len;
248
            if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
249
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
250
                    (s->inbuf[2] << 8) | s->inbuf[3];
251
                if (check_header(header) < 0) {
252
                    /* no sync found : move by one byte (inefficient, but simple!) */
253
                    memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
254
                    s->inbuf_ptr--;
255
                } else {
256
                    decode_header(s, header);
257
                    /* update codec info */
258
                    avctx->sample_rate = s->sample_rate;
259
                    avctx->channels = s->mpstr.fr.stereo ? 2 : 1;
260
                    avctx->bit_rate = s->bit_rate;
261
                }
262
            }
263
        } else if (len < s->frame_size) {
264
            len = s->frame_size - len;
265
            if (len > buf_size)
266
                len = buf_size;
267

    
268
            memcpy(s->inbuf_ptr, buf_ptr, len);
269
            buf_ptr += len;
270
            s->inbuf_ptr += len;
271
            buf_size -= len;
272
        } else {
273
            out_size = mp_decode_frame(s, out_samples);
274
            s->inbuf_ptr = s->inbuf;
275
            s->frame_size = 0;
276
            *data_size = out_size;
277
            break;
278
        }
279
    }
280
    return buf_ptr - buf;
281
}
282

    
283
AVCodec mp3_decoder =
284
{
285
    "mpegaudio",
286
    CODEC_TYPE_AUDIO,
287
    CODEC_ID_MP2,
288
    sizeof(MPADecodeContext),
289
    decode_init,
290
    NULL,
291
    NULL,
292
    decode_frame,
293
};