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

📄 encode.c

📁 ISO mp3 sources (distribution 10) Layer 1/2/3, C Source, 512 k Sources of the Mpeg 1,2 layer 1,2
💻 C
📖 第 1 页 / 共 5 页
字号:
 *	buffer[6]	Lc *	buffer[7]	Rc * * This function matrixes the original audio samples to pass to the * psychoacoustic model. The model considers the matrixed and non- * matrixed versions of the signal, so both are retained here. *  * On exit, buffer[0] to buffer[5] contain the channels * L5, R5, C5, (LFE, ) Ls, Rs, respectively. * On exit, buffer_matr[7] to buffer_matr[11] contain the channels * L7, R7, C7, Lc and Rc respectively. * **************************************************************************/void matricing_aug_fft (double (*buffer)[1152],			double (*buffer_matr)[1152],			frame_params *fr_ps){    double matrNorm;	/* factor for normalizing JMZ */    double matrC;	/* weighting factor for C */    double matrLR;	/* weighting factor for L, R */    double matrLsRs;	/* weighting factor for Ls, Rs */    int i, j, k, l;    int lfe;    double dummy;        layer *info = fr_ps->header;    lfe = info->lfe;      switch (info->aug_mtx_proc)    {    case 0:	  matrNorm	= 1 / 1.75;	  matrC		= 0.25;	  matrLR	= 0.75;	  break;    case 1:	  matrNorm	= 0.5;	  matrC		= 0;	  matrLR	= 1;	  break;    case 3:	  matrNorm	= 1;	  matrC		= 0;	  matrLR	= 0;	  break;    }    for (i = 0; i < 1152; i++)    {	buffer_matr[3][i] = buffer[3+lfe][i];	/* Ls */	buffer_matr[4][i] = buffer[4+lfe][i];	/* Rs */   	buffer_matr[7][i] = buffer[0][i];	/* L7 */	buffer_matr[8][i] = buffer[1][i];	/* R7 */	buffer_matr[9][i] = buffer[2][i];	/* C7 */	buffer_matr[10][i] = buffer[5+lfe][i];	/* Lc */	buffer_matr[11][i] = buffer[6+lfe][i];	/* Rc */	/* calculate L5, R5, C5 */	buffer_matr[0][i] = matrNorm * (buffer_matr[7][i] + matrLR * buffer_matr[10][i]);	buffer_matr[1][i] = matrNorm * (buffer_matr[8][i] + matrLR * buffer_matr[11][i]);	buffer_matr[2][i] = matrNorm * (buffer_matr[9][i] + matrC * (buffer_matr[10][i] + buffer_matr[11][i]));    }    switch (info->matrix)    {    case 0:    case 2:	  matrNorm	= 1 / (1 + sqrt(2.0));	  matrC		= 1 / sqrt(2.0);	  matrLsRs	= 1 / sqrt(2.0);	  break;    case 1:	  matrNorm	= 1 / (1.5 + 0.5*sqrt(2.0));	  matrC		= 1 / sqrt(2.0);	  matrLsRs	= 0.5;	  break;    case 3:	  matrNorm	= 1;	  matrC		= 1;	  matrLsRs	= 1;	  break;    }    for (i = 0; i < 1152; i++)    {	buffer_matr[5][i] = buffer_matr[0][i];	buffer_matr[6][i] = buffer_matr[1][i];   	switch (info->matrix)	{	case 0:	case 1: buffer_matr[0][i] = matrNorm * (buffer_matr[0][i] +						matrC * buffer_matr[2][i] +						matrLsRs * buffer_matr[3][i]);		buffer_matr[1][i] = matrNorm * (buffer_matr[1][i] +						matrC * buffer_matr[2][i] +						matrLsRs * buffer_matr[4][i]);		break;	case 2: buffer_matr[0][i] = matrNorm * (buffer_matr[0][i] +						matrC * buffer_matr[2][i] -						matrLsRs * 0.5 *						 (buffer_matr[3][i] + buffer_matr[4][i]));		buffer_matr[1][i] = matrNorm * (buffer_matr[1][i] +						matrC * buffer_matr[2][i] +						matrLsRs * 0.5 *						 (buffer_matr[3][i] + buffer_matr[4][i]));		break;	}    }}#endif/************************************************************************//*/* read_ana_window()/*/* PURPOSE:  Reads encoder window file "enwindow" into array #ana_win#/*/************************************************************************/ void read_ana_window(double *ana_win)       /*far*/                   {    int i,j[4];    FILE *fp;    double f[4];    char t[150];     if (!(fp = OpenTableFile("enwindow") ) ) {       printf("Please check analysis window table 'enwindow'\n");       exit(1);    }    for (i=0;i<512;i+=4) {       fgets(t, 80, fp); /* changed from 150, 92-08-11 shn */       sscanf(t,"C[%d] = %lf C[%d] = %lf C[%d] = %lf C[%d] = %lf\n",              j, f,j+1,f+1,j+2,f+2,j+3,f+3);       if (i==j[0]) {          ana_win[i] = f[0];          ana_win[i+1] = f[1];          ana_win[i+2] = f[2];          ana_win[i+3] = f[3];       }       else {          printf("Check index in analysis window table\n");          exit(1);       }       fgets(t,80,fp); /* changed from 150, 92-08-11 shn */    }    fclose(fp);}/************************************************************************//*/* window_subband()/*/* PURPOSE:  Overlapping window on PCM samples/*/* SEMANTICS:/* 32 16-bit pcm samples are scaled to fractional 2's complement and/* concatenated to the end of the window buffer #x#. The updated window/* buffer #x# is then windowed by the analysis window #c# to produce the/* windowed sample #z#/*/************************************************************************/ void window_subband (double **buffer, double *z, int k){    typedef double XX[14][HAN_SIZE];	/* 08/03/1995 JMZ Multilingual */    static XX *x;    int i, j;    static off[14]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0};/* 08/03/1995 JMZ Multilingual */    static char init = 0;    static double *c;     if (!init)    {	c = (double *) mem_alloc (sizeof(double) * HAN_SIZE, "window");	read_ana_window (c);	x = (XX *) mem_alloc (sizeof(XX), "x");	for (i = 0; i < 14; i++)	   for (j = 0; j < HAN_SIZE; j++)		 (*x)[i][j] = 0;	init = 1;    }     for (i = 0; i < 32; i++)	(*x)[k][31-i+off[k]] = (double) *(*buffer)++ / SCALE;    for (i = 0; i < HAN_SIZE; i++)	z[i] = (*x)[k][(i+off[k])&HAN_SIZE-1] * c[i];    off[k] += 480; /*offset is modulo (HAN_SIZE-1)*/    off[k] &= HAN_SIZE-1;} /************************************************************************//*/* create_ana_filter()/*/* PURPOSE:  Calculates the analysis filter bank coefficients/*/* SEMANTICS:/* Calculates the analysis filterbank coefficients and rounds to the/* 9th decimal place accuracy of the filterbank tables in the ISO/* document.  The coefficients are stored in #filter#/*/************************************************************************/ void create_ana_filter(double (*filter)[64])       /*far*/                     {   register int i,k;    for (i = 0; i < 32; i++)      for (k = 0; k < 64; k++) {          if ((filter[i][k] = 1e9*cos((double)((2*i+1)*(16-k)*PI64))) >= 0)             modf(filter[i][k]+0.5, &filter[i][k]);          else             modf(filter[i][k]-0.5, &filter[i][k]);          filter[i][k] *= 1e-9;   }}/************************************************************************//*/* filter_subband()/*/* PURPOSE:  Calculates the analysis filter bank coefficients/*/* SEMANTICS:/*      The windowed samples #z# is filtered by the digital filter matrix #m#/* to produce the subband samples #s#. This done by first selectively/* picking out values from the windowed samples, and then multiplying/* them by the filter matrix, producing 32 subband samples./*/************************************************************************/ void filter_subband_old(double *z, double *s)       /*far*/                         {   double y[64];   int i,j, k;static char init = 0;   typedef double MM[SBLIMIT][64];static MM /*far*/ *m;   double sum1, sum2;   #ifdef MS_DOS   long    SIZE_OF_MM;   SIZE_OF_MM      = SBLIMIT*64;   SIZE_OF_MM      *= 8;   if (!init) {       m = (MM /*far*/ *) mem_alloc(SIZE_OF_MM, "filter");       create_ana_filter(*m);       init = 1;   }#else   if (!init) {       m = (MM /*far*/ *) mem_alloc(sizeof(MM), "filter");       create_ana_filter(*m);       init = 1;   }#endif   /* Window */   for (i=0; i<64; i++)   {      for (k=0, sum1 = 0.0; k<8; k++)         sum1 += z[i+64*k];      y[i] = sum1;   }   /* Filter */   for (i=0;i<SBLIMIT;i++)   {       for (k=0, sum1=0.0 ;k<64;k++)          sum1 += (*m)[i][k] * y[k];       s[i] = sum1;   }/*   for (i=0;i<64;i++) for (j=0, y[i] = 0;j<8;j++) y[i] += z[i+64*j];*//*   for (i=0;i<SBLIMIT;i++)*//*       for (j=0, s[i]= 0;j<64;j++) s[i] += (*m)[i][j] * y[j];*/}/************************************************************************//* JMZ 08/03/1995 FILTER */void filter_subband(double *z, double *s)       /*far*/                         {   double y[64];   int i,j, k;static char init = 0;   typedef double MM[SBLIMIT][64];static MM /*far*/ *m;   double sum1, sum2;     if (!init) {       m = (MM /*far*/ *) mem_alloc(sizeof(MM), "filter");       create_ana_filter(*m);       init = 1;   }   /* Window */   for (i=0; i<64; i++)   {      for (k=0, sum1 = 0.0; k<8; k++)         sum1 += z[i+64*k];      y[i] = sum1;   }   /* Filter */#if VERY_FAST_FILTER    for (i=0; i<SBLIMIT/2; i++)   {       for (k=0, sum1=0.0, sum2=0.0; k<16;)       {	sum1 += (*m)[i][k] * (y[k]+y[32-k]); 	sum2 += (*m)[i][k+1] * (y[k+1]+y[31-k]); 	sum2 += (*m)[i][k+33] * (y[k+33]-y[63-k]); 	sum1 += (*m)[i][k+34] * (y[k+34]-y[62-k]); 	k+=2;       }	sum1 += (*m)[i][16]*y[16] - (*m)[i][48]*y[48]; 	s[i]    = sum1 + sum2;	s[31-i] = sum1 - sum2;   }#else   for (i=0;i<SBLIMIT;i++)   {       for (k=0, sum1=0.0 ;k<64;k++)          sum1 += (*m)[i][k] * y[k];       s[i] = sum1;   }#endif /*VERY_FAST_FILTER*/}/* JMZ 08/03/1995 FILTER *//************************************************************************//************************************************************************//*/* encode_info()/* encode_infomc1() SR/* encode_infomc2() SR/*/* PURPOSE:  Puts the syncword and header information on the output/* bitstream./*/************************************************************************/ void encode_info (frame_params *fr_ps, Bit_stream_struc *bs){    layer *info = fr_ps->header;    putbits (bs, 0xfff, 12);                   /* syncword 12 bits */    put1bit (bs, info->version);               /* ID        1 bit  */    putbits (bs, 4-info->lay, 2);              /* layer     2 bits */    put1bit (bs, !info->error_protection);     /* bit set => no err prot */    putbits (bs, info->bitrate_index, 4);    putbits (bs, info->sampling_frequency, 2);    put1bit (bs, info->padding);    put1bit (bs, info->extension);             /* private_bit */    putbits (bs, info->mode, 2);    putbits (bs, info->mode_ext, 2);    put1bit (bs, info->copyright);    put1bit (bs, info->original);    putbits (bs, info->emphasis, 2);}void encode_info_mc1 (frame_params *fr_ps, Bit_stream_struc *bs){    layer *info = fr_ps->header;    put1bit (bs, info->ext_bit_stream_present);    if(info->ext_bit_stream_present == 1)       putbits (bs, info->n_ad_bytes, 8);     putbits (bs, info->center, 2);     putbits (bs, info->surround, 2);    put1bit (bs, info->lfe);    put1bit (bs, info->audio_mix);    putbits (bs, info->matrix, 2);    putbits (bs, info->multiling_ch, 3);    put1bit (bs, info->multiling_fs);    put1bit (bs, info->multiling_lay);    put1bit (bs, info->copy_ident_bit);    put1bit (bs, info->copy_ident_start);}void encode_info_mc2 (frame_params *fr_ps, Bit_stream_struc *bs){    layer *info = fr_ps->header;    int i, j;           put1bit (bs, info->tc_sbgr_select);    put1bit (bs, info->dyn_cross_on);    put1bit (bs, info->mc_prediction_on);    /* 960627 FdB tca bits dependent on configuration */    if (fr_ps->config == 320 || fr_ps->config == 310)    {	/* 3 bits for tca's */	if (info->tc_sbgr_select == 1)	    putbits (bs, info->tc_allocation, 3);	else	    for (i = 0; i < 12; i++)		putbits (bs, info->tc_alloc[i], 3);    }    else if (fr_ps->config == 300 || fr_ps->config == 302 ||	     fr_ps->config == 220 || fr_ps->config == 210)    {	/* 2 bits for tca's */	if (info->tc_sbgr_select == 1)	    putbits (bs, info->tc_allocation, 2);	else	    for (i = 0; i < 12; i++)		putbits (bs, info->tc_alloc[i], 2);    }    if (info->dyn_cross_on == 1)    {	put1bit (bs, info->dyn_cross_LR);	for (i = 0; i < 12; i++)	{	    /* 960627 FdB DynX bits dependent on configuration */	    if (fr_ps->config == 320)		/* 3/2 */		putbits (bs, info->dyn_cross[i], 4);	    else if (fr_ps->config == 310 || fr_ps->config == 220)		/* 3/1 and 2/2 */		putbits (bs, info->dyn_cross[i], 3);	    else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)

⌨️ 快捷键说明

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