📄 cup.c
字号:
/*____________________________________________________________________________
FreeAmp - The Free MP3 Player
MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
Corp. http://www.xingtech.com
Portions Copyright (C) 1998-1999 EMusic.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: cup.c,v 1.3 1999/10/19 07:13:08 elrod Exp $
____________________________________________________________________________*/
/**** cup.c ***************************************************
MPEG audio decoder Layer I/II mpeg1 and mpeg2
should be portable ANSI C, should be endian independent
mod 2/21/95 2/21/95 add bit skip, sb limiting
mods 11/15/95 for Layer I
******************************************************************/
/******************************************************************
MPEG audio software decoder portable ANSI c.
Decodes all Layer I/II to 16 bit linear pcm.
Optional stereo to mono conversion. Optional
output sample rate conversion to half or quarter of
native mpeg rate. dec8.c adds oupuut conversion features.
-------------------------------------
int audio_decode_init(MPEG_HEAD *h, int framebytes_arg,
int reduction_code, int transform_code, int convert_code,
int freq_limit)
initilize decoder:
return 0 = fail, not 0 = success
MPEG_HEAD *h input, mpeg header info (returned by call to head_info)
framebytes input, mpeg frame size (returned by call to head_info)
reduction_code input, sample rate reduction code
0 = full rate
1 = half rate
2 = quarter rate
transform_code input, ignored
convert_code input, channel conversion
convert_code: 0 = two chan output
1 = convert two chan to mono
2 = convert two chan to left chan
3 = convert two chan to right chan
freq_limit input, limits bandwidth of pcm output to specified
frequency. Special use. Set to 24000 for normal use.
---------------------------------
void audio_decode_info( DEC_INFO *info)
information return:
Call after audio_decode_init. See mhead.h for
information returned in DEC_INFO structure.
---------------------------------
IN_OUT audio_decode(unsigned char *bs, void *pcmbuf)
decode one mpeg audio frame:
bs input, mpeg bitstream, must start with
sync word. Caution: may read up to 3 bytes
beyond end of frame.
pcmbuf output, pcm samples.
IN_OUT structure returns:
Number bytes conceptually removed from mpeg bitstream.
Returns 0 if sync loss.
Number bytes of pcm output.
*******************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#include "mhead.h" /* mpeg header structure */
#ifdef _MSC_VER
#pragma warning(disable: 4709)
#endif
/*-------------------------------------------------------
NOTE: Decoder may read up to three bytes beyond end of
frame. Calling application must ensure that this does
not cause a memory access violation (protection fault)
---------------------------------------------------------*/
/*====================================================================*/
/*----------------*/
DEC_INFO decinfo; /* global for Layer III */
/*----------------*/
static float look_c_value[18]; /* built by init */
/*----------------*/
static int outbytes;
static int framebytes;
static int outvalues;
static int pad;
static int look_joint[16] =
{ /* lookup stereo sb's by mode+ext */
64, 64, 64, 64, /* stereo */
2 * 4, 2 * 8, 2 * 12, 2 * 16, /* joint */
64, 64, 64, 64, /* dual */
32, 32, 32, 32, /* mono */
};
/*----------------*/
static int max_sb;
static int stereo_sb;
/*----------------*/
static int nsb_limit = 6;
static int bit_skip;
static int bat_bit_master[] =
{
0, 5, 7, 9, 10, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48};
/*----------------*/
static int nbat[4] =
{3, 8, 12, 7};
static int bat[4][16];
static int ballo[64]; /* set by unpack_ba */
static unsigned int samp_dispatch[66]; /* set by unpack_ba */
static float c_value[64]; /* set by unpack_ba */
/*----------------*/
static unsigned int sf_dispatch[66]; /* set by unpack_ba */
static float sf_table[64];
static float cs_factor[3][64];
/*----------------*/
float sample[2304]; /* global for use by Later 3 */
static signed char group3_table[32][3];
static signed char group5_table[128][3];
static signed short group9_table[1024][3];
/*----------------*/
typedef void (*SBT_FUNCTION) (float *sample, short *pcm, int n);
void sbt_mono(float *sample, short *pcm, int n);
void sbt_dual(float *sample, short *pcm, int n);
static SBT_FUNCTION sbt = sbt_mono;
typedef IN_OUT(*AUDIO_DECODE_ROUTINE) (unsigned char *bs, signed short *pcm);
IN_OUT L2audio_decode(unsigned char *bs, signed short *pcm);
static AUDIO_DECODE_ROUTINE audio_decode_routine = L2audio_decode;
/*======================================================================*/
/*======================================================================*/
/* get bits from bitstream in endian independent way */
static unsigned char *bs_ptr;
static unsigned long bitbuf;
static int bits;
static long bitval;
/*------------- initialize bit getter -------------*/
static void load_init(unsigned char *buf)
{
bs_ptr = buf;
bits = 0;
bitbuf = 0;
}
/*------------- get n bits from bitstream -------------*/
static long load(int n)
{
unsigned long x;
if (bits < n)
{ /* refill bit buf if necessary */
while (bits <= 24)
{
bitbuf = (bitbuf << 8) | *bs_ptr++;
bits += 8;
}
}
bits -= n;
x = bitbuf >> bits;
bitbuf -= x << bits;
return x;
}
/*------------- skip over n bits in bitstream -------------*/
static void skip(int n)
{
int k;
if (bits < n)
{
n -= bits;
k = n >> 3;
/*--- bytes = n/8 --*/
bs_ptr += k;
n -= k << 3;
bitbuf = *bs_ptr++;
bits = 8;
}
bits -= n;
bitbuf -= (bitbuf >> bits) << bits;
}
/*--------------------------------------------------------------*/
#define mac_load_check(n) if( bits < (n) ) { \
while( bits <= 24 ) { \
bitbuf = (bitbuf << 8) | *bs_ptr++; \
bits += 8; \
} \
}
/*--------------------------------------------------------------*/
#define mac_load(n) ( bits -= n, \
bitval = bitbuf >> bits, \
bitbuf -= bitval << bits, \
bitval )
/*======================================================================*/
static void unpack_ba()
{
int i, j, k;
static int nbit[4] =
{4, 4, 3, 2};
int nstereo;
bit_skip = 0;
nstereo = stereo_sb;
k = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < nbat[i]; j++, k++)
{
mac_load_check(4);
ballo[k] = samp_dispatch[k] = bat[i][mac_load(nbit[i])];
if (k >= nsb_limit)
bit_skip += bat_bit_master[samp_dispatch[k]];
c_value[k] = look_c_value[samp_dispatch[k]];
if (--nstereo < 0)
{
ballo[k + 1] = ballo[k];
samp_dispatch[k] += 18; /* flag as joint */
samp_dispatch[k + 1] = samp_dispatch[k]; /* flag for sf */
c_value[k + 1] = c_value[k];
k++;
j++;
}
}
}
samp_dispatch[nsb_limit] = 37; /* terminate the dispatcher with skip */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -