X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2FTimedText_Parser.cpp;h=6b6d0066be16cfacd7cd3a8b904f42fd5b7d0005;hb=5d86f368b2bade7bca7700c74f198ef7f4129b44;hp=a1f3de4b4284937280c45eefb9a78d23816a1b76;hpb=eee42b6d7ce3d0857f96612919a1852e8c45283b;p=asdcplib.git diff --git a/src/TimedText_Parser.cpp b/src/TimedText_Parser.cpp index a1f3de4..6b6d006 100644 --- a/src/TimedText_Parser.cpp +++ b/src/TimedText_Parser.cpp @@ -1,5 +1,5 @@ /* -Copyright (c) 2007-2009, John Hurst +Copyright (c) 2007-2014, John Hurst All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,54 +44,65 @@ const char* c_dcst_namespace_name = "http://www.smpte-ra.org/schemas/428-7/2007/ //------------------------------------------------------------------------------------------ +ASDCP::TimedText::LocalFilenameResolver::LocalFilenameResolver() {} +ASDCP::TimedText::LocalFilenameResolver::~LocalFilenameResolver() {} -class LocalFilenameResolver : public ASDCP::TimedText::IResourceResolver +// +Result_t +ASDCP::TimedText::LocalFilenameResolver::OpenRead(const std::string& dirname) { - std::string m_Dirname; - - LocalFilenameResolver(); - bool operator==(const LocalFilenameResolver&); - -public: - LocalFilenameResolver(const std::string& dirname) - { - if ( PathIsDirectory(dirname) ) - { - m_Dirname = dirname; - return; - } - - DefaultLogSink().Error("Path '%s' is not a directory, defaulting to '.'\n", dirname.c_str()); - m_Dirname = "."; - } + if ( PathIsDirectory(dirname) ) + { + m_Dirname = dirname; + return RESULT_OK; + } - // - Result_t ResolveRID(const byte_t* uuid, TimedText::FrameBuffer& FrameBuf) const - { - FileReader Reader; - char buf[64]; - UUID RID(uuid); - std::string filename = m_Dirname + "/" + RID.EncodeHex(buf, 64); - DefaultLogSink().Debug("retrieving resource %s from file %s\n", buf, filename.c_str()); + DefaultLogSink().Error("Path '%s' is not a directory, defaulting to '.'\n", dirname.c_str()); + m_Dirname = "."; + return RESULT_FALSE; +} - Result_t result = Reader.OpenRead(filename.c_str()); +// +Result_t +ASDCP::TimedText::LocalFilenameResolver::ResolveRID(const byte_t* uuid, TimedText::FrameBuffer& FrameBuf) const +{ + Result_t result = RESULT_NOT_FOUND; + char buf[64]; + UUID RID(uuid); + PathList_t found_list; - if ( KM_SUCCESS(result) ) - { - ui32_t read_count, read_size = Reader.Size(); +#ifndef KM_WIN32 + // TODO, fix this for win32 (needs regex) + FindInPath(PathMatchRegex(RID.EncodeHex(buf, 64)), m_Dirname, found_list); +#endif - result = FrameBuf.Capacity(read_size); + if ( found_list.size() == 1 ) + { + FileReader Reader; + DefaultLogSink().Debug("Retrieving resource %s from file %s\n", buf, found_list.front().c_str()); - if ( KM_SUCCESS(result) ) - result = Reader.Read(FrameBuf.Data(), read_size, &read_count); + result = Reader.OpenRead(found_list.front().c_str()); - if ( KM_SUCCESS(result) ) - FrameBuf.Size(read_count); - } + if ( KM_SUCCESS(result) ) + { + ui32_t read_count, read_size = Reader.Size(); + result = FrameBuf.Capacity(read_size); + + if ( KM_SUCCESS(result) ) + result = Reader.Read(FrameBuf.Data(), read_size, &read_count); + + if ( KM_SUCCESS(result) ) + FrameBuf.Size(read_count); + } + } + else if ( ! found_list.empty() ) + { + DefaultLogSink().Error("More than one file in %s matches %s.\n", m_Dirname.c_str(), buf); + result = RESULT_RAW_FORMAT; + } - return result; - } -}; + return result; +} //------------------------------------------------------------------------------------------ @@ -121,13 +132,16 @@ public: TimedText::IResourceResolver* GetDefaultResolver() { if ( m_DefaultResolver.empty() ) - m_DefaultResolver = new LocalFilenameResolver(PathDirname(m_Filename)); - + { + m_DefaultResolver = new LocalFilenameResolver(); + m_DefaultResolver->OpenRead(PathDirname(m_Filename)); + } + return m_DefaultResolver; } - Result_t OpenRead(const char* filename); - Result_t OpenRead(const std::string& xml_doc, const char* filename); + Result_t OpenRead(const std::string& filename); + Result_t OpenRead(const std::string& xml_doc, const std::string& filename); Result_t ReadAncillaryResource(const byte_t* uuid, FrameBuffer& FrameBuf, const IResourceResolver& Resolver) const; }; @@ -137,7 +151,12 @@ get_UUID_from_element(XMLElement* Element, UUID& ID) { assert(Element); const char* p = Element->GetBody().c_str(); - if ( strncmp(p, "urn:uuid:", 9) == 0 ) p += 9; + + if ( strncmp(p, "urn:uuid:", 9) == 0 ) + { + p += 9; + } + return ID.DecodeHex(p); } @@ -145,30 +164,21 @@ get_UUID_from_element(XMLElement* Element, UUID& ID) bool get_UUID_from_child_element(const char* name, XMLElement* Parent, UUID& outID) { - assert(name); assert(Parent); + assert(name); + assert(Parent); XMLElement* Child = Parent->GetChildWithName(name); - if ( Child == 0 ) return false; - return get_UUID_from_element(Child, outID); -} -// -static ASDCP::Rational -decode_rational(const char* str_rat) -{ - assert(str_rat); - ui32_t Num = atoi(str_rat); - ui32_t Den = 0; - - const char* den_str = strrchr(str_rat, ' '); - if ( den_str != 0 ) - Den = atoi(den_str+1); + if ( Child == 0 ) + { + return false; + } - return ASDCP::Rational(Num, Den); + return get_UUID_from_element(Child, outID); } // Result_t -ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead(const char* filename) +ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead(const std::string& filename) { Result_t result = ReadFileIntoString(filename, m_XMLDoc); @@ -181,14 +191,18 @@ ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead(const char* file // Result_t -ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead(const std::string& xml_doc, const char* filename) +ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead(const std::string& xml_doc, const std::string& filename) { m_XMLDoc = xml_doc; - if ( filename != 0 ) - m_Filename = filename; + if ( filename.empty() ) + { + m_Filename = ""; + } else - m_Filename = ""; + { + m_Filename = filename; + } return OpenRead(); } @@ -197,7 +211,7 @@ ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead(const std::strin Result_t ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead() { - if ( ! m_Root.ParseString(m_XMLDoc.c_str()) ) + if ( ! m_Root.ParseString(m_XMLDoc) ) return RESULT_FORMAT; m_TDesc.EncodingName = "UTF-8"; // the XML parser demands UTF-8 @@ -207,7 +221,7 @@ ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead() if ( ns == 0 ) { - DefaultLogSink(). Warn("Document has no namespace name, assuming %s\n", c_dcst_namespace_name); + DefaultLogSink(). Warn("Document has no namespace name, assuming \"%s\".\n", c_dcst_namespace_name); m_TDesc.NamespaceName = c_dcst_namespace_name; } else @@ -218,7 +232,7 @@ ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead() UUID DocID; if ( ! get_UUID_from_child_element("Id", &m_Root, DocID) ) { - DefaultLogSink(). Error("Id element missing from input document\n"); + DefaultLogSink(). Error("Id element missing from input document.\n"); return RESULT_FORMAT; } @@ -227,11 +241,15 @@ ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead() if ( EditRate == 0 ) { - DefaultLogSink(). Error("EditRate element missing from input document\n"); + DefaultLogSink().Error("EditRate element missing from input document.\n"); return RESULT_FORMAT; } - m_TDesc.EditRate = decode_rational(EditRate->GetBody().c_str()); + if ( ! DecodeRational(EditRate->GetBody().c_str(), m_TDesc.EditRate) ) + { + DefaultLogSink().Error("Error decoding edit rate value: \"%s\"\n", EditRate->GetBody().c_str()); + return RESULT_FORMAT; + } if ( m_TDesc.EditRate != EditRate_23_98 && m_TDesc.EditRate != EditRate_24 @@ -239,7 +257,13 @@ ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead() && m_TDesc.EditRate != EditRate_30 && m_TDesc.EditRate != EditRate_48 && m_TDesc.EditRate != EditRate_50 - && m_TDesc.EditRate != EditRate_60 ) + && m_TDesc.EditRate != EditRate_60 + && m_TDesc.EditRate != EditRate_96 + && m_TDesc.EditRate != EditRate_100 + && m_TDesc.EditRate != EditRate_120 + && m_TDesc.EditRate != EditRate_192 + && m_TDesc.EditRate != EditRate_200 + && m_TDesc.EditRate != EditRate_240 ) { DefaultLogSink(). Error("Unexpected EditRate: %d/%d\n", m_TDesc.EditRate.Numerator, m_TDesc.EditRate.Denominator); @@ -384,7 +408,7 @@ ASDCP::TimedText::DCSubtitleParser::~DCSubtitleParser() // Opens the stream for reading, parses enough data to provide a complete // set of stream metadata for the MXFWriter below. ASDCP::Result_t -ASDCP::TimedText::DCSubtitleParser::OpenRead(const char* filename) const +ASDCP::TimedText::DCSubtitleParser::OpenRead(const std::string& filename) const { const_cast(this)->m_Parser = new h__SubtitleParser; @@ -398,7 +422,7 @@ ASDCP::TimedText::DCSubtitleParser::OpenRead(const char* filename) const // Parses an XML document to provide a complete set of stream metadata for the MXFWriter below. Result_t -ASDCP::TimedText::DCSubtitleParser::OpenRead(const std::string& xml_doc, const char* filename) const +ASDCP::TimedText::DCSubtitleParser::OpenRead(const std::string& xml_doc, const std::string& filename) const { const_cast(this)->m_Parser = new h__SubtitleParser; @@ -448,5 +472,5 @@ ASDCP::TimedText::DCSubtitleParser::ReadAncillaryResource(const byte_t* uuid, Fr // -// end AS_DCP_timedText.cpp +// end AS_DCP_TimedTextParser.cpp //