metadata reformed...
[asdcplib.git] / src / Index.cpp
1 /*
2 Copyright (c) 2005-2006, 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    Index.cpp
28     \version $Id$
29     \brief   MXF index segment objects
30 */
31
32 #include "MXF.h"
33 const ui32_t kl_length = ASDCP::SMPTE_UL_LENGTH + ASDCP::MXF_BER_LENGTH;
34
35
36 //
37 ASDCP::MXF::IndexTableSegment::IndexTableSegment() :
38   IndexStartPosition(0), IndexDuration(0), EditUnitByteCount(0),
39   IndexSID(129), BodySID(1), SliceCount(0), PosTableCount(0)
40 {
41 }
42
43 //
44 ASDCP::MXF::IndexTableSegment::~IndexTableSegment()
45 {
46 }
47
48 //
49 ASDCP::Result_t
50 ASDCP::MXF::IndexTableSegment::InitFromTLVSet(TLVReader& TLVSet)
51 {
52   Result_t result = InterchangeObject::InitFromTLVSet(TLVSet);
53   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(IndexTableSegmentBase, IndexEditRate));
54   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi64(OBJ_READ_ARGS(IndexTableSegmentBase, IndexStartPosition));
55   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi64(OBJ_READ_ARGS(IndexTableSegmentBase, IndexDuration));
56   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS(IndexTableSegmentBase, EditUnitByteCount));
57   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS(IndexTableSegmentBase, IndexSID));
58   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS(IndexTableSegmentBase, BodySID));
59   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi8(OBJ_READ_ARGS(IndexTableSegmentBase, SliceCount));
60   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi8(OBJ_READ_ARGS(IndexTableSegmentBase, PosTableCount));
61   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(IndexTableSegment, DeltaEntryArray));
62   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(IndexTableSegment, IndexEntryArray));
63
64 #if 0
65   if ( ASDCP_SUCCESS(result) )
66     {
67       Batch<IndexEntry>::iterator i;
68       ui32_t offset = 0;
69       for ( i = IndexEntryArray.begin(); i != IndexEntryArray.end(); i++ )
70         {
71           if ( (*i).Flags == 0x40 )
72             offset = 0;
73
74           (*i).KeyFrameOffset = offset++;
75         }
76     }
77 #endif
78
79   return result;
80 }
81
82 //
83 ASDCP::Result_t
84 ASDCP::MXF::IndexTableSegment::WriteToTLVSet(TLVWriter& TLVSet)
85 {
86   Result_t result = InterchangeObject::WriteToTLVSet(TLVSet);
87   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(IndexTableSegmentBase, IndexEditRate));
88   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi64(OBJ_WRITE_ARGS(IndexTableSegmentBase, IndexStartPosition));
89   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi64(OBJ_WRITE_ARGS(IndexTableSegmentBase, IndexDuration));
90   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS(IndexTableSegmentBase, EditUnitByteCount));
91   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS(IndexTableSegmentBase, IndexSID));
92   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS(IndexTableSegmentBase, BodySID));
93   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi8(OBJ_WRITE_ARGS(IndexTableSegmentBase, SliceCount));
94   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi8(OBJ_WRITE_ARGS(IndexTableSegmentBase, PosTableCount));
95   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(IndexTableSegment, DeltaEntryArray));
96   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(IndexTableSegment, IndexEntryArray));
97   return result;
98 }
99
100 //
101 ASDCP::Result_t
102 ASDCP::MXF::IndexTableSegment::InitFromBuffer(const byte_t* p, ui32_t l)
103 {
104   m_Typeinfo = &Dict::Type(MDD_IndexTableSegment);
105   return InterchangeObject::InitFromBuffer(p, l);
106 }
107
108 //
109 ASDCP::Result_t
110 ASDCP::MXF::IndexTableSegment::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
111 {
112   m_Typeinfo = &Dict::Type(MDD_IndexTableSegment);
113   return InterchangeObject::WriteToBuffer(Buffer);
114 }
115
116 //
117 void
118 ASDCP::MXF::IndexTableSegment::Dump(FILE* stream)
119 {
120   char identbuf[IdentBufferLen];
121
122   if ( stream == 0 )
123     stream = stderr;
124
125   InterchangeObject::Dump(stream);
126   fprintf(stream, "  IndexEditRate      = %s\n",  IndexEditRate.ToString(identbuf));
127   fprintf(stream, "  IndexStartPosition = %s\n",  i64sz(IndexStartPosition, identbuf));
128   fprintf(stream, "  IndexDuration      = %s\n",  i64sz(IndexDuration, identbuf));
129   fprintf(stream, "  EditUnitByteCount  = %lu\n", EditUnitByteCount);
130   fprintf(stream, "  IndexSID           = %lu\n", IndexSID);
131   fprintf(stream, "  BodySID            = %lu\n", BodySID);
132   fprintf(stream, "  SliceCount         = %hu\n", SliceCount);
133   fprintf(stream, "  PosTableCount      = %hu\n", PosTableCount);
134
135   fprintf(stream, "  DeltaEntryArray:\n");  DeltaEntryArray.Dump(stream);
136
137   if ( IndexEntryArray.size() < 100 )
138     {
139       fprintf(stream, "  IndexEntryArray:\n");
140       IndexEntryArray.Dump(stream);
141     }
142   else
143     {
144       fprintf(stream, "  IndexEntryArray: %lu entries\n", IndexEntryArray.size());
145     }
146 }
147
148 //------------------------------------------------------------------------------------------
149 //
150
151 //
152 const char*
153 ASDCP::MXF::IndexTableSegment::DeltaEntry::ToString(char* str_buf) const
154 {
155   snprintf(str_buf, IdentBufferLen, "%3i %-3hu %-3lu", PosTableIndex, Slice, ElementData);
156   return str_buf;
157 }
158
159 //
160 ASDCP::Result_t
161 ASDCP::MXF::IndexTableSegment::DeltaEntry::Unarchive(ASDCP::MemIOReader& Reader)
162 {
163   Result_t result = Reader.ReadUi8((ui8_t*)&PosTableIndex);
164   if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi8(&Slice);
165   if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi32BE(&ElementData);
166   return result;
167 }
168
169 //
170 ASDCP::Result_t
171 ASDCP::MXF::IndexTableSegment::DeltaEntry::Archive(ASDCP::MemIOWriter& Writer)
172 {
173   Result_t result = Writer.WriteUi8((ui8_t)PosTableIndex);
174   if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi8(Slice);
175   if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi32BE(ElementData);
176   return result;
177 }
178
179 //------------------------------------------------------------------------------------------
180 //
181
182 // Flags:
183 // Bit 7: Random Access
184 // Bit 6: Sequence Header
185 // Bit 5: forward prediction flag
186 // Bit 4: backward prediction flag 
187 //   e.g.
188 //   00== I frame (no prediction)
189 //   10== P frame(forward prediction from previous  frame)
190 //   01== B frame (backward prediction from future  frame)
191 //   11== B frame (forward & backward prediction)
192 // Bits 0-3: reserved  [RP210 Flags to indicate coding of elements in this  edit unit]
193
194 //
195 const char*
196 ASDCP::MXF::IndexTableSegment::IndexEntry::ToString(char* str_buf) const
197 {
198   char intbuf[IntBufferLen];
199   char txt_flags[6];
200
201   txt_flags[0] = ( (Flags & 0x80) != 0 ) ? 'r' : ' ';
202   txt_flags[1] = ( (Flags & 0x40) != 0 ) ? 's' : ' ';
203   txt_flags[2] = ( (Flags & 0x20) != 0 ) ? 'f' : ' ';
204   txt_flags[3] = ( (Flags & 0x10) != 0 ) ? 'b' : ' ';
205   txt_flags[4] = ( (Flags & 0x0f) == 3 ) ? 'B' : ( (Flags & 0x0f) == 2 ) ? 'P' : 'I';
206   txt_flags[5] = 0;
207
208   snprintf(str_buf, IdentBufferLen, "%3i %-3hu %s %s",
209           TemporalOffset, KeyFrameOffset, txt_flags,
210           i64sz(StreamOffset, intbuf));
211
212   return str_buf;
213 }
214
215 //
216 ASDCP::Result_t
217 ASDCP::MXF::IndexTableSegment::IndexEntry::Unarchive(ASDCP::MemIOReader& Reader)
218 {
219   Result_t result = Reader.ReadUi8((ui8_t*)&TemporalOffset);
220   if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi8((ui8_t*)&KeyFrameOffset);
221   if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi8(&Flags);
222   if ( ASDCP_SUCCESS(result) ) result = Reader.ReadUi64BE(&StreamOffset);
223   return result;
224 }
225
226 //
227 ASDCP::Result_t
228 ASDCP::MXF::IndexTableSegment::IndexEntry::Archive(ASDCP::MemIOWriter& Writer)
229 {
230   Result_t result = Writer.WriteUi8((ui8_t)TemporalOffset);
231   if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi8((ui8_t)KeyFrameOffset);
232   if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi8(Flags);
233   if ( ASDCP_SUCCESS(result) ) result = Writer.WriteUi64BE(StreamOffset);
234   return result;
235 }
236
237
238 //
239 // end Index.cpp
240 //
241