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
00041
00042
00043
00044
00045
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
00060 ::FLAC__StreamDecoderReadStatus FLACPushDecoder::read_callback(FLAC__byte outBuffer[], unsigned* outNumBytes)
00061 {
00062
00063 if (mInPacket != NULL) {
00064
00065 memcpy((void*)outBuffer, (const void*)mInPacket->packetData(), mInPacket->packetSize());
00066
00067
00068 *outNumBytes = mInPacket->packetSize();
00069
00070
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
00096
00097
00098 unsigned char* locBuff = new unsigned char[locActualSize];
00099
00100
00101 signed short* locShortBuffer = (signed short*)locBuff;
00102
00103 signed short tempInt = 0;
00104 int tempLong = 0;
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 for(unsigned long i = 0; i < locNumFrames; i++) {
00115 for (unsigned long j = 0; j < mNumChannels; j++) {
00116
00117
00118
00119
00120 tempLong = inBuffer[j][i];
00121
00122
00123
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 }