📄 mp3internals.cpp
字号:
/**********
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. (See <http://www.gnu.org/copyleft/lesser.html>.)
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 Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**********/
// "liveMedia"
// Copyright (c) 1996-2001 Live Networks, Inc. All rights reserved.
// MP3 internal implementation details
// Implementation
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "MP3Internals.hh"
// This is crufty old code that needs to be cleaned up #####
unsigned tabsel_123[2][3][16] = {
{ {32,32,64,96,128,160,192,224,256,288,320,352,384,416,448,448},
{32,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,384},
{32,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,320} },
{ {32,32,48,56,64,80,96,112,128,144,160,176,192,224,256,256},
{8,8,16,24,32,40,48,56,64,80,96,112,128,144,160,160},
{8,8,16,24,32,40,48,56,64,80,96,112,128,144,160,160} }
};
/* Note: tabsel_123[*][*][0 or 15] shouldn't occur; use dummy values there */
long freqs[7] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 };
static float ispow[8207];
static float aa_ca[8], aa_cs[8];
static float COS1[12][6];
static float win[4][36];
static float win1[4][36];
static float COS9[9];
static float COS6_1,COS6_2;
static float tfcos36[9];
static float tfcos12[3];
int longLimit[7][23];
int shortLimit[7][14];
struct bandInfoStruct {
int longIdx[23];
int longDiff[22];
int shortIdx[14];
int shortDiff[13];
};
struct bandInfoStruct bandInfo[7] = {
/* MPEG 1.0 */
{ {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
{4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
{0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3},
{4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
{ {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
{4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
{0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3},
{4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
{ {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
{4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
{0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3} ,
{4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
/* MPEG 2.0 */
{ {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
{0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} ,
{4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
{ {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
{6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
{0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3} ,
{4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
{ {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
{0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3},
{4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
/* MPEG 2.5, wrong! table (it's just a copy of MPEG 2.0/44.1kHz) */
{ {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
{0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} ,
{4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
};
static int mapbuf0[7][152];
static int mapbuf1[7][156];
static int mapbuf2[7][44];
static int *map[7][3];
static int *mapend[7][3];
unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
static float tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
static float pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
// Init tables for layer-3
static float gainpow2[256+118+4];
#define MPG_MD_MONO 3
#ifndef M_PI
#define M_PI 3.14159265358979323846 /* pi */
#endif
#ifndef M_SQRT2
#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
#endif
////////// MP3FrameParams //////////
MP3FrameParams::MP3FrameParams()
: bv(frameBytes, 0, sizeof frameBytes) /* by default */ {
oldHdr = firstHdr = 0;
static Boolean doneInit = False;
if (doneInit) return;
doneInit = True;
int i,j,k,l;
for (i=-256;i<118+4;i++) {
gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
}
for (i=0;i<8207;i++) {
ispow[i] = pow((double)i,(double)4.0/3.0);
}
for (i=0;i<8;i++) {
static double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
double sq=sqrt(1.0+Ci[i]*Ci[i]);
aa_cs[i] = 1.0/sq;
aa_ca[i] = Ci[i]/sq;
}
for (i=0;i<18;i++) {
win[0][i] = win[1][i] = 0.5 * sin( M_PI / 72.0 * (double) (2*(i+0) +1) ) / cos ( M_PI * (double) (2*(i+0) +19) / 72.0 );
win[0][i+18] = win[3][i+18] = 0.5 * sin( M_PI / 72.0 * (double) (2*(i+18)+1) ) / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
}
for (i=0;i<6;i++) {
win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
win[1][i+24] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 );
win[1][i+30] = win[3][i] = 0.0;
win[3][i+6 ] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 );
}
for (i=0;i<9;i++) {
COS9[i] = cos( M_PI / 18.0 * (double) i);
}
for (i=0;i<9;i++) {
tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
}
for(i=0;i<3;i++) {
tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
}
COS6_1 = cos( M_PI / 6.0 * (double) 1);
COS6_2 = cos( M_PI / 6.0 * (double) 2);
for (i=0;i<12;i++) {
win[2][i] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
for (j=0;j<6;j++) {
COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
}
}
for (j=0;j<4;j++) {
static int len[4] = { 36,36,12,36 };
for (i=0;i<len[j];i+=2) {
win1[j][i] = + win[j][i];
}
for (i=1;i<len[j];i+=2) {
win1[j][i] = - win[j][i];
}
}
for (i=0;i<16;i++) {
double t = tan( (double) i * M_PI / 12.0 );
tan1_1[i] = t / (1.0+t);
tan2_1[i] = 1.0 / (1.0 + t);
tan1_2[i] = M_SQRT2 * t / (1.0+t);
tan2_2[i] = M_SQRT2 / (1.0 + t);
for (j=0;j<2;j++) {
double base = pow(2.0,-0.25*(j+1.0));
double p1=1.0,p2=1.0;
if (i > 0) {
if (i & 1) {
p1 = pow(base,(i+1.0)*0.5);
} else {
p2 = pow(base,i*0.5);
}
}
pow1_1[j][i] = p1;
pow2_1[j][i] = p2;
pow1_2[j][i] = M_SQRT2 * p1;
pow2_2[j][i] = M_SQRT2 * p2;
}
}
for (j=0;j<7;j++) {
struct bandInfoStruct *bi = &bandInfo[j];
int *mp;
int cb,lwin;
int *bdf;
mp = map[j][0] = mapbuf0[j];
bdf = bi->longDiff;
for (i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
*mp++ = (*bdf) >> 1;
*mp++ = i;
*mp++ = 3;
*mp++ = cb;
}
bdf = bi->shortDiff+3;
for (cb=3;cb<13;cb++) {
int l = (*bdf++) >> 1;
for(lwin=0;lwin<3;lwin++) {
*mp++ = l;
*mp++ = i + lwin;
*mp++ = lwin;
*mp++ = cb;
}
i += 6*l;
}
mapend[j][0] = mp;
mp = map[j][1] = mapbuf1[j];
bdf = bi->shortDiff+0;
for (i=0,cb=0;cb<13;cb++) {
int l = (*bdf++) >> 1;
for (lwin=0;lwin<3;lwin++) {
*mp++ = l;
*mp++ = i + lwin;
*mp++ = lwin;
*mp++ = cb;
}
i += 6*l;
}
mapend[j][1] = mp;
mp = map[j][2] = mapbuf2[j];
bdf = bi->longDiff;
for (cb = 0; cb < 22 ; cb++) {
*mp++ = (*bdf++) >> 1;
*mp++ = cb;
}
mapend[j][2] = mp;
}
for (j=0;j<7;j++) {
for (i=0;i<23;i++) {
longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
if (longLimit[j][i] > SBLIMIT) {
longLimit[j][i] = SBLIMIT;
}
}
for (i=0;i<14;i++) {
shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
if (shortLimit[j][i] > SBLIMIT) {
shortLimit[j][i] = SBLIMIT;
}
}
}
for (i=0;i<5;i++) {
for (j=0;j<6;j++) {
for (k=0;k<6;k++) {
int n = k + j * 6 + i * 36;
i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
}
}
}
for (i=0;i<4;i++) {
for (j=0;j<4;j++) {
for (k=0;k<4;k++) {
int n = k + j * 4 + i * 16;
i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
}
}
}
for (i=0;i<4;i++) {
for (j=0;j<3;j++) {
int n = j + i * 3;
i_slen2[n+244] = i|(j<<3) | (5<<12);
n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
}
}
for (i=0;i<5;i++) {
for (j=0;j<5;j++) {
for (k=0;k<4;k++) {
for (l=0;l<4;l++) {
int n = l + k * 4 + j * 16 + i * 80;
n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
}
}
}
}
for (i=0;i<5;i++) {
for (j=0;j<5;j++) {
for (k=0;k<4;k++) {
int n = k + j * 4 + i * 20;
n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -