2 Copyright (c) 2004-2011, 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 PCM_Parser.cpp
29 \brief AS-DCP library, PCM raw essence reader implementation
35 using Kumu::DefaultLogSink;
37 using namespace ASDCP;
38 using namespace ASDCP::PCM;
39 using namespace ASDCP::Wav;
40 using namespace ASDCP::RF64;
43 //------------------------------------------------------------------------------------------
46 class ASDCP::PCM::WAVParser::h__WAVParser
48 Kumu::FileReader m_FileReader;
53 ui32_t m_FrameBufferSize;
55 Rational m_PictureRate;
57 ASDCP_NO_COPY_CONSTRUCT(h__WAVParser);
60 AudioDescriptor m_ADesc;
64 m_EOF(false), m_DataStart(0), m_DataLength(0), m_ReadCount(0),
65 m_FrameBufferSize(0), m_FramesRead(0) {}
72 Result_t OpenRead(const std::string& filename, const Rational& PictureRate);
75 Result_t ReadFrame(FrameBuffer&);
76 Result_t Seek(ui32_t frame_number);
82 ASDCP::PCM::WAVParser::h__WAVParser::Close()
89 ASDCP::PCM::WAVParser::h__WAVParser::Reset()
91 m_FileReader.Seek(m_DataStart);
98 ASDCP::PCM::WAVParser::h__WAVParser::OpenRead(const std::string& filename, const Rational& PictureRate)
100 Result_t result = m_FileReader.OpenRead(filename);
102 if ( ASDCP_SUCCESS(result) )
104 SimpleWaveHeader WavHeader;
105 result = WavHeader.ReadFromFile(m_FileReader, &m_DataStart);
107 if ( ASDCP_SUCCESS(result) )
109 WavHeader.FillADesc(m_ADesc, PictureRate);
110 m_FrameBufferSize = ASDCP::PCM::CalcFrameBufferSize(m_ADesc);
111 m_DataLength = WavHeader.data_len;
112 m_ADesc.ContainerDuration = m_DataLength / m_FrameBufferSize;
113 m_ADesc.ChannelFormat = PCM::CF_NONE;
118 ASDCP::AIFF::SimpleAIFFHeader AIFFHeader;
119 m_FileReader.Seek(0);
121 result = AIFFHeader.ReadFromFile(m_FileReader, &m_DataStart);
123 if ( ASDCP_SUCCESS(result) )
125 AIFFHeader.FillADesc(m_ADesc, PictureRate);
126 m_FrameBufferSize = ASDCP::PCM::CalcFrameBufferSize(m_ADesc);
127 m_DataLength = AIFFHeader.data_len;
128 m_ADesc.ContainerDuration = m_DataLength / m_FrameBufferSize;
129 m_ADesc.ChannelFormat = PCM::CF_NONE;
134 SimpleRF64Header RF64Header;
135 m_FileReader.Seek(0);
136 result = RF64Header.ReadFromFile(m_FileReader, &m_DataStart);
138 if ( ASDCP_SUCCESS(result) )
140 RF64Header.FillADesc(m_ADesc, PictureRate);
141 m_FrameBufferSize = ASDCP::PCM::CalcFrameBufferSize(m_ADesc);
142 m_DataLength = RF64Header.data_len;
143 m_ADesc.ContainerDuration = m_DataLength / m_FrameBufferSize;
144 m_ADesc.ChannelFormat = PCM::CF_NONE;
156 ASDCP::PCM::WAVParser::h__WAVParser::ReadFrame(FrameBuffer& FB)
162 return RESULT_ENDOFFILE;
165 if ( FB.Capacity() < m_FrameBufferSize )
167 DefaultLogSink().Error("FrameBuf.Capacity: %u FrameLength: %u\n",
168 FB.Capacity(), m_FrameBufferSize);
169 return RESULT_SMALLBUF;
172 ui32_t read_count = 0;
173 Result_t result = m_FileReader.Read(FB.Data(), m_FrameBufferSize, &read_count);
175 if ( result == RESULT_ENDOFFILE )
179 if ( read_count > 0 )
185 if ( ASDCP_SUCCESS(result) )
187 m_ReadCount += read_count;
189 FB.FrameNumber(m_FramesRead++);
191 if ( read_count < FB.Capacity() )
193 memset(FB.Data() + FB.Size(), 0, FB.Capacity() - FB.Size());
201 ASDCP::Result_t ASDCP::PCM::WAVParser::h__WAVParser::Seek(ui32_t frame_number)
203 m_FramesRead = frame_number - 1;
205 return m_FileReader.Seek(m_DataStart + m_FrameBufferSize * frame_number);
209 //------------------------------------------------------------------------------------------
211 ASDCP::PCM::WAVParser::WAVParser()
215 ASDCP::PCM::WAVParser::~WAVParser()
219 // Opens the stream for reading, parses enough data to provide a complete
220 // set of stream metadata for the MXFWriter below.
222 ASDCP::PCM::WAVParser::OpenRead(const std::string& filename, const Rational& PictureRate) const
224 const_cast<ASDCP::PCM::WAVParser*>(this)->m_Parser = new h__WAVParser;
226 Result_t result = m_Parser->OpenRead(filename, PictureRate);
228 if ( ASDCP_FAILURE(result) )
229 const_cast<ASDCP::PCM::WAVParser*>(this)->m_Parser.release();
234 // Rewinds the stream to the beginning.
236 ASDCP::PCM::WAVParser::Reset() const
238 if ( m_Parser.empty() )
245 // Places a frame of data in the frame buffer. Fails if the buffer is too small
246 // or the stream is empty.
248 ASDCP::PCM::WAVParser::ReadFrame(FrameBuffer& FB) const
250 if ( m_Parser.empty() )
253 return m_Parser->ReadFrame(FB);
257 ASDCP::PCM::WAVParser::FillAudioDescriptor(AudioDescriptor& ADesc) const
259 if ( m_Parser.empty() )
262 ADesc = m_Parser->m_ADesc;
266 ASDCP::Result_t ASDCP::PCM::WAVParser::Seek(ui32_t frame_number) const
268 if ( m_Parser.empty() )
271 return m_Parser->Seek(frame_number);;
275 // end PCM_Parser.cpp