summaryrefslogtreecommitdiff
path: root/asdcplib/src/Dict.cpp
blob: c046aa7c15f7191fb84083c4abeb856803bdf742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Copyright (c) 2006-2009, John Hurst
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file    Dict.cpp
  \version $Id: Dict.cpp,v 1.15 2012/02/08 02:59:21 jhurst Exp $
  \brief   MXF dictionary
*/


#include "KM_mutex.h"
#include "KM_log.h"
#include "KLV.h"
#include "MDD.cpp"

//------------------------------------------------------------------------------------------

// The composite dict is the union of the SMPTE and Interop dicts
//
static ASDCP::Dictionary s_CompositeDict;
static Kumu::Mutex s_CompositeDictLock;
static bool s_CompositeDictInit = false;

//
const ASDCP::Dictionary&
ASDCP::DefaultCompositeDict()
{
  if ( ! s_CompositeDictInit )
    {
      Kumu::AutoMutex AL(s_CompositeDictLock);

      if ( ! s_CompositeDictInit )
	{
	  s_CompositeDict.Init();
	  s_CompositeDictInit = true;
	}
    }

  return s_CompositeDict;
}

//
//
static ASDCP::Dictionary s_InteropDict;
static Kumu::Mutex s_InteropDictLock;
static bool s_InteropDictInit = false;

//
const ASDCP::Dictionary&
ASDCP::DefaultInteropDict()
{
  if ( ! s_InteropDictInit )
    {
      Kumu::AutoMutex AL(s_InteropDictLock);

      if ( ! s_InteropDictInit )
	{
	  s_InteropDict.Init();

	  s_InteropDict.DeleteEntry(MDD_MXFInterop_OPAtom);
	  s_InteropDict.DeleteEntry(MDD_MXFInterop_CryptEssence);
	  s_InteropDict.DeleteEntry(MDD_MXFInterop_GenericDescriptor_SubDescriptors);

	  s_InteropDict.AddEntry(s_MDD_Table[MDD_MXFInterop_OPAtom], MDD_OPAtom);
	  s_InteropDict.AddEntry(s_MDD_Table[MDD_MXFInterop_CryptEssence], MDD_CryptEssence);
	  s_InteropDict.AddEntry(s_MDD_Table[MDD_MXFInterop_GenericDescriptor_SubDescriptors],
				     MDD_GenericDescriptor_SubDescriptors);

	  s_InteropDictInit = true;
	}
    }

  return s_InteropDict;
}

//
//
static ASDCP::Dictionary s_SMPTEDict;
static Kumu::Mutex s_SMPTEDictLock;
static bool s_SMPTEDictInit = false;

//
const ASDCP::Dictionary&
ASDCP::DefaultSMPTEDict()
{
  if ( ! s_SMPTEDictInit )
    {
      Kumu::AutoMutex AL(s_SMPTEDictLock);

      if ( ! s_SMPTEDictInit )
	{
	  s_SMPTEDict.Init();

	  s_SMPTEDict.DeleteEntry(MDD_MXFInterop_OPAtom);
	  s_SMPTEDict.DeleteEntry(MDD_MXFInterop_CryptEssence);
	  s_SMPTEDict.DeleteEntry(MDD_MXFInterop_GenericDescriptor_SubDescriptors);

	  s_SMPTEDictInit = true;
	}
    }

  return s_SMPTEDict;
}

//
const ASDCP::MDDEntry&
ASDCP::MXFInterop_OPAtom_Entry() {
  return s_MDD_Table[MDD_MXFInterop_OPAtom];
}

//
const ASDCP::MDDEntry&
ASDCP::SMPTE_390_OPAtom_Entry() {
  return s_MDD_Table[MDD_OPAtom];
}


//------------------------------------------------------------------------------------------
//

ASDCP::Dictionary::Dictionary() {}
ASDCP::Dictionary::~Dictionary() {}

//
void
ASDCP::Dictionary::Init()
{
  m_md_lookup.clear();
  memset(m_MDD_Table, 0, sizeof(m_MDD_Table));

  for ( ui32_t x = 0; x < (ui32_t)ASDCP::MDD_Max; x++ )
    {
      if ( x == MDD_PartitionMetadata_IndexSID_DEPRECATED  // 30
	   || x == MDD_PartitionMetadata_BodySID_DEPRECATED  // 32
	   || x == MDD_PartitionMetadata_EssenceContainers_DEPRECATED  // 34
	   || x == MDD_IndexTableSegmentBase_IndexSID_DEPRECATED  // 56
	   || x == MDD_IndexTableSegmentBase_BodySID_DEPRECATED  // 57
	   || x == MDD_PartitionArray_RandomIndexMetadata_BodySID_DEPRECATED  // 73
	   || x == MDD_Preface_EssenceContainers_DEPRECATED  // 85
	   || x == MDD_EssenceContainerData_IndexSID_DEPRECATED  // 103
	   || x == MDD_EssenceContainerData_BodySID_DEPRECATED  // 104
	   || x == MDD_DMSegment_DataDefinition_DEPRECATED // 266
	   || x == MDD_DMSegment_Duration_DEPRECATED // 267
	   || x == MDD_PartitionMetadata_OperationalPattern_DEPRECATED  // 33
	   || x == MDD_Preface_OperationalPattern_DEPRECATED  // 84
	   || x == MDD_TimedTextResourceSubDescriptor_EssenceStreamID_DEPRECATED // 264
	   )
	continue;

      AddEntry(s_MDD_Table[x], x);
    }
}

//
bool
ASDCP::Dictionary::AddEntry(const MDDEntry& Entry, ui32_t index)
{
  if ( index >= (ui32_t)MDD_Max )
    {
      Kumu::DefaultLogSink().Warn("UL Dictionary: index exceeds maximum: %d\n", index);
      return false;
    }

  bool result = true;
  // is this index already there?
  std::map<ui32_t, ASDCP::UL>::iterator rii = m_md_rev_lookup.find(index);

  if ( rii != m_md_rev_lookup.end() )
    {
      DeleteEntry(index);
      result = false;
    }

  UL TmpUL(Entry.ul);

#ifdef MDD_AUTHORING_MODE
  char buf[64];
  std::map<ASDCP::UL, ui32_t>::iterator ii = m_md_lookup.find(TmpUL);
  if ( ii != m_md_lookup.end() )
    {
      fprintf(stderr, "DUPE! %s (%02x, %02x) %s | (%02x, %02x) %s\n",
	      TmpUL.EncodeString(buf, 64),
	      m_MDD_Table[ii->second].tag.a, m_MDD_Table[ii->second].tag.b,
	      m_MDD_Table[ii->second].name,
	      Entry.tag.a, Entry.tag.b, Entry.name);
    }
#endif

  m_md_lookup.insert(std::map<UL, ui32_t>::value_type(TmpUL, index));
  m_md_rev_lookup.insert(std::map<ui32_t, UL>::value_type(index, TmpUL));
  m_md_sym_lookup.insert(std::map<std::string, ui32_t>::value_type(Entry.name, index));
  m_MDD_Table[index] = Entry;

  return result;
}

//
bool
ASDCP::Dictionary::DeleteEntry(ui32_t index)
{
  std::map<ui32_t, ASDCP::UL>::iterator rii = m_md_rev_lookup.find(index);
  if ( rii != m_md_rev_lookup.end() )
    {
      std::map<ASDCP::UL, ui32_t>::iterator ii = m_md_lookup.find(rii->second);
      assert(ii != m_md_lookup.end());

      MDDEntry NilEntry;
      memset(&NilEntry, 0, sizeof(NilEntry));

      m_md_lookup.erase(ii);
      m_md_rev_lookup.erase(rii);
      m_MDD_Table[index] = NilEntry;
      return true;
    }

  return false;
}

//
const ASDCP::MDDEntry&
ASDCP::Dictionary::Type(MDD_t type_id) const
{
  assert(m_MDD_Table[0].name[0]);
  std::map<ui32_t, ASDCP::UL>::const_iterator rii = m_md_rev_lookup.find(type_id);

  if ( rii == m_md_rev_lookup.end() )
    Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL type_id: %d\n", type_id);

  return m_MDD_Table[type_id];
}

//
const ASDCP::MDDEntry*
ASDCP::Dictionary::FindUL(const byte_t* ul_buf) const
{
  assert(m_MDD_Table[0].name[0]);
  std::map<UL, ui32_t>::const_iterator i = m_md_lookup.find(UL(ul_buf));
  
  if ( i == m_md_lookup.end() )
    {
      byte_t tmp_ul[SMPTE_UL_LENGTH];
      memcpy(tmp_ul, ul_buf, SMPTE_UL_LENGTH);
      tmp_ul[SMPTE_UL_LENGTH-1] = 0;

      i = m_md_lookup.find(UL(tmp_ul));

      if ( i == m_md_lookup.end() )
	{
	  char buf[64];
	  UL TmpUL(ul_buf);
	  Kumu::DefaultLogSink().Warn("UL Dictionary: unknown UL: %s\n", TmpUL.EncodeString(buf, 64));
	  return 0;
	}
    }

  return &m_MDD_Table[i->second];
}

//
const ASDCP::MDDEntry*
ASDCP::Dictionary::FindSymbol(const std::string& str) const
{
  assert(m_MDD_Table[0].name[0]);
  std::map<std::string, ui32_t>::const_iterator i = m_md_sym_lookup.find(str);
  
  if ( i == m_md_sym_lookup.end() )
    {
      Kumu::DefaultLogSink().Warn("UL Dictionary: unknown symbol: %s\n", str.c_str());
      return 0;
    }

  return &m_MDD_Table[i->second];
}

//
void
ASDCP::Dictionary::Dump(FILE* stream) const
{
  if ( stream == 0 )
    stream = stderr;

  MDD_t di = (MDD_t)0;
  char     str_buf[64];

  while ( di < MDD_Max )
    {
      if ( m_MDD_Table[di].name != 0 )
	{
	  UL TmpUL(m_MDD_Table[di].ul);
	  fprintf(stream, "%s: %s\n", TmpUL.EncodeString(str_buf, 64), m_MDD_Table[di].name);
	}

      di = (MDD_t)(di + 1);
    }
}

//
// end Dict.cpp
//