2002-06-04 19:53:46 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) Michael Hipp and other authors of the mpglib project.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-06-04 19:53:46 +02:00
|
|
|
*/
|
|
|
|
|
2008-03-31 20:19:58 +02:00
|
|
|
#include "config.h"
|
2002-06-04 19:53:46 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2008-03-31 20:19:58 +02:00
|
|
|
#include "wine/debug.h"
|
2002-06-04 19:53:46 +02:00
|
|
|
#include "mpg123.h"
|
|
|
|
#include "mpglib.h"
|
|
|
|
|
2008-04-02 23:58:23 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
|
2002-06-04 19:53:46 +02:00
|
|
|
|
|
|
|
BOOL InitMP3(struct mpstr *mp)
|
|
|
|
{
|
|
|
|
static int init = 0;
|
|
|
|
|
|
|
|
memset(mp,0,sizeof(struct mpstr));
|
|
|
|
|
|
|
|
mp->framesize = 0;
|
|
|
|
mp->fsizeold = -1;
|
|
|
|
mp->bsize = 0;
|
|
|
|
mp->head = mp->tail = NULL;
|
|
|
|
mp->fr.single = -1;
|
|
|
|
mp->bsnum = 0;
|
|
|
|
mp->synth_bo = 1;
|
2007-07-10 21:09:25 +02:00
|
|
|
mp->fr.mp = mp;
|
2002-06-04 19:53:46 +02:00
|
|
|
|
|
|
|
if(!init) {
|
|
|
|
init = 1;
|
|
|
|
make_decode_tables(32767);
|
|
|
|
init_layer2();
|
|
|
|
init_layer3(SBLIMIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 14:14:07 +01:00
|
|
|
void ClearMP3Buffer(struct mpstr *mp)
|
2002-06-04 19:53:46 +02:00
|
|
|
{
|
|
|
|
struct buf *b,*bn;
|
|
|
|
|
|
|
|
b = mp->tail;
|
|
|
|
while(b) {
|
|
|
|
free(b->pnt);
|
|
|
|
bn = b->next;
|
|
|
|
free(b);
|
|
|
|
b = bn;
|
|
|
|
}
|
2008-02-20 14:14:07 +01:00
|
|
|
mp->tail = NULL;
|
|
|
|
mp->head = NULL;
|
|
|
|
mp->bsize = 0;
|
2002-06-04 19:53:46 +02:00
|
|
|
}
|
|
|
|
|
2005-07-29 16:15:31 +02:00
|
|
|
static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
|
2002-06-04 19:53:46 +02:00
|
|
|
{
|
|
|
|
struct buf *nbuf;
|
|
|
|
|
|
|
|
nbuf = malloc( sizeof(struct buf) );
|
|
|
|
if(!nbuf) {
|
2008-04-02 23:58:23 +02:00
|
|
|
WARN("Out of memory!\n");
|
2002-06-04 19:53:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
nbuf->pnt = malloc(size);
|
|
|
|
if(!nbuf->pnt) {
|
|
|
|
free(nbuf);
|
2008-04-02 23:58:23 +02:00
|
|
|
WARN("Out of memory!\n");
|
2002-06-04 19:53:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
nbuf->size = size;
|
|
|
|
memcpy(nbuf->pnt,buf,size);
|
|
|
|
nbuf->next = NULL;
|
|
|
|
nbuf->prev = mp->head;
|
|
|
|
nbuf->pos = 0;
|
|
|
|
|
|
|
|
if(!mp->tail) {
|
|
|
|
mp->tail = nbuf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mp->head->next = nbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp->head = nbuf;
|
|
|
|
mp->bsize += size;
|
|
|
|
|
|
|
|
return nbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_buf(struct mpstr *mp)
|
|
|
|
{
|
|
|
|
struct buf *buf = mp->tail;
|
|
|
|
|
|
|
|
mp->tail = buf->next;
|
|
|
|
if(mp->tail)
|
|
|
|
mp->tail->prev = NULL;
|
|
|
|
else {
|
|
|
|
mp->tail = mp->head = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf->pnt);
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_buf_byte(struct mpstr *mp)
|
|
|
|
{
|
|
|
|
unsigned int b;
|
|
|
|
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
pos = mp->tail->pos;
|
|
|
|
while(pos >= mp->tail->size) {
|
|
|
|
remove_buf(mp);
|
|
|
|
pos = mp->tail->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = mp->tail->pnt[pos];
|
|
|
|
mp->bsize--;
|
|
|
|
mp->tail->pos++;
|
|
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_head(struct mpstr *mp)
|
|
|
|
{
|
|
|
|
unsigned long head;
|
|
|
|
|
|
|
|
head = read_buf_byte(mp);
|
|
|
|
head <<= 8;
|
|
|
|
head |= read_buf_byte(mp);
|
|
|
|
head <<= 8;
|
|
|
|
head |= read_buf_byte(mp);
|
|
|
|
head <<= 8;
|
|
|
|
head |= read_buf_byte(mp);
|
|
|
|
|
|
|
|
mp->header = head;
|
|
|
|
}
|
|
|
|
|
2005-07-29 16:15:31 +02:00
|
|
|
int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
|
2002-06-04 19:53:46 +02:00
|
|
|
int osize,int *done)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if(osize < 4608) {
|
2008-04-02 23:58:23 +02:00
|
|
|
ERR("Output buffer too small\n");
|
2002-06-04 19:53:46 +02:00
|
|
|
return MP3_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(in) {
|
|
|
|
if(addbuf(mp,in,isize) == NULL) {
|
|
|
|
return MP3_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First decode header */
|
|
|
|
if(mp->framesize == 0) {
|
2008-06-10 16:52:06 +02:00
|
|
|
int ret;
|
2002-06-04 19:53:46 +02:00
|
|
|
if(mp->bsize < 4) {
|
|
|
|
return MP3_NEED_MORE;
|
|
|
|
}
|
|
|
|
read_head(mp);
|
2008-06-10 16:52:06 +02:00
|
|
|
while (!(ret = decode_header(&mp->fr,mp->header)) && mp->bsize)
|
|
|
|
{
|
|
|
|
mp->header = mp->header << 8;
|
|
|
|
mp->header |= read_buf_byte(mp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
return MP3_NEED_MORE;
|
2003-12-08 22:47:50 +01:00
|
|
|
}
|
2002-06-04 19:53:46 +02:00
|
|
|
mp->framesize = mp->fr.framesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mp->fr.framesize > mp->bsize)
|
|
|
|
return MP3_NEED_MORE;
|
|
|
|
|
|
|
|
wordpointer = mp->bsspace[mp->bsnum] + 512;
|
|
|
|
mp->bsnum = (mp->bsnum + 1) & 0x1;
|
|
|
|
bitindex = 0;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
while(len < mp->framesize) {
|
|
|
|
int nlen;
|
|
|
|
int blen = mp->tail->size - mp->tail->pos;
|
|
|
|
if( (mp->framesize - len) <= blen) {
|
|
|
|
nlen = mp->framesize-len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nlen = blen;
|
|
|
|
}
|
|
|
|
memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
|
|
|
|
len += nlen;
|
|
|
|
mp->tail->pos += nlen;
|
|
|
|
mp->bsize -= nlen;
|
|
|
|
if(mp->tail->pos == mp->tail->size) {
|
|
|
|
remove_buf(mp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*done = 0;
|
|
|
|
if(mp->fr.error_protection)
|
|
|
|
getbits(16);
|
|
|
|
switch(mp->fr.lay) {
|
|
|
|
case 1:
|
2008-02-02 18:27:11 +01:00
|
|
|
do_layer1(&mp->fr,out,done);
|
2002-06-04 19:53:46 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2008-02-02 18:27:11 +01:00
|
|
|
do_layer2(&mp->fr,out,done);
|
2002-06-04 19:53:46 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
2008-02-02 18:27:11 +01:00
|
|
|
do_layer3(&mp->fr,out,done);
|
2002-06-04 19:53:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp->fsizeold = mp->framesize;
|
|
|
|
mp->framesize = 0;
|
|
|
|
|
|
|
|
return MP3_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-10 21:09:25 +02:00
|
|
|
int set_pointer(struct mpstr *mp, long backstep)
|
2002-06-04 19:53:46 +02:00
|
|
|
{
|
|
|
|
unsigned char *bsbufold;
|
2007-07-10 21:09:25 +02:00
|
|
|
if(mp->fsizeold < 0 && backstep > 0) {
|
2008-04-02 23:58:23 +02:00
|
|
|
/* This is not a bug if we just did seeking, the first frame is dropped then */
|
|
|
|
WARN("Can't step back %ld!\n",backstep);
|
2002-06-04 19:53:46 +02:00
|
|
|
return MP3_ERR;
|
|
|
|
}
|
2007-07-10 21:09:25 +02:00
|
|
|
bsbufold = mp->bsspace[mp->bsnum] + 512;
|
2002-06-04 19:53:46 +02:00
|
|
|
wordpointer -= backstep;
|
|
|
|
if (backstep)
|
2007-07-10 21:09:25 +02:00
|
|
|
memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);
|
2002-06-04 19:53:46 +02:00
|
|
|
bitindex = 0;
|
|
|
|
return MP3_OK;
|
|
|
|
}
|