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

📄 aac_qc.c

📁 MPEG2/MPEG4编解码参考程序(实现了MPEG4的部分功能)
💻 C
📖 第 1 页 / 共 4 页
字号:
      for (i=0 ; i<no_coded_lines; i++ ) {         spectrum[ch][k*lines_per_subblock+i] = p_spectrum[ch][k*lines_per_subblock+i];       }      for (i=no_coded_lines; i<lines_per_subblock; i++ ) {         spectrum[ch][k*lines_per_subblock+i] = 0.0;       }    }    /** create the sfb_offset tables **/    nr_of_sfs = nr_of_sfb[MONO_CHAN] * num_window_groups;    if (windowSequence[MONO_CHAN] == EIGHT_SHORT_SEQUENCE) {      sort_for_grouping( sfb_offset, sfb_width_table[MONO_CHAN],	                 spectrum[ch], num_window_groups,		         window_group_length, nr_of_sfb[MONO_CHAN],		         allowed_distortion, allowed_dist[MONO_CHAN], blockSizeSamples		       );      if  ((qc_select != AAC_SCALABLE) && (qc_select != AAC_SYS)) {        extra_bits = 51 * nr_of_chan;      }    } else {      if ((windowSequence[MONO_CHAN] == ONLY_LONG_SEQUENCE) ||  	  (windowSequence[MONO_CHAN] == LONG_START_SEQUENCE) ||           (windowSequence[MONO_CHAN] == LONG_STOP_SEQUENCE)) {        sfb_offset[0] = 0;        k=0;        for( i=0; i<nr_of_sfb[MONO_CHAN]; i++ ){	  sfb_offset[i] = k;          k +=sfb_width_table[MONO_CHAN][i];          allowed_distortion[i] = allowed_dist[MONO_CHAN][i];        }        sfb_offset[i] = k;        if  ((qc_select != AAC_SCALABLE) && (qc_select != AAC_SYS)){          extra_bits = 120 * nr_of_chan; /* header bits and more ... */        }      } else {        CommonExit(-1,"\nERROR : unsupported window type, window type = %d\n",windowSequence[0]);      }     }    /* bits reservation for the TNS side info */	/*    if (tnsInfo && tnsInfo[MONO_CHAN]){      extra_bits += write_tns_data( tnsInfo[MONO_CHAN], windowSequence[MONO_CHAN], fixed_stream, 0 );    }	*/    /* bits reservation for the gain_control_data() TK */    if (gcBitBuf){        extra_bits += gcBitBuf[MONO_CHAN]->numBit;    }  }  for( ch=0; ch<nr_of_chan; ch++ ) {    /** Prepare Perceptual Noise Substitution **/    for(sfb=0; sfb<nr_of_sfs; sfb++ )      pns_sfb_flag[ch][sfb] = 0;    if (windowSequence[MONO_CHAN] != EIGHT_SHORT_SEQUENCE) {     /* long blocks only */      for(sfb=pns_sfb_start; sfb<nr_of_sfs; sfb++ ) {        /* Calc. pseudo scalefactor */        if (energy[ch][sfb] < FLT_MIN) {          pns_sfb_flag[ch][sfb] = 0;          continue;        }              pns_sfb_flag[ch][sfb] = 1;        pns_sfb_nrg[ch][sfb] = (int) (2.0 * log(energy[ch][sfb]+1e-60) / log(2.0) + 0.5) + PNS_SF_OFFSET;            /* Erase spectral lines */        for( i=sfb_offset[sfb]; i<sfb_offset[sfb+1]; i++ ) {          spectrum[ch][i] = 0.0;        }      }    }    /** find the maximum spectral coefficient **/    max_dct_line[ch] = 0;    for(i=0; i<sfb_offset[nr_of_sfs]; i++){      pow_spectrum[ch][i]=(pow(fabs(spectrum[ch][i]), 0.75));      if (fabs(spectrum[ch][i]) > max_dct_line[ch]){        max_dct_line[ch] = fabs(spectrum[ch][i]);      }    }    if (max_dct_line[ch] > 1)      common_scalefac[ch] = start_com_sf + (int)ceil(16./3. * (log(pow(max_dct_line[ch],0.75)/MAX_QUANT)/log(2.0)));    else      common_scalefac[ch] = 0;    if ( qdebug > 5 )      fprintf(stderr,"aac_qc.c: start common scalefactor %d, max_dct_line %f\n", common_scalefac[ch], max_dct_line[ch]);    if ((common_scalefac[ch]>200) || (common_scalefac[ch]<0) )      common_scalefac[ch] = 20;    /* initialize the scale_factors */    for(k=0; k<nr_of_sfs;k++){      sfb_amplify_check[ch][k] = 0;      calc_sf[ch][k]=1;      scale_factor[ch][k] = 0;      ratio[ch][k] = 0.0;    }  }  for( ch=0; ch<nr_of_chan; ch++ ) {    maxRatio    = 1;    largest_sb  = 0;    rateLoopCnt = 0;    do {  /* rate loop */      do {  /* distortion loop */        max_quant = 0;        largest_ratio = 0;        for (sb=0; sb<nr_of_sfs; sb++){	  sfb_amplify_check[ch][sb] = 0;	  if (calc_sf[ch][sb]){	    calc_sf[ch][sb] = 0;            quantFac = pow(2.0, 0.1875*(scale_factor[ch][sb] - common_scalefac[ch]));            invQuantFac = pow(2.0, -0.25 * (scale_factor[ch][sb] - common_scalefac[ch]));            error_energy[ch][sb] = 0.0;            ftmp=0;            for (i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++){              quant[ch][i] = (int)(pow_spectrum[ch][i] * quantFac  + MAGIC_NUMBER);              if (quant[ch][i]>MAX_QUANT)                CommonExit(-1,"\nMaxquant exceeded");              requant[ch][i] =  pow_quant[quant[ch][i]]  * invQuantFac;               quant[ch][i] = sgn(spectrum[ch][i]) * quant[ch][i];  /* restore the original sign */              /*  requant[][] = pow(quant[ch][i], (1.333333333)) * invQuantFac ; */              /* measure the distortion in each scalefactor band */              /* error_energy[sb] += pow((spectrum[ch][i] - requant), 2.0); */              linediff[ch][i] = (fabs(spectrum[ch][i]) - requant[ch][i]);              error_energy[ch][sb] += linediff[ch][i]*linediff[ch][i];            } /* --- for (i=sfb_offset[sb] --- */	    scale_factor_save[sb] = scale_factor[ch][sb];            if ( ( allowed_distortion[sb] != 0) && (energy[ch][sb] > allowed_distortion[sb]) ){              ratio[ch][sb] = error_energy[ch][sb] / allowed_distortion[sb];            } else {              ratio[ch][sb] = 1e-15;              sfb_amplify_check[ch][sb] = 1;            }            /* find the scalefactor band with the largest error ratio */            if ((ratio[ch][sb] > maxRatio) && (scale_factor[ch][sb]<60) && aacAllowScalefacs ) {              scale_factor[ch][sb]++;              sfb_amplify_check[ch][sb] = 1;              calc_sf[ch][sb]=1;            } else {              calc_sf[ch][sb]=0;	              }            if ( (ratio[ch][sb] > largest_ratio)&& (scale_factor[ch][sb]<60) ){              largest_ratio = ratio[ch][sb];            }          }        } /* ---  for (sb=0; --- */        /* amplify the scalefactor of the worst scalefactor band */        /* check to see if all the sfb's have been amplified.*/        /* if they have been all amplified, then all_amplified remains at 1 and we're done iterating */        all_amplified = 1;        for(j=0; j<nr_of_sfs-1;j++){          if (sfb_amplify_check[ch][j] == 0   )            all_amplified = 0;        }      } while ((largest_ratio > maxRatio) && (all_amplified == 0)  );       for (i=0; i<nr_of_sfs; i++){	scale_factor[ch][i] = scale_factor_save[i];      }        /* find a good method to section the scalefactor bands into huffman codebook sections */        bit_search( quant[ch], book_vector[ch], huffman_code_table, sfb_offset, nr_of_sfs, windowSequence[MONO_CHAN],nr_of_sfb[MONO_CHAN] );                /* Set special codebook for bands coded via PNS  */        if (windowSequence[MONO_CHAN] != EIGHT_SHORT_SEQUENCE) {     /* long blocks only */          for(k=0;k<nr_of_sfs;k++) {            if (pns_sfb_flag[ch][k]) {              if (book_vector[ch][k])                fprintf(stderr,"\n PNS: Subst. over non-zero book (%d), sfb=%d !", book_vector[ch][k], k);              book_vector[ch][k] = PNS_HCB;            }          }        }                /* calculate the amount of bits needed for encoding the huffman codebook numbers */        book_bits = sort_book_numbers( book_vector[ch], output_book_vector[ch], nr_of_sfs, qdebug, windowSequence[MONO_CHAN],                                        fixed_stream, 0 ,num_window_groups);                /* calculate the amount of bits needed for the spectral values */        counter = 0;        spectral_bits = 0;        for(k=0;k<nr_of_sfs;k++) {            calc_sf[ch][k]=1;          spectral_bits += output_bits( windowSequence[ch], huffman_code_table, book_vector[ch][k], quant[ch], sfb_offset[k], sfb_offset[k+1]-sfb_offset[k], data, len, &counter, 0 );        }#if 0        tmp = 0;        for(i=0;i<counter;i++) {          tmp += len[i];        }        if (spectral_bits != tmp)          CommonExit(-1,"\nwrong spectral bitcount");#endif                /* the number of bits for the scalefactors */        sf_bits = write_scalefactor_bitstream( nr_of_sfs, scale_factor[ch],                                               book_vector[ch], fixed_stream, 0,                                               window_group_length, num_window_groups, common_scalefac[ch], windowSequence[ch],                                               pns_sfb_nrg[ch], qdebug, huffman_code_table);                /* the total amount of bits required */        max_bits = spectral_bits + sf_bits + book_bits + extra_bits;        if (predictor_type == NOK_LTP)          max_bits += nok_lt_status[ch].side_info;        else if (predictor_type == NOK_BWP)          max_bits += nok_bwp_status[ch].side_info;	if(tnsInfo)	  max_bits += write_tns_data(tnsInfo[ch], windowSequence[ch], fixed_stream, 0);            /*     fprintf(stderr,"\n^^^ [bits to transmit = %d] global=%d spectral=%d sf=%d book=%d extra=%d", */      /* 	   max_bits,common_scalefac,spectral_bits,\ */      /* 	   sf_bits, book_bits, extra_bits); */      common_scalefac_save = common_scalefac[ch];      if (all_amplified){         maxRatio = maxRatio*2;        common_scalefac[ch] += 1;            }            common_scalefac[ch] += 1;      if ( common_scalefac[ch] > 200 ) {        CommonExit(-1,"\nERROR in loops : common_scalefac is %d",common_scalefac[ch] );      }      rateLoopCnt++;      /* --- TMP --- */      /* byte align !          printf("MAX bits %d to byte align=%d\n", max_bits, (8-(max_bits%8))%8);          max_bits += ((8-(max_bits%8))%8);       */          } while( max_bits > (average_block_bits/nr_of_chan) );        common_scalefac[ch] = common_scalefac_save;    if ( qdebug > 0 ) {      fprintf(stderr,"\n*** [bits transmitted = %d] spectral=%d sf=%d book=%d extra=%d largestRatio=%4.1f loopCnt=%d comm_sf=%d",	      max_bits, spectral_bits, sf_bits, book_bits, extra_bits,10*log10(largest_ratio+1e-15), rateLoopCnt, common_scalefac[ch] );    }    /* offset the difference of common_scalefac and scalefactors by SF_OFFSET  */    for (i=0; i<nr_of_sfs; i++){      if ( qdebug > 0 ) {        if ((error_energy[ch][i]>energy[ch][i]))          CommonWarning("\nerror_energy greater than energy in sfb: %d ", i);      }      scale_factor[ch][i] = common_scalefac[ch] - scale_factor[ch][i] + SF_OFFSET;    }    /* write the reconstructed spectrum to the output for use with prediction */    {      int i;      for (sb=0; sb<nr_of_sfs; sb++){	if ( qdebug > 4 ) {	  fprintf(stderr,"\nrequantval band %d : ",sb);	}	for (i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++){	  p_reconstructed_spectrum[ch][i] = sgn(quant[ch][i]) * requant[ch][i]; 	  if ( qdebug > 4 ) {	    fprintf(stderr,"%7.0f",(float)p_reconstructed_spectrum[ch][i]);	  }	}      }    }  } /* channel loop */  {    int ms_used[10][10];    int common_window=0;    int scale_flag=0;    int ms_mask = 0;  switch(qc_select)    {    case AAC_QC:        scale_flag = 0;        if (nr_of_chan == 1) {          common_window = 0;          bits_written += write_aac_sce(fixed_stream);        } else {          common_window = 1;          bits_written += write_aac_cpe(nr_of_sfs, windowSequence[MONO_CHAN], fixed_stream, num_window_groups,                                        window_group_length, window_shape[MONO_CHAN], common_window, ms_mask, ms_used,					predictor_type, nok_bwp_status, qc_select, nok_lt_status);        }        break;    case AAC_SCALABLE:    case AAC_SYS:        scale_flag = 1;      /* bits_written += write_sca_main_stream( */        break;    default:      CommonExit(-1,"\nundefined qc_mode in aac_qc.c");      break;    }  for( ch=0; ch<nr_of_chan; ch++ ) {    /* place the codewords and their respective lengths in arrays data[] and len[] respectively */    /* there are 'counter' elements in each array, and these are variable length arrays depending on the input */    counter = 0;    for(k=0;k<nr_of_sfs;k++) {        if ( qdebug>3 )        fprintf(stderr,"\n{band %d}  ",k);      output_bits(windowSequence[ch],huffman_code_table,book_vector[ch][k],quant[ch],sfb_offset[k],sfb_offset[k+1]-sfb_offset[k],data,len,&counter,1);    }    /* create the extra header information, and send out the raw data into the bitstream */    bits_written += write_ind_cha_stream(nr_of_sfs, windowSequence[MONO_CHAN], scale_factor[ch],                                         fixed_stream, gcBitBuf?gcBitBuf[ch]:(BsBitBuffer *)NULL,                                         tnsInfo ? tnsInfo[MONO_CHAN]:(TNS_INFO *)NULL,                                         book_vector[ch], data, len, qdebug, scale_flag, huffman_code_table,                                         counter, num_window_groups, window_group_length, scale_factor[ch][0],                                          window_shape[ch], pns_sfb_nrg[ch], predictor_type,                                          &nok_lt_status[ch], &nok_bwp_status[ch],                                         common_window, qc_select);    if ( qdebug > 1 ) {      debugPrint( nr_of_sfs, ratio[ch], energy[ch] ,allowed_distortion, error_energy[ch], scale_factor[ch], common_scalefac[ch] );    }    if ( qdebug > 5 ) {      for (sb=0; sb<nr_of_sfs; sb++){        fprintf(stderr,"\nquantval band %d : ",sb);        for (i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++){	  fprintf(stderr,"%5i",quant[ch][i]);        }      }        }  } /* channel loop */

⌨️ 快捷键说明

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