0ed26492e82e4f8ccce9295d92c3247a219108e0
[asdcplib.git] / src / JP2K_Codestream_Parser.cpp
1 /*
2 Copyright (c) 2004-2013, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27 /*! \file    Codestream_Parser.cpp
28     \version $Id$
29     \brief   AS-DCP library, JPEG 2000 codestream essence reader implementation
30 */
31
32 #include <KM_fileio.h>
33 #include <AS_DCP.h>
34 #include <JP2K.h>
35 #include <assert.h>
36 #include <KM_log.h>
37 using Kumu::DefaultLogSink;
38
39 //------------------------------------------------------------------------------------------
40
41 class ASDCP::JP2K::CodestreamParser::h__CodestreamParser
42 {
43   ASDCP_NO_COPY_CONSTRUCT(h__CodestreamParser);
44
45 public:
46   PictureDescriptor  m_PDesc;
47   Kumu::FileReader   m_File;
48
49   h__CodestreamParser()
50   {
51     memset(&m_PDesc, 0, sizeof(m_PDesc));
52     m_PDesc.EditRate = Rational(24,1);
53     m_PDesc.SampleRate = m_PDesc.EditRate;
54   }
55
56   ~h__CodestreamParser() {}
57
58   Result_t OpenReadFrame(const std::string& filename, FrameBuffer& FB)
59   {
60     m_File.Close();
61     Result_t result = m_File.OpenRead(filename);
62
63     if ( ASDCP_SUCCESS(result) )
64       {
65         Kumu::fsize_t file_size = m_File.Size();
66
67         if ( FB.Capacity() < file_size )
68           {
69             DefaultLogSink().Error("FrameBuf.Capacity: %u frame length: %u\n", FB.Capacity(), (ui32_t)file_size);
70             return RESULT_SMALLBUF;
71           }
72       }
73
74     ui32_t read_count;
75
76     if ( ASDCP_SUCCESS(result) )
77       result = m_File.Read(FB.Data(), FB.Capacity(), &read_count);
78
79     if ( ASDCP_SUCCESS(result) )
80       FB.Size(read_count);
81
82     if ( ASDCP_SUCCESS(result) )
83       {
84         byte_t start_of_data = 0; // out param
85         result = ParseMetadataIntoDesc(FB, m_PDesc, &start_of_data);
86
87         if ( ASDCP_SUCCESS(result) )
88           FB.PlaintextOffset(start_of_data);
89       }
90
91     return result;
92   }
93 };
94
95 ASDCP::Result_t
96 ASDCP::JP2K::ParseMetadataIntoDesc(const FrameBuffer& FB, PictureDescriptor& PDesc, byte_t* start_of_data)
97 {
98   Result_t result = RESULT_OK;
99   Marker NextMarker;
100   ui32_t i;
101   const byte_t* p = FB.RoData();
102   const byte_t* end_p = p + FB.Size();
103
104   while ( p < end_p && ASDCP_SUCCESS(result) )
105     {
106       result = GetNextMarker(&p, NextMarker);
107
108       if ( ASDCP_FAILURE(result) )
109         {
110           result = RESULT_RAW_ESS;
111           break;
112         }
113
114       switch ( NextMarker.m_Type )
115         {
116         case MRK_SOD:
117           if ( start_of_data != 0 )
118             *start_of_data = p - FB.RoData();
119
120           p = end_p;
121           break;
122
123         case MRK_SIZ:
124           {
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();
139
140             if ( PDesc.Csize != 3 )
141               {
142                 DefaultLogSink().Error("Unexpected number of components: %u\n", PDesc.Csize);
143                 return RESULT_RAW_FORMAT;
144               }
145             
146             for ( i = 0; i < PDesc.Csize; i++ )
147               SIZ_.ReadComponent(i, PDesc.ImageComponents[i]);
148           }
149           break;
150
151         case MRK_COD:
152           memset(&PDesc.CodingStyleDefault, 0, sizeof(CodingStyleDefault_t));
153
154           if ( NextMarker.m_DataSize > sizeof(CodingStyleDefault_t) )
155             {
156               DefaultLogSink().Error("Unexpectedly large CodingStyle data: %u\n", NextMarker.m_DataSize);
157               return RESULT_RAW_FORMAT;
158             }
159           
160           memcpy(&PDesc.CodingStyleDefault, NextMarker.m_Data, NextMarker.m_DataSize);
161           break;
162
163         case MRK_QCD:
164           memset(&PDesc.QuantizationDefault, 0, sizeof(QuantizationDefault_t));
165
166           if ( NextMarker.m_DataSize < 3 ) // ( Sqcd = 8 bits, SPqcd = 8 bits ) == 2 bytes, error if not greater
167             {
168               DefaultLogSink().Error("No quantization signaled. QCD size=%s.\n", NextMarker.m_DataSize);
169               return RESULT_RAW_FORMAT;
170             }
171           
172           if ( NextMarker.m_DataSize > MaxDefaults )
173             {
174               DefaultLogSink().Error("Quantization Default length exceeds maximum %d\n", NextMarker.m_DataSize);
175               return RESULT_RAW_FORMAT;
176             }
177
178           memcpy(&PDesc.QuantizationDefault, NextMarker.m_Data, NextMarker.m_DataSize);
179           PDesc.QuantizationDefault.SPqcdLength = NextMarker.m_DataSize - 1;
180           break;
181         }
182     }
183
184   return result;
185 }
186
187 //------------------------------------------------------------------------------------------
188
189 ASDCP::JP2K::CodestreamParser::CodestreamParser()
190 {
191 }
192
193 ASDCP::JP2K::CodestreamParser::~CodestreamParser()
194 {
195 }
196
197 // Opens the stream for reading, parses enough data to provide a complete
198 // set of stream metadata for the MXFWriter below.
199 ASDCP::Result_t
200 ASDCP::JP2K::CodestreamParser::OpenReadFrame(const std::string& filename, FrameBuffer& FB) const
201 {
202   const_cast<ASDCP::JP2K::CodestreamParser*>(this)->m_Parser = new h__CodestreamParser;
203   return m_Parser->OpenReadFrame(filename, FB);
204 }
205
206 //
207 ASDCP::Result_t
208 ASDCP::JP2K::CodestreamParser::FillPictureDescriptor(PictureDescriptor& PDesc) const
209 {
210   if ( m_Parser.empty() )
211     return RESULT_INIT;
212
213   PDesc = m_Parser->m_PDesc;
214   return RESULT_OK;
215 }
216
217
218 //
219 // end Codestream_Parser.cpp
220 //