2 Copyright (c) 2007, 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.h
29 \brief experimental AS-DCP timed-text container
31 Implements Draft S429-5
38 #ifndef _AS_DCP_SUBTITLE_H_
39 #define _AS_DCP_SUBTITLE_H_
47 enum MIMEType_t { MT_BIN, MT_PNG, MT_OPENTYPE };
49 struct TimedTextResourceDescriptor
51 byte_t ResourceID[UUIDlen];
54 TimedTextResourceDescriptor() : Type(MT_BIN) {}
57 typedef std::list<TimedTextResourceDescriptor> ResourceList_t;
59 struct TimedTextDescriptor
62 ui32_t ContainerDuration;
63 byte_t AssetID[UUIDlen];
64 std::string NamespaceName;
65 std::string EncodingName;
66 ResourceList_t ResourceList;
68 TimedTextDescriptor() : ContainerDuration(0), EncodingName("UTF-8") {} // D-Cinema format is always UTF-8
71 // Print debugging information to stream (stderr default)
72 void DescriptorDump(const TimedTextDescriptor&, FILE* = 0);
75 class FrameBuffer : public ASDCP::FrameBuffer
77 ASDCP_NO_COPY_CONSTRUCT(FrameBuffer); // TODO: should have copy construct
80 byte_t m_AssetID[UUIDlen];
81 std::string m_MIMEType;
84 FrameBuffer() { memset(m_AssetID, 0, UUIDlen); }
85 FrameBuffer(ui32_t size) { Capacity(size); memset(m_AssetID, 0, UUIDlen); }
86 virtual ~FrameBuffer() {}
88 inline const byte_t* AssetID() const { return m_AssetID; }
89 inline void AssetID(const byte_t* buf) { memcpy(m_AssetID, buf, UUIDlen); }
90 inline const char* MIMEType() const { return m_MIMEType.c_str(); }
91 inline void MIMEType(const std::string& s) { m_MIMEType = s; }
93 // Print debugging information to stream (stderr default)
94 void Dump(FILE* = 0, ui32_t dump_bytes = 0) const;
98 class IResourceResolver
101 virtual ~IResourceResolver() {}
102 virtual Result_t ResolveRID(const byte_t* uuid, FrameBuffer&) const = 0; // return data for RID
106 class DCSubtitleParser
108 class h__SubtitleParser;
109 mem_ptr<h__SubtitleParser> m_Parser;
110 ASDCP_NO_COPY_CONSTRUCT(DCSubtitleParser);
114 virtual ~DCSubtitleParser();
116 // Opens the XML file for reading, parse data to provide a complete
117 // set of stream metadata for the MXFWriter below.
118 Result_t OpenRead(const char* filename) const;
120 // Fill a TimedTextDescriptor struct with the values from the file's contents.
121 // Returns RESULT_INIT if the file is not open.
122 Result_t FillDescriptor(TimedTextDescriptor&) const;
124 // Reads the complete Timed Text Resource into the given string.
125 Result_t ReadTimedTextResource(std::string&) const;
127 // Reads the Ancillary Resource having the given ID. Fails if the buffer
128 // is too small or the resource does not exist. The optional Resolver
129 // argument can be provided which will be used to retrieve the resource
130 // having a particulat UUID. If a Resolver is not supplied, the default
131 // internal resolver will return the contents of the file having the UUID
132 // as the filename. The filename must exist in the same directory as the
133 // XML file opened with OpenRead().
134 Result_t ReadAncillaryResource(const byte_t* uuid, FrameBuffer&,
135 const IResourceResolver* Resolver = 0) const;
142 mem_ptr<h__Writer> m_Writer;
143 ASDCP_NO_COPY_CONSTRUCT(MXFWriter);
147 virtual ~MXFWriter();
149 // Open the file for writing. The file must not exist. Returns error if
150 // the operation cannot be completed or if nonsensical data is discovered
151 // in the essence descriptor.
152 Result_t OpenWrite(const char* filename, const WriterInfo&,
153 const TimedTextDescriptor&, ui32_t HeaderSize = 16384);
155 // Writes the Timed-Text Resource to the MXF file. The file must be UTF-8
156 // encoded. If the optional AESEncContext argument is present, the essence
157 // is encrypted prior to writing. Fails if the file is not open, is finalized,
158 // or an operating system error occurs.
159 // This method may only be called once, and it must be called before any
160 // call to WriteAncillaryResource(). RESULT_STATE will be returned if these
161 // conditions are not met.
162 Result_t WriteTimedTextResource(const std::string& XMLDoc, AESEncContext* = 0, HMACContext* = 0);
164 // Writes an Ancillary Resource to the MXF file. If the optional AESEncContext
165 // argument is present, the essence is encrypted prior to writing.
166 // Fails if the file is not open, is finalized, or an operating system
167 // error occurs. RESULT_STATE will be returned if the method is called before
168 // WriteTimedTextResource()
169 Result_t WriteAncillaryResource(const FrameBuffer&, AESEncContext* = 0, HMACContext* = 0);
171 // Closes the MXF file, writing the index and revised header.
179 mem_ptr<h__Reader> m_Reader;
180 ASDCP_NO_COPY_CONSTRUCT(MXFReader);
184 virtual ~MXFReader();
186 // Open the file for reading. The file must exist. Returns error if the
187 // operation cannot be completed.
188 Result_t OpenRead(const char* filename) const;
190 // Returns RESULT_INIT if the file is not open.
191 Result_t Close() const;
193 // Fill a TimedTextDescriptor struct with the values from the file's header.
194 // Returns RESULT_INIT if the file is not open.
195 Result_t FillDescriptor(TimedTextDescriptor&) const;
197 // Fill a WriterInfo struct with the values from the file's header.
198 // Returns RESULT_INIT if the file is not open.
199 Result_t FillWriterInfo(WriterInfo&) const;
201 // Reads the complete Timed Text Resource into the given string. Fails if the resource
202 // is encrypted and AESDecContext is NULL (use the following method to retrieve the
203 // raw ciphertet block).
204 Result_t ReadTimedTextResource(std::string&, AESDecContext* = 0, HMACContext* = 0) const;
206 // Reads the complete Timed Text Resource from the MXF file. If the optional AESEncContext
207 // argument is present, the resource is decrypted after reading. If the MXF
208 // file is encrypted and the AESDecContext argument is NULL, the frame buffer
209 // will contain the ciphertext frame data. If the HMACContext argument is
210 // not NULL, the HMAC will be calculated (if the file supports it).
211 // Returns RESULT_INIT if the file is not open, failure if the frame number is
212 // out of range, or if optional decrypt or HAMC operations fail.
213 Result_t ReadTimedTextResource(FrameBuffer&, AESDecContext* = 0, HMACContext* = 0) const;
215 // Reads the timed-text resource having the given UUID from the MXF file. If the
216 // optional AESEncContext argument is present, the resource is decrypted after
217 // reading. If the MXF file is encrypted and the AESDecContext argument is NULL,
218 // the frame buffer will contain the ciphertext frame data. If the HMACContext
219 // argument is not NULL, the HMAC will be calculated (if the file supports it).
220 // Returns RESULT_INIT if the file is not open, failure if the frame number is
221 // out of range, or if optional decrypt or HAMC operations fail.
222 Result_t ReadAncillaryResource(const byte_t* uuid, FrameBuffer&, AESDecContext* = 0, HMACContext* = 0) const;
224 // Print debugging information to stream
225 void DumpHeaderMetadata(FILE* = 0) const;
226 void DumpIndex(FILE* = 0) const;
228 } // namespace TimedText
235 #endif // _AS_DCP_SUBTITLE_H_
239 // end AS_DCP_Subtitle.h