2 Copyright (c) 2007-2008, 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.
27 /*! \file AS_DCP_TimedText.cpp
29 \brief AS-DCP library, PCM essence reader and writer implementation
33 #include "AS_DCP_internal.h"
34 #include "S12MTimecode.h"
38 using namespace ASDCP;
40 using Kumu::DefaultLogSink;
42 const char* c_dcst_namespace_name = "http://www.smpte-ra.org/schemas/428-7/2007/DCST";
44 //------------------------------------------------------------------------------------------
48 class FilenameResolver : public ASDCP::TimedText::IResourceResolver
50 std::string m_Dirname;
53 bool operator==(const FilenameResolver&);
56 FilenameResolver(const std::string& dirname)
58 if ( PathIsDirectory(dirname) )
64 DefaultLogSink().Error("Path '%s' is not a directory, defaulting to '.'\n", dirname.c_str());
69 Result_t ResolveRID(const byte_t* uuid, TimedText::FrameBuffer& FrameBuf) const
74 std::string filename = m_Dirname + "/" + RID.EncodeHex(buf, 64);
75 DefaultLogSink().Debug("retrieving resource %s from file %s\n", buf, filename.c_str());
77 Result_t result = Reader.OpenRead(filename.c_str());
79 if ( KM_SUCCESS(result) )
81 ui32_t read_count = 0;
82 result = Reader.Read(FrameBuf.Data(), FrameBuf.Capacity(), &read_count);
84 if ( KM_SUCCESS(result) )
85 FrameBuf.Size(read_count);
92 //------------------------------------------------------------------------------------------
94 typedef std::map<Kumu::UUID, TimedText::MIMEType_t> ResourceTypeMap_t;
96 class ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser
99 ResourceTypeMap_t m_ResourceTypes;
101 ASDCP_NO_COPY_CONSTRUCT(h__SubtitleParser);
104 std::string m_Filename;
105 std::string m_XMLDoc;
106 TimedTextDescriptor m_TDesc;
107 mem_ptr<FilenameResolver> m_DefaultResolver;
109 h__SubtitleParser() : m_Root("**ParserRoot**")
111 memset(&m_TDesc.AssetID, 0, UUIDlen);
114 ~h__SubtitleParser() {}
116 TimedText::IResourceResolver* GetDefaultResolver()
118 if ( m_DefaultResolver.empty() )
119 m_DefaultResolver = new FilenameResolver(PathDirname(m_Filename));
121 return m_DefaultResolver;
124 Result_t OpenRead(const char* filename);
125 Result_t ReadAncillaryResource(const byte_t* uuid, FrameBuffer& FrameBuf, const IResourceResolver& Resolver) const;
130 get_UUID_from_element(XMLElement* Element, UUID& ID)
133 const char* p = Element->GetBody().c_str();
134 if ( strncmp(p, "urn:uuid:", 9) == 0 ) p += 9;
135 return ID.DecodeHex(p);
140 get_UUID_from_child_element(const char* name, XMLElement* Parent, UUID& outID)
142 assert(name); assert(Parent);
143 XMLElement* Child = Parent->GetChildWithName(name);
144 if ( Child == 0 ) return false;
145 return get_UUID_from_element(Child, outID);
149 static ASDCP::Rational
150 decode_rational(const char* str_rat)
153 ui32_t Num = atoi(str_rat);
156 const char* den_str = strrchr(str_rat, ' ');
158 Den = atoi(den_str+1);
160 return ASDCP::Rational(Num, Den);
165 ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::OpenRead(const char* filename)
167 Result_t result = ReadFileIntoString(filename, m_XMLDoc);
169 if ( ! m_Root.ParseString(m_XMLDoc.c_str()) )
170 return RESULT_FORMAT;
172 m_Filename = filename;
173 m_TDesc.EncodingName = "UTF-8"; // the XML parser demands UTF-8
174 m_TDesc.ResourceList.clear();
175 m_TDesc.ContainerDuration = 0;
176 const XMLNamespace* ns = m_Root.Namespace();
180 DefaultLogSink(). Warn("Document has no namespace name, assuming %s\n", c_dcst_namespace_name);
181 m_TDesc.NamespaceName = c_dcst_namespace_name;
184 m_TDesc.NamespaceName = ns->Name();
187 if ( ! get_UUID_from_child_element("Id", &m_Root, DocID) )
189 DefaultLogSink(). Error("Id element missing from input document\n");
190 return RESULT_FORMAT;
193 memcpy(m_TDesc.AssetID, DocID.Value(), DocID.Size());
194 XMLElement* EditRate = m_Root.GetChildWithName("EditRate");
198 DefaultLogSink(). Error("EditRate element missing from input document\n");
199 return RESULT_FORMAT;
202 m_TDesc.EditRate = decode_rational(EditRate->GetBody().c_str());
204 if ( m_TDesc.EditRate != EditRate_24 && m_TDesc.EditRate != EditRate_48 )
206 DefaultLogSink(). Error("EditRate must be 24/1 or 48/1\n");
207 return RESULT_FORMAT;
211 ElementList FontList;
212 m_Root.GetChildrenWithName("LoadFont", FontList);
214 for ( Elem_i i = FontList.begin(); i != FontList.end(); i++ )
217 if ( ! get_UUID_from_element(*i, AssetID) )
219 DefaultLogSink(). Error("LoadFont element does not contain a urn:uuid value as expected.\n");
220 return RESULT_FORMAT;
223 TimedTextResourceDescriptor TmpResource;
224 memcpy(TmpResource.ResourceID, AssetID.Value(), UUIDlen);
225 TmpResource.Type = MT_OPENTYPE;
226 m_TDesc.ResourceList.push_back(TmpResource);
227 m_ResourceTypes.insert(ResourceTypeMap_t::value_type(UUID(TmpResource.ResourceID), MT_OPENTYPE));
231 ElementList ImageList;
232 m_Root.GetChildrenWithName("Image", ImageList);
234 for ( Elem_i i = ImageList.begin(); i != ImageList.end(); i++ )
237 if ( ! get_UUID_from_element(*i, AssetID) )
239 DefaultLogSink(). Error("Image element does not contain a urn:uuid value as expected.\n");
240 return RESULT_FORMAT;
243 TimedTextResourceDescriptor TmpResource;
244 memcpy(TmpResource.ResourceID, AssetID.Value(), UUIDlen);
245 TmpResource.Type = MT_PNG;
246 m_TDesc.ResourceList.push_back(TmpResource);
247 m_ResourceTypes.insert(ResourceTypeMap_t::value_type(UUID(TmpResource.ResourceID), MT_PNG));
250 // Calculate the timeline duration.
251 // This is a little ugly because the last element in the file is not necessarily
252 // the last instance to be displayed, e.g., element n and element n-1 may have the
253 // same start time but n-1 may have a greater duration making it the last to be seen.
254 // We must scan the list to accumulate the latest TimeOut value.
255 ElementList InstanceList;
256 ElementList::const_iterator ei;
257 ui32_t end_count = 0;
259 m_Root.GetChildrenWithName("Subtitle", InstanceList);
261 if ( InstanceList.empty() )
263 DefaultLogSink(). Error("XML document contains no Subtitle elements!\n");
264 return RESULT_FORMAT;
267 // assumes 24/1 or 48/1 as constrained above
268 S12MTimecode beginTC(InstanceList.front()->GetAttrWithName("TimeIn"), m_TDesc.EditRate.Numerator);
270 for ( ei = InstanceList.begin(); ei != InstanceList.end(); ei++ )
272 S12MTimecode tmpTC((*ei)->GetAttrWithName("TimeOut"), m_TDesc.EditRate.Numerator);
273 if ( end_count < tmpTC.GetFrames() )
274 end_count = tmpTC.GetFrames();
277 assert( end_count > beginTC.GetFrames() );
278 m_TDesc.ContainerDuration = end_count - beginTC.GetFrames();
286 ASDCP::TimedText::DCSubtitleParser::h__SubtitleParser::ReadAncillaryResource(const byte_t* uuid, FrameBuffer& FrameBuf,
287 const IResourceResolver& Resolver) const
289 FrameBuf.AssetID(uuid);
293 ResourceTypeMap_t::const_iterator rmi = m_ResourceTypes.find(TmpID);
295 if ( rmi == m_ResourceTypes.end() )
297 DefaultLogSink().Error("Unknown ancillary resource id: %s\n", TmpID.EncodeHex(buf, 64));
301 Result_t result = Resolver.ResolveRID(uuid, FrameBuf);
303 if ( KM_SUCCESS(result) )
305 if ( (*rmi).second == MT_PNG )
306 FrameBuf.MIMEType("image/png");
308 else if ( (*rmi).second == MT_OPENTYPE )
309 FrameBuf.MIMEType("application/x-font-opentype");
312 FrameBuf.MIMEType("application/octet-stream");
318 //------------------------------------------------------------------------------------------
320 ASDCP::TimedText::DCSubtitleParser::DCSubtitleParser()
324 ASDCP::TimedText::DCSubtitleParser::~DCSubtitleParser()
328 // Opens the stream for reading, parses enough data to provide a complete
329 // set of stream metadata for the MXFWriter below.
331 ASDCP::TimedText::DCSubtitleParser::OpenRead(const char* filename) const
333 const_cast<ASDCP::TimedText::DCSubtitleParser*>(this)->m_Parser = new h__SubtitleParser;
335 Result_t result = m_Parser->OpenRead(filename);
337 if ( ASDCP_FAILURE(result) )
338 const_cast<ASDCP::TimedText::DCSubtitleParser*>(this)->m_Parser = 0;
345 ASDCP::TimedText::DCSubtitleParser::FillDescriptor(TimedTextDescriptor& TDesc) const
347 if ( m_Parser.empty() )
350 TDesc = m_Parser->m_TDesc;
354 // Reads the complete Timed Text Resource into the given string.
356 ASDCP::TimedText::DCSubtitleParser::ReadTimedTextResource(std::string& s) const
358 if ( m_Parser.empty() )
361 s = m_Parser->m_XMLDoc;
367 ASDCP::TimedText::DCSubtitleParser::ReadAncillaryResource(const byte_t* uuid, FrameBuffer& FrameBuf,
368 const IResourceResolver* Resolver) const
370 if ( m_Parser.empty() )
374 Resolver = m_Parser->GetDefaultResolver();
376 return m_Parser->ReadAncillaryResource(uuid, FrameBuf, *Resolver);
381 // end AS_DCP_timedText.cpp