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 JP2K_Sequence_Parser.cpp
29 \brief AS-DCP library, JPEG 2000 codestream essence reader implementation
33 #include <KM_fileio.h>
41 using namespace ASDCP;
44 //------------------------------------------------------------------------------------------
46 class FileList : public std::list<std::string>
48 std::string m_DirName;
54 const FileList& operator=(const std::list<std::string>& pathlist) {
55 std::list<std::string>::const_iterator i;
56 for ( i = pathlist.begin(); i != pathlist.end(); i++ )
62 Result_t InitFromDirectory(const std::string& path)
64 char next_file[Kumu::MaxFilePath];
65 Kumu::DirScanner Scanner;
67 Result_t result = Scanner.Open(path);
69 if ( ASDCP_SUCCESS(result) )
73 while ( ASDCP_SUCCESS(Scanner.GetNext(next_file)) )
75 if ( next_file[0] == '.' ) // no hidden files or internal links
78 std::string Str(m_DirName);
82 if ( ! Kumu::PathIsDirectory(Str) )
93 //------------------------------------------------------------------------------------------
95 class ASDCP::JP2K::SequenceParser::h__SequenceParser
98 Rational m_PictureRate;
100 FileList::iterator m_CurrentFile;
101 CodestreamParser m_Parser;
106 ASDCP_NO_COPY_CONSTRUCT(h__SequenceParser);
109 PictureDescriptor m_PDesc;
111 h__SequenceParser() : m_FramesRead(0), m_Pedantic(false)
113 memset(&m_PDesc, 0, sizeof(m_PDesc));
114 m_PDesc.EditRate = Rational(24,1);
122 Result_t OpenRead(const std::string& filename, bool pedantic);
123 Result_t OpenRead(const std::list<std::string>& file_list, bool pedantic);
129 m_CurrentFile = m_FileList.begin();
133 Result_t ReadFrame(FrameBuffer&);
139 ASDCP::JP2K::SequenceParser::h__SequenceParser::OpenRead()
141 if ( m_FileList.empty() )
142 return RESULT_ENDOFFILE;
144 m_CurrentFile = m_FileList.begin();
145 CodestreamParser Parser;
146 FrameBuffer TmpBuffer;
148 Kumu::fsize_t file_size = Kumu::FileSize((*m_CurrentFile).c_str());
150 if ( file_size == 0 )
151 return RESULT_NOT_FOUND;
153 assert(file_size <= 0xFFFFFFFFL);
154 Result_t result = TmpBuffer.Capacity((ui32_t) file_size);
156 if ( ASDCP_SUCCESS(result) )
157 result = Parser.OpenReadFrame((*m_CurrentFile).c_str(), TmpBuffer);
159 if ( ASDCP_SUCCESS(result) )
160 result = Parser.FillPictureDescriptor(m_PDesc);
163 if ( ASDCP_SUCCESS(result) )
164 m_PDesc.ContainerDuration = m_FileList.size();
171 ASDCP::JP2K::SequenceParser::h__SequenceParser::OpenRead(const std::string& filename, bool pedantic)
173 m_Pedantic = pedantic;
175 Result_t result = m_FileList.InitFromDirectory(filename);
177 if ( ASDCP_SUCCESS(result) )
186 ASDCP::JP2K::SequenceParser::h__SequenceParser::OpenRead(const std::list<std::string>& file_list, bool pedantic)
188 m_Pedantic = pedantic;
189 m_FileList = file_list;
196 operator==(const ASDCP::JP2K::ImageComponent_t& lhs, const ASDCP::JP2K::ImageComponent_t& rhs)
198 if ( lhs.Ssize != rhs.Ssize ) return false;
199 if ( lhs.XRsize != rhs.XRsize ) return false;
200 if ( lhs.YRsize != rhs.YRsize ) return false;
206 operator==(const ASDCP::JP2K::QuantizationDefault_t& lhs, const ASDCP::JP2K::QuantizationDefault_t& rhs)
208 if ( lhs.Sqcd != rhs.Sqcd ) return false;
209 if ( lhs.SPqcdLength != rhs.SPqcdLength ) return false;
211 for ( ui32_t i = 0; i < JP2K::MaxDefaults; i++ )
213 if ( lhs.SPqcd[i] != rhs.SPqcd[i] )
222 operator==(const ASDCP::JP2K::CodingStyleDefault_t& lhs, const ASDCP::JP2K::CodingStyleDefault_t& rhs)
224 if ( lhs.Scod != rhs.Scod ) return false;
227 if ( lhs.SGcod.ProgressionOrder != rhs.SGcod.ProgressionOrder ) return false;
228 if ( lhs.SGcod.MultiCompTransform != rhs.SGcod.MultiCompTransform ) return false;
230 for ( ui32_t i = 0; i < sizeof(ui16_t); i++ )
232 if ( lhs.SGcod.NumberOfLayers[i] != lhs.SGcod.NumberOfLayers[i] )
237 if ( lhs.SPcod.DecompositionLevels != rhs.SPcod.DecompositionLevels ) return false;
238 if ( lhs.SPcod.CodeblockWidth != rhs.SPcod.CodeblockWidth ) return false;
239 if ( lhs.SPcod.CodeblockHeight != rhs.SPcod.CodeblockHeight ) return false;
240 if ( lhs.SPcod.CodeblockStyle != rhs.SPcod.CodeblockStyle ) return false;
241 if ( lhs.SPcod.Transformation != rhs.SPcod.Transformation ) return false;
243 for ( ui32_t i = 0; i < JP2K::MaxPrecincts; i++ )
245 if ( lhs.SPcod.PrecinctSize[i] != rhs.SPcod.PrecinctSize[i] )
254 operator==(const ASDCP::JP2K::ExtendedCapabilities_t& lhs, const ASDCP::JP2K::ExtendedCapabilities_t& rhs)
256 if (lhs.Pcap != rhs.Pcap) return false;
258 for (ui32_t i = 0; i < JP2K::MaxCapabilities; i++)
260 if (lhs.Ccap[i] != rhs.Ccap[i])
269 operator==(const ASDCP::JP2K::CorrespondingProfile_t& lhs, const ASDCP::JP2K::CorrespondingProfile_t& rhs)
271 if (lhs.N != rhs.N) return false;
273 for (ui32_t i = 0; i < lhs.N; i++)
275 if (lhs.Pcpf[i] != rhs.Pcpf[i])
284 operator==(const ASDCP::JP2K::Profile_t& lhs, const ASDCP::JP2K::Profile_t& rhs)
286 if (lhs.N != rhs.N) return false;
288 for (ui32_t i = 0; i < lhs.N; i++)
290 if (lhs.Pprf[i] != rhs.Pprf[i])
299 operator==(const ASDCP::JP2K::PictureDescriptor& lhs, const ASDCP::JP2K::PictureDescriptor& rhs)
301 if ( lhs.EditRate != rhs.EditRate ) return false;
302 // if ( lhs.ContainerDuration != rhs.ContainerDuration ) return false;
303 if ( lhs.SampleRate != rhs.SampleRate ) return false;
304 if ( lhs.StoredWidth != rhs.StoredWidth ) return false;
305 if ( lhs.StoredHeight != rhs.StoredHeight ) return false;
306 if ( lhs.AspectRatio != rhs.AspectRatio ) return false;
307 if ( lhs.Rsize != rhs.Rsize ) return false;
308 if ( lhs.Xsize != rhs.Xsize ) return false;
309 if ( lhs.Ysize != rhs.Ysize ) return false;
310 if ( lhs.XOsize != rhs.XOsize ) return false;
311 if ( lhs.YOsize != rhs.YOsize ) return false;
312 if ( lhs.XTsize != rhs.XTsize ) return false;
313 if ( lhs.YTsize != rhs.YTsize ) return false;
314 if ( lhs.XTOsize != rhs.XTOsize ) return false;
315 if ( lhs.YTOsize != rhs.YTOsize ) return false;
316 if ( lhs.Csize != rhs.Csize ) return false;
317 if ( ! ( lhs.CodingStyleDefault == rhs.CodingStyleDefault ) ) return false;
318 if ( ! ( lhs.QuantizationDefault == rhs.QuantizationDefault ) ) return false;
319 if (!(lhs.Profile == rhs.Profile)) return false;
320 if (!(lhs.CorrespondingProfile == rhs.CorrespondingProfile)) return false;
321 if (!(lhs.ExtendedCapabilities == rhs.ExtendedCapabilities)) return false;
323 for ( ui32_t i = 0; i < JP2K::MaxComponents; i++ )
325 if ( ! ( lhs.ImageComponents[i] == rhs.ImageComponents[i] ) )
334 ASDCP::JP2K::SequenceParser::h__SequenceParser::ReadFrame(FrameBuffer& FB)
336 if ( m_CurrentFile == m_FileList.end() )
337 return RESULT_ENDOFFILE;
340 Result_t result = m_Parser.OpenReadFrame((*m_CurrentFile).c_str(), FB);
342 if ( ASDCP_SUCCESS(result) && m_Pedantic )
344 PictureDescriptor PDesc;
345 result = m_Parser.FillPictureDescriptor(PDesc);
347 if ( ASDCP_SUCCESS(result) && ! ( m_PDesc == PDesc ) )
349 Kumu::DefaultLogSink().Error("JPEG-2000 codestream parameters do not match at frame %d\n", m_FramesRead + 1);
350 result = RESULT_RAW_FORMAT;
354 if ( ASDCP_SUCCESS(result) )
356 FB.FrameNumber(m_FramesRead++);
364 //------------------------------------------------------------------------------------------
366 ASDCP::JP2K::SequenceParser::SequenceParser()
370 ASDCP::JP2K::SequenceParser::~SequenceParser()
374 // Opens the stream for reading, parses enough data to provide a complete
375 // set of stream metadata for the MXFWriter below.
377 ASDCP::JP2K::SequenceParser::OpenRead(const std::string& filename, bool pedantic) const
379 const_cast<ASDCP::JP2K::SequenceParser*>(this)->m_Parser = new h__SequenceParser;
381 Result_t result = m_Parser->OpenRead(filename, pedantic);
383 if ( ASDCP_FAILURE(result) )
384 const_cast<ASDCP::JP2K::SequenceParser*>(this)->m_Parser.release();
391 ASDCP::JP2K::SequenceParser::OpenRead(const std::list<std::string>& file_list, bool pedantic) const
393 const_cast<ASDCP::JP2K::SequenceParser*>(this)->m_Parser = new h__SequenceParser;
395 Result_t result = m_Parser->OpenRead(file_list, pedantic);
397 if ( ASDCP_FAILURE(result) )
398 const_cast<ASDCP::JP2K::SequenceParser*>(this)->m_Parser.release();
404 // Rewinds the stream to the beginning.
406 ASDCP::JP2K::SequenceParser::Reset() const
408 if ( m_Parser.empty() )
411 return m_Parser->Reset();
414 // Places a frame of data in the frame buffer. Fails if the buffer is too small
415 // or the stream is empty.
417 ASDCP::JP2K::SequenceParser::ReadFrame(FrameBuffer& FB) const
419 if ( m_Parser.empty() )
422 return m_Parser->ReadFrame(FB);
427 ASDCP::JP2K::SequenceParser::FillPictureDescriptor(PictureDescriptor& PDesc) const
429 if ( m_Parser.empty() )
432 PDesc = m_Parser->m_PDesc;
438 // end JP2K_Sequence_Parser.cpp