the big-pre-as-02-refactor
[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 h__AS02Reader : public ASDCP::MXF::TrackFileReader<ASDCP::MXF::OP1aHeader, AS_02::MXF::AS02IndexReader>
76     {
77       ASDCP_NO_COPY_CONSTRUCT(h__AS02Reader);
78       h__AS02Reader();
79
80     public:
81       Partition *m_pCurrentBodyPartition;
82       AS_02::MXF::OP1aIndexBodyPartion* m_pCurrentIndexPartition;
83       ui64_t     m_EssenceStart;
84       std::vector<Partition*> m_BodyPartList;
85       ui32_t     m_start_pos;
86       ui32_t     m_PartitionSpace;
87       IndexStrategy_t    m_IndexStrategy; //Shim parameter index_strategy_frame/clip
88
89       h__AS02Reader(const ASDCP::Dictionary&);
90       virtual ~h__AS02Reader();
91
92       Result_t OpenMXFRead(const char* filename);
93       Result_t ReadEKLVFrame(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
94                              const byte_t* EssenceUL, ASDCP::AESDecContext* Ctx, ASDCP::HMACContext* HMAC);
95       Result_t LocateFrame(ui32_t FrameNum, Kumu::fpos_t& streamOffset,
96                            i8_t& temporalOffset, i8_t& keyFrameOffset);
97
98     };
99
100   //
101   class h__AS02Writer : public ASDCP::MXF::TrackFileWriter<ASDCP::MXF::OP1aHeader>
102     {
103       ASDCP_NO_COPY_CONSTRUCT(h__AS02Writer);
104       h__AS02Writer();
105
106     public:
107       AS_02::MXF::OP1aIndexBodyPartion*  m_CurrentIndexBodyPartition;
108       ui64_t     m_BodyOffset;
109       ui32_t     m_PartitionSpace;
110       IndexStrategy_t    m_IndexStrategy; //Shim parameter index_strategy_frame/clip
111       std::vector<Partition*> m_BodyPartList;
112
113       h__AS02Writer(const Dictionary&);
114       virtual ~h__AS02Writer();
115
116       // all the above for a single source clip
117       Result_t WriteAS02Header(const std::string& PackageLabel, const ASDCP::UL& WrappingUL,
118                                const std::string& TrackName, const ASDCP::UL& EssenceUL,
119                                const ASDCP::UL& DataDefinition, const ASDCP::Rational& EditRate,
120                                ui32_t TCFrameRate, ui32_t BytesPerEditUnit = 0);
121
122       Result_t CreateBodyPart(const ASDCP::MXF::Rational& EditRate, ui32_t BytesPerEditUnit);
123       Result_t CreateBodyPartPair();
124       Result_t CompleteIndexBodyPart();
125
126       Result_t WriteAS02Footer();
127     };
128
129 } // namespace AS_02
130
131 #endif // _AS_02_INTERNAL_H_
132
133 //
134 // end AS_02_internal.h
135 //