fixing reserved symbol conflict with AS_02_USE (USE_AS_02)
[asdcplib.git] / src / AS_02_internal.h
1 /*
2   Copyright (c) 2011-2013, 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_log.h"
36 #include "AS_DCP_internal.h"
37 #include "AS_02.h"
38
39 using Kumu::DefaultLogSink;
40
41 #ifdef DEFAULT_MD_DECL
42 AS_02::MXF::AS02IndexReader *g_AS02IndexReader;
43 #else
44 extern AS_02::MXF::AS02IndexReader *g_AS02IndexReader;
45 #endif
46
47
48 namespace AS_02
49 {
50
51   void default_md_object_init();
52
53   static void CalculateIndexPartitionSize(ui32_t& size,ui32_t numberOfIndexEntries)
54   {
55     if(numberOfIndexEntries){
56       //Partition::ArchiveSize(); HeaderSize = 124 bytes
57       //KLV-Item = 20 bytes
58       //ItemSize IndexEntry = 11 bytes
59       //number of IndexEntries - parameter
60       //IndexEntryArray = 12 bytes
61       //size for other Elements(PosTableCount etc.) = 108 bytes
62       //see Index.cpp ASDCP::MXF::IndexTableSegment::WriteToTLVSet(TLVWriter& TLVSet) how this is computed 
63       size = 124 + 20 + 11 * numberOfIndexEntries + 12 +108;
64       size += 20;//because maybe we must fill the last partition, minimum required space for KLV-Item
65     }
66     else{
67       //Partition HeaderSize = 124 bytes
68       //KLV-Item = 20 bytes
69       //244 for 2 IndexTableSegments
70       size = 124 + 20 + 244;
71     }
72   }
73
74   //
75   class AS02IndexWriter : public ASDCP::MXF::Partition
76     {
77       ASDCP::MXF::IndexTableSegment*  m_CurrentSegment;
78       ui32_t  m_BytesPerEditUnit;
79       ASDCP::MXF::Rational m_EditRate;
80
81       KM_NO_COPY_CONSTRUCT(AS02IndexWriter);
82       AS02IndexWriter();
83
84     public:
85       const ASDCP::Dictionary*&  m_Dict;
86       Kumu::fpos_t        m_ECOffset;
87       ASDCP::IPrimerLookup*      m_Lookup;
88       
89       AS02IndexWriter(const ASDCP::Dictionary*&);
90       virtual ~AS02IndexWriter();
91
92       Result_t WriteToFile(Kumu::FileWriter& Writer);
93       void     ResetCBR(Kumu::fpos_t offset);
94       void     Dump(FILE* = 0);
95
96       ui32_t GetDuration() const;
97       void PushIndexEntry(const ASDCP::MXF::IndexTableSegment::IndexEntry&);
98       void SetIndexParamsCBR(ASDCP::IPrimerLookup* lookup, ui32_t size, const ASDCP::Rational& Rate);
99       void SetIndexParamsVBR(ASDCP::IPrimerLookup* lookup, const ASDCP::Rational& Rate, Kumu::fpos_t offset);
100     };
101
102   //
103   class h__AS02Reader : public ASDCP::MXF::TrackFileReader<ASDCP::MXF::OP1aHeader, AS_02::MXF::AS02IndexReader>
104     {
105       ASDCP_NO_COPY_CONSTRUCT(h__AS02Reader);
106       h__AS02Reader();
107
108     public:
109       //      Partition *m_pCurrentBodyPartition;
110       //      ui64_t     m_EssenceStart;
111       //      std::vector<Partition*> m_BodyPartList;
112       //      ui32_t     m_start_pos;
113       //      ui32_t     m_PartitionSpace;
114       //      IndexStrategy_t    m_IndexStrategy; //Shim parameter index_strategy_frame/clip
115
116       h__AS02Reader(const ASDCP::Dictionary&);
117       virtual ~h__AS02Reader();
118
119       Result_t OpenMXFRead(const char* filename);
120
121       Result_t ReadEKLVFrame(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
122                              const byte_t* EssenceUL, ASDCP::AESDecContext* Ctx, ASDCP::HMACContext* HMAC);
123
124       Result_t LocateFrame(ui32_t FrameNum, Kumu::fpos_t& streamOffset, i8_t& temporalOffset, i8_t& keyFrameOffset);
125     };
126
127   //
128   class h__AS02Writer : public ASDCP::MXF::TrackFileWriter<ASDCP::MXF::OP1aHeader>
129     {
130       ASDCP_NO_COPY_CONSTRUCT(h__AS02Writer);
131       h__AS02Writer();
132
133     public:
134       AS02IndexWriter m_IndexWriter;
135       ui32_t     m_PartitionSpace;  // edit units per partition
136       IndexStrategy_t    m_IndexStrategy; //Shim parameter index_strategy_frame/clip
137
138       h__AS02Writer(const Dictionary&);
139       virtual ~h__AS02Writer();
140
141       // all the above for a single source clip
142       Result_t WriteAS02Header(const std::string& PackageLabel, const ASDCP::UL& WrappingUL,
143                                const std::string& TrackName, const ASDCP::UL& EssenceUL,
144                                const ASDCP::UL& DataDefinition, const ASDCP::Rational& EditRate,
145                                ui32_t TCFrameRate, ui32_t BytesPerEditUnit = 0);
146
147       Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,const byte_t* EssenceUL,
148                                AESEncContext* Ctx, HMACContext* HMAC);
149
150       Result_t WriteAS02Footer();
151     };
152
153 } // namespace AS_02
154
155 #endif // _AS_02_INTERNAL_H_
156
157 //
158 // end AS_02_internal.h
159 //