Skip to content

Commit

Permalink
Merge pull request #15 from DolbyLaboratories/dolby/atmos_storage/asd…
Browse files Browse the repository at this point in the history
…cplib_integration/templatize_h02writerclip_depends_on_pluggable_filereader

Templatize h__AS02WriteClip, move functions definition to header. Note that this depends on filereader pluggable at runtime commit as the reader is called as a pointer here.
  • Loading branch information
jhursty authored Aug 27, 2021
2 parents 623b3ae + 301b00b commit 3e71fd6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 104 deletions.
6 changes: 3 additions & 3 deletions src/AS_02_PCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ AS_02::PCM::MXFReader::DumpIndex(FILE* stream) const
//------------------------------------------------------------------------------------------

//
class AS_02::PCM::MXFWriter::h__Writer : public AS_02::h__AS02WriterClip
class AS_02::PCM::MXFWriter::h__Writer : public AS_02::h__AS02WriterClip<AS_02::MXF::AS02IndexWriterCBR>
{
ASDCP_NO_COPY_CONSTRUCT(h__Writer);
h__Writer();
Expand All @@ -335,8 +335,8 @@ class AS_02::PCM::MXFWriter::h__Writer : public AS_02::h__AS02WriterClip
ASDCP::MXF::WaveAudioDescriptor *m_WaveAudioDescriptor;
byte_t m_EssenceUL[SMPTE_UL_LENGTH];
ui32_t m_BytesPerSample;

h__Writer(const Dictionary *d) : AS_02::h__AS02WriterClip(d), m_WaveAudioDescriptor(0), m_BytesPerSample(0)
h__Writer(const Dictionary *d) : AS_02::h__AS02WriterClip<AS_02::MXF::AS02IndexWriterCBR>(d), m_WaveAudioDescriptor(0), m_BytesPerSample(0)
{
memset(m_EssenceUL, 0, SMPTE_UL_LENGTH);
}
Expand Down
4 changes: 2 additions & 2 deletions src/AS_02_TimedText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ AS_02::TimedText::MXFReader::Close() const


//
class AS_02::TimedText::MXFWriter::h__Writer : public AS_02::h__AS02WriterClip
class AS_02::TimedText::MXFWriter::h__Writer : public AS_02::h__AS02WriterClip<AS_02::MXF::AS02IndexWriterCBR>
{
ASDCP_NO_COPY_CONSTRUCT(h__Writer);
h__Writer();
Expand All @@ -397,7 +397,7 @@ class AS_02::TimedText::MXFWriter::h__Writer : public AS_02::h__AS02WriterClip
ui32_t m_EssenceStreamID;
ASDCP::Rational m_EditRate;

h__Writer(const Dictionary *d) : AS_02::h__AS02WriterClip(d), m_EssenceStreamID(10)
h__Writer(const Dictionary *d) : AS_02::h__AS02WriterClip<AS_02::MXF::AS02IndexWriterCBR>(d), m_EssenceStreamID(10)
{
memset(m_EssenceUL, 0, SMPTE_UL_LENGTH);
}
Expand Down
115 changes: 102 additions & 13 deletions src/AS_02_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,112 @@ namespace AS_02
};

//
class h__AS02WriterClip : public h__AS02Writer<AS_02::MXF::AS02IndexWriterCBR>
template <typename IndexWriterType>
class h__AS02WriterClip : public h__AS02Writer<IndexWriterType>
{
ASDCP_NO_COPY_CONSTRUCT(h__AS02WriterClip);
h__AS02WriterClip();
h__AS02WriterClip() {}

public:
ui64_t m_ECStart; // offset of the first essence element
ui64_t m_ClipStart; // state variable for clip-wrap-in-progress
IndexStrategy_t m_IndexStrategy; // per SMPTE ST 2067-5

h__AS02WriterClip(const Dictionary*);
virtual ~h__AS02WriterClip();

bool HasOpenClip() const;
Result_t StartClip(const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC);
Result_t WriteClipBlock(const ASDCP::FrameBuffer& FrameBuf);
Result_t FinalizeClip(ui32_t bytes_per_frame);
ui64_t m_ECStart; // offset of the first essence element
ui64_t m_ClipStart; // state variable for clip-wrap-in-progress
IndexStrategy_t m_IndexStrategy; // per SMPTE ST 2067-5

h__AS02WriterClip(const Dictionary* d) :
h__AS02Writer<IndexWriterType>(d),
m_ECStart(0), m_ClipStart(0), m_IndexStrategy(AS_02::IS_FOLLOW)
{}
virtual ~h__AS02WriterClip()
{}

bool HasOpenClip() const { return m_ClipStart != 0; }
Result_t StartClip(const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC)
{
if (Ctx != 0)
{
DefaultLogSink().Error("Encryption not yet supported for PCM clip-wrap.\n");
return RESULT_STATE;
}

if (m_ClipStart != 0)
{
DefaultLogSink().Error("Cannot open clip, clip already open.\n");
return RESULT_STATE;
}

m_ClipStart = h__AS02Writer<IndexWriterType>::m_File.TellPosition();
byte_t clip_buffer[24] = { 0 };
memcpy(clip_buffer, EssenceUL, 16);
bool check = Kumu::write_BER(clip_buffer + 16, 0, 8);
assert(check);
return h__AS02Writer<IndexWriterType>::m_File.Write(clip_buffer, 24);
}
Result_t WriteClipBlock(const ASDCP::FrameBuffer& FrameBuf)
{
if (m_ClipStart == 0)
{
DefaultLogSink().Error("Cannot write clip block, no clip open.\n");
return RESULT_STATE;
}

return h__AS02Writer<IndexWriterType>::m_File.Write(FrameBuf.RoData(), FrameBuf.Size());
}
Result_t FinalizeClip(ui32_t bytes_per_frame)
{
if (m_ClipStart == 0)
{
DefaultLogSink().Error("Cannot close clip, clip not open.\n");
return RESULT_STATE;
}

ui64_t current_position = h__AS02Writer<IndexWriterType>::m_File.TellPosition();
Result_t result = h__AS02Writer<IndexWriterType>::m_File.Seek(m_ClipStart + 16);

if (KM_SUCCESS(result))
{
byte_t clip_buffer[8] = { 0 };
ui64_t size = static_cast<ui64_t>(h__AS02Writer<IndexWriterType>::m_FramesWritten) * bytes_per_frame;
bool check = Kumu::write_BER(clip_buffer, size, 8);
assert(check);
result = h__AS02Writer<IndexWriterType>::m_File.Write(clip_buffer, 8);
}

if (KM_SUCCESS(result))
{
result = h__AS02Writer<IndexWriterType>::m_File.Seek(current_position);
m_ClipStart = 0;
}

return result;
}
Result_t FinalizeClip(ui64_t total_bytes_written)
{
if (m_ClipStart == 0)
{
DefaultLogSink().Error("Cannot close clip, clip not open.\n");
return RESULT_STATE;
}

ui64_t current_position = h__AS02Writer<IndexWriterType>::m_File.TellPosition();
Result_t result = h__AS02Writer<IndexWriterType>::m_File.Seek(m_ClipStart + 16);

if (KM_SUCCESS(result))
{
byte_t clip_buffer[8] = { 0 };
bool check = Kumu::write_BER(clip_buffer, total_bytes_written, 8);
assert(check);
result = h__AS02Writer<IndexWriterType>::m_File.Write(clip_buffer, 8);
}

if (KM_SUCCESS(result))
{
result = h__AS02Writer<IndexWriterType>::m_File.Seek(current_position);
m_ClipStart = 0;
}

return result;

}
};

} // namespace AS_02
Expand Down
86 changes: 0 additions & 86 deletions src/h__02_Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,89 +309,3 @@ AS_02::MXF::AS02IndexWriterCBR::SetEditRate(const ASDCP::Rational& edit_rate, co

//------------------------------------------------------------------------------------------
//

//
AS_02::h__AS02WriterClip::h__AS02WriterClip(const ASDCP::Dictionary* d) :
h__AS02Writer<AS_02::MXF::AS02IndexWriterCBR>(d),
m_ECStart(0), m_ClipStart(0), m_IndexStrategy(AS_02::IS_FOLLOW) {}

AS_02::h__AS02WriterClip::~h__AS02WriterClip() {}

//
bool
AS_02::h__AS02WriterClip::HasOpenClip() const
{
return m_ClipStart != 0;
}

//
Result_t
AS_02::h__AS02WriterClip::StartClip(const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext*)
{
if ( Ctx != 0 )
{
DefaultLogSink().Error("Encryption not yet supported for PCM clip-wrap.\n");
return RESULT_STATE;
}

if ( m_ClipStart != 0 )
{
DefaultLogSink().Error("Cannot open clip, clip already open.\n");
return RESULT_STATE;
}

m_ClipStart = m_File.Tell();
byte_t clip_buffer[24] = {0};
memcpy(clip_buffer, EssenceUL, 16);
bool check = Kumu::write_BER(clip_buffer+16, 0, 8);
assert(check);
return m_File.Write(clip_buffer, 24);
}

//
Result_t
AS_02::h__AS02WriterClip::WriteClipBlock(const ASDCP::FrameBuffer& FrameBuf)
{
if ( m_ClipStart == 0 )
{
DefaultLogSink().Error("Cannot write clip block, no clip open.\n");
return RESULT_STATE;
}

return m_File.Write(FrameBuf.RoData(), FrameBuf.Size());
}

//
Result_t
AS_02::h__AS02WriterClip::FinalizeClip(ui32_t bytes_per_frame)
{
if ( m_ClipStart == 0 )
{
DefaultLogSink().Error("Cannot close clip, clip not open.\n");
return RESULT_STATE;
}

ui64_t current_position = m_File.Tell();
Result_t result = m_File.Seek(m_ClipStart+16);

if ( KM_SUCCESS(result) )
{
byte_t clip_buffer[8] = {0};
ui64_t size = static_cast<ui64_t>(m_FramesWritten) * bytes_per_frame;
bool check = Kumu::write_BER(clip_buffer, size, 8);
assert(check);
result = m_File.Write(clip_buffer, 8);
}

if ( KM_SUCCESS(result) )
{
result = m_File.Seek(current_position);
m_ClipStart = 0;
}

return result;
}

//
// end h__02_Writer.cpp
//

0 comments on commit 3e71fd6

Please sign in to comment.