⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 layer3.c

📁 mpg123 是 MPEG 1.0/2.0/2.5 的实时播放软件
💻 C
📖 第 1 页 / 共 4 页
字号:
/*	leyer3.c: the layer 3 decoder	copyright 1995-2006 by the mpg123 project - free software under the terms of the LGPL 2.1	see COPYING and AUTHORS files in distribution or http://mpg123.de	initially written by Michael Hipp	Optimize-TODO: put short bands into the band-field without the stride of 3 reals	Length-optimze: unify long and short band code where it is possible	The int-vs-pointer situation has to be cleaned up.*/#include <stdlib.h>#include "config.h"#include "mpg123.h"#include "huffman.h"#include "common.h"#include "debug.h"#include "getbits.h"static real ispow[8207];static real aa_ca[8],aa_cs[8];static real COS1[12][6];static real win[4][36];static real win1[4][36];static real gainpow2[256+118+4];#ifdef USE_3DNOWreal COS9[9];static real COS6_1,COS6_2;real tfcos36[9];#elsestatic real COS9[9];static real COS6_1,COS6_2;static real tfcos36[9];#endifstatic real tfcos12[3];#define NEW_DCT9#ifdef NEW_DCT9static real cos9[3],cos18[3];#endifstruct bandInfoStruct {  int longIdx[23];  int longDiff[22];  int shortIdx[14];  int shortDiff[13];};int longLimit[9][23];int shortLimit[9][14];struct bandInfoStruct bandInfo[9] = { /* 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 } } ,/* mhipp trunk has 330 -> 332 without further explanation ... */ { {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 */ { {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,12,24,36,54,78,108,144,186,240,312,402,522,576},   {4,4,4,6,8,10,12,14,18,24,30,40,18} }, { {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,12,24,36,54,78,108,144,186,240,312,402,522,576},   {4,4,4,6,8,10,12,14,18,24,30,40,18} }, { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},   {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},   {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},   {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,};static int mapbuf0[9][152];static int mapbuf1[9][156];static int mapbuf2[9][44];static int *map[9][3];static int *mapend[9][3];static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];#ifdef GAPLESS/* still a dirty hack, places in bytes (zero-based)... */static unsigned long position; /* position in raw decoder bytestream */static unsigned long begin; /* first byte to play == number to skip */static unsigned long end; /* last byte to play */static unsigned long ignore; /* forcedly ignore stuff in between */static int bytified;/* input in bytes already */void layer3_gapless_init(unsigned long b, unsigned long e){	bytified = 0;	position = 0;	ignore = 0;	begin = b;	end = e;	debug2("layer3_gapless_init: from %lu to %lu samples", begin, end);}void layer3_gapless_set_position(unsigned long frames, struct frame* fr, struct audio_info_struct *ai){	position = samples_to_bytes(frames*spf(fr), fr, ai);	debug1("set; position now %lu", position);}void layer3_gapless_bytify(struct frame *fr, struct audio_info_struct *ai){	if(!bytified)	{		begin = samples_to_bytes(begin, fr, ai);		end = samples_to_bytes(end, fr, ai);		bytified = 1;		debug2("bytified: begin=%lu; end=%5lu", begin, end);	}}/* I need initialized fr here! */void layer3_gapless_set_ignore(unsigned long frames, struct frame *fr, struct audio_info_struct *ai){	ignore = samples_to_bytes(frames*spf(fr), fr, ai);}/*	take the (partially or fully) filled and remove stuff for gapless mode if needed	pcm_point may then be smaller than before...*/void layer3_gapless_buffercheck(){	/* pcm_point bytes added since last position... */	unsigned long new_pos = position + pcm_point;	if(begin && (position < begin))	{		debug4("new_pos %lu (old: %lu), begin %lu, pcm_point %i", new_pos, position, begin, pcm_point);		if(new_pos < begin)		{			if(ignore > pcm_point) ignore -= pcm_point;			else ignore = 0;			pcm_point = 0; /* full of padding/delay */		}		else		{			unsigned long ignored = begin-position;			/* we need to shift the memory to the left... */			debug3("old pcm_point: %i, begin %lu; good bytes: %i", pcm_point, begin, (int)(new_pos-begin));			if(ignore > ignored) ignore -= ignored;			else ignore = 0;			pcm_point -= ignored;			debug3("shifting %i bytes from %p to %p", pcm_point, pcm_sample+(int)(begin-position), pcm_sample);			memmove(pcm_sample, pcm_sample+(int)(begin-position), pcm_point);		}	}	/* I don't cover the case with both end and begin in chunk! */	else if(end && (new_pos > end))	{		ignore = 0;		/* either end in current chunk or chunk totally out */		debug2("ending at position %lu / point %i", new_pos, pcm_point);		if(position < end)	pcm_point -= new_pos-end;		else pcm_point = 0;		debug1("set pcm_point to %i", pcm_point);	}	else if(ignore)	{		if(pcm_point < ignore)		{			ignore -= pcm_point;			debug2("ignored %i bytes; pcm_point = 0; %lu bytes left", pcm_point, ignore);			pcm_point = 0;		}		else		{			/* we need to shift the memory to the left... */			debug3("old pcm_point: %i, to ignore: %lu; good bytes: %i", pcm_point, ignore, pcm_point-(int)ignore);			pcm_point -= ignore;			debug3("shifting %i bytes from %p to %p", pcm_point, pcm_sample+ignore, pcm_sample);			memmove(pcm_sample, pcm_sample+ignore, pcm_point);			ignore = 0;		}	}	position = new_pos;}#endif/*  * init tables for layer-3  */void init_layer3(int down_sample_sblimit){  int i,j,k,l;  for(i=-256;i<118+4;i++)#ifdef USE_MMX    if(!param.down_sample)      gainpow2[i+256] = 16384.0 * pow((double)2.0,-0.25 * (double) (i+210) );    else#endif    gainpow2[i+256] = DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210)));  for(i=0;i<8207;i++)    ispow[i] = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(1.0/sq);    aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq);  }  for(i=0;i<18;i++) {    win[0][i]    = win[1][i]    = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ));    win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 ));    win[1][i+24] = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(0.0);    win[3][i+6 ] = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i));  for(i=0;i<9;i++)    tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 ));  for(i=0;i<3;i++)    tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 ));  COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1));  COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2));#ifdef NEW_DCT9  cos9[0]  = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0));  cos9[1]  = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0));  cos9[2]  = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0));  cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0));  cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0));  cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0));#endif  for(i=0;i<12;i++) {    win[2][i]  = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(t / (1.0+t));    tan2_1[i] = DOUBLE_TO_REAL(1.0 / (1.0 + t));    tan1_2[i] = DOUBLE_TO_REAL(M_SQRT2 * t / (1.0+t));    tan2_2[i] = DOUBLE_TO_REAL(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] = DOUBLE_TO_REAL(p1);      pow2_1[j][i] = DOUBLE_TO_REAL(p2);      pow1_2[j][i] = DOUBLE_TO_REAL(M_SQRT2 * p1);      pow2_2[j][i] = DOUBLE_TO_REAL(M_SQRT2 * p2);    }  }  for(j=0;j<9;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<9;j++) {    for(i=0;i<23;i++) {      longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;      if(longLimit[j][i] > (down_sample_sblimit) )        longLimit[j][i] = down_sample_sblimit;    }    for(i=0;i<14;i++) {      shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;      if(shortLimit[j][i] > (down_sample_sblimit) )        shortLimit[j][i] = down_sample_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);      }    }  }}/* * read additional side information (for MPEG 1 and MPEG 2) */static int III_get_side_info(struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single,int lsf){   int ch, gr;   int powdiff = (single == 3) ? 4 : 0;   static const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };   const int *tab = tabs[lsf];      si->main_data_begin = getbits(tab[1]);   if (stereo == 1)     si->private_bits = getbits_fast(tab[2]);   else      si->private_bits = getbits_fast(tab[3]);   if(!lsf) {     for (ch=0; ch<stereo; ch++) {         si->ch[ch].gr[0].scfsi = -1;         si->ch[ch].gr[1].scfsi = getbits_fast(4);     }   }   for (gr=0; gr<tab[0]; gr++) {     for (ch=0; ch<stereo; ch++) {       register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);       gr_info->part2_3_length = getbits(12);       gr_info->big_values = getbits(9);       if(gr_info->big_values > 288) {          error("big_values too large!");          gr_info->big_values = 288;       }       gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;       if(ms_stereo)         gr_info->pow2gain += 2;       gr_info->scalefac_compress = getbits(tab[4]);       if(get1bit()) { /* window switch flag  */         int i;         gr_info->block_type       = getbits_fast(2);         gr_info->mixed_block_flag = get1bit();         gr_info->table_select[0]  = getbits_fast(5);         gr_info->table_select[1]  = getbits_fast(5);         /*          * table_select[2] not needed, because there is no region2,          * but to satisfy some verifications tools we set it either.          */         gr_info->table_select[2] = 0;         for(i=0;i<3;i++)           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);         if(gr_info->block_type == 0) {           error("Blocktype == 0 and window-switching == 1 not allowed.");           /* exit(1); */           return 1;         }               /* region_count/start parameters are implicit in this case. */                if(!lsf || gr_info->block_type == 2)           gr_info->region1start = 36>>1;         else {/* check this again for 2.5 and sfreq=8 */           if(sfreq == 8)             gr_info->region1start = 108>>1;           else             gr_info->region1start = 54>>1;         }         gr_info->region2start = 576>>1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -