checky
[asdcplib.git] / src / AS_02_internal.h
1 /*
2   Copyright (c) 2011-2012, Robert Scheler, Heiko Sparenberg Fraunhofer IIS, John Hurst
3   All rights reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
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.
15
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.
26 */ 
27 /*! \file    AS_02_internal.h
28   \version $Id: AS_02_internal.h ***       
29   \brief   AS-02 library, non-public common elements
30 */
31
32 #ifndef _AS_02_INTERNAL_H_
33 #define _AS_02_INTERNAL_H_
34
35 #include "KM_platform.h"
36 #include "KM_util.h"
37 #include "KM_log.h"
38 #include "Metadata.h"
39 #include "AS_DCP_internal.h"
40 #include "AS_02.h"
41
42 using Kumu::DefaultLogSink;
43
44 using namespace ASDCP;
45 using namespace ASDCP::MXF;
46
47 namespace AS_02
48 {
49   static void CalculateIndexPartitionSize(ui32_t& size,ui32_t numberOfIndexEntries)
50   {
51     if(numberOfIndexEntries){
52       //Partition::ArchiveSize(); HeaderSize = 124 bytes
53       //KLV-Item = 20 bytes
54       //ItemSize IndexEntry = 11 bytes
55       //number of IndexEntries - parameter
56       //IndexEntryArray = 12 bytes
57       //size for other Elements(PosTableCount etc.) = 108 bytes
58       //see Index.cpp ASDCP::MXF::IndexTableSegment::WriteToTLVSet(TLVWriter& TLVSet) how this is computed 
59       size = 124 + 20 + 11 * numberOfIndexEntries + 12 +108;
60       size += 20;//because maybe we must fill the last partition, minimum required space for KLV-Item
61     }
62     else{
63       //Partition HeaderSize = 124 bytes
64       //KLV-Item = 20 bytes
65       //244 for 2 IndexTableSegments
66       size = 124 + 20 + 244;
67     }
68   }
69
70   //
71   class h__Reader
72   {
73     ASDCP_NO_COPY_CONSTRUCT(h__Reader);
74     h__Reader();
75
76   public:
77     const Dictionary*  m_Dict;
78     Kumu::FileReader   m_File;
79     OPAtomHeader       m_HeaderPart;
80     //more than one Body-Partition use a list 
81     std::vector<Partition*> m_BodyPartList;
82     OPAtomIndexFooter  m_FooterPart;
83     ui64_t             m_EssenceStart;
84     WriterInfo         m_Info;
85     ASDCP::FrameBuffer m_CtFrameBuf;
86     Kumu::fpos_t       m_LastPosition;
87
88     IndexStrategy_t    m_IndexStrategy; //Shim parameter index_strategy_frame/clip
89     ui32_t             m_PartitionSpace;
90
91     ////new elements for AS-02
92     Partition*         m_pCurrentBodyPartition;
93     AS_02::MXF::OP1aIndexBodyPartion* m_pCurrentIndexPartition;
94     ui32_t             m_start_pos;
95
96     h__Reader(const Dictionary&);
97     virtual ~h__Reader();
98
99     Result_t InitInfo();
100     virtual Result_t OpenMXFRead(const char* filename) = 0;
101     Result_t InitMXFIndex();
102
103     // positions file before reading
104     virtual Result_t ReadEKLVFrame(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
105                                    const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC) = 0;
106
107     // reads from current position
108     virtual Result_t ReadEKLVPacket(ui32_t FrameNum, ui32_t SequenceNum, ASDCP::FrameBuffer& FrameBuf,
109                                     const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC) = 0;
110     void     Close();
111   };
112
113   //
114   class h__Writer
115   {
116     ASDCP_NO_COPY_CONSTRUCT(h__Writer);
117     h__Writer();
118
119   public:
120     const Dictionary*  m_Dict;
121     Kumu::FileWriter   m_File;
122     ui32_t             m_HeaderSize;
123     OPAtomHeader       m_HeaderPart;
124     //more than one Body-Partition -> use a list of Partitions
125     std::vector<Partition*> m_BodyPartList;
126     //we don't use the footer like in the dcps but we need also a footer
127     AS_02::MXF::OP1aIndexFooter    m_FooterPart;
128     ui64_t             m_EssenceStart;
129
130     MaterialPackage*   m_MaterialPackage;
131     SourcePackage*     m_FilePackage;
132
133     FileDescriptor*    m_EssenceDescriptor;
134     std::list<InterchangeObject*> m_EssenceSubDescriptorList;
135
136     ui32_t             m_FramesWritten;
137     ui64_t             m_StreamOffset;
138     ASDCP::FrameBuffer m_CtFrameBuf;
139     h__WriterState     m_State;
140     WriterInfo         m_Info;
141     DurationElementList_t m_DurationUpdateList;
142
143     //new elements for AS-02
144     ui64_t             m_BodyOffset;
145     //TODO: Currently not used, delete if not necessary
146     // Counter values for BodySID and IndexSID
147     ui32_t             m_CurrentBodySID;
148     ui32_t             m_CurrentIndexSID;
149     //TODO: maybe set this to the lf__Writer class because this is only in JP2K creation necessary
150     //our computed PartitionSpace
151     ui32_t             m_PartitionSpace;
152     IndexStrategy_t    m_IndexStrategy; //Shim parameter index_strategy_frame/clip
153
154     //the EditRate
155     ASDCP::MXF::Rational      m_EditRate;
156     //the BytesPerEditUnit
157     ui32_t                         m_BytesPerEditUnit;
158     //pointer to the current Body Partition(Index)
159     AS_02::MXF::OP1aIndexBodyPartion*  m_CurrentIndexBodyPartition;
160
161     h__Writer(const Dictionary&);
162     virtual ~h__Writer();
163
164     //virtual methods, implementation details for JP2K or PCM are in the MXFWriter::h__Writer classes if they are different
165     virtual void InitHeader();
166     /*virtual*/ void AddSourceClip(const ASDCP::MXF::Rational& EditRate, ui32_t TCFrameRate,
167                                    const std::string& TrackName, const UL& EssenceUL,
168                                    const UL& DataDefinition, const std::string& PackageLabel);
169     /*virtual*/ void AddDMSegment(const ASDCP::MXF::Rational& EditRate, ui32_t TCFrameRate,
170                                   const std::string& TrackName, const UL& DataDefinition,
171                                   const std::string& PackageLabel);
172     /*virtual*/ void AddEssenceDescriptor(const ASDCP::UL& WrappingUL);
173     virtual Result_t CreateBodyPart(const ASDCP::MXF::Rational& EditRate, ui32_t BytesPerEditUnit = 0);
174
175     //new method to create BodyPartition for essence and index
176     virtual Result_t CreateBodyPartPair();
177     //new method to finalize BodyPartion(index)
178     virtual Result_t CompleteIndexBodyPart();
179
180     // all the above for a single source clip
181     virtual Result_t WriteMXFHeader(const std::string& PackageLabel, const UL& WrappingUL,
182                                     const std::string& TrackName, const UL& EssenceUL,
183                                     const UL& DataDefinition, const ASDCP::MXF::Rational& EditRate,
184                                     ui32_t TCFrameRate, ui32_t BytesPerEditUnit = 0);
185
186     virtual Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,
187                                      const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC);
188
189     virtual Result_t WriteMXFFooter() = 0;
190
191   };
192
193 } // namespace AS_02
194
195 #endif // _AS_02_INTERNAL_H_
196
197 //
198 // end AS_02_internal.h
199 //