stereoscopic JP2K writing
[asdcplib.git] / src / JP2K_Codestream_Parser.cpp
1 /*
2 Copyright (c) 2004-2006, 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 char* filename, FrameBuffer& FB)
59   {
60     ASDCP_TEST_NULL_STR(filename);
61     m_File.Close();
62     Result_t result = m_File.OpenRead(filename);
63
64     if ( ASDCP_SUCCESS(result) )
65       {
66         Kumu::fsize_t file_size = m_File.Size();
67
68         if ( FB.Capacity() < file_size )
69           {
70             DefaultLogSink().Error("FrameBuf.Capacity: %u frame length: %u\n", FB.Capacity(), (ui32_t)file_size);
71             return RESULT_SMALLBUF;
72           }
73       }
74
75     ui32_t read_count;
76
77     if ( ASDCP_SUCCESS(result) )
78       result = m_File.Read(FB.Data(), FB.Capacity(), &read_count);
79
80     if ( ASDCP_SUCCESS(result) )
81       FB.Size(read_count);
82
83     if ( ASDCP_SUCCESS(result) )
84       {
85         Marker NextMarker;
86         ui32_t i;
87         const byte_t* p = FB.RoData();
88         const byte_t* end_p = p + FB.Size();
89
90         while ( p < end_p && ASDCP_SUCCESS(result) )
91           {
92             result = GetNextMarker(&p, NextMarker);
93
94             if ( ASDCP_FAILURE(result) )
95               {
96                 result = RESULT_RAW_ESS;
97                 break;
98               }
99 #if 0
100             fprintf(stderr, "%s Length: %u\n",
101                     GetMarkerString(NextMarker.m_Type), NextMarker.m_DataSize);
102 #endif
103
104             switch ( NextMarker.m_Type )
105               {
106               case MRK_SOD:
107                 FB.PlaintextOffset(p - FB.RoData());
108                 p = end_p;
109                 break;
110
111               case MRK_SIZ:
112                 {
113                   Accessor::SIZ SIZ_(NextMarker);
114                   m_PDesc.StoredWidth = SIZ_.Xsize();
115                   m_PDesc.StoredHeight = SIZ_.Ysize();
116                   m_PDesc.AspectRatio = Rational(SIZ_.Xsize(), SIZ_.Ysize());
117                   m_PDesc.Rsize = SIZ_.Rsize();
118                   m_PDesc.Xsize = SIZ_.Xsize();
119                   m_PDesc.Ysize = SIZ_.Ysize();
120                   m_PDesc.XOsize = SIZ_.XOsize();
121                   m_PDesc.YOsize = SIZ_.YOsize();
122                   m_PDesc.XTsize = SIZ_.XTsize();
123                   m_PDesc.YTsize = SIZ_.YTsize();
124                   m_PDesc.XTOsize = SIZ_.XTOsize();
125                   m_PDesc.YTOsize = SIZ_.YTOsize();
126                   m_PDesc.Csize = SIZ_.Csize();
127
128                   if ( m_PDesc.Csize != 3 )
129                     {
130                       DefaultLogSink().Error("Unexpected number of components: %u\n", m_PDesc.Csize);
131                       return RESULT_RAW_FORMAT;
132                     }
133
134                   for ( i = 0; i < m_PDesc.Csize; i++ )
135                     SIZ_.ReadComponent(i, m_PDesc.ImageComponents[i]);
136                 }
137                 break;
138
139               case MRK_COD:
140                 if ( NextMarker.m_DataSize > DefaultCodingDataLength )
141                   {
142                     DefaultLogSink().Error("Unexpectedly large CodingStyle data: %u\n", NextMarker.m_DataSize);
143                     return RESULT_RAW_FORMAT;
144                   }
145
146                 m_PDesc.CodingStyleLength = NextMarker.m_DataSize;
147                 memcpy(m_PDesc.CodingStyle, NextMarker.m_Data, m_PDesc.CodingStyleLength);
148                 break;
149
150               case MRK_QCD:
151                 if ( NextMarker.m_DataSize > DefaultCodingDataLength )
152                   {
153                     DefaultLogSink().Error("Unexpectedly large QuantDefault data: %u\n", NextMarker.m_DataSize);
154                     return RESULT_RAW_FORMAT;
155                   }
156
157                 m_PDesc.QuantDefaultLength = NextMarker.m_DataSize;
158                 memcpy(m_PDesc.QuantDefault, NextMarker.m_Data, m_PDesc.QuantDefaultLength);
159                 break;
160               }
161           }
162       }
163
164     return result;
165   }
166 };
167
168 //------------------------------------------------------------------------------------------
169
170 ASDCP::JP2K::CodestreamParser::CodestreamParser()
171 {
172 }
173
174 ASDCP::JP2K::CodestreamParser::~CodestreamParser()
175 {
176 }
177
178 // Opens the stream for reading, parses enough data to provide a complete
179 // set of stream metadata for the MXFWriter below.
180 ASDCP::Result_t
181 ASDCP::JP2K::CodestreamParser::OpenReadFrame(const char* filename, FrameBuffer& FB) const
182 {
183   const_cast<ASDCP::JP2K::CodestreamParser*>(this)->m_Parser = new h__CodestreamParser;
184   return m_Parser->OpenReadFrame(filename, FB);
185 }
186
187 //
188 ASDCP::Result_t
189 ASDCP::JP2K::CodestreamParser::FillPictureDescriptor(PictureDescriptor& PDesc) const
190 {
191   if ( m_Parser.empty() )
192     return RESULT_INIT;
193
194   PDesc = m_Parser->m_PDesc;
195   return RESULT_OK;
196 }
197
198
199 //
200 // end Codestream_Parser.cpp
201 //