3
* Copyright (c) 2001 Gerard Lantau.
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.
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.
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.
23
#include "mpglib/mpg123.h"
28
* - do not rely anymore on mpglib (first step: implement dct64 and decoding filter)
32
#define BACKSTEP_SIZE 512
34
typedef struct MPADecodeContext {
36
UINT8 inbuf1[2][MAXFRAMESIZE + BACKSTEP_SIZE]; /* input buffer */
38
UINT8 *inbuf_ptr, *inbuf;
48
/* XXX: suppress that mess */
50
GetBitContext *gmp_gb;
51
static MPADecodeContext *gmp_s;
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}
64
static unsigned short mp_freq_tab[3] = { 44100, 48000, 32000 };
66
static int decode_init(AVCodecContext * avctx)
68
MPADecodeContext *s = avctx->priv_data;
69
struct mpstr *mp = &s->mpstr;
77
make_decode_tables(32767);
83
s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
84
s->inbuf_ptr = s->inbuf;
89
/* fast header check for resync */
90
static int check_header(UINT32 header)
93
if ((header & 0xffe00000) != 0xffe00000)
96
if (((header >> 17) & 3) == 0)
98
/* bit rate : currently no free format supported */
99
if (((header >> 12) & 0xf) == 0xf ||
100
((header >> 12) & 0xf) == 0x0)
103
if (((header >> 10) & 3) == 3)
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)
112
struct frame *fr = &s->mpstr.fr;
113
int sample_rate, frame_size;
115
if (header & (1<<20)) {
116
fr->lsf = (header & (1<<19)) ? 0 : 1;
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);
129
s->error_protection = ((header>>16) & 1) ^ 1;
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;
140
fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
143
frame_size = mp_bitrate_tab[fr->lsf][s->layer - 1][fr->bitrate_index];
144
s->bit_rate = frame_size * 1000;
147
frame_size = (frame_size * 12000) / sample_rate;
148
frame_size = ((frame_size + fr->padding) << 2);
151
frame_size = (frame_size * 144000) / sample_rate;
152
frame_size += fr->padding;
155
frame_size = (frame_size * 144000) / (sample_rate << fr->lsf);
156
frame_size += fr->padding;
159
s->frame_size = frame_size;
160
s->sample_rate = sample_rate;
163
printf("layer%d, %d Hz, %d kbits/s, %s\n",
164
s->layer, s->sample_rate, s->bit_rate, fr->stereo ? "stereo" : "mono");
168
static int mp_decode_frame(MPADecodeContext *s,
173
init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, s->inbuf_ptr - s->inbuf - HEADER_SIZE);
175
/* skip error protection field */
176
if (s->error_protection)
177
get_bits(&s->gb, 16);
179
/* XXX: horrible: global! */
187
do_layer1(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
190
do_layer2(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
193
do_layer3(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
195
s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
196
s->old_frame_size = s->frame_size;
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!)
209
int set_pointer(long backstep)
213
/* compute current position in stream */
214
ptr = gmp_gb->buf_ptr - (gmp_gb->bit_cnt >> 3);
215
/* copy old data before current one */
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);
225
static int decode_frame(AVCodecContext * avctx,
226
void *data, int *data_size,
227
UINT8 * buf, int buf_size)
229
MPADecodeContext *s = avctx->priv_data;
233
short *out_samples = data;
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;
244
memcpy(s->inbuf_ptr, buf_ptr, 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);
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;
263
} else if (len < s->frame_size) {
264
len = s->frame_size - len;
268
memcpy(s->inbuf_ptr, buf_ptr, len);
273
out_size = mp_decode_frame(s, out_samples);
274
s->inbuf_ptr = s->inbuf;
276
*data_size = out_size;
280
return buf_ptr - buf;
283
AVCodec mp3_decoder =
288
sizeof(MPADecodeContext),