Fix typo.
[asdcplib.git] / src / AS_02.h
index 34371524b9654ef8e8e30239f3c224966f029eac..61a2c1e0ba6b83c0e1fd715086c01c5361643d52 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2011-2013, Robert Scheler, Heiko Sparenberg Fraunhofer IIS,
+Copyright (c) 2011-2018, Robert Scheler, Heiko Sparenberg Fraunhofer IIS,
 John Hurst
 
 All rights reserved.
@@ -97,7 +97,6 @@ namespace AS_02
       Result_t Lookup(ui32_t frame_num, ASDCP::MXF::IndexTableSegment::IndexEntry&) const;
     };
 
-
     
     // Returns size in bytes of a single sample of data described by ADesc
     inline ui32_t CalcSampleSize(const ASDCP::MXF::WaveAudioDescriptor& d)
@@ -119,15 +118,27 @@ namespace AS_02
     }
 
     // Returns number of frames for data described by ADesc, given a duration in samples and an edit rate
-    inline ui32_t CalcFramesFromDurationInSamples(const ui32_t durationInSamples, const ASDCP::MXF::WaveAudioDescriptor& d, const ASDCP::Rational& edit_rate)
+    inline ui32_t CalcFramesFromDurationInSamples(const ui32_t duration_samples, const ASDCP::MXF::WaveAudioDescriptor& d,
+                                                 const ASDCP::Rational& edit_rate)
     {
-      return static_cast<ui32_t>(static_cast<ui64_t>(durationInSamples) *
-                                static_cast<ui64_t>(d.AudioSamplingRate.Denominator * edit_rate.Numerator) /
-                                static_cast<ui64_t>(d.AudioSamplingRate.Numerator * edit_rate.Denominator));
+      ui32_t spf = CalcSamplesPerFrame(d, edit_rate);
+      ui32_t frames = duration_samples / spf;
+      
+      if ( duration_samples % spf != 0 )
+       {
+         ++frames;
+       }
+
+      return frames;
     }
 
   } // namespace MXF
 
+
+  // IMF App 2 edit rates not already exposed in namespace ASDCP
+  const ASDCP::Rational EditRate_29_97 = ASDCP::Rational(30000, 1001);
+  const ASDCP::Rational EditRate_59_94 = ASDCP::Rational(60000, 1001);
+
   //---------------------------------------------------------------------------------
   // Accessors in the MXFReader and MXFWriter classes below return these types to
   // provide direct access to MXF metadata structures declared in MXF.h and Metadata.h
@@ -142,7 +153,7 @@ namespace AS_02
   };
  
   namespace JP2K
-  {
+  { 
     //
     class MXFWriter
     {
@@ -167,7 +178,7 @@ namespace AS_02
                         ASDCP::MXF::InterchangeObject_list_t& essence_sub_descriptor_list,
                         const ASDCP::Rational& edit_rate, const ui32_t& header_size = 16384,
                         const IndexStrategy_t& strategy = IS_FOLLOW, const ui32_t& partition_space = 10);
-      
+
       // Writes a frame of essence to the MXF file. If the optional AESEncContext
       // argument is present, the essence is encrypted prior to writing.
       // Fails if the file is not open, is finalized, or an operating system
@@ -238,7 +249,7 @@ namespace AS_02
     // the reader to signal the number of samples to be read by each call to ReadFrame();
 
     //
-    class MXFWriter
+      class MXFWriter
     {
       class h__Writer;
       ASDCP::mem_ptr<h__Writer> m_Writer;
@@ -290,7 +301,7 @@ namespace AS_02
 
       // Open the file for reading. The file must exist. Returns error if the
       // operation cannot be completed.
-      Result_t OpenRead(const std::string& filename, const ASDCP::Rational& EditRate);
+      Result_t OpenRead(const std::string& filename, const ASDCP::Rational& EditRate) const;
 
       // Returns RESULT_INIT if the file is not open.
       Result_t Close() const;
@@ -322,6 +333,27 @@ namespace AS_02
       using ASDCP::TimedText::TimedTextResourceDescriptor;
       using ASDCP::TimedText::ResourceList_t;
 
+      //
+      class Type5UUIDFilenameResolver : public ASDCP::TimedText::IResourceResolver
+       {
+         typedef std::map<Kumu::UUID, std::string> ResourceMap;
+           
+         ResourceMap m_ResourceMap;
+         std::string m_Dirname;
+         KM_NO_COPY_CONSTRUCT(Type5UUIDFilenameResolver);
+
+       public:
+         Type5UUIDFilenameResolver();
+         virtual ~Type5UUIDFilenameResolver();
+         Result_t OpenRead(const std::string& dirname);
+         Result_t ResolveRID(const byte_t* uuid, ASDCP::TimedText::FrameBuffer& FrameBuf) const;
+       };
+      
+
+      // Generate UUID asset ID values from file contents
+      Kumu::UUID CreatePNGNameId(const std::string& image_name);
+      Kumu::UUID CreateFontNameId(const std::string& font_name);
+
       //
       class ST2052_TextParser
        {
@@ -340,6 +372,11 @@ namespace AS_02
          // Parse an XML string 
          Result_t OpenRead(const std::string& xml_doc, const std::string& filename) const;
 
+         // The "profile_name" parameter was removed from OpenRead() because it made the API
+         // awkward WRT lexical compatibility with TimedText_Parser. The value can still be
+         // modified by changing the descriptor's NamespaceName property after the call to
+         // FillTimedTextDescriptor() has set it.
+                             
          // Fill a TimedTextDescriptor struct with the values from the file's contents.
          // Returns RESULT_INIT if the file is not open.
          Result_t FillTimedTextDescriptor(ASDCP::TimedText::TimedTextDescriptor&) const;
@@ -461,6 +498,98 @@ namespace AS_02
        };
     } // namespace TimedText
 
+  namespace ISXD
+  { 
+    //
+    class MXFWriter
+    {
+      class h__Writer;
+      ASDCP::mem_ptr<h__Writer> m_Writer;
+      ASDCP_NO_COPY_CONSTRUCT(MXFWriter);
+      
+    public:
+      MXFWriter();
+      virtual ~MXFWriter();
+
+      // Warning: direct manipulation of MXF structures can interfere
+      // with the normal operation of the wrapper.  Caveat emptor!
+      virtual ASDCP::MXF::OP1aHeader& OP1aHeader();
+      virtual ASDCP::MXF::RIP& RIP();
+
+      // Open the file for writing. The file must not exist. Returns error if
+      // the operation cannot be completed or if nonsensical data is discovered
+      // in the essence descriptor.
+      Result_t OpenWrite(const std::string& filename, const ASDCP::WriterInfo&,
+                        const std::string& isxd_document_namespace,
+                        const ASDCP::Rational& edit_rate, const ui32_t& header_size = 16384,
+                        const IndexStrategy_t& strategy = IS_FOLLOW, const ui32_t& partition_space = 10);
+
+      // Writes a frame of essence to the MXF file. If the optional AESEncContext
+      // argument is present, the essence is encrypted prior to writing.
+      // Fails if the file is not open, is finalized, or an operating system
+      // error occurs.
+      Result_t WriteFrame(const ASDCP::FrameBuffer&, ASDCP::AESEncContext* = 0, ASDCP::HMACContext* = 0);
+
+      // Writes an XML text document to the MXF file as per RP 2057. If the
+      // optional AESEncContext argument is present, the document is encrypted
+      // prior to writing. Fails if the file is not open, is finalized, or an
+      // operating system error occurs.
+      Result_t AddDmsGenericPartUtf8Text(const ASDCP::FrameBuffer& frame_buffer, ASDCP::AESEncContext* enc = 0, ASDCP::HMACContext* hmac = 0);
+
+      // Closes the MXF file, writing the index and revised header.
+      Result_t Finalize();
+    };
+
+    //
+    class MXFReader
+    {
+      class h__Reader;
+      ASDCP::mem_ptr<h__Reader> m_Reader;
+      ASDCP_NO_COPY_CONSTRUCT(MXFReader);
+
+    public:
+      MXFReader();
+      virtual ~MXFReader();
+
+      // Warning: direct manipulation of MXF structures can interfere
+      // with the normal operation of the wrapper.  Caveat emptor!
+      virtual ASDCP::MXF::OP1aHeader& OP1aHeader();
+      virtual AS_02::MXF::AS02IndexReader& AS02IndexReader();
+      virtual ASDCP::MXF::RIP& RIP();
+
+      // Open the file for reading. The file must exist. Returns error if the
+      // operation cannot be completed.
+      Result_t OpenRead(const std::string& filename) const;
+
+      // Returns RESULT_INIT if the file is not open.
+      Result_t Close() const;
+
+      // Fill a WriterInfo struct with the values from the file's header.
+      // Returns RESULT_INIT if the file is not open.
+      Result_t FillWriterInfo(ASDCP::WriterInfo&) const;
+
+      // Reads a frame of essence from the MXF file. If the optional AESEncContext
+      // argument is present, the essence is decrypted after reading. If the MXF
+      // file is encrypted and the AESDecContext argument is NULL, the frame buffer
+      // will contain the ciphertext frame data. If the HMACContext argument is
+      // not NULL, the HMAC will be calculated (if the file supports it).
+      // Returns RESULT_INIT if the file is not open, failure if the frame number is
+      // out of range, or if optional decrypt or HAMC operations fail.
+      Result_t ReadFrame(ui32_t frame_number, ASDCP::FrameBuffer&,
+                        ASDCP::AESDecContext* = 0, ASDCP::HMACContext* = 0) const;
+
+      // Reads a Generic Stream Partition payload. Returns RESULT_INIT if the file is
+      // not open, or RESULT_FORMAT if the SID is not present in the  RIP, or if the
+      // actual partition at ByteOffset does not have a matching BodySID value.
+      // Encryption is not currently supported.
+      Result_t ReadGenericStreamPartitionPayload(ui32_t SID, ASDCP::FrameBuffer& FrameBuf);
+  
+      // Print debugging information to stream
+      void     DumpHeaderMetadata(FILE* = 0) const;
+      void     DumpIndex(FILE* = 0) const;
+    };
+    
+  }
 
 } // namespace AS_02