2 Copyright (c) 2004-2006, 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;
42 //------------------------------------------------------------------------------------------
45 class ASDCP::PCM::WAVParser::h__WAVParser
47 Kumu::FileReader m_FileReader;
52 ui32_t m_FrameBufferSize;
54 Rational m_PictureRate;
56 ASDCP_NO_COPY_CONSTRUCT(h__WAVParser);
59 AudioDescriptor m_ADesc;
63 m_EOF(false), m_DataStart(0), m_DataLength(0), m_ReadCount(0),
64 m_FrameBufferSize(0), m_FramesRead(0) {}
71 Result_t OpenRead(const char* filename, const Rational& PictureRate);
74 Result_t ReadFrame(FrameBuffer&);
80 ASDCP::PCM::WAVParser::h__WAVParser::Close()
87 ASDCP::PCM::WAVParser::h__WAVParser::Reset()
89 m_FileReader.Seek(m_DataStart);
96 ASDCP::PCM::WAVParser::h__WAVParser::OpenRead(const char* filename, const Rational& PictureRate)
98 ASDCP_TEST_NULL_STR(filename);
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;
117 ASDCP::AIFF::SimpleAIFFHeader AIFFHeader;
118 m_FileReader.Seek(0);
120 result = AIFFHeader.ReadFromFile(m_FileReader, &m_DataStart);
122 if ( ASDCP_SUCCESS(result) )
124 AIFFHeader.FillADesc(m_ADesc, PictureRate);
125 m_FrameBufferSize = ASDCP::PCM::CalcFrameBufferSize(m_ADesc);
126 m_DataLength = AIFFHeader.data_len;
127 m_ADesc.ContainerDuration = m_DataLength / m_FrameBufferSize;
138 ASDCP::PCM::WAVParser::h__WAVParser::ReadFrame(FrameBuffer& FB)
142 if ( m_EOF || m_ReadCount >= m_DataLength )
143 return RESULT_ENDOFFILE;
145 if ( FB.Capacity() < m_FrameBufferSize )
147 DefaultLogSink().Error("FrameBuf.Capacity: %lu FrameLength: %lu\n",
148 FB.Capacity(), m_FrameBufferSize);
149 return RESULT_SMALLBUF;
152 ui32_t read_count = 0;
153 Result_t result = m_FileReader.Read(FB.Data(), m_FrameBufferSize, &read_count);
155 if ( result == RESULT_ENDOFFILE )
159 if ( read_count > 0 )
163 if ( ASDCP_SUCCESS(result) )
165 m_ReadCount += read_count;
167 FB.FrameNumber(m_FramesRead++);
174 //------------------------------------------------------------------------------------------
176 ASDCP::PCM::WAVParser::WAVParser()
180 ASDCP::PCM::WAVParser::~WAVParser()
184 // Opens the stream for reading, parses enough data to provide a complete
185 // set of stream metadata for the MXFWriter below.
187 ASDCP::PCM::WAVParser::OpenRead(const char* filename, const Rational& PictureRate) const
189 const_cast<ASDCP::PCM::WAVParser*>(this)->m_Parser = new h__WAVParser;
191 Result_t result = m_Parser->OpenRead(filename, PictureRate);
193 if ( ASDCP_FAILURE(result) )
194 const_cast<ASDCP::PCM::WAVParser*>(this)->m_Parser.release();
199 // Rewinds the stream to the beginning.
201 ASDCP::PCM::WAVParser::Reset() const
203 if ( m_Parser.empty() )
210 // Places a frame of data in the frame buffer. Fails if the buffer is too small
211 // or the stream is empty.
213 ASDCP::PCM::WAVParser::ReadFrame(FrameBuffer& FB) const
215 if ( m_Parser.empty() )
218 return m_Parser->ReadFrame(FB);
222 ASDCP::PCM::WAVParser::FillAudioDescriptor(AudioDescriptor& ADesc) const
224 if ( m_Parser.empty() )
227 ADesc = m_Parser->m_ADesc;
233 // end PCM_Parser.cpp