ISXDDataEssenceDescriptor_NamespaceURI UL fixed
[asdcplib.git] / src / AtmosSyncChannel_Generator.cpp
1 /*
2 Copyright (c) 2013-2013, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27 /*! \file    AtmosSyncChannel_Generator.cpp
28     \version $Id$
29     \brief   Dolby Atmos sync channel generator implementation
30 */
31
32 #include <AtmosSyncChannel_Generator.h>
33
34 #include <AS_DCP.h>
35
36 using namespace ASDCP;
37
38 //
39 ASDCP::PCM::AtmosSyncChannelGenerator::AtmosSyncChannelGenerator(ui16_t bitsPerSample, ui32_t sampleRate,
40                                                                  const ASDCP::Rational& editRate, const byte_t* uuid)
41     : m_syncEncoder(),
42       m_audioTrackUUID(),
43       m_ADesc(),
44       m_syncSignalBuffer(NULL),
45       m_numSamplesPerFrame(0),
46       m_currentFrameNumber(0),
47       m_numBytesPerFrame(0),
48       m_isSyncEncoderInitialized(false)
49 {
50
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);
57
58     memcpy(&m_audioTrackUUID.abyUUIDBytes[0], uuid, UUIDlen);
59     m_numSamplesPerFrame = (editRate.Denominator * sampleRate) / editRate.Numerator;
60     m_numBytesPerFrame = m_numSamplesPerFrame * m_ADesc.BlockAlign;
61
62     if (bitsPerSample == 24)
63     {
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];
67     }
68     else
69     {
70         m_isSyncEncoderInitialized = false;
71     }
72 }
73
74 ASDCP::PCM::AtmosSyncChannelGenerator::~AtmosSyncChannelGenerator()
75 {
76     delete [] m_syncSignalBuffer;
77 }
78
79 ASDCP::Result_t
80 ASDCP::PCM::AtmosSyncChannelGenerator::ReadFrame(FrameBuffer& OutFB)
81 {
82     if (OutFB.Capacity() < m_numBytesPerFrame)
83     {
84         return RESULT_SMALLBUF;
85     }
86
87     /**
88      * Update frame number and size.
89      */
90     OutFB.FrameNumber(m_currentFrameNumber);
91     OutFB.Size(m_numBytesPerFrame);
92
93     /**
94      * Get pointer to frame essence.
95      */
96     byte_t* frameEssence = OutFB.Data();
97
98     if (m_isSyncEncoderInitialized)
99     {
100         /**
101          * Generate sync signal frame.
102          */
103         int ret = EncodeSync(&m_syncEncoder, m_numSamplesPerFrame, m_syncSignalBuffer, m_currentFrameNumber);
104         if (ret == SYNC_ENCODER_ERROR_NONE)
105         {
106             for (unsigned int i = 0; i < m_numSamplesPerFrame; ++i)
107             {
108                 /**
109                  * Convert each encoded float sample to a signed 24 bit integer and
110                  * copy into the essence buffer.
111                  */
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;
115             }
116         }
117         else
118         {
119             /**
120              * Encoding error, zero out the frame.
121              */
122             memset(frameEssence, 0, m_numBytesPerFrame);
123         }
124     }
125     else
126     {
127         /**
128          * Sync encoder not initialize, zero out the frame.
129          */
130         memset(frameEssence, 0, m_numBytesPerFrame);
131     }
132     ++m_currentFrameNumber;
133     return RESULT_OK;
134 }
135
136 ASDCP::Result_t
137 ASDCP::PCM::AtmosSyncChannelGenerator::Reset()
138 {
139   m_currentFrameNumber = 0;
140   return RESULT_OK;
141 }
142
143 Result_t
144 ASDCP::PCM::AtmosSyncChannelGenerator::FillAudioDescriptor(AudioDescriptor& ADesc) const
145 {
146   ADesc = m_ADesc;
147   return RESULT_OK;
148 }
149