ENCODE.C
资源名称:mpeg.zip [点击查看]
上传用户:njqiyou
上传日期:2007-01-08
资源大小:574k
文件大小:145k
源码类别:
mpeg/mp3
开发平台:
C/C++
- static int pattern[5][5] = {0x123, 0x122, 0x122, 0x133, 0x123,
- 0x113, 0x111, 0x111, 0x444, 0x113,
- 0x111, 0x111, 0x111, 0x333, 0x113,
- 0x222, 0x222, 0x222, 0x333, 0x123,
- 0x123, 0x122, 0x122, 0x133, 0x123};
- if (stereomc > 0 && fr_ps->sblimit_mc > fr_ps->sblimit)
- sblimit = fr_ps->sblimit_mc;
- else
- sblimit = fr_ps->sblimit;
- /* for (k = 0; k < stereo+stereomc+2; k++) 960814 FdB bug for some configurations */
- for (k = 0; k < 12; k++)
- for (i = 0; i < SBLIMIT; i++)
- {
- dscf[0] = (scalar[k][0][i] - scalar[k][1][i]);
- dscf[1] = (scalar[k][1][i] - scalar[k][2][i]);
- for (j = 0; j < 2; j++)
- {
- if (dscf[j] <= -3)
- class[j] = 0;
- else if (dscf[j] > -3 && dscf[j] < 0)
- class[j] = 1;
- else if (dscf[j] == 0)
- class[j] = 2;
- else if (dscf[j] > 0 && dscf[j] < 3)
- class[j] = 3;
- else
- class[j] = 4;
- }
- switch (pattern[class[0]][class[1]])
- {
- case 0x123 : scfsi[k][i] = 0;
- break;
- case 0x122 : scfsi[k][i] = 3;
- scalar[k][2][i] = scalar[k][1][i];
- break;
- case 0x133 : scfsi[k][i] = 3;
- scalar[k][1][i] = scalar[k][2][i];
- break;
- case 0x113 : scfsi[k][i] = 1;
- scalar[k][1][i] = scalar[k][0][i];
- break;
- case 0x111 : scfsi[k][i] = 2;
- scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
- break;
- case 0x222 : scfsi[k][i] = 2;
- scalar[k][0][i] = scalar[k][2][i] = scalar[k][1][i];
- break;
- case 0x333 : scfsi[k][i] = 2;
- scalar[k][0][i] = scalar[k][1][i] = scalar[k][2][i];
- break;
- case 0x444 : scfsi[k][i] = 2;
- if (scalar[k][0][i] > scalar[k][2][i])
- scalar[k][0][i] = scalar[k][2][i];
- scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
- } /* switch */
- } /* subband */
- if (n_ml_ch > 0)
- {
- for (k = 7; k < 7 + n_ml_ch; k++)
- for (i = 0; i < sblimit_ml; i++)
- {
- dscf[0] = (scalar[k][0][i] - scalar[k][1][i]);
- dscf[1] = (scalar[k][1][i] - scalar[k][2][i]);
- for (j = 0; j < 2; j++)
- {
- if (dscf[j] <= -3)
- class[j] = 0;
- else if (dscf[j] > -3 && dscf[j] < 0)
- class[j] = 1;
- else if (dscf[j] == 0)
- class[j] = 2;
- else if (dscf[j] > 0 && dscf[j] < 3)
- class[j] = 3;
- else
- class[j] = 4;
- }
- switch (pattern[class[0]][class[1]])
- {
- case 0x123 : scfsi[k][i] = 0;
- break;
- case 0x122 : scfsi[k][i] = 3;
- scalar[k][2][i] = scalar[k][1][i];
- break;
- case 0x133 : scfsi[k][i] = 3;
- scalar[k][1][i] = scalar[k][2][i];
- break;
- case 0x113 : scfsi[k][i] = 1;
- scalar[k][1][i] = scalar[k][0][i];
- break;
- case 0x111 : scfsi[k][i] = 2;
- scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
- break;
- case 0x222 : scfsi[k][i] = 2;
- scalar[k][0][i] = scalar[k][2][i] = scalar[k][1][i];
- break;
- case 0x333 : scfsi[k][i] = 2;
- scalar[k][0][i] = scalar[k][1][i] = scalar[k][2][i];
- break;
- case 0x444 : scfsi[k][i] = 2;
- if (scalar[k][0][i] > scalar[k][2][i])
- scalar[k][0][i] = scalar[k][2][i];
- scalar[k][1][i] = scalar[k][2][i] = scalar[k][0][i];
- } /* switch */
- } /* subband */
- }
- }
- /************************************************************************
- /*
- /* I_encode_scale (Layer I)
- /* II_encode_scale (Layer II)
- /*
- /* PURPOSE:The encoded scalar factor information is arranged and
- /* queued into the output fifo to be transmitted.
- /*
- /* For Layer II, the three scale factors associated with
- /* a given subband and channel are transmitted in accordance
- /* with the scfsi, which is transmitted first.
- /*
- /************************************************************************/
- void I_encode_scale(unsigned int (*scalar)[3][32], unsigned int (*bit_alloc)[32], frame_params *fr_ps, Bit_stream_struc *bs)
- {
- int stereo = fr_ps->stereo;
- int sblimit = fr_ps->sblimit;
- int i,j;
- for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
- if (bit_alloc[j][i]) putbits(bs,scalar[j][0][i],6);
- }
- /***************************** Layer II ********************************/
- void II_encode_scale (
- unsigned int (*bit_alloc)[32],
- unsigned int (*scfsi)[32],
- unsigned int (*scalar)[3][32], /* JMZ 08/03/1995 Multilingual */
- unsigned int lfe_alloc,
- unsigned int lfe_scalar,
- frame_params *fr_ps,
- Bit_stream_struc *bs,
- int *l,
- int *z
- )
- {
- layer *info = fr_ps->header;
- int center = info->center;
- int surround = info->surround;
- int stereo = fr_ps->stereo;
- int sblimit;
- int lfe = fr_ps->header->lfe;
- int i,j,k, m, n, pci;
- int pred;
- if (*l == 0)
- sblimit = fr_ps->sblimit;
- else
- sblimit = fr_ps->sblimit_mc;
- for (i = 0; i < sblimit; i++)
- {
- n = sbgrp[i];
- for (m = *l; m < *z; m++)
- {
- k = transmission_channel (fr_ps, n, m);
- if (bit_alloc[k][i] && (i < 12 || m != 2 || center != 3))
- putbits (bs, scfsi[k][i], 2);
- }
- }
- pred = 0;
- if (*l == stereo)
- {
- if (fr_ps->header->mc_prediction_on == 1)
- {
- for (i = 0; i < 8; i++)
- {
- if (fr_ps->header->mc_pred[i] == 1)
- {
- for (m = 0; m < n_pred_coef[fr_ps->header->dyn_cross[i]]; m++)
- {
- if (fr_ps->header->predsi[i][m] != 0)
- {
- putbits (bs, fr_ps->header->delay_comp[i][m], 3);
- pred += 3;
- for (pci = 0; pci < fr_ps->header->predsi[i][m]; pci++)
- {
- putbits (bs, fr_ps->header->pred_coef[i][m][pci], 8);
- pred += 8;
- }
- }
- }
- }
- }
- }
- }
- if (*l == stereo && lfe && lfe_alloc)
- putbits (bs, lfe_scalar, 6);
- for (i = 0; i < sblimit; i++)
- {
- n = sbgrp[i];
- for (m = *l; m < *z; m++)
- {
- k = transmission_channel (fr_ps, n, m);
- if (bit_alloc[k][i] && (i < 12 || m != 2 || center != 3))
- switch (scfsi[k][i])
- {
- case 0: for (j = 0; j < 3; j++)
- putbits (bs, scalar[k][j][i], 6);
- break;
- case 1:
- case 3: putbits (bs, scalar[k][0][i], 6);
- putbits (bs, scalar[k][2][i], 6);
- break;
- case 2: putbits (bs, scalar[k][0][i], 6);
- }
- }
- }
- }
- /*JMZ 24/2/95 Multilingual , WtK 7/8/95*/
- void II_encode_scale_ml (unsigned int (*bit_alloc)[32], unsigned int (*scfsi)[32],
- unsigned int (*scalar)[3][32], frame_params *fr_ps,
- Bit_stream_struc *bs, int *l, int *z)
- {
- int stereo = fr_ps->stereo;
- int sblimit_ml = fr_ps->sblimit_ml;
- int i,j,k, m, n, pci;
- for (i=0;i<sblimit_ml;i++)
- for (m = *l; m < *z; m++)
- {
- k=m;
- if (bit_alloc[k][i])
- putbits(bs,scfsi[k][i],2);
- }
- for (i=0;i<sblimit_ml;i++)
- for (m = *l;m < *z; m++)
- {
- k=m;
- if (bit_alloc[k][i])
- switch (scfsi[k][i])
- {
- case 0: for (j=0;j<3;j++)
- putbits(bs,scalar[k][j][i],6);
- break;
- case 1:
- case 3: putbits(bs,scalar[k][0][i],6);
- putbits(bs,scalar[k][2][i],6);
- break;
- case 2: putbits(bs,scalar[k][0][i],6);
- break;
- }
- }
- }
- /*=======================================================================
- | |
- | The following routines are done after the masking threshold |
- | has been calculated by the fft analysis routines in the Psychoacoustic |
- | model. Using the MNR calculated, the actual number of bits allocated |
- | to each subband is found iteratively. |
- | |
- =======================================================================*/
- /************************************************************************
- /*
- /* I_bits_for_nonoise (Layer I)
- /* II_bits_for_nonoise (Layer II)
- /*
- /* PURPOSE:Returns the number of bits required to produce a
- /* mask-to-noise ratio better or equal to the noise/no_noise threshold.
- /*
- /* SEMANTICS:
- /* bbal = # bits needed for encoding bit allocation
- /* bsel = # bits needed for encoding scalefactor select information
- /* banc = # bits needed for ancillary data (header info included)
- /*
- /* For each subband and channel, will add bits until one of the
- /* following occurs:
- /* - Hit maximum number of bits we can allocate for that subband
- /* - MNR is better than or equal to the minimum masking level
- /* (NOISY_MIN_MNR)
- /* Then the bits required for scalefactors, scfsi, bit allocation,
- /* and the subband samples are tallied (#req_bits#) and returned.
- /*
- /* (NOISY_MIN_MNR) is the smallest MNR a subband can have before it is
- /* counted as 'noisy' by the logic which chooses the number of JS
- /* subbands.
- /*
- /* Joint stereo is supported.
- /*
- /************************************************************************/
- /*static double snr[18] = {0.00, 7.00, 11.00, 16.00, 20.84,
- 25.28, 31.59, 37.75, 43.84,
- 49.89, 55.93, 61.96, 67.98, 74.01,
- 80.03, 86.05, 92.01, 98.01};*/
- static double snr[18] = { 0.00, 6.03, 11.80, 15.81, /* 0, 3, 5, 7 */
- 19.03, 23.50, 29.82, 35.99, /* 9,15,31,63 */
- 42.08, 48.13, 54.17, 60.20, /* 127, ... */
- 66.22, 72.25, 78.27, 84.29, /* 2047, ... */
- 90.31, 96.33}; /* 16383, ... */
- int I_bits_for_nonoise(double (*perm_smr)[32], frame_params *fr_ps)
- {
- int i,j,k;
- int stereo = fr_ps->stereo;
- int sblimit = fr_ps->sblimit;
- int jsbound = fr_ps->jsbound;
- int req_bits = 0;
- /* initial b_anc (header) allocation bits */
- req_bits = 32 + 4 * ( (jsbound * stereo) + (SBLIMIT-jsbound) );
- for(i=0; i<SBLIMIT; ++i)
- for(j=0; j<((i<jsbound)?stereo:1); ++j) {
- for(k=0;k<14; ++k)
- if( (-perm_smr[j][i] + snr[k]) >= fr_ps->mnr_min)
- break; /* we found enough bits */
- if(stereo == 2 && i >= jsbound) /* check other JS channel */
- for(;k<14; ++k)
- if( (-perm_smr[1-j][i] + snr[k]) >= fr_ps->mnr_min) break;
- if(k>0) req_bits += (k+1)*12 + 6*((i>=jsbound)?stereo:1);
- }
- return req_bits;
- }
- /***************************** Layer II ********************************/
- int II_bits_for_nonoise(double (*perm_smr)[32], unsigned int (*scfsi)[32], frame_params *fr_ps, int a, int b, int *aiff)
- {
- int sb,ch,ba,i;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int stereoaug = fr_ps->stereoaug;
- int n_ml_ch = fr_ps->header->multiling_ch; /* 23/03/1995 JMZ Multilingual */
- int sblimit = fr_ps->sblimit;
- int sblimit_mc = fr_ps->sblimit_mc;
- int sblimit_ml = fr_ps->sblimit_ml;
- int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc;
- al_table *alloc_mc = fr_ps->alloc_mc;
- al_table *alloc_ml = fr_ps->alloc_ml;
- int bbal = 0;
- int berr; /* before: =0 92-08-11 shn */
- int banc = 32; /* header ISO Layer II */
- int bancmc = 0; /* header multichannel = 93, 5.7.93,SR*/
- int bancext = 0; /* header multichannel = 93, 5.7.93,SR*/
- int maxAlloc, sel_bits, sc_bits, smp_bits;
- int req_bits = 0;
- int tca_bits, dynx_bits, aug_tca_bits;
- static int sfsPerScfsi[] = { 3,2,1,2 }; /* lookup # sfs per scfsi */
- if (verbosity >= 3) printf("bits_for_nonoisen");
- if( *aiff == 1)
- {
- if (fr_ps->header->ext_bit_stream_present == 0)
- bancmc += 35; /* mc_header + crc + tc_sbgr_select+ dyn_cross_on +
- mc_prediction_on 01/05/94, SR new! 05/04/94, SR*/
- else
- {
- bancmc += 43;
- bancext = 40;
- }
- /* 960627 FdB TCA bits dependent on configuration */
- if (fr_ps->config == 320)
- tca_bits = 3;
- else if (fr_ps->config == 310)
- tca_bits = 3;
- else if (fr_ps->config == 220)
- tca_bits = 2;
- else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)
- tca_bits = 2;
- else
- tca_bits = 0;
- if (fr_ps->header->tc_sbgr_select == 0)
- bancmc += 12 * tca_bits;
- else
- bancmc += tca_bits;
- if (fr_ps->header->dyn_cross_on == 1)
- {
- /* 960627 FdB DynX dependent on configuration */
- if (fr_ps->config == 320)
- dynx_bits = 4;
- else if (fr_ps->config == 310 || fr_ps->config == 220)
- dynx_bits = 3;
- else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)
- dynx_bits = 1;
- else
- dynx_bits = 0;
- bancmc = 1 + 12 * dynx_bits;
- if (fr_ps->header->surround == 3)
- bancmc += 12; /* now with dyn_second_stereo, 17/02/95, SR*/
- }
- if (fr_ps->header->mc_prediction_on == 1)
- {
- /* mc_pred, predsi, delay_comp, pred_coef */
- /* bancmc += 8 * (3 + 6 * (3 + 3 * 8)); */
- for (i = 0; i < 8; i++)
- {
- bancmc += 3;
- bancmc += n_pred_coef[fr_ps->header->dyn_cross[i]] * 27;
- }
- }
- #ifdef Augmentation_7ch
- if (stereoaug == 2)
- {
- if (fr_ps->header->aug_mtx_proc == 0)
- aug_tca_bits = 3;
- else if (fr_ps->header->aug_mtx_proc == 1)
- aug_tca_bits = 2;
- else
- aug_tca_bits = 0;
- bancmc += 12 * aug_tca_bits;
- if (fr_ps->header->aug_dyn_cross_on == 1)
- bancmc += 12 * 5;
- }
- #endif
- }
- else
- bancmc = 0;
- if (fr_ps->header->error_protection)
- berr=16;
- else
- berr=0; /* added 92-08-11 shn */
- for (sb = 0; sb < jsbound; sb++)
- bbal += (stereo) * (*alloc)[sb][0].bits;
- for (sb = jsbound; sb < sblimit; sb++)
- bbal += (stereo-1) * (*alloc)[sb][0].bits;
- for (sb = 0; sb < sblimit_mc; sb++)
- bbal += (stereomc+stereoaug) * (*alloc_mc)[sb][0].bits;
- for (sb = 0; sb < sblimit_ml; sb++)
- bbal += (n_ml_ch) * (*alloc_ml)[sb][0].bits;
- req_bits = banc + bancmc + bbal + berr;
- for (sb = 0; sb < SBLIMIT; sb++)
- for (ch = 0; ch < ((sb < jsbound)? stereo+stereomc+stereoaug : 1); ch++)
- {
- if (ch < stereo)
- {
- alloc = fr_ps->alloc;
- sblimit = fr_ps->sblimit;
- }
- else
- {
- alloc = fr_ps->alloc_mc;
- sblimit = fr_ps->sblimit_mc;
- }
- if (sb < sblimit)
- {
- maxAlloc = (1<<(*alloc)[sb][0].bits)-1;
- sel_bits = sc_bits = smp_bits = 0;
- for (ba = 0; ba < maxAlloc-1; ba++)
- if ((-perm_smr[ch][sb] + snr[(*alloc)[sb][ba].quant + ((ba>0)?1:0)])
- >= fr_ps->mnr_min)
- break;
- if (((b - a) >= 1) && (sb >= jsbound))
- { /* check other JS channels */
- for (; ba < maxAlloc-1; ba++)
- if ((-perm_smr[1-ch][sb] + snr[(*alloc)[sb][ba].quant + ((ba>0)?1:0)])
- >= fr_ps->mnr_min)
- break;
- }
- if (ba > 0)
- {
- smp_bits = 12 * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
- /* scale factor bits required for subband */
- sel_bits = 2;
- sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
- if (stereo == 2 && sb >= jsbound)
- {
- /* each new js sb has L+R scfsis*/
- sel_bits += 2;
- sc_bits += 6 * sfsPerScfsi[scfsi[1-ch][sb]];
- }
- req_bits += (smp_bits+sel_bits+sc_bits);
- }
- }
- }
- if (n_ml_ch > 0)
- {
- /* remaining part from jsbound to sblimit_ml in II_bits_for_indi() , WtK*/
- for (sb = 0; sb < (jsbound < sblimit_ml) ? jsbound:sblimit_ml; sb++)
- {
- for (ch = 7; ch < 7+n_ml_ch; ch++)
- {
- maxAlloc = (1<<(*alloc_ml)[sb][0].bits)-1;
- sel_bits = sc_bits = smp_bits = 0;
- for (ba = 0; ba < maxAlloc-1; ba++)
- if ((-perm_smr[ch][sb] + snr[(*alloc_ml)[sb][ba].quant + ((ba>0)?1:0)])
- >= fr_ps->mnr_min)
- break;
- if (ba > 0)
- {
- if (fr_ps->header->multiling_fs==1)
- smp_bits = 6 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
- else
- smp_bits = 12 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
- /* scale factor bits required for subband */
- sel_bits = 2;
- sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
- req_bits += (smp_bits+sel_bits+sc_bits);
- }
- }
- }
- }
- return req_bits;
- }
- /**********************************************************************/
- /*now for the independent channels, 8/6/93, SR */
- /**********************************************************************/
- int II_bits_for_indi(double (*perm_smr)[32], unsigned int (*scfsi)[32], frame_params *fr_ps, int *a, int *b, int *aiff)
- {
- int sb,ch,ba,i;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int stereoaug = fr_ps->stereoaug;
- int n_ml_ch = fr_ps->header->multiling_ch; /* 23/03/1995 JMZ Multilingual */
- int sblimit = fr_ps->sblimit_mc;
- int sblimit_ml = fr_ps->sblimit_ml;
- int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc_mc;
- al_table *alloc_ml = fr_ps->alloc_ml;
- int req_bits = 0;
- int maxAlloc, sel_bits, sc_bits, smp_bits;
- static int sfsPerScfsi[] = { 3,2,1,2 }; /* lookup # sfs per scfsi */
- for (sb = jsbound; sb < sblimit; sb++)
- {
- for (ch = stereo; ch < stereo+stereomc+stereoaug; ch++)
- {
- maxAlloc = (1<<(*alloc)[sb][0].bits)-1;
- sel_bits = sc_bits = smp_bits = 0;
- for (ba = 0; ba < maxAlloc-1; ba++)
- if ((-perm_smr[ch][sb] + snr[(*alloc)[sb][ba].quant + ((ba>0)?1:0)])
- >= fr_ps->mnr_min)
- break; /* we found enough bits */
- if (ba > 0)
- {
- smp_bits = 12 * ((*alloc)[sb][ba].group * (*alloc)[sb][ba].bits);
- /* scale factor bits required for subband */
- sel_bits = 2;
- sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
- req_bits += smp_bits+sel_bits+sc_bits;
- }
- }
- }
- if (n_ml_ch > 0)
- for (sb = jsbound; sb < sblimit_ml; sb++)
- {
- for (ch = 7; ch < 7+n_ml_ch; ch++)
- {
- maxAlloc = (1<<(*alloc_ml)[sb][0].bits)-1;
- sel_bits = sc_bits = smp_bits = 0;
- for (ba = 0; ba < maxAlloc-1; ba++)
- if ((-perm_smr[ch][sb] + snr[(*alloc_ml)[sb][ba].quant + ((ba>0)?1:0)])
- >= fr_ps->mnr_min)
- break; /* we found enough bits */
- if (ba > 0)
- {
- if (fr_ps->header->multiling_fs == 1)
- smp_bits = 6 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
- else
- smp_bits = 12 * ((*alloc_ml)[sb][ba].group * (*alloc_ml)[sb][ba].bits);
- /* scale factor bits required for subband */
- sel_bits = 2;
- sc_bits = 6 * sfsPerScfsi[scfsi[ch][sb]];
- req_bits += smp_bits+sel_bits+sc_bits;
- }
- }
- }
- return req_bits;
- }
- /************************************************************************
- /*
- /* I_main_bit_allocation (Layer I)
- /* II_main_bit_allocation (Layer II)
- /*
- /* PURPOSE:For joint stereo mode, determines which of the 4 joint
- /* stereo modes is needed. Then calls *_a_bit_allocation(), which
- /* allocates bits for each of the subbands until there are no more bits
- /* left, or the MNR is at the noise/no_noise threshold.
- /*
- /* SEMANTICS:
- /*
- /* For joint stereo mode, joint stereo is changed to stereo if
- /* there are enough bits to encode stereo at or better than the
- /* no-noise threshold (fr_ps->mnr_min). Otherwise, the system
- /* iteratively allocates less bits by using joint stereo until one
- /* of the following occurs:
- /* - there are no more noisy subbands (MNR >= fr_ps->mnr_min)
- /* - mode_ext has been reduced to 0, which means that all but the
- /* lowest 4 subbands have been converted from stereo to joint
- /* stereo, and no more subbands may be converted
- /*
- /* This function calls *_bits_for_nonoise() and *_a_bit_allocation().
- /*
- /************************************************************************/
- void I_main_bit_allocation(double (*perm_smr)[32], unsigned int (*bit_alloc)[32], int *adb, frame_params *fr_ps)
- {
- int noisy_sbs;
- int mode, mode_ext, lay, i;
- int rq_db, av_db = *adb;
- static int init = 0;
- if(init == 0) {
- /* rearrange snr for layer I */
- snr[2] = snr[3];
- for (i=3;i<16;i++) snr[i] = snr[i+2];
- init = 1;
- }
- 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 = I_bits_for_nonoise(perm_smr, 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 = I_bits_for_nonoise(perm_smr, fr_ps);
- } while( (rq_db > *adb) && (mode_ext > 0));
- fr_ps->header->mode_ext = mode_ext;
- } /* well we either eliminated noisy sbs or mode_ext == 0 */
- }
- noisy_sbs = I_a_bit_allocation(perm_smr, bit_alloc, adb, fr_ps);
- }
- /***************************** Layer II ********************************/
- void II_main_bit_allocation (
- double (*perm_smr)[32], /* minimum masking level *//* JMZ 08/03/1995 Multilingual */
- double (*ltmin)[32], /* minimum masking level *//* JMZ 08/03/1995 Multilingual */
- unsigned int (*scfsi)[32], /* JMZ 08/03/1995 Multilingual */
- unsigned int (*bit_alloc)[32], /* JMZ 08/03/1995 Multilingual */
- int *adb,
- frame_params *fr_ps,
- int *aiff,
- double (*sb_sample)[3][12][32], /* JMZ 08/03/1995 Multilingual */
- unsigned int (*scalar)[3][32], /* JMZ 08/03/1995 Multilingual */
- double (*max_sc)[32], /* JMZ 08/03/1995 Multilingual */
- double (*buffer)[1152], /* JMZ 08/03/1995 Multilingual */
- double (*spiki)[32], /* JMZ 08/03/1995 Multilingual */
- double (*joint_sample)[3][12][32],
- unsigned int (*j_scale)[3][32],
- int dyn_cr,
- int aug_dyn_cr,
- unsigned int (*scfsi_dyn)[SBLIMIT],
- unsigned int (*scalar_dyn)[3][SBLIMIT]
- )
- {
- int noisy_sbs, nn;
- int mode, mode_ext, lay, modemc_hlp;
- int rq_db, av_db = *adb, bits_nonoise, bits_indi;
- int a, b, i, l, m, sb, k;
- int sbbound = -30;
- float adb_help;
- double smr_pred[7][SBLIMIT];
- float preco[12][4];
- float prega[3][32];
- int bits1, bits2, ba, ba1, subgr;
- int maxAlloc;
- al_table *alloc = fr_ps->alloc;
- int stereoaug = fr_ps->stereoaug;
- static int sfsPerScfsi[] = { 3,2,1,2 }; /* lookup # sfs per scfsi */
- double sb_sample_sum[5][3][12][32];
- int subfr, ch, sum_chs;
- unsigned int scalar_sum[5][3][SBLIMIT];
- unsigned int scfsi_sum[5][SBLIMIT];
- /***************************layer II two channels *******************/
- if (*aiff == 0)
- {
- if ((mode = fr_ps->actual_mode) == MPG_MD_JOINT_STEREO)
- {
- a = 0;
- b = 1;
- 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, a, b, aiff) > *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, a, b, aiff);
- } while ((rq_db > *adb) && (mode_ext > 0));
- fr_ps->header->mode_ext = mode_ext;
- } /* well we either eliminated noisy sbs or mode_ext == 0 */
- }
- }
- /***************** layer II five channels ****************************/
- else
- {
- trans_chan (fr_ps);
- #ifdef Augmentation_7ch
- if (stereoaug == 2)
- trans_chan_aug (fr_ps);
- #endif
- if (verbosity >= 2)
- printf ("js actual_mode: %d mode: %d mode_ext: %d jsbound: %d dyn_cr: %2d aug_dyn_cr: %2dn",
- fr_ps->actual_mode, fr_ps->header->mode, fr_ps->header->mode_ext, fr_ps->jsbound, dyn_cr, aug_dyn_cr);
- for (sb = 0; sb < SBLIMIT; ++sb)
- {
- subgr = sbgrp[sb];
- maxAlloc = (1 << (*alloc)[sb][0].bits) - 1;
- for (ba = 0; ba < maxAlloc-1; ++ba)
- if ((-perm_smr[0][sb] + snr[(*alloc)[sb][ba].quant+((ba>0)?1:0)])
- >= fr_ps->mnr_min)
- break; /* we found enough bits */
- for (ba1 = 0; ba1 < maxAlloc-1; ++ba1)
- if ((-perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][0]][sb] +
- snr[(*alloc)[sb][ba1].quant+((ba1>0)?1:0)])
- >= fr_ps->mnr_min)
- break; /* we found enough bits */
- if (ba1 > ba)
- {
- perm_smr[0][sb] = perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][0]][sb];
- ba = ba1;
- }
- maxAlloc = (1<<(*alloc)[sb][0].bits) - 1;
- for (ba = 0; ba < maxAlloc-1; ++ba)
- if ((-perm_smr[1][sb] + snr[(*alloc)[sb][ba].quant+((ba>0)?1:0)])
- >= fr_ps->mnr_min)
- break; /* we found enough bits */
- for (ba1 = 0; ba1 < maxAlloc-1; ++ba1)
- if ((-perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][1]][sb] +
- snr[(*alloc)[sb][ba1].quant+((ba1>0)?1:0)])
- >= fr_ps->mnr_min)
- break; /* we found enough bits */
- if (ba1 > ba)
- {
- perm_smr[1][sb] = perm_smr[no_channel[fr_ps->header->tc_alloc[subgr]][1]][sb];
- ba = ba1;
- }
- }
- /************ 05/24/95, SR, dyn_cross talk **********************/
- if (dyn_cr != 0 || (fr_ps->header->dyn_cross_LR &&
- (fr_ps->config == 302 || fr_ps->config == 202 || fr_ps->config == 102)))
- {
- combine (fr_ps, sb_sample, sb_sample_sum);
- for (ch = 0; ch < CHANMAX2; ch++)
- {
- for (subfr = 0; subfr < 3; subfr++)
- for (sb = 0; sb < fr_ps->sblimit; sb++)
- scalar_dyn[ch][subfr][sb] = scalar[ch][subfr][sb];
- scfsi_calc_dyn (scalar_dyn, ch, fr_ps->sblimit, scfsi_dyn);
- scfsi_calc (scalar, ch, fr_ps->sblimit, scfsi);
- }
- if (fr_ps->config == 320)
- sum_chs = 4;
- else if (fr_ps->config == 302 || fr_ps->config == 310 || fr_ps->config == 220 || fr_ps->config == 202 || fr_ps->config == 102)
- sum_chs = 1;
- else
- sum_chs = 0;
- for (ch = 0; ch < sum_chs; ch++)
- {
- scf_calc (sb_sample_sum, fr_ps->sblimit, ch, scalar_sum);
- scfsi_calc_dyn (scalar_sum, ch, fr_ps->sblimit, scfsi_sum);
- }
- take_dyn_cross (dyn_cr, fr_ps, sb_sample_sum, scalar_sum, scfsi_sum, scfsi_dyn,
- sb_sample, scalar, scfsi);
- }
- /************ 05/24/95, SR, dyn_cross talk **********************/
- #ifdef Augmentation_7ch
- if (aug_dyn_cr != 0)
- {
- combine_aug (fr_ps, sb_sample, sb_sample_sum);
- for (ch = CHANMAX2; ch < CHANMAX3; ch++)
- {
- for (subfr = 0; subfr < 3; subfr++)
- for (sb = 0; sb < fr_ps->sblimit; sb++)
- scalar_dyn[ch][subfr][sb] = scalar[ch][subfr][sb];
- scfsi_calc_dyn (scalar_dyn, ch, fr_ps->sblimit, scfsi_dyn);
- scfsi_calc (scalar, ch, fr_ps->sblimit, scfsi);
- }
- scf_calc (sb_sample_sum, fr_ps->sblimit, 4, scalar_sum);
- scfsi_calc_dyn (scalar_sum, 4, fr_ps->sblimit, scfsi_sum);
- take_dyn_cross_aug (aug_dyn_cr, fr_ps, sb_sample_sum, scalar_sum,
- scfsi_sum, scfsi_dyn, sb_sample, scalar, scfsi);
- }
- #endif
- if ((mode = fr_ps->actual_mode) == MPG_MD_JOINT_STEREO)
- {
- a = 0;
- b = 1;
- fr_ps->header->mode = MPG_MD_STEREO;
- fr_ps->header->mode_ext = 0;
- fr_ps->jsbound = fr_ps->sblimit;
- adb_help = *adb;
- bits_nonoise = II_bits_for_nonoise (perm_smr, scfsi, fr_ps, a, b, aiff);
- bits_indi = II_bits_for_indi (perm_smr, scfsi, fr_ps, &a, &b, aiff);
- rq_db = ((bits_nonoise / 10) % 2) * 10 * bits_nonoise + bits_indi;
- if (verbosity >= 2)
- printf ("rq_db: %5d ( %5d + %5d ) available: %5dn",
- rq_db, bits_nonoise, bits_indi, *adb);
- if (rq_db > adb_help)
- {
- 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);
- bits_nonoise = II_bits_for_nonoise (perm_smr, scfsi, fr_ps, a, b, aiff);
- bits_indi = II_bits_for_indi (perm_smr, scfsi, fr_ps, &a, &b, aiff);
- rq_db = ((bits_nonoise / 10) % 2) * 10 * bits_nonoise + bits_indi;
- if (verbosity >= 2)
- printf ("rq_db: %5d ( %5d + %5d ) available: %5dn",
- rq_db, bits_nonoise, bits_indi, *adb);
- } while ((rq_db >*adb) && (mode_ext > 0));
- fr_ps->header->mode_ext = mode_ext;
- } /* well we either eliminated noisy sbs or mode_ext == 0 */
- }
- } /* end of else (five-channel) */
- noisy_sbs = II_a_bit_allocation (perm_smr, scfsi, bit_alloc, adb, fr_ps, aiff);
- }
- /************************************************************************
- /*
- /* I_a_bit_allocation (Layer I)
- /* II_a_bit_allocation (Layer II)
- /*
- /* PURPOSE:Adds bits to the subbands with the lowest mask-to-noise
- /* ratios, until the maximum number of bits for the subband has
- /* been allocated.
- /*
- /* SEMANTICS:
- /* 1. Find the subband and channel with the smallest MNR (#min_sb#,
- /* and #min_ch#)
- /* 2. Calculate the increase in bits needed if we increase the bit
- /* allocation to the next higher level
- /* 3. If there are enough bits available for increasing the resolution
- /* in #min_sb#, #min_ch#, and the subband has not yet reached its
- /* maximum allocation, update the bit allocation, MNR, and bits
- /* available accordingly
- /* 4. Repeat until there are no more bits left, or no more available
- /* subbands. (A subband is still available until the maximum
- /* number of bits for the subband has been allocated, or there
- /* aren't enough bits to go to the next higher resolution in the
- /* subband.)
- /*
- /************************************************************************/
- int I_a_bit_allocation(double (*perm_smr)[32], unsigned int (*bit_alloc)[32], int *adb, frame_params *fr_ps) /* return noisy sbs */
- /*far*/
- {
- int i, k, smpl_bits, scale_bits, min_sb, min_ch, oth_ch;
- int bspl, bscf, ad, noisy_sbs, done = 0;
- double mnr[2][SBLIMIT], small;
- 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 bbal, banc, berr;
- if (!init) {
- banc = 32; /* before: berr = 0; 92-08-11 shn */
- init = 1;
- if (fr_ps->header->error_protection) berr = 16; else berr= 0; /* 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 {
- /* locate the subband with minimum SMR */
- small = mnr[0][0]+1; min_sb = -1; min_ch = -1;
- for (i=0;i<SBLIMIT;i++) for (k=0;k<stereo;k++)
- /* go on only if there are bits left */
- if (used[k][i] != 2 && small > mnr[k][i]) {
- small = mnr[k][i];
- min_sb = i; min_ch = k;
- }
- if(min_sb > -1) { /* there was something to find */
- /* first step of bit allocation is biggest */
- if (used[min_ch][min_sb]) { smpl_bits = 12; scale_bits = 0; }
- else { smpl_bits = 24; scale_bits = 6; }
- if(min_sb >= jsbound) scale_bits *= stereo;
- /* check to see enough bits were available for */
- /* increasing resolution in the minimum band */
- if (ad > bspl + bscf + scale_bits + smpl_bits) {
- bspl += smpl_bits; /* bit for subband sample */
- bscf += scale_bits; /* bit for scale factor */
- bit_alloc[min_ch][min_sb]++;
- used[min_ch][min_sb] = 1; /* subband has bits */
- mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb]
- + snr[bit_alloc[min_ch][min_sb]];
- /* Check if subband has been fully allocated max bits */
- if (bit_alloc[min_ch][min_sb] == 14 ) used[min_ch][min_sb] = 2;
- }
- else /* no room to improve this band */
- used[min_ch][min_sb] = 2; /* for allocation anymore */
- if(stereo == 2 && min_sb >= jsbound) {
- oth_ch = 1-min_ch; /* joint-st : fix other 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); /* i.e. still some sub-bands to find */
- /* Calculate the number of bits left, add on to pointed var */
- ad -= bspl+bscf;
- *adb = ad;
- /* see how many channels are noisy */
- noisy_sbs = 0; small = mnr[0][0];
- for(k=0; k<stereo; ++k) {
- for(i = 0; i< SBLIMIT; ++i) {
- if(mnr[k][i] < fr_ps->mnr_min) ++noisy_sbs;
- if(small > mnr[k][i]) small = mnr[k][i];
- }
- }
- return noisy_sbs;
- }
- /***************************** Layer II ********************************/
- int II_a_bit_allocation (double (*perm_smr)[32], unsigned int (*scfsi)[32],
- unsigned int (*bit_alloc)[32], int *adb,
- frame_params *fr_ps, int *aiff)
- {
- int n_ml_ch = fr_ps->header->multiling_ch; /*JMZ 08/03/95 Multilingual */
- int i, min_ch, min_sb, oth_ch, k, increment, scale, seli, ba, j, l;
- int increment1, scale1, seli1;
- int adb_hlp, adb_hlp1, adb_hlp2;
- int bspl, bscf, bsel, ad, noisy_sbs;
- int bspl_mpg1, bscf_mpg1, bsel_mpg1;
- double mnr[14][SBLIMIT], small; /* JMZ 08/03/1995 Multilingual */
- char used[14][SBLIMIT]; /* JMZ 08/03/1995 Multilingual */
- layer *info = fr_ps->header;
- int center = info->center;
- int surround = info->surround;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int stereoaug = fr_ps->stereoaug;
- int sblimit = fr_ps->sblimit;
- int sblimit_mc = fr_ps->sblimit_mc;
- int sblimit_ml = fr_ps->sblimit_ml;
- int jsbound = fr_ps->jsbound;
- al_table *alloc = fr_ps->alloc;
- al_table *alloc_mc = fr_ps->alloc_mc;
- al_table *alloc_ml = fr_ps->alloc_ml;
- double dynsmr = 0.0; /* border of SMR for dynamic datarate */
- static char init= 0;
- static int banc, berr;
- int bbal, bancmc, bancext, bbal_mpg1;
- int ll, pred, pci, adb_mpg1, sbgr, sb, tca_bits, dynx_bits, aug_tca_bits;
- static int sfsPerScfsi[] = { 3,2,1,2 }; /* lookup # sfs per scfsi */
- if (!init)
- {
- init = 1;
- banc = 32; /* banc: bits for header */;
- /* 960627 FdB reserve extra bits dependent wrt bugs */
- banc += 200;
- if (fr_ps->header->error_protection)
- berr = 16;
- else
- berr=0; /* added 92-08-11 shn */
- }
- pred = 0;
- bancmc = 0;
- bbal = 0;
- bbal_mpg1 = 0;
- bancext = 0;
- adb_mpg1 = bitrate[fr_ps->header->lay-1][fr_ps->header->bitrate_index] * 24;
- /* 960814 FdB next line added for debugging */
- /* adb_mpg1 -= banc; */
- if (*aiff == 1)
- {
- if (fr_ps->actual_mode == MPG_MD_JOINT_STEREO)
- {
- for (i = 0; i < jsbound; ++i)
- bbal += (stereo) * (*alloc)[i][0].bits;
- for (i = jsbound; i < sblimit; ++i)
- bbal += (stereo-1) * (*alloc)[i][0].bits;
- for (i = 0; i < sblimit_mc; ++i)
- bbal += (stereomc+stereoaug) * (*alloc_mc)[i][0].bits;
- for (i = 0; i < sblimit_ml; ++i)
- bbal += (n_ml_ch) * (*alloc_ml)[i][0].bits;
- }
- else
- {
- for (i = 0; i < sblimit; ++i)
- bbal += (stereo) * (*alloc)[i][0].bits;
- for (i = 0; i < sblimit_mc; ++i)
- bbal += (stereomc+stereoaug) * (*alloc_mc)[i][0].bits;
- for (i = 0; i < sblimit_ml; ++i)
- bbal += (n_ml_ch) * (*alloc_ml)[i][0].bits;
- }
- if (fr_ps->header->center == 3) bbal -= 41;
- }
- else
- {
- for (i = 0; i < jsbound; ++i)
- bbal += stereo * (*alloc)[i][0].bits;
- for (i = jsbound; i < sblimit; ++i)
- bbal += (*alloc)[i][0].bits;
- }
- if (fr_ps->header->dyn_cross_on == 1)
- for (i = 0; i < 12; i++)
- {
- bbal -= dyn_bbal (fr_ps->config, fr_ps->header->center, fr_ps->header->dyn_cross[i], i);
- if (fr_ps->header->surround == 3)
- bbal -= dyn_bbal_2ndst (fr_ps->header->dyn_second_stereo[i], i);
- }
- if (fr_ps->header->ext_bit_stream_present == 0)
- bancmc += 35; /* mc_header + crc + tc_sbgr_select+ dyn_cross_on +
- mc_prediction_on 01/05/94, SR new! 05/04/94, SR*/
- else
- {
- bancmc += 43;
- bancext = 40;
- }
- /* 960627 FdB TCA bits dependent on configuration */
- if (fr_ps->config == 320)
- tca_bits = 3;
- else if (fr_ps->config == 310)
- tca_bits = 3;
- else if (fr_ps->config == 220)
- tca_bits = 2;
- else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)
- tca_bits = 2;
- else
- tca_bits = 0;
- if (fr_ps->header->tc_sbgr_select == 0)
- bancmc += 12 * tca_bits;
- else
- bancmc += tca_bits;
- if (fr_ps->header->dyn_cross_on == 1)
- {
- /* 960627 FdB DynX dependent on configuration */
- if (fr_ps->config == 320)
- dynx_bits = 4;
- else if (fr_ps->config == 310 || fr_ps->config == 220)
- dynx_bits = 3;
- else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)
- dynx_bits = 1;
- else
- dynx_bits = 0;
- bancmc += 12 * dynx_bits + 1;
- if (fr_ps->header->surround == 3)
- bancmc += 12; /* now with dyn_second_stereo, 17/02/95, SR*/
- }
- if (fr_ps->header->mc_prediction_on == 1)
- {
- /* for (i = 0; i < 8; i++)
- {
- bancmc += 1;
- if (fr_ps->header->mc_pred[i] == 1)
- bancmc += n_pred_coef[fr_ps->header->dyn_cross[i]] * 2;
- }
- */
- /* mc_pred, predsi, delay_comp, pred_coef */
- /* bancmc += 8 * (3 + 6 * (3 + 3 * 8)); */
- for (i = 0; i < 8; i++)
- {
- bancmc += 3;
- bancmc += n_pred_coef[fr_ps->header->dyn_cross[i]] * 27;
- }
- }
- #ifdef Augmentation_7ch
- if (stereoaug == 2)
- {
- if (fr_ps->header->aug_mtx_proc == 0)
- aug_tca_bits = 3;
- else if (fr_ps->header->aug_mtx_proc == 1)
- aug_tca_bits = 2;
- else
- aug_tca_bits = 0;
- bancmc += 12 * aug_tca_bits;
- if (fr_ps->header->aug_dyn_cross_on == 1)
- {
- bancmc += 12 * 5;
- for (i = 0; i < 12; i++)
- bbal -= dyn_bbal_aug (fr_ps->header->aug_dyn_cross[i], i);
- }
- }
- #endif
- for (i = 0; i < SBLIMIT; i++)
- /* for (k = 0; k < (stereo+stereomc+2); k++) 960814 FdB bug for some configurations */
- for (k = 0; k < 12; k++)
- {
- mnr[k][i] = snr[0] - perm_smr[k][i];
- /* mask-to-noise-level = signal-to-noise-level - minimum-masking- */
- /* threshold */
- bit_alloc[k][i] = 0;
- used[k][i] = 0;
- }
- for (i = 0; i < sblimit_ml; i++)
- for (k = 7; k < 7 + n_ml_ch; k++)
- {
- mnr[k][i]=snr[0]-perm_smr[k][i];
- /* mask-to-noise-level = signal-to-noise-level - minimum-masking-*/
- /* threshold*/
- bit_alloc[k][i] = 0;
- used[k][i] = 0;
- }
- /* dyamic crosstalk, lock sbgr which are use for dyncr. */
- if (fr_ps->header->dyn_cross_on == 1)
- {
- for (sbgr = 0; sbgr < SBGRS; sbgr++)
- {
- for (sb = ((sbgr == 0)?0:sb_groups[sbgr-1]+1); sb <= sb_groups[sbgr]; sb++)
- {
- /* 960627 FdB DynX dependent on configuration */
- if (fr_ps->config == 320)
- {
- /* 3/2 */
- switch(fr_ps->header->dyn_cross[sbgr])
- {
- case 0:
- break;
- case 1: case 8: case 10:
- used[T4[sbgr]][sb] = 2;
- break;
- case 2: case 9:
- used[T3[sbgr]][sb] = 2;
- break;
- case 3:
- used[T2[sbgr]][sb] = 2;
- break;
- case 4: case 11: case 12: case 14:
- used[T3[sbgr]][sb] = 2;
- used[T4[sbgr]][sb] = 2;
- break;
- case 5: case 13:
- used[T2[sbgr]][sb] = 2;
- used[T4[sbgr]][sb] = 2;
- break;
- case 6:
- used[T2[sbgr]][sb] = 2;
- used[T3[sbgr]][sb] = 2;
- break;
- case 7:
- used[T2[sbgr]][sb] = 2;
- used[T4[sbgr]][sb] = 2;
- used[T3[sbgr]][sb] = 2;
- break;
- }
- }
- else if (fr_ps->config == 310 || fr_ps->config == 220)
- {
- /* 3/1 and 2/2 */
- switch(fr_ps->header->dyn_cross[sbgr])
- {
- case 0:
- break;
- case 1: case 4:
- used[T3[sbgr]][sb] = 2;
- break;
- case 2:
- used[T2[sbgr]][sb] = 2;
- break;
- case 3:
- used[T2[sbgr]][sb] = 2;
- used[T3[sbgr]][sb] = 2;
- break;
- }
- }
- else if (fr_ps->config == 300 || fr_ps->config == 302 || fr_ps->config == 210)
- {
- /* 3/0 (+2/0) and 2/1 */
- switch(fr_ps->header->dyn_cross[sbgr])
- {
- case 0:
- break;
- case 1:
- used[T2[sbgr]][sb] = 2;
- break;
- }
- if (fr_ps->header->dyn_second_stereo[sbgr])
- used[T4[sbgr]][sb] = 2;
- }
- else if (fr_ps->config == 202)
- {
- if (fr_ps->header->dyn_second_stereo[sbgr])
- used[T3[sbgr]][sb] = 2;
- }
- else if (fr_ps->config == 102)
- {
- if (fr_ps->header->dyn_second_stereo[sbgr])
- used[T2[sbgr]][sb] = 2;
- }
- } /* for(sb.. */
- } /* for(sbgr.. */
- } /* if(fr_ps.. */
- #ifdef Augmentation_7ch
- if (fr_ps->header->aug_dyn_cross_on == 1)
- {
- for (sbgr = 0; sbgr < SBGRS; sbgr++)
- {
- for (sb = ((sbgr == 0)?0:sb_groups[sbgr-1]+1); sb <= sb_groups[sbgr]; sb++)
- {
- /* 5/2 */
- switch (fr_ps->header->aug_dyn_cross[sbgr])
- {
- case 0:
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- used[T6[sbgr]][sb] = 2;
- break;
- case 5:
- case 10:
- case 14:
- used[T5[sbgr]][sb] = 2;
- break;
- case 6:
- case 7:
- case 8:
- case 9:
- case 11:
- case 12:
- case 13:
- case 15:
- case 16:
- case 17:
- case 18:
- used[T5[sbgr]][sb] = 2;
- used[T6[sbgr]][sb] = 2;
- break;
- }
- } /* for(sb.. */
- } /* for(sbgr.. */
- } /* if(fr_ps.. */
- #endif
- /*
- if (fr_ps->header->mc_prediction_on == 1)
- {
- for (i = 0; i < 8; i++)
- {
- if (fr_ps->header->mc_pred[i] == 1)
- {
- for (j = 0; j < n_pred_coef[fr_ps->header->dyn_cross[i]]; j++)
- {
- if (fr_ps->header->predsi[i][j] != 0)
- {
- pred += 3;
- for (pci = 0; pci < fr_ps->header->predsi[i][j]; pci++)
- pred += 8;
- }
- }
- }
- }
- }
- */
- adb_hlp = *adb;
- if (*aiff != 1)
- {
- *adb -= bbal + berr + banc;
- }
- else
- {
- if (fr_ps->header->ext_bit_stream_present == 0)
- *adb -= bbal + berr + banc + bancmc + pred +
- (fr_ps->header->n_ad_bytes * 8);
- else
- {
- *adb -= bbal + berr + banc + bancmc + pred +
- bancext + (fr_ps->header->n_ad_bytes * 8);
- for(i = 0; i < jsbound; ++i)
- bbal_mpg1 += stereo * (*alloc)[i][0].bits;
- for(i = jsbound; i < sblimit; ++i)
- bbal_mpg1 += (*alloc)[i][0].bits;
- adb_mpg1 -= bbal_mpg1 + berr + banc + bancmc +
- (fr_ps->header->n_ad_bytes * 8);
- }
- }
- ad = *adb;
- bspl = bscf = bsel = bspl_mpg1 = bscf_mpg1 = bsel_mpg1 = 0;
- do
- {
- /* locate the subband with minimum SMR */
- small = 999999.0;
- min_sb = -1;
- min_ch = -1;
- for (i = 0; i < SBLIMIT; i++) /* searching for the sb min SMR */
- {
- l = sbgrp[i];
- for (j = 0; j < (stereo+stereomc+stereoaug); ++j)
- if ((j < stereo && i < sblimit) ||
- (j >= stereo && i < sblimit_mc))
- {
- k = transmission_channel (fr_ps, l, j);
- if ((i >= 12) && (fr_ps->header->center == 3) && (k == 2))
- used[k][i] = 2;
- if ((used[k][i] != 2) && (small > mnr[k][i]))
- {
- small = mnr[k][i];
- min_sb = i;
- min_ch = k;
- ll = l; /*sb-group*/
- }
- }
- }
- /******************************************************************/
- /* Multilingual JMZ 08/03/1995 */
- if (n_ml_ch > 0)
- {
- for (i = 0; i < sblimit_ml; i++)
- for (j = 7; j < (n_ml_ch+7); ++j)
- {
- k = j;
- if ((used[k][i] != 2) && (small > mnr[k][i]))
- {
- small = mnr[k][i];
- min_sb = i;
- min_ch = k;
- ll = l;
- }
- }
- }
- /* Multilingual JMZ 08/03/1995 */
- /******************************************************************/
- if (min_sb > -1)
- { /* there was something to find */
- /* find increase in bit allocation in subband [min] */
- if (min_ch < stereo)
- {
- increment = 12 * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]+1].group *
- (*alloc)[min_sb][bit_alloc[min_ch][min_sb]+1].bits);
- /* how many bits are needed */
- if (used[min_ch][min_sb])
- increment -= 12 * ((*alloc)[min_sb][bit_alloc[min_ch][min_sb]].group *
- (*alloc)[min_sb][bit_alloc[min_ch][min_sb]].bits);
- }
- else if (min_ch < 7 || n_ml_ch == 0) /* Multichannel */
- {
- increment = 12 * ((*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb]+1].group *
- (*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb]+1].bits);
- /* how many bits are needed */
- if (used[min_ch][min_sb])
- increment -= 12 * ((*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb]].group *
- (*alloc_mc)[min_sb][bit_alloc[min_ch][min_sb]].bits);
- }
- else /* MultiLingual 7/8/95 WtK */
- {
- increment = ((*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb]+1].group *
- (*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb]+1].bits);
- if (used[min_ch][min_sb])
- increment -= ((*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb]].group *
- (*alloc_ml)[min_sb][bit_alloc[min_ch][min_sb]].bits);
- if (fr_ps->header->multiling_fs == 1) increment *= 6;
- else increment *= 12;
- }
- /* scale factor bits required for subband [min] */
- /* above js bound, need both chans */
- if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) &&
- ((min_ch == 0) || (min_ch == 1)))
- 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 ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound) &&
- ((min_ch == 0) || (min_ch == 1)))
- {
- /* each new js sb has L+R scfsis */
- seli += 2;
- scale += 6 * sfsPerScfsi[scfsi[oth_ch][min_sb]];
- }
- if (fr_ps->header->dyn_cross_on == 1)
- {
- dyn_bal (scfsi, ll, fr_ps, min_ch, min_sb, &seli, &scale);
- /* 960819 FdB joint stereo in combination with DynX added */
- if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound) &&
- (stereo == 2) && ((min_ch == 0) || (min_ch == 1)))
- dyn_bal (scfsi, ll, fr_ps, oth_ch, min_sb, &seli, &scale);
- }
- #ifdef Augmentation_7ch
- if (fr_ps->header->aug_dyn_cross_on == 1)
- {
- dyn_bal_aug (scfsi, ll, fr_ps, min_ch, min_sb, &seli, &scale);
- /* 960819 FdB joint stereo in combination with DynX added */
- if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound) &&
- (stereo == 2) && ((min_ch == 0) || (min_ch == 1)))
- dyn_bal_aug (scfsi, ll, fr_ps, oth_ch, min_sb, &seli, &scale);
- }
- #endif
- }
- /* check to see enough bits were available for */
- /* increasing resolution in the minimum band */
- if (fr_ps->header->ext_bit_stream_present == 1)
- {
- if ((min_ch == 0) || (min_ch == 1 && stereo == 2))
- {
- if (adb_mpg1 > bspl_mpg1 + bscf_mpg1 + bsel_mpg1 + seli + scale + increment)
- {
- bspl_mpg1 += increment; /* bits for subband sample */
- bscf_mpg1 += scale; /* bits for scale factor */
- bsel_mpg1 += seli; /* bits for scfsi code */
- }
- else
- used[min_ch][min_sb] = 2; /* can't increase this alloc */
- }
- }
- if ((ad > bspl + bscf + bsel + seli + scale + increment) && (used[min_ch][min_sb] != 2))
- {
- 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 */
- if (min_ch < stereo)
- {
- 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 if (min_ch < 7 || n_ml_ch == 0) /* Multichannel */
- {
- mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb] +
- snr[(*alloc_mc)[min_sb][ba].quant+1];
- /* Check if subband has been fully allocated max bits */
- if (ba >= (1<<(*alloc_mc)[min_sb][0].bits) - 1)
- used[min_ch][min_sb] = 2;
- }
- else /* MultiLingual 7/8/95 WtK */
- {
- mnr[min_ch][min_sb] = -perm_smr[min_ch][min_sb] +
- snr[(*alloc_ml)[min_sb][ba].quant+1];
- if (ba >= (1<<(*alloc_ml)[min_sb][0].bits) - 1)
- used[min_ch][min_sb] = 2;
- }
- }
- else
- used[min_ch][min_sb] = 2; /* can't increase this alloc */
- if (verbosity >= 3)
- {
- if (fr_ps->header->ext_bit_stream_present == 1)
- printf("ch: %02d sb: %02d used: %d adb: %05d used: %05d ad: %05d used: %05dn",
- min_ch, min_sb, used[min_ch][min_sb],
- adb_mpg1, bspl_mpg1 + bscf_mpg1 + bsel_mpg1,
- ad, bspl + bscf + bsel);
- else
- printf("ch: %02d sb: %02d used: %d ad: %05d used: %05dn",
- min_ch, min_sb, used[min_ch][min_sb],
- ad, bspl + bscf + bsel);
- }
- if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound) && (stereo == 2) &&
- ((min_ch == 0) || (min_ch == 1)))
- {
- /* 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];
- }
- if (fr_ps->header->dyn_cross_on == 1)
- {
- choose_dyn (fr_ps, min_ch, min_sb, ll, bit_alloc);
- /* 960819 FdB joint stereo in combination with DynX added */
- if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound) &&
- (stereo == 2) && ((min_ch == 0) || (min_ch == 1)))
- choose_dyn (fr_ps, oth_ch, min_sb, ll, bit_alloc);
- }
- #ifdef Augmentation_7ch
- if (fr_ps->header->aug_dyn_cross_on == 1)
- {
- choose_dyn_aug (fr_ps, min_ch, min_sb, ll, bit_alloc);
- /* 960819 FdB joint stereo in combination with DynX added */
- if ((fr_ps->actual_mode == MPG_MD_JOINT_STEREO) && (min_sb >= jsbound) &&
- (stereo == 2) && ((min_ch == 0) || (min_ch == 1)))
- choose_dyn_aug (fr_ps, oth_ch, min_sb, ll, bit_alloc);
- }
- #endif
- } /* end of if-loop if min_sb >-1 */
- } while (min_sb > -1); /* until could find no channel */
- /* Calculate the number of bits left */
- ad -= bspl+bscf+bsel;
- *adb = ad;
- for (k = 0; k < stereo; k++)
- for (i = sblimit; i < SBLIMIT; i++)
- bit_alloc[k][i] = 0;
- if (n_ml_ch > 0)
- {
- for (k = stereo; k < 7; k++)
- for (i = sblimit_mc; i < SBLIMIT; i++)
- bit_alloc[k][i] = 0;
- for (i = sblimit_ml; i < SBLIMIT; i++)
- for (k = 7; k < 14; k++)
- bit_alloc[k][i] = 0;
- }
- else
- for (k = stereo; k < 12; k++)
- for (i = sblimit_mc; i < SBLIMIT; i++)
- bit_alloc[k][i] = 0;
- /* not used !?! perhaps later!! 8/21/93, SR */
- noisy_sbs = 0;
- small = mnr[0][0]; /* calc worst noise in case */
- for (k = 0; k < (stereo+stereomc+stereoaug); k++)
- {
- for (i = 0; i < sblimit; i++)
- {
- if (small > mnr[k][i])
- small = mnr[k][i];
- if (mnr[k][i] < fr_ps->mnr_min)
- noisy_sbs++; /* noise is not masked */
- }
- }
- if (n_ml_ch > 0)
- {
- for (k = 7; k < (7+n_ml_ch); k++)
- {
- for (i = 0; i < sblimit_ml; i++)
- {
- if (small > mnr[k][i])
- small = mnr[k][i];
- if (mnr[k][i] < fr_ps->mnr_min)
- noisy_sbs++; /* noise is not masked */
- }
- }
- }
- return noisy_sbs;
- }
- /************************************************************************
- /*
- /* I_subband_quantization (Layer I)
- /* II_subband_quantization (Layer II)
- /* II_subband_quantisationmc (MPEG2) SR
- /* PURPOSE:Quantizes subband samples to appropriate number of bits
- /*
- /* SEMANTICS: Subband samples are divided by their scalefactors, which
- /* makes the quantization more efficient. The scaled samples are
- /* quantized by the function a*x+b, where a and b are functions of
- /* the number of quantization levels. The result is then truncated
- /* to the appropriate number of bits and the MSB is inverted.
- /*
- /* Note that for fractional 2's complement, inverting the MSB for a
- /* negative number x is equivalent to adding 1 to it.
- /*
- /************************************************************************/
- static double I_a[15] = {
- 0.750000000, 0.875000000, 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 I_b[15] = {
- -0.250000000, -0.125000000, -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 };
- static double II_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 II_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 (unsigned int (*scalar)[3][32],
- double (*sb_samples)[3][12][32],
- unsigned int (*j_scale)[3][32],
- double (*j_samps)[3][12][32],
- unsigned int (*bit_alloc)[32],
- unsigned int (*sbband)[3][12][32],
- 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;
- for (j=0;j<12;j++) for (i=0;i<SBLIMIT;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- if (bit_alloc[k][i]) {
- /* for joint stereo mode, have to construct a single subband stream
- for the js channels. At present, we calculate a set of mono
- subband samples and pass them through the scaling system to
- generate an alternate normalised sample stream.
- Could normalise both streams (divide by their scfs), then average
- them. In bad conditions, this could give rise to spurious
- cancellations. Instead, we could just select the sb stream from
- the larger channel (higher scf), in which case _that_ channel
- would be 'properly' reconstructed, and the mate would just be a
- scaled version. Spec recommends averaging the two (unnormalised)
- subband channels, then normalising this new signal without
- actually sending this scale factor... This means looking ahead.
- */
- if(stereo == 2 && i>=jsbound)
- /* use the joint data passed in */
- d = j_samps[0][0][j][i] / multiple[j_scale[0][0][i]];
- else
- d = sb_samples[k][0][j][i] / multiple[scalar[k][0][i]];
- /* scale and quantize floating point sample */
- n = bit_alloc[k][i];
- d = d * I_a[n-1] + I_b[n-1];
- /* extract MSB N-1 bits from the floating point sample */
- if (d >= 0) sig = 1;
- else { sig = 0; d += 1.0; }
- sbband[k][0][j][i] = (unsigned int) (d * (double) (1L<<n));
- /* tag the inverted sign bit to sbband at position N */
- if (sig) sbband[k][0][j][i] |= 1<<n;
- }
- }
- /***************************** Layer II ********************************/
- void II_subband_quantization (unsigned int (*scalar)[3][32], double (*sb_samples)[3][12][32],
- unsigned int (*j_scale)[3][32], double (*j_samps)[3][12][32],
- unsigned int (*bit_alloc)[32], unsigned int (*sbband)[3][12][32],
- frame_params *fr_ps)
- {
- int i, j, k, s, n, qnt, sig, l, m;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int sblimit = fr_ps->sblimit;
- int jsbound = fr_ps->jsbound;
- unsigned int stps;
- double d;
- al_table *alloc = fr_ps->alloc;
- for (s=0;s<3;s++)
- for (j=0;j<12;j++)
- for (i=0;i<sblimit;i++)
- for (k=0;k<((i<jsbound)?stereo:1);k++)
- {
- if (bit_alloc[k][i])
- {
- /* scale and quantize floating point sample */
- if(stereo == 2 && i>=jsbound) /* use j-stereo samples */
- d = j_samps[0][s][j][i] / multiple[j_scale[0][s][i]];
- else
- d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
- if (mod(d) >= 1.0) /* > changed to >=, 1992-11-06 shn */
- {
- printf("In compatible part, not scaled properly, %d %d %d %dn",k,s,j,i);
- printf("Value %1.10fn",sb_samples[k][s][j][i]);
- printf("Channel %dn", k);
- }
- qnt = (*alloc)[i][bit_alloc[k][i]].quant;
- d = d * II_a[qnt] + II_b[qnt];
- /* extract MSB N-1 bits from the floating point sample */
- 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));
- /* tag the inverted sign bit to sbband at position N */
- /* The bit inversion is a must for grouping with 3,5,9 steps
- so it is done for all subbands */
- 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<12;i++)
- for (m=0;m<stereo;m++)
- sbband[m][s][i][j] = 0;
- }
- void II_subband_quantization_mc (unsigned int (*scalar)[3][32], double (*sb_samples)[3][12][32],
- unsigned int (*j_scale)[3][32], double (*j_samps)[3][12][32],
- unsigned int (*bit_alloc)[32], unsigned int (*sbband)[3][12][32],
- frame_params *fr_ps)
- {
- int i, j, k, s, n, qnt, sig, m, l, ll;
- layer *info = fr_ps->header;
- int center = info->center;
- int surround = info->surround;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int sblimit = fr_ps->sblimit_mc;
- unsigned int stps;
- double d;
- al_table *alloc = fr_ps->alloc_mc;
- for (s = 0; s < 3; s++)
- for (j = 0; j < 12; j++)
- for (i = 0; i < sblimit; i++)
- {
- ll = sbgrp[i];
- for (m = stereo; m < stereo + stereomc; ++m)
- {
- k = transmission_channel (fr_ps, ll, m);
- if (fr_ps->header->dyn_cross_on == 1)
- {
- /* 960627 FdB DynX dependent on configuration */
- if (dyn_ch (fr_ps, ll, m) == 0)
- bit_alloc[k][i] = 0;
- }
- if (bit_alloc[k][i])
- {
- d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
- if (mod (d) >= 1.0) /* > changed to >=, 1992-11-06 shn */
- {
- printf ("In MC, not scaled properly, %d %d %d %dn", k, s, j, i);
- printf ("Value %1.10fn", sb_samples[k][s][j][i]);
- }
- qnt = (*alloc)[i][bit_alloc[k][i]].quant;
- d = d * II_a[qnt] + II_b[qnt];
- /* extract MSB N-1 bits from the floating point sample */
- 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));
- /* tag the inverted sign bit to sbband at position N */
- /* The bit inversion is a must for grouping with 3,5,9 steps
- so it is done for all subbands */
- if (sig)
- sbband[k][s][j][i] |= 1 << n;
- }
- else
- sbband[k][s][j][i] = 0;
- }
- }
- for (s = 0; s < 3; s++)
- for (j = sblimit; j < SBLIMIT; j++)
- for (i = 0; i < 12; i++)
- for (k = stereo; k < 7; k++)
- sbband[k][s][i][j] = 0;
- }
- #ifdef Augmentation_7ch
- void II_subband_quantization_aug (unsigned int (*scalar)[3][32], double (*sb_samples)[3][12][32],
- unsigned int (*j_scale)[3][32], double (*j_samps)[3][12][32],
- unsigned int (*bit_alloc)[32], unsigned int (*sbband)[3][12][32],
- frame_params *fr_ps)
- {
- int i, j, k, s, n, qnt, sig, m, l, ll;
- layer *info = fr_ps->header;
- int center = info->center;
- int surround = info->surround;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int sblimit = fr_ps->sblimit_mc;
- unsigned int stps;
- double d;
- al_table *alloc = fr_ps->alloc_mc;
- for (s = 0; s < 3; s++)
- for (j = 0; j < 12; j++)
- for (i = 0; i < sblimit; i++)
- {
- ll = sbgrp[i];
- for (m = 5; m < 7; ++m)
- {
- k = transmission_channel (fr_ps, ll, m);
- if (fr_ps->header->aug_dyn_cross_on == 1)
- {
- /* 960627 FdB DynX dependent on configuration */
- if (dyn_ch (fr_ps, ll, m) == 0)
- bit_alloc[k][i] = 0;
- }
- if (bit_alloc[k][i])
- {
- d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
- if (mod (d) >= 1.0) /* > changed to >=, 1992-11-06 shn */
- {
- printf ("In MC, not scaled properly, %d %d %d %dn", k, s, j, i);
- printf ("Value %1.10fn", sb_samples[k][s][j][i]);
- }
- qnt = (*alloc)[i][bit_alloc[k][i]].quant;
- d = d * II_a[qnt] + II_b[qnt];
- /* extract MSB N-1 bits from the floating point sample */
- 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));
- /* tag the inverted sign bit to sbband at position N */
- /* The bit inversion is a must for grouping with 3,5,9 steps
- so it is done for all subbands */
- if (sig)
- sbband[k][s][j][i] |= 1 << n;
- }
- else
- sbband[k][s][j][i] = 0;
- }
- }
- for (s = 0; s < 3; s++)
- for (j = sblimit; j < SBLIMIT; j++)
- for (i = 0; i < 12; i++)
- for (k = 7; k < 12; k++)
- sbband[k][s][i][j] = 0;
- }
- #endif
- void II_subband_quantization_ml (unsigned int (*scalar)[3][32], double (*sb_samples)[3][12][32],
- unsigned int (*j_scale)[3][32], double (*j_samps)[3][12][32],
- unsigned int (*bit_alloc)[32], unsigned int (*sbband)[3][12][32],
- frame_params *fr_ps)
- {
- int i, j, k, s, n, qnt, sig, m, l, ll;
- int n_ml_ch = fr_ps->header->multiling_ch;
- int sblimit = fr_ps->sblimit_ml;
- int ml_lsf = fr_ps->header->multiling_fs;
- unsigned int stps;
- double d;
- al_table *alloc = fr_ps->alloc_ml;
- for (s=0;s<3;s++)
- for (j=0;j<((ml_lsf)?6:12);j++) /* WtK half sample frequency 7/8/95 */
- for (i=0;i<sblimit;i++)
- {
- for (m = 7; m < 7+n_ml_ch; ++m)
- {
- k = m;
- if (bit_alloc[k][i])
- {
- d = sb_samples[k][s][j][i] / multiple[scalar[k][s][i]];
- if (mod(d) >= 1.0) /* > changed to >=, 1992-11-06 shn */
- { printf("In ML, not scaled properly, %d %d %d %dn",k,s,j,i);
- printf("Value %1.10fn",sb_samples[k][s][j][i]);
- }
- qnt = (*alloc)[i][bit_alloc[k][i]].quant;
- d = d * II_a[qnt] + II_b[qnt];
- /* extract MSB N-1 bits from the floating point sample */
- if (d >= 0) sig = 1;
- else { sig = 0; d += 1.0; }
- n = 0;
- stps = (*alloc)[i][bit_alloc[k][i]].steps;
- while ((1L<<n) < stps) n++;
- n--;
- sbband[k][s][j][i] = (unsigned int) (d * (double) (1L<<n));
- /* tag the inverted sign bit to sbband at position N */
- /* The bit inversion is a must for grouping with 3,5,9 steps
- so it is done for all subbands */
- if (sig) sbband[k][s][j][i] |= 1<<n;
- }
- else
- sbband[k][s][j][i] = 0;
- }
- }
- for (s=0;s<3;s++)
- for (j=sblimit;j<SBLIMIT;j++)
- for (i=0;i<((ml_lsf)?6:12);i++)
- for (k = 7; k < 7+n_ml_ch; k++)
- sbband[k][s][i][j] = 0;
- }
- /************************************************************************
- /*
- /* I_encode_bit_alloc (Layer I)
- /* II_encode_bit_alloc (Layer II)
- /* II_encode_bit_alloc_mc (Layer II multichannel)
- #ifdef Augmentation_7ch
- /* II_encode_bit_alloc_aug (Layer II multichannel 7ch-augmentation)
- #endif
- /*
- /* PURPOSE:Writes bit allocation information onto bitstream
- /*
- /* Layer I uses 4 bits/subband for bit allocation information,
- /* and Layer II uses 4,3,2, or 0 bits depending on the
- /* quantization table used.
- /*
- /************************************************************************/
- void I_encode_bit_alloc(unsigned int (*bit_alloc)[32], 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++) putbits(bs,bit_alloc[k][i],4);
- }
- /***************************** Layer II ********************************/
- void II_encode_bit_alloc (unsigned int (*bit_alloc)[32], 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++)
- putbits(bs,bit_alloc[k][i],(*alloc)[i][0].bits);
- }
- void II_encode_bit_alloc_mc (unsigned int (*bit_alloc)[32], frame_params *fr_ps, Bit_stream_struc *bs)
- {
- int i, k, l, m;
- layer *info = fr_ps->header;
- int center = info->center;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int sblimit = fr_ps->sblimit_mc;
- al_table *alloc = fr_ps->alloc_mc;
- for (i = 0; i < sblimit; i++)
- {
- l = sbgrp[i];
- for (m = stereo; m < stereo + stereomc; ++m)
- {
- k = transmission_channel (fr_ps, l, m);
- if ((fr_ps->header->center != 3) || (i < 12) || (k !=2))
- /* 960627 FdB DynX dependent on configuration */
- if (dyn_ch (fr_ps, l, m) == 1)
- putbits (bs, bit_alloc[k][i], (*alloc)[i][0].bits);
- }
- }
- }
- #ifdef Augmentation_7ch
- void II_encode_bit_alloc_aug (unsigned int (*bit_alloc)[32], frame_params *fr_ps, Bit_stream_struc *bs)
- {
- int i, k, l, m;
- layer *info = fr_ps->header;
- int center = info->center;
- int stereomc = fr_ps->stereomc;
- int sblimit = fr_ps->sblimit_mc;
- al_table *alloc = fr_ps->alloc_mc;
- for (i = 0; i < sblimit; i++)
- {
- l = sbgrp[i];
- for (m = 5; m < 7; ++m)
- {
- k = transmission_channel (fr_ps, l, m);
- if (dyn_ch (fr_ps, l, m) == 1)
- putbits (bs, bit_alloc[k][i], (*alloc)[i][0].bits);
- }
- }
- }
- #endif
- /************************************************************************
- /*
- /* I_sample_encoding (Layer I)
- /* II_sample_encoding (Layer II)
- /* II_sample_encoding_mc (Layer II) SR
- #ifdef Augmentation_7ch
- /* II_sample_encoding_aug (Layer II) FdB
- #endif
- /*
- /* PURPOSE:Put one frame of subband samples on to the bitstream
- /*
- /* SEMANTICS: The number of bits allocated per sample is read from
- /* the bit allocation information #bit_alloc#. Layer 2
- /* supports writing grouped samples for quantization steps
- /* that are not a power of 2.
- /*
- /************************************************************************/
- void I_sample_encoding (unsigned int (*sbband)[3][12][32], unsigned int (*bit_alloc)[32],
- 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<12;j++) {
- for(i=0;i<SBLIMIT;i++)
- for(k=0;k<((i<jsbound)?stereo:1);k++)
- if(bit_alloc[k][i]) putbits(bs,sbband[k][0][j][i],bit_alloc[k][i]+1);
- }
- }
- /***************************** Layer II ********************************/
- void II_sample_encoding (unsigned int (*sbband)[3][12][32], unsigned int (*bit_alloc)[32],
- frame_params *fr_ps, Bit_stream_struc *bs)
- {
- unsigned int temp;
- unsigned int i,j,k,s,x,y;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- 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<12;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++) putbits(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;
- putbits(bs,temp,(*alloc)[i][bit_alloc[k][i]].bits);
- }
- }
- }
- /******************* Layer II five channels ******************************/
- void II_sample_encoding_mc (unsigned int (*sbband)[3][12][32], unsigned int lfe_sbband[12],
- unsigned int (*bit_alloc)[32], unsigned int lfe_alloc,
- frame_params *fr_ps, Bit_stream_struc *bs)
- {
- unsigned int temp;
- unsigned int i,j,k,s,x,y, l, m;
- layer *info = fr_ps->header;
- int center = info->center;
- int stereo = fr_ps->stereo;
- int stereomc = fr_ps->stereomc;
- int sblimit = fr_ps->sblimit_mc;
- al_table *alloc = fr_ps->alloc_mc;
- int lfe = fr_ps->header->lfe;
- for (s = 0; s < 3; s++)
- for (j = 0; j < 12; j += 3)
- {
- if (lfe)
- putbits (bs, lfe_sbband[s*4+j/3], (*alloc)[0][lfe_alloc].bits);
- for (i = 0; i < sblimit; i++)
- {
- l = sbgrp[i];
- for (m = stereo; m < stereo+stereomc; m++)
- {
- k = transmission_channel (fr_ps, l, m);
- if (bit_alloc[k][i] && (i < 12 || m != 2 || center != 3))
- {
- if ((*alloc)[i][bit_alloc[k][i]].group == 3)
- for (x = 0; x < 3; x++)
- putbits (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;
- putbits (bs, temp, (*alloc)[i][bit_alloc[k][i]].bits);
- }
- }
- }
- }
- }
- }
- #ifdef Augmentation_7ch
- void II_sample_encoding_aug (unsigned int (*sbband)[3][12][32], unsigned int (*bit_alloc)[32],
- frame_params *fr_ps, Bit_stream_struc *bs)
- {
- unsigned int temp;
- unsigned int i,j,k,s,x,y, l, m;
- int sblimit = fr_ps->sblimit_mc;
- al_table *alloc = fr_ps->alloc_mc;
- for (s = 0; s < 3; s++)
- for (j = 0; j < 12; j += 3)
- for (i = 0; i < sblimit; i++)
- {
- l = sbgrp[i];
- for (m = 5; m < 7; m++)
- {
- k = transmission_channel (fr_ps, l, m);
- if (bit_alloc[k][i])
- {
- if ((*alloc)[i][bit_alloc[k][i]].group == 3)
- for (x = 0; x < 3; x++)
- putbits (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;
- putbits (bs, temp, (*alloc)[i][bit_alloc[k][i]].bits);
- }
- }
- }
- }
- }
- #endif
- /************************************************************************
- /*
- /* encode_CRC
- /*
- /************************************************************************/
- void encode_CRC (unsigned int crc, Bit_stream_struc *bs)
- {
- putbits (bs, crc, 16);
- }
- /***************************************************************************
- /*
- /* ancillary_encode
- /*
- /* PURPOSE: The use of ancillary part of the bitstream for information
- /* storage.
- /*
- /*
- /**************************************************************************/
- void ancillary_encode(fr_ps,bs,adb)
- frame_params *fr_ps;
- Bit_stream_struc *bs;
- int adb;
- {
- int bitsPerSlot;
- int samplesPerFrame;
- int bit_rate;
- int avg_slots_per_frame;
- int whole_SpF;
- int usedAdb;
- int adbNumberStart;
- int l;
- char *mesg="This bitstream use ancillary part.";
- extern int mesg_index;
- register int i;
- double frac_SpF;
- double slot_lag;
- layer *info = fr_ps->header;
- if (info->lay == 1) { bitsPerSlot = 32; samplesPerFrame = 384; }
- else { bitsPerSlot = 8; samplesPerFrame = 1152; }
- bit_rate = bitrate[info->lay-1][info->bitrate_index];
- avg_slots_per_frame = ((double)samplesPerFrame /
- s_freq[info->sampling_frequency]) *
- ((double)bit_rate / (double)bitsPerSlot);
- whole_SpF = (int) avg_slots_per_frame;
- adbNumberStart = (whole_SpF+info->padding) * bitsPerSlot;
- usedAdb = adbNumberStart - adb;
- if(usedAdb <= (adbNumberStart-8)) {
- if(usedAdb % 8) {
- for(i=0;i<(8-(usedAdb%8));i++)
- put1bit(bs,0);
- usedAdb += (8 - (usedAdb % 8));
- }
- while(usedAdb < adbNumberStart - 8) {
- for(i=0;i<8;i++)
- put1bit(bs,0);
- usedAdb += 8;
- }
- if(mesg_index >= strlen(mesg)){
- for(i=0;i<8;i++)
- put1bit(bs,0);
- } else {
- putbits(bs,(int)(mesg[mesg_index++]), 8);
- }
- } else {
- for(i=0;i<adb;i++)
- put1bit(bs,0);
- }
- }