Reverting const accessor for class optional_property
[asdcplib.git] / src / Dict.cpp
1 /*
2 Copyright (c) 2006-2018, 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    Dict.cpp
28   \version $Id$
29   \brief   MXF dictionary
30 */
31
32
33 #include "KM_mutex.h"
34 #include "KM_log.h"
35 #include "KLV.h"
36 #include "MDD.cpp"
37
38 //------------------------------------------------------------------------------------------
39
40 // The composite dict is the union of the SMPTE and Interop dicts
41 //
42 static ASDCP::Dictionary s_CompositeDict;
43 static Kumu::Mutex s_CompositeDictLock;
44 static bool s_CompositeDictInit = false;
45
46 //
47 const ASDCP::Dictionary&
48 ASDCP::DefaultCompositeDict()
49 {
50   if ( ! s_CompositeDictInit )
51     {
52       Kumu::AutoMutex AL(s_CompositeDictLock);
53
54       if ( ! s_CompositeDictInit )
55         {
56           s_CompositeDict.Init();
57           s_CompositeDictInit = true;
58         }
59     }
60
61   return s_CompositeDict;
62 }
63
64 //
65 //
66 static ASDCP::Dictionary s_InteropDict;
67 static Kumu::Mutex s_InteropDictLock;
68 static bool s_InteropDictInit = false;
69
70 //
71 const ASDCP::Dictionary&
72 ASDCP::DefaultInteropDict()
73 {
74   if ( ! s_InteropDictInit )
75     {
76       Kumu::AutoMutex AL(s_InteropDictLock);
77
78       if ( ! s_InteropDictInit )
79         {
80           s_InteropDict.Init();
81
82           s_InteropDict.DeleteEntry(MDD_MXFInterop_OPAtom);
83           s_InteropDict.DeleteEntry(MDD_MXFInterop_CryptEssence);
84           s_InteropDict.DeleteEntry(MDD_MXFInterop_GenericDescriptor_SubDescriptors);
85
86           s_InteropDict.AddEntry(s_MDD_Table[MDD_MXFInterop_OPAtom], MDD_OPAtom);
87           s_InteropDict.AddEntry(s_MDD_Table[MDD_MXFInterop_CryptEssence], MDD_CryptEssence);
88           s_InteropDict.AddEntry(s_MDD_Table[MDD_MXFInterop_GenericDescriptor_SubDescriptors],
89                                      MDD_GenericDescriptor_SubDescriptors);
90
91           s_InteropDictInit = true;
92         }
93     }
94
95   return s_InteropDict;
96 }
97
98 //
99 //
100 static ASDCP::Dictionary s_SMPTEDict;
101 static Kumu::Mutex s_SMPTEDictLock;
102 static bool s_SMPTEDictInit = false;
103
104 //
105 const ASDCP::Dictionary&
106 ASDCP::DefaultSMPTEDict()
107 {
108   if ( ! s_SMPTEDictInit )
109     {
110       Kumu::AutoMutex AL(s_SMPTEDictLock);
111
112       if ( ! s_SMPTEDictInit )
113         {
114           s_SMPTEDict.Init();
115
116           s_SMPTEDict.DeleteEntry(MDD_MXFInterop_OPAtom);
117           s_SMPTEDict.DeleteEntry(MDD_MXFInterop_CryptEssence);
118           s_SMPTEDict.DeleteEntry(MDD_MXFInterop_GenericDescriptor_SubDescriptors);
119
120           s_SMPTEDictInit = true;
121         }
122     }
123
124   return s_SMPTEDict;
125 }
126
127 //
128 const ASDCP::MDDEntry&
129 ASDCP::MXFInterop_OPAtom_Entry() {
130   return s_MDD_Table[MDD_MXFInterop_OPAtom];
131 }
132
133 //
134 const ASDCP::MDDEntry&
135 ASDCP::SMPTE_390_OPAtom_Entry() {
136   return s_MDD_Table[MDD_OPAtom];
137 }
138
139 //
140 //
141 static ASDCP::Dictionary s_AtmosSMPTEDict;
142 static Kumu::Mutex s_AtmosSMPTEDictLock;
143 static bool s_AtmosSMPTEDictInit = false;
144
145 //
146 const ASDCP::Dictionary&
147 ASDCP::AtmosSMPTEDict()
148 {
149   if ( ! s_AtmosSMPTEDictInit )
150     {
151       Kumu::AutoMutex AL(s_AtmosSMPTEDictLock);
152
153       if ( ! s_AtmosSMPTEDictInit )
154         {
155           s_AtmosSMPTEDict.Init();
156
157           s_AtmosSMPTEDict.DeleteEntry(MDD_MXFInterop_OPAtom);
158           s_AtmosSMPTEDict.DeleteEntry(MDD_MXFInterop_CryptEssence);
159           s_AtmosSMPTEDict.DeleteEntry(MDD_MXFInterop_GenericDescriptor_SubDescriptors);
160
161           // legacy Atmos files have the wrong version byte
162           assert(s_AtmosSMPTEDict.Type(MDD_GenericDataEssenceDescriptor_DataEssenceCoding).ul[7] == 0x03);
163           s_AtmosSMPTEDict.MutableType(MDD_GenericDataEssenceDescriptor_DataEssenceCoding).ul[7] = 0x05;
164           
165           s_AtmosSMPTEDictInit = true;
166         }
167     }
168
169   return s_AtmosSMPTEDict;
170 }
171
172 //------------------------------------------------------------------------------------------
173 //
174
175 ASDCP::Dictionary::Dictionary() {}
176 ASDCP::Dictionary::~Dictionary() {}
177
178 //
179 void
180 ASDCP::Dictionary::Init()
181 {
182   m_md_lookup.clear();
183   memset(m_MDD_Table, 0, sizeof(m_MDD_Table));
184
185   for ( ui32_t x = 0; x < (ui32_t)ASDCP::MDD_Max; ++x )
186     {
187       if ( x == MDD_PartitionMetadata_IndexSID_DEPRECATED  // 30
188            || x == MDD_PartitionMetadata_BodySID_DEPRECATED  // 32
189            || x == MDD_PartitionMetadata_OperationalPattern_DEPRECATED  // 33
190            || x == MDD_PartitionMetadata_EssenceContainers_DEPRECATED  // 34
191            || x == MDD_IndexTableSegmentBase_IndexSID_DEPRECATED  // 56
192            || x == MDD_IndexTableSegmentBase_BodySID_DEPRECATED  // 57
193            || x == MDD_PartitionArray_RandomIndexMetadata_BodySID_DEPRECATED  // 73
194            || x == MDD_Preface_OperationalPattern_DEPRECATED  // 84
195            || x == MDD_Preface_EssenceContainers_DEPRECATED  // 85
196            || x == MDD_EssenceContainerData_IndexSID_DEPRECATED  // 103
197            || x == MDD_EssenceContainerData_BodySID_DEPRECATED  // 104
198            || x == MDD_TimedTextResourceSubDescriptor_EssenceStreamID_DEPRECATED // 264
199            || x == MDD_DMSegment_DataDefinition_DEPRECATED // 266
200            || x == MDD_DMSegment_Duration_DEPRECATED // 267
201            )
202         continue;
203
204       AddEntry(s_MDD_Table[x], x);
205     }
206 }
207
208 //
209 bool
210 ASDCP::Dictionary::AddEntry(const MDDEntry& Entry, ui32_t index)
211 {
212   if ( index >= (ui32_t)MDD_Max )
213     {
214       Kumu::DefaultLogSink().Warn("UL Dictionary: index exceeds maximum: %d\n", index);
215       return false;
216     }
217
218   bool result = true;
219   // is this index already there?
220   std::map<ui32_t, ASDCP::UL>::iterator rii = m_md_rev_lookup.find(index);
221
222   if ( rii != m_md_rev_lookup.end() )
223     {
224       DeleteEntry(index);
225       result = false;
226     }
227
228   UL TmpUL(Entry.ul);
229
230 #define MDD_AUTHORING_MODE
231 #ifdef MDD_AUTHORING_MODE
232   char buf[64];
233   std::map<ASDCP::UL, ui32_t>::iterator ii = m_md_lookup.find(TmpUL);
234   if ( ii != m_md_lookup.end() )
235     {
236       Kumu::DefaultLogSink().Warn("Duplicate Dictionary item: %s (%02x, %02x) %s | (%02x, %02x) %s\n",
237               TmpUL.EncodeString(buf, 64),
238               m_MDD_Table[ii->second].tag.a, m_MDD_Table[ii->second].tag.b,
239               m_MDD_Table[ii->second].name,
240               Entry.tag.a, Entry.tag.b, Entry.name);
241     }
242 #endif
243
244   m_md_lookup.insert(std::map<UL, ui32_t>::value_type(TmpUL, index));
245   m_md_rev_lookup.insert(std::map<ui32_t, UL>::value_type(index, TmpUL));
246   m_md_sym_lookup.insert(std::map<std::string, ui32_t>::value_type(Entry.name, index));
247   m_MDD_Table[index] = Entry;
248
249   return result;
250 }
251
252 //
253 bool
254 ASDCP::Dictionary::DeleteEntry(ui32_t index)
255 {
256   std::map<ui32_t, ASDCP::UL>::iterator rii = m_md_rev_lookup.find(index);
257   if ( rii != m_md_rev_lookup.end() )
258     {
259       std::map<ASDCP::UL, ui32_t>::iterator ii = m_md_lookup.find(rii->second);
260       assert(ii != m_md_lookup.end());
261
262       MDDEntry NilEntry;
263       memset(&NilEntry, 0, sizeof(NilEntry));
264
265       m_md_lookup.erase(ii);
266       m_md_rev_lookup.erase(rii);
267       m_MDD_Table[index] = NilEntry;
268       return true;
269     }
270
271   return false;
272 }
273
274 //
275 const ASDCP::MDDEntry&
276 ASDCP::Dictionary::Type(MDD_t type_id) const
277 {
278   assert(m_MDD_Table[0].name[0]);
279   std::map<ui32_t, ASDCP::UL>::const_iterator rii = m_md_rev_lookup.find(type_id);
280
281   if ( rii == m_md_rev_lookup.end() )
282     Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL type_id: %d\n", type_id);
283
284   return m_MDD_Table[type_id];
285 }
286
287 //
288 ASDCP::MDDEntry&
289 ASDCP::Dictionary::MutableType(MDD_t type_id)
290 {
291   assert(m_MDD_Table[0].name[0]);
292   std::map<ui32_t, ASDCP::UL>::iterator rii = m_md_rev_lookup.find(type_id);
293
294   if ( rii == m_md_rev_lookup.end() )
295     Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL type_id: %d\n", type_id);
296
297   return m_MDD_Table[type_id];
298 }
299
300 //
301 const ASDCP::MDDEntry*
302 ASDCP::Dictionary::FindULAnyVersion(const byte_t* ul_buf) const
303 {
304   assert(m_MDD_Table[0].name[0]);
305   byte_t search_ul[SMPTE_UL_LENGTH];
306   memcpy(search_ul, ul_buf, SMPTE_UL_LENGTH);
307   memset(search_ul+7, 0, SMPTE_UL_LENGTH-7);
308
309   UL target(ul_buf);
310   const ASDCP::MDDEntry *found_entry = 0;
311
312   std::map<UL, ui32_t>::const_iterator lower = m_md_lookup.lower_bound(UL(search_ul));
313
314   for ( ; lower != m_md_lookup.end(); ++lower )
315     {
316       if ( lower->first.MatchExact(target) )
317         {
318           found_entry = &m_MDD_Table[lower->second];
319           break;
320         }
321       else if ( found_entry == 0 && lower->first.MatchIgnoreStream(target) )
322         {
323           found_entry = &m_MDD_Table[lower->second];
324         }
325       else if ( found_entry != 0 && ! lower->first.MatchIgnoreStream(target) )
326         {
327           break;
328         }
329     }
330
331   if ( found_entry == 0 )
332     {
333       char buf[64];
334       Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL: %s\n", target.EncodeString(buf, 64));
335     }
336
337   return found_entry;
338 }
339
340 //
341 const ASDCP::MDDEntry*
342 ASDCP::Dictionary::FindULExact(const byte_t* ul_buf) const
343 {
344   assert(m_MDD_Table[0].name[0]);
345   std::map<UL, ui32_t>::const_iterator i = m_md_lookup.find(UL(ul_buf));
346   
347   if ( i == m_md_lookup.end() )
348     {
349       char buf[64];
350       UL tmp_ul(ul_buf);
351       Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL: %s\n", tmp_ul.EncodeString(buf, 64));
352       return 0;
353     }
354
355   return &m_MDD_Table[i->second];
356 }
357
358 //
359 const ASDCP::MDDEntry*
360 ASDCP::Dictionary::FindSymbol(const std::string& str) const
361 {
362   assert(m_MDD_Table[0].name[0]);
363   std::map<std::string, ui32_t>::const_iterator i = m_md_sym_lookup.find(str);
364   
365   if ( i == m_md_sym_lookup.end() )
366     {
367       Kumu::DefaultLogSink().Warn("UL Dictionary: unknown symbol: %s\n", str.c_str());
368       return 0;
369     }
370
371   return &m_MDD_Table[i->second];
372 }
373
374 //
375 void
376 ASDCP::Dictionary::Dump(FILE* stream) const
377 {
378   if ( stream == 0 )
379     stream = stderr;
380
381   MDD_t di = (MDD_t)0;
382   char     str_buf[64];
383
384   while ( di < MDD_Max )
385     {
386       if ( m_MDD_Table[di].name != 0 )
387         {
388           UL TmpUL(m_MDD_Table[di].ul);
389           fprintf(stream, "%s: %s\n", TmpUL.EncodeString(str_buf, 64), m_MDD_Table[di].name);
390         }
391
392       di = (MDD_t)(di + 1);
393     }
394 }
395
396 //
397 // end Dict.cpp
398 //