nok_prediction.c
来自「C写的MPEG4音频源代码(G.723/G.729)」· C语言 代码 · 共 498 行 · 第 1/2 页
C
498 行
Output: - References: - Explanation: Function stores the float into a variable of type 'unsigned long int', and performs the rounding with bit manipulation. The implementation assumes that the machine uses IEEE floating point numbers and that the size of the unsigned integers is 32 bits. This is based on code written for MPEG development. See document ISO/IEC JTC1/SC29/WG11 MPEG97/M1893. Author(s): - Modified by: Mikko Suonio *************************************************************************/static voidflt_round_8(float *pf){ int flg; unsigned long tmp, tmp1; float *pt = (float *)&tmp; *pt = *pf; /* Write float to memory */ tmp1 = tmp; /* save in tmp1 */ /* 'ul' denotes 'unsigned long' */ flg = tmp & 0x00008000ul; /* rounding position */ tmp &= 0xffff0000ul; /* truncated float */ *pf = *pt; /* round 1/2 lsb towards infinity */ if (flg) { tmp = tmp1 & 0xff810000ul; /* 2^e + 1/2 lsb */ *pf += *pt; /* add 2^e + 1/2 lsb */ tmp &= 0xff800000ul; /* extract 2^e */ *pf -= *pt; /* substract 2^e */ }}/******************************************************************************** *** FUNCTION: nok_predict() * *** * *** carry out prediction for all allowed spectral lines * *** * ********************************************************************************//* This function is based on the AT&T's implementation of the AAC decoder (compliant to the draft international standard). 1997-03-11 Mikko Suonio 1997-06-26 Lin Yin Final version */voidnok_predict (Info *info, int profile, int *lpflag, NOK_PRED_STATUS *psp, Float *prev_quant, Float *coef){ int i, j, ki, b, to, flag0, i0,i1,i2,i3,i4; short *top; Float pred_temp; Float coef_temp[1024]; float lpc_coef[NOK_LPC], tmp1, tmp2, tmp3, err; float y[DATA_LEN+2]; float r[2][2], r1[2]; unsigned short xtemp, temp1, temp2; if (profile != Main_Profile) { if (*lpflag != 0) { CommonExit(1, "Prediction not allowed in this profile!\n"); } else { /* prediction calculations not required */ return; } } if (info->islong) { for (i = 0; i < NOK_CLEN; i++) { coef_temp[i] = coef[i]; flt_round_8 (&coef_temp[i]); } b = 0; i = 0; top = info->sbk_sfb_top[b]; flag0 = *lpflag++; for (j = 0; j < MAX_PRED_SFB; j++) { to = *top++; for ( ; i < to/NRCSTA; i++) { i4 = i*4; i0 = i4 + psp->indicate; i1 = i4 + (psp->indicate + 1) % NRCSTA; i2 = i4 + (psp->indicate + 2) % NRCSTA; i3 = i4 + (psp->indicate + 3) % NRCSTA; xtemp=fs(prev_quant[i0]); for (ki = 0; ki<DATA_LEN+1; ki++) y[ki] = sf(psp->x_buffer[ki][i]); y[DATA_LEN+1] = sf(xtemp); tmp1 = y[1]*y[1]; tmp2 = y[2]*y[2]; tmp3 = y[3]*y[3]; r[0][0] = (tmp1+tmp2+tmp3)*2.0*1.005; r[1][1] = (y[0]*y[0]+tmp1+tmp2*2.0+tmp3+y[4]*y[4])*1.005; r[0][1] = y[0]*y[1]+((y[1]+y[3])*y[2])*2+y[3]*y[4]; r1[1] = ((y[0]+y[4])*y[2]+y[1]*y[3])*2.0; err = r[0][0]*r[1][1]-r[0][1]*r[0][1]; if (fabs(err) < 1.0e+1) { lpc_coef[0]=0.0; lpc_coef[1]=0.0; } else { temp1 = fs(err); temp2 = (temp1>>7); temp1 &= 0x007F; tmp1 = codebook[temp1+128]*codebook[temp2+256]; lpc_coef[0] = (r[1][1]-r1[1])*r[0][1]*tmp1; lpc_coef[1] = (-r[0][1]*r[0][1]+r[0][0]*r1[1])*tmp1; } quantize(lpc_coef[0], &psp->pred[0][i0]); quantize(lpc_coef[1], &psp->pred[1][i0]); for (ki=0; ki<DATA_LEN;ki++) psp->x_buffer[ki][i] = psp->x_buffer[ki+4][i]; psp->x_buffer[3][i] = fs(prev_quant[i1]); for (ki=0; ki<DATA_LEN-1;ki++) psp->x_buffer[4+ki][i] = psp->x_buffer[ki+7][i]; psp->x_buffer[6][i] = fs(prev_quant[i2]); for (ki=0; ki<DATA_LEN-2;ki++) psp->x_buffer[7+ki][i] = psp->x_buffer[ki+9][i]; psp->x_buffer[8][i] = fs(prev_quant[i3]); psp->x_buffer[9][i] = xtemp; if (flag0 && *lpflag) { pred_temp = codebook[psp->pred[0][i0]]*y[DATA_LEN+1] + codebook[psp->pred[1][i0]]*y[DATA_LEN]; coef[i0] += pred_temp; coef_temp[i0] += pred_temp; pred_temp = codebook[psp->pred[0][i1]]*sf(psp->x_buffer[3][i]) + codebook[psp->pred[1][i1]]*sf(psp->x_buffer[2][i]); coef[i1] += pred_temp; coef_temp[i1] += pred_temp; pred_temp = codebook[psp->pred[0][i2]]*sf(psp->x_buffer[6][i]) + codebook[psp->pred[1][i2]]*sf(psp->x_buffer[5][i]); coef[i2] += pred_temp; coef_temp[i2] += pred_temp; pred_temp = codebook[psp->pred[0][i3]]*sf(psp->x_buffer[8][i]) + codebook[psp->pred[1][i3]]*sf(psp->x_buffer[7][i]); coef[i3] += pred_temp; coef_temp[i3] += pred_temp; } } lpflag++; } psp->indicate++; psp->indicate = psp->indicate % NRCSTA; fltcpy(prev_quant, coef_temp, LN2); }}/******************************************************************************** *** FUNCTION: predict_reset() * *** * *** carry out cyclic predictor reset mechanism (long blocks) * *** resp. full reset (short blocks) * *** * ********************************************************************************/voidnok_predict_reset(Info* info, int *prstflag, NOK_PRED_STATUS **psp, int firstCh, int lastCh){ int j, i, prstflag0, prstgrp, ch; prstgrp = 0; if (info->islong) { prstflag0 = *prstflag++; if (prstflag0) { for (j=0; j<LEN_PRED_RSTGRP-1; j++) { prstgrp |= prstflag[j]; prstgrp <<= 1; } prstgrp |= prstflag[LEN_PRED_RSTGRP-1]; if (debug['r']) { fprintf(stderr,"PRST: prstgrp: %d prstbits: ", prstgrp); for (j=0; j<LEN_PRED_RSTGRP; j++) fprintf(stderr,"%d ", prstflag[j]); fprintf(stderr,"FIRST: %d LAST %d\n", firstCh, lastCh); } if ( (prstgrp<1) || (prstgrp>30) ) { fprintf(stderr, "ERROR in prediction reset pattern\n"); return; } for (ch=firstCh; ch<=lastCh; ch++) { for (j=prstgrp-1; j<NOK_CLEN; j+=30) { psp[ch]->pred[0][j] = 63; psp[ch]->pred[1][j] = 63; i=j/4; if(psp[ch]->indicate==0) { psp[ch]->x_buffer[0][i] = 0; psp[ch]->x_buffer[1][i] = 0; psp[ch]->x_buffer[2][i] = 0; psp[ch]->x_buffer[3][i] = 0; } if(psp[ch]->indicate==1) { psp[ch]->x_buffer[4][i] = 0; psp[ch]->x_buffer[5][i] = 0; psp[ch]->x_buffer[6][i] = 0; } if(psp[ch]->indicate==2) { psp[ch]->x_buffer[7][i] = 0; psp[ch]->x_buffer[8][i] = 0; } if(psp[ch]->indicate==3) psp[ch]->x_buffer[9][i] = 0; } for (j=prstgrp-1; j<LN2; j+=30) psp[ch]->prev_quant[j] = 0.0F; } } /* end predictor reset */ } /* end islong */ else { /* short blocks */ /* complete prediction reset in all bins */ for (ch=firstCh; ch<=lastCh; ch++) { nok_reset_pred_state (&psp[ch][0]); fltclr (psp[ch]->prev_quant, LN2); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?