esp_jp2k.h

Go to the documentation of this file.
00001 
00007 /*
00008  *  Copyright (c) 2005, Matt Beard
00009  *
00010  *  This software is provided 'as-is', without any express or implied warranty.
00011  *  In no event will the authors be held liable for any damages arising from
00012  *  the use of this software.
00013  *
00014  *  Permission is granted to anyone to use this software for any purpose,
00015  *  including commercial applications, and to alter it and redistribute it
00016  *  freely, subject to the following restrictions:
00017  *
00018  *    1. The origin of this software must not be misrepresented; you must
00019  *       not claim that you wrote the original software. If you use this
00020  *       software in a product, an acknowledgment in the product
00021  *       documentation would be appreciated but is not required.
00022  *  
00023  *    2. Altered source versions must be plainly marked as such, and must
00024  *       not be misrepresented as being the original software.
00025  *  
00026  *    3. This notice may not be removed or altered from any source
00027  *       distribution.
00028  */
00029 
00030 #ifndef MXFLIB__ESP_JP2K_H
00031 #define MXFLIB__ESP_JP2K_H
00032 
00033 #include <math.h>   // For "floor"
00034 
00035 
00036 namespace mxflib
00037 {
00038     class JP2K_EssenceSubParser : public EssenceSubParserBase
00039     {
00040     protected:
00041         UInt32 SampleRate;                                  
00042         Rational UseEditRate;                               
00043 
00044         Position PictureNumber;                             
00045 
00046         Position DataStart;                                 
00047         Length DataSize;                                    
00048         Position CurrentPos;                                
00049 
00055         size_t CachedDataSize;                              
00056 
00057         MDObjectParent CurrentDescriptor;                   
00058 
00060     public:
00062         class ESP_EssenceSource : public EssenceSubParserBase::ESP_EssenceSource
00063         {
00064         public:
00066             ESP_EssenceSource(EssenceSubParserPtr TheCaller, FileHandle InFile, UInt32 UseStream, UInt64 Count = 1/*, IndexTablePtr UseIndex = NULL*/)
00067                 : EssenceSubParserBase::ESP_EssenceSource(TheCaller, InFile, UseStream, Count/*, UseIndex*/) 
00068             {
00069             };
00070 
00072 
00074             virtual size_t GetEssenceDataSize(void) 
00075             {
00076                 JP2K_EssenceSubParser *pCaller = SmartPtr_Cast(Caller, JP2K_EssenceSubParser);
00077                 return pCaller->ReadInternal(File, Stream, RequestedCount);
00078             };
00079 
00081 
00086             virtual DataChunkPtr GetEssenceData(size_t Size = 0, size_t MaxSize = 0)
00087             {
00088                 return BaseGetEssenceData(Size, MaxSize);
00089             }
00090 
00092             virtual int GetBERSize(void) 
00093             { 
00094                 JP2K_EssenceSubParser *pCaller = SmartPtr_Cast(Caller, JP2K_EssenceSubParser);
00095 
00096                 if(pCaller->SelectedWrapping->ThisWrapType == WrappingOption::Clip) return 8;
00097                 return 4;
00098             }
00099         };
00100 
00101         // Give our essence source class privilaged access
00102         friend class JP2K_EssenceSubParser::ESP_EssenceSource;
00103 
00104     public:
00105         JP2K_EssenceSubParser()
00106         {
00107             PictureNumber = 0;
00108 
00109             SampleRate = 1;
00110             DataStart = 0;
00111             DataSize = 0;
00112             CurrentPos = 0;
00113 
00114             // Use a sensible default if no edit rate is set - not ideal, but better than one sample!
00115             // It should always be possible to wrap at this rate, but the end of the data may not be a whole edit unit
00116             UseEditRate.Numerator = 1;
00117             UseEditRate.Denominator = 1;
00118 
00119             CachedDataSize = static_cast<size_t>(-1);
00120         }
00121 
00123         virtual EssenceSubParserPtr NewParser(void) const { return new JP2K_EssenceSubParser; }
00124 
00126         virtual StringList HandledExtensions(void)
00127         {
00128             StringList ExtensionList;
00129 
00130             // TODO: Add any supported extentions here
00131             //       This is used as a hint to the overall essence parser to decide which sub-parsers to try.
00132             //       Calls may still be made to this sub-parser for files of different extensions, but this is a starting point.
00133 
00134             ExtensionList.push_back("JP2");
00135 
00136             return ExtensionList;
00137         }
00138 
00140         virtual EssenceStreamDescriptorList IdentifyEssence(FileHandle InFile);
00141 
00143         virtual WrappingOptionList IdentifyWrappingOptions(FileHandle InFile, EssenceStreamDescriptor &Descriptor);
00144 
00146 
00147         virtual void Use(UInt32 Stream, WrappingOptionPtr UseWrapping)
00148         {
00149             SelectedWrapping = UseWrapping;
00150 
00151             CurrentPos = 0;
00152         }
00153 
00155 
00158         virtual bool SetEditRate(Rational EditRate)
00159         {
00160             UseEditRate = EditRate;
00161 
00162             // Pretend that the essence is sampled at whatever rate we are wrapping at
00163             MDObjectPtr Ptr;
00164             if(CurrentDescriptor) Ptr = CurrentDescriptor->AddChild(SampleRate_UL);
00165             if(Ptr)
00166             {
00167                 Ptr->SetInt("Numerator", UseEditRate.Numerator);
00168                 Ptr->SetInt("Denominator", UseEditRate.Denominator);
00169             }
00170             
00171             return true;
00172         }
00173 
00175         virtual Rational GetEditRate(void) { return UseEditRate; }
00176 
00178 
00180         virtual Rational GetPreferredEditRate(void) { return Rational(24, 1); };
00181 
00183         virtual Position GetCurrentPosition(void) { return PictureNumber; }
00184 
00186         virtual DataChunkPtr Read(FileHandle InFile, UInt32 Stream, UInt64 Count = 1);
00187 
00189         virtual EssenceSourcePtr GetEssenceSource(FileHandle InFile, UInt32 Stream, UInt64 Count = 1)
00190         {
00191             return new ESP_EssenceSource(this, InFile, Stream, Count);
00192         };
00193 
00195         virtual Length Write(FileHandle InFile, UInt32 Stream, MXFFilePtr OutFile, UInt64 Count = 1);
00196 
00198 
00202         virtual std::string GetParserName(void) const { return "jp2k"; }
00203 
00204 
00205     protected:
00207         typedef std::multimap<std::string, DataChunkPtr> HeaderType;
00208 
00210         HeaderType Header;
00211 
00213         bool CalcWrappingSequence(Rational EditRate);
00214 
00216 
00217         MDObjectPtr BuildDescriptorFromCodeStream(FileHandle InFile, Position Offset = 0 );
00218 
00220 
00221         MDObjectPtr BuildDescriptorFromJP2(FileHandle InFile);
00222 
00224         size_t ReadInternal(FileHandle InFile, UInt32 Stream, Length Count);
00225 
00227         bool ParseJP2Header(FileHandle InFile);
00228 
00230 
00232         bool ParseJP2KCodestreamHeader(FileHandle InFile, Position Offset);
00233 
00234 
00236         UInt32 GreatestCommonDivisor(UInt32 Large, UInt32 Small)
00237         {
00238             // Zero is never the GCD
00239             if(Large == 0) return 1;
00240 
00241             UInt32 Temp;
00242 
00243             // The rest of the algorithm assumes Large >= Small
00244             if(Large < Small)
00245             {
00246                 Temp = Large;
00247                 Large = Small;
00248                 Small = Temp;
00249             }
00250 
00251             while (Small > 0)
00252             {
00253                 Temp = Large % Small;
00254                 Large = Small;
00255                 Small = Temp;
00256             }
00257             
00258             return Large;
00259         }
00260 
00262         void ReduceRational(UInt32 &Numerator, UInt32 &Denominator)
00263         {
00264             if(Denominator == 1) return;
00265 
00266             UInt32 GCD = GreatestCommonDivisor(Numerator, Denominator);
00267             
00268             Numerator /= GCD;
00269             Denominator /= GCD;
00270         }
00271     };
00272 }
00273 
00274 #endif // MXFLIB__ESP_JP2K_H

Generated on Mon Apr 2 15:20:53 2007 for MXFLib by  doxygen 1.5.1-p1