+//
+bool
+ASDCP::MXF::decode_mca_string(const std::string& s, const mca_label_map_t& labels, const Dictionary*& dict, const std::string& language,
+ InterchangeObject_list_t& descriptor_list, ui32_t& channel_count)
+{
+ std::string symbol_buf;
+ channel_count = 0;
+ ASDCP::MXF::SoundfieldGroupLabelSubDescriptor *current_soundfield = 0, *prev_soundfield = 0;
+ std::string::const_iterator i;
+
+ for ( i = s.begin(); i != s.end(); ++i )
+ {
+ if ( *i == '(' )
+ {
+ if ( current_soundfield != 0 && symbol_buf.empty() )
+ {
+ // appending to the existing soundfield group
+ continue;
+ }
+ else if ( current_soundfield != 0 )
+ {
+ DefaultLogSink().Error("Encountered '(', already processing a soundfield group.\n");
+ return false;
+ }
+ else if ( symbol_buf.empty() )
+ {
+ if ( prev_soundfield != 0 )
+ {
+ current_soundfield = prev_soundfield;
+ // appending to the existing soundfield group
+ continue;
+ }
+ else
+ {
+ DefaultLogSink().Error("Encountered '(', without leading soundfield group symbol.\n");
+ return false;
+ }
+ }
+
+ mca_label_map_t::const_iterator i = labels.find(symbol_buf);
+
+ if ( i == labels.end() )
+ {
+ DefaultLogSink().Error("Unknown symbol: '%s'\n", symbol_buf.c_str());
+ return false;
+ }
+
+ if ( ! ul_is_an_mca_group(i->second.ul) )
+ {
+ DefaultLogSink().Error("Not a soundfield group symbol: '%s'\n", symbol_buf.c_str());
+ return false;
+ }
+
+ current_soundfield = new ASDCP::MXF::SoundfieldGroupLabelSubDescriptor(dict);
+ GenRandomValue(current_soundfield->MCALinkID);
+
+ current_soundfield->MCATagSymbol = (i->second.requires_prefix ? "sg" : "") + i->first;
+ current_soundfield->MCATagName = i->second.tag_name;
+ current_soundfield->RFC5646SpokenLanguage = language;
+ current_soundfield->MCALabelDictionaryID = i->second.ul;
+ descriptor_list.push_back(reinterpret_cast<ASDCP::MXF::InterchangeObject*>(current_soundfield));
+ prev_soundfield = current_soundfield;
+ symbol_buf.clear();
+ }
+ else if ( *i == ')' )
+ {
+ if ( current_soundfield == 0 )
+ {
+ DefaultLogSink().Error("Encountered ')', not currently processing a soundfield group.\n");
+ return false;
+ }
+
+ if ( symbol_buf.empty() )
+ {
+ DefaultLogSink().Error("Soundfield group description contains no channels.\n");
+ return false;
+ }
+
+ mca_label_map_t::const_iterator i = labels.find(symbol_buf);
+
+ if ( i == labels.end() )
+ {
+ DefaultLogSink().Error("Unknown symbol: '%s'\n", symbol_buf.c_str());
+ return false;
+ }
+
+ assert(current_soundfield);
+
+ ASDCP::MXF::AudioChannelLabelSubDescriptor *channel_descr =
+ new ASDCP::MXF::AudioChannelLabelSubDescriptor(dict);
+ GenRandomValue(channel_descr->MCALinkID);
+
+ channel_descr->SoundfieldGroupLinkID = current_soundfield->MCALinkID;
+ channel_descr->MCAChannelID = channel_count++ + 1;
+ channel_descr->MCATagSymbol = (i->second.requires_prefix ? "ch" : "") + i->first;
+ channel_descr->MCATagName = i->second.tag_name;
+ channel_descr->RFC5646SpokenLanguage = language;
+ channel_descr->MCALabelDictionaryID = i->second.ul;
+ descriptor_list.push_back(reinterpret_cast<ASDCP::MXF::InterchangeObject*>(channel_descr));
+ symbol_buf.clear();
+ current_soundfield = 0;
+ }
+ else if ( *i == ',' )
+ {
+ if ( ! symbol_buf.empty() && ! symbol_buf.compare("-") )
+ {
+ channel_count++;
+ symbol_buf.clear();
+ }
+ else if ( ! symbol_buf.empty() )
+ {
+ mca_label_map_t::const_iterator i = labels.find(symbol_buf);
+
+ if ( i == labels.end() )
+ {
+ DefaultLogSink().Error("Unknown symbol: '%s'\n", symbol_buf.c_str());
+ return false;
+ }
+
+ if ( ! ul_is_an_mca_channel(i->second.ul) )
+ {
+ DefaultLogSink().Error("Not a channel symbol: '%s'\n", symbol_buf.c_str());
+ return false;
+ }
+
+ ASDCP::MXF::AudioChannelLabelSubDescriptor *channel_descr =
+ new ASDCP::MXF::AudioChannelLabelSubDescriptor(dict);
+ GenRandomValue(channel_descr->MCALinkID);
+
+ if ( current_soundfield != 0 )
+ {
+ channel_descr->SoundfieldGroupLinkID = current_soundfield->MCALinkID;
+ }
+
+ channel_descr->MCAChannelID = channel_count++ + 1;
+ channel_descr->MCATagSymbol = (i->second.requires_prefix ? "ch" : "") + i->first;
+ channel_descr->MCATagName = i->second.tag_name;
+ channel_descr->RFC5646SpokenLanguage = language;
+ channel_descr->MCALabelDictionaryID = i->second.ul;
+ descriptor_list.push_back(reinterpret_cast<ASDCP::MXF::InterchangeObject*>(channel_descr));
+ symbol_buf.clear();
+ }
+ }
+ else if ( *i == '-' || isalnum(*i) )
+ {
+ symbol_buf += *i;
+ }
+ else if ( ! isspace(*i) )
+ {
+ DefaultLogSink().Error("Unexpected character '%c'.\n", *i);
+ return false;
+ }
+ }
+
+ if ( ! symbol_buf.empty() && ! symbol_buf.compare("-") )
+ {
+ channel_count++;
+ }
+ else if ( ! symbol_buf.empty() )
+ {
+ mca_label_map_t::const_iterator i = labels.find(symbol_buf);
+
+ if ( i == labels.end() )
+ {
+ DefaultLogSink().Error("Unknown symbol: '%s'\n", symbol_buf.c_str());
+ return false;
+ }
+
+ ASDCP::MXF::AudioChannelLabelSubDescriptor *channel_descr =
+ new ASDCP::MXF::AudioChannelLabelSubDescriptor(dict);
+ GenRandomValue(channel_descr->MCALinkID);
+
+ if ( current_soundfield != 0 )
+ {
+ channel_descr->SoundfieldGroupLinkID = current_soundfield->MCALinkID;
+ }
+
+ channel_descr->MCAChannelID = channel_count++ + 1;
+ channel_descr->MCATagSymbol = (i->second.requires_prefix ? "ch" : "") + i->first;
+ channel_descr->MCATagName = i->second.tag_name;
+ channel_descr->RFC5646SpokenLanguage = language;
+ channel_descr->MCALabelDictionaryID = i->second.ul;
+ descriptor_list.push_back(reinterpret_cast<ASDCP::MXF::InterchangeObject*>(channel_descr));
+ }
+
+ return true;
+}
+
+//
+ASDCP::MXF::ASDCP_MCAConfigParser::ASDCP_MCAConfigParser(const Dictionary*& d) : m_Dict(d), m_ChannelCount(0)
+{
+ typedef mca_label_map_t::value_type pair;
+ m_LabelMap.insert(pair("L", label_traits("Left" , true, m_Dict->ul(MDD_DCAudioChannel_L))));
+ m_LabelMap.insert(pair("R", label_traits("Right" , true, m_Dict->ul(MDD_DCAudioChannel_R))));
+ m_LabelMap.insert(pair("C", label_traits("Center" , true, m_Dict->ul(MDD_DCAudioChannel_C))));
+ m_LabelMap.insert(pair("LFE", label_traits("LFE" , true, m_Dict->ul(MDD_DCAudioChannel_LFE))));
+ m_LabelMap.insert(pair("Ls", label_traits("Left Surround" , true, m_Dict->ul(MDD_DCAudioChannel_Ls))));
+ m_LabelMap.insert(pair("Rs", label_traits("Right Surround" , true, m_Dict->ul(MDD_DCAudioChannel_Rs))));
+ m_LabelMap.insert(pair("Lss", label_traits("Left Side Surround" , true, m_Dict->ul(MDD_DCAudioChannel_Lss))));
+ m_LabelMap.insert(pair("Rss", label_traits("Right Side Surround" , true, m_Dict->ul(MDD_DCAudioChannel_Rss))));
+ m_LabelMap.insert(pair("Lrs", label_traits("Left Rear Surround" , true, m_Dict->ul(MDD_DCAudioChannel_Lrs))));
+ m_LabelMap.insert(pair("Rrs", label_traits("Right Rear Surround" , true, m_Dict->ul(MDD_DCAudioChannel_Rrs))));
+ m_LabelMap.insert(pair("Lc", label_traits("Left Center" , true, m_Dict->ul(MDD_DCAudioChannel_Lc))));
+ m_LabelMap.insert(pair("Rc", label_traits("Right Center" , true, m_Dict->ul(MDD_DCAudioChannel_Rc))));
+ m_LabelMap.insert(pair("Cs", label_traits("Center Surround" , true, m_Dict->ul(MDD_DCAudioChannel_Cs))));
+ m_LabelMap.insert(pair("HI", label_traits("Hearing Impaired" , true, m_Dict->ul(MDD_DCAudioChannel_HI))));
+ m_LabelMap.insert(pair("VIN", label_traits("Visually Impaired-Narrative" , true, m_Dict->ul(MDD_DCAudioChannel_VIN))));
+ m_LabelMap.insert(pair("51", label_traits("5.1" , true, m_Dict->ul(MDD_DCAudioSoundfield_51))));
+ m_LabelMap.insert(pair("71", label_traits("7.1DS" , true, m_Dict->ul(MDD_DCAudioSoundfield_71))));
+ m_LabelMap.insert(pair("SDS", label_traits("7.1SDS" , true, m_Dict->ul(MDD_DCAudioSoundfield_SDS))));
+ m_LabelMap.insert(pair("61", label_traits("6.1" , true, m_Dict->ul(MDD_DCAudioSoundfield_61))));
+ m_LabelMap.insert(pair("M", label_traits("1.0 Monaural" , true, m_Dict->ul(MDD_DCAudioSoundfield_M))));
+ m_LabelMap.insert(pair("FSKSync", label_traits("FSK Sync" , true, m_Dict->ul(MDD_DCAudioChannel_FSKSyncSignalChannel))));
+ m_LabelMap.insert(pair("DBOX", label_traits("D-BOX Motion Code Primary Stream" , false, m_Dict->ul(MDD_DBOXMotionCodePrimaryStream))));
+ m_LabelMap.insert(pair("DBOX2", label_traits("D-BOX Motion Code Secondary Stream", false, m_Dict->ul(MDD_DBOXMotionCodeSecondaryStream))));
+ m_LabelMap.insert(pair("SLVS", label_traits("Sign Language Video Stream" , false, m_Dict->ul(MDD_AudioChannelSLVS))));
+}
+
+//
+ui32_t
+ASDCP::MXF::ASDCP_MCAConfigParser::ChannelCount() const
+{
+ return m_ChannelCount;
+}
+
+// 51(L,R,C,LFE,Ls,Rs),HI,VIN
+bool
+ASDCP::MXF::ASDCP_MCAConfigParser::DecodeString(const std::string& s, const std::string& language)
+{
+ return decode_mca_string(s, m_LabelMap, m_Dict, language, *this, m_ChannelCount);
+}
+
+// ST(L,R),DNS(NSC001,NSC002),-,VIN
+ASDCP::MXF::AS02_MCAConfigParser::AS02_MCAConfigParser(const Dictionary*& d) : ASDCP::MXF::ASDCP_MCAConfigParser(d)
+{
+ typedef mca_label_map_t::value_type pair;
+ m_LabelMap.insert(pair("M1", label_traits("Mono One", true, m_Dict->ul(MDD_IMFAudioChannel_M1))));
+ m_LabelMap.insert(pair("M2", label_traits("Mono Two", true, m_Dict->ul(MDD_IMFAudioChannel_M2))));
+ m_LabelMap.insert(pair("Lt", label_traits("Left Total", true, m_Dict->ul(MDD_IMFAudioChannel_Lt))));
+ m_LabelMap.insert(pair("Rt", label_traits("Right Total", true, m_Dict->ul(MDD_IMFAudioChannel_Rt))));
+ m_LabelMap.insert(pair("Lst", label_traits("Left Surround Total", true, m_Dict->ul(MDD_IMFAudioChannel_Lst))));
+ m_LabelMap.insert(pair("Rst", label_traits("Right Surround Total", true, m_Dict->ul(MDD_IMFAudioChannel_Rst))));
+ m_LabelMap.insert(pair("S", label_traits("Surround", true, m_Dict->ul(MDD_IMFAudioChannel_S))));
+ m_LabelMap.insert(pair("ST", label_traits("Standard Stereo", true, m_Dict->ul(MDD_IMFAudioSoundfield_ST))));
+ m_LabelMap.insert(pair("DM", label_traits("Dual Mono", true, m_Dict->ul(MDD_IMFAudioSoundfield_DM))));
+ m_LabelMap.insert(pair("DNS", label_traits("Discrete Numbered Sources", true, m_Dict->ul(MDD_IMFAudioSoundfield_DNS))));
+ m_LabelMap.insert(pair("30", label_traits("3.0", true, m_Dict->ul(MDD_IMFAudioSoundfield_30))));
+ m_LabelMap.insert(pair("40", label_traits("4.0", true, m_Dict->ul(MDD_IMFAudioSoundfield_40))));
+ m_LabelMap.insert(pair("50", label_traits("5.0", true, m_Dict->ul(MDD_IMFAudioSoundfield_50))));
+ m_LabelMap.insert(pair("60", label_traits("6.0", true, m_Dict->ul(MDD_IMFAudioSoundfield_60))));
+ m_LabelMap.insert(pair("70", label_traits("7.0DS", true, m_Dict->ul(MDD_IMFAudioSoundfield_70))));
+ m_LabelMap.insert(pair("LtRt", label_traits("Lt-Rt",true, m_Dict->ul(MDD_IMFAudioSoundfield_LtRt))));
+ m_LabelMap.insert(pair("51Ex", label_traits("5.1EX",true, m_Dict->ul(MDD_IMFAudioSoundfield_51Ex))));
+ m_LabelMap.insert(pair("HA", label_traits("Hearing Accessibility", true, m_Dict->ul(MDD_IMFAudioSoundfield_HI))));
+ m_LabelMap.insert(pair("VA", label_traits("Visual Accessibility", true, m_Dict->ul(MDD_IMFAudioSoundfield_VIN))));
+
+ // assemble the set of Numbered Source Channel labels
+ char name_buf[64], symbol_buf[64];
+ byte_t ul_buf[16];
+ memcpy(ul_buf, m_Dict->ul(MDD_IMFNumberedSourceChannel), 16);
+
+ for ( int i = 1; i < 128; ++i )
+ {
+ snprintf(name_buf, 64, "Numbered Source Channel %03d", i);
+ snprintf(symbol_buf, 64, "NSC%03d", i);
+ ul_buf[13] = i;
+ m_LabelMap.insert(pair(symbol_buf, label_traits(name_buf, true, UL(ul_buf))));
+ }
+}
+
+//
+bool
+ASDCP::MXF::GetEditRateFromFP(ASDCP::MXF::OP1aHeader& header, ASDCP::Rational& edit_rate)
+{
+ bool has_first_item = false;
+
+ MXF::InterchangeObject* temp_item;
+ std::list<MXF::InterchangeObject*> temp_items;
+
+ Result_t result = header.GetMDObjectsByType(DefaultCompositeDict().ul(MDD_SourcePackage), temp_items);
+
+ if ( KM_FAILURE(result) )
+ {
+ DefaultLogSink().Error("The MXF header does not contain a FilePackage item.\n");
+ return false;
+ }
+
+ if ( temp_items.size() != 1 )
+ {
+ DefaultLogSink().Error("The MXF header must contain one FilePackage item, found %d.\n", temp_items.size());
+ return false;
+ }
+
+ char buf[64];
+ MXF::Array<UUID>::const_iterator i;
+ MXF::SourcePackage *source_package = dynamic_cast<MXF::SourcePackage*>(temp_items.front());
+ assert(source_package);
+
+ for ( i = source_package->Tracks.begin(); i != source_package->Tracks.end(); ++i )
+ {
+ // Track
+ result = header.GetMDObjectByID(*i, &temp_item);
+
+ if ( KM_FAILURE(result) )
+ {
+ DefaultLogSink().Error("The MXF header is incomplete: strong reference %s leads nowhere.\n",
+ i->EncodeHex(buf, 64));
+ return false;
+ }
+
+ MXF::Track *track = dynamic_cast<MXF::Track*>(temp_item);
+
+ if ( track == 0 )
+ {
+ DefaultLogSink().Error("The MXF header is incomplete: %s is not a Track item.\n",
+ i->EncodeHex(buf, 64));
+ return false;
+ }
+
+ // Sequence
+ result = header.GetMDObjectByID(track->Sequence, &temp_item);
+
+ if ( KM_FAILURE(result) )
+ {
+ DefaultLogSink().Error("The MXF header is incomplete: strong reference %s leads nowhere.\n",
+ i->EncodeHex(buf, 64));
+ return false;
+ }
+
+ MXF::Sequence *sequence = dynamic_cast<MXF::Sequence*>(temp_item);
+
+ if ( sequence == 0 )
+ {
+ DefaultLogSink().Error("The MXF header is incomplete: %s is not a Sequence item.\n",
+ track->Sequence.get().EncodeHex(buf, 64));
+ return false;
+ }
+
+ if ( sequence->StructuralComponents.size() != 1 )
+ {
+ DefaultLogSink().Error("The Sequence item must contain one reference to an esence item, found %d.\n",
+ sequence->StructuralComponents.size());
+ return false;
+ }
+
+ // SourceClip
+ result = header.GetMDObjectByID(sequence->StructuralComponents.front(), &temp_item);
+
+ if ( KM_FAILURE(result) )
+ {
+ DefaultLogSink().Error("The MXF header is incomplete: strong reference %s leads nowhere.\n",
+ sequence->StructuralComponents.front().EncodeHex(buf, 64));
+ return false;
+ }
+
+ if ( temp_item->IsA(DefaultCompositeDict().ul(MDD_SourceClip)) )
+ {
+ MXF::SourceClip *source_clip = dynamic_cast<MXF::SourceClip*>(temp_item);
+
+ if ( source_clip == 0 )
+ {
+ DefaultLogSink().Error("The MXF header is incomplete: %s is not a SourceClip item.\n",
+ sequence->StructuralComponents.front().EncodeHex(buf, 64));
+ return false;
+ }
+
+ if ( ! has_first_item )
+ {
+ edit_rate = track->EditRate;
+ has_first_item = true;
+ }
+ else if ( edit_rate != track->EditRate )
+ {
+ DefaultLogSink().Error("The MXF header is incomplete: %s EditRate value does not match others in the file.\n",
+ sequence->StructuralComponents.front().EncodeHex(buf, 64));
+ return false;
+ }
+ }
+ else if ( ! temp_item->IsA(DefaultCompositeDict().ul(MDD_TimecodeComponent)) )
+ {
+ DefaultLogSink().Error("Reference from Sequence to an unexpected type: %s.\n", temp_item->ObjectName());
+ return false;
+ }
+ }
+
+ return true;
+}
+
+