00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef MXFLIB__ESP_JP2K_H
00031 #define MXFLIB__ESP_JP2K_H
00032
00033 #include <math.h>
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)
00067 : EssenceSubParserBase::ESP_EssenceSource(TheCaller, InFile, UseStream, Count)
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
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
00115
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
00131
00132
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
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
00239 if(Large == 0) return 1;
00240
00241 UInt32 Temp;
00242
00243
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