liveMedia/MP3Internals.cpp File Reference

#include "MP3InternalsHuffman.hh"
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

Include dependency graph for MP3Internals.cpp:

Go to the source code of this file.

Data Structures

struct  bandInfoStruct

Defines

#define MPG_MD_MONO   3
#define TRUNC_FAIRLY
#define MPG_MD_JOINT_STEREO   1

Functions

unsigned ComputeFrameSize (unsigned bitrate, unsigned samplingFreq, Boolean usePadding, Boolean isMPEG2, unsigned char layer)
static unsigned updateSideInfoSizes (MP3SideInfo &sideInfo, Boolean isMPEG2, unsigned char const *mainDataPtr, unsigned allowedNumBits, unsigned &part23Length0a, unsigned &part23Length0aTruncation, unsigned &part23Length0b, unsigned &part23Length0bTruncation, unsigned &part23Length1a, unsigned &part23Length1aTruncation, unsigned &part23Length1b, unsigned &part23Length1bTruncation)
Boolean GetADUInfoFromMP3Frame (unsigned char const *framePtr, unsigned totFrameSize, unsigned &hdr, unsigned &frameSize, MP3SideInfo &sideInfo, unsigned &sideInfoSize, unsigned &backpointer, unsigned &aduSize)
static void getSideInfo1 (MP3FrameParams &fr, MP3SideInfo &si, int stereo, int ms_stereo, long sfreq, int)
static void getSideInfo2 (MP3FrameParams &fr, MP3SideInfo &si, int stereo, int ms_stereo, long sfreq, int)
static void putSideInfo1 (BitVector &bv, MP3SideInfo const &si, Boolean isStereo)
static void putSideInfo2 (BitVector &bv, MP3SideInfo const &si, Boolean isStereo)
static void PutMP3SideInfoIntoFrame (MP3SideInfo const &si, MP3FrameParams const &fr, unsigned char *framePtr)
Boolean ZeroOutMP3SideInfo (unsigned char *framePtr, unsigned totFrameSize, unsigned newBackpointer)
static unsigned MP3BitrateToBitrateIndex (unsigned bitrate, Boolean isMPEG2)
static void outputHeader (unsigned char *toPtr, unsigned hdr)
static void assignADUBackpointer (MP3FrameParams const &fr, unsigned aduSize, MP3SideInfo &sideInfo, unsigned &availableBytesForBackpointer)
unsigned TranscodeMP3ADU (unsigned char const *fromPtr, unsigned fromSize, unsigned toBitrate, unsigned char *toPtr, unsigned toMaxSize, unsigned &availableBytesForBackpointer)

Variables

static unsigned const live_tabsel [2][3][16]
static long const live_freqs [] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 }
static struct bandInfoStruct
const 
bandInfo [7]
unsigned int n_slen2 [512]
unsigned int i_slen2 [256]


Define Documentation

#define MPG_MD_JOINT_STEREO   1

Definition at line 512 of file MP3Internals.cpp.

Referenced by MP3FrameParams::getSideInfo().

#define MPG_MD_MONO   3

Definition at line 94 of file MP3Internals.cpp.

Referenced by MP3FrameParams::setParamsFromHeader().

#define TRUNC_FAIRLY

Definition at line 233 of file MP3Internals.cpp.


Function Documentation

static void assignADUBackpointer ( MP3FrameParams const &  fr,
unsigned  aduSize,
MP3SideInfo sideInfo,
unsigned &  availableBytesForBackpointer 
) [static]

Definition at line 682 of file MP3Internals.cpp.

References MP3FrameParams::frameSize, MP3FrameParams::isMPEG2, MP3SideInfo::main_data_begin, and MP3FrameParams::sideInfoSize.

Referenced by TranscodeMP3ADU().

00685                                                                          {
00686   // Give the ADU as large a backpointer as possible:
00687   unsigned maxBackpointerSize = fr.isMPEG2 ? 255 : 511;
00688 
00689   unsigned backpointerSize = availableBytesForBackpointer;
00690   if (backpointerSize > maxBackpointerSize) {
00691     backpointerSize = maxBackpointerSize;
00692   }
00693 
00694   // Store the new backpointer now:
00695   sideInfo.main_data_begin = backpointerSize;
00696 
00697   // Figure out how many bytes are available for the *next* ADU's backpointer:
00698   availableBytesForBackpointer
00699     = backpointerSize + fr.frameSize - fr.sideInfoSize ;
00700   if (availableBytesForBackpointer < aduSize) {
00701     availableBytesForBackpointer = 0;
00702   } else {
00703     availableBytesForBackpointer -= aduSize;
00704   }
00705 }

unsigned ComputeFrameSize ( unsigned  bitrate,
unsigned  samplingFreq,
Boolean  usePadding,
Boolean  isMPEG2,
unsigned char  layer 
)

Definition at line 219 of file MP3Internals.cpp.

Referenced by MP3FrameParams::setParamsFromHeader().

00221                                                {
00222   if (samplingFreq == 0) return 0;
00223   unsigned const bitrateMultiplier = (layer == 1) ? 12000*4 : 144000;
00224   unsigned framesize;
00225 
00226   framesize = bitrate*bitrateMultiplier;
00227   framesize /= samplingFreq<<isMPEG2;
00228   framesize = framesize + usePadding - 4;
00229 
00230   return framesize;
00231 }

Boolean GetADUInfoFromMP3Frame ( unsigned char const *  framePtr,
unsigned  totFrameSize,
unsigned &  hdr,
unsigned &  frameSize,
MP3SideInfo sideInfo,
unsigned &  sideInfoSize,
unsigned &  backpointer,
unsigned &  aduSize 
)

Definition at line 303 of file MP3Internals.cpp.

References MP3SideInfo::ch, False, MP3FrameParams::frameSize, MP3FrameParams::getSideInfo(), MP3SideInfo::gr, MP3FrameParams::hdr, MP3FrameParams::layer, MP3SideInfo::main_data_begin, MP3FrameParams::setBytePointer(), MP3FrameParams::setParamsFromHeader(), MP3FrameParams::sideInfoSize, and True.

Referenced by SegmentQueue::sqAfterGettingCommon(), and TranscodeMP3ADU().

00307                                                                          {
00308   if (totFrameSize < 4) return False; // there's not enough data
00309 
00310   MP3FrameParams fr;
00311   fr.hdr =   ((unsigned)framePtr[0] << 24) | ((unsigned)framePtr[1] << 16)
00312            | ((unsigned)framePtr[2] << 8) | (unsigned)framePtr[3];
00313   fr.setParamsFromHeader();
00314   fr.setBytePointer(framePtr + 4, totFrameSize - 4); // skip hdr
00315 
00316   frameSize = 4 + fr.frameSize;
00317 
00318   if (fr.layer != 3) {
00319     // Special case for non-layer III frames
00320     backpointer = 0;
00321     sideInfoSize = 0;
00322     aduSize = fr.frameSize;
00323     return True;
00324   }
00325 
00326   sideInfoSize = fr.sideInfoSize;
00327   if (totFrameSize < 4 + sideInfoSize) return False; // not enough data
00328 
00329   fr.getSideInfo(sideInfo);
00330 
00331   hdr = fr.hdr;
00332   backpointer = sideInfo.main_data_begin;
00333   unsigned numBits = sideInfo.ch[0].gr[0].part2_3_length;
00334   numBits += sideInfo.ch[0].gr[1].part2_3_length;
00335   numBits += sideInfo.ch[1].gr[0].part2_3_length;
00336   numBits += sideInfo.ch[1].gr[1].part2_3_length;
00337   aduSize = (numBits+7)/8;
00338 #ifdef DEBUG
00339   fprintf(stderr, "mp3GetADUInfoFromFrame: hdr: %08x, frameSize: %d, part2_3_lengths: %d,%d,%d,%d, aduSize: %d, backpointer: %d\n", hdr, frameSize, sideInfo.ch[0].gr[0].part2_3_length, sideInfo.ch[0].gr[1].part2_3_length, sideInfo.ch[1].gr[0].part2_3_length, sideInfo.ch[1].gr[1].part2_3_length, aduSize, backpointer);
00340 #endif
00341 
00342   return True;
00343 }

static void getSideInfo1 ( MP3FrameParams fr,
MP3SideInfo si,
int  stereo,
int  ms_stereo,
long  sfreq,
int   
) [static]

Definition at line 346 of file MP3Internals.cpp.

References bandInfo, MP3SideInfo::ch, MP3FrameParams::get1Bit(), MP3FrameParams::getBits(), MP3SideInfo::gr, bandInfoStruct::longIdx, MP3SideInfo::main_data_begin, and MP3SideInfo::private_bits.

Referenced by MP3FrameParams::getSideInfo().

00348                                          {
00349    int ch, gr;
00350 #if 0
00351    int powdiff = (single == 3) ? 4 : 0;
00352 #endif
00353 
00354    /* initialize all four "part2_3_length" fields to zero: */
00355    si.ch[0].gr[0].part2_3_length = 0; si.ch[1].gr[0].part2_3_length = 0;
00356    si.ch[0].gr[1].part2_3_length = 0; si.ch[1].gr[1].part2_3_length = 0;
00357 
00358    si.main_data_begin = fr.getBits(9);
00359    if (stereo == 1)
00360      si.private_bits = fr.getBits(5);
00361    else
00362      si.private_bits = fr.getBits(3);
00363 
00364    for (ch=0; ch<stereo; ch++) {
00365        si.ch[ch].gr[0].scfsi = -1;
00366        si.ch[ch].gr[1].scfsi = fr.getBits(4);
00367    }
00368 
00369    for (gr=0; gr<2; gr++) {
00370      for (ch=0; ch<stereo; ch++) {
00371        MP3SideInfo::gr_info_s_t& gr_info = si.ch[ch].gr[gr];
00372 
00373        gr_info.part2_3_length = fr.getBits(12);
00374        gr_info.big_values = fr.getBits(9);
00375        gr_info.global_gain = fr.getBits(8);
00376 #if 0
00377        gr_info.pow2gain = gainpow2+256 - gr_info.global_gain + powdiff;
00378        if (ms_stereo) gr_info.pow2gain += 2;
00379 #endif
00380        gr_info.scalefac_compress = fr.getBits(4);
00381 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
00382        gr_info.window_switching_flag = fr.get1Bit();
00383        if (gr_info.window_switching_flag) {
00384          int i;
00385          gr_info.block_type = fr.getBits(2);
00386          gr_info.mixed_block_flag = fr.get1Bit();
00387          gr_info.table_select[0] = fr.getBits(5);
00388          gr_info.table_select[1] = fr.getBits(5);
00389          /*
00390           * table_select[2] not needed, because there is no region2,
00391           * but to satisfy some verifications tools we set it either.
00392           */
00393          gr_info.table_select[2] = 0;
00394          for (i=0;i<3;i++) {
00395            gr_info.subblock_gain[i] = fr.getBits(3);
00396            gr_info.full_gain[i]
00397              = gr_info.pow2gain + ((gr_info.subblock_gain[i])<<3);
00398          }
00399 
00400 #ifdef DEBUG_ERRORS
00401          if (gr_info.block_type == 0) {
00402            fprintf(stderr,"Blocktype == 0 and window-switching == 1 not allowed.\n");
00403          }
00404 #endif
00405          /* region_count/start parameters are implicit in this case. */
00406          gr_info.region1start = 36>>1;
00407          gr_info.region2start = 576>>1;
00408        }
00409        else
00410        {
00411          int i,r0c,r1c;
00412          for (i=0; i<3; i++) {
00413            gr_info.table_select[i] = fr.getBits(5);
00414          }
00415          r0c = gr_info.region0_count = fr.getBits(4);
00416          r1c = gr_info.region1_count = fr.getBits(3);
00417          gr_info.region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
00418          gr_info.region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
00419          gr_info.block_type = 0;
00420          gr_info.mixed_block_flag = 0;
00421        }
00422        gr_info.preflag = fr.get1Bit();
00423        gr_info.scalefac_scale = fr.get1Bit();
00424        gr_info.count1table_select = fr.get1Bit();
00425      }
00426    }
00427 }

static void getSideInfo2 ( MP3FrameParams fr,
MP3SideInfo si,
int  stereo,
int  ms_stereo,
long  sfreq,
int   
) [static]

Definition at line 429 of file MP3Internals.cpp.

References bandInfo, MP3SideInfo::ch, MP3FrameParams::get1Bit(), MP3FrameParams::getBits(), MP3SideInfo::gr, bandInfoStruct::longIdx, MP3SideInfo::main_data_begin, and MP3SideInfo::private_bits.

Referenced by MP3FrameParams::getSideInfo().

00431                                          {
00432    int ch;
00433 #if 0
00434    int powdiff = (single == 3) ? 4 : 0;
00435 #endif
00436 
00437    /* initialize all four "part2_3_length" fields to zero: */
00438    si.ch[0].gr[0].part2_3_length = 0; si.ch[1].gr[0].part2_3_length = 0;
00439    si.ch[0].gr[1].part2_3_length = 0; si.ch[1].gr[1].part2_3_length = 0;
00440 
00441    si.main_data_begin = fr.getBits(8);
00442    if (stereo == 1)
00443      si.private_bits = fr.get1Bit();
00444    else
00445      si.private_bits = fr.getBits(2);
00446 
00447    for (ch=0; ch<stereo; ch++) {
00448        MP3SideInfo::gr_info_s_t& gr_info = si.ch[ch].gr[0];
00449 
00450        gr_info.part2_3_length = fr.getBits(12);
00451        si.ch[ch].gr[1].part2_3_length = 0; /* to ensure granule 1 unused */
00452 
00453        gr_info.big_values = fr.getBits(9);
00454        gr_info.global_gain = fr.getBits(8);
00455 #if 0
00456        gr_info.pow2gain = gainpow2+256 - gr_info.global_gain + powdiff;
00457        if (ms_stereo) gr_info.pow2gain += 2;
00458 #endif
00459        gr_info.scalefac_compress = fr.getBits(9);
00460 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
00461        gr_info.window_switching_flag = fr.get1Bit();
00462        if (gr_info.window_switching_flag) {
00463          int i;
00464          gr_info.block_type = fr.getBits(2);
00465          gr_info.mixed_block_flag = fr.get1Bit();
00466          gr_info.table_select[0] = fr.getBits(5);
00467          gr_info.table_select[1] = fr.getBits(5);
00468          /*
00469           * table_select[2] not needed, because there is no region2,
00470           * but to satisfy some verifications tools we set it either.
00471           */
00472          gr_info.table_select[2] = 0;
00473          for (i=0;i<3;i++) {
00474            gr_info.subblock_gain[i] = fr.getBits(3);
00475            gr_info.full_gain[i]
00476              = gr_info.pow2gain + ((gr_info.subblock_gain[i])<<3);
00477          }
00478 
00479 #ifdef DEBUG_ERRORS
00480          if (gr_info.block_type == 0) {
00481            fprintf(stderr,"Blocktype == 0 and window-switching == 1 not allowed.\n");
00482          }
00483 #endif
00484          /* region_count/start parameters are implicit in this case. */
00485 /* check this again! */
00486          if (gr_info.block_type == 2)
00487            gr_info.region1start = 36>>1;
00488          else {
00489            gr_info.region1start = 54>>1;
00490          }
00491          gr_info.region2start = 576>>1;
00492        }
00493        else
00494        {
00495          int i,r0c,r1c;
00496          for (i=0; i<3; i++) {
00497            gr_info.table_select[i] = fr.getBits(5);
00498          }
00499          r0c = gr_info.region0_count = fr.getBits(4);
00500          r1c = gr_info.region1_count = fr.getBits(3);
00501          gr_info.region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
00502          gr_info.region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
00503          gr_info.block_type = 0;
00504          gr_info.mixed_block_flag = 0;
00505        }
00506        gr_info.scalefac_scale = fr.get1Bit();
00507        gr_info.count1table_select = fr.get1Bit();
00508    }
00509 }

static unsigned MP3BitrateToBitrateIndex ( unsigned  bitrate,
Boolean  isMPEG2 
) [static]

Definition at line 664 of file MP3Internals.cpp.

References live_tabsel.

Referenced by TranscodeMP3ADU().

00665                                                           {
00666   for (unsigned i = 1; i < 15; ++i) {
00667     if (live_tabsel[isMPEG2][2][i] >= bitrate)
00668       return i;
00669   }
00670 
00671   // "bitrate" was larger than any possible, so return the largest possible:
00672   return 14;
00673 }

static void outputHeader ( unsigned char *  toPtr,
unsigned  hdr 
) [static]

Definition at line 675 of file MP3Internals.cpp.

Referenced by TranscodeMP3ADU().

00675                                                              {
00676   toPtr[0] = (unsigned char)(hdr>>24);
00677   toPtr[1] = (unsigned char)(hdr>>16);
00678   toPtr[2] = (unsigned char)(hdr>>8);
00679   toPtr[3] = (unsigned char)(hdr);
00680 }

static void PutMP3SideInfoIntoFrame ( MP3SideInfo const &  si,
MP3FrameParams const &  fr,
unsigned char *  framePtr 
) [static]

Definition at line 621 of file MP3Internals.cpp.

References MP3FrameParams::bv, MP3FrameParams::hasCRC, MP3FrameParams::isMPEG2, MP3FrameParams::isStereo, putSideInfo1(), putSideInfo2(), and MP3FrameParams::sideInfoSize.

Referenced by TranscodeMP3ADU(), and ZeroOutMP3SideInfo().

00623                                                              {
00624   if (fr.hasCRC) framePtr += 2; // skip CRC
00625 
00626   BitVector bv(framePtr, 0, 8*fr.sideInfoSize);
00627 
00628   if (fr.isMPEG2) {
00629     putSideInfo2(bv, si, fr.isStereo);
00630   } else {
00631     putSideInfo1(bv, si, fr.isStereo);
00632   }
00633 }

static void putSideInfo1 ( BitVector bv,
MP3SideInfo const &  si,
Boolean  isStereo 
) [static]

Definition at line 536 of file MP3Internals.cpp.

References MP3FrameParams::bv, MP3SideInfo::ch, MP3SideInfo::gr, MP3SideInfo::main_data_begin, MP3SideInfo::private_bits, BitVector::put1Bit(), BitVector::putBits(), and MP3FrameParams::stereo.

Referenced by PutMP3SideInfoIntoFrame().

00537                                                                   {
00538   int ch, gr, i;
00539   int stereo = isStereo ? 2 : 1;
00540 
00541   bv.putBits(si.main_data_begin,9);
00542   if (stereo == 1)
00543     bv.putBits(si.private_bits, 5);
00544   else
00545     bv.putBits(si.private_bits, 3);
00546 
00547   for (ch=0; ch<stereo; ch++) {
00548     bv.putBits(si.ch[ch].gr[1].scfsi, 4);
00549   }
00550 
00551   for (gr=0; gr<2; gr++) {
00552     for (ch=0; ch<stereo; ch++) {
00553       MP3SideInfo::gr_info_s_t const& gr_info = si.ch[ch].gr[gr];
00554 
00555       bv.putBits(gr_info.part2_3_length, 12);
00556       bv.putBits(gr_info.big_values, 9);
00557       bv.putBits(gr_info.global_gain, 8);
00558       bv.putBits(gr_info.scalefac_compress, 4);
00559       bv.put1Bit(gr_info.window_switching_flag);
00560       if (gr_info.window_switching_flag) {
00561         bv.putBits(gr_info.block_type, 2);
00562         bv.put1Bit(gr_info.mixed_block_flag);
00563         for (i=0; i<2; i++)
00564           bv.putBits(gr_info.table_select[i], 5);
00565         for (i=0; i<3; i++)
00566           bv.putBits(gr_info.subblock_gain[i], 3);
00567       }
00568       else {
00569         for (i=0; i<3; i++)
00570           bv.putBits(gr_info.table_select[i], 5);
00571         bv.putBits(gr_info.region0_count, 4);
00572         bv.putBits(gr_info.region1_count, 3);
00573       }
00574 
00575       bv.put1Bit(gr_info.preflag);
00576       bv.put1Bit(gr_info.scalefac_scale);
00577       bv.put1Bit(gr_info.count1table_select);
00578     }
00579   }
00580 }

static void putSideInfo2 ( BitVector bv,
MP3SideInfo const &  si,
Boolean  isStereo 
) [static]

Definition at line 582 of file MP3Internals.cpp.

References MP3FrameParams::bv, MP3SideInfo::ch, MP3SideInfo::gr, MP3SideInfo::main_data_begin, MP3SideInfo::private_bits, BitVector::put1Bit(), BitVector::putBits(), and MP3FrameParams::stereo.

Referenced by PutMP3SideInfoIntoFrame().

00583                                                                   {
00584   int ch, i;
00585   int stereo = isStereo ? 2 : 1;
00586 
00587   bv.putBits(si.main_data_begin,8);
00588   if (stereo == 1)
00589     bv.put1Bit(si.private_bits);
00590   else
00591     bv.putBits(si.private_bits, 2);
00592 
00593   for (ch=0; ch<stereo; ch++) {
00594     MP3SideInfo::gr_info_s_t const& gr_info = si.ch[ch].gr[0];
00595 
00596     bv.putBits(gr_info.part2_3_length, 12);
00597     bv.putBits(gr_info.big_values, 9);
00598     bv.putBits(gr_info.global_gain, 8);
00599     bv.putBits(gr_info.scalefac_compress, 9);
00600     bv.put1Bit(gr_info.window_switching_flag);
00601     if (gr_info.window_switching_flag) {
00602       bv.putBits(gr_info.block_type, 2);
00603       bv.put1Bit(gr_info.mixed_block_flag);
00604       for (i=0; i<2; i++)
00605         bv.putBits(gr_info.table_select[i], 5);
00606       for (i=0; i<3; i++)
00607         bv.putBits(gr_info.subblock_gain[i], 3);
00608     }
00609     else {
00610       for (i=0; i<3; i++)
00611         bv.putBits(gr_info.table_select[i], 5);
00612       bv.putBits(gr_info.region0_count, 4);
00613       bv.putBits(gr_info.region1_count, 3);
00614     }
00615 
00616     bv.put1Bit(gr_info.scalefac_scale);
00617     bv.put1Bit(gr_info.count1table_select);
00618   }
00619 }

unsigned TranscodeMP3ADU ( unsigned char const *  fromPtr,
unsigned  fromSize,
unsigned  toBitrate,
unsigned char *  toPtr,
unsigned  toMaxSize,
unsigned &  availableBytesForBackpointer 
)

Definition at line 707 of file MP3Internals.cpp.

References assignADUBackpointer(), MP3FrameParams::frameSize, GetADUInfoFromMP3Frame(), MP3FrameParams::hdr, MP3FrameParams::isMPEG2, MP3BitrateToBitrateIndex(), outputHeader(), PutMP3SideInfoIntoFrame(), MP3FrameParams::setParamsFromHeader(), shiftBits(), MP3FrameParams::sideInfoSize, and updateSideInfoSizes().

Referenced by MP3ADUTranscoder::afterGettingFrame1().

00710                                                               {
00711   // Begin by parsing the input ADU's parameters:
00712   unsigned hdr, inFrameSize, inSideInfoSize, backpointer, inAduSize;
00713   MP3SideInfo sideInfo;
00714   if (!GetADUInfoFromMP3Frame(fromPtr, fromSize,
00715                               hdr, inFrameSize, sideInfo, inSideInfoSize,
00716                               backpointer, inAduSize)) {
00717     return 0;
00718   }
00719   fromPtr += (4+inSideInfoSize); // skip to 'main data'
00720 
00721   // Alter the 4-byte MPEG header to reflect the output ADU:
00722   // (different bitrate; mono; no CRC)
00723   Boolean isMPEG2 = ((hdr&0x00080000) == 0);
00724   unsigned toBitrateIndex = MP3BitrateToBitrateIndex(toBitrate, isMPEG2);
00725   hdr &=~ 0xF000; hdr |= (toBitrateIndex<<12); // set bitrate index
00726   hdr |= 0x10200; // turn on !error-prot and padding bits
00727   hdr &=~ 0xC0; hdr |= 0xC0; // set mode to 3 (mono)
00728 
00729   // Set up the rest of the parameters of the new ADU:
00730   MP3FrameParams outFr;
00731   outFr.hdr = hdr;
00732   outFr.setParamsFromHeader();
00733 
00734   // Figure out how big to make the output ADU:
00735   unsigned inAveAduSize = inFrameSize - inSideInfoSize;
00736   unsigned outAveAduSize = outFr.frameSize - outFr.sideInfoSize;
00737   unsigned desiredOutAduSize /*=inAduSize*outAveAduSize/inAveAduSize*/
00738     = (2*inAduSize*outAveAduSize + inAveAduSize)/(2*inAveAduSize);
00739       // this rounds to the nearest integer
00740 
00741   if (toMaxSize < (4 + outFr.sideInfoSize)) return 0;
00742   unsigned maxOutAduSize = toMaxSize - (4 + outFr.sideInfoSize);
00743   if (desiredOutAduSize > maxOutAduSize) {
00744     desiredOutAduSize = maxOutAduSize;
00745   }
00746 
00747   // Figure out the new sizes of the various 'part23 lengths',
00748   // and how much they are truncated:
00749   unsigned part23Length0a, part23Length0aTruncation;
00750   unsigned part23Length0b, part23Length0bTruncation;
00751   unsigned part23Length1a, part23Length1aTruncation;
00752   unsigned part23Length1b, part23Length1bTruncation;
00753   unsigned numAduBits
00754     = updateSideInfoSizes(sideInfo, outFr.isMPEG2,
00755                           fromPtr, 8*desiredOutAduSize,
00756                           part23Length0a, part23Length0aTruncation,
00757                           part23Length0b, part23Length0bTruncation,
00758                           part23Length1a, part23Length1aTruncation,
00759                           part23Length1b, part23Length1bTruncation);
00760 #ifdef DEBUG
00761 fprintf(stderr, "shrinkage %d->%d [(%d,%d),(%d,%d)] (trunc: [(%d,%d),(%d,%d)]) {%d}\n", inAduSize, (numAduBits+7)/8,
00762               part23Length0a, part23Length0b, part23Length1a, part23Length1b,
00763               part23Length0aTruncation, part23Length0bTruncation,
00764               part23Length1aTruncation, part23Length1bTruncation,
00765               maxOutAduSize);
00766 #endif
00767  unsigned actualOutAduSize = (numAduBits+7)/8;
00768 
00769  // Give the new ADU an appropriate 'backpointer':
00770  assignADUBackpointer(outFr, actualOutAduSize, sideInfo, availableBytesForBackpointer);
00771 
00773 
00774  // 4-byte header
00775  outputHeader(toPtr, hdr); toPtr += 4;
00776 
00777  // side info
00778  PutMP3SideInfoIntoFrame(sideInfo, outFr, toPtr); toPtr += outFr.sideInfoSize;
00779 
00780  // 'main data', using the new lengths
00781  unsigned toBitOffset = 0;
00782  unsigned fromBitOffset = 0;
00783 
00784  /* rebuild portion 0a: */
00785  memmove(toPtr, fromPtr, (part23Length0a+7)/8);
00786  toBitOffset += part23Length0a;
00787  fromBitOffset += part23Length0a + part23Length0aTruncation;
00788 
00789  /* rebuild portion 0b: */
00790  shiftBits(toPtr, toBitOffset, fromPtr, fromBitOffset, part23Length0b);
00791  toBitOffset += part23Length0b;
00792  fromBitOffset += part23Length0b + part23Length0bTruncation;
00793 
00794  /* rebuild portion 1a: */
00795  shiftBits(toPtr, toBitOffset, fromPtr, fromBitOffset, part23Length1a);
00796  toBitOffset += part23Length1a;
00797  fromBitOffset += part23Length1a + part23Length1aTruncation;
00798 
00799  /* rebuild portion 1b: */
00800  shiftBits(toPtr, toBitOffset, fromPtr, fromBitOffset, part23Length1b);
00801  toBitOffset += part23Length1b;
00802 
00803  /* zero out any remaining bits (probably unnecessary, but...) */
00804  unsigned char const zero = '\0';
00805  shiftBits(toPtr, toBitOffset, &zero, 0,
00806            actualOutAduSize*8 - numAduBits);
00807 
00808  return 4 + outFr.sideInfoSize + actualOutAduSize;
00809 }

static unsigned updateSideInfoSizes ( MP3SideInfo sideInfo,
Boolean  isMPEG2,
unsigned char const *  mainDataPtr,
unsigned  allowedNumBits,
unsigned &  part23Length0a,
unsigned &  part23Length0aTruncation,
unsigned &  part23Length0b,
unsigned &  part23Length0bTruncation,
unsigned &  part23Length1a,
unsigned &  part23Length1aTruncation,
unsigned &  part23Length1b,
unsigned &  part23Length1bTruncation 
) [static]

Definition at line 234 of file MP3Internals.cpp.

References MP3SideInfo::ch, MP3SideInfo::gr, and updateSideInfoForHuffman().

Referenced by TranscodeMP3ADU().

00244                                                                         {
00245   unsigned p23L0, p23L1 = 0, p23L0Trunc = 0, p23L1Trunc = 0;
00246 
00247   p23L0 = sideInfo.ch[0].gr[0].part2_3_length;
00248   p23L1 = isMPEG2 ? 0 : sideInfo.ch[0].gr[1].part2_3_length;
00249 #ifdef TRUNC_ONLY0
00250   if (p23L0 < allowedNumBits)
00251     allowedNumBits = p23L0;
00252 #endif
00253 #ifdef TRUNC_ONLY1
00254   if (p23L1 < allowedNumBits)
00255     allowedNumBits = p23L1;
00256 #endif
00257   if (p23L0 + p23L1 > allowedNumBits) {
00258     /* We need to shorten one or both fields */
00259     unsigned truncation = p23L0 + p23L1 - allowedNumBits;
00260 #ifdef TRUNC_FAIRLY
00261     p23L0Trunc = (truncation*p23L0)/(p23L0 + p23L1);
00262     p23L1Trunc = truncation - p23L0Trunc;
00263 #endif
00264 #if defined(TRUNC_FAVOR0) || defined(TRUNC_ONLY0)
00265     p23L1Trunc = (truncation>p23L1) ? p23L1 : truncation;
00266     p23L0Trunc = truncation - p23L1Trunc;
00267 #endif
00268 #if defined(TRUNC_FAVOR1) || defined(TRUNC_ONLY1)
00269     p23L0Trunc = (truncation>p23L0) ? p23L0 : truncation;
00270     p23L1Trunc = truncation - p23L0Trunc;
00271 #endif
00272   }
00273 
00274   /* ASSERT: (p23L0Trunc <= p23L0) && (p23l1Trunc <= p23L1) */
00275   p23L0 -= p23L0Trunc; p23L1 -= p23L1Trunc;
00276 #ifdef DEBUG
00277   fprintf(stderr, "updateSideInfoSizes (allowed: %d): %d->%d, %d->%d\n", allowedNumBits, p23L0+p23L0Trunc, p23L0, p23L1+p23L1Trunc, p23L1);
00278 #endif
00279 
00280   // The truncations computed above are still estimates.  We need to
00281   // adjust them so that the new fields will continue to end on
00282   // Huffman-encoded sample boundaries:
00283   updateSideInfoForHuffman(sideInfo, isMPEG2, mainDataPtr,
00284                            p23L0, p23L1,
00285                            part23Length0a, part23Length0aTruncation,
00286                            part23Length0b, part23Length0bTruncation,
00287                            part23Length1a, part23Length1aTruncation,
00288                            part23Length1b, part23Length1bTruncation);
00289   p23L0 = part23Length0a + part23Length0b;
00290   p23L1 = part23Length1a + part23Length1b;
00291 
00292   sideInfo.ch[0].gr[0].part2_3_length = p23L0;
00293   sideInfo.ch[0].gr[1].part2_3_length = p23L1;
00294   part23Length0bTruncation
00295     += sideInfo.ch[1].gr[0].part2_3_length; /* allow for stereo */
00296   sideInfo.ch[1].gr[0].part2_3_length = 0; /* output mono */
00297   sideInfo.ch[1].gr[1].part2_3_length = 0; /* output mono */
00298 
00299   return p23L0 + p23L1;
00300 }

Boolean ZeroOutMP3SideInfo ( unsigned char *  framePtr,
unsigned  totFrameSize,
unsigned  newBackpointer 
)

Definition at line 636 of file MP3Internals.cpp.

References MP3SideInfo::ch, False, MP3FrameParams::getSideInfo(), MP3SideInfo::gr, MP3FrameParams::hdr, MP3SideInfo::main_data_begin, PutMP3SideInfoIntoFrame(), MP3FrameParams::setBytePointer(), MP3FrameParams::setParamsFromHeader(), MP3FrameParams::sideInfoSize, and True.

Referenced by SegmentQueue::insertDummyBeforeTail().

00637                                                     {
00638   if (totFrameSize < 4) return False; // there's not enough data
00639 
00640   MP3FrameParams fr;
00641   fr.hdr =   ((unsigned)framePtr[0] << 24) | ((unsigned)framePtr[1] << 16)
00642            | ((unsigned)framePtr[2] << 8) | (unsigned)framePtr[3];
00643   fr.setParamsFromHeader();
00644   fr.setBytePointer(framePtr + 4, totFrameSize - 4); // skip hdr
00645 
00646   if (totFrameSize < 4 + fr.sideInfoSize) return False; // not enough data
00647 
00648   MP3SideInfo si;
00649   fr.getSideInfo(si);
00650 
00651   si.main_data_begin = newBackpointer; /* backpointer */
00652   /* set all four "part2_3_length" and "big_values" fields to zero: */
00653   si.ch[0].gr[0].part2_3_length = si.ch[0].gr[0].big_values = 0;
00654   si.ch[1].gr[0].part2_3_length = si.ch[1].gr[0].big_values = 0;
00655   si.ch[0].gr[1].part2_3_length = si.ch[0].gr[1].big_values = 0;
00656   si.ch[1].gr[1].part2_3_length = si.ch[1].gr[1].big_values = 0;
00657 
00658   PutMP3SideInfoIntoFrame(si, fr, framePtr + 4);
00659 
00660   return True;
00661 }


Variable Documentation

struct bandInfoStruct const bandInfo[7] [static]

Definition at line 51 of file MP3Internals.cpp.

Referenced by getSideInfo1(), and getSideInfo2().

unsigned int i_slen2[256]

Definition at line 92 of file MP3Internals.cpp.

Referenced by MP3FrameParams::MP3FrameParams().

long const live_freqs[] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 } [static]

Definition at line 42 of file MP3Internals.cpp.

Referenced by MP3FrameParams::setParamsFromHeader().

unsigned const live_tabsel[2][3][16] [static]

Initial value:

 {
   { {32,32,64,96,128,160,192,224,256,288,320,352,384,416,448,448},
     {32,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,384},
     {32,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,320} },

   { {32,32,48,56,64,80,96,112,128,144,160,176,192,224,256,256},
     {8,8,16,24,32,40,48,56,64,80,96,112,128,144,160,160},
     {8,8,16,24,32,40,48,56,64,80,96,112,128,144,160,160} }
}

Definition at line 30 of file MP3Internals.cpp.

Referenced by MP3BitrateToBitrateIndex(), and MP3FrameParams::setParamsFromHeader().

unsigned int n_slen2[512]

Definition at line 91 of file MP3Internals.cpp.

Referenced by MP3FrameParams::MP3FrameParams(), and rsf_get_scale_factors_2().


Generated on Thu May 17 07:14:05 2012 for live by  doxygen 1.5.2