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

📄 encode.c

📁 avi2mpg1_src 中包含了mpeg1编码的源程序
💻 C
📖 第 1 页 / 共 3 页
字号:
       mode_ext = 4;           /* 3 is least severe reduction */       lay = fr_ps->header->lay;       do {          --mode_ext;          fr_ps->jsbound = js_bound(lay, mode_ext);          rq_db = I_bits_for_nonoise(perm_smr, fr_ps);       } while( (rq_db > *adb) && (mode_ext > 0));       fr_ps->header->mode_ext = mode_ext;     }       }   noisy_sbs = I_a_bit_allocation(perm_smr, bit_alloc, adb, fr_ps);} /***************************** Layer II  ********************************/ void II_main_bit_allocation(perm_smr, scfsi, bit_alloc, adb, fr_ps)double perm_smr[2][SBLIMIT];unsigned int scfsi[2][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];int *adb;frame_params *fr_ps;{   int  noisy_sbs;   int  mode, mode_ext, lay;   int  rq_db, av_db = *adb;    if((mode = fr_ps->actual_mode) == MPG_MD_JOINT_STEREO) {     fr_ps->header->mode = MPG_MD_STEREO;     fr_ps->header->mode_ext = 0;     fr_ps->jsbound = fr_ps->sblimit;     if((rq_db=II_bits_for_nonoise(perm_smr, scfsi, fr_ps)) > *adb) {       fr_ps->header->mode = MPG_MD_JOINT_STEREO;       mode_ext = 4;           /* 3 is least severe reduction */       lay = fr_ps->header->lay;       do {         --mode_ext;         fr_ps->jsbound = js_bound(lay, mode_ext);         rq_db = II_bits_for_nonoise(perm_smr, scfsi, fr_ps);       } while( (rq_db > *adb) && (mode_ext > 0));       fr_ps->header->mode_ext = mode_ext;     }      }   noisy_sbs = II_a_bit_allocation(perm_smr, scfsi, bit_alloc, adb, fr_ps);} /************************************************************************/*/* I_a_bit_allocation  (Layer I)/* II_a_bit_allocation (Layer II)/*/* 功能:将bit加到掩蔽噪声比最低的子带上,直到达到子带所能容忍的最大bit数/*/* 结构描述:/* 1. 找到有最小掩蔽噪声比的子带和信道/* 2. 计算若将bit分配提高到更高层次所需的bit数目/* 3. 如果有足够的bit,并且子带没有达到最大的可分配数,则更新bit分配,
/*    掩蔽噪声比和相应的可供分配的位数/* 4. 重复进行上面的过程,知道没有bit剩余或没有满足条件的子带/*/************************************************************************/ int I_a_bit_allocation(perm_smr, bit_alloc, adb, fr_ps) /* return noisy sbs */double perm_smr[2][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];int *adb;frame_params *fr_ps;{   int i, k, smpl_bits, scale_bits, min_sb, min_ch, oth_ch;   int bspl, bscf, ad, noisy_sbs, done = 0, bbal ;   double mnr[2][SBLIMIT];   double smallm;   char used[2][SBLIMIT];   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;   al_table *alloc = fr_ps->alloc;   static char init= 0;   static int banc=32, berr=0;    if (!init)   {      init = 1;      if (fr_ps->header->error_protection) berr = 16;  /* added 92-08-11 shn */   }   bbal = 4 * ( (jsbound * stereo) + (SBLIMIT-jsbound) );   *adb -= bbal + berr + banc;   ad= *adb;    for (i=0;i<SBLIMIT;i++) for (k=0;k<stereo;k++)   {     mnr[k][i]=snr[0]-perm_smr[k][i];     bit_alloc[k][i] = 0;     used[k][i] = 0;   }   bspl = bscf = 0;    do   {      /* 找到有最小信掩(SMR)比的子带*/     smallm = mnr[0][0]+1;    min_sb = -1; min_ch = -1;     for (i=0;i<SBLIMIT;i++) for (k=0;k<stereo;k++)       /* 继续直到没有bit剩余 */       if (used[k][i] != 2 && smallm > mnr[k][i])	   {         smallm = mnr[k][i];         min_sb = i;  min_ch = k;       }     if(min_sb > -1) {         if (used[min_ch][min_sb])	   {		   smpl_bits = SCALE_BLOCK;		   scale_bits = 0;	   }       else	   {		   smpl_bits = 24;		   scale_bits = 6;	   }       if(min_sb >= jsbound)		   scale_bits *= stereo;        /* 检查是否还有足够的bit用来增加在最小的带宽中分辨率*/        if (ad >= bspl + bscf + scale_bits + smpl_bits)	   {         bspl += smpl_bits; /* 子带抽样bit */         bscf += scale_bits; /* 比例因子bit */         bit_alloc[min_ch][min_sb]++;         used[min_ch][min_sb] = 1;          mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb]                               + snr[bit_alloc[min_ch][min_sb]];                  if (bit_alloc[min_ch][min_sb] ==  14 )			 used[min_ch][min_sb] = 2;       }       else                     used[min_ch][min_sb] = 2;        if(stereo == 2 && min_sb >= jsbound)	   {         oth_ch = 1-min_ch;           bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];         used[oth_ch][min_sb] = used[min_ch][min_sb];         mnr[oth_ch][min_sb] = -perm_smr[oth_ch][min_sb]                               + snr[bit_alloc[oth_ch][min_sb]];       }     }   } while(min_sb>-1);           ad -= bspl+bscf;   *adb = ad;      noisy_sbs = 0; smallm = mnr[0][0];   for(k=0; k<stereo; ++k)   {     for(i = 0; i< SBLIMIT; ++i)	 {       if(mnr[k][i] < NOISY_MIN_MNR)		   ++noisy_sbs;       if(smallm > mnr[k][i])		   smallm = mnr[k][i];     }   }   return noisy_sbs;}/***************************** Layer II  ********************************/ int II_a_bit_allocation(perm_smr, scfsi, bit_alloc, adb, fr_ps)double perm_smr[2][SBLIMIT];unsigned int scfsi[2][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];int *adb;frame_params *fr_ps;{   int i, min_ch, min_sb, oth_ch, k, increment, scale, seli, ba;   int bspl, bscf, bsel, ad, noisy_sbs, bbal=0;   double mnr[2][SBLIMIT], smallm;   char used[2][SBLIMIT];   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;   al_table *alloc = fr_ps->alloc;static char init= 0;static int banc=32, berr=0;static int sfsPerScfsi[] = { 3,2,1,2 };       if (!init) {        init = 1;         if (fr_ps->header->error_protection) berr=16;    }   for (i=0; i<jsbound; ++i)     bbal += stereo * (*alloc)[i][0].bits;   for (i=jsbound; i<sblimit; ++i)     bbal += (*alloc)[i][0].bits;   *adb -= bbal + berr + banc;   ad = *adb;    for (i=0;i<sblimit;i++) for (k=0;k<stereo;k++) {     mnr[k][i]=snr[0]-perm_smr[k][i];     bit_alloc[k][i] = 0;     used[k][i] = 0;   }   bspl = bscf = bsel = 0;    do  {     smallm = 999999.0; min_sb = -1; min_ch = -1;     for (i=0;i<sblimit;i++) for(k=0;k<stereo;++k)       if (used[k][i]  != 2 && smallm > mnr[k][i]) {         smallm = mnr[k][i];         min_sb = i;  min_ch = k;     }     if(min_sb > -1) {          increment = SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]+1].group *                        (*alloc)[min_sb][bit_alloc[min_ch][min_sb]+1].bits);       if (used[min_ch][min_sb])         increment -= SCALE_BLOCK * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]].group*                           (*alloc)[min_sb][bit_alloc[min_ch][min_sb]].bits);        oth_ch = 1 - min_ch;          if (used[min_ch][min_sb]) scale = seli = 0;       else {          /* this channel had no bits or scfs before */         seli = 2;         scale = 6 * sfsPerScfsi[scfsi[min_ch][min_sb]];         if(stereo == 2 && min_sb >= jsbound) {           /* each new js sb has L+R scfsis */           seli += 2;           scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];         }       }             if (ad >= bspl + bscf + bsel + seli + scale + increment) {         ba = ++bit_alloc[min_ch][min_sb]; /* next up alloc */         bspl += increment;  /* bits for subband sample */         bscf += scale;      /* bits for scale factor */         bsel += seli;       /* bits for scfsi code */         used[min_ch][min_sb] = 1; /* subband has bits */         mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb] +                               snr[(*alloc)[min_sb][ba].quant+1];         /* Check if subband has been fully allocated max bits */         if (ba >= (1<<(*alloc)[min_sb][0].bits)-1) used[min_ch][min_sb] = 2;       }       else used[min_ch][min_sb] = 2; /* can't increase this alloc */       if(min_sb >= jsbound && stereo == 2) {         /* above jsbound, alloc applies L+R */         ba = bit_alloc[oth_ch][min_sb] = bit_alloc[min_ch][min_sb];         used[oth_ch][min_sb] = used[min_ch][min_sb];         mnr[oth_ch][min_sb] = -perm_smr[oth_ch][min_sb] +                               snr[(*alloc)[min_sb][ba].quant+1];       }     }   } while(min_sb > -1);   ///   ad -= bspl+bscf+bsel;   *adb = ad;   for (i=sblimit;i<SBLIMIT;i++) for (k=0;k<stereo;k++) bit_alloc[k][i]=0;    noisy_sbs = 0;  smallm = mnr[0][0];      /* calc worst noise in case */   for(k=0;k<stereo;++k) {     for (i=0;i<sblimit;i++) {       if (smallm > mnr[k][i]) smallm = mnr[k][i];       if(mnr[k][i] < NOISY_MIN_MNR) ++noisy_sbs; /* noise is not masked */     }   }   return noisy_sbs;} /************************************************************************/*/* I_subband_quantization  (Layer I)/* II_subband_quantization (Layer II)/*/* 功能 : 根据计算的比特分配进行量化子带/*/* 结构描述:  除以比例因子后的采样根据函数a×x+b进行量化,其中a和b是量化
  等级的函数。结果将根据量化位数进行截断,并将最高有效字节取反。/*/* /*/************************************************************************/ static double a[17] = {  0.750000000, 0.625000000, 0.875000000, 0.562500000, 0.937500000,  0.968750000, 0.984375000, 0.992187500, 0.996093750, 0.998046875,  0.999023438, 0.999511719, 0.999755859, 0.999877930, 0.999938965,  0.999969482, 0.999984741 }; static double b[17] = {  -0.250000000, -0.375000000, -0.125000000, -0.437500000, -0.062500000,  -0.031250000, -0.015625000, -0.007812500, -0.003906250, -0.001953125,  -0.000976563, -0.000488281, -0.000244141, -0.000122070, -0.000061035,  -0.000030518, -0.000015259 }; void I_subband_quantization(scalar, sb_samples, j_scale, j_samps,                            bit_alloc, sbband, fr_ps)unsigned int scalar[2][3][SBLIMIT];double sb_samples[2][3][SCALE_BLOCK][SBLIMIT];unsigned int j_scale[3][SBLIMIT];double j_samps[3][SCALE_BLOCK][SBLIMIT]; /* L+R for j-stereo if necess */unsigned int bit_alloc[2][SBLIMIT];unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT];frame_params *fr_ps;{   int i, j, k, n, sig;   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;   double d;static char init = 0;   if (!init) {     init = 1;     a[1] = a[2]; b[1] = b[2];     for (i=2;i<15;i++) { a[i] = a[i+2]; b[i] = b[i+2]; }   }   for (j=0;j<SCALE_BLOCK;j++) for (i=0;i<SBLIMIT;i++)     for (k=0;k<((i<jsbound)?stereo:1);k++)       if (bit_alloc[k][i]) {                  if(stereo == 2 && i>=jsbound)           d = j_samps[0][j][i] / multiple[j_scale[0][i]];         else           d = sb_samples[k][0][j][i] / multiple[scalar[k][0][i]];         n = bit_alloc[k][i];         d = d * a[n-1] + b[n-1];         if (d >= 0) sig = 1;         else { sig = 0; d += 1.0; }         sbband[k][0][j][i] = (unsigned int) (d * (double) (1L<<n));         if (sig) sbband[k][0][j][i] |= 1<<n;       }} /***************************** Layer II  ********************************/ void II_subband_quantization(scalar, sb_samples, j_scale, j_samps,                             bit_alloc, sbband, fr_ps)unsigned int scalar[2][3][SBLIMIT];double sb_samples[2][3][SCALE_BLOCK][SBLIMIT];unsigned int j_scale[3][SBLIMIT];double j_samps[3][SCALE_BLOCK][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT];frame_params *fr_ps;{   int i, j, k, s, n, qnt, sig;   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;   double d;   al_table *alloc = fr_ps->alloc;   for (s=0;s<3;s++)     for (j=0;j<SCALE_BLOCK;j++)       for (i=0;i<sblimit;i++)         for (k=0;k<((i<jsbound)?stereo:1);k++)           if (bit_alloc[k][i]) {                          if(stereo == 2 && i>=jsbound)       /* 使用联合立体声模式*/               d = j_samps[s][j][i] / multiple[j_scale[s][i]];             else               d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];             if (mod(d) > 1.0)               printf("Not scaled properly %d %d %d %d\n",k,s,j,i);             qnt = (*alloc)[i][bit_alloc[k][i]].quant;             d = d * a[qnt] + b[qnt];                          if (d >= 0) sig = 1;             else { sig = 0; d += 1.0; }             n = 0;#ifndef MS_DOS             stps = (*alloc)[i][bit_alloc[k][i]].steps;             while ((1L<<n) < stps) n++;#else             while  ( ( (unsigned long)(1L<<(long)n) <                       ((unsigned long) ((*alloc)[i][bit_alloc[k][i]].steps)                        & 0xffff                        )                       ) && ( n <16)                     ) n++;#endif             n--;             sbband[k][s][j][i] = (unsigned int) (d * (double) (1L<<n));                          if (sig) sbband[k][s][j][i] |= 1<<n;           }           for (s=0;s<3;s++)             for (j=sblimit;j<SBLIMIT;j++)               for (i=0;i<SCALE_BLOCK;i++) for (k=0;k<stereo;k++) sbband[k][s][i][j] = 0;} /************************************************************************/*/* I_encode_bit_alloc  (Layer I)/* II_encode_bit_alloc (Layer II)/*/* 功能 : 向比特流中写入比特分配信息
/*//************************************************************************/ void I_encode_bit_alloc(bit_alloc, fr_ps, bs)unsigned int bit_alloc[2][SBLIMIT];frame_params *fr_ps;Bit_stream_struc *bs;{   int i,k;   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;    for (i=0;i<SBLIMIT;i++)     for (k=0;k<((i<jsbound)?stereo:1);k++) putabits(bs,bit_alloc[k][i],4);} /***************************** Layer II  ********************************/ void II_encode_bit_alloc(bit_alloc, fr_ps, bs)unsigned int bit_alloc[2][SBLIMIT];frame_params *fr_ps;Bit_stream_struc *bs;{   int i,k;   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;   al_table *alloc = fr_ps->alloc;    for (i=0;i<sblimit;i++)     for (k=0;k<((i<jsbound)?stereo:1);k++)       putabits(bs,bit_alloc[k][i],(*alloc)[i][0].bits);} /************************************************************************/*/* I_sample_encoding   (Layer I)/* II_sample_encoding  (Layer II)/*/* 功能 :将一帧子带采样信号送入比特流/*/* 结构描述:  每一个抽样的bit数由#bit_alloc#提供。层2支持非2的幂的量化步长。/*/************************************************************************/ void I_sample_encoding(sbband, bit_alloc, fr_ps, bs)unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];frame_params *fr_ps;Bit_stream_struc *bs;{   int i,j,k;   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;    for(j=0;j<SCALE_BLOCK;j++) {     for(i=0;i<SBLIMIT;i++)       for(k=0;k<((i<jsbound)?stereo:1);k++)         if(bit_alloc[k][i]) putabits(bs,sbband[k][0][j][i],bit_alloc[k][i]+1);   }} /***************************** Layer II  ********************************/ void II_sample_encoding(sbband, bit_alloc, fr_ps, bs)unsigned int sbband[2][3][SCALE_BLOCK][SBLIMIT];unsigned int bit_alloc[2][SBLIMIT];frame_params *fr_ps;Bit_stream_struc *bs;{   unsigned int temp;   unsigned int j,s,x,y;   int i, k;   int stereo  = fr_ps->stereo;   int sblimit = fr_ps->sblimit;   int jsbound = fr_ps->jsbound;   al_table *alloc = fr_ps->alloc;    for (s=0;s<3;s++)     for (j=0;j<SCALE_BLOCK;j+=3)       for (i=0;i<sblimit;i++)         for (k=0;k<((i<jsbound)?stereo:1);k++)           if (bit_alloc[k][i]) {             if ((*alloc)[i][bit_alloc[k][i]].group == 3) {               for (x=0;x<3;x++) putabits(bs,sbband[k][s][j+x][i],                                         (*alloc)[i][bit_alloc[k][i]].bits);             }             else {               y =(*alloc)[i][bit_alloc[k][i]].steps;               temp = sbband[k][s][j][i] +                      sbband[k][s][j+1][i] * y +                      sbband[k][s][j+2][i] * y * y;               putabits(bs,temp,(*alloc)[i][bit_alloc[k][i]].bits);             }           }} /************************************************************************/*/* encode_CRC/*/************************************************************************/ void encode_CRC(crc, bs)unsigned int crc;Bit_stream_struc *bs;{   putabits(bs, crc, 16);}

⌨️ 快捷键说明

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