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

📄 layer3.cpp

📁 < VC++视频音频开发>> 这本书的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			new_slen[1] = (int_scalefac_comp % 36 ) / 6 ;
			new_slen[2] = (int_scalefac_comp % 36) % 6;
			new_slen[3] = 0;
         si->ch[ch].gr[gr].preflag = 0;
         blocknumber = 3;
      } else if (int_scalefac_comp < 244) {
			new_slen[0] = ((int_scalefac_comp - 180 )  & 0x3F) >> 4 ;
			new_slen[1] = ((int_scalefac_comp - 180) & 0xF) >> 2 ;
			new_slen[2] = (int_scalefac_comp - 180 ) & 3 ;
			new_slen[3] = 0;
         si->ch[ch].gr[gr].preflag = 0;
         blocknumber = 4;
      } else if (int_scalefac_comp < 255) {
			new_slen[0] = (int_scalefac_comp - 244 ) / 3 ;
			new_slen[1] = (int_scalefac_comp - 244 )  % 3 ;
			new_slen[2] = 0 ;
			new_slen[3] = 0;
         si->ch[ch].gr[gr].preflag = 0;
         blocknumber = 5;
      }
   }

   for (uint32 x=0; x<45; x++) // why 45, not 54?
   	scalefac_buffer[x] = 0;

   m = 0;
   for (uint32 i=0; i<4;i++) {
     	for (uint32 j = 0; j < nr_of_sfb_block[blocknumber][blocktypenumber][i];
      	 j++)
      {
        scalefac_buffer[m] = (new_slen[i] == 0) ? 0 :
        							  br->hgetbits(new_slen[i]);
        m++;

      } // for (unint32 j ...
   } // for (uint32 i ...
}

void LayerIII_Decoder::get_LSF_scale_factors(uint32 ch, uint32 gr)
{
	uint32 m = 0;
   uint32 sfb, window;
	gr_info_s *gr_info = &(si->ch[ch].gr[gr]);

   get_LSF_scale_data(ch, gr);

   if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
      if (gr_info->mixed_block_flag) { 	// MIXED
         for (sfb = 0; sfb < 8; sfb++)
         {
              scalefac[ch].l[sfb] = scalefac_buffer[m];
              m++;
         }
         for (sfb = 3; sfb < 12; sfb++) {
            for (window=0; window<3; window++)
            {
               scalefac[ch].s[window][sfb] = scalefac_buffer[m];
               m++;
            }
         }
         for (window=0; window<3; window++)
            scalefac[ch].s[window][12] = 0;

      } else {  // SHORT

           for (sfb = 0; sfb < 12; sfb++) {
               for (window=0; window<3; window++)
               {
                  scalefac[ch].s[window][sfb] = scalefac_buffer[m];
                  m++;
               }
           }

           for (window=0; window<3; window++)
               scalefac[ch].s[window][12] = 0;
      }
   } else {   // LONG types 0,1,3

      for (sfb = 0; sfb < 21; sfb++) {
          scalefac[ch].l[sfb] = scalefac_buffer[m];
          m++;
      }
      scalefac[ch].l[21] = 0; // Jeff
      scalefac[ch].l[22] = 0;
	}
}

void LayerIII_Decoder::huffman_decode(uint32 ch, uint32 gr)
{
	int32 x, y;
	int32 v, w;
   int32 part2_3_end = part2_start + si->ch[ch].gr[gr].part2_3_length;
   int32 num_bits;
	int32 region1Start;
	int32 region2Start;
   int32 index;
 	struct huffcodetab *h;

	// Find region boundary for short block case

	if ( (si->ch[ch].gr[gr].window_switching_flag) &&
		  (si->ch[ch].gr[gr].block_type == 2) ) {

		// Region2.

		region1Start = 36;  // sfb[9/3]*3=36
		region2Start = 576; // No Region2 for short block case

	} else {          // Find region boundary for long block case

		region1Start = sfBandIndex[sfreq].l[si->ch[ch].gr[gr].region0_count
                                             + 1];
		region2Start = sfBandIndex[sfreq].l[si->ch[ch].gr[gr].region0_count +
										   si->ch[ch].gr[gr].region1_count + 2]; /* MI */
   }

   index = 0;
	// Read bigvalues area
	for (uint32 i=0; i<(si->ch[ch].gr[gr].big_values<<1); i+=2) {
		if      (i<region1Start) h = &ht[si->ch[ch].gr[gr].table_select[0]];
		else if (i<region2Start) h = &ht[si->ch[ch].gr[gr].table_select[1]];
			  else                h = &ht[si->ch[ch].gr[gr].table_select[2]];

		huffman_decoder(h, &x, &y, &v, &w, br);

      is_1d[index++] = x;
      is_1d[index++] = y;
	}

	// Read count1 area
	h = &ht[si->ch[ch].gr[gr].count1table_select+32];
   num_bits = br->hsstell();

	while ((num_bits < part2_3_end) && (index < 576)) {

		huffman_decoder(h, &x, &y, &v, &w, br);

      is_1d[index++] = v;
      is_1d[index++] = w;
      is_1d[index++] = x;
      is_1d[index++] = y;
      num_bits = br->hsstell();
   }

	if (num_bits > part2_3_end) {
		br->rewindNbits(num_bits - part2_3_end);
      index-=4;
   }

   num_bits = br->hsstell();

	// Dismiss stuffing bits
	if (num_bits < part2_3_end)
   	br->hgetbits(part2_3_end - num_bits);

	// Zero out rest

   if (index < 576)
	   nonzero[ch] = index;
   else
   	nonzero[ch] = 576;

   // may not be necessary
   for (; index<576; index++)
   	is_1d[index] = 0;
}

void LayerIII_Decoder::dequantize_sample(real xr[SBLIMIT][SSLIMIT],
												     uint32 ch, uint32 gr)
{
	gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
	int32  cb=0;
	int32  next_cb_boundary, cb_begin, cb_width;
	int32  index=0, t_index, j;
   real   g_gain;
   real  *xr_1d = &xr[0][0];

	// choose correct scalefactor band per block type, initalize boundary

	if (gr_info->window_switching_flag && (gr_info->block_type == 2) ) {
		if (gr_info->mixed_block_flag)
			next_cb_boundary=sfBandIndex[sfreq].l[1];  // LONG blocks: 0,1,3
		else {
         cb_width = sfBandIndex[sfreq].s[1];
		   next_cb_boundary = (cb_width << 2) - cb_width;
	 	   cb_begin = 0;
		}
	} else {
		next_cb_boundary=sfBandIndex[sfreq].l[1];  // LONG blocks: 0,1,3
   }

   // Compute overall (global) scaling.

	g_gain = (real) pow(2.0 , (0.25 * (gr_info->global_gain - 210.0)));

  	for (j=0; j<nonzero[ch]; j++) {
    	if (is_1d[j] == 0) {
			xr_1d[j] = 0.0f;
      } else {
         int32 abv = is_1d[j];
         if (is_1d[j] > 0)
	  		   xr_1d[j] = g_gain * t_43[abv];
         else
         	xr_1d[j] = -g_gain * t_43[-abv];
      }
   }

	// apply formula per block type

   for (j=0; j<nonzero[ch]; j++) {

		if (index == next_cb_boundary)  { /* Adjust critical band boundary */
      	if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
         	if (gr_info->mixed_block_flag)  {

            	if (index == sfBandIndex[sfreq].l[8])  {
                  next_cb_boundary = sfBandIndex[sfreq].s[4];
                  next_cb_boundary = (next_cb_boundary << 2) -
                    			           next_cb_boundary;
                  cb = 3;
                  cb_width = sfBandIndex[sfreq].s[4] -
                    			  sfBandIndex[sfreq].s[3];

                  cb_begin = sfBandIndex[sfreq].s[3];
                  cb_begin = (cb_begin << 2) - cb_begin;

               } else if (index < sfBandIndex[sfreq].l[8]) {

               	next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];

               } else {

               	next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1];
                  next_cb_boundary = (next_cb_boundary << 2) -
                    				        next_cb_boundary;

                  cb_begin = sfBandIndex[sfreq].s[cb];
						cb_width = sfBandIndex[sfreq].s[cb+1] -
                             cb_begin;
                  cb_begin = (cb_begin << 2) - cb_begin;
               }

            } else  {

               next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1];
               next_cb_boundary = (next_cb_boundary << 2) -
                                  next_cb_boundary;

               cb_begin = sfBandIndex[sfreq].s[cb];
					cb_width = sfBandIndex[sfreq].s[cb+1] -
                          cb_begin;
               cb_begin = (cb_begin << 2) - cb_begin;
            }

         } else  { // long blocks

					next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];

         }
      }

		// Do long/short dependent scaling operations

		if (gr_info->window_switching_flag &&
			 (((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 0)) ||
			  ((gr_info->block_type == 2) && gr_info->mixed_block_flag && (j >= 36)) ))
      {

			t_index = (index - cb_begin) / cb_width;
/*            xr[sb][ss] *= pow(2.0, ((-2.0 * gr_info->subblock_gain[t_index])
                                    -(0.5 * (1.0 + gr_info->scalefac_scale)
                                      * scalefac[ch].s[t_index][cb]))); */
			uint32 idx = scalefac[ch].s[t_index][cb]
           				 << gr_info->scalefac_scale;
         idx += (gr_info->subblock_gain[t_index] << 2);

			xr_1d[j] *= two_to_negative_half_pow[idx];

		} else {   // LONG block types 0,1,3 & 1st 2 subbands of switched blocks
/*				xr[sb][ss] *= pow(2.0, -0.5 * (1.0+gr_info->scalefac_scale)
													 * (scalefac[ch].l[cb]
													 + gr_info->preflag * pretab[cb])); */
			uint32 idx = scalefac[ch].l[cb];

   		if (gr_info->preflag)
		   	idx += pretab[cb];

		   idx = idx << gr_info->scalefac_scale;
         xr_1d[j] *= two_to_negative_half_pow[idx];
		}
      index++;
	}

   for (j=nonzero[ch]; j<576; j++)
     	xr_1d[j] = 0.0f;

   return;
}

void LayerIII_Decoder::reorder(real xr[SBLIMIT][SSLIMIT], uint32 ch, uint32 gr)
{

	gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
   uint32 freq, freq3;
   int32 index;
	int32 sfb, sfb_start, sfb_lines;
   int32 src_line, des_line;

   real *xr_1d = &xr[0][0];

	if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {

      for(index=0; index<576; index++)
         out_1d[index] = 0.0f;

		if (gr_info->mixed_block_flag) {
			// NO REORDER FOR LOW 2 SUBBANDS
            for (index = 0; index < 36; index++)
               out_1d[index] = xr_1d[index];

			// REORDERING FOR REST SWITCHED SHORT
				for(sfb=3,sfb_start=sfBandIndex[sfreq].s[3],
	  				 sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start;
	  				 sfb < 13; sfb++,sfb_start = sfBandIndex[sfreq].s[sfb],
				    (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
            {
					   int32 sfb_start3 = (sfb_start << 2) - sfb_start;

						for(freq=0, freq3=0; freq<sfb_lines;
                      freq++, freq3+=3) {

							src_line = sfb_start3 + freq;
							des_line = sfb_start3 + freq3;
							out_1d[des_line] = xr_1d[src_line];
							src_line += sfb_lines;
							des_line++;
							out_1d[des_line] = xr_1d[src_line];
							src_line += sfb_lines;
							des_line++;
							out_1d[des_line] = xr_1d[src_line];
					   }
            }

		} else {  // pure short
      	for(index=0;index<576;index++)
            out_1d[index] = xr_1d[reorder_table[sfreq][index]];
		}
	}
	else {   // long blocks
      for(index=0; index<576; index++)
      	out_1d[index] = xr_1d[index];
	}
}

void LayerIII_Decoder::i_stereo_k_values(uint32 is_pos, uint32 io_type,
												     uint32 i)
{
   if (is_pos == 0) {
      k[0][i] = 1.0f;
      k[1][i] = 1.0f;
   } else if (is_pos & 1) {
		k[0][i] = io[io_type][(is_pos + 1) >> 1];
      k[1][i] = 1.0f;
   } else {
      k[0][i] = 1.0f;
      k[1][i] = io[io_type][is_pos >> 1];
   }
}

void LayerIII_Decoder::stereo(uint32 gr)
{
   int32 sb, ss;

	if  (channels == 1) { // mono , bypass xr[0][][] to lr[0][][]

		for(sb=0;sb<SBLIMIT;sb++)
			for(ss=0;ss<SSLIMIT;ss+=3) {
				lr[0][sb][ss]   = ro[0][sb][ss];
            lr[0][sb][ss+1] = ro[0][sb][ss+1];
				lr[0][sb][ss+2] = ro[0][sb][ss+2];
         }

   } else {

	uint32 is_pos[576];
	real  is_ratio[576];
	gr_info_s *gr_info = &(si->ch[0].gr[gr]);
   uint32 mode_ext = header->mode_extension();
	int32 sfb;
	int32 i;
   int32 lines, temp, temp2;
	bool ms_stereo = (header->mode() == joint_stereo) && (mode_ext & 0x2);
	bool i_stereo  = (header->mode() == joint_stereo) && (mode_ext & 0x1);
   bool lsf = (header->version() == MPEG2_LSF);

	uint32 io_type = (gr_info->scalefac_compress & 1);

 	// initialization

   for (i=0; i<576; i++)
   	is_pos[i] = 7;

	if (i_stereo) {
   	if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
      	if (gr_info->mixed_block_flag) {

         	 int32 max_sfb = 0;

				 for (uint32 j=0; j<3; j++) {
            	int32 sfbcnt;
					sfbcnt = 2;
					for( sfb=12; sfb >=3; sfb-- ) {
               	i = sfBandIndex[sfreq].s[sfb];
						lines = sfBandIndex[sfreq].s[sfb+1] - i;
                  i = (i << 2) - i + (j+1) * lines - 1;

						while (lines > 0) {
                  	if (ro[1][ss_div[i]][ss_mod[i]] != 0.0f) {
                     	sfbcnt = sfb;
								sfb = -10;

⌨️ 快捷键说明

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