(no commit message)
[openjpeg.git] / libopenjpeg / jpt.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2002-2003, Yannick Verschueren
5  * Copyright (c) 2005, Herve Drolon, FreeImage Team
6  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "jpt.h"
32 #include "openjpeg.h"
33 #include "cio.h"
34 #include "event.h"
35 /*
36  * Read the information contains in VBAS [JPP/JPT stream message header]
37  * Store information (7 bits) in value
38  * @param p_cio the stream to read from.
39  * @param p_value the data to update
40  * @return the nb of bytes read or -1 if an io error occurred.
41  */
42 bool jpt_read_VBAS_info(opj_stream_private_t * p_cio, OPJ_UINT32 * p_nb_bytes_read, OPJ_UINT32 * p_value, opj_event_mgr_t * p_manager) 
43 {
44         OPJ_BYTE l_elmt;
45         OPJ_UINT32 l_nb_bytes_read = 0;
46         
47         // read data till the MSB of the current byte is 1.
48         // concatenate 7 bits of data, last bit is finish flag
49
50         // read data from the stream
51         
52         if
53                 (opj_stream_read_data(p_cio,&l_elmt,1,p_manager) != 1)
54         {
55                 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data.\n");
56                 return false;
57         }
58         ++l_nb_bytes_read;
59
60         // is the MSB equal to 1 ?
61         while 
62                 (l_elmt & 0x80) 
63         {
64                 // concatenate 7 bits of data, last bit is finish flag
65                 *p_value = (*p_value  << 7) | (l_elmt & 0x7f);
66                 if
67                         (opj_stream_read_data(p_cio,&l_elmt,1,p_manager) != 1)
68                 {
69                         opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data.\n");
70                         return false;
71                 }
72                 ++l_nb_bytes_read;
73         }
74         // concatenate 7 bits of data, last bit is finish flag
75         *p_value = (*p_value  << 7) | (l_elmt & 0x7f);
76         * p_nb_bytes_read = l_nb_bytes_read;
77         return true;
78 }
79
80 /*
81  * Initialize the value of the message header structure 
82  *
83  */
84 void jpt_init_msg_header(opj_jpt_msg_header_t * header) 
85 {
86         header->Id = 0;         /* In-class Identifier    */
87         header->last_byte = 0;  /* Last byte information  */
88         header->Class_Id = 0;           /* Class Identifier       */
89         header->CSn_Id = 0;             /* CSn : index identifier */
90         header->Msg_offset = 0; /* Message offset         */
91         header->Msg_length = 0; /* Message length         */
92         header->Layer_nb = 0;           /* Auxiliary for JPP case */
93 }
94
95 /*
96  * Re-initialize the value of the message header structure
97  *
98  * Only parameters always present in message header
99  *
100  */
101 void jpt_reinit_msg_header(opj_jpt_msg_header_t * header) 
102 {
103         header->Id = 0;         /* In-class Identifier    */
104         header->last_byte = 0;  /* Last byte information  */
105         header->Msg_offset = 0; /* Message offset         */
106         header->Msg_length = 0; /* Message length         */
107 }
108
109 /*
110  * Read the message header for a JPP/JPT - stream
111  *
112  */
113 bool jpt_read_msg_header(opj_stream_private_t *cio, opj_jpt_msg_header_t *header, OPJ_UINT32 * p_nb_bytes_read, opj_event_mgr_t * p_manager) 
114 {
115         OPJ_BYTE elmt, Class = 0, CSn = 0;
116         OPJ_UINT32 l_nb_bytes_read = 0;
117         OPJ_UINT32 l_last_nb_bytes_read;
118
119
120         jpt_reinit_msg_header(header);
121         
122         /* ------------- */
123         /* VBAS : Bin-ID */
124         /* ------------- */
125         if
126                 (opj_stream_read_data(cio,&elmt,1,p_manager) != 1)
127         {
128                 opj_event_msg(p_manager, EVT_ERROR, "Forbidden value encounter in message header !!\n");
129                 return false;
130         }
131         ++l_nb_bytes_read;
132
133         /* See for Class and CSn */
134         switch ((elmt >> 5) & 0x03)
135         {
136                 case 0:
137                         opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
138                         break;
139                 case 1:
140                         Class = 0;
141                         CSn = 0;
142                         break;
143                 case 2:
144                         Class = 1;
145                         CSn = 0;
146                         break;
147                 case 3:
148                         Class = 1;
149                         CSn = 1;
150                         break;
151                 default:
152                         break;
153         }
154
155         /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */
156         if 
157                 (((elmt >> 4) & 0x01) == 1)
158         {
159                 header->last_byte = 1;
160         }
161
162         /* In-class identifier */
163         header->Id |= (elmt & 0x0f);
164         if 
165                 ((elmt >> 7) == 1)
166         {
167                 l_last_nb_bytes_read = 0;
168                 if
169                         (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Id), p_manager))
170                 {
171                         opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
172                         return false;
173                 }
174                 l_nb_bytes_read += l_last_nb_bytes_read;
175         }
176
177         /* ------------ */
178         /* VBAS : Class */
179         /* ------------ */
180         if (Class == 1) 
181         {
182                 header->Class_Id = 0;
183                 l_last_nb_bytes_read = 0;
184                 if
185                         (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Class_Id), p_manager))
186                 {
187                         opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
188                         return false;
189                 }
190                 l_nb_bytes_read += l_last_nb_bytes_read;
191         }
192
193         /* ---------- */
194         /* VBAS : CSn */
195         /* ---------- */
196         if (CSn == 1) 
197         {
198                 header->CSn_Id = 0;
199                 l_last_nb_bytes_read = 0;
200                 if
201                         (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->CSn_Id), p_manager))
202                 {
203                         opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
204                         return false;
205                 }
206                 l_nb_bytes_read += l_last_nb_bytes_read;
207         }
208
209         /* ----------------- */
210         /* VBAS : Msg_offset */
211         /* ----------------- */
212         l_last_nb_bytes_read = 0;
213         if
214                 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Msg_offset), p_manager))
215         {
216                 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
217                 return false;
218         }
219         l_nb_bytes_read += l_last_nb_bytes_read;
220
221         /* ----------------- */
222         /* VBAS : Msg_length */
223         /* ----------------- */
224         l_last_nb_bytes_read = 0;
225         if
226                 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Msg_length), p_manager))
227         {
228                 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
229                 return false;
230         }
231         l_nb_bytes_read += l_last_nb_bytes_read;
232
233         /* ---------- */
234         /* VBAS : Aux */
235         /* ---------- */
236         if ((header->Class_Id & 0x01) == 1) 
237         {
238                 header->Layer_nb = 0;
239                 if
240                         (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Layer_nb), p_manager))
241                 {
242                         opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
243                         return false;
244                 }
245                 l_nb_bytes_read += l_last_nb_bytes_read;
246         }
247         * p_nb_bytes_read = l_nb_bytes_read;
248         return true;
249 }