2 Copyright (c) 2004-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 Codestream_Parser.cpp
29 \brief AS-DCP library, JPEG 2000 codestream essence reader implementation
32 #include <KM_fileio.h>
37 using Kumu::DefaultLogSink;
39 //------------------------------------------------------------------------------------------
41 class ASDCP::JP2K::CodestreamParser::h__CodestreamParser
43 ASDCP_NO_COPY_CONSTRUCT(h__CodestreamParser);
46 PictureDescriptor m_PDesc;
47 Kumu::FileReader m_File;
51 memset(&m_PDesc, 0, sizeof(m_PDesc));
52 m_PDesc.EditRate = Rational(24,1);
53 m_PDesc.SampleRate = m_PDesc.EditRate;
56 ~h__CodestreamParser() {}
58 Result_t OpenReadFrame(const std::string& filename, FrameBuffer& FB)
61 Result_t result = m_File.OpenRead(filename);
63 if ( ASDCP_SUCCESS(result) )
65 Kumu::fsize_t file_size = m_File.Size();
67 if ( FB.Capacity() < file_size )
69 DefaultLogSink().Error("FrameBuf.Capacity: %u frame length: %u\n", FB.Capacity(), (ui32_t)file_size);
70 return RESULT_SMALLBUF;
76 if ( ASDCP_SUCCESS(result) )
77 result = m_File.Read(FB.Data(), FB.Capacity(), &read_count);
79 if ( ASDCP_SUCCESS(result) )
82 if ( ASDCP_SUCCESS(result) )
84 byte_t start_of_data = 0; // out param
85 result = ParseMetadataIntoDesc(FB, m_PDesc, &start_of_data);
87 if ( ASDCP_SUCCESS(result) )
88 FB.PlaintextOffset(start_of_data);
96 ASDCP::JP2K::ParseMetadataIntoDesc(const FrameBuffer& FB, PictureDescriptor& PDesc, byte_t* start_of_data)
98 Result_t result = RESULT_OK;
101 const byte_t* p = FB.RoData();
102 const byte_t* end_p = p + FB.Size();
104 while ( p < end_p && ASDCP_SUCCESS(result) )
106 result = GetNextMarker(&p, NextMarker);
108 if ( ASDCP_FAILURE(result) )
110 result = RESULT_RAW_ESS;
114 switch ( NextMarker.m_Type )
117 if ( start_of_data != 0 )
118 *start_of_data = p - FB.RoData();
125 Accessor::SIZ SIZ_(NextMarker);
126 PDesc.StoredWidth = SIZ_.Xsize();
127 PDesc.StoredHeight = SIZ_.Ysize();
128 PDesc.AspectRatio = Rational(SIZ_.Xsize(), SIZ_.Ysize());
129 PDesc.Rsize = SIZ_.Rsize();
130 PDesc.Xsize = SIZ_.Xsize();
131 PDesc.Ysize = SIZ_.Ysize();
132 PDesc.XOsize = SIZ_.XOsize();
133 PDesc.YOsize = SIZ_.YOsize();
134 PDesc.XTsize = SIZ_.XTsize();
135 PDesc.YTsize = SIZ_.YTsize();
136 PDesc.XTOsize = SIZ_.XTOsize();
137 PDesc.YTOsize = SIZ_.YTOsize();
138 PDesc.Csize = SIZ_.Csize();
140 if ( PDesc.Csize != 3 )
142 DefaultLogSink().Error("Unexpected number of components: %u\n", PDesc.Csize);
143 return RESULT_RAW_FORMAT;
146 for ( i = 0; i < PDesc.Csize; i++ )
147 SIZ_.ReadComponent(i, PDesc.ImageComponents[i]);
152 memset(&PDesc.CodingStyleDefault, 0, sizeof(CodingStyleDefault_t));
154 if ( NextMarker.m_DataSize > sizeof(CodingStyleDefault_t) )
156 DefaultLogSink().Error("Unexpectedly large CodingStyle data: %u\n", NextMarker.m_DataSize);
157 return RESULT_RAW_FORMAT;
160 memcpy(&PDesc.CodingStyleDefault, NextMarker.m_Data, NextMarker.m_DataSize);
164 memset(&PDesc.QuantizationDefault, 0, sizeof(QuantizationDefault_t));
166 if ( NextMarker.m_DataSize < 3 ) // ( Sqcd = 8 bits, SPqcd = 8 bits ) == 2 bytes, error if not greater
168 DefaultLogSink().Error("No quantization signaled. QCD size=%s.\n", NextMarker.m_DataSize);
169 return RESULT_RAW_FORMAT;
172 if ( NextMarker.m_DataSize > MaxDefaults )
174 DefaultLogSink().Error("Quantization Default length exceeds maximum %d\n", NextMarker.m_DataSize);
175 return RESULT_RAW_FORMAT;
178 memcpy(&PDesc.QuantizationDefault, NextMarker.m_Data, NextMarker.m_DataSize);
179 PDesc.QuantizationDefault.SPqcdLength = NextMarker.m_DataSize - 1;
184 Accessor::CAP CAP_(NextMarker);
186 PDesc.ExtendedCapabilities.Pcap = CAP_.pcap();
188 for(i32_t b = 32, i = 1; b > 0; b--) {
190 if ( (PDesc.ExtendedCapabilities.Pcap >> (b - 1)) & 0x1 ) {
192 PDesc.ExtendedCapabilities.Ccap[32 - b] = CAP_.ccap(i++);
196 PDesc.ExtendedCapabilities.Ccap[32 - b] = 0;
206 Accessor::PRF PRF_(NextMarker);
212 DefaultLogSink().Error("Number (%d) of Pprf^i exceeds maximum supported\n", n);
213 return RESULT_RAW_FORMAT;
218 for(i32_t i = 0; i < n ; i++) {
220 PDesc.Profile.Pprf[i] = PRF_.pprf(i+1);
227 Accessor::CPF CPF_(NextMarker);
233 DefaultLogSink().Error("Number (%d) of Pcpf^i exceeds maximum supported\n", n);
234 return RESULT_RAW_FORMAT;
237 PDesc.CorrespondingProfile.N = n;
239 for(i32_t i = 0; i < n; i++) {
241 PDesc.CorrespondingProfile.Pcpf[i] = CPF_.pcpf(i+1);
252 //------------------------------------------------------------------------------------------
254 ASDCP::JP2K::CodestreamParser::CodestreamParser()
258 ASDCP::JP2K::CodestreamParser::~CodestreamParser()
262 // Opens the stream for reading, parses enough data to provide a complete
263 // set of stream metadata for the MXFWriter below.
265 ASDCP::JP2K::CodestreamParser::OpenReadFrame(const std::string& filename, FrameBuffer& FB) const
267 const_cast<ASDCP::JP2K::CodestreamParser*>(this)->m_Parser = new h__CodestreamParser;
268 return m_Parser->OpenReadFrame(filename, FB);
273 ASDCP::JP2K::CodestreamParser::FillPictureDescriptor(PictureDescriptor& PDesc) const
275 if ( m_Parser.empty() )
278 PDesc = m_Parser->m_PDesc;
284 // end Codestream_Parser.cpp