liveMedia/MP3Internals.hh File Reference

#include "Boolean.hh"
#include "BitVector.hh"

Include dependency graph for MP3Internals.hh:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  MP3SideInfo
struct  MP3SideInfo::gr_info_s
class  MP3FrameParams

Defines

#define SBLIMIT   32
#define MAX_MP3_FRAME_SIZE   2500

Typedefs

typedef MP3SideInfo MP3SideInfo_t

Functions

unsigned ComputeFrameSize (unsigned bitrate, unsigned samplingFreq, Boolean usePadding, Boolean isMPEG2, unsigned char layer)
Boolean GetADUInfoFromMP3Frame (unsigned char const *framePtr, unsigned totFrameSize, unsigned &hdr, unsigned &frameSize, MP3SideInfo &sideInfo, unsigned &sideInfoSize, unsigned &backpointer, unsigned &aduSize)
Boolean ZeroOutMP3SideInfo (unsigned char *framePtr, unsigned totFrameSize, unsigned newBackpointer)
unsigned TranscodeMP3ADU (unsigned char const *fromPtr, unsigned fromSize, unsigned toBitrate, unsigned char *toPtr, unsigned toMaxSize, unsigned &availableBytesForBackpointer)


Define Documentation

#define MAX_MP3_FRAME_SIZE   2500

Definition at line 64 of file MP3Internals.hh.

Referenced by MP3ADUTranscoder::doGetNextFrame().

#define SBLIMIT   32

Definition at line 63 of file MP3Internals.hh.

Referenced by MP3HuffmanEncodingInfo::MP3HuffmanEncodingInfo().


Typedef Documentation

typedef struct MP3SideInfo MP3SideInfo_t


Function Documentation

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 ? 1 : 0);
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 }

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

Definition at line 706 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().

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

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

Definition at line 635 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().

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


Generated on Tue Mar 25 14:38:08 2014 for live by  doxygen 1.5.2