2 Copyright (c) 2006-2016, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
38 //------------------------------------------------------------------------------------------
40 // The composite dict is the union of the SMPTE and Interop dicts
42 static ASDCP::Dictionary s_CompositeDict;
43 static Kumu::Mutex s_CompositeDictLock;
44 static bool s_CompositeDictInit = false;
47 const ASDCP::Dictionary&
48 ASDCP::DefaultCompositeDict()
50 if ( ! s_CompositeDictInit )
52 Kumu::AutoMutex AL(s_CompositeDictLock);
54 if ( ! s_CompositeDictInit )
56 s_CompositeDict.Init();
57 s_CompositeDictInit = true;
61 return s_CompositeDict;
66 static ASDCP::Dictionary s_InteropDict;
67 static Kumu::Mutex s_InteropDictLock;
68 static bool s_InteropDictInit = false;
71 const ASDCP::Dictionary&
72 ASDCP::DefaultInteropDict()
74 if ( ! s_InteropDictInit )
76 Kumu::AutoMutex AL(s_InteropDictLock);
78 if ( ! s_InteropDictInit )
82 s_InteropDict.DeleteEntry(MDD_MXFInterop_OPAtom);
83 s_InteropDict.DeleteEntry(MDD_MXFInterop_CryptEssence);
84 s_InteropDict.DeleteEntry(MDD_MXFInterop_GenericDescriptor_SubDescriptors);
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);
91 s_InteropDictInit = true;
100 static ASDCP::Dictionary s_SMPTEDict;
101 static Kumu::Mutex s_SMPTEDictLock;
102 static bool s_SMPTEDictInit = false;
105 const ASDCP::Dictionary&
106 ASDCP::DefaultSMPTEDict()
108 if ( ! s_SMPTEDictInit )
110 Kumu::AutoMutex AL(s_SMPTEDictLock);
112 if ( ! s_SMPTEDictInit )
116 s_SMPTEDict.DeleteEntry(MDD_MXFInterop_OPAtom);
117 s_SMPTEDict.DeleteEntry(MDD_MXFInterop_CryptEssence);
118 s_SMPTEDict.DeleteEntry(MDD_MXFInterop_GenericDescriptor_SubDescriptors);
120 s_SMPTEDictInit = true;
128 const ASDCP::MDDEntry&
129 ASDCP::MXFInterop_OPAtom_Entry() {
130 return s_MDD_Table[MDD_MXFInterop_OPAtom];
134 const ASDCP::MDDEntry&
135 ASDCP::SMPTE_390_OPAtom_Entry() {
136 return s_MDD_Table[MDD_OPAtom];
140 //------------------------------------------------------------------------------------------
143 ASDCP::Dictionary::Dictionary() {}
144 ASDCP::Dictionary::~Dictionary() {}
148 ASDCP::Dictionary::Init()
151 memset(m_MDD_Table, 0, sizeof(m_MDD_Table));
153 for ( ui32_t x = 0; x < (ui32_t)ASDCP::MDD_Max; ++x )
155 if ( x == MDD_PartitionMetadata_IndexSID_DEPRECATED // 30
156 || x == MDD_PartitionMetadata_BodySID_DEPRECATED // 32
157 || x == MDD_PartitionMetadata_OperationalPattern_DEPRECATED // 33
158 || x == MDD_PartitionMetadata_EssenceContainers_DEPRECATED // 34
159 || x == MDD_IndexTableSegmentBase_IndexSID_DEPRECATED // 56
160 || x == MDD_IndexTableSegmentBase_BodySID_DEPRECATED // 57
161 || x == MDD_PartitionArray_RandomIndexMetadata_BodySID_DEPRECATED // 73
162 || x == MDD_Preface_OperationalPattern_DEPRECATED // 84
163 || x == MDD_Preface_EssenceContainers_DEPRECATED // 85
164 || x == MDD_EssenceContainerData_IndexSID_DEPRECATED // 103
165 || x == MDD_EssenceContainerData_BodySID_DEPRECATED // 104
166 || x == MDD_TimedTextResourceSubDescriptor_EssenceStreamID_DEPRECATED // 264
167 || x == MDD_DMSegment_DataDefinition_DEPRECATED // 266
168 || x == MDD_DMSegment_Duration_DEPRECATED // 267
172 AddEntry(s_MDD_Table[x], x);
178 ASDCP::Dictionary::AddEntry(const MDDEntry& Entry, ui32_t index)
180 if ( index >= (ui32_t)MDD_Max )
182 Kumu::DefaultLogSink().Warn("UL Dictionary: index exceeds maximum: %d\n", index);
187 // is this index already there?
188 std::map<ui32_t, ASDCP::UL>::iterator rii = m_md_rev_lookup.find(index);
190 if ( rii != m_md_rev_lookup.end() )
198 #define MDD_AUTHORING_MODE
199 #ifdef MDD_AUTHORING_MODE
201 std::map<ASDCP::UL, ui32_t>::iterator ii = m_md_lookup.find(TmpUL);
202 if ( ii != m_md_lookup.end() )
204 Kumu::DefaultLogSink().Warn("Duplicate Dictionary item: %s (%02x, %02x) %s | (%02x, %02x) %s\n",
205 TmpUL.EncodeString(buf, 64),
206 m_MDD_Table[ii->second].tag.a, m_MDD_Table[ii->second].tag.b,
207 m_MDD_Table[ii->second].name,
208 Entry.tag.a, Entry.tag.b, Entry.name);
212 m_md_lookup.insert(std::map<UL, ui32_t>::value_type(TmpUL, index));
213 m_md_rev_lookup.insert(std::map<ui32_t, UL>::value_type(index, TmpUL));
214 m_md_sym_lookup.insert(std::map<std::string, ui32_t>::value_type(Entry.name, index));
215 m_MDD_Table[index] = Entry;
222 ASDCP::Dictionary::DeleteEntry(ui32_t index)
224 std::map<ui32_t, ASDCP::UL>::iterator rii = m_md_rev_lookup.find(index);
225 if ( rii != m_md_rev_lookup.end() )
227 std::map<ASDCP::UL, ui32_t>::iterator ii = m_md_lookup.find(rii->second);
228 assert(ii != m_md_lookup.end());
231 memset(&NilEntry, 0, sizeof(NilEntry));
233 m_md_lookup.erase(ii);
234 m_md_rev_lookup.erase(rii);
235 m_MDD_Table[index] = NilEntry;
243 const ASDCP::MDDEntry&
244 ASDCP::Dictionary::Type(MDD_t type_id) const
246 assert(m_MDD_Table[0].name[0]);
247 std::map<ui32_t, ASDCP::UL>::const_iterator rii = m_md_rev_lookup.find(type_id);
249 if ( rii == m_md_rev_lookup.end() )
250 Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL type_id: %d\n", type_id);
252 return m_MDD_Table[type_id];
256 const ASDCP::MDDEntry*
257 ASDCP::Dictionary::FindULAnyVersion(const byte_t* ul_buf) const
259 assert(m_MDD_Table[0].name[0]);
260 byte_t search_ul[SMPTE_UL_LENGTH];
261 memcpy(search_ul, ul_buf, SMPTE_UL_LENGTH);
262 memset(search_ul+7, 0, SMPTE_UL_LENGTH-7);
265 const ASDCP::MDDEntry *found_entry = 0;
267 std::map<UL, ui32_t>::const_iterator lower = m_md_lookup.lower_bound(UL(search_ul));
269 for ( ; lower != m_md_lookup.end(); ++lower )
271 if ( lower->first.MatchExact(target) )
273 found_entry = &m_MDD_Table[lower->second];
276 else if ( found_entry == 0 && lower->first.MatchIgnoreStream(target) )
278 found_entry = &m_MDD_Table[lower->second];
280 else if ( found_entry != 0 && ! lower->first.MatchIgnoreStream(target) )
286 if ( found_entry == 0 )
289 Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL: %s\n", target.EncodeString(buf, 64));
296 const ASDCP::MDDEntry*
297 ASDCP::Dictionary::FindULExact(const byte_t* ul_buf) const
299 assert(m_MDD_Table[0].name[0]);
300 std::map<UL, ui32_t>::const_iterator i = m_md_lookup.find(UL(ul_buf));
302 if ( i == m_md_lookup.end() )
306 Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL: %s\n", tmp_ul.EncodeString(buf, 64));
310 return &m_MDD_Table[i->second];
314 const ASDCP::MDDEntry*
315 ASDCP::Dictionary::FindSymbol(const std::string& str) const
317 assert(m_MDD_Table[0].name[0]);
318 std::map<std::string, ui32_t>::const_iterator i = m_md_sym_lookup.find(str);
320 if ( i == m_md_sym_lookup.end() )
322 Kumu::DefaultLogSink().Warn("UL Dictionary: unknown symbol: %s\n", str.c_str());
326 return &m_MDD_Table[i->second];
331 ASDCP::Dictionary::Dump(FILE* stream) const
339 while ( di < MDD_Max )
341 if ( m_MDD_Table[di].name != 0 )
343 UL TmpUL(m_MDD_Table[di].ul);
344 fprintf(stream, "%s: %s\n", TmpUL.EncodeString(str_buf, 64), m_MDD_Table[di].name);
347 di = (MDD_t)(di + 1);