FLACMetadataSplitter.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2004 Zentaro Kavanagh
00003 //
00004 //Redistribution and use in source and binary forms, with or without
00005 //modification, are permitted provided that the following conditions
00006 //are met:
00007 //
00008 //- Redistributions of source code must retain the above copyright
00009 //  notice, this list of conditions and the following disclaimer.
00010 //
00011 //- Redistributions in binary form must reproduce the above copyright
00012 //  notice, this list of conditions and the following disclaimer in the
00013 //  documentation and/or other materials provided with the distribution.
00014 //
00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00016 //  may be used to endorse or promote products derived from this software 
00017 //  without specific prior written permission.
00018 //
00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00022 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //===========================================================================
00031 
00032 #include "stdafx.h"
00033 #include ".\flacmetadatasplitter.h"
00034 
00035 FLACMetadataSplitter::FLACMetadataSplitter(void)
00036         :       mMetadataBlock(NULL)
00037 {
00038         //debugLog.open("G:\\logs\\flacsplitter.log", ios_base::out);
00039 }
00040 
00041 FLACMetadataSplitter::~FLACMetadataSplitter(void)
00042 {
00043         delete mMetadataBlock;
00044         //Delete stuff !!
00045         //debugLog.close();
00046 }
00047 StampedOggPacket* FLACMetadataSplitter::convertToStampedPacket(OggPacket* inPacket) {
00048         //Convert the old packet to the new one.
00049         //This function deletes the incoming packet... and transfers the buffer directly into
00050         // the stamped packet without a memcpy
00051         //debugLog<<"Convert packet..."<<endl;
00052         StampedOggPacket* locStamped = new StampedOggPacket(inPacket->packetData(), inPacket->packetSize(), false, false, 0,0,StampedOggPacket::OGG_END_ONLY);
00053         //Ensure when we delete the old packet, it doesn't delete it's buffer
00054         inPacket->setPacketData(NULL);
00055         delete inPacket;                
00056         //debugLog<<"Post delete..."<<endl;
00057         return locStamped;
00058 }
00059 
00060 bool FLACMetadataSplitter::loadMetadata(OggPacket* inMetadata) {
00061         //debugLog<<"Load Metadata"<<endl;
00062         delete mMetadataBlock;
00063         mMetadataBlock = inMetadata;
00064         return splitMetadata();
00065 }
00066 unsigned long FLACMetadataSplitter::numHeaders() {
00067         return mHeaderTweaker.numNewHeaders();
00068 }
00069 StampedOggPacket* FLACMetadataSplitter::getHeader(unsigned long inIndex) {
00070         if (inIndex < mHeaderTweaker.numNewHeaders()) {
00071                 return (FLACMetadataSplitter::convertToStampedPacket(mHeaderTweaker.getHeader(inIndex)));
00072         } else {
00073                 return NULL;
00074         }
00075 }
00076 
00077 void FLACMetadataSplitter::emptyList() {
00078 
00079 
00080 }
00081 bool FLACMetadataSplitter::splitMetadata() {
00082         //debugLog<<"Splitmetadata"<<endl;
00083         //emptyList();
00084         //OggPacket* locPacket = NULL;
00085         //unsigned char* locBuff = NULL;
00086         if (mMetadataBlock == NULL) {
00087                 return false;
00088         } else {
00089                 if (verifyCodecID()) {
00090                         //debugLog<<"Start adding packets..."<<endl;
00091                         addCodecIdent();
00092                         addStreamInfo();
00093                         addOtherHeaders();
00094                         //debugLog<<"Done adding packets..."<<endl;
00095 
00096                         //TODO::: Should really verify this !
00097                         return true;
00098                 } else {
00099                         return false;
00100                 }
00101 
00102         }
00103 
00104 }
00105 
00106 bool FLACMetadataSplitter::addOtherHeaders() {
00107         //debugLog<<"Add other headers..."<<endl;
00108         unsigned long locUpto = 42;
00109         unsigned long locMetaSize = mMetadataBlock->packetSize();
00110         unsigned char* locSourceBuff = mMetadataBlock->packetData();    //Don't delete !
00111         unsigned char* locNewBuff = NULL;
00112         unsigned long locPacketSize = 0;
00113         OggPacket* locPacket = NULL;
00114 
00115         //debugLog<<"Metadata size = "<<locMetaSize<<endl;
00116         while ( locUpto < locMetaSize) {
00117                 //debugLog<<"Add others loop... upto = "<<locUpto<<endl;
00118                 for (int i = 1; i < 4; i++) {
00119                         locPacketSize <<=8;
00120                         locPacketSize += locSourceBuff[locUpto+i];
00121                 }
00122                 locPacketSize += 4;
00123                 //debugLog<<"Packet size = "<<locPacketSize<<endl;
00124                 //locUpto += 4;
00125 
00126                 locNewBuff = new unsigned char[locPacketSize];
00127                 memcpy((void*)locNewBuff, (const void*)(locSourceBuff + locUpto), locPacketSize);
00128 
00129                 locPacket = new OggPacket(locNewBuff, locPacketSize, false, false);
00130                 //debugLog<<"Adding other packet..."<<endl;
00131                 //debugLog<<locPacket->toPackDumpString()<<endl;
00132                 mHeaderTweaker.acceptHeader(locPacket);
00133                 locPacket = NULL;
00134 
00135                 locUpto += (locPacketSize);
00136                 
00137 
00138         }
00139 
00140         return true;
00141 }
00142 bool FLACMetadataSplitter::addStreamInfo() {
00143         //debugLog<<"addstreaminfo..."<<endl;
00144         OggPacket* locPacket = NULL;
00145         unsigned char* locBuff = new unsigned char[38];
00146         
00147         memcpy((void*)locBuff, (const void*)(mMetadataBlock->packetData()+4), 38);
00148         locPacket = new OggPacket(locBuff, 38, false, false);           //No need to delete
00149         //debugLog<<"Adding stream info packet..."<<endl;
00150         //debugLog<<locPacket->toPackDumpString()<<endl;
00151         mHeaderTweaker.acceptHeader(locPacket);
00152         return true;
00153 }
00154 bool FLACMetadataSplitter::addCodecIdent() {
00155         //debugLog<<"Add codec ident"<<endl;
00156         OggPacket* locPacket = NULL;
00157         unsigned char* locBuff = new unsigned char[4];
00158         locBuff[0] = 'f';
00159         locBuff[1] = 'L';
00160         locBuff[2] = 'a';
00161         locBuff[3] = 'C';
00162         locPacket = new OggPacket(locBuff, 4, false, false);            //No need to delete.
00163         mHeaderTweaker.acceptHeader(locPacket);
00164         return true;
00165 }
00166 
00167 bool FLACMetadataSplitter::verifyCodecID() {
00168         if ((strncmp((char*)mMetadataBlock->packetData(), "fLaC\000\000\000\042", 8)) == 0) {
00169                 //debugLog<<"Codec verified"<<endl;
00170                 return true;
00171         } else {
00172                 //debugLog<<"Codec NOT VERIFIED"<<endl;
00173                 return false;
00174         }
00175 
00176 }

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