/src/openjpeg/src/lib/openjp2/j2k.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * The copyright in this software is being made available under the 2-clauses |
3 | | * BSD License, included below. This software may be subject to other third |
4 | | * party and contributor rights, including patent rights, and no such rights |
5 | | * are granted under this license. |
6 | | * |
7 | | * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium |
8 | | * Copyright (c) 2002-2014, Professor Benoit Macq |
9 | | * Copyright (c) 2001-2003, David Janssens |
10 | | * Copyright (c) 2002-2003, Yannick Verschueren |
11 | | * Copyright (c) 2003-2007, Francois-Olivier Devaux |
12 | | * Copyright (c) 2003-2014, Antonin Descampe |
13 | | * Copyright (c) 2005, Herve Drolon, FreeImage Team |
14 | | * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr> |
15 | | * Copyright (c) 2006-2007, Parvatha Elangovan |
16 | | * Copyright (c) 2010-2011, Kaori Hagihara |
17 | | * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France |
18 | | * Copyright (c) 2012, CS Systemes d'Information, France |
19 | | * Copyright (c) 2017, IntoPIX SA <support@intopix.com> |
20 | | * All rights reserved. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the above copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * |
31 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' |
32 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
33 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
34 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
35 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
36 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
37 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
38 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
39 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
40 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
41 | | * POSSIBILITY OF SUCH DAMAGE. |
42 | | */ |
43 | | |
44 | | #include "opj_includes.h" |
45 | | |
46 | | /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ |
47 | | /*@{*/ |
48 | | |
49 | | /** @name Local static functions */ |
50 | | /*@{*/ |
51 | | |
52 | | /** |
53 | | * Sets up the procedures to do on reading header. Developers wanting to extend the library can add their own reading procedures. |
54 | | */ |
55 | | static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, |
56 | | opj_event_mgr_t * p_manager); |
57 | | |
58 | | /** |
59 | | * The read header procedure. |
60 | | */ |
61 | | static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, |
62 | | opj_stream_private_t *p_stream, |
63 | | opj_event_mgr_t * p_manager); |
64 | | |
65 | | /** |
66 | | * The default encoding validation procedure without any extension. |
67 | | * |
68 | | * @param p_j2k the jpeg2000 codec to validate. |
69 | | * @param p_stream the input stream to validate. |
70 | | * @param p_manager the user event manager. |
71 | | * |
72 | | * @return true if the parameters are correct. |
73 | | */ |
74 | | static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, |
75 | | opj_stream_private_t *p_stream, |
76 | | opj_event_mgr_t * p_manager); |
77 | | |
78 | | /** |
79 | | * The default decoding validation procedure without any extension. |
80 | | * |
81 | | * @param p_j2k the jpeg2000 codec to validate. |
82 | | * @param p_stream the input stream to validate. |
83 | | * @param p_manager the user event manager. |
84 | | * |
85 | | * @return true if the parameters are correct. |
86 | | */ |
87 | | static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t * p_j2k, |
88 | | opj_stream_private_t *p_stream, |
89 | | opj_event_mgr_t * p_manager); |
90 | | |
91 | | /** |
92 | | * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters |
93 | | * are valid. Developers wanting to extend the library can add their own validation procedures. |
94 | | */ |
95 | | static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, |
96 | | opj_event_mgr_t * p_manager); |
97 | | |
98 | | /** |
99 | | * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters |
100 | | * are valid. Developers wanting to extend the library can add their own validation procedures. |
101 | | */ |
102 | | static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, |
103 | | opj_event_mgr_t * p_manager); |
104 | | |
105 | | /** |
106 | | * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters |
107 | | * are valid. Developers wanting to extend the library can add their own validation procedures. |
108 | | */ |
109 | | static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, |
110 | | opj_event_mgr_t * p_manager); |
111 | | |
112 | | /** |
113 | | * The mct encoding validation procedure. |
114 | | * |
115 | | * @param p_j2k the jpeg2000 codec to validate. |
116 | | * @param p_stream the input stream to validate. |
117 | | * @param p_manager the user event manager. |
118 | | * |
119 | | * @return true if the parameters are correct. |
120 | | */ |
121 | | static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, |
122 | | opj_stream_private_t *p_stream, |
123 | | opj_event_mgr_t * p_manager); |
124 | | |
125 | | /** |
126 | | * Builds the tcd decoder to use to decode tile. |
127 | | */ |
128 | | static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, |
129 | | opj_stream_private_t *p_stream, |
130 | | opj_event_mgr_t * p_manager); |
131 | | /** |
132 | | * Builds the tcd encoder to use to encode tile. |
133 | | */ |
134 | | static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, |
135 | | opj_stream_private_t *p_stream, |
136 | | opj_event_mgr_t * p_manager); |
137 | | |
138 | | /** |
139 | | * Creates a tile-coder encoder. |
140 | | * |
141 | | * @param p_stream the stream to write data to. |
142 | | * @param p_j2k J2K codec. |
143 | | * @param p_manager the user event manager. |
144 | | */ |
145 | | static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, |
146 | | opj_stream_private_t *p_stream, |
147 | | opj_event_mgr_t * p_manager); |
148 | | |
149 | | /** |
150 | | * Executes the given procedures on the given codec. |
151 | | * |
152 | | * @param p_procedure_list the list of procedures to execute |
153 | | * @param p_j2k the jpeg2000 codec to execute the procedures on. |
154 | | * @param p_stream the stream to execute the procedures on. |
155 | | * @param p_manager the user manager. |
156 | | * |
157 | | * @return true if all the procedures were successfully executed. |
158 | | */ |
159 | | static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, |
160 | | opj_procedure_list_t * p_procedure_list, |
161 | | opj_stream_private_t *p_stream, |
162 | | opj_event_mgr_t * p_manager); |
163 | | |
164 | | /** |
165 | | * Updates the rates of the tcp. |
166 | | * |
167 | | * @param p_stream the stream to write data to. |
168 | | * @param p_j2k J2K codec. |
169 | | * @param p_manager the user event manager. |
170 | | */ |
171 | | static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, |
172 | | opj_stream_private_t *p_stream, |
173 | | opj_event_mgr_t * p_manager); |
174 | | |
175 | | /** |
176 | | * Copies the decoding tile parameters onto all the tile parameters. |
177 | | * Creates also the tile decoder. |
178 | | */ |
179 | | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, |
180 | | opj_stream_private_t *p_stream, |
181 | | opj_event_mgr_t * p_manager); |
182 | | |
183 | | /** |
184 | | * Destroys the memory associated with the decoding of headers. |
185 | | */ |
186 | | static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, |
187 | | opj_stream_private_t *p_stream, |
188 | | opj_event_mgr_t * p_manager); |
189 | | |
190 | | /** |
191 | | * Reads the lookup table containing all the marker, status and action, and returns the handler associated |
192 | | * with the marker value. |
193 | | * @param p_id Marker value to look up |
194 | | * |
195 | | * @return the handler associated with the id. |
196 | | */ |
197 | | static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler( |
198 | | OPJ_UINT32 p_id); |
199 | | |
200 | | /** |
201 | | * Destroys a tile coding parameter structure. |
202 | | * |
203 | | * @param p_tcp the tile coding parameter to destroy. |
204 | | */ |
205 | | static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp); |
206 | | |
207 | | /** |
208 | | * Destroys the data inside a tile coding parameter structure. |
209 | | * |
210 | | * @param p_tcp the tile coding parameter which contain data to destroy. |
211 | | */ |
212 | | static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp); |
213 | | |
214 | | /** |
215 | | * Destroys a coding parameter structure. |
216 | | * |
217 | | * @param p_cp the coding parameter to destroy. |
218 | | */ |
219 | | static void opj_j2k_cp_destroy(opj_cp_t *p_cp); |
220 | | |
221 | | /** |
222 | | * Compare 2 a SPCod/ SPCoc elements, i.e. the coding style of a given component of a tile. |
223 | | * |
224 | | * @param p_j2k J2K codec. |
225 | | * @param p_tile_no Tile number |
226 | | * @param p_first_comp_no The 1st component number to compare. |
227 | | * @param p_second_comp_no The 1st component number to compare. |
228 | | * |
229 | | * @return OPJ_TRUE if SPCdod are equals. |
230 | | */ |
231 | | static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, |
232 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
233 | | |
234 | | /** |
235 | | * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
236 | | * |
237 | | * @param p_j2k J2K codec. |
238 | | * @param p_tile_no FIXME DOC |
239 | | * @param p_comp_no the component number to output. |
240 | | * @param p_data FIXME DOC |
241 | | * @param p_header_size FIXME DOC |
242 | | * @param p_manager the user event manager. |
243 | | * |
244 | | * @return FIXME DOC |
245 | | */ |
246 | | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, |
247 | | OPJ_UINT32 p_tile_no, |
248 | | OPJ_UINT32 p_comp_no, |
249 | | OPJ_BYTE * p_data, |
250 | | OPJ_UINT32 * p_header_size, |
251 | | opj_event_mgr_t * p_manager); |
252 | | |
253 | | /** |
254 | | * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. |
255 | | * |
256 | | * @param p_j2k the J2K codec. |
257 | | * @param p_tile_no the tile index. |
258 | | * @param p_comp_no the component being outputted. |
259 | | * |
260 | | * @return the number of bytes taken by the SPCod element. |
261 | | */ |
262 | | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, |
263 | | OPJ_UINT32 p_tile_no, |
264 | | OPJ_UINT32 p_comp_no); |
265 | | |
266 | | /** |
267 | | * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
268 | | * @param p_j2k the jpeg2000 codec. |
269 | | * @param compno FIXME DOC |
270 | | * @param p_header_data the data contained in the COM box. |
271 | | * @param p_header_size the size of the data contained in the COM marker. |
272 | | * @param p_manager the user event manager. |
273 | | */ |
274 | | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, |
275 | | OPJ_UINT32 compno, |
276 | | OPJ_BYTE * p_header_data, |
277 | | OPJ_UINT32 * p_header_size, |
278 | | opj_event_mgr_t * p_manager); |
279 | | |
280 | | /** |
281 | | * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
282 | | * |
283 | | * @param p_tile_no the tile index. |
284 | | * @param p_comp_no the component being outputted. |
285 | | * @param p_j2k the J2K codec. |
286 | | * |
287 | | * @return the number of bytes taken by the SPCod element. |
288 | | */ |
289 | | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, |
290 | | OPJ_UINT32 p_tile_no, |
291 | | OPJ_UINT32 p_comp_no); |
292 | | |
293 | | /** |
294 | | * Compares 2 SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
295 | | * |
296 | | * @param p_j2k J2K codec. |
297 | | * @param p_tile_no the tile to output. |
298 | | * @param p_first_comp_no the first component number to compare. |
299 | | * @param p_second_comp_no the second component number to compare. |
300 | | * |
301 | | * @return OPJ_TRUE if equals. |
302 | | */ |
303 | | static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, |
304 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
305 | | |
306 | | |
307 | | /** |
308 | | * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
309 | | * |
310 | | * @param p_tile_no the tile to output. |
311 | | * @param p_comp_no the component number to output. |
312 | | * @param p_data the data buffer. |
313 | | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
314 | | * @param p_j2k J2K codec. |
315 | | * @param p_manager the user event manager. |
316 | | * |
317 | | */ |
318 | | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
319 | | OPJ_UINT32 p_tile_no, |
320 | | OPJ_UINT32 p_comp_no, |
321 | | OPJ_BYTE * p_data, |
322 | | OPJ_UINT32 * p_header_size, |
323 | | opj_event_mgr_t * p_manager); |
324 | | |
325 | | /** |
326 | | * Updates the Tile Length Marker. |
327 | | */ |
328 | | static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size); |
329 | | |
330 | | /** |
331 | | * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
332 | | * |
333 | | * @param p_j2k J2K codec. |
334 | | * @param compno the component number to output. |
335 | | * @param p_header_data the data buffer. |
336 | | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
337 | | * @param p_manager the user event manager. |
338 | | * |
339 | | */ |
340 | | static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
341 | | OPJ_UINT32 compno, |
342 | | OPJ_BYTE * p_header_data, |
343 | | OPJ_UINT32 * p_header_size, |
344 | | opj_event_mgr_t * p_manager); |
345 | | |
346 | | /** |
347 | | * Copies the tile component parameters of all the component from the first tile component. |
348 | | * |
349 | | * @param p_j2k the J2k codec. |
350 | | */ |
351 | | static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k); |
352 | | |
353 | | /** |
354 | | * Copies the tile quantization parameters of all the component from the first tile component. |
355 | | * |
356 | | * @param p_j2k the J2k codec. |
357 | | */ |
358 | | static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k); |
359 | | |
360 | | /** |
361 | | * Reads the tiles. |
362 | | */ |
363 | | static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
364 | | opj_stream_private_t *p_stream, |
365 | | opj_event_mgr_t * p_manager); |
366 | | |
367 | | static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, |
368 | | OPJ_UINT32 p_tile_index, |
369 | | opj_stream_private_t *p_stream, |
370 | | opj_event_mgr_t * p_manager); |
371 | | |
372 | | static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, |
373 | | opj_image_t* p_output_image); |
374 | | |
375 | | static void opj_get_tile_dimensions(opj_image_t * l_image, |
376 | | opj_tcd_tilecomp_t * l_tilec, |
377 | | opj_image_comp_t * l_img_comp, |
378 | | OPJ_UINT32* l_size_comp, |
379 | | OPJ_UINT32* l_width, |
380 | | OPJ_UINT32* l_height, |
381 | | OPJ_UINT32* l_offset_x, |
382 | | OPJ_UINT32* l_offset_y, |
383 | | OPJ_UINT32* l_image_width, |
384 | | OPJ_UINT32* l_stride, |
385 | | OPJ_UINT32* l_tile_offset); |
386 | | |
387 | | static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data); |
388 | | |
389 | | static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, |
390 | | opj_stream_private_t *p_stream, |
391 | | opj_event_mgr_t * p_manager); |
392 | | |
393 | | /** |
394 | | * Sets up the procedures to do on writing header. |
395 | | * Developers wanting to extend the library can add their own writing procedures. |
396 | | */ |
397 | | static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, |
398 | | opj_event_mgr_t * p_manager); |
399 | | |
400 | | static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, |
401 | | OPJ_BYTE * p_data, |
402 | | OPJ_UINT32 * p_data_written, |
403 | | OPJ_UINT32 total_data_size, |
404 | | opj_stream_private_t *p_stream, |
405 | | struct opj_event_mgr * p_manager); |
406 | | |
407 | | static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, |
408 | | OPJ_BYTE * p_data, |
409 | | OPJ_UINT32 * p_data_written, |
410 | | OPJ_UINT32 total_data_size, |
411 | | opj_stream_private_t *p_stream, |
412 | | struct opj_event_mgr * p_manager); |
413 | | |
414 | | /** |
415 | | * Gets the offset of the header. |
416 | | * |
417 | | * @param p_stream the stream to write data to. |
418 | | * @param p_j2k J2K codec. |
419 | | * @param p_manager the user event manager. |
420 | | */ |
421 | | static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, |
422 | | opj_stream_private_t *p_stream, |
423 | | opj_event_mgr_t * p_manager); |
424 | | |
425 | | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k); |
426 | | |
427 | | /* |
428 | | * ----------------------------------------------------------------------- |
429 | | * ----------------------------------------------------------------------- |
430 | | * ----------------------------------------------------------------------- |
431 | | */ |
432 | | |
433 | | /** |
434 | | * Writes the SOC marker (Start Of Codestream) |
435 | | * |
436 | | * @param p_stream the stream to write data to. |
437 | | * @param p_j2k J2K codec. |
438 | | * @param p_manager the user event manager. |
439 | | */ |
440 | | static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k, |
441 | | opj_stream_private_t *p_stream, |
442 | | opj_event_mgr_t * p_manager); |
443 | | |
444 | | /** |
445 | | * Reads a SOC marker (Start of Codestream) |
446 | | * @param p_j2k the jpeg2000 file codec. |
447 | | * @param p_stream XXX needs data |
448 | | * @param p_manager the user event manager. |
449 | | */ |
450 | | static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k, |
451 | | opj_stream_private_t *p_stream, |
452 | | opj_event_mgr_t * p_manager); |
453 | | |
454 | | /** |
455 | | * Writes the SIZ marker (image and tile size) |
456 | | * |
457 | | * @param p_j2k J2K codec. |
458 | | * @param p_stream the stream to write data to. |
459 | | * @param p_manager the user event manager. |
460 | | */ |
461 | | static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, |
462 | | opj_stream_private_t *p_stream, |
463 | | opj_event_mgr_t * p_manager); |
464 | | |
465 | | /** |
466 | | * Reads a SIZ marker (image and tile size) |
467 | | * @param p_j2k the jpeg2000 file codec. |
468 | | * @param p_header_data the data contained in the SIZ box. |
469 | | * @param p_header_size the size of the data contained in the SIZ marker. |
470 | | * @param p_manager the user event manager. |
471 | | */ |
472 | | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, |
473 | | OPJ_BYTE * p_header_data, |
474 | | OPJ_UINT32 p_header_size, |
475 | | opj_event_mgr_t * p_manager); |
476 | | |
477 | | /** |
478 | | * Writes the COM marker (comment) |
479 | | * |
480 | | * @param p_stream the stream to write data to. |
481 | | * @param p_j2k J2K codec. |
482 | | * @param p_manager the user event manager. |
483 | | */ |
484 | | static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, |
485 | | opj_stream_private_t *p_stream, |
486 | | opj_event_mgr_t * p_manager); |
487 | | |
488 | | /** |
489 | | * Reads a COM marker (comments) |
490 | | * @param p_j2k the jpeg2000 file codec. |
491 | | * @param p_header_data the data contained in the COM box. |
492 | | * @param p_header_size the size of the data contained in the COM marker. |
493 | | * @param p_manager the user event manager. |
494 | | */ |
495 | | static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, |
496 | | OPJ_BYTE * p_header_data, |
497 | | OPJ_UINT32 p_header_size, |
498 | | opj_event_mgr_t * p_manager); |
499 | | /** |
500 | | * Writes the COD marker (Coding style default) |
501 | | * |
502 | | * @param p_stream the stream to write data to. |
503 | | * @param p_j2k J2K codec. |
504 | | * @param p_manager the user event manager. |
505 | | */ |
506 | | static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, |
507 | | opj_stream_private_t *p_stream, |
508 | | opj_event_mgr_t * p_manager); |
509 | | |
510 | | /** |
511 | | * Reads a COD marker (Coding style defaults) |
512 | | * @param p_header_data the data contained in the COD box. |
513 | | * @param p_j2k the jpeg2000 codec. |
514 | | * @param p_header_size the size of the data contained in the COD marker. |
515 | | * @param p_manager the user event manager. |
516 | | */ |
517 | | static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, |
518 | | OPJ_BYTE * p_header_data, |
519 | | OPJ_UINT32 p_header_size, |
520 | | opj_event_mgr_t * p_manager); |
521 | | |
522 | | /** |
523 | | * Compares 2 COC markers (Coding style component) |
524 | | * |
525 | | * @param p_j2k J2K codec. |
526 | | * @param p_first_comp_no the index of the first component to compare. |
527 | | * @param p_second_comp_no the index of the second component to compare. |
528 | | * |
529 | | * @return OPJ_TRUE if equals |
530 | | */ |
531 | | static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, |
532 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
533 | | |
534 | | /** |
535 | | * Writes the COC marker (Coding style component) |
536 | | * |
537 | | * @param p_j2k J2K codec. |
538 | | * @param p_comp_no the index of the component to output. |
539 | | * @param p_stream the stream to write data to. |
540 | | * @param p_manager the user event manager. |
541 | | */ |
542 | | static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, |
543 | | OPJ_UINT32 p_comp_no, |
544 | | opj_stream_private_t *p_stream, |
545 | | opj_event_mgr_t * p_manager); |
546 | | |
547 | | /** |
548 | | * Writes the COC marker (Coding style component) |
549 | | * |
550 | | * @param p_j2k J2K codec. |
551 | | * @param p_comp_no the index of the component to output. |
552 | | * @param p_data FIXME DOC |
553 | | * @param p_data_written FIXME DOC |
554 | | * @param p_manager the user event manager. |
555 | | */ |
556 | | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, |
557 | | OPJ_UINT32 p_comp_no, |
558 | | OPJ_BYTE * p_data, |
559 | | OPJ_UINT32 * p_data_written, |
560 | | opj_event_mgr_t * p_manager); |
561 | | |
562 | | /** |
563 | | * Gets the maximum size taken by a coc. |
564 | | * |
565 | | * @param p_j2k the jpeg2000 codec to use. |
566 | | */ |
567 | | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k); |
568 | | |
569 | | /** |
570 | | * Reads a COC marker (Coding Style Component) |
571 | | * @param p_header_data the data contained in the COC box. |
572 | | * @param p_j2k the jpeg2000 codec. |
573 | | * @param p_header_size the size of the data contained in the COC marker. |
574 | | * @param p_manager the user event manager. |
575 | | */ |
576 | | static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, |
577 | | OPJ_BYTE * p_header_data, |
578 | | OPJ_UINT32 p_header_size, |
579 | | opj_event_mgr_t * p_manager); |
580 | | |
581 | | /** |
582 | | * Writes the QCD marker (quantization default) |
583 | | * |
584 | | * @param p_j2k J2K codec. |
585 | | * @param p_stream the stream to write data to. |
586 | | * @param p_manager the user event manager. |
587 | | */ |
588 | | static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, |
589 | | opj_stream_private_t *p_stream, |
590 | | opj_event_mgr_t * p_manager); |
591 | | |
592 | | /** |
593 | | * Reads a QCD marker (Quantization defaults) |
594 | | * @param p_header_data the data contained in the QCD box. |
595 | | * @param p_j2k the jpeg2000 codec. |
596 | | * @param p_header_size the size of the data contained in the QCD marker. |
597 | | * @param p_manager the user event manager. |
598 | | */ |
599 | | static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, |
600 | | OPJ_BYTE * p_header_data, |
601 | | OPJ_UINT32 p_header_size, |
602 | | opj_event_mgr_t * p_manager); |
603 | | |
604 | | /** |
605 | | * Compare QCC markers (quantization component) |
606 | | * |
607 | | * @param p_j2k J2K codec. |
608 | | * @param p_first_comp_no the index of the first component to compare. |
609 | | * @param p_second_comp_no the index of the second component to compare. |
610 | | * |
611 | | * @return OPJ_TRUE if equals. |
612 | | */ |
613 | | static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, |
614 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
615 | | |
616 | | /** |
617 | | * Writes the QCC marker (quantization component) |
618 | | * |
619 | | * @param p_comp_no the index of the component to output. |
620 | | * @param p_stream the stream to write data to. |
621 | | * @param p_j2k J2K codec. |
622 | | * @param p_manager the user event manager. |
623 | | */ |
624 | | static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, |
625 | | OPJ_UINT32 p_comp_no, |
626 | | opj_stream_private_t *p_stream, |
627 | | opj_event_mgr_t * p_manager); |
628 | | |
629 | | /** |
630 | | * Writes the QCC marker (quantization component) |
631 | | * |
632 | | * @param p_j2k J2K codec. |
633 | | * @param p_comp_no the index of the component to output. |
634 | | * @param p_data FIXME DOC |
635 | | * @param p_data_written the stream to write data to. |
636 | | * @param p_manager the user event manager. |
637 | | */ |
638 | | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, |
639 | | OPJ_UINT32 p_comp_no, |
640 | | OPJ_BYTE * p_data, |
641 | | OPJ_UINT32 * p_data_written, |
642 | | opj_event_mgr_t * p_manager); |
643 | | |
644 | | /** |
645 | | * Gets the maximum size taken by a qcc. |
646 | | */ |
647 | | static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k); |
648 | | |
649 | | /** |
650 | | * Reads a QCC marker (Quantization component) |
651 | | * @param p_header_data the data contained in the QCC box. |
652 | | * @param p_j2k the jpeg2000 codec. |
653 | | * @param p_header_size the size of the data contained in the QCC marker. |
654 | | * @param p_manager the user event manager. |
655 | | */ |
656 | | static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, |
657 | | OPJ_BYTE * p_header_data, |
658 | | OPJ_UINT32 p_header_size, |
659 | | opj_event_mgr_t * p_manager); |
660 | | /** |
661 | | * Writes the POC marker (Progression Order Change) |
662 | | * |
663 | | * @param p_stream the stream to write data to. |
664 | | * @param p_j2k J2K codec. |
665 | | * @param p_manager the user event manager. |
666 | | */ |
667 | | static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, |
668 | | opj_stream_private_t *p_stream, |
669 | | opj_event_mgr_t * p_manager); |
670 | | /** |
671 | | * Writes the POC marker (Progression Order Change) |
672 | | * |
673 | | * @param p_j2k J2K codec. |
674 | | * @param p_data FIXME DOC |
675 | | * @param p_data_written the stream to write data to. |
676 | | * @param p_manager the user event manager. |
677 | | */ |
678 | | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, |
679 | | OPJ_BYTE * p_data, |
680 | | OPJ_UINT32 * p_data_written, |
681 | | opj_event_mgr_t * p_manager); |
682 | | /** |
683 | | * Gets the maximum size taken by the writing of a POC. |
684 | | */ |
685 | | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k); |
686 | | |
687 | | /** |
688 | | * Reads a POC marker (Progression Order Change) |
689 | | * |
690 | | * @param p_header_data the data contained in the POC box. |
691 | | * @param p_j2k the jpeg2000 codec. |
692 | | * @param p_header_size the size of the data contained in the POC marker. |
693 | | * @param p_manager the user event manager. |
694 | | */ |
695 | | static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, |
696 | | OPJ_BYTE * p_header_data, |
697 | | OPJ_UINT32 p_header_size, |
698 | | opj_event_mgr_t * p_manager); |
699 | | |
700 | | /** |
701 | | * Gets the maximum size taken by the toc headers of all the tile parts of any given tile. |
702 | | */ |
703 | | static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k); |
704 | | |
705 | | /** |
706 | | * Gets the maximum size taken by the headers of the SOT. |
707 | | * |
708 | | * @param p_j2k the jpeg2000 codec to use. |
709 | | */ |
710 | | static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k); |
711 | | |
712 | | /** |
713 | | * Reads a CRG marker (Component registration) |
714 | | * |
715 | | * @param p_header_data the data contained in the TLM box. |
716 | | * @param p_j2k the jpeg2000 codec. |
717 | | * @param p_header_size the size of the data contained in the TLM marker. |
718 | | * @param p_manager the user event manager. |
719 | | */ |
720 | | static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, |
721 | | OPJ_BYTE * p_header_data, |
722 | | OPJ_UINT32 p_header_size, |
723 | | opj_event_mgr_t * p_manager); |
724 | | /** |
725 | | * Reads a TLM marker (Tile Length Marker) |
726 | | * |
727 | | * @param p_header_data the data contained in the TLM box. |
728 | | * @param p_j2k the jpeg2000 codec. |
729 | | * @param p_header_size the size of the data contained in the TLM marker. |
730 | | * @param p_manager the user event manager. |
731 | | */ |
732 | | static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, |
733 | | OPJ_BYTE * p_header_data, |
734 | | OPJ_UINT32 p_header_size, |
735 | | opj_event_mgr_t * p_manager); |
736 | | |
737 | | /** |
738 | | * Writes the updated tlm. |
739 | | * |
740 | | * @param p_stream the stream to write data to. |
741 | | * @param p_j2k J2K codec. |
742 | | * @param p_manager the user event manager. |
743 | | */ |
744 | | static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, |
745 | | opj_stream_private_t *p_stream, |
746 | | opj_event_mgr_t * p_manager); |
747 | | |
748 | | /** |
749 | | * Reads a PLM marker (Packet length, main header marker) |
750 | | * |
751 | | * @param p_header_data the data contained in the TLM box. |
752 | | * @param p_j2k the jpeg2000 codec. |
753 | | * @param p_header_size the size of the data contained in the TLM marker. |
754 | | * @param p_manager the user event manager. |
755 | | */ |
756 | | static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, |
757 | | OPJ_BYTE * p_header_data, |
758 | | OPJ_UINT32 p_header_size, |
759 | | opj_event_mgr_t * p_manager); |
760 | | /** |
761 | | * Reads a PLT marker (Packet length, tile-part header) |
762 | | * |
763 | | * @param p_header_data the data contained in the PLT box. |
764 | | * @param p_j2k the jpeg2000 codec. |
765 | | * @param p_header_size the size of the data contained in the PLT marker. |
766 | | * @param p_manager the user event manager. |
767 | | */ |
768 | | static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, |
769 | | OPJ_BYTE * p_header_data, |
770 | | OPJ_UINT32 p_header_size, |
771 | | opj_event_mgr_t * p_manager); |
772 | | |
773 | | /** |
774 | | * Reads a PPM marker (Packed headers, main header) |
775 | | * |
776 | | * @param p_header_data the data contained in the POC box. |
777 | | * @param p_j2k the jpeg2000 codec. |
778 | | * @param p_header_size the size of the data contained in the POC marker. |
779 | | * @param p_manager the user event manager. |
780 | | */ |
781 | | |
782 | | static OPJ_BOOL opj_j2k_read_ppm( |
783 | | opj_j2k_t *p_j2k, |
784 | | OPJ_BYTE * p_header_data, |
785 | | OPJ_UINT32 p_header_size, |
786 | | opj_event_mgr_t * p_manager); |
787 | | |
788 | | /** |
789 | | * Merges all PPM markers read (Packed headers, main header) |
790 | | * |
791 | | * @param p_cp main coding parameters. |
792 | | * @param p_manager the user event manager. |
793 | | */ |
794 | | static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager); |
795 | | |
796 | | /** |
797 | | * Reads a PPT marker (Packed packet headers, tile-part header) |
798 | | * |
799 | | * @param p_header_data the data contained in the PPT box. |
800 | | * @param p_j2k the jpeg2000 codec. |
801 | | * @param p_header_size the size of the data contained in the PPT marker. |
802 | | * @param p_manager the user event manager. |
803 | | */ |
804 | | static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, |
805 | | OPJ_BYTE * p_header_data, |
806 | | OPJ_UINT32 p_header_size, |
807 | | opj_event_mgr_t * p_manager); |
808 | | |
809 | | /** |
810 | | * Merges all PPT markers read (Packed headers, tile-part header) |
811 | | * |
812 | | * @param p_tcp the tile. |
813 | | * @param p_manager the user event manager. |
814 | | */ |
815 | | static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, |
816 | | opj_event_mgr_t * p_manager); |
817 | | |
818 | | |
819 | | /** |
820 | | * Writes the TLM marker (Tile Length Marker) |
821 | | * |
822 | | * @param p_stream the stream to write data to. |
823 | | * @param p_j2k J2K codec. |
824 | | * @param p_manager the user event manager. |
825 | | */ |
826 | | static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, |
827 | | opj_stream_private_t *p_stream, |
828 | | opj_event_mgr_t * p_manager); |
829 | | |
830 | | /** |
831 | | * Writes the SOT marker (Start of tile-part) |
832 | | * |
833 | | * @param p_j2k J2K codec. |
834 | | * @param p_data Output buffer |
835 | | * @param total_data_size Output buffer size |
836 | | * @param p_data_written Number of bytes written into stream |
837 | | * @param p_stream the stream to write data to. |
838 | | * @param p_manager the user event manager. |
839 | | */ |
840 | | static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, |
841 | | OPJ_BYTE * p_data, |
842 | | OPJ_UINT32 total_data_size, |
843 | | OPJ_UINT32 * p_data_written, |
844 | | const opj_stream_private_t *p_stream, |
845 | | opj_event_mgr_t * p_manager); |
846 | | |
847 | | /** |
848 | | * Reads values from a SOT marker (Start of tile-part) |
849 | | * |
850 | | * the j2k decoder state is not affected. No side effects, no checks except for p_header_size. |
851 | | * |
852 | | * @param p_header_data the data contained in the SOT marker. |
853 | | * @param p_header_size the size of the data contained in the SOT marker. |
854 | | * @param p_tile_no Isot. |
855 | | * @param p_tot_len Psot. |
856 | | * @param p_current_part TPsot. |
857 | | * @param p_num_parts TNsot. |
858 | | * @param p_manager the user event manager. |
859 | | */ |
860 | | static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, |
861 | | OPJ_UINT32 p_header_size, |
862 | | OPJ_UINT32* p_tile_no, |
863 | | OPJ_UINT32* p_tot_len, |
864 | | OPJ_UINT32* p_current_part, |
865 | | OPJ_UINT32* p_num_parts, |
866 | | opj_event_mgr_t * p_manager); |
867 | | /** |
868 | | * Reads a SOT marker (Start of tile-part) |
869 | | * |
870 | | * @param p_header_data the data contained in the SOT marker. |
871 | | * @param p_j2k the jpeg2000 codec. |
872 | | * @param p_header_size the size of the data contained in the PPT marker. |
873 | | * @param p_manager the user event manager. |
874 | | */ |
875 | | static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, |
876 | | OPJ_BYTE * p_header_data, |
877 | | OPJ_UINT32 p_header_size, |
878 | | opj_event_mgr_t * p_manager); |
879 | | /** |
880 | | * Writes the SOD marker (Start of data) |
881 | | * |
882 | | * This also writes optional PLT markers (before SOD) |
883 | | * |
884 | | * @param p_j2k J2K codec. |
885 | | * @param p_tile_coder FIXME DOC |
886 | | * @param p_data FIXME DOC |
887 | | * @param p_data_written FIXME DOC |
888 | | * @param total_data_size FIXME DOC |
889 | | * @param p_stream the stream to write data to. |
890 | | * @param p_manager the user event manager. |
891 | | */ |
892 | | static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, |
893 | | opj_tcd_t * p_tile_coder, |
894 | | OPJ_BYTE * p_data, |
895 | | OPJ_UINT32 * p_data_written, |
896 | | OPJ_UINT32 total_data_size, |
897 | | const opj_stream_private_t *p_stream, |
898 | | opj_event_mgr_t * p_manager); |
899 | | |
900 | | /** |
901 | | * Reads a SOD marker (Start Of Data) |
902 | | * |
903 | | * @param p_j2k the jpeg2000 codec. |
904 | | * @param p_stream FIXME DOC |
905 | | * @param p_manager the user event manager. |
906 | | */ |
907 | | static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, |
908 | | opj_stream_private_t *p_stream, |
909 | | opj_event_mgr_t * p_manager); |
910 | | |
911 | | static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size) |
912 | 0 | { |
913 | 0 | if (p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte) { |
914 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
915 | 0 | p_j2k->m_current_tile_number, 1); |
916 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 1; |
917 | 0 | } else { |
918 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
919 | 0 | p_j2k->m_current_tile_number, 2); |
920 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 2; |
921 | 0 | } |
922 | |
|
923 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
924 | 0 | p_tile_part_size, 4); /* PSOT */ |
925 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; |
926 | 0 | } |
927 | | |
928 | | /** |
929 | | * Writes the RGN marker (Region Of Interest) |
930 | | * |
931 | | * @param p_tile_no the tile to output |
932 | | * @param p_comp_no the component to output |
933 | | * @param nb_comps the number of components |
934 | | * @param p_stream the stream to write data to. |
935 | | * @param p_j2k J2K codec. |
936 | | * @param p_manager the user event manager. |
937 | | */ |
938 | | static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, |
939 | | OPJ_UINT32 p_tile_no, |
940 | | OPJ_UINT32 p_comp_no, |
941 | | OPJ_UINT32 nb_comps, |
942 | | opj_stream_private_t *p_stream, |
943 | | opj_event_mgr_t * p_manager); |
944 | | |
945 | | /** |
946 | | * Reads a RGN marker (Region Of Interest) |
947 | | * |
948 | | * @param p_header_data the data contained in the POC box. |
949 | | * @param p_j2k the jpeg2000 codec. |
950 | | * @param p_header_size the size of the data contained in the POC marker. |
951 | | * @param p_manager the user event manager. |
952 | | */ |
953 | | static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, |
954 | | OPJ_BYTE * p_header_data, |
955 | | OPJ_UINT32 p_header_size, |
956 | | opj_event_mgr_t * p_manager); |
957 | | |
958 | | /** |
959 | | * Writes the EOC marker (End of Codestream) |
960 | | * |
961 | | * @param p_stream the stream to write data to. |
962 | | * @param p_j2k J2K codec. |
963 | | * @param p_manager the user event manager. |
964 | | */ |
965 | | static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, |
966 | | opj_stream_private_t *p_stream, |
967 | | opj_event_mgr_t * p_manager); |
968 | | |
969 | | #if 0 |
970 | | /** |
971 | | * Reads a EOC marker (End Of Codestream) |
972 | | * |
973 | | * @param p_j2k the jpeg2000 codec. |
974 | | * @param p_stream FIXME DOC |
975 | | * @param p_manager the user event manager. |
976 | | */ |
977 | | static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, |
978 | | opj_stream_private_t *p_stream, |
979 | | opj_event_mgr_t * p_manager); |
980 | | #endif |
981 | | |
982 | | /** |
983 | | * Writes the CBD-MCT-MCC-MCO markers (Multi components transform) |
984 | | * |
985 | | * @param p_stream the stream to write data to. |
986 | | * @param p_j2k J2K codec. |
987 | | * @param p_manager the user event manager. |
988 | | */ |
989 | | static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, |
990 | | opj_stream_private_t *p_stream, |
991 | | opj_event_mgr_t * p_manager); |
992 | | |
993 | | /** |
994 | | * Inits the Info |
995 | | * |
996 | | * @param p_stream the stream to write data to. |
997 | | * @param p_j2k J2K codec. |
998 | | * @param p_manager the user event manager. |
999 | | */ |
1000 | | static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, |
1001 | | opj_stream_private_t *p_stream, |
1002 | | opj_event_mgr_t * p_manager); |
1003 | | |
1004 | | /** |
1005 | | Add main header marker information |
1006 | | @param cstr_index Codestream information structure |
1007 | | @param type marker type |
1008 | | @param pos byte offset of marker segment |
1009 | | @param len length of marker segment |
1010 | | */ |
1011 | | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, |
1012 | | OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ; |
1013 | | /** |
1014 | | Add tile header marker information |
1015 | | @param tileno tile index number |
1016 | | @param cstr_index Codestream information structure |
1017 | | @param type marker type |
1018 | | @param pos byte offset of marker segment |
1019 | | @param len length of marker segment |
1020 | | */ |
1021 | | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, |
1022 | | opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, |
1023 | | OPJ_UINT32 len); |
1024 | | |
1025 | | /** |
1026 | | * Reads an unknown marker |
1027 | | * |
1028 | | * @param p_j2k the jpeg2000 codec. |
1029 | | * @param p_stream the stream object to read from. |
1030 | | * @param output_marker FIXME DOC |
1031 | | * @param p_manager the user event manager. |
1032 | | * |
1033 | | * @return true if the marker could be deduced. |
1034 | | */ |
1035 | | static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, |
1036 | | opj_stream_private_t *p_stream, |
1037 | | OPJ_UINT32 *output_marker, |
1038 | | opj_event_mgr_t * p_manager); |
1039 | | |
1040 | | /** |
1041 | | * Writes the MCT marker (Multiple Component Transform) |
1042 | | * |
1043 | | * @param p_j2k J2K codec. |
1044 | | * @param p_mct_record FIXME DOC |
1045 | | * @param p_stream the stream to write data to. |
1046 | | * @param p_manager the user event manager. |
1047 | | */ |
1048 | | static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, |
1049 | | opj_mct_data_t * p_mct_record, |
1050 | | opj_stream_private_t *p_stream, |
1051 | | opj_event_mgr_t * p_manager); |
1052 | | |
1053 | | /** |
1054 | | * Reads a MCT marker (Multiple Component Transform) |
1055 | | * |
1056 | | * @param p_header_data the data contained in the MCT box. |
1057 | | * @param p_j2k the jpeg2000 codec. |
1058 | | * @param p_header_size the size of the data contained in the MCT marker. |
1059 | | * @param p_manager the user event manager. |
1060 | | */ |
1061 | | static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, |
1062 | | OPJ_BYTE * p_header_data, |
1063 | | OPJ_UINT32 p_header_size, |
1064 | | opj_event_mgr_t * p_manager); |
1065 | | |
1066 | | /** |
1067 | | * Writes the MCC marker (Multiple Component Collection) |
1068 | | * |
1069 | | * @param p_j2k J2K codec. |
1070 | | * @param p_mcc_record FIXME DOC |
1071 | | * @param p_stream the stream to write data to. |
1072 | | * @param p_manager the user event manager. |
1073 | | */ |
1074 | | static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, |
1075 | | opj_simple_mcc_decorrelation_data_t * p_mcc_record, |
1076 | | opj_stream_private_t *p_stream, |
1077 | | opj_event_mgr_t * p_manager); |
1078 | | |
1079 | | /** |
1080 | | * Reads a MCC marker (Multiple Component Collection) |
1081 | | * |
1082 | | * @param p_header_data the data contained in the MCC box. |
1083 | | * @param p_j2k the jpeg2000 codec. |
1084 | | * @param p_header_size the size of the data contained in the MCC marker. |
1085 | | * @param p_manager the user event manager. |
1086 | | */ |
1087 | | static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, |
1088 | | OPJ_BYTE * p_header_data, |
1089 | | OPJ_UINT32 p_header_size, |
1090 | | opj_event_mgr_t * p_manager); |
1091 | | |
1092 | | /** |
1093 | | * Writes the MCO marker (Multiple component transformation ordering) |
1094 | | * |
1095 | | * @param p_stream the stream to write data to. |
1096 | | * @param p_j2k J2K codec. |
1097 | | * @param p_manager the user event manager. |
1098 | | */ |
1099 | | static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, |
1100 | | opj_stream_private_t *p_stream, |
1101 | | opj_event_mgr_t * p_manager); |
1102 | | |
1103 | | /** |
1104 | | * Reads a MCO marker (Multiple Component Transform Ordering) |
1105 | | * |
1106 | | * @param p_header_data the data contained in the MCO box. |
1107 | | * @param p_j2k the jpeg2000 codec. |
1108 | | * @param p_header_size the size of the data contained in the MCO marker. |
1109 | | * @param p_manager the user event manager. |
1110 | | */ |
1111 | | static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, |
1112 | | OPJ_BYTE * p_header_data, |
1113 | | OPJ_UINT32 p_header_size, |
1114 | | opj_event_mgr_t * p_manager); |
1115 | | |
1116 | | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, |
1117 | | OPJ_UINT32 p_index); |
1118 | | |
1119 | | static void opj_j2k_read_int16_to_float(const void * p_src_data, |
1120 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1121 | | static void opj_j2k_read_int32_to_float(const void * p_src_data, |
1122 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1123 | | static void opj_j2k_read_float32_to_float(const void * p_src_data, |
1124 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1125 | | static void opj_j2k_read_float64_to_float(const void * p_src_data, |
1126 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1127 | | |
1128 | | static void opj_j2k_read_int16_to_int32(const void * p_src_data, |
1129 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1130 | | static void opj_j2k_read_int32_to_int32(const void * p_src_data, |
1131 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1132 | | static void opj_j2k_read_float32_to_int32(const void * p_src_data, |
1133 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1134 | | static void opj_j2k_read_float64_to_int32(const void * p_src_data, |
1135 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1136 | | |
1137 | | static void opj_j2k_write_float_to_int16(const void * p_src_data, |
1138 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1139 | | static void opj_j2k_write_float_to_int32(const void * p_src_data, |
1140 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1141 | | static void opj_j2k_write_float_to_float(const void * p_src_data, |
1142 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1143 | | static void opj_j2k_write_float_to_float64(const void * p_src_data, |
1144 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1145 | | |
1146 | | /** |
1147 | | * Ends the encoding, i.e. frees memory. |
1148 | | * |
1149 | | * @param p_stream the stream to write data to. |
1150 | | * @param p_j2k J2K codec. |
1151 | | * @param p_manager the user event manager. |
1152 | | */ |
1153 | | static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, |
1154 | | opj_stream_private_t *p_stream, |
1155 | | opj_event_mgr_t * p_manager); |
1156 | | |
1157 | | /** |
1158 | | * Writes the CBD marker (Component bit depth definition) |
1159 | | * |
1160 | | * @param p_stream the stream to write data to. |
1161 | | * @param p_j2k J2K codec. |
1162 | | * @param p_manager the user event manager. |
1163 | | */ |
1164 | | static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, |
1165 | | opj_stream_private_t *p_stream, |
1166 | | opj_event_mgr_t * p_manager); |
1167 | | |
1168 | | /** |
1169 | | * Reads a CBD marker (Component bit depth definition) |
1170 | | * @param p_header_data the data contained in the CBD box. |
1171 | | * @param p_j2k the jpeg2000 codec. |
1172 | | * @param p_header_size the size of the data contained in the CBD marker. |
1173 | | * @param p_manager the user event manager. |
1174 | | */ |
1175 | | static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, |
1176 | | OPJ_BYTE * p_header_data, |
1177 | | OPJ_UINT32 p_header_size, |
1178 | | opj_event_mgr_t * p_manager); |
1179 | | |
1180 | | /** |
1181 | | * Reads a CAP marker (extended capabilities definition). Empty implementation. |
1182 | | * Found in HTJ2K files |
1183 | | * |
1184 | | * @param p_header_data the data contained in the CAP box. |
1185 | | * @param p_j2k the jpeg2000 codec. |
1186 | | * @param p_header_size the size of the data contained in the CAP marker. |
1187 | | * @param p_manager the user event manager. |
1188 | | */ |
1189 | | static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k, |
1190 | | OPJ_BYTE * p_header_data, |
1191 | | OPJ_UINT32 p_header_size, |
1192 | | opj_event_mgr_t * p_manager); |
1193 | | |
1194 | | /** |
1195 | | * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files |
1196 | | * @param p_header_data the data contained in the CPF box. |
1197 | | * @param p_j2k the jpeg2000 codec. |
1198 | | * @param p_header_size the size of the data contained in the CPF marker. |
1199 | | * @param p_manager the user event manager. |
1200 | | */ |
1201 | | static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k, |
1202 | | OPJ_BYTE * p_header_data, |
1203 | | OPJ_UINT32 p_header_size, |
1204 | | opj_event_mgr_t * p_manager); |
1205 | | |
1206 | | |
1207 | | /** |
1208 | | * Writes COC marker for each component. |
1209 | | * |
1210 | | * @param p_stream the stream to write data to. |
1211 | | * @param p_j2k J2K codec. |
1212 | | * @param p_manager the user event manager. |
1213 | | */ |
1214 | | static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k, |
1215 | | opj_stream_private_t *p_stream, |
1216 | | opj_event_mgr_t * p_manager); |
1217 | | |
1218 | | /** |
1219 | | * Writes QCC marker for each component. |
1220 | | * |
1221 | | * @param p_stream the stream to write data to. |
1222 | | * @param p_j2k J2K codec. |
1223 | | * @param p_manager the user event manager. |
1224 | | */ |
1225 | | static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k, |
1226 | | opj_stream_private_t *p_stream, |
1227 | | opj_event_mgr_t * p_manager); |
1228 | | |
1229 | | /** |
1230 | | * Writes regions of interests. |
1231 | | * |
1232 | | * @param p_stream the stream to write data to. |
1233 | | * @param p_j2k J2K codec. |
1234 | | * @param p_manager the user event manager. |
1235 | | */ |
1236 | | static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, |
1237 | | opj_stream_private_t *p_stream, |
1238 | | opj_event_mgr_t * p_manager); |
1239 | | |
1240 | | /** |
1241 | | * Writes EPC ???? |
1242 | | * |
1243 | | * @param p_stream the stream to write data to. |
1244 | | * @param p_j2k J2K codec. |
1245 | | * @param p_manager the user event manager. |
1246 | | */ |
1247 | | static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, |
1248 | | opj_stream_private_t *p_stream, |
1249 | | opj_event_mgr_t * p_manager); |
1250 | | |
1251 | | /** |
1252 | | * Checks the progression order changes values. Tells of the poc given as input are valid. |
1253 | | * A nice message is outputted at errors. |
1254 | | * |
1255 | | * @param p_pocs the progression order changes. |
1256 | | * @param tileno the tile number of interest |
1257 | | * @param p_nb_pocs the number of progression order changes. |
1258 | | * @param p_nb_resolutions the number of resolutions. |
1259 | | * @param numcomps the number of components |
1260 | | * @param numlayers the number of layers. |
1261 | | * @param p_manager the user event manager. |
1262 | | * |
1263 | | * @return true if the pocs are valid. |
1264 | | */ |
1265 | | static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, |
1266 | | OPJ_UINT32 tileno, |
1267 | | OPJ_UINT32 p_nb_pocs, |
1268 | | OPJ_UINT32 p_nb_resolutions, |
1269 | | OPJ_UINT32 numcomps, |
1270 | | OPJ_UINT32 numlayers, |
1271 | | opj_event_mgr_t * p_manager); |
1272 | | |
1273 | | /** |
1274 | | * Gets the number of tile parts used for the given change of progression (if any) and the given tile. |
1275 | | * |
1276 | | * @param cp the coding parameters. |
1277 | | * @param pino the offset of the given poc (i.e. its position in the coding parameter). |
1278 | | * @param tileno the given tile. |
1279 | | * |
1280 | | * @return the number of tile parts. |
1281 | | */ |
1282 | | static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, |
1283 | | OPJ_UINT32 tileno); |
1284 | | |
1285 | | /** |
1286 | | * Calculates the total number of tile parts needed by the encoder to |
1287 | | * encode such an image. If not enough memory is available, then the function return false. |
1288 | | * |
1289 | | * @param p_nb_tiles pointer that will hold the number of tile parts. |
1290 | | * @param cp the coding parameters for the image. |
1291 | | * @param image the image to encode. |
1292 | | * @param p_j2k the p_j2k encoder. |
1293 | | * @param p_manager the user event manager. |
1294 | | * |
1295 | | * @return true if the function was successful, false else. |
1296 | | */ |
1297 | | static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k, |
1298 | | opj_cp_t *cp, |
1299 | | OPJ_UINT32 * p_nb_tiles, |
1300 | | opj_image_t *image, |
1301 | | opj_event_mgr_t * p_manager); |
1302 | | |
1303 | | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream); |
1304 | | |
1305 | | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream); |
1306 | | |
1307 | | static opj_codestream_index_t* opj_j2k_create_cstr_index(void); |
1308 | | |
1309 | | static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp); |
1310 | | |
1311 | | static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp); |
1312 | | |
1313 | | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres); |
1314 | | |
1315 | | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, |
1316 | | opj_image_t *image, opj_event_mgr_t *p_manager); |
1317 | | |
1318 | | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, |
1319 | | opj_event_mgr_t *p_manager); |
1320 | | |
1321 | | static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters, |
1322 | | opj_image_t *image, opj_event_mgr_t *p_manager); |
1323 | | |
1324 | | static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters, |
1325 | | opj_image_t *image, |
1326 | | opj_event_mgr_t *p_manager); |
1327 | | |
1328 | | /** |
1329 | | * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254. |
1330 | | * |
1331 | | * @param p_stream the stream to read data from. |
1332 | | * @param tile_no tile number we're looking for. |
1333 | | * @param p_correction_needed output value. if true, non conformant codestream needs TNsot correction. |
1334 | | * @param p_manager the user event manager. |
1335 | | * |
1336 | | * @return true if the function was successful, false else. |
1337 | | */ |
1338 | | static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t |
1339 | | *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, |
1340 | | opj_event_mgr_t * p_manager); |
1341 | | |
1342 | | /*@}*/ |
1343 | | |
1344 | | /*@}*/ |
1345 | | |
1346 | | /* ----------------------------------------------------------------------- */ |
1347 | | typedef struct j2k_prog_order { |
1348 | | OPJ_PROG_ORDER enum_prog; |
1349 | | char str_prog[5]; |
1350 | | } j2k_prog_order_t; |
1351 | | |
1352 | | static const j2k_prog_order_t j2k_prog_order_list[] = { |
1353 | | {OPJ_CPRL, "CPRL"}, |
1354 | | {OPJ_LRCP, "LRCP"}, |
1355 | | {OPJ_PCRL, "PCRL"}, |
1356 | | {OPJ_RLCP, "RLCP"}, |
1357 | | {OPJ_RPCL, "RPCL"}, |
1358 | | {(OPJ_PROG_ORDER) - 1, ""} |
1359 | | }; |
1360 | | |
1361 | | /** |
1362 | | * FIXME DOC |
1363 | | */ |
1364 | | static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = { |
1365 | | 2, |
1366 | | 4, |
1367 | | 4, |
1368 | | 8 |
1369 | | }; |
1370 | | |
1371 | | typedef void (* opj_j2k_mct_function)(const void * p_src_data, |
1372 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1373 | | |
1374 | | static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = { |
1375 | | opj_j2k_read_int16_to_float, |
1376 | | opj_j2k_read_int32_to_float, |
1377 | | opj_j2k_read_float32_to_float, |
1378 | | opj_j2k_read_float64_to_float |
1379 | | }; |
1380 | | |
1381 | | static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = { |
1382 | | opj_j2k_read_int16_to_int32, |
1383 | | opj_j2k_read_int32_to_int32, |
1384 | | opj_j2k_read_float32_to_int32, |
1385 | | opj_j2k_read_float64_to_int32 |
1386 | | }; |
1387 | | |
1388 | | static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = { |
1389 | | opj_j2k_write_float_to_int16, |
1390 | | opj_j2k_write_float_to_int32, |
1391 | | opj_j2k_write_float_to_float, |
1392 | | opj_j2k_write_float_to_float64 |
1393 | | }; |
1394 | | |
1395 | | typedef struct opj_dec_memory_marker_handler { |
1396 | | /** marker value */ |
1397 | | OPJ_UINT32 id; |
1398 | | /** value of the state when the marker can appear */ |
1399 | | OPJ_UINT32 states; |
1400 | | /** action linked to the marker */ |
1401 | | OPJ_BOOL(*handler)(opj_j2k_t *p_j2k, |
1402 | | OPJ_BYTE * p_header_data, |
1403 | | OPJ_UINT32 p_header_size, |
1404 | | opj_event_mgr_t * p_manager); |
1405 | | } |
1406 | | opj_dec_memory_marker_handler_t; |
1407 | | |
1408 | | static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] = |
1409 | | { |
1410 | | {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot}, |
1411 | | {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod}, |
1412 | | {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc}, |
1413 | | {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn}, |
1414 | | {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd}, |
1415 | | {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc}, |
1416 | | {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc}, |
1417 | | {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz}, |
1418 | | {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm}, |
1419 | | {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm}, |
1420 | | {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt}, |
1421 | | {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm}, |
1422 | | {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt}, |
1423 | | {J2K_MS_SOP, 0, 0}, |
1424 | | {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg}, |
1425 | | {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com}, |
1426 | | {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct}, |
1427 | | {J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd}, |
1428 | | {J2K_MS_CAP, J2K_STATE_MH, opj_j2k_read_cap}, |
1429 | | {J2K_MS_CPF, J2K_STATE_MH, opj_j2k_read_cpf}, |
1430 | | {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc}, |
1431 | | {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco}, |
1432 | | #ifdef USE_JPWL |
1433 | | #ifdef TODO_MS /* remove these functions which are not compatible with the v2 API */ |
1434 | | {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, |
1435 | | {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, |
1436 | | {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, |
1437 | | {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, |
1438 | | #endif |
1439 | | #endif /* USE_JPWL */ |
1440 | | #ifdef USE_JPSEC |
1441 | | {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, |
1442 | | {J2K_MS_INSEC, 0, j2k_read_insec} |
1443 | | #endif /* USE_JPSEC */ |
1444 | | {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/ |
1445 | | }; |
1446 | | |
1447 | | static void opj_j2k_read_int16_to_float(const void * p_src_data, |
1448 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1449 | 2.03k | { |
1450 | 2.03k | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1451 | 2.03k | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1452 | 2.03k | OPJ_UINT32 i; |
1453 | 2.03k | OPJ_UINT32 l_temp; |
1454 | | |
1455 | 5.80k | for (i = 0; i < p_nb_elem; ++i) { |
1456 | 3.76k | opj_read_bytes(l_src_data, &l_temp, 2); |
1457 | | |
1458 | 3.76k | l_src_data += sizeof(OPJ_INT16); |
1459 | | |
1460 | 3.76k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1461 | 3.76k | } |
1462 | 2.03k | } |
1463 | | |
1464 | | static void opj_j2k_read_int32_to_float(const void * p_src_data, |
1465 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1466 | 529 | { |
1467 | 529 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1468 | 529 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1469 | 529 | OPJ_UINT32 i; |
1470 | 529 | OPJ_UINT32 l_temp; |
1471 | | |
1472 | 2.47k | for (i = 0; i < p_nb_elem; ++i) { |
1473 | 1.94k | opj_read_bytes(l_src_data, &l_temp, 4); |
1474 | | |
1475 | 1.94k | l_src_data += sizeof(OPJ_INT32); |
1476 | | |
1477 | 1.94k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1478 | 1.94k | } |
1479 | 529 | } |
1480 | | |
1481 | | static void opj_j2k_read_float32_to_float(const void * p_src_data, |
1482 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1483 | 424 | { |
1484 | 424 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1485 | 424 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1486 | 424 | OPJ_UINT32 i; |
1487 | 424 | OPJ_FLOAT32 l_temp; |
1488 | | |
1489 | 1.88k | for (i = 0; i < p_nb_elem; ++i) { |
1490 | 1.46k | opj_read_float(l_src_data, &l_temp); |
1491 | | |
1492 | 1.46k | l_src_data += sizeof(OPJ_FLOAT32); |
1493 | | |
1494 | 1.46k | *(l_dest_data++) = l_temp; |
1495 | 1.46k | } |
1496 | 424 | } |
1497 | | |
1498 | | static void opj_j2k_read_float64_to_float(const void * p_src_data, |
1499 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1500 | 363 | { |
1501 | 363 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1502 | 363 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1503 | 363 | OPJ_UINT32 i; |
1504 | 363 | OPJ_FLOAT64 l_temp; |
1505 | | |
1506 | 1.80k | for (i = 0; i < p_nb_elem; ++i) { |
1507 | 1.44k | opj_read_double(l_src_data, &l_temp); |
1508 | | |
1509 | 1.44k | l_src_data += sizeof(OPJ_FLOAT64); |
1510 | | |
1511 | 1.44k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1512 | 1.44k | } |
1513 | 363 | } |
1514 | | |
1515 | | static void opj_j2k_read_int16_to_int32(const void * p_src_data, |
1516 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1517 | 1.51k | { |
1518 | 1.51k | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1519 | 1.51k | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1520 | 1.51k | OPJ_UINT32 i; |
1521 | 1.51k | OPJ_UINT32 l_temp; |
1522 | | |
1523 | 3.07k | for (i = 0; i < p_nb_elem; ++i) { |
1524 | 1.56k | opj_read_bytes(l_src_data, &l_temp, 2); |
1525 | | |
1526 | 1.56k | l_src_data += sizeof(OPJ_INT16); |
1527 | | |
1528 | 1.56k | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1529 | 1.56k | } |
1530 | 1.51k | } |
1531 | | |
1532 | | static void opj_j2k_read_int32_to_int32(const void * p_src_data, |
1533 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1534 | 122 | { |
1535 | 122 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1536 | 122 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1537 | 122 | OPJ_UINT32 i; |
1538 | 122 | OPJ_UINT32 l_temp; |
1539 | | |
1540 | 315 | for (i = 0; i < p_nb_elem; ++i) { |
1541 | 193 | opj_read_bytes(l_src_data, &l_temp, 4); |
1542 | | |
1543 | 193 | l_src_data += sizeof(OPJ_INT32); |
1544 | | |
1545 | 193 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1546 | 193 | } |
1547 | 122 | } |
1548 | | |
1549 | | static void opj_j2k_read_float32_to_int32(const void * p_src_data, |
1550 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1551 | 448 | { |
1552 | 448 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1553 | 448 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1554 | 448 | OPJ_UINT32 i; |
1555 | 448 | OPJ_FLOAT32 l_temp; |
1556 | | |
1557 | 1.24k | for (i = 0; i < p_nb_elem; ++i) { |
1558 | 796 | opj_read_float(l_src_data, &l_temp); |
1559 | | |
1560 | 796 | l_src_data += sizeof(OPJ_FLOAT32); |
1561 | | |
1562 | 796 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1563 | 796 | } |
1564 | 448 | } |
1565 | | |
1566 | | static void opj_j2k_read_float64_to_int32(const void * p_src_data, |
1567 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1568 | 77 | { |
1569 | 77 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1570 | 77 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1571 | 77 | OPJ_UINT32 i; |
1572 | 77 | OPJ_FLOAT64 l_temp; |
1573 | | |
1574 | 228 | for (i = 0; i < p_nb_elem; ++i) { |
1575 | 151 | opj_read_double(l_src_data, &l_temp); |
1576 | | |
1577 | 151 | l_src_data += sizeof(OPJ_FLOAT64); |
1578 | | |
1579 | 151 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1580 | 151 | } |
1581 | 77 | } |
1582 | | |
1583 | | static void opj_j2k_write_float_to_int16(const void * p_src_data, |
1584 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1585 | 0 | { |
1586 | 0 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1587 | 0 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1588 | 0 | OPJ_UINT32 i; |
1589 | 0 | OPJ_UINT32 l_temp; |
1590 | |
|
1591 | 0 | for (i = 0; i < p_nb_elem; ++i) { |
1592 | 0 | l_temp = (OPJ_UINT32) * (l_src_data++); |
1593 | |
|
1594 | 0 | opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT16)); |
1595 | |
|
1596 | 0 | l_dest_data += sizeof(OPJ_INT16); |
1597 | 0 | } |
1598 | 0 | } |
1599 | | |
1600 | | static void opj_j2k_write_float_to_int32(const void * p_src_data, |
1601 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1602 | 0 | { |
1603 | 0 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1604 | 0 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1605 | 0 | OPJ_UINT32 i; |
1606 | 0 | OPJ_UINT32 l_temp; |
1607 | |
|
1608 | 0 | for (i = 0; i < p_nb_elem; ++i) { |
1609 | 0 | l_temp = (OPJ_UINT32) * (l_src_data++); |
1610 | |
|
1611 | 0 | opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT32)); |
1612 | |
|
1613 | 0 | l_dest_data += sizeof(OPJ_INT32); |
1614 | 0 | } |
1615 | 0 | } |
1616 | | |
1617 | | static void opj_j2k_write_float_to_float(const void * p_src_data, |
1618 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1619 | 0 | { |
1620 | 0 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1621 | 0 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1622 | 0 | OPJ_UINT32 i; |
1623 | 0 | OPJ_FLOAT32 l_temp; |
1624 | |
|
1625 | 0 | for (i = 0; i < p_nb_elem; ++i) { |
1626 | 0 | l_temp = (OPJ_FLOAT32) * (l_src_data++); |
1627 | |
|
1628 | 0 | opj_write_float(l_dest_data, l_temp); |
1629 | |
|
1630 | 0 | l_dest_data += sizeof(OPJ_FLOAT32); |
1631 | 0 | } |
1632 | 0 | } |
1633 | | |
1634 | | static void opj_j2k_write_float_to_float64(const void * p_src_data, |
1635 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1636 | 0 | { |
1637 | 0 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1638 | 0 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1639 | 0 | OPJ_UINT32 i; |
1640 | 0 | OPJ_FLOAT64 l_temp; |
1641 | |
|
1642 | 0 | for (i = 0; i < p_nb_elem; ++i) { |
1643 | 0 | l_temp = (OPJ_FLOAT64) * (l_src_data++); |
1644 | |
|
1645 | 0 | opj_write_double(l_dest_data, l_temp); |
1646 | |
|
1647 | 0 | l_dest_data += sizeof(OPJ_FLOAT64); |
1648 | 0 | } |
1649 | 0 | } |
1650 | | |
1651 | | const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order) |
1652 | 0 | { |
1653 | 0 | const j2k_prog_order_t *po; |
1654 | 0 | for (po = j2k_prog_order_list; po->enum_prog != -1; po++) { |
1655 | 0 | if (po->enum_prog == prg_order) { |
1656 | 0 | return po->str_prog; |
1657 | 0 | } |
1658 | 0 | } |
1659 | 0 | return po->str_prog; |
1660 | 0 | } |
1661 | | |
1662 | | static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, |
1663 | | OPJ_UINT32 tileno, |
1664 | | OPJ_UINT32 p_nb_pocs, |
1665 | | OPJ_UINT32 p_nb_resolutions, |
1666 | | OPJ_UINT32 p_num_comps, |
1667 | | OPJ_UINT32 p_num_layers, |
1668 | | opj_event_mgr_t * p_manager) |
1669 | 0 | { |
1670 | 0 | OPJ_UINT32* packet_array; |
1671 | 0 | OPJ_UINT32 index, resno, compno, layno; |
1672 | 0 | OPJ_UINT32 i; |
1673 | 0 | OPJ_UINT32 step_c = 1; |
1674 | 0 | OPJ_UINT32 step_r = p_num_comps * step_c; |
1675 | 0 | OPJ_UINT32 step_l = p_nb_resolutions * step_r; |
1676 | 0 | OPJ_BOOL loss = OPJ_FALSE; |
1677 | |
|
1678 | 0 | assert(p_nb_pocs > 0); |
1679 | |
|
1680 | 0 | packet_array = (OPJ_UINT32*) opj_calloc((size_t)step_l * p_num_layers, |
1681 | 0 | sizeof(OPJ_UINT32)); |
1682 | 0 | if (packet_array == 00) { |
1683 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
1684 | 0 | "Not enough memory for checking the poc values.\n"); |
1685 | 0 | return OPJ_FALSE; |
1686 | 0 | } |
1687 | | |
1688 | | /* iterate through all the pocs that match our tile of interest. */ |
1689 | 0 | for (i = 0; i < p_nb_pocs; ++i) { |
1690 | 0 | const opj_poc_t *poc = &p_pocs[i]; |
1691 | 0 | if (tileno + 1 == poc->tile) { |
1692 | 0 | index = step_r * poc->resno0; |
1693 | | |
1694 | | /* take each resolution for each poc */ |
1695 | 0 | for (resno = poc->resno0 ; |
1696 | 0 | resno < opj_uint_min(poc->resno1, p_nb_resolutions); ++resno) { |
1697 | 0 | OPJ_UINT32 res_index = index + poc->compno0 * step_c; |
1698 | | |
1699 | | /* take each comp of each resolution for each poc */ |
1700 | 0 | for (compno = poc->compno0 ; |
1701 | 0 | compno < opj_uint_min(poc->compno1, p_num_comps); ++compno) { |
1702 | | /* The layer index always starts at zero for every progression. */ |
1703 | 0 | const OPJ_UINT32 layno0 = 0; |
1704 | 0 | OPJ_UINT32 comp_index = res_index + layno0 * step_l; |
1705 | | |
1706 | | /* and finally take each layer of each res of ... */ |
1707 | 0 | for (layno = layno0; layno < opj_uint_min(poc->layno1, p_num_layers); |
1708 | 0 | ++layno) { |
1709 | 0 | packet_array[comp_index] = 1; |
1710 | 0 | comp_index += step_l; |
1711 | 0 | } |
1712 | |
|
1713 | 0 | res_index += step_c; |
1714 | 0 | } |
1715 | |
|
1716 | 0 | index += step_r; |
1717 | 0 | } |
1718 | 0 | } |
1719 | 0 | } |
1720 | |
|
1721 | 0 | index = 0; |
1722 | 0 | for (layno = 0; layno < p_num_layers ; ++layno) { |
1723 | 0 | for (resno = 0; resno < p_nb_resolutions; ++resno) { |
1724 | 0 | for (compno = 0; compno < p_num_comps; ++compno) { |
1725 | 0 | loss |= (packet_array[index] != 1); |
1726 | | #ifdef DEBUG_VERBOSE |
1727 | | if (packet_array[index] != 1) { |
1728 | | fprintf(stderr, |
1729 | | "Missing packet in POC: layno=%d resno=%d compno=%d\n", |
1730 | | layno, resno, compno); |
1731 | | } |
1732 | | #endif |
1733 | 0 | index += step_c; |
1734 | 0 | } |
1735 | 0 | } |
1736 | 0 | } |
1737 | |
|
1738 | 0 | if (loss) { |
1739 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Missing packets possible loss of data\n"); |
1740 | 0 | } |
1741 | |
|
1742 | 0 | opj_free(packet_array); |
1743 | |
|
1744 | 0 | return !loss; |
1745 | 0 | } |
1746 | | |
1747 | | /* ----------------------------------------------------------------------- */ |
1748 | | |
1749 | | static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, |
1750 | | OPJ_UINT32 tileno) |
1751 | 0 | { |
1752 | 0 | const OPJ_CHAR *prog = 00; |
1753 | 0 | OPJ_INT32 i; |
1754 | 0 | OPJ_UINT32 tpnum = 1; |
1755 | 0 | opj_tcp_t *tcp = 00; |
1756 | 0 | opj_poc_t * l_current_poc = 00; |
1757 | | |
1758 | | /* preconditions */ |
1759 | 0 | assert(tileno < (cp->tw * cp->th)); |
1760 | 0 | assert(pino < (cp->tcps[tileno].numpocs + 1)); |
1761 | | |
1762 | | /* get the given tile coding parameter */ |
1763 | 0 | tcp = &cp->tcps[tileno]; |
1764 | 0 | assert(tcp != 00); |
1765 | |
|
1766 | 0 | l_current_poc = &(tcp->pocs[pino]); |
1767 | 0 | assert(l_current_poc != 0); |
1768 | | |
1769 | | /* get the progression order as a character string */ |
1770 | 0 | prog = opj_j2k_convert_progression_order(tcp->prg); |
1771 | 0 | assert(strlen(prog) > 0); |
1772 | |
|
1773 | 0 | if (cp->m_specific_param.m_enc.m_tp_on == 1) { |
1774 | 0 | for (i = 0; i < 4; ++i) { |
1775 | 0 | switch (prog[i]) { |
1776 | | /* component wise */ |
1777 | 0 | case 'C': |
1778 | 0 | tpnum *= l_current_poc->compE; |
1779 | 0 | break; |
1780 | | /* resolution wise */ |
1781 | 0 | case 'R': |
1782 | 0 | tpnum *= l_current_poc->resE; |
1783 | 0 | break; |
1784 | | /* precinct wise */ |
1785 | 0 | case 'P': |
1786 | 0 | tpnum *= l_current_poc->prcE; |
1787 | 0 | break; |
1788 | | /* layer wise */ |
1789 | 0 | case 'L': |
1790 | 0 | tpnum *= l_current_poc->layE; |
1791 | 0 | break; |
1792 | 0 | } |
1793 | | /* would we split here ? */ |
1794 | 0 | if (cp->m_specific_param.m_enc.m_tp_flag == prog[i]) { |
1795 | 0 | cp->m_specific_param.m_enc.m_tp_pos = i; |
1796 | 0 | break; |
1797 | 0 | } |
1798 | 0 | } |
1799 | 0 | } else { |
1800 | 0 | tpnum = 1; |
1801 | 0 | } |
1802 | | |
1803 | 0 | return tpnum; |
1804 | 0 | } |
1805 | | |
1806 | | static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k, |
1807 | | opj_cp_t *cp, |
1808 | | OPJ_UINT32 * p_nb_tiles, |
1809 | | opj_image_t *image, |
1810 | | opj_event_mgr_t * p_manager |
1811 | | ) |
1812 | 0 | { |
1813 | 0 | OPJ_UINT32 pino, tileno; |
1814 | 0 | OPJ_UINT32 l_nb_tiles; |
1815 | 0 | opj_tcp_t *tcp; |
1816 | | |
1817 | | /* preconditions */ |
1818 | 0 | assert(p_nb_tiles != 00); |
1819 | 0 | assert(cp != 00); |
1820 | 0 | assert(image != 00); |
1821 | 0 | assert(p_j2k != 00); |
1822 | 0 | assert(p_manager != 00); |
1823 | |
|
1824 | 0 | OPJ_UNUSED(p_j2k); |
1825 | 0 | OPJ_UNUSED(p_manager); |
1826 | |
|
1827 | 0 | l_nb_tiles = cp->tw * cp->th; |
1828 | 0 | * p_nb_tiles = 0; |
1829 | 0 | tcp = cp->tcps; |
1830 | | |
1831 | | /* INDEX >> */ |
1832 | | /* TODO mergeV2: check this part which use cstr_info */ |
1833 | | /*if (p_j2k->cstr_info) { |
1834 | | opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile; |
1835 | | |
1836 | | for (tileno = 0; tileno < l_nb_tiles; ++tileno) { |
1837 | | OPJ_UINT32 cur_totnum_tp = 0; |
1838 | | |
1839 | | opj_pi_update_encoding_parameters(image,cp,tileno); |
1840 | | |
1841 | | for (pino = 0; pino <= tcp->numpocs; ++pino) |
1842 | | { |
1843 | | OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); |
1844 | | |
1845 | | *p_nb_tiles = *p_nb_tiles + tp_num; |
1846 | | |
1847 | | cur_totnum_tp += tp_num; |
1848 | | } |
1849 | | |
1850 | | tcp->m_nb_tile_parts = cur_totnum_tp; |
1851 | | |
1852 | | l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); |
1853 | | if (l_info_tile_ptr->tp == 00) { |
1854 | | return OPJ_FALSE; |
1855 | | } |
1856 | | |
1857 | | memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t)); |
1858 | | |
1859 | | l_info_tile_ptr->num_tps = cur_totnum_tp; |
1860 | | |
1861 | | ++l_info_tile_ptr; |
1862 | | ++tcp; |
1863 | | } |
1864 | | } |
1865 | | else */{ |
1866 | 0 | for (tileno = 0; tileno < l_nb_tiles; ++tileno) { |
1867 | 0 | OPJ_UINT32 cur_totnum_tp = 0; |
1868 | |
|
1869 | 0 | opj_pi_update_encoding_parameters(image, cp, tileno); |
1870 | |
|
1871 | 0 | for (pino = 0; pino <= tcp->numpocs; ++pino) { |
1872 | 0 | OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp, pino, tileno); |
1873 | |
|
1874 | 0 | *p_nb_tiles = *p_nb_tiles + tp_num; |
1875 | |
|
1876 | 0 | cur_totnum_tp += tp_num; |
1877 | 0 | } |
1878 | 0 | tcp->m_nb_tile_parts = cur_totnum_tp; |
1879 | |
|
1880 | 0 | ++tcp; |
1881 | 0 | } |
1882 | 0 | } |
1883 | |
|
1884 | 0 | return OPJ_TRUE; |
1885 | 0 | } |
1886 | | |
1887 | | static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k, |
1888 | | opj_stream_private_t *p_stream, |
1889 | | opj_event_mgr_t * p_manager) |
1890 | 0 | { |
1891 | | /* 2 bytes will be written */ |
1892 | 0 | OPJ_BYTE * l_start_stream = 00; |
1893 | | |
1894 | | /* preconditions */ |
1895 | 0 | assert(p_stream != 00); |
1896 | 0 | assert(p_j2k != 00); |
1897 | 0 | assert(p_manager != 00); |
1898 | |
|
1899 | 0 | l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
1900 | | |
1901 | | /* write SOC identifier */ |
1902 | 0 | opj_write_bytes(l_start_stream, J2K_MS_SOC, 2); |
1903 | |
|
1904 | 0 | if (opj_stream_write_data(p_stream, l_start_stream, 2, p_manager) != 2) { |
1905 | 0 | return OPJ_FALSE; |
1906 | 0 | } |
1907 | | |
1908 | | /* UniPG>> */ |
1909 | | #ifdef USE_JPWL |
1910 | | /* update markers struct */ |
1911 | | /* |
1912 | | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2); |
1913 | | */ |
1914 | | assert(0 && "TODO"); |
1915 | | #endif /* USE_JPWL */ |
1916 | | /* <<UniPG */ |
1917 | | |
1918 | 0 | return OPJ_TRUE; |
1919 | 0 | } |
1920 | | |
1921 | | /** |
1922 | | * Reads a SOC marker (Start of Codestream) |
1923 | | * @param p_j2k the jpeg2000 file codec. |
1924 | | * @param p_stream FIXME DOC |
1925 | | * @param p_manager the user event manager. |
1926 | | */ |
1927 | | static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k, |
1928 | | opj_stream_private_t *p_stream, |
1929 | | opj_event_mgr_t * p_manager |
1930 | | ) |
1931 | 16.6k | { |
1932 | 16.6k | OPJ_BYTE l_data [2]; |
1933 | 16.6k | OPJ_UINT32 l_marker; |
1934 | | |
1935 | | /* preconditions */ |
1936 | 16.6k | assert(p_j2k != 00); |
1937 | 16.6k | assert(p_manager != 00); |
1938 | 16.6k | assert(p_stream != 00); |
1939 | | |
1940 | 16.6k | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
1941 | 34 | return OPJ_FALSE; |
1942 | 34 | } |
1943 | | |
1944 | 16.6k | opj_read_bytes(l_data, &l_marker, 2); |
1945 | 16.6k | if (l_marker != J2K_MS_SOC) { |
1946 | 25 | return OPJ_FALSE; |
1947 | 25 | } |
1948 | | |
1949 | | /* Next marker should be a SIZ marker in the main header */ |
1950 | 16.5k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; |
1951 | | |
1952 | | /* FIXME move it in a index structure included in p_j2k*/ |
1953 | 16.5k | p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; |
1954 | | |
1955 | 16.5k | opj_event_msg(p_manager, EVT_INFO, |
1956 | 16.5k | "Start to read j2k main header (%" PRId64 ").\n", |
1957 | 16.5k | p_j2k->cstr_index->main_head_start); |
1958 | | |
1959 | | /* Add the marker to the codestream index*/ |
1960 | 16.5k | if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, |
1961 | 16.5k | p_j2k->cstr_index->main_head_start, 2)) { |
1962 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
1963 | 0 | return OPJ_FALSE; |
1964 | 0 | } |
1965 | 16.5k | return OPJ_TRUE; |
1966 | 16.5k | } |
1967 | | |
1968 | | static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, |
1969 | | opj_stream_private_t *p_stream, |
1970 | | opj_event_mgr_t * p_manager) |
1971 | 0 | { |
1972 | 0 | OPJ_UINT32 i; |
1973 | 0 | OPJ_UINT32 l_size_len; |
1974 | 0 | OPJ_BYTE * l_current_ptr; |
1975 | 0 | opj_image_t * l_image = 00; |
1976 | 0 | opj_cp_t *cp = 00; |
1977 | 0 | opj_image_comp_t * l_img_comp = 00; |
1978 | | |
1979 | | /* preconditions */ |
1980 | 0 | assert(p_stream != 00); |
1981 | 0 | assert(p_j2k != 00); |
1982 | 0 | assert(p_manager != 00); |
1983 | |
|
1984 | 0 | l_image = p_j2k->m_private_image; |
1985 | 0 | cp = &(p_j2k->m_cp); |
1986 | 0 | l_size_len = 40 + 3 * l_image->numcomps; |
1987 | 0 | l_img_comp = l_image->comps; |
1988 | |
|
1989 | 0 | if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
1990 | |
|
1991 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
1992 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len); |
1993 | 0 | if (! new_header_tile_data) { |
1994 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
1995 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
1996 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
1997 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n"); |
1998 | 0 | return OPJ_FALSE; |
1999 | 0 | } |
2000 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2001 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len; |
2002 | 0 | } |
2003 | | |
2004 | 0 | l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2005 | | |
2006 | | /* write SOC identifier */ |
2007 | 0 | opj_write_bytes(l_current_ptr, J2K_MS_SIZ, 2); /* SIZ */ |
2008 | 0 | l_current_ptr += 2; |
2009 | |
|
2010 | 0 | opj_write_bytes(l_current_ptr, l_size_len - 2, 2); /* L_SIZ */ |
2011 | 0 | l_current_ptr += 2; |
2012 | |
|
2013 | 0 | opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */ |
2014 | 0 | l_current_ptr += 2; |
2015 | |
|
2016 | 0 | opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */ |
2017 | 0 | l_current_ptr += 4; |
2018 | |
|
2019 | 0 | opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */ |
2020 | 0 | l_current_ptr += 4; |
2021 | |
|
2022 | 0 | opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */ |
2023 | 0 | l_current_ptr += 4; |
2024 | |
|
2025 | 0 | opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */ |
2026 | 0 | l_current_ptr += 4; |
2027 | |
|
2028 | 0 | opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */ |
2029 | 0 | l_current_ptr += 4; |
2030 | |
|
2031 | 0 | opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */ |
2032 | 0 | l_current_ptr += 4; |
2033 | |
|
2034 | 0 | opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */ |
2035 | 0 | l_current_ptr += 4; |
2036 | |
|
2037 | 0 | opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */ |
2038 | 0 | l_current_ptr += 4; |
2039 | |
|
2040 | 0 | opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */ |
2041 | 0 | l_current_ptr += 2; |
2042 | |
|
2043 | 0 | for (i = 0; i < l_image->numcomps; ++i) { |
2044 | | /* TODO here with MCT ? */ |
2045 | 0 | opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), |
2046 | 0 | 1); /* Ssiz_i */ |
2047 | 0 | ++l_current_ptr; |
2048 | |
|
2049 | 0 | opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */ |
2050 | 0 | ++l_current_ptr; |
2051 | |
|
2052 | 0 | opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */ |
2053 | 0 | ++l_current_ptr; |
2054 | |
|
2055 | 0 | ++l_img_comp; |
2056 | 0 | } |
2057 | |
|
2058 | 0 | if (opj_stream_write_data(p_stream, |
2059 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len, |
2060 | 0 | p_manager) != l_size_len) { |
2061 | 0 | return OPJ_FALSE; |
2062 | 0 | } |
2063 | | |
2064 | 0 | return OPJ_TRUE; |
2065 | 0 | } |
2066 | | |
2067 | | /** |
2068 | | * Reads a SIZ marker (image and tile size) |
2069 | | * @param p_j2k the jpeg2000 file codec. |
2070 | | * @param p_header_data the data contained in the SIZ box. |
2071 | | * @param p_header_size the size of the data contained in the SIZ marker. |
2072 | | * @param p_manager the user event manager. |
2073 | | */ |
2074 | | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, |
2075 | | OPJ_BYTE * p_header_data, |
2076 | | OPJ_UINT32 p_header_size, |
2077 | | opj_event_mgr_t * p_manager |
2078 | | ) |
2079 | 16.3k | { |
2080 | 16.3k | OPJ_UINT32 i; |
2081 | 16.3k | OPJ_UINT32 l_nb_comp; |
2082 | 16.3k | OPJ_UINT32 l_nb_comp_remain; |
2083 | 16.3k | OPJ_UINT32 l_remaining_size; |
2084 | 16.3k | OPJ_UINT32 l_nb_tiles; |
2085 | 16.3k | OPJ_UINT32 l_tmp, l_tx1, l_ty1; |
2086 | 16.3k | OPJ_UINT32 l_prec0, l_sgnd0; |
2087 | 16.3k | opj_image_t *l_image = 00; |
2088 | 16.3k | opj_cp_t *l_cp = 00; |
2089 | 16.3k | opj_image_comp_t * l_img_comp = 00; |
2090 | 16.3k | opj_tcp_t * l_current_tile_param = 00; |
2091 | | |
2092 | | /* preconditions */ |
2093 | 16.3k | assert(p_j2k != 00); |
2094 | 16.3k | assert(p_manager != 00); |
2095 | 16.3k | assert(p_header_data != 00); |
2096 | | |
2097 | 16.3k | l_image = p_j2k->m_private_image; |
2098 | 16.3k | l_cp = &(p_j2k->m_cp); |
2099 | | |
2100 | | /* minimum size == 39 - 3 (= minimum component parameter) */ |
2101 | 16.3k | if (p_header_size < 36) { |
2102 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); |
2103 | 12 | return OPJ_FALSE; |
2104 | 12 | } |
2105 | | |
2106 | 16.3k | l_remaining_size = p_header_size - 36; |
2107 | 16.3k | l_nb_comp = l_remaining_size / 3; |
2108 | 16.3k | l_nb_comp_remain = l_remaining_size % 3; |
2109 | 16.3k | if (l_nb_comp_remain != 0) { |
2110 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); |
2111 | 2 | return OPJ_FALSE; |
2112 | 2 | } |
2113 | | |
2114 | 16.3k | opj_read_bytes(p_header_data, &l_tmp, |
2115 | 16.3k | 2); /* Rsiz (capabilities) */ |
2116 | 16.3k | p_header_data += 2; |
2117 | 16.3k | l_cp->rsiz = (OPJ_UINT16) l_tmp; |
2118 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ |
2119 | 16.3k | p_header_data += 4; |
2120 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ |
2121 | 16.3k | p_header_data += 4; |
2122 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ |
2123 | 16.3k | p_header_data += 4; |
2124 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ |
2125 | 16.3k | p_header_data += 4; |
2126 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, |
2127 | 16.3k | 4); /* XTsiz */ |
2128 | 16.3k | p_header_data += 4; |
2129 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, |
2130 | 16.3k | 4); /* YTsiz */ |
2131 | 16.3k | p_header_data += 4; |
2132 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, |
2133 | 16.3k | 4); /* XT0siz */ |
2134 | 16.3k | p_header_data += 4; |
2135 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, |
2136 | 16.3k | 4); /* YT0siz */ |
2137 | 16.3k | p_header_data += 4; |
2138 | 16.3k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, |
2139 | 16.3k | 2); /* Csiz */ |
2140 | 16.3k | p_header_data += 2; |
2141 | 16.3k | if (l_tmp < 16385) { |
2142 | 16.3k | l_image->numcomps = (OPJ_UINT16) l_tmp; |
2143 | 16.3k | } else { |
2144 | 11 | opj_event_msg(p_manager, EVT_ERROR, |
2145 | 11 | "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp); |
2146 | 11 | return OPJ_FALSE; |
2147 | 11 | } |
2148 | | |
2149 | 16.3k | if (l_image->numcomps != l_nb_comp) { |
2150 | 55 | opj_event_msg(p_manager, EVT_ERROR, |
2151 | 55 | "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", |
2152 | 55 | l_image->numcomps, l_nb_comp); |
2153 | 55 | return OPJ_FALSE; |
2154 | 55 | } |
2155 | | |
2156 | | /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ |
2157 | | /* testcase issue427-null-image-size.jp2 */ |
2158 | 16.2k | if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { |
2159 | 27 | opj_event_msg(p_manager, EVT_ERROR, |
2160 | 27 | "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64 |
2161 | 27 | ")\n", (OPJ_INT64)l_image->x1 - l_image->x0, |
2162 | 27 | (OPJ_INT64)l_image->y1 - l_image->y0); |
2163 | 27 | return OPJ_FALSE; |
2164 | 27 | } |
2165 | | /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ |
2166 | 16.2k | if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) { |
2167 | 7 | opj_event_msg(p_manager, EVT_ERROR, |
2168 | 7 | "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, |
2169 | 7 | l_cp->tdy); |
2170 | 7 | return OPJ_FALSE; |
2171 | 7 | } |
2172 | | |
2173 | | /* testcase issue427-illegal-tile-offset.jp2 */ |
2174 | 16.2k | l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */ |
2175 | 16.2k | l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */ |
2176 | 16.2k | if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || |
2177 | 16.2k | (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) { |
2178 | 140 | opj_event_msg(p_manager, EVT_ERROR, |
2179 | 140 | "Error with SIZ marker: illegal tile offset\n"); |
2180 | 140 | return OPJ_FALSE; |
2181 | 140 | } |
2182 | 16.0k | if (!p_j2k->dump_state) { |
2183 | 16.0k | OPJ_UINT32 siz_w, siz_h; |
2184 | | |
2185 | 16.0k | siz_w = l_image->x1 - l_image->x0; |
2186 | 16.0k | siz_h = l_image->y1 - l_image->y0; |
2187 | | |
2188 | 16.0k | if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0 |
2189 | 16.0k | && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) { |
2190 | 110 | opj_event_msg(p_manager, EVT_ERROR, |
2191 | 110 | "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w, |
2192 | 110 | p_j2k->ihdr_h, siz_w, siz_h); |
2193 | 110 | return OPJ_FALSE; |
2194 | 110 | } |
2195 | 16.0k | } |
2196 | | #ifdef USE_JPWL |
2197 | | if (l_cp->correct) { |
2198 | | /* if JPWL is on, we check whether TX errors have damaged |
2199 | | too much the SIZ parameters */ |
2200 | | if (!(l_image->x1 * l_image->y1)) { |
2201 | | opj_event_msg(p_manager, EVT_ERROR, |
2202 | | "JPWL: bad image size (%d x %d)\n", |
2203 | | l_image->x1, l_image->y1); |
2204 | | if (!JPWL_ASSUME) { |
2205 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
2206 | | return OPJ_FALSE; |
2207 | | } |
2208 | | } |
2209 | | |
2210 | | /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ? |
2211 | | if (l_image->numcomps != ((len - 38) / 3)) { |
2212 | | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2213 | | "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", |
2214 | | l_image->numcomps, ((len - 38) / 3)); |
2215 | | if (!JPWL_ASSUME) { |
2216 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
2217 | | return OPJ_FALSE; |
2218 | | } |
2219 | | */ /* we try to correct */ |
2220 | | /* opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"); |
2221 | | if (l_image->numcomps < ((len - 38) / 3)) { |
2222 | | len = 38 + 3 * l_image->numcomps; |
2223 | | opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", |
2224 | | len); |
2225 | | } else { |
2226 | | l_image->numcomps = ((len - 38) / 3); |
2227 | | opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", |
2228 | | l_image->numcomps); |
2229 | | } |
2230 | | } |
2231 | | */ |
2232 | | |
2233 | | /* update components number in the jpwl_exp_comps filed */ |
2234 | | l_cp->exp_comps = l_image->numcomps; |
2235 | | } |
2236 | | #endif /* USE_JPWL */ |
2237 | | |
2238 | | /* Allocate the resulting image components */ |
2239 | 15.9k | l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, |
2240 | 15.9k | sizeof(opj_image_comp_t)); |
2241 | 15.9k | if (l_image->comps == 00) { |
2242 | 34 | l_image->numcomps = 0; |
2243 | 34 | opj_event_msg(p_manager, EVT_ERROR, |
2244 | 34 | "Not enough memory to take in charge SIZ marker\n"); |
2245 | 34 | return OPJ_FALSE; |
2246 | 34 | } |
2247 | | |
2248 | 15.9k | l_img_comp = l_image->comps; |
2249 | | |
2250 | 15.9k | l_prec0 = 0; |
2251 | 15.9k | l_sgnd0 = 0; |
2252 | | /* Read the component information */ |
2253 | 73.9k | for (i = 0; i < l_image->numcomps; ++i) { |
2254 | 58.1k | OPJ_UINT32 tmp; |
2255 | 58.1k | opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */ |
2256 | 58.1k | ++p_header_data; |
2257 | 58.1k | l_img_comp->prec = (tmp & 0x7f) + 1; |
2258 | 58.1k | l_img_comp->sgnd = tmp >> 7; |
2259 | | |
2260 | 58.1k | if (p_j2k->dump_state == 0) { |
2261 | 58.1k | if (i == 0) { |
2262 | 15.9k | l_prec0 = l_img_comp->prec; |
2263 | 15.9k | l_sgnd0 = l_img_comp->sgnd; |
2264 | 42.1k | } else if (!l_cp->allow_different_bit_depth_sign |
2265 | 42.1k | && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) { |
2266 | 18.6k | opj_event_msg(p_manager, EVT_WARNING, |
2267 | 18.6k | "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n" |
2268 | 18.6k | " [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0, |
2269 | 18.6k | i, l_img_comp->prec, l_img_comp->sgnd); |
2270 | 18.6k | } |
2271 | | /* TODO: we should perhaps also check against JP2 BPCC values */ |
2272 | 58.1k | } |
2273 | 58.1k | opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */ |
2274 | 58.1k | ++p_header_data; |
2275 | 58.1k | l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2276 | 58.1k | opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */ |
2277 | 58.1k | ++p_header_data; |
2278 | 58.1k | l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2279 | 58.1k | if (l_img_comp->dx < 1 || l_img_comp->dx > 255 || |
2280 | 58.1k | l_img_comp->dy < 1 || l_img_comp->dy > 255) { |
2281 | 23 | opj_event_msg(p_manager, EVT_ERROR, |
2282 | 23 | "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n", |
2283 | 23 | i, l_img_comp->dx, l_img_comp->dy); |
2284 | 23 | return OPJ_FALSE; |
2285 | 23 | } |
2286 | | /* Avoids later undefined shift in computation of */ |
2287 | | /* p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 |
2288 | | << (l_image->comps[i].prec - 1); */ |
2289 | 58.0k | if (l_img_comp->prec > 31) { |
2290 | 14 | opj_event_msg(p_manager, EVT_ERROR, |
2291 | 14 | "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n", |
2292 | 14 | i, l_img_comp->prec); |
2293 | 14 | return OPJ_FALSE; |
2294 | 14 | } |
2295 | | #ifdef USE_JPWL |
2296 | | if (l_cp->correct) { |
2297 | | /* if JPWL is on, we check whether TX errors have damaged |
2298 | | too much the SIZ parameters, again */ |
2299 | | if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { |
2300 | | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2301 | | "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n", |
2302 | | i, i, l_image->comps[i].dx, l_image->comps[i].dy); |
2303 | | if (!JPWL_ASSUME) { |
2304 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
2305 | | return OPJ_FALSE; |
2306 | | } |
2307 | | /* we try to correct */ |
2308 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); |
2309 | | if (!l_image->comps[i].dx) { |
2310 | | l_image->comps[i].dx = 1; |
2311 | | opj_event_msg(p_manager, EVT_WARNING, |
2312 | | "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", |
2313 | | i, l_image->comps[i].dx); |
2314 | | } |
2315 | | if (!l_image->comps[i].dy) { |
2316 | | l_image->comps[i].dy = 1; |
2317 | | opj_event_msg(p_manager, EVT_WARNING, |
2318 | | "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", |
2319 | | i, l_image->comps[i].dy); |
2320 | | } |
2321 | | } |
2322 | | } |
2323 | | #endif /* USE_JPWL */ |
2324 | 58.0k | l_img_comp->resno_decoded = |
2325 | 58.0k | 0; /* number of resolution decoded */ |
2326 | 58.0k | l_img_comp->factor = |
2327 | 58.0k | l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ |
2328 | 58.0k | ++l_img_comp; |
2329 | 58.0k | } |
2330 | | |
2331 | 15.8k | if (l_cp->tdx == 0 || l_cp->tdy == 0) { |
2332 | 0 | return OPJ_FALSE; |
2333 | 0 | } |
2334 | | |
2335 | | /* Compute the number of tiles */ |
2336 | 15.8k | l_cp->tw = opj_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx); |
2337 | 15.8k | l_cp->th = opj_uint_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy); |
2338 | | |
2339 | | /* Check that the number of tiles is valid */ |
2340 | 15.8k | if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { |
2341 | 22 | opj_event_msg(p_manager, EVT_ERROR, |
2342 | 22 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", |
2343 | 22 | l_cp->tw, l_cp->th); |
2344 | 22 | return OPJ_FALSE; |
2345 | 22 | } |
2346 | 15.8k | l_nb_tiles = l_cp->tw * l_cp->th; |
2347 | | |
2348 | | /* Define the tiles which will be decoded */ |
2349 | 15.8k | if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) { |
2350 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = |
2351 | 0 | (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx; |
2352 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = |
2353 | 0 | (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy; |
2354 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv( |
2355 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0, |
2356 | 0 | l_cp->tdx); |
2357 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv( |
2358 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0, |
2359 | 0 | l_cp->tdy); |
2360 | 15.8k | } else { |
2361 | 15.8k | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
2362 | 15.8k | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
2363 | 15.8k | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
2364 | 15.8k | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
2365 | 15.8k | } |
2366 | | |
2367 | | #ifdef USE_JPWL |
2368 | | if (l_cp->correct) { |
2369 | | /* if JPWL is on, we check whether TX errors have damaged |
2370 | | too much the SIZ parameters */ |
2371 | | if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || |
2372 | | (l_cp->th > l_cp->max_tiles)) { |
2373 | | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2374 | | "JPWL: bad number of tiles (%d x %d)\n", |
2375 | | l_cp->tw, l_cp->th); |
2376 | | if (!JPWL_ASSUME) { |
2377 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
2378 | | return OPJ_FALSE; |
2379 | | } |
2380 | | /* we try to correct */ |
2381 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); |
2382 | | if (l_cp->tw < 1) { |
2383 | | l_cp->tw = 1; |
2384 | | opj_event_msg(p_manager, EVT_WARNING, |
2385 | | "- setting %d tiles in x => HYPOTHESIS!!!\n", |
2386 | | l_cp->tw); |
2387 | | } |
2388 | | if (l_cp->tw > l_cp->max_tiles) { |
2389 | | l_cp->tw = 1; |
2390 | | opj_event_msg(p_manager, EVT_WARNING, |
2391 | | "- too large x, increase expectance of %d\n" |
2392 | | "- setting %d tiles in x => HYPOTHESIS!!!\n", |
2393 | | l_cp->max_tiles, l_cp->tw); |
2394 | | } |
2395 | | if (l_cp->th < 1) { |
2396 | | l_cp->th = 1; |
2397 | | opj_event_msg(p_manager, EVT_WARNING, |
2398 | | "- setting %d tiles in y => HYPOTHESIS!!!\n", |
2399 | | l_cp->th); |
2400 | | } |
2401 | | if (l_cp->th > l_cp->max_tiles) { |
2402 | | l_cp->th = 1; |
2403 | | opj_event_msg(p_manager, EVT_WARNING, |
2404 | | "- too large y, increase expectance of %d to continue\n", |
2405 | | "- setting %d tiles in y => HYPOTHESIS!!!\n", |
2406 | | l_cp->max_tiles, l_cp->th); |
2407 | | } |
2408 | | } |
2409 | | } |
2410 | | #endif /* USE_JPWL */ |
2411 | | |
2412 | | /* memory allocations */ |
2413 | 15.8k | l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); |
2414 | 15.8k | if (l_cp->tcps == 00) { |
2415 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2416 | 0 | "Not enough memory to take in charge SIZ marker\n"); |
2417 | 0 | return OPJ_FALSE; |
2418 | 0 | } |
2419 | | |
2420 | | #ifdef USE_JPWL |
2421 | | if (l_cp->correct) { |
2422 | | if (!l_cp->tcps) { |
2423 | | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2424 | | "JPWL: could not alloc tcps field of cp\n"); |
2425 | | if (!JPWL_ASSUME) { |
2426 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
2427 | | return OPJ_FALSE; |
2428 | | } |
2429 | | } |
2430 | | } |
2431 | | #endif /* USE_JPWL */ |
2432 | | |
2433 | 15.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = |
2434 | 15.8k | (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); |
2435 | 15.8k | if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) { |
2436 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2437 | 0 | "Not enough memory to take in charge SIZ marker\n"); |
2438 | 0 | return OPJ_FALSE; |
2439 | 0 | } |
2440 | | |
2441 | 15.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = |
2442 | 15.8k | (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS, |
2443 | 15.8k | sizeof(opj_mct_data_t)); |
2444 | | |
2445 | 15.8k | if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { |
2446 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2447 | 0 | "Not enough memory to take in charge SIZ marker\n"); |
2448 | 0 | return OPJ_FALSE; |
2449 | 0 | } |
2450 | 15.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = |
2451 | 15.8k | OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
2452 | | |
2453 | 15.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = |
2454 | 15.8k | (opj_simple_mcc_decorrelation_data_t*) |
2455 | 15.8k | opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, |
2456 | 15.8k | sizeof(opj_simple_mcc_decorrelation_data_t)); |
2457 | | |
2458 | 15.8k | if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { |
2459 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2460 | 0 | "Not enough memory to take in charge SIZ marker\n"); |
2461 | 0 | return OPJ_FALSE; |
2462 | 0 | } |
2463 | 15.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = |
2464 | 15.8k | OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
2465 | | |
2466 | | /* set up default dc level shift */ |
2467 | 73.6k | for (i = 0; i < l_image->numcomps; ++i) { |
2468 | 57.7k | if (! l_image->comps[i].sgnd) { |
2469 | 41.0k | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 |
2470 | 41.0k | << (l_image->comps[i].prec - 1); |
2471 | 41.0k | } |
2472 | 57.7k | } |
2473 | | |
2474 | 15.8k | l_current_tile_param = l_cp->tcps; |
2475 | 20.3M | for (i = 0; i < l_nb_tiles; ++i) { |
2476 | 20.3M | l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, |
2477 | 20.3M | sizeof(opj_tccp_t)); |
2478 | 20.3M | if (l_current_tile_param->tccps == 00) { |
2479 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2480 | 0 | "Not enough memory to take in charge SIZ marker\n"); |
2481 | 0 | return OPJ_FALSE; |
2482 | 0 | } |
2483 | | |
2484 | 20.3M | ++l_current_tile_param; |
2485 | 20.3M | } |
2486 | | |
2487 | 15.8k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; |
2488 | 15.8k | opj_image_comp_header_update(l_image, l_cp); |
2489 | | |
2490 | 15.8k | return OPJ_TRUE; |
2491 | 15.8k | } |
2492 | | |
2493 | | static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, |
2494 | | opj_stream_private_t *p_stream, |
2495 | | opj_event_mgr_t * p_manager |
2496 | | ) |
2497 | 0 | { |
2498 | 0 | OPJ_UINT32 l_comment_size; |
2499 | 0 | OPJ_UINT32 l_total_com_size; |
2500 | 0 | const OPJ_CHAR *l_comment; |
2501 | 0 | OPJ_BYTE * l_current_ptr = 00; |
2502 | | |
2503 | | /* preconditions */ |
2504 | 0 | assert(p_j2k != 00); |
2505 | 0 | assert(p_stream != 00); |
2506 | 0 | assert(p_manager != 00); |
2507 | |
|
2508 | 0 | l_comment = p_j2k->m_cp.comment; |
2509 | 0 | l_comment_size = (OPJ_UINT32)strlen(l_comment); |
2510 | 0 | l_total_com_size = l_comment_size + 6; |
2511 | |
|
2512 | 0 | if (l_total_com_size > |
2513 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2514 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
2515 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size); |
2516 | 0 | if (! new_header_tile_data) { |
2517 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2518 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2519 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2520 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2521 | 0 | "Not enough memory to write the COM marker\n"); |
2522 | 0 | return OPJ_FALSE; |
2523 | 0 | } |
2524 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2525 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; |
2526 | 0 | } |
2527 | | |
2528 | 0 | l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2529 | |
|
2530 | 0 | opj_write_bytes(l_current_ptr, J2K_MS_COM, 2); /* COM */ |
2531 | 0 | l_current_ptr += 2; |
2532 | |
|
2533 | 0 | opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2); /* L_COM */ |
2534 | 0 | l_current_ptr += 2; |
2535 | |
|
2536 | 0 | opj_write_bytes(l_current_ptr, 1, |
2537 | 0 | 2); /* General use (IS 8859-15:1999 (Latin) values) */ |
2538 | 0 | l_current_ptr += 2; |
2539 | |
|
2540 | 0 | memcpy(l_current_ptr, l_comment, l_comment_size); |
2541 | |
|
2542 | 0 | if (opj_stream_write_data(p_stream, |
2543 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size, |
2544 | 0 | p_manager) != l_total_com_size) { |
2545 | 0 | return OPJ_FALSE; |
2546 | 0 | } |
2547 | | |
2548 | 0 | return OPJ_TRUE; |
2549 | 0 | } |
2550 | | |
2551 | | /** |
2552 | | * Reads a COM marker (comments) |
2553 | | * @param p_j2k the jpeg2000 file codec. |
2554 | | * @param p_header_data the data contained in the COM box. |
2555 | | * @param p_header_size the size of the data contained in the COM marker. |
2556 | | * @param p_manager the user event manager. |
2557 | | */ |
2558 | | static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, |
2559 | | OPJ_BYTE * p_header_data, |
2560 | | OPJ_UINT32 p_header_size, |
2561 | | opj_event_mgr_t * p_manager |
2562 | | ) |
2563 | 3.01k | { |
2564 | | /* preconditions */ |
2565 | 3.01k | assert(p_j2k != 00); |
2566 | 3.01k | assert(p_manager != 00); |
2567 | 3.01k | assert(p_header_data != 00); |
2568 | | |
2569 | 3.01k | OPJ_UNUSED(p_j2k); |
2570 | 3.01k | OPJ_UNUSED(p_header_data); |
2571 | 3.01k | OPJ_UNUSED(p_header_size); |
2572 | 3.01k | OPJ_UNUSED(p_manager); |
2573 | | |
2574 | 3.01k | return OPJ_TRUE; |
2575 | 3.01k | } |
2576 | | |
2577 | | static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, |
2578 | | opj_stream_private_t *p_stream, |
2579 | | opj_event_mgr_t * p_manager) |
2580 | 0 | { |
2581 | 0 | opj_cp_t *l_cp = 00; |
2582 | 0 | opj_tcp_t *l_tcp = 00; |
2583 | 0 | OPJ_UINT32 l_code_size, l_remaining_size; |
2584 | 0 | OPJ_BYTE * l_current_data = 00; |
2585 | | |
2586 | | /* preconditions */ |
2587 | 0 | assert(p_j2k != 00); |
2588 | 0 | assert(p_manager != 00); |
2589 | 0 | assert(p_stream != 00); |
2590 | |
|
2591 | 0 | l_cp = &(p_j2k->m_cp); |
2592 | 0 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2593 | 0 | l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2594 | 0 | p_j2k->m_current_tile_number, 0); |
2595 | 0 | l_remaining_size = l_code_size; |
2596 | |
|
2597 | 0 | if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2598 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
2599 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size); |
2600 | 0 | if (! new_header_tile_data) { |
2601 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2602 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2603 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2604 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n"); |
2605 | 0 | return OPJ_FALSE; |
2606 | 0 | } |
2607 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2608 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; |
2609 | 0 | } |
2610 | | |
2611 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2612 | |
|
2613 | 0 | opj_write_bytes(l_current_data, J2K_MS_COD, 2); /* COD */ |
2614 | 0 | l_current_data += 2; |
2615 | |
|
2616 | 0 | opj_write_bytes(l_current_data, l_code_size - 2, 2); /* L_COD */ |
2617 | 0 | l_current_data += 2; |
2618 | |
|
2619 | 0 | opj_write_bytes(l_current_data, l_tcp->csty, 1); /* Scod */ |
2620 | 0 | ++l_current_data; |
2621 | |
|
2622 | 0 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */ |
2623 | 0 | ++l_current_data; |
2624 | |
|
2625 | 0 | opj_write_bytes(l_current_data, l_tcp->numlayers, 2); /* SGcod (B) */ |
2626 | 0 | l_current_data += 2; |
2627 | |
|
2628 | 0 | opj_write_bytes(l_current_data, l_tcp->mct, 1); /* SGcod (C) */ |
2629 | 0 | ++l_current_data; |
2630 | |
|
2631 | 0 | l_remaining_size -= 9; |
2632 | |
|
2633 | 0 | if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, |
2634 | 0 | l_current_data, &l_remaining_size, p_manager)) { |
2635 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); |
2636 | 0 | return OPJ_FALSE; |
2637 | 0 | } |
2638 | | |
2639 | 0 | if (l_remaining_size != 0) { |
2640 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); |
2641 | 0 | return OPJ_FALSE; |
2642 | 0 | } |
2643 | | |
2644 | 0 | if (opj_stream_write_data(p_stream, |
2645 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size, |
2646 | 0 | p_manager) != l_code_size) { |
2647 | 0 | return OPJ_FALSE; |
2648 | 0 | } |
2649 | | |
2650 | 0 | return OPJ_TRUE; |
2651 | 0 | } |
2652 | | |
2653 | | /** |
2654 | | * Reads a COD marker (Coding style defaults) |
2655 | | * @param p_header_data the data contained in the COD box. |
2656 | | * @param p_j2k the jpeg2000 codec. |
2657 | | * @param p_header_size the size of the data contained in the COD marker. |
2658 | | * @param p_manager the user event manager. |
2659 | | */ |
2660 | | static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, |
2661 | | OPJ_BYTE * p_header_data, |
2662 | | OPJ_UINT32 p_header_size, |
2663 | | opj_event_mgr_t * p_manager |
2664 | | ) |
2665 | 19.6k | { |
2666 | | /* loop */ |
2667 | 19.6k | OPJ_UINT32 i; |
2668 | 19.6k | OPJ_UINT32 l_tmp; |
2669 | 19.6k | opj_cp_t *l_cp = 00; |
2670 | 19.6k | opj_tcp_t *l_tcp = 00; |
2671 | 19.6k | opj_image_t *l_image = 00; |
2672 | | |
2673 | | /* preconditions */ |
2674 | 19.6k | assert(p_header_data != 00); |
2675 | 19.6k | assert(p_j2k != 00); |
2676 | 19.6k | assert(p_manager != 00); |
2677 | | |
2678 | 19.6k | l_image = p_j2k->m_private_image; |
2679 | 19.6k | l_cp = &(p_j2k->m_cp); |
2680 | | |
2681 | | /* If we are in the first tile-part header of the current tile */ |
2682 | 19.6k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
2683 | 512 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2684 | 19.6k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
2685 | | |
2686 | | #if 0 |
2687 | | /* This check was added per https://github.com/uclouvain/openjpeg/commit/daed8cc9195555e101ab708a501af2dfe6d5e001 */ |
2688 | | /* but this is no longer necessary to handle issue476.jp2 */ |
2689 | | /* and this actually cause issues on legit files. See https://github.com/uclouvain/openjpeg/issues/1043 */ |
2690 | | /* Only one COD per tile */ |
2691 | | if (l_tcp->cod) { |
2692 | | opj_event_msg(p_manager, EVT_ERROR, |
2693 | | "COD marker already read. No more than one COD marker per tile.\n"); |
2694 | | return OPJ_FALSE; |
2695 | | } |
2696 | | #endif |
2697 | 19.6k | l_tcp->cod = 1; |
2698 | | |
2699 | | /* Make sure room is sufficient */ |
2700 | 19.6k | if (p_header_size < 5) { |
2701 | 8 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2702 | 8 | return OPJ_FALSE; |
2703 | 8 | } |
2704 | | |
2705 | 19.6k | opj_read_bytes(p_header_data, &l_tcp->csty, 1); /* Scod */ |
2706 | 19.6k | ++p_header_data; |
2707 | | /* Make sure we know how to decode this */ |
2708 | 19.6k | if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | |
2709 | 19.6k | J2K_CP_CSTY_EPH)) != 0U) { |
2710 | 7 | opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n"); |
2711 | 7 | return OPJ_FALSE; |
2712 | 7 | } |
2713 | 19.6k | opj_read_bytes(p_header_data, &l_tmp, 1); /* SGcod (A) */ |
2714 | 19.6k | ++p_header_data; |
2715 | 19.6k | l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; |
2716 | | /* Make sure progression order is valid */ |
2717 | 19.6k | if (l_tcp->prg > OPJ_CPRL) { |
2718 | 1.14k | opj_event_msg(p_manager, EVT_ERROR, |
2719 | 1.14k | "Unknown progression order in COD marker\n"); |
2720 | 1.14k | l_tcp->prg = OPJ_PROG_UNKNOWN; |
2721 | 1.14k | } |
2722 | 19.6k | opj_read_bytes(p_header_data, &l_tcp->numlayers, 2); /* SGcod (B) */ |
2723 | 19.6k | p_header_data += 2; |
2724 | | |
2725 | 19.6k | if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) { |
2726 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
2727 | 3 | "Invalid number of layers in COD marker : %d not in range [1-65535]\n", |
2728 | 3 | l_tcp->numlayers); |
2729 | 3 | return OPJ_FALSE; |
2730 | 3 | } |
2731 | | |
2732 | | /* If user didn't set a number layer to decode take the max specify in the codestream. */ |
2733 | 19.6k | if (l_cp->m_specific_param.m_dec.m_layer) { |
2734 | 0 | l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; |
2735 | 19.6k | } else { |
2736 | 19.6k | l_tcp->num_layers_to_decode = l_tcp->numlayers; |
2737 | 19.6k | } |
2738 | | |
2739 | 19.6k | opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */ |
2740 | 19.6k | ++p_header_data; |
2741 | | |
2742 | 19.6k | if (l_tcp->mct > 1) { |
2743 | 19 | opj_event_msg(p_manager, EVT_ERROR, |
2744 | 19 | "Invalid multiple component transformation\n"); |
2745 | 19 | return OPJ_FALSE; |
2746 | 19 | } |
2747 | | |
2748 | 19.5k | p_header_size -= 5; |
2749 | 101k | for (i = 0; i < l_image->numcomps; ++i) { |
2750 | 82.0k | l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; |
2751 | 82.0k | } |
2752 | | |
2753 | 19.5k | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size, |
2754 | 19.5k | p_manager)) { |
2755 | 56 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2756 | 56 | return OPJ_FALSE; |
2757 | 56 | } |
2758 | | |
2759 | 19.5k | if (p_header_size != 0) { |
2760 | 17 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2761 | 17 | return OPJ_FALSE; |
2762 | 17 | } |
2763 | | |
2764 | | /* Apply the coding style to other components of the current tile or the m_default_tcp*/ |
2765 | 19.5k | opj_j2k_copy_tile_component_parameters(p_j2k); |
2766 | | |
2767 | | /* Index */ |
2768 | | #ifdef WIP_REMOVE_MSD |
2769 | | if (p_j2k->cstr_info) { |
2770 | | /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/ |
2771 | | p_j2k->cstr_info->prog = l_tcp->prg; |
2772 | | p_j2k->cstr_info->numlayers = l_tcp->numlayers; |
2773 | | p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc( |
2774 | | l_image->numcomps * sizeof(OPJ_UINT32)); |
2775 | | if (!p_j2k->cstr_info->numdecompos) { |
2776 | | return OPJ_FALSE; |
2777 | | } |
2778 | | for (i = 0; i < l_image->numcomps; ++i) { |
2779 | | p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; |
2780 | | } |
2781 | | } |
2782 | | #endif |
2783 | | |
2784 | 19.5k | return OPJ_TRUE; |
2785 | 19.5k | } |
2786 | | |
2787 | | static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, |
2788 | | OPJ_UINT32 p_comp_no, |
2789 | | opj_stream_private_t *p_stream, |
2790 | | opj_event_mgr_t * p_manager) |
2791 | 0 | { |
2792 | 0 | OPJ_UINT32 l_coc_size, l_remaining_size; |
2793 | 0 | OPJ_UINT32 l_comp_room; |
2794 | | |
2795 | | /* preconditions */ |
2796 | 0 | assert(p_j2k != 00); |
2797 | 0 | assert(p_manager != 00); |
2798 | 0 | assert(p_stream != 00); |
2799 | |
|
2800 | 0 | l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2; |
2801 | |
|
2802 | 0 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2803 | 0 | p_j2k->m_current_tile_number, p_comp_no); |
2804 | |
|
2805 | 0 | if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2806 | 0 | OPJ_BYTE *new_header_tile_data; |
2807 | | /*p_j2k->m_specific_param.m_encoder.m_header_tile_data |
2808 | | = (OPJ_BYTE*)opj_realloc( |
2809 | | p_j2k->m_specific_param.m_encoder.m_header_tile_data, |
2810 | | l_coc_size);*/ |
2811 | |
|
2812 | 0 | new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
2813 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size); |
2814 | 0 | if (! new_header_tile_data) { |
2815 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2816 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2817 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2818 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n"); |
2819 | 0 | return OPJ_FALSE; |
2820 | 0 | } |
2821 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2822 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; |
2823 | 0 | } |
2824 | | |
2825 | 0 | opj_j2k_write_coc_in_memory(p_j2k, p_comp_no, |
2826 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, |
2827 | 0 | p_manager); |
2828 | |
|
2829 | 0 | if (opj_stream_write_data(p_stream, |
2830 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size, |
2831 | 0 | p_manager) != l_coc_size) { |
2832 | 0 | return OPJ_FALSE; |
2833 | 0 | } |
2834 | | |
2835 | 0 | return OPJ_TRUE; |
2836 | 0 | } |
2837 | | |
2838 | | static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, |
2839 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
2840 | 0 | { |
2841 | 0 | opj_cp_t *l_cp = NULL; |
2842 | 0 | opj_tcp_t *l_tcp = NULL; |
2843 | | |
2844 | | /* preconditions */ |
2845 | 0 | assert(p_j2k != 00); |
2846 | |
|
2847 | 0 | l_cp = &(p_j2k->m_cp); |
2848 | 0 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2849 | |
|
2850 | 0 | if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) { |
2851 | 0 | return OPJ_FALSE; |
2852 | 0 | } |
2853 | | |
2854 | | |
2855 | 0 | return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, |
2856 | 0 | p_first_comp_no, p_second_comp_no); |
2857 | 0 | } |
2858 | | |
2859 | | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, |
2860 | | OPJ_UINT32 p_comp_no, |
2861 | | OPJ_BYTE * p_data, |
2862 | | OPJ_UINT32 * p_data_written, |
2863 | | opj_event_mgr_t * p_manager |
2864 | | ) |
2865 | 0 | { |
2866 | 0 | opj_cp_t *l_cp = 00; |
2867 | 0 | opj_tcp_t *l_tcp = 00; |
2868 | 0 | OPJ_UINT32 l_coc_size, l_remaining_size; |
2869 | 0 | OPJ_BYTE * l_current_data = 00; |
2870 | 0 | opj_image_t *l_image = 00; |
2871 | 0 | OPJ_UINT32 l_comp_room; |
2872 | | |
2873 | | /* preconditions */ |
2874 | 0 | assert(p_j2k != 00); |
2875 | 0 | assert(p_manager != 00); |
2876 | |
|
2877 | 0 | l_cp = &(p_j2k->m_cp); |
2878 | 0 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2879 | 0 | l_image = p_j2k->m_private_image; |
2880 | 0 | l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; |
2881 | |
|
2882 | 0 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2883 | 0 | p_j2k->m_current_tile_number, p_comp_no); |
2884 | 0 | l_remaining_size = l_coc_size; |
2885 | |
|
2886 | 0 | l_current_data = p_data; |
2887 | |
|
2888 | 0 | opj_write_bytes(l_current_data, J2K_MS_COC, |
2889 | 0 | 2); /* COC */ |
2890 | 0 | l_current_data += 2; |
2891 | |
|
2892 | 0 | opj_write_bytes(l_current_data, l_coc_size - 2, |
2893 | 0 | 2); /* L_COC */ |
2894 | 0 | l_current_data += 2; |
2895 | |
|
2896 | 0 | opj_write_bytes(l_current_data, p_comp_no, l_comp_room); /* Ccoc */ |
2897 | 0 | l_current_data += l_comp_room; |
2898 | |
|
2899 | 0 | opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, |
2900 | 0 | 1); /* Scoc */ |
2901 | 0 | ++l_current_data; |
2902 | |
|
2903 | 0 | l_remaining_size -= (5 + l_comp_room); |
2904 | 0 | opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, |
2905 | 0 | l_current_data, &l_remaining_size, p_manager); |
2906 | 0 | * p_data_written = l_coc_size; |
2907 | 0 | } |
2908 | | |
2909 | | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k) |
2910 | 0 | { |
2911 | 0 | OPJ_UINT32 i, j; |
2912 | 0 | OPJ_UINT32 l_nb_comp; |
2913 | 0 | OPJ_UINT32 l_nb_tiles; |
2914 | 0 | OPJ_UINT32 l_max = 0; |
2915 | | |
2916 | | /* preconditions */ |
2917 | |
|
2918 | 0 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
2919 | 0 | l_nb_comp = p_j2k->m_private_image->numcomps; |
2920 | |
|
2921 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
2922 | 0 | for (j = 0; j < l_nb_comp; ++j) { |
2923 | 0 | l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j)); |
2924 | 0 | } |
2925 | 0 | } |
2926 | |
|
2927 | 0 | return 6 + l_max; |
2928 | 0 | } |
2929 | | |
2930 | | /** |
2931 | | * Reads a COC marker (Coding Style Component) |
2932 | | * @param p_header_data the data contained in the COC box. |
2933 | | * @param p_j2k the jpeg2000 codec. |
2934 | | * @param p_header_size the size of the data contained in the COC marker. |
2935 | | * @param p_manager the user event manager. |
2936 | | */ |
2937 | | static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, |
2938 | | OPJ_BYTE * p_header_data, |
2939 | | OPJ_UINT32 p_header_size, |
2940 | | opj_event_mgr_t * p_manager |
2941 | | ) |
2942 | 797 | { |
2943 | 797 | opj_cp_t *l_cp = NULL; |
2944 | 797 | opj_tcp_t *l_tcp = NULL; |
2945 | 797 | opj_image_t *l_image = NULL; |
2946 | 797 | OPJ_UINT32 l_comp_room; |
2947 | 797 | OPJ_UINT32 l_comp_no; |
2948 | | |
2949 | | /* preconditions */ |
2950 | 797 | assert(p_header_data != 00); |
2951 | 797 | assert(p_j2k != 00); |
2952 | 797 | assert(p_manager != 00); |
2953 | | |
2954 | 797 | l_cp = &(p_j2k->m_cp); |
2955 | 797 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
2956 | 797 | ? |
2957 | 250 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2958 | 797 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
2959 | 797 | l_image = p_j2k->m_private_image; |
2960 | | |
2961 | 797 | l_comp_room = l_image->numcomps <= 256 ? 1 : 2; |
2962 | | |
2963 | | /* make sure room is sufficient*/ |
2964 | 797 | if (p_header_size < l_comp_room + 1) { |
2965 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2966 | 4 | return OPJ_FALSE; |
2967 | 4 | } |
2968 | 793 | p_header_size -= l_comp_room + 1; |
2969 | | |
2970 | 793 | opj_read_bytes(p_header_data, &l_comp_no, |
2971 | 793 | l_comp_room); /* Ccoc */ |
2972 | 793 | p_header_data += l_comp_room; |
2973 | 793 | if (l_comp_no >= l_image->numcomps) { |
2974 | 12 | opj_event_msg(p_manager, EVT_ERROR, |
2975 | 12 | "Error reading COC marker (bad number of components)\n"); |
2976 | 12 | return OPJ_FALSE; |
2977 | 12 | } |
2978 | | |
2979 | 781 | opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty, |
2980 | 781 | 1); /* Scoc */ |
2981 | 781 | ++p_header_data ; |
2982 | | |
2983 | 781 | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
2984 | 781 | p_manager)) { |
2985 | 21 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2986 | 21 | return OPJ_FALSE; |
2987 | 21 | } |
2988 | | |
2989 | 760 | if (p_header_size != 0) { |
2990 | 9 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2991 | 9 | return OPJ_FALSE; |
2992 | 9 | } |
2993 | 751 | return OPJ_TRUE; |
2994 | 760 | } |
2995 | | |
2996 | | static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, |
2997 | | opj_stream_private_t *p_stream, |
2998 | | opj_event_mgr_t * p_manager |
2999 | | ) |
3000 | 0 | { |
3001 | 0 | OPJ_UINT32 l_qcd_size, l_remaining_size; |
3002 | 0 | OPJ_BYTE * l_current_data = 00; |
3003 | | |
3004 | | /* preconditions */ |
3005 | 0 | assert(p_j2k != 00); |
3006 | 0 | assert(p_manager != 00); |
3007 | 0 | assert(p_stream != 00); |
3008 | |
|
3009 | 0 | l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3010 | 0 | 0); |
3011 | 0 | l_remaining_size = l_qcd_size; |
3012 | |
|
3013 | 0 | if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3014 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
3015 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size); |
3016 | 0 | if (! new_header_tile_data) { |
3017 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3018 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3019 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3020 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n"); |
3021 | 0 | return OPJ_FALSE; |
3022 | 0 | } |
3023 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3024 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; |
3025 | 0 | } |
3026 | | |
3027 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
3028 | |
|
3029 | 0 | opj_write_bytes(l_current_data, J2K_MS_QCD, 2); /* QCD */ |
3030 | 0 | l_current_data += 2; |
3031 | |
|
3032 | 0 | opj_write_bytes(l_current_data, l_qcd_size - 2, 2); /* L_QCD */ |
3033 | 0 | l_current_data += 2; |
3034 | |
|
3035 | 0 | l_remaining_size -= 4; |
3036 | |
|
3037 | 0 | if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0, |
3038 | 0 | l_current_data, &l_remaining_size, p_manager)) { |
3039 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); |
3040 | 0 | return OPJ_FALSE; |
3041 | 0 | } |
3042 | | |
3043 | 0 | if (l_remaining_size != 0) { |
3044 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); |
3045 | 0 | return OPJ_FALSE; |
3046 | 0 | } |
3047 | | |
3048 | 0 | if (opj_stream_write_data(p_stream, |
3049 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size, |
3050 | 0 | p_manager) != l_qcd_size) { |
3051 | 0 | return OPJ_FALSE; |
3052 | 0 | } |
3053 | | |
3054 | 0 | return OPJ_TRUE; |
3055 | 0 | } |
3056 | | |
3057 | | /** |
3058 | | * Reads a QCD marker (Quantization defaults) |
3059 | | * @param p_header_data the data contained in the QCD box. |
3060 | | * @param p_j2k the jpeg2000 codec. |
3061 | | * @param p_header_size the size of the data contained in the QCD marker. |
3062 | | * @param p_manager the user event manager. |
3063 | | */ |
3064 | | static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, |
3065 | | OPJ_BYTE * p_header_data, |
3066 | | OPJ_UINT32 p_header_size, |
3067 | | opj_event_mgr_t * p_manager |
3068 | | ) |
3069 | 22.9k | { |
3070 | | /* preconditions */ |
3071 | 22.9k | assert(p_header_data != 00); |
3072 | 22.9k | assert(p_j2k != 00); |
3073 | 22.9k | assert(p_manager != 00); |
3074 | | |
3075 | 22.9k | if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size, |
3076 | 22.9k | p_manager)) { |
3077 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
3078 | 4 | return OPJ_FALSE; |
3079 | 4 | } |
3080 | | |
3081 | 22.9k | if (p_header_size != 0) { |
3082 | 18 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
3083 | 18 | return OPJ_FALSE; |
3084 | 18 | } |
3085 | | |
3086 | | /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ |
3087 | 22.8k | opj_j2k_copy_tile_quantization_parameters(p_j2k); |
3088 | | |
3089 | 22.8k | return OPJ_TRUE; |
3090 | 22.9k | } |
3091 | | |
3092 | | static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, |
3093 | | OPJ_UINT32 p_comp_no, |
3094 | | opj_stream_private_t *p_stream, |
3095 | | opj_event_mgr_t * p_manager |
3096 | | ) |
3097 | 0 | { |
3098 | 0 | OPJ_UINT32 l_qcc_size, l_remaining_size; |
3099 | | |
3100 | | /* preconditions */ |
3101 | 0 | assert(p_j2k != 00); |
3102 | 0 | assert(p_manager != 00); |
3103 | 0 | assert(p_stream != 00); |
3104 | |
|
3105 | 0 | l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3106 | 0 | p_comp_no); |
3107 | 0 | l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1; |
3108 | 0 | l_remaining_size = l_qcc_size; |
3109 | |
|
3110 | 0 | if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3111 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
3112 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size); |
3113 | 0 | if (! new_header_tile_data) { |
3114 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3115 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3116 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3117 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n"); |
3118 | 0 | return OPJ_FALSE; |
3119 | 0 | } |
3120 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3121 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; |
3122 | 0 | } |
3123 | | |
3124 | 0 | opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no, |
3125 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, |
3126 | 0 | p_manager); |
3127 | |
|
3128 | 0 | if (opj_stream_write_data(p_stream, |
3129 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size, |
3130 | 0 | p_manager) != l_qcc_size) { |
3131 | 0 | return OPJ_FALSE; |
3132 | 0 | } |
3133 | | |
3134 | 0 | return OPJ_TRUE; |
3135 | 0 | } |
3136 | | |
3137 | | static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, |
3138 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
3139 | 0 | { |
3140 | 0 | return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, |
3141 | 0 | p_first_comp_no, p_second_comp_no); |
3142 | 0 | } |
3143 | | |
3144 | | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, |
3145 | | OPJ_UINT32 p_comp_no, |
3146 | | OPJ_BYTE * p_data, |
3147 | | OPJ_UINT32 * p_data_written, |
3148 | | opj_event_mgr_t * p_manager |
3149 | | ) |
3150 | 0 | { |
3151 | 0 | OPJ_UINT32 l_qcc_size, l_remaining_size; |
3152 | 0 | OPJ_BYTE * l_current_data = 00; |
3153 | | |
3154 | | /* preconditions */ |
3155 | 0 | assert(p_j2k != 00); |
3156 | 0 | assert(p_manager != 00); |
3157 | |
|
3158 | 0 | l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3159 | 0 | p_comp_no); |
3160 | 0 | l_remaining_size = l_qcc_size; |
3161 | |
|
3162 | 0 | l_current_data = p_data; |
3163 | |
|
3164 | 0 | opj_write_bytes(l_current_data, J2K_MS_QCC, 2); /* QCC */ |
3165 | 0 | l_current_data += 2; |
3166 | |
|
3167 | 0 | if (p_j2k->m_private_image->numcomps <= 256) { |
3168 | 0 | --l_qcc_size; |
3169 | |
|
3170 | 0 | opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ |
3171 | 0 | l_current_data += 2; |
3172 | |
|
3173 | 0 | opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ |
3174 | 0 | ++l_current_data; |
3175 | | |
3176 | | /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */ |
3177 | 0 | l_remaining_size -= 6; |
3178 | 0 | } else { |
3179 | 0 | opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ |
3180 | 0 | l_current_data += 2; |
3181 | |
|
3182 | 0 | opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ |
3183 | 0 | l_current_data += 2; |
3184 | |
|
3185 | 0 | l_remaining_size -= 6; |
3186 | 0 | } |
3187 | |
|
3188 | 0 | opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no, |
3189 | 0 | l_current_data, &l_remaining_size, p_manager); |
3190 | |
|
3191 | 0 | *p_data_written = l_qcc_size; |
3192 | 0 | } |
3193 | | |
3194 | | static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k) |
3195 | 0 | { |
3196 | 0 | return opj_j2k_get_max_coc_size(p_j2k); |
3197 | 0 | } |
3198 | | |
3199 | | /** |
3200 | | * Reads a QCC marker (Quantization component) |
3201 | | * @param p_header_data the data contained in the QCC box. |
3202 | | * @param p_j2k the jpeg2000 codec. |
3203 | | * @param p_header_size the size of the data contained in the QCC marker. |
3204 | | * @param p_manager the user event manager. |
3205 | | */ |
3206 | | static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, |
3207 | | OPJ_BYTE * p_header_data, |
3208 | | OPJ_UINT32 p_header_size, |
3209 | | opj_event_mgr_t * p_manager |
3210 | | ) |
3211 | 2.53k | { |
3212 | 2.53k | OPJ_UINT32 l_num_comp, l_comp_no; |
3213 | | |
3214 | | /* preconditions */ |
3215 | 2.53k | assert(p_header_data != 00); |
3216 | 2.53k | assert(p_j2k != 00); |
3217 | 2.53k | assert(p_manager != 00); |
3218 | | |
3219 | 2.53k | l_num_comp = p_j2k->m_private_image->numcomps; |
3220 | | |
3221 | 2.53k | if (l_num_comp <= 256) { |
3222 | 1.90k | if (p_header_size < 1) { |
3223 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3224 | 2 | return OPJ_FALSE; |
3225 | 2 | } |
3226 | 1.89k | opj_read_bytes(p_header_data, &l_comp_no, 1); |
3227 | 1.89k | ++p_header_data; |
3228 | 1.89k | --p_header_size; |
3229 | 1.89k | } else { |
3230 | 630 | if (p_header_size < 2) { |
3231 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3232 | 1 | return OPJ_FALSE; |
3233 | 1 | } |
3234 | 629 | opj_read_bytes(p_header_data, &l_comp_no, 2); |
3235 | 629 | p_header_data += 2; |
3236 | 629 | p_header_size -= 2; |
3237 | 629 | } |
3238 | | |
3239 | | #ifdef USE_JPWL |
3240 | | if (p_j2k->m_cp.correct) { |
3241 | | |
3242 | | static OPJ_UINT32 backup_compno = 0; |
3243 | | |
3244 | | /* compno is negative or larger than the number of components!!! */ |
3245 | | if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { |
3246 | | opj_event_msg(p_manager, EVT_ERROR, |
3247 | | "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", |
3248 | | l_comp_no, l_num_comp); |
3249 | | if (!JPWL_ASSUME) { |
3250 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
3251 | | return OPJ_FALSE; |
3252 | | } |
3253 | | /* we try to correct */ |
3254 | | l_comp_no = backup_compno % l_num_comp; |
3255 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
3256 | | "- setting component number to %d\n", |
3257 | | l_comp_no); |
3258 | | } |
3259 | | |
3260 | | /* keep your private count of tiles */ |
3261 | | backup_compno++; |
3262 | | }; |
3263 | | #endif /* USE_JPWL */ |
3264 | | |
3265 | 2.52k | if (l_comp_no >= p_j2k->m_private_image->numcomps) { |
3266 | 14 | opj_event_msg(p_manager, EVT_ERROR, |
3267 | 14 | "Invalid component number: %d, regarding the number of components %d\n", |
3268 | 14 | l_comp_no, p_j2k->m_private_image->numcomps); |
3269 | 14 | return OPJ_FALSE; |
3270 | 14 | } |
3271 | | |
3272 | 2.51k | if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
3273 | 2.51k | p_manager)) { |
3274 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3275 | 3 | return OPJ_FALSE; |
3276 | 3 | } |
3277 | | |
3278 | 2.51k | if (p_header_size != 0) { |
3279 | 6 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3280 | 6 | return OPJ_FALSE; |
3281 | 6 | } |
3282 | | |
3283 | 2.50k | return OPJ_TRUE; |
3284 | 2.51k | } |
3285 | | |
3286 | | static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, |
3287 | | opj_stream_private_t *p_stream, |
3288 | | opj_event_mgr_t * p_manager |
3289 | | ) |
3290 | 0 | { |
3291 | 0 | OPJ_UINT32 l_nb_comp; |
3292 | 0 | OPJ_UINT32 l_nb_poc; |
3293 | 0 | OPJ_UINT32 l_poc_size; |
3294 | 0 | OPJ_UINT32 l_written_size = 0; |
3295 | 0 | opj_tcp_t *l_tcp = 00; |
3296 | 0 | OPJ_UINT32 l_poc_room; |
3297 | | |
3298 | | /* preconditions */ |
3299 | 0 | assert(p_j2k != 00); |
3300 | 0 | assert(p_manager != 00); |
3301 | 0 | assert(p_stream != 00); |
3302 | |
|
3303 | 0 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
3304 | 0 | l_nb_comp = p_j2k->m_private_image->numcomps; |
3305 | 0 | l_nb_poc = 1 + l_tcp->numpocs; |
3306 | |
|
3307 | 0 | if (l_nb_comp <= 256) { |
3308 | 0 | l_poc_room = 1; |
3309 | 0 | } else { |
3310 | 0 | l_poc_room = 2; |
3311 | 0 | } |
3312 | 0 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
3313 | |
|
3314 | 0 | if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3315 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
3316 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size); |
3317 | 0 | if (! new_header_tile_data) { |
3318 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3319 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3320 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3321 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n"); |
3322 | 0 | return OPJ_FALSE; |
3323 | 0 | } |
3324 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3325 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; |
3326 | 0 | } |
3327 | | |
3328 | 0 | opj_j2k_write_poc_in_memory(p_j2k, |
3329 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size, |
3330 | 0 | p_manager); |
3331 | |
|
3332 | 0 | if (opj_stream_write_data(p_stream, |
3333 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size, |
3334 | 0 | p_manager) != l_poc_size) { |
3335 | 0 | return OPJ_FALSE; |
3336 | 0 | } |
3337 | | |
3338 | 0 | return OPJ_TRUE; |
3339 | 0 | } |
3340 | | |
3341 | | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, |
3342 | | OPJ_BYTE * p_data, |
3343 | | OPJ_UINT32 * p_data_written, |
3344 | | opj_event_mgr_t * p_manager |
3345 | | ) |
3346 | 0 | { |
3347 | 0 | OPJ_UINT32 i; |
3348 | 0 | OPJ_BYTE * l_current_data = 00; |
3349 | 0 | OPJ_UINT32 l_nb_comp; |
3350 | 0 | OPJ_UINT32 l_nb_poc; |
3351 | 0 | OPJ_UINT32 l_poc_size; |
3352 | 0 | opj_image_t *l_image = 00; |
3353 | 0 | opj_tcp_t *l_tcp = 00; |
3354 | 0 | opj_tccp_t *l_tccp = 00; |
3355 | 0 | opj_poc_t *l_current_poc = 00; |
3356 | 0 | OPJ_UINT32 l_poc_room; |
3357 | | |
3358 | | /* preconditions */ |
3359 | 0 | assert(p_j2k != 00); |
3360 | 0 | assert(p_manager != 00); |
3361 | |
|
3362 | 0 | OPJ_UNUSED(p_manager); |
3363 | |
|
3364 | 0 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
3365 | 0 | l_tccp = &l_tcp->tccps[0]; |
3366 | 0 | l_image = p_j2k->m_private_image; |
3367 | 0 | l_nb_comp = l_image->numcomps; |
3368 | 0 | l_nb_poc = 1 + l_tcp->numpocs; |
3369 | |
|
3370 | 0 | if (l_nb_comp <= 256) { |
3371 | 0 | l_poc_room = 1; |
3372 | 0 | } else { |
3373 | 0 | l_poc_room = 2; |
3374 | 0 | } |
3375 | |
|
3376 | 0 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
3377 | |
|
3378 | 0 | l_current_data = p_data; |
3379 | |
|
3380 | 0 | opj_write_bytes(l_current_data, J2K_MS_POC, |
3381 | 0 | 2); /* POC */ |
3382 | 0 | l_current_data += 2; |
3383 | |
|
3384 | 0 | opj_write_bytes(l_current_data, l_poc_size - 2, |
3385 | 0 | 2); /* Lpoc */ |
3386 | 0 | l_current_data += 2; |
3387 | |
|
3388 | 0 | l_current_poc = l_tcp->pocs; |
3389 | 0 | for (i = 0; i < l_nb_poc; ++i) { |
3390 | 0 | opj_write_bytes(l_current_data, l_current_poc->resno0, |
3391 | 0 | 1); /* RSpoc_i */ |
3392 | 0 | ++l_current_data; |
3393 | |
|
3394 | 0 | opj_write_bytes(l_current_data, l_current_poc->compno0, |
3395 | 0 | l_poc_room); /* CSpoc_i */ |
3396 | 0 | l_current_data += l_poc_room; |
3397 | |
|
3398 | 0 | opj_write_bytes(l_current_data, l_current_poc->layno1, |
3399 | 0 | 2); /* LYEpoc_i */ |
3400 | 0 | l_current_data += 2; |
3401 | |
|
3402 | 0 | opj_write_bytes(l_current_data, l_current_poc->resno1, |
3403 | 0 | 1); /* REpoc_i */ |
3404 | 0 | ++l_current_data; |
3405 | |
|
3406 | 0 | opj_write_bytes(l_current_data, l_current_poc->compno1, |
3407 | 0 | l_poc_room); /* CEpoc_i */ |
3408 | 0 | l_current_data += l_poc_room; |
3409 | |
|
3410 | 0 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg, |
3411 | 0 | 1); /* Ppoc_i */ |
3412 | 0 | ++l_current_data; |
3413 | | |
3414 | | /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ |
3415 | 0 | l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3416 | 0 | l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers); |
3417 | 0 | l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3418 | 0 | l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions); |
3419 | 0 | l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3420 | 0 | l_current_poc->compno1, (OPJ_INT32)l_nb_comp); |
3421 | |
|
3422 | 0 | ++l_current_poc; |
3423 | 0 | } |
3424 | |
|
3425 | 0 | *p_data_written = l_poc_size; |
3426 | 0 | } |
3427 | | |
3428 | | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k) |
3429 | 0 | { |
3430 | 0 | opj_tcp_t * l_tcp = 00; |
3431 | 0 | OPJ_UINT32 l_nb_tiles = 0; |
3432 | 0 | OPJ_UINT32 l_max_poc = 0; |
3433 | 0 | OPJ_UINT32 i; |
3434 | |
|
3435 | 0 | l_tcp = p_j2k->m_cp.tcps; |
3436 | 0 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
3437 | |
|
3438 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
3439 | 0 | l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs); |
3440 | 0 | ++l_tcp; |
3441 | 0 | } |
3442 | |
|
3443 | 0 | ++l_max_poc; |
3444 | |
|
3445 | 0 | return 4 + 9 * l_max_poc; |
3446 | 0 | } |
3447 | | |
3448 | | static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k) |
3449 | 0 | { |
3450 | 0 | OPJ_UINT32 i; |
3451 | 0 | OPJ_UINT32 l_nb_tiles; |
3452 | 0 | OPJ_UINT32 l_max = 0; |
3453 | 0 | opj_tcp_t * l_tcp = 00; |
3454 | |
|
3455 | 0 | l_tcp = p_j2k->m_cp.tcps; |
3456 | 0 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
3457 | |
|
3458 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
3459 | 0 | l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts); |
3460 | |
|
3461 | 0 | ++l_tcp; |
3462 | 0 | } |
3463 | |
|
3464 | 0 | return 12 * l_max; |
3465 | 0 | } |
3466 | | |
3467 | | static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k) |
3468 | 0 | { |
3469 | 0 | OPJ_UINT32 l_nb_bytes = 0; |
3470 | 0 | OPJ_UINT32 l_nb_comps; |
3471 | 0 | OPJ_UINT32 l_coc_bytes, l_qcc_bytes; |
3472 | |
|
3473 | 0 | l_nb_comps = p_j2k->m_private_image->numcomps - 1; |
3474 | 0 | l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k); |
3475 | |
|
3476 | 0 | if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) { |
3477 | 0 | l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k); |
3478 | 0 | l_nb_bytes += l_nb_comps * l_coc_bytes; |
3479 | |
|
3480 | 0 | l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k); |
3481 | 0 | l_nb_bytes += l_nb_comps * l_qcc_bytes; |
3482 | 0 | } |
3483 | |
|
3484 | 0 | l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k); |
3485 | |
|
3486 | 0 | if (p_j2k->m_specific_param.m_encoder.m_PLT) { |
3487 | | /* Reserve space for PLT markers */ |
3488 | |
|
3489 | 0 | OPJ_UINT32 i; |
3490 | 0 | const opj_cp_t * l_cp = &(p_j2k->m_cp); |
3491 | 0 | OPJ_UINT32 l_max_packet_count = 0; |
3492 | 0 | for (i = 0; i < l_cp->th * l_cp->tw; ++i) { |
3493 | 0 | l_max_packet_count = opj_uint_max(l_max_packet_count, |
3494 | 0 | opj_get_encoding_packet_count(p_j2k->m_private_image, l_cp, i)); |
3495 | 0 | } |
3496 | | /* Minimum 6 bytes per PLT marker, and at a minimum (taking a pessimistic */ |
3497 | | /* estimate of 4 bytes for a packet size), one can write */ |
3498 | | /* (65536-6) / 4 = 16382 paquet sizes per PLT marker */ |
3499 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT = |
3500 | 0 | 6 * opj_uint_ceildiv(l_max_packet_count, 16382); |
3501 | | /* Maximum 5 bytes per packet to encode a full UINT32 */ |
3502 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += |
3503 | 0 | l_nb_bytes += 5 * l_max_packet_count; |
3504 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += 1; |
3505 | 0 | l_nb_bytes += p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT; |
3506 | 0 | } |
3507 | | |
3508 | | /*** DEVELOPER CORNER, Add room for your headers ***/ |
3509 | |
|
3510 | 0 | return l_nb_bytes; |
3511 | 0 | } |
3512 | | |
3513 | | /** |
3514 | | * Reads a POC marker (Progression Order Change) |
3515 | | * |
3516 | | * @param p_header_data the data contained in the POC box. |
3517 | | * @param p_j2k the jpeg2000 codec. |
3518 | | * @param p_header_size the size of the data contained in the POC marker. |
3519 | | * @param p_manager the user event manager. |
3520 | | */ |
3521 | | static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, |
3522 | | OPJ_BYTE * p_header_data, |
3523 | | OPJ_UINT32 p_header_size, |
3524 | | opj_event_mgr_t * p_manager |
3525 | | ) |
3526 | 1.96k | { |
3527 | 1.96k | OPJ_UINT32 i, l_nb_comp, l_tmp; |
3528 | 1.96k | opj_image_t * l_image = 00; |
3529 | 1.96k | OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; |
3530 | 1.96k | OPJ_UINT32 l_chunk_size, l_comp_room; |
3531 | | |
3532 | 1.96k | opj_cp_t *l_cp = 00; |
3533 | 1.96k | opj_tcp_t *l_tcp = 00; |
3534 | 1.96k | opj_poc_t *l_current_poc = 00; |
3535 | | |
3536 | | /* preconditions */ |
3537 | 1.96k | assert(p_header_data != 00); |
3538 | 1.96k | assert(p_j2k != 00); |
3539 | 1.96k | assert(p_manager != 00); |
3540 | | |
3541 | 1.96k | l_image = p_j2k->m_private_image; |
3542 | 1.96k | l_nb_comp = l_image->numcomps; |
3543 | 1.96k | if (l_nb_comp <= 256) { |
3544 | 1.92k | l_comp_room = 1; |
3545 | 1.92k | } else { |
3546 | 40 | l_comp_room = 2; |
3547 | 40 | } |
3548 | 1.96k | l_chunk_size = 5 + 2 * l_comp_room; |
3549 | 1.96k | l_current_poc_nb = p_header_size / l_chunk_size; |
3550 | 1.96k | l_current_poc_remaining = p_header_size % l_chunk_size; |
3551 | | |
3552 | 1.96k | if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { |
3553 | 9 | opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n"); |
3554 | 9 | return OPJ_FALSE; |
3555 | 9 | } |
3556 | | |
3557 | 1.95k | l_cp = &(p_j2k->m_cp); |
3558 | 1.95k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
3559 | 71 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
3560 | 1.95k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
3561 | 1.95k | l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; |
3562 | 1.95k | l_current_poc_nb += l_old_poc_nb; |
3563 | | |
3564 | 1.95k | if (l_current_poc_nb >= J2K_MAX_POCS) { |
3565 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb); |
3566 | 5 | return OPJ_FALSE; |
3567 | 5 | } |
3568 | | |
3569 | | /* now poc is in use.*/ |
3570 | 1.95k | l_tcp->POC = 1; |
3571 | | |
3572 | 1.95k | l_current_poc = &l_tcp->pocs[l_old_poc_nb]; |
3573 | 9.98k | for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { |
3574 | 8.03k | opj_read_bytes(p_header_data, &(l_current_poc->resno0), |
3575 | 8.03k | 1); /* RSpoc_i */ |
3576 | 8.03k | ++p_header_data; |
3577 | 8.03k | opj_read_bytes(p_header_data, &(l_current_poc->compno0), |
3578 | 8.03k | l_comp_room); /* CSpoc_i */ |
3579 | 8.03k | p_header_data += l_comp_room; |
3580 | 8.03k | opj_read_bytes(p_header_data, &(l_current_poc->layno1), |
3581 | 8.03k | 2); /* LYEpoc_i */ |
3582 | | /* make sure layer end is in acceptable bounds */ |
3583 | 8.03k | l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers); |
3584 | 8.03k | p_header_data += 2; |
3585 | 8.03k | opj_read_bytes(p_header_data, &(l_current_poc->resno1), |
3586 | 8.03k | 1); /* REpoc_i */ |
3587 | 8.03k | ++p_header_data; |
3588 | 8.03k | opj_read_bytes(p_header_data, &(l_current_poc->compno1), |
3589 | 8.03k | l_comp_room); /* CEpoc_i */ |
3590 | 8.03k | p_header_data += l_comp_room; |
3591 | 8.03k | opj_read_bytes(p_header_data, &l_tmp, |
3592 | 8.03k | 1); /* Ppoc_i */ |
3593 | 8.03k | ++p_header_data; |
3594 | 8.03k | l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; |
3595 | | /* make sure comp is in acceptable bounds */ |
3596 | 8.03k | l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp); |
3597 | 8.03k | ++l_current_poc; |
3598 | 8.03k | } |
3599 | | |
3600 | 1.95k | l_tcp->numpocs = l_current_poc_nb - 1; |
3601 | 1.95k | return OPJ_TRUE; |
3602 | 1.95k | } |
3603 | | |
3604 | | /** |
3605 | | * Reads a CRG marker (Component registration) |
3606 | | * |
3607 | | * @param p_header_data the data contained in the TLM box. |
3608 | | * @param p_j2k the jpeg2000 codec. |
3609 | | * @param p_header_size the size of the data contained in the TLM marker. |
3610 | | * @param p_manager the user event manager. |
3611 | | */ |
3612 | | static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, |
3613 | | OPJ_BYTE * p_header_data, |
3614 | | OPJ_UINT32 p_header_size, |
3615 | | opj_event_mgr_t * p_manager |
3616 | | ) |
3617 | 265 | { |
3618 | 265 | OPJ_UINT32 l_nb_comp; |
3619 | | /* preconditions */ |
3620 | 265 | assert(p_header_data != 00); |
3621 | 265 | assert(p_j2k != 00); |
3622 | 265 | assert(p_manager != 00); |
3623 | | |
3624 | 265 | OPJ_UNUSED(p_header_data); |
3625 | | |
3626 | 265 | l_nb_comp = p_j2k->m_private_image->numcomps; |
3627 | | |
3628 | 265 | if (p_header_size != l_nb_comp * 4) { |
3629 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n"); |
3630 | 5 | return OPJ_FALSE; |
3631 | 5 | } |
3632 | | /* Do not care of this at the moment since only local variables are set here */ |
3633 | | /* |
3634 | | for |
3635 | | (i = 0; i < l_nb_comp; ++i) |
3636 | | { |
3637 | | opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i |
3638 | | p_header_data+=2; |
3639 | | opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i |
3640 | | p_header_data+=2; |
3641 | | } |
3642 | | */ |
3643 | 260 | return OPJ_TRUE; |
3644 | 265 | } |
3645 | | |
3646 | | /** |
3647 | | * Reads a TLM marker (Tile Length Marker) |
3648 | | * |
3649 | | * @param p_header_data the data contained in the TLM box. |
3650 | | * @param p_j2k the jpeg2000 codec. |
3651 | | * @param p_header_size the size of the data contained in the TLM marker. |
3652 | | * @param p_manager the user event manager. |
3653 | | */ |
3654 | | static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, |
3655 | | OPJ_BYTE * p_header_data, |
3656 | | OPJ_UINT32 p_header_size, |
3657 | | opj_event_mgr_t * p_manager |
3658 | | ) |
3659 | 353 | { |
3660 | 353 | OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, |
3661 | 353 | l_Ptlm_size; |
3662 | | /* preconditions */ |
3663 | 353 | assert(p_header_data != 00); |
3664 | 353 | assert(p_j2k != 00); |
3665 | 353 | assert(p_manager != 00); |
3666 | | |
3667 | 353 | OPJ_UNUSED(p_j2k); |
3668 | | |
3669 | 353 | if (p_header_size < 2) { |
3670 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); |
3671 | 3 | return OPJ_FALSE; |
3672 | 3 | } |
3673 | 350 | p_header_size -= 2; |
3674 | | |
3675 | 350 | opj_read_bytes(p_header_data, &l_Ztlm, |
3676 | 350 | 1); /* Ztlm */ |
3677 | 350 | ++p_header_data; |
3678 | 350 | opj_read_bytes(p_header_data, &l_Stlm, |
3679 | 350 | 1); /* Stlm */ |
3680 | 350 | ++p_header_data; |
3681 | | |
3682 | 350 | l_ST = ((l_Stlm >> 4) & 0x3); |
3683 | 350 | l_SP = (l_Stlm >> 6) & 0x1; |
3684 | | |
3685 | 350 | l_Ptlm_size = (l_SP + 1) * 2; |
3686 | 350 | l_quotient = l_Ptlm_size + l_ST; |
3687 | | |
3688 | 350 | l_tot_num_tp_remaining = p_header_size % l_quotient; |
3689 | | |
3690 | 350 | if (l_tot_num_tp_remaining != 0) { |
3691 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); |
3692 | 2 | return OPJ_FALSE; |
3693 | 2 | } |
3694 | | /* FIXME Do not care of this at the moment since only local variables are set here */ |
3695 | | /* |
3696 | | for |
3697 | | (i = 0; i < l_tot_num_tp; ++i) |
3698 | | { |
3699 | | opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i |
3700 | | p_header_data += l_ST; |
3701 | | opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i |
3702 | | p_header_data += l_Ptlm_size; |
3703 | | }*/ |
3704 | 348 | return OPJ_TRUE; |
3705 | 350 | } |
3706 | | |
3707 | | /** |
3708 | | * Reads a PLM marker (Packet length, main header marker) |
3709 | | * |
3710 | | * @param p_header_data the data contained in the TLM box. |
3711 | | * @param p_j2k the jpeg2000 codec. |
3712 | | * @param p_header_size the size of the data contained in the TLM marker. |
3713 | | * @param p_manager the user event manager. |
3714 | | */ |
3715 | | static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, |
3716 | | OPJ_BYTE * p_header_data, |
3717 | | OPJ_UINT32 p_header_size, |
3718 | | opj_event_mgr_t * p_manager |
3719 | | ) |
3720 | 6.23k | { |
3721 | | /* preconditions */ |
3722 | 6.23k | assert(p_header_data != 00); |
3723 | 6.23k | assert(p_j2k != 00); |
3724 | 6.23k | assert(p_manager != 00); |
3725 | | |
3726 | 6.23k | OPJ_UNUSED(p_j2k); |
3727 | 6.23k | OPJ_UNUSED(p_header_data); |
3728 | | |
3729 | 6.23k | if (p_header_size < 1) { |
3730 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3731 | 2 | return OPJ_FALSE; |
3732 | 2 | } |
3733 | | /* Do not care of this at the moment since only local variables are set here */ |
3734 | | /* |
3735 | | opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm |
3736 | | ++p_header_data; |
3737 | | --p_header_size; |
3738 | | |
3739 | | while |
3740 | | (p_header_size > 0) |
3741 | | { |
3742 | | opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm |
3743 | | ++p_header_data; |
3744 | | p_header_size -= (1+l_Nplm); |
3745 | | if |
3746 | | (p_header_size < 0) |
3747 | | { |
3748 | | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3749 | | return false; |
3750 | | } |
3751 | | for |
3752 | | (i = 0; i < l_Nplm; ++i) |
3753 | | { |
3754 | | opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij |
3755 | | ++p_header_data; |
3756 | | // take only the last seven bytes |
3757 | | l_packet_len |= (l_tmp & 0x7f); |
3758 | | if |
3759 | | (l_tmp & 0x80) |
3760 | | { |
3761 | | l_packet_len <<= 7; |
3762 | | } |
3763 | | else |
3764 | | { |
3765 | | // store packet length and proceed to next packet |
3766 | | l_packet_len = 0; |
3767 | | } |
3768 | | } |
3769 | | if |
3770 | | (l_packet_len != 0) |
3771 | | { |
3772 | | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3773 | | return false; |
3774 | | } |
3775 | | } |
3776 | | */ |
3777 | 6.22k | return OPJ_TRUE; |
3778 | 6.23k | } |
3779 | | |
3780 | | /** |
3781 | | * Reads a PLT marker (Packet length, tile-part header) |
3782 | | * |
3783 | | * @param p_header_data the data contained in the PLT box. |
3784 | | * @param p_j2k the jpeg2000 codec. |
3785 | | * @param p_header_size the size of the data contained in the PLT marker. |
3786 | | * @param p_manager the user event manager. |
3787 | | */ |
3788 | | static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, |
3789 | | OPJ_BYTE * p_header_data, |
3790 | | OPJ_UINT32 p_header_size, |
3791 | | opj_event_mgr_t * p_manager |
3792 | | ) |
3793 | 668 | { |
3794 | 668 | OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; |
3795 | | |
3796 | | /* preconditions */ |
3797 | 668 | assert(p_header_data != 00); |
3798 | 668 | assert(p_j2k != 00); |
3799 | 668 | assert(p_manager != 00); |
3800 | | |
3801 | 668 | OPJ_UNUSED(p_j2k); |
3802 | | |
3803 | 668 | if (p_header_size < 1) { |
3804 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
3805 | 2 | return OPJ_FALSE; |
3806 | 2 | } |
3807 | | |
3808 | 666 | opj_read_bytes(p_header_data, &l_Zplt, 1); /* Zplt */ |
3809 | 666 | ++p_header_data; |
3810 | 666 | --p_header_size; |
3811 | | |
3812 | 69.1k | for (i = 0; i < p_header_size; ++i) { |
3813 | 68.4k | opj_read_bytes(p_header_data, &l_tmp, 1); /* Iplt_ij */ |
3814 | 68.4k | ++p_header_data; |
3815 | | /* take only the last seven bytes */ |
3816 | 68.4k | l_packet_len |= (l_tmp & 0x7f); |
3817 | 68.4k | if (l_tmp & 0x80) { |
3818 | 20.7k | l_packet_len <<= 7; |
3819 | 47.6k | } else { |
3820 | | /* store packet length and proceed to next packet */ |
3821 | 47.6k | l_packet_len = 0; |
3822 | 47.6k | } |
3823 | 68.4k | } |
3824 | | |
3825 | 666 | if (l_packet_len != 0) { |
3826 | 28 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
3827 | 28 | return OPJ_FALSE; |
3828 | 28 | } |
3829 | | |
3830 | 638 | return OPJ_TRUE; |
3831 | 666 | } |
3832 | | |
3833 | | /** |
3834 | | * Reads a PPM marker (Packed packet headers, main header) |
3835 | | * |
3836 | | * @param p_header_data the data contained in the POC box. |
3837 | | * @param p_j2k the jpeg2000 codec. |
3838 | | * @param p_header_size the size of the data contained in the POC marker. |
3839 | | * @param p_manager the user event manager. |
3840 | | */ |
3841 | | |
3842 | | static OPJ_BOOL opj_j2k_read_ppm( |
3843 | | opj_j2k_t *p_j2k, |
3844 | | OPJ_BYTE * p_header_data, |
3845 | | OPJ_UINT32 p_header_size, |
3846 | | opj_event_mgr_t * p_manager) |
3847 | 741 | { |
3848 | 741 | opj_cp_t *l_cp = 00; |
3849 | 741 | OPJ_UINT32 l_Z_ppm; |
3850 | | |
3851 | | /* preconditions */ |
3852 | 741 | assert(p_header_data != 00); |
3853 | 741 | assert(p_j2k != 00); |
3854 | 741 | assert(p_manager != 00); |
3855 | | |
3856 | | /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */ |
3857 | 741 | if (p_header_size < 2) { |
3858 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
3859 | 4 | return OPJ_FALSE; |
3860 | 4 | } |
3861 | | |
3862 | 737 | l_cp = &(p_j2k->m_cp); |
3863 | 737 | l_cp->ppm = 1; |
3864 | | |
3865 | 737 | opj_read_bytes(p_header_data, &l_Z_ppm, 1); /* Z_ppm */ |
3866 | 737 | ++p_header_data; |
3867 | 737 | --p_header_size; |
3868 | | |
3869 | | /* check allocation needed */ |
3870 | 737 | if (l_cp->ppm_markers == NULL) { /* first PPM marker */ |
3871 | 218 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3872 | 218 | assert(l_cp->ppm_markers_count == 0U); |
3873 | | |
3874 | 218 | l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
3875 | 218 | if (l_cp->ppm_markers == NULL) { |
3876 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
3877 | 0 | return OPJ_FALSE; |
3878 | 0 | } |
3879 | 218 | l_cp->ppm_markers_count = l_newCount; |
3880 | 519 | } else if (l_cp->ppm_markers_count <= l_Z_ppm) { |
3881 | 258 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3882 | 258 | opj_ppx *new_ppm_markers; |
3883 | 258 | new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers, |
3884 | 258 | l_newCount * sizeof(opj_ppx)); |
3885 | 258 | if (new_ppm_markers == NULL) { |
3886 | | /* clean up to be done on l_cp destruction */ |
3887 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
3888 | 0 | return OPJ_FALSE; |
3889 | 0 | } |
3890 | 258 | l_cp->ppm_markers = new_ppm_markers; |
3891 | 258 | memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0, |
3892 | 258 | (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx)); |
3893 | 258 | l_cp->ppm_markers_count = l_newCount; |
3894 | 258 | } |
3895 | | |
3896 | 737 | if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) { |
3897 | | /* clean up to be done on l_cp destruction */ |
3898 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm); |
3899 | 2 | return OPJ_FALSE; |
3900 | 2 | } |
3901 | | |
3902 | 735 | l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
3903 | 735 | if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) { |
3904 | | /* clean up to be done on l_cp destruction */ |
3905 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
3906 | 0 | return OPJ_FALSE; |
3907 | 0 | } |
3908 | 735 | l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size; |
3909 | 735 | memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size); |
3910 | | |
3911 | 735 | return OPJ_TRUE; |
3912 | 735 | } |
3913 | | |
3914 | | /** |
3915 | | * Merges all PPM markers read (Packed headers, main header) |
3916 | | * |
3917 | | * @param p_cp main coding parameters. |
3918 | | * @param p_manager the user event manager. |
3919 | | */ |
3920 | | static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager) |
3921 | 14.2k | { |
3922 | 14.2k | OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining; |
3923 | | |
3924 | | /* preconditions */ |
3925 | 14.2k | assert(p_cp != 00); |
3926 | 14.2k | assert(p_manager != 00); |
3927 | 14.2k | assert(p_cp->ppm_buffer == NULL); |
3928 | | |
3929 | 14.2k | if (p_cp->ppm == 0U) { |
3930 | 14.0k | return OPJ_TRUE; |
3931 | 14.0k | } |
3932 | | |
3933 | 169 | l_ppm_data_size = 0U; |
3934 | 169 | l_N_ppm_remaining = 0U; |
3935 | 15.9k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
3936 | 15.8k | if (p_cp->ppm_markers[i].m_data != |
3937 | 15.8k | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
3938 | 544 | OPJ_UINT32 l_N_ppm; |
3939 | 544 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
3940 | 544 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
3941 | | |
3942 | 544 | if (l_N_ppm_remaining >= l_data_size) { |
3943 | 211 | l_N_ppm_remaining -= l_data_size; |
3944 | 211 | l_data_size = 0U; |
3945 | 333 | } else { |
3946 | 333 | l_data += l_N_ppm_remaining; |
3947 | 333 | l_data_size -= l_N_ppm_remaining; |
3948 | 333 | l_N_ppm_remaining = 0U; |
3949 | 333 | } |
3950 | | |
3951 | 544 | if (l_data_size > 0U) { |
3952 | 1.31k | do { |
3953 | | /* read Nppm */ |
3954 | 1.31k | if (l_data_size < 4U) { |
3955 | | /* clean up to be done on l_cp destruction */ |
3956 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); |
3957 | 4 | return OPJ_FALSE; |
3958 | 4 | } |
3959 | 1.30k | opj_read_bytes(l_data, &l_N_ppm, 4); |
3960 | 1.30k | l_data += 4; |
3961 | 1.30k | l_data_size -= 4; |
3962 | | |
3963 | 1.30k | if (l_ppm_data_size > UINT_MAX - l_N_ppm) { |
3964 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Too large value for Nppm\n"); |
3965 | 2 | return OPJ_FALSE; |
3966 | 2 | } |
3967 | 1.30k | l_ppm_data_size += l_N_ppm; |
3968 | 1.30k | if (l_data_size >= l_N_ppm) { |
3969 | 1.15k | l_data_size -= l_N_ppm; |
3970 | 1.15k | l_data += l_N_ppm; |
3971 | 1.15k | } else { |
3972 | 150 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
3973 | 150 | l_data_size = 0U; |
3974 | 150 | } |
3975 | 1.30k | } while (l_data_size > 0U); |
3976 | 333 | } |
3977 | 544 | } |
3978 | 15.8k | } |
3979 | | |
3980 | 163 | if (l_N_ppm_remaining != 0U) { |
3981 | | /* clean up to be done on l_cp destruction */ |
3982 | 114 | opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n"); |
3983 | 114 | return OPJ_FALSE; |
3984 | 114 | } |
3985 | | |
3986 | 49 | p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size); |
3987 | 49 | if (p_cp->ppm_buffer == 00) { |
3988 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
3989 | 2 | return OPJ_FALSE; |
3990 | 2 | } |
3991 | 47 | p_cp->ppm_len = l_ppm_data_size; |
3992 | 47 | l_ppm_data_size = 0U; |
3993 | 47 | l_N_ppm_remaining = 0U; |
3994 | 3.87k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
3995 | 3.83k | if (p_cp->ppm_markers[i].m_data != |
3996 | 3.83k | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
3997 | 200 | OPJ_UINT32 l_N_ppm; |
3998 | 200 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
3999 | 200 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
4000 | | |
4001 | 200 | if (l_N_ppm_remaining >= l_data_size) { |
4002 | 24 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
4003 | 24 | l_ppm_data_size += l_data_size; |
4004 | 24 | l_N_ppm_remaining -= l_data_size; |
4005 | 24 | l_data_size = 0U; |
4006 | 176 | } else { |
4007 | 176 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining); |
4008 | 176 | l_ppm_data_size += l_N_ppm_remaining; |
4009 | 176 | l_data += l_N_ppm_remaining; |
4010 | 176 | l_data_size -= l_N_ppm_remaining; |
4011 | 176 | l_N_ppm_remaining = 0U; |
4012 | 176 | } |
4013 | | |
4014 | 200 | if (l_data_size > 0U) { |
4015 | 364 | do { |
4016 | | /* read Nppm */ |
4017 | 364 | if (l_data_size < 4U) { |
4018 | | /* clean up to be done on l_cp destruction */ |
4019 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); |
4020 | 0 | return OPJ_FALSE; |
4021 | 0 | } |
4022 | 364 | opj_read_bytes(l_data, &l_N_ppm, 4); |
4023 | 364 | l_data += 4; |
4024 | 364 | l_data_size -= 4; |
4025 | | |
4026 | 364 | if (l_data_size >= l_N_ppm) { |
4027 | 345 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm); |
4028 | 345 | l_ppm_data_size += l_N_ppm; |
4029 | 345 | l_data_size -= l_N_ppm; |
4030 | 345 | l_data += l_N_ppm; |
4031 | 345 | } else { |
4032 | 19 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
4033 | 19 | l_ppm_data_size += l_data_size; |
4034 | 19 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
4035 | 19 | l_data_size = 0U; |
4036 | 19 | } |
4037 | 364 | } while (l_data_size > 0U); |
4038 | 176 | } |
4039 | 200 | opj_free(p_cp->ppm_markers[i].m_data); |
4040 | 200 | p_cp->ppm_markers[i].m_data = NULL; |
4041 | 200 | p_cp->ppm_markers[i].m_data_size = 0U; |
4042 | 200 | } |
4043 | 3.83k | } |
4044 | | |
4045 | 47 | p_cp->ppm_data = p_cp->ppm_buffer; |
4046 | 47 | p_cp->ppm_data_size = p_cp->ppm_len; |
4047 | | |
4048 | 47 | p_cp->ppm_markers_count = 0U; |
4049 | 47 | opj_free(p_cp->ppm_markers); |
4050 | 47 | p_cp->ppm_markers = NULL; |
4051 | | |
4052 | 47 | return OPJ_TRUE; |
4053 | 47 | } |
4054 | | |
4055 | | /** |
4056 | | * Reads a PPT marker (Packed packet headers, tile-part header) |
4057 | | * |
4058 | | * @param p_header_data the data contained in the PPT box. |
4059 | | * @param p_j2k the jpeg2000 codec. |
4060 | | * @param p_header_size the size of the data contained in the PPT marker. |
4061 | | * @param p_manager the user event manager. |
4062 | | */ |
4063 | | static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, |
4064 | | OPJ_BYTE * p_header_data, |
4065 | | OPJ_UINT32 p_header_size, |
4066 | | opj_event_mgr_t * p_manager |
4067 | | ) |
4068 | 224 | { |
4069 | 224 | opj_cp_t *l_cp = 00; |
4070 | 224 | opj_tcp_t *l_tcp = 00; |
4071 | 224 | OPJ_UINT32 l_Z_ppt; |
4072 | | |
4073 | | /* preconditions */ |
4074 | 224 | assert(p_header_data != 00); |
4075 | 224 | assert(p_j2k != 00); |
4076 | 224 | assert(p_manager != 00); |
4077 | | |
4078 | | /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */ |
4079 | 224 | if (p_header_size < 2) { |
4080 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n"); |
4081 | 2 | return OPJ_FALSE; |
4082 | 2 | } |
4083 | | |
4084 | 222 | l_cp = &(p_j2k->m_cp); |
4085 | 222 | if (l_cp->ppm) { |
4086 | 2 | opj_event_msg(p_manager, EVT_ERROR, |
4087 | 2 | "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); |
4088 | 2 | return OPJ_FALSE; |
4089 | 2 | } |
4090 | | |
4091 | 220 | l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); |
4092 | 220 | l_tcp->ppt = 1; |
4093 | | |
4094 | 220 | opj_read_bytes(p_header_data, &l_Z_ppt, 1); /* Z_ppt */ |
4095 | 220 | ++p_header_data; |
4096 | 220 | --p_header_size; |
4097 | | |
4098 | | /* check allocation needed */ |
4099 | 220 | if (l_tcp->ppt_markers == NULL) { /* first PPT marker */ |
4100 | 172 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4101 | 172 | assert(l_tcp->ppt_markers_count == 0U); |
4102 | | |
4103 | 172 | l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
4104 | 172 | if (l_tcp->ppt_markers == NULL) { |
4105 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4106 | 0 | return OPJ_FALSE; |
4107 | 0 | } |
4108 | 172 | l_tcp->ppt_markers_count = l_newCount; |
4109 | 172 | } else if (l_tcp->ppt_markers_count <= l_Z_ppt) { |
4110 | 18 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4111 | 18 | opj_ppx *new_ppt_markers; |
4112 | 18 | new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers, |
4113 | 18 | l_newCount * sizeof(opj_ppx)); |
4114 | 18 | if (new_ppt_markers == NULL) { |
4115 | | /* clean up to be done on l_tcp destruction */ |
4116 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4117 | 0 | return OPJ_FALSE; |
4118 | 0 | } |
4119 | 18 | l_tcp->ppt_markers = new_ppt_markers; |
4120 | 18 | memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0, |
4121 | 18 | (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx)); |
4122 | 18 | l_tcp->ppt_markers_count = l_newCount; |
4123 | 18 | } |
4124 | | |
4125 | 220 | if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) { |
4126 | | /* clean up to be done on l_tcp destruction */ |
4127 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt); |
4128 | 2 | return OPJ_FALSE; |
4129 | 2 | } |
4130 | | |
4131 | 218 | l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
4132 | 218 | if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) { |
4133 | | /* clean up to be done on l_tcp destruction */ |
4134 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4135 | 0 | return OPJ_FALSE; |
4136 | 0 | } |
4137 | 218 | l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size; |
4138 | 218 | memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size); |
4139 | 218 | return OPJ_TRUE; |
4140 | 218 | } |
4141 | | |
4142 | | /** |
4143 | | * Merges all PPT markers read (Packed packet headers, tile-part header) |
4144 | | * |
4145 | | * @param p_tcp the tile. |
4146 | | * @param p_manager the user event manager. |
4147 | | */ |
4148 | | static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) |
4149 | 15.1k | { |
4150 | 15.1k | OPJ_UINT32 i, l_ppt_data_size; |
4151 | | /* preconditions */ |
4152 | 15.1k | assert(p_tcp != 00); |
4153 | 15.1k | assert(p_manager != 00); |
4154 | | |
4155 | 15.1k | if (p_tcp->ppt_buffer != NULL) { |
4156 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4157 | 0 | "opj_j2k_merge_ppt() has already been called\n"); |
4158 | 0 | return OPJ_FALSE; |
4159 | 0 | } |
4160 | | |
4161 | 15.1k | if (p_tcp->ppt == 0U) { |
4162 | 14.9k | return OPJ_TRUE; |
4163 | 14.9k | } |
4164 | | |
4165 | 106 | l_ppt_data_size = 0U; |
4166 | 4.43k | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4167 | 4.32k | l_ppt_data_size += |
4168 | 4.32k | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4169 | 4.32k | } |
4170 | | |
4171 | 106 | p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size); |
4172 | 106 | if (p_tcp->ppt_buffer == 00) { |
4173 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4174 | 0 | return OPJ_FALSE; |
4175 | 0 | } |
4176 | 106 | p_tcp->ppt_len = l_ppt_data_size; |
4177 | 106 | l_ppt_data_size = 0U; |
4178 | 4.43k | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4179 | 4.32k | if (p_tcp->ppt_markers[i].m_data != |
4180 | 4.32k | NULL) { /* standard doesn't seem to require contiguous Zppt */ |
4181 | 116 | memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data, |
4182 | 116 | p_tcp->ppt_markers[i].m_data_size); |
4183 | 116 | l_ppt_data_size += |
4184 | 116 | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4185 | | |
4186 | 116 | opj_free(p_tcp->ppt_markers[i].m_data); |
4187 | 116 | p_tcp->ppt_markers[i].m_data = NULL; |
4188 | 116 | p_tcp->ppt_markers[i].m_data_size = 0U; |
4189 | 116 | } |
4190 | 4.32k | } |
4191 | | |
4192 | 106 | p_tcp->ppt_markers_count = 0U; |
4193 | 106 | opj_free(p_tcp->ppt_markers); |
4194 | 106 | p_tcp->ppt_markers = NULL; |
4195 | | |
4196 | 106 | p_tcp->ppt_data = p_tcp->ppt_buffer; |
4197 | 106 | p_tcp->ppt_data_size = p_tcp->ppt_len; |
4198 | 106 | return OPJ_TRUE; |
4199 | 106 | } |
4200 | | |
4201 | | static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, |
4202 | | opj_stream_private_t *p_stream, |
4203 | | opj_event_mgr_t * p_manager |
4204 | | ) |
4205 | 0 | { |
4206 | 0 | OPJ_BYTE * l_current_data = 00; |
4207 | 0 | OPJ_UINT32 l_tlm_size; |
4208 | 0 | OPJ_UINT32 size_per_tile_part; |
4209 | | |
4210 | | /* preconditions */ |
4211 | 0 | assert(p_j2k != 00); |
4212 | 0 | assert(p_manager != 00); |
4213 | 0 | assert(p_stream != 00); |
4214 | | |
4215 | | /* 10921 = (65535 - header_size) / size_per_tile_part where */ |
4216 | | /* header_size = 4 and size_per_tile_part = 6 */ |
4217 | 0 | if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts > 10921) { |
4218 | | /* We could do more but it would require writing several TLM markers */ |
4219 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4220 | 0 | "A maximum of 10921 tile-parts are supported currently " |
4221 | 0 | "when writing TLM marker\n"); |
4222 | 0 | return OPJ_FALSE; |
4223 | 0 | } |
4224 | | |
4225 | 0 | if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts <= 255) { |
4226 | 0 | size_per_tile_part = 5; |
4227 | 0 | p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_TRUE; |
4228 | 0 | } else { |
4229 | 0 | size_per_tile_part = 6; |
4230 | 0 | p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_FALSE; |
4231 | 0 | } |
4232 | |
|
4233 | 0 | l_tlm_size = 2 + 4 + (size_per_tile_part * |
4234 | 0 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
4235 | |
|
4236 | 0 | if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
4237 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
4238 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size); |
4239 | 0 | if (! new_header_tile_data) { |
4240 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
4241 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
4242 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
4243 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n"); |
4244 | 0 | return OPJ_FALSE; |
4245 | 0 | } |
4246 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
4247 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; |
4248 | 0 | } |
4249 | 0 | memset(p_j2k->m_specific_param.m_encoder.m_header_tile_data, 0, l_tlm_size); |
4250 | |
|
4251 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
4252 | | |
4253 | | /* change the way data is written to avoid seeking if possible */ |
4254 | | /* TODO */ |
4255 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); |
4256 | |
|
4257 | 0 | opj_write_bytes(l_current_data, J2K_MS_TLM, |
4258 | 0 | 2); /* TLM */ |
4259 | 0 | l_current_data += 2; |
4260 | |
|
4261 | 0 | opj_write_bytes(l_current_data, l_tlm_size - 2, |
4262 | 0 | 2); /* Lpoc */ |
4263 | 0 | l_current_data += 2; |
4264 | |
|
4265 | 0 | opj_write_bytes(l_current_data, 0, |
4266 | 0 | 1); /* Ztlm=0*/ |
4267 | 0 | ++l_current_data; |
4268 | | |
4269 | | /* Stlm 0x50= ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ |
4270 | | /* Stlm 0x60= ST=2(16bits-65535 tiles max),SP=1(Ptlm=32bits) */ |
4271 | 0 | opj_write_bytes(l_current_data, |
4272 | 0 | size_per_tile_part == 5 ? 0x50 : 0x60, |
4273 | 0 | 1); |
4274 | 0 | ++l_current_data; |
4275 | | |
4276 | | /* do nothing on the size_per_tile_part * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ |
4277 | 0 | if (opj_stream_write_data(p_stream, |
4278 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size, |
4279 | 0 | p_manager) != l_tlm_size) { |
4280 | 0 | return OPJ_FALSE; |
4281 | 0 | } |
4282 | | |
4283 | 0 | return OPJ_TRUE; |
4284 | 0 | } |
4285 | | |
4286 | | static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, |
4287 | | OPJ_BYTE * p_data, |
4288 | | OPJ_UINT32 total_data_size, |
4289 | | OPJ_UINT32 * p_data_written, |
4290 | | const opj_stream_private_t *p_stream, |
4291 | | opj_event_mgr_t * p_manager |
4292 | | ) |
4293 | 0 | { |
4294 | | /* preconditions */ |
4295 | 0 | assert(p_j2k != 00); |
4296 | 0 | assert(p_manager != 00); |
4297 | 0 | assert(p_stream != 00); |
4298 | |
|
4299 | 0 | OPJ_UNUSED(p_stream); |
4300 | |
|
4301 | 0 | if (total_data_size < 12) { |
4302 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4303 | 0 | "Not enough bytes in output buffer to write SOT marker\n"); |
4304 | 0 | return OPJ_FALSE; |
4305 | 0 | } |
4306 | | |
4307 | 0 | opj_write_bytes(p_data, J2K_MS_SOT, |
4308 | 0 | 2); /* SOT */ |
4309 | 0 | p_data += 2; |
4310 | |
|
4311 | 0 | opj_write_bytes(p_data, 10, |
4312 | 0 | 2); /* Lsot */ |
4313 | 0 | p_data += 2; |
4314 | |
|
4315 | 0 | opj_write_bytes(p_data, p_j2k->m_current_tile_number, |
4316 | 0 | 2); /* Isot */ |
4317 | 0 | p_data += 2; |
4318 | | |
4319 | | /* Psot */ |
4320 | 0 | p_data += 4; |
4321 | |
|
4322 | 0 | opj_write_bytes(p_data, |
4323 | 0 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number, |
4324 | 0 | 1); /* TPsot */ |
4325 | 0 | ++p_data; |
4326 | |
|
4327 | 0 | opj_write_bytes(p_data, |
4328 | 0 | p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts, |
4329 | 0 | 1); /* TNsot */ |
4330 | 0 | ++p_data; |
4331 | | |
4332 | | /* UniPG>> */ |
4333 | | #ifdef USE_JPWL |
4334 | | /* update markers struct */ |
4335 | | /* |
4336 | | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); |
4337 | | */ |
4338 | | assert(0 && "TODO"); |
4339 | | #endif /* USE_JPWL */ |
4340 | |
|
4341 | 0 | * p_data_written = 12; |
4342 | |
|
4343 | 0 | return OPJ_TRUE; |
4344 | 0 | } |
4345 | | |
4346 | | static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, |
4347 | | OPJ_UINT32 p_header_size, |
4348 | | OPJ_UINT32* p_tile_no, |
4349 | | OPJ_UINT32* p_tot_len, |
4350 | | OPJ_UINT32* p_current_part, |
4351 | | OPJ_UINT32* p_num_parts, |
4352 | | opj_event_mgr_t * p_manager) |
4353 | 19.7k | { |
4354 | | /* preconditions */ |
4355 | 19.7k | assert(p_header_data != 00); |
4356 | 19.7k | assert(p_manager != 00); |
4357 | | |
4358 | | /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ |
4359 | 19.7k | if (p_header_size != 8) { |
4360 | 13 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); |
4361 | 13 | return OPJ_FALSE; |
4362 | 13 | } |
4363 | | |
4364 | 19.7k | opj_read_bytes(p_header_data, p_tile_no, 2); /* Isot */ |
4365 | 19.7k | p_header_data += 2; |
4366 | 19.7k | opj_read_bytes(p_header_data, p_tot_len, 4); /* Psot */ |
4367 | 19.7k | p_header_data += 4; |
4368 | 19.7k | opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */ |
4369 | 19.7k | ++p_header_data; |
4370 | 19.7k | opj_read_bytes(p_header_data, p_num_parts, 1); /* TNsot */ |
4371 | 19.7k | ++p_header_data; |
4372 | 19.7k | return OPJ_TRUE; |
4373 | 19.7k | } |
4374 | | |
4375 | | static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, |
4376 | | OPJ_BYTE * p_header_data, |
4377 | | OPJ_UINT32 p_header_size, |
4378 | | opj_event_mgr_t * p_manager) |
4379 | 16.4k | { |
4380 | 16.4k | opj_cp_t *l_cp = 00; |
4381 | 16.4k | opj_tcp_t *l_tcp = 00; |
4382 | 16.4k | OPJ_UINT32 l_tot_len, l_num_parts = 0; |
4383 | 16.4k | OPJ_UINT32 l_current_part; |
4384 | 16.4k | OPJ_UINT32 l_tile_x, l_tile_y; |
4385 | | |
4386 | | /* preconditions */ |
4387 | | |
4388 | 16.4k | assert(p_j2k != 00); |
4389 | 16.4k | assert(p_manager != 00); |
4390 | | |
4391 | 16.4k | if (! opj_j2k_get_sot_values(p_header_data, p_header_size, |
4392 | 16.4k | &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts, |
4393 | 16.4k | p_manager)) { |
4394 | 13 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); |
4395 | 13 | return OPJ_FALSE; |
4396 | 13 | } |
4397 | | #ifdef DEBUG_VERBOSE |
4398 | | fprintf(stderr, "SOT %d %d %d %d\n", |
4399 | | p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts); |
4400 | | #endif |
4401 | | |
4402 | 16.4k | l_cp = &(p_j2k->m_cp); |
4403 | | |
4404 | | /* testcase 2.pdf.SIGFPE.706.1112 */ |
4405 | 16.4k | if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { |
4406 | 66 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", |
4407 | 66 | p_j2k->m_current_tile_number); |
4408 | 66 | return OPJ_FALSE; |
4409 | 66 | } |
4410 | | |
4411 | 16.4k | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
4412 | 16.4k | l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; |
4413 | 16.4k | l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; |
4414 | | |
4415 | 16.4k | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 || |
4416 | 16.4k | p_j2k->m_current_tile_number == (OPJ_UINT32) |
4417 | 16.4k | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) { |
4418 | | /* Do only this check if we decode all tile part headers, or if */ |
4419 | | /* we decode one precise tile. Otherwise the m_current_tile_part_number */ |
4420 | | /* might not be valid */ |
4421 | | /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */ |
4422 | | /* of https://github.com/uclouvain/openjpeg/issues/939 */ |
4423 | | /* We must avoid reading twice the same tile part number for a given tile */ |
4424 | | /* so as to avoid various issues, like opj_j2k_merge_ppt being called */ |
4425 | | /* several times. */ |
4426 | | /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */ |
4427 | | /* should appear in increasing order. */ |
4428 | 16.4k | if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) { |
4429 | 96 | opj_event_msg(p_manager, EVT_ERROR, |
4430 | 96 | "Invalid tile part index for tile number %d. " |
4431 | 96 | "Got %d, expected %d\n", |
4432 | 96 | p_j2k->m_current_tile_number, |
4433 | 96 | l_current_part, |
4434 | 96 | l_tcp->m_current_tile_part_number + 1); |
4435 | 96 | return OPJ_FALSE; |
4436 | 96 | } |
4437 | 16.4k | } |
4438 | | |
4439 | 16.3k | l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part; |
4440 | | |
4441 | | #ifdef USE_JPWL |
4442 | | if (l_cp->correct) { |
4443 | | |
4444 | | OPJ_UINT32 tileno = p_j2k->m_current_tile_number; |
4445 | | static OPJ_UINT32 backup_tileno = 0; |
4446 | | |
4447 | | /* tileno is negative or larger than the number of tiles!!! */ |
4448 | | if (tileno > (l_cp->tw * l_cp->th)) { |
4449 | | opj_event_msg(p_manager, EVT_ERROR, |
4450 | | "JPWL: bad tile number (%d out of a maximum of %d)\n", |
4451 | | tileno, (l_cp->tw * l_cp->th)); |
4452 | | if (!JPWL_ASSUME) { |
4453 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
4454 | | return OPJ_FALSE; |
4455 | | } |
4456 | | /* we try to correct */ |
4457 | | tileno = backup_tileno; |
4458 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
4459 | | "- setting tile number to %d\n", |
4460 | | tileno); |
4461 | | } |
4462 | | |
4463 | | /* keep your private count of tiles */ |
4464 | | backup_tileno++; |
4465 | | }; |
4466 | | #endif /* USE_JPWL */ |
4467 | | |
4468 | | /* look for the tile in the list of already processed tile (in parts). */ |
4469 | | /* Optimization possible here with a more complex data structure and with the removing of tiles */ |
4470 | | /* since the time taken by this function can only grow at the time */ |
4471 | | |
4472 | | /* PSot should be equal to zero or >=14 or <= 2^32-1 */ |
4473 | 16.3k | if ((l_tot_len != 0) && (l_tot_len < 14)) { |
4474 | 8 | if (l_tot_len == |
4475 | 8 | 12) { /* MSD: Special case for the PHR data which are read by kakadu*/ |
4476 | 4 | opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", |
4477 | 4 | l_tot_len); |
4478 | 4 | } else { |
4479 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
4480 | 4 | "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len); |
4481 | 4 | return OPJ_FALSE; |
4482 | 4 | } |
4483 | 8 | } |
4484 | | |
4485 | | #ifdef USE_JPWL |
4486 | | if (l_cp->correct) { |
4487 | | |
4488 | | /* totlen is negative or larger than the bytes left!!! */ |
4489 | | if (/*(l_tot_len < 0) ||*/ (l_tot_len > |
4490 | | p_header_size)) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ |
4491 | | opj_event_msg(p_manager, EVT_ERROR, |
4492 | | "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", |
4493 | | l_tot_len, |
4494 | | p_header_size); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */ |
4495 | | if (!JPWL_ASSUME) { |
4496 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
4497 | | return OPJ_FALSE; |
4498 | | } |
4499 | | /* we try to correct */ |
4500 | | l_tot_len = 0; |
4501 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
4502 | | "- setting Psot to %d => assuming it is the last tile\n", |
4503 | | l_tot_len); |
4504 | | } |
4505 | | }; |
4506 | | #endif /* USE_JPWL */ |
4507 | | |
4508 | | /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/ |
4509 | 16.3k | if (!l_tot_len) { |
4510 | 12.6k | opj_event_msg(p_manager, EVT_INFO, |
4511 | 12.6k | "Psot value of the current tile-part is equal to zero, " |
4512 | 12.6k | "we assuming it is the last tile-part of the codestream.\n"); |
4513 | 12.6k | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4514 | 12.6k | } |
4515 | | |
4516 | 16.3k | if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) { |
4517 | | /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */ |
4518 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
4519 | 3 | "In SOT marker, TPSot (%d) is not valid regards to the previous " |
4520 | 3 | "number of tile-part (%d), giving up\n", l_current_part, |
4521 | 3 | l_tcp->m_nb_tile_parts); |
4522 | 3 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4523 | 3 | return OPJ_FALSE; |
4524 | 3 | } |
4525 | | |
4526 | 16.3k | if (l_num_parts != |
4527 | 16.3k | 0) { /* Number of tile-part header is provided by this tile-part header */ |
4528 | 13.5k | l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction; |
4529 | | /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of |
4530 | | * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */ |
4531 | 13.5k | if (l_tcp->m_nb_tile_parts) { |
4532 | 56 | if (l_current_part >= l_tcp->m_nb_tile_parts) { |
4533 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4534 | 0 | "In SOT marker, TPSot (%d) is not valid regards to the current " |
4535 | 0 | "number of tile-part (%d), giving up\n", l_current_part, |
4536 | 0 | l_tcp->m_nb_tile_parts); |
4537 | 0 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4538 | 0 | return OPJ_FALSE; |
4539 | 0 | } |
4540 | 56 | } |
4541 | 13.5k | if (l_current_part >= l_num_parts) { |
4542 | | /* testcase 451.pdf.SIGSEGV.ce9.3723 */ |
4543 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
4544 | 3 | "In SOT marker, TPSot (%d) is not valid regards to the current " |
4545 | 3 | "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts); |
4546 | 3 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4547 | 3 | return OPJ_FALSE; |
4548 | 3 | } |
4549 | 13.5k | l_tcp->m_nb_tile_parts = l_num_parts; |
4550 | 13.5k | } |
4551 | | |
4552 | | /* If know the number of tile part header we will check if we didn't read the last*/ |
4553 | 16.3k | if (l_tcp->m_nb_tile_parts) { |
4554 | 13.5k | if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) { |
4555 | 3.83k | p_j2k->m_specific_param.m_decoder.m_can_decode = |
4556 | 3.83k | 1; /* Process the last tile-part header*/ |
4557 | 3.83k | } |
4558 | 13.5k | } |
4559 | | |
4560 | 16.3k | if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
4561 | | /* Keep the size of data to skip after this marker */ |
4562 | 3.65k | p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - |
4563 | 3.65k | 12; /* SOT_marker_size = 12 */ |
4564 | 12.6k | } else { |
4565 | | /* FIXME: need to be computed from the number of bytes remaining in the codestream */ |
4566 | 12.6k | p_j2k->m_specific_param.m_decoder.m_sot_length = 0; |
4567 | 12.6k | } |
4568 | | |
4569 | 16.3k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH; |
4570 | | |
4571 | | /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/ |
4572 | 16.3k | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { |
4573 | 16.3k | p_j2k->m_specific_param.m_decoder.m_skip_data = |
4574 | 16.3k | (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) |
4575 | 16.3k | || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) |
4576 | 16.3k | || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) |
4577 | 16.3k | || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); |
4578 | 16.3k | } else { |
4579 | 0 | assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0); |
4580 | 0 | p_j2k->m_specific_param.m_decoder.m_skip_data = |
4581 | 0 | (p_j2k->m_current_tile_number != (OPJ_UINT32) |
4582 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec); |
4583 | 0 | } |
4584 | | |
4585 | | /* Index */ |
4586 | 16.3k | if (p_j2k->cstr_index) { |
4587 | 16.3k | assert(p_j2k->cstr_index->tile_index != 00); |
4588 | 16.3k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = |
4589 | 16.3k | p_j2k->m_current_tile_number; |
4590 | 16.3k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = |
4591 | 16.3k | l_current_part; |
4592 | | |
4593 | 16.3k | if (l_num_parts != 0) { |
4594 | 13.5k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = |
4595 | 13.5k | l_num_parts; |
4596 | 13.5k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = |
4597 | 13.5k | l_num_parts; |
4598 | | |
4599 | 13.5k | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4600 | 13.4k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4601 | 13.4k | (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); |
4602 | 13.4k | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4603 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4604 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4605 | 0 | return OPJ_FALSE; |
4606 | 0 | } |
4607 | 13.4k | } else { |
4608 | 53 | opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( |
4609 | 53 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
4610 | 53 | l_num_parts * sizeof(opj_tp_index_t)); |
4611 | 53 | if (! new_tp_index) { |
4612 | 0 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
4613 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
4614 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4615 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4616 | 0 | return OPJ_FALSE; |
4617 | 0 | } |
4618 | 53 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4619 | 53 | new_tp_index; |
4620 | 53 | } |
4621 | 13.5k | } else { |
4622 | 2.76k | /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { |
4623 | | |
4624 | 2.76k | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4625 | 2.76k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; |
4626 | 2.76k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4627 | 2.76k | (opj_tp_index_t*)opj_calloc( |
4628 | 2.76k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, |
4629 | 2.76k | sizeof(opj_tp_index_t)); |
4630 | 2.76k | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4631 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
4632 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4633 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4634 | 0 | return OPJ_FALSE; |
4635 | 0 | } |
4636 | 2.76k | } |
4637 | | |
4638 | 2.76k | if (l_current_part >= |
4639 | 2.76k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) { |
4640 | 0 | opj_tp_index_t *new_tp_index; |
4641 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = |
4642 | 0 | l_current_part + 1; |
4643 | 0 | new_tp_index = (opj_tp_index_t *) opj_realloc( |
4644 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
4645 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * |
4646 | 0 | sizeof(opj_tp_index_t)); |
4647 | 0 | if (! new_tp_index) { |
4648 | 0 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
4649 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
4650 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
4651 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4652 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4653 | 0 | return OPJ_FALSE; |
4654 | 0 | } |
4655 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4656 | 0 | new_tp_index; |
4657 | 0 | } |
4658 | 2.76k | } |
4659 | | |
4660 | 2.76k | } |
4661 | | |
4662 | 16.3k | } |
4663 | | |
4664 | | /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */ |
4665 | | /* if (p_j2k->cstr_info) { |
4666 | | if (l_tcp->first) { |
4667 | | if (tileno == 0) { |
4668 | | p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13; |
4669 | | } |
4670 | | |
4671 | | p_j2k->cstr_info->tile[tileno].tileno = tileno; |
4672 | | p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12; |
4673 | | p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1; |
4674 | | p_j2k->cstr_info->tile[tileno].num_tps = numparts; |
4675 | | |
4676 | | if (numparts) { |
4677 | | p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); |
4678 | | } |
4679 | | else { |
4680 | | p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) |
4681 | | } |
4682 | | } |
4683 | | else { |
4684 | | p_j2k->cstr_info->tile[tileno].end_pos += totlen; |
4685 | | } |
4686 | | |
4687 | | p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12; |
4688 | | p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = |
4689 | | p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; |
4690 | | }*/ |
4691 | 16.3k | return OPJ_TRUE; |
4692 | 16.3k | } |
4693 | | |
4694 | | /** |
4695 | | * Write one or more PLT markers in the provided buffer |
4696 | | */ |
4697 | | static OPJ_BOOL opj_j2k_write_plt_in_memory(opj_j2k_t *p_j2k, |
4698 | | opj_tcd_marker_info_t* marker_info, |
4699 | | OPJ_BYTE * p_data, |
4700 | | OPJ_UINT32 * p_data_written, |
4701 | | opj_event_mgr_t * p_manager) |
4702 | 0 | { |
4703 | 0 | OPJ_BYTE Zplt = 0; |
4704 | 0 | OPJ_UINT16 Lplt; |
4705 | 0 | OPJ_BYTE* p_data_start = p_data; |
4706 | 0 | OPJ_BYTE* p_data_Lplt = p_data + 2; |
4707 | 0 | OPJ_UINT32 i; |
4708 | |
|
4709 | 0 | OPJ_UNUSED(p_j2k); |
4710 | |
|
4711 | 0 | opj_write_bytes(p_data, J2K_MS_PLT, 2); |
4712 | 0 | p_data += 2; |
4713 | | |
4714 | | /* Reserve space for Lplt */ |
4715 | 0 | p_data += 2; |
4716 | |
|
4717 | 0 | opj_write_bytes(p_data, Zplt, 1); |
4718 | 0 | p_data += 1; |
4719 | |
|
4720 | 0 | Lplt = 3; |
4721 | |
|
4722 | 0 | for (i = 0; i < marker_info->packet_count; i++) { |
4723 | 0 | OPJ_BYTE var_bytes[5]; |
4724 | 0 | OPJ_UINT8 var_bytes_size = 0; |
4725 | 0 | OPJ_UINT32 packet_size = marker_info->p_packet_size[i]; |
4726 | | |
4727 | | /* Packet size written in variable-length way, starting with LSB */ |
4728 | 0 | var_bytes[var_bytes_size] = (OPJ_BYTE)(packet_size & 0x7f); |
4729 | 0 | var_bytes_size ++; |
4730 | 0 | packet_size >>= 7; |
4731 | 0 | while (packet_size > 0) { |
4732 | 0 | var_bytes[var_bytes_size] = (OPJ_BYTE)((packet_size & 0x7f) | 0x80); |
4733 | 0 | var_bytes_size ++; |
4734 | 0 | packet_size >>= 7; |
4735 | 0 | } |
4736 | | |
4737 | | /* Check if that can fit in the current PLT marker. If not, finish */ |
4738 | | /* current one, and start a new one */ |
4739 | 0 | if (Lplt + var_bytes_size > 65535) { |
4740 | 0 | if (Zplt == 255) { |
4741 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4742 | 0 | "More than 255 PLT markers would be needed for current tile-part !\n"); |
4743 | 0 | return OPJ_FALSE; |
4744 | 0 | } |
4745 | | |
4746 | | /* Patch Lplt */ |
4747 | 0 | opj_write_bytes(p_data_Lplt, Lplt, 2); |
4748 | | |
4749 | | /* Start new segment */ |
4750 | 0 | opj_write_bytes(p_data, J2K_MS_PLT, 2); |
4751 | 0 | p_data += 2; |
4752 | | |
4753 | | /* Reserve space for Lplt */ |
4754 | 0 | p_data_Lplt = p_data; |
4755 | 0 | p_data += 2; |
4756 | |
|
4757 | 0 | Zplt ++; |
4758 | 0 | opj_write_bytes(p_data, Zplt, 1); |
4759 | 0 | p_data += 1; |
4760 | |
|
4761 | 0 | Lplt = 3; |
4762 | 0 | } |
4763 | | |
4764 | 0 | Lplt = (OPJ_UINT16)(Lplt + var_bytes_size); |
4765 | | |
4766 | | /* Serialize variable-length packet size, starting with MSB */ |
4767 | 0 | for (; var_bytes_size > 0; --var_bytes_size) { |
4768 | 0 | opj_write_bytes(p_data, var_bytes[var_bytes_size - 1], 1); |
4769 | 0 | p_data += 1; |
4770 | 0 | } |
4771 | 0 | } |
4772 | | |
4773 | 0 | *p_data_written = (OPJ_UINT32)(p_data - p_data_start); |
4774 | | |
4775 | | /* Patch Lplt */ |
4776 | 0 | opj_write_bytes(p_data_Lplt, Lplt, 2); |
4777 | |
|
4778 | 0 | return OPJ_TRUE; |
4779 | 0 | } |
4780 | | |
4781 | | static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, |
4782 | | opj_tcd_t * p_tile_coder, |
4783 | | OPJ_BYTE * p_data, |
4784 | | OPJ_UINT32 * p_data_written, |
4785 | | OPJ_UINT32 total_data_size, |
4786 | | const opj_stream_private_t *p_stream, |
4787 | | opj_event_mgr_t * p_manager |
4788 | | ) |
4789 | 0 | { |
4790 | 0 | opj_codestream_info_t *l_cstr_info = 00; |
4791 | 0 | OPJ_UINT32 l_remaining_data; |
4792 | 0 | opj_tcd_marker_info_t* marker_info = NULL; |
4793 | | |
4794 | | /* preconditions */ |
4795 | 0 | assert(p_j2k != 00); |
4796 | 0 | assert(p_manager != 00); |
4797 | 0 | assert(p_stream != 00); |
4798 | |
|
4799 | 0 | OPJ_UNUSED(p_stream); |
4800 | |
|
4801 | 0 | if (total_data_size < 4) { |
4802 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4803 | 0 | "Not enough bytes in output buffer to write SOD marker\n"); |
4804 | 0 | return OPJ_FALSE; |
4805 | 0 | } |
4806 | | |
4807 | 0 | opj_write_bytes(p_data, J2K_MS_SOD, |
4808 | 0 | 2); /* SOD */ |
4809 | | |
4810 | | /* make room for the EOF marker */ |
4811 | 0 | l_remaining_data = total_data_size - 4; |
4812 | | |
4813 | | /* update tile coder */ |
4814 | 0 | p_tile_coder->tp_num = |
4815 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; |
4816 | 0 | p_tile_coder->cur_tp_num = |
4817 | 0 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
4818 | | |
4819 | | /* INDEX >> */ |
4820 | | /* TODO mergeV2: check this part which use cstr_info */ |
4821 | | /*l_cstr_info = p_j2k->cstr_info; |
4822 | | if (l_cstr_info) { |
4823 | | if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { |
4824 | | //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; |
4825 | | l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; |
4826 | | } |
4827 | | else {*/ |
4828 | | /* |
4829 | | TODO |
4830 | | if |
4831 | | (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) |
4832 | | { |
4833 | | cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); |
4834 | | }*/ |
4835 | | /*}*/ |
4836 | | /* UniPG>> */ |
4837 | | #ifdef USE_JPWL |
4838 | | /* update markers struct */ |
4839 | | /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); |
4840 | | */ |
4841 | | assert(0 && "TODO"); |
4842 | | #endif /* USE_JPWL */ |
4843 | | /* <<UniPG */ |
4844 | | /*}*/ |
4845 | | /* << INDEX */ |
4846 | |
|
4847 | 0 | if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) { |
4848 | 0 | p_tile_coder->tcd_image->tiles->packno = 0; |
4849 | | #ifdef deadcode |
4850 | | if (l_cstr_info) { |
4851 | | l_cstr_info->packno = 0; |
4852 | | } |
4853 | | #endif |
4854 | 0 | } |
4855 | |
|
4856 | 0 | *p_data_written = 0; |
4857 | |
|
4858 | 0 | if (p_j2k->m_specific_param.m_encoder.m_PLT) { |
4859 | 0 | marker_info = opj_tcd_marker_info_create( |
4860 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT); |
4861 | 0 | if (marker_info == NULL) { |
4862 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4863 | 0 | "Cannot encode tile: opj_tcd_marker_info_create() failed\n"); |
4864 | 0 | return OPJ_FALSE; |
4865 | 0 | } |
4866 | 0 | } |
4867 | | |
4868 | 0 | if (l_remaining_data < |
4869 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT) { |
4870 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4871 | 0 | "Not enough bytes in output buffer to write SOD marker\n"); |
4872 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4873 | 0 | return OPJ_FALSE; |
4874 | 0 | } |
4875 | 0 | l_remaining_data -= p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT; |
4876 | |
|
4877 | 0 | if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, |
4878 | 0 | p_data + 2, |
4879 | 0 | p_data_written, l_remaining_data, l_cstr_info, |
4880 | 0 | marker_info, |
4881 | 0 | p_manager)) { |
4882 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n"); |
4883 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4884 | 0 | return OPJ_FALSE; |
4885 | 0 | } |
4886 | | |
4887 | | /* For SOD */ |
4888 | 0 | *p_data_written += 2; |
4889 | |
|
4890 | 0 | if (p_j2k->m_specific_param.m_encoder.m_PLT) { |
4891 | 0 | OPJ_UINT32 l_data_written_PLT = 0; |
4892 | 0 | OPJ_BYTE* p_PLT_buffer = (OPJ_BYTE*)opj_malloc( |
4893 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT); |
4894 | 0 | if (!p_PLT_buffer) { |
4895 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Cannot allocate memory\n"); |
4896 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4897 | 0 | return OPJ_FALSE; |
4898 | 0 | } |
4899 | 0 | if (!opj_j2k_write_plt_in_memory(p_j2k, |
4900 | 0 | marker_info, |
4901 | 0 | p_PLT_buffer, |
4902 | 0 | &l_data_written_PLT, |
4903 | 0 | p_manager)) { |
4904 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4905 | 0 | opj_free(p_PLT_buffer); |
4906 | 0 | return OPJ_FALSE; |
4907 | 0 | } |
4908 | | |
4909 | 0 | assert(l_data_written_PLT <= |
4910 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT); |
4911 | | |
4912 | | /* Move PLT marker(s) before SOD */ |
4913 | 0 | memmove(p_data + l_data_written_PLT, p_data, *p_data_written); |
4914 | 0 | memcpy(p_data, p_PLT_buffer, l_data_written_PLT); |
4915 | 0 | opj_free(p_PLT_buffer); |
4916 | 0 | *p_data_written += l_data_written_PLT; |
4917 | 0 | } |
4918 | | |
4919 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4920 | |
|
4921 | 0 | return OPJ_TRUE; |
4922 | 0 | } |
4923 | | |
4924 | | static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, |
4925 | | opj_stream_private_t *p_stream, |
4926 | | opj_event_mgr_t * p_manager |
4927 | | ) |
4928 | 17.2k | { |
4929 | 17.2k | OPJ_SIZE_T l_current_read_size; |
4930 | 17.2k | opj_codestream_index_t * l_cstr_index = 00; |
4931 | 17.2k | OPJ_BYTE ** l_current_data = 00; |
4932 | 17.2k | opj_tcp_t * l_tcp = 00; |
4933 | 17.2k | OPJ_UINT32 * l_tile_len = 00; |
4934 | 17.2k | OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; |
4935 | | |
4936 | | /* preconditions */ |
4937 | 17.2k | assert(p_j2k != 00); |
4938 | 17.2k | assert(p_manager != 00); |
4939 | 17.2k | assert(p_stream != 00); |
4940 | | |
4941 | 17.2k | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
4942 | | |
4943 | 17.2k | if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
4944 | | /* opj_stream_get_number_byte_left returns OPJ_OFF_T |
4945 | | // but we are in the last tile part, |
4946 | | // so its result will fit on OPJ_UINT32 unless we find |
4947 | | // a file with a single tile part of more than 4 GB...*/ |
4948 | 12.4k | p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)( |
4949 | 12.4k | opj_stream_get_number_byte_left(p_stream) - 2); |
4950 | 12.4k | } else { |
4951 | | /* Check to avoid pass the limit of OPJ_UINT32 */ |
4952 | 4.78k | if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) { |
4953 | 3.22k | p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; |
4954 | 3.22k | } else { |
4955 | | /* MSD: case commented to support empty SOT marker (PHR data) */ |
4956 | 1.56k | } |
4957 | 4.78k | } |
4958 | | |
4959 | 17.2k | l_current_data = &(l_tcp->m_data); |
4960 | 17.2k | l_tile_len = &l_tcp->m_data_size; |
4961 | | |
4962 | | /* Patch to support new PHR data */ |
4963 | 17.2k | if (p_j2k->m_specific_param.m_decoder.m_sot_length) { |
4964 | | /* If we are here, we'll try to read the data after allocation */ |
4965 | | /* Check enough bytes left in stream before allocation */ |
4966 | 15.6k | if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > |
4967 | 15.6k | opj_stream_get_number_byte_left(p_stream)) { |
4968 | 164 | if (p_j2k->m_cp.strict) { |
4969 | 164 | opj_event_msg(p_manager, EVT_ERROR, |
4970 | 164 | "Tile part length size inconsistent with stream length\n"); |
4971 | 164 | return OPJ_FALSE; |
4972 | 164 | } else { |
4973 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
4974 | 0 | "Tile part length size inconsistent with stream length\n"); |
4975 | 0 | } |
4976 | 164 | } |
4977 | 15.4k | if (p_j2k->m_specific_param.m_decoder.m_sot_length > |
4978 | 15.4k | UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) { |
4979 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4980 | 0 | "p_j2k->m_specific_param.m_decoder.m_sot_length > " |
4981 | 0 | "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA"); |
4982 | 0 | return OPJ_FALSE; |
4983 | 0 | } |
4984 | | /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */ |
4985 | | /* do so that opj_mqc_init_dec_common() can safely add a synthetic */ |
4986 | | /* 0xFFFF marker. */ |
4987 | 15.4k | if (! *l_current_data) { |
4988 | | /* LH: oddly enough, in this path, l_tile_len!=0. |
4989 | | * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...). |
4990 | | */ |
4991 | 15.3k | *l_current_data = (OPJ_BYTE*) opj_malloc( |
4992 | 15.3k | p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA); |
4993 | 15.3k | } else { |
4994 | 181 | OPJ_BYTE *l_new_current_data; |
4995 | 181 | if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - |
4996 | 181 | p_j2k->m_specific_param.m_decoder.m_sot_length) { |
4997 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4998 | 0 | "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - " |
4999 | 0 | "p_j2k->m_specific_param.m_decoder.m_sot_length"); |
5000 | 0 | return OPJ_FALSE; |
5001 | 0 | } |
5002 | | |
5003 | 181 | l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, |
5004 | 181 | *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length + |
5005 | 181 | OPJ_COMMON_CBLK_DATA_EXTRA); |
5006 | 181 | if (! l_new_current_data) { |
5007 | 0 | opj_free(*l_current_data); |
5008 | | /*nothing more is done as l_current_data will be set to null, and just |
5009 | | afterward we enter in the error path |
5010 | | and the actual tile_len is updated (committed) at the end of the |
5011 | | function. */ |
5012 | 0 | } |
5013 | 181 | *l_current_data = l_new_current_data; |
5014 | 181 | } |
5015 | | |
5016 | 15.4k | if (*l_current_data == 00) { |
5017 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n"); |
5018 | 0 | return OPJ_FALSE; |
5019 | 0 | } |
5020 | 15.4k | } else { |
5021 | 1.59k | l_sot_length_pb_detected = OPJ_TRUE; |
5022 | 1.59k | } |
5023 | | |
5024 | | /* Index */ |
5025 | 17.0k | l_cstr_index = p_j2k->cstr_index; |
5026 | 17.0k | if (l_cstr_index) { |
5027 | 17.0k | OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; |
5028 | | |
5029 | 17.0k | OPJ_UINT32 l_current_tile_part = |
5030 | 17.0k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; |
5031 | 17.0k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header |
5032 | 17.0k | = |
5033 | 17.0k | l_current_pos; |
5034 | 17.0k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos |
5035 | 17.0k | = |
5036 | 17.0k | l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; |
5037 | | |
5038 | 17.0k | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
5039 | 17.0k | l_cstr_index, |
5040 | 17.0k | J2K_MS_SOD, |
5041 | 17.0k | l_current_pos, |
5042 | 17.0k | p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) { |
5043 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); |
5044 | 0 | return OPJ_FALSE; |
5045 | 0 | } |
5046 | | |
5047 | | /*l_cstr_index->packno = 0;*/ |
5048 | 17.0k | } |
5049 | | |
5050 | | /* Patch to support new PHR data */ |
5051 | 17.0k | if (!l_sot_length_pb_detected) { |
5052 | 15.4k | l_current_read_size = opj_stream_read_data( |
5053 | 15.4k | p_stream, |
5054 | 15.4k | *l_current_data + *l_tile_len, |
5055 | 15.4k | p_j2k->m_specific_param.m_decoder.m_sot_length, |
5056 | 15.4k | p_manager); |
5057 | 15.4k | } else { |
5058 | 1.59k | l_current_read_size = 0; |
5059 | 1.59k | } |
5060 | | |
5061 | 17.0k | if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
5062 | 0 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
5063 | 17.0k | } else { |
5064 | 17.0k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
5065 | 17.0k | } |
5066 | | |
5067 | 17.0k | *l_tile_len += (OPJ_UINT32)l_current_read_size; |
5068 | | |
5069 | 17.0k | return OPJ_TRUE; |
5070 | 17.0k | } |
5071 | | |
5072 | | static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, |
5073 | | OPJ_UINT32 p_tile_no, |
5074 | | OPJ_UINT32 p_comp_no, |
5075 | | OPJ_UINT32 nb_comps, |
5076 | | opj_stream_private_t *p_stream, |
5077 | | opj_event_mgr_t * p_manager |
5078 | | ) |
5079 | 0 | { |
5080 | 0 | OPJ_BYTE * l_current_data = 00; |
5081 | 0 | OPJ_UINT32 l_rgn_size; |
5082 | 0 | opj_cp_t *l_cp = 00; |
5083 | 0 | opj_tcp_t *l_tcp = 00; |
5084 | 0 | opj_tccp_t *l_tccp = 00; |
5085 | 0 | OPJ_UINT32 l_comp_room; |
5086 | | |
5087 | | /* preconditions */ |
5088 | 0 | assert(p_j2k != 00); |
5089 | 0 | assert(p_manager != 00); |
5090 | 0 | assert(p_stream != 00); |
5091 | |
|
5092 | 0 | l_cp = &(p_j2k->m_cp); |
5093 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
5094 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
5095 | |
|
5096 | 0 | if (nb_comps <= 256) { |
5097 | 0 | l_comp_room = 1; |
5098 | 0 | } else { |
5099 | 0 | l_comp_room = 2; |
5100 | 0 | } |
5101 | |
|
5102 | 0 | l_rgn_size = 6 + l_comp_room; |
5103 | |
|
5104 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
5105 | |
|
5106 | 0 | opj_write_bytes(l_current_data, J2K_MS_RGN, |
5107 | 0 | 2); /* RGN */ |
5108 | 0 | l_current_data += 2; |
5109 | |
|
5110 | 0 | opj_write_bytes(l_current_data, l_rgn_size - 2, |
5111 | 0 | 2); /* Lrgn */ |
5112 | 0 | l_current_data += 2; |
5113 | |
|
5114 | 0 | opj_write_bytes(l_current_data, p_comp_no, |
5115 | 0 | l_comp_room); /* Crgn */ |
5116 | 0 | l_current_data += l_comp_room; |
5117 | |
|
5118 | 0 | opj_write_bytes(l_current_data, 0, |
5119 | 0 | 1); /* Srgn */ |
5120 | 0 | ++l_current_data; |
5121 | |
|
5122 | 0 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift, |
5123 | 0 | 1); /* SPrgn */ |
5124 | 0 | ++l_current_data; |
5125 | |
|
5126 | 0 | if (opj_stream_write_data(p_stream, |
5127 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_rgn_size, |
5128 | 0 | p_manager) != l_rgn_size) { |
5129 | 0 | return OPJ_FALSE; |
5130 | 0 | } |
5131 | | |
5132 | 0 | return OPJ_TRUE; |
5133 | 0 | } |
5134 | | |
5135 | | static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, |
5136 | | opj_stream_private_t *p_stream, |
5137 | | opj_event_mgr_t * p_manager |
5138 | | ) |
5139 | 0 | { |
5140 | | /* preconditions */ |
5141 | 0 | assert(p_j2k != 00); |
5142 | 0 | assert(p_manager != 00); |
5143 | 0 | assert(p_stream != 00); |
5144 | |
|
5145 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data, |
5146 | 0 | J2K_MS_EOC, 2); /* EOC */ |
5147 | | |
5148 | | /* UniPG>> */ |
5149 | | #ifdef USE_JPWL |
5150 | | /* update markers struct */ |
5151 | | /* |
5152 | | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); |
5153 | | */ |
5154 | | #endif /* USE_JPWL */ |
5155 | |
|
5156 | 0 | if (opj_stream_write_data(p_stream, |
5157 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, 2, p_manager) != 2) { |
5158 | 0 | return OPJ_FALSE; |
5159 | 0 | } |
5160 | | |
5161 | 0 | if (! opj_stream_flush(p_stream, p_manager)) { |
5162 | 0 | return OPJ_FALSE; |
5163 | 0 | } |
5164 | | |
5165 | 0 | return OPJ_TRUE; |
5166 | 0 | } |
5167 | | |
5168 | | /** |
5169 | | * Reads a RGN marker (Region Of Interest) |
5170 | | * |
5171 | | * @param p_header_data the data contained in the POC box. |
5172 | | * @param p_j2k the jpeg2000 codec. |
5173 | | * @param p_header_size the size of the data contained in the POC marker. |
5174 | | * @param p_manager the user event manager. |
5175 | | */ |
5176 | | static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, |
5177 | | OPJ_BYTE * p_header_data, |
5178 | | OPJ_UINT32 p_header_size, |
5179 | | opj_event_mgr_t * p_manager |
5180 | | ) |
5181 | 2.45k | { |
5182 | 2.45k | OPJ_UINT32 l_nb_comp; |
5183 | 2.45k | opj_image_t * l_image = 00; |
5184 | | |
5185 | 2.45k | opj_cp_t *l_cp = 00; |
5186 | 2.45k | opj_tcp_t *l_tcp = 00; |
5187 | 2.45k | OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; |
5188 | | |
5189 | | /* preconditions*/ |
5190 | 2.45k | assert(p_header_data != 00); |
5191 | 2.45k | assert(p_j2k != 00); |
5192 | 2.45k | assert(p_manager != 00); |
5193 | | |
5194 | 2.45k | l_image = p_j2k->m_private_image; |
5195 | 2.45k | l_nb_comp = l_image->numcomps; |
5196 | | |
5197 | 2.45k | if (l_nb_comp <= 256) { |
5198 | 2.20k | l_comp_room = 1; |
5199 | 2.20k | } else { |
5200 | 259 | l_comp_room = 2; |
5201 | 259 | } |
5202 | | |
5203 | 2.45k | if (p_header_size != 2 + l_comp_room) { |
5204 | 8 | opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n"); |
5205 | 8 | return OPJ_FALSE; |
5206 | 8 | } |
5207 | | |
5208 | 2.45k | l_cp = &(p_j2k->m_cp); |
5209 | 2.45k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
5210 | 1.59k | &l_cp->tcps[p_j2k->m_current_tile_number] : |
5211 | 2.45k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5212 | | |
5213 | 2.45k | opj_read_bytes(p_header_data, &l_comp_no, l_comp_room); /* Crgn */ |
5214 | 2.45k | p_header_data += l_comp_room; |
5215 | 2.45k | opj_read_bytes(p_header_data, &l_roi_sty, |
5216 | 2.45k | 1); /* Srgn */ |
5217 | 2.45k | ++p_header_data; |
5218 | | |
5219 | | #ifdef USE_JPWL |
5220 | | if (l_cp->correct) { |
5221 | | /* totlen is negative or larger than the bytes left!!! */ |
5222 | | if (l_comp_room >= l_nb_comp) { |
5223 | | opj_event_msg(p_manager, EVT_ERROR, |
5224 | | "JPWL: bad component number in RGN (%d when there are only %d)\n", |
5225 | | l_comp_room, l_nb_comp); |
5226 | | if (!JPWL_ASSUME) { |
5227 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
5228 | | return OPJ_FALSE; |
5229 | | } |
5230 | | } |
5231 | | }; |
5232 | | #endif /* USE_JPWL */ |
5233 | | |
5234 | | /* testcase 3635.pdf.asan.77.2930 */ |
5235 | 2.45k | if (l_comp_no >= l_nb_comp) { |
5236 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
5237 | 6 | "bad component number in RGN (%d when there are only %d)\n", |
5238 | 6 | l_comp_no, l_nb_comp); |
5239 | 6 | return OPJ_FALSE; |
5240 | 6 | } |
5241 | | |
5242 | 2.44k | opj_read_bytes(p_header_data, |
5243 | 2.44k | (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1); /* SPrgn */ |
5244 | 2.44k | ++p_header_data; |
5245 | | |
5246 | 2.44k | return OPJ_TRUE; |
5247 | | |
5248 | 2.45k | } |
5249 | | |
5250 | | static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp) |
5251 | 0 | { |
5252 | 0 | return (OPJ_FLOAT32)((p_tcp->m_nb_tile_parts - 1) * 14); |
5253 | 0 | } |
5254 | | |
5255 | | static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp) |
5256 | 0 | { |
5257 | 0 | (void)p_tcp; |
5258 | 0 | return 0; |
5259 | 0 | } |
5260 | | |
5261 | | static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, |
5262 | | opj_stream_private_t *p_stream, |
5263 | | opj_event_mgr_t * p_manager) |
5264 | 0 | { |
5265 | 0 | opj_cp_t * l_cp = 00; |
5266 | 0 | opj_image_t * l_image = 00; |
5267 | 0 | opj_tcp_t * l_tcp = 00; |
5268 | 0 | opj_image_comp_t * l_img_comp = 00; |
5269 | |
|
5270 | 0 | OPJ_UINT32 i, j, k; |
5271 | 0 | OPJ_INT32 l_x0, l_y0, l_x1, l_y1; |
5272 | 0 | OPJ_FLOAT32 * l_rates = 0; |
5273 | 0 | OPJ_FLOAT32 l_sot_remove; |
5274 | 0 | OPJ_UINT32 l_bits_empty, l_size_pixel; |
5275 | 0 | OPJ_UINT64 l_tile_size = 0; |
5276 | 0 | OPJ_UINT32 l_last_res; |
5277 | 0 | OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00; |
5278 | | |
5279 | | /* preconditions */ |
5280 | 0 | assert(p_j2k != 00); |
5281 | 0 | assert(p_manager != 00); |
5282 | 0 | assert(p_stream != 00); |
5283 | |
|
5284 | 0 | OPJ_UNUSED(p_manager); |
5285 | |
|
5286 | 0 | l_cp = &(p_j2k->m_cp); |
5287 | 0 | l_image = p_j2k->m_private_image; |
5288 | 0 | l_tcp = l_cp->tcps; |
5289 | |
|
5290 | 0 | l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; |
5291 | 0 | l_size_pixel = l_image->numcomps * l_image->comps->prec; |
5292 | 0 | l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)( |
5293 | 0 | l_cp->th * l_cp->tw); |
5294 | |
|
5295 | 0 | if (l_cp->m_specific_param.m_enc.m_tp_on) { |
5296 | 0 | l_tp_stride_func = opj_j2k_get_tp_stride; |
5297 | 0 | } else { |
5298 | 0 | l_tp_stride_func = opj_j2k_get_default_stride; |
5299 | 0 | } |
5300 | |
|
5301 | 0 | for (i = 0; i < l_cp->th; ++i) { |
5302 | 0 | for (j = 0; j < l_cp->tw; ++j) { |
5303 | 0 | OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / |
5304 | 0 | (OPJ_FLOAT32)l_tcp->numlayers; |
5305 | | |
5306 | | /* 4 borders of the tile rescale on the image if necessary */ |
5307 | 0 | l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), |
5308 | 0 | (OPJ_INT32)l_image->x0); |
5309 | 0 | l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), |
5310 | 0 | (OPJ_INT32)l_image->y0); |
5311 | 0 | l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), |
5312 | 0 | (OPJ_INT32)l_image->x1); |
5313 | 0 | l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), |
5314 | 0 | (OPJ_INT32)l_image->y1); |
5315 | |
|
5316 | 0 | l_rates = l_tcp->rates; |
5317 | | |
5318 | | /* Modification of the RATE >> */ |
5319 | 0 | for (k = 0; k < l_tcp->numlayers; ++k) { |
5320 | 0 | if (*l_rates > 0.0f) { |
5321 | 0 | *l_rates = (OPJ_FLOAT32)(((OPJ_FLOAT64)l_size_pixel * (OPJ_UINT32)( |
5322 | 0 | l_x1 - l_x0) * |
5323 | 0 | (OPJ_UINT32)(l_y1 - l_y0)) |
5324 | 0 | / ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)) |
5325 | 0 | - |
5326 | 0 | l_offset; |
5327 | 0 | } |
5328 | |
|
5329 | 0 | ++l_rates; |
5330 | 0 | } |
5331 | |
|
5332 | 0 | ++l_tcp; |
5333 | |
|
5334 | 0 | } |
5335 | 0 | } |
5336 | |
|
5337 | 0 | l_tcp = l_cp->tcps; |
5338 | |
|
5339 | 0 | for (i = 0; i < l_cp->th; ++i) { |
5340 | 0 | for (j = 0; j < l_cp->tw; ++j) { |
5341 | 0 | l_rates = l_tcp->rates; |
5342 | |
|
5343 | 0 | if (*l_rates > 0.0f) { |
5344 | 0 | *l_rates -= l_sot_remove; |
5345 | |
|
5346 | 0 | if (*l_rates < 30.0f) { |
5347 | 0 | *l_rates = 30.0f; |
5348 | 0 | } |
5349 | 0 | } |
5350 | |
|
5351 | 0 | ++l_rates; |
5352 | |
|
5353 | 0 | l_last_res = l_tcp->numlayers - 1; |
5354 | |
|
5355 | 0 | for (k = 1; k < l_last_res; ++k) { |
5356 | |
|
5357 | 0 | if (*l_rates > 0.0f) { |
5358 | 0 | *l_rates -= l_sot_remove; |
5359 | |
|
5360 | 0 | if (*l_rates < * (l_rates - 1) + 10.0f) { |
5361 | 0 | *l_rates = (*(l_rates - 1)) + 20.0f; |
5362 | 0 | } |
5363 | 0 | } |
5364 | |
|
5365 | 0 | ++l_rates; |
5366 | 0 | } |
5367 | |
|
5368 | 0 | if (*l_rates > 0.0f) { |
5369 | 0 | *l_rates -= (l_sot_remove + 2.f); |
5370 | |
|
5371 | 0 | if (*l_rates < * (l_rates - 1) + 10.0f) { |
5372 | 0 | *l_rates = (*(l_rates - 1)) + 20.0f; |
5373 | 0 | } |
5374 | 0 | } |
5375 | |
|
5376 | 0 | ++l_tcp; |
5377 | 0 | } |
5378 | 0 | } |
5379 | |
|
5380 | 0 | l_img_comp = l_image->comps; |
5381 | 0 | l_tile_size = 0; |
5382 | |
|
5383 | 0 | for (i = 0; i < l_image->numcomps; ++i) { |
5384 | 0 | l_tile_size += (OPJ_UINT64)opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx) |
5385 | 0 | * |
5386 | 0 | opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy) |
5387 | 0 | * |
5388 | 0 | l_img_comp->prec; |
5389 | |
|
5390 | 0 | ++l_img_comp; |
5391 | 0 | } |
5392 | | |
5393 | | /* TODO: where does this magic value come from ? */ |
5394 | | /* This used to be 1.3 / 8, but with random data and very small code */ |
5395 | | /* block sizes, this is not enough. For example with */ |
5396 | | /* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */ |
5397 | | /* TODO revise this to take into account the overhead linked to the */ |
5398 | | /* number of packets and number of code blocks in packets */ |
5399 | 0 | l_tile_size = (OPJ_UINT64)((double)l_tile_size * 1.4 / 8); |
5400 | | |
5401 | | /* Arbitrary amount to make the following work: */ |
5402 | | /* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */ |
5403 | 0 | l_tile_size += 500; |
5404 | |
|
5405 | 0 | l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k); |
5406 | |
|
5407 | 0 | if (l_tile_size > UINT_MAX) { |
5408 | 0 | l_tile_size = UINT_MAX; |
5409 | 0 | } |
5410 | |
|
5411 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = (OPJ_UINT32)l_tile_size; |
5412 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = |
5413 | 0 | (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); |
5414 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { |
5415 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
5416 | 0 | "Not enough memory to allocate m_encoded_tile_data. %u MB required\n", |
5417 | 0 | (OPJ_UINT32)(l_tile_size / 1024 / 1024)); |
5418 | 0 | return OPJ_FALSE; |
5419 | 0 | } |
5420 | | |
5421 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
5422 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = |
5423 | 0 | (OPJ_BYTE *) opj_malloc(6 * |
5424 | 0 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
5425 | 0 | if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
5426 | 0 | return OPJ_FALSE; |
5427 | 0 | } |
5428 | | |
5429 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = |
5430 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; |
5431 | 0 | } |
5432 | | |
5433 | 0 | return OPJ_TRUE; |
5434 | 0 | } |
5435 | | |
5436 | | #if 0 |
5437 | | static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, |
5438 | | opj_stream_private_t *p_stream, |
5439 | | opj_event_mgr_t * p_manager) |
5440 | | { |
5441 | | OPJ_UINT32 i; |
5442 | | opj_tcd_t * l_tcd = 00; |
5443 | | OPJ_UINT32 l_nb_tiles; |
5444 | | opj_tcp_t * l_tcp = 00; |
5445 | | OPJ_BOOL l_success; |
5446 | | |
5447 | | /* preconditions */ |
5448 | | assert(p_j2k != 00); |
5449 | | assert(p_manager != 00); |
5450 | | assert(p_stream != 00); |
5451 | | |
5452 | | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
5453 | | l_tcp = p_j2k->m_cp.tcps; |
5454 | | |
5455 | | l_tcd = opj_tcd_create(OPJ_TRUE); |
5456 | | if (l_tcd == 00) { |
5457 | | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
5458 | | return OPJ_FALSE; |
5459 | | } |
5460 | | |
5461 | | for (i = 0; i < l_nb_tiles; ++i) { |
5462 | | if (l_tcp->m_data) { |
5463 | | if (! opj_tcd_init_decode_tile(l_tcd, i)) { |
5464 | | opj_tcd_destroy(l_tcd); |
5465 | | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
5466 | | return OPJ_FALSE; |
5467 | | } |
5468 | | |
5469 | | l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, |
5470 | | p_j2k->cstr_index); |
5471 | | /* cleanup */ |
5472 | | |
5473 | | if (! l_success) { |
5474 | | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
5475 | | break; |
5476 | | } |
5477 | | } |
5478 | | |
5479 | | opj_j2k_tcp_destroy(l_tcp); |
5480 | | ++l_tcp; |
5481 | | } |
5482 | | |
5483 | | opj_tcd_destroy(l_tcd); |
5484 | | return OPJ_TRUE; |
5485 | | } |
5486 | | #endif |
5487 | | |
5488 | | static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, |
5489 | | struct opj_stream_private *p_stream, |
5490 | | struct opj_event_mgr * p_manager) |
5491 | 0 | { |
5492 | | /* preconditions */ |
5493 | 0 | assert(p_j2k != 00); |
5494 | 0 | assert(p_manager != 00); |
5495 | 0 | assert(p_stream != 00); |
5496 | |
|
5497 | 0 | OPJ_UNUSED(p_manager); |
5498 | |
|
5499 | 0 | p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream); |
5500 | |
|
5501 | 0 | return OPJ_TRUE; |
5502 | 0 | } |
5503 | | |
5504 | | static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, |
5505 | | struct opj_stream_private *p_stream, |
5506 | | struct opj_event_mgr * p_manager) |
5507 | 0 | { |
5508 | 0 | OPJ_UINT32 i; |
5509 | 0 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
5510 | 0 | opj_mct_data_t * l_mct_record; |
5511 | 0 | opj_tcp_t * l_tcp; |
5512 | | |
5513 | | /* preconditions */ |
5514 | 0 | assert(p_j2k != 00); |
5515 | 0 | assert(p_stream != 00); |
5516 | 0 | assert(p_manager != 00); |
5517 | |
|
5518 | 0 | if (! opj_j2k_write_cbd(p_j2k, p_stream, p_manager)) { |
5519 | 0 | return OPJ_FALSE; |
5520 | 0 | } |
5521 | | |
5522 | 0 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
5523 | 0 | l_mct_record = l_tcp->m_mct_records; |
5524 | |
|
5525 | 0 | for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { |
5526 | |
|
5527 | 0 | if (! opj_j2k_write_mct_record(p_j2k, l_mct_record, p_stream, p_manager)) { |
5528 | 0 | return OPJ_FALSE; |
5529 | 0 | } |
5530 | | |
5531 | 0 | ++l_mct_record; |
5532 | 0 | } |
5533 | | |
5534 | 0 | l_mcc_record = l_tcp->m_mcc_records; |
5535 | |
|
5536 | 0 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5537 | |
|
5538 | 0 | if (! opj_j2k_write_mcc_record(p_j2k, l_mcc_record, p_stream, p_manager)) { |
5539 | 0 | return OPJ_FALSE; |
5540 | 0 | } |
5541 | | |
5542 | 0 | ++l_mcc_record; |
5543 | 0 | } |
5544 | | |
5545 | 0 | if (! opj_j2k_write_mco(p_j2k, p_stream, p_manager)) { |
5546 | 0 | return OPJ_FALSE; |
5547 | 0 | } |
5548 | | |
5549 | 0 | return OPJ_TRUE; |
5550 | 0 | } |
5551 | | |
5552 | | static OPJ_BOOL opj_j2k_write_all_coc( |
5553 | | opj_j2k_t *p_j2k, |
5554 | | struct opj_stream_private *p_stream, |
5555 | | struct opj_event_mgr * p_manager) |
5556 | 0 | { |
5557 | 0 | OPJ_UINT32 compno; |
5558 | | |
5559 | | /* preconditions */ |
5560 | 0 | assert(p_j2k != 00); |
5561 | 0 | assert(p_manager != 00); |
5562 | 0 | assert(p_stream != 00); |
5563 | |
|
5564 | 0 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5565 | | /* cod is first component of first tile */ |
5566 | 0 | if (! opj_j2k_compare_coc(p_j2k, 0, compno)) { |
5567 | 0 | if (! opj_j2k_write_coc(p_j2k, compno, p_stream, p_manager)) { |
5568 | 0 | return OPJ_FALSE; |
5569 | 0 | } |
5570 | 0 | } |
5571 | 0 | } |
5572 | | |
5573 | 0 | return OPJ_TRUE; |
5574 | 0 | } |
5575 | | |
5576 | | static OPJ_BOOL opj_j2k_write_all_qcc( |
5577 | | opj_j2k_t *p_j2k, |
5578 | | struct opj_stream_private *p_stream, |
5579 | | struct opj_event_mgr * p_manager) |
5580 | 0 | { |
5581 | 0 | OPJ_UINT32 compno; |
5582 | | |
5583 | | /* preconditions */ |
5584 | 0 | assert(p_j2k != 00); |
5585 | 0 | assert(p_manager != 00); |
5586 | 0 | assert(p_stream != 00); |
5587 | |
|
5588 | 0 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5589 | | /* qcd is first component of first tile */ |
5590 | 0 | if (! opj_j2k_compare_qcc(p_j2k, 0, compno)) { |
5591 | 0 | if (! opj_j2k_write_qcc(p_j2k, compno, p_stream, p_manager)) { |
5592 | 0 | return OPJ_FALSE; |
5593 | 0 | } |
5594 | 0 | } |
5595 | 0 | } |
5596 | 0 | return OPJ_TRUE; |
5597 | 0 | } |
5598 | | |
5599 | | static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, |
5600 | | struct opj_stream_private *p_stream, |
5601 | | struct opj_event_mgr * p_manager) |
5602 | 0 | { |
5603 | 0 | OPJ_UINT32 compno; |
5604 | 0 | const opj_tccp_t *l_tccp = 00; |
5605 | | |
5606 | | /* preconditions */ |
5607 | 0 | assert(p_j2k != 00); |
5608 | 0 | assert(p_manager != 00); |
5609 | 0 | assert(p_stream != 00); |
5610 | |
|
5611 | 0 | l_tccp = p_j2k->m_cp.tcps->tccps; |
5612 | |
|
5613 | 0 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5614 | 0 | if (l_tccp->roishift) { |
5615 | |
|
5616 | 0 | if (! opj_j2k_write_rgn(p_j2k, 0, compno, p_j2k->m_private_image->numcomps, |
5617 | 0 | p_stream, p_manager)) { |
5618 | 0 | return OPJ_FALSE; |
5619 | 0 | } |
5620 | 0 | } |
5621 | | |
5622 | 0 | ++l_tccp; |
5623 | 0 | } |
5624 | | |
5625 | 0 | return OPJ_TRUE; |
5626 | 0 | } |
5627 | | |
5628 | | static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, |
5629 | | struct opj_stream_private *p_stream, |
5630 | | struct opj_event_mgr * p_manager) |
5631 | 0 | { |
5632 | 0 | opj_codestream_index_t * l_cstr_index = 00; |
5633 | | |
5634 | | /* preconditions */ |
5635 | 0 | assert(p_j2k != 00); |
5636 | 0 | assert(p_manager != 00); |
5637 | 0 | assert(p_stream != 00); |
5638 | |
|
5639 | 0 | OPJ_UNUSED(p_manager); |
5640 | |
|
5641 | 0 | l_cstr_index = p_j2k->cstr_index; |
5642 | 0 | if (l_cstr_index) { |
5643 | 0 | l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream); |
5644 | | /* UniPG>> */ |
5645 | | /* The following adjustment is done to adjust the codestream size */ |
5646 | | /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ |
5647 | | /* the first bunch of bytes is not in the codestream */ |
5648 | 0 | l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start; |
5649 | | /* <<UniPG */ |
5650 | 0 | } |
5651 | |
|
5652 | | #ifdef USE_JPWL |
5653 | | /* preparation of JPWL marker segments */ |
5654 | | #if 0 |
5655 | | if (cp->epc_on) { |
5656 | | |
5657 | | /* encode according to JPWL */ |
5658 | | jpwl_encode(p_j2k, p_stream, image); |
5659 | | |
5660 | | } |
5661 | | #endif |
5662 | | assert(0 && "TODO"); |
5663 | | #endif /* USE_JPWL */ |
5664 | |
|
5665 | 0 | return OPJ_TRUE; |
5666 | 0 | } |
5667 | | |
5668 | | static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, |
5669 | | opj_stream_private_t *p_stream, |
5670 | | OPJ_UINT32 *output_marker, |
5671 | | opj_event_mgr_t * p_manager |
5672 | | ) |
5673 | 114k | { |
5674 | 114k | OPJ_UINT32 l_unknown_marker; |
5675 | 114k | const opj_dec_memory_marker_handler_t * l_marker_handler; |
5676 | 114k | OPJ_UINT32 l_size_unk = 2; |
5677 | | |
5678 | | /* preconditions*/ |
5679 | 114k | assert(p_j2k != 00); |
5680 | 114k | assert(p_manager != 00); |
5681 | 114k | assert(p_stream != 00); |
5682 | | |
5683 | 114k | opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); |
5684 | | |
5685 | 3.90M | for (;;) { |
5686 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
5687 | 3.90M | if (opj_stream_read_data(p_stream, |
5688 | 3.90M | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
5689 | 322 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
5690 | 322 | return OPJ_FALSE; |
5691 | 322 | } |
5692 | | |
5693 | | /* read 2 bytes as the new marker ID*/ |
5694 | 3.90M | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
5695 | 3.90M | &l_unknown_marker, 2); |
5696 | | |
5697 | 3.90M | if (!(l_unknown_marker < 0xff00)) { |
5698 | | |
5699 | | /* Get the marker handler from the marker ID*/ |
5700 | 1.22M | l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); |
5701 | | |
5702 | 1.22M | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
5703 | 33 | opj_event_msg(p_manager, EVT_ERROR, |
5704 | 33 | "Marker is not compliant with its position\n"); |
5705 | 33 | return OPJ_FALSE; |
5706 | 1.22M | } else { |
5707 | 1.22M | if (l_marker_handler->id != J2K_MS_UNK) { |
5708 | | /* Add the marker to the codestream index*/ |
5709 | 113k | if (l_marker_handler->id != J2K_MS_SOT) { |
5710 | 111k | OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, |
5711 | 111k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, |
5712 | 111k | l_size_unk); |
5713 | 111k | if (res == OPJ_FALSE) { |
5714 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
5715 | 0 | return OPJ_FALSE; |
5716 | 0 | } |
5717 | 111k | } |
5718 | 113k | break; /* next marker is known and well located */ |
5719 | 1.11M | } else { |
5720 | 1.11M | l_size_unk += 2; |
5721 | 1.11M | } |
5722 | 1.22M | } |
5723 | 1.22M | } |
5724 | 3.90M | } |
5725 | | |
5726 | 113k | *output_marker = l_marker_handler->id ; |
5727 | | |
5728 | 113k | return OPJ_TRUE; |
5729 | 114k | } |
5730 | | |
5731 | | static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, |
5732 | | opj_mct_data_t * p_mct_record, |
5733 | | struct opj_stream_private *p_stream, |
5734 | | struct opj_event_mgr * p_manager) |
5735 | 0 | { |
5736 | 0 | OPJ_UINT32 l_mct_size; |
5737 | 0 | OPJ_BYTE * l_current_data = 00; |
5738 | 0 | OPJ_UINT32 l_tmp; |
5739 | | |
5740 | | /* preconditions */ |
5741 | 0 | assert(p_j2k != 00); |
5742 | 0 | assert(p_manager != 00); |
5743 | 0 | assert(p_stream != 00); |
5744 | |
|
5745 | 0 | l_mct_size = 10 + p_mct_record->m_data_size; |
5746 | |
|
5747 | 0 | if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
5748 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
5749 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size); |
5750 | 0 | if (! new_header_tile_data) { |
5751 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
5752 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
5753 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
5754 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n"); |
5755 | 0 | return OPJ_FALSE; |
5756 | 0 | } |
5757 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
5758 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; |
5759 | 0 | } |
5760 | | |
5761 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
5762 | |
|
5763 | 0 | opj_write_bytes(l_current_data, J2K_MS_MCT, |
5764 | 0 | 2); /* MCT */ |
5765 | 0 | l_current_data += 2; |
5766 | |
|
5767 | 0 | opj_write_bytes(l_current_data, l_mct_size - 2, |
5768 | 0 | 2); /* Lmct */ |
5769 | 0 | l_current_data += 2; |
5770 | |
|
5771 | 0 | opj_write_bytes(l_current_data, 0, |
5772 | 0 | 2); /* Zmct */ |
5773 | 0 | l_current_data += 2; |
5774 | | |
5775 | | /* only one marker atm */ |
5776 | 0 | l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | |
5777 | 0 | (p_mct_record->m_element_type << 10); |
5778 | |
|
5779 | 0 | opj_write_bytes(l_current_data, l_tmp, 2); |
5780 | 0 | l_current_data += 2; |
5781 | |
|
5782 | 0 | opj_write_bytes(l_current_data, 0, |
5783 | 0 | 2); /* Ymct */ |
5784 | 0 | l_current_data += 2; |
5785 | |
|
5786 | 0 | memcpy(l_current_data, p_mct_record->m_data, p_mct_record->m_data_size); |
5787 | |
|
5788 | 0 | if (opj_stream_write_data(p_stream, |
5789 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size, |
5790 | 0 | p_manager) != l_mct_size) { |
5791 | 0 | return OPJ_FALSE; |
5792 | 0 | } |
5793 | | |
5794 | 0 | return OPJ_TRUE; |
5795 | 0 | } |
5796 | | |
5797 | | /** |
5798 | | * Reads a MCT marker (Multiple Component Transform) |
5799 | | * |
5800 | | * @param p_header_data the data contained in the MCT box. |
5801 | | * @param p_j2k the jpeg2000 codec. |
5802 | | * @param p_header_size the size of the data contained in the MCT marker. |
5803 | | * @param p_manager the user event manager. |
5804 | | */ |
5805 | | static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, |
5806 | | OPJ_BYTE * p_header_data, |
5807 | | OPJ_UINT32 p_header_size, |
5808 | | opj_event_mgr_t * p_manager |
5809 | | ) |
5810 | 10.2k | { |
5811 | 10.2k | OPJ_UINT32 i; |
5812 | 10.2k | opj_tcp_t *l_tcp = 00; |
5813 | 10.2k | OPJ_UINT32 l_tmp; |
5814 | 10.2k | OPJ_UINT32 l_indix; |
5815 | 10.2k | opj_mct_data_t * l_mct_data; |
5816 | | |
5817 | | /* preconditions */ |
5818 | 10.2k | assert(p_header_data != 00); |
5819 | 10.2k | assert(p_j2k != 00); |
5820 | | |
5821 | 10.2k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
5822 | 91 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
5823 | 10.2k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5824 | | |
5825 | 10.2k | if (p_header_size < 2) { |
5826 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5827 | 4 | return OPJ_FALSE; |
5828 | 4 | } |
5829 | | |
5830 | | /* first marker */ |
5831 | 10.2k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmct */ |
5832 | 10.2k | p_header_data += 2; |
5833 | 10.2k | if (l_tmp != 0) { |
5834 | 1.91k | opj_event_msg(p_manager, EVT_WARNING, |
5835 | 1.91k | "Cannot take in charge mct data within multiple MCT records\n"); |
5836 | 1.91k | return OPJ_TRUE; |
5837 | 1.91k | } |
5838 | | |
5839 | 8.30k | if (p_header_size <= 6) { |
5840 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5841 | 3 | return OPJ_FALSE; |
5842 | 3 | } |
5843 | | |
5844 | | /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ |
5845 | 8.29k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Imct */ |
5846 | 8.29k | p_header_data += 2; |
5847 | | |
5848 | 8.29k | l_indix = l_tmp & 0xff; |
5849 | 8.29k | l_mct_data = l_tcp->m_mct_records; |
5850 | | |
5851 | 28.4k | for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { |
5852 | 27.3k | if (l_mct_data->m_index == l_indix) { |
5853 | 7.13k | break; |
5854 | 7.13k | } |
5855 | 20.1k | ++l_mct_data; |
5856 | 20.1k | } |
5857 | | |
5858 | | /* NOT FOUND */ |
5859 | 8.29k | if (i == l_tcp->m_nb_mct_records) { |
5860 | 1.16k | if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { |
5861 | 59 | opj_mct_data_t *new_mct_records; |
5862 | 59 | l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
5863 | | |
5864 | 59 | new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, |
5865 | 59 | l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
5866 | 59 | if (! new_mct_records) { |
5867 | 0 | opj_free(l_tcp->m_mct_records); |
5868 | 0 | l_tcp->m_mct_records = NULL; |
5869 | 0 | l_tcp->m_nb_max_mct_records = 0; |
5870 | 0 | l_tcp->m_nb_mct_records = 0; |
5871 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n"); |
5872 | 0 | return OPJ_FALSE; |
5873 | 0 | } |
5874 | | |
5875 | | /* Update m_mcc_records[].m_offset_array and m_decorrelation_array |
5876 | | * to point to the new addresses */ |
5877 | 59 | if (new_mct_records != l_tcp->m_mct_records) { |
5878 | 212 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5879 | 167 | opj_simple_mcc_decorrelation_data_t* l_mcc_record = |
5880 | 167 | &(l_tcp->m_mcc_records[i]); |
5881 | 167 | if (l_mcc_record->m_decorrelation_array) { |
5882 | 41 | l_mcc_record->m_decorrelation_array = |
5883 | 41 | new_mct_records + |
5884 | 41 | (l_mcc_record->m_decorrelation_array - |
5885 | 41 | l_tcp->m_mct_records); |
5886 | 41 | } |
5887 | 167 | if (l_mcc_record->m_offset_array) { |
5888 | 41 | l_mcc_record->m_offset_array = |
5889 | 41 | new_mct_records + |
5890 | 41 | (l_mcc_record->m_offset_array - |
5891 | 41 | l_tcp->m_mct_records); |
5892 | 41 | } |
5893 | 167 | } |
5894 | 45 | } |
5895 | | |
5896 | 59 | l_tcp->m_mct_records = new_mct_records; |
5897 | 59 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5898 | 59 | memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * |
5899 | 59 | sizeof(opj_mct_data_t)); |
5900 | 59 | } |
5901 | | |
5902 | 1.16k | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5903 | 1.16k | ++l_tcp->m_nb_mct_records; |
5904 | 1.16k | } |
5905 | | |
5906 | 8.29k | if (l_mct_data->m_data) { |
5907 | 1.82k | opj_free(l_mct_data->m_data); |
5908 | 1.82k | l_mct_data->m_data = 00; |
5909 | 1.82k | l_mct_data->m_data_size = 0; |
5910 | 1.82k | } |
5911 | | |
5912 | 8.29k | l_mct_data->m_index = l_indix; |
5913 | 8.29k | l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); |
5914 | 8.29k | l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); |
5915 | | |
5916 | 8.29k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymct */ |
5917 | 8.29k | p_header_data += 2; |
5918 | 8.29k | if (l_tmp != 0) { |
5919 | 6.10k | opj_event_msg(p_manager, EVT_WARNING, |
5920 | 6.10k | "Cannot take in charge multiple MCT markers\n"); |
5921 | 6.10k | return OPJ_TRUE; |
5922 | 6.10k | } |
5923 | | |
5924 | 2.19k | p_header_size -= 6; |
5925 | | |
5926 | 2.19k | l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); |
5927 | 2.19k | if (! l_mct_data->m_data) { |
5928 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5929 | 0 | return OPJ_FALSE; |
5930 | 0 | } |
5931 | 2.19k | memcpy(l_mct_data->m_data, p_header_data, p_header_size); |
5932 | | |
5933 | 2.19k | l_mct_data->m_data_size = p_header_size; |
5934 | | |
5935 | 2.19k | return OPJ_TRUE; |
5936 | 2.19k | } |
5937 | | |
5938 | | static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, |
5939 | | struct opj_simple_mcc_decorrelation_data * p_mcc_record, |
5940 | | struct opj_stream_private *p_stream, |
5941 | | struct opj_event_mgr * p_manager) |
5942 | 0 | { |
5943 | 0 | OPJ_UINT32 i; |
5944 | 0 | OPJ_UINT32 l_mcc_size; |
5945 | 0 | OPJ_BYTE * l_current_data = 00; |
5946 | 0 | OPJ_UINT32 l_nb_bytes_for_comp; |
5947 | 0 | OPJ_UINT32 l_mask; |
5948 | 0 | OPJ_UINT32 l_tmcc; |
5949 | | |
5950 | | /* preconditions */ |
5951 | 0 | assert(p_j2k != 00); |
5952 | 0 | assert(p_manager != 00); |
5953 | 0 | assert(p_stream != 00); |
5954 | |
|
5955 | 0 | if (p_mcc_record->m_nb_comps > 255) { |
5956 | 0 | l_nb_bytes_for_comp = 2; |
5957 | 0 | l_mask = 0x8000; |
5958 | 0 | } else { |
5959 | 0 | l_nb_bytes_for_comp = 1; |
5960 | 0 | l_mask = 0; |
5961 | 0 | } |
5962 | |
|
5963 | 0 | l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; |
5964 | 0 | if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
5965 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
5966 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size); |
5967 | 0 | if (! new_header_tile_data) { |
5968 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
5969 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
5970 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
5971 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n"); |
5972 | 0 | return OPJ_FALSE; |
5973 | 0 | } |
5974 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
5975 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; |
5976 | 0 | } |
5977 | | |
5978 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
5979 | |
|
5980 | 0 | opj_write_bytes(l_current_data, J2K_MS_MCC, |
5981 | 0 | 2); /* MCC */ |
5982 | 0 | l_current_data += 2; |
5983 | |
|
5984 | 0 | opj_write_bytes(l_current_data, l_mcc_size - 2, |
5985 | 0 | 2); /* Lmcc */ |
5986 | 0 | l_current_data += 2; |
5987 | | |
5988 | | /* first marker */ |
5989 | 0 | opj_write_bytes(l_current_data, 0, |
5990 | 0 | 2); /* Zmcc */ |
5991 | 0 | l_current_data += 2; |
5992 | |
|
5993 | 0 | opj_write_bytes(l_current_data, p_mcc_record->m_index, |
5994 | 0 | 1); /* Imcc -> no need for other values, take the first */ |
5995 | 0 | ++l_current_data; |
5996 | | |
5997 | | /* only one marker atm */ |
5998 | 0 | opj_write_bytes(l_current_data, 0, |
5999 | 0 | 2); /* Ymcc */ |
6000 | 0 | l_current_data += 2; |
6001 | |
|
6002 | 0 | opj_write_bytes(l_current_data, 1, |
6003 | 0 | 2); /* Qmcc -> number of collections -> 1 */ |
6004 | 0 | l_current_data += 2; |
6005 | |
|
6006 | 0 | opj_write_bytes(l_current_data, 0x1, |
6007 | 0 | 1); /* Xmcci type of component transformation -> array based decorrelation */ |
6008 | 0 | ++l_current_data; |
6009 | |
|
6010 | 0 | opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, |
6011 | 0 | 2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ |
6012 | 0 | l_current_data += 2; |
6013 | |
|
6014 | 0 | for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { |
6015 | 0 | opj_write_bytes(l_current_data, i, |
6016 | 0 | l_nb_bytes_for_comp); /* Cmccij Component offset*/ |
6017 | 0 | l_current_data += l_nb_bytes_for_comp; |
6018 | 0 | } |
6019 | |
|
6020 | 0 | opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, |
6021 | 0 | 2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ |
6022 | 0 | l_current_data += 2; |
6023 | |
|
6024 | 0 | for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { |
6025 | 0 | opj_write_bytes(l_current_data, i, |
6026 | 0 | l_nb_bytes_for_comp); /* Wmccij Component offset*/ |
6027 | 0 | l_current_data += l_nb_bytes_for_comp; |
6028 | 0 | } |
6029 | |
|
6030 | 0 | l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16; |
6031 | |
|
6032 | 0 | if (p_mcc_record->m_decorrelation_array) { |
6033 | 0 | l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; |
6034 | 0 | } |
6035 | |
|
6036 | 0 | if (p_mcc_record->m_offset_array) { |
6037 | 0 | l_tmcc |= ((p_mcc_record->m_offset_array->m_index) << 8); |
6038 | 0 | } |
6039 | |
|
6040 | 0 | opj_write_bytes(l_current_data, l_tmcc, |
6041 | 0 | 3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ |
6042 | 0 | l_current_data += 3; |
6043 | |
|
6044 | 0 | if (opj_stream_write_data(p_stream, |
6045 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size, |
6046 | 0 | p_manager) != l_mcc_size) { |
6047 | 0 | return OPJ_FALSE; |
6048 | 0 | } |
6049 | | |
6050 | 0 | return OPJ_TRUE; |
6051 | 0 | } |
6052 | | |
6053 | | static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, |
6054 | | OPJ_BYTE * p_header_data, |
6055 | | OPJ_UINT32 p_header_size, |
6056 | | opj_event_mgr_t * p_manager) |
6057 | 52.6k | { |
6058 | 52.6k | OPJ_UINT32 i, j; |
6059 | 52.6k | OPJ_UINT32 l_tmp; |
6060 | 52.6k | OPJ_UINT32 l_indix; |
6061 | 52.6k | opj_tcp_t * l_tcp; |
6062 | 52.6k | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6063 | 52.6k | opj_mct_data_t * l_mct_data; |
6064 | 52.6k | OPJ_UINT32 l_nb_collections; |
6065 | 52.6k | OPJ_UINT32 l_nb_comps; |
6066 | 52.6k | OPJ_UINT32 l_nb_bytes_by_comp; |
6067 | 52.6k | OPJ_BOOL l_new_mcc = OPJ_FALSE; |
6068 | | |
6069 | | /* preconditions */ |
6070 | 52.6k | assert(p_header_data != 00); |
6071 | 52.6k | assert(p_j2k != 00); |
6072 | 52.6k | assert(p_manager != 00); |
6073 | | |
6074 | 52.6k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
6075 | 107 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
6076 | 52.6k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
6077 | | |
6078 | 52.6k | if (p_header_size < 2) { |
6079 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6080 | 2 | return OPJ_FALSE; |
6081 | 2 | } |
6082 | | |
6083 | | /* first marker */ |
6084 | 52.6k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmcc */ |
6085 | 52.6k | p_header_data += 2; |
6086 | 52.6k | if (l_tmp != 0) { |
6087 | 1.87k | opj_event_msg(p_manager, EVT_WARNING, |
6088 | 1.87k | "Cannot take in charge multiple data spanning\n"); |
6089 | 1.87k | return OPJ_TRUE; |
6090 | 1.87k | } |
6091 | | |
6092 | 50.7k | if (p_header_size < 7) { |
6093 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6094 | 3 | return OPJ_FALSE; |
6095 | 3 | } |
6096 | | |
6097 | 50.7k | opj_read_bytes(p_header_data, &l_indix, |
6098 | 50.7k | 1); /* Imcc -> no need for other values, take the first */ |
6099 | 50.7k | ++p_header_data; |
6100 | | |
6101 | 50.7k | l_mcc_record = l_tcp->m_mcc_records; |
6102 | | |
6103 | 74.1k | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
6104 | 54.6k | if (l_mcc_record->m_index == l_indix) { |
6105 | 31.2k | break; |
6106 | 31.2k | } |
6107 | 23.4k | ++l_mcc_record; |
6108 | 23.4k | } |
6109 | | |
6110 | | /** NOT FOUND */ |
6111 | 50.7k | if (i == l_tcp->m_nb_mcc_records) { |
6112 | 19.4k | if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { |
6113 | 15 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
6114 | 15 | l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
6115 | | |
6116 | 15 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
6117 | 15 | l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof( |
6118 | 15 | opj_simple_mcc_decorrelation_data_t)); |
6119 | 15 | if (! new_mcc_records) { |
6120 | 0 | opj_free(l_tcp->m_mcc_records); |
6121 | 0 | l_tcp->m_mcc_records = NULL; |
6122 | 0 | l_tcp->m_nb_max_mcc_records = 0; |
6123 | 0 | l_tcp->m_nb_mcc_records = 0; |
6124 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n"); |
6125 | 0 | return OPJ_FALSE; |
6126 | 0 | } |
6127 | 15 | l_tcp->m_mcc_records = new_mcc_records; |
6128 | 15 | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
6129 | 15 | memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records) |
6130 | 15 | * sizeof(opj_simple_mcc_decorrelation_data_t)); |
6131 | 15 | } |
6132 | 19.4k | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
6133 | 19.4k | l_new_mcc = OPJ_TRUE; |
6134 | 19.4k | } |
6135 | 50.7k | l_mcc_record->m_index = l_indix; |
6136 | | |
6137 | | /* only one marker atm */ |
6138 | 50.7k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymcc */ |
6139 | 50.7k | p_header_data += 2; |
6140 | 50.7k | if (l_tmp != 0) { |
6141 | 16.2k | opj_event_msg(p_manager, EVT_WARNING, |
6142 | 16.2k | "Cannot take in charge multiple data spanning\n"); |
6143 | 16.2k | return OPJ_TRUE; |
6144 | 16.2k | } |
6145 | | |
6146 | 34.5k | opj_read_bytes(p_header_data, &l_nb_collections, |
6147 | 34.5k | 2); /* Qmcc -> number of collections -> 1 */ |
6148 | 34.5k | p_header_data += 2; |
6149 | | |
6150 | 34.5k | if (l_nb_collections > 1) { |
6151 | 2.21k | opj_event_msg(p_manager, EVT_WARNING, |
6152 | 2.21k | "Cannot take in charge multiple collections\n"); |
6153 | 2.21k | return OPJ_TRUE; |
6154 | 2.21k | } |
6155 | | |
6156 | 32.2k | p_header_size -= 7; |
6157 | | |
6158 | 41.8k | for (i = 0; i < l_nb_collections; ++i) { |
6159 | 30.5k | if (p_header_size < 3) { |
6160 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6161 | 2 | return OPJ_FALSE; |
6162 | 2 | } |
6163 | | |
6164 | 30.5k | opj_read_bytes(p_header_data, &l_tmp, |
6165 | 30.5k | 1); /* Xmcci type of component transformation -> array based decorrelation */ |
6166 | 30.5k | ++p_header_data; |
6167 | | |
6168 | 30.5k | if (l_tmp != 1) { |
6169 | 1.24k | opj_event_msg(p_manager, EVT_WARNING, |
6170 | 1.24k | "Cannot take in charge collections other than array decorrelation\n"); |
6171 | 1.24k | return OPJ_TRUE; |
6172 | 1.24k | } |
6173 | | |
6174 | 29.3k | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
6175 | | |
6176 | 29.3k | p_header_data += 2; |
6177 | 29.3k | p_header_size -= 3; |
6178 | | |
6179 | 29.3k | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
6180 | 29.3k | l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; |
6181 | | |
6182 | 29.3k | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { |
6183 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6184 | 12 | return OPJ_FALSE; |
6185 | 12 | } |
6186 | | |
6187 | 29.2k | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); |
6188 | | |
6189 | 54.9k | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
6190 | 28.3k | opj_read_bytes(p_header_data, &l_tmp, |
6191 | 28.3k | l_nb_bytes_by_comp); /* Cmccij Component offset*/ |
6192 | 28.3k | p_header_data += l_nb_bytes_by_comp; |
6193 | | |
6194 | 28.3k | if (l_tmp != j) { |
6195 | 2.64k | opj_event_msg(p_manager, EVT_WARNING, |
6196 | 2.64k | "Cannot take in charge collections with indix shuffle\n"); |
6197 | 2.64k | return OPJ_TRUE; |
6198 | 2.64k | } |
6199 | 28.3k | } |
6200 | | |
6201 | 26.6k | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
6202 | 26.6k | p_header_data += 2; |
6203 | | |
6204 | 26.6k | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
6205 | 26.6k | l_nb_comps &= 0x7fff; |
6206 | | |
6207 | 26.6k | if (l_nb_comps != l_mcc_record->m_nb_comps) { |
6208 | 10.4k | opj_event_msg(p_manager, EVT_WARNING, |
6209 | 10.4k | "Cannot take in charge collections without same number of indixes\n"); |
6210 | 10.4k | return OPJ_TRUE; |
6211 | 10.4k | } |
6212 | | |
6213 | 16.2k | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { |
6214 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6215 | 2 | return OPJ_FALSE; |
6216 | 2 | } |
6217 | | |
6218 | 16.2k | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); |
6219 | | |
6220 | 24.8k | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
6221 | 15.2k | opj_read_bytes(p_header_data, &l_tmp, |
6222 | 15.2k | l_nb_bytes_by_comp); /* Wmccij Component offset*/ |
6223 | 15.2k | p_header_data += l_nb_bytes_by_comp; |
6224 | | |
6225 | 15.2k | if (l_tmp != j) { |
6226 | 6.64k | opj_event_msg(p_manager, EVT_WARNING, |
6227 | 6.64k | "Cannot take in charge collections with indix shuffle\n"); |
6228 | 6.64k | return OPJ_TRUE; |
6229 | 6.64k | } |
6230 | 15.2k | } |
6231 | | |
6232 | 9.59k | opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/ |
6233 | 9.59k | p_header_data += 3; |
6234 | | |
6235 | 9.59k | l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1); |
6236 | 9.59k | l_mcc_record->m_decorrelation_array = 00; |
6237 | 9.59k | l_mcc_record->m_offset_array = 00; |
6238 | | |
6239 | 9.59k | l_indix = l_tmp & 0xff; |
6240 | 9.59k | if (l_indix != 0) { |
6241 | 1.31k | l_mct_data = l_tcp->m_mct_records; |
6242 | 4.35k | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6243 | 4.34k | if (l_mct_data->m_index == l_indix) { |
6244 | 1.30k | l_mcc_record->m_decorrelation_array = l_mct_data; |
6245 | 1.30k | break; |
6246 | 1.30k | } |
6247 | 3.04k | ++l_mct_data; |
6248 | 3.04k | } |
6249 | | |
6250 | 1.31k | if (l_mcc_record->m_decorrelation_array == 00) { |
6251 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6252 | 12 | return OPJ_FALSE; |
6253 | 12 | } |
6254 | 1.31k | } |
6255 | | |
6256 | 9.58k | l_indix = (l_tmp >> 8) & 0xff; |
6257 | 9.58k | if (l_indix != 0) { |
6258 | 1.35k | l_mct_data = l_tcp->m_mct_records; |
6259 | 4.40k | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6260 | 4.39k | if (l_mct_data->m_index == l_indix) { |
6261 | 1.34k | l_mcc_record->m_offset_array = l_mct_data; |
6262 | 1.34k | break; |
6263 | 1.34k | } |
6264 | 3.04k | ++l_mct_data; |
6265 | 3.04k | } |
6266 | | |
6267 | 1.35k | if (l_mcc_record->m_offset_array == 00) { |
6268 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6269 | 12 | return OPJ_FALSE; |
6270 | 12 | } |
6271 | 1.35k | } |
6272 | 9.58k | } |
6273 | | |
6274 | 11.3k | if (p_header_size != 0) { |
6275 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6276 | 5 | return OPJ_FALSE; |
6277 | 5 | } |
6278 | | |
6279 | 11.3k | if (l_new_mcc) { |
6280 | 767 | ++l_tcp->m_nb_mcc_records; |
6281 | 767 | } |
6282 | | |
6283 | 11.3k | return OPJ_TRUE; |
6284 | 11.3k | } |
6285 | | |
6286 | | static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, |
6287 | | struct opj_stream_private *p_stream, |
6288 | | struct opj_event_mgr * p_manager |
6289 | | ) |
6290 | 0 | { |
6291 | 0 | OPJ_BYTE * l_current_data = 00; |
6292 | 0 | OPJ_UINT32 l_mco_size; |
6293 | 0 | opj_tcp_t * l_tcp = 00; |
6294 | 0 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6295 | 0 | OPJ_UINT32 i; |
6296 | | |
6297 | | /* preconditions */ |
6298 | 0 | assert(p_j2k != 00); |
6299 | 0 | assert(p_manager != 00); |
6300 | 0 | assert(p_stream != 00); |
6301 | |
|
6302 | 0 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
6303 | |
|
6304 | 0 | l_mco_size = 5 + l_tcp->m_nb_mcc_records; |
6305 | 0 | if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
6306 | |
|
6307 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
6308 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size); |
6309 | 0 | if (! new_header_tile_data) { |
6310 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
6311 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
6312 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
6313 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n"); |
6314 | 0 | return OPJ_FALSE; |
6315 | 0 | } |
6316 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
6317 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; |
6318 | 0 | } |
6319 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
6320 | | |
6321 | |
|
6322 | 0 | opj_write_bytes(l_current_data, J2K_MS_MCO, 2); /* MCO */ |
6323 | 0 | l_current_data += 2; |
6324 | |
|
6325 | 0 | opj_write_bytes(l_current_data, l_mco_size - 2, 2); /* Lmco */ |
6326 | 0 | l_current_data += 2; |
6327 | |
|
6328 | 0 | opj_write_bytes(l_current_data, l_tcp->m_nb_mcc_records, |
6329 | 0 | 1); /* Nmco : only one transform stage*/ |
6330 | 0 | ++l_current_data; |
6331 | |
|
6332 | 0 | l_mcc_record = l_tcp->m_mcc_records; |
6333 | 0 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
6334 | 0 | opj_write_bytes(l_current_data, l_mcc_record->m_index, |
6335 | 0 | 1); /* Imco -> use the mcc indicated by 1*/ |
6336 | 0 | ++l_current_data; |
6337 | 0 | ++l_mcc_record; |
6338 | 0 | } |
6339 | |
|
6340 | 0 | if (opj_stream_write_data(p_stream, |
6341 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size, |
6342 | 0 | p_manager) != l_mco_size) { |
6343 | 0 | return OPJ_FALSE; |
6344 | 0 | } |
6345 | | |
6346 | 0 | return OPJ_TRUE; |
6347 | 0 | } |
6348 | | |
6349 | | /** |
6350 | | * Reads a MCO marker (Multiple Component Transform Ordering) |
6351 | | * |
6352 | | * @param p_header_data the data contained in the MCO box. |
6353 | | * @param p_j2k the jpeg2000 codec. |
6354 | | * @param p_header_size the size of the data contained in the MCO marker. |
6355 | | * @param p_manager the user event manager. |
6356 | | */ |
6357 | | static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, |
6358 | | OPJ_BYTE * p_header_data, |
6359 | | OPJ_UINT32 p_header_size, |
6360 | | opj_event_mgr_t * p_manager |
6361 | | ) |
6362 | 52.7k | { |
6363 | 52.7k | OPJ_UINT32 l_tmp, i; |
6364 | 52.7k | OPJ_UINT32 l_nb_stages; |
6365 | 52.7k | opj_tcp_t * l_tcp; |
6366 | 52.7k | opj_tccp_t * l_tccp; |
6367 | 52.7k | opj_image_t * l_image; |
6368 | | |
6369 | | /* preconditions */ |
6370 | 52.7k | assert(p_header_data != 00); |
6371 | 52.7k | assert(p_j2k != 00); |
6372 | 52.7k | assert(p_manager != 00); |
6373 | | |
6374 | 52.7k | l_image = p_j2k->m_private_image; |
6375 | 52.7k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
6376 | 1.27k | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
6377 | 52.7k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
6378 | | |
6379 | 52.7k | if (p_header_size < 1) { |
6380 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n"); |
6381 | 3 | return OPJ_FALSE; |
6382 | 3 | } |
6383 | | |
6384 | 52.7k | opj_read_bytes(p_header_data, &l_nb_stages, |
6385 | 52.7k | 1); /* Nmco : only one transform stage*/ |
6386 | 52.7k | ++p_header_data; |
6387 | | |
6388 | 52.7k | if (l_nb_stages > 1) { |
6389 | 25.8k | opj_event_msg(p_manager, EVT_WARNING, |
6390 | 25.8k | "Cannot take in charge multiple transformation stages.\n"); |
6391 | 25.8k | return OPJ_TRUE; |
6392 | 25.8k | } |
6393 | | |
6394 | 26.9k | if (p_header_size != l_nb_stages + 1) { |
6395 | 11 | opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n"); |
6396 | 11 | return OPJ_FALSE; |
6397 | 11 | } |
6398 | | |
6399 | 26.9k | l_tccp = l_tcp->tccps; |
6400 | | |
6401 | 115k | for (i = 0; i < l_image->numcomps; ++i) { |
6402 | 88.2k | l_tccp->m_dc_level_shift = 0; |
6403 | 88.2k | ++l_tccp; |
6404 | 88.2k | } |
6405 | | |
6406 | 26.9k | if (l_tcp->m_mct_decoding_matrix) { |
6407 | 3.27k | opj_free(l_tcp->m_mct_decoding_matrix); |
6408 | 3.27k | l_tcp->m_mct_decoding_matrix = 00; |
6409 | 3.27k | } |
6410 | | |
6411 | 53.3k | for (i = 0; i < l_nb_stages; ++i) { |
6412 | 26.4k | opj_read_bytes(p_header_data, &l_tmp, 1); |
6413 | 26.4k | ++p_header_data; |
6414 | | |
6415 | 26.4k | if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) { |
6416 | 5 | return OPJ_FALSE; |
6417 | 5 | } |
6418 | 26.4k | } |
6419 | | |
6420 | 26.9k | return OPJ_TRUE; |
6421 | 26.9k | } |
6422 | | |
6423 | | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, |
6424 | | OPJ_UINT32 p_index) |
6425 | 26.4k | { |
6426 | 26.4k | OPJ_UINT32 i; |
6427 | 26.4k | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6428 | 26.4k | opj_mct_data_t * l_deco_array, * l_offset_array; |
6429 | 26.4k | OPJ_UINT32 l_data_size, l_mct_size, l_offset_size; |
6430 | 26.4k | OPJ_UINT32 l_nb_elem; |
6431 | 26.4k | OPJ_UINT32 * l_offset_data, * l_current_offset_data; |
6432 | 26.4k | opj_tccp_t * l_tccp; |
6433 | | |
6434 | | /* preconditions */ |
6435 | 26.4k | assert(p_tcp != 00); |
6436 | | |
6437 | 26.4k | l_mcc_record = p_tcp->m_mcc_records; |
6438 | | |
6439 | 28.0k | for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) { |
6440 | 24.0k | if (l_mcc_record->m_index == p_index) { |
6441 | 22.3k | break; |
6442 | 22.3k | } |
6443 | 24.0k | } |
6444 | | |
6445 | 26.4k | if (i == p_tcp->m_nb_mcc_records) { |
6446 | | /** element discarded **/ |
6447 | 4.05k | return OPJ_TRUE; |
6448 | 4.05k | } |
6449 | | |
6450 | 22.3k | if (l_mcc_record->m_nb_comps != p_image->numcomps) { |
6451 | | /** do not support number of comps != image */ |
6452 | 7.68k | return OPJ_TRUE; |
6453 | 7.68k | } |
6454 | | |
6455 | 14.6k | l_deco_array = l_mcc_record->m_decorrelation_array; |
6456 | | |
6457 | 14.6k | if (l_deco_array) { |
6458 | 3.35k | l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps |
6459 | 3.35k | * p_image->numcomps; |
6460 | 3.35k | if (l_deco_array->m_data_size != l_data_size) { |
6461 | 2 | return OPJ_FALSE; |
6462 | 2 | } |
6463 | | |
6464 | 3.35k | l_nb_elem = p_image->numcomps * p_image->numcomps; |
6465 | 3.35k | l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32); |
6466 | 3.35k | p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
6467 | | |
6468 | 3.35k | if (! p_tcp->m_mct_decoding_matrix) { |
6469 | 0 | return OPJ_FALSE; |
6470 | 0 | } |
6471 | | |
6472 | 3.35k | j2k_mct_read_functions_to_float[l_deco_array->m_element_type]( |
6473 | 3.35k | l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem); |
6474 | 3.35k | } |
6475 | | |
6476 | 14.6k | l_offset_array = l_mcc_record->m_offset_array; |
6477 | | |
6478 | 14.6k | if (l_offset_array) { |
6479 | 2.16k | l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * |
6480 | 2.16k | p_image->numcomps; |
6481 | 2.16k | if (l_offset_array->m_data_size != l_data_size) { |
6482 | 3 | return OPJ_FALSE; |
6483 | 3 | } |
6484 | | |
6485 | 2.16k | l_nb_elem = p_image->numcomps; |
6486 | 2.16k | l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32); |
6487 | 2.16k | l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); |
6488 | | |
6489 | 2.16k | if (! l_offset_data) { |
6490 | 0 | return OPJ_FALSE; |
6491 | 0 | } |
6492 | | |
6493 | 2.16k | j2k_mct_read_functions_to_int32[l_offset_array->m_element_type]( |
6494 | 2.16k | l_offset_array->m_data, l_offset_data, l_nb_elem); |
6495 | | |
6496 | 2.16k | l_tccp = p_tcp->tccps; |
6497 | 2.16k | l_current_offset_data = l_offset_data; |
6498 | | |
6499 | 4.86k | for (i = 0; i < p_image->numcomps; ++i) { |
6500 | 2.70k | l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++); |
6501 | 2.70k | ++l_tccp; |
6502 | 2.70k | } |
6503 | | |
6504 | 2.16k | opj_free(l_offset_data); |
6505 | 2.16k | } |
6506 | | |
6507 | 14.6k | return OPJ_TRUE; |
6508 | 14.6k | } |
6509 | | |
6510 | | static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, |
6511 | | struct opj_stream_private *p_stream, |
6512 | | struct opj_event_mgr * p_manager) |
6513 | 0 | { |
6514 | 0 | OPJ_UINT32 i; |
6515 | 0 | OPJ_UINT32 l_cbd_size; |
6516 | 0 | OPJ_BYTE * l_current_data = 00; |
6517 | 0 | opj_image_t *l_image = 00; |
6518 | 0 | opj_image_comp_t * l_comp = 00; |
6519 | | |
6520 | | /* preconditions */ |
6521 | 0 | assert(p_j2k != 00); |
6522 | 0 | assert(p_manager != 00); |
6523 | 0 | assert(p_stream != 00); |
6524 | |
|
6525 | 0 | l_image = p_j2k->m_private_image; |
6526 | 0 | l_cbd_size = 6 + p_j2k->m_private_image->numcomps; |
6527 | |
|
6528 | 0 | if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
6529 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
6530 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size); |
6531 | 0 | if (! new_header_tile_data) { |
6532 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
6533 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
6534 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
6535 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n"); |
6536 | 0 | return OPJ_FALSE; |
6537 | 0 | } |
6538 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
6539 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; |
6540 | 0 | } |
6541 | | |
6542 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
6543 | |
|
6544 | 0 | opj_write_bytes(l_current_data, J2K_MS_CBD, 2); /* CBD */ |
6545 | 0 | l_current_data += 2; |
6546 | |
|
6547 | 0 | opj_write_bytes(l_current_data, l_cbd_size - 2, 2); /* L_CBD */ |
6548 | 0 | l_current_data += 2; |
6549 | |
|
6550 | 0 | opj_write_bytes(l_current_data, l_image->numcomps, 2); /* Ncbd */ |
6551 | 0 | l_current_data += 2; |
6552 | |
|
6553 | 0 | l_comp = l_image->comps; |
6554 | |
|
6555 | 0 | for (i = 0; i < l_image->numcomps; ++i) { |
6556 | 0 | opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), |
6557 | 0 | 1); /* Component bit depth */ |
6558 | 0 | ++l_current_data; |
6559 | |
|
6560 | 0 | ++l_comp; |
6561 | 0 | } |
6562 | |
|
6563 | 0 | if (opj_stream_write_data(p_stream, |
6564 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size, |
6565 | 0 | p_manager) != l_cbd_size) { |
6566 | 0 | return OPJ_FALSE; |
6567 | 0 | } |
6568 | | |
6569 | 0 | return OPJ_TRUE; |
6570 | 0 | } |
6571 | | |
6572 | | /** |
6573 | | * Reads a CBD marker (Component bit depth definition) |
6574 | | * @param p_header_data the data contained in the CBD box. |
6575 | | * @param p_j2k the jpeg2000 codec. |
6576 | | * @param p_header_size the size of the data contained in the CBD marker. |
6577 | | * @param p_manager the user event manager. |
6578 | | */ |
6579 | | static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, |
6580 | | OPJ_BYTE * p_header_data, |
6581 | | OPJ_UINT32 p_header_size, |
6582 | | opj_event_mgr_t * p_manager |
6583 | | ) |
6584 | 326 | { |
6585 | 326 | OPJ_UINT32 l_nb_comp, l_num_comp; |
6586 | 326 | OPJ_UINT32 l_comp_def; |
6587 | 326 | OPJ_UINT32 i; |
6588 | 326 | opj_image_comp_t * l_comp = 00; |
6589 | | |
6590 | | /* preconditions */ |
6591 | 326 | assert(p_header_data != 00); |
6592 | 326 | assert(p_j2k != 00); |
6593 | 326 | assert(p_manager != 00); |
6594 | | |
6595 | 326 | l_num_comp = p_j2k->m_private_image->numcomps; |
6596 | | |
6597 | 326 | if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { |
6598 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
6599 | 5 | return OPJ_FALSE; |
6600 | 5 | } |
6601 | | |
6602 | 321 | opj_read_bytes(p_header_data, &l_nb_comp, |
6603 | 321 | 2); /* Ncbd */ |
6604 | 321 | p_header_data += 2; |
6605 | | |
6606 | 321 | if (l_nb_comp != l_num_comp) { |
6607 | 25 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
6608 | 25 | return OPJ_FALSE; |
6609 | 25 | } |
6610 | | |
6611 | 296 | l_comp = p_j2k->m_private_image->comps; |
6612 | 1.28k | for (i = 0; i < l_num_comp; ++i) { |
6613 | 999 | opj_read_bytes(p_header_data, &l_comp_def, |
6614 | 999 | 1); /* Component bit depth */ |
6615 | 999 | ++p_header_data; |
6616 | 999 | l_comp->sgnd = (l_comp_def >> 7) & 1; |
6617 | 999 | l_comp->prec = (l_comp_def & 0x7f) + 1; |
6618 | | |
6619 | 999 | if (l_comp->prec > 31) { |
6620 | 7 | opj_event_msg(p_manager, EVT_ERROR, |
6621 | 7 | "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n", |
6622 | 7 | i, l_comp->prec); |
6623 | 7 | return OPJ_FALSE; |
6624 | 7 | } |
6625 | 992 | ++l_comp; |
6626 | 992 | } |
6627 | | |
6628 | 289 | return OPJ_TRUE; |
6629 | 296 | } |
6630 | | |
6631 | | /** |
6632 | | * Reads a CAP marker (extended capabilities definition). Empty implementation. |
6633 | | * Found in HTJ2K files. |
6634 | | * |
6635 | | * @param p_header_data the data contained in the CAP box. |
6636 | | * @param p_j2k the jpeg2000 codec. |
6637 | | * @param p_header_size the size of the data contained in the CAP marker. |
6638 | | * @param p_manager the user event manager. |
6639 | | */ |
6640 | | static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k, |
6641 | | OPJ_BYTE * p_header_data, |
6642 | | OPJ_UINT32 p_header_size, |
6643 | | opj_event_mgr_t * p_manager |
6644 | | ) |
6645 | 960 | { |
6646 | | /* preconditions */ |
6647 | 960 | assert(p_header_data != 00); |
6648 | 960 | assert(p_j2k != 00); |
6649 | 960 | assert(p_manager != 00); |
6650 | | |
6651 | 960 | (void)p_j2k; |
6652 | 960 | (void)p_header_data; |
6653 | 960 | (void)p_header_size; |
6654 | 960 | (void)p_manager; |
6655 | | |
6656 | 960 | return OPJ_TRUE; |
6657 | 960 | } |
6658 | | |
6659 | | /** |
6660 | | * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files |
6661 | | * @param p_header_data the data contained in the CPF box. |
6662 | | * @param p_j2k the jpeg2000 codec. |
6663 | | * @param p_header_size the size of the data contained in the CPF marker. |
6664 | | * @param p_manager the user event manager. |
6665 | | */ |
6666 | | static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k, |
6667 | | OPJ_BYTE * p_header_data, |
6668 | | OPJ_UINT32 p_header_size, |
6669 | | opj_event_mgr_t * p_manager |
6670 | | ) |
6671 | 213 | { |
6672 | | /* preconditions */ |
6673 | 213 | assert(p_header_data != 00); |
6674 | 213 | assert(p_j2k != 00); |
6675 | 213 | assert(p_manager != 00); |
6676 | | |
6677 | 213 | (void)p_j2k; |
6678 | 213 | (void)p_header_data; |
6679 | 213 | (void)p_header_size; |
6680 | 213 | (void)p_manager; |
6681 | | |
6682 | 213 | return OPJ_TRUE; |
6683 | 213 | } |
6684 | | |
6685 | | /* ----------------------------------------------------------------------- */ |
6686 | | /* J2K / JPT decoder interface */ |
6687 | | /* ----------------------------------------------------------------------- */ |
6688 | | |
6689 | | void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) |
6690 | 17.8k | { |
6691 | 17.8k | if (j2k && parameters) { |
6692 | 17.8k | j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; |
6693 | 17.8k | j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; |
6694 | | |
6695 | 17.8k | j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG); |
6696 | | #ifdef USE_JPWL |
6697 | | j2k->m_cp.correct = parameters->jpwl_correct; |
6698 | | j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; |
6699 | | j2k->m_cp.max_tiles = parameters->jpwl_max_tiles; |
6700 | | #endif /* USE_JPWL */ |
6701 | 17.8k | } |
6702 | 17.8k | } |
6703 | | |
6704 | | void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict) |
6705 | 0 | { |
6706 | 0 | if (j2k) { |
6707 | 0 | j2k->m_cp.strict = strict; |
6708 | 0 | } |
6709 | 0 | } |
6710 | | |
6711 | | OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads) |
6712 | 0 | { |
6713 | | /* Currently we pass the thread-pool to the tcd, so we cannot re-set it */ |
6714 | | /* afterwards */ |
6715 | 0 | if (opj_has_thread_support() && j2k->m_tcd == NULL) { |
6716 | 0 | opj_thread_pool_destroy(j2k->m_tp); |
6717 | 0 | j2k->m_tp = NULL; |
6718 | 0 | if (num_threads <= (OPJ_UINT32)INT_MAX) { |
6719 | 0 | j2k->m_tp = opj_thread_pool_create((int)num_threads); |
6720 | 0 | } |
6721 | 0 | if (j2k->m_tp == NULL) { |
6722 | 0 | j2k->m_tp = opj_thread_pool_create(0); |
6723 | 0 | return OPJ_FALSE; |
6724 | 0 | } |
6725 | 0 | return OPJ_TRUE; |
6726 | 0 | } |
6727 | 0 | return OPJ_FALSE; |
6728 | 0 | } |
6729 | | |
6730 | | static int opj_j2k_get_default_thread_count(void) |
6731 | 17.8k | { |
6732 | 17.8k | const char* num_threads_str = getenv("OPJ_NUM_THREADS"); |
6733 | 17.8k | int num_cpus; |
6734 | 17.8k | int num_threads; |
6735 | | |
6736 | 17.8k | if (num_threads_str == NULL || !opj_has_thread_support()) { |
6737 | 17.8k | return 0; |
6738 | 17.8k | } |
6739 | 0 | num_cpus = opj_get_num_cpus(); |
6740 | 0 | if (strcmp(num_threads_str, "ALL_CPUS") == 0) { |
6741 | 0 | return num_cpus; |
6742 | 0 | } |
6743 | 0 | if (num_cpus == 0) { |
6744 | 0 | num_cpus = 32; |
6745 | 0 | } |
6746 | 0 | num_threads = atoi(num_threads_str); |
6747 | 0 | if (num_threads < 0) { |
6748 | 0 | num_threads = 0; |
6749 | 0 | } else if (num_threads > 2 * num_cpus) { |
6750 | 0 | num_threads = 2 * num_cpus; |
6751 | 0 | } |
6752 | 0 | return num_threads; |
6753 | 0 | } |
6754 | | |
6755 | | /* ----------------------------------------------------------------------- */ |
6756 | | /* J2K encoder interface */ |
6757 | | /* ----------------------------------------------------------------------- */ |
6758 | | |
6759 | | opj_j2k_t* opj_j2k_create_compress(void) |
6760 | 0 | { |
6761 | 0 | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
6762 | 0 | if (!l_j2k) { |
6763 | 0 | return NULL; |
6764 | 0 | } |
6765 | | |
6766 | | |
6767 | 0 | l_j2k->m_is_decoder = 0; |
6768 | 0 | l_j2k->m_cp.m_is_decoder = 0; |
6769 | |
|
6770 | 0 | l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc( |
6771 | 0 | OPJ_J2K_DEFAULT_HEADER_SIZE); |
6772 | 0 | if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
6773 | 0 | opj_j2k_destroy(l_j2k); |
6774 | 0 | return NULL; |
6775 | 0 | } |
6776 | | |
6777 | 0 | l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = |
6778 | 0 | OPJ_J2K_DEFAULT_HEADER_SIZE; |
6779 | | |
6780 | | /* validation list creation*/ |
6781 | 0 | l_j2k->m_validation_list = opj_procedure_list_create(); |
6782 | 0 | if (! l_j2k->m_validation_list) { |
6783 | 0 | opj_j2k_destroy(l_j2k); |
6784 | 0 | return NULL; |
6785 | 0 | } |
6786 | | |
6787 | | /* execution list creation*/ |
6788 | 0 | l_j2k->m_procedure_list = opj_procedure_list_create(); |
6789 | 0 | if (! l_j2k->m_procedure_list) { |
6790 | 0 | opj_j2k_destroy(l_j2k); |
6791 | 0 | return NULL; |
6792 | 0 | } |
6793 | | |
6794 | 0 | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
6795 | 0 | if (!l_j2k->m_tp) { |
6796 | 0 | l_j2k->m_tp = opj_thread_pool_create(0); |
6797 | 0 | } |
6798 | 0 | if (!l_j2k->m_tp) { |
6799 | 0 | opj_j2k_destroy(l_j2k); |
6800 | 0 | return NULL; |
6801 | 0 | } |
6802 | | |
6803 | 0 | return l_j2k; |
6804 | 0 | } |
6805 | | |
6806 | | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres) |
6807 | 0 | { |
6808 | 0 | POC[0].tile = 1; |
6809 | 0 | POC[0].resno0 = 0; |
6810 | 0 | POC[0].compno0 = 0; |
6811 | 0 | POC[0].layno1 = 1; |
6812 | 0 | POC[0].resno1 = (OPJ_UINT32)(numres - 1); |
6813 | 0 | POC[0].compno1 = 3; |
6814 | 0 | POC[0].prg1 = OPJ_CPRL; |
6815 | 0 | POC[1].tile = 1; |
6816 | 0 | POC[1].resno0 = (OPJ_UINT32)(numres - 1); |
6817 | 0 | POC[1].compno0 = 0; |
6818 | 0 | POC[1].layno1 = 1; |
6819 | 0 | POC[1].resno1 = (OPJ_UINT32)numres; |
6820 | 0 | POC[1].compno1 = 3; |
6821 | 0 | POC[1].prg1 = OPJ_CPRL; |
6822 | 0 | return 2; |
6823 | 0 | } |
6824 | | |
6825 | | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, |
6826 | | opj_image_t *image, opj_event_mgr_t *p_manager) |
6827 | 0 | { |
6828 | | /* Configure cinema parameters */ |
6829 | 0 | int i; |
6830 | | |
6831 | | /* No tiling */ |
6832 | 0 | parameters->tile_size_on = OPJ_FALSE; |
6833 | 0 | parameters->cp_tdx = 1; |
6834 | 0 | parameters->cp_tdy = 1; |
6835 | | |
6836 | | /* One tile part for each component */ |
6837 | 0 | parameters->tp_flag = 'C'; |
6838 | 0 | parameters->tp_on = 1; |
6839 | | |
6840 | | /* Tile and Image shall be at (0,0) */ |
6841 | 0 | parameters->cp_tx0 = 0; |
6842 | 0 | parameters->cp_ty0 = 0; |
6843 | 0 | parameters->image_offset_x0 = 0; |
6844 | 0 | parameters->image_offset_y0 = 0; |
6845 | | |
6846 | | /* Codeblock size= 32*32 */ |
6847 | 0 | parameters->cblockw_init = 32; |
6848 | 0 | parameters->cblockh_init = 32; |
6849 | | |
6850 | | /* Codeblock style: no mode switch enabled */ |
6851 | 0 | parameters->mode = 0; |
6852 | | |
6853 | | /* No ROI */ |
6854 | 0 | parameters->roi_compno = -1; |
6855 | | |
6856 | | /* No subsampling */ |
6857 | 0 | parameters->subsampling_dx = 1; |
6858 | 0 | parameters->subsampling_dy = 1; |
6859 | | |
6860 | | /* 9-7 transform */ |
6861 | 0 | parameters->irreversible = 1; |
6862 | | |
6863 | | /* Number of layers */ |
6864 | 0 | if (parameters->tcp_numlayers > 1) { |
6865 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6866 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6867 | 0 | "1 single quality layer" |
6868 | 0 | "-> Number of layers forced to 1 (rather than %d)\n" |
6869 | 0 | "-> Rate of the last layer (%3.1f) will be used", |
6870 | 0 | parameters->tcp_numlayers, |
6871 | 0 | parameters->tcp_rates[parameters->tcp_numlayers - 1]); |
6872 | 0 | parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1]; |
6873 | 0 | parameters->tcp_numlayers = 1; |
6874 | 0 | } |
6875 | | |
6876 | | /* Resolution levels */ |
6877 | 0 | switch (parameters->rsiz) { |
6878 | 0 | case OPJ_PROFILE_CINEMA_2K: |
6879 | 0 | if (parameters->numresolution > 6) { |
6880 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6881 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6882 | 0 | "Number of decomposition levels <= 5\n" |
6883 | 0 | "-> Number of decomposition levels forced to 5 (rather than %d)\n", |
6884 | 0 | parameters->numresolution + 1); |
6885 | 0 | parameters->numresolution = 6; |
6886 | 0 | } |
6887 | 0 | break; |
6888 | 0 | case OPJ_PROFILE_CINEMA_4K: |
6889 | 0 | if (parameters->numresolution < 2) { |
6890 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6891 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6892 | 0 | "Number of decomposition levels >= 1 && <= 6\n" |
6893 | 0 | "-> Number of decomposition levels forced to 1 (rather than %d)\n", |
6894 | 0 | parameters->numresolution + 1); |
6895 | 0 | parameters->numresolution = 1; |
6896 | 0 | } else if (parameters->numresolution > 7) { |
6897 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6898 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6899 | 0 | "Number of decomposition levels >= 1 && <= 6\n" |
6900 | 0 | "-> Number of decomposition levels forced to 6 (rather than %d)\n", |
6901 | 0 | parameters->numresolution + 1); |
6902 | 0 | parameters->numresolution = 7; |
6903 | 0 | } |
6904 | 0 | break; |
6905 | 0 | default : |
6906 | 0 | break; |
6907 | 0 | } |
6908 | | |
6909 | | /* Precincts */ |
6910 | 0 | parameters->csty |= J2K_CP_CSTY_PRT; |
6911 | 0 | if (parameters->numresolution == 1) { |
6912 | 0 | parameters->res_spec = 1; |
6913 | 0 | parameters->prcw_init[0] = 128; |
6914 | 0 | parameters->prch_init[0] = 128; |
6915 | 0 | } else { |
6916 | 0 | parameters->res_spec = parameters->numresolution - 1; |
6917 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
6918 | 0 | parameters->prcw_init[i] = 256; |
6919 | 0 | parameters->prch_init[i] = 256; |
6920 | 0 | } |
6921 | 0 | } |
6922 | | |
6923 | | /* The progression order shall be CPRL */ |
6924 | 0 | parameters->prog_order = OPJ_CPRL; |
6925 | | |
6926 | | /* Progression order changes for 4K, disallowed for 2K */ |
6927 | 0 | if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) { |
6928 | 0 | parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC, |
6929 | 0 | parameters->numresolution); |
6930 | 0 | } else { |
6931 | 0 | parameters->numpocs = 0; |
6932 | 0 | } |
6933 | | |
6934 | | /* Limited bit-rate */ |
6935 | 0 | parameters->cp_disto_alloc = 1; |
6936 | 0 | if (parameters->max_cs_size <= 0) { |
6937 | | /* No rate has been introduced, 24 fps is assumed */ |
6938 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
6939 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6940 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6941 | 0 | "Maximum 1302083 compressed bytes @ 24fps\n" |
6942 | 0 | "As no rate has been given, this limit will be used.\n"); |
6943 | 0 | } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) { |
6944 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6945 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6946 | 0 | "Maximum 1302083 compressed bytes @ 24fps\n" |
6947 | 0 | "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n"); |
6948 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
6949 | 0 | } |
6950 | |
|
6951 | 0 | if (parameters->max_comp_size <= 0) { |
6952 | | /* No rate has been introduced, 24 fps is assumed */ |
6953 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
6954 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6955 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6956 | 0 | "Maximum 1041666 compressed bytes @ 24fps\n" |
6957 | 0 | "As no rate has been given, this limit will be used.\n"); |
6958 | 0 | } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) { |
6959 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6960 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6961 | 0 | "Maximum 1041666 compressed bytes @ 24fps\n" |
6962 | 0 | "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n"); |
6963 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
6964 | 0 | } |
6965 | |
|
6966 | 0 | parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * |
6967 | 0 | image->comps[0].h * image->comps[0].prec) / |
6968 | 0 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * |
6969 | 0 | image->comps[0].dy); |
6970 | |
|
6971 | 0 | } |
6972 | | |
6973 | | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, |
6974 | | opj_event_mgr_t *p_manager) |
6975 | 0 | { |
6976 | 0 | OPJ_UINT32 i; |
6977 | | |
6978 | | /* Number of components */ |
6979 | 0 | if (image->numcomps != 3) { |
6980 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6981 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6982 | 0 | "3 components" |
6983 | 0 | "-> Number of components of input image (%d) is not compliant\n" |
6984 | 0 | "-> Non-profile-3 codestream will be generated\n", |
6985 | 0 | image->numcomps); |
6986 | 0 | return OPJ_FALSE; |
6987 | 0 | } |
6988 | | |
6989 | | /* Bitdepth */ |
6990 | 0 | for (i = 0; i < image->numcomps; i++) { |
6991 | 0 | if ((image->comps[i].prec != 12) | (image->comps[i].sgnd)) { |
6992 | 0 | char signed_str[] = "signed"; |
6993 | 0 | char unsigned_str[] = "unsigned"; |
6994 | 0 | char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; |
6995 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6996 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6997 | 0 | "Precision of each component shall be 12 bits unsigned" |
6998 | 0 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
6999 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7000 | 0 | i, image->comps[i].prec, tmp_str); |
7001 | 0 | return OPJ_FALSE; |
7002 | 0 | } |
7003 | 0 | } |
7004 | | |
7005 | | /* Image size */ |
7006 | 0 | switch (rsiz) { |
7007 | 0 | case OPJ_PROFILE_CINEMA_2K: |
7008 | 0 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) { |
7009 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7010 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
7011 | 0 | "width <= 2048 and height <= 1080\n" |
7012 | 0 | "-> Input image size %d x %d is not compliant\n" |
7013 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7014 | 0 | image->comps[0].w, image->comps[0].h); |
7015 | 0 | return OPJ_FALSE; |
7016 | 0 | } |
7017 | 0 | break; |
7018 | 0 | case OPJ_PROFILE_CINEMA_4K: |
7019 | 0 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) { |
7020 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7021 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
7022 | 0 | "width <= 4096 and height <= 2160\n" |
7023 | 0 | "-> Image size %d x %d is not compliant\n" |
7024 | 0 | "-> Non-profile-4 codestream will be generated\n", |
7025 | 0 | image->comps[0].w, image->comps[0].h); |
7026 | 0 | return OPJ_FALSE; |
7027 | 0 | } |
7028 | 0 | break; |
7029 | 0 | default : |
7030 | 0 | break; |
7031 | 0 | } |
7032 | | |
7033 | 0 | return OPJ_TRUE; |
7034 | 0 | } |
7035 | | |
7036 | | static int opj_j2k_get_imf_max_NL(opj_cparameters_t *parameters, |
7037 | | opj_image_t *image) |
7038 | 0 | { |
7039 | | /* Decomposition levels */ |
7040 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7041 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7042 | 0 | const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) |
7043 | 0 | parameters->cp_tdx : image->x1; |
7044 | 0 | switch (profile) { |
7045 | 0 | case OPJ_PROFILE_IMF_2K: |
7046 | 0 | return 5; |
7047 | 0 | case OPJ_PROFILE_IMF_4K: |
7048 | 0 | return 6; |
7049 | 0 | case OPJ_PROFILE_IMF_8K: |
7050 | 0 | return 7; |
7051 | 0 | case OPJ_PROFILE_IMF_2K_R: { |
7052 | 0 | if (XTsiz >= 2048) { |
7053 | 0 | return 5; |
7054 | 0 | } else if (XTsiz >= 1024) { |
7055 | 0 | return 4; |
7056 | 0 | } |
7057 | 0 | break; |
7058 | 0 | } |
7059 | 0 | case OPJ_PROFILE_IMF_4K_R: { |
7060 | 0 | if (XTsiz >= 4096) { |
7061 | 0 | return 6; |
7062 | 0 | } else if (XTsiz >= 2048) { |
7063 | 0 | return 5; |
7064 | 0 | } else if (XTsiz >= 1024) { |
7065 | 0 | return 4; |
7066 | 0 | } |
7067 | 0 | break; |
7068 | 0 | } |
7069 | 0 | case OPJ_PROFILE_IMF_8K_R: { |
7070 | 0 | if (XTsiz >= 8192) { |
7071 | 0 | return 7; |
7072 | 0 | } else if (XTsiz >= 4096) { |
7073 | 0 | return 6; |
7074 | 0 | } else if (XTsiz >= 2048) { |
7075 | 0 | return 5; |
7076 | 0 | } else if (XTsiz >= 1024) { |
7077 | 0 | return 4; |
7078 | 0 | } |
7079 | 0 | break; |
7080 | 0 | } |
7081 | 0 | default: |
7082 | 0 | break; |
7083 | 0 | } |
7084 | 0 | return -1; |
7085 | 0 | } |
7086 | | |
7087 | | static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters, |
7088 | | opj_image_t *image, opj_event_mgr_t *p_manager) |
7089 | 0 | { |
7090 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7091 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7092 | |
|
7093 | 0 | OPJ_UNUSED(p_manager); |
7094 | | |
7095 | | /* Override defaults set by opj_set_default_encoder_parameters */ |
7096 | 0 | if (parameters->cblockw_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKW && |
7097 | 0 | parameters->cblockh_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKH) { |
7098 | 0 | parameters->cblockw_init = 32; |
7099 | 0 | parameters->cblockh_init = 32; |
7100 | 0 | } |
7101 | | |
7102 | | /* One tile part for each component */ |
7103 | 0 | parameters->tp_flag = 'C'; |
7104 | 0 | parameters->tp_on = 1; |
7105 | |
|
7106 | 0 | if (parameters->prog_order == OPJ_COMP_PARAM_DEFAULT_PROG_ORDER) { |
7107 | 0 | parameters->prog_order = OPJ_CPRL; |
7108 | 0 | } |
7109 | |
|
7110 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7111 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7112 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7113 | | /* 9-7 transform */ |
7114 | 0 | parameters->irreversible = 1; |
7115 | 0 | } |
7116 | | |
7117 | | /* Adjust the number of resolutions if set to its defaults */ |
7118 | 0 | if (parameters->numresolution == OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION && |
7119 | 0 | image->x0 == 0 && |
7120 | 0 | image->y0 == 0) { |
7121 | 0 | const int max_NL = opj_j2k_get_imf_max_NL(parameters, image); |
7122 | 0 | if (max_NL >= 0 && parameters->numresolution > max_NL) { |
7123 | 0 | parameters->numresolution = max_NL + 1; |
7124 | 0 | } |
7125 | | |
7126 | | /* Note: below is generic logic */ |
7127 | 0 | if (!parameters->tile_size_on) { |
7128 | 0 | while (parameters->numresolution > 0) { |
7129 | 0 | if (image->x1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { |
7130 | 0 | parameters->numresolution --; |
7131 | 0 | continue; |
7132 | 0 | } |
7133 | 0 | if (image->y1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { |
7134 | 0 | parameters->numresolution --; |
7135 | 0 | continue; |
7136 | 0 | } |
7137 | 0 | break; |
7138 | 0 | } |
7139 | 0 | } |
7140 | 0 | } |
7141 | | |
7142 | | /* Set defaults precincts */ |
7143 | 0 | if (parameters->csty == 0) { |
7144 | 0 | parameters->csty |= J2K_CP_CSTY_PRT; |
7145 | 0 | if (parameters->numresolution == 1) { |
7146 | 0 | parameters->res_spec = 1; |
7147 | 0 | parameters->prcw_init[0] = 128; |
7148 | 0 | parameters->prch_init[0] = 128; |
7149 | 0 | } else { |
7150 | 0 | int i; |
7151 | 0 | parameters->res_spec = parameters->numresolution - 1; |
7152 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
7153 | 0 | parameters->prcw_init[i] = 256; |
7154 | 0 | parameters->prch_init[i] = 256; |
7155 | 0 | } |
7156 | 0 | } |
7157 | 0 | } |
7158 | 0 | } |
7159 | | |
7160 | | /* Table A.53 from JPEG2000 standard */ |
7161 | | static const OPJ_UINT16 tabMaxSubLevelFromMainLevel[] = { |
7162 | | 15, /* unspecified */ |
7163 | | 1, |
7164 | | 1, |
7165 | | 1, |
7166 | | 2, |
7167 | | 3, |
7168 | | 4, |
7169 | | 5, |
7170 | | 6, |
7171 | | 7, |
7172 | | 8, |
7173 | | 9 |
7174 | | }; |
7175 | | |
7176 | | static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters, |
7177 | | opj_image_t *image, |
7178 | | opj_event_mgr_t *p_manager) |
7179 | 0 | { |
7180 | 0 | OPJ_UINT32 i; |
7181 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7182 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7183 | 0 | const OPJ_UINT16 mainlevel = OPJ_GET_IMF_MAINLEVEL(rsiz); |
7184 | 0 | const OPJ_UINT16 sublevel = OPJ_GET_IMF_SUBLEVEL(rsiz); |
7185 | 0 | const int NL = parameters->numresolution - 1; |
7186 | 0 | const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) |
7187 | 0 | parameters->cp_tdx : image->x1; |
7188 | 0 | OPJ_BOOL ret = OPJ_TRUE; |
7189 | | |
7190 | | /* Validate mainlevel */ |
7191 | 0 | if (mainlevel > OPJ_IMF_MAINLEVEL_MAX) { |
7192 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7193 | 0 | "IMF profile require mainlevel <= 11.\n" |
7194 | 0 | "-> %d is thus not compliant\n" |
7195 | 0 | "-> Non-IMF codestream will be generated\n", |
7196 | 0 | mainlevel); |
7197 | 0 | ret = OPJ_FALSE; |
7198 | 0 | } else { |
7199 | | /* Validate sublevel */ |
7200 | 0 | assert(sizeof(tabMaxSubLevelFromMainLevel) == |
7201 | 0 | (OPJ_IMF_MAINLEVEL_MAX + 1) * sizeof(tabMaxSubLevelFromMainLevel[0])); |
7202 | 0 | if (sublevel > tabMaxSubLevelFromMainLevel[mainlevel]) { |
7203 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7204 | 0 | "IMF profile require sublevel <= %d for mainlevel = %d.\n" |
7205 | 0 | "-> %d is thus not compliant\n" |
7206 | 0 | "-> Non-IMF codestream will be generated\n", |
7207 | 0 | tabMaxSubLevelFromMainLevel[mainlevel], |
7208 | 0 | mainlevel, |
7209 | 0 | sublevel); |
7210 | 0 | ret = OPJ_FALSE; |
7211 | 0 | } |
7212 | 0 | } |
7213 | | |
7214 | | /* Number of components */ |
7215 | 0 | if (image->numcomps > 3) { |
7216 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7217 | 0 | "IMF profiles require at most 3 components.\n" |
7218 | 0 | "-> Number of components of input image (%d) is not compliant\n" |
7219 | 0 | "-> Non-IMF codestream will be generated\n", |
7220 | 0 | image->numcomps); |
7221 | 0 | ret = OPJ_FALSE; |
7222 | 0 | } |
7223 | |
|
7224 | 0 | if (image->x0 != 0 || image->y0 != 0) { |
7225 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7226 | 0 | "IMF profiles require image origin to be at 0,0.\n" |
7227 | 0 | "-> %d,%d is not compliant\n" |
7228 | 0 | "-> Non-IMF codestream will be generated\n", |
7229 | 0 | image->x0, image->y0 != 0); |
7230 | 0 | ret = OPJ_FALSE; |
7231 | 0 | } |
7232 | |
|
7233 | 0 | if (parameters->cp_tx0 != 0 || parameters->cp_ty0 != 0) { |
7234 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7235 | 0 | "IMF profiles require tile origin to be at 0,0.\n" |
7236 | 0 | "-> %d,%d is not compliant\n" |
7237 | 0 | "-> Non-IMF codestream will be generated\n", |
7238 | 0 | parameters->cp_tx0, parameters->cp_ty0); |
7239 | 0 | ret = OPJ_FALSE; |
7240 | 0 | } |
7241 | |
|
7242 | 0 | if (parameters->tile_size_on) { |
7243 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7244 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7245 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7246 | 0 | if ((OPJ_UINT32)parameters->cp_tdx < image->x1 || |
7247 | 0 | (OPJ_UINT32)parameters->cp_tdy < image->y1) { |
7248 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7249 | 0 | "IMF 2K/4K/8K single tile profiles require tile to be greater or equal to image size.\n" |
7250 | 0 | "-> %d,%d is lesser than %d,%d\n" |
7251 | 0 | "-> Non-IMF codestream will be generated\n", |
7252 | 0 | parameters->cp_tdx, |
7253 | 0 | parameters->cp_tdy, |
7254 | 0 | image->x1, |
7255 | 0 | image->y1); |
7256 | 0 | ret = OPJ_FALSE; |
7257 | 0 | } |
7258 | 0 | } else { |
7259 | 0 | if ((OPJ_UINT32)parameters->cp_tdx >= image->x1 && |
7260 | 0 | (OPJ_UINT32)parameters->cp_tdy >= image->y1) { |
7261 | | /* ok */ |
7262 | 0 | } else if (parameters->cp_tdx == 1024 && |
7263 | 0 | parameters->cp_tdy == 1024) { |
7264 | | /* ok */ |
7265 | 0 | } else if (parameters->cp_tdx == 2048 && |
7266 | 0 | parameters->cp_tdy == 2048 && |
7267 | 0 | (profile == OPJ_PROFILE_IMF_4K || |
7268 | 0 | profile == OPJ_PROFILE_IMF_8K)) { |
7269 | | /* ok */ |
7270 | 0 | } else if (parameters->cp_tdx == 4096 && |
7271 | 0 | parameters->cp_tdy == 4096 && |
7272 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7273 | | /* ok */ |
7274 | 0 | } else { |
7275 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7276 | 0 | "IMF 2K_R/4K_R/8K_R single/multiple tile profiles " |
7277 | 0 | "require tile to be greater or equal to image size,\n" |
7278 | 0 | "or to be (1024,1024), or (2048,2048) for 4K_R/8K_R " |
7279 | 0 | "or (4096,4096) for 8K_R.\n" |
7280 | 0 | "-> %d,%d is non conformant\n" |
7281 | 0 | "-> Non-IMF codestream will be generated\n", |
7282 | 0 | parameters->cp_tdx, |
7283 | 0 | parameters->cp_tdy); |
7284 | 0 | ret = OPJ_FALSE; |
7285 | 0 | } |
7286 | 0 | } |
7287 | 0 | } |
7288 | | |
7289 | | /* Bitdepth */ |
7290 | 0 | for (i = 0; i < image->numcomps; i++) { |
7291 | 0 | if (!(image->comps[i].prec >= 8 && image->comps[i].prec <= 16) || |
7292 | 0 | (image->comps[i].sgnd)) { |
7293 | 0 | char signed_str[] = "signed"; |
7294 | 0 | char unsigned_str[] = "unsigned"; |
7295 | 0 | char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; |
7296 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7297 | 0 | "IMF profiles require precision of each component to b in [8-16] bits unsigned" |
7298 | 0 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
7299 | 0 | "-> Non-IMF codestream will be generated\n", |
7300 | 0 | i, image->comps[i].prec, tmp_str); |
7301 | 0 | ret = OPJ_FALSE; |
7302 | 0 | } |
7303 | 0 | } |
7304 | | |
7305 | | /* Sub-sampling */ |
7306 | 0 | for (i = 0; i < image->numcomps; i++) { |
7307 | 0 | if (i == 0 && image->comps[i].dx != 1) { |
7308 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7309 | 0 | "IMF profiles require XRSiz1 == 1. Here it is set to %d.\n" |
7310 | 0 | "-> Non-IMF codestream will be generated\n", |
7311 | 0 | image->comps[i].dx); |
7312 | 0 | ret = OPJ_FALSE; |
7313 | 0 | } |
7314 | 0 | if (i == 1 && image->comps[i].dx != 1 && image->comps[i].dx != 2) { |
7315 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7316 | 0 | "IMF profiles require XRSiz2 == 1 or 2. Here it is set to %d.\n" |
7317 | 0 | "-> Non-IMF codestream will be generated\n", |
7318 | 0 | image->comps[i].dx); |
7319 | 0 | ret = OPJ_FALSE; |
7320 | 0 | } |
7321 | 0 | if (i > 1 && image->comps[i].dx != image->comps[i - 1].dx) { |
7322 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7323 | 0 | "IMF profiles require XRSiz%d to be the same as XRSiz2. " |
7324 | 0 | "Here it is set to %d instead of %d.\n" |
7325 | 0 | "-> Non-IMF codestream will be generated\n", |
7326 | 0 | i + 1, image->comps[i].dx, image->comps[i - 1].dx); |
7327 | 0 | ret = OPJ_FALSE; |
7328 | 0 | } |
7329 | 0 | if (image->comps[i].dy != 1) { |
7330 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7331 | 0 | "IMF profiles require YRsiz == 1. " |
7332 | 0 | "Here it is set to %d for component %d.\n" |
7333 | 0 | "-> Non-IMF codestream will be generated\n", |
7334 | 0 | image->comps[i].dy, i); |
7335 | 0 | ret = OPJ_FALSE; |
7336 | 0 | } |
7337 | 0 | } |
7338 | | |
7339 | | /* Image size */ |
7340 | 0 | switch (profile) { |
7341 | 0 | case OPJ_PROFILE_IMF_2K: |
7342 | 0 | case OPJ_PROFILE_IMF_2K_R: |
7343 | 0 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1556))) { |
7344 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7345 | 0 | "IMF 2K/2K_R profile require:\n" |
7346 | 0 | "width <= 2048 and height <= 1556\n" |
7347 | 0 | "-> Input image size %d x %d is not compliant\n" |
7348 | 0 | "-> Non-IMF codestream will be generated\n", |
7349 | 0 | image->comps[0].w, image->comps[0].h); |
7350 | 0 | ret = OPJ_FALSE; |
7351 | 0 | } |
7352 | 0 | break; |
7353 | 0 | case OPJ_PROFILE_IMF_4K: |
7354 | 0 | case OPJ_PROFILE_IMF_4K_R: |
7355 | 0 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 3112))) { |
7356 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7357 | 0 | "IMF 4K/4K_R profile require:\n" |
7358 | 0 | "width <= 4096 and height <= 3112\n" |
7359 | 0 | "-> Input image size %d x %d is not compliant\n" |
7360 | 0 | "-> Non-IMF codestream will be generated\n", |
7361 | 0 | image->comps[0].w, image->comps[0].h); |
7362 | 0 | ret = OPJ_FALSE; |
7363 | 0 | } |
7364 | 0 | break; |
7365 | 0 | case OPJ_PROFILE_IMF_8K: |
7366 | 0 | case OPJ_PROFILE_IMF_8K_R: |
7367 | 0 | if (((image->comps[0].w > 8192) | (image->comps[0].h > 6224))) { |
7368 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7369 | 0 | "IMF 8K/8K_R profile require:\n" |
7370 | 0 | "width <= 8192 and height <= 6224\n" |
7371 | 0 | "-> Input image size %d x %d is not compliant\n" |
7372 | 0 | "-> Non-IMF codestream will be generated\n", |
7373 | 0 | image->comps[0].w, image->comps[0].h); |
7374 | 0 | ret = OPJ_FALSE; |
7375 | 0 | } |
7376 | 0 | break; |
7377 | 0 | default : |
7378 | 0 | assert(0); |
7379 | 0 | return OPJ_FALSE; |
7380 | 0 | } |
7381 | | |
7382 | 0 | if (parameters->roi_compno != -1) { |
7383 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7384 | 0 | "IMF profile forbid RGN / region of interest marker.\n" |
7385 | 0 | "-> Compression parameters specify a ROI\n" |
7386 | 0 | "-> Non-IMF codestream will be generated\n"); |
7387 | 0 | ret = OPJ_FALSE; |
7388 | 0 | } |
7389 | |
|
7390 | 0 | if (parameters->cblockw_init != 32 || parameters->cblockh_init != 32) { |
7391 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7392 | 0 | "IMF profile require code block size to be 32x32.\n" |
7393 | 0 | "-> Compression parameters set it to %dx%d.\n" |
7394 | 0 | "-> Non-IMF codestream will be generated\n", |
7395 | 0 | parameters->cblockw_init, |
7396 | 0 | parameters->cblockh_init); |
7397 | 0 | ret = OPJ_FALSE; |
7398 | 0 | } |
7399 | |
|
7400 | 0 | if (parameters->prog_order != OPJ_CPRL) { |
7401 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7402 | 0 | "IMF profile require progression order to be CPRL.\n" |
7403 | 0 | "-> Compression parameters set it to %d.\n" |
7404 | 0 | "-> Non-IMF codestream will be generated\n", |
7405 | 0 | parameters->prog_order); |
7406 | 0 | ret = OPJ_FALSE; |
7407 | 0 | } |
7408 | |
|
7409 | 0 | if (parameters->numpocs != 0) { |
7410 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7411 | 0 | "IMF profile forbid POC markers.\n" |
7412 | 0 | "-> Compression parameters set %d POC.\n" |
7413 | 0 | "-> Non-IMF codestream will be generated\n", |
7414 | 0 | parameters->numpocs); |
7415 | 0 | ret = OPJ_FALSE; |
7416 | 0 | } |
7417 | | |
7418 | | /* Codeblock style: no mode switch enabled */ |
7419 | 0 | if (parameters->mode != 0) { |
7420 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7421 | 0 | "IMF profile forbid mode switch in code block style.\n" |
7422 | 0 | "-> Compression parameters set code block style to %d.\n" |
7423 | 0 | "-> Non-IMF codestream will be generated\n", |
7424 | 0 | parameters->mode); |
7425 | 0 | ret = OPJ_FALSE; |
7426 | 0 | } |
7427 | |
|
7428 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7429 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7430 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7431 | | /* Expect 9-7 transform */ |
7432 | 0 | if (parameters->irreversible != 1) { |
7433 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7434 | 0 | "IMF 2K/4K/8K profiles require 9-7 Irreversible Transform.\n" |
7435 | 0 | "-> Compression parameters set it to reversible.\n" |
7436 | 0 | "-> Non-IMF codestream will be generated\n"); |
7437 | 0 | ret = OPJ_FALSE; |
7438 | 0 | } |
7439 | 0 | } else { |
7440 | | /* Expect 5-3 transform */ |
7441 | 0 | if (parameters->irreversible != 0) { |
7442 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7443 | 0 | "IMF 2K/4K/8K profiles require 5-3 reversible Transform.\n" |
7444 | 0 | "-> Compression parameters set it to irreversible.\n" |
7445 | 0 | "-> Non-IMF codestream will be generated\n"); |
7446 | 0 | ret = OPJ_FALSE; |
7447 | 0 | } |
7448 | 0 | } |
7449 | | |
7450 | | /* Number of layers */ |
7451 | 0 | if (parameters->tcp_numlayers != 1) { |
7452 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7453 | 0 | "IMF 2K/4K/8K profiles require 1 single quality layer.\n" |
7454 | 0 | "-> Number of layers is %d.\n" |
7455 | 0 | "-> Non-IMF codestream will be generated\n", |
7456 | 0 | parameters->tcp_numlayers); |
7457 | 0 | ret = OPJ_FALSE; |
7458 | 0 | } |
7459 | | |
7460 | | /* Decomposition levels */ |
7461 | 0 | switch (profile) { |
7462 | 0 | case OPJ_PROFILE_IMF_2K: |
7463 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7464 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7465 | 0 | "IMF 2K profile requires 1 <= NL <= 5:\n" |
7466 | 0 | "-> Number of decomposition levels is %d.\n" |
7467 | 0 | "-> Non-IMF codestream will be generated\n", |
7468 | 0 | NL); |
7469 | 0 | ret = OPJ_FALSE; |
7470 | 0 | } |
7471 | 0 | break; |
7472 | 0 | case OPJ_PROFILE_IMF_4K: |
7473 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7474 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7475 | 0 | "IMF 4K profile requires 1 <= NL <= 6:\n" |
7476 | 0 | "-> Number of decomposition levels is %d.\n" |
7477 | 0 | "-> Non-IMF codestream will be generated\n", |
7478 | 0 | NL); |
7479 | 0 | ret = OPJ_FALSE; |
7480 | 0 | } |
7481 | 0 | break; |
7482 | 0 | case OPJ_PROFILE_IMF_8K: |
7483 | 0 | if (!(NL >= 1 && NL <= 7)) { |
7484 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7485 | 0 | "IMF 8K profile requires 1 <= NL <= 7:\n" |
7486 | 0 | "-> Number of decomposition levels is %d.\n" |
7487 | 0 | "-> Non-IMF codestream will be generated\n", |
7488 | 0 | NL); |
7489 | 0 | ret = OPJ_FALSE; |
7490 | 0 | } |
7491 | 0 | break; |
7492 | 0 | case OPJ_PROFILE_IMF_2K_R: { |
7493 | 0 | if (XTsiz >= 2048) { |
7494 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7495 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7496 | 0 | "IMF 2K_R profile requires 1 <= NL <= 5 for XTsiz >= 2048:\n" |
7497 | 0 | "-> Number of decomposition levels is %d.\n" |
7498 | 0 | "-> Non-IMF codestream will be generated\n", |
7499 | 0 | NL); |
7500 | 0 | ret = OPJ_FALSE; |
7501 | 0 | } |
7502 | 0 | } else if (XTsiz >= 1024) { |
7503 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7504 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7505 | 0 | "IMF 2K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7506 | 0 | "-> Number of decomposition levels is %d.\n" |
7507 | 0 | "-> Non-IMF codestream will be generated\n", |
7508 | 0 | NL); |
7509 | 0 | ret = OPJ_FALSE; |
7510 | 0 | } |
7511 | 0 | } |
7512 | 0 | break; |
7513 | 0 | } |
7514 | 0 | case OPJ_PROFILE_IMF_4K_R: { |
7515 | 0 | if (XTsiz >= 4096) { |
7516 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7517 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7518 | 0 | "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz >= 4096:\n" |
7519 | 0 | "-> Number of decomposition levels is %d.\n" |
7520 | 0 | "-> Non-IMF codestream will be generated\n", |
7521 | 0 | NL); |
7522 | 0 | ret = OPJ_FALSE; |
7523 | 0 | } |
7524 | 0 | } else if (XTsiz >= 2048) { |
7525 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7526 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7527 | 0 | "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" |
7528 | 0 | "-> Number of decomposition levels is %d.\n" |
7529 | 0 | "-> Non-IMF codestream will be generated\n", |
7530 | 0 | NL); |
7531 | 0 | ret = OPJ_FALSE; |
7532 | 0 | } |
7533 | 0 | } else if (XTsiz >= 1024) { |
7534 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7535 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7536 | 0 | "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7537 | 0 | "-> Number of decomposition levels is %d.\n" |
7538 | 0 | "-> Non-IMF codestream will be generated\n", |
7539 | 0 | NL); |
7540 | 0 | ret = OPJ_FALSE; |
7541 | 0 | } |
7542 | 0 | } |
7543 | 0 | break; |
7544 | 0 | } |
7545 | 0 | case OPJ_PROFILE_IMF_8K_R: { |
7546 | 0 | if (XTsiz >= 8192) { |
7547 | 0 | if (!(NL >= 1 && NL <= 7)) { |
7548 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7549 | 0 | "IMF 4K_R profile requires 1 <= NL <= 7 for XTsiz >= 8192:\n" |
7550 | 0 | "-> Number of decomposition levels is %d.\n" |
7551 | 0 | "-> Non-IMF codestream will be generated\n", |
7552 | 0 | NL); |
7553 | 0 | ret = OPJ_FALSE; |
7554 | 0 | } |
7555 | 0 | } else if (XTsiz >= 4096) { |
7556 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7557 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7558 | 0 | "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz in [4096,8192[:\n" |
7559 | 0 | "-> Number of decomposition levels is %d.\n" |
7560 | 0 | "-> Non-IMF codestream will be generated\n", |
7561 | 0 | NL); |
7562 | 0 | ret = OPJ_FALSE; |
7563 | 0 | } |
7564 | 0 | } else if (XTsiz >= 2048) { |
7565 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7566 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7567 | 0 | "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" |
7568 | 0 | "-> Number of decomposition levels is %d.\n" |
7569 | 0 | "-> Non-IMF codestream will be generated\n", |
7570 | 0 | NL); |
7571 | 0 | ret = OPJ_FALSE; |
7572 | 0 | } |
7573 | 0 | } else if (XTsiz >= 1024) { |
7574 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7575 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7576 | 0 | "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7577 | 0 | "-> Number of decomposition levels is %d.\n" |
7578 | 0 | "-> Non-IMF codestream will be generated\n", |
7579 | 0 | NL); |
7580 | 0 | ret = OPJ_FALSE; |
7581 | 0 | } |
7582 | 0 | } |
7583 | 0 | break; |
7584 | 0 | } |
7585 | 0 | default: |
7586 | 0 | break; |
7587 | 0 | } |
7588 | | |
7589 | 0 | if (parameters->numresolution == 1) { |
7590 | 0 | if (parameters->res_spec != 1 || |
7591 | 0 | parameters->prcw_init[0] != 128 || |
7592 | 0 | parameters->prch_init[0] != 128) { |
7593 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7594 | 0 | "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" |
7595 | 0 | "-> Supplied values are different from that.\n" |
7596 | 0 | "-> Non-IMF codestream will be generated\n"); |
7597 | 0 | ret = OPJ_FALSE; |
7598 | 0 | } |
7599 | 0 | } else { |
7600 | 0 | int i; |
7601 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
7602 | 0 | if (parameters->prcw_init[i] != 256 || |
7603 | 0 | parameters->prch_init[i] != 256) { |
7604 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7605 | 0 | "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" |
7606 | 0 | "-> Supplied values are different from that.\n" |
7607 | 0 | "-> Non-IMF codestream will be generated\n"); |
7608 | 0 | ret = OPJ_FALSE; |
7609 | 0 | } |
7610 | 0 | } |
7611 | 0 | } |
7612 | |
|
7613 | 0 | return ret; |
7614 | 0 | } |
7615 | | |
7616 | | |
7617 | | OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, |
7618 | | opj_cparameters_t *parameters, |
7619 | | opj_image_t *image, |
7620 | | opj_event_mgr_t * p_manager) |
7621 | 0 | { |
7622 | 0 | OPJ_UINT32 i, j, tileno, numpocs_tile; |
7623 | 0 | opj_cp_t *cp = 00; |
7624 | 0 | OPJ_UINT32 cblkw, cblkh; |
7625 | |
|
7626 | 0 | if (!p_j2k || !parameters || ! image) { |
7627 | 0 | return OPJ_FALSE; |
7628 | 0 | } |
7629 | | |
7630 | 0 | if ((parameters->numresolution <= 0) || |
7631 | 0 | (parameters->numresolution > OPJ_J2K_MAXRLVLS)) { |
7632 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7633 | 0 | "Invalid number of resolutions : %d not in range [1,%d]\n", |
7634 | 0 | parameters->numresolution, OPJ_J2K_MAXRLVLS); |
7635 | 0 | return OPJ_FALSE; |
7636 | 0 | } |
7637 | | |
7638 | 0 | if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) { |
7639 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7640 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7641 | 0 | parameters->cblockw_init); |
7642 | 0 | return OPJ_FALSE; |
7643 | 0 | } |
7644 | 0 | if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) { |
7645 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7646 | 0 | "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n", |
7647 | 0 | parameters->cblockh_init); |
7648 | 0 | return OPJ_FALSE; |
7649 | 0 | } |
7650 | 0 | if (parameters->cblockw_init * parameters->cblockh_init > 4096) { |
7651 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7652 | 0 | "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n"); |
7653 | 0 | return OPJ_FALSE; |
7654 | 0 | } |
7655 | 0 | cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
7656 | 0 | cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
7657 | 0 | if (parameters->cblockw_init != (1 << cblkw)) { |
7658 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7659 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7660 | 0 | parameters->cblockw_init); |
7661 | 0 | return OPJ_FALSE; |
7662 | 0 | } |
7663 | 0 | if (parameters->cblockh_init != (1 << cblkh)) { |
7664 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7665 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7666 | 0 | parameters->cblockh_init); |
7667 | 0 | return OPJ_FALSE; |
7668 | 0 | } |
7669 | | |
7670 | 0 | if (parameters->cp_fixed_alloc) { |
7671 | 0 | if (parameters->cp_matrice == NULL) { |
7672 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7673 | 0 | "cp_fixed_alloc set, but cp_matrice missing\n"); |
7674 | 0 | return OPJ_FALSE; |
7675 | 0 | } |
7676 | | |
7677 | 0 | if (parameters->tcp_numlayers > J2K_TCD_MATRIX_MAX_LAYER_COUNT) { |
7678 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7679 | 0 | "tcp_numlayers when cp_fixed_alloc set should not exceed %d\n", |
7680 | 0 | J2K_TCD_MATRIX_MAX_LAYER_COUNT); |
7681 | 0 | return OPJ_FALSE; |
7682 | 0 | } |
7683 | 0 | if (parameters->numresolution > J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT) { |
7684 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7685 | 0 | "numresolution when cp_fixed_alloc set should not exceed %d\n", |
7686 | 0 | J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT); |
7687 | 0 | return OPJ_FALSE; |
7688 | 0 | } |
7689 | 0 | } |
7690 | | |
7691 | 0 | p_j2k->m_specific_param.m_encoder.m_nb_comps = image->numcomps; |
7692 | | |
7693 | | /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ |
7694 | 0 | cp = &(p_j2k->m_cp); |
7695 | | |
7696 | | /* set default values for cp */ |
7697 | 0 | cp->tw = 1; |
7698 | 0 | cp->th = 1; |
7699 | | |
7700 | | /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */ |
7701 | 0 | if (parameters->rsiz == |
7702 | 0 | OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */ |
7703 | 0 | OPJ_BOOL deprecated_used = OPJ_FALSE; |
7704 | 0 | switch (parameters->cp_cinema) { |
7705 | 0 | case OPJ_CINEMA2K_24: |
7706 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7707 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7708 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7709 | 0 | deprecated_used = OPJ_TRUE; |
7710 | 0 | break; |
7711 | 0 | case OPJ_CINEMA2K_48: |
7712 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7713 | 0 | parameters->max_cs_size = OPJ_CINEMA_48_CS; |
7714 | 0 | parameters->max_comp_size = OPJ_CINEMA_48_COMP; |
7715 | 0 | deprecated_used = OPJ_TRUE; |
7716 | 0 | break; |
7717 | 0 | case OPJ_CINEMA4K_24: |
7718 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
7719 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7720 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7721 | 0 | deprecated_used = OPJ_TRUE; |
7722 | 0 | break; |
7723 | 0 | case OPJ_OFF: |
7724 | 0 | default: |
7725 | 0 | break; |
7726 | 0 | } |
7727 | 0 | switch (parameters->cp_rsiz) { |
7728 | 0 | case OPJ_CINEMA2K: |
7729 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7730 | 0 | deprecated_used = OPJ_TRUE; |
7731 | 0 | break; |
7732 | 0 | case OPJ_CINEMA4K: |
7733 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
7734 | 0 | deprecated_used = OPJ_TRUE; |
7735 | 0 | break; |
7736 | 0 | case OPJ_MCT: |
7737 | 0 | parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT; |
7738 | 0 | deprecated_used = OPJ_TRUE; |
7739 | 0 | case OPJ_STD_RSIZ: |
7740 | 0 | default: |
7741 | 0 | break; |
7742 | 0 | } |
7743 | 0 | if (deprecated_used) { |
7744 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7745 | 0 | "Deprecated fields cp_cinema or cp_rsiz are used\n" |
7746 | 0 | "Please consider using only the rsiz field\n" |
7747 | 0 | "See openjpeg.h documentation for more details\n"); |
7748 | 0 | } |
7749 | 0 | } |
7750 | | |
7751 | | /* If no explicit layers are provided, use lossless settings */ |
7752 | 0 | if (parameters->tcp_numlayers == 0) { |
7753 | 0 | parameters->tcp_numlayers = 1; |
7754 | 0 | parameters->cp_disto_alloc = 1; |
7755 | 0 | parameters->tcp_rates[0] = 0; |
7756 | 0 | } |
7757 | |
|
7758 | 0 | if (parameters->cp_disto_alloc) { |
7759 | | /* Emit warnings if tcp_rates are not decreasing */ |
7760 | 0 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7761 | 0 | OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i]; |
7762 | 0 | OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1]; |
7763 | 0 | if (rate_i_corr <= 1.0) { |
7764 | 0 | rate_i_corr = 1.0; |
7765 | 0 | } |
7766 | 0 | if (rate_i_m_1_corr <= 1.0) { |
7767 | 0 | rate_i_m_1_corr = 1.0; |
7768 | 0 | } |
7769 | 0 | if (rate_i_corr >= rate_i_m_1_corr) { |
7770 | 0 | if (rate_i_corr != parameters->tcp_rates[i] && |
7771 | 0 | rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
7772 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7773 | 0 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
7774 | 0 | "than tcp_rates[%d]=%f (corrected as %f)\n", |
7775 | 0 | i, parameters->tcp_rates[i], rate_i_corr, |
7776 | 0 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
7777 | 0 | } else if (rate_i_corr != parameters->tcp_rates[i]) { |
7778 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7779 | 0 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
7780 | 0 | "than tcp_rates[%d]=%f\n", |
7781 | 0 | i, parameters->tcp_rates[i], rate_i_corr, |
7782 | 0 | i - 1, parameters->tcp_rates[i - 1]); |
7783 | 0 | } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
7784 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7785 | 0 | "tcp_rates[%d]=%f should be strictly lesser " |
7786 | 0 | "than tcp_rates[%d]=%f (corrected as %f)\n", |
7787 | 0 | i, parameters->tcp_rates[i], |
7788 | 0 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
7789 | 0 | } else { |
7790 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7791 | 0 | "tcp_rates[%d]=%f should be strictly lesser " |
7792 | 0 | "than tcp_rates[%d]=%f\n", |
7793 | 0 | i, parameters->tcp_rates[i], |
7794 | 0 | i - 1, parameters->tcp_rates[i - 1]); |
7795 | 0 | } |
7796 | 0 | } |
7797 | 0 | } |
7798 | 0 | } else if (parameters->cp_fixed_quality) { |
7799 | | /* Emit warnings if tcp_distoratio are not increasing */ |
7800 | 0 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7801 | 0 | if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] && |
7802 | 0 | !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 && |
7803 | 0 | parameters->tcp_distoratio[i] == 0)) { |
7804 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7805 | 0 | "tcp_distoratio[%d]=%f should be strictly greater " |
7806 | 0 | "than tcp_distoratio[%d]=%f\n", |
7807 | 0 | i, parameters->tcp_distoratio[i], i - 1, |
7808 | 0 | parameters->tcp_distoratio[i - 1]); |
7809 | 0 | } |
7810 | 0 | } |
7811 | 0 | } |
7812 | | |
7813 | | /* see if max_codestream_size does limit input rate */ |
7814 | 0 | if (parameters->max_cs_size <= 0) { |
7815 | 0 | if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) { |
7816 | 0 | OPJ_FLOAT32 temp_size; |
7817 | 0 | temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
7818 | 0 | image->comps[0].h * image->comps[0].prec) / |
7819 | 0 | ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 * |
7820 | 0 | image->comps[0].dx * image->comps[0].dy)); |
7821 | 0 | if (temp_size > (OPJ_FLOAT32)INT_MAX) { |
7822 | 0 | parameters->max_cs_size = INT_MAX; |
7823 | 0 | } else { |
7824 | 0 | parameters->max_cs_size = (int) floor(temp_size); |
7825 | 0 | } |
7826 | 0 | } else { |
7827 | 0 | parameters->max_cs_size = 0; |
7828 | 0 | } |
7829 | 0 | } else { |
7830 | 0 | OPJ_FLOAT32 temp_rate; |
7831 | 0 | OPJ_BOOL cap = OPJ_FALSE; |
7832 | |
|
7833 | 0 | if (OPJ_IS_IMF(parameters->rsiz) && parameters->max_cs_size > 0 && |
7834 | 0 | parameters->tcp_numlayers == 1 && parameters->tcp_rates[0] == 0) { |
7835 | 0 | parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * |
7836 | 0 | image->comps[0].h * image->comps[0].prec) / |
7837 | 0 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7838 | 0 | image->comps[0].dy); |
7839 | 0 | } |
7840 | |
|
7841 | 0 | temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
7842 | 0 | image->comps[0].h * image->comps[0].prec) / |
7843 | 0 | (((double)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7844 | 0 | image->comps[0].dy)); |
7845 | 0 | for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7846 | 0 | if (parameters->tcp_rates[i] < temp_rate) { |
7847 | 0 | parameters->tcp_rates[i] = temp_rate; |
7848 | 0 | cap = OPJ_TRUE; |
7849 | 0 | } |
7850 | 0 | } |
7851 | 0 | if (cap) { |
7852 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7853 | 0 | "The desired maximum codestream size has limited\n" |
7854 | 0 | "at least one of the desired quality layers\n"); |
7855 | 0 | } |
7856 | 0 | } |
7857 | |
|
7858 | 0 | if (OPJ_IS_CINEMA(parameters->rsiz) || OPJ_IS_IMF(parameters->rsiz)) { |
7859 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE; |
7860 | 0 | } |
7861 | | |
7862 | | /* Manage profiles and applications and set RSIZ */ |
7863 | | /* set cinema parameters if required */ |
7864 | 0 | if (OPJ_IS_CINEMA(parameters->rsiz)) { |
7865 | 0 | if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K) |
7866 | 0 | || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) { |
7867 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7868 | 0 | "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n"); |
7869 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7870 | 0 | } else { |
7871 | 0 | opj_j2k_set_cinema_parameters(parameters, image, p_manager); |
7872 | 0 | if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) { |
7873 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7874 | 0 | } |
7875 | 0 | } |
7876 | 0 | } else if (OPJ_IS_STORAGE(parameters->rsiz)) { |
7877 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7878 | 0 | "JPEG 2000 Long Term Storage profile not yet supported\n"); |
7879 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7880 | 0 | } else if (OPJ_IS_BROADCAST(parameters->rsiz)) { |
7881 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7882 | 0 | "JPEG 2000 Broadcast profiles not yet supported\n"); |
7883 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7884 | 0 | } else if (OPJ_IS_IMF(parameters->rsiz)) { |
7885 | 0 | opj_j2k_set_imf_parameters(parameters, image, p_manager); |
7886 | 0 | if (!opj_j2k_is_imf_compliant(parameters, image, p_manager)) { |
7887 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7888 | 0 | } |
7889 | 0 | } else if (OPJ_IS_PART2(parameters->rsiz)) { |
7890 | 0 | if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) { |
7891 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7892 | 0 | "JPEG 2000 Part-2 profile defined\n" |
7893 | 0 | "but no Part-2 extension enabled.\n" |
7894 | 0 | "Profile set to NONE.\n"); |
7895 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7896 | 0 | } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) { |
7897 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7898 | 0 | "Unsupported Part-2 extension enabled\n" |
7899 | 0 | "Profile set to NONE.\n"); |
7900 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7901 | 0 | } |
7902 | 0 | } |
7903 | | |
7904 | | /* |
7905 | | copy user encoding parameters |
7906 | | */ |
7907 | 0 | cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32) |
7908 | 0 | parameters->max_comp_size; |
7909 | 0 | cp->rsiz = parameters->rsiz; |
7910 | 0 | if (parameters->cp_fixed_alloc) { |
7911 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = FIXED_LAYER; |
7912 | 0 | } else if (parameters->cp_fixed_quality) { |
7913 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = |
7914 | 0 | FIXED_DISTORTION_RATIO; |
7915 | 0 | } else { |
7916 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = |
7917 | 0 | RATE_DISTORTION_RATIO; |
7918 | 0 | } |
7919 | |
|
7920 | 0 | if (parameters->cp_fixed_alloc) { |
7921 | 0 | size_t array_size = (size_t)parameters->tcp_numlayers * |
7922 | 0 | (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32); |
7923 | 0 | cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); |
7924 | 0 | if (!cp->m_specific_param.m_enc.m_matrice) { |
7925 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7926 | 0 | "Not enough memory to allocate copy of user encoding parameters matrix \n"); |
7927 | 0 | return OPJ_FALSE; |
7928 | 0 | } |
7929 | 0 | memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, |
7930 | 0 | array_size); |
7931 | 0 | } |
7932 | | |
7933 | | /* tiles */ |
7934 | 0 | cp->tdx = (OPJ_UINT32)parameters->cp_tdx; |
7935 | 0 | cp->tdy = (OPJ_UINT32)parameters->cp_tdy; |
7936 | | |
7937 | | /* tile offset */ |
7938 | 0 | cp->tx0 = (OPJ_UINT32)parameters->cp_tx0; |
7939 | 0 | cp->ty0 = (OPJ_UINT32)parameters->cp_ty0; |
7940 | | |
7941 | | /* comment string */ |
7942 | 0 | if (parameters->cp_comment) { |
7943 | 0 | cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U); |
7944 | 0 | if (!cp->comment) { |
7945 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7946 | 0 | "Not enough memory to allocate copy of comment string\n"); |
7947 | 0 | return OPJ_FALSE; |
7948 | 0 | } |
7949 | 0 | strcpy(cp->comment, parameters->cp_comment); |
7950 | 0 | } else { |
7951 | | /* Create default comment for codestream */ |
7952 | 0 | const char comment[] = "Created by OpenJPEG version "; |
7953 | 0 | const size_t clen = strlen(comment); |
7954 | 0 | const char *version = opj_version(); |
7955 | | |
7956 | | /* UniPG>> */ |
7957 | | #ifdef USE_JPWL |
7958 | | const size_t cp_comment_buf_size = clen + strlen(version) + 11; |
7959 | | cp->comment = (char*)opj_malloc(cp_comment_buf_size); |
7960 | | if (!cp->comment) { |
7961 | | opj_event_msg(p_manager, EVT_ERROR, |
7962 | | "Not enough memory to allocate comment string\n"); |
7963 | | return OPJ_FALSE; |
7964 | | } |
7965 | | snprintf(cp->comment, cp_comment_buf_size, "%s%s with JPWL", |
7966 | | comment, version); |
7967 | | #else |
7968 | 0 | const size_t cp_comment_buf_size = clen + strlen(version) + 1; |
7969 | 0 | cp->comment = (char*)opj_malloc(cp_comment_buf_size); |
7970 | 0 | if (!cp->comment) { |
7971 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7972 | 0 | "Not enough memory to allocate comment string\n"); |
7973 | 0 | return OPJ_FALSE; |
7974 | 0 | } |
7975 | 0 | snprintf(cp->comment, cp_comment_buf_size, "%s%s", comment, version); |
7976 | 0 | #endif |
7977 | | /* <<UniPG */ |
7978 | 0 | } |
7979 | | |
7980 | | /* |
7981 | | calculate other encoding parameters |
7982 | | */ |
7983 | | |
7984 | 0 | if (parameters->tile_size_on) { |
7985 | 0 | if (cp->tdx == 0) { |
7986 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile width\n"); |
7987 | 0 | return OPJ_FALSE; |
7988 | 0 | } |
7989 | 0 | if (cp->tdy == 0) { |
7990 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile height\n"); |
7991 | 0 | return OPJ_FALSE; |
7992 | 0 | } |
7993 | 0 | cp->tw = opj_uint_ceildiv(image->x1 - cp->tx0, cp->tdx); |
7994 | 0 | cp->th = opj_uint_ceildiv(image->y1 - cp->ty0, cp->tdy); |
7995 | | /* Check that the number of tiles is valid */ |
7996 | 0 | if (cp->tw > 65535 / cp->th) { |
7997 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7998 | 0 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", |
7999 | 0 | cp->tw, cp->th); |
8000 | 0 | return OPJ_FALSE; |
8001 | 0 | } |
8002 | 0 | } else { |
8003 | 0 | cp->tdx = image->x1 - cp->tx0; |
8004 | 0 | cp->tdy = image->y1 - cp->ty0; |
8005 | 0 | } |
8006 | | |
8007 | 0 | if (parameters->tp_on) { |
8008 | 0 | cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag; |
8009 | 0 | cp->m_specific_param.m_enc.m_tp_on = 1; |
8010 | 0 | } |
8011 | |
|
8012 | | #ifdef USE_JPWL |
8013 | | /* |
8014 | | calculate JPWL encoding parameters |
8015 | | */ |
8016 | | |
8017 | | if (parameters->jpwl_epc_on) { |
8018 | | OPJ_INT32 i; |
8019 | | |
8020 | | /* set JPWL on */ |
8021 | | cp->epc_on = OPJ_TRUE; |
8022 | | cp->info_on = OPJ_FALSE; /* no informative technique */ |
8023 | | |
8024 | | /* set EPB on */ |
8025 | | if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { |
8026 | | cp->epb_on = OPJ_TRUE; |
8027 | | |
8028 | | cp->hprot_MH = parameters->jpwl_hprot_MH; |
8029 | | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
8030 | | cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; |
8031 | | cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; |
8032 | | } |
8033 | | /* if tile specs are not specified, copy MH specs */ |
8034 | | if (cp->hprot_TPH[0] == -1) { |
8035 | | cp->hprot_TPH_tileno[0] = 0; |
8036 | | cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; |
8037 | | } |
8038 | | for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { |
8039 | | cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; |
8040 | | cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; |
8041 | | cp->pprot[i] = parameters->jpwl_pprot[i]; |
8042 | | } |
8043 | | } |
8044 | | |
8045 | | /* set ESD writing */ |
8046 | | if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { |
8047 | | cp->esd_on = OPJ_TRUE; |
8048 | | |
8049 | | cp->sens_size = parameters->jpwl_sens_size; |
8050 | | cp->sens_addr = parameters->jpwl_sens_addr; |
8051 | | cp->sens_range = parameters->jpwl_sens_range; |
8052 | | |
8053 | | cp->sens_MH = parameters->jpwl_sens_MH; |
8054 | | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
8055 | | cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; |
8056 | | cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; |
8057 | | } |
8058 | | } |
8059 | | |
8060 | | /* always set RED writing to false: we are at the encoder */ |
8061 | | cp->red_on = OPJ_FALSE; |
8062 | | |
8063 | | } else { |
8064 | | cp->epc_on = OPJ_FALSE; |
8065 | | } |
8066 | | #endif /* USE_JPWL */ |
8067 | | |
8068 | | /* initialize the multiple tiles */ |
8069 | | /* ---------------------------- */ |
8070 | 0 | cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); |
8071 | 0 | if (!cp->tcps) { |
8072 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8073 | 0 | "Not enough memory to allocate tile coding parameters\n"); |
8074 | 0 | return OPJ_FALSE; |
8075 | 0 | } |
8076 | | |
8077 | 0 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
8078 | 0 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
8079 | 0 | const OPJ_BOOL fixed_distoratio = |
8080 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy == |
8081 | 0 | FIXED_DISTORTION_RATIO; |
8082 | 0 | tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers; |
8083 | |
|
8084 | 0 | for (j = 0; j < tcp->numlayers; j++) { |
8085 | 0 | if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) { |
8086 | 0 | if (fixed_distoratio) { |
8087 | 0 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
8088 | 0 | } |
8089 | 0 | tcp->rates[j] = parameters->tcp_rates[j]; |
8090 | 0 | } else { |
8091 | 0 | if (fixed_distoratio) { |
8092 | 0 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
8093 | 0 | } else { |
8094 | 0 | tcp->rates[j] = parameters->tcp_rates[j]; |
8095 | 0 | } |
8096 | 0 | } |
8097 | 0 | if (!fixed_distoratio && |
8098 | 0 | tcp->rates[j] <= 1.0) { |
8099 | 0 | tcp->rates[j] = 0.0; /* force lossless */ |
8100 | 0 | } |
8101 | 0 | } |
8102 | |
|
8103 | 0 | tcp->csty = (OPJ_UINT32)parameters->csty; |
8104 | 0 | tcp->prg = parameters->prog_order; |
8105 | 0 | tcp->mct = (OPJ_UINT32)parameters->tcp_mct; |
8106 | |
|
8107 | 0 | numpocs_tile = 0; |
8108 | 0 | tcp->POC = 0; |
8109 | |
|
8110 | 0 | if (parameters->numpocs) { |
8111 | | /* initialisation of POC */ |
8112 | 0 | for (i = 0; i < parameters->numpocs; i++) { |
8113 | 0 | if (tileno + 1 == parameters->POC[i].tile) { |
8114 | 0 | opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; |
8115 | |
|
8116 | 0 | if (parameters->POC[numpocs_tile].compno0 >= image->numcomps) { |
8117 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8118 | 0 | "Invalid compno0 for POC %d\n", i); |
8119 | 0 | return OPJ_FALSE; |
8120 | 0 | } |
8121 | | |
8122 | 0 | tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; |
8123 | 0 | tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; |
8124 | 0 | tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; |
8125 | 0 | tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; |
8126 | 0 | tcp_poc->compno1 = opj_uint_min(parameters->POC[numpocs_tile].compno1, |
8127 | 0 | image->numcomps); |
8128 | 0 | tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; |
8129 | 0 | tcp_poc->tile = parameters->POC[numpocs_tile].tile; |
8130 | |
|
8131 | 0 | numpocs_tile++; |
8132 | 0 | } |
8133 | 0 | } |
8134 | | |
8135 | 0 | if (numpocs_tile) { |
8136 | | |
8137 | | /* TODO MSD use the return value*/ |
8138 | 0 | opj_j2k_check_poc_val(parameters->POC, tileno, parameters->numpocs, |
8139 | 0 | (OPJ_UINT32)parameters->numresolution, image->numcomps, |
8140 | 0 | (OPJ_UINT32)parameters->tcp_numlayers, p_manager); |
8141 | |
|
8142 | 0 | tcp->POC = 1; |
8143 | 0 | tcp->numpocs = numpocs_tile - 1 ; |
8144 | 0 | } |
8145 | 0 | } else { |
8146 | 0 | tcp->numpocs = 0; |
8147 | 0 | } |
8148 | | |
8149 | 0 | tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); |
8150 | 0 | if (!tcp->tccps) { |
8151 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8152 | 0 | "Not enough memory to allocate tile component coding parameters\n"); |
8153 | 0 | return OPJ_FALSE; |
8154 | 0 | } |
8155 | 0 | if (parameters->mct_data) { |
8156 | |
|
8157 | 0 | OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof( |
8158 | 0 | OPJ_FLOAT32); |
8159 | 0 | OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8160 | 0 | OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data + |
8161 | 0 | lMctSize); |
8162 | |
|
8163 | 0 | if (!lTmpBuf) { |
8164 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8165 | 0 | "Not enough memory to allocate temp buffer\n"); |
8166 | 0 | return OPJ_FALSE; |
8167 | 0 | } |
8168 | | |
8169 | 0 | tcp->mct = 2; |
8170 | 0 | tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8171 | 0 | if (! tcp->m_mct_coding_matrix) { |
8172 | 0 | opj_free(lTmpBuf); |
8173 | 0 | lTmpBuf = NULL; |
8174 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8175 | 0 | "Not enough memory to allocate encoder MCT coding matrix \n"); |
8176 | 0 | return OPJ_FALSE; |
8177 | 0 | } |
8178 | 0 | memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize); |
8179 | 0 | memcpy(lTmpBuf, parameters->mct_data, lMctSize); |
8180 | |
|
8181 | 0 | tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8182 | 0 | if (! tcp->m_mct_decoding_matrix) { |
8183 | 0 | opj_free(lTmpBuf); |
8184 | 0 | lTmpBuf = NULL; |
8185 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8186 | 0 | "Not enough memory to allocate encoder MCT decoding matrix \n"); |
8187 | 0 | return OPJ_FALSE; |
8188 | 0 | } |
8189 | 0 | if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix), |
8190 | 0 | image->numcomps) == OPJ_FALSE) { |
8191 | 0 | opj_free(lTmpBuf); |
8192 | 0 | lTmpBuf = NULL; |
8193 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8194 | 0 | "Failed to inverse encoder MCT decoding matrix \n"); |
8195 | 0 | return OPJ_FALSE; |
8196 | 0 | } |
8197 | | |
8198 | 0 | tcp->mct_norms = (OPJ_FLOAT64*) |
8199 | 0 | opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); |
8200 | 0 | if (! tcp->mct_norms) { |
8201 | 0 | opj_free(lTmpBuf); |
8202 | 0 | lTmpBuf = NULL; |
8203 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8204 | 0 | "Not enough memory to allocate encoder MCT norms \n"); |
8205 | 0 | return OPJ_FALSE; |
8206 | 0 | } |
8207 | 0 | opj_calculate_norms(tcp->mct_norms, image->numcomps, |
8208 | 0 | tcp->m_mct_decoding_matrix); |
8209 | 0 | opj_free(lTmpBuf); |
8210 | |
|
8211 | 0 | for (i = 0; i < image->numcomps; i++) { |
8212 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8213 | 0 | tccp->m_dc_level_shift = l_dc_shift[i]; |
8214 | 0 | } |
8215 | |
|
8216 | 0 | if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) { |
8217 | | /* free will be handled by opj_j2k_destroy */ |
8218 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n"); |
8219 | 0 | return OPJ_FALSE; |
8220 | 0 | } |
8221 | 0 | } else { |
8222 | 0 | if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */ |
8223 | 0 | if ((image->comps[0].dx != image->comps[1].dx) || |
8224 | 0 | (image->comps[0].dx != image->comps[2].dx) || |
8225 | 0 | (image->comps[0].dy != image->comps[1].dy) || |
8226 | 0 | (image->comps[0].dy != image->comps[2].dy)) { |
8227 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
8228 | 0 | "Cannot perform MCT on components with different sizes. Disabling MCT.\n"); |
8229 | 0 | tcp->mct = 0; |
8230 | 0 | } |
8231 | 0 | } |
8232 | 0 | for (i = 0; i < image->numcomps; i++) { |
8233 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8234 | 0 | opj_image_comp_t * l_comp = &(image->comps[i]); |
8235 | |
|
8236 | 0 | if (! l_comp->sgnd) { |
8237 | 0 | tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); |
8238 | 0 | } |
8239 | 0 | } |
8240 | 0 | } |
8241 | | |
8242 | 0 | for (i = 0; i < image->numcomps; i++) { |
8243 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8244 | |
|
8245 | 0 | tccp->csty = parameters->csty & |
8246 | 0 | 0x01; /* 0 => one precinct || 1 => custom precinct */ |
8247 | 0 | tccp->numresolutions = (OPJ_UINT32)parameters->numresolution; |
8248 | 0 | tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
8249 | 0 | tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
8250 | 0 | tccp->cblksty = (OPJ_UINT32)parameters->mode; |
8251 | 0 | tccp->qmfbid = parameters->irreversible ? 0 : 1; |
8252 | 0 | tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : |
8253 | 0 | J2K_CCP_QNTSTY_NOQNT; |
8254 | 0 | tccp->numgbits = 2; |
8255 | |
|
8256 | 0 | if ((OPJ_INT32)i == parameters->roi_compno) { |
8257 | 0 | tccp->roishift = parameters->roi_shift; |
8258 | 0 | } else { |
8259 | 0 | tccp->roishift = 0; |
8260 | 0 | } |
8261 | |
|
8262 | 0 | if (parameters->csty & J2K_CCP_CSTY_PRT) { |
8263 | 0 | OPJ_INT32 p = 0, it_res; |
8264 | 0 | assert(tccp->numresolutions > 0); |
8265 | 0 | for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) { |
8266 | 0 | if (p < parameters->res_spec) { |
8267 | |
|
8268 | 0 | if (parameters->prcw_init[p] < 1) { |
8269 | 0 | tccp->prcw[it_res] = 1; |
8270 | 0 | } else { |
8271 | 0 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]); |
8272 | 0 | } |
8273 | |
|
8274 | 0 | if (parameters->prch_init[p] < 1) { |
8275 | 0 | tccp->prch[it_res] = 1; |
8276 | 0 | } else { |
8277 | 0 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]); |
8278 | 0 | } |
8279 | |
|
8280 | 0 | } else { |
8281 | 0 | OPJ_INT32 res_spec = parameters->res_spec; |
8282 | 0 | OPJ_INT32 size_prcw = 0; |
8283 | 0 | OPJ_INT32 size_prch = 0; |
8284 | |
|
8285 | 0 | assert(res_spec > 0); /* issue 189 */ |
8286 | 0 | size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); |
8287 | 0 | size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); |
8288 | | |
8289 | |
|
8290 | 0 | if (size_prcw < 1) { |
8291 | 0 | tccp->prcw[it_res] = 1; |
8292 | 0 | } else { |
8293 | 0 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw); |
8294 | 0 | } |
8295 | |
|
8296 | 0 | if (size_prch < 1) { |
8297 | 0 | tccp->prch[it_res] = 1; |
8298 | 0 | } else { |
8299 | 0 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch); |
8300 | 0 | } |
8301 | 0 | } |
8302 | 0 | p++; |
8303 | | /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ |
8304 | 0 | } /*end for*/ |
8305 | 0 | } else { |
8306 | 0 | for (j = 0; j < tccp->numresolutions; j++) { |
8307 | 0 | tccp->prcw[j] = 15; |
8308 | 0 | tccp->prch[j] = 15; |
8309 | 0 | } |
8310 | 0 | } |
8311 | |
|
8312 | 0 | opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); |
8313 | 0 | } |
8314 | 0 | } |
8315 | | |
8316 | 0 | if (parameters->mct_data) { |
8317 | 0 | opj_free(parameters->mct_data); |
8318 | 0 | parameters->mct_data = 00; |
8319 | 0 | } |
8320 | 0 | return OPJ_TRUE; |
8321 | 0 | } |
8322 | | |
8323 | | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, |
8324 | | OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) |
8325 | 316k | { |
8326 | 316k | assert(cstr_index != 00); |
8327 | | |
8328 | | /* expand the list? */ |
8329 | 316k | if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { |
8330 | 2.21k | opj_marker_info_t *new_marker; |
8331 | 2.21k | cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) |
8332 | 2.21k | cstr_index->maxmarknum); |
8333 | 2.21k | new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, |
8334 | 2.21k | cstr_index->maxmarknum * sizeof(opj_marker_info_t)); |
8335 | 2.21k | if (! new_marker) { |
8336 | 0 | opj_free(cstr_index->marker); |
8337 | 0 | cstr_index->marker = NULL; |
8338 | 0 | cstr_index->maxmarknum = 0; |
8339 | 0 | cstr_index->marknum = 0; |
8340 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */ |
8341 | 0 | return OPJ_FALSE; |
8342 | 0 | } |
8343 | 2.21k | cstr_index->marker = new_marker; |
8344 | 2.21k | } |
8345 | | |
8346 | | /* add the marker */ |
8347 | 316k | cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; |
8348 | 316k | cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; |
8349 | 316k | cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; |
8350 | 316k | cstr_index->marknum++; |
8351 | 316k | return OPJ_TRUE; |
8352 | 316k | } |
8353 | | |
8354 | | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, |
8355 | | opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, |
8356 | | OPJ_UINT32 len) |
8357 | 39.1k | { |
8358 | 39.1k | assert(cstr_index != 00); |
8359 | 39.1k | assert(cstr_index->tile_index != 00); |
8360 | | |
8361 | | /* expand the list? */ |
8362 | 39.1k | if ((cstr_index->tile_index[tileno].marknum + 1) > |
8363 | 39.1k | cstr_index->tile_index[tileno].maxmarknum) { |
8364 | 44 | opj_marker_info_t *new_marker; |
8365 | 44 | cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + |
8366 | 44 | (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum); |
8367 | 44 | new_marker = (opj_marker_info_t *) opj_realloc( |
8368 | 44 | cstr_index->tile_index[tileno].marker, |
8369 | 44 | cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t)); |
8370 | 44 | if (! new_marker) { |
8371 | 0 | opj_free(cstr_index->tile_index[tileno].marker); |
8372 | 0 | cstr_index->tile_index[tileno].marker = NULL; |
8373 | 0 | cstr_index->tile_index[tileno].maxmarknum = 0; |
8374 | 0 | cstr_index->tile_index[tileno].marknum = 0; |
8375 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */ |
8376 | 0 | return OPJ_FALSE; |
8377 | 0 | } |
8378 | 44 | cstr_index->tile_index[tileno].marker = new_marker; |
8379 | 44 | } |
8380 | | |
8381 | | /* add the marker */ |
8382 | 39.1k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type |
8383 | 39.1k | = (OPJ_UINT16)type; |
8384 | 39.1k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos |
8385 | 39.1k | = (OPJ_INT32)pos; |
8386 | 39.1k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len |
8387 | 39.1k | = (OPJ_INT32)len; |
8388 | 39.1k | cstr_index->tile_index[tileno].marknum++; |
8389 | | |
8390 | 39.1k | if (type == J2K_MS_SOT) { |
8391 | 16.3k | OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; |
8392 | | |
8393 | 16.3k | if (cstr_index->tile_index[tileno].tp_index) { |
8394 | 16.3k | cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; |
8395 | 16.3k | } |
8396 | | |
8397 | 16.3k | } |
8398 | 39.1k | return OPJ_TRUE; |
8399 | 39.1k | } |
8400 | | |
8401 | | /* |
8402 | | * ----------------------------------------------------------------------- |
8403 | | * ----------------------------------------------------------------------- |
8404 | | * ----------------------------------------------------------------------- |
8405 | | */ |
8406 | | |
8407 | | OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k, |
8408 | | opj_stream_private_t *p_stream, |
8409 | | opj_event_mgr_t * p_manager |
8410 | | ) |
8411 | 14.1k | { |
8412 | 14.1k | (void)p_j2k; |
8413 | 14.1k | (void)p_stream; |
8414 | 14.1k | (void)p_manager; |
8415 | 14.1k | return OPJ_TRUE; |
8416 | 14.1k | } |
8417 | | |
8418 | | OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream, |
8419 | | opj_j2k_t* p_j2k, |
8420 | | opj_image_t** p_image, |
8421 | | opj_event_mgr_t* p_manager) |
8422 | 16.6k | { |
8423 | | /* preconditions */ |
8424 | 16.6k | assert(p_j2k != 00); |
8425 | 16.6k | assert(p_stream != 00); |
8426 | 16.6k | assert(p_manager != 00); |
8427 | | |
8428 | | /* create an empty image header */ |
8429 | 16.6k | p_j2k->m_private_image = opj_image_create0(); |
8430 | 16.6k | if (! p_j2k->m_private_image) { |
8431 | 0 | return OPJ_FALSE; |
8432 | 0 | } |
8433 | | |
8434 | | /* customization of the validation */ |
8435 | 16.6k | if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) { |
8436 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8437 | 0 | p_j2k->m_private_image = NULL; |
8438 | 0 | return OPJ_FALSE; |
8439 | 0 | } |
8440 | | |
8441 | | /* validation of the parameters codec */ |
8442 | 16.6k | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
8443 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8444 | 0 | p_j2k->m_private_image = NULL; |
8445 | 0 | return OPJ_FALSE; |
8446 | 0 | } |
8447 | | |
8448 | | /* customization of the encoding */ |
8449 | 16.6k | if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) { |
8450 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8451 | 0 | p_j2k->m_private_image = NULL; |
8452 | 0 | return OPJ_FALSE; |
8453 | 0 | } |
8454 | | |
8455 | | /* read header */ |
8456 | 16.6k | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
8457 | 2.53k | opj_image_destroy(p_j2k->m_private_image); |
8458 | 2.53k | p_j2k->m_private_image = NULL; |
8459 | 2.53k | return OPJ_FALSE; |
8460 | 2.53k | } |
8461 | | |
8462 | 14.1k | *p_image = opj_image_create0(); |
8463 | 14.1k | if (!(*p_image)) { |
8464 | 0 | return OPJ_FALSE; |
8465 | 0 | } |
8466 | | |
8467 | | /* Copy codestream image information to the output image */ |
8468 | 14.1k | opj_copy_image_header(p_j2k->m_private_image, *p_image); |
8469 | | |
8470 | | /*Allocate and initialize some elements of codestrem index*/ |
8471 | 14.1k | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { |
8472 | 0 | opj_image_destroy(*p_image); |
8473 | 0 | *p_image = NULL; |
8474 | 0 | return OPJ_FALSE; |
8475 | 0 | } |
8476 | | |
8477 | 14.1k | return OPJ_TRUE; |
8478 | 14.1k | } |
8479 | | |
8480 | | static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, |
8481 | | opj_event_mgr_t * p_manager) |
8482 | 16.6k | { |
8483 | | /* preconditions*/ |
8484 | 16.6k | assert(p_j2k != 00); |
8485 | 16.6k | assert(p_manager != 00); |
8486 | | |
8487 | 16.6k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
8488 | 16.6k | (opj_procedure)opj_j2k_read_header_procedure, p_manager)) { |
8489 | 0 | return OPJ_FALSE; |
8490 | 0 | } |
8491 | | |
8492 | | /* DEVELOPER CORNER, add your custom procedures */ |
8493 | 16.6k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
8494 | 16.6k | (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) { |
8495 | 0 | return OPJ_FALSE; |
8496 | 0 | } |
8497 | | |
8498 | 16.6k | return OPJ_TRUE; |
8499 | 16.6k | } |
8500 | | |
8501 | | static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, |
8502 | | opj_event_mgr_t * p_manager) |
8503 | 16.6k | { |
8504 | | /* preconditions*/ |
8505 | 16.6k | assert(p_j2k != 00); |
8506 | 16.6k | assert(p_manager != 00); |
8507 | | |
8508 | 16.6k | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
8509 | 16.6k | (opj_procedure)opj_j2k_build_decoder, p_manager)) { |
8510 | 0 | return OPJ_FALSE; |
8511 | 0 | } |
8512 | 16.6k | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
8513 | 16.6k | (opj_procedure)opj_j2k_decoding_validation, p_manager)) { |
8514 | 0 | return OPJ_FALSE; |
8515 | 0 | } |
8516 | | |
8517 | | /* DEVELOPER CORNER, add your custom validation procedure */ |
8518 | 16.6k | return OPJ_TRUE; |
8519 | 16.6k | } |
8520 | | |
8521 | | static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, |
8522 | | opj_stream_private_t *p_stream, |
8523 | | opj_event_mgr_t * p_manager) |
8524 | 0 | { |
8525 | 0 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8526 | 0 | OPJ_UINT32 i, j; |
8527 | | |
8528 | | /* preconditions */ |
8529 | 0 | assert(p_j2k != 00); |
8530 | 0 | assert(p_stream != 00); |
8531 | 0 | assert(p_manager != 00); |
8532 | |
|
8533 | 0 | OPJ_UNUSED(p_stream); |
8534 | 0 | OPJ_UNUSED(p_manager); |
8535 | |
|
8536 | 0 | if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { |
8537 | 0 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
8538 | 0 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
8539 | |
|
8540 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
8541 | 0 | if (l_tcp->mct == 2) { |
8542 | 0 | opj_tccp_t * l_tccp = l_tcp->tccps; |
8543 | 0 | l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); |
8544 | |
|
8545 | 0 | for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) { |
8546 | 0 | l_is_valid &= !(l_tccp->qmfbid & 1); |
8547 | 0 | ++l_tccp; |
8548 | 0 | } |
8549 | 0 | } |
8550 | 0 | ++l_tcp; |
8551 | 0 | } |
8552 | 0 | } |
8553 | |
|
8554 | 0 | return l_is_valid; |
8555 | 0 | } |
8556 | | |
8557 | | OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) |
8558 | 0 | { |
8559 | 0 | OPJ_UINT32 i; |
8560 | 0 | OPJ_UINT32 l_indix = 1; |
8561 | 0 | opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00; |
8562 | 0 | opj_simple_mcc_decorrelation_data_t * l_mcc_data; |
8563 | 0 | OPJ_UINT32 l_mct_size, l_nb_elem; |
8564 | 0 | OPJ_FLOAT32 * l_data, * l_current_data; |
8565 | 0 | opj_tccp_t * l_tccp; |
8566 | | |
8567 | | /* preconditions */ |
8568 | 0 | assert(p_tcp != 00); |
8569 | |
|
8570 | 0 | if (p_tcp->mct != 2) { |
8571 | 0 | return OPJ_TRUE; |
8572 | 0 | } |
8573 | | |
8574 | 0 | if (p_tcp->m_mct_decoding_matrix) { |
8575 | 0 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
8576 | 0 | opj_mct_data_t *new_mct_records; |
8577 | 0 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8578 | |
|
8579 | 0 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
8580 | 0 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
8581 | 0 | if (! new_mct_records) { |
8582 | 0 | opj_free(p_tcp->m_mct_records); |
8583 | 0 | p_tcp->m_mct_records = NULL; |
8584 | 0 | p_tcp->m_nb_max_mct_records = 0; |
8585 | 0 | p_tcp->m_nb_mct_records = 0; |
8586 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8587 | 0 | return OPJ_FALSE; |
8588 | 0 | } |
8589 | 0 | p_tcp->m_mct_records = new_mct_records; |
8590 | 0 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8591 | |
|
8592 | 0 | memset(l_mct_deco_data, 0, |
8593 | 0 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
8594 | 0 | opj_mct_data_t)); |
8595 | 0 | } |
8596 | 0 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8597 | |
|
8598 | 0 | if (l_mct_deco_data->m_data) { |
8599 | 0 | opj_free(l_mct_deco_data->m_data); |
8600 | 0 | l_mct_deco_data->m_data = 00; |
8601 | 0 | } |
8602 | |
|
8603 | 0 | l_mct_deco_data->m_index = l_indix++; |
8604 | 0 | l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; |
8605 | 0 | l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; |
8606 | 0 | l_nb_elem = p_image->numcomps * p_image->numcomps; |
8607 | 0 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; |
8608 | 0 | l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
8609 | |
|
8610 | 0 | if (! l_mct_deco_data->m_data) { |
8611 | 0 | return OPJ_FALSE; |
8612 | 0 | } |
8613 | | |
8614 | 0 | j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type]( |
8615 | 0 | p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem); |
8616 | |
|
8617 | 0 | l_mct_deco_data->m_data_size = l_mct_size; |
8618 | 0 | ++p_tcp->m_nb_mct_records; |
8619 | 0 | } |
8620 | | |
8621 | 0 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
8622 | 0 | opj_mct_data_t *new_mct_records; |
8623 | 0 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8624 | 0 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
8625 | 0 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
8626 | 0 | if (! new_mct_records) { |
8627 | 0 | opj_free(p_tcp->m_mct_records); |
8628 | 0 | p_tcp->m_mct_records = NULL; |
8629 | 0 | p_tcp->m_nb_max_mct_records = 0; |
8630 | 0 | p_tcp->m_nb_mct_records = 0; |
8631 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8632 | 0 | return OPJ_FALSE; |
8633 | 0 | } |
8634 | 0 | p_tcp->m_mct_records = new_mct_records; |
8635 | 0 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8636 | |
|
8637 | 0 | memset(l_mct_offset_data, 0, |
8638 | 0 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
8639 | 0 | opj_mct_data_t)); |
8640 | |
|
8641 | 0 | if (l_mct_deco_data) { |
8642 | 0 | l_mct_deco_data = l_mct_offset_data - 1; |
8643 | 0 | } |
8644 | 0 | } |
8645 | | |
8646 | 0 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8647 | |
|
8648 | 0 | if (l_mct_offset_data->m_data) { |
8649 | 0 | opj_free(l_mct_offset_data->m_data); |
8650 | 0 | l_mct_offset_data->m_data = 00; |
8651 | 0 | } |
8652 | |
|
8653 | 0 | l_mct_offset_data->m_index = l_indix++; |
8654 | 0 | l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; |
8655 | 0 | l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; |
8656 | 0 | l_nb_elem = p_image->numcomps; |
8657 | 0 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; |
8658 | 0 | l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
8659 | |
|
8660 | 0 | if (! l_mct_offset_data->m_data) { |
8661 | 0 | return OPJ_FALSE; |
8662 | 0 | } |
8663 | | |
8664 | 0 | l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); |
8665 | 0 | if (! l_data) { |
8666 | 0 | opj_free(l_mct_offset_data->m_data); |
8667 | 0 | l_mct_offset_data->m_data = 00; |
8668 | 0 | return OPJ_FALSE; |
8669 | 0 | } |
8670 | | |
8671 | 0 | l_tccp = p_tcp->tccps; |
8672 | 0 | l_current_data = l_data; |
8673 | |
|
8674 | 0 | for (i = 0; i < l_nb_elem; ++i) { |
8675 | 0 | *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift); |
8676 | 0 | ++l_tccp; |
8677 | 0 | } |
8678 | |
|
8679 | 0 | j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data, |
8680 | 0 | l_mct_offset_data->m_data, l_nb_elem); |
8681 | |
|
8682 | 0 | opj_free(l_data); |
8683 | |
|
8684 | 0 | l_mct_offset_data->m_data_size = l_mct_size; |
8685 | |
|
8686 | 0 | ++p_tcp->m_nb_mct_records; |
8687 | |
|
8688 | 0 | if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { |
8689 | 0 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
8690 | 0 | p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8691 | 0 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
8692 | 0 | p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof( |
8693 | 0 | opj_simple_mcc_decorrelation_data_t)); |
8694 | 0 | if (! new_mcc_records) { |
8695 | 0 | opj_free(p_tcp->m_mcc_records); |
8696 | 0 | p_tcp->m_mcc_records = NULL; |
8697 | 0 | p_tcp->m_nb_max_mcc_records = 0; |
8698 | 0 | p_tcp->m_nb_mcc_records = 0; |
8699 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8700 | 0 | return OPJ_FALSE; |
8701 | 0 | } |
8702 | 0 | p_tcp->m_mcc_records = new_mcc_records; |
8703 | 0 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
8704 | 0 | memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * |
8705 | 0 | sizeof(opj_simple_mcc_decorrelation_data_t)); |
8706 | |
|
8707 | 0 | } |
8708 | | |
8709 | 0 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
8710 | 0 | l_mcc_data->m_decorrelation_array = l_mct_deco_data; |
8711 | 0 | l_mcc_data->m_is_irreversible = 1; |
8712 | 0 | l_mcc_data->m_nb_comps = p_image->numcomps; |
8713 | 0 | l_mcc_data->m_index = l_indix++; |
8714 | 0 | l_mcc_data->m_offset_array = l_mct_offset_data; |
8715 | 0 | ++p_tcp->m_nb_mcc_records; |
8716 | |
|
8717 | 0 | return OPJ_TRUE; |
8718 | 0 | } |
8719 | | |
8720 | | static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, |
8721 | | opj_stream_private_t *p_stream, |
8722 | | opj_event_mgr_t * p_manager) |
8723 | 16.6k | { |
8724 | | /* add here initialization of cp |
8725 | | copy paste of setup_decoder */ |
8726 | 16.6k | (void)p_j2k; |
8727 | 16.6k | (void)p_stream; |
8728 | 16.6k | (void)p_manager; |
8729 | 16.6k | return OPJ_TRUE; |
8730 | 16.6k | } |
8731 | | |
8732 | | static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, |
8733 | | opj_stream_private_t *p_stream, |
8734 | | opj_event_mgr_t * p_manager) |
8735 | 0 | { |
8736 | | /* add here initialization of cp |
8737 | | copy paste of setup_encoder */ |
8738 | 0 | (void)p_j2k; |
8739 | 0 | (void)p_stream; |
8740 | 0 | (void)p_manager; |
8741 | 0 | return OPJ_TRUE; |
8742 | 0 | } |
8743 | | |
8744 | | static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, |
8745 | | opj_stream_private_t *p_stream, |
8746 | | opj_event_mgr_t * p_manager) |
8747 | 0 | { |
8748 | 0 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8749 | | |
8750 | | /* preconditions */ |
8751 | 0 | assert(p_j2k != 00); |
8752 | 0 | assert(p_stream != 00); |
8753 | 0 | assert(p_manager != 00); |
8754 | |
|
8755 | 0 | OPJ_UNUSED(p_stream); |
8756 | | |
8757 | | /* STATE checking */ |
8758 | | /* make sure the state is at 0 */ |
8759 | 0 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); |
8760 | | |
8761 | | /* POINTER validation */ |
8762 | | /* make sure a p_j2k codec is present */ |
8763 | 0 | l_is_valid &= (p_j2k->m_procedure_list != 00); |
8764 | | /* make sure a validation list is present */ |
8765 | 0 | l_is_valid &= (p_j2k->m_validation_list != 00); |
8766 | | |
8767 | | /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */ |
8768 | | /* 33 (32) would always fail the check below (if a cast to 64bits was done) */ |
8769 | | /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */ |
8770 | 0 | if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || |
8771 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) { |
8772 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8773 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8774 | 0 | return OPJ_FALSE; |
8775 | 0 | } |
8776 | | |
8777 | 0 | if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 << |
8778 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
8779 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8780 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8781 | 0 | return OPJ_FALSE; |
8782 | 0 | } |
8783 | | |
8784 | 0 | if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 << |
8785 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
8786 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8787 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8788 | 0 | return OPJ_FALSE; |
8789 | 0 | } |
8790 | | |
8791 | | /* PARAMETER VALIDATION */ |
8792 | 0 | return l_is_valid; |
8793 | 0 | } |
8794 | | |
8795 | | static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k, |
8796 | | opj_stream_private_t *p_stream, |
8797 | | opj_event_mgr_t * p_manager |
8798 | | ) |
8799 | 16.6k | { |
8800 | 16.6k | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8801 | | |
8802 | | /* preconditions*/ |
8803 | 16.6k | assert(p_j2k != 00); |
8804 | 16.6k | assert(p_stream != 00); |
8805 | 16.6k | assert(p_manager != 00); |
8806 | | |
8807 | 16.6k | OPJ_UNUSED(p_stream); |
8808 | 16.6k | OPJ_UNUSED(p_manager); |
8809 | | |
8810 | | /* STATE checking */ |
8811 | | /* make sure the state is at 0 */ |
8812 | | #ifdef TODO_MSD |
8813 | | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); |
8814 | | #endif |
8815 | 16.6k | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); |
8816 | | |
8817 | | /* POINTER validation */ |
8818 | | /* make sure a p_j2k codec is present */ |
8819 | | /* make sure a procedure list is present */ |
8820 | 16.6k | l_is_valid &= (p_j2k->m_procedure_list != 00); |
8821 | | /* make sure a validation list is present */ |
8822 | 16.6k | l_is_valid &= (p_j2k->m_validation_list != 00); |
8823 | | |
8824 | | /* PARAMETER VALIDATION */ |
8825 | 16.6k | return l_is_valid; |
8826 | 16.6k | } |
8827 | | |
8828 | | static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, |
8829 | | opj_stream_private_t *p_stream, |
8830 | | opj_event_mgr_t * p_manager) |
8831 | 16.6k | { |
8832 | 16.6k | OPJ_UINT32 l_current_marker; |
8833 | 16.6k | OPJ_UINT32 l_marker_size; |
8834 | 16.6k | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
8835 | 16.6k | OPJ_BOOL l_has_siz = 0; |
8836 | 16.6k | OPJ_BOOL l_has_cod = 0; |
8837 | 16.6k | OPJ_BOOL l_has_qcd = 0; |
8838 | | |
8839 | | /* preconditions */ |
8840 | 16.6k | assert(p_stream != 00); |
8841 | 16.6k | assert(p_j2k != 00); |
8842 | 16.6k | assert(p_manager != 00); |
8843 | | |
8844 | | /* We enter in the main header */ |
8845 | 16.6k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; |
8846 | | |
8847 | | /* Try to read the SOC marker, the codestream must begin with SOC marker */ |
8848 | 16.6k | if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) { |
8849 | 59 | opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n"); |
8850 | 59 | return OPJ_FALSE; |
8851 | 59 | } |
8852 | | |
8853 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8854 | 16.5k | if (opj_stream_read_data(p_stream, |
8855 | 16.5k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8856 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
8857 | 2 | return OPJ_FALSE; |
8858 | 2 | } |
8859 | | |
8860 | | /* Read 2 bytes as the new marker ID */ |
8861 | 16.5k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
8862 | 16.5k | &l_current_marker, 2); |
8863 | | |
8864 | | /* Try to read until the SOT is detected */ |
8865 | 204k | while (l_current_marker != J2K_MS_SOT) { |
8866 | | |
8867 | | /* Check if the current marker ID is valid */ |
8868 | 192k | if (l_current_marker < 0xff00) { |
8869 | 316 | opj_event_msg(p_manager, EVT_ERROR, |
8870 | 316 | "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker); |
8871 | 316 | return OPJ_FALSE; |
8872 | 316 | } |
8873 | | |
8874 | | /* Get the marker handler from the marker ID */ |
8875 | 192k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
8876 | | |
8877 | | /* Manage case where marker is unknown */ |
8878 | 192k | if (l_marker_handler->id == J2K_MS_UNK) { |
8879 | 114k | if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) { |
8880 | 355 | opj_event_msg(p_manager, EVT_ERROR, |
8881 | 355 | "Unknown marker has been detected and generated error.\n"); |
8882 | 355 | return OPJ_FALSE; |
8883 | 355 | } |
8884 | | |
8885 | 113k | if (l_current_marker == J2K_MS_SOT) { |
8886 | 2.44k | break; /* SOT marker is detected main header is completely read */ |
8887 | 111k | } else { /* Get the marker handler from the marker ID */ |
8888 | 111k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
8889 | 111k | } |
8890 | 113k | } |
8891 | | |
8892 | 189k | if (l_marker_handler->id == J2K_MS_SIZ) { |
8893 | | /* Mark required SIZ marker as found */ |
8894 | 16.4k | l_has_siz = 1; |
8895 | 16.4k | } |
8896 | 189k | if (l_marker_handler->id == J2K_MS_COD) { |
8897 | | /* Mark required COD marker as found */ |
8898 | 19.1k | l_has_cod = 1; |
8899 | 19.1k | } |
8900 | 189k | if (l_marker_handler->id == J2K_MS_QCD) { |
8901 | | /* Mark required QCD marker as found */ |
8902 | 22.4k | l_has_qcd = 1; |
8903 | 22.4k | } |
8904 | | |
8905 | | /* Check if the marker is known and if it is the right place to find it */ |
8906 | 189k | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
8907 | 18 | opj_event_msg(p_manager, EVT_ERROR, |
8908 | 18 | "Marker is not compliant with its position\n"); |
8909 | 18 | return OPJ_FALSE; |
8910 | 18 | } |
8911 | | |
8912 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
8913 | 189k | if (opj_stream_read_data(p_stream, |
8914 | 189k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8915 | 76 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
8916 | 76 | return OPJ_FALSE; |
8917 | 76 | } |
8918 | | |
8919 | | /* read 2 bytes as the marker size */ |
8920 | 189k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
8921 | 189k | 2); |
8922 | 189k | if (l_marker_size < 2) { |
8923 | 18 | opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n"); |
8924 | 18 | return OPJ_FALSE; |
8925 | 18 | } |
8926 | 189k | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
8927 | | |
8928 | | /* Check if the marker size is compatible with the header data size */ |
8929 | 189k | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
8930 | 225 | OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc( |
8931 | 225 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
8932 | 225 | if (! new_header_data) { |
8933 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
8934 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
8935 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
8936 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
8937 | 0 | return OPJ_FALSE; |
8938 | 0 | } |
8939 | 225 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
8940 | 225 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
8941 | 225 | } |
8942 | | |
8943 | | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
8944 | 189k | if (opj_stream_read_data(p_stream, |
8945 | 189k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
8946 | 189k | p_manager) != l_marker_size) { |
8947 | 340 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
8948 | 340 | return OPJ_FALSE; |
8949 | 340 | } |
8950 | | |
8951 | | /* Read the marker segment with the correct marker handler */ |
8952 | 189k | if (!(*(l_marker_handler->handler))(p_j2k, |
8953 | 189k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
8954 | 795 | opj_event_msg(p_manager, EVT_ERROR, |
8955 | 795 | "Marker handler function failed to read the marker segment\n"); |
8956 | 795 | return OPJ_FALSE; |
8957 | 795 | } |
8958 | | |
8959 | | /* Add the marker to the codestream index*/ |
8960 | 188k | if (OPJ_FALSE == opj_j2k_add_mhmarker( |
8961 | 188k | p_j2k->cstr_index, |
8962 | 188k | l_marker_handler->id, |
8963 | 188k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
8964 | 188k | l_marker_size + 4)) { |
8965 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
8966 | 0 | return OPJ_FALSE; |
8967 | 0 | } |
8968 | | |
8969 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8970 | 188k | if (opj_stream_read_data(p_stream, |
8971 | 188k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8972 | 424 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
8973 | 424 | return OPJ_FALSE; |
8974 | 424 | } |
8975 | | |
8976 | | /* read 2 bytes as the new marker ID */ |
8977 | 188k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
8978 | 188k | &l_current_marker, 2); |
8979 | 188k | } |
8980 | | |
8981 | 14.2k | if (l_has_siz == 0) { |
8982 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
8983 | 1 | "required SIZ marker not found in main header\n"); |
8984 | 1 | return OPJ_FALSE; |
8985 | 1 | } |
8986 | 14.2k | if (l_has_cod == 0) { |
8987 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
8988 | 6 | "required COD marker not found in main header\n"); |
8989 | 6 | return OPJ_FALSE; |
8990 | 6 | } |
8991 | 14.2k | if (l_has_qcd == 0) { |
8992 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
8993 | 3 | "required QCD marker not found in main header\n"); |
8994 | 3 | return OPJ_FALSE; |
8995 | 3 | } |
8996 | | |
8997 | 14.2k | if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) { |
8998 | 122 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n"); |
8999 | 122 | return OPJ_FALSE; |
9000 | 122 | } |
9001 | | |
9002 | 14.1k | opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); |
9003 | | |
9004 | | /* Position of the last element if the main header */ |
9005 | 14.1k | p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; |
9006 | | |
9007 | | /* Next step: read a tile-part header */ |
9008 | 14.1k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
9009 | | |
9010 | 14.1k | return OPJ_TRUE; |
9011 | 14.2k | } |
9012 | | |
9013 | | static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, |
9014 | | opj_procedure_list_t * p_procedure_list, |
9015 | | opj_stream_private_t *p_stream, |
9016 | | opj_event_mgr_t * p_manager) |
9017 | 47.3k | { |
9018 | 47.3k | OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *, |
9019 | 47.3k | opj_event_mgr_t *) = 00; |
9020 | 47.3k | OPJ_BOOL l_result = OPJ_TRUE; |
9021 | 47.3k | OPJ_UINT32 l_nb_proc, i; |
9022 | | |
9023 | | /* preconditions*/ |
9024 | 47.3k | assert(p_procedure_list != 00); |
9025 | 47.3k | assert(p_j2k != 00); |
9026 | 47.3k | assert(p_stream != 00); |
9027 | 47.3k | assert(p_manager != 00); |
9028 | | |
9029 | 47.3k | l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); |
9030 | 47.3k | l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *, |
9031 | 47.3k | opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); |
9032 | | |
9033 | 128k | for (i = 0; i < l_nb_proc; ++i) { |
9034 | 80.6k | l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager)); |
9035 | 80.6k | ++l_procedure; |
9036 | 80.6k | } |
9037 | | |
9038 | | /* and clear the procedure list at the end.*/ |
9039 | 47.3k | opj_procedure_list_clear(p_procedure_list); |
9040 | 47.3k | return l_result; |
9041 | 47.3k | } |
9042 | | |
9043 | | /* FIXME DOC*/ |
9044 | | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, |
9045 | | opj_stream_private_t *p_stream, |
9046 | | opj_event_mgr_t * p_manager |
9047 | | ) |
9048 | 14.1k | { |
9049 | 14.1k | opj_tcp_t * l_tcp = 00; |
9050 | 14.1k | opj_tcp_t * l_default_tcp = 00; |
9051 | 14.1k | OPJ_UINT32 l_nb_tiles; |
9052 | 14.1k | OPJ_UINT32 i, j; |
9053 | 14.1k | opj_tccp_t *l_current_tccp = 00; |
9054 | 14.1k | OPJ_UINT32 l_tccp_size; |
9055 | 14.1k | OPJ_UINT32 l_mct_size; |
9056 | 14.1k | opj_image_t * l_image; |
9057 | 14.1k | OPJ_UINT32 l_mcc_records_size, l_mct_records_size; |
9058 | 14.1k | opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; |
9059 | 14.1k | opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; |
9060 | 14.1k | OPJ_UINT32 l_offset; |
9061 | | |
9062 | | /* preconditions */ |
9063 | 14.1k | assert(p_j2k != 00); |
9064 | 14.1k | assert(p_stream != 00); |
9065 | 14.1k | assert(p_manager != 00); |
9066 | | |
9067 | 14.1k | OPJ_UNUSED(p_stream); |
9068 | | |
9069 | 14.1k | l_image = p_j2k->m_private_image; |
9070 | 14.1k | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
9071 | 14.1k | l_tcp = p_j2k->m_cp.tcps; |
9072 | 14.1k | l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t); |
9073 | 14.1k | l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
9074 | 14.1k | l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof( |
9075 | 14.1k | OPJ_FLOAT32); |
9076 | | |
9077 | | /* For each tile */ |
9078 | 18.6M | for (i = 0; i < l_nb_tiles; ++i) { |
9079 | | /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/ |
9080 | 18.6M | l_current_tccp = l_tcp->tccps; |
9081 | | /*Copy default coding parameters into the current tile coding parameters*/ |
9082 | 18.6M | memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t)); |
9083 | | /* Initialize some values of the current tile coding parameters*/ |
9084 | 18.6M | l_tcp->cod = 0; |
9085 | 18.6M | l_tcp->ppt = 0; |
9086 | 18.6M | l_tcp->ppt_data = 00; |
9087 | 18.6M | l_tcp->m_current_tile_part_number = -1; |
9088 | | /* Remove memory not owned by this tile in case of early error return. */ |
9089 | 18.6M | l_tcp->m_mct_decoding_matrix = 00; |
9090 | 18.6M | l_tcp->m_nb_max_mct_records = 0; |
9091 | 18.6M | l_tcp->m_mct_records = 00; |
9092 | 18.6M | l_tcp->m_nb_max_mcc_records = 0; |
9093 | 18.6M | l_tcp->m_mcc_records = 00; |
9094 | | /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ |
9095 | 18.6M | l_tcp->tccps = l_current_tccp; |
9096 | | |
9097 | | /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ |
9098 | 18.6M | if (l_default_tcp->m_mct_decoding_matrix) { |
9099 | 6.22k | l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
9100 | 6.22k | if (! l_tcp->m_mct_decoding_matrix) { |
9101 | 0 | return OPJ_FALSE; |
9102 | 0 | } |
9103 | 6.22k | memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix, |
9104 | 6.22k | l_mct_size); |
9105 | 6.22k | } |
9106 | | |
9107 | | /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ |
9108 | 18.6M | l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof( |
9109 | 18.6M | opj_mct_data_t); |
9110 | 18.6M | l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); |
9111 | 18.6M | if (! l_tcp->m_mct_records) { |
9112 | 0 | return OPJ_FALSE; |
9113 | 0 | } |
9114 | 18.6M | memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size); |
9115 | | |
9116 | | /* Copy the mct record data from dflt_tile_cp to the current tile*/ |
9117 | 18.6M | l_src_mct_rec = l_default_tcp->m_mct_records; |
9118 | 18.6M | l_dest_mct_rec = l_tcp->m_mct_records; |
9119 | | |
9120 | 18.7M | for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) { |
9121 | | |
9122 | 94.1k | if (l_src_mct_rec->m_data) { |
9123 | | |
9124 | 13.5k | l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); |
9125 | 13.5k | if (! l_dest_mct_rec->m_data) { |
9126 | 0 | return OPJ_FALSE; |
9127 | 0 | } |
9128 | 13.5k | memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data, |
9129 | 13.5k | l_src_mct_rec->m_data_size); |
9130 | 13.5k | } |
9131 | | |
9132 | 94.1k | ++l_src_mct_rec; |
9133 | 94.1k | ++l_dest_mct_rec; |
9134 | | /* Update with each pass to free exactly what has been allocated on early return. */ |
9135 | 94.1k | l_tcp->m_nb_max_mct_records += 1; |
9136 | 94.1k | } |
9137 | | |
9138 | | /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ |
9139 | 18.6M | l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof( |
9140 | 18.6M | opj_simple_mcc_decorrelation_data_t); |
9141 | 18.6M | l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc( |
9142 | 18.6M | l_mcc_records_size); |
9143 | 18.6M | if (! l_tcp->m_mcc_records) { |
9144 | 0 | return OPJ_FALSE; |
9145 | 0 | } |
9146 | 18.6M | memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size); |
9147 | 18.6M | l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records; |
9148 | | |
9149 | | /* Copy the mcc record data from dflt_tile_cp to the current tile*/ |
9150 | 18.6M | l_src_mcc_rec = l_default_tcp->m_mcc_records; |
9151 | 18.6M | l_dest_mcc_rec = l_tcp->m_mcc_records; |
9152 | | |
9153 | 204M | for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) { |
9154 | | |
9155 | 186M | if (l_src_mcc_rec->m_decorrelation_array) { |
9156 | 8.00k | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - |
9157 | 8.00k | l_default_tcp->m_mct_records); |
9158 | 8.00k | l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; |
9159 | 8.00k | } |
9160 | | |
9161 | 186M | if (l_src_mcc_rec->m_offset_array) { |
9162 | 7.70k | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - |
9163 | 7.70k | l_default_tcp->m_mct_records); |
9164 | 7.70k | l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; |
9165 | 7.70k | } |
9166 | | |
9167 | 186M | ++l_src_mcc_rec; |
9168 | 186M | ++l_dest_mcc_rec; |
9169 | 186M | } |
9170 | | |
9171 | | /* Copy all the dflt_tile_compo_cp to the current tile cp */ |
9172 | 18.6M | memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size); |
9173 | | |
9174 | | /* Move to next tile cp*/ |
9175 | 18.6M | ++l_tcp; |
9176 | 18.6M | } |
9177 | | |
9178 | | /* Create the current tile decoder*/ |
9179 | 14.1k | p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE); |
9180 | 14.1k | if (! p_j2k->m_tcd) { |
9181 | 0 | return OPJ_FALSE; |
9182 | 0 | } |
9183 | | |
9184 | 14.1k | if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) { |
9185 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
9186 | 0 | p_j2k->m_tcd = 00; |
9187 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
9188 | 0 | return OPJ_FALSE; |
9189 | 0 | } |
9190 | | |
9191 | 14.1k | return OPJ_TRUE; |
9192 | 14.1k | } |
9193 | | |
9194 | | static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler( |
9195 | | OPJ_UINT32 p_id) |
9196 | 1.55M | { |
9197 | 1.55M | const opj_dec_memory_marker_handler_t *e; |
9198 | 33.2M | for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { |
9199 | 32.0M | if (e->id == p_id) { |
9200 | 326k | break; /* we find a handler corresponding to the marker ID*/ |
9201 | 326k | } |
9202 | 32.0M | } |
9203 | 1.55M | return e; |
9204 | 1.55M | } |
9205 | | |
9206 | | void opj_j2k_destroy(opj_j2k_t *p_j2k) |
9207 | 17.8k | { |
9208 | 17.8k | if (p_j2k == 00) { |
9209 | 0 | return; |
9210 | 0 | } |
9211 | | |
9212 | 17.8k | if (p_j2k->m_is_decoder) { |
9213 | | |
9214 | 17.8k | if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { |
9215 | 17.8k | opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
9216 | 17.8k | opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
9217 | 17.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; |
9218 | 17.8k | } |
9219 | | |
9220 | 17.8k | if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { |
9221 | 17.8k | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9222 | 17.8k | p_j2k->m_specific_param.m_decoder.m_header_data = 00; |
9223 | 17.8k | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9224 | 17.8k | } |
9225 | | |
9226 | 17.8k | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
9227 | 17.8k | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00; |
9228 | 17.8k | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
9229 | | |
9230 | 17.8k | } else { |
9231 | |
|
9232 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
9233 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
9234 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; |
9235 | 0 | } |
9236 | |
|
9237 | 0 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
9238 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
9239 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; |
9240 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; |
9241 | 0 | } |
9242 | |
|
9243 | 0 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
9244 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
9245 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; |
9246 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
9247 | 0 | } |
9248 | 0 | } |
9249 | | |
9250 | 17.8k | opj_tcd_destroy(p_j2k->m_tcd); |
9251 | | |
9252 | 17.8k | opj_j2k_cp_destroy(&(p_j2k->m_cp)); |
9253 | 17.8k | memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t)); |
9254 | | |
9255 | 17.8k | opj_procedure_list_destroy(p_j2k->m_procedure_list); |
9256 | 17.8k | p_j2k->m_procedure_list = 00; |
9257 | | |
9258 | 17.8k | opj_procedure_list_destroy(p_j2k->m_validation_list); |
9259 | 17.8k | p_j2k->m_procedure_list = 00; |
9260 | | |
9261 | 17.8k | j2k_destroy_cstr_index(p_j2k->cstr_index); |
9262 | 17.8k | p_j2k->cstr_index = NULL; |
9263 | | |
9264 | 17.8k | opj_image_destroy(p_j2k->m_private_image); |
9265 | 17.8k | p_j2k->m_private_image = NULL; |
9266 | | |
9267 | 17.8k | opj_image_destroy(p_j2k->m_output_image); |
9268 | 17.8k | p_j2k->m_output_image = NULL; |
9269 | | |
9270 | 17.8k | opj_thread_pool_destroy(p_j2k->m_tp); |
9271 | 17.8k | p_j2k->m_tp = NULL; |
9272 | | |
9273 | 17.8k | opj_free(p_j2k); |
9274 | 17.8k | } |
9275 | | |
9276 | | void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind) |
9277 | 17.8k | { |
9278 | 17.8k | if (p_cstr_ind) { |
9279 | | |
9280 | 17.8k | if (p_cstr_ind->marker) { |
9281 | 17.8k | opj_free(p_cstr_ind->marker); |
9282 | 17.8k | p_cstr_ind->marker = NULL; |
9283 | 17.8k | } |
9284 | | |
9285 | 17.8k | if (p_cstr_ind->tile_index) { |
9286 | 14.1k | OPJ_UINT32 it_tile = 0; |
9287 | | |
9288 | 18.6M | for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { |
9289 | | |
9290 | 18.6M | if (p_cstr_ind->tile_index[it_tile].packet_index) { |
9291 | 0 | opj_free(p_cstr_ind->tile_index[it_tile].packet_index); |
9292 | 0 | p_cstr_ind->tile_index[it_tile].packet_index = NULL; |
9293 | 0 | } |
9294 | | |
9295 | 18.6M | if (p_cstr_ind->tile_index[it_tile].tp_index) { |
9296 | 16.2k | opj_free(p_cstr_ind->tile_index[it_tile].tp_index); |
9297 | 16.2k | p_cstr_ind->tile_index[it_tile].tp_index = NULL; |
9298 | 16.2k | } |
9299 | | |
9300 | 18.6M | if (p_cstr_ind->tile_index[it_tile].marker) { |
9301 | 18.6M | opj_free(p_cstr_ind->tile_index[it_tile].marker); |
9302 | 18.6M | p_cstr_ind->tile_index[it_tile].marker = NULL; |
9303 | | |
9304 | 18.6M | } |
9305 | 18.6M | } |
9306 | | |
9307 | 14.1k | opj_free(p_cstr_ind->tile_index); |
9308 | 14.1k | p_cstr_ind->tile_index = NULL; |
9309 | 14.1k | } |
9310 | | |
9311 | 17.8k | opj_free(p_cstr_ind); |
9312 | 17.8k | } |
9313 | 17.8k | } |
9314 | | |
9315 | | static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp) |
9316 | 20.3M | { |
9317 | 20.3M | if (p_tcp == 00) { |
9318 | 0 | return; |
9319 | 0 | } |
9320 | | |
9321 | 20.3M | if (p_tcp->ppt_markers != 00) { |
9322 | 66 | OPJ_UINT32 i; |
9323 | 3.50k | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
9324 | 3.44k | if (p_tcp->ppt_markers[i].m_data != NULL) { |
9325 | 102 | opj_free(p_tcp->ppt_markers[i].m_data); |
9326 | 102 | } |
9327 | 3.44k | } |
9328 | 66 | p_tcp->ppt_markers_count = 0U; |
9329 | 66 | opj_free(p_tcp->ppt_markers); |
9330 | 66 | p_tcp->ppt_markers = NULL; |
9331 | 66 | } |
9332 | | |
9333 | 20.3M | if (p_tcp->ppt_buffer != 00) { |
9334 | 106 | opj_free(p_tcp->ppt_buffer); |
9335 | 106 | p_tcp->ppt_buffer = 00; |
9336 | 106 | } |
9337 | | |
9338 | 20.3M | if (p_tcp->tccps != 00) { |
9339 | 20.3M | opj_free(p_tcp->tccps); |
9340 | 20.3M | p_tcp->tccps = 00; |
9341 | 20.3M | } |
9342 | | |
9343 | 20.3M | if (p_tcp->m_mct_coding_matrix != 00) { |
9344 | 0 | opj_free(p_tcp->m_mct_coding_matrix); |
9345 | 0 | p_tcp->m_mct_coding_matrix = 00; |
9346 | 0 | } |
9347 | | |
9348 | 20.3M | if (p_tcp->m_mct_decoding_matrix != 00) { |
9349 | 6.30k | opj_free(p_tcp->m_mct_decoding_matrix); |
9350 | 6.30k | p_tcp->m_mct_decoding_matrix = 00; |
9351 | 6.30k | } |
9352 | | |
9353 | 20.3M | if (p_tcp->m_mcc_records) { |
9354 | 18.6M | opj_free(p_tcp->m_mcc_records); |
9355 | 18.6M | p_tcp->m_mcc_records = 00; |
9356 | 18.6M | p_tcp->m_nb_max_mcc_records = 0; |
9357 | 18.6M | p_tcp->m_nb_mcc_records = 0; |
9358 | 18.6M | } |
9359 | | |
9360 | 20.3M | if (p_tcp->m_mct_records) { |
9361 | 18.6M | opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; |
9362 | 18.6M | OPJ_UINT32 i; |
9363 | | |
9364 | 18.7M | for (i = 0; i < p_tcp->m_nb_mct_records; ++i) { |
9365 | 95.3k | if (l_mct_data->m_data) { |
9366 | 13.9k | opj_free(l_mct_data->m_data); |
9367 | 13.9k | l_mct_data->m_data = 00; |
9368 | 13.9k | } |
9369 | | |
9370 | 95.3k | ++l_mct_data; |
9371 | 95.3k | } |
9372 | | |
9373 | 18.6M | opj_free(p_tcp->m_mct_records); |
9374 | 18.6M | p_tcp->m_mct_records = 00; |
9375 | 18.6M | } |
9376 | | |
9377 | 20.3M | if (p_tcp->mct_norms != 00) { |
9378 | 0 | opj_free(p_tcp->mct_norms); |
9379 | 0 | p_tcp->mct_norms = 00; |
9380 | 0 | } |
9381 | | |
9382 | 20.3M | opj_j2k_tcp_data_destroy(p_tcp); |
9383 | | |
9384 | 20.3M | } |
9385 | | |
9386 | | static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp) |
9387 | 20.3M | { |
9388 | 20.3M | if (p_tcp->m_data) { |
9389 | 15.3k | opj_free(p_tcp->m_data); |
9390 | 15.3k | p_tcp->m_data = NULL; |
9391 | 15.3k | p_tcp->m_data_size = 0; |
9392 | 15.3k | } |
9393 | 20.3M | } |
9394 | | |
9395 | | static void opj_j2k_cp_destroy(opj_cp_t *p_cp) |
9396 | 17.8k | { |
9397 | 17.8k | OPJ_UINT32 l_nb_tiles; |
9398 | 17.8k | opj_tcp_t * l_current_tile = 00; |
9399 | | |
9400 | 17.8k | if (p_cp == 00) { |
9401 | 0 | return; |
9402 | 0 | } |
9403 | 17.8k | if (p_cp->tcps != 00) { |
9404 | 15.8k | OPJ_UINT32 i; |
9405 | 15.8k | l_current_tile = p_cp->tcps; |
9406 | 15.8k | l_nb_tiles = p_cp->th * p_cp->tw; |
9407 | | |
9408 | 20.3M | for (i = 0U; i < l_nb_tiles; ++i) { |
9409 | 20.3M | opj_j2k_tcp_destroy(l_current_tile); |
9410 | 20.3M | ++l_current_tile; |
9411 | 20.3M | } |
9412 | 15.8k | opj_free(p_cp->tcps); |
9413 | 15.8k | p_cp->tcps = 00; |
9414 | 15.8k | } |
9415 | 17.8k | if (p_cp->ppm_markers != 00) { |
9416 | 171 | OPJ_UINT32 i; |
9417 | 18.5k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
9418 | 18.3k | if (p_cp->ppm_markers[i].m_data != NULL) { |
9419 | 535 | opj_free(p_cp->ppm_markers[i].m_data); |
9420 | 535 | } |
9421 | 18.3k | } |
9422 | 171 | p_cp->ppm_markers_count = 0U; |
9423 | 171 | opj_free(p_cp->ppm_markers); |
9424 | 171 | p_cp->ppm_markers = NULL; |
9425 | 171 | } |
9426 | 17.8k | opj_free(p_cp->ppm_buffer); |
9427 | 17.8k | p_cp->ppm_buffer = 00; |
9428 | 17.8k | p_cp->ppm_data = |
9429 | 17.8k | NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */ |
9430 | 17.8k | opj_free(p_cp->comment); |
9431 | 17.8k | p_cp->comment = 00; |
9432 | 17.8k | if (! p_cp->m_is_decoder) { |
9433 | 0 | opj_free(p_cp->m_specific_param.m_enc.m_matrice); |
9434 | 0 | p_cp->m_specific_param.m_enc.m_matrice = 00; |
9435 | 0 | } |
9436 | 17.8k | } |
9437 | | |
9438 | | static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t |
9439 | | *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, |
9440 | | opj_event_mgr_t * p_manager) |
9441 | 1.60k | { |
9442 | 1.60k | OPJ_BYTE l_header_data[10]; |
9443 | 1.60k | OPJ_OFF_T l_stream_pos_backup; |
9444 | 1.60k | OPJ_UINT32 l_current_marker; |
9445 | 1.60k | OPJ_UINT32 l_marker_size; |
9446 | 1.60k | OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts; |
9447 | | |
9448 | | /* initialize to no correction needed */ |
9449 | 1.60k | *p_correction_needed = OPJ_FALSE; |
9450 | | |
9451 | 1.60k | if (!opj_stream_has_seek(p_stream)) { |
9452 | | /* We can't do much in this case, seek is needed */ |
9453 | 0 | return OPJ_TRUE; |
9454 | 0 | } |
9455 | | |
9456 | 1.60k | l_stream_pos_backup = opj_stream_tell(p_stream); |
9457 | 1.60k | if (l_stream_pos_backup == -1) { |
9458 | | /* let's do nothing */ |
9459 | 0 | return OPJ_TRUE; |
9460 | 0 | } |
9461 | | |
9462 | 4.52k | for (;;) { |
9463 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
9464 | 4.52k | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
9465 | | /* assume all is OK */ |
9466 | 63 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9467 | 0 | return OPJ_FALSE; |
9468 | 0 | } |
9469 | 63 | return OPJ_TRUE; |
9470 | 63 | } |
9471 | | |
9472 | | /* Read 2 bytes from buffer as the new marker ID */ |
9473 | 4.46k | opj_read_bytes(l_header_data, &l_current_marker, 2); |
9474 | | |
9475 | 4.46k | if (l_current_marker != J2K_MS_SOT) { |
9476 | | /* assume all is OK */ |
9477 | 1.16k | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9478 | 0 | return OPJ_FALSE; |
9479 | 0 | } |
9480 | 1.16k | return OPJ_TRUE; |
9481 | 1.16k | } |
9482 | | |
9483 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9484 | 3.29k | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
9485 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9486 | 3 | return OPJ_FALSE; |
9487 | 3 | } |
9488 | | |
9489 | | /* Read 2 bytes from the buffer as the marker size */ |
9490 | 3.29k | opj_read_bytes(l_header_data, &l_marker_size, 2); |
9491 | | |
9492 | | /* Check marker size for SOT Marker */ |
9493 | 3.29k | if (l_marker_size != 10) { |
9494 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); |
9495 | 2 | return OPJ_FALSE; |
9496 | 2 | } |
9497 | 3.29k | l_marker_size -= 2; |
9498 | | |
9499 | 3.29k | if (opj_stream_read_data(p_stream, l_header_data, l_marker_size, |
9500 | 3.29k | p_manager) != l_marker_size) { |
9501 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9502 | 1 | return OPJ_FALSE; |
9503 | 1 | } |
9504 | | |
9505 | 3.29k | if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no, |
9506 | 3.29k | &l_tot_len, &l_current_part, &l_num_parts, p_manager)) { |
9507 | 0 | return OPJ_FALSE; |
9508 | 0 | } |
9509 | | |
9510 | 3.29k | if (l_tile_no == tile_no) { |
9511 | | /* we found what we were looking for */ |
9512 | 59 | break; |
9513 | 59 | } |
9514 | | |
9515 | 3.23k | if (l_tot_len < 14U) { |
9516 | | /* last SOT until EOC or invalid Psot value */ |
9517 | | /* assume all is OK */ |
9518 | 72 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9519 | 0 | return OPJ_FALSE; |
9520 | 0 | } |
9521 | 72 | return OPJ_TRUE; |
9522 | 72 | } |
9523 | 3.15k | l_tot_len -= 12U; |
9524 | | /* look for next SOT marker */ |
9525 | 3.15k | if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len), |
9526 | 3.15k | p_manager) != (OPJ_OFF_T)(l_tot_len)) { |
9527 | | /* assume all is OK */ |
9528 | 236 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9529 | 0 | return OPJ_FALSE; |
9530 | 0 | } |
9531 | 236 | return OPJ_TRUE; |
9532 | 236 | } |
9533 | 3.15k | } |
9534 | | |
9535 | | /* check for correction */ |
9536 | 59 | if (l_current_part == l_num_parts) { |
9537 | 46 | *p_correction_needed = OPJ_TRUE; |
9538 | 46 | } |
9539 | | |
9540 | 59 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9541 | 0 | return OPJ_FALSE; |
9542 | 0 | } |
9543 | 59 | return OPJ_TRUE; |
9544 | 59 | } |
9545 | | |
9546 | | OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, |
9547 | | OPJ_UINT32 * p_tile_index, |
9548 | | OPJ_UINT32 * p_data_size, |
9549 | | OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, |
9550 | | OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, |
9551 | | OPJ_UINT32 * p_nb_comps, |
9552 | | OPJ_BOOL * p_go_on, |
9553 | | opj_stream_private_t *p_stream, |
9554 | | opj_event_mgr_t * p_manager) |
9555 | 16.2k | { |
9556 | 16.2k | OPJ_UINT32 l_current_marker = J2K_MS_SOT; |
9557 | 16.2k | OPJ_UINT32 l_marker_size; |
9558 | 16.2k | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
9559 | 16.2k | opj_tcp_t * l_tcp = NULL; |
9560 | 16.2k | const OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
9561 | | |
9562 | | /* preconditions */ |
9563 | 16.2k | assert(p_stream != 00); |
9564 | 16.2k | assert(p_j2k != 00); |
9565 | 16.2k | assert(p_manager != 00); |
9566 | | |
9567 | | /* Reach the End Of Codestream ?*/ |
9568 | 16.2k | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
9569 | 24 | l_current_marker = J2K_MS_EOC; |
9570 | 24 | } |
9571 | | /* We need to encounter a SOT marker (a new tile-part header) */ |
9572 | 16.2k | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
9573 | 0 | return OPJ_FALSE; |
9574 | 0 | } |
9575 | | |
9576 | | /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ |
9577 | 33.6k | while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) && |
9578 | 33.6k | (l_current_marker != J2K_MS_EOC)) { |
9579 | | |
9580 | | /* Try to read until the Start Of Data is detected */ |
9581 | 52.0k | while (l_current_marker != J2K_MS_SOD) { |
9582 | | |
9583 | 34.4k | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
9584 | 11.7k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
9585 | 11.7k | break; |
9586 | 11.7k | } |
9587 | | |
9588 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9589 | 22.6k | if (opj_stream_read_data(p_stream, |
9590 | 22.6k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9591 | 22 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9592 | 22 | return OPJ_FALSE; |
9593 | 22 | } |
9594 | | |
9595 | | /* Read 2 bytes from the buffer as the marker size */ |
9596 | 22.6k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
9597 | 22.6k | 2); |
9598 | | |
9599 | | /* Check marker size (does not include marker ID but includes marker size) */ |
9600 | 22.6k | if (l_marker_size < 2) { |
9601 | 44 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); |
9602 | 44 | return OPJ_FALSE; |
9603 | 44 | } |
9604 | | |
9605 | | /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */ |
9606 | 22.6k | if (l_current_marker == 0x8080 && |
9607 | 22.6k | opj_stream_get_number_byte_left(p_stream) == 0) { |
9608 | 2 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
9609 | 2 | break; |
9610 | 2 | } |
9611 | | |
9612 | | /* Why this condition? FIXME */ |
9613 | 22.6k | if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) { |
9614 | 5.98k | p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); |
9615 | 5.98k | } |
9616 | 22.6k | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
9617 | | |
9618 | | /* Get the marker handler from the marker ID */ |
9619 | 22.6k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
9620 | | |
9621 | | /* Check if the marker is known and if it is the right place to find it */ |
9622 | 22.6k | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
9623 | 48 | opj_event_msg(p_manager, EVT_ERROR, |
9624 | 48 | "Marker is not compliant with its position\n"); |
9625 | 48 | return OPJ_FALSE; |
9626 | 48 | } |
9627 | | /* FIXME manage case of unknown marker as in the main header ? */ |
9628 | | |
9629 | | /* Check if the marker size is compatible with the header data size */ |
9630 | 22.5k | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
9631 | 182 | OPJ_BYTE *new_header_data = NULL; |
9632 | | /* If we are here, this means we consider this marker as known & we will read it */ |
9633 | | /* Check enough bytes left in stream before allocation */ |
9634 | 182 | if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) { |
9635 | 139 | opj_event_msg(p_manager, EVT_ERROR, |
9636 | 139 | "Marker size inconsistent with stream length\n"); |
9637 | 139 | return OPJ_FALSE; |
9638 | 139 | } |
9639 | 43 | new_header_data = (OPJ_BYTE *) opj_realloc( |
9640 | 43 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
9641 | 43 | if (! new_header_data) { |
9642 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9643 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
9644 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9645 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
9646 | 0 | return OPJ_FALSE; |
9647 | 0 | } |
9648 | 43 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
9649 | 43 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
9650 | 43 | } |
9651 | | |
9652 | | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
9653 | 22.4k | if (opj_stream_read_data(p_stream, |
9654 | 22.4k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
9655 | 22.4k | p_manager) != l_marker_size) { |
9656 | 125 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9657 | 125 | return OPJ_FALSE; |
9658 | 125 | } |
9659 | | |
9660 | 22.3k | if (!l_marker_handler->handler) { |
9661 | | /* See issue #175 */ |
9662 | 24 | opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n"); |
9663 | 24 | return OPJ_FALSE; |
9664 | 24 | } |
9665 | | /* Read the marker segment with the correct marker handler */ |
9666 | 22.2k | if (!(*(l_marker_handler->handler))(p_j2k, |
9667 | 22.2k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
9668 | 246 | opj_event_msg(p_manager, EVT_ERROR, |
9669 | 246 | "Fail to read the current marker segment (%#x)\n", l_current_marker); |
9670 | 246 | return OPJ_FALSE; |
9671 | 246 | } |
9672 | | |
9673 | | /* Add the marker to the codestream index*/ |
9674 | 22.0k | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
9675 | 22.0k | p_j2k->cstr_index, |
9676 | 22.0k | l_marker_handler->id, |
9677 | 22.0k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
9678 | 22.0k | l_marker_size + 4)) { |
9679 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); |
9680 | 0 | return OPJ_FALSE; |
9681 | 0 | } |
9682 | | |
9683 | | /* Keep the position of the last SOT marker read */ |
9684 | 22.0k | if (l_marker_handler->id == J2K_MS_SOT) { |
9685 | 16.3k | OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 |
9686 | 16.3k | ; |
9687 | 16.3k | if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) { |
9688 | 16.3k | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; |
9689 | 16.3k | } |
9690 | 16.3k | } |
9691 | | |
9692 | 22.0k | if (p_j2k->m_specific_param.m_decoder.m_skip_data) { |
9693 | | /* Skip the rest of the tile part header*/ |
9694 | 477 | if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length, |
9695 | 477 | p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
9696 | 117 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9697 | 117 | return OPJ_FALSE; |
9698 | 117 | } |
9699 | 360 | l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ |
9700 | 21.5k | } else { |
9701 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
9702 | 21.5k | if (opj_stream_read_data(p_stream, |
9703 | 21.5k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9704 | 59 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9705 | 59 | return OPJ_FALSE; |
9706 | 59 | } |
9707 | | /* Read 2 bytes from the buffer as the new marker ID */ |
9708 | 21.5k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9709 | 21.5k | &l_current_marker, 2); |
9710 | 21.5k | } |
9711 | 22.0k | } |
9712 | 29.3k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
9713 | 29.3k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
9714 | 11.7k | break; |
9715 | 11.7k | } |
9716 | | |
9717 | | /* If we didn't skip data before, we need to read the SOD marker*/ |
9718 | 17.6k | if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { |
9719 | | /* Try to read the SOD marker and skip data ? FIXME */ |
9720 | 17.2k | if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { |
9721 | 164 | return OPJ_FALSE; |
9722 | 164 | } |
9723 | 17.0k | if (p_j2k->m_specific_param.m_decoder.m_can_decode && |
9724 | 17.0k | !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) { |
9725 | | /* Issue 254 */ |
9726 | 1.60k | OPJ_BOOL l_correction_needed; |
9727 | | |
9728 | 1.60k | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
9729 | 1.60k | if (!opj_j2k_need_nb_tile_parts_correction(p_stream, |
9730 | 1.60k | p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { |
9731 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
9732 | 6 | "opj_j2k_apply_nb_tile_parts_correction error\n"); |
9733 | 6 | return OPJ_FALSE; |
9734 | 6 | } |
9735 | 1.59k | if (l_correction_needed) { |
9736 | 46 | OPJ_UINT32 l_tile_no; |
9737 | | |
9738 | 46 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
9739 | 46 | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1; |
9740 | | /* correct tiles */ |
9741 | 14.7k | for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { |
9742 | 14.7k | if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) { |
9743 | 58 | p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1; |
9744 | 58 | } |
9745 | 14.7k | } |
9746 | 46 | opj_event_msg(p_manager, EVT_WARNING, |
9747 | 46 | "Non conformant codestream TPsot==TNsot.\n"); |
9748 | 46 | } |
9749 | 1.59k | } |
9750 | 17.0k | } else { |
9751 | | /* Indicate we will try to read a new tile-part header*/ |
9752 | 360 | p_j2k->m_specific_param.m_decoder.m_skip_data = 0; |
9753 | 360 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
9754 | 360 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
9755 | 360 | } |
9756 | | |
9757 | 17.4k | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
9758 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
9759 | 14.0k | if (opj_stream_read_data(p_stream, |
9760 | 14.0k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9761 | | |
9762 | | /* Deal with likely non conformant SPOT6 files, where the last */ |
9763 | | /* row of tiles have TPsot == 0 and TNsot == 0, and missing EOC, */ |
9764 | | /* but no other tile-parts were found. */ |
9765 | 35 | if (p_j2k->m_current_tile_number + 1 == l_nb_tiles) { |
9766 | 29 | OPJ_UINT32 l_tile_no; |
9767 | 480 | for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { |
9768 | 458 | if (p_j2k->m_cp.tcps[l_tile_no].m_current_tile_part_number == 0 && |
9769 | 458 | p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts == 0) { |
9770 | 7 | break; |
9771 | 7 | } |
9772 | 458 | } |
9773 | 29 | if (l_tile_no < l_nb_tiles) { |
9774 | 7 | opj_event_msg(p_manager, EVT_INFO, |
9775 | 7 | "Tile %u has TPsot == 0 and TNsot == 0, " |
9776 | 7 | "but no other tile-parts were found. " |
9777 | 7 | "EOC is also missing.\n", |
9778 | 7 | l_tile_no); |
9779 | 7 | p_j2k->m_current_tile_number = l_tile_no; |
9780 | 7 | l_current_marker = J2K_MS_EOC; |
9781 | 7 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
9782 | 7 | break; |
9783 | 7 | } |
9784 | 29 | } |
9785 | | |
9786 | 28 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9787 | 28 | return OPJ_FALSE; |
9788 | 35 | } |
9789 | | |
9790 | | /* Read 2 bytes from buffer as the new marker ID */ |
9791 | 14.0k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9792 | 14.0k | &l_current_marker, 2); |
9793 | 14.0k | } |
9794 | 17.4k | } |
9795 | | |
9796 | | /* Current marker is the EOC marker ?*/ |
9797 | 15.2k | if (l_current_marker == J2K_MS_EOC) { |
9798 | 43 | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { |
9799 | 12 | p_j2k->m_current_tile_number = 0; |
9800 | 12 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
9801 | 12 | } |
9802 | 43 | } |
9803 | | |
9804 | | /* Deal with tiles that have a single tile-part with TPsot == 0 and TNsot == 0 */ |
9805 | 15.2k | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
9806 | 11.7k | l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; |
9807 | | |
9808 | 298k | while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) { |
9809 | 287k | ++p_j2k->m_current_tile_number; |
9810 | 287k | ++l_tcp; |
9811 | 287k | } |
9812 | | |
9813 | 11.7k | if (p_j2k->m_current_tile_number == l_nb_tiles) { |
9814 | 110 | *p_go_on = OPJ_FALSE; |
9815 | 110 | return OPJ_TRUE; |
9816 | 110 | } |
9817 | 11.7k | } |
9818 | | |
9819 | 15.1k | if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number, |
9820 | 15.1k | p_manager)) { |
9821 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n"); |
9822 | 0 | return OPJ_FALSE; |
9823 | 0 | } |
9824 | | /*FIXME ???*/ |
9825 | 15.1k | if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
9826 | 15.1k | p_manager)) { |
9827 | 56 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
9828 | 56 | return OPJ_FALSE; |
9829 | 56 | } |
9830 | | |
9831 | 15.0k | opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", |
9832 | 15.0k | p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw)); |
9833 | | |
9834 | 15.0k | *p_tile_index = p_j2k->m_current_tile_number; |
9835 | 15.0k | *p_go_on = OPJ_TRUE; |
9836 | 15.0k | if (p_data_size) { |
9837 | | /* For internal use in j2k.c, we don't need this */ |
9838 | | /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */ |
9839 | 0 | *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE); |
9840 | 0 | if (*p_data_size == UINT_MAX) { |
9841 | 0 | return OPJ_FALSE; |
9842 | 0 | } |
9843 | 0 | } |
9844 | 15.0k | *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; |
9845 | 15.0k | *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; |
9846 | 15.0k | *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; |
9847 | 15.0k | *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; |
9848 | 15.0k | *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; |
9849 | | |
9850 | 15.0k | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
9851 | | |
9852 | 15.0k | return OPJ_TRUE; |
9853 | 15.0k | } |
9854 | | |
9855 | | OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, |
9856 | | OPJ_UINT32 p_tile_index, |
9857 | | OPJ_BYTE * p_data, |
9858 | | OPJ_UINT32 p_data_size, |
9859 | | opj_stream_private_t *p_stream, |
9860 | | opj_event_mgr_t * p_manager) |
9861 | 15.0k | { |
9862 | 15.0k | OPJ_UINT32 l_current_marker; |
9863 | 15.0k | OPJ_BYTE l_data [2]; |
9864 | 15.0k | opj_tcp_t * l_tcp; |
9865 | 15.0k | opj_image_t* l_image_for_bounds; |
9866 | | |
9867 | | /* preconditions */ |
9868 | 15.0k | assert(p_stream != 00); |
9869 | 15.0k | assert(p_j2k != 00); |
9870 | 15.0k | assert(p_manager != 00); |
9871 | | |
9872 | 15.0k | if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA) |
9873 | 15.0k | || (p_tile_index != p_j2k->m_current_tile_number)) { |
9874 | 1 | return OPJ_FALSE; |
9875 | 1 | } |
9876 | | |
9877 | 15.0k | l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); |
9878 | 15.0k | if (! l_tcp->m_data) { |
9879 | 22 | opj_j2k_tcp_destroy(l_tcp); |
9880 | 22 | return OPJ_FALSE; |
9881 | 22 | } |
9882 | | |
9883 | | /* When using the opj_read_tile_header / opj_decode_tile_data API */ |
9884 | | /* such as in test_tile_decoder, m_output_image is NULL, so fall back */ |
9885 | | /* to the full image dimension. This is a bit surprising that */ |
9886 | | /* opj_set_decode_area() is only used to determine intersecting tiles, */ |
9887 | | /* but full tile decoding is done */ |
9888 | 15.0k | l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image : |
9889 | 15.0k | p_j2k->m_private_image; |
9890 | 15.0k | if (! opj_tcd_decode_tile(p_j2k->m_tcd, |
9891 | 15.0k | l_image_for_bounds->x0, |
9892 | 15.0k | l_image_for_bounds->y0, |
9893 | 15.0k | l_image_for_bounds->x1, |
9894 | 15.0k | l_image_for_bounds->y1, |
9895 | 15.0k | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode, |
9896 | 15.0k | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
9897 | 15.0k | l_tcp->m_data, |
9898 | 15.0k | l_tcp->m_data_size, |
9899 | 15.0k | p_tile_index, |
9900 | 15.0k | p_j2k->cstr_index, p_manager)) { |
9901 | 2.36k | opj_j2k_tcp_destroy(l_tcp); |
9902 | 2.36k | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
9903 | 2.36k | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n"); |
9904 | 2.36k | return OPJ_FALSE; |
9905 | 2.36k | } |
9906 | | |
9907 | | /* p_data can be set to NULL when the call will take care of using */ |
9908 | | /* itself the TCD data. This is typically the case for whole single */ |
9909 | | /* tile decoding optimization. */ |
9910 | 12.6k | if (p_data != NULL) { |
9911 | 0 | if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
9912 | 0 | return OPJ_FALSE; |
9913 | 0 | } |
9914 | | |
9915 | | /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) |
9916 | | * we destroy just the data which will be re-read in read_tile_header*/ |
9917 | | /*opj_j2k_tcp_destroy(l_tcp); |
9918 | | p_j2k->m_tcd->tcp = 0;*/ |
9919 | 0 | opj_j2k_tcp_data_destroy(l_tcp); |
9920 | 0 | } |
9921 | | |
9922 | 12.6k | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
9923 | 12.6k | p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA); |
9924 | | |
9925 | 12.6k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
9926 | 12.6k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
9927 | 9.59k | return OPJ_TRUE; |
9928 | 9.59k | } |
9929 | | |
9930 | 3.07k | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { |
9931 | 3.05k | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
9932 | 44 | opj_event_msg(p_manager, p_j2k->m_cp.strict ? EVT_ERROR : EVT_WARNING, |
9933 | 44 | "Stream too short\n"); |
9934 | 44 | return p_j2k->m_cp.strict ? OPJ_FALSE : OPJ_TRUE; |
9935 | 44 | } |
9936 | 3.00k | opj_read_bytes(l_data, &l_current_marker, 2); |
9937 | | |
9938 | 3.00k | if (l_current_marker == J2K_MS_EOC) { |
9939 | 3 | p_j2k->m_current_tile_number = 0; |
9940 | 3 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
9941 | 3.00k | } else if (l_current_marker != J2K_MS_SOT) { |
9942 | 845 | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
9943 | 599 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
9944 | 599 | opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n"); |
9945 | 599 | return OPJ_TRUE; |
9946 | 599 | } |
9947 | 246 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); |
9948 | 246 | return OPJ_FALSE; |
9949 | 845 | } |
9950 | 3.00k | } |
9951 | | |
9952 | 2.18k | return OPJ_TRUE; |
9953 | 3.07k | } |
9954 | | |
9955 | | static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, |
9956 | | opj_image_t* p_output_image) |
9957 | 12.3k | { |
9958 | 12.3k | OPJ_UINT32 i, j; |
9959 | 12.3k | OPJ_UINT32 l_width_src, l_height_src; |
9960 | 12.3k | OPJ_UINT32 l_width_dest, l_height_dest; |
9961 | 12.3k | OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src; |
9962 | 12.3k | OPJ_SIZE_T l_start_offset_src; |
9963 | 12.3k | OPJ_UINT32 l_start_x_dest, l_start_y_dest; |
9964 | 12.3k | OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest; |
9965 | 12.3k | OPJ_SIZE_T l_start_offset_dest; |
9966 | | |
9967 | 12.3k | opj_image_comp_t * l_img_comp_src = 00; |
9968 | 12.3k | opj_image_comp_t * l_img_comp_dest = 00; |
9969 | | |
9970 | 12.3k | opj_tcd_tilecomp_t * l_tilec = 00; |
9971 | 12.3k | opj_image_t * l_image_src = 00; |
9972 | 12.3k | OPJ_INT32 * l_dest_ptr; |
9973 | | |
9974 | 12.3k | l_tilec = p_tcd->tcd_image->tiles->comps; |
9975 | 12.3k | l_image_src = p_tcd->image; |
9976 | 12.3k | l_img_comp_src = l_image_src->comps; |
9977 | | |
9978 | 12.3k | l_img_comp_dest = p_output_image->comps; |
9979 | | |
9980 | 56.0k | for (i = 0; i < l_image_src->numcomps; |
9981 | 43.7k | i++, ++l_img_comp_dest, ++l_img_comp_src, ++l_tilec) { |
9982 | 43.7k | OPJ_INT32 res_x0, res_x1, res_y0, res_y1; |
9983 | 43.7k | OPJ_UINT32 src_data_stride; |
9984 | 43.7k | const OPJ_INT32* p_src_data; |
9985 | | |
9986 | | /* Copy info from decoded comp image to output image */ |
9987 | 43.7k | l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; |
9988 | | |
9989 | 43.7k | if (p_tcd->whole_tile_decoding) { |
9990 | 19.8k | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
9991 | 19.8k | l_img_comp_src->resno_decoded; |
9992 | 19.8k | res_x0 = l_res->x0; |
9993 | 19.8k | res_y0 = l_res->y0; |
9994 | 19.8k | res_x1 = l_res->x1; |
9995 | 19.8k | res_y1 = l_res->y1; |
9996 | 19.8k | src_data_stride = (OPJ_UINT32)( |
9997 | 19.8k | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 - |
9998 | 19.8k | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0); |
9999 | 19.8k | p_src_data = l_tilec->data; |
10000 | 23.8k | } else { |
10001 | 23.8k | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
10002 | 23.8k | l_img_comp_src->resno_decoded; |
10003 | 23.8k | res_x0 = (OPJ_INT32)l_res->win_x0; |
10004 | 23.8k | res_y0 = (OPJ_INT32)l_res->win_y0; |
10005 | 23.8k | res_x1 = (OPJ_INT32)l_res->win_x1; |
10006 | 23.8k | res_y1 = (OPJ_INT32)l_res->win_y1; |
10007 | 23.8k | src_data_stride = l_res->win_x1 - l_res->win_x0; |
10008 | 23.8k | p_src_data = l_tilec->data_win; |
10009 | 23.8k | } |
10010 | | |
10011 | 43.7k | if (p_src_data == NULL) { |
10012 | | /* Happens for partial component decoding */ |
10013 | 6.83k | continue; |
10014 | 6.83k | } |
10015 | | |
10016 | 36.8k | l_width_src = (OPJ_UINT32)(res_x1 - res_x0); |
10017 | 36.8k | l_height_src = (OPJ_UINT32)(res_y1 - res_y0); |
10018 | | |
10019 | | |
10020 | | /* Current tile component size*/ |
10021 | | /*if (i == 0) { |
10022 | | fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", |
10023 | | res_x0, res_x1, res_y0, res_y1); |
10024 | | }*/ |
10025 | | |
10026 | | |
10027 | | /* Border of the current output component*/ |
10028 | 36.8k | l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor); |
10029 | 36.8k | l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor); |
10030 | 36.8k | l_x1_dest = l_x0_dest + |
10031 | 36.8k | l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */ |
10032 | 36.8k | l_y1_dest = l_y0_dest + l_img_comp_dest->h; |
10033 | | |
10034 | | /*if (i == 0) { |
10035 | | fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", |
10036 | | l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); |
10037 | | }*/ |
10038 | | |
10039 | | /*-----*/ |
10040 | | /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) |
10041 | | * of the input buffer (decoded tile component) which will be move |
10042 | | * in the output buffer. Compute the area of the output buffer (l_start_x_dest, |
10043 | | * l_start_y_dest, l_width_dest, l_height_dest) which will be modified |
10044 | | * by this input area. |
10045 | | * */ |
10046 | 36.8k | assert(res_x0 >= 0); |
10047 | 36.8k | assert(res_x1 >= 0); |
10048 | 36.8k | if (l_x0_dest < (OPJ_UINT32)res_x0) { |
10049 | 4.16k | l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest; |
10050 | 4.16k | l_offset_x0_src = 0; |
10051 | | |
10052 | 4.16k | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
10053 | 4.16k | l_width_dest = l_width_src; |
10054 | 4.16k | l_offset_x1_src = 0; |
10055 | 4.16k | } else { |
10056 | 0 | l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ; |
10057 | 0 | l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest); |
10058 | 0 | } |
10059 | 32.7k | } else { |
10060 | 32.7k | l_start_x_dest = 0U; |
10061 | 32.7k | l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0; |
10062 | | |
10063 | 32.7k | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
10064 | 32.7k | l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src; |
10065 | 32.7k | l_offset_x1_src = 0; |
10066 | 32.7k | } else { |
10067 | 0 | l_width_dest = l_img_comp_dest->w ; |
10068 | 0 | l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest; |
10069 | 0 | } |
10070 | 32.7k | } |
10071 | | |
10072 | 36.8k | if (l_y0_dest < (OPJ_UINT32)res_y0) { |
10073 | 6.27k | l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest; |
10074 | 6.27k | l_offset_y0_src = 0; |
10075 | | |
10076 | 6.27k | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
10077 | 6.27k | l_height_dest = l_height_src; |
10078 | 6.27k | l_offset_y1_src = 0; |
10079 | 6.27k | } else { |
10080 | 0 | l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ; |
10081 | 0 | l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest); |
10082 | 0 | } |
10083 | 30.6k | } else { |
10084 | 30.6k | l_start_y_dest = 0U; |
10085 | 30.6k | l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0; |
10086 | | |
10087 | 30.6k | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
10088 | 30.6k | l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src; |
10089 | 30.6k | l_offset_y1_src = 0; |
10090 | 30.6k | } else { |
10091 | 0 | l_height_dest = l_img_comp_dest->h ; |
10092 | 0 | l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest; |
10093 | 0 | } |
10094 | 30.6k | } |
10095 | | |
10096 | 36.8k | if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) || |
10097 | 36.8k | (l_offset_y1_src < 0)) { |
10098 | 0 | return OPJ_FALSE; |
10099 | 0 | } |
10100 | | /* testcase 2977.pdf.asan.67.2198 */ |
10101 | 36.8k | if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) { |
10102 | 57 | return OPJ_FALSE; |
10103 | 57 | } |
10104 | | /*-----*/ |
10105 | | |
10106 | | /* Compute the input buffer offset */ |
10107 | 36.8k | l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src |
10108 | 36.8k | * (OPJ_SIZE_T)src_data_stride; |
10109 | | |
10110 | | /* Compute the output buffer offset */ |
10111 | 36.8k | l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest |
10112 | 36.8k | * (OPJ_SIZE_T)l_img_comp_dest->w; |
10113 | | |
10114 | | /* Allocate output component buffer if necessary */ |
10115 | 36.8k | if (l_img_comp_dest->data == NULL && |
10116 | 36.8k | l_start_offset_src == 0 && l_start_offset_dest == 0 && |
10117 | 36.8k | src_data_stride == l_img_comp_dest->w && |
10118 | 36.8k | l_width_dest == l_img_comp_dest->w && |
10119 | 36.8k | l_height_dest == l_img_comp_dest->h) { |
10120 | | /* If the final image matches the tile buffer, then borrow it */ |
10121 | | /* directly to save a copy */ |
10122 | 16.3k | if (p_tcd->whole_tile_decoding) { |
10123 | 2.79k | l_img_comp_dest->data = l_tilec->data; |
10124 | 2.79k | l_tilec->data = NULL; |
10125 | 13.5k | } else { |
10126 | 13.5k | l_img_comp_dest->data = l_tilec->data_win; |
10127 | 13.5k | l_tilec->data_win = NULL; |
10128 | 13.5k | } |
10129 | 16.3k | continue; |
10130 | 20.5k | } else if (l_img_comp_dest->data == NULL) { |
10131 | 14.1k | OPJ_SIZE_T l_width = l_img_comp_dest->w; |
10132 | 14.1k | OPJ_SIZE_T l_height = l_img_comp_dest->h; |
10133 | | |
10134 | 14.1k | if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) || |
10135 | 14.1k | l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) { |
10136 | | /* would overflow */ |
10137 | 0 | return OPJ_FALSE; |
10138 | 0 | } |
10139 | 14.1k | l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height * |
10140 | 14.1k | sizeof(OPJ_INT32)); |
10141 | 14.1k | if (! l_img_comp_dest->data) { |
10142 | 0 | return OPJ_FALSE; |
10143 | 0 | } |
10144 | | |
10145 | 14.1k | if (l_img_comp_dest->w != l_width_dest || |
10146 | 14.1k | l_img_comp_dest->h != l_height_dest) { |
10147 | 14.1k | memset(l_img_comp_dest->data, 0, |
10148 | 14.1k | (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32)); |
10149 | 14.1k | } |
10150 | 14.1k | } |
10151 | | |
10152 | | /* Move the output buffer to the first place where we will write*/ |
10153 | 20.5k | l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; |
10154 | | |
10155 | 20.5k | { |
10156 | 20.5k | const OPJ_INT32 * l_src_ptr = p_src_data; |
10157 | 20.5k | l_src_ptr += l_start_offset_src; |
10158 | | |
10159 | 2.98M | for (j = 0; j < l_height_dest; ++j) { |
10160 | 2.96M | memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32)); |
10161 | 2.96M | l_dest_ptr += l_img_comp_dest->w; |
10162 | 2.96M | l_src_ptr += src_data_stride; |
10163 | 2.96M | } |
10164 | 20.5k | } |
10165 | | |
10166 | | |
10167 | 20.5k | } |
10168 | | |
10169 | 12.3k | return OPJ_TRUE; |
10170 | 12.3k | } |
10171 | | |
10172 | | static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image, |
10173 | | opj_event_mgr_t * p_manager) |
10174 | 14.0k | { |
10175 | 14.0k | OPJ_UINT32 it_comp; |
10176 | 14.0k | OPJ_INT32 l_comp_x1, l_comp_y1; |
10177 | 14.0k | opj_image_comp_t* l_img_comp = NULL; |
10178 | | |
10179 | 14.0k | l_img_comp = p_image->comps; |
10180 | 61.8k | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
10181 | 47.7k | OPJ_INT32 l_h, l_w; |
10182 | 47.7k | if (p_image->x0 > (OPJ_UINT32)INT_MAX || |
10183 | 47.7k | p_image->y0 > (OPJ_UINT32)INT_MAX || |
10184 | 47.7k | p_image->x1 > (OPJ_UINT32)INT_MAX || |
10185 | 47.7k | p_image->y1 > (OPJ_UINT32)INT_MAX) { |
10186 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10187 | 0 | "Image coordinates above INT_MAX are not supported\n"); |
10188 | 0 | return OPJ_FALSE; |
10189 | 0 | } |
10190 | | |
10191 | 47.7k | l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx); |
10192 | 47.7k | l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy); |
10193 | 47.7k | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
10194 | 47.7k | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
10195 | | |
10196 | 47.7k | l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) |
10197 | 47.7k | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor); |
10198 | 47.7k | if (l_w < 0) { |
10199 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10200 | 0 | "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", |
10201 | 0 | it_comp, l_w); |
10202 | 0 | return OPJ_FALSE; |
10203 | 0 | } |
10204 | 47.7k | l_img_comp->w = (OPJ_UINT32)l_w; |
10205 | | |
10206 | 47.7k | l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) |
10207 | 47.7k | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor); |
10208 | 47.7k | if (l_h < 0) { |
10209 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10210 | 0 | "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", |
10211 | 0 | it_comp, l_h); |
10212 | 0 | return OPJ_FALSE; |
10213 | 0 | } |
10214 | 47.7k | l_img_comp->h = (OPJ_UINT32)l_h; |
10215 | | |
10216 | 47.7k | l_img_comp++; |
10217 | 47.7k | } |
10218 | | |
10219 | 14.0k | return OPJ_TRUE; |
10220 | 14.0k | } |
10221 | | |
10222 | | OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k, |
10223 | | OPJ_UINT32 numcomps, |
10224 | | const OPJ_UINT32* comps_indices, |
10225 | | opj_event_mgr_t * p_manager) |
10226 | 0 | { |
10227 | 0 | OPJ_UINT32 i; |
10228 | 0 | OPJ_BOOL* already_mapped; |
10229 | |
|
10230 | 0 | if (p_j2k->m_private_image == NULL) { |
10231 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10232 | 0 | "opj_read_header() should be called before " |
10233 | 0 | "opj_set_decoded_components().\n"); |
10234 | 0 | return OPJ_FALSE; |
10235 | 0 | } |
10236 | | |
10237 | 0 | already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL), |
10238 | 0 | p_j2k->m_private_image->numcomps); |
10239 | 0 | if (already_mapped == NULL) { |
10240 | 0 | return OPJ_FALSE; |
10241 | 0 | } |
10242 | | |
10243 | 0 | for (i = 0; i < numcomps; i++) { |
10244 | 0 | if (comps_indices[i] >= p_j2k->m_private_image->numcomps) { |
10245 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10246 | 0 | "Invalid component index: %u\n", |
10247 | 0 | comps_indices[i]); |
10248 | 0 | opj_free(already_mapped); |
10249 | 0 | return OPJ_FALSE; |
10250 | 0 | } |
10251 | 0 | if (already_mapped[comps_indices[i]]) { |
10252 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10253 | 0 | "Component index %u used several times\n", |
10254 | 0 | comps_indices[i]); |
10255 | 0 | opj_free(already_mapped); |
10256 | 0 | return OPJ_FALSE; |
10257 | 0 | } |
10258 | 0 | already_mapped[comps_indices[i]] = OPJ_TRUE; |
10259 | 0 | } |
10260 | 0 | opj_free(already_mapped); |
10261 | |
|
10262 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
10263 | 0 | if (numcomps) { |
10264 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = |
10265 | 0 | (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32)); |
10266 | 0 | if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) { |
10267 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
10268 | 0 | return OPJ_FALSE; |
10269 | 0 | } |
10270 | 0 | memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
10271 | 0 | comps_indices, |
10272 | 0 | numcomps * sizeof(OPJ_UINT32)); |
10273 | 0 | } else { |
10274 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL; |
10275 | 0 | } |
10276 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps; |
10277 | |
|
10278 | 0 | return OPJ_TRUE; |
10279 | 0 | } |
10280 | | |
10281 | | |
10282 | | OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k, |
10283 | | opj_image_t* p_image, |
10284 | | OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, |
10285 | | OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, |
10286 | | opj_event_mgr_t * p_manager) |
10287 | 14.1k | { |
10288 | 14.1k | opj_cp_t * l_cp = &(p_j2k->m_cp); |
10289 | 14.1k | opj_image_t * l_image = p_j2k->m_private_image; |
10290 | 14.1k | OPJ_BOOL ret; |
10291 | 14.1k | OPJ_UINT32 it_comp; |
10292 | | |
10293 | 14.1k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
10294 | 14.1k | p_j2k->m_cp.tcps[0].m_data != NULL) { |
10295 | | /* In the case of a single-tiled image whose codestream we have already */ |
10296 | | /* ingested, go on */ |
10297 | 0 | } |
10298 | | /* Check if we are read the main header */ |
10299 | 14.1k | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
10300 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10301 | 0 | "Need to decode the main header before begin to decode the remaining codestream.\n"); |
10302 | 0 | return OPJ_FALSE; |
10303 | 0 | } |
10304 | | |
10305 | | /* Update the comps[].factor member of the output image with the one */ |
10306 | | /* of m_reduce */ |
10307 | 61.9k | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
10308 | 47.8k | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
10309 | 47.8k | } |
10310 | | |
10311 | 14.1k | if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) { |
10312 | 0 | opj_event_msg(p_manager, EVT_INFO, |
10313 | 0 | "No decoded area parameters, set the decoded area to the whole image\n"); |
10314 | |
|
10315 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
10316 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
10317 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
10318 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
10319 | |
|
10320 | 0 | p_image->x0 = l_image->x0; |
10321 | 0 | p_image->y0 = l_image->y0; |
10322 | 0 | p_image->x1 = l_image->x1; |
10323 | 0 | p_image->y1 = l_image->y1; |
10324 | |
|
10325 | 0 | return opj_j2k_update_image_dimensions(p_image, p_manager); |
10326 | 0 | } |
10327 | | |
10328 | | /* ----- */ |
10329 | | /* Check if the positions provided by the user are correct */ |
10330 | | |
10331 | | /* Left */ |
10332 | 14.1k | if (p_start_x < 0) { |
10333 | 13 | opj_event_msg(p_manager, EVT_ERROR, |
10334 | 13 | "Left position of the decoded area (region_x0=%d) should be >= 0.\n", |
10335 | 13 | p_start_x); |
10336 | 13 | return OPJ_FALSE; |
10337 | 14.0k | } else if ((OPJ_UINT32)p_start_x > l_image->x1) { |
10338 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10339 | 0 | "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n", |
10340 | 0 | p_start_x, l_image->x1); |
10341 | 0 | return OPJ_FALSE; |
10342 | 14.0k | } else if ((OPJ_UINT32)p_start_x < l_image->x0) { |
10343 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10344 | 0 | "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n", |
10345 | 0 | p_start_x, l_image->x0); |
10346 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
10347 | 0 | p_image->x0 = l_image->x0; |
10348 | 14.0k | } else { |
10349 | 14.0k | p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - |
10350 | 14.0k | l_cp->tx0) / l_cp->tdx; |
10351 | 14.0k | p_image->x0 = (OPJ_UINT32)p_start_x; |
10352 | 14.0k | } |
10353 | | |
10354 | | /* Up */ |
10355 | 14.0k | if (p_start_y < 0) { |
10356 | 21 | opj_event_msg(p_manager, EVT_ERROR, |
10357 | 21 | "Up position of the decoded area (region_y0=%d) should be >= 0.\n", |
10358 | 21 | p_start_y); |
10359 | 21 | return OPJ_FALSE; |
10360 | 14.0k | } else if ((OPJ_UINT32)p_start_y > l_image->y1) { |
10361 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10362 | 0 | "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n", |
10363 | 0 | p_start_y, l_image->y1); |
10364 | 0 | return OPJ_FALSE; |
10365 | 14.0k | } else if ((OPJ_UINT32)p_start_y < l_image->y0) { |
10366 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10367 | 0 | "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n", |
10368 | 0 | p_start_y, l_image->y0); |
10369 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
10370 | 0 | p_image->y0 = l_image->y0; |
10371 | 14.0k | } else { |
10372 | 14.0k | p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - |
10373 | 14.0k | l_cp->ty0) / l_cp->tdy; |
10374 | 14.0k | p_image->y0 = (OPJ_UINT32)p_start_y; |
10375 | 14.0k | } |
10376 | | |
10377 | | /* Right */ |
10378 | 14.0k | if (p_end_x <= 0) { |
10379 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
10380 | 1 | "Right position of the decoded area (region_x1=%d) should be > 0.\n", |
10381 | 1 | p_end_x); |
10382 | 1 | return OPJ_FALSE; |
10383 | 14.0k | } else if ((OPJ_UINT32)p_end_x < l_image->x0) { |
10384 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10385 | 0 | "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n", |
10386 | 0 | p_end_x, l_image->x0); |
10387 | 0 | return OPJ_FALSE; |
10388 | 14.0k | } else if ((OPJ_UINT32)p_end_x > l_image->x1) { |
10389 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10390 | 0 | "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n", |
10391 | 0 | p_end_x, l_image->x1); |
10392 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
10393 | 0 | p_image->x1 = l_image->x1; |
10394 | 14.0k | } else { |
10395 | 14.0k | p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv(( |
10396 | 14.0k | OPJ_UINT32)p_end_x - l_cp->tx0, l_cp->tdx); |
10397 | 14.0k | p_image->x1 = (OPJ_UINT32)p_end_x; |
10398 | 14.0k | } |
10399 | | |
10400 | | /* Bottom */ |
10401 | 14.0k | if (p_end_y <= 0) { |
10402 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
10403 | 1 | "Bottom position of the decoded area (region_y1=%d) should be > 0.\n", |
10404 | 1 | p_end_y); |
10405 | 1 | return OPJ_FALSE; |
10406 | 14.0k | } else if ((OPJ_UINT32)p_end_y < l_image->y0) { |
10407 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10408 | 0 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n", |
10409 | 0 | p_end_y, l_image->y0); |
10410 | 0 | return OPJ_FALSE; |
10411 | 0 | } |
10412 | 14.0k | if ((OPJ_UINT32)p_end_y > l_image->y1) { |
10413 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10414 | 0 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n", |
10415 | 0 | p_end_y, l_image->y1); |
10416 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
10417 | 0 | p_image->y1 = l_image->y1; |
10418 | 14.0k | } else { |
10419 | 14.0k | p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv(( |
10420 | 14.0k | OPJ_UINT32)p_end_y - l_cp->ty0, l_cp->tdy); |
10421 | 14.0k | p_image->y1 = (OPJ_UINT32)p_end_y; |
10422 | 14.0k | } |
10423 | | /* ----- */ |
10424 | | |
10425 | 14.0k | p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; |
10426 | | |
10427 | 14.0k | ret = opj_j2k_update_image_dimensions(p_image, p_manager); |
10428 | | |
10429 | 14.0k | if (ret) { |
10430 | 14.0k | opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n", |
10431 | 14.0k | p_image->x0, p_image->y0, p_image->x1, p_image->y1); |
10432 | 14.0k | } |
10433 | | |
10434 | 14.0k | return ret; |
10435 | 14.0k | } |
10436 | | |
10437 | | opj_j2k_t* opj_j2k_create_decompress(void) |
10438 | 17.8k | { |
10439 | 17.8k | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
10440 | 17.8k | if (!l_j2k) { |
10441 | 0 | return 00; |
10442 | 0 | } |
10443 | | |
10444 | 17.8k | l_j2k->m_is_decoder = 1; |
10445 | 17.8k | l_j2k->m_cp.m_is_decoder = 1; |
10446 | | /* in the absence of JP2 boxes, consider different bit depth / sign */ |
10447 | | /* per component is allowed */ |
10448 | 17.8k | l_j2k->m_cp.allow_different_bit_depth_sign = 1; |
10449 | | |
10450 | | /* Default to using strict mode. */ |
10451 | 17.8k | l_j2k->m_cp.strict = OPJ_TRUE; |
10452 | | |
10453 | | #ifdef OPJ_DISABLE_TPSOT_FIX |
10454 | | l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
10455 | | #endif |
10456 | | |
10457 | 17.8k | l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1, |
10458 | 17.8k | sizeof(opj_tcp_t)); |
10459 | 17.8k | if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { |
10460 | 0 | opj_j2k_destroy(l_j2k); |
10461 | 0 | return 00; |
10462 | 0 | } |
10463 | | |
10464 | 17.8k | l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1, |
10465 | 17.8k | OPJ_J2K_DEFAULT_HEADER_SIZE); |
10466 | 17.8k | if (! l_j2k->m_specific_param.m_decoder.m_header_data) { |
10467 | 0 | opj_j2k_destroy(l_j2k); |
10468 | 0 | return 00; |
10469 | 0 | } |
10470 | | |
10471 | 17.8k | l_j2k->m_specific_param.m_decoder.m_header_data_size = |
10472 | 17.8k | OPJ_J2K_DEFAULT_HEADER_SIZE; |
10473 | | |
10474 | 17.8k | l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; |
10475 | | |
10476 | 17.8k | l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; |
10477 | | |
10478 | | /* codestream index creation */ |
10479 | 17.8k | l_j2k->cstr_index = opj_j2k_create_cstr_index(); |
10480 | 17.8k | if (!l_j2k->cstr_index) { |
10481 | 0 | opj_j2k_destroy(l_j2k); |
10482 | 0 | return 00; |
10483 | 0 | } |
10484 | | |
10485 | | /* validation list creation */ |
10486 | 17.8k | l_j2k->m_validation_list = opj_procedure_list_create(); |
10487 | 17.8k | if (! l_j2k->m_validation_list) { |
10488 | 0 | opj_j2k_destroy(l_j2k); |
10489 | 0 | return 00; |
10490 | 0 | } |
10491 | | |
10492 | | /* execution list creation */ |
10493 | 17.8k | l_j2k->m_procedure_list = opj_procedure_list_create(); |
10494 | 17.8k | if (! l_j2k->m_procedure_list) { |
10495 | 0 | opj_j2k_destroy(l_j2k); |
10496 | 0 | return 00; |
10497 | 0 | } |
10498 | | |
10499 | 17.8k | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
10500 | 17.8k | if (!l_j2k->m_tp) { |
10501 | 0 | l_j2k->m_tp = opj_thread_pool_create(0); |
10502 | 0 | } |
10503 | 17.8k | if (!l_j2k->m_tp) { |
10504 | 0 | opj_j2k_destroy(l_j2k); |
10505 | 0 | return NULL; |
10506 | 0 | } |
10507 | | |
10508 | 17.8k | return l_j2k; |
10509 | 17.8k | } |
10510 | | |
10511 | | static opj_codestream_index_t* opj_j2k_create_cstr_index(void) |
10512 | 17.8k | { |
10513 | 17.8k | opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) |
10514 | 17.8k | opj_calloc(1, sizeof(opj_codestream_index_t)); |
10515 | 17.8k | if (!cstr_index) { |
10516 | 0 | return NULL; |
10517 | 0 | } |
10518 | | |
10519 | 17.8k | cstr_index->maxmarknum = 100; |
10520 | 17.8k | cstr_index->marknum = 0; |
10521 | 17.8k | cstr_index->marker = (opj_marker_info_t*) |
10522 | 17.8k | opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); |
10523 | 17.8k | if (!cstr_index-> marker) { |
10524 | 0 | opj_free(cstr_index); |
10525 | 0 | return NULL; |
10526 | 0 | } |
10527 | | |
10528 | 17.8k | cstr_index->tile_index = NULL; |
10529 | | |
10530 | 17.8k | return cstr_index; |
10531 | 17.8k | } |
10532 | | |
10533 | | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, |
10534 | | OPJ_UINT32 p_tile_no, |
10535 | | OPJ_UINT32 p_comp_no) |
10536 | 0 | { |
10537 | 0 | opj_cp_t *l_cp = 00; |
10538 | 0 | opj_tcp_t *l_tcp = 00; |
10539 | 0 | opj_tccp_t *l_tccp = 00; |
10540 | | |
10541 | | /* preconditions */ |
10542 | 0 | assert(p_j2k != 00); |
10543 | |
|
10544 | 0 | l_cp = &(p_j2k->m_cp); |
10545 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10546 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10547 | | |
10548 | | /* preconditions again */ |
10549 | 0 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
10550 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
10551 | |
|
10552 | 0 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
10553 | 0 | return 5 + l_tccp->numresolutions; |
10554 | 0 | } else { |
10555 | 0 | return 5; |
10556 | 0 | } |
10557 | 0 | } |
10558 | | |
10559 | | static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10560 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
10561 | 0 | { |
10562 | 0 | OPJ_UINT32 i; |
10563 | 0 | opj_cp_t *l_cp = NULL; |
10564 | 0 | opj_tcp_t *l_tcp = NULL; |
10565 | 0 | opj_tccp_t *l_tccp0 = NULL; |
10566 | 0 | opj_tccp_t *l_tccp1 = NULL; |
10567 | | |
10568 | | /* preconditions */ |
10569 | 0 | assert(p_j2k != 00); |
10570 | |
|
10571 | 0 | l_cp = &(p_j2k->m_cp); |
10572 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10573 | 0 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
10574 | 0 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
10575 | |
|
10576 | 0 | if (l_tccp0->numresolutions != l_tccp1->numresolutions) { |
10577 | 0 | return OPJ_FALSE; |
10578 | 0 | } |
10579 | 0 | if (l_tccp0->cblkw != l_tccp1->cblkw) { |
10580 | 0 | return OPJ_FALSE; |
10581 | 0 | } |
10582 | 0 | if (l_tccp0->cblkh != l_tccp1->cblkh) { |
10583 | 0 | return OPJ_FALSE; |
10584 | 0 | } |
10585 | 0 | if (l_tccp0->cblksty != l_tccp1->cblksty) { |
10586 | 0 | return OPJ_FALSE; |
10587 | 0 | } |
10588 | 0 | if (l_tccp0->qmfbid != l_tccp1->qmfbid) { |
10589 | 0 | return OPJ_FALSE; |
10590 | 0 | } |
10591 | 0 | if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) { |
10592 | 0 | return OPJ_FALSE; |
10593 | 0 | } |
10594 | | |
10595 | 0 | for (i = 0U; i < l_tccp0->numresolutions; ++i) { |
10596 | 0 | if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) { |
10597 | 0 | return OPJ_FALSE; |
10598 | 0 | } |
10599 | 0 | if (l_tccp0->prch[i] != l_tccp1->prch[i]) { |
10600 | 0 | return OPJ_FALSE; |
10601 | 0 | } |
10602 | 0 | } |
10603 | 0 | return OPJ_TRUE; |
10604 | 0 | } |
10605 | | |
10606 | | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10607 | | OPJ_UINT32 p_tile_no, |
10608 | | OPJ_UINT32 p_comp_no, |
10609 | | OPJ_BYTE * p_data, |
10610 | | OPJ_UINT32 * p_header_size, |
10611 | | struct opj_event_mgr * p_manager) |
10612 | 0 | { |
10613 | 0 | OPJ_UINT32 i; |
10614 | 0 | opj_cp_t *l_cp = 00; |
10615 | 0 | opj_tcp_t *l_tcp = 00; |
10616 | 0 | opj_tccp_t *l_tccp = 00; |
10617 | | |
10618 | | /* preconditions */ |
10619 | 0 | assert(p_j2k != 00); |
10620 | 0 | assert(p_header_size != 00); |
10621 | 0 | assert(p_manager != 00); |
10622 | 0 | assert(p_data != 00); |
10623 | |
|
10624 | 0 | l_cp = &(p_j2k->m_cp); |
10625 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10626 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10627 | | |
10628 | | /* preconditions again */ |
10629 | 0 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
10630 | 0 | assert(p_comp_no < (p_j2k->m_private_image->numcomps)); |
10631 | |
|
10632 | 0 | if (*p_header_size < 5) { |
10633 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
10634 | 0 | return OPJ_FALSE; |
10635 | 0 | } |
10636 | | |
10637 | 0 | opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ |
10638 | 0 | ++p_data; |
10639 | |
|
10640 | 0 | opj_write_bytes(p_data, l_tccp->cblkw - 2, 1); /* SPcoc (E) */ |
10641 | 0 | ++p_data; |
10642 | |
|
10643 | 0 | opj_write_bytes(p_data, l_tccp->cblkh - 2, 1); /* SPcoc (F) */ |
10644 | 0 | ++p_data; |
10645 | |
|
10646 | 0 | opj_write_bytes(p_data, l_tccp->cblksty, |
10647 | 0 | 1); /* SPcoc (G) */ |
10648 | 0 | ++p_data; |
10649 | |
|
10650 | 0 | opj_write_bytes(p_data, l_tccp->qmfbid, |
10651 | 0 | 1); /* SPcoc (H) */ |
10652 | 0 | ++p_data; |
10653 | |
|
10654 | 0 | *p_header_size = *p_header_size - 5; |
10655 | |
|
10656 | 0 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
10657 | |
|
10658 | 0 | if (*p_header_size < l_tccp->numresolutions) { |
10659 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
10660 | 0 | return OPJ_FALSE; |
10661 | 0 | } |
10662 | | |
10663 | 0 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
10664 | 0 | opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4), |
10665 | 0 | 1); /* SPcoc (I_i) */ |
10666 | 0 | ++p_data; |
10667 | 0 | } |
10668 | |
|
10669 | 0 | *p_header_size = *p_header_size - l_tccp->numresolutions; |
10670 | 0 | } |
10671 | | |
10672 | 0 | return OPJ_TRUE; |
10673 | 0 | } |
10674 | | |
10675 | | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10676 | | OPJ_UINT32 compno, |
10677 | | OPJ_BYTE * p_header_data, |
10678 | | OPJ_UINT32 * p_header_size, |
10679 | | opj_event_mgr_t * p_manager) |
10680 | 20.3k | { |
10681 | 20.3k | OPJ_UINT32 i, l_tmp; |
10682 | 20.3k | opj_cp_t *l_cp = NULL; |
10683 | 20.3k | opj_tcp_t *l_tcp = NULL; |
10684 | 20.3k | opj_tccp_t *l_tccp = NULL; |
10685 | 20.3k | OPJ_BYTE * l_current_ptr = NULL; |
10686 | | |
10687 | | /* preconditions */ |
10688 | 20.3k | assert(p_j2k != 00); |
10689 | 20.3k | assert(p_manager != 00); |
10690 | 20.3k | assert(p_header_data != 00); |
10691 | | |
10692 | 20.3k | l_cp = &(p_j2k->m_cp); |
10693 | 20.3k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
10694 | 758 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
10695 | 20.3k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
10696 | | |
10697 | | /* precondition again */ |
10698 | 20.3k | assert(compno < p_j2k->m_private_image->numcomps); |
10699 | | |
10700 | 20.3k | l_tccp = &l_tcp->tccps[compno]; |
10701 | 20.3k | l_current_ptr = p_header_data; |
10702 | | |
10703 | | /* make sure room is sufficient */ |
10704 | 20.3k | if (*p_header_size < 5) { |
10705 | 10 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
10706 | 10 | return OPJ_FALSE; |
10707 | 10 | } |
10708 | | |
10709 | | /* SPcod (D) / SPcoc (A) */ |
10710 | 20.3k | opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, 1); |
10711 | 20.3k | ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ |
10712 | 20.3k | if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { |
10713 | 11 | opj_event_msg(p_manager, EVT_ERROR, |
10714 | 11 | "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n", |
10715 | 11 | l_tccp->numresolutions, OPJ_J2K_MAXRLVLS); |
10716 | 11 | return OPJ_FALSE; |
10717 | 11 | } |
10718 | 20.3k | ++l_current_ptr; |
10719 | | |
10720 | | /* If user wants to remove more resolutions than the codestream contains, return error */ |
10721 | 20.3k | if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { |
10722 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10723 | 0 | "Error decoding component %d.\nThe number of resolutions " |
10724 | 0 | "to remove (%d) is greater or equal than the number " |
10725 | 0 | "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n", |
10726 | 0 | compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions); |
10727 | 0 | p_j2k->m_specific_param.m_decoder.m_state |= |
10728 | 0 | 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ |
10729 | 0 | return OPJ_FALSE; |
10730 | 0 | } |
10731 | | |
10732 | | /* SPcod (E) / SPcoc (B) */ |
10733 | 20.3k | opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1); |
10734 | 20.3k | ++l_current_ptr; |
10735 | 20.3k | l_tccp->cblkw += 2; |
10736 | | |
10737 | | /* SPcod (F) / SPcoc (C) */ |
10738 | 20.3k | opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1); |
10739 | 20.3k | ++l_current_ptr; |
10740 | 20.3k | l_tccp->cblkh += 2; |
10741 | | |
10742 | 20.3k | if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || |
10743 | 20.3k | ((l_tccp->cblkw + l_tccp->cblkh) > 12)) { |
10744 | 24 | opj_event_msg(p_manager, EVT_ERROR, |
10745 | 24 | "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n"); |
10746 | 24 | return OPJ_FALSE; |
10747 | 24 | } |
10748 | | |
10749 | | /* SPcod (G) / SPcoc (D) */ |
10750 | 20.3k | opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1); |
10751 | 20.3k | ++l_current_ptr; |
10752 | 20.3k | if ((l_tccp->cblksty & J2K_CCP_CBLKSTY_HTMIXED) != 0) { |
10753 | | /* We do not support HT mixed mode yet. For conformance, it should be supported.*/ |
10754 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
10755 | 1 | "Error reading SPCod SPCoc element. Unsupported Mixed HT code-block style found\n"); |
10756 | 1 | return OPJ_FALSE; |
10757 | 1 | } |
10758 | | |
10759 | | /* SPcod (H) / SPcoc (E) */ |
10760 | 20.3k | opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1); |
10761 | 20.3k | ++l_current_ptr; |
10762 | | |
10763 | 20.3k | if (l_tccp->qmfbid > 1) { |
10764 | 15 | opj_event_msg(p_manager, EVT_ERROR, |
10765 | 15 | "Error reading SPCod SPCoc element, Invalid transformation found\n"); |
10766 | 15 | return OPJ_FALSE; |
10767 | 15 | } |
10768 | | |
10769 | 20.3k | *p_header_size = *p_header_size - 5; |
10770 | | |
10771 | | /* use custom precinct size ? */ |
10772 | 20.3k | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
10773 | 6.29k | if (*p_header_size < l_tccp->numresolutions) { |
10774 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
10775 | 3 | return OPJ_FALSE; |
10776 | 3 | } |
10777 | | |
10778 | | /* SPcod (I_i) / SPcoc (F_i) */ |
10779 | 14.3k | for (i = 0; i < l_tccp->numresolutions; ++i) { |
10780 | 8.08k | opj_read_bytes(l_current_ptr, &l_tmp, 1); |
10781 | 8.08k | ++l_current_ptr; |
10782 | | /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */ |
10783 | 8.08k | if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) { |
10784 | 13 | opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n"); |
10785 | 13 | return OPJ_FALSE; |
10786 | 13 | } |
10787 | 8.07k | l_tccp->prcw[i] = l_tmp & 0xf; |
10788 | 8.07k | l_tccp->prch[i] = l_tmp >> 4; |
10789 | 8.07k | } |
10790 | | |
10791 | 6.27k | *p_header_size = *p_header_size - l_tccp->numresolutions; |
10792 | 14.0k | } else { |
10793 | | /* set default size for the precinct width and height */ |
10794 | 79.0k | for (i = 0; i < l_tccp->numresolutions; ++i) { |
10795 | 64.9k | l_tccp->prcw[i] = 15; |
10796 | 64.9k | l_tccp->prch[i] = 15; |
10797 | 64.9k | } |
10798 | 14.0k | } |
10799 | | |
10800 | | #ifdef WIP_REMOVE_MSD |
10801 | | /* INDEX >> */ |
10802 | | if (p_j2k->cstr_info && compno == 0) { |
10803 | | OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); |
10804 | | |
10805 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = |
10806 | | l_tccp->cblkh; |
10807 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = |
10808 | | l_tccp->cblkw; |
10809 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions |
10810 | | = l_tccp->numresolutions; |
10811 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = |
10812 | | l_tccp->cblksty; |
10813 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = |
10814 | | l_tccp->qmfbid; |
10815 | | |
10816 | | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw, |
10817 | | l_data_size); |
10818 | | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch, |
10819 | | l_data_size); |
10820 | | } |
10821 | | /* << INDEX */ |
10822 | | #endif |
10823 | | |
10824 | 20.2k | return OPJ_TRUE; |
10825 | 20.3k | } |
10826 | | |
10827 | | static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k) |
10828 | 19.5k | { |
10829 | | /* loop */ |
10830 | 19.5k | OPJ_UINT32 i; |
10831 | 19.5k | opj_cp_t *l_cp = NULL; |
10832 | 19.5k | opj_tcp_t *l_tcp = NULL; |
10833 | 19.5k | opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; |
10834 | 19.5k | OPJ_UINT32 l_prc_size; |
10835 | | |
10836 | | /* preconditions */ |
10837 | 19.5k | assert(p_j2k != 00); |
10838 | | |
10839 | 19.5k | l_cp = &(p_j2k->m_cp); |
10840 | 19.5k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
10841 | 19.5k | ? |
10842 | 505 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
10843 | 19.5k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
10844 | | |
10845 | 19.5k | l_ref_tccp = &l_tcp->tccps[0]; |
10846 | 19.5k | l_copied_tccp = l_ref_tccp + 1; |
10847 | 19.5k | l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32); |
10848 | | |
10849 | 81.6k | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
10850 | 62.1k | l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; |
10851 | 62.1k | l_copied_tccp->cblkw = l_ref_tccp->cblkw; |
10852 | 62.1k | l_copied_tccp->cblkh = l_ref_tccp->cblkh; |
10853 | 62.1k | l_copied_tccp->cblksty = l_ref_tccp->cblksty; |
10854 | 62.1k | l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; |
10855 | 62.1k | memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size); |
10856 | 62.1k | memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size); |
10857 | 62.1k | ++l_copied_tccp; |
10858 | 62.1k | } |
10859 | 19.5k | } |
10860 | | |
10861 | | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, |
10862 | | OPJ_UINT32 p_tile_no, |
10863 | | OPJ_UINT32 p_comp_no) |
10864 | 0 | { |
10865 | 0 | OPJ_UINT32 l_num_bands; |
10866 | |
|
10867 | 0 | opj_cp_t *l_cp = 00; |
10868 | 0 | opj_tcp_t *l_tcp = 00; |
10869 | 0 | opj_tccp_t *l_tccp = 00; |
10870 | | |
10871 | | /* preconditions */ |
10872 | 0 | assert(p_j2k != 00); |
10873 | |
|
10874 | 0 | l_cp = &(p_j2k->m_cp); |
10875 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10876 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10877 | | |
10878 | | /* preconditions again */ |
10879 | 0 | assert(p_tile_no < l_cp->tw * l_cp->th); |
10880 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
10881 | |
|
10882 | 0 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
10883 | 0 | (l_tccp->numresolutions * 3 - 2); |
10884 | |
|
10885 | 0 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
10886 | 0 | return 1 + l_num_bands; |
10887 | 0 | } else { |
10888 | 0 | return 1 + 2 * l_num_bands; |
10889 | 0 | } |
10890 | 0 | } |
10891 | | |
10892 | | static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, |
10893 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
10894 | 0 | { |
10895 | 0 | opj_cp_t *l_cp = NULL; |
10896 | 0 | opj_tcp_t *l_tcp = NULL; |
10897 | 0 | opj_tccp_t *l_tccp0 = NULL; |
10898 | 0 | opj_tccp_t *l_tccp1 = NULL; |
10899 | 0 | OPJ_UINT32 l_band_no, l_num_bands; |
10900 | | |
10901 | | /* preconditions */ |
10902 | 0 | assert(p_j2k != 00); |
10903 | |
|
10904 | 0 | l_cp = &(p_j2k->m_cp); |
10905 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10906 | 0 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
10907 | 0 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
10908 | |
|
10909 | 0 | if (l_tccp0->qntsty != l_tccp1->qntsty) { |
10910 | 0 | return OPJ_FALSE; |
10911 | 0 | } |
10912 | 0 | if (l_tccp0->numgbits != l_tccp1->numgbits) { |
10913 | 0 | return OPJ_FALSE; |
10914 | 0 | } |
10915 | 0 | if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
10916 | 0 | l_num_bands = 1U; |
10917 | 0 | } else { |
10918 | 0 | l_num_bands = l_tccp0->numresolutions * 3U - 2U; |
10919 | 0 | if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) { |
10920 | 0 | return OPJ_FALSE; |
10921 | 0 | } |
10922 | 0 | } |
10923 | | |
10924 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
10925 | 0 | if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) { |
10926 | 0 | return OPJ_FALSE; |
10927 | 0 | } |
10928 | 0 | } |
10929 | 0 | if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) { |
10930 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
10931 | 0 | if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) { |
10932 | 0 | return OPJ_FALSE; |
10933 | 0 | } |
10934 | 0 | } |
10935 | 0 | } |
10936 | 0 | return OPJ_TRUE; |
10937 | 0 | } |
10938 | | |
10939 | | |
10940 | | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
10941 | | OPJ_UINT32 p_tile_no, |
10942 | | OPJ_UINT32 p_comp_no, |
10943 | | OPJ_BYTE * p_data, |
10944 | | OPJ_UINT32 * p_header_size, |
10945 | | struct opj_event_mgr * p_manager) |
10946 | 0 | { |
10947 | 0 | OPJ_UINT32 l_header_size; |
10948 | 0 | OPJ_UINT32 l_band_no, l_num_bands; |
10949 | 0 | OPJ_UINT32 l_expn, l_mant; |
10950 | |
|
10951 | 0 | opj_cp_t *l_cp = 00; |
10952 | 0 | opj_tcp_t *l_tcp = 00; |
10953 | 0 | opj_tccp_t *l_tccp = 00; |
10954 | | |
10955 | | /* preconditions */ |
10956 | 0 | assert(p_j2k != 00); |
10957 | 0 | assert(p_header_size != 00); |
10958 | 0 | assert(p_manager != 00); |
10959 | 0 | assert(p_data != 00); |
10960 | |
|
10961 | 0 | l_cp = &(p_j2k->m_cp); |
10962 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10963 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10964 | | |
10965 | | /* preconditions again */ |
10966 | 0 | assert(p_tile_no < l_cp->tw * l_cp->th); |
10967 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
10968 | |
|
10969 | 0 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
10970 | 0 | (l_tccp->numresolutions * 3 - 2); |
10971 | |
|
10972 | 0 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
10973 | 0 | l_header_size = 1 + l_num_bands; |
10974 | |
|
10975 | 0 | if (*p_header_size < l_header_size) { |
10976 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
10977 | 0 | return OPJ_FALSE; |
10978 | 0 | } |
10979 | | |
10980 | 0 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
10981 | 0 | 1); /* Sqcx */ |
10982 | 0 | ++p_data; |
10983 | |
|
10984 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
10985 | 0 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
10986 | 0 | opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ |
10987 | 0 | ++p_data; |
10988 | 0 | } |
10989 | 0 | } else { |
10990 | 0 | l_header_size = 1 + 2 * l_num_bands; |
10991 | |
|
10992 | 0 | if (*p_header_size < l_header_size) { |
10993 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
10994 | 0 | return OPJ_FALSE; |
10995 | 0 | } |
10996 | | |
10997 | 0 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
10998 | 0 | 1); /* Sqcx */ |
10999 | 0 | ++p_data; |
11000 | |
|
11001 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11002 | 0 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
11003 | 0 | l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant; |
11004 | |
|
11005 | 0 | opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ |
11006 | 0 | p_data += 2; |
11007 | 0 | } |
11008 | 0 | } |
11009 | | |
11010 | 0 | *p_header_size = *p_header_size - l_header_size; |
11011 | |
|
11012 | 0 | return OPJ_TRUE; |
11013 | 0 | } |
11014 | | |
11015 | | static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
11016 | | OPJ_UINT32 p_comp_no, |
11017 | | OPJ_BYTE* p_header_data, |
11018 | | OPJ_UINT32 * p_header_size, |
11019 | | opj_event_mgr_t * p_manager |
11020 | | ) |
11021 | 25.4k | { |
11022 | | /* loop*/ |
11023 | 25.4k | OPJ_UINT32 l_band_no; |
11024 | 25.4k | opj_cp_t *l_cp = 00; |
11025 | 25.4k | opj_tcp_t *l_tcp = 00; |
11026 | 25.4k | opj_tccp_t *l_tccp = 00; |
11027 | 25.4k | OPJ_BYTE * l_current_ptr = 00; |
11028 | 25.4k | OPJ_UINT32 l_tmp, l_num_band; |
11029 | | |
11030 | | /* preconditions*/ |
11031 | 25.4k | assert(p_j2k != 00); |
11032 | 25.4k | assert(p_manager != 00); |
11033 | 25.4k | assert(p_header_data != 00); |
11034 | | |
11035 | 25.4k | l_cp = &(p_j2k->m_cp); |
11036 | | /* come from tile part header or main header ?*/ |
11037 | 25.4k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
11038 | 25.4k | ? |
11039 | 986 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11040 | 25.4k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11041 | | |
11042 | | /* precondition again*/ |
11043 | 25.4k | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
11044 | | |
11045 | 25.4k | l_tccp = &l_tcp->tccps[p_comp_no]; |
11046 | 25.4k | l_current_ptr = p_header_data; |
11047 | | |
11048 | 25.4k | if (*p_header_size < 1) { |
11049 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); |
11050 | 4 | return OPJ_FALSE; |
11051 | 4 | } |
11052 | 25.4k | *p_header_size -= 1; |
11053 | | |
11054 | 25.4k | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* Sqcx */ |
11055 | 25.4k | ++l_current_ptr; |
11056 | | |
11057 | 25.4k | l_tccp->qntsty = l_tmp & 0x1f; |
11058 | 25.4k | l_tccp->numgbits = l_tmp >> 5; |
11059 | 25.4k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11060 | 2.24k | l_num_band = 1; |
11061 | 23.1k | } else { |
11062 | 23.1k | l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? |
11063 | 12.7k | (*p_header_size) : |
11064 | 23.1k | (*p_header_size) / 2; |
11065 | | |
11066 | 23.1k | if (l_num_band > OPJ_J2K_MAXBANDS) { |
11067 | 737 | opj_event_msg(p_manager, EVT_WARNING, |
11068 | 737 | "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " |
11069 | 737 | "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to " |
11070 | 737 | "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, |
11071 | 737 | OPJ_J2K_MAXBANDS); |
11072 | | /*return OPJ_FALSE;*/ |
11073 | 737 | } |
11074 | 23.1k | } |
11075 | | |
11076 | | #ifdef USE_JPWL |
11077 | | if (l_cp->correct) { |
11078 | | |
11079 | | /* if JPWL is on, we check whether there are too many subbands */ |
11080 | | if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { |
11081 | | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
11082 | | "JPWL: bad number of subbands in Sqcx (%d)\n", |
11083 | | l_num_band); |
11084 | | if (!JPWL_ASSUME) { |
11085 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
11086 | | return OPJ_FALSE; |
11087 | | } |
11088 | | /* we try to correct */ |
11089 | | l_num_band = 1; |
11090 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" |
11091 | | "- setting number of bands to %d => HYPOTHESIS!!!\n", |
11092 | | l_num_band); |
11093 | | }; |
11094 | | |
11095 | | }; |
11096 | | #endif /* USE_JPWL */ |
11097 | | |
11098 | 25.4k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
11099 | 114k | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
11100 | 101k | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPqcx_i */ |
11101 | 101k | ++l_current_ptr; |
11102 | 101k | if (l_band_no < OPJ_J2K_MAXBANDS) { |
11103 | 75.5k | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3); |
11104 | 75.5k | l_tccp->stepsizes[l_band_no].mant = 0; |
11105 | 75.5k | } |
11106 | 101k | } |
11107 | | |
11108 | 12.7k | if (*p_header_size < l_num_band) { |
11109 | 0 | return OPJ_FALSE; |
11110 | 0 | } |
11111 | 12.7k | *p_header_size = *p_header_size - l_num_band; |
11112 | 12.7k | } else { |
11113 | 739k | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
11114 | 726k | opj_read_bytes(l_current_ptr, &l_tmp, 2); /* SPqcx_i */ |
11115 | 726k | l_current_ptr += 2; |
11116 | 726k | if (l_band_no < OPJ_J2K_MAXBANDS) { |
11117 | 120k | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11); |
11118 | 120k | l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; |
11119 | 120k | } |
11120 | 726k | } |
11121 | | |
11122 | 12.6k | if (*p_header_size < 2 * l_num_band) { |
11123 | 3 | return OPJ_FALSE; |
11124 | 3 | } |
11125 | 12.6k | *p_header_size = *p_header_size - 2 * l_num_band; |
11126 | 12.6k | } |
11127 | | |
11128 | | /* Add Antonin : if scalar_derived -> compute other stepsizes */ |
11129 | 25.4k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11130 | 217k | for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) { |
11131 | 215k | l_tccp->stepsizes[l_band_no].expn = |
11132 | 215k | ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) |
11133 | 215k | ? |
11134 | 112k | (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0; |
11135 | 215k | l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; |
11136 | 215k | } |
11137 | 2.24k | } |
11138 | | |
11139 | 25.4k | return OPJ_TRUE; |
11140 | 25.4k | } |
11141 | | |
11142 | | static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k) |
11143 | 22.8k | { |
11144 | 22.8k | OPJ_UINT32 i; |
11145 | 22.8k | opj_cp_t *l_cp = NULL; |
11146 | 22.8k | opj_tcp_t *l_tcp = NULL; |
11147 | 22.8k | opj_tccp_t *l_ref_tccp = NULL; |
11148 | 22.8k | opj_tccp_t *l_copied_tccp = NULL; |
11149 | 22.8k | OPJ_UINT32 l_size; |
11150 | | |
11151 | | /* preconditions */ |
11152 | 22.8k | assert(p_j2k != 00); |
11153 | | |
11154 | 22.8k | l_cp = &(p_j2k->m_cp); |
11155 | 22.8k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
11156 | 517 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11157 | 22.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11158 | | |
11159 | 22.8k | l_ref_tccp = &l_tcp->tccps[0]; |
11160 | 22.8k | l_copied_tccp = l_ref_tccp + 1; |
11161 | 22.8k | l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t); |
11162 | | |
11163 | 119k | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
11164 | 96.8k | l_copied_tccp->qntsty = l_ref_tccp->qntsty; |
11165 | 96.8k | l_copied_tccp->numgbits = l_ref_tccp->numgbits; |
11166 | 96.8k | memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size); |
11167 | 96.8k | ++l_copied_tccp; |
11168 | 96.8k | } |
11169 | 22.8k | } |
11170 | | |
11171 | | static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile, |
11172 | | OPJ_INT32 numcomps, FILE* out_stream) |
11173 | 0 | { |
11174 | 0 | if (l_default_tile) { |
11175 | 0 | OPJ_INT32 compno; |
11176 | |
|
11177 | 0 | fprintf(out_stream, "\t default tile {\n"); |
11178 | 0 | fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty); |
11179 | 0 | fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg); |
11180 | 0 | fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers); |
11181 | 0 | fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct); |
11182 | |
|
11183 | 0 | for (compno = 0; compno < numcomps; compno++) { |
11184 | 0 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
11185 | 0 | OPJ_UINT32 resno; |
11186 | 0 | OPJ_INT32 bandno, numbands; |
11187 | | |
11188 | | /* coding style*/ |
11189 | 0 | fprintf(out_stream, "\t\t comp %d {\n", compno); |
11190 | 0 | fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty); |
11191 | 0 | fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions); |
11192 | 0 | fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw); |
11193 | 0 | fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh); |
11194 | 0 | fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty); |
11195 | 0 | fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid); |
11196 | |
|
11197 | 0 | fprintf(out_stream, "\t\t\t preccintsize (w,h)="); |
11198 | 0 | for (resno = 0; resno < l_tccp->numresolutions; resno++) { |
11199 | 0 | fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]); |
11200 | 0 | } |
11201 | 0 | fprintf(out_stream, "\n"); |
11202 | | |
11203 | | /* quantization style*/ |
11204 | 0 | fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty); |
11205 | 0 | fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits); |
11206 | 0 | fprintf(out_stream, "\t\t\t stepsizes (m,e)="); |
11207 | 0 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11208 | 0 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
11209 | 0 | for (bandno = 0; bandno < numbands; bandno++) { |
11210 | 0 | fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant, |
11211 | 0 | l_tccp->stepsizes[bandno].expn); |
11212 | 0 | } |
11213 | 0 | fprintf(out_stream, "\n"); |
11214 | | |
11215 | | /* RGN value*/ |
11216 | 0 | fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift); |
11217 | |
|
11218 | 0 | fprintf(out_stream, "\t\t }\n"); |
11219 | 0 | } /*end of component of default tile*/ |
11220 | 0 | fprintf(out_stream, "\t }\n"); /*end of default tile*/ |
11221 | 0 | } |
11222 | 0 | } |
11223 | | |
11224 | | void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream) |
11225 | 0 | { |
11226 | | /* Check if the flag is compatible with j2k file*/ |
11227 | 0 | if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) { |
11228 | 0 | fprintf(out_stream, "Wrong flag\n"); |
11229 | 0 | return; |
11230 | 0 | } |
11231 | | |
11232 | | /* Dump the image_header */ |
11233 | 0 | if (flag & OPJ_IMG_INFO) { |
11234 | 0 | if (p_j2k->m_private_image) { |
11235 | 0 | j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); |
11236 | 0 | } |
11237 | 0 | } |
11238 | | |
11239 | | /* Dump the codestream info from main header */ |
11240 | 0 | if (flag & OPJ_J2K_MH_INFO) { |
11241 | 0 | if (p_j2k->m_private_image) { |
11242 | 0 | opj_j2k_dump_MH_info(p_j2k, out_stream); |
11243 | 0 | } |
11244 | 0 | } |
11245 | | /* Dump all tile/codestream info */ |
11246 | 0 | if (flag & OPJ_J2K_TCH_INFO) { |
11247 | 0 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
11248 | 0 | OPJ_UINT32 i; |
11249 | 0 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
11250 | 0 | if (p_j2k->m_private_image) { |
11251 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
11252 | 0 | opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps, |
11253 | 0 | out_stream); |
11254 | 0 | ++l_tcp; |
11255 | 0 | } |
11256 | 0 | } |
11257 | 0 | } |
11258 | | |
11259 | | /* Dump the codestream info of the current tile */ |
11260 | 0 | if (flag & OPJ_J2K_TH_INFO) { |
11261 | |
|
11262 | 0 | } |
11263 | | |
11264 | | /* Dump the codestream index from main header */ |
11265 | 0 | if (flag & OPJ_J2K_MH_IND) { |
11266 | 0 | opj_j2k_dump_MH_index(p_j2k, out_stream); |
11267 | 0 | } |
11268 | | |
11269 | | /* Dump the codestream index of the current tile */ |
11270 | 0 | if (flag & OPJ_J2K_TH_IND) { |
11271 | |
|
11272 | 0 | } |
11273 | |
|
11274 | 0 | } |
11275 | | |
11276 | | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream) |
11277 | 0 | { |
11278 | 0 | opj_codestream_index_t* cstr_index = p_j2k->cstr_index; |
11279 | 0 | OPJ_UINT32 it_marker, it_tile, it_tile_part; |
11280 | |
|
11281 | 0 | fprintf(out_stream, "Codestream index from main header: {\n"); |
11282 | |
|
11283 | 0 | fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" |
11284 | 0 | "\t Main header end position=%" PRIi64 "\n", |
11285 | 0 | cstr_index->main_head_start, cstr_index->main_head_end); |
11286 | |
|
11287 | 0 | fprintf(out_stream, "\t Marker list: {\n"); |
11288 | |
|
11289 | 0 | if (cstr_index->marker) { |
11290 | 0 | for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) { |
11291 | 0 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
11292 | 0 | cstr_index->marker[it_marker].type, |
11293 | 0 | cstr_index->marker[it_marker].pos, |
11294 | 0 | cstr_index->marker[it_marker].len); |
11295 | 0 | } |
11296 | 0 | } |
11297 | |
|
11298 | 0 | fprintf(out_stream, "\t }\n"); |
11299 | |
|
11300 | 0 | if (cstr_index->tile_index) { |
11301 | | |
11302 | | /* Simple test to avoid to write empty information*/ |
11303 | 0 | OPJ_UINT32 l_acc_nb_of_tile_part = 0; |
11304 | 0 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
11305 | 0 | l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; |
11306 | 0 | } |
11307 | |
|
11308 | 0 | if (l_acc_nb_of_tile_part) { |
11309 | 0 | fprintf(out_stream, "\t Tile index: {\n"); |
11310 | |
|
11311 | 0 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
11312 | 0 | OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; |
11313 | |
|
11314 | 0 | fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, |
11315 | 0 | nb_of_tile_part); |
11316 | |
|
11317 | 0 | if (cstr_index->tile_index[it_tile].tp_index) { |
11318 | 0 | for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) { |
11319 | 0 | fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" |
11320 | 0 | PRIi64 ", end_pos=%" PRIi64 ".\n", |
11321 | 0 | it_tile_part, |
11322 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, |
11323 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, |
11324 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); |
11325 | 0 | } |
11326 | 0 | } |
11327 | |
|
11328 | 0 | if (cstr_index->tile_index[it_tile].marker) { |
11329 | 0 | for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ; |
11330 | 0 | it_marker++) { |
11331 | 0 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
11332 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].type, |
11333 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].pos, |
11334 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].len); |
11335 | 0 | } |
11336 | 0 | } |
11337 | 0 | } |
11338 | 0 | fprintf(out_stream, "\t }\n"); |
11339 | 0 | } |
11340 | 0 | } |
11341 | |
|
11342 | 0 | fprintf(out_stream, "}\n"); |
11343 | |
|
11344 | 0 | } |
11345 | | |
11346 | | |
11347 | | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream) |
11348 | 0 | { |
11349 | |
|
11350 | 0 | fprintf(out_stream, "Codestream info from main header: {\n"); |
11351 | |
|
11352 | 0 | fprintf(out_stream, "\t tx0=%" PRIu32 ", ty0=%" PRIu32 "\n", p_j2k->m_cp.tx0, |
11353 | 0 | p_j2k->m_cp.ty0); |
11354 | 0 | fprintf(out_stream, "\t tdx=%" PRIu32 ", tdy=%" PRIu32 "\n", p_j2k->m_cp.tdx, |
11355 | 0 | p_j2k->m_cp.tdy); |
11356 | 0 | fprintf(out_stream, "\t tw=%" PRIu32 ", th=%" PRIu32 "\n", p_j2k->m_cp.tw, |
11357 | 0 | p_j2k->m_cp.th); |
11358 | 0 | opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp, |
11359 | 0 | (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream); |
11360 | 0 | fprintf(out_stream, "}\n"); |
11361 | 0 | } |
11362 | | |
11363 | | void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, |
11364 | | FILE* out_stream) |
11365 | 0 | { |
11366 | 0 | char tab[2]; |
11367 | |
|
11368 | 0 | if (dev_dump_flag) { |
11369 | 0 | fprintf(stdout, "[DEV] Dump an image_header struct {\n"); |
11370 | 0 | tab[0] = '\0'; |
11371 | 0 | } else { |
11372 | 0 | fprintf(out_stream, "Image info {\n"); |
11373 | 0 | tab[0] = '\t'; |
11374 | 0 | tab[1] = '\0'; |
11375 | 0 | } |
11376 | |
|
11377 | 0 | fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0); |
11378 | 0 | fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, |
11379 | 0 | img_header->y1); |
11380 | 0 | fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps); |
11381 | |
|
11382 | 0 | if (img_header->comps) { |
11383 | 0 | OPJ_UINT32 compno; |
11384 | 0 | for (compno = 0; compno < img_header->numcomps; compno++) { |
11385 | 0 | fprintf(out_stream, "%s\t component %d {\n", tab, compno); |
11386 | 0 | j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, |
11387 | 0 | out_stream); |
11388 | 0 | fprintf(out_stream, "%s}\n", tab); |
11389 | 0 | } |
11390 | 0 | } |
11391 | |
|
11392 | 0 | fprintf(out_stream, "}\n"); |
11393 | 0 | } |
11394 | | |
11395 | | void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, |
11396 | | OPJ_BOOL dev_dump_flag, FILE* out_stream) |
11397 | 0 | { |
11398 | 0 | char tab[3]; |
11399 | |
|
11400 | 0 | if (dev_dump_flag) { |
11401 | 0 | fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); |
11402 | 0 | tab[0] = '\0'; |
11403 | 0 | } else { |
11404 | 0 | tab[0] = '\t'; |
11405 | 0 | tab[1] = '\t'; |
11406 | 0 | tab[2] = '\0'; |
11407 | 0 | } |
11408 | |
|
11409 | 0 | fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy); |
11410 | 0 | fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec); |
11411 | 0 | fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd); |
11412 | |
|
11413 | 0 | if (dev_dump_flag) { |
11414 | 0 | fprintf(out_stream, "}\n"); |
11415 | 0 | } |
11416 | 0 | } |
11417 | | |
11418 | | opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) |
11419 | 0 | { |
11420 | 0 | OPJ_UINT32 compno; |
11421 | 0 | OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps; |
11422 | 0 | opj_tcp_t *l_default_tile; |
11423 | 0 | opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1, |
11424 | 0 | sizeof(opj_codestream_info_v2_t)); |
11425 | 0 | if (!cstr_info) { |
11426 | 0 | return NULL; |
11427 | 0 | } |
11428 | | |
11429 | 0 | cstr_info->nbcomps = p_j2k->m_private_image->numcomps; |
11430 | |
|
11431 | 0 | cstr_info->tx0 = p_j2k->m_cp.tx0; |
11432 | 0 | cstr_info->ty0 = p_j2k->m_cp.ty0; |
11433 | 0 | cstr_info->tdx = p_j2k->m_cp.tdx; |
11434 | 0 | cstr_info->tdy = p_j2k->m_cp.tdy; |
11435 | 0 | cstr_info->tw = p_j2k->m_cp.tw; |
11436 | 0 | cstr_info->th = p_j2k->m_cp.th; |
11437 | |
|
11438 | 0 | cstr_info->tile_info = NULL; /* Not fill from the main header*/ |
11439 | |
|
11440 | 0 | l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11441 | |
|
11442 | 0 | cstr_info->m_default_tile_info.csty = l_default_tile->csty; |
11443 | 0 | cstr_info->m_default_tile_info.prg = l_default_tile->prg; |
11444 | 0 | cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; |
11445 | 0 | cstr_info->m_default_tile_info.mct = l_default_tile->mct; |
11446 | |
|
11447 | 0 | cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc( |
11448 | 0 | cstr_info->nbcomps, sizeof(opj_tccp_info_t)); |
11449 | 0 | if (!cstr_info->m_default_tile_info.tccp_info) { |
11450 | 0 | opj_destroy_cstr_info(&cstr_info); |
11451 | 0 | return NULL; |
11452 | 0 | } |
11453 | | |
11454 | 0 | for (compno = 0; compno < numcomps; compno++) { |
11455 | 0 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
11456 | 0 | opj_tccp_info_t *l_tccp_info = & |
11457 | 0 | (cstr_info->m_default_tile_info.tccp_info[compno]); |
11458 | 0 | OPJ_INT32 bandno, numbands; |
11459 | | |
11460 | | /* coding style*/ |
11461 | 0 | l_tccp_info->csty = l_tccp->csty; |
11462 | 0 | l_tccp_info->numresolutions = l_tccp->numresolutions; |
11463 | 0 | l_tccp_info->cblkw = l_tccp->cblkw; |
11464 | 0 | l_tccp_info->cblkh = l_tccp->cblkh; |
11465 | 0 | l_tccp_info->cblksty = l_tccp->cblksty; |
11466 | 0 | l_tccp_info->qmfbid = l_tccp->qmfbid; |
11467 | 0 | if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) { |
11468 | 0 | memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); |
11469 | 0 | memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); |
11470 | 0 | } |
11471 | | |
11472 | | /* quantization style*/ |
11473 | 0 | l_tccp_info->qntsty = l_tccp->qntsty; |
11474 | 0 | l_tccp_info->numgbits = l_tccp->numgbits; |
11475 | |
|
11476 | 0 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11477 | 0 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
11478 | 0 | if (numbands < OPJ_J2K_MAXBANDS) { |
11479 | 0 | for (bandno = 0; bandno < numbands; bandno++) { |
11480 | 0 | l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32) |
11481 | 0 | l_tccp->stepsizes[bandno].mant; |
11482 | 0 | l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32) |
11483 | 0 | l_tccp->stepsizes[bandno].expn; |
11484 | 0 | } |
11485 | 0 | } |
11486 | | |
11487 | | /* RGN value*/ |
11488 | 0 | l_tccp_info->roishift = l_tccp->roishift; |
11489 | 0 | } |
11490 | |
|
11491 | 0 | return cstr_info; |
11492 | 0 | } |
11493 | | |
11494 | | opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) |
11495 | 0 | { |
11496 | 0 | opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) |
11497 | 0 | opj_calloc(1, sizeof(opj_codestream_index_t)); |
11498 | 0 | if (!l_cstr_index) { |
11499 | 0 | return NULL; |
11500 | 0 | } |
11501 | | |
11502 | 0 | l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; |
11503 | 0 | l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; |
11504 | 0 | l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; |
11505 | |
|
11506 | 0 | l_cstr_index->marknum = p_j2k->cstr_index->marknum; |
11507 | 0 | l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum * |
11508 | 0 | sizeof(opj_marker_info_t)); |
11509 | 0 | if (!l_cstr_index->marker) { |
11510 | 0 | opj_free(l_cstr_index); |
11511 | 0 | return NULL; |
11512 | 0 | } |
11513 | | |
11514 | 0 | if (p_j2k->cstr_index->marker) { |
11515 | 0 | memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, |
11516 | 0 | l_cstr_index->marknum * sizeof(opj_marker_info_t)); |
11517 | 0 | } else { |
11518 | 0 | opj_free(l_cstr_index->marker); |
11519 | 0 | l_cstr_index->marker = NULL; |
11520 | 0 | } |
11521 | |
|
11522 | 0 | l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; |
11523 | 0 | l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
11524 | 0 | l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
11525 | 0 | if (!l_cstr_index->tile_index) { |
11526 | 0 | opj_free(l_cstr_index->marker); |
11527 | 0 | opj_free(l_cstr_index); |
11528 | 0 | return NULL; |
11529 | 0 | } |
11530 | | |
11531 | 0 | if (!p_j2k->cstr_index->tile_index) { |
11532 | 0 | opj_free(l_cstr_index->tile_index); |
11533 | 0 | l_cstr_index->tile_index = NULL; |
11534 | 0 | } else { |
11535 | 0 | OPJ_UINT32 it_tile = 0; |
11536 | 0 | for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) { |
11537 | | |
11538 | | /* Tile Marker*/ |
11539 | 0 | l_cstr_index->tile_index[it_tile].marknum = |
11540 | 0 | p_j2k->cstr_index->tile_index[it_tile].marknum; |
11541 | |
|
11542 | 0 | l_cstr_index->tile_index[it_tile].marker = |
11543 | 0 | (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum * |
11544 | 0 | sizeof(opj_marker_info_t)); |
11545 | |
|
11546 | 0 | if (!l_cstr_index->tile_index[it_tile].marker) { |
11547 | 0 | OPJ_UINT32 it_tile_free; |
11548 | |
|
11549 | 0 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
11550 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
11551 | 0 | } |
11552 | |
|
11553 | 0 | opj_free(l_cstr_index->tile_index); |
11554 | 0 | opj_free(l_cstr_index->marker); |
11555 | 0 | opj_free(l_cstr_index); |
11556 | 0 | return NULL; |
11557 | 0 | } |
11558 | | |
11559 | 0 | if (p_j2k->cstr_index->tile_index[it_tile].marker) |
11560 | 0 | memcpy(l_cstr_index->tile_index[it_tile].marker, |
11561 | 0 | p_j2k->cstr_index->tile_index[it_tile].marker, |
11562 | 0 | l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t)); |
11563 | 0 | else { |
11564 | 0 | opj_free(l_cstr_index->tile_index[it_tile].marker); |
11565 | 0 | l_cstr_index->tile_index[it_tile].marker = NULL; |
11566 | 0 | } |
11567 | | |
11568 | | /* Tile part index*/ |
11569 | 0 | l_cstr_index->tile_index[it_tile].nb_tps = |
11570 | 0 | p_j2k->cstr_index->tile_index[it_tile].nb_tps; |
11571 | |
|
11572 | 0 | l_cstr_index->tile_index[it_tile].tp_index = |
11573 | 0 | (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof( |
11574 | 0 | opj_tp_index_t)); |
11575 | |
|
11576 | 0 | if (!l_cstr_index->tile_index[it_tile].tp_index) { |
11577 | 0 | OPJ_UINT32 it_tile_free; |
11578 | |
|
11579 | 0 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
11580 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
11581 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); |
11582 | 0 | } |
11583 | |
|
11584 | 0 | opj_free(l_cstr_index->tile_index); |
11585 | 0 | opj_free(l_cstr_index->marker); |
11586 | 0 | opj_free(l_cstr_index); |
11587 | 0 | return NULL; |
11588 | 0 | } |
11589 | | |
11590 | 0 | if (p_j2k->cstr_index->tile_index[it_tile].tp_index) { |
11591 | 0 | memcpy(l_cstr_index->tile_index[it_tile].tp_index, |
11592 | 0 | p_j2k->cstr_index->tile_index[it_tile].tp_index, |
11593 | 0 | l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t)); |
11594 | 0 | } else { |
11595 | 0 | opj_free(l_cstr_index->tile_index[it_tile].tp_index); |
11596 | 0 | l_cstr_index->tile_index[it_tile].tp_index = NULL; |
11597 | 0 | } |
11598 | | |
11599 | | /* Packet index (NOT USED)*/ |
11600 | 0 | l_cstr_index->tile_index[it_tile].nb_packet = 0; |
11601 | 0 | l_cstr_index->tile_index[it_tile].packet_index = NULL; |
11602 | |
|
11603 | 0 | } |
11604 | 0 | } |
11605 | | |
11606 | 0 | return l_cstr_index; |
11607 | 0 | } |
11608 | | |
11609 | | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) |
11610 | 14.1k | { |
11611 | 14.1k | OPJ_UINT32 it_tile = 0; |
11612 | | |
11613 | 14.1k | p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
11614 | 14.1k | p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
11615 | 14.1k | p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
11616 | 14.1k | if (!p_j2k->cstr_index->tile_index) { |
11617 | 0 | return OPJ_FALSE; |
11618 | 0 | } |
11619 | | |
11620 | 18.6M | for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) { |
11621 | 18.6M | p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; |
11622 | 18.6M | p_j2k->cstr_index->tile_index[it_tile].marknum = 0; |
11623 | 18.6M | p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) |
11624 | 18.6M | opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, |
11625 | 18.6M | sizeof(opj_marker_info_t)); |
11626 | 18.6M | if (!p_j2k->cstr_index->tile_index[it_tile].marker) { |
11627 | 0 | return OPJ_FALSE; |
11628 | 0 | } |
11629 | 18.6M | } |
11630 | | |
11631 | 14.1k | return OPJ_TRUE; |
11632 | 14.1k | } |
11633 | | |
11634 | | static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k, |
11635 | | opj_event_mgr_t * p_manager) |
11636 | 10.2k | { |
11637 | 10.2k | OPJ_UINT32 compno; |
11638 | 10.2k | OPJ_BOOL decoded_all_used_components = OPJ_TRUE; |
11639 | | |
11640 | 10.2k | if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) { |
11641 | 0 | for (compno = 0; |
11642 | 0 | compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { |
11643 | 0 | OPJ_UINT32 dec_compno = |
11644 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; |
11645 | 0 | if (p_j2k->m_output_image->comps[dec_compno].data == NULL) { |
11646 | 0 | opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", |
11647 | 0 | dec_compno); |
11648 | 0 | decoded_all_used_components = OPJ_FALSE; |
11649 | 0 | } |
11650 | 0 | } |
11651 | 10.2k | } else { |
11652 | 41.6k | for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) { |
11653 | 31.3k | if (p_j2k->m_output_image->comps[compno].data == NULL) { |
11654 | 4.08k | opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", |
11655 | 4.08k | compno); |
11656 | 4.08k | decoded_all_used_components = OPJ_FALSE; |
11657 | 4.08k | } |
11658 | 31.3k | } |
11659 | 10.2k | } |
11660 | | |
11661 | 10.2k | if (decoded_all_used_components == OPJ_FALSE) { |
11662 | 327 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n"); |
11663 | 327 | return OPJ_FALSE; |
11664 | 327 | } |
11665 | | |
11666 | 9.93k | return OPJ_TRUE; |
11667 | 10.2k | } |
11668 | | |
11669 | | |
11670 | | static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
11671 | | opj_stream_private_t *p_stream, |
11672 | | opj_event_mgr_t * p_manager) |
11673 | 14.0k | { |
11674 | 14.0k | OPJ_BOOL l_go_on = OPJ_TRUE; |
11675 | 14.0k | OPJ_UINT32 l_current_tile_no; |
11676 | 14.0k | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
11677 | 14.0k | OPJ_UINT32 l_nb_comps; |
11678 | 14.0k | OPJ_UINT32 nr_tiles = 0; |
11679 | | |
11680 | | /* Particular case for whole single tile decoding */ |
11681 | | /* We can avoid allocating intermediate tile buffers */ |
11682 | 14.0k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
11683 | 14.0k | p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 && |
11684 | 14.0k | p_j2k->m_output_image->x0 == 0 && |
11685 | 14.0k | p_j2k->m_output_image->y0 == 0 && |
11686 | 14.0k | p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx && |
11687 | 14.0k | p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) { |
11688 | 5 | OPJ_UINT32 i; |
11689 | 5 | if (! opj_j2k_read_tile_header(p_j2k, |
11690 | 5 | &l_current_tile_no, |
11691 | 5 | NULL, |
11692 | 5 | &l_tile_x0, &l_tile_y0, |
11693 | 5 | &l_tile_x1, &l_tile_y1, |
11694 | 5 | &l_nb_comps, |
11695 | 5 | &l_go_on, |
11696 | 5 | p_stream, |
11697 | 5 | p_manager)) { |
11698 | 1 | return OPJ_FALSE; |
11699 | 1 | } |
11700 | | |
11701 | 4 | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
11702 | 4 | p_stream, p_manager)) { |
11703 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n"); |
11704 | 2 | return OPJ_FALSE; |
11705 | 2 | } |
11706 | | |
11707 | | /* Transfer TCD data to output image data */ |
11708 | 7 | for (i = 0; i < p_j2k->m_output_image->numcomps; i++) { |
11709 | 5 | opj_image_data_free(p_j2k->m_output_image->comps[i].data); |
11710 | 5 | p_j2k->m_output_image->comps[i].data = |
11711 | 5 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data; |
11712 | 5 | p_j2k->m_output_image->comps[i].resno_decoded = |
11713 | 5 | p_j2k->m_tcd->image->comps[i].resno_decoded; |
11714 | 5 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL; |
11715 | 5 | } |
11716 | | |
11717 | 2 | return OPJ_TRUE; |
11718 | 4 | } |
11719 | | |
11720 | 16.2k | for (;;) { |
11721 | 16.2k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
11722 | 16.2k | p_j2k->m_cp.tcps[0].m_data != NULL) { |
11723 | 0 | l_current_tile_no = 0; |
11724 | 0 | p_j2k->m_current_tile_number = 0; |
11725 | 0 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
11726 | 16.2k | } else { |
11727 | 16.2k | if (! opj_j2k_read_tile_header(p_j2k, |
11728 | 16.2k | &l_current_tile_no, |
11729 | 16.2k | NULL, |
11730 | 16.2k | &l_tile_x0, &l_tile_y0, |
11731 | 16.2k | &l_tile_x1, &l_tile_y1, |
11732 | 16.2k | &l_nb_comps, |
11733 | 16.2k | &l_go_on, |
11734 | 16.2k | p_stream, |
11735 | 16.2k | p_manager)) { |
11736 | 1.07k | return OPJ_FALSE; |
11737 | 1.07k | } |
11738 | | |
11739 | 15.1k | if (! l_go_on) { |
11740 | 109 | break; |
11741 | 109 | } |
11742 | 15.1k | } |
11743 | | |
11744 | 15.0k | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
11745 | 15.0k | p_stream, p_manager)) { |
11746 | 2.67k | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", |
11747 | 2.67k | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
11748 | 2.67k | return OPJ_FALSE; |
11749 | 2.67k | } |
11750 | | |
11751 | 12.3k | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", |
11752 | 12.3k | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
11753 | | |
11754 | 12.3k | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
11755 | 12.3k | p_j2k->m_output_image)) { |
11756 | 57 | return OPJ_FALSE; |
11757 | 57 | } |
11758 | | |
11759 | 12.3k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
11760 | 12.3k | !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 && |
11761 | 6.09k | p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 && |
11762 | 6.09k | p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 && |
11763 | 6.09k | p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) { |
11764 | | /* Keep current tcp data */ |
11765 | 8.03k | } else { |
11766 | 8.03k | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
11767 | 8.03k | } |
11768 | | |
11769 | 12.3k | opj_event_msg(p_manager, EVT_INFO, |
11770 | 12.3k | "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
11771 | | |
11772 | 12.3k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
11773 | 12.3k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
11774 | 10.1k | break; |
11775 | 10.1k | } |
11776 | 2.17k | if (++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) { |
11777 | 15 | break; |
11778 | 15 | } |
11779 | 2.17k | } |
11780 | | |
11781 | 10.2k | if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { |
11782 | 327 | return OPJ_FALSE; |
11783 | 327 | } |
11784 | | |
11785 | 9.93k | return OPJ_TRUE; |
11786 | 10.2k | } |
11787 | | |
11788 | | /** |
11789 | | * Sets up the procedures to do on decoding data. Developers wanting to extend the library can add their own reading procedures. |
11790 | | */ |
11791 | | static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k, |
11792 | | opj_event_mgr_t * p_manager) |
11793 | 14.0k | { |
11794 | | /* preconditions*/ |
11795 | 14.0k | assert(p_j2k != 00); |
11796 | 14.0k | assert(p_manager != 00); |
11797 | | |
11798 | 14.0k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11799 | 14.0k | (opj_procedure)opj_j2k_decode_tiles, p_manager)) { |
11800 | 0 | return OPJ_FALSE; |
11801 | 0 | } |
11802 | | /* DEVELOPER CORNER, add your custom procedures */ |
11803 | | |
11804 | 14.0k | return OPJ_TRUE; |
11805 | 14.0k | } |
11806 | | |
11807 | | /* |
11808 | | * Read and decode one tile. |
11809 | | */ |
11810 | | static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k, |
11811 | | opj_stream_private_t *p_stream, |
11812 | | opj_event_mgr_t * p_manager) |
11813 | 0 | { |
11814 | 0 | OPJ_BOOL l_go_on = OPJ_TRUE; |
11815 | 0 | OPJ_UINT32 l_current_tile_no; |
11816 | 0 | OPJ_UINT32 l_tile_no_to_dec; |
11817 | 0 | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
11818 | 0 | OPJ_UINT32 l_nb_comps; |
11819 | 0 | OPJ_UINT32 l_nb_tiles; |
11820 | 0 | OPJ_UINT32 i; |
11821 | | |
11822 | | /*Allocate and initialize some elements of codestrem index if not already done*/ |
11823 | 0 | if (!p_j2k->cstr_index->tile_index) { |
11824 | 0 | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { |
11825 | 0 | return OPJ_FALSE; |
11826 | 0 | } |
11827 | 0 | } |
11828 | | /* Move into the codestream to the first SOT used to decode the desired tile */ |
11829 | 0 | l_tile_no_to_dec = (OPJ_UINT32) |
11830 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; |
11831 | 0 | if (p_j2k->cstr_index->tile_index) |
11832 | 0 | if (p_j2k->cstr_index->tile_index->tp_index) { |
11833 | 0 | if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { |
11834 | | /* the index for this tile has not been built, |
11835 | | * so move to the last SOT read */ |
11836 | 0 | if (!(opj_stream_read_seek(p_stream, |
11837 | 0 | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) { |
11838 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
11839 | 0 | return OPJ_FALSE; |
11840 | 0 | } |
11841 | 0 | } else { |
11842 | 0 | if (!(opj_stream_read_seek(p_stream, |
11843 | 0 | p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos + 2, |
11844 | 0 | p_manager))) { |
11845 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
11846 | 0 | return OPJ_FALSE; |
11847 | 0 | } |
11848 | 0 | } |
11849 | | /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ |
11850 | 0 | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
11851 | 0 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
11852 | 0 | } |
11853 | 0 | } |
11854 | | |
11855 | | /* Reset current tile part number for all tiles, and not only the one */ |
11856 | | /* of interest. */ |
11857 | | /* Not completely sure this is always correct but required for */ |
11858 | | /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */ |
11859 | 0 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
11860 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
11861 | 0 | p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1; |
11862 | 0 | } |
11863 | |
|
11864 | 0 | for (;;) { |
11865 | 0 | if (! opj_j2k_read_tile_header(p_j2k, |
11866 | 0 | &l_current_tile_no, |
11867 | 0 | NULL, |
11868 | 0 | &l_tile_x0, &l_tile_y0, |
11869 | 0 | &l_tile_x1, &l_tile_y1, |
11870 | 0 | &l_nb_comps, |
11871 | 0 | &l_go_on, |
11872 | 0 | p_stream, |
11873 | 0 | p_manager)) { |
11874 | 0 | return OPJ_FALSE; |
11875 | 0 | } |
11876 | | |
11877 | 0 | if (! l_go_on) { |
11878 | 0 | break; |
11879 | 0 | } |
11880 | | |
11881 | 0 | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
11882 | 0 | p_stream, p_manager)) { |
11883 | 0 | return OPJ_FALSE; |
11884 | 0 | } |
11885 | 0 | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", |
11886 | 0 | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
11887 | |
|
11888 | 0 | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
11889 | 0 | p_j2k->m_output_image)) { |
11890 | 0 | return OPJ_FALSE; |
11891 | 0 | } |
11892 | 0 | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
11893 | |
|
11894 | 0 | opj_event_msg(p_manager, EVT_INFO, |
11895 | 0 | "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
11896 | |
|
11897 | 0 | if (l_current_tile_no == l_tile_no_to_dec) { |
11898 | | /* move into the codestream to the first SOT (FIXME or not move?)*/ |
11899 | 0 | if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, |
11900 | 0 | p_manager))) { |
11901 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
11902 | 0 | return OPJ_FALSE; |
11903 | 0 | } |
11904 | 0 | break; |
11905 | 0 | } else { |
11906 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
11907 | 0 | "Tile read, decoded and updated is not the desired one (%d vs %d).\n", |
11908 | 0 | l_current_tile_no + 1, l_tile_no_to_dec + 1); |
11909 | 0 | } |
11910 | |
|
11911 | 0 | } |
11912 | | |
11913 | 0 | if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { |
11914 | 0 | return OPJ_FALSE; |
11915 | 0 | } |
11916 | | |
11917 | 0 | return OPJ_TRUE; |
11918 | 0 | } |
11919 | | |
11920 | | /** |
11921 | | * Sets up the procedures to do on decoding one tile. Developers wanting to extend the library can add their own reading procedures. |
11922 | | */ |
11923 | | static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k, |
11924 | | opj_event_mgr_t * p_manager) |
11925 | 0 | { |
11926 | | /* preconditions*/ |
11927 | 0 | assert(p_j2k != 00); |
11928 | 0 | assert(p_manager != 00); |
11929 | |
|
11930 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11931 | 0 | (opj_procedure)opj_j2k_decode_one_tile, p_manager)) { |
11932 | 0 | return OPJ_FALSE; |
11933 | 0 | } |
11934 | | /* DEVELOPER CORNER, add your custom procedures */ |
11935 | | |
11936 | 0 | return OPJ_TRUE; |
11937 | 0 | } |
11938 | | |
11939 | | static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k, |
11940 | | opj_image_t * p_image) |
11941 | 9.93k | { |
11942 | 9.93k | OPJ_UINT32 compno; |
11943 | | |
11944 | | /* Move data and copy one information from codec to output image*/ |
11945 | 9.93k | if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) { |
11946 | 0 | opj_image_comp_t* newcomps = |
11947 | 0 | (opj_image_comp_t*) opj_malloc( |
11948 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode * |
11949 | 0 | sizeof(opj_image_comp_t)); |
11950 | 0 | if (newcomps == NULL) { |
11951 | 0 | opj_image_destroy(p_j2k->m_private_image); |
11952 | 0 | p_j2k->m_private_image = NULL; |
11953 | 0 | return OPJ_FALSE; |
11954 | 0 | } |
11955 | 0 | for (compno = 0; compno < p_image->numcomps; compno++) { |
11956 | 0 | opj_image_data_free(p_image->comps[compno].data); |
11957 | 0 | p_image->comps[compno].data = NULL; |
11958 | 0 | } |
11959 | 0 | for (compno = 0; |
11960 | 0 | compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { |
11961 | 0 | OPJ_UINT32 src_compno = |
11962 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; |
11963 | 0 | memcpy(&(newcomps[compno]), |
11964 | 0 | &(p_j2k->m_output_image->comps[src_compno]), |
11965 | 0 | sizeof(opj_image_comp_t)); |
11966 | 0 | newcomps[compno].resno_decoded = |
11967 | 0 | p_j2k->m_output_image->comps[src_compno].resno_decoded; |
11968 | 0 | newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data; |
11969 | 0 | p_j2k->m_output_image->comps[src_compno].data = NULL; |
11970 | 0 | } |
11971 | 0 | for (compno = 0; compno < p_image->numcomps; compno++) { |
11972 | 0 | assert(p_j2k->m_output_image->comps[compno].data == NULL); |
11973 | 0 | opj_image_data_free(p_j2k->m_output_image->comps[compno].data); |
11974 | 0 | p_j2k->m_output_image->comps[compno].data = NULL; |
11975 | 0 | } |
11976 | 0 | p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; |
11977 | 0 | opj_free(p_image->comps); |
11978 | 0 | p_image->comps = newcomps; |
11979 | 9.93k | } else { |
11980 | 32.5k | for (compno = 0; compno < p_image->numcomps; compno++) { |
11981 | 22.6k | p_image->comps[compno].resno_decoded = |
11982 | 22.6k | p_j2k->m_output_image->comps[compno].resno_decoded; |
11983 | 22.6k | opj_image_data_free(p_image->comps[compno].data); |
11984 | 22.6k | p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; |
11985 | | #if 0 |
11986 | | char fn[256]; |
11987 | | snprintf(fn, sizeof fn, "/tmp/%d.raw", compno); |
11988 | | FILE *debug = fopen(fn, "wb"); |
11989 | | fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32), |
11990 | | p_image->comps[compno].w * p_image->comps[compno].h, debug); |
11991 | | fclose(debug); |
11992 | | #endif |
11993 | 22.6k | p_j2k->m_output_image->comps[compno].data = NULL; |
11994 | 22.6k | } |
11995 | 9.93k | } |
11996 | 9.93k | return OPJ_TRUE; |
11997 | 9.93k | } |
11998 | | |
11999 | | OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, |
12000 | | opj_stream_private_t * p_stream, |
12001 | | opj_image_t * p_image, |
12002 | | opj_event_mgr_t * p_manager) |
12003 | 14.0k | { |
12004 | 14.0k | if (!p_image) { |
12005 | 0 | return OPJ_FALSE; |
12006 | 0 | } |
12007 | | |
12008 | | /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */ |
12009 | | /* and finally opj_decode_image() without manual setting of comps[].factor */ |
12010 | | /* We could potentially always execute it, if we don't allow people to do */ |
12011 | | /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */ |
12012 | 14.0k | if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 && |
12013 | 14.0k | p_j2k->m_private_image != NULL && |
12014 | 14.0k | p_j2k->m_private_image->numcomps > 0 && |
12015 | 14.0k | p_j2k->m_private_image->comps[0].factor == |
12016 | 0 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce && |
12017 | 14.0k | p_image->numcomps > 0 && |
12018 | 14.0k | p_image->comps[0].factor == 0 && |
12019 | | /* Don't mess with image dimension if the user has allocated it */ |
12020 | 14.0k | p_image->comps[0].data == NULL) { |
12021 | 0 | OPJ_UINT32 it_comp; |
12022 | | |
12023 | | /* Update the comps[].factor member of the output image with the one */ |
12024 | | /* of m_reduce */ |
12025 | 0 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
12026 | 0 | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
12027 | 0 | } |
12028 | 0 | if (!opj_j2k_update_image_dimensions(p_image, p_manager)) { |
12029 | 0 | return OPJ_FALSE; |
12030 | 0 | } |
12031 | 0 | } |
12032 | | |
12033 | 14.0k | if (p_j2k->m_output_image == NULL) { |
12034 | 14.0k | p_j2k->m_output_image = opj_image_create0(); |
12035 | 14.0k | if (!(p_j2k->m_output_image)) { |
12036 | 0 | return OPJ_FALSE; |
12037 | 0 | } |
12038 | 14.0k | } |
12039 | 14.0k | opj_copy_image_header(p_image, p_j2k->m_output_image); |
12040 | | |
12041 | | /* customization of the decoding */ |
12042 | 14.0k | if (!opj_j2k_setup_decoding(p_j2k, p_manager)) { |
12043 | 0 | return OPJ_FALSE; |
12044 | 0 | } |
12045 | | |
12046 | | /* Decode the codestream */ |
12047 | 14.0k | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12048 | 4.13k | opj_image_destroy(p_j2k->m_private_image); |
12049 | 4.13k | p_j2k->m_private_image = NULL; |
12050 | 4.13k | return OPJ_FALSE; |
12051 | 4.13k | } |
12052 | | |
12053 | | /* Move data and copy one information from codec to output image*/ |
12054 | 9.93k | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
12055 | 14.0k | } |
12056 | | |
12057 | | OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k, |
12058 | | opj_stream_private_t *p_stream, |
12059 | | opj_image_t* p_image, |
12060 | | opj_event_mgr_t * p_manager, |
12061 | | OPJ_UINT32 tile_index) |
12062 | 0 | { |
12063 | 0 | OPJ_UINT32 compno; |
12064 | 0 | OPJ_UINT32 l_tile_x, l_tile_y; |
12065 | 0 | opj_image_comp_t* l_img_comp; |
12066 | |
|
12067 | 0 | if (!p_image) { |
12068 | 0 | opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n"); |
12069 | 0 | return OPJ_FALSE; |
12070 | 0 | } |
12071 | | |
12072 | 0 | if (p_image->numcomps < p_j2k->m_private_image->numcomps) { |
12073 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12074 | 0 | "Image has less components than codestream.\n"); |
12075 | 0 | return OPJ_FALSE; |
12076 | 0 | } |
12077 | | |
12078 | 0 | if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) { |
12079 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12080 | 0 | "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, |
12081 | 0 | (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); |
12082 | 0 | return OPJ_FALSE; |
12083 | 0 | } |
12084 | | |
12085 | | /* Compute the dimension of the desired tile*/ |
12086 | 0 | l_tile_x = tile_index % p_j2k->m_cp.tw; |
12087 | 0 | l_tile_y = tile_index / p_j2k->m_cp.tw; |
12088 | |
|
12089 | 0 | p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
12090 | 0 | if (p_image->x0 < p_j2k->m_private_image->x0) { |
12091 | 0 | p_image->x0 = p_j2k->m_private_image->x0; |
12092 | 0 | } |
12093 | 0 | p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
12094 | 0 | if (p_image->x1 > p_j2k->m_private_image->x1) { |
12095 | 0 | p_image->x1 = p_j2k->m_private_image->x1; |
12096 | 0 | } |
12097 | |
|
12098 | 0 | p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
12099 | 0 | if (p_image->y0 < p_j2k->m_private_image->y0) { |
12100 | 0 | p_image->y0 = p_j2k->m_private_image->y0; |
12101 | 0 | } |
12102 | 0 | p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
12103 | 0 | if (p_image->y1 > p_j2k->m_private_image->y1) { |
12104 | 0 | p_image->y1 = p_j2k->m_private_image->y1; |
12105 | 0 | } |
12106 | |
|
12107 | 0 | l_img_comp = p_image->comps; |
12108 | 0 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
12109 | 0 | OPJ_INT32 l_comp_x1, l_comp_y1; |
12110 | |
|
12111 | 0 | l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; |
12112 | |
|
12113 | 0 | l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx); |
12114 | 0 | l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy); |
12115 | 0 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
12116 | 0 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
12117 | |
|
12118 | 0 | l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, |
12119 | 0 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, |
12120 | 0 | (OPJ_INT32)l_img_comp->factor)); |
12121 | 0 | l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, |
12122 | 0 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, |
12123 | 0 | (OPJ_INT32)l_img_comp->factor)); |
12124 | |
|
12125 | 0 | l_img_comp++; |
12126 | 0 | } |
12127 | |
|
12128 | 0 | if (p_image->numcomps > p_j2k->m_private_image->numcomps) { |
12129 | | /* Can happen when calling repeatdly opj_get_decoded_tile() on an |
12130 | | * image with a color palette, where color palette expansion is done |
12131 | | * later in jp2.c */ |
12132 | 0 | for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps; |
12133 | 0 | ++compno) { |
12134 | 0 | opj_image_data_free(p_image->comps[compno].data); |
12135 | 0 | p_image->comps[compno].data = NULL; |
12136 | 0 | } |
12137 | 0 | p_image->numcomps = p_j2k->m_private_image->numcomps; |
12138 | 0 | } |
12139 | | |
12140 | | /* Destroy the previous output image*/ |
12141 | 0 | if (p_j2k->m_output_image) { |
12142 | 0 | opj_image_destroy(p_j2k->m_output_image); |
12143 | 0 | } |
12144 | | |
12145 | | /* Create the output image from the information previously computed*/ |
12146 | 0 | p_j2k->m_output_image = opj_image_create0(); |
12147 | 0 | if (!(p_j2k->m_output_image)) { |
12148 | 0 | return OPJ_FALSE; |
12149 | 0 | } |
12150 | 0 | opj_copy_image_header(p_image, p_j2k->m_output_image); |
12151 | |
|
12152 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index; |
12153 | | |
12154 | | /* customization of the decoding */ |
12155 | 0 | if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) { |
12156 | 0 | return OPJ_FALSE; |
12157 | 0 | } |
12158 | | |
12159 | | /* Decode the codestream */ |
12160 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12161 | 0 | opj_image_destroy(p_j2k->m_private_image); |
12162 | 0 | p_j2k->m_private_image = NULL; |
12163 | 0 | return OPJ_FALSE; |
12164 | 0 | } |
12165 | | |
12166 | | /* Move data and copy one information from codec to output image*/ |
12167 | 0 | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
12168 | 0 | } |
12169 | | |
12170 | | OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, |
12171 | | OPJ_UINT32 res_factor, |
12172 | | opj_event_mgr_t * p_manager) |
12173 | 0 | { |
12174 | 0 | OPJ_UINT32 it_comp; |
12175 | |
|
12176 | 0 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; |
12177 | |
|
12178 | 0 | if (p_j2k->m_private_image) { |
12179 | 0 | if (p_j2k->m_private_image->comps) { |
12180 | 0 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { |
12181 | 0 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { |
12182 | 0 | for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { |
12183 | 0 | OPJ_UINT32 max_res = |
12184 | 0 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; |
12185 | 0 | if (res_factor >= max_res) { |
12186 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12187 | 0 | "Resolution factor is greater than the maximum resolution in the component.\n"); |
12188 | 0 | return OPJ_FALSE; |
12189 | 0 | } |
12190 | 0 | p_j2k->m_private_image->comps[it_comp].factor = res_factor; |
12191 | 0 | } |
12192 | 0 | return OPJ_TRUE; |
12193 | 0 | } |
12194 | 0 | } |
12195 | 0 | } |
12196 | 0 | } |
12197 | | |
12198 | 0 | return OPJ_FALSE; |
12199 | 0 | } |
12200 | | |
12201 | | /* ----------------------------------------------------------------------- */ |
12202 | | |
12203 | | OPJ_BOOL opj_j2k_encoder_set_extra_options( |
12204 | | opj_j2k_t *p_j2k, |
12205 | | const char* const* p_options, |
12206 | | opj_event_mgr_t * p_manager) |
12207 | 0 | { |
12208 | 0 | const char* const* p_option_iter; |
12209 | |
|
12210 | 0 | if (p_options == NULL) { |
12211 | 0 | return OPJ_TRUE; |
12212 | 0 | } |
12213 | | |
12214 | 0 | for (p_option_iter = p_options; *p_option_iter != NULL; ++p_option_iter) { |
12215 | 0 | if (strncmp(*p_option_iter, "PLT=", 4) == 0) { |
12216 | 0 | if (strcmp(*p_option_iter, "PLT=YES") == 0) { |
12217 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_TRUE; |
12218 | 0 | } else if (strcmp(*p_option_iter, "PLT=NO") == 0) { |
12219 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_FALSE; |
12220 | 0 | } else { |
12221 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12222 | 0 | "Invalid value for option: %s.\n", *p_option_iter); |
12223 | 0 | return OPJ_FALSE; |
12224 | 0 | } |
12225 | 0 | } else if (strncmp(*p_option_iter, "TLM=", 4) == 0) { |
12226 | 0 | if (strcmp(*p_option_iter, "TLM=YES") == 0) { |
12227 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE; |
12228 | 0 | } else if (strcmp(*p_option_iter, "TLM=NO") == 0) { |
12229 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_FALSE; |
12230 | 0 | } else { |
12231 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12232 | 0 | "Invalid value for option: %s.\n", *p_option_iter); |
12233 | 0 | return OPJ_FALSE; |
12234 | 0 | } |
12235 | 0 | } else if (strncmp(*p_option_iter, "GUARD_BITS=", strlen("GUARD_BITS=")) == 0) { |
12236 | 0 | OPJ_UINT32 tileno; |
12237 | 0 | opj_cp_t *cp = cp = &(p_j2k->m_cp); |
12238 | |
|
12239 | 0 | int numgbits = atoi(*p_option_iter + strlen("GUARD_BITS=")); |
12240 | 0 | if (numgbits < 0 || numgbits > 7) { |
12241 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12242 | 0 | "Invalid value for option: %s. Should be in [0,7]\n", *p_option_iter); |
12243 | 0 | return OPJ_FALSE; |
12244 | 0 | } |
12245 | | |
12246 | 0 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
12247 | 0 | OPJ_UINT32 i; |
12248 | 0 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
12249 | 0 | for (i = 0; i < p_j2k->m_specific_param.m_encoder.m_nb_comps; i++) { |
12250 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
12251 | 0 | tccp->numgbits = (OPJ_UINT32)numgbits; |
12252 | 0 | } |
12253 | 0 | } |
12254 | 0 | } else { |
12255 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12256 | 0 | "Invalid option: %s.\n", *p_option_iter); |
12257 | 0 | return OPJ_FALSE; |
12258 | 0 | } |
12259 | 0 | } |
12260 | | |
12261 | 0 | return OPJ_TRUE; |
12262 | 0 | } |
12263 | | |
12264 | | /* ----------------------------------------------------------------------- */ |
12265 | | |
12266 | | OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, |
12267 | | opj_stream_private_t *p_stream, |
12268 | | opj_event_mgr_t * p_manager) |
12269 | 0 | { |
12270 | 0 | OPJ_UINT32 i, j; |
12271 | 0 | OPJ_UINT32 l_nb_tiles; |
12272 | 0 | OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size; |
12273 | 0 | OPJ_BYTE * l_current_data = 00; |
12274 | 0 | OPJ_BOOL l_reuse_data = OPJ_FALSE; |
12275 | 0 | opj_tcd_t* p_tcd = 00; |
12276 | | |
12277 | | /* preconditions */ |
12278 | 0 | assert(p_j2k != 00); |
12279 | 0 | assert(p_stream != 00); |
12280 | 0 | assert(p_manager != 00); |
12281 | |
|
12282 | 0 | p_tcd = p_j2k->m_tcd; |
12283 | |
|
12284 | 0 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
12285 | 0 | if (l_nb_tiles == 1) { |
12286 | 0 | l_reuse_data = OPJ_TRUE; |
12287 | 0 | #ifdef __SSE__ |
12288 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
12289 | 0 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
12290 | 0 | if (((size_t)l_img_comp->data & 0xFU) != |
12291 | 0 | 0U) { /* tile data shall be aligned on 16 bytes */ |
12292 | 0 | l_reuse_data = OPJ_FALSE; |
12293 | 0 | } |
12294 | 0 | } |
12295 | 0 | #endif |
12296 | 0 | } |
12297 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
12298 | 0 | if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) { |
12299 | 0 | if (l_current_data) { |
12300 | 0 | opj_free(l_current_data); |
12301 | 0 | } |
12302 | 0 | return OPJ_FALSE; |
12303 | 0 | } |
12304 | | |
12305 | | /* if we only have one tile, then simply set tile component data equal to image component data */ |
12306 | | /* otherwise, allocate the data */ |
12307 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
12308 | 0 | opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; |
12309 | 0 | if (l_reuse_data) { |
12310 | 0 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
12311 | 0 | l_tilec->data = l_img_comp->data; |
12312 | 0 | l_tilec->ownsData = OPJ_FALSE; |
12313 | 0 | } else { |
12314 | 0 | if (! opj_alloc_tile_component_data(l_tilec)) { |
12315 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); |
12316 | 0 | if (l_current_data) { |
12317 | 0 | opj_free(l_current_data); |
12318 | 0 | } |
12319 | 0 | return OPJ_FALSE; |
12320 | 0 | } |
12321 | 0 | } |
12322 | 0 | } |
12323 | 0 | l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd); |
12324 | 0 | if (!l_reuse_data) { |
12325 | 0 | if (l_current_tile_size > l_max_tile_size) { |
12326 | 0 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, |
12327 | 0 | l_current_tile_size); |
12328 | 0 | if (! l_new_current_data) { |
12329 | 0 | if (l_current_data) { |
12330 | 0 | opj_free(l_current_data); |
12331 | 0 | } |
12332 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); |
12333 | 0 | return OPJ_FALSE; |
12334 | 0 | } |
12335 | 0 | l_current_data = l_new_current_data; |
12336 | 0 | l_max_tile_size = l_current_tile_size; |
12337 | 0 | } |
12338 | 0 | if (l_current_data == NULL) { |
12339 | | /* Should not happen in practice, but will avoid Coverity to */ |
12340 | | /* complain about a null pointer dereference */ |
12341 | 0 | assert(0); |
12342 | 0 | return OPJ_FALSE; |
12343 | 0 | } |
12344 | | |
12345 | | /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */ |
12346 | | /* 32 bit components @ 8 bit precision get converted to 8 bit */ |
12347 | | /* 32 bit components @ 16 bit precision get converted to 16 bit */ |
12348 | 0 | opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data); |
12349 | | |
12350 | | /* now copy this data into the tile component */ |
12351 | 0 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data, |
12352 | 0 | l_current_tile_size)) { |
12353 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12354 | 0 | "Size mismatch between tile data and sent data."); |
12355 | 0 | opj_free(l_current_data); |
12356 | 0 | return OPJ_FALSE; |
12357 | 0 | } |
12358 | 0 | } |
12359 | | |
12360 | 0 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
12361 | 0 | if (l_current_data) { |
12362 | 0 | opj_free(l_current_data); |
12363 | 0 | } |
12364 | 0 | return OPJ_FALSE; |
12365 | 0 | } |
12366 | 0 | } |
12367 | | |
12368 | 0 | if (l_current_data) { |
12369 | 0 | opj_free(l_current_data); |
12370 | 0 | } |
12371 | 0 | return OPJ_TRUE; |
12372 | 0 | } |
12373 | | |
12374 | | OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, |
12375 | | opj_stream_private_t *p_stream, |
12376 | | opj_event_mgr_t * p_manager) |
12377 | 0 | { |
12378 | | /* customization of the encoding */ |
12379 | 0 | if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) { |
12380 | 0 | return OPJ_FALSE; |
12381 | 0 | } |
12382 | | |
12383 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12384 | 0 | return OPJ_FALSE; |
12385 | 0 | } |
12386 | | |
12387 | 0 | return OPJ_TRUE; |
12388 | 0 | } |
12389 | | |
12390 | | OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, |
12391 | | opj_stream_private_t *p_stream, |
12392 | | opj_image_t * p_image, |
12393 | | opj_event_mgr_t * p_manager) |
12394 | 0 | { |
12395 | | /* preconditions */ |
12396 | 0 | assert(p_j2k != 00); |
12397 | 0 | assert(p_stream != 00); |
12398 | 0 | assert(p_manager != 00); |
12399 | |
|
12400 | 0 | p_j2k->m_private_image = opj_image_create0(); |
12401 | 0 | if (! p_j2k->m_private_image) { |
12402 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header."); |
12403 | 0 | return OPJ_FALSE; |
12404 | 0 | } |
12405 | 0 | opj_copy_image_header(p_image, p_j2k->m_private_image); |
12406 | | |
12407 | | /* TODO_MSD: Find a better way */ |
12408 | 0 | if (p_image->comps) { |
12409 | 0 | OPJ_UINT32 it_comp; |
12410 | 0 | for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { |
12411 | 0 | if (p_image->comps[it_comp].data) { |
12412 | 0 | p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data; |
12413 | 0 | p_image->comps[it_comp].data = NULL; |
12414 | |
|
12415 | 0 | } |
12416 | 0 | } |
12417 | 0 | } |
12418 | | |
12419 | | /* customization of the validation */ |
12420 | 0 | if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) { |
12421 | 0 | return OPJ_FALSE; |
12422 | 0 | } |
12423 | | |
12424 | | /* validation of the parameters codec */ |
12425 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
12426 | 0 | return OPJ_FALSE; |
12427 | 0 | } |
12428 | | |
12429 | | /* customization of the encoding */ |
12430 | 0 | if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) { |
12431 | 0 | return OPJ_FALSE; |
12432 | 0 | } |
12433 | | |
12434 | | /* write header */ |
12435 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12436 | 0 | return OPJ_FALSE; |
12437 | 0 | } |
12438 | | |
12439 | 0 | return OPJ_TRUE; |
12440 | 0 | } |
12441 | | |
12442 | | static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, |
12443 | | OPJ_UINT32 p_tile_index, |
12444 | | opj_stream_private_t *p_stream, |
12445 | | opj_event_mgr_t * p_manager) |
12446 | 0 | { |
12447 | 0 | (void)p_stream; |
12448 | 0 | if (p_tile_index != p_j2k->m_current_tile_number) { |
12449 | 0 | opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match."); |
12450 | 0 | return OPJ_FALSE; |
12451 | 0 | } |
12452 | | |
12453 | 0 | opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", |
12454 | 0 | p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); |
12455 | |
|
12456 | 0 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; |
12457 | 0 | p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; |
12458 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
12459 | | |
12460 | | /* initialisation before tile encoding */ |
12461 | 0 | if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
12462 | 0 | p_manager)) { |
12463 | 0 | return OPJ_FALSE; |
12464 | 0 | } |
12465 | | |
12466 | 0 | return OPJ_TRUE; |
12467 | 0 | } |
12468 | | |
12469 | | static void opj_get_tile_dimensions(opj_image_t * l_image, |
12470 | | opj_tcd_tilecomp_t * l_tilec, |
12471 | | opj_image_comp_t * l_img_comp, |
12472 | | OPJ_UINT32* l_size_comp, |
12473 | | OPJ_UINT32* l_width, |
12474 | | OPJ_UINT32* l_height, |
12475 | | OPJ_UINT32* l_offset_x, |
12476 | | OPJ_UINT32* l_offset_y, |
12477 | | OPJ_UINT32* l_image_width, |
12478 | | OPJ_UINT32* l_stride, |
12479 | | OPJ_UINT32* l_tile_offset) |
12480 | 0 | { |
12481 | 0 | OPJ_UINT32 l_remaining; |
12482 | 0 | *l_size_comp = l_img_comp->prec >> 3; /* (/8) */ |
12483 | 0 | l_remaining = l_img_comp->prec & 7; /* (%8) */ |
12484 | 0 | if (l_remaining) { |
12485 | 0 | *l_size_comp += 1; |
12486 | 0 | } |
12487 | |
|
12488 | 0 | if (*l_size_comp == 3) { |
12489 | 0 | *l_size_comp = 4; |
12490 | 0 | } |
12491 | |
|
12492 | 0 | *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0); |
12493 | 0 | *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0); |
12494 | 0 | *l_offset_x = opj_uint_ceildiv(l_image->x0, l_img_comp->dx); |
12495 | 0 | *l_offset_y = opj_uint_ceildiv(l_image->y0, l_img_comp->dy); |
12496 | 0 | *l_image_width = opj_uint_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx); |
12497 | 0 | *l_stride = *l_image_width - *l_width; |
12498 | 0 | *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + (( |
12499 | 0 | OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width; |
12500 | 0 | } |
12501 | | |
12502 | | static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data) |
12503 | 0 | { |
12504 | 0 | OPJ_UINT32 i, j, k = 0; |
12505 | |
|
12506 | 0 | for (i = 0; i < p_tcd->image->numcomps; ++i) { |
12507 | 0 | opj_image_t * l_image = p_tcd->image; |
12508 | 0 | OPJ_INT32 * l_src_ptr; |
12509 | 0 | opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i; |
12510 | 0 | opj_image_comp_t * l_img_comp = l_image->comps + i; |
12511 | 0 | OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y, |
12512 | 0 | l_image_width, l_stride, l_tile_offset; |
12513 | |
|
12514 | 0 | opj_get_tile_dimensions(l_image, |
12515 | 0 | l_tilec, |
12516 | 0 | l_img_comp, |
12517 | 0 | &l_size_comp, |
12518 | 0 | &l_width, |
12519 | 0 | &l_height, |
12520 | 0 | &l_offset_x, |
12521 | 0 | &l_offset_y, |
12522 | 0 | &l_image_width, |
12523 | 0 | &l_stride, |
12524 | 0 | &l_tile_offset); |
12525 | |
|
12526 | 0 | l_src_ptr = l_img_comp->data + l_tile_offset; |
12527 | |
|
12528 | 0 | switch (l_size_comp) { |
12529 | 0 | case 1: { |
12530 | 0 | OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; |
12531 | 0 | if (l_img_comp->sgnd) { |
12532 | 0 | for (j = 0; j < l_height; ++j) { |
12533 | 0 | for (k = 0; k < l_width; ++k) { |
12534 | 0 | *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr); |
12535 | 0 | ++l_dest_ptr; |
12536 | 0 | ++l_src_ptr; |
12537 | 0 | } |
12538 | 0 | l_src_ptr += l_stride; |
12539 | 0 | } |
12540 | 0 | } else { |
12541 | 0 | for (j = 0; j < l_height; ++j) { |
12542 | 0 | for (k = 0; k < l_width; ++k) { |
12543 | 0 | *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff); |
12544 | 0 | ++l_dest_ptr; |
12545 | 0 | ++l_src_ptr; |
12546 | 0 | } |
12547 | 0 | l_src_ptr += l_stride; |
12548 | 0 | } |
12549 | 0 | } |
12550 | |
|
12551 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12552 | 0 | } |
12553 | 0 | break; |
12554 | 0 | case 2: { |
12555 | 0 | OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data; |
12556 | 0 | if (l_img_comp->sgnd) { |
12557 | 0 | for (j = 0; j < l_height; ++j) { |
12558 | 0 | for (k = 0; k < l_width; ++k) { |
12559 | 0 | *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++)); |
12560 | 0 | } |
12561 | 0 | l_src_ptr += l_stride; |
12562 | 0 | } |
12563 | 0 | } else { |
12564 | 0 | for (j = 0; j < l_height; ++j) { |
12565 | 0 | for (k = 0; k < l_width; ++k) { |
12566 | 0 | *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff); |
12567 | 0 | } |
12568 | 0 | l_src_ptr += l_stride; |
12569 | 0 | } |
12570 | 0 | } |
12571 | |
|
12572 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12573 | 0 | } |
12574 | 0 | break; |
12575 | 0 | case 4: { |
12576 | 0 | OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data; |
12577 | 0 | for (j = 0; j < l_height; ++j) { |
12578 | 0 | for (k = 0; k < l_width; ++k) { |
12579 | 0 | *(l_dest_ptr++) = *(l_src_ptr++); |
12580 | 0 | } |
12581 | 0 | l_src_ptr += l_stride; |
12582 | 0 | } |
12583 | |
|
12584 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12585 | 0 | } |
12586 | 0 | break; |
12587 | 0 | } |
12588 | 0 | } |
12589 | 0 | } |
12590 | | |
12591 | | static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, |
12592 | | opj_stream_private_t *p_stream, |
12593 | | opj_event_mgr_t * p_manager) |
12594 | 0 | { |
12595 | 0 | OPJ_UINT32 l_nb_bytes_written; |
12596 | 0 | OPJ_BYTE * l_current_data = 00; |
12597 | 0 | OPJ_UINT32 l_tile_size = 0; |
12598 | 0 | OPJ_UINT32 l_available_data; |
12599 | | |
12600 | | /* preconditions */ |
12601 | 0 | assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
12602 | |
|
12603 | 0 | l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; |
12604 | 0 | l_available_data = l_tile_size; |
12605 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; |
12606 | |
|
12607 | 0 | l_nb_bytes_written = 0; |
12608 | 0 | if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written, |
12609 | 0 | l_available_data, p_stream, p_manager)) { |
12610 | 0 | return OPJ_FALSE; |
12611 | 0 | } |
12612 | 0 | l_current_data += l_nb_bytes_written; |
12613 | 0 | l_available_data -= l_nb_bytes_written; |
12614 | |
|
12615 | 0 | l_nb_bytes_written = 0; |
12616 | 0 | if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written, |
12617 | 0 | l_available_data, p_stream, p_manager)) { |
12618 | 0 | return OPJ_FALSE; |
12619 | 0 | } |
12620 | | |
12621 | 0 | l_available_data -= l_nb_bytes_written; |
12622 | 0 | l_nb_bytes_written = l_tile_size - l_available_data; |
12623 | |
|
12624 | 0 | if (opj_stream_write_data(p_stream, |
12625 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, |
12626 | 0 | l_nb_bytes_written, p_manager) != l_nb_bytes_written) { |
12627 | 0 | return OPJ_FALSE; |
12628 | 0 | } |
12629 | | |
12630 | 0 | ++p_j2k->m_current_tile_number; |
12631 | |
|
12632 | 0 | return OPJ_TRUE; |
12633 | 0 | } |
12634 | | |
12635 | | static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, |
12636 | | opj_event_mgr_t * p_manager) |
12637 | 0 | { |
12638 | | /* preconditions */ |
12639 | 0 | assert(p_j2k != 00); |
12640 | 0 | assert(p_manager != 00); |
12641 | | |
12642 | | /* DEVELOPER CORNER, insert your custom procedures */ |
12643 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12644 | 0 | (opj_procedure)opj_j2k_write_eoc, p_manager)) { |
12645 | 0 | return OPJ_FALSE; |
12646 | 0 | } |
12647 | | |
12648 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
12649 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12650 | 0 | (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) { |
12651 | 0 | return OPJ_FALSE; |
12652 | 0 | } |
12653 | 0 | } |
12654 | | |
12655 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12656 | 0 | (opj_procedure)opj_j2k_write_epc, p_manager)) { |
12657 | 0 | return OPJ_FALSE; |
12658 | 0 | } |
12659 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12660 | 0 | (opj_procedure)opj_j2k_end_encoding, p_manager)) { |
12661 | 0 | return OPJ_FALSE; |
12662 | 0 | } |
12663 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12664 | 0 | (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) { |
12665 | 0 | return OPJ_FALSE; |
12666 | 0 | } |
12667 | 0 | return OPJ_TRUE; |
12668 | 0 | } |
12669 | | |
12670 | | static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, |
12671 | | opj_event_mgr_t * p_manager) |
12672 | 0 | { |
12673 | | /* preconditions */ |
12674 | 0 | assert(p_j2k != 00); |
12675 | 0 | assert(p_manager != 00); |
12676 | |
|
12677 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
12678 | 0 | (opj_procedure)opj_j2k_build_encoder, p_manager)) { |
12679 | 0 | return OPJ_FALSE; |
12680 | 0 | } |
12681 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
12682 | 0 | (opj_procedure)opj_j2k_encoding_validation, p_manager)) { |
12683 | 0 | return OPJ_FALSE; |
12684 | 0 | } |
12685 | | |
12686 | | /* DEVELOPER CORNER, add your custom validation procedure */ |
12687 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
12688 | 0 | (opj_procedure)opj_j2k_mct_validation, p_manager)) { |
12689 | 0 | return OPJ_FALSE; |
12690 | 0 | } |
12691 | | |
12692 | 0 | return OPJ_TRUE; |
12693 | 0 | } |
12694 | | |
12695 | | static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, |
12696 | | opj_event_mgr_t * p_manager) |
12697 | 0 | { |
12698 | | /* preconditions */ |
12699 | 0 | assert(p_j2k != 00); |
12700 | 0 | assert(p_manager != 00); |
12701 | |
|
12702 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12703 | 0 | (opj_procedure)opj_j2k_init_info, p_manager)) { |
12704 | 0 | return OPJ_FALSE; |
12705 | 0 | } |
12706 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12707 | 0 | (opj_procedure)opj_j2k_write_soc, p_manager)) { |
12708 | 0 | return OPJ_FALSE; |
12709 | 0 | } |
12710 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12711 | 0 | (opj_procedure)opj_j2k_write_siz, p_manager)) { |
12712 | 0 | return OPJ_FALSE; |
12713 | 0 | } |
12714 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12715 | 0 | (opj_procedure)opj_j2k_write_cod, p_manager)) { |
12716 | 0 | return OPJ_FALSE; |
12717 | 0 | } |
12718 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12719 | 0 | (opj_procedure)opj_j2k_write_qcd, p_manager)) { |
12720 | 0 | return OPJ_FALSE; |
12721 | 0 | } |
12722 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12723 | 0 | (opj_procedure)opj_j2k_write_all_coc, p_manager)) { |
12724 | 0 | return OPJ_FALSE; |
12725 | 0 | } |
12726 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12727 | 0 | (opj_procedure)opj_j2k_write_all_qcc, p_manager)) { |
12728 | 0 | return OPJ_FALSE; |
12729 | 0 | } |
12730 | | |
12731 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
12732 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12733 | 0 | (opj_procedure)opj_j2k_write_tlm, p_manager)) { |
12734 | 0 | return OPJ_FALSE; |
12735 | 0 | } |
12736 | | |
12737 | 0 | if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) { |
12738 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12739 | 0 | (opj_procedure)opj_j2k_write_poc, p_manager)) { |
12740 | 0 | return OPJ_FALSE; |
12741 | 0 | } |
12742 | 0 | } |
12743 | 0 | } |
12744 | | |
12745 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12746 | 0 | (opj_procedure)opj_j2k_write_regions, p_manager)) { |
12747 | 0 | return OPJ_FALSE; |
12748 | 0 | } |
12749 | | |
12750 | 0 | if (p_j2k->m_cp.comment != 00) { |
12751 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12752 | 0 | (opj_procedure)opj_j2k_write_com, p_manager)) { |
12753 | 0 | return OPJ_FALSE; |
12754 | 0 | } |
12755 | 0 | } |
12756 | | |
12757 | | /* DEVELOPER CORNER, insert your custom procedures */ |
12758 | 0 | if ((p_j2k->m_cp.rsiz & (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) == |
12759 | 0 | (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) { |
12760 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12761 | 0 | (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) { |
12762 | 0 | return OPJ_FALSE; |
12763 | 0 | } |
12764 | 0 | } |
12765 | | /* End of Developer Corner */ |
12766 | | |
12767 | 0 | if (p_j2k->cstr_index) { |
12768 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12769 | 0 | (opj_procedure)opj_j2k_get_end_header, p_manager)) { |
12770 | 0 | return OPJ_FALSE; |
12771 | 0 | } |
12772 | 0 | } |
12773 | | |
12774 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12775 | 0 | (opj_procedure)opj_j2k_create_tcd, p_manager)) { |
12776 | 0 | return OPJ_FALSE; |
12777 | 0 | } |
12778 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12779 | 0 | (opj_procedure)opj_j2k_update_rates, p_manager)) { |
12780 | 0 | return OPJ_FALSE; |
12781 | 0 | } |
12782 | | |
12783 | 0 | return OPJ_TRUE; |
12784 | 0 | } |
12785 | | |
12786 | | static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, |
12787 | | OPJ_BYTE * p_data, |
12788 | | OPJ_UINT32 * p_data_written, |
12789 | | OPJ_UINT32 total_data_size, |
12790 | | opj_stream_private_t *p_stream, |
12791 | | struct opj_event_mgr * p_manager) |
12792 | 0 | { |
12793 | 0 | OPJ_UINT32 l_nb_bytes_written = 0; |
12794 | 0 | OPJ_UINT32 l_current_nb_bytes_written; |
12795 | 0 | OPJ_BYTE * l_begin_data = 00; |
12796 | |
|
12797 | 0 | opj_tcd_t * l_tcd = 00; |
12798 | 0 | opj_cp_t * l_cp = 00; |
12799 | |
|
12800 | 0 | l_tcd = p_j2k->m_tcd; |
12801 | 0 | l_cp = &(p_j2k->m_cp); |
12802 | |
|
12803 | 0 | l_tcd->cur_pino = 0; |
12804 | | |
12805 | | /*Get number of tile parts*/ |
12806 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
12807 | | |
12808 | | /* INDEX >> */ |
12809 | | /* << INDEX */ |
12810 | |
|
12811 | 0 | l_current_nb_bytes_written = 0; |
12812 | 0 | l_begin_data = p_data; |
12813 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, total_data_size, |
12814 | 0 | &l_current_nb_bytes_written, p_stream, |
12815 | 0 | p_manager)) { |
12816 | 0 | return OPJ_FALSE; |
12817 | 0 | } |
12818 | | |
12819 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
12820 | 0 | p_data += l_current_nb_bytes_written; |
12821 | 0 | total_data_size -= l_current_nb_bytes_written; |
12822 | |
|
12823 | 0 | if (!OPJ_IS_CINEMA(l_cp->rsiz)) { |
12824 | | #if 0 |
12825 | | for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { |
12826 | | l_current_nb_bytes_written = 0; |
12827 | | opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
12828 | | p_manager); |
12829 | | l_nb_bytes_written += l_current_nb_bytes_written; |
12830 | | p_data += l_current_nb_bytes_written; |
12831 | | total_data_size -= l_current_nb_bytes_written; |
12832 | | |
12833 | | l_current_nb_bytes_written = 0; |
12834 | | opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
12835 | | p_manager); |
12836 | | l_nb_bytes_written += l_current_nb_bytes_written; |
12837 | | p_data += l_current_nb_bytes_written; |
12838 | | total_data_size -= l_current_nb_bytes_written; |
12839 | | } |
12840 | | #endif |
12841 | 0 | if (l_cp->tcps[p_j2k->m_current_tile_number].POC) { |
12842 | 0 | l_current_nb_bytes_written = 0; |
12843 | 0 | opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written, |
12844 | 0 | p_manager); |
12845 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
12846 | 0 | p_data += l_current_nb_bytes_written; |
12847 | 0 | total_data_size -= l_current_nb_bytes_written; |
12848 | 0 | } |
12849 | 0 | } |
12850 | |
|
12851 | 0 | l_current_nb_bytes_written = 0; |
12852 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
12853 | 0 | total_data_size, p_stream, p_manager)) { |
12854 | 0 | return OPJ_FALSE; |
12855 | 0 | } |
12856 | | |
12857 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
12858 | 0 | * p_data_written = l_nb_bytes_written; |
12859 | | |
12860 | | /* Writing Psot in SOT marker */ |
12861 | 0 | opj_write_bytes(l_begin_data + 6, l_nb_bytes_written, |
12862 | 0 | 4); /* PSOT */ |
12863 | |
|
12864 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
12865 | 0 | opj_j2k_update_tlm(p_j2k, l_nb_bytes_written); |
12866 | 0 | } |
12867 | |
|
12868 | 0 | return OPJ_TRUE; |
12869 | 0 | } |
12870 | | |
12871 | | static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, |
12872 | | OPJ_BYTE * p_data, |
12873 | | OPJ_UINT32 * p_data_written, |
12874 | | OPJ_UINT32 total_data_size, |
12875 | | opj_stream_private_t *p_stream, |
12876 | | struct opj_event_mgr * p_manager |
12877 | | ) |
12878 | 0 | { |
12879 | 0 | OPJ_UINT32 tilepartno = 0; |
12880 | 0 | OPJ_UINT32 l_nb_bytes_written = 0; |
12881 | 0 | OPJ_UINT32 l_current_nb_bytes_written; |
12882 | 0 | OPJ_UINT32 l_part_tile_size; |
12883 | 0 | OPJ_UINT32 tot_num_tp; |
12884 | 0 | OPJ_UINT32 pino; |
12885 | |
|
12886 | 0 | OPJ_BYTE * l_begin_data; |
12887 | 0 | opj_tcp_t *l_tcp = 00; |
12888 | 0 | opj_tcd_t * l_tcd = 00; |
12889 | 0 | opj_cp_t * l_cp = 00; |
12890 | |
|
12891 | 0 | l_tcd = p_j2k->m_tcd; |
12892 | 0 | l_cp = &(p_j2k->m_cp); |
12893 | 0 | l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; |
12894 | | |
12895 | | /*Get number of tile parts*/ |
12896 | 0 | tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number); |
12897 | | |
12898 | | /* start writing remaining tile parts */ |
12899 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
12900 | 0 | for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { |
12901 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
12902 | 0 | l_current_nb_bytes_written = 0; |
12903 | 0 | l_part_tile_size = 0; |
12904 | 0 | l_begin_data = p_data; |
12905 | |
|
12906 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, |
12907 | 0 | total_data_size, |
12908 | 0 | &l_current_nb_bytes_written, |
12909 | 0 | p_stream, |
12910 | 0 | p_manager)) { |
12911 | 0 | return OPJ_FALSE; |
12912 | 0 | } |
12913 | | |
12914 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
12915 | 0 | p_data += l_current_nb_bytes_written; |
12916 | 0 | total_data_size -= l_current_nb_bytes_written; |
12917 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
12918 | |
|
12919 | 0 | l_current_nb_bytes_written = 0; |
12920 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
12921 | 0 | total_data_size, p_stream, p_manager)) { |
12922 | 0 | return OPJ_FALSE; |
12923 | 0 | } |
12924 | | |
12925 | 0 | p_data += l_current_nb_bytes_written; |
12926 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
12927 | 0 | total_data_size -= l_current_nb_bytes_written; |
12928 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
12929 | | |
12930 | | /* Writing Psot in SOT marker */ |
12931 | 0 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
12932 | 0 | 4); /* PSOT */ |
12933 | |
|
12934 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
12935 | 0 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
12936 | 0 | } |
12937 | |
|
12938 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
12939 | 0 | } |
12940 | | |
12941 | 0 | for (pino = 1; pino <= l_tcp->numpocs; ++pino) { |
12942 | 0 | l_tcd->cur_pino = pino; |
12943 | | |
12944 | | /*Get number of tile parts*/ |
12945 | 0 | tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number); |
12946 | 0 | for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { |
12947 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
12948 | 0 | l_current_nb_bytes_written = 0; |
12949 | 0 | l_part_tile_size = 0; |
12950 | 0 | l_begin_data = p_data; |
12951 | |
|
12952 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, |
12953 | 0 | total_data_size, |
12954 | 0 | &l_current_nb_bytes_written, p_stream, |
12955 | 0 | p_manager)) { |
12956 | 0 | return OPJ_FALSE; |
12957 | 0 | } |
12958 | | |
12959 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
12960 | 0 | p_data += l_current_nb_bytes_written; |
12961 | 0 | total_data_size -= l_current_nb_bytes_written; |
12962 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
12963 | |
|
12964 | 0 | l_current_nb_bytes_written = 0; |
12965 | |
|
12966 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
12967 | 0 | total_data_size, p_stream, p_manager)) { |
12968 | 0 | return OPJ_FALSE; |
12969 | 0 | } |
12970 | | |
12971 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
12972 | 0 | p_data += l_current_nb_bytes_written; |
12973 | 0 | total_data_size -= l_current_nb_bytes_written; |
12974 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
12975 | | |
12976 | | /* Writing Psot in SOT marker */ |
12977 | 0 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
12978 | 0 | 4); /* PSOT */ |
12979 | |
|
12980 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
12981 | 0 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
12982 | 0 | } |
12983 | |
|
12984 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
12985 | 0 | } |
12986 | 0 | } |
12987 | | |
12988 | 0 | *p_data_written = l_nb_bytes_written; |
12989 | |
|
12990 | 0 | return OPJ_TRUE; |
12991 | 0 | } |
12992 | | |
12993 | | static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, |
12994 | | struct opj_stream_private *p_stream, |
12995 | | struct opj_event_mgr * p_manager) |
12996 | 0 | { |
12997 | 0 | OPJ_UINT32 l_tlm_size; |
12998 | 0 | OPJ_OFF_T l_tlm_position, l_current_position; |
12999 | 0 | OPJ_UINT32 size_per_tile_part; |
13000 | | |
13001 | | /* preconditions */ |
13002 | 0 | assert(p_j2k != 00); |
13003 | 0 | assert(p_manager != 00); |
13004 | 0 | assert(p_stream != 00); |
13005 | |
|
13006 | 0 | size_per_tile_part = p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte ? 5 : 6; |
13007 | 0 | l_tlm_size = size_per_tile_part * |
13008 | 0 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts; |
13009 | 0 | l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; |
13010 | 0 | l_current_position = opj_stream_tell(p_stream); |
13011 | |
|
13012 | 0 | if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) { |
13013 | 0 | return OPJ_FALSE; |
13014 | 0 | } |
13015 | | |
13016 | 0 | if (opj_stream_write_data(p_stream, |
13017 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size, |
13018 | 0 | p_manager) != l_tlm_size) { |
13019 | 0 | return OPJ_FALSE; |
13020 | 0 | } |
13021 | | |
13022 | 0 | if (! opj_stream_seek(p_stream, l_current_position, p_manager)) { |
13023 | 0 | return OPJ_FALSE; |
13024 | 0 | } |
13025 | | |
13026 | 0 | return OPJ_TRUE; |
13027 | 0 | } |
13028 | | |
13029 | | static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, |
13030 | | struct opj_stream_private *p_stream, |
13031 | | struct opj_event_mgr * p_manager) |
13032 | 0 | { |
13033 | | /* preconditions */ |
13034 | 0 | assert(p_j2k != 00); |
13035 | 0 | assert(p_manager != 00); |
13036 | 0 | assert(p_stream != 00); |
13037 | |
|
13038 | 0 | OPJ_UNUSED(p_stream); |
13039 | 0 | OPJ_UNUSED(p_manager); |
13040 | |
|
13041 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
13042 | 0 | p_j2k->m_tcd = 00; |
13043 | |
|
13044 | 0 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
13045 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
13046 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; |
13047 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; |
13048 | 0 | } |
13049 | |
|
13050 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
13051 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
13052 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; |
13053 | 0 | } |
13054 | |
|
13055 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; |
13056 | |
|
13057 | 0 | return OPJ_TRUE; |
13058 | 0 | } |
13059 | | |
13060 | | /** |
13061 | | * Destroys the memory associated with the decoding of headers. |
13062 | | */ |
13063 | | static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, |
13064 | | opj_stream_private_t *p_stream, |
13065 | | opj_event_mgr_t * p_manager |
13066 | | ) |
13067 | 0 | { |
13068 | | /* preconditions */ |
13069 | 0 | assert(p_j2k != 00); |
13070 | 0 | assert(p_stream != 00); |
13071 | 0 | assert(p_manager != 00); |
13072 | |
|
13073 | 0 | OPJ_UNUSED(p_stream); |
13074 | 0 | OPJ_UNUSED(p_manager); |
13075 | |
|
13076 | 0 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
13077 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
13078 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; |
13079 | 0 | } |
13080 | |
|
13081 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
13082 | |
|
13083 | 0 | return OPJ_TRUE; |
13084 | 0 | } |
13085 | | |
13086 | | static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, |
13087 | | struct opj_stream_private *p_stream, |
13088 | | struct opj_event_mgr * p_manager) |
13089 | 0 | { |
13090 | 0 | opj_codestream_info_t * l_cstr_info = 00; |
13091 | | |
13092 | | /* preconditions */ |
13093 | 0 | assert(p_j2k != 00); |
13094 | 0 | assert(p_manager != 00); |
13095 | 0 | assert(p_stream != 00); |
13096 | 0 | (void)l_cstr_info; |
13097 | |
|
13098 | 0 | OPJ_UNUSED(p_stream); |
13099 | | |
13100 | | /* TODO mergeV2: check this part which use cstr_info */ |
13101 | | /*l_cstr_info = p_j2k->cstr_info; |
13102 | | |
13103 | | if (l_cstr_info) { |
13104 | | OPJ_UINT32 compno; |
13105 | | l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t)); |
13106 | | |
13107 | | l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; |
13108 | | l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; |
13109 | | |
13110 | | l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; |
13111 | | |
13112 | | l_cstr_info->tw = p_j2k->m_cp.tw; |
13113 | | l_cstr_info->th = p_j2k->m_cp.th; |
13114 | | |
13115 | | l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ |
13116 | | /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ |
13117 | | /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ |
13118 | | /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ |
13119 | | |
13120 | | /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; |
13121 | | |
13122 | | l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; |
13123 | | |
13124 | | l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); |
13125 | | |
13126 | | for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { |
13127 | | l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; |
13128 | | } |
13129 | | |
13130 | | l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ |
13131 | | |
13132 | | /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ |
13133 | | |
13134 | | /*l_cstr_info->maxmarknum = 100; |
13135 | | l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); |
13136 | | l_cstr_info->marknum = 0; |
13137 | | }*/ |
13138 | |
|
13139 | 0 | return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp), |
13140 | 0 | &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image, |
13141 | 0 | p_manager); |
13142 | 0 | } |
13143 | | |
13144 | | /** |
13145 | | * Creates a tile-coder encoder. |
13146 | | * |
13147 | | * @param p_stream the stream to write data to. |
13148 | | * @param p_j2k J2K codec. |
13149 | | * @param p_manager the user event manager. |
13150 | | */ |
13151 | | static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, |
13152 | | opj_stream_private_t *p_stream, |
13153 | | opj_event_mgr_t * p_manager |
13154 | | ) |
13155 | 0 | { |
13156 | | /* preconditions */ |
13157 | 0 | assert(p_j2k != 00); |
13158 | 0 | assert(p_manager != 00); |
13159 | 0 | assert(p_stream != 00); |
13160 | |
|
13161 | 0 | OPJ_UNUSED(p_stream); |
13162 | |
|
13163 | 0 | p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); |
13164 | |
|
13165 | 0 | if (! p_j2k->m_tcd) { |
13166 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); |
13167 | 0 | return OPJ_FALSE; |
13168 | 0 | } |
13169 | | |
13170 | 0 | if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp, |
13171 | 0 | p_j2k->m_tp)) { |
13172 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
13173 | 0 | p_j2k->m_tcd = 00; |
13174 | 0 | return OPJ_FALSE; |
13175 | 0 | } |
13176 | | |
13177 | 0 | return OPJ_TRUE; |
13178 | 0 | } |
13179 | | |
13180 | | OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k, |
13181 | | OPJ_UINT32 p_tile_index, |
13182 | | OPJ_BYTE * p_data, |
13183 | | OPJ_UINT32 p_data_size, |
13184 | | opj_stream_private_t *p_stream, |
13185 | | opj_event_mgr_t * p_manager) |
13186 | 0 | { |
13187 | 0 | if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) { |
13188 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13189 | 0 | "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); |
13190 | 0 | return OPJ_FALSE; |
13191 | 0 | } else { |
13192 | 0 | OPJ_UINT32 j; |
13193 | | /* Allocate data */ |
13194 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
13195 | 0 | opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j; |
13196 | |
|
13197 | 0 | if (! opj_alloc_tile_component_data(l_tilec)) { |
13198 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); |
13199 | 0 | return OPJ_FALSE; |
13200 | 0 | } |
13201 | 0 | } |
13202 | | |
13203 | | /* now copy data into the tile component */ |
13204 | 0 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
13205 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13206 | 0 | "Size mismatch between tile data and sent data."); |
13207 | 0 | return OPJ_FALSE; |
13208 | 0 | } |
13209 | 0 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
13210 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13211 | 0 | "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); |
13212 | 0 | return OPJ_FALSE; |
13213 | 0 | } |
13214 | 0 | } |
13215 | | |
13216 | 0 | return OPJ_TRUE; |
13217 | 0 | } |