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 /* initialize optional items */
106 PDesc.ExtendedCapabilities.N = JP2K::NoExtendedCapabilitiesSignaled;
108 PDesc.CorrespondingProfile.N = 0;
110 while ( p < end_p && ASDCP_SUCCESS(result) )
112 result = GetNextMarker(&p, NextMarker);
114 if ( ASDCP_FAILURE(result) )
116 result = RESULT_RAW_ESS;
120 switch ( NextMarker.m_Type )
123 if ( start_of_data != 0 )
124 *start_of_data = p - FB.RoData();
131 Accessor::SIZ SIZ_(NextMarker);
132 PDesc.StoredWidth = SIZ_.Xsize();
133 PDesc.StoredHeight = SIZ_.Ysize();
134 PDesc.AspectRatio = Rational(SIZ_.Xsize(), SIZ_.Ysize());
135 PDesc.Rsize = SIZ_.Rsize();
136 PDesc.Xsize = SIZ_.Xsize();
137 PDesc.Ysize = SIZ_.Ysize();
138 PDesc.XOsize = SIZ_.XOsize();
139 PDesc.YOsize = SIZ_.YOsize();
140 PDesc.XTsize = SIZ_.XTsize();
141 PDesc.YTsize = SIZ_.YTsize();
142 PDesc.XTOsize = SIZ_.XTOsize();
143 PDesc.YTOsize = SIZ_.YTOsize();
144 PDesc.Csize = SIZ_.Csize();
146 if ( PDesc.Csize != 3 )
148 DefaultLogSink().Error("Unexpected number of components: %u\n", PDesc.Csize);
149 return RESULT_RAW_FORMAT;
152 for ( i = 0; i < PDesc.Csize; i++ )
153 SIZ_.ReadComponent(i, PDesc.ImageComponents[i]);
158 memset(&PDesc.CodingStyleDefault, 0, sizeof(CodingStyleDefault_t));
160 if ( NextMarker.m_DataSize > sizeof(CodingStyleDefault_t) )
162 DefaultLogSink().Error("Unexpectedly large CodingStyle data: %u\n", NextMarker.m_DataSize);
163 return RESULT_RAW_FORMAT;
166 memcpy(&PDesc.CodingStyleDefault, NextMarker.m_Data, NextMarker.m_DataSize);
170 memset(&PDesc.QuantizationDefault, 0, sizeof(QuantizationDefault_t));
172 if ( NextMarker.m_DataSize < 3 ) // ( Sqcd = 8 bits, SPqcd = 8 bits ) == 2 bytes, error if not greater
174 DefaultLogSink().Error("No quantization signaled. QCD size=%s.\n", NextMarker.m_DataSize);
175 return RESULT_RAW_FORMAT;
178 if ( NextMarker.m_DataSize > MaxDefaults )
180 DefaultLogSink().Error("Quantization Default length exceeds maximum %d\n", NextMarker.m_DataSize);
181 return RESULT_RAW_FORMAT;
184 memcpy(&PDesc.QuantizationDefault, NextMarker.m_Data, NextMarker.m_DataSize);
185 PDesc.QuantizationDefault.SPqcdLength = NextMarker.m_DataSize - 1;
190 Accessor::CAP CAP_(NextMarker);
192 PDesc.ExtendedCapabilities.Pcap = CAP_.pcap();
194 PDesc.ExtendedCapabilities.N = CAP_.N();
196 for (i32_t i = 0; i < CAP_.N(); i++) {
198 PDesc.ExtendedCapabilities.Ccap[i] = CAP_.ccap(i);
207 Accessor::PRF PRF_(NextMarker);
213 DefaultLogSink().Error("Number (%d) of Pprf^i exceeds maximum supported\n", n);
214 return RESULT_RAW_FORMAT;
219 for(i32_t i = 0; i < n ; i++) {
221 PDesc.Profile.Pprf[i] = PRF_.pprf(i+1);
228 Accessor::CPF CPF_(NextMarker);
234 DefaultLogSink().Error("Number (%d) of Pcpf^i exceeds maximum supported\n", n);
235 return RESULT_RAW_FORMAT;
238 PDesc.CorrespondingProfile.N = n;
240 for(i32_t i = 0; i < n; i++) {
242 PDesc.CorrespondingProfile.Pcpf[i] = CPF_.pcpf(i+1);
253 //------------------------------------------------------------------------------------------
255 ASDCP::JP2K::CodestreamParser::CodestreamParser()
259 ASDCP::JP2K::CodestreamParser::~CodestreamParser()
263 // Opens the stream for reading, parses enough data to provide a complete
264 // set of stream metadata for the MXFWriter below.
266 ASDCP::JP2K::CodestreamParser::OpenReadFrame(const std::string& filename, FrameBuffer& FB) const
268 const_cast<ASDCP::JP2K::CodestreamParser*>(this)->m_Parser = new h__CodestreamParser;
269 return m_Parser->OpenReadFrame(filename, FB);
274 ASDCP::JP2K::CodestreamParser::FillPictureDescriptor(PictureDescriptor& PDesc) const
276 if ( m_Parser.empty() )
279 PDesc = m_Parser->m_PDesc;
285 // end Codestream_Parser.cpp