2 Copyright (c) 2013-2013, 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 AtmosSyncChannel_Generator.cpp
29 \brief Dolby Atmos sync channel generator implementation
32 #include <AtmosSyncChannel_Generator.h>
36 using namespace ASDCP;
39 ASDCP::PCM::AtmosSyncChannelGenerator::AtmosSyncChannelGenerator(ui16_t bitsPerSample, ui32_t sampleRate,
40 const ASDCP::Rational& editRate, const byte_t* uuid)
44 m_syncSignalBuffer(NULL),
45 m_numSamplesPerFrame(0),
46 m_currentFrameNumber(0),
47 m_numBytesPerFrame(0),
48 m_isSyncEncoderInitialized(false)
51 m_ADesc.EditRate = editRate;
52 m_ADesc.ChannelCount = 1;
53 m_ADesc.QuantizationBits = bitsPerSample;
54 m_ADesc.AudioSamplingRate = Rational(sampleRate, 1);
55 m_ADesc.BlockAlign = ((bitsPerSample + 7) / 8);
56 m_ADesc.AvgBps = (sampleRate * m_ADesc.BlockAlign);
58 memcpy(&m_audioTrackUUID.abyUUIDBytes[0], uuid, UUIDlen);
59 m_numSamplesPerFrame = (editRate.Denominator * sampleRate) / editRate.Numerator;
60 m_numBytesPerFrame = m_numSamplesPerFrame * m_ADesc.BlockAlign;
62 if (bitsPerSample == 24)
64 ui32_t frameRate = editRate.Numerator/editRate.Denominator; // intentionally allowing for imprecise cast to int
65 m_isSyncEncoderInitialized = (SyncEncoderInit(&m_syncEncoder, sampleRate, frameRate, &m_audioTrackUUID) == SYNC_ENCODER_ERROR_NONE);
66 m_syncSignalBuffer = new float[m_numSamplesPerFrame];
70 m_isSyncEncoderInitialized = false;
74 ASDCP::PCM::AtmosSyncChannelGenerator::~AtmosSyncChannelGenerator()
76 delete [] m_syncSignalBuffer;
80 ASDCP::PCM::AtmosSyncChannelGenerator::ReadFrame(FrameBuffer& OutFB)
82 if (OutFB.Capacity() < m_numBytesPerFrame)
84 return RESULT_SMALLBUF;
88 * Update frame number and size.
90 OutFB.FrameNumber(m_currentFrameNumber);
91 OutFB.Size(m_numBytesPerFrame);
94 * Get pointer to frame essence.
96 byte_t* frameEssence = OutFB.Data();
98 if (m_isSyncEncoderInitialized)
101 * Generate sync signal frame.
103 int ret = EncodeSync(&m_syncEncoder, m_numSamplesPerFrame, m_syncSignalBuffer, m_currentFrameNumber);
104 if (ret == SYNC_ENCODER_ERROR_NONE)
106 for (unsigned int i = 0; i < m_numSamplesPerFrame; ++i)
109 * Convert each encoded float sample to a signed 24 bit integer and
110 * copy into the essence buffer.
112 i32_t sample = convertSampleFloatToInt24(m_syncSignalBuffer[i]);
113 memcpy(frameEssence, ((byte_t*)(&sample))+1, NUM_BYTES_PER_INT24);
114 frameEssence += NUM_BYTES_PER_INT24;
120 * Encoding error, zero out the frame.
122 memset(frameEssence, 0, m_numBytesPerFrame);
128 * Sync encoder not initialize, zero out the frame.
130 memset(frameEssence, 0, m_numBytesPerFrame);
132 ++m_currentFrameNumber;
137 ASDCP::PCM::AtmosSyncChannelGenerator::Reset()
139 m_currentFrameNumber = 0;
144 ASDCP::PCM::AtmosSyncChannelGenerator::FillAudioDescriptor(AudioDescriptor& ADesc) const