FLACPushDecoder.cpp

Go to the documentation of this file.
00001 #include "stdafx.h"
00002 #include ".\flacpushdecoder.h"
00003 
00004 FLACPushDecoder::FLACPushDecoder(void)
00005         :       mInPacket(NULL)
00006         ,       mOutPacket(NULL)
00007         ,       mNumChannels(0)
00008         ,       mFrameSize(0)
00009         ,       mSampleRate(0)
00010         ,       mBegun(false)
00011         ,       mGotMetaData(false)
00012 {
00013 
00014 }
00015 
00016 FLACPushDecoder::~FLACPushDecoder(void)
00017 {
00018         delete mInPacket;
00019         delete mOutPacket;
00020 }
00021 
00022 void FLACPushDecoder::initCodec() {
00023         init();
00024 }
00025 void FLACPushDecoder::flushCodec() {
00026         flush();
00027 }
00028 
00029 bool FLACPushDecoder::acceptMetadata(OggPacket* inPacket) {
00030         delete mInPacket;
00031         mInPacket = inPacket;
00032         bool locMetaOK = process_until_end_of_metadata();
00033         delete mInPacket;
00034         mInPacket = NULL;
00035         delete mOutPacket;
00036         mOutPacket = NULL;
00037         return locMetaOK;
00038 }
00039 StampedOggPacket* FLACPushDecoder::decodeFLAC(OggPacket* inPacket) {
00040         //Basically puts the incoming packet into the member variable.
00041         //Calls process_single() and the read call back is fired.
00042         //The read callback feeds in the packet we just saved.
00043         //The write callback fires.
00044         //The write callback sets the outpacket into a member variable.
00045         //We return the member variable.
00046         delete mInPacket;
00047         mInPacket = inPacket;
00048         if(process_single()) {
00049                 return mOutPacket;
00050         } else {
00051                 delete mInPacket;
00052                 mInPacket = NULL;
00053                 delete mOutPacket;
00054                 mOutPacket = NULL;
00055                 return NULL;
00056         }
00057 
00058 }
00059 //FLAC Callbacks
00060 ::FLAC__StreamDecoderReadStatus FLACPushDecoder::read_callback(FLAC__byte outBuffer[], unsigned* outNumBytes) 
00061 {
00062         //If we have a packet waiting...
00063         if (mInPacket != NULL) {
00064                 //Copy it onto the buffer.
00065                 memcpy((void*)outBuffer, (const void*)mInPacket->packetData(), mInPacket->packetSize());
00066 
00067                 //Tell the decoder how big it is.
00068                 *outNumBytes = mInPacket->packetSize();
00069                 
00070                 //Delete the packet.
00071                 delete mInPacket;
00072                 mInPacket = NULL;
00073         
00074                 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
00075 
00076         } else {
00077                 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
00078         }
00079 
00080 }
00081 ::FLAC__StreamDecoderWriteStatus FLACPushDecoder::write_callback(const ::FLAC__Frame* inFrame, const FLAC__int32* const inBuffer[]) 
00082 {
00083 
00084         if (! mBegun) {
00085         
00086                 mBegun = true;
00087                 
00088                 mNumChannels = inFrame->header.channels;
00089                 mFrameSize = mNumChannels * SIZE_16_BITS;
00090                 mSampleRate = inFrame->header.sample_rate;
00091                 
00092         }
00093         unsigned long locNumFrames = inFrame->header.blocksize;
00094         unsigned long locActualSize = locNumFrames * mFrameSize;
00095         //unsigned long locTotalFrameCount = locNumFrames * mNumChannels;
00096 
00097         //BUG::: There's a bug here. Implicitly assumes 2 channels.
00098         unsigned char* locBuff = new unsigned char[locActualSize];
00099 
00100 
00101         signed short* locShortBuffer = (signed short*)locBuff;          //Don't delete this.
00102         
00103         signed short tempInt = 0;
00104         int tempLong = 0;
00105         //float tempFloat = 0;
00106         
00107         //FIX:::Move the clipping to the abstract function
00108         //Make sure our sample buffer is big enough
00109 
00110         //Modified for FLAC int32 not float
00111 
00112                 
00113         //Must interleave and convert sample size.
00114         for(unsigned long i = 0; i < locNumFrames; i++) {
00115                 for (unsigned long j = 0; j < mNumChannels; j++) {
00116                         
00117                         
00118                                 //No clipping required for ints
00119                                 //FIX:::Take out the unnescessary variable.
00120                         tempLong = inBuffer[j][i];
00121                                 //Convert 32 bit to 16 bit
00122 
00123                         //FIX::: Why on earth are you dividing by 2 ? It does not make sense !
00124                         tempInt = (signed short)(tempLong/2);
00125                 
00126                         *locShortBuffer = tempInt;
00127                         locShortBuffer++;
00128                 }
00129         }
00130         delete mOutPacket;
00131         mOutPacket = new StampedOggPacket(locBuff, locActualSize, false, false, 0, locNumFrames, StampedOggPacket::OGG_END_ONLY);
00132         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
00133 
00134 }
00135 void FLACPushDecoder::metadata_callback(const ::FLAC__StreamMetadata* inMetadata) 
00136 {
00137 
00138 }
00139 void FLACPushDecoder::error_callback(::FLAC__StreamDecoderErrorStatus inStatus) 
00140 {
00141 
00142 }

Generated on Tue Feb 15 14:54:15 2005 for oggdsf by  doxygen 1.3.9