2 Copyright (c) 2004-2012, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /*! \file AS_DCP_internal.h
29 \brief AS-DCP library, non-public common elements
32 #ifndef _AS_DCP_INTERNAL_H_
33 #define _AS_DCP_INTERNAL_H_
35 #include <KM_platform.h>
40 using Kumu::DefaultLogSink;
41 // using namespace std;
42 using namespace ASDCP;
43 using namespace ASDCP::MXF;
45 #ifdef DEFAULT_MD_DECL
46 ASDCP::MXF::OPAtomHeader *g_OPAtomHeader;
47 ASDCP::MXF::OPAtomIndexFooter *g_OPAtomIndexFooter;
49 extern MXF::OPAtomHeader *g_OPAtomHeader;
50 extern MXF::OPAtomIndexFooter *g_OPAtomIndexFooter;
56 void default_md_object_init();
59 static std::vector<int>
60 version_split(const char* str)
62 std::vector<int> result;
63 const char* pstr = str;
64 const char* r = strchr(pstr, '.');
70 result.push_back(atoi(pstr));
73 r = strchr(pstr, '.');
76 if( strlen(pstr) > 0 )
77 result.push_back(atoi(pstr));
79 assert(result.size() == 3);
83 // constant values used to calculate KLV and EKLV packet sizes
84 static const ui32_t klv_cryptinfo_size =
86 + UUIDlen /* ContextID */
88 + sizeof(ui64_t) /* PlaintextOffset */
90 + SMPTE_UL_LENGTH /* SourceKey */
92 + sizeof(ui64_t) /* SourceLength */
93 + MXF_BER_LENGTH /* ESV length */ ;
95 static const ui32_t klv_intpack_size =
97 + UUIDlen /* TrackFileID */
99 + sizeof(ui64_t) /* SequenceNumber */
101 + 20; /* HMAC length*/
103 // calculate size of encrypted essence with IV, CheckValue, and padding
105 calc_esv_length(ui32_t source_length, ui32_t plaintext_offset)
107 ui32_t ct_size = source_length - plaintext_offset;
108 ui32_t diff = ct_size % CBC_BLOCK_SIZE;
109 ui32_t block_size = ct_size - diff;
110 return plaintext_offset + block_size + (CBC_BLOCK_SIZE * 3);
113 // the check value for EKLV packets
115 static const byte_t ESV_CheckValue[CBC_BLOCK_SIZE] =
116 { 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b,
117 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b };
119 //------------------------------------------------------------------------------------------
122 Result_t MD_to_WriterInfo(MXF::Identification*, WriterInfo&);
123 Result_t MD_to_CryptoInfo(MXF::CryptographicContext*, WriterInfo&, const Dictionary&);
124 Result_t EncryptFrameBuffer(const ASDCP::FrameBuffer&, ASDCP::FrameBuffer&, AESEncContext*);
125 Result_t DecryptFrameBuffer(const ASDCP::FrameBuffer&, ASDCP::FrameBuffer&, AESDecContext*);
126 Result_t PCM_ADesc_to_MD(PCM::AudioDescriptor& ADesc, ASDCP::MXF::WaveAudioDescriptor* ADescObj);
127 Result_t MD_to_PCM_ADesc(ASDCP::MXF::WaveAudioDescriptor* ADescObj, PCM::AudioDescriptor& ADesc);
128 void AddDMScrypt(Partition& HeaderPart, SourcePackage& Package,
129 WriterInfo& Descr, const UL& WrappingUL, const Dictionary*& Dict);
132 class KLReader : public ASDCP::KLVPacket
134 ASDCP_NO_COPY_CONSTRUCT(KLReader);
135 byte_t m_KeyBuf[SMPTE_UL_LENGTH*2];
141 inline const byte_t* Key() { return m_KeyBuf; }
142 inline const ui64_t Length() { return m_ValueLength; }
143 inline const ui64_t KLLength() { return m_KLLength; }
145 Result_t ReadKLFromFile(Kumu::FileReader& Reader);
151 ASDCP_NO_COPY_CONSTRUCT(h__Reader);
155 const Dictionary* m_Dict;
156 Kumu::FileReader m_File;
157 OPAtomHeader m_HeaderPart;
158 Partition m_BodyPart;
159 OPAtomIndexFooter m_FooterPart;
160 ui64_t m_EssenceStart;
162 ASDCP::FrameBuffer m_CtFrameBuf;
163 Kumu::fpos_t m_LastPosition;
165 h__Reader(const Dictionary&);
166 virtual ~h__Reader();
169 Result_t OpenMXFRead(const char* filename);
170 Result_t InitMXFIndex();
172 // positions file before reading
173 Result_t ReadEKLVFrame(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
174 const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC);
176 // reads from current position
177 Result_t ReadEKLVPacket(ui32_t FrameNum, ui32_t SequenceNum, ASDCP::FrameBuffer& FrameBuf,
178 const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC);
183 // state machine for mxf writer
185 ST_BEGIN, // waiting for Open()
186 ST_INIT, // waiting for SetSourceStream()
187 ST_READY, // ready to write frames
188 ST_RUNNING, // one or more frames written
189 ST_FINAL, // index written, file closed
192 // implementation of h__WriterState class Goto_* methods
193 #define Goto_body(s1,s2) if ( m_State != (s1) ) \
194 return RESULT_STATE; \
200 ASDCP_NO_COPY_CONSTRUCT(h__WriterState);
203 WriterState_t m_State;
204 h__WriterState() : m_State(ST_BEGIN) {}
207 inline bool Test_BEGIN() { return m_State == ST_BEGIN; }
208 inline bool Test_INIT() { return m_State == ST_INIT; }
209 inline bool Test_READY() { return m_State == ST_READY;}
210 inline bool Test_RUNNING() { return m_State == ST_RUNNING; }
211 inline bool Test_FINAL() { return m_State == ST_FINAL; }
212 inline Result_t Goto_INIT() { Goto_body(ST_BEGIN, ST_INIT); }
213 inline Result_t Goto_READY() { Goto_body(ST_INIT, ST_READY); }
214 inline Result_t Goto_RUNNING() { Goto_body(ST_READY, ST_RUNNING); }
215 inline Result_t Goto_FINAL() { Goto_body(ST_RUNNING, ST_FINAL); }
218 typedef std::list<ui64_t*> DurationElementList_t;
223 ASDCP_NO_COPY_CONSTRUCT(h__Writer);
227 const Dictionary* m_Dict;
228 Kumu::FileWriter m_File;
230 OPAtomHeader m_HeaderPart;
231 Partition m_BodyPart;
232 OPAtomIndexFooter m_FooterPart;
233 ui64_t m_EssenceStart;
235 MaterialPackage* m_MaterialPackage;
236 SourcePackage* m_FilePackage;
238 FileDescriptor* m_EssenceDescriptor;
239 std::list<InterchangeObject*> m_EssenceSubDescriptorList;
241 ui32_t m_FramesWritten;
242 ui64_t m_StreamOffset;
243 ASDCP::FrameBuffer m_CtFrameBuf;
244 h__WriterState m_State;
246 DurationElementList_t m_DurationUpdateList;
248 h__Writer(const Dictionary&);
249 virtual ~h__Writer();
252 void AddSourceClip(const MXF::Rational& EditRate, ui32_t TCFrameRate,
253 const std::string& TrackName, const UL& EssenceUL,
254 const UL& DataDefinition, const std::string& PackageLabel);
255 void AddDMSegment(const MXF::Rational& EditRate, ui32_t TCFrameRate,
256 const std::string& TrackName, const UL& DataDefinition,
257 const std::string& PackageLabel);
258 void AddEssenceDescriptor(const UL& WrappingUL);
259 Result_t CreateBodyPart(const MXF::Rational& EditRate, ui32_t BytesPerEditUnit = 0);
261 // all the above for a single source clip
262 Result_t WriteMXFHeader(const std::string& PackageLabel, const UL& WrappingUL,
263 const std::string& TrackName, const UL& EssenceUL,
264 const UL& DataDefinition, const MXF::Rational& EditRate,
265 ui32_t TCFrameRate, ui32_t BytesPerEditUnit = 0);
267 Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,
268 const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC);
270 Result_t WriteMXFFooter();
275 // helper class for calculating Integrity Packs, used by WriteEKLVPacket() below.
280 byte_t Data[klv_intpack_size];
283 memset(Data, 0, klv_intpack_size);
288 Result_t CalcValues(const ASDCP::FrameBuffer&, byte_t* AssetID, ui32_t sequence, HMACContext* HMAC);
289 Result_t TestValues(const ASDCP::FrameBuffer&, byte_t* AssetID, ui32_t sequence, HMACContext* HMAC);
295 #endif // _AS_DCP_INTERNAL_H_
299 // end AS_DCP_internal.h