/* 将数据从hdr结构翻译为fr_ps */ void hdr_to_frps(frame_params *fr_ps) { layer *hdr = fr_ps->header; /* (or pass in as arg?) */ fr_ps->actual_mode = hdr->mode; fr_ps->stereo = (hdr->mode == MPG_MD_MONO) ? 1 : 2; fr_ps->sblimit = SBLIMIT; if(hdr->mode == MPG_MD_JOINT_STEREO) fr_ps->jsbound = js_bound(hdr->lay, hdr->mode_ext); else fr_ps->jsbound = fr_ps->sblimit; }
void II_main_bit_allocation ( DOUBLE perm_smr[2][SBLIMIT], UINT scfsi[2][SBLIMIT], UINT bit_alloc[2][SBLIMIT], INT* adb, FRAME* pFrame, PSETTINGS pSettings ) { int noisy_sbs; int rq_db, av_db = *adb; // If mode equals to joint stereo, find the optimum jsbound setting if( pFrame->actual_mode == MPG_MD_JOINT_STEREO) { // Start with standard stereo setup pFrame->pHeader->mode = MPG_MD_STEREO; pFrame->pHeader->mode_ext = 0; pFrame->jsbound = pFrame->sblimit; // Calculate the required bits if((rq_db=II_bits_for_nonoise(perm_smr, scfsi, pFrame,0)) > *adb) { int mode_ext=4; // Were a few bits short, switch to joint stereo pFrame->pHeader->mode = MPG_MD_JOINT_STEREO; // Start with mode extention 3 (is decremented first in while loop) do { // Decrease mode extention mode_ext--; // Get new joint stereo subband boundary pFrame->jsbound = js_bound(pFrame->pHeader->lay, mode_ext); // Calculate the number of required bits with the new settings rq_db = II_bits_for_nonoise(perm_smr, scfsi, pFrame,0); // Check if we have enough bits, and if we still can lower the // joint stereo subband boundary } while( (rq_db > *adb) && (mode_ext > 0)); pFrame->pHeader->mode_ext = mode_ext; } /* well we either eliminated noisy sbs or mode_ext == 0 */ } if (pSettings->bUseVbr == FALSE) { noisy_sbs = II_a_bit_allocation(perm_smr, scfsi, bit_alloc, adb, pFrame); } else { int nCurrentAllocTab=0; int nReqBits=0; int nChanIdx = pSettings->pFrame->nChannels-1; int i; // Start with minimum bitrate for (i=0;i<15;i++) { if ( pSettings->nMinBitRateIndex<=AllowedBrIdx[nChanIdx][i]) break; } pFrame->pHeader->bitrate_index=AllowedBrIdx[nChanIdx][i++]; // Recalculate the sbLimit and select proper ALLOC table hdr_to_frps(pFrame); // Determine the available bits for this frame, with current bitrate settings *adb = available_bits(pSettings); // Work out how many bits are needed for there to be no noise (ie all MNR > 0.0 + VBRLEVEL) // Keep bumping up the bitrate until we have more than this number of bits nCurrentAllocTab=pFrame->tab_num; if (pSettings->nFrame==192) { printf("break\n"); } nReqBits=II_bits_for_nonoise(perm_smr, scfsi, pFrame,pSettings->nVbrQuality+(nCurrentAllocTab==2)?20:0); // nReqBits=II_bits_for_nonoise(perm_smr, scfsi, pFrame,pSettings->nVbrQuality); if ( pSettings->nDebugLevel > 3 ) { DebugPrintf("VBR: req_bits=%i for bitrate setting %d adb=%d table=%d\n", nReqBits, bitrate[pFrame->pHeader->version][pFrame->pHeader->lay-1][pFrame->pHeader->bitrate_index], *adb, nCurrentAllocTab ); } while ( (nReqBits > *adb) && (AllowedBrIdx[nChanIdx][i]!=-1) ) { // We are not there yet, increase bit rate if possible pFrame->pHeader->bitrate_index=AllowedBrIdx[nChanIdx][i++]; // Limit to maximum bitrate index //if (pFrame->pHeader->bitrate_index > 14) // pFrame->pHeader->bitrate_index = 14; // Get new frame header parameters for this bitrate setting hdr_to_frps(pFrame); // Did we switch from alloc table? If so, calculate new required bits if (nCurrentAllocTab!=pFrame->tab_num) { nReqBits=II_bits_for_nonoise(perm_smr, scfsi, pFrame,pSettings->nVbrQuality); nCurrentAllocTab=pFrame->tab_num; } // Get the number of available bits for the new frame settings *adb = available_bits(pSettings); if ( pSettings->nDebugLevel > 2 ) { DebugPrintf("VBR: req_bits=%i for bitrate setting %d adb=%d table=%d\n", nReqBits, bitrate[pFrame->pHeader->version][pFrame->pHeader->lay-1][pFrame->pHeader->bitrate_index], *adb, nCurrentAllocTab ); } } noisy_sbs = VBR_bit_allocation(perm_smr, scfsi, bit_alloc, adb, pFrame,pSettings); } if ( pSettings->nDebugLevel > 2 ) { DebugPrintf("VBR frame=%d bitrate=%4d, noisy_sbs=%4d , adb is %3d sblimit=%2d sbound=%2d\n", pSettings->nFrame, bitrate[pFrame->pHeader->version][pFrame->pHeader->lay-1][pFrame->pHeader->bitrate_index], noisy_sbs, *adb, pFrame->sblimit, pFrame->jsbound ); } }