/***************************************************************************** functionname: encodeSbrGrid description: if hBitStream != NULL writes bits that describes the time/frequency grouping of a frame; else counts them only returns: number of bits written or counted input: output: *****************************************************************************/ static INT encodeSbrGrid(HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream) { INT payloadBits = 0; INT i, temp; INT bufferFrameStart = sbrEnvData->hSbrBSGrid->bufferFrameStart; INT numberTimeSlots = sbrEnvData->hSbrBSGrid->numberTimeSlots; if (sbrEnvData->ldGrid) payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->frameClass, SBR_CLA_BITS_LD); else payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->frameClass, SBR_CLA_BITS); switch (sbrEnvData->hSbrBSGrid->frameClass) { case FIXFIXonly: FDK_ASSERT(0 /* Fatal error in encodeSbrGrid! */); break; case FIXFIX: temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_env); payloadBits += FDKwriteBits(hBitStream, temp, SBR_ENV_BITS); if ((sbrEnvData->ldGrid) && (sbrEnvData->hSbrBSGrid->bs_num_env == 1)) payloadBits += FDKwriteBits(hBitStream, sbrEnvData->currentAmpResFF, SI_SBR_AMP_RES_BITS); payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->v_f[0], SBR_RES_BITS); break; case FIXVAR: case VARFIX: if (sbrEnvData->hSbrBSGrid->frameClass == FIXVAR) temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - (bufferFrameStart + numberTimeSlots); else temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - bufferFrameStart; payloadBits += FDKwriteBits(hBitStream, temp, SBR_ABS_BITS); payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->n, SBR_NUM_BITS); for (i = 0; i < sbrEnvData->hSbrBSGrid->n; i++) { temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord[i] - 2) >> 1; payloadBits += FDKwriteBits(hBitStream, temp, SBR_REL_BITS); } temp = ceil_ln2(sbrEnvData->hSbrBSGrid->n + 2); payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->p, temp); for (i = 0; i < sbrEnvData->hSbrBSGrid->n + 1; i++) { payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->v_f[i], SBR_RES_BITS); } break; case VARVAR: temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_0 - bufferFrameStart; payloadBits += FDKwriteBits(hBitStream, temp, SBR_ABS_BITS); temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_1 - (bufferFrameStart + numberTimeSlots); payloadBits += FDKwriteBits(hBitStream, temp, SBR_ABS_BITS); payloadBits += FDKwriteBits( hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_0, SBR_NUM_BITS); payloadBits += FDKwriteBits( hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_1, SBR_NUM_BITS); for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_0; i++) { temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_0[i] - 2) >> 1; payloadBits += FDKwriteBits(hBitStream, temp, SBR_REL_BITS); } for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_1; i++) { temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_1[i] - 2) >> 1; payloadBits += FDKwriteBits(hBitStream, temp, SBR_REL_BITS); } temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_rel_0 + sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 2); payloadBits += FDKwriteBits(hBitStream, sbrEnvData->hSbrBSGrid->p, temp); temp = sbrEnvData->hSbrBSGrid->bs_num_rel_0 + sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 1; for (i = 0; i < temp; i++) { payloadBits += FDKwriteBits( hBitStream, sbrEnvData->hSbrBSGrid->v_fLR[i], SBR_RES_BITS); } break; } return payloadBits; }
/***************************************************************************** description: Encode SBR grid information *****************************************************************************/ static int encodeSbrGrid (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_BIT_BUF hBitStream) { int payloadBits = 0; int i, temp; /* counting previous operations */ payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->frameClass, SBR_CLA_BITS); switch (sbrEnvData->hSbrBSGrid->frameClass) { case FIXFIX: temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_env); payloadBits += WriteBits (hBitStream, temp, SBR_ENV_BITS); payloadBits += WriteBits (hBitStream, sbrEnvData->freq_res_fixfix, SBR_RES_BITS); break; case FIXVAR: case VARFIX: if (sbrEnvData->hSbrBSGrid->frameClass == FIXVAR) { temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - 16; } else { temp = sbrEnvData->hSbrBSGrid->bs_abs_bord; } payloadBits += WriteBits (hBitStream, temp, SBR_ABS_BITS); payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->n, SBR_NUM_BITS); /* sbrEnvData->hSbrBSGrid->bs_rel_bord[] */ for (i = 0; i < sbrEnvData->hSbrBSGrid->n; i++) { temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord[i] - 2) >> 1; payloadBits += WriteBits (hBitStream, temp, SBR_REL_BITS); } temp = ceil_ln2(sbrEnvData->hSbrBSGrid->n + 2); payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->p, temp); /* sbrEnvData->hSbrBSGrid->v_f[] */ for (i = 0; i < sbrEnvData->hSbrBSGrid->n + 1; i++) { payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_f[i], SBR_RES_BITS); } break; case VARVAR: temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_0; payloadBits += WriteBits (hBitStream, temp, SBR_ABS_BITS); temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_1 - 16; payloadBits += WriteBits (hBitStream, temp, SBR_ABS_BITS); payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_0, SBR_NUM_BITS); payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_1, SBR_NUM_BITS); /* sbrEnvData->hSbrBSGrid->bs_rel_bord_0[] */ for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_0; i++) { temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_0[i] - 2) >> 1; payloadBits += WriteBits (hBitStream, temp, SBR_REL_BITS); } /* sbrEnvData->hSbrBSGrid->bs_rel_bord_1[] */ for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_1; i++) { temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_1[i] - 2) >> 1; payloadBits += WriteBits (hBitStream, temp, SBR_REL_BITS); } temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_rel_0 + sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 2); payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->p, temp); temp = sbrEnvData->hSbrBSGrid->bs_num_rel_0 + sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 1; /* sbrEnvData->hSbrBSGrid->v_fLR[] */ for (i = 0; i < temp; i++) { payloadBits += WriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_fLR[i], SBR_RES_BITS); } break; } return payloadBits; }