AnxCutter.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh
00003 //
00004 //Copyright (C) 2004 Commonwealth Scientific and Industrial Research
00005 //   Organisation (CSIRO) Australia
00006 //
00007 //Redistribution and use in source and binary forms, with or without
00008 //modification, are permitted provided that the following conditions
00009 //are met:
00010 //
00011 //- Redistributions of source code must retain the above copyright
00012 //  notice, this list of conditions and the following disclaimer.
00013 //
00014 //- Redistributions in binary form must reproduce the above copyright
00015 //  notice, this list of conditions and the following disclaimer in the
00016 //  documentation and/or other materials provided with the distribution.
00017 //
00018 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00019 //  may be used to endorse or promote products derived from this software 
00020 //  without specific prior written permission.
00021 //
00022 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00026 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 //===========================================================================
00034 
00035 // AnxCutter.cpp : Defines the entry point for the console application.
00036 //
00037 
00038 #include "stdafx.h"
00039 #include <libOOOgg/libOOOgg.h>
00040 #include <libOOOgg/dllstuff.h>
00041 #include <libOOOggSeek/AutoAnxSeekTable.h>
00042 
00043 #include <iostream>
00044 #include <fstream>
00045 
00046 typedef pair<unsigned long, unsigned long> tSerial_HeadCountPair;
00047 
00048 unsigned long bytePos;
00049 
00050 bool gotAllHeaders;
00051 
00052 fstream outputFile;
00053 vector<tSerial_HeadCountPair> theStreams;
00054 
00055 using namespace std;
00056 
00057 enum eDemuxState {
00058         SEEN_NOTHING,
00059         SEEN_ANNODEX_BOS,
00060         SEEN_ANNODEX_EOS,
00061         SEEN_ALL_CODEC_HEADERS,
00062         INVALID = 100
00063 };
00064 
00065 eDemuxState demuxState;
00066 
00067 unsigned long annodexSerialNo;
00068 
00069 bool writePageToOutputFile(OggPage* inOggPage) {
00070         outputFile.write((char*)inOggPage->createRawPageData(), inOggPage->pageSize());
00071         return true;
00072 }
00073 
00074 unsigned long headerCount(OggPacket* inPacket) {
00075         const unsigned short NUM_SEC_HEADERS_OFFSET = 24;
00076         return iLE_Math::charArrToULong(inPacket->packetData() + NUM_SEC_HEADERS_OFFSET);
00077 
00078 }
00079 //This will be called by the callback
00080 bool pageCB(OggPage* inOggPage, void* /* ignored */) {
00081 
00082         bool allEmpty = true;
00083 
00084         switch (demuxState) {
00085 
00086                 case SEEN_NOTHING:
00087                         if (            (inOggPage->numPackets() == 1)
00088                                         &&      (inOggPage->header()->isBOS())
00089                                         &&      (strncmp((char*)inOggPage->getPacket(0)->packetData(), "Annodex\0", 8) == 0)) {
00090                                 
00091                                 //Advance the state
00092                                 demuxState = SEEN_ANNODEX_BOS;
00093 
00094                                 //Remember the annodex streams serial no
00095                                 annodexSerialNo = inOggPage->header()->StreamSerialNo();
00096 
00097                                 //Write out the page.
00098                                 writePageToOutputFile(inOggPage);
00099                         } else {
00100 
00101                                 demuxState = INVALID;
00102                         }
00103 
00104                         break;
00105                 case SEEN_ANNODEX_BOS:
00106                         if (            (inOggPage->numPackets() == 1)
00107                                         &&      (inOggPage->header()->isBOS())
00108                                         &&      (strncmp((char*)inOggPage->getPacket(0)->packetData(), "AnxData\0", 8) == 0)) {
00109                                 
00110 
00111                                 //Create an association of serial no and num headers
00112                                 tSerial_HeadCountPair locMap;
00113                                 locMap.first = inOggPage->header()->StreamSerialNo();
00114                                 locMap.second = headerCount(inOggPage->getPacket(0));
00115                                 
00116                                 //Add the association to the list
00117                                 theStreams.push_back(locMap);
00118 
00119                                 //Write the page out to the output file.
00120                                 writePageToOutputFile(inOggPage);
00121                         } else if (                     (inOggPage->header()->isEOS())
00122                                                         &&      (inOggPage->header()->StreamSerialNo() == annodexSerialNo)) {
00123 
00124                                 //It's the Annodex EOS.
00125                                 demuxState = SEEN_ANNODEX_EOS;
00126                                 writePageToOutputFile(inOggPage);
00127                         } else {
00128                                 demuxState = INVALID;
00129                         }
00130                         break;
00131                 case SEEN_ANNODEX_EOS:
00132                         for (unsigned int i = 0; i < theStreams.size(); i++) {
00133                                 if (theStreams[i].first == inOggPage->header()->StreamSerialNo()) {
00134                                         if (theStreams[i].second >= 1) {
00135                                                 theStreams[i].second--;
00136                                                 writePageToOutputFile(inOggPage);
00137                                         } else {
00138                                                 demuxState = INVALID;
00139                                         }
00140                                 }
00141                         }
00142 
00143                         
00144                         for (unsigned int i = 0; i < theStreams.size(); i++) {
00145                                 if (theStreams[i].second != 0) {
00146                                         allEmpty = false;
00147                                 }
00148                         }
00149 
00150                         if (allEmpty) {
00151 
00152                                 demuxState = SEEN_ALL_CODEC_HEADERS;
00153                         }
00154                         break;
00155                 case SEEN_ALL_CODEC_HEADERS:
00156                         {
00157                                 // We've processed all the codec headers, so all the incoming packets should be codec data
00158                                 unsigned long packetsInThisPage = inOggPage->numPackets();
00159                                 for (unsigned long i = 0; i < packetsInThisPage; i++)
00160                                 {
00161                                         StampedOggPacket *packet = inOggPage->getStampedPacket(i);
00162                                         cout << "Packet " << i << " start time: " << packet->startTime() << endl;
00163                                 }
00164                                 break;
00165                         }
00166                 case INVALID:
00167                         break;
00168                 default:
00169                         break;
00170         }
00171 
00172 
00173         if (demuxState == INVALID) {
00174                 cout << "Invalid file. Broken = very yes."<<endl;
00175         }
00176         delete inOggPage;
00177 
00178         return true;
00179 }
00180 
00181 #ifdef WIN32
00182 int __cdecl _tmain(int argc, _TCHAR* argv[])
00183 #else
00184 int main(int argc, char * argv[])
00185 #endif
00186 {
00187         demuxState = SEEN_NOTHING;
00188 
00189         bytePos = 0;
00190         gotAllHeaders = false;
00191 
00192         if (argc < 4) {
00193                 cout << "Usage : AnxCutter <input_filename> <output_filename> <start_time>" << endl;
00194         } else {
00195                 OggDataBuffer testOggBuff;
00196                 
00197                 testOggBuff.registerStaticCallback(&pageCB, NULL);
00198 
00199                 fstream inputFile;
00200                 
00201                 inputFile.open(argv[1], ios_base::in | ios_base::binary);
00202                 outputFile.open(argv[2], ios_base::out | ios_base::binary);
00203                 
00204                 const unsigned short BUFF_SIZE = 8092;
00205                 char* locBuff = new char[BUFF_SIZE];
00206                 while (demuxState < SEEN_ALL_CODEC_HEADERS) {
00207                         inputFile.read(locBuff, BUFF_SIZE);
00208                         unsigned long locBytesRead = inputFile.gcount();
00209                 testOggBuff.feed((const unsigned char*)locBuff, locBytesRead);
00210                 }
00211                 inputFile.close();
00212 
00213                 // Build a seek table for the file
00214                 AutoAnxSeekTable *locSeekTable = new AutoAnxSeekTable(argv[1]);
00215                 locSeekTable->buildTable();
00216 
00217                 // Seek to the user's requested start time
00218                 LOOG_UINT64 locStartTime = StringHelper::stringToNum(argv[3]);
00219                 OggSeekTable::tSeekPair locSeekResult = locSeekTable->getStartPos(locStartTime);
00220                 cout << "Seek result for " << locStartTime << " * 100 nanoseconds: " << locSeekResult.first << " at " << locSeekResult.second << " bytes" << endl;
00221 
00222                 // Stream-copy everything from the requested timepoint onward to the output file
00223                 inputFile.open(argv[1], ios_base::in | ios_base::binary);
00224                 inputFile.seekg(locSeekResult.second);
00225                 for (;;)
00226                 {
00227                         inputFile.read(locBuff, BUFF_SIZE);
00228                         unsigned long locBytesRead = inputFile.gcount();
00229                         if (locBytesRead == 0) break;
00230                         outputFile.write(locBuff, locBytesRead);
00231                 }
00232 
00233                 outputFile.close();
00234 
00235                 delete[] locBuff;
00236         }
00237 
00238         return 0;
00239 }
00240 

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