📄 encode.c
字号:
if(small > mnr[k][i]) small = 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], 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 banc=32, berr=0;static int sfsPerScfsi[] = { 3,2,1,2 }; /* lookup # sfs per scfsi */ if (!init) { init = 1; if (fr_ps->header->error_protection) berr=16; /* added 92-08-11 shn */ } 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 { /* locate the subband with minimum SMR */ small = 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 && small > mnr[k][i]) { small = mnr[k][i]; min_sb = i; min_ch = k; } if(min_sb > -1) { /* there was something to find */ /* find increase in bit allocation in subband [min] */ 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); /* scale factor bits required for subband [min] */ oth_ch = 1 - min_ch; /* above js bound, need both chans */ 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]]; } } /* check to see enough bits were available for */ /* increasing resolution in the minimum band */ 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); /* until could find no channel */ /* Calculate the number of bits left */ 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; small = mnr[0][0]; /* calc worst noise in case */ for(k=0;k<stereo;++k) { for (i=0;i<sblimit;i++) { if (small > mnr[k][i]) small = 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) * * 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 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 jsbound = fr_ps->jsbound; double d;static char init = 0; if (!init) { init = 1; /* rearrange quantization coef to correspond to layer I table */ 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]) { /* 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][j][i] / multiple[j_scale[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 * a[n-1] + 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(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; unsigned int stps; 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]) { /* scale and quantize floating point sample */ if(stereo == 2 && i>=jsbound) /* use j-stereo samples */ 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) mjpeg_info("Not scaled properly %d %d %d %d",k,s,j,i); qnt = (*alloc)[i][bit_alloc[k][i]].quant; d = d * a[qnt] + 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; } 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) * * 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(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 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(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++) putbits(bs,bit_alloc[k][i],(*alloc)[i][0].bits);} /************************************************************************ * * I_sample_encoding (Layer I) * II_sample_encoding (Layer II) * * 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(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 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]) putbits(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 i,j,k,s,x,y; 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++) 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); } }} /************************************************************************ * * encode_CRC * ************************************************************************/ void encode_CRC(crc, bs)unsigned int crc;Bit_stream_struc *bs;{ putbits(bs, crc, 16);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -