/src/opencv/3rdparty/openjpeg/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 | 987 | { |
1450 | 987 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1451 | 987 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1452 | 987 | OPJ_UINT32 i; |
1453 | 987 | OPJ_UINT32 l_temp; |
1454 | | |
1455 | 3.62k | for (i = 0; i < p_nb_elem; ++i) { |
1456 | 2.63k | opj_read_bytes(l_src_data, &l_temp, 2); |
1457 | | |
1458 | 2.63k | l_src_data += sizeof(OPJ_INT16); |
1459 | | |
1460 | 2.63k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1461 | 2.63k | } |
1462 | 987 | } |
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 | 308 | { |
1467 | 308 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1468 | 308 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1469 | 308 | OPJ_UINT32 i; |
1470 | 308 | OPJ_UINT32 l_temp; |
1471 | | |
1472 | 1.33k | for (i = 0; i < p_nb_elem; ++i) { |
1473 | 1.02k | opj_read_bytes(l_src_data, &l_temp, 4); |
1474 | | |
1475 | 1.02k | l_src_data += sizeof(OPJ_INT32); |
1476 | | |
1477 | 1.02k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1478 | 1.02k | } |
1479 | 308 | } |
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 | 4.04k | { |
1484 | 4.04k | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1485 | 4.04k | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1486 | 4.04k | OPJ_UINT32 i; |
1487 | 4.04k | OPJ_FLOAT32 l_temp; |
1488 | | |
1489 | 19.5k | for (i = 0; i < p_nb_elem; ++i) { |
1490 | 15.4k | opj_read_float(l_src_data, &l_temp); |
1491 | | |
1492 | 15.4k | l_src_data += sizeof(OPJ_FLOAT32); |
1493 | | |
1494 | 15.4k | *(l_dest_data++) = l_temp; |
1495 | 15.4k | } |
1496 | 4.04k | } |
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 | 796 | { |
1501 | 796 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1502 | 796 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1503 | 796 | OPJ_UINT32 i; |
1504 | 796 | OPJ_FLOAT64 l_temp; |
1505 | | |
1506 | 3.71k | for (i = 0; i < p_nb_elem; ++i) { |
1507 | 2.92k | opj_read_double(l_src_data, &l_temp); |
1508 | | |
1509 | 2.92k | l_src_data += sizeof(OPJ_FLOAT64); |
1510 | | |
1511 | 2.92k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1512 | 2.92k | } |
1513 | 796 | } |
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 | 768 | { |
1518 | 768 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1519 | 768 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1520 | 768 | OPJ_UINT32 i; |
1521 | 768 | OPJ_UINT32 l_temp; |
1522 | | |
1523 | 1.88k | for (i = 0; i < p_nb_elem; ++i) { |
1524 | 1.11k | opj_read_bytes(l_src_data, &l_temp, 2); |
1525 | | |
1526 | 1.11k | l_src_data += sizeof(OPJ_INT16); |
1527 | | |
1528 | 1.11k | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1529 | 1.11k | } |
1530 | 768 | } |
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 | 314 | { |
1535 | 314 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1536 | 314 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1537 | 314 | OPJ_UINT32 i; |
1538 | 314 | OPJ_UINT32 l_temp; |
1539 | | |
1540 | 872 | for (i = 0; i < p_nb_elem; ++i) { |
1541 | 558 | opj_read_bytes(l_src_data, &l_temp, 4); |
1542 | | |
1543 | 558 | l_src_data += sizeof(OPJ_INT32); |
1544 | | |
1545 | 558 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1546 | 558 | } |
1547 | 314 | } |
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 | 323 | { |
1552 | 323 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1553 | 323 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1554 | 323 | OPJ_UINT32 i; |
1555 | 323 | OPJ_FLOAT32 l_temp; |
1556 | | |
1557 | 793 | for (i = 0; i < p_nb_elem; ++i) { |
1558 | 470 | opj_read_float(l_src_data, &l_temp); |
1559 | | |
1560 | 470 | l_src_data += sizeof(OPJ_FLOAT32); |
1561 | | |
1562 | 470 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1563 | 470 | } |
1564 | 323 | } |
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 | 90 | { |
1569 | 90 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1570 | 90 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1571 | 90 | OPJ_UINT32 i; |
1572 | 90 | OPJ_FLOAT64 l_temp; |
1573 | | |
1574 | 189 | for (i = 0; i < p_nb_elem; ++i) { |
1575 | 99 | opj_read_double(l_src_data, &l_temp); |
1576 | | |
1577 | 99 | l_src_data += sizeof(OPJ_FLOAT64); |
1578 | | |
1579 | 99 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1580 | 99 | } |
1581 | 90 | } |
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 | 8.05k | { |
1932 | 8.05k | OPJ_BYTE l_data [2]; |
1933 | 8.05k | OPJ_UINT32 l_marker; |
1934 | | |
1935 | | /* preconditions */ |
1936 | 8.05k | assert(p_j2k != 00); |
1937 | 8.05k | assert(p_manager != 00); |
1938 | 8.05k | assert(p_stream != 00); |
1939 | | |
1940 | 8.05k | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
1941 | 5 | return OPJ_FALSE; |
1942 | 5 | } |
1943 | | |
1944 | 8.05k | opj_read_bytes(l_data, &l_marker, 2); |
1945 | 8.05k | if (l_marker != J2K_MS_SOC) { |
1946 | 2 | return OPJ_FALSE; |
1947 | 2 | } |
1948 | | |
1949 | | /* Next marker should be a SIZ marker in the main header */ |
1950 | 8.04k | 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 | 8.04k | p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; |
1954 | | |
1955 | 8.04k | opj_event_msg(p_manager, EVT_INFO, |
1956 | 8.04k | "Start to read j2k main header (%" PRId64 ").\n", |
1957 | 8.04k | p_j2k->cstr_index->main_head_start); |
1958 | | |
1959 | | /* Add the marker to the codestream index*/ |
1960 | 8.04k | if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, |
1961 | 8.04k | 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 | 8.04k | return OPJ_TRUE; |
1966 | 8.04k | } |
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 | 8.04k | { |
2080 | 8.04k | OPJ_UINT32 i; |
2081 | 8.04k | OPJ_UINT32 l_nb_comp; |
2082 | 8.04k | OPJ_UINT32 l_nb_comp_remain; |
2083 | 8.04k | OPJ_UINT32 l_remaining_size; |
2084 | 8.04k | OPJ_UINT32 l_nb_tiles; |
2085 | 8.04k | OPJ_UINT32 l_tmp, l_tx1, l_ty1; |
2086 | 8.04k | OPJ_UINT32 l_prec0, l_sgnd0; |
2087 | 8.04k | opj_image_t *l_image = 00; |
2088 | 8.04k | opj_cp_t *l_cp = 00; |
2089 | 8.04k | opj_image_comp_t * l_img_comp = 00; |
2090 | 8.04k | opj_tcp_t * l_current_tile_param = 00; |
2091 | | |
2092 | | /* preconditions */ |
2093 | 8.04k | assert(p_j2k != 00); |
2094 | 8.04k | assert(p_manager != 00); |
2095 | 8.04k | assert(p_header_data != 00); |
2096 | | |
2097 | 8.04k | l_image = p_j2k->m_private_image; |
2098 | 8.04k | l_cp = &(p_j2k->m_cp); |
2099 | | |
2100 | | /* minimum size == 39 - 3 (= minimum component parameter) */ |
2101 | 8.04k | if (p_header_size < 36) { |
2102 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); |
2103 | 0 | return OPJ_FALSE; |
2104 | 0 | } |
2105 | | |
2106 | 8.04k | l_remaining_size = p_header_size - 36; |
2107 | 8.04k | l_nb_comp = l_remaining_size / 3; |
2108 | 8.04k | l_nb_comp_remain = l_remaining_size % 3; |
2109 | 8.04k | 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 | 8.04k | opj_read_bytes(p_header_data, &l_tmp, |
2115 | 8.04k | 2); /* Rsiz (capabilities) */ |
2116 | 8.04k | p_header_data += 2; |
2117 | 8.04k | l_cp->rsiz = (OPJ_UINT16) l_tmp; |
2118 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ |
2119 | 8.04k | p_header_data += 4; |
2120 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ |
2121 | 8.04k | p_header_data += 4; |
2122 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ |
2123 | 8.04k | p_header_data += 4; |
2124 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ |
2125 | 8.04k | p_header_data += 4; |
2126 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, |
2127 | 8.04k | 4); /* XTsiz */ |
2128 | 8.04k | p_header_data += 4; |
2129 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, |
2130 | 8.04k | 4); /* YTsiz */ |
2131 | 8.04k | p_header_data += 4; |
2132 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, |
2133 | 8.04k | 4); /* XT0siz */ |
2134 | 8.04k | p_header_data += 4; |
2135 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, |
2136 | 8.04k | 4); /* YT0siz */ |
2137 | 8.04k | p_header_data += 4; |
2138 | 8.04k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, |
2139 | 8.04k | 2); /* Csiz */ |
2140 | 8.04k | p_header_data += 2; |
2141 | 8.04k | if (l_tmp < 16385) { |
2142 | 8.04k | l_image->numcomps = (OPJ_UINT16) l_tmp; |
2143 | 8.04k | } else { |
2144 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2145 | 0 | "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp); |
2146 | 0 | return OPJ_FALSE; |
2147 | 0 | } |
2148 | | |
2149 | 8.04k | if (l_image->numcomps != l_nb_comp) { |
2150 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
2151 | 1 | "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", |
2152 | 1 | l_image->numcomps, l_nb_comp); |
2153 | 1 | return OPJ_FALSE; |
2154 | 1 | } |
2155 | | |
2156 | | /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ |
2157 | | /* testcase issue427-null-image-size.jp2 */ |
2158 | 8.04k | if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { |
2159 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2160 | 0 | "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64 |
2161 | 0 | ")\n", (OPJ_INT64)l_image->x1 - l_image->x0, |
2162 | 0 | (OPJ_INT64)l_image->y1 - l_image->y0); |
2163 | 0 | return OPJ_FALSE; |
2164 | 0 | } |
2165 | | /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ |
2166 | 8.04k | if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) { |
2167 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2168 | 0 | "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, |
2169 | 0 | l_cp->tdy); |
2170 | 0 | return OPJ_FALSE; |
2171 | 0 | } |
2172 | | |
2173 | | /* testcase issue427-illegal-tile-offset.jp2 */ |
2174 | 8.04k | l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */ |
2175 | 8.04k | l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */ |
2176 | 8.04k | if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || |
2177 | 8.04k | (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) { |
2178 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
2179 | 4 | "Error with SIZ marker: illegal tile offset\n"); |
2180 | 4 | return OPJ_FALSE; |
2181 | 4 | } |
2182 | 8.03k | if (!p_j2k->dump_state) { |
2183 | 8.03k | OPJ_UINT32 siz_w, siz_h; |
2184 | | |
2185 | 8.03k | siz_w = l_image->x1 - l_image->x0; |
2186 | 8.03k | siz_h = l_image->y1 - l_image->y0; |
2187 | | |
2188 | 8.03k | if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0 |
2189 | 8.03k | && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) { |
2190 | 5 | opj_event_msg(p_manager, EVT_ERROR, |
2191 | 5 | "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w, |
2192 | 5 | p_j2k->ihdr_h, siz_w, siz_h); |
2193 | 5 | return OPJ_FALSE; |
2194 | 5 | } |
2195 | 8.03k | } |
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 | 8.03k | l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, |
2240 | 8.03k | sizeof(opj_image_comp_t)); |
2241 | 8.03k | if (l_image->comps == 00) { |
2242 | 0 | l_image->numcomps = 0; |
2243 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2244 | 0 | "Not enough memory to take in charge SIZ marker\n"); |
2245 | 0 | return OPJ_FALSE; |
2246 | 0 | } |
2247 | | |
2248 | 8.03k | l_img_comp = l_image->comps; |
2249 | | |
2250 | 8.03k | l_prec0 = 0; |
2251 | 8.03k | l_sgnd0 = 0; |
2252 | | /* Read the component information */ |
2253 | 19.6k | for (i = 0; i < l_image->numcomps; ++i) { |
2254 | 11.6k | OPJ_UINT32 tmp; |
2255 | 11.6k | opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */ |
2256 | 11.6k | ++p_header_data; |
2257 | 11.6k | l_img_comp->prec = (tmp & 0x7f) + 1; |
2258 | 11.6k | l_img_comp->sgnd = tmp >> 7; |
2259 | | |
2260 | 11.6k | if (p_j2k->dump_state == 0) { |
2261 | 11.6k | if (i == 0) { |
2262 | 8.03k | l_prec0 = l_img_comp->prec; |
2263 | 8.03k | l_sgnd0 = l_img_comp->sgnd; |
2264 | 8.03k | } else if (!l_cp->allow_different_bit_depth_sign |
2265 | 3.63k | && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) { |
2266 | 6 | opj_event_msg(p_manager, EVT_WARNING, |
2267 | 6 | "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n" |
2268 | 6 | " [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0, |
2269 | 6 | i, l_img_comp->prec, l_img_comp->sgnd); |
2270 | 6 | } |
2271 | | /* TODO: we should perhaps also check against JP2 BPCC values */ |
2272 | 11.6k | } |
2273 | 11.6k | opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */ |
2274 | 11.6k | ++p_header_data; |
2275 | 11.6k | l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2276 | 11.6k | opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */ |
2277 | 11.6k | ++p_header_data; |
2278 | 11.6k | l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2279 | 11.6k | if (l_img_comp->dx < 1 || l_img_comp->dx > 255 || |
2280 | 11.6k | l_img_comp->dy < 1 || l_img_comp->dy > 255) { |
2281 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
2282 | 4 | "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n", |
2283 | 4 | i, l_img_comp->dx, l_img_comp->dy); |
2284 | 4 | return OPJ_FALSE; |
2285 | 4 | } |
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 | 11.6k | if (l_img_comp->prec > 31) { |
2290 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
2291 | 3 | "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 | 3 | i, l_img_comp->prec); |
2293 | 3 | return OPJ_FALSE; |
2294 | 3 | } |
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 | 11.6k | l_img_comp->resno_decoded = |
2325 | 11.6k | 0; /* number of resolution decoded */ |
2326 | 11.6k | l_img_comp->factor = |
2327 | 11.6k | l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ |
2328 | 11.6k | ++l_img_comp; |
2329 | 11.6k | } |
2330 | | |
2331 | 8.02k | if (l_cp->tdx == 0 || l_cp->tdy == 0) { |
2332 | 0 | return OPJ_FALSE; |
2333 | 0 | } |
2334 | | |
2335 | | /* Compute the number of tiles */ |
2336 | 8.02k | l_cp->tw = opj_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx); |
2337 | 8.02k | 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 | 8.02k | if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { |
2341 | 2 | opj_event_msg(p_manager, EVT_ERROR, |
2342 | 2 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", |
2343 | 2 | l_cp->tw, l_cp->th); |
2344 | 2 | return OPJ_FALSE; |
2345 | 2 | } |
2346 | 8.02k | l_nb_tiles = l_cp->tw * l_cp->th; |
2347 | | |
2348 | | /* Define the tiles which will be decoded */ |
2349 | 8.02k | 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 | 8.02k | } else { |
2361 | 8.02k | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
2362 | 8.02k | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
2363 | 8.02k | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
2364 | 8.02k | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
2365 | 8.02k | } |
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 | 8.02k | l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); |
2414 | 8.02k | 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 | 8.02k | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = |
2434 | 8.02k | (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); |
2435 | 8.02k | 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 | 8.02k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = |
2442 | 8.02k | (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS, |
2443 | 8.02k | sizeof(opj_mct_data_t)); |
2444 | | |
2445 | 8.02k | 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 | 8.02k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = |
2451 | 8.02k | OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
2452 | | |
2453 | 8.02k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = |
2454 | 8.02k | (opj_simple_mcc_decorrelation_data_t*) |
2455 | 8.02k | opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, |
2456 | 8.02k | sizeof(opj_simple_mcc_decorrelation_data_t)); |
2457 | | |
2458 | 8.02k | 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 | 8.02k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = |
2464 | 8.02k | OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
2465 | | |
2466 | | /* set up default dc level shift */ |
2467 | 19.3k | for (i = 0; i < l_image->numcomps; ++i) { |
2468 | 11.3k | if (! l_image->comps[i].sgnd) { |
2469 | 10.4k | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 |
2470 | 10.4k | << (l_image->comps[i].prec - 1); |
2471 | 10.4k | } |
2472 | 11.3k | } |
2473 | | |
2474 | 8.02k | l_current_tile_param = l_cp->tcps; |
2475 | 359k | for (i = 0; i < l_nb_tiles; ++i) { |
2476 | 351k | l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, |
2477 | 351k | sizeof(opj_tccp_t)); |
2478 | 351k | 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 | 351k | ++l_current_tile_param; |
2485 | 351k | } |
2486 | | |
2487 | | /*Allocate and initialize some elements of codestrem index*/ |
2488 | 8.02k | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { |
2489 | 0 | return OPJ_FALSE; |
2490 | 0 | } |
2491 | | |
2492 | 8.02k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; |
2493 | 8.02k | opj_image_comp_header_update(l_image, l_cp); |
2494 | | |
2495 | 8.02k | return OPJ_TRUE; |
2496 | 8.02k | } |
2497 | | |
2498 | | static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, |
2499 | | opj_stream_private_t *p_stream, |
2500 | | opj_event_mgr_t * p_manager |
2501 | | ) |
2502 | 0 | { |
2503 | 0 | OPJ_UINT32 l_comment_size; |
2504 | 0 | OPJ_UINT32 l_total_com_size; |
2505 | 0 | const OPJ_CHAR *l_comment; |
2506 | 0 | OPJ_BYTE * l_current_ptr = 00; |
2507 | | |
2508 | | /* preconditions */ |
2509 | 0 | assert(p_j2k != 00); |
2510 | 0 | assert(p_stream != 00); |
2511 | 0 | assert(p_manager != 00); |
2512 | |
|
2513 | 0 | l_comment = p_j2k->m_cp.comment; |
2514 | 0 | l_comment_size = (OPJ_UINT32)strlen(l_comment); |
2515 | 0 | l_total_com_size = l_comment_size + 6; |
2516 | |
|
2517 | 0 | if (l_total_com_size > |
2518 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2519 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
2520 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size); |
2521 | 0 | if (! new_header_tile_data) { |
2522 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2523 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2524 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2525 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2526 | 0 | "Not enough memory to write the COM marker\n"); |
2527 | 0 | return OPJ_FALSE; |
2528 | 0 | } |
2529 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2530 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; |
2531 | 0 | } |
2532 | | |
2533 | 0 | l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2534 | |
|
2535 | 0 | opj_write_bytes(l_current_ptr, J2K_MS_COM, 2); /* COM */ |
2536 | 0 | l_current_ptr += 2; |
2537 | |
|
2538 | 0 | opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2); /* L_COM */ |
2539 | 0 | l_current_ptr += 2; |
2540 | |
|
2541 | 0 | opj_write_bytes(l_current_ptr, 1, |
2542 | 0 | 2); /* General use (IS 8859-15:1999 (Latin) values) */ |
2543 | 0 | l_current_ptr += 2; |
2544 | |
|
2545 | 0 | memcpy(l_current_ptr, l_comment, l_comment_size); |
2546 | |
|
2547 | 0 | if (opj_stream_write_data(p_stream, |
2548 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size, |
2549 | 0 | p_manager) != l_total_com_size) { |
2550 | 0 | return OPJ_FALSE; |
2551 | 0 | } |
2552 | | |
2553 | 0 | return OPJ_TRUE; |
2554 | 0 | } |
2555 | | |
2556 | | /** |
2557 | | * Reads a COM marker (comments) |
2558 | | * @param p_j2k the jpeg2000 file codec. |
2559 | | * @param p_header_data the data contained in the COM box. |
2560 | | * @param p_header_size the size of the data contained in the COM marker. |
2561 | | * @param p_manager the user event manager. |
2562 | | */ |
2563 | | static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, |
2564 | | OPJ_BYTE * p_header_data, |
2565 | | OPJ_UINT32 p_header_size, |
2566 | | opj_event_mgr_t * p_manager |
2567 | | ) |
2568 | 2.00k | { |
2569 | | /* preconditions */ |
2570 | 2.00k | assert(p_j2k != 00); |
2571 | 2.00k | assert(p_manager != 00); |
2572 | 2.00k | assert(p_header_data != 00); |
2573 | | |
2574 | 2.00k | OPJ_UNUSED(p_j2k); |
2575 | 2.00k | OPJ_UNUSED(p_header_data); |
2576 | 2.00k | OPJ_UNUSED(p_header_size); |
2577 | 2.00k | OPJ_UNUSED(p_manager); |
2578 | | |
2579 | 2.00k | return OPJ_TRUE; |
2580 | 2.00k | } |
2581 | | |
2582 | | static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, |
2583 | | opj_stream_private_t *p_stream, |
2584 | | opj_event_mgr_t * p_manager) |
2585 | 0 | { |
2586 | 0 | opj_cp_t *l_cp = 00; |
2587 | 0 | opj_tcp_t *l_tcp = 00; |
2588 | 0 | OPJ_UINT32 l_code_size, l_remaining_size; |
2589 | 0 | OPJ_BYTE * l_current_data = 00; |
2590 | | |
2591 | | /* preconditions */ |
2592 | 0 | assert(p_j2k != 00); |
2593 | 0 | assert(p_manager != 00); |
2594 | 0 | assert(p_stream != 00); |
2595 | |
|
2596 | 0 | l_cp = &(p_j2k->m_cp); |
2597 | 0 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2598 | 0 | l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2599 | 0 | p_j2k->m_current_tile_number, 0); |
2600 | 0 | l_remaining_size = l_code_size; |
2601 | |
|
2602 | 0 | if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2603 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
2604 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size); |
2605 | 0 | if (! new_header_tile_data) { |
2606 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2607 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2608 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2609 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n"); |
2610 | 0 | return OPJ_FALSE; |
2611 | 0 | } |
2612 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2613 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; |
2614 | 0 | } |
2615 | | |
2616 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2617 | |
|
2618 | 0 | opj_write_bytes(l_current_data, J2K_MS_COD, 2); /* COD */ |
2619 | 0 | l_current_data += 2; |
2620 | |
|
2621 | 0 | opj_write_bytes(l_current_data, l_code_size - 2, 2); /* L_COD */ |
2622 | 0 | l_current_data += 2; |
2623 | |
|
2624 | 0 | opj_write_bytes(l_current_data, l_tcp->csty, 1); /* Scod */ |
2625 | 0 | ++l_current_data; |
2626 | |
|
2627 | 0 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */ |
2628 | 0 | ++l_current_data; |
2629 | |
|
2630 | 0 | opj_write_bytes(l_current_data, l_tcp->numlayers, 2); /* SGcod (B) */ |
2631 | 0 | l_current_data += 2; |
2632 | |
|
2633 | 0 | opj_write_bytes(l_current_data, l_tcp->mct, 1); /* SGcod (C) */ |
2634 | 0 | ++l_current_data; |
2635 | |
|
2636 | 0 | l_remaining_size -= 9; |
2637 | |
|
2638 | 0 | if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, |
2639 | 0 | l_current_data, &l_remaining_size, p_manager)) { |
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 (l_remaining_size != 0) { |
2645 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); |
2646 | 0 | return OPJ_FALSE; |
2647 | 0 | } |
2648 | | |
2649 | 0 | if (opj_stream_write_data(p_stream, |
2650 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size, |
2651 | 0 | p_manager) != l_code_size) { |
2652 | 0 | return OPJ_FALSE; |
2653 | 0 | } |
2654 | | |
2655 | 0 | return OPJ_TRUE; |
2656 | 0 | } |
2657 | | |
2658 | | /** |
2659 | | * Reads a COD marker (Coding style defaults) |
2660 | | * @param p_header_data the data contained in the COD box. |
2661 | | * @param p_j2k the jpeg2000 codec. |
2662 | | * @param p_header_size the size of the data contained in the COD marker. |
2663 | | * @param p_manager the user event manager. |
2664 | | */ |
2665 | | static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, |
2666 | | OPJ_BYTE * p_header_data, |
2667 | | OPJ_UINT32 p_header_size, |
2668 | | opj_event_mgr_t * p_manager |
2669 | | ) |
2670 | 12.9k | { |
2671 | | /* loop */ |
2672 | 12.9k | OPJ_UINT32 i; |
2673 | 12.9k | OPJ_UINT32 l_tmp; |
2674 | 12.9k | opj_cp_t *l_cp = 00; |
2675 | 12.9k | opj_tcp_t *l_tcp = 00; |
2676 | 12.9k | opj_image_t *l_image = 00; |
2677 | | |
2678 | | /* preconditions */ |
2679 | 12.9k | assert(p_header_data != 00); |
2680 | 12.9k | assert(p_j2k != 00); |
2681 | 12.9k | assert(p_manager != 00); |
2682 | | |
2683 | 12.9k | l_image = p_j2k->m_private_image; |
2684 | 12.9k | l_cp = &(p_j2k->m_cp); |
2685 | | |
2686 | | /* If we are in the first tile-part header of the current tile */ |
2687 | 12.9k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
2688 | 54 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2689 | 12.9k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
2690 | | |
2691 | | #if 0 |
2692 | | /* This check was added per https://github.com/uclouvain/openjpeg/commit/daed8cc9195555e101ab708a501af2dfe6d5e001 */ |
2693 | | /* but this is no longer necessary to handle issue476.jp2 */ |
2694 | | /* and this actually cause issues on legit files. See https://github.com/uclouvain/openjpeg/issues/1043 */ |
2695 | | /* Only one COD per tile */ |
2696 | | if (l_tcp->cod) { |
2697 | | opj_event_msg(p_manager, EVT_ERROR, |
2698 | | "COD marker already read. No more than one COD marker per tile.\n"); |
2699 | | return OPJ_FALSE; |
2700 | | } |
2701 | | #endif |
2702 | 12.9k | l_tcp->cod = 1; |
2703 | | |
2704 | | /* Make sure room is sufficient */ |
2705 | 12.9k | if (p_header_size < 5) { |
2706 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2707 | 0 | return OPJ_FALSE; |
2708 | 0 | } |
2709 | | |
2710 | 12.9k | opj_read_bytes(p_header_data, &l_tcp->csty, 1); /* Scod */ |
2711 | 12.9k | ++p_header_data; |
2712 | | /* Make sure we know how to decode this */ |
2713 | 12.9k | if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | |
2714 | 12.9k | J2K_CP_CSTY_EPH)) != 0U) { |
2715 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n"); |
2716 | 4 | return OPJ_FALSE; |
2717 | 4 | } |
2718 | 12.9k | opj_read_bytes(p_header_data, &l_tmp, 1); /* SGcod (A) */ |
2719 | 12.9k | ++p_header_data; |
2720 | 12.9k | l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; |
2721 | | /* Make sure progression order is valid */ |
2722 | 12.9k | if (l_tcp->prg > OPJ_CPRL) { |
2723 | 910 | opj_event_msg(p_manager, EVT_ERROR, |
2724 | 910 | "Unknown progression order in COD marker\n"); |
2725 | 910 | l_tcp->prg = OPJ_PROG_UNKNOWN; |
2726 | 910 | } |
2727 | 12.9k | opj_read_bytes(p_header_data, &l_tcp->numlayers, 2); /* SGcod (B) */ |
2728 | 12.9k | p_header_data += 2; |
2729 | | |
2730 | 12.9k | if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) { |
2731 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2732 | 0 | "Invalid number of layers in COD marker : %d not in range [1-65535]\n", |
2733 | 0 | l_tcp->numlayers); |
2734 | 0 | return OPJ_FALSE; |
2735 | 0 | } |
2736 | | |
2737 | | /* If user didn't set a number layer to decode take the max specify in the codestream. */ |
2738 | 12.9k | if (l_cp->m_specific_param.m_dec.m_layer) { |
2739 | 0 | l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; |
2740 | 12.9k | } else { |
2741 | 12.9k | l_tcp->num_layers_to_decode = l_tcp->numlayers; |
2742 | 12.9k | } |
2743 | | |
2744 | 12.9k | opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */ |
2745 | 12.9k | ++p_header_data; |
2746 | | |
2747 | 12.9k | if (l_tcp->mct > 1) { |
2748 | 8 | opj_event_msg(p_manager, EVT_ERROR, |
2749 | 8 | "Invalid multiple component transformation\n"); |
2750 | 8 | return OPJ_FALSE; |
2751 | 8 | } |
2752 | | |
2753 | 12.9k | p_header_size -= 5; |
2754 | 34.6k | for (i = 0; i < l_image->numcomps; ++i) { |
2755 | 21.6k | l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; |
2756 | 21.6k | } |
2757 | | |
2758 | 12.9k | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size, |
2759 | 12.9k | p_manager)) { |
2760 | 15 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2761 | 15 | return OPJ_FALSE; |
2762 | 15 | } |
2763 | | |
2764 | 12.9k | if (p_header_size != 0) { |
2765 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2766 | 2 | return OPJ_FALSE; |
2767 | 2 | } |
2768 | | |
2769 | | /* Apply the coding style to other components of the current tile or the m_default_tcp*/ |
2770 | 12.9k | opj_j2k_copy_tile_component_parameters(p_j2k); |
2771 | | |
2772 | | /* Index */ |
2773 | | #ifdef WIP_REMOVE_MSD |
2774 | | if (p_j2k->cstr_info) { |
2775 | | /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/ |
2776 | | p_j2k->cstr_info->prog = l_tcp->prg; |
2777 | | p_j2k->cstr_info->numlayers = l_tcp->numlayers; |
2778 | | p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc( |
2779 | | l_image->numcomps * sizeof(OPJ_UINT32)); |
2780 | | if (!p_j2k->cstr_info->numdecompos) { |
2781 | | return OPJ_FALSE; |
2782 | | } |
2783 | | for (i = 0; i < l_image->numcomps; ++i) { |
2784 | | p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; |
2785 | | } |
2786 | | } |
2787 | | #endif |
2788 | | |
2789 | 12.9k | return OPJ_TRUE; |
2790 | 12.9k | } |
2791 | | |
2792 | | static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, |
2793 | | OPJ_UINT32 p_comp_no, |
2794 | | opj_stream_private_t *p_stream, |
2795 | | opj_event_mgr_t * p_manager) |
2796 | 0 | { |
2797 | 0 | OPJ_UINT32 l_coc_size, l_remaining_size; |
2798 | 0 | OPJ_UINT32 l_comp_room; |
2799 | | |
2800 | | /* preconditions */ |
2801 | 0 | assert(p_j2k != 00); |
2802 | 0 | assert(p_manager != 00); |
2803 | 0 | assert(p_stream != 00); |
2804 | |
|
2805 | 0 | l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2; |
2806 | |
|
2807 | 0 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2808 | 0 | p_j2k->m_current_tile_number, p_comp_no); |
2809 | |
|
2810 | 0 | if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2811 | 0 | OPJ_BYTE *new_header_tile_data; |
2812 | | /*p_j2k->m_specific_param.m_encoder.m_header_tile_data |
2813 | | = (OPJ_BYTE*)opj_realloc( |
2814 | | p_j2k->m_specific_param.m_encoder.m_header_tile_data, |
2815 | | l_coc_size);*/ |
2816 | |
|
2817 | 0 | new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
2818 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size); |
2819 | 0 | if (! new_header_tile_data) { |
2820 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2821 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2822 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2823 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n"); |
2824 | 0 | return OPJ_FALSE; |
2825 | 0 | } |
2826 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2827 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; |
2828 | 0 | } |
2829 | | |
2830 | 0 | opj_j2k_write_coc_in_memory(p_j2k, p_comp_no, |
2831 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, |
2832 | 0 | p_manager); |
2833 | |
|
2834 | 0 | if (opj_stream_write_data(p_stream, |
2835 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size, |
2836 | 0 | p_manager) != l_coc_size) { |
2837 | 0 | return OPJ_FALSE; |
2838 | 0 | } |
2839 | | |
2840 | 0 | return OPJ_TRUE; |
2841 | 0 | } |
2842 | | |
2843 | | static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, |
2844 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
2845 | 0 | { |
2846 | 0 | opj_cp_t *l_cp = NULL; |
2847 | 0 | opj_tcp_t *l_tcp = NULL; |
2848 | | |
2849 | | /* preconditions */ |
2850 | 0 | assert(p_j2k != 00); |
2851 | |
|
2852 | 0 | l_cp = &(p_j2k->m_cp); |
2853 | 0 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2854 | |
|
2855 | 0 | if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) { |
2856 | 0 | return OPJ_FALSE; |
2857 | 0 | } |
2858 | | |
2859 | | |
2860 | 0 | return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, |
2861 | 0 | p_first_comp_no, p_second_comp_no); |
2862 | 0 | } |
2863 | | |
2864 | | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, |
2865 | | OPJ_UINT32 p_comp_no, |
2866 | | OPJ_BYTE * p_data, |
2867 | | OPJ_UINT32 * p_data_written, |
2868 | | opj_event_mgr_t * p_manager |
2869 | | ) |
2870 | 0 | { |
2871 | 0 | opj_cp_t *l_cp = 00; |
2872 | 0 | opj_tcp_t *l_tcp = 00; |
2873 | 0 | OPJ_UINT32 l_coc_size, l_remaining_size; |
2874 | 0 | OPJ_BYTE * l_current_data = 00; |
2875 | 0 | opj_image_t *l_image = 00; |
2876 | 0 | OPJ_UINT32 l_comp_room; |
2877 | | |
2878 | | /* preconditions */ |
2879 | 0 | assert(p_j2k != 00); |
2880 | 0 | assert(p_manager != 00); |
2881 | |
|
2882 | 0 | l_cp = &(p_j2k->m_cp); |
2883 | 0 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2884 | 0 | l_image = p_j2k->m_private_image; |
2885 | 0 | l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; |
2886 | |
|
2887 | 0 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2888 | 0 | p_j2k->m_current_tile_number, p_comp_no); |
2889 | 0 | l_remaining_size = l_coc_size; |
2890 | |
|
2891 | 0 | l_current_data = p_data; |
2892 | |
|
2893 | 0 | opj_write_bytes(l_current_data, J2K_MS_COC, |
2894 | 0 | 2); /* COC */ |
2895 | 0 | l_current_data += 2; |
2896 | |
|
2897 | 0 | opj_write_bytes(l_current_data, l_coc_size - 2, |
2898 | 0 | 2); /* L_COC */ |
2899 | 0 | l_current_data += 2; |
2900 | |
|
2901 | 0 | opj_write_bytes(l_current_data, p_comp_no, l_comp_room); /* Ccoc */ |
2902 | 0 | l_current_data += l_comp_room; |
2903 | |
|
2904 | 0 | opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, |
2905 | 0 | 1); /* Scoc */ |
2906 | 0 | ++l_current_data; |
2907 | |
|
2908 | 0 | l_remaining_size -= (5 + l_comp_room); |
2909 | 0 | opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, |
2910 | 0 | l_current_data, &l_remaining_size, p_manager); |
2911 | 0 | * p_data_written = l_coc_size; |
2912 | 0 | } |
2913 | | |
2914 | | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k) |
2915 | 0 | { |
2916 | 0 | OPJ_UINT32 i, j; |
2917 | 0 | OPJ_UINT32 l_nb_comp; |
2918 | 0 | OPJ_UINT32 l_nb_tiles; |
2919 | 0 | OPJ_UINT32 l_max = 0; |
2920 | | |
2921 | | /* preconditions */ |
2922 | |
|
2923 | 0 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
2924 | 0 | l_nb_comp = p_j2k->m_private_image->numcomps; |
2925 | |
|
2926 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
2927 | 0 | for (j = 0; j < l_nb_comp; ++j) { |
2928 | 0 | l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j)); |
2929 | 0 | } |
2930 | 0 | } |
2931 | |
|
2932 | 0 | return 6 + l_max; |
2933 | 0 | } |
2934 | | |
2935 | | /** |
2936 | | * Reads a COC marker (Coding Style Component) |
2937 | | * @param p_header_data the data contained in the COC box. |
2938 | | * @param p_j2k the jpeg2000 codec. |
2939 | | * @param p_header_size the size of the data contained in the COC marker. |
2940 | | * @param p_manager the user event manager. |
2941 | | */ |
2942 | | static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, |
2943 | | OPJ_BYTE * p_header_data, |
2944 | | OPJ_UINT32 p_header_size, |
2945 | | opj_event_mgr_t * p_manager |
2946 | | ) |
2947 | 298 | { |
2948 | 298 | opj_cp_t *l_cp = NULL; |
2949 | 298 | opj_tcp_t *l_tcp = NULL; |
2950 | 298 | opj_image_t *l_image = NULL; |
2951 | 298 | OPJ_UINT32 l_comp_room; |
2952 | 298 | OPJ_UINT32 l_comp_no; |
2953 | | |
2954 | | /* preconditions */ |
2955 | 298 | assert(p_header_data != 00); |
2956 | 298 | assert(p_j2k != 00); |
2957 | 298 | assert(p_manager != 00); |
2958 | | |
2959 | 298 | l_cp = &(p_j2k->m_cp); |
2960 | 298 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
2961 | 298 | ? |
2962 | 1 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2963 | 298 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
2964 | 298 | l_image = p_j2k->m_private_image; |
2965 | | |
2966 | 298 | l_comp_room = l_image->numcomps <= 256 ? 1 : 2; |
2967 | | |
2968 | | /* make sure room is sufficient*/ |
2969 | 298 | if (p_header_size < l_comp_room + 1) { |
2970 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2971 | 1 | return OPJ_FALSE; |
2972 | 1 | } |
2973 | 297 | p_header_size -= l_comp_room + 1; |
2974 | | |
2975 | 297 | opj_read_bytes(p_header_data, &l_comp_no, |
2976 | 297 | l_comp_room); /* Ccoc */ |
2977 | 297 | p_header_data += l_comp_room; |
2978 | 297 | if (l_comp_no >= l_image->numcomps) { |
2979 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2980 | 0 | "Error reading COC marker (bad number of components)\n"); |
2981 | 0 | return OPJ_FALSE; |
2982 | 0 | } |
2983 | | |
2984 | 297 | opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty, |
2985 | 297 | 1); /* Scoc */ |
2986 | 297 | ++p_header_data ; |
2987 | | |
2988 | 297 | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
2989 | 297 | p_manager)) { |
2990 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2991 | 5 | return OPJ_FALSE; |
2992 | 5 | } |
2993 | | |
2994 | 292 | if (p_header_size != 0) { |
2995 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2996 | 1 | return OPJ_FALSE; |
2997 | 1 | } |
2998 | 291 | return OPJ_TRUE; |
2999 | 292 | } |
3000 | | |
3001 | | static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, |
3002 | | opj_stream_private_t *p_stream, |
3003 | | opj_event_mgr_t * p_manager |
3004 | | ) |
3005 | 0 | { |
3006 | 0 | OPJ_UINT32 l_qcd_size, l_remaining_size; |
3007 | 0 | OPJ_BYTE * l_current_data = 00; |
3008 | | |
3009 | | /* preconditions */ |
3010 | 0 | assert(p_j2k != 00); |
3011 | 0 | assert(p_manager != 00); |
3012 | 0 | assert(p_stream != 00); |
3013 | |
|
3014 | 0 | l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3015 | 0 | 0); |
3016 | 0 | l_remaining_size = l_qcd_size; |
3017 | |
|
3018 | 0 | if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3019 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
3020 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size); |
3021 | 0 | if (! new_header_tile_data) { |
3022 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3023 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3024 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3025 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n"); |
3026 | 0 | return OPJ_FALSE; |
3027 | 0 | } |
3028 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3029 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; |
3030 | 0 | } |
3031 | | |
3032 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
3033 | |
|
3034 | 0 | opj_write_bytes(l_current_data, J2K_MS_QCD, 2); /* QCD */ |
3035 | 0 | l_current_data += 2; |
3036 | |
|
3037 | 0 | opj_write_bytes(l_current_data, l_qcd_size - 2, 2); /* L_QCD */ |
3038 | 0 | l_current_data += 2; |
3039 | |
|
3040 | 0 | l_remaining_size -= 4; |
3041 | |
|
3042 | 0 | if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0, |
3043 | 0 | l_current_data, &l_remaining_size, p_manager)) { |
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 (l_remaining_size != 0) { |
3049 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); |
3050 | 0 | return OPJ_FALSE; |
3051 | 0 | } |
3052 | | |
3053 | 0 | if (opj_stream_write_data(p_stream, |
3054 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size, |
3055 | 0 | p_manager) != l_qcd_size) { |
3056 | 0 | return OPJ_FALSE; |
3057 | 0 | } |
3058 | | |
3059 | 0 | return OPJ_TRUE; |
3060 | 0 | } |
3061 | | |
3062 | | /** |
3063 | | * Reads a QCD marker (Quantization defaults) |
3064 | | * @param p_header_data the data contained in the QCD box. |
3065 | | * @param p_j2k the jpeg2000 codec. |
3066 | | * @param p_header_size the size of the data contained in the QCD marker. |
3067 | | * @param p_manager the user event manager. |
3068 | | */ |
3069 | | static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, |
3070 | | OPJ_BYTE * p_header_data, |
3071 | | OPJ_UINT32 p_header_size, |
3072 | | opj_event_mgr_t * p_manager |
3073 | | ) |
3074 | 22.6k | { |
3075 | | /* preconditions */ |
3076 | 22.6k | assert(p_header_data != 00); |
3077 | 22.6k | assert(p_j2k != 00); |
3078 | 22.6k | assert(p_manager != 00); |
3079 | | |
3080 | 22.6k | if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size, |
3081 | 22.6k | p_manager)) { |
3082 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
3083 | 0 | return OPJ_FALSE; |
3084 | 0 | } |
3085 | | |
3086 | 22.6k | if (p_header_size != 0) { |
3087 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
3088 | 12 | return OPJ_FALSE; |
3089 | 12 | } |
3090 | | |
3091 | | /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ |
3092 | 22.6k | opj_j2k_copy_tile_quantization_parameters(p_j2k); |
3093 | | |
3094 | 22.6k | return OPJ_TRUE; |
3095 | 22.6k | } |
3096 | | |
3097 | | static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, |
3098 | | OPJ_UINT32 p_comp_no, |
3099 | | opj_stream_private_t *p_stream, |
3100 | | opj_event_mgr_t * p_manager |
3101 | | ) |
3102 | 0 | { |
3103 | 0 | OPJ_UINT32 l_qcc_size, l_remaining_size; |
3104 | | |
3105 | | /* preconditions */ |
3106 | 0 | assert(p_j2k != 00); |
3107 | 0 | assert(p_manager != 00); |
3108 | 0 | assert(p_stream != 00); |
3109 | |
|
3110 | 0 | l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3111 | 0 | p_comp_no); |
3112 | 0 | l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1; |
3113 | 0 | l_remaining_size = l_qcc_size; |
3114 | |
|
3115 | 0 | if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3116 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
3117 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size); |
3118 | 0 | if (! new_header_tile_data) { |
3119 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3120 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3121 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3122 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n"); |
3123 | 0 | return OPJ_FALSE; |
3124 | 0 | } |
3125 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3126 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; |
3127 | 0 | } |
3128 | | |
3129 | 0 | opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no, |
3130 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, |
3131 | 0 | p_manager); |
3132 | |
|
3133 | 0 | if (opj_stream_write_data(p_stream, |
3134 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size, |
3135 | 0 | p_manager) != l_qcc_size) { |
3136 | 0 | return OPJ_FALSE; |
3137 | 0 | } |
3138 | | |
3139 | 0 | return OPJ_TRUE; |
3140 | 0 | } |
3141 | | |
3142 | | static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, |
3143 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
3144 | 0 | { |
3145 | 0 | return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, |
3146 | 0 | p_first_comp_no, p_second_comp_no); |
3147 | 0 | } |
3148 | | |
3149 | | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, |
3150 | | OPJ_UINT32 p_comp_no, |
3151 | | OPJ_BYTE * p_data, |
3152 | | OPJ_UINT32 * p_data_written, |
3153 | | opj_event_mgr_t * p_manager |
3154 | | ) |
3155 | 0 | { |
3156 | 0 | OPJ_UINT32 l_qcc_size, l_remaining_size; |
3157 | 0 | OPJ_BYTE * l_current_data = 00; |
3158 | | |
3159 | | /* preconditions */ |
3160 | 0 | assert(p_j2k != 00); |
3161 | 0 | assert(p_manager != 00); |
3162 | |
|
3163 | 0 | l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3164 | 0 | p_comp_no); |
3165 | 0 | l_remaining_size = l_qcc_size; |
3166 | |
|
3167 | 0 | l_current_data = p_data; |
3168 | |
|
3169 | 0 | opj_write_bytes(l_current_data, J2K_MS_QCC, 2); /* QCC */ |
3170 | 0 | l_current_data += 2; |
3171 | |
|
3172 | 0 | if (p_j2k->m_private_image->numcomps <= 256) { |
3173 | 0 | --l_qcc_size; |
3174 | |
|
3175 | 0 | opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ |
3176 | 0 | l_current_data += 2; |
3177 | |
|
3178 | 0 | opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ |
3179 | 0 | ++l_current_data; |
3180 | | |
3181 | | /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */ |
3182 | 0 | l_remaining_size -= 6; |
3183 | 0 | } else { |
3184 | 0 | opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ |
3185 | 0 | l_current_data += 2; |
3186 | |
|
3187 | 0 | opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ |
3188 | 0 | l_current_data += 2; |
3189 | |
|
3190 | 0 | l_remaining_size -= 6; |
3191 | 0 | } |
3192 | |
|
3193 | 0 | opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no, |
3194 | 0 | l_current_data, &l_remaining_size, p_manager); |
3195 | |
|
3196 | 0 | *p_data_written = l_qcc_size; |
3197 | 0 | } |
3198 | | |
3199 | | static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k) |
3200 | 0 | { |
3201 | 0 | return opj_j2k_get_max_coc_size(p_j2k); |
3202 | 0 | } |
3203 | | |
3204 | | /** |
3205 | | * Reads a QCC marker (Quantization component) |
3206 | | * @param p_header_data the data contained in the QCC box. |
3207 | | * @param p_j2k the jpeg2000 codec. |
3208 | | * @param p_header_size the size of the data contained in the QCC marker. |
3209 | | * @param p_manager the user event manager. |
3210 | | */ |
3211 | | static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, |
3212 | | OPJ_BYTE * p_header_data, |
3213 | | OPJ_UINT32 p_header_size, |
3214 | | opj_event_mgr_t * p_manager |
3215 | | ) |
3216 | 504 | { |
3217 | 504 | OPJ_UINT32 l_num_comp, l_comp_no; |
3218 | | |
3219 | | /* preconditions */ |
3220 | 504 | assert(p_header_data != 00); |
3221 | 504 | assert(p_j2k != 00); |
3222 | 504 | assert(p_manager != 00); |
3223 | | |
3224 | 504 | l_num_comp = p_j2k->m_private_image->numcomps; |
3225 | | |
3226 | 504 | if (l_num_comp <= 256) { |
3227 | 504 | if (p_header_size < 1) { |
3228 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3229 | 0 | return OPJ_FALSE; |
3230 | 0 | } |
3231 | 504 | opj_read_bytes(p_header_data, &l_comp_no, 1); |
3232 | 504 | ++p_header_data; |
3233 | 504 | --p_header_size; |
3234 | 504 | } else { |
3235 | 0 | if (p_header_size < 2) { |
3236 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3237 | 0 | return OPJ_FALSE; |
3238 | 0 | } |
3239 | 0 | opj_read_bytes(p_header_data, &l_comp_no, 2); |
3240 | 0 | p_header_data += 2; |
3241 | 0 | p_header_size -= 2; |
3242 | 0 | } |
3243 | | |
3244 | | #ifdef USE_JPWL |
3245 | | if (p_j2k->m_cp.correct) { |
3246 | | |
3247 | | static OPJ_UINT32 backup_compno = 0; |
3248 | | |
3249 | | /* compno is negative or larger than the number of components!!! */ |
3250 | | if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { |
3251 | | opj_event_msg(p_manager, EVT_ERROR, |
3252 | | "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", |
3253 | | l_comp_no, l_num_comp); |
3254 | | if (!JPWL_ASSUME) { |
3255 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
3256 | | return OPJ_FALSE; |
3257 | | } |
3258 | | /* we try to correct */ |
3259 | | l_comp_no = backup_compno % l_num_comp; |
3260 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
3261 | | "- setting component number to %d\n", |
3262 | | l_comp_no); |
3263 | | } |
3264 | | |
3265 | | /* keep your private count of tiles */ |
3266 | | backup_compno++; |
3267 | | }; |
3268 | | #endif /* USE_JPWL */ |
3269 | | |
3270 | 504 | if (l_comp_no >= p_j2k->m_private_image->numcomps) { |
3271 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
3272 | 1 | "Invalid component number: %d, regarding the number of components %d\n", |
3273 | 1 | l_comp_no, p_j2k->m_private_image->numcomps); |
3274 | 1 | return OPJ_FALSE; |
3275 | 1 | } |
3276 | | |
3277 | 503 | if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
3278 | 503 | p_manager)) { |
3279 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3280 | 1 | return OPJ_FALSE; |
3281 | 1 | } |
3282 | | |
3283 | 502 | if (p_header_size != 0) { |
3284 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3285 | 1 | return OPJ_FALSE; |
3286 | 1 | } |
3287 | | |
3288 | 501 | return OPJ_TRUE; |
3289 | 502 | } |
3290 | | |
3291 | | static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, |
3292 | | opj_stream_private_t *p_stream, |
3293 | | opj_event_mgr_t * p_manager |
3294 | | ) |
3295 | 0 | { |
3296 | 0 | OPJ_UINT32 l_nb_comp; |
3297 | 0 | OPJ_UINT32 l_nb_poc; |
3298 | 0 | OPJ_UINT32 l_poc_size; |
3299 | 0 | OPJ_UINT32 l_written_size = 0; |
3300 | 0 | opj_tcp_t *l_tcp = 00; |
3301 | 0 | OPJ_UINT32 l_poc_room; |
3302 | | |
3303 | | /* preconditions */ |
3304 | 0 | assert(p_j2k != 00); |
3305 | 0 | assert(p_manager != 00); |
3306 | 0 | assert(p_stream != 00); |
3307 | |
|
3308 | 0 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
3309 | 0 | l_nb_comp = p_j2k->m_private_image->numcomps; |
3310 | 0 | l_nb_poc = 1 + l_tcp->numpocs; |
3311 | |
|
3312 | 0 | if (l_nb_comp <= 256) { |
3313 | 0 | l_poc_room = 1; |
3314 | 0 | } else { |
3315 | 0 | l_poc_room = 2; |
3316 | 0 | } |
3317 | 0 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
3318 | |
|
3319 | 0 | if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3320 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
3321 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size); |
3322 | 0 | if (! new_header_tile_data) { |
3323 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3324 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3325 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3326 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n"); |
3327 | 0 | return OPJ_FALSE; |
3328 | 0 | } |
3329 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3330 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; |
3331 | 0 | } |
3332 | | |
3333 | 0 | opj_j2k_write_poc_in_memory(p_j2k, |
3334 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size, |
3335 | 0 | p_manager); |
3336 | |
|
3337 | 0 | if (opj_stream_write_data(p_stream, |
3338 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size, |
3339 | 0 | p_manager) != l_poc_size) { |
3340 | 0 | return OPJ_FALSE; |
3341 | 0 | } |
3342 | | |
3343 | 0 | return OPJ_TRUE; |
3344 | 0 | } |
3345 | | |
3346 | | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, |
3347 | | OPJ_BYTE * p_data, |
3348 | | OPJ_UINT32 * p_data_written, |
3349 | | opj_event_mgr_t * p_manager |
3350 | | ) |
3351 | 0 | { |
3352 | 0 | OPJ_UINT32 i; |
3353 | 0 | OPJ_BYTE * l_current_data = 00; |
3354 | 0 | OPJ_UINT32 l_nb_comp; |
3355 | 0 | OPJ_UINT32 l_nb_poc; |
3356 | 0 | OPJ_UINT32 l_poc_size; |
3357 | 0 | opj_image_t *l_image = 00; |
3358 | 0 | opj_tcp_t *l_tcp = 00; |
3359 | 0 | opj_tccp_t *l_tccp = 00; |
3360 | 0 | opj_poc_t *l_current_poc = 00; |
3361 | 0 | OPJ_UINT32 l_poc_room; |
3362 | | |
3363 | | /* preconditions */ |
3364 | 0 | assert(p_j2k != 00); |
3365 | 0 | assert(p_manager != 00); |
3366 | |
|
3367 | 0 | OPJ_UNUSED(p_manager); |
3368 | |
|
3369 | 0 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
3370 | 0 | l_tccp = &l_tcp->tccps[0]; |
3371 | 0 | l_image = p_j2k->m_private_image; |
3372 | 0 | l_nb_comp = l_image->numcomps; |
3373 | 0 | l_nb_poc = 1 + l_tcp->numpocs; |
3374 | |
|
3375 | 0 | if (l_nb_comp <= 256) { |
3376 | 0 | l_poc_room = 1; |
3377 | 0 | } else { |
3378 | 0 | l_poc_room = 2; |
3379 | 0 | } |
3380 | |
|
3381 | 0 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
3382 | |
|
3383 | 0 | l_current_data = p_data; |
3384 | |
|
3385 | 0 | opj_write_bytes(l_current_data, J2K_MS_POC, |
3386 | 0 | 2); /* POC */ |
3387 | 0 | l_current_data += 2; |
3388 | |
|
3389 | 0 | opj_write_bytes(l_current_data, l_poc_size - 2, |
3390 | 0 | 2); /* Lpoc */ |
3391 | 0 | l_current_data += 2; |
3392 | |
|
3393 | 0 | l_current_poc = l_tcp->pocs; |
3394 | 0 | for (i = 0; i < l_nb_poc; ++i) { |
3395 | 0 | opj_write_bytes(l_current_data, l_current_poc->resno0, |
3396 | 0 | 1); /* RSpoc_i */ |
3397 | 0 | ++l_current_data; |
3398 | |
|
3399 | 0 | opj_write_bytes(l_current_data, l_current_poc->compno0, |
3400 | 0 | l_poc_room); /* CSpoc_i */ |
3401 | 0 | l_current_data += l_poc_room; |
3402 | |
|
3403 | 0 | opj_write_bytes(l_current_data, l_current_poc->layno1, |
3404 | 0 | 2); /* LYEpoc_i */ |
3405 | 0 | l_current_data += 2; |
3406 | |
|
3407 | 0 | opj_write_bytes(l_current_data, l_current_poc->resno1, |
3408 | 0 | 1); /* REpoc_i */ |
3409 | 0 | ++l_current_data; |
3410 | |
|
3411 | 0 | opj_write_bytes(l_current_data, l_current_poc->compno1, |
3412 | 0 | l_poc_room); /* CEpoc_i */ |
3413 | 0 | l_current_data += l_poc_room; |
3414 | |
|
3415 | 0 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg, |
3416 | 0 | 1); /* Ppoc_i */ |
3417 | 0 | ++l_current_data; |
3418 | | |
3419 | | /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ |
3420 | 0 | l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3421 | 0 | l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers); |
3422 | 0 | l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3423 | 0 | l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions); |
3424 | 0 | l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3425 | 0 | l_current_poc->compno1, (OPJ_INT32)l_nb_comp); |
3426 | |
|
3427 | 0 | ++l_current_poc; |
3428 | 0 | } |
3429 | |
|
3430 | 0 | *p_data_written = l_poc_size; |
3431 | 0 | } |
3432 | | |
3433 | | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k) |
3434 | 0 | { |
3435 | 0 | opj_tcp_t * l_tcp = 00; |
3436 | 0 | OPJ_UINT32 l_nb_tiles = 0; |
3437 | 0 | OPJ_UINT32 l_max_poc = 0; |
3438 | 0 | OPJ_UINT32 i; |
3439 | |
|
3440 | 0 | l_tcp = p_j2k->m_cp.tcps; |
3441 | 0 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
3442 | |
|
3443 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
3444 | 0 | l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs); |
3445 | 0 | ++l_tcp; |
3446 | 0 | } |
3447 | |
|
3448 | 0 | ++l_max_poc; |
3449 | |
|
3450 | 0 | return 4 + 9 * l_max_poc; |
3451 | 0 | } |
3452 | | |
3453 | | static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k) |
3454 | 0 | { |
3455 | 0 | OPJ_UINT32 i; |
3456 | 0 | OPJ_UINT32 l_nb_tiles; |
3457 | 0 | OPJ_UINT32 l_max = 0; |
3458 | 0 | opj_tcp_t * l_tcp = 00; |
3459 | |
|
3460 | 0 | l_tcp = p_j2k->m_cp.tcps; |
3461 | 0 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
3462 | |
|
3463 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
3464 | 0 | l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts); |
3465 | |
|
3466 | 0 | ++l_tcp; |
3467 | 0 | } |
3468 | |
|
3469 | 0 | return 12 * l_max; |
3470 | 0 | } |
3471 | | |
3472 | | static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k) |
3473 | 0 | { |
3474 | 0 | OPJ_UINT32 l_nb_bytes = 0; |
3475 | 0 | OPJ_UINT32 l_nb_comps; |
3476 | 0 | OPJ_UINT32 l_coc_bytes, l_qcc_bytes; |
3477 | |
|
3478 | 0 | l_nb_comps = p_j2k->m_private_image->numcomps - 1; |
3479 | 0 | l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k); |
3480 | |
|
3481 | 0 | if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) { |
3482 | 0 | l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k); |
3483 | 0 | l_nb_bytes += l_nb_comps * l_coc_bytes; |
3484 | |
|
3485 | 0 | l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k); |
3486 | 0 | l_nb_bytes += l_nb_comps * l_qcc_bytes; |
3487 | 0 | } |
3488 | |
|
3489 | 0 | l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k); |
3490 | |
|
3491 | 0 | if (p_j2k->m_specific_param.m_encoder.m_PLT) { |
3492 | | /* Reserve space for PLT markers */ |
3493 | |
|
3494 | 0 | OPJ_UINT32 i; |
3495 | 0 | const opj_cp_t * l_cp = &(p_j2k->m_cp); |
3496 | 0 | OPJ_UINT32 l_max_packet_count = 0; |
3497 | 0 | for (i = 0; i < l_cp->th * l_cp->tw; ++i) { |
3498 | 0 | l_max_packet_count = opj_uint_max(l_max_packet_count, |
3499 | 0 | opj_get_encoding_packet_count(p_j2k->m_private_image, l_cp, i)); |
3500 | 0 | } |
3501 | | /* Minimum 6 bytes per PLT marker, and at a minimum (taking a pessimistic */ |
3502 | | /* estimate of 4 bytes for a packet size), one can write */ |
3503 | | /* (65536-6) / 4 = 16382 paquet sizes per PLT marker */ |
3504 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT = |
3505 | 0 | 6 * opj_uint_ceildiv(l_max_packet_count, 16382); |
3506 | | /* Maximum 5 bytes per packet to encode a full UINT32 */ |
3507 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += |
3508 | 0 | l_nb_bytes += 5 * l_max_packet_count; |
3509 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += 1; |
3510 | 0 | l_nb_bytes += p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT; |
3511 | 0 | } |
3512 | | |
3513 | | /*** DEVELOPER CORNER, Add room for your headers ***/ |
3514 | |
|
3515 | 0 | return l_nb_bytes; |
3516 | 0 | } |
3517 | | |
3518 | | /** |
3519 | | * Reads a POC marker (Progression Order Change) |
3520 | | * |
3521 | | * @param p_header_data the data contained in the POC box. |
3522 | | * @param p_j2k the jpeg2000 codec. |
3523 | | * @param p_header_size the size of the data contained in the POC marker. |
3524 | | * @param p_manager the user event manager. |
3525 | | */ |
3526 | | static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, |
3527 | | OPJ_BYTE * p_header_data, |
3528 | | OPJ_UINT32 p_header_size, |
3529 | | opj_event_mgr_t * p_manager |
3530 | | ) |
3531 | 1.58k | { |
3532 | 1.58k | OPJ_UINT32 i, l_nb_comp, l_tmp; |
3533 | 1.58k | opj_image_t * l_image = 00; |
3534 | 1.58k | OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; |
3535 | 1.58k | OPJ_UINT32 l_chunk_size, l_comp_room; |
3536 | | |
3537 | 1.58k | opj_cp_t *l_cp = 00; |
3538 | 1.58k | opj_tcp_t *l_tcp = 00; |
3539 | 1.58k | opj_poc_t *l_current_poc = 00; |
3540 | | |
3541 | | /* preconditions */ |
3542 | 1.58k | assert(p_header_data != 00); |
3543 | 1.58k | assert(p_j2k != 00); |
3544 | 1.58k | assert(p_manager != 00); |
3545 | | |
3546 | 1.58k | l_image = p_j2k->m_private_image; |
3547 | 1.58k | l_nb_comp = l_image->numcomps; |
3548 | 1.58k | if (l_nb_comp <= 256) { |
3549 | 1.58k | l_comp_room = 1; |
3550 | 1.58k | } else { |
3551 | 0 | l_comp_room = 2; |
3552 | 0 | } |
3553 | 1.58k | l_chunk_size = 5 + 2 * l_comp_room; |
3554 | 1.58k | l_current_poc_nb = p_header_size / l_chunk_size; |
3555 | 1.58k | l_current_poc_remaining = p_header_size % l_chunk_size; |
3556 | | |
3557 | 1.58k | if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { |
3558 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n"); |
3559 | 0 | return OPJ_FALSE; |
3560 | 0 | } |
3561 | | |
3562 | 1.58k | l_cp = &(p_j2k->m_cp); |
3563 | 1.58k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
3564 | 10 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
3565 | 1.58k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
3566 | 1.58k | l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; |
3567 | 1.58k | l_current_poc_nb += l_old_poc_nb; |
3568 | | |
3569 | 1.58k | if (l_current_poc_nb >= J2K_MAX_POCS) { |
3570 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb); |
3571 | 5 | return OPJ_FALSE; |
3572 | 5 | } |
3573 | | |
3574 | | /* now poc is in use.*/ |
3575 | 1.57k | l_tcp->POC = 1; |
3576 | | |
3577 | 1.57k | l_current_poc = &l_tcp->pocs[l_old_poc_nb]; |
3578 | 4.24k | for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { |
3579 | 2.67k | opj_read_bytes(p_header_data, &(l_current_poc->resno0), |
3580 | 2.67k | 1); /* RSpoc_i */ |
3581 | 2.67k | ++p_header_data; |
3582 | 2.67k | opj_read_bytes(p_header_data, &(l_current_poc->compno0), |
3583 | 2.67k | l_comp_room); /* CSpoc_i */ |
3584 | 2.67k | p_header_data += l_comp_room; |
3585 | 2.67k | opj_read_bytes(p_header_data, &(l_current_poc->layno1), |
3586 | 2.67k | 2); /* LYEpoc_i */ |
3587 | | /* make sure layer end is in acceptable bounds */ |
3588 | 2.67k | l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers); |
3589 | 2.67k | p_header_data += 2; |
3590 | 2.67k | opj_read_bytes(p_header_data, &(l_current_poc->resno1), |
3591 | 2.67k | 1); /* REpoc_i */ |
3592 | 2.67k | ++p_header_data; |
3593 | 2.67k | opj_read_bytes(p_header_data, &(l_current_poc->compno1), |
3594 | 2.67k | l_comp_room); /* CEpoc_i */ |
3595 | 2.67k | p_header_data += l_comp_room; |
3596 | 2.67k | opj_read_bytes(p_header_data, &l_tmp, |
3597 | 2.67k | 1); /* Ppoc_i */ |
3598 | 2.67k | ++p_header_data; |
3599 | 2.67k | l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; |
3600 | | /* make sure comp is in acceptable bounds */ |
3601 | 2.67k | l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp); |
3602 | 2.67k | ++l_current_poc; |
3603 | 2.67k | } |
3604 | | |
3605 | 1.57k | l_tcp->numpocs = l_current_poc_nb - 1; |
3606 | 1.57k | return OPJ_TRUE; |
3607 | 1.58k | } |
3608 | | |
3609 | | /** |
3610 | | * Reads a CRG marker (Component registration) |
3611 | | * |
3612 | | * @param p_header_data the data contained in the TLM box. |
3613 | | * @param p_j2k the jpeg2000 codec. |
3614 | | * @param p_header_size the size of the data contained in the TLM marker. |
3615 | | * @param p_manager the user event manager. |
3616 | | */ |
3617 | | static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, |
3618 | | OPJ_BYTE * p_header_data, |
3619 | | OPJ_UINT32 p_header_size, |
3620 | | opj_event_mgr_t * p_manager |
3621 | | ) |
3622 | 102 | { |
3623 | 102 | OPJ_UINT32 l_nb_comp; |
3624 | | /* preconditions */ |
3625 | 102 | assert(p_header_data != 00); |
3626 | 102 | assert(p_j2k != 00); |
3627 | 102 | assert(p_manager != 00); |
3628 | | |
3629 | 102 | OPJ_UNUSED(p_header_data); |
3630 | | |
3631 | 102 | l_nb_comp = p_j2k->m_private_image->numcomps; |
3632 | | |
3633 | 102 | if (p_header_size != l_nb_comp * 4) { |
3634 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n"); |
3635 | 0 | return OPJ_FALSE; |
3636 | 0 | } |
3637 | | /* Do not care of this at the moment since only local variables are set here */ |
3638 | | /* |
3639 | | for |
3640 | | (i = 0; i < l_nb_comp; ++i) |
3641 | | { |
3642 | | opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i |
3643 | | p_header_data+=2; |
3644 | | opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i |
3645 | | p_header_data+=2; |
3646 | | } |
3647 | | */ |
3648 | 102 | return OPJ_TRUE; |
3649 | 102 | } |
3650 | | |
3651 | | /** |
3652 | | * Reads a TLM marker (Tile Length Marker) |
3653 | | * |
3654 | | * @param p_header_data the data contained in the TLM box. |
3655 | | * @param p_j2k the jpeg2000 codec. |
3656 | | * @param p_header_size the size of the data contained in the TLM marker. |
3657 | | * @param p_manager the user event manager. |
3658 | | */ |
3659 | | static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, |
3660 | | OPJ_BYTE * p_header_data, |
3661 | | OPJ_UINT32 p_header_size, |
3662 | | opj_event_mgr_t * p_manager |
3663 | | ) |
3664 | 4.99k | { |
3665 | 4.99k | OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, |
3666 | 4.99k | l_Ptlm_size, l_entry_size, l_num_tileparts; |
3667 | 4.99k | OPJ_UINT32 i; |
3668 | 4.99k | opj_j2k_tlm_tile_part_info_t* l_tile_part_infos; |
3669 | 4.99k | opj_j2k_tlm_info_t* l_tlm; |
3670 | | |
3671 | | /* preconditions */ |
3672 | 4.99k | assert(p_header_data != 00); |
3673 | 4.99k | assert(p_j2k != 00); |
3674 | 4.99k | assert(p_manager != 00); |
3675 | | |
3676 | 4.99k | l_tlm = &(p_j2k->m_specific_param.m_decoder.m_tlm); |
3677 | | |
3678 | 4.99k | if (p_header_size < 2) { |
3679 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker.\n"); |
3680 | 0 | return OPJ_FALSE; |
3681 | 0 | } |
3682 | 4.99k | p_header_size -= 2; |
3683 | | |
3684 | 4.99k | if (l_tlm->m_is_invalid) { |
3685 | 1.74k | return OPJ_TRUE; |
3686 | 1.74k | } |
3687 | | |
3688 | 3.24k | opj_read_bytes(p_header_data, &l_Ztlm, |
3689 | 3.24k | 1); /* Ztlm */ |
3690 | 3.24k | ++p_header_data; |
3691 | 3.24k | opj_read_bytes(p_header_data, &l_Stlm, |
3692 | 3.24k | 1); /* Stlm */ |
3693 | 3.24k | ++p_header_data; |
3694 | | |
3695 | 3.24k | l_ST = ((l_Stlm >> 4) & 0x3); |
3696 | 3.24k | if (l_ST == 3) { |
3697 | 1 | l_tlm->m_is_invalid = OPJ_TRUE; |
3698 | 1 | opj_event_msg(p_manager, EVT_WARNING, |
3699 | 1 | "opj_j2k_read_tlm(): ST = 3 is invalid.\n"); |
3700 | 1 | return OPJ_TRUE; |
3701 | 1 | } |
3702 | 3.24k | l_SP = (l_Stlm >> 6) & 0x1; |
3703 | | |
3704 | 3.24k | l_Ptlm_size = (l_SP + 1) * 2; |
3705 | 3.24k | l_entry_size = l_Ptlm_size + l_ST; |
3706 | | |
3707 | 3.24k | if ((p_header_size % l_entry_size) != 0) { |
3708 | 4 | l_tlm->m_is_invalid = OPJ_TRUE; |
3709 | 4 | opj_event_msg(p_manager, EVT_WARNING, |
3710 | 4 | "opj_j2k_read_tlm(): TLM marker not of expected size.\n"); |
3711 | 4 | return OPJ_TRUE; |
3712 | 4 | } |
3713 | | |
3714 | 3.23k | l_num_tileparts = p_header_size / l_entry_size; |
3715 | 3.23k | if (l_num_tileparts == 0) { |
3716 | | /* not totally sure if this is valid... */ |
3717 | 235 | return OPJ_TRUE; |
3718 | 235 | } |
3719 | | |
3720 | | /* Highly unlikely, unless there are gazillions of TLM markers */ |
3721 | 3.00k | if (l_tlm->m_entries_count > UINT32_MAX - l_num_tileparts || |
3722 | 3.00k | l_tlm->m_entries_count + l_num_tileparts > UINT32_MAX / sizeof( |
3723 | 3.00k | opj_j2k_tlm_tile_part_info_t)) { |
3724 | 0 | l_tlm->m_is_invalid = OPJ_TRUE; |
3725 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
3726 | 0 | "opj_j2k_read_tlm(): too many TLM markers.\n"); |
3727 | 0 | return OPJ_TRUE; |
3728 | 0 | } |
3729 | | |
3730 | 3.00k | l_tile_part_infos = (opj_j2k_tlm_tile_part_info_t*)opj_realloc( |
3731 | 3.00k | l_tlm->m_tile_part_infos, |
3732 | 3.00k | (l_tlm->m_entries_count + l_num_tileparts) * sizeof( |
3733 | 3.00k | opj_j2k_tlm_tile_part_info_t)); |
3734 | 3.00k | if (!l_tile_part_infos) { |
3735 | 0 | l_tlm->m_is_invalid = OPJ_TRUE; |
3736 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
3737 | 0 | "opj_j2k_read_tlm(): cannot allocate m_tile_part_infos.\n"); |
3738 | 0 | return OPJ_TRUE; |
3739 | 0 | } |
3740 | | |
3741 | 3.00k | l_tlm->m_tile_part_infos = l_tile_part_infos; |
3742 | | |
3743 | 11.3k | for (i = 0; i < l_num_tileparts; ++ i) { |
3744 | 8.35k | OPJ_UINT32 l_tile_index; |
3745 | 8.35k | OPJ_UINT32 l_length; |
3746 | | |
3747 | | /* Read Ttlm_i */ |
3748 | 8.35k | if (l_ST == 0) { |
3749 | 3.47k | l_tile_index = l_tlm->m_entries_count; |
3750 | 4.88k | } else { |
3751 | 4.88k | opj_read_bytes(p_header_data, &l_tile_index, l_ST); |
3752 | 4.88k | p_header_data += l_ST; |
3753 | 4.88k | } |
3754 | | |
3755 | 8.35k | if (l_tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) { |
3756 | 26 | l_tlm->m_is_invalid = OPJ_TRUE; |
3757 | 26 | opj_event_msg(p_manager, EVT_WARNING, |
3758 | 26 | "opj_j2k_read_tlm(): invalid tile number %d\n", |
3759 | 26 | l_tile_index); |
3760 | 26 | return OPJ_TRUE; |
3761 | 26 | } |
3762 | | |
3763 | | /* Read Ptlm_i */ |
3764 | 8.32k | opj_read_bytes(p_header_data, &l_length, l_Ptlm_size); |
3765 | 8.32k | p_header_data += l_Ptlm_size; |
3766 | | |
3767 | 8.32k | l_tile_part_infos[l_tlm->m_entries_count].m_tile_index = |
3768 | 8.32k | (OPJ_UINT16)l_tile_index; |
3769 | 8.32k | l_tile_part_infos[l_tlm->m_entries_count].m_length = l_length; |
3770 | 8.32k | ++l_tlm->m_entries_count; |
3771 | 8.32k | } |
3772 | | |
3773 | 2.97k | return OPJ_TRUE; |
3774 | 3.00k | } |
3775 | | |
3776 | | /** |
3777 | | * Reads a PLM marker (Packet length, main header marker) |
3778 | | * |
3779 | | * @param p_header_data the data contained in the TLM box. |
3780 | | * @param p_j2k the jpeg2000 codec. |
3781 | | * @param p_header_size the size of the data contained in the TLM marker. |
3782 | | * @param p_manager the user event manager. |
3783 | | */ |
3784 | | static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, |
3785 | | OPJ_BYTE * p_header_data, |
3786 | | OPJ_UINT32 p_header_size, |
3787 | | opj_event_mgr_t * p_manager |
3788 | | ) |
3789 | 1.64k | { |
3790 | | /* preconditions */ |
3791 | 1.64k | assert(p_header_data != 00); |
3792 | 1.64k | assert(p_j2k != 00); |
3793 | 1.64k | assert(p_manager != 00); |
3794 | | |
3795 | 1.64k | OPJ_UNUSED(p_j2k); |
3796 | 1.64k | OPJ_UNUSED(p_header_data); |
3797 | | |
3798 | 1.64k | if (p_header_size < 1) { |
3799 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3800 | 1 | return OPJ_FALSE; |
3801 | 1 | } |
3802 | | /* Do not care of this at the moment since only local variables are set here */ |
3803 | | /* |
3804 | | opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm |
3805 | | ++p_header_data; |
3806 | | --p_header_size; |
3807 | | |
3808 | | while |
3809 | | (p_header_size > 0) |
3810 | | { |
3811 | | opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm |
3812 | | ++p_header_data; |
3813 | | p_header_size -= (1+l_Nplm); |
3814 | | if |
3815 | | (p_header_size < 0) |
3816 | | { |
3817 | | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3818 | | return false; |
3819 | | } |
3820 | | for |
3821 | | (i = 0; i < l_Nplm; ++i) |
3822 | | { |
3823 | | opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij |
3824 | | ++p_header_data; |
3825 | | // take only the last seven bytes |
3826 | | l_packet_len |= (l_tmp & 0x7f); |
3827 | | if |
3828 | | (l_tmp & 0x80) |
3829 | | { |
3830 | | l_packet_len <<= 7; |
3831 | | } |
3832 | | else |
3833 | | { |
3834 | | // store packet length and proceed to next packet |
3835 | | l_packet_len = 0; |
3836 | | } |
3837 | | } |
3838 | | if |
3839 | | (l_packet_len != 0) |
3840 | | { |
3841 | | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3842 | | return false; |
3843 | | } |
3844 | | } |
3845 | | */ |
3846 | 1.63k | return OPJ_TRUE; |
3847 | 1.64k | } |
3848 | | |
3849 | | /** |
3850 | | * Reads a PLT marker (Packet length, tile-part header) |
3851 | | * |
3852 | | * @param p_header_data the data contained in the PLT box. |
3853 | | * @param p_j2k the jpeg2000 codec. |
3854 | | * @param p_header_size the size of the data contained in the PLT marker. |
3855 | | * @param p_manager the user event manager. |
3856 | | */ |
3857 | | static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, |
3858 | | OPJ_BYTE * p_header_data, |
3859 | | OPJ_UINT32 p_header_size, |
3860 | | opj_event_mgr_t * p_manager |
3861 | | ) |
3862 | 58 | { |
3863 | 58 | OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; |
3864 | | |
3865 | | /* preconditions */ |
3866 | 58 | assert(p_header_data != 00); |
3867 | 58 | assert(p_j2k != 00); |
3868 | 58 | assert(p_manager != 00); |
3869 | | |
3870 | 58 | OPJ_UNUSED(p_j2k); |
3871 | | |
3872 | 58 | if (p_header_size < 1) { |
3873 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
3874 | 0 | return OPJ_FALSE; |
3875 | 0 | } |
3876 | | |
3877 | 58 | opj_read_bytes(p_header_data, &l_Zplt, 1); /* Zplt */ |
3878 | 58 | ++p_header_data; |
3879 | 58 | --p_header_size; |
3880 | | |
3881 | 7.28k | for (i = 0; i < p_header_size; ++i) { |
3882 | 7.22k | opj_read_bytes(p_header_data, &l_tmp, 1); /* Iplt_ij */ |
3883 | 7.22k | ++p_header_data; |
3884 | | /* take only the last seven bytes */ |
3885 | 7.22k | l_packet_len |= (l_tmp & 0x7f); |
3886 | 7.22k | if (l_tmp & 0x80) { |
3887 | 2.83k | l_packet_len <<= 7; |
3888 | 4.39k | } else { |
3889 | | /* store packet length and proceed to next packet */ |
3890 | 4.39k | l_packet_len = 0; |
3891 | 4.39k | } |
3892 | 7.22k | } |
3893 | | |
3894 | 58 | if (l_packet_len != 0) { |
3895 | 21 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
3896 | 21 | return OPJ_FALSE; |
3897 | 21 | } |
3898 | | |
3899 | 37 | return OPJ_TRUE; |
3900 | 58 | } |
3901 | | |
3902 | | /** |
3903 | | * Reads a PPM marker (Packed packet headers, main header) |
3904 | | * |
3905 | | * @param p_header_data the data contained in the POC box. |
3906 | | * @param p_j2k the jpeg2000 codec. |
3907 | | * @param p_header_size the size of the data contained in the POC marker. |
3908 | | * @param p_manager the user event manager. |
3909 | | */ |
3910 | | |
3911 | | static OPJ_BOOL opj_j2k_read_ppm( |
3912 | | opj_j2k_t *p_j2k, |
3913 | | OPJ_BYTE * p_header_data, |
3914 | | OPJ_UINT32 p_header_size, |
3915 | | opj_event_mgr_t * p_manager) |
3916 | 115 | { |
3917 | 115 | opj_cp_t *l_cp = 00; |
3918 | 115 | OPJ_UINT32 l_Z_ppm; |
3919 | | |
3920 | | /* preconditions */ |
3921 | 115 | assert(p_header_data != 00); |
3922 | 115 | assert(p_j2k != 00); |
3923 | 115 | assert(p_manager != 00); |
3924 | | |
3925 | | /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */ |
3926 | 115 | if (p_header_size < 2) { |
3927 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
3928 | 0 | return OPJ_FALSE; |
3929 | 0 | } |
3930 | | |
3931 | 115 | l_cp = &(p_j2k->m_cp); |
3932 | 115 | l_cp->ppm = 1; |
3933 | | |
3934 | 115 | opj_read_bytes(p_header_data, &l_Z_ppm, 1); /* Z_ppm */ |
3935 | 115 | ++p_header_data; |
3936 | 115 | --p_header_size; |
3937 | | |
3938 | | /* check allocation needed */ |
3939 | 115 | if (l_cp->ppm_markers == NULL) { /* first PPM marker */ |
3940 | 60 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3941 | 60 | assert(l_cp->ppm_markers_count == 0U); |
3942 | | |
3943 | 60 | l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
3944 | 60 | if (l_cp->ppm_markers == NULL) { |
3945 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
3946 | 0 | return OPJ_FALSE; |
3947 | 0 | } |
3948 | 60 | l_cp->ppm_markers_count = l_newCount; |
3949 | 60 | } else if (l_cp->ppm_markers_count <= l_Z_ppm) { |
3950 | 28 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3951 | 28 | opj_ppx *new_ppm_markers; |
3952 | 28 | new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers, |
3953 | 28 | l_newCount * sizeof(opj_ppx)); |
3954 | 28 | if (new_ppm_markers == NULL) { |
3955 | | /* clean up to be done on l_cp destruction */ |
3956 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
3957 | 0 | return OPJ_FALSE; |
3958 | 0 | } |
3959 | 28 | l_cp->ppm_markers = new_ppm_markers; |
3960 | 28 | memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0, |
3961 | 28 | (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx)); |
3962 | 28 | l_cp->ppm_markers_count = l_newCount; |
3963 | 28 | } |
3964 | | |
3965 | 115 | if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) { |
3966 | | /* clean up to be done on l_cp destruction */ |
3967 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm); |
3968 | 5 | return OPJ_FALSE; |
3969 | 5 | } |
3970 | | |
3971 | 110 | l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
3972 | 110 | if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) { |
3973 | | /* clean up to be done on l_cp destruction */ |
3974 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
3975 | 0 | return OPJ_FALSE; |
3976 | 0 | } |
3977 | 110 | l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size; |
3978 | 110 | memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size); |
3979 | | |
3980 | 110 | return OPJ_TRUE; |
3981 | 110 | } |
3982 | | |
3983 | | /** |
3984 | | * Merges all PPM markers read (Packed headers, main header) |
3985 | | * |
3986 | | * @param p_cp main coding parameters. |
3987 | | * @param p_manager the user event manager. |
3988 | | */ |
3989 | | static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager) |
3990 | 7.26k | { |
3991 | 7.26k | OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining; |
3992 | | |
3993 | | /* preconditions */ |
3994 | 7.26k | assert(p_cp != 00); |
3995 | 7.26k | assert(p_manager != 00); |
3996 | 7.26k | assert(p_cp->ppm_buffer == NULL); |
3997 | | |
3998 | 7.26k | if (p_cp->ppm == 0U) { |
3999 | 7.22k | return OPJ_TRUE; |
4000 | 7.22k | } |
4001 | | |
4002 | 39 | l_ppm_data_size = 0U; |
4003 | 39 | l_N_ppm_remaining = 0U; |
4004 | 3.40k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
4005 | 3.37k | if (p_cp->ppm_markers[i].m_data != |
4006 | 3.37k | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
4007 | 68 | OPJ_UINT32 l_N_ppm; |
4008 | 68 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
4009 | 68 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
4010 | | |
4011 | 68 | if (l_N_ppm_remaining >= l_data_size) { |
4012 | 10 | l_N_ppm_remaining -= l_data_size; |
4013 | 10 | l_data_size = 0U; |
4014 | 58 | } else { |
4015 | 58 | l_data += l_N_ppm_remaining; |
4016 | 58 | l_data_size -= l_N_ppm_remaining; |
4017 | 58 | l_N_ppm_remaining = 0U; |
4018 | 58 | } |
4019 | | |
4020 | 68 | if (l_data_size > 0U) { |
4021 | 103 | do { |
4022 | | /* read Nppm */ |
4023 | 103 | if (l_data_size < 4U) { |
4024 | | /* clean up to be done on l_cp destruction */ |
4025 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); |
4026 | 1 | return OPJ_FALSE; |
4027 | 1 | } |
4028 | 102 | opj_read_bytes(l_data, &l_N_ppm, 4); |
4029 | 102 | l_data += 4; |
4030 | 102 | l_data_size -= 4; |
4031 | | |
4032 | 102 | if (l_ppm_data_size > UINT_MAX - l_N_ppm) { |
4033 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Too large value for Nppm\n"); |
4034 | 1 | return OPJ_FALSE; |
4035 | 1 | } |
4036 | 101 | l_ppm_data_size += l_N_ppm; |
4037 | 101 | if (l_data_size >= l_N_ppm) { |
4038 | 81 | l_data_size -= l_N_ppm; |
4039 | 81 | l_data += l_N_ppm; |
4040 | 81 | } else { |
4041 | 20 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
4042 | 20 | l_data_size = 0U; |
4043 | 20 | } |
4044 | 101 | } while (l_data_size > 0U); |
4045 | 58 | } |
4046 | 68 | } |
4047 | 3.37k | } |
4048 | | |
4049 | 37 | if (l_N_ppm_remaining != 0U) { |
4050 | | /* clean up to be done on l_cp destruction */ |
4051 | 19 | opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n"); |
4052 | 19 | return OPJ_FALSE; |
4053 | 19 | } |
4054 | | |
4055 | 18 | p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size); |
4056 | 18 | if (p_cp->ppm_buffer == 00) { |
4057 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
4058 | 0 | return OPJ_FALSE; |
4059 | 0 | } |
4060 | 18 | p_cp->ppm_len = l_ppm_data_size; |
4061 | 18 | l_ppm_data_size = 0U; |
4062 | 18 | l_N_ppm_remaining = 0U; |
4063 | 934 | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
4064 | 916 | if (p_cp->ppm_markers[i].m_data != |
4065 | 916 | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
4066 | 28 | OPJ_UINT32 l_N_ppm; |
4067 | 28 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
4068 | 28 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
4069 | | |
4070 | 28 | if (l_N_ppm_remaining >= l_data_size) { |
4071 | 0 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
4072 | 0 | l_ppm_data_size += l_data_size; |
4073 | 0 | l_N_ppm_remaining -= l_data_size; |
4074 | 0 | l_data_size = 0U; |
4075 | 28 | } else { |
4076 | 28 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining); |
4077 | 28 | l_ppm_data_size += l_N_ppm_remaining; |
4078 | 28 | l_data += l_N_ppm_remaining; |
4079 | 28 | l_data_size -= l_N_ppm_remaining; |
4080 | 28 | l_N_ppm_remaining = 0U; |
4081 | 28 | } |
4082 | | |
4083 | 28 | if (l_data_size > 0U) { |
4084 | 46 | do { |
4085 | | /* read Nppm */ |
4086 | 46 | if (l_data_size < 4U) { |
4087 | | /* clean up to be done on l_cp destruction */ |
4088 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); |
4089 | 0 | return OPJ_FALSE; |
4090 | 0 | } |
4091 | 46 | opj_read_bytes(l_data, &l_N_ppm, 4); |
4092 | 46 | l_data += 4; |
4093 | 46 | l_data_size -= 4; |
4094 | | |
4095 | 46 | if (l_data_size >= l_N_ppm) { |
4096 | 46 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm); |
4097 | 46 | l_ppm_data_size += l_N_ppm; |
4098 | 46 | l_data_size -= l_N_ppm; |
4099 | 46 | l_data += l_N_ppm; |
4100 | 46 | } else { |
4101 | 0 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
4102 | 0 | l_ppm_data_size += l_data_size; |
4103 | 0 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
4104 | 0 | l_data_size = 0U; |
4105 | 0 | } |
4106 | 46 | } while (l_data_size > 0U); |
4107 | 28 | } |
4108 | 28 | opj_free(p_cp->ppm_markers[i].m_data); |
4109 | 28 | p_cp->ppm_markers[i].m_data = NULL; |
4110 | 28 | p_cp->ppm_markers[i].m_data_size = 0U; |
4111 | 28 | } |
4112 | 916 | } |
4113 | | |
4114 | 18 | p_cp->ppm_data = p_cp->ppm_buffer; |
4115 | 18 | p_cp->ppm_data_size = p_cp->ppm_len; |
4116 | | |
4117 | 18 | p_cp->ppm_markers_count = 0U; |
4118 | 18 | opj_free(p_cp->ppm_markers); |
4119 | 18 | p_cp->ppm_markers = NULL; |
4120 | | |
4121 | 18 | return OPJ_TRUE; |
4122 | 18 | } |
4123 | | |
4124 | | /** |
4125 | | * Reads a PPT marker (Packed packet headers, tile-part header) |
4126 | | * |
4127 | | * @param p_header_data the data contained in the PPT box. |
4128 | | * @param p_j2k the jpeg2000 codec. |
4129 | | * @param p_header_size the size of the data contained in the PPT marker. |
4130 | | * @param p_manager the user event manager. |
4131 | | */ |
4132 | | static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, |
4133 | | OPJ_BYTE * p_header_data, |
4134 | | OPJ_UINT32 p_header_size, |
4135 | | opj_event_mgr_t * p_manager |
4136 | | ) |
4137 | 67 | { |
4138 | 67 | opj_cp_t *l_cp = 00; |
4139 | 67 | opj_tcp_t *l_tcp = 00; |
4140 | 67 | OPJ_UINT32 l_Z_ppt; |
4141 | | |
4142 | | /* preconditions */ |
4143 | 67 | assert(p_header_data != 00); |
4144 | 67 | assert(p_j2k != 00); |
4145 | 67 | assert(p_manager != 00); |
4146 | | |
4147 | | /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */ |
4148 | 67 | if (p_header_size < 2) { |
4149 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n"); |
4150 | 1 | return OPJ_FALSE; |
4151 | 1 | } |
4152 | | |
4153 | 66 | l_cp = &(p_j2k->m_cp); |
4154 | 66 | if (l_cp->ppm) { |
4155 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
4156 | 1 | "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); |
4157 | 1 | return OPJ_FALSE; |
4158 | 1 | } |
4159 | | |
4160 | 65 | l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); |
4161 | 65 | l_tcp->ppt = 1; |
4162 | | |
4163 | 65 | opj_read_bytes(p_header_data, &l_Z_ppt, 1); /* Z_ppt */ |
4164 | 65 | ++p_header_data; |
4165 | 65 | --p_header_size; |
4166 | | |
4167 | | /* check allocation needed */ |
4168 | 65 | if (l_tcp->ppt_markers == NULL) { /* first PPT marker */ |
4169 | 42 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4170 | 42 | assert(l_tcp->ppt_markers_count == 0U); |
4171 | | |
4172 | 42 | l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
4173 | 42 | if (l_tcp->ppt_markers == NULL) { |
4174 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4175 | 0 | return OPJ_FALSE; |
4176 | 0 | } |
4177 | 42 | l_tcp->ppt_markers_count = l_newCount; |
4178 | 42 | } else if (l_tcp->ppt_markers_count <= l_Z_ppt) { |
4179 | 10 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4180 | 10 | opj_ppx *new_ppt_markers; |
4181 | 10 | new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers, |
4182 | 10 | l_newCount * sizeof(opj_ppx)); |
4183 | 10 | if (new_ppt_markers == NULL) { |
4184 | | /* clean up to be done on l_tcp destruction */ |
4185 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4186 | 0 | return OPJ_FALSE; |
4187 | 0 | } |
4188 | 10 | l_tcp->ppt_markers = new_ppt_markers; |
4189 | 10 | memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0, |
4190 | 10 | (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx)); |
4191 | 10 | l_tcp->ppt_markers_count = l_newCount; |
4192 | 10 | } |
4193 | | |
4194 | 65 | if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) { |
4195 | | /* clean up to be done on l_tcp destruction */ |
4196 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt); |
4197 | 2 | return OPJ_FALSE; |
4198 | 2 | } |
4199 | | |
4200 | 63 | l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
4201 | 63 | if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) { |
4202 | | /* clean up to be done on l_tcp destruction */ |
4203 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4204 | 0 | return OPJ_FALSE; |
4205 | 0 | } |
4206 | 63 | l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size; |
4207 | 63 | memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size); |
4208 | 63 | return OPJ_TRUE; |
4209 | 63 | } |
4210 | | |
4211 | | /** |
4212 | | * Merges all PPT markers read (Packed packet headers, tile-part header) |
4213 | | * |
4214 | | * @param p_tcp the tile. |
4215 | | * @param p_manager the user event manager. |
4216 | | */ |
4217 | | static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) |
4218 | 9.29k | { |
4219 | 9.29k | OPJ_UINT32 i, l_ppt_data_size; |
4220 | | /* preconditions */ |
4221 | 9.29k | assert(p_tcp != 00); |
4222 | 9.29k | assert(p_manager != 00); |
4223 | | |
4224 | 9.29k | if (p_tcp->ppt_buffer != NULL) { |
4225 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4226 | 0 | "opj_j2k_merge_ppt() has already been called\n"); |
4227 | 0 | return OPJ_FALSE; |
4228 | 0 | } |
4229 | | |
4230 | 9.29k | if (p_tcp->ppt == 0U) { |
4231 | 9.27k | return OPJ_TRUE; |
4232 | 9.27k | } |
4233 | | |
4234 | 19 | l_ppt_data_size = 0U; |
4235 | 581 | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4236 | 562 | l_ppt_data_size += |
4237 | 562 | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4238 | 562 | } |
4239 | | |
4240 | 19 | p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size); |
4241 | 19 | if (p_tcp->ppt_buffer == 00) { |
4242 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
4243 | 0 | return OPJ_FALSE; |
4244 | 0 | } |
4245 | 19 | p_tcp->ppt_len = l_ppt_data_size; |
4246 | 19 | l_ppt_data_size = 0U; |
4247 | 581 | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4248 | 562 | if (p_tcp->ppt_markers[i].m_data != |
4249 | 562 | NULL) { /* standard doesn't seem to require contiguous Zppt */ |
4250 | 22 | memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data, |
4251 | 22 | p_tcp->ppt_markers[i].m_data_size); |
4252 | 22 | l_ppt_data_size += |
4253 | 22 | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4254 | | |
4255 | 22 | opj_free(p_tcp->ppt_markers[i].m_data); |
4256 | 22 | p_tcp->ppt_markers[i].m_data = NULL; |
4257 | 22 | p_tcp->ppt_markers[i].m_data_size = 0U; |
4258 | 22 | } |
4259 | 562 | } |
4260 | | |
4261 | 19 | p_tcp->ppt_markers_count = 0U; |
4262 | 19 | opj_free(p_tcp->ppt_markers); |
4263 | 19 | p_tcp->ppt_markers = NULL; |
4264 | | |
4265 | 19 | p_tcp->ppt_data = p_tcp->ppt_buffer; |
4266 | 19 | p_tcp->ppt_data_size = p_tcp->ppt_len; |
4267 | 19 | return OPJ_TRUE; |
4268 | 19 | } |
4269 | | |
4270 | | static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, |
4271 | | opj_stream_private_t *p_stream, |
4272 | | opj_event_mgr_t * p_manager |
4273 | | ) |
4274 | 0 | { |
4275 | 0 | OPJ_BYTE * l_current_data = 00; |
4276 | 0 | OPJ_UINT32 l_tlm_size; |
4277 | 0 | OPJ_UINT32 size_per_tile_part; |
4278 | | |
4279 | | /* preconditions */ |
4280 | 0 | assert(p_j2k != 00); |
4281 | 0 | assert(p_manager != 00); |
4282 | 0 | assert(p_stream != 00); |
4283 | | |
4284 | | /* 10921 = (65535 - header_size) / size_per_tile_part where */ |
4285 | | /* header_size = 4 and size_per_tile_part = 6 */ |
4286 | 0 | if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts > 10921) { |
4287 | | /* We could do more but it would require writing several TLM markers */ |
4288 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4289 | 0 | "A maximum of 10921 tile-parts are supported currently " |
4290 | 0 | "when writing TLM marker\n"); |
4291 | 0 | return OPJ_FALSE; |
4292 | 0 | } |
4293 | | |
4294 | 0 | if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts <= 255) { |
4295 | 0 | size_per_tile_part = 5; |
4296 | 0 | p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_TRUE; |
4297 | 0 | } else { |
4298 | 0 | size_per_tile_part = 6; |
4299 | 0 | p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_FALSE; |
4300 | 0 | } |
4301 | |
|
4302 | 0 | l_tlm_size = 2 + 4 + (size_per_tile_part * |
4303 | 0 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
4304 | |
|
4305 | 0 | if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
4306 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
4307 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size); |
4308 | 0 | if (! new_header_tile_data) { |
4309 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
4310 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
4311 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
4312 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n"); |
4313 | 0 | return OPJ_FALSE; |
4314 | 0 | } |
4315 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
4316 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; |
4317 | 0 | } |
4318 | 0 | memset(p_j2k->m_specific_param.m_encoder.m_header_tile_data, 0, l_tlm_size); |
4319 | |
|
4320 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
4321 | | |
4322 | | /* change the way data is written to avoid seeking if possible */ |
4323 | | /* TODO */ |
4324 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); |
4325 | |
|
4326 | 0 | opj_write_bytes(l_current_data, J2K_MS_TLM, |
4327 | 0 | 2); /* TLM */ |
4328 | 0 | l_current_data += 2; |
4329 | |
|
4330 | 0 | opj_write_bytes(l_current_data, l_tlm_size - 2, |
4331 | 0 | 2); /* Lpoc */ |
4332 | 0 | l_current_data += 2; |
4333 | |
|
4334 | 0 | opj_write_bytes(l_current_data, 0, |
4335 | 0 | 1); /* Ztlm=0*/ |
4336 | 0 | ++l_current_data; |
4337 | | |
4338 | | /* Stlm 0x50= ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ |
4339 | | /* Stlm 0x60= ST=2(16bits-65535 tiles max),SP=1(Ptlm=32bits) */ |
4340 | 0 | opj_write_bytes(l_current_data, |
4341 | 0 | size_per_tile_part == 5 ? 0x50 : 0x60, |
4342 | 0 | 1); |
4343 | 0 | ++l_current_data; |
4344 | | |
4345 | | /* do nothing on the size_per_tile_part * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ |
4346 | 0 | if (opj_stream_write_data(p_stream, |
4347 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size, |
4348 | 0 | p_manager) != l_tlm_size) { |
4349 | 0 | return OPJ_FALSE; |
4350 | 0 | } |
4351 | | |
4352 | 0 | return OPJ_TRUE; |
4353 | 0 | } |
4354 | | |
4355 | | static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, |
4356 | | OPJ_BYTE * p_data, |
4357 | | OPJ_UINT32 total_data_size, |
4358 | | OPJ_UINT32 * p_data_written, |
4359 | | const opj_stream_private_t *p_stream, |
4360 | | opj_event_mgr_t * p_manager |
4361 | | ) |
4362 | 0 | { |
4363 | | /* preconditions */ |
4364 | 0 | assert(p_j2k != 00); |
4365 | 0 | assert(p_manager != 00); |
4366 | 0 | assert(p_stream != 00); |
4367 | |
|
4368 | 0 | OPJ_UNUSED(p_stream); |
4369 | |
|
4370 | 0 | if (total_data_size < 12) { |
4371 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4372 | 0 | "Not enough bytes in output buffer to write SOT marker\n"); |
4373 | 0 | return OPJ_FALSE; |
4374 | 0 | } |
4375 | | |
4376 | 0 | opj_write_bytes(p_data, J2K_MS_SOT, |
4377 | 0 | 2); /* SOT */ |
4378 | 0 | p_data += 2; |
4379 | |
|
4380 | 0 | opj_write_bytes(p_data, 10, |
4381 | 0 | 2); /* Lsot */ |
4382 | 0 | p_data += 2; |
4383 | |
|
4384 | 0 | opj_write_bytes(p_data, p_j2k->m_current_tile_number, |
4385 | 0 | 2); /* Isot */ |
4386 | 0 | p_data += 2; |
4387 | | |
4388 | | /* Psot */ |
4389 | 0 | p_data += 4; |
4390 | |
|
4391 | 0 | opj_write_bytes(p_data, |
4392 | 0 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number, |
4393 | 0 | 1); /* TPsot */ |
4394 | 0 | ++p_data; |
4395 | |
|
4396 | 0 | opj_write_bytes(p_data, |
4397 | 0 | p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts, |
4398 | 0 | 1); /* TNsot */ |
4399 | 0 | ++p_data; |
4400 | | |
4401 | | /* UniPG>> */ |
4402 | | #ifdef USE_JPWL |
4403 | | /* update markers struct */ |
4404 | | /* |
4405 | | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); |
4406 | | */ |
4407 | | assert(0 && "TODO"); |
4408 | | #endif /* USE_JPWL */ |
4409 | |
|
4410 | 0 | * p_data_written = 12; |
4411 | |
|
4412 | 0 | return OPJ_TRUE; |
4413 | 0 | } |
4414 | | |
4415 | | static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, |
4416 | | OPJ_UINT32 p_header_size, |
4417 | | OPJ_UINT32* p_tile_no, |
4418 | | OPJ_UINT32* p_tot_len, |
4419 | | OPJ_UINT32* p_current_part, |
4420 | | OPJ_UINT32* p_num_parts, |
4421 | | opj_event_mgr_t * p_manager) |
4422 | 9.91k | { |
4423 | | /* preconditions */ |
4424 | 9.91k | assert(p_header_data != 00); |
4425 | 9.91k | assert(p_manager != 00); |
4426 | | |
4427 | | /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ |
4428 | 9.91k | if (p_header_size != 8) { |
4429 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); |
4430 | 3 | return OPJ_FALSE; |
4431 | 3 | } |
4432 | | |
4433 | 9.90k | opj_read_bytes(p_header_data, p_tile_no, 2); /* Isot */ |
4434 | 9.90k | p_header_data += 2; |
4435 | 9.90k | opj_read_bytes(p_header_data, p_tot_len, 4); /* Psot */ |
4436 | 9.90k | p_header_data += 4; |
4437 | 9.90k | opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */ |
4438 | 9.90k | ++p_header_data; |
4439 | 9.90k | opj_read_bytes(p_header_data, p_num_parts, 1); /* TNsot */ |
4440 | 9.90k | ++p_header_data; |
4441 | 9.90k | return OPJ_TRUE; |
4442 | 9.91k | } |
4443 | | |
4444 | | static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, |
4445 | | OPJ_BYTE * p_header_data, |
4446 | | OPJ_UINT32 p_header_size, |
4447 | | opj_event_mgr_t * p_manager) |
4448 | 9.87k | { |
4449 | 9.87k | opj_cp_t *l_cp = 00; |
4450 | 9.87k | opj_tcp_t *l_tcp = 00; |
4451 | 9.87k | OPJ_UINT32 l_tot_len, l_num_parts = 0; |
4452 | 9.87k | OPJ_UINT32 l_current_part; |
4453 | 9.87k | OPJ_UINT32 l_tile_x, l_tile_y; |
4454 | | |
4455 | | /* preconditions */ |
4456 | | |
4457 | 9.87k | assert(p_j2k != 00); |
4458 | 9.87k | assert(p_manager != 00); |
4459 | | |
4460 | 9.87k | if (! opj_j2k_get_sot_values(p_header_data, p_header_size, |
4461 | 9.87k | &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts, |
4462 | 9.87k | p_manager)) { |
4463 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); |
4464 | 3 | return OPJ_FALSE; |
4465 | 3 | } |
4466 | | #ifdef DEBUG_VERBOSE |
4467 | | fprintf(stderr, "SOT %d %d %d %d\n", |
4468 | | p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts); |
4469 | | #endif |
4470 | | |
4471 | 9.87k | l_cp = &(p_j2k->m_cp); |
4472 | | |
4473 | | /* testcase 2.pdf.SIGFPE.706.1112 */ |
4474 | 9.87k | if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { |
4475 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", |
4476 | 3 | p_j2k->m_current_tile_number); |
4477 | 3 | return OPJ_FALSE; |
4478 | 3 | } |
4479 | | |
4480 | 9.87k | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
4481 | 9.87k | l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; |
4482 | 9.87k | l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; |
4483 | | |
4484 | 9.87k | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 || |
4485 | 9.87k | p_j2k->m_current_tile_number == (OPJ_UINT32) |
4486 | 9.87k | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) { |
4487 | | /* Do only this check if we decode all tile part headers, or if */ |
4488 | | /* we decode one precise tile. Otherwise the m_current_tile_part_number */ |
4489 | | /* might not be valid */ |
4490 | | /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */ |
4491 | | /* of https://github.com/uclouvain/openjpeg/issues/939 */ |
4492 | | /* We must avoid reading twice the same tile part number for a given tile */ |
4493 | | /* so as to avoid various issues, like opj_j2k_merge_ppt being called */ |
4494 | | /* several times. */ |
4495 | | /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */ |
4496 | | /* should appear in increasing order. */ |
4497 | 9.87k | if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) { |
4498 | 12 | opj_event_msg(p_manager, EVT_ERROR, |
4499 | 12 | "Invalid tile part index for tile number %d. " |
4500 | 12 | "Got %d, expected %d\n", |
4501 | 12 | p_j2k->m_current_tile_number, |
4502 | 12 | l_current_part, |
4503 | 12 | l_tcp->m_current_tile_part_number + 1); |
4504 | 12 | return OPJ_FALSE; |
4505 | 12 | } |
4506 | 9.87k | } |
4507 | | |
4508 | 9.86k | l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part; |
4509 | | |
4510 | | #ifdef USE_JPWL |
4511 | | if (l_cp->correct) { |
4512 | | |
4513 | | OPJ_UINT32 tileno = p_j2k->m_current_tile_number; |
4514 | | static OPJ_UINT32 backup_tileno = 0; |
4515 | | |
4516 | | /* tileno is negative or larger than the number of tiles!!! */ |
4517 | | if (tileno > (l_cp->tw * l_cp->th)) { |
4518 | | opj_event_msg(p_manager, EVT_ERROR, |
4519 | | "JPWL: bad tile number (%d out of a maximum of %d)\n", |
4520 | | tileno, (l_cp->tw * l_cp->th)); |
4521 | | if (!JPWL_ASSUME) { |
4522 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
4523 | | return OPJ_FALSE; |
4524 | | } |
4525 | | /* we try to correct */ |
4526 | | tileno = backup_tileno; |
4527 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
4528 | | "- setting tile number to %d\n", |
4529 | | tileno); |
4530 | | } |
4531 | | |
4532 | | /* keep your private count of tiles */ |
4533 | | backup_tileno++; |
4534 | | }; |
4535 | | #endif /* USE_JPWL */ |
4536 | | |
4537 | | /* look for the tile in the list of already processed tile (in parts). */ |
4538 | | /* Optimization possible here with a more complex data structure and with the removing of tiles */ |
4539 | | /* since the time taken by this function can only grow at the time */ |
4540 | | |
4541 | | /* PSot should be equal to zero or >=14 or <= 2^32-1 */ |
4542 | 9.86k | if ((l_tot_len != 0) && (l_tot_len < 14)) { |
4543 | 2 | if (l_tot_len == |
4544 | 2 | 12) { /* MSD: Special case for the PHR data which are read by kakadu*/ |
4545 | 2 | opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", |
4546 | 2 | l_tot_len); |
4547 | 2 | } else { |
4548 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4549 | 0 | "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len); |
4550 | 0 | return OPJ_FALSE; |
4551 | 0 | } |
4552 | 2 | } |
4553 | | |
4554 | | #ifdef USE_JPWL |
4555 | | if (l_cp->correct) { |
4556 | | |
4557 | | /* totlen is negative or larger than the bytes left!!! */ |
4558 | | if (/*(l_tot_len < 0) ||*/ (l_tot_len > |
4559 | | p_header_size)) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ |
4560 | | opj_event_msg(p_manager, EVT_ERROR, |
4561 | | "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", |
4562 | | l_tot_len, |
4563 | | p_header_size); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */ |
4564 | | if (!JPWL_ASSUME) { |
4565 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
4566 | | return OPJ_FALSE; |
4567 | | } |
4568 | | /* we try to correct */ |
4569 | | l_tot_len = 0; |
4570 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
4571 | | "- setting Psot to %d => assuming it is the last tile\n", |
4572 | | l_tot_len); |
4573 | | } |
4574 | | }; |
4575 | | #endif /* USE_JPWL */ |
4576 | | |
4577 | | /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/ |
4578 | 9.86k | if (!l_tot_len) { |
4579 | 6.81k | opj_event_msg(p_manager, EVT_INFO, |
4580 | 6.81k | "Psot value of the current tile-part is equal to zero, " |
4581 | 6.81k | "we assuming it is the last tile-part of the codestream.\n"); |
4582 | 6.81k | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4583 | 6.81k | } |
4584 | | |
4585 | 9.86k | if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) { |
4586 | | /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */ |
4587 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
4588 | 1 | "In SOT marker, TPSot (%d) is not valid regards to the previous " |
4589 | 1 | "number of tile-part (%d), giving up\n", l_current_part, |
4590 | 1 | l_tcp->m_nb_tile_parts); |
4591 | 1 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4592 | 1 | return OPJ_FALSE; |
4593 | 1 | } |
4594 | | |
4595 | 9.86k | if (l_num_parts != |
4596 | 9.86k | 0) { /* Number of tile-part header is provided by this tile-part header */ |
4597 | 9.78k | l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction; |
4598 | | /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of |
4599 | | * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */ |
4600 | 9.78k | if (l_tcp->m_nb_tile_parts) { |
4601 | 259 | if (l_current_part >= l_tcp->m_nb_tile_parts) { |
4602 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4603 | 0 | "In SOT marker, TPSot (%d) is not valid regards to the current " |
4604 | 0 | "number of tile-part (%d), giving up\n", l_current_part, |
4605 | 0 | l_tcp->m_nb_tile_parts); |
4606 | 0 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4607 | 0 | return OPJ_FALSE; |
4608 | 0 | } |
4609 | 259 | } |
4610 | 9.78k | if (l_current_part >= l_num_parts) { |
4611 | | /* testcase 451.pdf.SIGSEGV.ce9.3723 */ |
4612 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
4613 | 1 | "In SOT marker, TPSot (%d) is not valid regards to the current " |
4614 | 1 | "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts); |
4615 | 1 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4616 | 1 | return OPJ_FALSE; |
4617 | 1 | } |
4618 | 9.78k | l_tcp->m_nb_tile_parts = l_num_parts; |
4619 | 9.78k | } |
4620 | | |
4621 | | /* If know the number of tile part header we will check if we didn't read the last*/ |
4622 | 9.85k | if (l_tcp->m_nb_tile_parts) { |
4623 | 9.78k | if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) { |
4624 | 2.71k | p_j2k->m_specific_param.m_decoder.m_can_decode = |
4625 | 2.71k | 1; /* Process the last tile-part header*/ |
4626 | 2.71k | } |
4627 | 9.78k | } |
4628 | | |
4629 | 9.85k | if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
4630 | | /* Keep the size of data to skip after this marker */ |
4631 | 3.04k | p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - |
4632 | 3.04k | 12; /* SOT_marker_size = 12 */ |
4633 | 6.81k | } else { |
4634 | | /* FIXME: need to be computed from the number of bytes remaining in the codestream */ |
4635 | 6.81k | p_j2k->m_specific_param.m_decoder.m_sot_length = 0; |
4636 | 6.81k | } |
4637 | | |
4638 | 9.85k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH; |
4639 | | |
4640 | | /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/ |
4641 | 9.85k | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { |
4642 | 9.85k | p_j2k->m_specific_param.m_decoder.m_skip_data = |
4643 | 9.85k | (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) |
4644 | 9.85k | || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) |
4645 | 9.85k | || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) |
4646 | 9.85k | || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); |
4647 | 9.85k | } else { |
4648 | 0 | assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0); |
4649 | 0 | p_j2k->m_specific_param.m_decoder.m_skip_data = |
4650 | 0 | (p_j2k->m_current_tile_number != (OPJ_UINT32) |
4651 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec); |
4652 | 0 | } |
4653 | | |
4654 | | /* Index */ |
4655 | 9.85k | { |
4656 | 9.85k | assert(p_j2k->cstr_index->tile_index != 00); |
4657 | 9.85k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = |
4658 | 9.85k | p_j2k->m_current_tile_number; |
4659 | 9.85k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = |
4660 | 9.85k | l_current_part; |
4661 | | |
4662 | 9.85k | if (!p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid && |
4663 | 9.85k | l_num_parts > |
4664 | 35 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps) { |
4665 | 4 | opj_event_msg(p_manager, EVT_WARNING, |
4666 | 4 | "SOT marker for tile %u declares more tile-parts than found in TLM marker.", |
4667 | 4 | p_j2k->m_current_tile_number); |
4668 | 4 | p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid = OPJ_TRUE; |
4669 | 4 | } |
4670 | | |
4671 | 9.85k | if (!p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid) { |
4672 | | /* do nothing */ |
4673 | 9.82k | } else if (l_num_parts != 0) { |
4674 | | |
4675 | 9.75k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = |
4676 | 9.75k | l_num_parts; |
4677 | 9.75k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = |
4678 | 9.75k | l_num_parts; |
4679 | | |
4680 | 9.75k | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4681 | 9.48k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4682 | 9.48k | (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); |
4683 | 9.48k | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4684 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4685 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4686 | 0 | return OPJ_FALSE; |
4687 | 0 | } |
4688 | 9.48k | } else { |
4689 | 262 | opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( |
4690 | 262 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
4691 | 262 | l_num_parts * sizeof(opj_tp_index_t)); |
4692 | 262 | if (! new_tp_index) { |
4693 | 0 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
4694 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
4695 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4696 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4697 | 0 | return OPJ_FALSE; |
4698 | 0 | } |
4699 | 262 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4700 | 262 | new_tp_index; |
4701 | 262 | } |
4702 | 9.75k | } else { |
4703 | 77 | /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { |
4704 | | |
4705 | 77 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4706 | 55 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; |
4707 | 55 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4708 | 55 | (opj_tp_index_t*)opj_calloc( |
4709 | 55 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, |
4710 | 55 | sizeof(opj_tp_index_t)); |
4711 | 55 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4712 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
4713 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4714 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4715 | 0 | return OPJ_FALSE; |
4716 | 0 | } |
4717 | 55 | } |
4718 | | |
4719 | 77 | if (l_current_part >= |
4720 | 77 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) { |
4721 | 0 | opj_tp_index_t *new_tp_index; |
4722 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = |
4723 | 0 | l_current_part + 1; |
4724 | 0 | new_tp_index = (opj_tp_index_t *) opj_realloc( |
4725 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
4726 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * |
4727 | 0 | sizeof(opj_tp_index_t)); |
4728 | 0 | if (! new_tp_index) { |
4729 | 0 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
4730 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
4731 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
4732 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4733 | 0 | "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
4734 | 0 | return OPJ_FALSE; |
4735 | 0 | } |
4736 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4737 | 0 | new_tp_index; |
4738 | 0 | } |
4739 | 77 | } |
4740 | | |
4741 | 77 | } |
4742 | | |
4743 | 9.85k | } |
4744 | | |
4745 | 9.85k | return OPJ_TRUE; |
4746 | 9.85k | } |
4747 | | |
4748 | | /** |
4749 | | * Write one or more PLT markers in the provided buffer |
4750 | | */ |
4751 | | static OPJ_BOOL opj_j2k_write_plt_in_memory(opj_j2k_t *p_j2k, |
4752 | | opj_tcd_marker_info_t* marker_info, |
4753 | | OPJ_BYTE * p_data, |
4754 | | OPJ_UINT32 * p_data_written, |
4755 | | opj_event_mgr_t * p_manager) |
4756 | 0 | { |
4757 | 0 | OPJ_BYTE Zplt = 0; |
4758 | 0 | OPJ_UINT16 Lplt; |
4759 | 0 | OPJ_BYTE* p_data_start = p_data; |
4760 | 0 | OPJ_BYTE* p_data_Lplt = p_data + 2; |
4761 | 0 | OPJ_UINT32 i; |
4762 | |
|
4763 | 0 | OPJ_UNUSED(p_j2k); |
4764 | |
|
4765 | 0 | opj_write_bytes(p_data, J2K_MS_PLT, 2); |
4766 | 0 | p_data += 2; |
4767 | | |
4768 | | /* Reserve space for Lplt */ |
4769 | 0 | p_data += 2; |
4770 | |
|
4771 | 0 | opj_write_bytes(p_data, Zplt, 1); |
4772 | 0 | p_data += 1; |
4773 | |
|
4774 | 0 | Lplt = 3; |
4775 | |
|
4776 | 0 | for (i = 0; i < marker_info->packet_count; i++) { |
4777 | 0 | OPJ_BYTE var_bytes[5]; |
4778 | 0 | OPJ_UINT8 var_bytes_size = 0; |
4779 | 0 | OPJ_UINT32 packet_size = marker_info->p_packet_size[i]; |
4780 | | |
4781 | | /* Packet size written in variable-length way, starting with LSB */ |
4782 | 0 | var_bytes[var_bytes_size] = (OPJ_BYTE)(packet_size & 0x7f); |
4783 | 0 | var_bytes_size ++; |
4784 | 0 | packet_size >>= 7; |
4785 | 0 | while (packet_size > 0) { |
4786 | 0 | var_bytes[var_bytes_size] = (OPJ_BYTE)((packet_size & 0x7f) | 0x80); |
4787 | 0 | var_bytes_size ++; |
4788 | 0 | packet_size >>= 7; |
4789 | 0 | } |
4790 | | |
4791 | | /* Check if that can fit in the current PLT marker. If not, finish */ |
4792 | | /* current one, and start a new one */ |
4793 | 0 | if (Lplt + var_bytes_size > 65535) { |
4794 | 0 | if (Zplt == 255) { |
4795 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4796 | 0 | "More than 255 PLT markers would be needed for current tile-part !\n"); |
4797 | 0 | return OPJ_FALSE; |
4798 | 0 | } |
4799 | | |
4800 | | /* Patch Lplt */ |
4801 | 0 | opj_write_bytes(p_data_Lplt, Lplt, 2); |
4802 | | |
4803 | | /* Start new segment */ |
4804 | 0 | opj_write_bytes(p_data, J2K_MS_PLT, 2); |
4805 | 0 | p_data += 2; |
4806 | | |
4807 | | /* Reserve space for Lplt */ |
4808 | 0 | p_data_Lplt = p_data; |
4809 | 0 | p_data += 2; |
4810 | |
|
4811 | 0 | Zplt ++; |
4812 | 0 | opj_write_bytes(p_data, Zplt, 1); |
4813 | 0 | p_data += 1; |
4814 | |
|
4815 | 0 | Lplt = 3; |
4816 | 0 | } |
4817 | | |
4818 | 0 | Lplt = (OPJ_UINT16)(Lplt + var_bytes_size); |
4819 | | |
4820 | | /* Serialize variable-length packet size, starting with MSB */ |
4821 | 0 | for (; var_bytes_size > 0; --var_bytes_size) { |
4822 | 0 | opj_write_bytes(p_data, var_bytes[var_bytes_size - 1], 1); |
4823 | 0 | p_data += 1; |
4824 | 0 | } |
4825 | 0 | } |
4826 | | |
4827 | 0 | *p_data_written = (OPJ_UINT32)(p_data - p_data_start); |
4828 | | |
4829 | | /* Patch Lplt */ |
4830 | 0 | opj_write_bytes(p_data_Lplt, Lplt, 2); |
4831 | |
|
4832 | 0 | return OPJ_TRUE; |
4833 | 0 | } |
4834 | | |
4835 | | static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, |
4836 | | opj_tcd_t * p_tile_coder, |
4837 | | OPJ_BYTE * p_data, |
4838 | | OPJ_UINT32 * p_data_written, |
4839 | | OPJ_UINT32 total_data_size, |
4840 | | const opj_stream_private_t *p_stream, |
4841 | | opj_event_mgr_t * p_manager |
4842 | | ) |
4843 | 0 | { |
4844 | 0 | opj_codestream_info_t *l_cstr_info = 00; |
4845 | 0 | OPJ_UINT32 l_remaining_data; |
4846 | 0 | opj_tcd_marker_info_t* marker_info = NULL; |
4847 | | |
4848 | | /* preconditions */ |
4849 | 0 | assert(p_j2k != 00); |
4850 | 0 | assert(p_manager != 00); |
4851 | 0 | assert(p_stream != 00); |
4852 | |
|
4853 | 0 | OPJ_UNUSED(p_stream); |
4854 | |
|
4855 | 0 | if (total_data_size < 4) { |
4856 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4857 | 0 | "Not enough bytes in output buffer to write SOD marker\n"); |
4858 | 0 | return OPJ_FALSE; |
4859 | 0 | } |
4860 | | |
4861 | 0 | opj_write_bytes(p_data, J2K_MS_SOD, |
4862 | 0 | 2); /* SOD */ |
4863 | | |
4864 | | /* make room for the EOF marker */ |
4865 | 0 | l_remaining_data = total_data_size - 4; |
4866 | | |
4867 | | /* update tile coder */ |
4868 | 0 | p_tile_coder->tp_num = |
4869 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; |
4870 | 0 | p_tile_coder->cur_tp_num = |
4871 | 0 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
4872 | | |
4873 | | /* INDEX >> */ |
4874 | | /* TODO mergeV2: check this part which use cstr_info */ |
4875 | | /*l_cstr_info = p_j2k->cstr_info; |
4876 | | if (l_cstr_info) { |
4877 | | if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { |
4878 | | //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; |
4879 | | l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; |
4880 | | } |
4881 | | else {*/ |
4882 | | /* |
4883 | | TODO |
4884 | | if |
4885 | | (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) |
4886 | | { |
4887 | | cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); |
4888 | | }*/ |
4889 | | /*}*/ |
4890 | | /* UniPG>> */ |
4891 | | #ifdef USE_JPWL |
4892 | | /* update markers struct */ |
4893 | | /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); |
4894 | | */ |
4895 | | assert(0 && "TODO"); |
4896 | | #endif /* USE_JPWL */ |
4897 | | /* <<UniPG */ |
4898 | | /*}*/ |
4899 | | /* << INDEX */ |
4900 | |
|
4901 | 0 | if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) { |
4902 | 0 | p_tile_coder->tcd_image->tiles->packno = 0; |
4903 | | #ifdef deadcode |
4904 | | if (l_cstr_info) { |
4905 | | l_cstr_info->packno = 0; |
4906 | | } |
4907 | | #endif |
4908 | 0 | } |
4909 | |
|
4910 | 0 | *p_data_written = 0; |
4911 | |
|
4912 | 0 | if (p_j2k->m_specific_param.m_encoder.m_PLT) { |
4913 | 0 | marker_info = opj_tcd_marker_info_create( |
4914 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT); |
4915 | 0 | if (marker_info == NULL) { |
4916 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4917 | 0 | "Cannot encode tile: opj_tcd_marker_info_create() failed\n"); |
4918 | 0 | return OPJ_FALSE; |
4919 | 0 | } |
4920 | 0 | } |
4921 | | |
4922 | 0 | if (l_remaining_data < |
4923 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT) { |
4924 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
4925 | 0 | "Not enough bytes in output buffer to write SOD marker\n"); |
4926 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4927 | 0 | return OPJ_FALSE; |
4928 | 0 | } |
4929 | 0 | l_remaining_data -= p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT; |
4930 | |
|
4931 | 0 | if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, |
4932 | 0 | p_data + 2, |
4933 | 0 | p_data_written, l_remaining_data, l_cstr_info, |
4934 | 0 | marker_info, |
4935 | 0 | p_manager)) { |
4936 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n"); |
4937 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4938 | 0 | return OPJ_FALSE; |
4939 | 0 | } |
4940 | | |
4941 | | /* For SOD */ |
4942 | 0 | *p_data_written += 2; |
4943 | |
|
4944 | 0 | if (p_j2k->m_specific_param.m_encoder.m_PLT) { |
4945 | 0 | OPJ_UINT32 l_data_written_PLT = 0; |
4946 | 0 | OPJ_BYTE* p_PLT_buffer = (OPJ_BYTE*)opj_malloc( |
4947 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT); |
4948 | 0 | if (!p_PLT_buffer) { |
4949 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Cannot allocate memory\n"); |
4950 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4951 | 0 | return OPJ_FALSE; |
4952 | 0 | } |
4953 | 0 | if (!opj_j2k_write_plt_in_memory(p_j2k, |
4954 | 0 | marker_info, |
4955 | 0 | p_PLT_buffer, |
4956 | 0 | &l_data_written_PLT, |
4957 | 0 | p_manager)) { |
4958 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4959 | 0 | opj_free(p_PLT_buffer); |
4960 | 0 | return OPJ_FALSE; |
4961 | 0 | } |
4962 | | |
4963 | 0 | assert(l_data_written_PLT <= |
4964 | 0 | p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT); |
4965 | | |
4966 | | /* Move PLT marker(s) before SOD */ |
4967 | 0 | memmove(p_data + l_data_written_PLT, p_data, *p_data_written); |
4968 | 0 | memcpy(p_data, p_PLT_buffer, l_data_written_PLT); |
4969 | 0 | opj_free(p_PLT_buffer); |
4970 | 0 | *p_data_written += l_data_written_PLT; |
4971 | 0 | } |
4972 | | |
4973 | 0 | opj_tcd_marker_info_destroy(marker_info); |
4974 | |
|
4975 | 0 | return OPJ_TRUE; |
4976 | 0 | } |
4977 | | |
4978 | | static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, |
4979 | | opj_stream_private_t *p_stream, |
4980 | | opj_event_mgr_t * p_manager |
4981 | | ) |
4982 | 9.80k | { |
4983 | 9.80k | OPJ_SIZE_T l_current_read_size; |
4984 | 9.80k | opj_codestream_index_t * l_cstr_index = 00; |
4985 | 9.80k | OPJ_BYTE ** l_current_data = 00; |
4986 | 9.80k | opj_tcp_t * l_tcp = 00; |
4987 | 9.80k | OPJ_UINT32 * l_tile_len = 00; |
4988 | 9.80k | OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; |
4989 | | |
4990 | | /* preconditions */ |
4991 | 9.80k | assert(p_j2k != 00); |
4992 | 9.80k | assert(p_manager != 00); |
4993 | 9.80k | assert(p_stream != 00); |
4994 | | |
4995 | 9.80k | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
4996 | | |
4997 | 9.80k | if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
4998 | | /* opj_stream_get_number_byte_left returns OPJ_OFF_T |
4999 | | // but we are in the last tile part, |
5000 | | // so its result will fit on OPJ_UINT32 unless we find |
5001 | | // a file with a single tile part of more than 4 GB...*/ |
5002 | 6.76k | p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)( |
5003 | 6.76k | opj_stream_get_number_byte_left(p_stream) - 2); |
5004 | 6.76k | } else { |
5005 | | /* Check to avoid pass the limit of OPJ_UINT32 */ |
5006 | 3.04k | if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) { |
5007 | 3.00k | p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; |
5008 | 3.00k | } else { |
5009 | | /* MSD: case commented to support empty SOT marker (PHR data) */ |
5010 | 40 | } |
5011 | 3.04k | } |
5012 | | |
5013 | 9.80k | l_current_data = &(l_tcp->m_data); |
5014 | 9.80k | l_tile_len = &l_tcp->m_data_size; |
5015 | | |
5016 | | /* Patch to support new PHR data */ |
5017 | 9.80k | if (p_j2k->m_specific_param.m_decoder.m_sot_length) { |
5018 | | /* If we are here, we'll try to read the data after allocation */ |
5019 | | /* Check enough bytes left in stream before allocation */ |
5020 | 9.75k | if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > |
5021 | 9.75k | opj_stream_get_number_byte_left(p_stream)) { |
5022 | 42 | if (p_j2k->m_cp.strict) { |
5023 | 42 | opj_event_msg(p_manager, EVT_ERROR, |
5024 | 42 | "Tile part length size inconsistent with stream length\n"); |
5025 | 42 | return OPJ_FALSE; |
5026 | 42 | } else { |
5027 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
5028 | 0 | "Tile part length size inconsistent with stream length\n"); |
5029 | 0 | } |
5030 | 42 | } |
5031 | 9.71k | if (p_j2k->m_specific_param.m_decoder.m_sot_length > |
5032 | 9.71k | UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) { |
5033 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
5034 | 0 | "p_j2k->m_specific_param.m_decoder.m_sot_length > " |
5035 | 0 | "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA"); |
5036 | 0 | return OPJ_FALSE; |
5037 | 0 | } |
5038 | | /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */ |
5039 | | /* do so that opj_mqc_init_dec_common() can safely add a synthetic */ |
5040 | | /* 0xFFFF marker. */ |
5041 | 9.71k | if (! *l_current_data) { |
5042 | | /* LH: oddly enough, in this path, l_tile_len!=0. |
5043 | | * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...). |
5044 | | */ |
5045 | 9.42k | *l_current_data = (OPJ_BYTE*) opj_malloc( |
5046 | 9.42k | p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA); |
5047 | 9.42k | } else { |
5048 | 293 | OPJ_BYTE *l_new_current_data; |
5049 | 293 | if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - |
5050 | 293 | p_j2k->m_specific_param.m_decoder.m_sot_length) { |
5051 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
5052 | 0 | "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - " |
5053 | 0 | "p_j2k->m_specific_param.m_decoder.m_sot_length"); |
5054 | 0 | return OPJ_FALSE; |
5055 | 0 | } |
5056 | | |
5057 | 293 | l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, |
5058 | 293 | *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length + |
5059 | 293 | OPJ_COMMON_CBLK_DATA_EXTRA); |
5060 | 293 | if (! l_new_current_data) { |
5061 | 0 | opj_free(*l_current_data); |
5062 | | /*nothing more is done as l_current_data will be set to null, and just |
5063 | | afterward we enter in the error path |
5064 | | and the actual tile_len is updated (committed) at the end of the |
5065 | | function. */ |
5066 | 0 | } |
5067 | 293 | *l_current_data = l_new_current_data; |
5068 | 293 | } |
5069 | | |
5070 | 9.71k | if (*l_current_data == 00) { |
5071 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n"); |
5072 | 0 | return OPJ_FALSE; |
5073 | 0 | } |
5074 | 9.71k | } else { |
5075 | 50 | l_sot_length_pb_detected = OPJ_TRUE; |
5076 | 50 | } |
5077 | | |
5078 | | /* Index */ |
5079 | 9.76k | l_cstr_index = p_j2k->cstr_index; |
5080 | 9.76k | { |
5081 | 9.76k | OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; |
5082 | | |
5083 | 9.76k | OPJ_UINT32 l_current_tile_part = |
5084 | 9.76k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; |
5085 | 9.76k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header |
5086 | 9.76k | = |
5087 | 9.76k | l_current_pos; |
5088 | 9.76k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos |
5089 | 9.76k | = |
5090 | 9.76k | l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; |
5091 | | |
5092 | 9.76k | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
5093 | 9.76k | l_cstr_index, |
5094 | 9.76k | J2K_MS_SOD, |
5095 | 9.76k | l_current_pos, |
5096 | 9.76k | p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) { |
5097 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); |
5098 | 0 | return OPJ_FALSE; |
5099 | 0 | } |
5100 | | |
5101 | | /*l_cstr_index->packno = 0;*/ |
5102 | 9.76k | } |
5103 | | |
5104 | | /* Patch to support new PHR data */ |
5105 | 9.76k | if (!l_sot_length_pb_detected) { |
5106 | 9.71k | l_current_read_size = opj_stream_read_data( |
5107 | 9.71k | p_stream, |
5108 | 9.71k | *l_current_data + *l_tile_len, |
5109 | 9.71k | p_j2k->m_specific_param.m_decoder.m_sot_length, |
5110 | 9.71k | p_manager); |
5111 | 9.71k | } else { |
5112 | 50 | l_current_read_size = 0; |
5113 | 50 | } |
5114 | | |
5115 | 9.76k | if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
5116 | 0 | if (l_current_read_size == (OPJ_SIZE_T)(-1)) { |
5117 | | /* Avoid issue of https://github.com/uclouvain/openjpeg/issues/1533 */ |
5118 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
5119 | 0 | return OPJ_FALSE; |
5120 | 0 | } |
5121 | 0 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
5122 | 9.76k | } else { |
5123 | 9.76k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
5124 | 9.76k | } |
5125 | | |
5126 | 9.76k | *l_tile_len += (OPJ_UINT32)l_current_read_size; |
5127 | | |
5128 | 9.76k | return OPJ_TRUE; |
5129 | 9.76k | } |
5130 | | |
5131 | | static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, |
5132 | | OPJ_UINT32 p_tile_no, |
5133 | | OPJ_UINT32 p_comp_no, |
5134 | | OPJ_UINT32 nb_comps, |
5135 | | opj_stream_private_t *p_stream, |
5136 | | opj_event_mgr_t * p_manager |
5137 | | ) |
5138 | 0 | { |
5139 | 0 | OPJ_BYTE * l_current_data = 00; |
5140 | 0 | OPJ_UINT32 l_rgn_size; |
5141 | 0 | opj_cp_t *l_cp = 00; |
5142 | 0 | opj_tcp_t *l_tcp = 00; |
5143 | 0 | opj_tccp_t *l_tccp = 00; |
5144 | 0 | OPJ_UINT32 l_comp_room; |
5145 | | |
5146 | | /* preconditions */ |
5147 | 0 | assert(p_j2k != 00); |
5148 | 0 | assert(p_manager != 00); |
5149 | 0 | assert(p_stream != 00); |
5150 | |
|
5151 | 0 | l_cp = &(p_j2k->m_cp); |
5152 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
5153 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
5154 | |
|
5155 | 0 | if (nb_comps <= 256) { |
5156 | 0 | l_comp_room = 1; |
5157 | 0 | } else { |
5158 | 0 | l_comp_room = 2; |
5159 | 0 | } |
5160 | |
|
5161 | 0 | l_rgn_size = 6 + l_comp_room; |
5162 | |
|
5163 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
5164 | |
|
5165 | 0 | opj_write_bytes(l_current_data, J2K_MS_RGN, |
5166 | 0 | 2); /* RGN */ |
5167 | 0 | l_current_data += 2; |
5168 | |
|
5169 | 0 | opj_write_bytes(l_current_data, l_rgn_size - 2, |
5170 | 0 | 2); /* Lrgn */ |
5171 | 0 | l_current_data += 2; |
5172 | |
|
5173 | 0 | opj_write_bytes(l_current_data, p_comp_no, |
5174 | 0 | l_comp_room); /* Crgn */ |
5175 | 0 | l_current_data += l_comp_room; |
5176 | |
|
5177 | 0 | opj_write_bytes(l_current_data, 0, |
5178 | 0 | 1); /* Srgn */ |
5179 | 0 | ++l_current_data; |
5180 | |
|
5181 | 0 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift, |
5182 | 0 | 1); /* SPrgn */ |
5183 | 0 | ++l_current_data; |
5184 | |
|
5185 | 0 | if (opj_stream_write_data(p_stream, |
5186 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_rgn_size, |
5187 | 0 | p_manager) != l_rgn_size) { |
5188 | 0 | return OPJ_FALSE; |
5189 | 0 | } |
5190 | | |
5191 | 0 | return OPJ_TRUE; |
5192 | 0 | } |
5193 | | |
5194 | | static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, |
5195 | | opj_stream_private_t *p_stream, |
5196 | | opj_event_mgr_t * p_manager |
5197 | | ) |
5198 | 0 | { |
5199 | | /* preconditions */ |
5200 | 0 | assert(p_j2k != 00); |
5201 | 0 | assert(p_manager != 00); |
5202 | 0 | assert(p_stream != 00); |
5203 | |
|
5204 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data, |
5205 | 0 | J2K_MS_EOC, 2); /* EOC */ |
5206 | | |
5207 | | /* UniPG>> */ |
5208 | | #ifdef USE_JPWL |
5209 | | /* update markers struct */ |
5210 | | /* |
5211 | | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); |
5212 | | */ |
5213 | | #endif /* USE_JPWL */ |
5214 | |
|
5215 | 0 | if (opj_stream_write_data(p_stream, |
5216 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, 2, p_manager) != 2) { |
5217 | 0 | return OPJ_FALSE; |
5218 | 0 | } |
5219 | | |
5220 | 0 | if (! opj_stream_flush(p_stream, p_manager)) { |
5221 | 0 | return OPJ_FALSE; |
5222 | 0 | } |
5223 | | |
5224 | 0 | return OPJ_TRUE; |
5225 | 0 | } |
5226 | | |
5227 | | /** |
5228 | | * Reads a RGN marker (Region Of Interest) |
5229 | | * |
5230 | | * @param p_header_data the data contained in the POC box. |
5231 | | * @param p_j2k the jpeg2000 codec. |
5232 | | * @param p_header_size the size of the data contained in the POC marker. |
5233 | | * @param p_manager the user event manager. |
5234 | | */ |
5235 | | static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, |
5236 | | OPJ_BYTE * p_header_data, |
5237 | | OPJ_UINT32 p_header_size, |
5238 | | opj_event_mgr_t * p_manager |
5239 | | ) |
5240 | 1.44k | { |
5241 | 1.44k | OPJ_UINT32 l_nb_comp; |
5242 | 1.44k | opj_image_t * l_image = 00; |
5243 | | |
5244 | 1.44k | opj_cp_t *l_cp = 00; |
5245 | 1.44k | opj_tcp_t *l_tcp = 00; |
5246 | 1.44k | OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; |
5247 | | |
5248 | | /* preconditions*/ |
5249 | 1.44k | assert(p_header_data != 00); |
5250 | 1.44k | assert(p_j2k != 00); |
5251 | 1.44k | assert(p_manager != 00); |
5252 | | |
5253 | 1.44k | l_image = p_j2k->m_private_image; |
5254 | 1.44k | l_nb_comp = l_image->numcomps; |
5255 | | |
5256 | 1.44k | if (l_nb_comp <= 256) { |
5257 | 1.44k | l_comp_room = 1; |
5258 | 1.44k | } else { |
5259 | 0 | l_comp_room = 2; |
5260 | 0 | } |
5261 | | |
5262 | 1.44k | if (p_header_size != 2 + l_comp_room) { |
5263 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n"); |
5264 | 0 | return OPJ_FALSE; |
5265 | 0 | } |
5266 | | |
5267 | 1.44k | l_cp = &(p_j2k->m_cp); |
5268 | 1.44k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
5269 | 43 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
5270 | 1.44k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5271 | | |
5272 | 1.44k | opj_read_bytes(p_header_data, &l_comp_no, l_comp_room); /* Crgn */ |
5273 | 1.44k | p_header_data += l_comp_room; |
5274 | 1.44k | opj_read_bytes(p_header_data, &l_roi_sty, |
5275 | 1.44k | 1); /* Srgn */ |
5276 | 1.44k | ++p_header_data; |
5277 | | |
5278 | | #ifdef USE_JPWL |
5279 | | if (l_cp->correct) { |
5280 | | /* totlen is negative or larger than the bytes left!!! */ |
5281 | | if (l_comp_room >= l_nb_comp) { |
5282 | | opj_event_msg(p_manager, EVT_ERROR, |
5283 | | "JPWL: bad component number in RGN (%d when there are only %d)\n", |
5284 | | l_comp_room, l_nb_comp); |
5285 | | if (!JPWL_ASSUME) { |
5286 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
5287 | | return OPJ_FALSE; |
5288 | | } |
5289 | | } |
5290 | | }; |
5291 | | #endif /* USE_JPWL */ |
5292 | | |
5293 | | /* testcase 3635.pdf.asan.77.2930 */ |
5294 | 1.44k | if (l_comp_no >= l_nb_comp) { |
5295 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
5296 | 1 | "bad component number in RGN (%d when there are only %d)\n", |
5297 | 1 | l_comp_no, l_nb_comp); |
5298 | 1 | return OPJ_FALSE; |
5299 | 1 | } |
5300 | | |
5301 | 1.44k | opj_read_bytes(p_header_data, |
5302 | 1.44k | (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1); /* SPrgn */ |
5303 | 1.44k | ++p_header_data; |
5304 | | |
5305 | 1.44k | return OPJ_TRUE; |
5306 | | |
5307 | 1.44k | } |
5308 | | |
5309 | | static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp) |
5310 | 0 | { |
5311 | 0 | return (OPJ_FLOAT32)((p_tcp->m_nb_tile_parts - 1) * 14); |
5312 | 0 | } |
5313 | | |
5314 | | static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp) |
5315 | 0 | { |
5316 | 0 | (void)p_tcp; |
5317 | 0 | return 0; |
5318 | 0 | } |
5319 | | |
5320 | | static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, |
5321 | | opj_stream_private_t *p_stream, |
5322 | | opj_event_mgr_t * p_manager) |
5323 | 0 | { |
5324 | 0 | opj_cp_t * l_cp = 00; |
5325 | 0 | opj_image_t * l_image = 00; |
5326 | 0 | opj_tcp_t * l_tcp = 00; |
5327 | 0 | opj_image_comp_t * l_img_comp = 00; |
5328 | |
|
5329 | 0 | OPJ_UINT32 i, j, k; |
5330 | 0 | OPJ_INT32 l_x0, l_y0, l_x1, l_y1; |
5331 | 0 | OPJ_FLOAT32 * l_rates = 0; |
5332 | 0 | OPJ_FLOAT32 l_sot_remove; |
5333 | 0 | OPJ_UINT32 l_bits_empty, l_size_pixel; |
5334 | 0 | OPJ_UINT64 l_tile_size = 0; |
5335 | 0 | OPJ_UINT32 l_last_res; |
5336 | 0 | OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00; |
5337 | | |
5338 | | /* preconditions */ |
5339 | 0 | assert(p_j2k != 00); |
5340 | 0 | assert(p_manager != 00); |
5341 | 0 | assert(p_stream != 00); |
5342 | |
|
5343 | 0 | OPJ_UNUSED(p_manager); |
5344 | |
|
5345 | 0 | l_cp = &(p_j2k->m_cp); |
5346 | 0 | l_image = p_j2k->m_private_image; |
5347 | 0 | l_tcp = l_cp->tcps; |
5348 | |
|
5349 | 0 | l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; |
5350 | 0 | l_size_pixel = l_image->numcomps * l_image->comps->prec; |
5351 | 0 | l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)( |
5352 | 0 | l_cp->th * l_cp->tw); |
5353 | |
|
5354 | 0 | if (l_cp->m_specific_param.m_enc.m_tp_on) { |
5355 | 0 | l_tp_stride_func = opj_j2k_get_tp_stride; |
5356 | 0 | } else { |
5357 | 0 | l_tp_stride_func = opj_j2k_get_default_stride; |
5358 | 0 | } |
5359 | |
|
5360 | 0 | for (i = 0; i < l_cp->th; ++i) { |
5361 | 0 | for (j = 0; j < l_cp->tw; ++j) { |
5362 | 0 | OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / |
5363 | 0 | (OPJ_FLOAT32)l_tcp->numlayers; |
5364 | | |
5365 | | /* 4 borders of the tile rescale on the image if necessary */ |
5366 | 0 | l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), |
5367 | 0 | (OPJ_INT32)l_image->x0); |
5368 | 0 | l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), |
5369 | 0 | (OPJ_INT32)l_image->y0); |
5370 | 0 | l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), |
5371 | 0 | (OPJ_INT32)l_image->x1); |
5372 | 0 | l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), |
5373 | 0 | (OPJ_INT32)l_image->y1); |
5374 | |
|
5375 | 0 | l_rates = l_tcp->rates; |
5376 | | |
5377 | | /* Modification of the RATE >> */ |
5378 | 0 | for (k = 0; k < l_tcp->numlayers; ++k) { |
5379 | 0 | if (*l_rates > 0.0f) { |
5380 | 0 | *l_rates = (OPJ_FLOAT32)(((OPJ_FLOAT64)l_size_pixel * (OPJ_UINT32)( |
5381 | 0 | l_x1 - l_x0) * |
5382 | 0 | (OPJ_UINT32)(l_y1 - l_y0)) |
5383 | 0 | / ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)) |
5384 | 0 | - |
5385 | 0 | l_offset; |
5386 | 0 | } |
5387 | |
|
5388 | 0 | ++l_rates; |
5389 | 0 | } |
5390 | |
|
5391 | 0 | ++l_tcp; |
5392 | |
|
5393 | 0 | } |
5394 | 0 | } |
5395 | |
|
5396 | 0 | l_tcp = l_cp->tcps; |
5397 | |
|
5398 | 0 | for (i = 0; i < l_cp->th; ++i) { |
5399 | 0 | for (j = 0; j < l_cp->tw; ++j) { |
5400 | 0 | l_rates = l_tcp->rates; |
5401 | |
|
5402 | 0 | if (*l_rates > 0.0f) { |
5403 | 0 | *l_rates -= l_sot_remove; |
5404 | |
|
5405 | 0 | if (*l_rates < 30.0f) { |
5406 | 0 | *l_rates = 30.0f; |
5407 | 0 | } |
5408 | 0 | } |
5409 | |
|
5410 | 0 | ++l_rates; |
5411 | |
|
5412 | 0 | l_last_res = l_tcp->numlayers - 1; |
5413 | |
|
5414 | 0 | for (k = 1; k < l_last_res; ++k) { |
5415 | |
|
5416 | 0 | if (*l_rates > 0.0f) { |
5417 | 0 | *l_rates -= l_sot_remove; |
5418 | |
|
5419 | 0 | if (*l_rates < * (l_rates - 1) + 10.0f) { |
5420 | 0 | *l_rates = (*(l_rates - 1)) + 20.0f; |
5421 | 0 | } |
5422 | 0 | } |
5423 | |
|
5424 | 0 | ++l_rates; |
5425 | 0 | } |
5426 | |
|
5427 | 0 | if (*l_rates > 0.0f) { |
5428 | 0 | *l_rates -= (l_sot_remove + 2.f); |
5429 | |
|
5430 | 0 | if (*l_rates < * (l_rates - 1) + 10.0f) { |
5431 | 0 | *l_rates = (*(l_rates - 1)) + 20.0f; |
5432 | 0 | } |
5433 | 0 | } |
5434 | |
|
5435 | 0 | ++l_tcp; |
5436 | 0 | } |
5437 | 0 | } |
5438 | |
|
5439 | 0 | l_img_comp = l_image->comps; |
5440 | 0 | l_tile_size = 0; |
5441 | |
|
5442 | 0 | for (i = 0; i < l_image->numcomps; ++i) { |
5443 | 0 | l_tile_size += (OPJ_UINT64)opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx) |
5444 | 0 | * |
5445 | 0 | opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy) |
5446 | 0 | * |
5447 | 0 | l_img_comp->prec; |
5448 | |
|
5449 | 0 | ++l_img_comp; |
5450 | 0 | } |
5451 | | |
5452 | | /* TODO: where does this magic value come from ? */ |
5453 | | /* This used to be 1.3 / 8, but with random data and very small code */ |
5454 | | /* block sizes, this is not enough. For example with */ |
5455 | | /* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */ |
5456 | | /* TODO revise this to take into account the overhead linked to the */ |
5457 | | /* number of packets and number of code blocks in packets */ |
5458 | 0 | l_tile_size = (OPJ_UINT64)((double)l_tile_size * 1.4 / 8); |
5459 | | |
5460 | | /* Arbitrary amount to make the following work: */ |
5461 | | /* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */ |
5462 | 0 | l_tile_size += 500; |
5463 | |
|
5464 | 0 | l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k); |
5465 | |
|
5466 | 0 | if (l_tile_size > UINT_MAX) { |
5467 | 0 | l_tile_size = UINT_MAX; |
5468 | 0 | } |
5469 | |
|
5470 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = (OPJ_UINT32)l_tile_size; |
5471 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = |
5472 | 0 | (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); |
5473 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { |
5474 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
5475 | 0 | "Not enough memory to allocate m_encoded_tile_data. %u MB required\n", |
5476 | 0 | (OPJ_UINT32)(l_tile_size / 1024 / 1024)); |
5477 | 0 | return OPJ_FALSE; |
5478 | 0 | } |
5479 | | |
5480 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
5481 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = |
5482 | 0 | (OPJ_BYTE *) opj_malloc(6 * |
5483 | 0 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
5484 | 0 | if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
5485 | 0 | return OPJ_FALSE; |
5486 | 0 | } |
5487 | | |
5488 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = |
5489 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; |
5490 | 0 | } |
5491 | | |
5492 | 0 | return OPJ_TRUE; |
5493 | 0 | } |
5494 | | |
5495 | | #if 0 |
5496 | | static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, |
5497 | | opj_stream_private_t *p_stream, |
5498 | | opj_event_mgr_t * p_manager) |
5499 | | { |
5500 | | OPJ_UINT32 i; |
5501 | | opj_tcd_t * l_tcd = 00; |
5502 | | OPJ_UINT32 l_nb_tiles; |
5503 | | opj_tcp_t * l_tcp = 00; |
5504 | | OPJ_BOOL l_success; |
5505 | | |
5506 | | /* preconditions */ |
5507 | | assert(p_j2k != 00); |
5508 | | assert(p_manager != 00); |
5509 | | assert(p_stream != 00); |
5510 | | |
5511 | | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
5512 | | l_tcp = p_j2k->m_cp.tcps; |
5513 | | |
5514 | | l_tcd = opj_tcd_create(OPJ_TRUE); |
5515 | | if (l_tcd == 00) { |
5516 | | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
5517 | | return OPJ_FALSE; |
5518 | | } |
5519 | | |
5520 | | for (i = 0; i < l_nb_tiles; ++i) { |
5521 | | if (l_tcp->m_data) { |
5522 | | if (! opj_tcd_init_decode_tile(l_tcd, i)) { |
5523 | | opj_tcd_destroy(l_tcd); |
5524 | | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
5525 | | return OPJ_FALSE; |
5526 | | } |
5527 | | |
5528 | | l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, |
5529 | | p_j2k->cstr_index); |
5530 | | /* cleanup */ |
5531 | | |
5532 | | if (! l_success) { |
5533 | | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
5534 | | break; |
5535 | | } |
5536 | | } |
5537 | | |
5538 | | opj_j2k_tcp_destroy(l_tcp); |
5539 | | ++l_tcp; |
5540 | | } |
5541 | | |
5542 | | opj_tcd_destroy(l_tcd); |
5543 | | return OPJ_TRUE; |
5544 | | } |
5545 | | #endif |
5546 | | |
5547 | | static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, |
5548 | | struct opj_stream_private *p_stream, |
5549 | | struct opj_event_mgr * p_manager) |
5550 | 0 | { |
5551 | | /* preconditions */ |
5552 | 0 | assert(p_j2k != 00); |
5553 | 0 | assert(p_manager != 00); |
5554 | 0 | assert(p_stream != 00); |
5555 | |
|
5556 | 0 | OPJ_UNUSED(p_manager); |
5557 | |
|
5558 | 0 | p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream); |
5559 | |
|
5560 | 0 | return OPJ_TRUE; |
5561 | 0 | } |
5562 | | |
5563 | | static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, |
5564 | | struct opj_stream_private *p_stream, |
5565 | | struct opj_event_mgr * p_manager) |
5566 | 0 | { |
5567 | 0 | OPJ_UINT32 i; |
5568 | 0 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
5569 | 0 | opj_mct_data_t * l_mct_record; |
5570 | 0 | opj_tcp_t * l_tcp; |
5571 | | |
5572 | | /* preconditions */ |
5573 | 0 | assert(p_j2k != 00); |
5574 | 0 | assert(p_stream != 00); |
5575 | 0 | assert(p_manager != 00); |
5576 | |
|
5577 | 0 | if (! opj_j2k_write_cbd(p_j2k, p_stream, p_manager)) { |
5578 | 0 | return OPJ_FALSE; |
5579 | 0 | } |
5580 | | |
5581 | 0 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
5582 | 0 | l_mct_record = l_tcp->m_mct_records; |
5583 | |
|
5584 | 0 | for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { |
5585 | |
|
5586 | 0 | if (! opj_j2k_write_mct_record(p_j2k, l_mct_record, p_stream, p_manager)) { |
5587 | 0 | return OPJ_FALSE; |
5588 | 0 | } |
5589 | | |
5590 | 0 | ++l_mct_record; |
5591 | 0 | } |
5592 | | |
5593 | 0 | l_mcc_record = l_tcp->m_mcc_records; |
5594 | |
|
5595 | 0 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5596 | |
|
5597 | 0 | if (! opj_j2k_write_mcc_record(p_j2k, l_mcc_record, p_stream, p_manager)) { |
5598 | 0 | return OPJ_FALSE; |
5599 | 0 | } |
5600 | | |
5601 | 0 | ++l_mcc_record; |
5602 | 0 | } |
5603 | | |
5604 | 0 | if (! opj_j2k_write_mco(p_j2k, p_stream, p_manager)) { |
5605 | 0 | return OPJ_FALSE; |
5606 | 0 | } |
5607 | | |
5608 | 0 | return OPJ_TRUE; |
5609 | 0 | } |
5610 | | |
5611 | | static OPJ_BOOL opj_j2k_write_all_coc( |
5612 | | opj_j2k_t *p_j2k, |
5613 | | struct opj_stream_private *p_stream, |
5614 | | struct opj_event_mgr * p_manager) |
5615 | 0 | { |
5616 | 0 | OPJ_UINT32 compno; |
5617 | | |
5618 | | /* preconditions */ |
5619 | 0 | assert(p_j2k != 00); |
5620 | 0 | assert(p_manager != 00); |
5621 | 0 | assert(p_stream != 00); |
5622 | |
|
5623 | 0 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5624 | | /* cod is first component of first tile */ |
5625 | 0 | if (! opj_j2k_compare_coc(p_j2k, 0, compno)) { |
5626 | 0 | if (! opj_j2k_write_coc(p_j2k, compno, p_stream, p_manager)) { |
5627 | 0 | return OPJ_FALSE; |
5628 | 0 | } |
5629 | 0 | } |
5630 | 0 | } |
5631 | | |
5632 | 0 | return OPJ_TRUE; |
5633 | 0 | } |
5634 | | |
5635 | | static OPJ_BOOL opj_j2k_write_all_qcc( |
5636 | | opj_j2k_t *p_j2k, |
5637 | | struct opj_stream_private *p_stream, |
5638 | | struct opj_event_mgr * p_manager) |
5639 | 0 | { |
5640 | 0 | OPJ_UINT32 compno; |
5641 | | |
5642 | | /* preconditions */ |
5643 | 0 | assert(p_j2k != 00); |
5644 | 0 | assert(p_manager != 00); |
5645 | 0 | assert(p_stream != 00); |
5646 | |
|
5647 | 0 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5648 | | /* qcd is first component of first tile */ |
5649 | 0 | if (! opj_j2k_compare_qcc(p_j2k, 0, compno)) { |
5650 | 0 | if (! opj_j2k_write_qcc(p_j2k, compno, p_stream, p_manager)) { |
5651 | 0 | return OPJ_FALSE; |
5652 | 0 | } |
5653 | 0 | } |
5654 | 0 | } |
5655 | 0 | return OPJ_TRUE; |
5656 | 0 | } |
5657 | | |
5658 | | static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, |
5659 | | struct opj_stream_private *p_stream, |
5660 | | struct opj_event_mgr * p_manager) |
5661 | 0 | { |
5662 | 0 | OPJ_UINT32 compno; |
5663 | 0 | const opj_tccp_t *l_tccp = 00; |
5664 | | |
5665 | | /* preconditions */ |
5666 | 0 | assert(p_j2k != 00); |
5667 | 0 | assert(p_manager != 00); |
5668 | 0 | assert(p_stream != 00); |
5669 | |
|
5670 | 0 | l_tccp = p_j2k->m_cp.tcps->tccps; |
5671 | |
|
5672 | 0 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5673 | 0 | if (l_tccp->roishift) { |
5674 | |
|
5675 | 0 | if (! opj_j2k_write_rgn(p_j2k, 0, compno, p_j2k->m_private_image->numcomps, |
5676 | 0 | p_stream, p_manager)) { |
5677 | 0 | return OPJ_FALSE; |
5678 | 0 | } |
5679 | 0 | } |
5680 | | |
5681 | 0 | ++l_tccp; |
5682 | 0 | } |
5683 | | |
5684 | 0 | return OPJ_TRUE; |
5685 | 0 | } |
5686 | | |
5687 | | static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, |
5688 | | struct opj_stream_private *p_stream, |
5689 | | struct opj_event_mgr * p_manager) |
5690 | 0 | { |
5691 | 0 | opj_codestream_index_t * l_cstr_index = 00; |
5692 | | |
5693 | | /* preconditions */ |
5694 | 0 | assert(p_j2k != 00); |
5695 | 0 | assert(p_manager != 00); |
5696 | 0 | assert(p_stream != 00); |
5697 | |
|
5698 | 0 | OPJ_UNUSED(p_manager); |
5699 | |
|
5700 | 0 | l_cstr_index = p_j2k->cstr_index; |
5701 | 0 | if (l_cstr_index) { |
5702 | 0 | l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream); |
5703 | | /* UniPG>> */ |
5704 | | /* The following adjustment is done to adjust the codestream size */ |
5705 | | /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ |
5706 | | /* the first bunch of bytes is not in the codestream */ |
5707 | 0 | l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start; |
5708 | | /* <<UniPG */ |
5709 | 0 | } |
5710 | |
|
5711 | | #ifdef USE_JPWL |
5712 | | /* preparation of JPWL marker segments */ |
5713 | | #if 0 |
5714 | | if (cp->epc_on) { |
5715 | | |
5716 | | /* encode according to JPWL */ |
5717 | | jpwl_encode(p_j2k, p_stream, image); |
5718 | | |
5719 | | } |
5720 | | #endif |
5721 | | assert(0 && "TODO"); |
5722 | | #endif /* USE_JPWL */ |
5723 | |
|
5724 | 0 | return OPJ_TRUE; |
5725 | 0 | } |
5726 | | |
5727 | | static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, |
5728 | | opj_stream_private_t *p_stream, |
5729 | | OPJ_UINT32 *output_marker, |
5730 | | opj_event_mgr_t * p_manager |
5731 | | ) |
5732 | 52.8k | { |
5733 | 52.8k | OPJ_UINT32 l_unknown_marker; |
5734 | 52.8k | const opj_dec_memory_marker_handler_t * l_marker_handler; |
5735 | 52.8k | OPJ_UINT32 l_size_unk = 2; |
5736 | | |
5737 | | /* preconditions*/ |
5738 | 52.8k | assert(p_j2k != 00); |
5739 | 52.8k | assert(p_manager != 00); |
5740 | 52.8k | assert(p_stream != 00); |
5741 | | |
5742 | 52.8k | opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); |
5743 | | |
5744 | 1.18M | for (;;) { |
5745 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
5746 | 1.18M | if (opj_stream_read_data(p_stream, |
5747 | 1.18M | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
5748 | 237 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
5749 | 237 | return OPJ_FALSE; |
5750 | 237 | } |
5751 | | |
5752 | | /* read 2 bytes as the new marker ID*/ |
5753 | 1.18M | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
5754 | 1.18M | &l_unknown_marker, 2); |
5755 | | |
5756 | 1.18M | if (!(l_unknown_marker < 0xff00)) { |
5757 | | |
5758 | | /* Get the marker handler from the marker ID*/ |
5759 | 252k | l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); |
5760 | | |
5761 | 252k | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
5762 | 5 | opj_event_msg(p_manager, EVT_ERROR, |
5763 | 5 | "Marker is not compliant with its position\n"); |
5764 | 5 | return OPJ_FALSE; |
5765 | 252k | } else { |
5766 | 252k | if (l_marker_handler->id != J2K_MS_UNK) { |
5767 | | /* Add the marker to the codestream index*/ |
5768 | 52.6k | if (l_marker_handler->id != J2K_MS_SOT) { |
5769 | 52.3k | OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, |
5770 | 52.3k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, |
5771 | 52.3k | l_size_unk); |
5772 | 52.3k | if (res == OPJ_FALSE) { |
5773 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
5774 | 0 | return OPJ_FALSE; |
5775 | 0 | } |
5776 | 52.3k | } |
5777 | 52.6k | break; /* next marker is known and well located */ |
5778 | 200k | } else { |
5779 | 200k | l_size_unk += 2; |
5780 | 200k | } |
5781 | 252k | } |
5782 | 252k | } |
5783 | 1.18M | } |
5784 | | |
5785 | 52.6k | *output_marker = l_marker_handler->id ; |
5786 | | |
5787 | 52.6k | return OPJ_TRUE; |
5788 | 52.8k | } |
5789 | | |
5790 | | static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, |
5791 | | opj_mct_data_t * p_mct_record, |
5792 | | struct opj_stream_private *p_stream, |
5793 | | struct opj_event_mgr * p_manager) |
5794 | 0 | { |
5795 | 0 | OPJ_UINT32 l_mct_size; |
5796 | 0 | OPJ_BYTE * l_current_data = 00; |
5797 | 0 | OPJ_UINT32 l_tmp; |
5798 | | |
5799 | | /* preconditions */ |
5800 | 0 | assert(p_j2k != 00); |
5801 | 0 | assert(p_manager != 00); |
5802 | 0 | assert(p_stream != 00); |
5803 | |
|
5804 | 0 | l_mct_size = 10 + p_mct_record->m_data_size; |
5805 | |
|
5806 | 0 | if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
5807 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
5808 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size); |
5809 | 0 | if (! new_header_tile_data) { |
5810 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
5811 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
5812 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
5813 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n"); |
5814 | 0 | return OPJ_FALSE; |
5815 | 0 | } |
5816 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
5817 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; |
5818 | 0 | } |
5819 | | |
5820 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
5821 | |
|
5822 | 0 | opj_write_bytes(l_current_data, J2K_MS_MCT, |
5823 | 0 | 2); /* MCT */ |
5824 | 0 | l_current_data += 2; |
5825 | |
|
5826 | 0 | opj_write_bytes(l_current_data, l_mct_size - 2, |
5827 | 0 | 2); /* Lmct */ |
5828 | 0 | l_current_data += 2; |
5829 | |
|
5830 | 0 | opj_write_bytes(l_current_data, 0, |
5831 | 0 | 2); /* Zmct */ |
5832 | 0 | l_current_data += 2; |
5833 | | |
5834 | | /* only one marker atm */ |
5835 | 0 | l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | |
5836 | 0 | (p_mct_record->m_element_type << 10); |
5837 | |
|
5838 | 0 | opj_write_bytes(l_current_data, l_tmp, 2); |
5839 | 0 | l_current_data += 2; |
5840 | |
|
5841 | 0 | opj_write_bytes(l_current_data, 0, |
5842 | 0 | 2); /* Ymct */ |
5843 | 0 | l_current_data += 2; |
5844 | |
|
5845 | 0 | memcpy(l_current_data, p_mct_record->m_data, p_mct_record->m_data_size); |
5846 | |
|
5847 | 0 | if (opj_stream_write_data(p_stream, |
5848 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size, |
5849 | 0 | p_manager) != l_mct_size) { |
5850 | 0 | return OPJ_FALSE; |
5851 | 0 | } |
5852 | | |
5853 | 0 | return OPJ_TRUE; |
5854 | 0 | } |
5855 | | |
5856 | | /** |
5857 | | * Reads a MCT marker (Multiple Component Transform) |
5858 | | * |
5859 | | * @param p_header_data the data contained in the MCT box. |
5860 | | * @param p_j2k the jpeg2000 codec. |
5861 | | * @param p_header_size the size of the data contained in the MCT marker. |
5862 | | * @param p_manager the user event manager. |
5863 | | */ |
5864 | | static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, |
5865 | | OPJ_BYTE * p_header_data, |
5866 | | OPJ_UINT32 p_header_size, |
5867 | | opj_event_mgr_t * p_manager |
5868 | | ) |
5869 | 8.27k | { |
5870 | 8.27k | OPJ_UINT32 i; |
5871 | 8.27k | opj_tcp_t *l_tcp = 00; |
5872 | 8.27k | OPJ_UINT32 l_tmp; |
5873 | 8.27k | OPJ_UINT32 l_indix; |
5874 | 8.27k | opj_mct_data_t * l_mct_data; |
5875 | | |
5876 | | /* preconditions */ |
5877 | 8.27k | assert(p_header_data != 00); |
5878 | 8.27k | assert(p_j2k != 00); |
5879 | | |
5880 | 8.27k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
5881 | 34 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
5882 | 8.27k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5883 | | |
5884 | 8.27k | if (p_header_size < 2) { |
5885 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5886 | 0 | return OPJ_FALSE; |
5887 | 0 | } |
5888 | | |
5889 | | /* first marker */ |
5890 | 8.27k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmct */ |
5891 | 8.27k | p_header_data += 2; |
5892 | 8.27k | if (l_tmp != 0) { |
5893 | 1.22k | opj_event_msg(p_manager, EVT_WARNING, |
5894 | 1.22k | "Cannot take in charge mct data within multiple MCT records\n"); |
5895 | 1.22k | return OPJ_TRUE; |
5896 | 1.22k | } |
5897 | | |
5898 | 7.04k | if (p_header_size <= 6) { |
5899 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5900 | 0 | return OPJ_FALSE; |
5901 | 0 | } |
5902 | | |
5903 | | /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ |
5904 | 7.04k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Imct */ |
5905 | 7.04k | p_header_data += 2; |
5906 | | |
5907 | 7.04k | l_indix = l_tmp & 0xff; |
5908 | 7.04k | l_mct_data = l_tcp->m_mct_records; |
5909 | | |
5910 | 19.4k | for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { |
5911 | 18.5k | if (l_mct_data->m_index == l_indix) { |
5912 | 6.15k | break; |
5913 | 6.15k | } |
5914 | 12.4k | ++l_mct_data; |
5915 | 12.4k | } |
5916 | | |
5917 | | /* NOT FOUND */ |
5918 | 7.04k | if (i == l_tcp->m_nb_mct_records) { |
5919 | 899 | if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { |
5920 | 27 | opj_mct_data_t *new_mct_records; |
5921 | 27 | l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
5922 | | |
5923 | 27 | new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, |
5924 | 27 | l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
5925 | 27 | if (! new_mct_records) { |
5926 | 0 | opj_free(l_tcp->m_mct_records); |
5927 | 0 | l_tcp->m_mct_records = NULL; |
5928 | 0 | l_tcp->m_nb_max_mct_records = 0; |
5929 | 0 | l_tcp->m_nb_mct_records = 0; |
5930 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n"); |
5931 | 0 | return OPJ_FALSE; |
5932 | 0 | } |
5933 | | |
5934 | | /* Update m_mcc_records[].m_offset_array and m_decorrelation_array |
5935 | | * to point to the new addresses */ |
5936 | 27 | if (new_mct_records != l_tcp->m_mct_records) { |
5937 | 47 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5938 | 34 | opj_simple_mcc_decorrelation_data_t* l_mcc_record = |
5939 | 34 | &(l_tcp->m_mcc_records[i]); |
5940 | 34 | if (l_mcc_record->m_decorrelation_array) { |
5941 | 21 | l_mcc_record->m_decorrelation_array = |
5942 | 21 | new_mct_records + |
5943 | 21 | (l_mcc_record->m_decorrelation_array - |
5944 | 21 | l_tcp->m_mct_records); |
5945 | 21 | } |
5946 | 34 | if (l_mcc_record->m_offset_array) { |
5947 | 20 | l_mcc_record->m_offset_array = |
5948 | 20 | new_mct_records + |
5949 | 20 | (l_mcc_record->m_offset_array - |
5950 | 20 | l_tcp->m_mct_records); |
5951 | 20 | } |
5952 | 34 | } |
5953 | 13 | } |
5954 | | |
5955 | 27 | l_tcp->m_mct_records = new_mct_records; |
5956 | 27 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5957 | 27 | memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * |
5958 | 27 | sizeof(opj_mct_data_t)); |
5959 | 27 | } |
5960 | | |
5961 | 899 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5962 | 899 | ++l_tcp->m_nb_mct_records; |
5963 | 899 | } |
5964 | | |
5965 | 7.04k | if (l_mct_data->m_data) { |
5966 | 3.63k | opj_free(l_mct_data->m_data); |
5967 | 3.63k | l_mct_data->m_data = 00; |
5968 | 3.63k | l_mct_data->m_data_size = 0; |
5969 | 3.63k | } |
5970 | | |
5971 | 7.04k | l_mct_data->m_index = l_indix; |
5972 | 7.04k | l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); |
5973 | 7.04k | l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); |
5974 | | |
5975 | 7.04k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymct */ |
5976 | 7.04k | p_header_data += 2; |
5977 | 7.04k | if (l_tmp != 0) { |
5978 | 2.92k | opj_event_msg(p_manager, EVT_WARNING, |
5979 | 2.92k | "Cannot take in charge multiple MCT markers\n"); |
5980 | 2.92k | return OPJ_TRUE; |
5981 | 2.92k | } |
5982 | | |
5983 | 4.12k | p_header_size -= 6; |
5984 | | |
5985 | 4.12k | l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); |
5986 | 4.12k | if (! l_mct_data->m_data) { |
5987 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5988 | 0 | return OPJ_FALSE; |
5989 | 0 | } |
5990 | 4.12k | memcpy(l_mct_data->m_data, p_header_data, p_header_size); |
5991 | | |
5992 | 4.12k | l_mct_data->m_data_size = p_header_size; |
5993 | | |
5994 | 4.12k | return OPJ_TRUE; |
5995 | 4.12k | } |
5996 | | |
5997 | | static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, |
5998 | | struct opj_simple_mcc_decorrelation_data * p_mcc_record, |
5999 | | struct opj_stream_private *p_stream, |
6000 | | struct opj_event_mgr * p_manager) |
6001 | 0 | { |
6002 | 0 | OPJ_UINT32 i; |
6003 | 0 | OPJ_UINT32 l_mcc_size; |
6004 | 0 | OPJ_BYTE * l_current_data = 00; |
6005 | 0 | OPJ_UINT32 l_nb_bytes_for_comp; |
6006 | 0 | OPJ_UINT32 l_mask; |
6007 | 0 | OPJ_UINT32 l_tmcc; |
6008 | | |
6009 | | /* preconditions */ |
6010 | 0 | assert(p_j2k != 00); |
6011 | 0 | assert(p_manager != 00); |
6012 | 0 | assert(p_stream != 00); |
6013 | |
|
6014 | 0 | if (p_mcc_record->m_nb_comps > 255) { |
6015 | 0 | l_nb_bytes_for_comp = 2; |
6016 | 0 | l_mask = 0x8000; |
6017 | 0 | } else { |
6018 | 0 | l_nb_bytes_for_comp = 1; |
6019 | 0 | l_mask = 0; |
6020 | 0 | } |
6021 | |
|
6022 | 0 | l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; |
6023 | 0 | if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
6024 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
6025 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size); |
6026 | 0 | if (! new_header_tile_data) { |
6027 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
6028 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
6029 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
6030 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n"); |
6031 | 0 | return OPJ_FALSE; |
6032 | 0 | } |
6033 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
6034 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; |
6035 | 0 | } |
6036 | | |
6037 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
6038 | |
|
6039 | 0 | opj_write_bytes(l_current_data, J2K_MS_MCC, |
6040 | 0 | 2); /* MCC */ |
6041 | 0 | l_current_data += 2; |
6042 | |
|
6043 | 0 | opj_write_bytes(l_current_data, l_mcc_size - 2, |
6044 | 0 | 2); /* Lmcc */ |
6045 | 0 | l_current_data += 2; |
6046 | | |
6047 | | /* first marker */ |
6048 | 0 | opj_write_bytes(l_current_data, 0, |
6049 | 0 | 2); /* Zmcc */ |
6050 | 0 | l_current_data += 2; |
6051 | |
|
6052 | 0 | opj_write_bytes(l_current_data, p_mcc_record->m_index, |
6053 | 0 | 1); /* Imcc -> no need for other values, take the first */ |
6054 | 0 | ++l_current_data; |
6055 | | |
6056 | | /* only one marker atm */ |
6057 | 0 | opj_write_bytes(l_current_data, 0, |
6058 | 0 | 2); /* Ymcc */ |
6059 | 0 | l_current_data += 2; |
6060 | |
|
6061 | 0 | opj_write_bytes(l_current_data, 1, |
6062 | 0 | 2); /* Qmcc -> number of collections -> 1 */ |
6063 | 0 | l_current_data += 2; |
6064 | |
|
6065 | 0 | opj_write_bytes(l_current_data, 0x1, |
6066 | 0 | 1); /* Xmcci type of component transformation -> array based decorrelation */ |
6067 | 0 | ++l_current_data; |
6068 | |
|
6069 | 0 | opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, |
6070 | 0 | 2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ |
6071 | 0 | l_current_data += 2; |
6072 | |
|
6073 | 0 | for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { |
6074 | 0 | opj_write_bytes(l_current_data, i, |
6075 | 0 | l_nb_bytes_for_comp); /* Cmccij Component offset*/ |
6076 | 0 | l_current_data += l_nb_bytes_for_comp; |
6077 | 0 | } |
6078 | |
|
6079 | 0 | opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, |
6080 | 0 | 2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ |
6081 | 0 | l_current_data += 2; |
6082 | |
|
6083 | 0 | for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { |
6084 | 0 | opj_write_bytes(l_current_data, i, |
6085 | 0 | l_nb_bytes_for_comp); /* Wmccij Component offset*/ |
6086 | 0 | l_current_data += l_nb_bytes_for_comp; |
6087 | 0 | } |
6088 | |
|
6089 | 0 | l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16; |
6090 | |
|
6091 | 0 | if (p_mcc_record->m_decorrelation_array) { |
6092 | 0 | l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; |
6093 | 0 | } |
6094 | |
|
6095 | 0 | if (p_mcc_record->m_offset_array) { |
6096 | 0 | l_tmcc |= ((p_mcc_record->m_offset_array->m_index) << 8); |
6097 | 0 | } |
6098 | |
|
6099 | 0 | opj_write_bytes(l_current_data, l_tmcc, |
6100 | 0 | 3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ |
6101 | 0 | l_current_data += 3; |
6102 | |
|
6103 | 0 | if (opj_stream_write_data(p_stream, |
6104 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size, |
6105 | 0 | p_manager) != l_mcc_size) { |
6106 | 0 | return OPJ_FALSE; |
6107 | 0 | } |
6108 | | |
6109 | 0 | return OPJ_TRUE; |
6110 | 0 | } |
6111 | | |
6112 | | static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, |
6113 | | OPJ_BYTE * p_header_data, |
6114 | | OPJ_UINT32 p_header_size, |
6115 | | opj_event_mgr_t * p_manager) |
6116 | 24.8k | { |
6117 | 24.8k | OPJ_UINT32 i, j; |
6118 | 24.8k | OPJ_UINT32 l_tmp; |
6119 | 24.8k | OPJ_UINT32 l_indix; |
6120 | 24.8k | opj_tcp_t * l_tcp; |
6121 | 24.8k | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6122 | 24.8k | opj_mct_data_t * l_mct_data; |
6123 | 24.8k | OPJ_UINT32 l_nb_collections; |
6124 | 24.8k | OPJ_UINT32 l_nb_comps; |
6125 | 24.8k | OPJ_UINT32 l_nb_bytes_by_comp; |
6126 | 24.8k | OPJ_BOOL l_new_mcc = OPJ_FALSE; |
6127 | | |
6128 | | /* preconditions */ |
6129 | 24.8k | assert(p_header_data != 00); |
6130 | 24.8k | assert(p_j2k != 00); |
6131 | 24.8k | assert(p_manager != 00); |
6132 | | |
6133 | 24.8k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
6134 | 3 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
6135 | 24.8k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
6136 | | |
6137 | 24.8k | if (p_header_size < 2) { |
6138 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6139 | 1 | return OPJ_FALSE; |
6140 | 1 | } |
6141 | | |
6142 | | /* first marker */ |
6143 | 24.8k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmcc */ |
6144 | 24.8k | p_header_data += 2; |
6145 | 24.8k | if (l_tmp != 0) { |
6146 | 2.44k | opj_event_msg(p_manager, EVT_WARNING, |
6147 | 2.44k | "Cannot take in charge multiple data spanning\n"); |
6148 | 2.44k | return OPJ_TRUE; |
6149 | 2.44k | } |
6150 | | |
6151 | 22.4k | if (p_header_size < 7) { |
6152 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6153 | 1 | return OPJ_FALSE; |
6154 | 1 | } |
6155 | | |
6156 | 22.4k | opj_read_bytes(p_header_data, &l_indix, |
6157 | 22.4k | 1); /* Imcc -> no need for other values, take the first */ |
6158 | 22.4k | ++p_header_data; |
6159 | | |
6160 | 22.4k | l_mcc_record = l_tcp->m_mcc_records; |
6161 | | |
6162 | 73.7k | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
6163 | 65.7k | if (l_mcc_record->m_index == l_indix) { |
6164 | 14.5k | break; |
6165 | 14.5k | } |
6166 | 51.2k | ++l_mcc_record; |
6167 | 51.2k | } |
6168 | | |
6169 | | /** NOT FOUND */ |
6170 | 22.4k | if (i == l_tcp->m_nb_mcc_records) { |
6171 | 7.93k | if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { |
6172 | 0 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
6173 | 0 | l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
6174 | |
|
6175 | 0 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
6176 | 0 | l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof( |
6177 | 0 | opj_simple_mcc_decorrelation_data_t)); |
6178 | 0 | if (! new_mcc_records) { |
6179 | 0 | opj_free(l_tcp->m_mcc_records); |
6180 | 0 | l_tcp->m_mcc_records = NULL; |
6181 | 0 | l_tcp->m_nb_max_mcc_records = 0; |
6182 | 0 | l_tcp->m_nb_mcc_records = 0; |
6183 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n"); |
6184 | 0 | return OPJ_FALSE; |
6185 | 0 | } |
6186 | 0 | l_tcp->m_mcc_records = new_mcc_records; |
6187 | 0 | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
6188 | 0 | memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records) |
6189 | 0 | * sizeof(opj_simple_mcc_decorrelation_data_t)); |
6190 | 0 | } |
6191 | 7.93k | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
6192 | 7.93k | l_new_mcc = OPJ_TRUE; |
6193 | 7.93k | } |
6194 | 22.4k | l_mcc_record->m_index = l_indix; |
6195 | | |
6196 | | /* only one marker atm */ |
6197 | 22.4k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymcc */ |
6198 | 22.4k | p_header_data += 2; |
6199 | 22.4k | if (l_tmp != 0) { |
6200 | 2.68k | opj_event_msg(p_manager, EVT_WARNING, |
6201 | 2.68k | "Cannot take in charge multiple data spanning\n"); |
6202 | 2.68k | return OPJ_TRUE; |
6203 | 2.68k | } |
6204 | | |
6205 | 19.7k | opj_read_bytes(p_header_data, &l_nb_collections, |
6206 | 19.7k | 2); /* Qmcc -> number of collections -> 1 */ |
6207 | 19.7k | p_header_data += 2; |
6208 | | |
6209 | 19.7k | if (l_nb_collections > 1) { |
6210 | 2.41k | opj_event_msg(p_manager, EVT_WARNING, |
6211 | 2.41k | "Cannot take in charge multiple collections\n"); |
6212 | 2.41k | return OPJ_TRUE; |
6213 | 2.41k | } |
6214 | | |
6215 | 17.3k | p_header_size -= 7; |
6216 | | |
6217 | 24.5k | for (i = 0; i < l_nb_collections; ++i) { |
6218 | 15.9k | if (p_header_size < 3) { |
6219 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6220 | 1 | return OPJ_FALSE; |
6221 | 1 | } |
6222 | | |
6223 | 15.9k | opj_read_bytes(p_header_data, &l_tmp, |
6224 | 15.9k | 1); /* Xmcci type of component transformation -> array based decorrelation */ |
6225 | 15.9k | ++p_header_data; |
6226 | | |
6227 | 15.9k | if (l_tmp != 1) { |
6228 | 2.39k | opj_event_msg(p_manager, EVT_WARNING, |
6229 | 2.39k | "Cannot take in charge collections other than array decorrelation\n"); |
6230 | 2.39k | return OPJ_TRUE; |
6231 | 2.39k | } |
6232 | | |
6233 | 13.5k | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
6234 | | |
6235 | 13.5k | p_header_data += 2; |
6236 | 13.5k | p_header_size -= 3; |
6237 | | |
6238 | 13.5k | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
6239 | 13.5k | l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; |
6240 | | |
6241 | 13.5k | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { |
6242 | 6 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6243 | 6 | return OPJ_FALSE; |
6244 | 6 | } |
6245 | | |
6246 | 13.5k | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); |
6247 | | |
6248 | 23.8k | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
6249 | 12.9k | opj_read_bytes(p_header_data, &l_tmp, |
6250 | 12.9k | l_nb_bytes_by_comp); /* Cmccij Component offset*/ |
6251 | 12.9k | p_header_data += l_nb_bytes_by_comp; |
6252 | | |
6253 | 12.9k | if (l_tmp != j) { |
6254 | 2.66k | opj_event_msg(p_manager, EVT_WARNING, |
6255 | 2.66k | "Cannot take in charge collections with indix shuffle\n"); |
6256 | 2.66k | return OPJ_TRUE; |
6257 | 2.66k | } |
6258 | 12.9k | } |
6259 | | |
6260 | 10.8k | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
6261 | 10.8k | p_header_data += 2; |
6262 | | |
6263 | 10.8k | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
6264 | 10.8k | l_nb_comps &= 0x7fff; |
6265 | | |
6266 | 10.8k | if (l_nb_comps != l_mcc_record->m_nb_comps) { |
6267 | 2.94k | opj_event_msg(p_manager, EVT_WARNING, |
6268 | 2.94k | "Cannot take in charge collections without same number of indixes\n"); |
6269 | 2.94k | return OPJ_TRUE; |
6270 | 2.94k | } |
6271 | | |
6272 | 7.94k | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { |
6273 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6274 | 1 | return OPJ_FALSE; |
6275 | 1 | } |
6276 | | |
6277 | 7.94k | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); |
6278 | | |
6279 | 13.3k | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
6280 | 6.15k | opj_read_bytes(p_header_data, &l_tmp, |
6281 | 6.15k | l_nb_bytes_by_comp); /* Wmccij Component offset*/ |
6282 | 6.15k | p_header_data += l_nb_bytes_by_comp; |
6283 | | |
6284 | 6.15k | if (l_tmp != j) { |
6285 | 702 | opj_event_msg(p_manager, EVT_WARNING, |
6286 | 702 | "Cannot take in charge collections with indix shuffle\n"); |
6287 | 702 | return OPJ_TRUE; |
6288 | 702 | } |
6289 | 6.15k | } |
6290 | | |
6291 | 7.24k | opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/ |
6292 | 7.24k | p_header_data += 3; |
6293 | | |
6294 | 7.24k | l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1); |
6295 | 7.24k | l_mcc_record->m_decorrelation_array = 00; |
6296 | 7.24k | l_mcc_record->m_offset_array = 00; |
6297 | | |
6298 | 7.24k | l_indix = l_tmp & 0xff; |
6299 | 7.24k | if (l_indix != 0) { |
6300 | 6.61k | l_mct_data = l_tcp->m_mct_records; |
6301 | 15.8k | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6302 | 15.7k | if (l_mct_data->m_index == l_indix) { |
6303 | 6.60k | l_mcc_record->m_decorrelation_array = l_mct_data; |
6304 | 6.60k | break; |
6305 | 6.60k | } |
6306 | 9.19k | ++l_mct_data; |
6307 | 9.19k | } |
6308 | | |
6309 | 6.61k | if (l_mcc_record->m_decorrelation_array == 00) { |
6310 | 17 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6311 | 17 | return OPJ_FALSE; |
6312 | 17 | } |
6313 | 6.61k | } |
6314 | | |
6315 | 7.22k | l_indix = (l_tmp >> 8) & 0xff; |
6316 | 7.22k | if (l_indix != 0) { |
6317 | 1.97k | l_mct_data = l_tcp->m_mct_records; |
6318 | 3.68k | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6319 | 3.67k | if (l_mct_data->m_index == l_indix) { |
6320 | 1.96k | l_mcc_record->m_offset_array = l_mct_data; |
6321 | 1.96k | break; |
6322 | 1.96k | } |
6323 | 1.71k | ++l_mct_data; |
6324 | 1.71k | } |
6325 | | |
6326 | 1.97k | if (l_mcc_record->m_offset_array == 00) { |
6327 | 8 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6328 | 8 | return OPJ_FALSE; |
6329 | 8 | } |
6330 | 1.97k | } |
6331 | 7.22k | } |
6332 | | |
6333 | 8.60k | if (p_header_size != 0) { |
6334 | 7 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6335 | 7 | return OPJ_FALSE; |
6336 | 7 | } |
6337 | | |
6338 | 8.59k | if (l_new_mcc) { |
6339 | 621 | ++l_tcp->m_nb_mcc_records; |
6340 | 621 | } |
6341 | | |
6342 | 8.59k | return OPJ_TRUE; |
6343 | 8.60k | } |
6344 | | |
6345 | | static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, |
6346 | | struct opj_stream_private *p_stream, |
6347 | | struct opj_event_mgr * p_manager |
6348 | | ) |
6349 | 0 | { |
6350 | 0 | OPJ_BYTE * l_current_data = 00; |
6351 | 0 | OPJ_UINT32 l_mco_size; |
6352 | 0 | opj_tcp_t * l_tcp = 00; |
6353 | 0 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6354 | 0 | OPJ_UINT32 i; |
6355 | | |
6356 | | /* preconditions */ |
6357 | 0 | assert(p_j2k != 00); |
6358 | 0 | assert(p_manager != 00); |
6359 | 0 | assert(p_stream != 00); |
6360 | |
|
6361 | 0 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
6362 | |
|
6363 | 0 | l_mco_size = 5 + l_tcp->m_nb_mcc_records; |
6364 | 0 | if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
6365 | |
|
6366 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
6367 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size); |
6368 | 0 | if (! new_header_tile_data) { |
6369 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
6370 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
6371 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
6372 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n"); |
6373 | 0 | return OPJ_FALSE; |
6374 | 0 | } |
6375 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
6376 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; |
6377 | 0 | } |
6378 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
6379 | | |
6380 | |
|
6381 | 0 | opj_write_bytes(l_current_data, J2K_MS_MCO, 2); /* MCO */ |
6382 | 0 | l_current_data += 2; |
6383 | |
|
6384 | 0 | opj_write_bytes(l_current_data, l_mco_size - 2, 2); /* Lmco */ |
6385 | 0 | l_current_data += 2; |
6386 | |
|
6387 | 0 | opj_write_bytes(l_current_data, l_tcp->m_nb_mcc_records, |
6388 | 0 | 1); /* Nmco : only one transform stage*/ |
6389 | 0 | ++l_current_data; |
6390 | |
|
6391 | 0 | l_mcc_record = l_tcp->m_mcc_records; |
6392 | 0 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
6393 | 0 | opj_write_bytes(l_current_data, l_mcc_record->m_index, |
6394 | 0 | 1); /* Imco -> use the mcc indicated by 1*/ |
6395 | 0 | ++l_current_data; |
6396 | 0 | ++l_mcc_record; |
6397 | 0 | } |
6398 | |
|
6399 | 0 | if (opj_stream_write_data(p_stream, |
6400 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size, |
6401 | 0 | p_manager) != l_mco_size) { |
6402 | 0 | return OPJ_FALSE; |
6403 | 0 | } |
6404 | | |
6405 | 0 | return OPJ_TRUE; |
6406 | 0 | } |
6407 | | |
6408 | | /** |
6409 | | * Reads a MCO marker (Multiple Component Transform Ordering) |
6410 | | * |
6411 | | * @param p_header_data the data contained in the MCO box. |
6412 | | * @param p_j2k the jpeg2000 codec. |
6413 | | * @param p_header_size the size of the data contained in the MCO marker. |
6414 | | * @param p_manager the user event manager. |
6415 | | */ |
6416 | | static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, |
6417 | | OPJ_BYTE * p_header_data, |
6418 | | OPJ_UINT32 p_header_size, |
6419 | | opj_event_mgr_t * p_manager |
6420 | | ) |
6421 | 17.9k | { |
6422 | 17.9k | OPJ_UINT32 l_tmp, i; |
6423 | 17.9k | OPJ_UINT32 l_nb_stages; |
6424 | 17.9k | opj_tcp_t * l_tcp; |
6425 | 17.9k | opj_tccp_t * l_tccp; |
6426 | 17.9k | opj_image_t * l_image; |
6427 | | |
6428 | | /* preconditions */ |
6429 | 17.9k | assert(p_header_data != 00); |
6430 | 17.9k | assert(p_j2k != 00); |
6431 | 17.9k | assert(p_manager != 00); |
6432 | | |
6433 | 17.9k | l_image = p_j2k->m_private_image; |
6434 | 17.9k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
6435 | 41 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
6436 | 17.9k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
6437 | | |
6438 | 17.9k | if (p_header_size < 1) { |
6439 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n"); |
6440 | 2 | return OPJ_FALSE; |
6441 | 2 | } |
6442 | | |
6443 | 17.9k | opj_read_bytes(p_header_data, &l_nb_stages, |
6444 | 17.9k | 1); /* Nmco : only one transform stage*/ |
6445 | 17.9k | ++p_header_data; |
6446 | | |
6447 | 17.9k | if (l_nb_stages > 1) { |
6448 | 2.40k | opj_event_msg(p_manager, EVT_WARNING, |
6449 | 2.40k | "Cannot take in charge multiple transformation stages.\n"); |
6450 | 2.40k | return OPJ_TRUE; |
6451 | 2.40k | } |
6452 | | |
6453 | 15.5k | if (p_header_size != l_nb_stages + 1) { |
6454 | 8 | opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n"); |
6455 | 8 | return OPJ_FALSE; |
6456 | 8 | } |
6457 | | |
6458 | 15.5k | l_tccp = l_tcp->tccps; |
6459 | | |
6460 | 47.7k | for (i = 0; i < l_image->numcomps; ++i) { |
6461 | 32.2k | l_tccp->m_dc_level_shift = 0; |
6462 | 32.2k | ++l_tccp; |
6463 | 32.2k | } |
6464 | | |
6465 | 15.5k | if (l_tcp->m_mct_decoding_matrix) { |
6466 | 6.03k | opj_free(l_tcp->m_mct_decoding_matrix); |
6467 | 6.03k | l_tcp->m_mct_decoding_matrix = 00; |
6468 | 6.03k | } |
6469 | | |
6470 | 30.8k | for (i = 0; i < l_nb_stages; ++i) { |
6471 | 15.3k | opj_read_bytes(p_header_data, &l_tmp, 1); |
6472 | 15.3k | ++p_header_data; |
6473 | | |
6474 | 15.3k | if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) { |
6475 | 3 | return OPJ_FALSE; |
6476 | 3 | } |
6477 | 15.3k | } |
6478 | | |
6479 | 15.5k | return OPJ_TRUE; |
6480 | 15.5k | } |
6481 | | |
6482 | | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, |
6483 | | OPJ_UINT32 p_index) |
6484 | 15.3k | { |
6485 | 15.3k | OPJ_UINT32 i; |
6486 | 15.3k | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6487 | 15.3k | opj_mct_data_t * l_deco_array, * l_offset_array; |
6488 | 15.3k | OPJ_UINT32 l_data_size, l_mct_size, l_offset_size; |
6489 | 15.3k | OPJ_UINT32 l_nb_elem; |
6490 | 15.3k | OPJ_UINT32 * l_offset_data, * l_current_offset_data; |
6491 | 15.3k | opj_tccp_t * l_tccp; |
6492 | | |
6493 | | /* preconditions */ |
6494 | 15.3k | assert(p_tcp != 00); |
6495 | | |
6496 | 15.3k | l_mcc_record = p_tcp->m_mcc_records; |
6497 | | |
6498 | 25.3k | for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) { |
6499 | 18.1k | if (l_mcc_record->m_index == p_index) { |
6500 | 8.06k | break; |
6501 | 8.06k | } |
6502 | 18.1k | } |
6503 | | |
6504 | 15.3k | if (i == p_tcp->m_nb_mcc_records) { |
6505 | | /** element discarded **/ |
6506 | 7.23k | return OPJ_TRUE; |
6507 | 7.23k | } |
6508 | | |
6509 | 8.06k | if (l_mcc_record->m_nb_comps != p_image->numcomps) { |
6510 | | /** do not support number of comps != image */ |
6511 | 880 | return OPJ_TRUE; |
6512 | 880 | } |
6513 | | |
6514 | 7.18k | l_deco_array = l_mcc_record->m_decorrelation_array; |
6515 | | |
6516 | 7.18k | if (l_deco_array) { |
6517 | 6.14k | l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps |
6518 | 6.14k | * p_image->numcomps; |
6519 | 6.14k | if (l_deco_array->m_data_size != l_data_size) { |
6520 | 2 | return OPJ_FALSE; |
6521 | 2 | } |
6522 | | |
6523 | 6.13k | l_nb_elem = p_image->numcomps * p_image->numcomps; |
6524 | 6.13k | l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32); |
6525 | 6.13k | p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
6526 | | |
6527 | 6.13k | if (! p_tcp->m_mct_decoding_matrix) { |
6528 | 0 | return OPJ_FALSE; |
6529 | 0 | } |
6530 | | |
6531 | 6.13k | j2k_mct_read_functions_to_float[l_deco_array->m_element_type]( |
6532 | 6.13k | l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem); |
6533 | 6.13k | } |
6534 | | |
6535 | 7.18k | l_offset_array = l_mcc_record->m_offset_array; |
6536 | | |
6537 | 7.18k | if (l_offset_array) { |
6538 | 1.49k | l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * |
6539 | 1.49k | p_image->numcomps; |
6540 | 1.49k | if (l_offset_array->m_data_size != l_data_size) { |
6541 | 1 | return OPJ_FALSE; |
6542 | 1 | } |
6543 | | |
6544 | 1.49k | l_nb_elem = p_image->numcomps; |
6545 | 1.49k | l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32); |
6546 | 1.49k | l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); |
6547 | | |
6548 | 1.49k | if (! l_offset_data) { |
6549 | 0 | return OPJ_FALSE; |
6550 | 0 | } |
6551 | | |
6552 | 1.49k | j2k_mct_read_functions_to_int32[l_offset_array->m_element_type]( |
6553 | 1.49k | l_offset_array->m_data, l_offset_data, l_nb_elem); |
6554 | | |
6555 | 1.49k | l_tccp = p_tcp->tccps; |
6556 | 1.49k | l_current_offset_data = l_offset_data; |
6557 | | |
6558 | 3.73k | for (i = 0; i < p_image->numcomps; ++i) { |
6559 | 2.24k | l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++); |
6560 | 2.24k | ++l_tccp; |
6561 | 2.24k | } |
6562 | | |
6563 | 1.49k | opj_free(l_offset_data); |
6564 | 1.49k | } |
6565 | | |
6566 | 7.18k | return OPJ_TRUE; |
6567 | 7.18k | } |
6568 | | |
6569 | | static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, |
6570 | | struct opj_stream_private *p_stream, |
6571 | | struct opj_event_mgr * p_manager) |
6572 | 0 | { |
6573 | 0 | OPJ_UINT32 i; |
6574 | 0 | OPJ_UINT32 l_cbd_size; |
6575 | 0 | OPJ_BYTE * l_current_data = 00; |
6576 | 0 | opj_image_t *l_image = 00; |
6577 | 0 | opj_image_comp_t * l_comp = 00; |
6578 | | |
6579 | | /* preconditions */ |
6580 | 0 | assert(p_j2k != 00); |
6581 | 0 | assert(p_manager != 00); |
6582 | 0 | assert(p_stream != 00); |
6583 | |
|
6584 | 0 | l_image = p_j2k->m_private_image; |
6585 | 0 | l_cbd_size = 6 + p_j2k->m_private_image->numcomps; |
6586 | |
|
6587 | 0 | if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
6588 | 0 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
6589 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size); |
6590 | 0 | if (! new_header_tile_data) { |
6591 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
6592 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
6593 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
6594 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n"); |
6595 | 0 | return OPJ_FALSE; |
6596 | 0 | } |
6597 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
6598 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; |
6599 | 0 | } |
6600 | | |
6601 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
6602 | |
|
6603 | 0 | opj_write_bytes(l_current_data, J2K_MS_CBD, 2); /* CBD */ |
6604 | 0 | l_current_data += 2; |
6605 | |
|
6606 | 0 | opj_write_bytes(l_current_data, l_cbd_size - 2, 2); /* L_CBD */ |
6607 | 0 | l_current_data += 2; |
6608 | |
|
6609 | 0 | opj_write_bytes(l_current_data, l_image->numcomps, 2); /* Ncbd */ |
6610 | 0 | l_current_data += 2; |
6611 | |
|
6612 | 0 | l_comp = l_image->comps; |
6613 | |
|
6614 | 0 | for (i = 0; i < l_image->numcomps; ++i) { |
6615 | 0 | opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), |
6616 | 0 | 1); /* Component bit depth */ |
6617 | 0 | ++l_current_data; |
6618 | |
|
6619 | 0 | ++l_comp; |
6620 | 0 | } |
6621 | |
|
6622 | 0 | if (opj_stream_write_data(p_stream, |
6623 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size, |
6624 | 0 | p_manager) != l_cbd_size) { |
6625 | 0 | return OPJ_FALSE; |
6626 | 0 | } |
6627 | | |
6628 | 0 | return OPJ_TRUE; |
6629 | 0 | } |
6630 | | |
6631 | | /** |
6632 | | * Reads a CBD marker (Component bit depth definition) |
6633 | | * @param p_header_data the data contained in the CBD box. |
6634 | | * @param p_j2k the jpeg2000 codec. |
6635 | | * @param p_header_size the size of the data contained in the CBD marker. |
6636 | | * @param p_manager the user event manager. |
6637 | | */ |
6638 | | static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, |
6639 | | OPJ_BYTE * p_header_data, |
6640 | | OPJ_UINT32 p_header_size, |
6641 | | opj_event_mgr_t * p_manager |
6642 | | ) |
6643 | 15 | { |
6644 | 15 | OPJ_UINT32 l_nb_comp, l_num_comp; |
6645 | 15 | OPJ_UINT32 l_comp_def; |
6646 | 15 | OPJ_UINT32 i; |
6647 | 15 | opj_image_comp_t * l_comp = 00; |
6648 | | |
6649 | | /* preconditions */ |
6650 | 15 | assert(p_header_data != 00); |
6651 | 15 | assert(p_j2k != 00); |
6652 | 15 | assert(p_manager != 00); |
6653 | | |
6654 | 15 | l_num_comp = p_j2k->m_private_image->numcomps; |
6655 | | |
6656 | 15 | if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { |
6657 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
6658 | 0 | return OPJ_FALSE; |
6659 | 0 | } |
6660 | | |
6661 | 15 | opj_read_bytes(p_header_data, &l_nb_comp, |
6662 | 15 | 2); /* Ncbd */ |
6663 | 15 | p_header_data += 2; |
6664 | | |
6665 | 15 | if (l_nb_comp != l_num_comp) { |
6666 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
6667 | 0 | return OPJ_FALSE; |
6668 | 0 | } |
6669 | | |
6670 | 15 | l_comp = p_j2k->m_private_image->comps; |
6671 | 30 | for (i = 0; i < l_num_comp; ++i) { |
6672 | 15 | opj_read_bytes(p_header_data, &l_comp_def, |
6673 | 15 | 1); /* Component bit depth */ |
6674 | 15 | ++p_header_data; |
6675 | 15 | l_comp->sgnd = (l_comp_def >> 7) & 1; |
6676 | 15 | l_comp->prec = (l_comp_def & 0x7f) + 1; |
6677 | | |
6678 | 15 | if (l_comp->prec > 31) { |
6679 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
6680 | 0 | "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", |
6681 | 0 | i, l_comp->prec); |
6682 | 0 | return OPJ_FALSE; |
6683 | 0 | } |
6684 | 15 | ++l_comp; |
6685 | 15 | } |
6686 | | |
6687 | 15 | return OPJ_TRUE; |
6688 | 15 | } |
6689 | | |
6690 | | /** |
6691 | | * Reads a CAP marker (extended capabilities definition). Empty implementation. |
6692 | | * Found in HTJ2K files. |
6693 | | * |
6694 | | * @param p_header_data the data contained in the CAP box. |
6695 | | * @param p_j2k the jpeg2000 codec. |
6696 | | * @param p_header_size the size of the data contained in the CAP marker. |
6697 | | * @param p_manager the user event manager. |
6698 | | */ |
6699 | | static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k, |
6700 | | OPJ_BYTE * p_header_data, |
6701 | | OPJ_UINT32 p_header_size, |
6702 | | opj_event_mgr_t * p_manager |
6703 | | ) |
6704 | 112 | { |
6705 | | /* preconditions */ |
6706 | 112 | assert(p_header_data != 00); |
6707 | 112 | assert(p_j2k != 00); |
6708 | 112 | assert(p_manager != 00); |
6709 | | |
6710 | 112 | (void)p_j2k; |
6711 | 112 | (void)p_header_data; |
6712 | 112 | (void)p_header_size; |
6713 | 112 | (void)p_manager; |
6714 | | |
6715 | 112 | return OPJ_TRUE; |
6716 | 112 | } |
6717 | | |
6718 | | /** |
6719 | | * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files |
6720 | | * @param p_header_data the data contained in the CPF box. |
6721 | | * @param p_j2k the jpeg2000 codec. |
6722 | | * @param p_header_size the size of the data contained in the CPF marker. |
6723 | | * @param p_manager the user event manager. |
6724 | | */ |
6725 | | static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k, |
6726 | | OPJ_BYTE * p_header_data, |
6727 | | OPJ_UINT32 p_header_size, |
6728 | | opj_event_mgr_t * p_manager |
6729 | | ) |
6730 | 487 | { |
6731 | | /* preconditions */ |
6732 | 487 | assert(p_header_data != 00); |
6733 | 487 | assert(p_j2k != 00); |
6734 | 487 | assert(p_manager != 00); |
6735 | | |
6736 | 487 | (void)p_j2k; |
6737 | 487 | (void)p_header_data; |
6738 | 487 | (void)p_header_size; |
6739 | 487 | (void)p_manager; |
6740 | | |
6741 | 487 | return OPJ_TRUE; |
6742 | 487 | } |
6743 | | |
6744 | | /* ----------------------------------------------------------------------- */ |
6745 | | /* J2K / JPT decoder interface */ |
6746 | | /* ----------------------------------------------------------------------- */ |
6747 | | |
6748 | | void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) |
6749 | 8.24k | { |
6750 | 8.24k | if (j2k && parameters) { |
6751 | 8.24k | j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; |
6752 | 8.24k | j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; |
6753 | | |
6754 | 8.24k | j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG); |
6755 | | #ifdef USE_JPWL |
6756 | | j2k->m_cp.correct = parameters->jpwl_correct; |
6757 | | j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; |
6758 | | j2k->m_cp.max_tiles = parameters->jpwl_max_tiles; |
6759 | | #endif /* USE_JPWL */ |
6760 | 8.24k | } |
6761 | 8.24k | } |
6762 | | |
6763 | | void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict) |
6764 | 0 | { |
6765 | 0 | if (j2k) { |
6766 | 0 | j2k->m_cp.strict = strict; |
6767 | 0 | if (strict) { |
6768 | 0 | j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
6769 | 0 | } |
6770 | 0 | } |
6771 | 0 | } |
6772 | | |
6773 | | OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads) |
6774 | 0 | { |
6775 | | /* Currently we pass the thread-pool to the tcd, so we cannot re-set it */ |
6776 | | /* afterwards */ |
6777 | 0 | if (opj_has_thread_support() && j2k->m_tcd == NULL) { |
6778 | 0 | opj_thread_pool_destroy(j2k->m_tp); |
6779 | 0 | j2k->m_tp = NULL; |
6780 | 0 | if (num_threads <= (OPJ_UINT32)INT_MAX) { |
6781 | 0 | j2k->m_tp = opj_thread_pool_create((int)num_threads); |
6782 | 0 | } |
6783 | 0 | if (j2k->m_tp == NULL) { |
6784 | 0 | j2k->m_tp = opj_thread_pool_create(0); |
6785 | 0 | return OPJ_FALSE; |
6786 | 0 | } |
6787 | 0 | return OPJ_TRUE; |
6788 | 0 | } |
6789 | 0 | return OPJ_FALSE; |
6790 | 0 | } |
6791 | | |
6792 | | static int opj_j2k_get_default_thread_count(void) |
6793 | 8.24k | { |
6794 | 8.24k | const char* num_threads_str = getenv("OPJ_NUM_THREADS"); |
6795 | 8.24k | int num_cpus; |
6796 | 8.24k | int num_threads; |
6797 | | |
6798 | 8.24k | if (num_threads_str == NULL || !opj_has_thread_support()) { |
6799 | 8.24k | return 0; |
6800 | 8.24k | } |
6801 | 0 | num_cpus = opj_get_num_cpus(); |
6802 | 0 | if (strcmp(num_threads_str, "ALL_CPUS") == 0) { |
6803 | 0 | return num_cpus; |
6804 | 0 | } |
6805 | 0 | if (num_cpus == 0) { |
6806 | 0 | num_cpus = 32; |
6807 | 0 | } |
6808 | 0 | num_threads = atoi(num_threads_str); |
6809 | 0 | if (num_threads < 0) { |
6810 | 0 | num_threads = 0; |
6811 | 0 | } else if (num_threads > 2 * num_cpus) { |
6812 | 0 | num_threads = 2 * num_cpus; |
6813 | 0 | } |
6814 | 0 | return num_threads; |
6815 | 0 | } |
6816 | | |
6817 | | /* ----------------------------------------------------------------------- */ |
6818 | | /* J2K encoder interface */ |
6819 | | /* ----------------------------------------------------------------------- */ |
6820 | | |
6821 | | opj_j2k_t* opj_j2k_create_compress(void) |
6822 | 0 | { |
6823 | 0 | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
6824 | 0 | if (!l_j2k) { |
6825 | 0 | return NULL; |
6826 | 0 | } |
6827 | | |
6828 | | |
6829 | 0 | l_j2k->m_is_decoder = 0; |
6830 | 0 | l_j2k->m_cp.m_is_decoder = 0; |
6831 | |
|
6832 | 0 | l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc( |
6833 | 0 | OPJ_J2K_DEFAULT_HEADER_SIZE); |
6834 | 0 | if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
6835 | 0 | opj_j2k_destroy(l_j2k); |
6836 | 0 | return NULL; |
6837 | 0 | } |
6838 | | |
6839 | 0 | l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = |
6840 | 0 | OPJ_J2K_DEFAULT_HEADER_SIZE; |
6841 | | |
6842 | | /* validation list creation*/ |
6843 | 0 | l_j2k->m_validation_list = opj_procedure_list_create(); |
6844 | 0 | if (! l_j2k->m_validation_list) { |
6845 | 0 | opj_j2k_destroy(l_j2k); |
6846 | 0 | return NULL; |
6847 | 0 | } |
6848 | | |
6849 | | /* execution list creation*/ |
6850 | 0 | l_j2k->m_procedure_list = opj_procedure_list_create(); |
6851 | 0 | if (! l_j2k->m_procedure_list) { |
6852 | 0 | opj_j2k_destroy(l_j2k); |
6853 | 0 | return NULL; |
6854 | 0 | } |
6855 | | |
6856 | 0 | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
6857 | 0 | if (!l_j2k->m_tp) { |
6858 | 0 | l_j2k->m_tp = opj_thread_pool_create(0); |
6859 | 0 | } |
6860 | 0 | if (!l_j2k->m_tp) { |
6861 | 0 | opj_j2k_destroy(l_j2k); |
6862 | 0 | return NULL; |
6863 | 0 | } |
6864 | | |
6865 | 0 | return l_j2k; |
6866 | 0 | } |
6867 | | |
6868 | | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres) |
6869 | 0 | { |
6870 | 0 | POC[0].tile = 1; |
6871 | 0 | POC[0].resno0 = 0; |
6872 | 0 | POC[0].compno0 = 0; |
6873 | 0 | POC[0].layno1 = 1; |
6874 | 0 | POC[0].resno1 = (OPJ_UINT32)(numres - 1); |
6875 | 0 | POC[0].compno1 = 3; |
6876 | 0 | POC[0].prg1 = OPJ_CPRL; |
6877 | 0 | POC[1].tile = 1; |
6878 | 0 | POC[1].resno0 = (OPJ_UINT32)(numres - 1); |
6879 | 0 | POC[1].compno0 = 0; |
6880 | 0 | POC[1].layno1 = 1; |
6881 | 0 | POC[1].resno1 = (OPJ_UINT32)numres; |
6882 | 0 | POC[1].compno1 = 3; |
6883 | 0 | POC[1].prg1 = OPJ_CPRL; |
6884 | 0 | return 2; |
6885 | 0 | } |
6886 | | |
6887 | | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, |
6888 | | opj_image_t *image, opj_event_mgr_t *p_manager) |
6889 | 0 | { |
6890 | | /* Configure cinema parameters */ |
6891 | 0 | int i; |
6892 | | |
6893 | | /* No tiling */ |
6894 | 0 | parameters->tile_size_on = OPJ_FALSE; |
6895 | 0 | parameters->cp_tdx = 1; |
6896 | 0 | parameters->cp_tdy = 1; |
6897 | | |
6898 | | /* One tile part for each component */ |
6899 | 0 | parameters->tp_flag = 'C'; |
6900 | 0 | parameters->tp_on = 1; |
6901 | | |
6902 | | /* Tile and Image shall be at (0,0) */ |
6903 | 0 | parameters->cp_tx0 = 0; |
6904 | 0 | parameters->cp_ty0 = 0; |
6905 | 0 | parameters->image_offset_x0 = 0; |
6906 | 0 | parameters->image_offset_y0 = 0; |
6907 | | |
6908 | | /* Codeblock size= 32*32 */ |
6909 | 0 | parameters->cblockw_init = 32; |
6910 | 0 | parameters->cblockh_init = 32; |
6911 | | |
6912 | | /* Codeblock style: no mode switch enabled */ |
6913 | 0 | parameters->mode = 0; |
6914 | | |
6915 | | /* No ROI */ |
6916 | 0 | parameters->roi_compno = -1; |
6917 | | |
6918 | | /* No subsampling */ |
6919 | 0 | parameters->subsampling_dx = 1; |
6920 | 0 | parameters->subsampling_dy = 1; |
6921 | | |
6922 | | /* 9-7 transform */ |
6923 | 0 | parameters->irreversible = 1; |
6924 | | |
6925 | | /* Number of layers */ |
6926 | 0 | if (parameters->tcp_numlayers > 1) { |
6927 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6928 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6929 | 0 | "1 single quality layer" |
6930 | 0 | "-> Number of layers forced to 1 (rather than %d)\n" |
6931 | 0 | "-> Rate of the last layer (%3.1f) will be used", |
6932 | 0 | parameters->tcp_numlayers, |
6933 | 0 | parameters->tcp_rates[parameters->tcp_numlayers - 1]); |
6934 | 0 | parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1]; |
6935 | 0 | parameters->tcp_numlayers = 1; |
6936 | 0 | } |
6937 | | |
6938 | | /* Resolution levels */ |
6939 | 0 | switch (parameters->rsiz) { |
6940 | 0 | case OPJ_PROFILE_CINEMA_2K: |
6941 | 0 | if (parameters->numresolution > 6) { |
6942 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6943 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6944 | 0 | "Number of decomposition levels <= 5\n" |
6945 | 0 | "-> Number of decomposition levels forced to 5 (rather than %d)\n", |
6946 | 0 | parameters->numresolution + 1); |
6947 | 0 | parameters->numresolution = 6; |
6948 | 0 | } |
6949 | 0 | break; |
6950 | 0 | case OPJ_PROFILE_CINEMA_4K: |
6951 | 0 | if (parameters->numresolution < 2) { |
6952 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6953 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6954 | 0 | "Number of decomposition levels >= 1 && <= 6\n" |
6955 | 0 | "-> Number of decomposition levels forced to 1 (rather than %d)\n", |
6956 | 0 | parameters->numresolution + 1); |
6957 | 0 | parameters->numresolution = 1; |
6958 | 0 | } else if (parameters->numresolution > 7) { |
6959 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6960 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6961 | 0 | "Number of decomposition levels >= 1 && <= 6\n" |
6962 | 0 | "-> Number of decomposition levels forced to 6 (rather than %d)\n", |
6963 | 0 | parameters->numresolution + 1); |
6964 | 0 | parameters->numresolution = 7; |
6965 | 0 | } |
6966 | 0 | break; |
6967 | 0 | default : |
6968 | 0 | break; |
6969 | 0 | } |
6970 | | |
6971 | | /* Precincts */ |
6972 | 0 | parameters->csty |= J2K_CP_CSTY_PRT; |
6973 | 0 | if (parameters->numresolution == 1) { |
6974 | 0 | parameters->res_spec = 1; |
6975 | 0 | parameters->prcw_init[0] = 128; |
6976 | 0 | parameters->prch_init[0] = 128; |
6977 | 0 | } else { |
6978 | 0 | parameters->res_spec = parameters->numresolution - 1; |
6979 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
6980 | 0 | parameters->prcw_init[i] = 256; |
6981 | 0 | parameters->prch_init[i] = 256; |
6982 | 0 | } |
6983 | 0 | } |
6984 | | |
6985 | | /* The progression order shall be CPRL */ |
6986 | 0 | parameters->prog_order = OPJ_CPRL; |
6987 | | |
6988 | | /* Progression order changes for 4K, disallowed for 2K */ |
6989 | 0 | if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) { |
6990 | 0 | parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC, |
6991 | 0 | parameters->numresolution); |
6992 | 0 | } else { |
6993 | 0 | parameters->numpocs = 0; |
6994 | 0 | } |
6995 | | |
6996 | | /* Limited bit-rate */ |
6997 | 0 | parameters->cp_disto_alloc = 1; |
6998 | 0 | if (parameters->max_cs_size <= 0) { |
6999 | | /* No rate has been introduced, 24 fps is assumed */ |
7000 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7001 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7002 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7003 | 0 | "Maximum 1302083 compressed bytes @ 24fps\n" |
7004 | 0 | "As no rate has been given, this limit will be used.\n"); |
7005 | 0 | } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) { |
7006 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7007 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7008 | 0 | "Maximum 1302083 compressed bytes @ 24fps\n" |
7009 | 0 | "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n"); |
7010 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7011 | 0 | } |
7012 | |
|
7013 | 0 | if (parameters->max_comp_size <= 0) { |
7014 | | /* No rate has been introduced, 24 fps is assumed */ |
7015 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7016 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7017 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7018 | 0 | "Maximum 1041666 compressed bytes @ 24fps\n" |
7019 | 0 | "As no rate has been given, this limit will be used.\n"); |
7020 | 0 | } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) { |
7021 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7022 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7023 | 0 | "Maximum 1041666 compressed bytes @ 24fps\n" |
7024 | 0 | "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n"); |
7025 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7026 | 0 | } |
7027 | |
|
7028 | 0 | parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * |
7029 | 0 | image->comps[0].h * image->comps[0].prec) / |
7030 | 0 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7031 | 0 | image->comps[0].dy); |
7032 | |
|
7033 | 0 | } |
7034 | | |
7035 | | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, |
7036 | | opj_event_mgr_t *p_manager) |
7037 | 0 | { |
7038 | 0 | OPJ_UINT32 i; |
7039 | | |
7040 | | /* Number of components */ |
7041 | 0 | if (image->numcomps != 3) { |
7042 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7043 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
7044 | 0 | "3 components" |
7045 | 0 | "-> Number of components of input image (%d) is not compliant\n" |
7046 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7047 | 0 | image->numcomps); |
7048 | 0 | return OPJ_FALSE; |
7049 | 0 | } |
7050 | | |
7051 | | /* Bitdepth */ |
7052 | 0 | for (i = 0; i < image->numcomps; i++) { |
7053 | 0 | if ((image->comps[i].prec != 12) | (image->comps[i].sgnd)) { |
7054 | 0 | char signed_str[] = "signed"; |
7055 | 0 | char unsigned_str[] = "unsigned"; |
7056 | 0 | char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; |
7057 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7058 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
7059 | 0 | "Precision of each component shall be 12 bits unsigned" |
7060 | 0 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
7061 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7062 | 0 | i, image->comps[i].prec, tmp_str); |
7063 | 0 | return OPJ_FALSE; |
7064 | 0 | } |
7065 | 0 | } |
7066 | | |
7067 | | /* Image size */ |
7068 | 0 | switch (rsiz) { |
7069 | 0 | case OPJ_PROFILE_CINEMA_2K: |
7070 | 0 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) { |
7071 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7072 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
7073 | 0 | "width <= 2048 and height <= 1080\n" |
7074 | 0 | "-> Input image size %d x %d is not compliant\n" |
7075 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7076 | 0 | image->comps[0].w, image->comps[0].h); |
7077 | 0 | return OPJ_FALSE; |
7078 | 0 | } |
7079 | 0 | break; |
7080 | 0 | case OPJ_PROFILE_CINEMA_4K: |
7081 | 0 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) { |
7082 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7083 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
7084 | 0 | "width <= 4096 and height <= 2160\n" |
7085 | 0 | "-> Image size %d x %d is not compliant\n" |
7086 | 0 | "-> Non-profile-4 codestream will be generated\n", |
7087 | 0 | image->comps[0].w, image->comps[0].h); |
7088 | 0 | return OPJ_FALSE; |
7089 | 0 | } |
7090 | 0 | break; |
7091 | 0 | default : |
7092 | 0 | break; |
7093 | 0 | } |
7094 | | |
7095 | 0 | return OPJ_TRUE; |
7096 | 0 | } |
7097 | | |
7098 | | static int opj_j2k_get_imf_max_NL(opj_cparameters_t *parameters, |
7099 | | opj_image_t *image) |
7100 | 0 | { |
7101 | | /* Decomposition levels */ |
7102 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7103 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7104 | 0 | const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) |
7105 | 0 | parameters->cp_tdx : image->x1; |
7106 | 0 | switch (profile) { |
7107 | 0 | case OPJ_PROFILE_IMF_2K: |
7108 | 0 | return 5; |
7109 | 0 | case OPJ_PROFILE_IMF_4K: |
7110 | 0 | return 6; |
7111 | 0 | case OPJ_PROFILE_IMF_8K: |
7112 | 0 | return 7; |
7113 | 0 | case OPJ_PROFILE_IMF_2K_R: { |
7114 | 0 | if (XTsiz >= 2048) { |
7115 | 0 | return 5; |
7116 | 0 | } else if (XTsiz >= 1024) { |
7117 | 0 | return 4; |
7118 | 0 | } |
7119 | 0 | break; |
7120 | 0 | } |
7121 | 0 | case OPJ_PROFILE_IMF_4K_R: { |
7122 | 0 | if (XTsiz >= 4096) { |
7123 | 0 | return 6; |
7124 | 0 | } else if (XTsiz >= 2048) { |
7125 | 0 | return 5; |
7126 | 0 | } else if (XTsiz >= 1024) { |
7127 | 0 | return 4; |
7128 | 0 | } |
7129 | 0 | break; |
7130 | 0 | } |
7131 | 0 | case OPJ_PROFILE_IMF_8K_R: { |
7132 | 0 | if (XTsiz >= 8192) { |
7133 | 0 | return 7; |
7134 | 0 | } else if (XTsiz >= 4096) { |
7135 | 0 | return 6; |
7136 | 0 | } else if (XTsiz >= 2048) { |
7137 | 0 | return 5; |
7138 | 0 | } else if (XTsiz >= 1024) { |
7139 | 0 | return 4; |
7140 | 0 | } |
7141 | 0 | break; |
7142 | 0 | } |
7143 | 0 | default: |
7144 | 0 | break; |
7145 | 0 | } |
7146 | 0 | return -1; |
7147 | 0 | } |
7148 | | |
7149 | | static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters, |
7150 | | opj_image_t *image, opj_event_mgr_t *p_manager) |
7151 | 0 | { |
7152 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7153 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7154 | |
|
7155 | 0 | OPJ_UNUSED(p_manager); |
7156 | | |
7157 | | /* Override defaults set by opj_set_default_encoder_parameters */ |
7158 | 0 | if (parameters->cblockw_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKW && |
7159 | 0 | parameters->cblockh_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKH) { |
7160 | 0 | parameters->cblockw_init = 32; |
7161 | 0 | parameters->cblockh_init = 32; |
7162 | 0 | } |
7163 | | |
7164 | | /* One tile part for each component */ |
7165 | 0 | parameters->tp_flag = 'C'; |
7166 | 0 | parameters->tp_on = 1; |
7167 | |
|
7168 | 0 | if (parameters->prog_order == OPJ_COMP_PARAM_DEFAULT_PROG_ORDER) { |
7169 | 0 | parameters->prog_order = OPJ_CPRL; |
7170 | 0 | } |
7171 | |
|
7172 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7173 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7174 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7175 | | /* 9-7 transform */ |
7176 | 0 | parameters->irreversible = 1; |
7177 | 0 | } |
7178 | | |
7179 | | /* Adjust the number of resolutions if set to its defaults */ |
7180 | 0 | if (parameters->numresolution == OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION && |
7181 | 0 | image->x0 == 0 && |
7182 | 0 | image->y0 == 0) { |
7183 | 0 | const int max_NL = opj_j2k_get_imf_max_NL(parameters, image); |
7184 | 0 | if (max_NL >= 0 && parameters->numresolution > max_NL) { |
7185 | 0 | parameters->numresolution = max_NL + 1; |
7186 | 0 | } |
7187 | | |
7188 | | /* Note: below is generic logic */ |
7189 | 0 | if (!parameters->tile_size_on) { |
7190 | 0 | while (parameters->numresolution > 0) { |
7191 | 0 | if (image->x1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { |
7192 | 0 | parameters->numresolution --; |
7193 | 0 | continue; |
7194 | 0 | } |
7195 | 0 | if (image->y1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { |
7196 | 0 | parameters->numresolution --; |
7197 | 0 | continue; |
7198 | 0 | } |
7199 | 0 | break; |
7200 | 0 | } |
7201 | 0 | } |
7202 | 0 | } |
7203 | | |
7204 | | /* Set defaults precincts */ |
7205 | 0 | if (parameters->csty == 0) { |
7206 | 0 | parameters->csty |= J2K_CP_CSTY_PRT; |
7207 | 0 | if (parameters->numresolution == 1) { |
7208 | 0 | parameters->res_spec = 1; |
7209 | 0 | parameters->prcw_init[0] = 128; |
7210 | 0 | parameters->prch_init[0] = 128; |
7211 | 0 | } else { |
7212 | 0 | int i; |
7213 | 0 | parameters->res_spec = parameters->numresolution - 1; |
7214 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
7215 | 0 | parameters->prcw_init[i] = 256; |
7216 | 0 | parameters->prch_init[i] = 256; |
7217 | 0 | } |
7218 | 0 | } |
7219 | 0 | } |
7220 | 0 | } |
7221 | | |
7222 | | /* Table A.53 from JPEG2000 standard */ |
7223 | | static const OPJ_UINT16 tabMaxSubLevelFromMainLevel[] = { |
7224 | | 15, /* unspecified */ |
7225 | | 1, |
7226 | | 1, |
7227 | | 1, |
7228 | | 2, |
7229 | | 3, |
7230 | | 4, |
7231 | | 5, |
7232 | | 6, |
7233 | | 7, |
7234 | | 8, |
7235 | | 9 |
7236 | | }; |
7237 | | |
7238 | | static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters, |
7239 | | opj_image_t *image, |
7240 | | opj_event_mgr_t *p_manager) |
7241 | 0 | { |
7242 | 0 | OPJ_UINT32 i; |
7243 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7244 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7245 | 0 | const OPJ_UINT16 mainlevel = OPJ_GET_IMF_MAINLEVEL(rsiz); |
7246 | 0 | const OPJ_UINT16 sublevel = OPJ_GET_IMF_SUBLEVEL(rsiz); |
7247 | 0 | const int NL = parameters->numresolution - 1; |
7248 | 0 | const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) |
7249 | 0 | parameters->cp_tdx : image->x1; |
7250 | 0 | OPJ_BOOL ret = OPJ_TRUE; |
7251 | | |
7252 | | /* Validate mainlevel */ |
7253 | 0 | if (mainlevel > OPJ_IMF_MAINLEVEL_MAX) { |
7254 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7255 | 0 | "IMF profile require mainlevel <= 11.\n" |
7256 | 0 | "-> %d is thus not compliant\n" |
7257 | 0 | "-> Non-IMF codestream will be generated\n", |
7258 | 0 | mainlevel); |
7259 | 0 | ret = OPJ_FALSE; |
7260 | 0 | } else { |
7261 | | /* Validate sublevel */ |
7262 | 0 | assert(sizeof(tabMaxSubLevelFromMainLevel) == |
7263 | 0 | (OPJ_IMF_MAINLEVEL_MAX + 1) * sizeof(tabMaxSubLevelFromMainLevel[0])); |
7264 | 0 | if (sublevel > tabMaxSubLevelFromMainLevel[mainlevel]) { |
7265 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7266 | 0 | "IMF profile require sublevel <= %d for mainlevel = %d.\n" |
7267 | 0 | "-> %d is thus not compliant\n" |
7268 | 0 | "-> Non-IMF codestream will be generated\n", |
7269 | 0 | tabMaxSubLevelFromMainLevel[mainlevel], |
7270 | 0 | mainlevel, |
7271 | 0 | sublevel); |
7272 | 0 | ret = OPJ_FALSE; |
7273 | 0 | } |
7274 | 0 | } |
7275 | | |
7276 | | /* Number of components */ |
7277 | 0 | if (image->numcomps > 3) { |
7278 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7279 | 0 | "IMF profiles require at most 3 components.\n" |
7280 | 0 | "-> Number of components of input image (%d) is not compliant\n" |
7281 | 0 | "-> Non-IMF codestream will be generated\n", |
7282 | 0 | image->numcomps); |
7283 | 0 | ret = OPJ_FALSE; |
7284 | 0 | } |
7285 | |
|
7286 | 0 | if (image->x0 != 0 || image->y0 != 0) { |
7287 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7288 | 0 | "IMF profiles require image origin to be at 0,0.\n" |
7289 | 0 | "-> %d,%d is not compliant\n" |
7290 | 0 | "-> Non-IMF codestream will be generated\n", |
7291 | 0 | image->x0, image->y0 != 0); |
7292 | 0 | ret = OPJ_FALSE; |
7293 | 0 | } |
7294 | |
|
7295 | 0 | if (parameters->cp_tx0 != 0 || parameters->cp_ty0 != 0) { |
7296 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7297 | 0 | "IMF profiles require tile origin to be at 0,0.\n" |
7298 | 0 | "-> %d,%d is not compliant\n" |
7299 | 0 | "-> Non-IMF codestream will be generated\n", |
7300 | 0 | parameters->cp_tx0, parameters->cp_ty0); |
7301 | 0 | ret = OPJ_FALSE; |
7302 | 0 | } |
7303 | |
|
7304 | 0 | if (parameters->tile_size_on) { |
7305 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7306 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7307 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7308 | 0 | if ((OPJ_UINT32)parameters->cp_tdx < image->x1 || |
7309 | 0 | (OPJ_UINT32)parameters->cp_tdy < image->y1) { |
7310 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7311 | 0 | "IMF 2K/4K/8K single tile profiles require tile to be greater or equal to image size.\n" |
7312 | 0 | "-> %d,%d is lesser than %d,%d\n" |
7313 | 0 | "-> Non-IMF codestream will be generated\n", |
7314 | 0 | parameters->cp_tdx, |
7315 | 0 | parameters->cp_tdy, |
7316 | 0 | image->x1, |
7317 | 0 | image->y1); |
7318 | 0 | ret = OPJ_FALSE; |
7319 | 0 | } |
7320 | 0 | } else { |
7321 | 0 | if ((OPJ_UINT32)parameters->cp_tdx >= image->x1 && |
7322 | 0 | (OPJ_UINT32)parameters->cp_tdy >= image->y1) { |
7323 | | /* ok */ |
7324 | 0 | } else if (parameters->cp_tdx == 1024 && |
7325 | 0 | parameters->cp_tdy == 1024) { |
7326 | | /* ok */ |
7327 | 0 | } else if (parameters->cp_tdx == 2048 && |
7328 | 0 | parameters->cp_tdy == 2048 && |
7329 | 0 | (profile == OPJ_PROFILE_IMF_4K || |
7330 | 0 | profile == OPJ_PROFILE_IMF_8K)) { |
7331 | | /* ok */ |
7332 | 0 | } else if (parameters->cp_tdx == 4096 && |
7333 | 0 | parameters->cp_tdy == 4096 && |
7334 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7335 | | /* ok */ |
7336 | 0 | } else { |
7337 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7338 | 0 | "IMF 2K_R/4K_R/8K_R single/multiple tile profiles " |
7339 | 0 | "require tile to be greater or equal to image size,\n" |
7340 | 0 | "or to be (1024,1024), or (2048,2048) for 4K_R/8K_R " |
7341 | 0 | "or (4096,4096) for 8K_R.\n" |
7342 | 0 | "-> %d,%d is non conformant\n" |
7343 | 0 | "-> Non-IMF codestream will be generated\n", |
7344 | 0 | parameters->cp_tdx, |
7345 | 0 | parameters->cp_tdy); |
7346 | 0 | ret = OPJ_FALSE; |
7347 | 0 | } |
7348 | 0 | } |
7349 | 0 | } |
7350 | | |
7351 | | /* Bitdepth */ |
7352 | 0 | for (i = 0; i < image->numcomps; i++) { |
7353 | 0 | if (!(image->comps[i].prec >= 8 && image->comps[i].prec <= 16) || |
7354 | 0 | (image->comps[i].sgnd)) { |
7355 | 0 | char signed_str[] = "signed"; |
7356 | 0 | char unsigned_str[] = "unsigned"; |
7357 | 0 | char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; |
7358 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7359 | 0 | "IMF profiles require precision of each component to b in [8-16] bits unsigned" |
7360 | 0 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
7361 | 0 | "-> Non-IMF codestream will be generated\n", |
7362 | 0 | i, image->comps[i].prec, tmp_str); |
7363 | 0 | ret = OPJ_FALSE; |
7364 | 0 | } |
7365 | 0 | } |
7366 | | |
7367 | | /* Sub-sampling */ |
7368 | 0 | for (i = 0; i < image->numcomps; i++) { |
7369 | 0 | if (i == 0 && image->comps[i].dx != 1) { |
7370 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7371 | 0 | "IMF profiles require XRSiz1 == 1. Here it is set to %d.\n" |
7372 | 0 | "-> Non-IMF codestream will be generated\n", |
7373 | 0 | image->comps[i].dx); |
7374 | 0 | ret = OPJ_FALSE; |
7375 | 0 | } |
7376 | 0 | if (i == 1 && image->comps[i].dx != 1 && image->comps[i].dx != 2) { |
7377 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7378 | 0 | "IMF profiles require XRSiz2 == 1 or 2. Here it is set to %d.\n" |
7379 | 0 | "-> Non-IMF codestream will be generated\n", |
7380 | 0 | image->comps[i].dx); |
7381 | 0 | ret = OPJ_FALSE; |
7382 | 0 | } |
7383 | 0 | if (i > 1 && image->comps[i].dx != image->comps[i - 1].dx) { |
7384 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7385 | 0 | "IMF profiles require XRSiz%d to be the same as XRSiz2. " |
7386 | 0 | "Here it is set to %d instead of %d.\n" |
7387 | 0 | "-> Non-IMF codestream will be generated\n", |
7388 | 0 | i + 1, image->comps[i].dx, image->comps[i - 1].dx); |
7389 | 0 | ret = OPJ_FALSE; |
7390 | 0 | } |
7391 | 0 | if (image->comps[i].dy != 1) { |
7392 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7393 | 0 | "IMF profiles require YRsiz == 1. " |
7394 | 0 | "Here it is set to %d for component %d.\n" |
7395 | 0 | "-> Non-IMF codestream will be generated\n", |
7396 | 0 | image->comps[i].dy, i); |
7397 | 0 | ret = OPJ_FALSE; |
7398 | 0 | } |
7399 | 0 | } |
7400 | | |
7401 | | /* Image size */ |
7402 | 0 | switch (profile) { |
7403 | 0 | case OPJ_PROFILE_IMF_2K: |
7404 | 0 | case OPJ_PROFILE_IMF_2K_R: |
7405 | 0 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1556))) { |
7406 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7407 | 0 | "IMF 2K/2K_R profile require:\n" |
7408 | 0 | "width <= 2048 and height <= 1556\n" |
7409 | 0 | "-> Input image size %d x %d is not compliant\n" |
7410 | 0 | "-> Non-IMF codestream will be generated\n", |
7411 | 0 | image->comps[0].w, image->comps[0].h); |
7412 | 0 | ret = OPJ_FALSE; |
7413 | 0 | } |
7414 | 0 | break; |
7415 | 0 | case OPJ_PROFILE_IMF_4K: |
7416 | 0 | case OPJ_PROFILE_IMF_4K_R: |
7417 | 0 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 3112))) { |
7418 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7419 | 0 | "IMF 4K/4K_R profile require:\n" |
7420 | 0 | "width <= 4096 and height <= 3112\n" |
7421 | 0 | "-> Input image size %d x %d is not compliant\n" |
7422 | 0 | "-> Non-IMF codestream will be generated\n", |
7423 | 0 | image->comps[0].w, image->comps[0].h); |
7424 | 0 | ret = OPJ_FALSE; |
7425 | 0 | } |
7426 | 0 | break; |
7427 | 0 | case OPJ_PROFILE_IMF_8K: |
7428 | 0 | case OPJ_PROFILE_IMF_8K_R: |
7429 | 0 | if (((image->comps[0].w > 8192) | (image->comps[0].h > 6224))) { |
7430 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7431 | 0 | "IMF 8K/8K_R profile require:\n" |
7432 | 0 | "width <= 8192 and height <= 6224\n" |
7433 | 0 | "-> Input image size %d x %d is not compliant\n" |
7434 | 0 | "-> Non-IMF codestream will be generated\n", |
7435 | 0 | image->comps[0].w, image->comps[0].h); |
7436 | 0 | ret = OPJ_FALSE; |
7437 | 0 | } |
7438 | 0 | break; |
7439 | 0 | default : |
7440 | 0 | assert(0); |
7441 | 0 | return OPJ_FALSE; |
7442 | 0 | } |
7443 | | |
7444 | 0 | if (parameters->roi_compno != -1) { |
7445 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7446 | 0 | "IMF profile forbid RGN / region of interest marker.\n" |
7447 | 0 | "-> Compression parameters specify a ROI\n" |
7448 | 0 | "-> Non-IMF codestream will be generated\n"); |
7449 | 0 | ret = OPJ_FALSE; |
7450 | 0 | } |
7451 | |
|
7452 | 0 | if (parameters->cblockw_init != 32 || parameters->cblockh_init != 32) { |
7453 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7454 | 0 | "IMF profile require code block size to be 32x32.\n" |
7455 | 0 | "-> Compression parameters set it to %dx%d.\n" |
7456 | 0 | "-> Non-IMF codestream will be generated\n", |
7457 | 0 | parameters->cblockw_init, |
7458 | 0 | parameters->cblockh_init); |
7459 | 0 | ret = OPJ_FALSE; |
7460 | 0 | } |
7461 | |
|
7462 | 0 | if (parameters->prog_order != OPJ_CPRL) { |
7463 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7464 | 0 | "IMF profile require progression order to be CPRL.\n" |
7465 | 0 | "-> Compression parameters set it to %d.\n" |
7466 | 0 | "-> Non-IMF codestream will be generated\n", |
7467 | 0 | parameters->prog_order); |
7468 | 0 | ret = OPJ_FALSE; |
7469 | 0 | } |
7470 | |
|
7471 | 0 | if (parameters->numpocs != 0) { |
7472 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7473 | 0 | "IMF profile forbid POC markers.\n" |
7474 | 0 | "-> Compression parameters set %d POC.\n" |
7475 | 0 | "-> Non-IMF codestream will be generated\n", |
7476 | 0 | parameters->numpocs); |
7477 | 0 | ret = OPJ_FALSE; |
7478 | 0 | } |
7479 | | |
7480 | | /* Codeblock style: no mode switch enabled */ |
7481 | 0 | if (parameters->mode != 0) { |
7482 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7483 | 0 | "IMF profile forbid mode switch in code block style.\n" |
7484 | 0 | "-> Compression parameters set code block style to %d.\n" |
7485 | 0 | "-> Non-IMF codestream will be generated\n", |
7486 | 0 | parameters->mode); |
7487 | 0 | ret = OPJ_FALSE; |
7488 | 0 | } |
7489 | |
|
7490 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7491 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7492 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7493 | | /* Expect 9-7 transform */ |
7494 | 0 | if (parameters->irreversible != 1) { |
7495 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7496 | 0 | "IMF 2K/4K/8K profiles require 9-7 Irreversible Transform.\n" |
7497 | 0 | "-> Compression parameters set it to reversible.\n" |
7498 | 0 | "-> Non-IMF codestream will be generated\n"); |
7499 | 0 | ret = OPJ_FALSE; |
7500 | 0 | } |
7501 | 0 | } else { |
7502 | | /* Expect 5-3 transform */ |
7503 | 0 | if (parameters->irreversible != 0) { |
7504 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7505 | 0 | "IMF 2K/4K/8K profiles require 5-3 reversible Transform.\n" |
7506 | 0 | "-> Compression parameters set it to irreversible.\n" |
7507 | 0 | "-> Non-IMF codestream will be generated\n"); |
7508 | 0 | ret = OPJ_FALSE; |
7509 | 0 | } |
7510 | 0 | } |
7511 | | |
7512 | | /* Number of layers */ |
7513 | 0 | if (parameters->tcp_numlayers != 1) { |
7514 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7515 | 0 | "IMF 2K/4K/8K profiles require 1 single quality layer.\n" |
7516 | 0 | "-> Number of layers is %d.\n" |
7517 | 0 | "-> Non-IMF codestream will be generated\n", |
7518 | 0 | parameters->tcp_numlayers); |
7519 | 0 | ret = OPJ_FALSE; |
7520 | 0 | } |
7521 | | |
7522 | | /* Decomposition levels */ |
7523 | 0 | switch (profile) { |
7524 | 0 | case OPJ_PROFILE_IMF_2K: |
7525 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7526 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7527 | 0 | "IMF 2K profile requires 1 <= NL <= 5:\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 | break; |
7534 | 0 | case OPJ_PROFILE_IMF_4K: |
7535 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7536 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7537 | 0 | "IMF 4K profile requires 1 <= NL <= 6:\n" |
7538 | 0 | "-> Number of decomposition levels is %d.\n" |
7539 | 0 | "-> Non-IMF codestream will be generated\n", |
7540 | 0 | NL); |
7541 | 0 | ret = OPJ_FALSE; |
7542 | 0 | } |
7543 | 0 | break; |
7544 | 0 | case OPJ_PROFILE_IMF_8K: |
7545 | 0 | if (!(NL >= 1 && NL <= 7)) { |
7546 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7547 | 0 | "IMF 8K profile requires 1 <= NL <= 7:\n" |
7548 | 0 | "-> Number of decomposition levels is %d.\n" |
7549 | 0 | "-> Non-IMF codestream will be generated\n", |
7550 | 0 | NL); |
7551 | 0 | ret = OPJ_FALSE; |
7552 | 0 | } |
7553 | 0 | break; |
7554 | 0 | case OPJ_PROFILE_IMF_2K_R: { |
7555 | 0 | if (XTsiz >= 2048) { |
7556 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7557 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7558 | 0 | "IMF 2K_R profile requires 1 <= NL <= 5 for XTsiz >= 2048:\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 >= 1024) { |
7565 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7566 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7567 | 0 | "IMF 2K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\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 | } |
7574 | 0 | break; |
7575 | 0 | } |
7576 | 0 | case OPJ_PROFILE_IMF_4K_R: { |
7577 | 0 | if (XTsiz >= 4096) { |
7578 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7579 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7580 | 0 | "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz >= 4096:\n" |
7581 | 0 | "-> Number of decomposition levels is %d.\n" |
7582 | 0 | "-> Non-IMF codestream will be generated\n", |
7583 | 0 | NL); |
7584 | 0 | ret = OPJ_FALSE; |
7585 | 0 | } |
7586 | 0 | } else if (XTsiz >= 2048) { |
7587 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7588 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7589 | 0 | "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" |
7590 | 0 | "-> Number of decomposition levels is %d.\n" |
7591 | 0 | "-> Non-IMF codestream will be generated\n", |
7592 | 0 | NL); |
7593 | 0 | ret = OPJ_FALSE; |
7594 | 0 | } |
7595 | 0 | } else if (XTsiz >= 1024) { |
7596 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7597 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7598 | 0 | "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7599 | 0 | "-> Number of decomposition levels is %d.\n" |
7600 | 0 | "-> Non-IMF codestream will be generated\n", |
7601 | 0 | NL); |
7602 | 0 | ret = OPJ_FALSE; |
7603 | 0 | } |
7604 | 0 | } |
7605 | 0 | break; |
7606 | 0 | } |
7607 | 0 | case OPJ_PROFILE_IMF_8K_R: { |
7608 | 0 | if (XTsiz >= 8192) { |
7609 | 0 | if (!(NL >= 1 && NL <= 7)) { |
7610 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7611 | 0 | "IMF 4K_R profile requires 1 <= NL <= 7 for XTsiz >= 8192:\n" |
7612 | 0 | "-> Number of decomposition levels is %d.\n" |
7613 | 0 | "-> Non-IMF codestream will be generated\n", |
7614 | 0 | NL); |
7615 | 0 | ret = OPJ_FALSE; |
7616 | 0 | } |
7617 | 0 | } else if (XTsiz >= 4096) { |
7618 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7619 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7620 | 0 | "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz in [4096,8192[:\n" |
7621 | 0 | "-> Number of decomposition levels is %d.\n" |
7622 | 0 | "-> Non-IMF codestream will be generated\n", |
7623 | 0 | NL); |
7624 | 0 | ret = OPJ_FALSE; |
7625 | 0 | } |
7626 | 0 | } else if (XTsiz >= 2048) { |
7627 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7628 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7629 | 0 | "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" |
7630 | 0 | "-> Number of decomposition levels is %d.\n" |
7631 | 0 | "-> Non-IMF codestream will be generated\n", |
7632 | 0 | NL); |
7633 | 0 | ret = OPJ_FALSE; |
7634 | 0 | } |
7635 | 0 | } else if (XTsiz >= 1024) { |
7636 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7637 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7638 | 0 | "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7639 | 0 | "-> Number of decomposition levels is %d.\n" |
7640 | 0 | "-> Non-IMF codestream will be generated\n", |
7641 | 0 | NL); |
7642 | 0 | ret = OPJ_FALSE; |
7643 | 0 | } |
7644 | 0 | } |
7645 | 0 | break; |
7646 | 0 | } |
7647 | 0 | default: |
7648 | 0 | break; |
7649 | 0 | } |
7650 | | |
7651 | 0 | if (parameters->numresolution == 1) { |
7652 | 0 | if (parameters->res_spec != 1 || |
7653 | 0 | parameters->prcw_init[0] != 128 || |
7654 | 0 | parameters->prch_init[0] != 128) { |
7655 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7656 | 0 | "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" |
7657 | 0 | "-> Supplied values are different from that.\n" |
7658 | 0 | "-> Non-IMF codestream will be generated\n"); |
7659 | 0 | ret = OPJ_FALSE; |
7660 | 0 | } |
7661 | 0 | } else { |
7662 | 0 | int i; |
7663 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
7664 | 0 | if (parameters->prcw_init[i] != 256 || |
7665 | 0 | parameters->prch_init[i] != 256) { |
7666 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7667 | 0 | "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" |
7668 | 0 | "-> Supplied values are different from that.\n" |
7669 | 0 | "-> Non-IMF codestream will be generated\n"); |
7670 | 0 | ret = OPJ_FALSE; |
7671 | 0 | } |
7672 | 0 | } |
7673 | 0 | } |
7674 | |
|
7675 | 0 | return ret; |
7676 | 0 | } |
7677 | | |
7678 | | |
7679 | | OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, |
7680 | | opj_cparameters_t *parameters, |
7681 | | opj_image_t *image, |
7682 | | opj_event_mgr_t * p_manager) |
7683 | 0 | { |
7684 | 0 | OPJ_UINT32 i, j, tileno, numpocs_tile; |
7685 | 0 | opj_cp_t *cp = 00; |
7686 | 0 | OPJ_UINT32 cblkw, cblkh; |
7687 | |
|
7688 | 0 | if (!p_j2k || !parameters || ! image) { |
7689 | 0 | return OPJ_FALSE; |
7690 | 0 | } |
7691 | | |
7692 | 0 | if ((parameters->numresolution <= 0) || |
7693 | 0 | (parameters->numresolution > OPJ_J2K_MAXRLVLS)) { |
7694 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7695 | 0 | "Invalid number of resolutions : %d not in range [1,%d]\n", |
7696 | 0 | parameters->numresolution, OPJ_J2K_MAXRLVLS); |
7697 | 0 | return OPJ_FALSE; |
7698 | 0 | } |
7699 | | |
7700 | 0 | if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) { |
7701 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7702 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7703 | 0 | parameters->cblockw_init); |
7704 | 0 | return OPJ_FALSE; |
7705 | 0 | } |
7706 | 0 | if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) { |
7707 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7708 | 0 | "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n", |
7709 | 0 | parameters->cblockh_init); |
7710 | 0 | return OPJ_FALSE; |
7711 | 0 | } |
7712 | 0 | if (parameters->cblockw_init * parameters->cblockh_init > 4096) { |
7713 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7714 | 0 | "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n"); |
7715 | 0 | return OPJ_FALSE; |
7716 | 0 | } |
7717 | 0 | cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
7718 | 0 | cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
7719 | 0 | if (parameters->cblockw_init != (1 << cblkw)) { |
7720 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7721 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7722 | 0 | parameters->cblockw_init); |
7723 | 0 | return OPJ_FALSE; |
7724 | 0 | } |
7725 | 0 | if (parameters->cblockh_init != (1 << cblkh)) { |
7726 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7727 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7728 | 0 | parameters->cblockh_init); |
7729 | 0 | return OPJ_FALSE; |
7730 | 0 | } |
7731 | | |
7732 | 0 | if (parameters->cp_fixed_alloc) { |
7733 | 0 | if (parameters->cp_matrice == NULL) { |
7734 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7735 | 0 | "cp_fixed_alloc set, but cp_matrice missing\n"); |
7736 | 0 | return OPJ_FALSE; |
7737 | 0 | } |
7738 | | |
7739 | 0 | if (parameters->tcp_numlayers > J2K_TCD_MATRIX_MAX_LAYER_COUNT) { |
7740 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7741 | 0 | "tcp_numlayers when cp_fixed_alloc set should not exceed %d\n", |
7742 | 0 | J2K_TCD_MATRIX_MAX_LAYER_COUNT); |
7743 | 0 | return OPJ_FALSE; |
7744 | 0 | } |
7745 | 0 | if (parameters->numresolution > J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT) { |
7746 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7747 | 0 | "numresolution when cp_fixed_alloc set should not exceed %d\n", |
7748 | 0 | J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT); |
7749 | 0 | return OPJ_FALSE; |
7750 | 0 | } |
7751 | 0 | } |
7752 | | |
7753 | 0 | p_j2k->m_specific_param.m_encoder.m_nb_comps = image->numcomps; |
7754 | | |
7755 | | /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ |
7756 | 0 | cp = &(p_j2k->m_cp); |
7757 | | |
7758 | | /* set default values for cp */ |
7759 | 0 | cp->tw = 1; |
7760 | 0 | cp->th = 1; |
7761 | | |
7762 | | /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */ |
7763 | 0 | if (parameters->rsiz == |
7764 | 0 | OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */ |
7765 | 0 | OPJ_BOOL deprecated_used = OPJ_FALSE; |
7766 | 0 | switch (parameters->cp_cinema) { |
7767 | 0 | case OPJ_CINEMA2K_24: |
7768 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7769 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7770 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7771 | 0 | deprecated_used = OPJ_TRUE; |
7772 | 0 | break; |
7773 | 0 | case OPJ_CINEMA2K_48: |
7774 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7775 | 0 | parameters->max_cs_size = OPJ_CINEMA_48_CS; |
7776 | 0 | parameters->max_comp_size = OPJ_CINEMA_48_COMP; |
7777 | 0 | deprecated_used = OPJ_TRUE; |
7778 | 0 | break; |
7779 | 0 | case OPJ_CINEMA4K_24: |
7780 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
7781 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7782 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7783 | 0 | deprecated_used = OPJ_TRUE; |
7784 | 0 | break; |
7785 | 0 | case OPJ_OFF: |
7786 | 0 | default: |
7787 | 0 | break; |
7788 | 0 | } |
7789 | 0 | switch (parameters->cp_rsiz) { |
7790 | 0 | case OPJ_CINEMA2K: |
7791 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7792 | 0 | deprecated_used = OPJ_TRUE; |
7793 | 0 | break; |
7794 | 0 | case OPJ_CINEMA4K: |
7795 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
7796 | 0 | deprecated_used = OPJ_TRUE; |
7797 | 0 | break; |
7798 | 0 | case OPJ_MCT: |
7799 | 0 | parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT; |
7800 | 0 | deprecated_used = OPJ_TRUE; |
7801 | 0 | case OPJ_STD_RSIZ: |
7802 | 0 | default: |
7803 | 0 | break; |
7804 | 0 | } |
7805 | 0 | if (deprecated_used) { |
7806 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7807 | 0 | "Deprecated fields cp_cinema or cp_rsiz are used\n" |
7808 | 0 | "Please consider using only the rsiz field\n" |
7809 | 0 | "See openjpeg.h documentation for more details\n"); |
7810 | 0 | } |
7811 | 0 | } |
7812 | | |
7813 | | /* If no explicit layers are provided, use lossless settings */ |
7814 | 0 | if (parameters->tcp_numlayers == 0) { |
7815 | 0 | parameters->tcp_numlayers = 1; |
7816 | 0 | parameters->cp_disto_alloc = 1; |
7817 | 0 | parameters->tcp_rates[0] = 0; |
7818 | 0 | } |
7819 | |
|
7820 | 0 | if (parameters->cp_disto_alloc) { |
7821 | | /* Emit warnings if tcp_rates are not decreasing */ |
7822 | 0 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7823 | 0 | OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i]; |
7824 | 0 | OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1]; |
7825 | 0 | if (rate_i_corr <= 1.0) { |
7826 | 0 | rate_i_corr = 1.0; |
7827 | 0 | } |
7828 | 0 | if (rate_i_m_1_corr <= 1.0) { |
7829 | 0 | rate_i_m_1_corr = 1.0; |
7830 | 0 | } |
7831 | 0 | if (rate_i_corr >= rate_i_m_1_corr) { |
7832 | 0 | if (rate_i_corr != parameters->tcp_rates[i] && |
7833 | 0 | rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
7834 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7835 | 0 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
7836 | 0 | "than tcp_rates[%d]=%f (corrected as %f)\n", |
7837 | 0 | i, parameters->tcp_rates[i], rate_i_corr, |
7838 | 0 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
7839 | 0 | } else if (rate_i_corr != parameters->tcp_rates[i]) { |
7840 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7841 | 0 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
7842 | 0 | "than tcp_rates[%d]=%f\n", |
7843 | 0 | i, parameters->tcp_rates[i], rate_i_corr, |
7844 | 0 | i - 1, parameters->tcp_rates[i - 1]); |
7845 | 0 | } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
7846 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7847 | 0 | "tcp_rates[%d]=%f should be strictly lesser " |
7848 | 0 | "than tcp_rates[%d]=%f (corrected as %f)\n", |
7849 | 0 | i, parameters->tcp_rates[i], |
7850 | 0 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
7851 | 0 | } else { |
7852 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7853 | 0 | "tcp_rates[%d]=%f should be strictly lesser " |
7854 | 0 | "than tcp_rates[%d]=%f\n", |
7855 | 0 | i, parameters->tcp_rates[i], |
7856 | 0 | i - 1, parameters->tcp_rates[i - 1]); |
7857 | 0 | } |
7858 | 0 | } |
7859 | 0 | } |
7860 | 0 | } else if (parameters->cp_fixed_quality) { |
7861 | | /* Emit warnings if tcp_distoratio are not increasing */ |
7862 | 0 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7863 | 0 | if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] && |
7864 | 0 | !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 && |
7865 | 0 | parameters->tcp_distoratio[i] == 0)) { |
7866 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7867 | 0 | "tcp_distoratio[%d]=%f should be strictly greater " |
7868 | 0 | "than tcp_distoratio[%d]=%f\n", |
7869 | 0 | i, parameters->tcp_distoratio[i], i - 1, |
7870 | 0 | parameters->tcp_distoratio[i - 1]); |
7871 | 0 | } |
7872 | 0 | } |
7873 | 0 | } |
7874 | | |
7875 | | /* see if max_codestream_size does limit input rate */ |
7876 | 0 | if (parameters->max_cs_size <= 0) { |
7877 | 0 | if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) { |
7878 | 0 | OPJ_FLOAT32 temp_size; |
7879 | 0 | temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
7880 | 0 | image->comps[0].h * image->comps[0].prec) / |
7881 | 0 | ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 * |
7882 | 0 | image->comps[0].dx * image->comps[0].dy)); |
7883 | 0 | if (temp_size > (OPJ_FLOAT32)INT_MAX) { |
7884 | 0 | parameters->max_cs_size = INT_MAX; |
7885 | 0 | } else { |
7886 | 0 | parameters->max_cs_size = (int) floor(temp_size); |
7887 | 0 | } |
7888 | 0 | } else { |
7889 | 0 | parameters->max_cs_size = 0; |
7890 | 0 | } |
7891 | 0 | } else { |
7892 | 0 | OPJ_FLOAT32 temp_rate; |
7893 | 0 | OPJ_BOOL cap = OPJ_FALSE; |
7894 | |
|
7895 | 0 | if (OPJ_IS_IMF(parameters->rsiz) && parameters->max_cs_size > 0 && |
7896 | 0 | parameters->tcp_numlayers == 1 && parameters->tcp_rates[0] == 0) { |
7897 | 0 | parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * |
7898 | 0 | image->comps[0].h * image->comps[0].prec) / |
7899 | 0 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7900 | 0 | image->comps[0].dy); |
7901 | 0 | } |
7902 | |
|
7903 | 0 | temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
7904 | 0 | image->comps[0].h * image->comps[0].prec) / |
7905 | 0 | (((double)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7906 | 0 | image->comps[0].dy)); |
7907 | 0 | for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7908 | 0 | if (parameters->tcp_rates[i] < temp_rate) { |
7909 | 0 | parameters->tcp_rates[i] = temp_rate; |
7910 | 0 | cap = OPJ_TRUE; |
7911 | 0 | } |
7912 | 0 | } |
7913 | 0 | if (cap) { |
7914 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7915 | 0 | "The desired maximum codestream size has limited\n" |
7916 | 0 | "at least one of the desired quality layers\n"); |
7917 | 0 | } |
7918 | 0 | } |
7919 | |
|
7920 | 0 | if (OPJ_IS_CINEMA(parameters->rsiz) || OPJ_IS_IMF(parameters->rsiz)) { |
7921 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE; |
7922 | 0 | } |
7923 | | |
7924 | | /* Manage profiles and applications and set RSIZ */ |
7925 | | /* set cinema parameters if required */ |
7926 | 0 | if (OPJ_IS_CINEMA(parameters->rsiz)) { |
7927 | 0 | if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K) |
7928 | 0 | || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) { |
7929 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7930 | 0 | "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n"); |
7931 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7932 | 0 | } else { |
7933 | 0 | opj_j2k_set_cinema_parameters(parameters, image, p_manager); |
7934 | 0 | if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) { |
7935 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7936 | 0 | } |
7937 | 0 | } |
7938 | 0 | } else if (OPJ_IS_STORAGE(parameters->rsiz)) { |
7939 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7940 | 0 | "JPEG 2000 Long Term Storage profile not yet supported\n"); |
7941 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7942 | 0 | } else if (OPJ_IS_BROADCAST(parameters->rsiz)) { |
7943 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7944 | 0 | "JPEG 2000 Broadcast profiles not yet supported\n"); |
7945 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7946 | 0 | } else if (OPJ_IS_IMF(parameters->rsiz)) { |
7947 | 0 | opj_j2k_set_imf_parameters(parameters, image, p_manager); |
7948 | 0 | if (!opj_j2k_is_imf_compliant(parameters, image, p_manager)) { |
7949 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7950 | 0 | } |
7951 | 0 | } else if (OPJ_IS_PART2(parameters->rsiz)) { |
7952 | 0 | if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) { |
7953 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7954 | 0 | "JPEG 2000 Part-2 profile defined\n" |
7955 | 0 | "but no Part-2 extension enabled.\n" |
7956 | 0 | "Profile set to NONE.\n"); |
7957 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7958 | 0 | } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) { |
7959 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7960 | 0 | "Unsupported Part-2 extension enabled\n" |
7961 | 0 | "Profile set to NONE.\n"); |
7962 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7963 | 0 | } |
7964 | 0 | } |
7965 | | |
7966 | | /* |
7967 | | copy user encoding parameters |
7968 | | */ |
7969 | 0 | cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32) |
7970 | 0 | parameters->max_comp_size; |
7971 | 0 | cp->rsiz = parameters->rsiz; |
7972 | 0 | if (parameters->cp_fixed_alloc) { |
7973 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = FIXED_LAYER; |
7974 | 0 | } else if (parameters->cp_fixed_quality) { |
7975 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = |
7976 | 0 | FIXED_DISTORTION_RATIO; |
7977 | 0 | } else { |
7978 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = |
7979 | 0 | RATE_DISTORTION_RATIO; |
7980 | 0 | } |
7981 | |
|
7982 | 0 | if (parameters->cp_fixed_alloc) { |
7983 | 0 | size_t array_size = (size_t)parameters->tcp_numlayers * |
7984 | 0 | (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32); |
7985 | 0 | cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); |
7986 | 0 | if (!cp->m_specific_param.m_enc.m_matrice) { |
7987 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7988 | 0 | "Not enough memory to allocate copy of user encoding parameters matrix \n"); |
7989 | 0 | return OPJ_FALSE; |
7990 | 0 | } |
7991 | 0 | memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, |
7992 | 0 | array_size); |
7993 | 0 | } |
7994 | | |
7995 | | /* tiles */ |
7996 | 0 | cp->tdx = (OPJ_UINT32)parameters->cp_tdx; |
7997 | 0 | cp->tdy = (OPJ_UINT32)parameters->cp_tdy; |
7998 | | |
7999 | | /* tile offset */ |
8000 | 0 | cp->tx0 = (OPJ_UINT32)parameters->cp_tx0; |
8001 | 0 | cp->ty0 = (OPJ_UINT32)parameters->cp_ty0; |
8002 | | |
8003 | | /* comment string */ |
8004 | 0 | if (parameters->cp_comment) { |
8005 | 0 | cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U); |
8006 | 0 | if (!cp->comment) { |
8007 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8008 | 0 | "Not enough memory to allocate copy of comment string\n"); |
8009 | 0 | return OPJ_FALSE; |
8010 | 0 | } |
8011 | 0 | strcpy(cp->comment, parameters->cp_comment); |
8012 | 0 | } else { |
8013 | | /* Create default comment for codestream */ |
8014 | 0 | const char comment[] = "Created by OpenJPEG version "; |
8015 | 0 | const size_t clen = strlen(comment); |
8016 | 0 | const char *version = opj_version(); |
8017 | | |
8018 | | /* UniPG>> */ |
8019 | | #ifdef USE_JPWL |
8020 | | const size_t cp_comment_buf_size = clen + strlen(version) + 11; |
8021 | | cp->comment = (char*)opj_malloc(cp_comment_buf_size); |
8022 | | if (!cp->comment) { |
8023 | | opj_event_msg(p_manager, EVT_ERROR, |
8024 | | "Not enough memory to allocate comment string\n"); |
8025 | | return OPJ_FALSE; |
8026 | | } |
8027 | | snprintf(cp->comment, cp_comment_buf_size, "%s%s with JPWL", |
8028 | | comment, version); |
8029 | | #else |
8030 | 0 | const size_t cp_comment_buf_size = clen + strlen(version) + 1; |
8031 | 0 | cp->comment = (char*)opj_malloc(cp_comment_buf_size); |
8032 | 0 | if (!cp->comment) { |
8033 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8034 | 0 | "Not enough memory to allocate comment string\n"); |
8035 | 0 | return OPJ_FALSE; |
8036 | 0 | } |
8037 | 0 | snprintf(cp->comment, cp_comment_buf_size, "%s%s", comment, version); |
8038 | 0 | #endif |
8039 | | /* <<UniPG */ |
8040 | 0 | } |
8041 | | |
8042 | | /* |
8043 | | calculate other encoding parameters |
8044 | | */ |
8045 | | |
8046 | 0 | if (parameters->tile_size_on) { |
8047 | 0 | if (cp->tdx == 0) { |
8048 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile width\n"); |
8049 | 0 | return OPJ_FALSE; |
8050 | 0 | } |
8051 | 0 | if (cp->tdy == 0) { |
8052 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile height\n"); |
8053 | 0 | return OPJ_FALSE; |
8054 | 0 | } |
8055 | 0 | cp->tw = opj_uint_ceildiv(image->x1 - cp->tx0, cp->tdx); |
8056 | 0 | cp->th = opj_uint_ceildiv(image->y1 - cp->ty0, cp->tdy); |
8057 | | /* Check that the number of tiles is valid */ |
8058 | 0 | if (cp->tw > 65535 / cp->th) { |
8059 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8060 | 0 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", |
8061 | 0 | cp->tw, cp->th); |
8062 | 0 | return OPJ_FALSE; |
8063 | 0 | } |
8064 | 0 | } else { |
8065 | 0 | cp->tdx = image->x1 - cp->tx0; |
8066 | 0 | cp->tdy = image->y1 - cp->ty0; |
8067 | 0 | } |
8068 | | |
8069 | 0 | if (parameters->tp_on) { |
8070 | 0 | cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag; |
8071 | 0 | cp->m_specific_param.m_enc.m_tp_on = 1; |
8072 | 0 | } |
8073 | |
|
8074 | | #ifdef USE_JPWL |
8075 | | /* |
8076 | | calculate JPWL encoding parameters |
8077 | | */ |
8078 | | |
8079 | | if (parameters->jpwl_epc_on) { |
8080 | | OPJ_INT32 i; |
8081 | | |
8082 | | /* set JPWL on */ |
8083 | | cp->epc_on = OPJ_TRUE; |
8084 | | cp->info_on = OPJ_FALSE; /* no informative technique */ |
8085 | | |
8086 | | /* set EPB on */ |
8087 | | if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { |
8088 | | cp->epb_on = OPJ_TRUE; |
8089 | | |
8090 | | cp->hprot_MH = parameters->jpwl_hprot_MH; |
8091 | | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
8092 | | cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; |
8093 | | cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; |
8094 | | } |
8095 | | /* if tile specs are not specified, copy MH specs */ |
8096 | | if (cp->hprot_TPH[0] == -1) { |
8097 | | cp->hprot_TPH_tileno[0] = 0; |
8098 | | cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; |
8099 | | } |
8100 | | for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { |
8101 | | cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; |
8102 | | cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; |
8103 | | cp->pprot[i] = parameters->jpwl_pprot[i]; |
8104 | | } |
8105 | | } |
8106 | | |
8107 | | /* set ESD writing */ |
8108 | | if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { |
8109 | | cp->esd_on = OPJ_TRUE; |
8110 | | |
8111 | | cp->sens_size = parameters->jpwl_sens_size; |
8112 | | cp->sens_addr = parameters->jpwl_sens_addr; |
8113 | | cp->sens_range = parameters->jpwl_sens_range; |
8114 | | |
8115 | | cp->sens_MH = parameters->jpwl_sens_MH; |
8116 | | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
8117 | | cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; |
8118 | | cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; |
8119 | | } |
8120 | | } |
8121 | | |
8122 | | /* always set RED writing to false: we are at the encoder */ |
8123 | | cp->red_on = OPJ_FALSE; |
8124 | | |
8125 | | } else { |
8126 | | cp->epc_on = OPJ_FALSE; |
8127 | | } |
8128 | | #endif /* USE_JPWL */ |
8129 | | |
8130 | | /* initialize the multiple tiles */ |
8131 | | /* ---------------------------- */ |
8132 | 0 | cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); |
8133 | 0 | if (!cp->tcps) { |
8134 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8135 | 0 | "Not enough memory to allocate tile coding parameters\n"); |
8136 | 0 | return OPJ_FALSE; |
8137 | 0 | } |
8138 | | |
8139 | 0 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
8140 | 0 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
8141 | 0 | const OPJ_BOOL fixed_distoratio = |
8142 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy == |
8143 | 0 | FIXED_DISTORTION_RATIO; |
8144 | 0 | tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers; |
8145 | |
|
8146 | 0 | for (j = 0; j < tcp->numlayers; j++) { |
8147 | 0 | if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) { |
8148 | 0 | if (fixed_distoratio) { |
8149 | 0 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
8150 | 0 | } |
8151 | 0 | tcp->rates[j] = parameters->tcp_rates[j]; |
8152 | 0 | } else { |
8153 | 0 | if (fixed_distoratio) { |
8154 | 0 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
8155 | 0 | } else { |
8156 | 0 | tcp->rates[j] = parameters->tcp_rates[j]; |
8157 | 0 | } |
8158 | 0 | } |
8159 | 0 | if (!fixed_distoratio && |
8160 | 0 | tcp->rates[j] <= 1.0) { |
8161 | 0 | tcp->rates[j] = 0.0; /* force lossless */ |
8162 | 0 | } |
8163 | 0 | } |
8164 | |
|
8165 | 0 | tcp->csty = (OPJ_UINT32)parameters->csty; |
8166 | 0 | tcp->prg = parameters->prog_order; |
8167 | 0 | tcp->mct = (OPJ_UINT32)parameters->tcp_mct; |
8168 | |
|
8169 | 0 | numpocs_tile = 0; |
8170 | 0 | tcp->POC = 0; |
8171 | |
|
8172 | 0 | if (parameters->numpocs) { |
8173 | | /* initialisation of POC */ |
8174 | 0 | for (i = 0; i < parameters->numpocs; i++) { |
8175 | 0 | if (tileno + 1 == parameters->POC[i].tile) { |
8176 | 0 | opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; |
8177 | |
|
8178 | 0 | if (parameters->POC[numpocs_tile].compno0 >= image->numcomps) { |
8179 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8180 | 0 | "Invalid compno0 for POC %d\n", i); |
8181 | 0 | return OPJ_FALSE; |
8182 | 0 | } |
8183 | | |
8184 | 0 | tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; |
8185 | 0 | tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; |
8186 | 0 | tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; |
8187 | 0 | tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; |
8188 | 0 | tcp_poc->compno1 = opj_uint_min(parameters->POC[numpocs_tile].compno1, |
8189 | 0 | image->numcomps); |
8190 | 0 | tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; |
8191 | 0 | tcp_poc->tile = parameters->POC[numpocs_tile].tile; |
8192 | |
|
8193 | 0 | numpocs_tile++; |
8194 | 0 | } |
8195 | 0 | } |
8196 | | |
8197 | 0 | if (numpocs_tile) { |
8198 | | |
8199 | | /* TODO MSD use the return value*/ |
8200 | 0 | opj_j2k_check_poc_val(parameters->POC, tileno, parameters->numpocs, |
8201 | 0 | (OPJ_UINT32)parameters->numresolution, image->numcomps, |
8202 | 0 | (OPJ_UINT32)parameters->tcp_numlayers, p_manager); |
8203 | |
|
8204 | 0 | tcp->POC = 1; |
8205 | 0 | tcp->numpocs = numpocs_tile - 1 ; |
8206 | 0 | } |
8207 | 0 | } else { |
8208 | 0 | tcp->numpocs = 0; |
8209 | 0 | } |
8210 | | |
8211 | 0 | tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); |
8212 | 0 | if (!tcp->tccps) { |
8213 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8214 | 0 | "Not enough memory to allocate tile component coding parameters\n"); |
8215 | 0 | return OPJ_FALSE; |
8216 | 0 | } |
8217 | 0 | if (parameters->mct_data) { |
8218 | |
|
8219 | 0 | OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof( |
8220 | 0 | OPJ_FLOAT32); |
8221 | 0 | OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8222 | 0 | OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data + |
8223 | 0 | lMctSize); |
8224 | |
|
8225 | 0 | if (!lTmpBuf) { |
8226 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8227 | 0 | "Not enough memory to allocate temp buffer\n"); |
8228 | 0 | return OPJ_FALSE; |
8229 | 0 | } |
8230 | | |
8231 | 0 | tcp->mct = 2; |
8232 | 0 | tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8233 | 0 | if (! tcp->m_mct_coding_matrix) { |
8234 | 0 | opj_free(lTmpBuf); |
8235 | 0 | lTmpBuf = NULL; |
8236 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8237 | 0 | "Not enough memory to allocate encoder MCT coding matrix \n"); |
8238 | 0 | return OPJ_FALSE; |
8239 | 0 | } |
8240 | 0 | memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize); |
8241 | 0 | memcpy(lTmpBuf, parameters->mct_data, lMctSize); |
8242 | |
|
8243 | 0 | tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8244 | 0 | if (! tcp->m_mct_decoding_matrix) { |
8245 | 0 | opj_free(lTmpBuf); |
8246 | 0 | lTmpBuf = NULL; |
8247 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8248 | 0 | "Not enough memory to allocate encoder MCT decoding matrix \n"); |
8249 | 0 | return OPJ_FALSE; |
8250 | 0 | } |
8251 | 0 | if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix), |
8252 | 0 | image->numcomps) == OPJ_FALSE) { |
8253 | 0 | opj_free(lTmpBuf); |
8254 | 0 | lTmpBuf = NULL; |
8255 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8256 | 0 | "Failed to inverse encoder MCT decoding matrix \n"); |
8257 | 0 | return OPJ_FALSE; |
8258 | 0 | } |
8259 | | |
8260 | 0 | tcp->mct_norms = (OPJ_FLOAT64*) |
8261 | 0 | opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); |
8262 | 0 | if (! tcp->mct_norms) { |
8263 | 0 | opj_free(lTmpBuf); |
8264 | 0 | lTmpBuf = NULL; |
8265 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8266 | 0 | "Not enough memory to allocate encoder MCT norms \n"); |
8267 | 0 | return OPJ_FALSE; |
8268 | 0 | } |
8269 | 0 | opj_calculate_norms(tcp->mct_norms, image->numcomps, |
8270 | 0 | tcp->m_mct_decoding_matrix); |
8271 | 0 | opj_free(lTmpBuf); |
8272 | |
|
8273 | 0 | for (i = 0; i < image->numcomps; i++) { |
8274 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8275 | 0 | tccp->m_dc_level_shift = l_dc_shift[i]; |
8276 | 0 | } |
8277 | |
|
8278 | 0 | if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) { |
8279 | | /* free will be handled by opj_j2k_destroy */ |
8280 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n"); |
8281 | 0 | return OPJ_FALSE; |
8282 | 0 | } |
8283 | 0 | } else { |
8284 | 0 | if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */ |
8285 | 0 | if ((image->comps[0].dx != image->comps[1].dx) || |
8286 | 0 | (image->comps[0].dx != image->comps[2].dx) || |
8287 | 0 | (image->comps[0].dy != image->comps[1].dy) || |
8288 | 0 | (image->comps[0].dy != image->comps[2].dy)) { |
8289 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
8290 | 0 | "Cannot perform MCT on components with different sizes. Disabling MCT.\n"); |
8291 | 0 | tcp->mct = 0; |
8292 | 0 | } |
8293 | 0 | } |
8294 | 0 | for (i = 0; i < image->numcomps; i++) { |
8295 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8296 | 0 | opj_image_comp_t * l_comp = &(image->comps[i]); |
8297 | |
|
8298 | 0 | if (! l_comp->sgnd) { |
8299 | 0 | tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); |
8300 | 0 | } |
8301 | 0 | } |
8302 | 0 | } |
8303 | | |
8304 | 0 | for (i = 0; i < image->numcomps; i++) { |
8305 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8306 | |
|
8307 | 0 | tccp->csty = parameters->csty & |
8308 | 0 | 0x01; /* 0 => one precinct || 1 => custom precinct */ |
8309 | 0 | tccp->numresolutions = (OPJ_UINT32)parameters->numresolution; |
8310 | 0 | tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
8311 | 0 | tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
8312 | 0 | tccp->cblksty = (OPJ_UINT32)parameters->mode; |
8313 | 0 | tccp->qmfbid = parameters->irreversible ? 0 : 1; |
8314 | 0 | tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : |
8315 | 0 | J2K_CCP_QNTSTY_NOQNT; |
8316 | |
|
8317 | 0 | if (OPJ_IS_CINEMA(parameters->rsiz) && |
8318 | 0 | parameters->rsiz == OPJ_PROFILE_CINEMA_2K) { |
8319 | | /* From https://github.com/uclouvain/openjpeg/issues/1340 */ |
8320 | 0 | tccp->numgbits = 1; |
8321 | 0 | } else { |
8322 | 0 | tccp->numgbits = 2; |
8323 | 0 | } |
8324 | |
|
8325 | 0 | if ((OPJ_INT32)i == parameters->roi_compno) { |
8326 | 0 | tccp->roishift = parameters->roi_shift; |
8327 | 0 | } else { |
8328 | 0 | tccp->roishift = 0; |
8329 | 0 | } |
8330 | |
|
8331 | 0 | if (parameters->csty & J2K_CCP_CSTY_PRT) { |
8332 | 0 | OPJ_INT32 p = 0, it_res; |
8333 | 0 | assert(tccp->numresolutions > 0); |
8334 | 0 | for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) { |
8335 | 0 | if (p < parameters->res_spec) { |
8336 | |
|
8337 | 0 | if (parameters->prcw_init[p] < 1) { |
8338 | 0 | tccp->prcw[it_res] = 1; |
8339 | 0 | } else { |
8340 | 0 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]); |
8341 | 0 | } |
8342 | |
|
8343 | 0 | if (parameters->prch_init[p] < 1) { |
8344 | 0 | tccp->prch[it_res] = 1; |
8345 | 0 | } else { |
8346 | 0 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]); |
8347 | 0 | } |
8348 | |
|
8349 | 0 | } else { |
8350 | 0 | OPJ_INT32 res_spec = parameters->res_spec; |
8351 | 0 | OPJ_INT32 size_prcw = 0; |
8352 | 0 | OPJ_INT32 size_prch = 0; |
8353 | |
|
8354 | 0 | assert(res_spec > 0); /* issue 189 */ |
8355 | 0 | size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); |
8356 | 0 | size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); |
8357 | | |
8358 | |
|
8359 | 0 | if (size_prcw < 1) { |
8360 | 0 | tccp->prcw[it_res] = 1; |
8361 | 0 | } else { |
8362 | 0 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw); |
8363 | 0 | } |
8364 | |
|
8365 | 0 | if (size_prch < 1) { |
8366 | 0 | tccp->prch[it_res] = 1; |
8367 | 0 | } else { |
8368 | 0 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch); |
8369 | 0 | } |
8370 | 0 | } |
8371 | 0 | p++; |
8372 | | /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ |
8373 | 0 | } /*end for*/ |
8374 | 0 | } else { |
8375 | 0 | for (j = 0; j < tccp->numresolutions; j++) { |
8376 | 0 | tccp->prcw[j] = 15; |
8377 | 0 | tccp->prch[j] = 15; |
8378 | 0 | } |
8379 | 0 | } |
8380 | |
|
8381 | 0 | opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); |
8382 | 0 | } |
8383 | 0 | } |
8384 | | |
8385 | 0 | if (parameters->mct_data) { |
8386 | 0 | opj_free(parameters->mct_data); |
8387 | 0 | parameters->mct_data = 00; |
8388 | 0 | } |
8389 | 0 | return OPJ_TRUE; |
8390 | 0 | } |
8391 | | |
8392 | | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, |
8393 | | OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) |
8394 | 168k | { |
8395 | 168k | assert(cstr_index != 00); |
8396 | | |
8397 | | /* expand the list? */ |
8398 | 168k | if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { |
8399 | 1.04k | opj_marker_info_t *new_marker; |
8400 | 1.04k | cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) |
8401 | 1.04k | cstr_index->maxmarknum); |
8402 | 1.04k | new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, |
8403 | 1.04k | cstr_index->maxmarknum * sizeof(opj_marker_info_t)); |
8404 | 1.04k | if (! new_marker) { |
8405 | 0 | opj_free(cstr_index->marker); |
8406 | 0 | cstr_index->marker = NULL; |
8407 | 0 | cstr_index->maxmarknum = 0; |
8408 | 0 | cstr_index->marknum = 0; |
8409 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */ |
8410 | 0 | return OPJ_FALSE; |
8411 | 0 | } |
8412 | 1.04k | cstr_index->marker = new_marker; |
8413 | 1.04k | } |
8414 | | |
8415 | | /* add the marker */ |
8416 | 168k | cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; |
8417 | 168k | cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; |
8418 | 168k | cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; |
8419 | 168k | cstr_index->marknum++; |
8420 | 168k | return OPJ_TRUE; |
8421 | 168k | } |
8422 | | |
8423 | | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, |
8424 | | opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, |
8425 | | OPJ_UINT32 len) |
8426 | 19.9k | { |
8427 | 19.9k | assert(cstr_index != 00); |
8428 | 19.9k | assert(cstr_index->tile_index != 00); |
8429 | | |
8430 | | /* expand the list? */ |
8431 | 19.9k | if ((cstr_index->tile_index[tileno].marknum + 1) > |
8432 | 19.9k | cstr_index->tile_index[tileno].maxmarknum) { |
8433 | 0 | opj_marker_info_t *new_marker; |
8434 | 0 | cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + |
8435 | 0 | (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum); |
8436 | 0 | new_marker = (opj_marker_info_t *) opj_realloc( |
8437 | 0 | cstr_index->tile_index[tileno].marker, |
8438 | 0 | cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t)); |
8439 | 0 | if (! new_marker) { |
8440 | 0 | opj_free(cstr_index->tile_index[tileno].marker); |
8441 | 0 | cstr_index->tile_index[tileno].marker = NULL; |
8442 | 0 | cstr_index->tile_index[tileno].maxmarknum = 0; |
8443 | 0 | cstr_index->tile_index[tileno].marknum = 0; |
8444 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */ |
8445 | 0 | return OPJ_FALSE; |
8446 | 0 | } |
8447 | 0 | cstr_index->tile_index[tileno].marker = new_marker; |
8448 | 0 | } |
8449 | | |
8450 | | /* add the marker */ |
8451 | 19.9k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type |
8452 | 19.9k | = (OPJ_UINT16)type; |
8453 | 19.9k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos |
8454 | 19.9k | = (OPJ_INT32)pos; |
8455 | 19.9k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len |
8456 | 19.9k | = (OPJ_INT32)len; |
8457 | 19.9k | cstr_index->tile_index[tileno].marknum++; |
8458 | | |
8459 | 19.9k | if (type == J2K_MS_SOT) { |
8460 | 9.85k | OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; |
8461 | | |
8462 | 9.85k | if (cstr_index->tile_index[tileno].tp_index && |
8463 | 9.85k | l_current_tile_part < cstr_index->tile_index[tileno].nb_tps) { |
8464 | 9.78k | cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; |
8465 | 9.78k | } |
8466 | | |
8467 | 9.85k | } |
8468 | 19.9k | return OPJ_TRUE; |
8469 | 19.9k | } |
8470 | | |
8471 | | /* |
8472 | | * ----------------------------------------------------------------------- |
8473 | | * ----------------------------------------------------------------------- |
8474 | | * ----------------------------------------------------------------------- |
8475 | | */ |
8476 | | |
8477 | | OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k, |
8478 | | opj_stream_private_t *p_stream, |
8479 | | opj_event_mgr_t * p_manager |
8480 | | ) |
8481 | 0 | { |
8482 | 0 | (void)p_j2k; |
8483 | 0 | (void)p_stream; |
8484 | 0 | (void)p_manager; |
8485 | 0 | return OPJ_TRUE; |
8486 | 0 | } |
8487 | | |
8488 | | OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream, |
8489 | | opj_j2k_t* p_j2k, |
8490 | | opj_image_t** p_image, |
8491 | | opj_event_mgr_t* p_manager) |
8492 | 8.05k | { |
8493 | | /* preconditions */ |
8494 | 8.05k | assert(p_j2k != 00); |
8495 | 8.05k | assert(p_stream != 00); |
8496 | 8.05k | assert(p_manager != 00); |
8497 | | |
8498 | | /* create an empty image header */ |
8499 | 8.05k | p_j2k->m_private_image = opj_image_create0(); |
8500 | 8.05k | if (! p_j2k->m_private_image) { |
8501 | 0 | return OPJ_FALSE; |
8502 | 0 | } |
8503 | | |
8504 | | /* customization of the validation */ |
8505 | 8.05k | if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) { |
8506 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8507 | 0 | p_j2k->m_private_image = NULL; |
8508 | 0 | return OPJ_FALSE; |
8509 | 0 | } |
8510 | | |
8511 | | /* validation of the parameters codec */ |
8512 | 8.05k | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
8513 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8514 | 0 | p_j2k->m_private_image = NULL; |
8515 | 0 | return OPJ_FALSE; |
8516 | 0 | } |
8517 | | |
8518 | | /* customization of the encoding */ |
8519 | 8.05k | if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) { |
8520 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8521 | 0 | p_j2k->m_private_image = NULL; |
8522 | 0 | return OPJ_FALSE; |
8523 | 0 | } |
8524 | | |
8525 | | /* read header */ |
8526 | 8.05k | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
8527 | 809 | opj_image_destroy(p_j2k->m_private_image); |
8528 | 809 | p_j2k->m_private_image = NULL; |
8529 | 809 | return OPJ_FALSE; |
8530 | 809 | } |
8531 | | |
8532 | 7.24k | *p_image = opj_image_create0(); |
8533 | 7.24k | if (!(*p_image)) { |
8534 | 0 | return OPJ_FALSE; |
8535 | 0 | } |
8536 | | |
8537 | | /* Copy codestream image information to the output image */ |
8538 | 7.24k | opj_copy_image_header(p_j2k->m_private_image, *p_image); |
8539 | | |
8540 | 7.24k | return OPJ_TRUE; |
8541 | 7.24k | } |
8542 | | |
8543 | | static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, |
8544 | | opj_event_mgr_t * p_manager) |
8545 | 8.05k | { |
8546 | | /* preconditions*/ |
8547 | 8.05k | assert(p_j2k != 00); |
8548 | 8.05k | assert(p_manager != 00); |
8549 | | |
8550 | 8.05k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
8551 | 8.05k | (opj_procedure)opj_j2k_read_header_procedure, p_manager)) { |
8552 | 0 | return OPJ_FALSE; |
8553 | 0 | } |
8554 | | |
8555 | | /* DEVELOPER CORNER, add your custom procedures */ |
8556 | 8.05k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
8557 | 8.05k | (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) { |
8558 | 0 | return OPJ_FALSE; |
8559 | 0 | } |
8560 | | |
8561 | 8.05k | return OPJ_TRUE; |
8562 | 8.05k | } |
8563 | | |
8564 | | static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, |
8565 | | opj_event_mgr_t * p_manager) |
8566 | 8.05k | { |
8567 | | /* preconditions*/ |
8568 | 8.05k | assert(p_j2k != 00); |
8569 | 8.05k | assert(p_manager != 00); |
8570 | | |
8571 | 8.05k | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
8572 | 8.05k | (opj_procedure)opj_j2k_build_decoder, p_manager)) { |
8573 | 0 | return OPJ_FALSE; |
8574 | 0 | } |
8575 | 8.05k | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
8576 | 8.05k | (opj_procedure)opj_j2k_decoding_validation, p_manager)) { |
8577 | 0 | return OPJ_FALSE; |
8578 | 0 | } |
8579 | | |
8580 | | /* DEVELOPER CORNER, add your custom validation procedure */ |
8581 | 8.05k | return OPJ_TRUE; |
8582 | 8.05k | } |
8583 | | |
8584 | | static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, |
8585 | | opj_stream_private_t *p_stream, |
8586 | | opj_event_mgr_t * p_manager) |
8587 | 0 | { |
8588 | 0 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8589 | 0 | OPJ_UINT32 i, j; |
8590 | | |
8591 | | /* preconditions */ |
8592 | 0 | assert(p_j2k != 00); |
8593 | 0 | assert(p_stream != 00); |
8594 | 0 | assert(p_manager != 00); |
8595 | |
|
8596 | 0 | OPJ_UNUSED(p_stream); |
8597 | 0 | OPJ_UNUSED(p_manager); |
8598 | |
|
8599 | 0 | if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { |
8600 | 0 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
8601 | 0 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
8602 | |
|
8603 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
8604 | 0 | if (l_tcp->mct == 2) { |
8605 | 0 | opj_tccp_t * l_tccp = l_tcp->tccps; |
8606 | 0 | l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); |
8607 | |
|
8608 | 0 | for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) { |
8609 | 0 | l_is_valid &= !(l_tccp->qmfbid & 1); |
8610 | 0 | ++l_tccp; |
8611 | 0 | } |
8612 | 0 | } |
8613 | 0 | ++l_tcp; |
8614 | 0 | } |
8615 | 0 | } |
8616 | |
|
8617 | 0 | return l_is_valid; |
8618 | 0 | } |
8619 | | |
8620 | | OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) |
8621 | 0 | { |
8622 | 0 | OPJ_UINT32 i; |
8623 | 0 | OPJ_UINT32 l_indix = 1; |
8624 | 0 | opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00; |
8625 | 0 | opj_simple_mcc_decorrelation_data_t * l_mcc_data; |
8626 | 0 | OPJ_UINT32 l_mct_size, l_nb_elem; |
8627 | 0 | OPJ_FLOAT32 * l_data, * l_current_data; |
8628 | 0 | opj_tccp_t * l_tccp; |
8629 | | |
8630 | | /* preconditions */ |
8631 | 0 | assert(p_tcp != 00); |
8632 | |
|
8633 | 0 | if (p_tcp->mct != 2) { |
8634 | 0 | return OPJ_TRUE; |
8635 | 0 | } |
8636 | | |
8637 | 0 | if (p_tcp->m_mct_decoding_matrix) { |
8638 | 0 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
8639 | 0 | opj_mct_data_t *new_mct_records; |
8640 | 0 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8641 | |
|
8642 | 0 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
8643 | 0 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
8644 | 0 | if (! new_mct_records) { |
8645 | 0 | opj_free(p_tcp->m_mct_records); |
8646 | 0 | p_tcp->m_mct_records = NULL; |
8647 | 0 | p_tcp->m_nb_max_mct_records = 0; |
8648 | 0 | p_tcp->m_nb_mct_records = 0; |
8649 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8650 | 0 | return OPJ_FALSE; |
8651 | 0 | } |
8652 | 0 | p_tcp->m_mct_records = new_mct_records; |
8653 | 0 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8654 | |
|
8655 | 0 | memset(l_mct_deco_data, 0, |
8656 | 0 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
8657 | 0 | opj_mct_data_t)); |
8658 | 0 | } |
8659 | 0 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8660 | |
|
8661 | 0 | if (l_mct_deco_data->m_data) { |
8662 | 0 | opj_free(l_mct_deco_data->m_data); |
8663 | 0 | l_mct_deco_data->m_data = 00; |
8664 | 0 | } |
8665 | |
|
8666 | 0 | l_mct_deco_data->m_index = l_indix++; |
8667 | 0 | l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; |
8668 | 0 | l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; |
8669 | 0 | l_nb_elem = p_image->numcomps * p_image->numcomps; |
8670 | 0 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; |
8671 | 0 | l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
8672 | |
|
8673 | 0 | if (! l_mct_deco_data->m_data) { |
8674 | 0 | return OPJ_FALSE; |
8675 | 0 | } |
8676 | | |
8677 | 0 | j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type]( |
8678 | 0 | p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem); |
8679 | |
|
8680 | 0 | l_mct_deco_data->m_data_size = l_mct_size; |
8681 | 0 | ++p_tcp->m_nb_mct_records; |
8682 | 0 | } |
8683 | | |
8684 | 0 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
8685 | 0 | opj_mct_data_t *new_mct_records; |
8686 | 0 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8687 | 0 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
8688 | 0 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
8689 | 0 | if (! new_mct_records) { |
8690 | 0 | opj_free(p_tcp->m_mct_records); |
8691 | 0 | p_tcp->m_mct_records = NULL; |
8692 | 0 | p_tcp->m_nb_max_mct_records = 0; |
8693 | 0 | p_tcp->m_nb_mct_records = 0; |
8694 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8695 | 0 | return OPJ_FALSE; |
8696 | 0 | } |
8697 | 0 | p_tcp->m_mct_records = new_mct_records; |
8698 | 0 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8699 | |
|
8700 | 0 | memset(l_mct_offset_data, 0, |
8701 | 0 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
8702 | 0 | opj_mct_data_t)); |
8703 | |
|
8704 | 0 | if (l_mct_deco_data) { |
8705 | 0 | l_mct_deco_data = l_mct_offset_data - 1; |
8706 | 0 | } |
8707 | 0 | } |
8708 | | |
8709 | 0 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8710 | |
|
8711 | 0 | if (l_mct_offset_data->m_data) { |
8712 | 0 | opj_free(l_mct_offset_data->m_data); |
8713 | 0 | l_mct_offset_data->m_data = 00; |
8714 | 0 | } |
8715 | |
|
8716 | 0 | l_mct_offset_data->m_index = l_indix++; |
8717 | 0 | l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; |
8718 | 0 | l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; |
8719 | 0 | l_nb_elem = p_image->numcomps; |
8720 | 0 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; |
8721 | 0 | l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
8722 | |
|
8723 | 0 | if (! l_mct_offset_data->m_data) { |
8724 | 0 | return OPJ_FALSE; |
8725 | 0 | } |
8726 | | |
8727 | 0 | l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); |
8728 | 0 | if (! l_data) { |
8729 | 0 | opj_free(l_mct_offset_data->m_data); |
8730 | 0 | l_mct_offset_data->m_data = 00; |
8731 | 0 | return OPJ_FALSE; |
8732 | 0 | } |
8733 | | |
8734 | 0 | l_tccp = p_tcp->tccps; |
8735 | 0 | l_current_data = l_data; |
8736 | |
|
8737 | 0 | for (i = 0; i < l_nb_elem; ++i) { |
8738 | 0 | *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift); |
8739 | 0 | ++l_tccp; |
8740 | 0 | } |
8741 | |
|
8742 | 0 | j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data, |
8743 | 0 | l_mct_offset_data->m_data, l_nb_elem); |
8744 | |
|
8745 | 0 | opj_free(l_data); |
8746 | |
|
8747 | 0 | l_mct_offset_data->m_data_size = l_mct_size; |
8748 | |
|
8749 | 0 | ++p_tcp->m_nb_mct_records; |
8750 | |
|
8751 | 0 | if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { |
8752 | 0 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
8753 | 0 | p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8754 | 0 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
8755 | 0 | p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof( |
8756 | 0 | opj_simple_mcc_decorrelation_data_t)); |
8757 | 0 | if (! new_mcc_records) { |
8758 | 0 | opj_free(p_tcp->m_mcc_records); |
8759 | 0 | p_tcp->m_mcc_records = NULL; |
8760 | 0 | p_tcp->m_nb_max_mcc_records = 0; |
8761 | 0 | p_tcp->m_nb_mcc_records = 0; |
8762 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8763 | 0 | return OPJ_FALSE; |
8764 | 0 | } |
8765 | 0 | p_tcp->m_mcc_records = new_mcc_records; |
8766 | 0 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
8767 | 0 | memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * |
8768 | 0 | sizeof(opj_simple_mcc_decorrelation_data_t)); |
8769 | |
|
8770 | 0 | } |
8771 | | |
8772 | 0 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
8773 | 0 | l_mcc_data->m_decorrelation_array = l_mct_deco_data; |
8774 | 0 | l_mcc_data->m_is_irreversible = 1; |
8775 | 0 | l_mcc_data->m_nb_comps = p_image->numcomps; |
8776 | 0 | l_mcc_data->m_index = l_indix++; |
8777 | 0 | l_mcc_data->m_offset_array = l_mct_offset_data; |
8778 | 0 | ++p_tcp->m_nb_mcc_records; |
8779 | |
|
8780 | 0 | return OPJ_TRUE; |
8781 | 0 | } |
8782 | | |
8783 | | static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, |
8784 | | opj_stream_private_t *p_stream, |
8785 | | opj_event_mgr_t * p_manager) |
8786 | 8.05k | { |
8787 | | /* add here initialization of cp |
8788 | | copy paste of setup_decoder */ |
8789 | 8.05k | (void)p_j2k; |
8790 | 8.05k | (void)p_stream; |
8791 | 8.05k | (void)p_manager; |
8792 | 8.05k | return OPJ_TRUE; |
8793 | 8.05k | } |
8794 | | |
8795 | | static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, |
8796 | | opj_stream_private_t *p_stream, |
8797 | | opj_event_mgr_t * p_manager) |
8798 | 0 | { |
8799 | | /* add here initialization of cp |
8800 | | copy paste of setup_encoder */ |
8801 | 0 | (void)p_j2k; |
8802 | 0 | (void)p_stream; |
8803 | 0 | (void)p_manager; |
8804 | 0 | return OPJ_TRUE; |
8805 | 0 | } |
8806 | | |
8807 | | static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, |
8808 | | opj_stream_private_t *p_stream, |
8809 | | opj_event_mgr_t * p_manager) |
8810 | 0 | { |
8811 | 0 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8812 | | |
8813 | | /* preconditions */ |
8814 | 0 | assert(p_j2k != 00); |
8815 | 0 | assert(p_stream != 00); |
8816 | 0 | assert(p_manager != 00); |
8817 | |
|
8818 | 0 | OPJ_UNUSED(p_stream); |
8819 | | |
8820 | | /* STATE checking */ |
8821 | | /* make sure the state is at 0 */ |
8822 | 0 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); |
8823 | | |
8824 | | /* POINTER validation */ |
8825 | | /* make sure a p_j2k codec is present */ |
8826 | 0 | l_is_valid &= (p_j2k->m_procedure_list != 00); |
8827 | | /* make sure a validation list is present */ |
8828 | 0 | l_is_valid &= (p_j2k->m_validation_list != 00); |
8829 | | |
8830 | | /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */ |
8831 | | /* 33 (32) would always fail the check below (if a cast to 64bits was done) */ |
8832 | | /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */ |
8833 | 0 | if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || |
8834 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) { |
8835 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8836 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8837 | 0 | return OPJ_FALSE; |
8838 | 0 | } |
8839 | | |
8840 | 0 | if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 << |
8841 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
8842 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8843 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8844 | 0 | return OPJ_FALSE; |
8845 | 0 | } |
8846 | | |
8847 | 0 | if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 << |
8848 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
8849 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8850 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8851 | 0 | return OPJ_FALSE; |
8852 | 0 | } |
8853 | | |
8854 | | /* PARAMETER VALIDATION */ |
8855 | 0 | return l_is_valid; |
8856 | 0 | } |
8857 | | |
8858 | | static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k, |
8859 | | opj_stream_private_t *p_stream, |
8860 | | opj_event_mgr_t * p_manager |
8861 | | ) |
8862 | 8.05k | { |
8863 | 8.05k | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8864 | | |
8865 | | /* preconditions*/ |
8866 | 8.05k | assert(p_j2k != 00); |
8867 | 8.05k | assert(p_stream != 00); |
8868 | 8.05k | assert(p_manager != 00); |
8869 | | |
8870 | 8.05k | OPJ_UNUSED(p_stream); |
8871 | 8.05k | OPJ_UNUSED(p_manager); |
8872 | | |
8873 | | /* STATE checking */ |
8874 | | /* make sure the state is at 0 */ |
8875 | | #ifdef TODO_MSD |
8876 | | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); |
8877 | | #endif |
8878 | 8.05k | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); |
8879 | | |
8880 | | /* POINTER validation */ |
8881 | | /* make sure a p_j2k codec is present */ |
8882 | | /* make sure a procedure list is present */ |
8883 | 8.05k | l_is_valid &= (p_j2k->m_procedure_list != 00); |
8884 | | /* make sure a validation list is present */ |
8885 | 8.05k | l_is_valid &= (p_j2k->m_validation_list != 00); |
8886 | | |
8887 | | /* PARAMETER VALIDATION */ |
8888 | 8.05k | return l_is_valid; |
8889 | 8.05k | } |
8890 | | |
8891 | | /** Fill p_j2k->cstr_index->tp_index[].start_pos/end_pos fields from TLM marker segments */ |
8892 | | static void opj_j2k_build_tp_index_from_tlm(opj_j2k_t* p_j2k, |
8893 | | opj_event_mgr_t * p_manager) |
8894 | 7.24k | { |
8895 | 7.24k | opj_j2k_tlm_info_t* l_tlm; |
8896 | 7.24k | OPJ_UINT32 i; |
8897 | 7.24k | OPJ_OFF_T l_cur_offset; |
8898 | | |
8899 | 7.24k | assert(p_j2k->cstr_index->main_head_end > 0); |
8900 | 7.24k | assert(p_j2k->cstr_index->nb_of_tiles > 0); |
8901 | 7.24k | assert(p_j2k->cstr_index->tile_index != NULL); |
8902 | | |
8903 | 7.24k | l_tlm = &(p_j2k->m_specific_param.m_decoder.m_tlm); |
8904 | | |
8905 | 7.24k | if (l_tlm->m_entries_count == 0) { |
8906 | 7.19k | l_tlm->m_is_invalid = OPJ_TRUE; |
8907 | 7.19k | return; |
8908 | 7.19k | } |
8909 | | |
8910 | 56 | if (l_tlm->m_is_invalid) { |
8911 | 5 | return; |
8912 | 5 | } |
8913 | | |
8914 | | /* Initial pass to count the number of tile-parts per tile */ |
8915 | 3.87k | for (i = 0; i < l_tlm->m_entries_count; ++i) { |
8916 | 3.82k | OPJ_UINT32 l_tile_index_no = l_tlm->m_tile_part_infos[i].m_tile_index; |
8917 | 3.82k | assert(l_tile_index_no < p_j2k->cstr_index->nb_of_tiles); |
8918 | 3.82k | p_j2k->cstr_index->tile_index[l_tile_index_no].tileno = l_tile_index_no; |
8919 | 3.82k | ++p_j2k->cstr_index->tile_index[l_tile_index_no].current_nb_tps; |
8920 | 3.82k | } |
8921 | | |
8922 | | /* Now check that all tiles have at least one tile-part */ |
8923 | 2.23k | for (i = 0; i < p_j2k->cstr_index->nb_of_tiles; ++i) { |
8924 | 2.20k | if (p_j2k->cstr_index->tile_index[i].current_nb_tps == 0) { |
8925 | 17 | opj_event_msg(p_manager, EVT_ERROR, |
8926 | 17 | "opj_j2k_build_tp_index_from_tlm(): tile %d has no " |
8927 | 17 | "registered tile-part in TLM marker segments.\n", i); |
8928 | 17 | goto error; |
8929 | 17 | } |
8930 | 2.20k | } |
8931 | | |
8932 | | /* Final pass to fill p_j2k->cstr_index */ |
8933 | 34 | l_cur_offset = p_j2k->cstr_index->main_head_end; |
8934 | 3.36k | for (i = 0; i < l_tlm->m_entries_count; ++i) { |
8935 | 3.33k | OPJ_UINT32 l_tile_index_no = l_tlm->m_tile_part_infos[i].m_tile_index; |
8936 | 3.33k | opj_tile_index_t* l_tile_index = & |
8937 | 3.33k | (p_j2k->cstr_index->tile_index[l_tile_index_no]); |
8938 | 3.33k | if (!l_tile_index->tp_index) { |
8939 | 1.86k | l_tile_index->tp_index = (opj_tp_index_t *) opj_calloc( |
8940 | 1.86k | l_tile_index->current_nb_tps, sizeof(opj_tp_index_t)); |
8941 | 1.86k | if (! l_tile_index->tp_index) { |
8942 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8943 | 0 | "opj_j2k_build_tp_index_from_tlm(): tile index allocation failed\n"); |
8944 | 0 | goto error; |
8945 | 0 | } |
8946 | 1.86k | } |
8947 | | |
8948 | 3.33k | assert(l_tile_index->nb_tps < l_tile_index->current_nb_tps); |
8949 | 3.33k | l_tile_index->tp_index[l_tile_index->nb_tps].start_pos = l_cur_offset; |
8950 | | /* We don't know how to set the tp_index[].end_header field, but this is not really needed */ |
8951 | | /* If there would be no markers between SOT and SOD, that would be : */ |
8952 | | /* l_tile_index->tp_index[l_tile_index->nb_tps].end_header = l_cur_offset + 12; */ |
8953 | 3.33k | l_tile_index->tp_index[l_tile_index->nb_tps].end_pos = l_cur_offset + |
8954 | 3.33k | l_tlm->m_tile_part_infos[i].m_length; |
8955 | 3.33k | ++l_tile_index->nb_tps; |
8956 | | |
8957 | 3.33k | l_cur_offset += l_tlm->m_tile_part_infos[i].m_length; |
8958 | 3.33k | } |
8959 | | |
8960 | 34 | return; |
8961 | | |
8962 | 34 | error: |
8963 | 17 | l_tlm->m_is_invalid = OPJ_TRUE; |
8964 | 505 | for (i = 0; i < l_tlm->m_entries_count; ++i) { |
8965 | 488 | OPJ_UINT32 l_tile_index = l_tlm->m_tile_part_infos[i].m_tile_index; |
8966 | 488 | p_j2k->cstr_index->tile_index[l_tile_index].current_nb_tps = 0; |
8967 | 488 | opj_free(p_j2k->cstr_index->tile_index[l_tile_index].tp_index); |
8968 | 488 | p_j2k->cstr_index->tile_index[l_tile_index].tp_index = NULL; |
8969 | 488 | } |
8970 | 17 | } |
8971 | | |
8972 | | static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, |
8973 | | opj_stream_private_t *p_stream, |
8974 | | opj_event_mgr_t * p_manager) |
8975 | 8.05k | { |
8976 | 8.05k | OPJ_UINT32 l_current_marker; |
8977 | 8.05k | OPJ_UINT32 l_marker_size; |
8978 | 8.05k | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
8979 | 8.05k | OPJ_BOOL l_has_siz = 0; |
8980 | 8.05k | OPJ_BOOL l_has_cod = 0; |
8981 | 8.05k | OPJ_BOOL l_has_qcd = 0; |
8982 | | |
8983 | | /* preconditions */ |
8984 | 8.05k | assert(p_stream != 00); |
8985 | 8.05k | assert(p_j2k != 00); |
8986 | 8.05k | assert(p_manager != 00); |
8987 | | |
8988 | | /* We enter in the main header */ |
8989 | 8.05k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; |
8990 | | |
8991 | | /* Try to read the SOC marker, the codestream must begin with SOC marker */ |
8992 | 8.05k | if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) { |
8993 | 7 | opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n"); |
8994 | 7 | return OPJ_FALSE; |
8995 | 7 | } |
8996 | | |
8997 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8998 | 8.04k | if (opj_stream_read_data(p_stream, |
8999 | 8.04k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9000 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9001 | 1 | return OPJ_FALSE; |
9002 | 1 | } |
9003 | | |
9004 | | /* Read 2 bytes as the new marker ID */ |
9005 | 8.04k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9006 | 8.04k | &l_current_marker, 2); |
9007 | | |
9008 | | /* Try to read until the SOT is detected */ |
9009 | 115k | while (l_current_marker != J2K_MS_SOT) { |
9010 | | |
9011 | | /* Check if the current marker ID is valid */ |
9012 | 108k | if (l_current_marker < 0xff00) { |
9013 | 201 | opj_event_msg(p_manager, EVT_ERROR, |
9014 | 201 | "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker); |
9015 | 201 | return OPJ_FALSE; |
9016 | 201 | } |
9017 | | |
9018 | | /* Get the marker handler from the marker ID */ |
9019 | 108k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
9020 | | |
9021 | | /* Manage case where marker is unknown */ |
9022 | 108k | if (l_marker_handler->id == J2K_MS_UNK) { |
9023 | 52.8k | if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) { |
9024 | 242 | opj_event_msg(p_manager, EVT_ERROR, |
9025 | 242 | "Unknown marker has been detected and generated error.\n"); |
9026 | 242 | return OPJ_FALSE; |
9027 | 242 | } |
9028 | | |
9029 | 52.6k | if (l_current_marker == J2K_MS_SOT) { |
9030 | 310 | break; /* SOT marker is detected main header is completely read */ |
9031 | 52.3k | } else { /* Get the marker handler from the marker ID */ |
9032 | 52.3k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
9033 | 52.3k | } |
9034 | 52.6k | } |
9035 | | |
9036 | 107k | if (l_marker_handler->id == J2K_MS_SIZ) { |
9037 | | /* Mark required SIZ marker as found */ |
9038 | 8.04k | l_has_siz = 1; |
9039 | 8.04k | } |
9040 | 107k | if (l_marker_handler->id == J2K_MS_COD) { |
9041 | | /* Mark required COD marker as found */ |
9042 | 12.9k | l_has_cod = 1; |
9043 | 12.9k | } |
9044 | 107k | if (l_marker_handler->id == J2K_MS_QCD) { |
9045 | | /* Mark required QCD marker as found */ |
9046 | 22.6k | l_has_qcd = 1; |
9047 | 22.6k | } |
9048 | | |
9049 | | /* Check if the marker is known and if it is the right place to find it */ |
9050 | 107k | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
9051 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
9052 | 1 | "Marker is not compliant with its position\n"); |
9053 | 1 | return OPJ_FALSE; |
9054 | 1 | } |
9055 | | |
9056 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9057 | 107k | if (opj_stream_read_data(p_stream, |
9058 | 107k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9059 | 26 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9060 | 26 | return OPJ_FALSE; |
9061 | 26 | } |
9062 | | |
9063 | | /* read 2 bytes as the marker size */ |
9064 | 107k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
9065 | 107k | 2); |
9066 | 107k | if (l_marker_size < 2) { |
9067 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n"); |
9068 | 12 | return OPJ_FALSE; |
9069 | 12 | } |
9070 | 107k | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
9071 | | |
9072 | | /* Check if the marker size is compatible with the header data size */ |
9073 | 107k | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
9074 | 50 | OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc( |
9075 | 50 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
9076 | 50 | if (! new_header_data) { |
9077 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9078 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
9079 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9080 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
9081 | 0 | return OPJ_FALSE; |
9082 | 0 | } |
9083 | 50 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
9084 | 50 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
9085 | 50 | } |
9086 | | |
9087 | | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
9088 | 107k | if (opj_stream_read_data(p_stream, |
9089 | 107k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
9090 | 107k | p_manager) != l_marker_size) { |
9091 | 114 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9092 | 114 | return OPJ_FALSE; |
9093 | 114 | } |
9094 | | |
9095 | | /* Read the marker segment with the correct marker handler */ |
9096 | 107k | if (!(*(l_marker_handler->handler))(p_j2k, |
9097 | 107k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
9098 | 132 | opj_event_msg(p_manager, EVT_ERROR, |
9099 | 132 | "Marker handler function failed to read the marker segment\n"); |
9100 | 132 | return OPJ_FALSE; |
9101 | 132 | } |
9102 | | |
9103 | | /* Add the marker to the codestream index*/ |
9104 | 107k | if (OPJ_FALSE == opj_j2k_add_mhmarker( |
9105 | 107k | p_j2k->cstr_index, |
9106 | 107k | l_marker_handler->id, |
9107 | 107k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
9108 | 107k | l_marker_size + 4)) { |
9109 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
9110 | 0 | return OPJ_FALSE; |
9111 | 0 | } |
9112 | | |
9113 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
9114 | 107k | if (opj_stream_read_data(p_stream, |
9115 | 107k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9116 | 40 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9117 | 40 | return OPJ_FALSE; |
9118 | 40 | } |
9119 | | |
9120 | | /* read 2 bytes as the new marker ID */ |
9121 | 107k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9122 | 107k | &l_current_marker, 2); |
9123 | 107k | } |
9124 | | |
9125 | 7.27k | if (l_has_siz == 0) { |
9126 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
9127 | 3 | "required SIZ marker not found in main header\n"); |
9128 | 3 | return OPJ_FALSE; |
9129 | 3 | } |
9130 | 7.27k | if (l_has_cod == 0) { |
9131 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
9132 | 4 | "required COD marker not found in main header\n"); |
9133 | 4 | return OPJ_FALSE; |
9134 | 4 | } |
9135 | 7.27k | if (l_has_qcd == 0) { |
9136 | 5 | opj_event_msg(p_manager, EVT_ERROR, |
9137 | 5 | "required QCD marker not found in main header\n"); |
9138 | 5 | return OPJ_FALSE; |
9139 | 5 | } |
9140 | | |
9141 | 7.26k | if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) { |
9142 | 21 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n"); |
9143 | 21 | return OPJ_FALSE; |
9144 | 21 | } |
9145 | | |
9146 | 7.24k | opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); |
9147 | | |
9148 | | /* Position of the last element if the main header */ |
9149 | 7.24k | p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; |
9150 | | |
9151 | | /* Build tile-part index from TLM information */ |
9152 | 7.24k | opj_j2k_build_tp_index_from_tlm(p_j2k, p_manager); |
9153 | | |
9154 | | /* Next step: read a tile-part header */ |
9155 | 7.24k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
9156 | | |
9157 | 7.24k | return OPJ_TRUE; |
9158 | 7.26k | } |
9159 | | |
9160 | | static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, |
9161 | | opj_procedure_list_t * p_procedure_list, |
9162 | | opj_stream_private_t *p_stream, |
9163 | | opj_event_mgr_t * p_manager) |
9164 | 23.2k | { |
9165 | 23.2k | OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *, |
9166 | 23.2k | opj_event_mgr_t *) = 00; |
9167 | 23.2k | OPJ_BOOL l_result = OPJ_TRUE; |
9168 | 23.2k | OPJ_UINT32 l_nb_proc, i; |
9169 | | |
9170 | | /* preconditions*/ |
9171 | 23.2k | assert(p_procedure_list != 00); |
9172 | 23.2k | assert(p_j2k != 00); |
9173 | 23.2k | assert(p_stream != 00); |
9174 | 23.2k | assert(p_manager != 00); |
9175 | | |
9176 | 23.2k | l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); |
9177 | 23.2k | l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *, |
9178 | 23.2k | opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); |
9179 | | |
9180 | 62.6k | for (i = 0; i < l_nb_proc; ++i) { |
9181 | 39.3k | l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager)); |
9182 | 39.3k | ++l_procedure; |
9183 | 39.3k | } |
9184 | | |
9185 | | /* and clear the procedure list at the end.*/ |
9186 | 23.2k | opj_procedure_list_clear(p_procedure_list); |
9187 | 23.2k | return l_result; |
9188 | 23.2k | } |
9189 | | |
9190 | | /* FIXME DOC*/ |
9191 | | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, |
9192 | | opj_stream_private_t *p_stream, |
9193 | | opj_event_mgr_t * p_manager |
9194 | | ) |
9195 | 7.24k | { |
9196 | 7.24k | opj_tcp_t * l_tcp = 00; |
9197 | 7.24k | opj_tcp_t * l_default_tcp = 00; |
9198 | 7.24k | OPJ_UINT32 l_nb_tiles; |
9199 | 7.24k | OPJ_UINT32 i, j; |
9200 | 7.24k | opj_tccp_t *l_current_tccp = 00; |
9201 | 7.24k | OPJ_UINT32 l_tccp_size; |
9202 | 7.24k | OPJ_UINT32 l_mct_size; |
9203 | 7.24k | opj_image_t * l_image; |
9204 | 7.24k | OPJ_UINT32 l_mcc_records_size, l_mct_records_size; |
9205 | 7.24k | opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; |
9206 | 7.24k | opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; |
9207 | 7.24k | OPJ_UINT32 l_offset; |
9208 | | |
9209 | | /* preconditions */ |
9210 | 7.24k | assert(p_j2k != 00); |
9211 | 7.24k | assert(p_stream != 00); |
9212 | 7.24k | assert(p_manager != 00); |
9213 | | |
9214 | 7.24k | OPJ_UNUSED(p_stream); |
9215 | | |
9216 | 7.24k | l_image = p_j2k->m_private_image; |
9217 | 7.24k | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
9218 | 7.24k | l_tcp = p_j2k->m_cp.tcps; |
9219 | 7.24k | l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t); |
9220 | 7.24k | l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
9221 | 7.24k | l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof( |
9222 | 7.24k | OPJ_FLOAT32); |
9223 | | |
9224 | | /* For each tile */ |
9225 | 251k | for (i = 0; i < l_nb_tiles; ++i) { |
9226 | | /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/ |
9227 | 244k | l_current_tccp = l_tcp->tccps; |
9228 | | /*Copy default coding parameters into the current tile coding parameters*/ |
9229 | 244k | memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t)); |
9230 | | /* Initialize some values of the current tile coding parameters*/ |
9231 | 244k | l_tcp->cod = 0; |
9232 | 244k | l_tcp->ppt = 0; |
9233 | 244k | l_tcp->ppt_data = 00; |
9234 | 244k | l_tcp->m_current_tile_part_number = -1; |
9235 | | /* Remove memory not owned by this tile in case of early error return. */ |
9236 | 244k | l_tcp->m_mct_decoding_matrix = 00; |
9237 | 244k | l_tcp->m_nb_max_mct_records = 0; |
9238 | 244k | l_tcp->m_mct_records = 00; |
9239 | 244k | l_tcp->m_nb_max_mcc_records = 0; |
9240 | 244k | l_tcp->m_mcc_records = 00; |
9241 | | /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ |
9242 | 244k | l_tcp->tccps = l_current_tccp; |
9243 | | |
9244 | | /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ |
9245 | 244k | if (l_default_tcp->m_mct_decoding_matrix) { |
9246 | 354 | l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
9247 | 354 | if (! l_tcp->m_mct_decoding_matrix) { |
9248 | 0 | return OPJ_FALSE; |
9249 | 0 | } |
9250 | 354 | memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix, |
9251 | 354 | l_mct_size); |
9252 | 354 | } |
9253 | | |
9254 | | /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ |
9255 | 244k | l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof( |
9256 | 244k | opj_mct_data_t); |
9257 | 244k | l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); |
9258 | 244k | if (! l_tcp->m_mct_records) { |
9259 | 0 | return OPJ_FALSE; |
9260 | 0 | } |
9261 | 244k | memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size); |
9262 | | |
9263 | | /* Copy the mct record data from dflt_tile_cp to the current tile*/ |
9264 | 244k | l_src_mct_rec = l_default_tcp->m_mct_records; |
9265 | 244k | l_dest_mct_rec = l_tcp->m_mct_records; |
9266 | | |
9267 | 249k | for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) { |
9268 | | |
9269 | 5.08k | if (l_src_mct_rec->m_data) { |
9270 | | |
9271 | 874 | l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); |
9272 | 874 | if (! l_dest_mct_rec->m_data) { |
9273 | 0 | return OPJ_FALSE; |
9274 | 0 | } |
9275 | 874 | memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data, |
9276 | 874 | l_src_mct_rec->m_data_size); |
9277 | 874 | } |
9278 | | |
9279 | 5.08k | ++l_src_mct_rec; |
9280 | 5.08k | ++l_dest_mct_rec; |
9281 | | /* Update with each pass to free exactly what has been allocated on early return. */ |
9282 | 5.08k | l_tcp->m_nb_max_mct_records += 1; |
9283 | 5.08k | } |
9284 | | |
9285 | | /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ |
9286 | 244k | l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof( |
9287 | 244k | opj_simple_mcc_decorrelation_data_t); |
9288 | 244k | l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc( |
9289 | 244k | l_mcc_records_size); |
9290 | 244k | if (! l_tcp->m_mcc_records) { |
9291 | 0 | return OPJ_FALSE; |
9292 | 0 | } |
9293 | 244k | memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size); |
9294 | 244k | l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records; |
9295 | | |
9296 | | /* Copy the mcc record data from dflt_tile_cp to the current tile*/ |
9297 | 244k | l_src_mcc_rec = l_default_tcp->m_mcc_records; |
9298 | 244k | l_dest_mcc_rec = l_tcp->m_mcc_records; |
9299 | | |
9300 | 2.68M | for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) { |
9301 | | |
9302 | 2.44M | if (l_src_mcc_rec->m_decorrelation_array) { |
9303 | 844 | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - |
9304 | 844 | l_default_tcp->m_mct_records); |
9305 | 844 | l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; |
9306 | 844 | } |
9307 | | |
9308 | 2.44M | if (l_src_mcc_rec->m_offset_array) { |
9309 | 566 | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - |
9310 | 566 | l_default_tcp->m_mct_records); |
9311 | 566 | l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; |
9312 | 566 | } |
9313 | | |
9314 | 2.44M | ++l_src_mcc_rec; |
9315 | 2.44M | ++l_dest_mcc_rec; |
9316 | 2.44M | } |
9317 | | |
9318 | | /* Copy all the dflt_tile_compo_cp to the current tile cp */ |
9319 | 244k | memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size); |
9320 | | |
9321 | | /* Move to next tile cp*/ |
9322 | 244k | ++l_tcp; |
9323 | 244k | } |
9324 | | |
9325 | | /* Create the current tile decoder*/ |
9326 | 7.24k | p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE); |
9327 | 7.24k | if (! p_j2k->m_tcd) { |
9328 | 0 | return OPJ_FALSE; |
9329 | 0 | } |
9330 | | |
9331 | 7.24k | if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) { |
9332 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
9333 | 0 | p_j2k->m_tcd = 00; |
9334 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
9335 | 0 | return OPJ_FALSE; |
9336 | 0 | } |
9337 | | |
9338 | 7.24k | return OPJ_TRUE; |
9339 | 7.24k | } |
9340 | | |
9341 | | static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler( |
9342 | | OPJ_UINT32 p_id) |
9343 | 424k | { |
9344 | 424k | const opj_dec_memory_marker_handler_t *e; |
9345 | 7.99M | for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { |
9346 | 7.74M | if (e->id == p_id) { |
9347 | 170k | break; /* we find a handler corresponding to the marker ID*/ |
9348 | 170k | } |
9349 | 7.74M | } |
9350 | 424k | return e; |
9351 | 424k | } |
9352 | | |
9353 | | void opj_j2k_destroy(opj_j2k_t *p_j2k) |
9354 | 8.24k | { |
9355 | 8.24k | if (p_j2k == 00) { |
9356 | 0 | return; |
9357 | 0 | } |
9358 | | |
9359 | 8.24k | if (p_j2k->m_is_decoder) { |
9360 | | |
9361 | 8.24k | if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { |
9362 | 8.24k | opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
9363 | 8.24k | opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
9364 | 8.24k | p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; |
9365 | 8.24k | } |
9366 | | |
9367 | 8.24k | if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { |
9368 | 8.24k | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9369 | 8.24k | p_j2k->m_specific_param.m_decoder.m_header_data = 00; |
9370 | 8.24k | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9371 | 8.24k | } |
9372 | | |
9373 | 8.24k | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
9374 | 8.24k | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00; |
9375 | 8.24k | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
9376 | | |
9377 | 8.24k | opj_free(p_j2k->m_specific_param.m_decoder.m_tlm.m_tile_part_infos); |
9378 | 8.24k | p_j2k->m_specific_param.m_decoder.m_tlm.m_tile_part_infos = NULL; |
9379 | | |
9380 | 8.24k | opj_free(p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset); |
9381 | 8.24k | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset = NULL; |
9382 | | |
9383 | 8.24k | } else { |
9384 | |
|
9385 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
9386 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
9387 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; |
9388 | 0 | } |
9389 | |
|
9390 | 0 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
9391 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
9392 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; |
9393 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; |
9394 | 0 | } |
9395 | |
|
9396 | 0 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
9397 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
9398 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; |
9399 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
9400 | 0 | } |
9401 | 0 | } |
9402 | | |
9403 | 8.24k | opj_tcd_destroy(p_j2k->m_tcd); |
9404 | | |
9405 | 8.24k | opj_j2k_cp_destroy(&(p_j2k->m_cp)); |
9406 | 8.24k | memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t)); |
9407 | | |
9408 | 8.24k | opj_procedure_list_destroy(p_j2k->m_procedure_list); |
9409 | 8.24k | p_j2k->m_procedure_list = 00; |
9410 | | |
9411 | 8.24k | opj_procedure_list_destroy(p_j2k->m_validation_list); |
9412 | 8.24k | p_j2k->m_procedure_list = 00; |
9413 | | |
9414 | 8.24k | j2k_destroy_cstr_index(p_j2k->cstr_index); |
9415 | 8.24k | p_j2k->cstr_index = NULL; |
9416 | | |
9417 | 8.24k | opj_image_destroy(p_j2k->m_private_image); |
9418 | 8.24k | p_j2k->m_private_image = NULL; |
9419 | | |
9420 | 8.24k | opj_image_destroy(p_j2k->m_output_image); |
9421 | 8.24k | p_j2k->m_output_image = NULL; |
9422 | | |
9423 | 8.24k | opj_thread_pool_destroy(p_j2k->m_tp); |
9424 | 8.24k | p_j2k->m_tp = NULL; |
9425 | | |
9426 | 8.24k | opj_free(p_j2k); |
9427 | 8.24k | } |
9428 | | |
9429 | | void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind) |
9430 | 8.24k | { |
9431 | 8.24k | if (p_cstr_ind) { |
9432 | | |
9433 | 8.24k | if (p_cstr_ind->marker) { |
9434 | 8.24k | opj_free(p_cstr_ind->marker); |
9435 | 8.24k | p_cstr_ind->marker = NULL; |
9436 | 8.24k | } |
9437 | | |
9438 | 8.24k | if (p_cstr_ind->tile_index) { |
9439 | 8.02k | OPJ_UINT32 it_tile = 0; |
9440 | | |
9441 | 359k | for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { |
9442 | | |
9443 | 351k | if (p_cstr_ind->tile_index[it_tile].packet_index) { |
9444 | 0 | opj_free(p_cstr_ind->tile_index[it_tile].packet_index); |
9445 | 0 | p_cstr_ind->tile_index[it_tile].packet_index = NULL; |
9446 | 0 | } |
9447 | | |
9448 | 351k | if (p_cstr_ind->tile_index[it_tile].tp_index) { |
9449 | 11.4k | opj_free(p_cstr_ind->tile_index[it_tile].tp_index); |
9450 | 11.4k | p_cstr_ind->tile_index[it_tile].tp_index = NULL; |
9451 | 11.4k | } |
9452 | | |
9453 | 351k | if (p_cstr_ind->tile_index[it_tile].marker) { |
9454 | 351k | opj_free(p_cstr_ind->tile_index[it_tile].marker); |
9455 | 351k | p_cstr_ind->tile_index[it_tile].marker = NULL; |
9456 | | |
9457 | 351k | } |
9458 | 351k | } |
9459 | | |
9460 | 8.02k | opj_free(p_cstr_ind->tile_index); |
9461 | 8.02k | p_cstr_ind->tile_index = NULL; |
9462 | 8.02k | } |
9463 | | |
9464 | 8.24k | opj_free(p_cstr_ind); |
9465 | 8.24k | } |
9466 | 8.24k | } |
9467 | | |
9468 | | static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp) |
9469 | 360k | { |
9470 | 360k | if (p_tcp == 00) { |
9471 | 0 | return; |
9472 | 0 | } |
9473 | | |
9474 | 360k | if (p_tcp->ppt_markers != 00) { |
9475 | 23 | OPJ_UINT32 i; |
9476 | 1.67k | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
9477 | 1.65k | if (p_tcp->ppt_markers[i].m_data != NULL) { |
9478 | 41 | opj_free(p_tcp->ppt_markers[i].m_data); |
9479 | 41 | } |
9480 | 1.65k | } |
9481 | 23 | p_tcp->ppt_markers_count = 0U; |
9482 | 23 | opj_free(p_tcp->ppt_markers); |
9483 | 23 | p_tcp->ppt_markers = NULL; |
9484 | 23 | } |
9485 | | |
9486 | 360k | if (p_tcp->ppt_buffer != 00) { |
9487 | 19 | opj_free(p_tcp->ppt_buffer); |
9488 | 19 | p_tcp->ppt_buffer = 00; |
9489 | 19 | } |
9490 | | |
9491 | 360k | if (p_tcp->tccps != 00) { |
9492 | 359k | opj_free(p_tcp->tccps); |
9493 | 359k | p_tcp->tccps = 00; |
9494 | 359k | } |
9495 | | |
9496 | 360k | if (p_tcp->m_mct_coding_matrix != 00) { |
9497 | 0 | opj_free(p_tcp->m_mct_coding_matrix); |
9498 | 0 | p_tcp->m_mct_coding_matrix = 00; |
9499 | 0 | } |
9500 | | |
9501 | 360k | if (p_tcp->m_mct_decoding_matrix != 00) { |
9502 | 462 | opj_free(p_tcp->m_mct_decoding_matrix); |
9503 | 462 | p_tcp->m_mct_decoding_matrix = 00; |
9504 | 462 | } |
9505 | | |
9506 | 360k | if (p_tcp->m_mcc_records) { |
9507 | 252k | opj_free(p_tcp->m_mcc_records); |
9508 | 252k | p_tcp->m_mcc_records = 00; |
9509 | 252k | p_tcp->m_nb_max_mcc_records = 0; |
9510 | 252k | p_tcp->m_nb_mcc_records = 0; |
9511 | 252k | } |
9512 | | |
9513 | 360k | if (p_tcp->m_mct_records) { |
9514 | 252k | opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; |
9515 | 252k | OPJ_UINT32 i; |
9516 | | |
9517 | 258k | for (i = 0; i < p_tcp->m_nb_mct_records; ++i) { |
9518 | 5.98k | if (l_mct_data->m_data) { |
9519 | 1.37k | opj_free(l_mct_data->m_data); |
9520 | 1.37k | l_mct_data->m_data = 00; |
9521 | 1.37k | } |
9522 | | |
9523 | 5.98k | ++l_mct_data; |
9524 | 5.98k | } |
9525 | | |
9526 | 252k | opj_free(p_tcp->m_mct_records); |
9527 | 252k | p_tcp->m_mct_records = 00; |
9528 | 252k | } |
9529 | | |
9530 | 360k | if (p_tcp->mct_norms != 00) { |
9531 | 0 | opj_free(p_tcp->mct_norms); |
9532 | 0 | p_tcp->mct_norms = 00; |
9533 | 0 | } |
9534 | | |
9535 | 360k | opj_j2k_tcp_data_destroy(p_tcp); |
9536 | | |
9537 | 360k | } |
9538 | | |
9539 | | static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp) |
9540 | 368k | { |
9541 | 368k | if (p_tcp->m_data) { |
9542 | 9.42k | opj_free(p_tcp->m_data); |
9543 | 9.42k | p_tcp->m_data = NULL; |
9544 | 9.42k | p_tcp->m_data_size = 0; |
9545 | 9.42k | } |
9546 | 368k | } |
9547 | | |
9548 | | static void opj_j2k_cp_destroy(opj_cp_t *p_cp) |
9549 | 8.24k | { |
9550 | 8.24k | OPJ_UINT32 l_nb_tiles; |
9551 | 8.24k | opj_tcp_t * l_current_tile = 00; |
9552 | | |
9553 | 8.24k | if (p_cp == 00) { |
9554 | 0 | return; |
9555 | 0 | } |
9556 | 8.24k | if (p_cp->tcps != 00) { |
9557 | 8.02k | OPJ_UINT32 i; |
9558 | 8.02k | l_current_tile = p_cp->tcps; |
9559 | 8.02k | l_nb_tiles = p_cp->th * p_cp->tw; |
9560 | | |
9561 | 359k | for (i = 0U; i < l_nb_tiles; ++i) { |
9562 | 351k | opj_j2k_tcp_destroy(l_current_tile); |
9563 | 351k | ++l_current_tile; |
9564 | 351k | } |
9565 | 8.02k | opj_free(p_cp->tcps); |
9566 | 8.02k | p_cp->tcps = 00; |
9567 | 8.02k | } |
9568 | 8.24k | if (p_cp->ppm_markers != 00) { |
9569 | 42 | OPJ_UINT32 i; |
9570 | 4.87k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
9571 | 4.83k | if (p_cp->ppm_markers[i].m_data != NULL) { |
9572 | 82 | opj_free(p_cp->ppm_markers[i].m_data); |
9573 | 82 | } |
9574 | 4.83k | } |
9575 | 42 | p_cp->ppm_markers_count = 0U; |
9576 | 42 | opj_free(p_cp->ppm_markers); |
9577 | 42 | p_cp->ppm_markers = NULL; |
9578 | 42 | } |
9579 | 8.24k | opj_free(p_cp->ppm_buffer); |
9580 | 8.24k | p_cp->ppm_buffer = 00; |
9581 | 8.24k | p_cp->ppm_data = |
9582 | 8.24k | NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */ |
9583 | 8.24k | opj_free(p_cp->comment); |
9584 | 8.24k | p_cp->comment = 00; |
9585 | 8.24k | if (! p_cp->m_is_decoder) { |
9586 | 0 | opj_free(p_cp->m_specific_param.m_enc.m_matrice); |
9587 | 0 | p_cp->m_specific_param.m_enc.m_matrice = 00; |
9588 | 0 | } |
9589 | 8.24k | } |
9590 | | |
9591 | | static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t |
9592 | | *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, |
9593 | | opj_event_mgr_t * p_manager) |
9594 | 2 | { |
9595 | 2 | OPJ_BYTE l_header_data[10]; |
9596 | 2 | OPJ_OFF_T l_stream_pos_backup; |
9597 | 2 | OPJ_UINT32 l_current_marker; |
9598 | 2 | OPJ_UINT32 l_marker_size; |
9599 | 2 | OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts; |
9600 | | |
9601 | | /* initialize to no correction needed */ |
9602 | 2 | *p_correction_needed = OPJ_FALSE; |
9603 | | |
9604 | 2 | if (!opj_stream_has_seek(p_stream)) { |
9605 | | /* We can't do much in this case, seek is needed */ |
9606 | 0 | return OPJ_TRUE; |
9607 | 0 | } |
9608 | | |
9609 | 2 | l_stream_pos_backup = opj_stream_tell(p_stream); |
9610 | 2 | if (l_stream_pos_backup == -1) { |
9611 | | /* let's do nothing */ |
9612 | 0 | return OPJ_TRUE; |
9613 | 0 | } |
9614 | | |
9615 | 34 | for (;;) { |
9616 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
9617 | 34 | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
9618 | | /* assume all is OK */ |
9619 | 0 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9620 | 0 | return OPJ_FALSE; |
9621 | 0 | } |
9622 | 0 | return OPJ_TRUE; |
9623 | 0 | } |
9624 | | |
9625 | | /* Read 2 bytes from buffer as the new marker ID */ |
9626 | 34 | opj_read_bytes(l_header_data, &l_current_marker, 2); |
9627 | | |
9628 | 34 | if (l_current_marker != J2K_MS_SOT) { |
9629 | | /* assume all is OK */ |
9630 | 1 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9631 | 0 | return OPJ_FALSE; |
9632 | 0 | } |
9633 | 1 | return OPJ_TRUE; |
9634 | 1 | } |
9635 | | |
9636 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9637 | 33 | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
9638 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9639 | 0 | return OPJ_FALSE; |
9640 | 0 | } |
9641 | | |
9642 | | /* Read 2 bytes from the buffer as the marker size */ |
9643 | 33 | opj_read_bytes(l_header_data, &l_marker_size, 2); |
9644 | | |
9645 | | /* Check marker size for SOT Marker */ |
9646 | 33 | if (l_marker_size != 10) { |
9647 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); |
9648 | 0 | return OPJ_FALSE; |
9649 | 0 | } |
9650 | 33 | l_marker_size -= 2; |
9651 | | |
9652 | 33 | if (opj_stream_read_data(p_stream, l_header_data, l_marker_size, |
9653 | 33 | p_manager) != l_marker_size) { |
9654 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9655 | 0 | return OPJ_FALSE; |
9656 | 0 | } |
9657 | | |
9658 | 33 | if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no, |
9659 | 33 | &l_tot_len, &l_current_part, &l_num_parts, p_manager)) { |
9660 | 0 | return OPJ_FALSE; |
9661 | 0 | } |
9662 | | |
9663 | 33 | if (l_tile_no == tile_no) { |
9664 | | /* we found what we were looking for */ |
9665 | 1 | break; |
9666 | 1 | } |
9667 | | |
9668 | 32 | if (l_tot_len < 14U) { |
9669 | | /* last SOT until EOC or invalid Psot value */ |
9670 | | /* assume all is OK */ |
9671 | 0 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9672 | 0 | return OPJ_FALSE; |
9673 | 0 | } |
9674 | 0 | return OPJ_TRUE; |
9675 | 0 | } |
9676 | 32 | l_tot_len -= 12U; |
9677 | | /* look for next SOT marker */ |
9678 | 32 | if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len), |
9679 | 32 | p_manager) != (OPJ_OFF_T)(l_tot_len)) { |
9680 | | /* assume all is OK */ |
9681 | 0 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9682 | 0 | return OPJ_FALSE; |
9683 | 0 | } |
9684 | 0 | return OPJ_TRUE; |
9685 | 0 | } |
9686 | 32 | } |
9687 | | |
9688 | | /* check for correction */ |
9689 | 1 | if (l_current_part == l_num_parts) { |
9690 | 1 | *p_correction_needed = OPJ_TRUE; |
9691 | 1 | } |
9692 | | |
9693 | 1 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9694 | 0 | return OPJ_FALSE; |
9695 | 0 | } |
9696 | 1 | return OPJ_TRUE; |
9697 | 1 | } |
9698 | | |
9699 | | OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, |
9700 | | OPJ_UINT32 * p_tile_index, |
9701 | | OPJ_UINT32 * p_data_size, |
9702 | | OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, |
9703 | | OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, |
9704 | | OPJ_UINT32 * p_nb_comps, |
9705 | | OPJ_BOOL * p_go_on, |
9706 | | opj_stream_private_t *p_stream, |
9707 | | opj_event_mgr_t * p_manager) |
9708 | 9.51k | { |
9709 | 9.51k | OPJ_UINT32 l_current_marker = J2K_MS_SOT; |
9710 | 9.51k | OPJ_UINT32 l_marker_size; |
9711 | 9.51k | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
9712 | 9.51k | opj_tcp_t * l_tcp = NULL; |
9713 | 9.51k | const OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
9714 | | |
9715 | | /* preconditions */ |
9716 | 9.51k | assert(p_stream != 00); |
9717 | 9.51k | assert(p_j2k != 00); |
9718 | 9.51k | assert(p_manager != 00); |
9719 | | |
9720 | | /* Reach the End Of Codestream ?*/ |
9721 | 9.51k | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
9722 | 14 | l_current_marker = J2K_MS_EOC; |
9723 | 14 | } |
9724 | | /* We need to encounter a SOT marker (a new tile-part header) */ |
9725 | 9.50k | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
9726 | 0 | return OPJ_FALSE; |
9727 | 0 | } |
9728 | | |
9729 | | /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ |
9730 | 19.2k | while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) && |
9731 | 19.2k | (l_current_marker != J2K_MS_EOC)) { |
9732 | | |
9733 | 16.5k | if (p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts > 0 && |
9734 | 16.5k | p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts < |
9735 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts) { |
9736 | 0 | OPJ_OFF_T next_tp_sot_pos; |
9737 | |
|
9738 | 0 | next_tp_sot_pos = |
9739 | 0 | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset[p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts]; |
9740 | 0 | ++p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts; |
9741 | 0 | if (!(opj_stream_read_seek(p_stream, |
9742 | 0 | next_tp_sot_pos, |
9743 | 0 | p_manager))) { |
9744 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
9745 | 0 | return OPJ_FALSE; |
9746 | 0 | } |
9747 | | |
9748 | | /* Try to read 2 bytes (the marker ID) from stream and copy them into the buffer */ |
9749 | 0 | if (opj_stream_read_data(p_stream, |
9750 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9751 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9752 | 0 | return OPJ_FALSE; |
9753 | 0 | } |
9754 | | |
9755 | | /* Read 2 bytes from the buffer as the marker ID */ |
9756 | 0 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9757 | 0 | &l_current_marker, |
9758 | 0 | 2); |
9759 | |
|
9760 | 0 | if (l_current_marker != J2K_MS_SOT) { |
9761 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Did not get expected SOT marker\n"); |
9762 | 0 | return OPJ_FALSE; |
9763 | 0 | } |
9764 | 0 | } |
9765 | | |
9766 | | /* Try to read until the Start Of Data is detected */ |
9767 | 26.7k | while (l_current_marker != J2K_MS_SOD) { |
9768 | | |
9769 | 16.9k | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
9770 | 6.58k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
9771 | 6.58k | break; |
9772 | 6.58k | } |
9773 | | |
9774 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9775 | 10.3k | if (opj_stream_read_data(p_stream, |
9776 | 10.3k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9777 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9778 | 3 | return OPJ_FALSE; |
9779 | 3 | } |
9780 | | |
9781 | | /* Read 2 bytes from the buffer as the marker size */ |
9782 | 10.3k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
9783 | 10.3k | 2); |
9784 | | |
9785 | | /* Check marker size (does not include marker ID but includes marker size) */ |
9786 | 10.3k | if (l_marker_size < 2) { |
9787 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); |
9788 | 3 | return OPJ_FALSE; |
9789 | 3 | } |
9790 | | |
9791 | | /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */ |
9792 | 10.3k | if (l_current_marker == 0x8080 && |
9793 | 10.3k | opj_stream_get_number_byte_left(p_stream) == 0) { |
9794 | 0 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
9795 | 0 | break; |
9796 | 0 | } |
9797 | | |
9798 | | /* Why this condition? FIXME */ |
9799 | 10.3k | if ((p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) && |
9800 | 10.3k | p_j2k->m_specific_param.m_decoder.m_sot_length != 0) { |
9801 | 129 | if (p_j2k->m_specific_param.m_decoder.m_sot_length < l_marker_size + 2) { |
9802 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
9803 | 4 | "Sot length is less than marker size + marker ID\n"); |
9804 | 4 | return OPJ_FALSE; |
9805 | 4 | } |
9806 | 125 | p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); |
9807 | 125 | } |
9808 | 10.3k | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
9809 | | |
9810 | | /* Get the marker handler from the marker ID */ |
9811 | 10.3k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
9812 | | |
9813 | | /* Check if the marker is known and if it is the right place to find it */ |
9814 | 10.3k | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
9815 | 19 | opj_event_msg(p_manager, EVT_ERROR, |
9816 | 19 | "Marker is not compliant with its position\n"); |
9817 | 19 | return OPJ_FALSE; |
9818 | 19 | } |
9819 | | /* FIXME manage case of unknown marker as in the main header ? */ |
9820 | | |
9821 | | /* Check if the marker size is compatible with the header data size */ |
9822 | 10.3k | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
9823 | 54 | OPJ_BYTE *new_header_data = NULL; |
9824 | | /* If we are here, this means we consider this marker as known & we will read it */ |
9825 | | /* Check enough bytes left in stream before allocation */ |
9826 | 54 | if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) { |
9827 | 43 | opj_event_msg(p_manager, EVT_ERROR, |
9828 | 43 | "Marker size inconsistent with stream length\n"); |
9829 | 43 | return OPJ_FALSE; |
9830 | 43 | } |
9831 | 11 | new_header_data = (OPJ_BYTE *) opj_realloc( |
9832 | 11 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
9833 | 11 | if (! new_header_data) { |
9834 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9835 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
9836 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9837 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
9838 | 0 | return OPJ_FALSE; |
9839 | 0 | } |
9840 | 11 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
9841 | 11 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
9842 | 11 | } |
9843 | | |
9844 | | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
9845 | 10.2k | if (opj_stream_read_data(p_stream, |
9846 | 10.2k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
9847 | 10.2k | p_manager) != l_marker_size) { |
9848 | 21 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9849 | 21 | return OPJ_FALSE; |
9850 | 21 | } |
9851 | | |
9852 | 10.2k | if (!l_marker_handler->handler) { |
9853 | | /* See issue #175 */ |
9854 | 17 | opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n"); |
9855 | 17 | return OPJ_FALSE; |
9856 | 17 | } |
9857 | | /* Read the marker segment with the correct marker handler */ |
9858 | 10.2k | if (!(*(l_marker_handler->handler))(p_j2k, |
9859 | 10.2k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
9860 | 52 | opj_event_msg(p_manager, EVT_ERROR, |
9861 | 52 | "Fail to read the current marker segment (%#x)\n", l_current_marker); |
9862 | 52 | return OPJ_FALSE; |
9863 | 52 | } |
9864 | | |
9865 | | /* Add the marker to the codestream index*/ |
9866 | 10.1k | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
9867 | 10.1k | p_j2k->cstr_index, |
9868 | 10.1k | l_marker_handler->id, |
9869 | 10.1k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
9870 | 10.1k | l_marker_size + 4)) { |
9871 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); |
9872 | 0 | return OPJ_FALSE; |
9873 | 0 | } |
9874 | | |
9875 | | /* Keep the position of the last SOT marker read */ |
9876 | 10.1k | if (l_marker_handler->id == J2K_MS_SOT) { |
9877 | 9.85k | OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 |
9878 | 9.85k | ; |
9879 | 9.85k | if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) { |
9880 | 9.85k | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; |
9881 | 9.85k | } |
9882 | 9.85k | } |
9883 | | |
9884 | 10.1k | if (p_j2k->m_specific_param.m_decoder.m_skip_data) { |
9885 | | /* Skip the rest of the tile part header*/ |
9886 | 0 | if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length, |
9887 | 0 | p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
9888 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9889 | 0 | return OPJ_FALSE; |
9890 | 0 | } |
9891 | 0 | l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ |
9892 | 10.1k | } else { |
9893 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
9894 | 10.1k | if (opj_stream_read_data(p_stream, |
9895 | 10.1k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9896 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9897 | 3 | return OPJ_FALSE; |
9898 | 3 | } |
9899 | | /* Read 2 bytes from the buffer as the new marker ID */ |
9900 | 10.1k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9901 | 10.1k | &l_current_marker, 2); |
9902 | 10.1k | } |
9903 | 10.1k | } |
9904 | 16.3k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
9905 | 16.3k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
9906 | 6.58k | break; |
9907 | 6.58k | } |
9908 | | |
9909 | | /* If we didn't skip data before, we need to read the SOD marker*/ |
9910 | 9.80k | if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { |
9911 | | /* Try to read the SOD marker and skip data ? FIXME */ |
9912 | 9.80k | if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { |
9913 | 42 | return OPJ_FALSE; |
9914 | 42 | } |
9915 | | |
9916 | | /* Check if we can use the TLM index to access the next tile-part */ |
9917 | 9.76k | if (!p_j2k->m_specific_param.m_decoder.m_can_decode && |
9918 | 9.76k | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 && |
9919 | 9.76k | p_j2k->m_current_tile_number == (OPJ_UINT32) |
9920 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec && |
9921 | 9.76k | !p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid && |
9922 | 9.76k | opj_stream_has_seek(p_stream)) { |
9923 | 0 | l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; |
9924 | 0 | if (l_tcp->m_nb_tile_parts == |
9925 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps && |
9926 | 0 | (OPJ_UINT32)l_tcp->m_current_tile_part_number + 1 < l_tcp->m_nb_tile_parts) { |
9927 | 0 | const OPJ_OFF_T next_tp_sot_pos = p_j2k->cstr_index->tile_index[ |
9928 | 0 | p_j2k->m_current_tile_number].tp_index[l_tcp->m_current_tile_part_number + |
9929 | 0 | 1].start_pos; |
9930 | |
|
9931 | 0 | if (next_tp_sot_pos != opj_stream_tell(p_stream)) { |
9932 | | #if 0 |
9933 | | opj_event_msg(p_manager, EVT_INFO, |
9934 | | "opj_j2k_read_tile_header(tile=%u): seek to tile part %u at %" PRId64 "\n", |
9935 | | p_j2k->m_current_tile_number, |
9936 | | l_tcp->m_current_tile_part_number + 1, |
9937 | | next_tp_sot_pos); |
9938 | | #endif |
9939 | |
|
9940 | 0 | if (!(opj_stream_read_seek(p_stream, |
9941 | 0 | next_tp_sot_pos, |
9942 | 0 | p_manager))) { |
9943 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
9944 | 0 | return OPJ_FALSE; |
9945 | 0 | } |
9946 | 0 | } |
9947 | | |
9948 | | /* Try to read 2 bytes (the marker ID) from stream and copy them into the buffer */ |
9949 | 0 | if (opj_stream_read_data(p_stream, |
9950 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9951 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9952 | 0 | return OPJ_FALSE; |
9953 | 0 | } |
9954 | | |
9955 | | /* Read 2 bytes from the buffer as the marker ID */ |
9956 | 0 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9957 | 0 | &l_current_marker, |
9958 | 0 | 2); |
9959 | |
|
9960 | 0 | if (l_current_marker != J2K_MS_SOT) { |
9961 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Did not get expected SOT marker\n"); |
9962 | 0 | return OPJ_FALSE; |
9963 | 0 | } |
9964 | | |
9965 | 0 | continue; |
9966 | 0 | } |
9967 | 0 | } |
9968 | | |
9969 | 9.76k | if (p_j2k->m_specific_param.m_decoder.m_can_decode && |
9970 | 9.76k | !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) { |
9971 | | /* Issue 254 */ |
9972 | 381 | OPJ_BOOL l_correction_needed = OPJ_FALSE; |
9973 | | |
9974 | 381 | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
9975 | 381 | if (p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts == 1) { |
9976 | | /* Skip opj_j2k_need_nb_tile_parts_correction() if there is |
9977 | | * only a single tile part declared. The |
9978 | | * opj_j2k_need_nb_tile_parts_correction() hack was needed |
9979 | | * for files with 5 declared tileparts (where they were |
9980 | | * actually 6). |
9981 | | * Doing it systematically hurts performance when reading |
9982 | | * Sentinel2 L1C JPEG2000 files as explained in |
9983 | | * https://lists.osgeo.org/pipermail/gdal-dev/2024-November/059805.html |
9984 | | */ |
9985 | 379 | } else if (!opj_j2k_need_nb_tile_parts_correction(p_stream, |
9986 | 2 | p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { |
9987 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
9988 | 0 | "opj_j2k_apply_nb_tile_parts_correction error\n"); |
9989 | 0 | return OPJ_FALSE; |
9990 | 0 | } |
9991 | 381 | if (l_correction_needed) { |
9992 | 1 | OPJ_UINT32 l_tile_no; |
9993 | | |
9994 | 1 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
9995 | 1 | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1; |
9996 | | /* correct tiles */ |
9997 | 21 | for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { |
9998 | 20 | if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) { |
9999 | 20 | p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1; |
10000 | 20 | } |
10001 | 20 | } |
10002 | 1 | opj_event_msg(p_manager, EVT_WARNING, |
10003 | 1 | "Non conformant codestream TPsot==TNsot.\n"); |
10004 | 1 | } |
10005 | 381 | } |
10006 | 9.76k | } else { |
10007 | | /* Indicate we will try to read a new tile-part header*/ |
10008 | 0 | p_j2k->m_specific_param.m_decoder.m_skip_data = 0; |
10009 | 0 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
10010 | 0 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
10011 | 0 | } |
10012 | | |
10013 | 9.76k | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
10014 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
10015 | 7.06k | if (opj_stream_read_data(p_stream, |
10016 | 7.06k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
10017 | | |
10018 | | /* Deal with likely non conformant SPOT6 files, where the last */ |
10019 | | /* row of tiles have TPsot == 0 and TNsot == 0, and missing EOC, */ |
10020 | | /* but no other tile-parts were found. */ |
10021 | 10 | if (p_j2k->m_current_tile_number + 1 == l_nb_tiles) { |
10022 | 10 | OPJ_UINT32 l_tile_no; |
10023 | 55 | for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { |
10024 | 52 | if (p_j2k->m_cp.tcps[l_tile_no].m_current_tile_part_number == 0 && |
10025 | 52 | p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts == 0) { |
10026 | 7 | break; |
10027 | 7 | } |
10028 | 52 | } |
10029 | 10 | if (l_tile_no < l_nb_tiles) { |
10030 | 7 | opj_event_msg(p_manager, EVT_INFO, |
10031 | 7 | "Tile %u has TPsot == 0 and TNsot == 0, " |
10032 | 7 | "but no other tile-parts were found. " |
10033 | 7 | "EOC is also missing.\n", |
10034 | 7 | l_tile_no); |
10035 | 7 | p_j2k->m_current_tile_number = l_tile_no; |
10036 | 7 | l_current_marker = J2K_MS_EOC; |
10037 | 7 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
10038 | 7 | break; |
10039 | 7 | } |
10040 | 10 | } |
10041 | | |
10042 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
10043 | 3 | return OPJ_FALSE; |
10044 | 10 | } |
10045 | | |
10046 | | /* Read 2 bytes from buffer as the new marker ID */ |
10047 | 7.05k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
10048 | 7.05k | &l_current_marker, 2); |
10049 | 7.05k | } |
10050 | 9.76k | } |
10051 | | |
10052 | | /* Current marker is the EOC marker ?*/ |
10053 | 9.30k | if (l_current_marker == J2K_MS_EOC) { |
10054 | 23 | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { |
10055 | 2 | p_j2k->m_current_tile_number = 0; |
10056 | 2 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
10057 | 2 | } |
10058 | 23 | } |
10059 | | |
10060 | | /* Deal with tiles that have a single tile-part with TPsot == 0 and TNsot == 0 */ |
10061 | 9.30k | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
10062 | 6.60k | l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; |
10063 | | |
10064 | 10.7k | while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) { |
10065 | 4.15k | ++p_j2k->m_current_tile_number; |
10066 | 4.15k | ++l_tcp; |
10067 | 4.15k | } |
10068 | | |
10069 | 6.60k | if (p_j2k->m_current_tile_number == l_nb_tiles) { |
10070 | 14 | *p_go_on = OPJ_FALSE; |
10071 | 14 | return OPJ_TRUE; |
10072 | 14 | } |
10073 | 6.60k | } |
10074 | | |
10075 | 9.29k | if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number, |
10076 | 9.29k | p_manager)) { |
10077 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n"); |
10078 | 0 | return OPJ_FALSE; |
10079 | 0 | } |
10080 | | /*FIXME ???*/ |
10081 | 9.29k | if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
10082 | 9.29k | p_manager)) { |
10083 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
10084 | 1 | return OPJ_FALSE; |
10085 | 1 | } |
10086 | | |
10087 | 9.29k | opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", |
10088 | 9.29k | p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw)); |
10089 | | |
10090 | 9.29k | *p_tile_index = p_j2k->m_current_tile_number; |
10091 | 9.29k | *p_go_on = OPJ_TRUE; |
10092 | 9.29k | if (p_data_size) { |
10093 | | /* For internal use in j2k.c, we don't need this */ |
10094 | | /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */ |
10095 | 0 | *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE); |
10096 | 0 | if (*p_data_size == UINT_MAX) { |
10097 | 0 | return OPJ_FALSE; |
10098 | 0 | } |
10099 | 0 | } |
10100 | 9.29k | *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; |
10101 | 9.29k | *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; |
10102 | 9.29k | *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; |
10103 | 9.29k | *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; |
10104 | 9.29k | *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; |
10105 | | |
10106 | 9.29k | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
10107 | | |
10108 | 9.29k | return OPJ_TRUE; |
10109 | 9.29k | } |
10110 | | |
10111 | | OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, |
10112 | | OPJ_UINT32 p_tile_index, |
10113 | | OPJ_BYTE * p_data, |
10114 | | OPJ_UINT32 p_data_size, |
10115 | | opj_stream_private_t *p_stream, |
10116 | | opj_event_mgr_t * p_manager) |
10117 | 9.29k | { |
10118 | 9.29k | OPJ_UINT32 l_current_marker; |
10119 | 9.29k | OPJ_BYTE l_data [2]; |
10120 | 9.29k | opj_tcp_t * l_tcp; |
10121 | 9.29k | opj_image_t* l_image_for_bounds; |
10122 | | |
10123 | | /* preconditions */ |
10124 | 9.29k | assert(p_stream != 00); |
10125 | 9.29k | assert(p_j2k != 00); |
10126 | 9.29k | assert(p_manager != 00); |
10127 | | |
10128 | 9.29k | if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA) |
10129 | 9.29k | || (p_tile_index != p_j2k->m_current_tile_number)) { |
10130 | 0 | return OPJ_FALSE; |
10131 | 0 | } |
10132 | | |
10133 | 9.29k | l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); |
10134 | 9.29k | if (! l_tcp->m_data) { |
10135 | 1 | opj_j2k_tcp_destroy(l_tcp); |
10136 | 1 | return OPJ_FALSE; |
10137 | 1 | } |
10138 | | |
10139 | | /* When using the opj_read_tile_header / opj_decode_tile_data API */ |
10140 | | /* such as in test_tile_decoder, m_output_image is NULL, so fall back */ |
10141 | | /* to the full image dimension. This is a bit surprising that */ |
10142 | | /* opj_set_decode_area() is only used to determine intersecting tiles, */ |
10143 | | /* but full tile decoding is done */ |
10144 | 9.29k | l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image : |
10145 | 9.29k | p_j2k->m_private_image; |
10146 | 9.29k | if (! opj_tcd_decode_tile(p_j2k->m_tcd, |
10147 | 9.29k | l_image_for_bounds->x0, |
10148 | 9.29k | l_image_for_bounds->y0, |
10149 | 9.29k | l_image_for_bounds->x1, |
10150 | 9.29k | l_image_for_bounds->y1, |
10151 | 9.29k | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode, |
10152 | 9.29k | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
10153 | 9.29k | l_tcp->m_data, |
10154 | 9.29k | l_tcp->m_data_size, |
10155 | 9.29k | p_tile_index, |
10156 | 9.29k | p_j2k->cstr_index, p_manager)) { |
10157 | 707 | opj_j2k_tcp_destroy(l_tcp); |
10158 | 707 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
10159 | 707 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n"); |
10160 | 707 | return OPJ_FALSE; |
10161 | 707 | } |
10162 | | |
10163 | | /* p_data can be set to NULL when the call will take care of using */ |
10164 | | /* itself the TCD data. This is typically the case for whole single */ |
10165 | | /* tile decoding optimization. */ |
10166 | 8.58k | if (p_data != NULL) { |
10167 | 0 | if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
10168 | 0 | return OPJ_FALSE; |
10169 | 0 | } |
10170 | | |
10171 | | /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) |
10172 | | * we destroy just the data which will be re-read in read_tile_header*/ |
10173 | | /*opj_j2k_tcp_destroy(l_tcp); |
10174 | | p_j2k->m_tcd->tcp = 0;*/ |
10175 | 0 | opj_j2k_tcp_data_destroy(l_tcp); |
10176 | 0 | } |
10177 | | |
10178 | 8.58k | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
10179 | 8.58k | p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA); |
10180 | | |
10181 | 8.58k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
10182 | 8.58k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
10183 | 5.91k | return OPJ_TRUE; |
10184 | 5.91k | } |
10185 | | |
10186 | 2.66k | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { |
10187 | 2.65k | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
10188 | 22 | opj_event_msg(p_manager, p_j2k->m_cp.strict ? EVT_ERROR : EVT_WARNING, |
10189 | 22 | "Stream too short\n"); |
10190 | 22 | return p_j2k->m_cp.strict ? OPJ_FALSE : OPJ_TRUE; |
10191 | 22 | } |
10192 | 2.63k | opj_read_bytes(l_data, &l_current_marker, 2); |
10193 | | |
10194 | 2.63k | if (l_current_marker == J2K_MS_EOC) { |
10195 | 17 | p_j2k->m_current_tile_number = 0; |
10196 | 17 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
10197 | 2.61k | } else if (l_current_marker != J2K_MS_SOT) { |
10198 | 250 | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
10199 | 218 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
10200 | 218 | opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n"); |
10201 | 218 | return OPJ_TRUE; |
10202 | 218 | } |
10203 | 32 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); |
10204 | 32 | return OPJ_FALSE; |
10205 | 250 | } |
10206 | 2.63k | } |
10207 | | |
10208 | 2.39k | return OPJ_TRUE; |
10209 | 2.66k | } |
10210 | | |
10211 | | static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, |
10212 | | opj_image_t* p_output_image) |
10213 | 8.50k | { |
10214 | 8.50k | OPJ_UINT32 i, j; |
10215 | 8.50k | OPJ_UINT32 l_width_src, l_height_src; |
10216 | 8.50k | OPJ_UINT32 l_width_dest, l_height_dest; |
10217 | 8.50k | OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src; |
10218 | 8.50k | OPJ_SIZE_T l_start_offset_src; |
10219 | 8.50k | OPJ_UINT32 l_start_x_dest, l_start_y_dest; |
10220 | 8.50k | OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest; |
10221 | 8.50k | OPJ_SIZE_T l_start_offset_dest; |
10222 | | |
10223 | 8.50k | opj_image_comp_t * l_img_comp_src = 00; |
10224 | 8.50k | opj_image_comp_t * l_img_comp_dest = 00; |
10225 | | |
10226 | 8.50k | opj_tcd_tilecomp_t * l_tilec = 00; |
10227 | 8.50k | opj_image_t * l_image_src = 00; |
10228 | 8.50k | OPJ_INT32 * l_dest_ptr; |
10229 | | |
10230 | 8.50k | l_tilec = p_tcd->tcd_image->tiles->comps; |
10231 | 8.50k | l_image_src = p_tcd->image; |
10232 | 8.50k | l_img_comp_src = l_image_src->comps; |
10233 | | |
10234 | 8.50k | l_img_comp_dest = p_output_image->comps; |
10235 | | |
10236 | 17.6k | for (i = 0; i < l_image_src->numcomps; |
10237 | 9.17k | i++, ++l_img_comp_dest, ++l_img_comp_src, ++l_tilec) { |
10238 | 9.17k | OPJ_INT32 res_x0, res_x1, res_y0, res_y1; |
10239 | 9.17k | OPJ_UINT32 src_data_stride; |
10240 | 9.17k | const OPJ_INT32* p_src_data; |
10241 | | |
10242 | | /* Copy info from decoded comp image to output image */ |
10243 | 9.17k | l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; |
10244 | | |
10245 | 9.17k | if (p_tcd->whole_tile_decoding) { |
10246 | 9.17k | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
10247 | 9.17k | l_img_comp_src->resno_decoded; |
10248 | 9.17k | res_x0 = l_res->x0; |
10249 | 9.17k | res_y0 = l_res->y0; |
10250 | 9.17k | res_x1 = l_res->x1; |
10251 | 9.17k | res_y1 = l_res->y1; |
10252 | 9.17k | src_data_stride = (OPJ_UINT32)( |
10253 | 9.17k | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 - |
10254 | 9.17k | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0); |
10255 | 9.17k | p_src_data = l_tilec->data; |
10256 | 9.17k | } else { |
10257 | 0 | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
10258 | 0 | l_img_comp_src->resno_decoded; |
10259 | 0 | res_x0 = (OPJ_INT32)l_res->win_x0; |
10260 | 0 | res_y0 = (OPJ_INT32)l_res->win_y0; |
10261 | 0 | res_x1 = (OPJ_INT32)l_res->win_x1; |
10262 | 0 | res_y1 = (OPJ_INT32)l_res->win_y1; |
10263 | 0 | src_data_stride = l_res->win_x1 - l_res->win_x0; |
10264 | 0 | p_src_data = l_tilec->data_win; |
10265 | 0 | } |
10266 | | |
10267 | 9.17k | if (p_src_data == NULL) { |
10268 | | /* Happens for partial component decoding */ |
10269 | 107 | continue; |
10270 | 107 | } |
10271 | | |
10272 | 9.06k | l_width_src = (OPJ_UINT32)(res_x1 - res_x0); |
10273 | 9.06k | l_height_src = (OPJ_UINT32)(res_y1 - res_y0); |
10274 | | |
10275 | | |
10276 | | /* Current tile component size*/ |
10277 | | /*if (i == 0) { |
10278 | | fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", |
10279 | | res_x0, res_x1, res_y0, res_y1); |
10280 | | }*/ |
10281 | | |
10282 | | |
10283 | | /* Border of the current output component*/ |
10284 | 9.06k | l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor); |
10285 | 9.06k | l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor); |
10286 | 9.06k | l_x1_dest = l_x0_dest + |
10287 | 9.06k | l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */ |
10288 | 9.06k | l_y1_dest = l_y0_dest + l_img_comp_dest->h; |
10289 | | |
10290 | | /*if (i == 0) { |
10291 | | fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", |
10292 | | l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); |
10293 | | }*/ |
10294 | | |
10295 | | /*-----*/ |
10296 | | /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) |
10297 | | * of the input buffer (decoded tile component) which will be move |
10298 | | * in the output buffer. Compute the area of the output buffer (l_start_x_dest, |
10299 | | * l_start_y_dest, l_width_dest, l_height_dest) which will be modified |
10300 | | * by this input area. |
10301 | | * */ |
10302 | 9.06k | assert(res_x0 >= 0); |
10303 | 9.06k | assert(res_x1 >= 0); |
10304 | 9.06k | if (l_x0_dest < (OPJ_UINT32)res_x0) { |
10305 | 2.39k | l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest; |
10306 | 2.39k | l_offset_x0_src = 0; |
10307 | | |
10308 | 2.39k | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
10309 | 2.39k | l_width_dest = l_width_src; |
10310 | 2.39k | l_offset_x1_src = 0; |
10311 | 2.39k | } else { |
10312 | 0 | l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ; |
10313 | 0 | l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest); |
10314 | 0 | } |
10315 | 6.67k | } else { |
10316 | 6.67k | l_start_x_dest = 0U; |
10317 | 6.67k | l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0; |
10318 | | |
10319 | 6.67k | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
10320 | 6.67k | l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src; |
10321 | 6.67k | l_offset_x1_src = 0; |
10322 | 6.67k | } else { |
10323 | 0 | l_width_dest = l_img_comp_dest->w ; |
10324 | 0 | l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest; |
10325 | 0 | } |
10326 | 6.67k | } |
10327 | | |
10328 | 9.06k | if (l_y0_dest < (OPJ_UINT32)res_y0) { |
10329 | 2.29k | l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest; |
10330 | 2.29k | l_offset_y0_src = 0; |
10331 | | |
10332 | 2.29k | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
10333 | 2.29k | l_height_dest = l_height_src; |
10334 | 2.29k | l_offset_y1_src = 0; |
10335 | 2.29k | } else { |
10336 | 0 | l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ; |
10337 | 0 | l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest); |
10338 | 0 | } |
10339 | 6.76k | } else { |
10340 | 6.76k | l_start_y_dest = 0U; |
10341 | 6.76k | l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0; |
10342 | | |
10343 | 6.76k | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
10344 | 6.76k | l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src; |
10345 | 6.76k | l_offset_y1_src = 0; |
10346 | 6.76k | } else { |
10347 | 0 | l_height_dest = l_img_comp_dest->h ; |
10348 | 0 | l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest; |
10349 | 0 | } |
10350 | 6.76k | } |
10351 | | |
10352 | 9.06k | if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) || |
10353 | 9.06k | (l_offset_y1_src < 0)) { |
10354 | 0 | return OPJ_FALSE; |
10355 | 0 | } |
10356 | | /* testcase 2977.pdf.asan.67.2198 */ |
10357 | 9.06k | if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) { |
10358 | 38 | return OPJ_FALSE; |
10359 | 38 | } |
10360 | | /*-----*/ |
10361 | | |
10362 | | /* Compute the input buffer offset */ |
10363 | 9.02k | l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src |
10364 | 9.02k | * (OPJ_SIZE_T)src_data_stride; |
10365 | | |
10366 | | /* Compute the output buffer offset */ |
10367 | 9.02k | l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest |
10368 | 9.02k | * (OPJ_SIZE_T)l_img_comp_dest->w; |
10369 | | |
10370 | | /* Allocate output component buffer if necessary */ |
10371 | 9.02k | if (l_img_comp_dest->data == NULL && |
10372 | 9.02k | l_start_offset_src == 0 && l_start_offset_dest == 0 && |
10373 | 9.02k | src_data_stride == l_img_comp_dest->w && |
10374 | 9.02k | l_width_dest == l_img_comp_dest->w && |
10375 | 9.02k | l_height_dest == l_img_comp_dest->h) { |
10376 | | /* If the final image matches the tile buffer, then borrow it */ |
10377 | | /* directly to save a copy */ |
10378 | 5.73k | if (p_tcd->whole_tile_decoding) { |
10379 | 5.73k | l_img_comp_dest->data = l_tilec->data; |
10380 | 5.73k | l_tilec->data = NULL; |
10381 | 5.73k | } else { |
10382 | 0 | l_img_comp_dest->data = l_tilec->data_win; |
10383 | 0 | l_tilec->data_win = NULL; |
10384 | 0 | } |
10385 | 5.73k | continue; |
10386 | 5.73k | } else if (l_img_comp_dest->data == NULL) { |
10387 | 796 | OPJ_SIZE_T l_width = l_img_comp_dest->w; |
10388 | 796 | OPJ_SIZE_T l_height = l_img_comp_dest->h; |
10389 | | |
10390 | 796 | if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) || |
10391 | 796 | l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) { |
10392 | | /* would overflow */ |
10393 | 0 | return OPJ_FALSE; |
10394 | 0 | } |
10395 | 796 | l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height * |
10396 | 796 | sizeof(OPJ_INT32)); |
10397 | 796 | if (! l_img_comp_dest->data) { |
10398 | 0 | return OPJ_FALSE; |
10399 | 0 | } |
10400 | | |
10401 | 796 | if (l_img_comp_dest->w != l_width_dest || |
10402 | 796 | l_img_comp_dest->h != l_height_dest) { |
10403 | 796 | memset(l_img_comp_dest->data, 0, |
10404 | 796 | (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32)); |
10405 | 796 | } |
10406 | 796 | } |
10407 | | |
10408 | | /* Move the output buffer to the first place where we will write*/ |
10409 | 3.29k | l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; |
10410 | | |
10411 | 3.29k | { |
10412 | 3.29k | const OPJ_INT32 * l_src_ptr = p_src_data; |
10413 | 3.29k | l_src_ptr += l_start_offset_src; |
10414 | | |
10415 | 767k | for (j = 0; j < l_height_dest; ++j) { |
10416 | 764k | memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32)); |
10417 | 764k | l_dest_ptr += l_img_comp_dest->w; |
10418 | 764k | l_src_ptr += src_data_stride; |
10419 | 764k | } |
10420 | 3.29k | } |
10421 | | |
10422 | | |
10423 | 3.29k | } |
10424 | | |
10425 | 8.46k | return OPJ_TRUE; |
10426 | 8.50k | } |
10427 | | |
10428 | | static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image, |
10429 | | opj_event_mgr_t * p_manager) |
10430 | 0 | { |
10431 | 0 | OPJ_UINT32 it_comp; |
10432 | 0 | OPJ_INT32 l_comp_x1, l_comp_y1; |
10433 | 0 | opj_image_comp_t* l_img_comp = NULL; |
10434 | |
|
10435 | 0 | l_img_comp = p_image->comps; |
10436 | 0 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
10437 | 0 | OPJ_INT32 l_h, l_w; |
10438 | 0 | if (p_image->x0 > (OPJ_UINT32)INT_MAX || |
10439 | 0 | p_image->y0 > (OPJ_UINT32)INT_MAX || |
10440 | 0 | p_image->x1 > (OPJ_UINT32)INT_MAX || |
10441 | 0 | p_image->y1 > (OPJ_UINT32)INT_MAX) { |
10442 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10443 | 0 | "Image coordinates above INT_MAX are not supported\n"); |
10444 | 0 | return OPJ_FALSE; |
10445 | 0 | } |
10446 | | |
10447 | 0 | l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx); |
10448 | 0 | l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy); |
10449 | 0 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
10450 | 0 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
10451 | |
|
10452 | 0 | l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) |
10453 | 0 | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor); |
10454 | 0 | if (l_w < 0) { |
10455 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10456 | 0 | "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", |
10457 | 0 | it_comp, l_w); |
10458 | 0 | return OPJ_FALSE; |
10459 | 0 | } |
10460 | 0 | l_img_comp->w = (OPJ_UINT32)l_w; |
10461 | |
|
10462 | 0 | l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) |
10463 | 0 | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor); |
10464 | 0 | if (l_h < 0) { |
10465 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10466 | 0 | "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", |
10467 | 0 | it_comp, l_h); |
10468 | 0 | return OPJ_FALSE; |
10469 | 0 | } |
10470 | 0 | l_img_comp->h = (OPJ_UINT32)l_h; |
10471 | |
|
10472 | 0 | l_img_comp++; |
10473 | 0 | } |
10474 | | |
10475 | 0 | return OPJ_TRUE; |
10476 | 0 | } |
10477 | | |
10478 | | OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k, |
10479 | | OPJ_UINT32 numcomps, |
10480 | | const OPJ_UINT32* comps_indices, |
10481 | | opj_event_mgr_t * p_manager) |
10482 | 0 | { |
10483 | 0 | OPJ_UINT32 i; |
10484 | 0 | OPJ_BOOL* already_mapped; |
10485 | |
|
10486 | 0 | if (p_j2k->m_private_image == NULL) { |
10487 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10488 | 0 | "opj_read_header() should be called before " |
10489 | 0 | "opj_set_decoded_components().\n"); |
10490 | 0 | return OPJ_FALSE; |
10491 | 0 | } |
10492 | | |
10493 | 0 | already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL), |
10494 | 0 | p_j2k->m_private_image->numcomps); |
10495 | 0 | if (already_mapped == NULL) { |
10496 | 0 | return OPJ_FALSE; |
10497 | 0 | } |
10498 | | |
10499 | 0 | for (i = 0; i < numcomps; i++) { |
10500 | 0 | if (comps_indices[i] >= p_j2k->m_private_image->numcomps) { |
10501 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10502 | 0 | "Invalid component index: %u\n", |
10503 | 0 | comps_indices[i]); |
10504 | 0 | opj_free(already_mapped); |
10505 | 0 | return OPJ_FALSE; |
10506 | 0 | } |
10507 | 0 | if (already_mapped[comps_indices[i]]) { |
10508 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10509 | 0 | "Component index %u used several times\n", |
10510 | 0 | comps_indices[i]); |
10511 | 0 | opj_free(already_mapped); |
10512 | 0 | return OPJ_FALSE; |
10513 | 0 | } |
10514 | 0 | already_mapped[comps_indices[i]] = OPJ_TRUE; |
10515 | 0 | } |
10516 | 0 | opj_free(already_mapped); |
10517 | |
|
10518 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
10519 | 0 | if (numcomps) { |
10520 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = |
10521 | 0 | (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32)); |
10522 | 0 | if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) { |
10523 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
10524 | 0 | return OPJ_FALSE; |
10525 | 0 | } |
10526 | 0 | memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
10527 | 0 | comps_indices, |
10528 | 0 | numcomps * sizeof(OPJ_UINT32)); |
10529 | 0 | } else { |
10530 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL; |
10531 | 0 | } |
10532 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps; |
10533 | |
|
10534 | 0 | return OPJ_TRUE; |
10535 | 0 | } |
10536 | | |
10537 | | |
10538 | | OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k, |
10539 | | opj_image_t* p_image, |
10540 | | OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, |
10541 | | OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, |
10542 | | opj_event_mgr_t * p_manager) |
10543 | 0 | { |
10544 | 0 | opj_cp_t * l_cp = &(p_j2k->m_cp); |
10545 | 0 | opj_image_t * l_image = p_j2k->m_private_image; |
10546 | 0 | OPJ_BOOL ret; |
10547 | 0 | OPJ_UINT32 it_comp; |
10548 | |
|
10549 | 0 | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
10550 | 0 | p_j2k->m_cp.tcps[0].m_data != NULL) { |
10551 | | /* In the case of a single-tiled image whose codestream we have already */ |
10552 | | /* ingested, go on */ |
10553 | 0 | } |
10554 | | /* Check if we are read the main header */ |
10555 | 0 | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
10556 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10557 | 0 | "Need to decode the main header before begin to decode the remaining codestream.\n"); |
10558 | 0 | return OPJ_FALSE; |
10559 | 0 | } |
10560 | | |
10561 | | /* Update the comps[].factor member of the output image with the one */ |
10562 | | /* of m_reduce */ |
10563 | 0 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
10564 | 0 | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
10565 | 0 | } |
10566 | |
|
10567 | 0 | if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) { |
10568 | 0 | opj_event_msg(p_manager, EVT_INFO, |
10569 | 0 | "No decoded area parameters, set the decoded area to the whole image\n"); |
10570 | |
|
10571 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
10572 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
10573 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
10574 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
10575 | |
|
10576 | 0 | p_image->x0 = l_image->x0; |
10577 | 0 | p_image->y0 = l_image->y0; |
10578 | 0 | p_image->x1 = l_image->x1; |
10579 | 0 | p_image->y1 = l_image->y1; |
10580 | |
|
10581 | 0 | return opj_j2k_update_image_dimensions(p_image, p_manager); |
10582 | 0 | } |
10583 | | |
10584 | | /* ----- */ |
10585 | | /* Check if the positions provided by the user are correct */ |
10586 | | |
10587 | | /* Left */ |
10588 | 0 | if (p_start_x < 0) { |
10589 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10590 | 0 | "Left position of the decoded area (region_x0=%d) should be >= 0.\n", |
10591 | 0 | p_start_x); |
10592 | 0 | return OPJ_FALSE; |
10593 | 0 | } else if ((OPJ_UINT32)p_start_x > l_image->x1) { |
10594 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10595 | 0 | "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n", |
10596 | 0 | p_start_x, l_image->x1); |
10597 | 0 | return OPJ_FALSE; |
10598 | 0 | } else if ((OPJ_UINT32)p_start_x < l_image->x0) { |
10599 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10600 | 0 | "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n", |
10601 | 0 | p_start_x, l_image->x0); |
10602 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
10603 | 0 | p_image->x0 = l_image->x0; |
10604 | 0 | } else { |
10605 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - |
10606 | 0 | l_cp->tx0) / l_cp->tdx; |
10607 | 0 | p_image->x0 = (OPJ_UINT32)p_start_x; |
10608 | 0 | } |
10609 | | |
10610 | | /* Up */ |
10611 | 0 | if (p_start_y < 0) { |
10612 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10613 | 0 | "Up position of the decoded area (region_y0=%d) should be >= 0.\n", |
10614 | 0 | p_start_y); |
10615 | 0 | return OPJ_FALSE; |
10616 | 0 | } else if ((OPJ_UINT32)p_start_y > l_image->y1) { |
10617 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10618 | 0 | "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n", |
10619 | 0 | p_start_y, l_image->y1); |
10620 | 0 | return OPJ_FALSE; |
10621 | 0 | } else if ((OPJ_UINT32)p_start_y < l_image->y0) { |
10622 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10623 | 0 | "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n", |
10624 | 0 | p_start_y, l_image->y0); |
10625 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
10626 | 0 | p_image->y0 = l_image->y0; |
10627 | 0 | } else { |
10628 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - |
10629 | 0 | l_cp->ty0) / l_cp->tdy; |
10630 | 0 | p_image->y0 = (OPJ_UINT32)p_start_y; |
10631 | 0 | } |
10632 | | |
10633 | | /* Right */ |
10634 | 0 | if (p_end_x <= 0) { |
10635 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10636 | 0 | "Right position of the decoded area (region_x1=%d) should be > 0.\n", |
10637 | 0 | p_end_x); |
10638 | 0 | return OPJ_FALSE; |
10639 | 0 | } else if ((OPJ_UINT32)p_end_x < l_image->x0) { |
10640 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10641 | 0 | "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n", |
10642 | 0 | p_end_x, l_image->x0); |
10643 | 0 | return OPJ_FALSE; |
10644 | 0 | } else if ((OPJ_UINT32)p_end_x > l_image->x1) { |
10645 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10646 | 0 | "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n", |
10647 | 0 | p_end_x, l_image->x1); |
10648 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
10649 | 0 | p_image->x1 = l_image->x1; |
10650 | 0 | } else { |
10651 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv(( |
10652 | 0 | OPJ_UINT32)p_end_x - l_cp->tx0, l_cp->tdx); |
10653 | 0 | p_image->x1 = (OPJ_UINT32)p_end_x; |
10654 | 0 | } |
10655 | | |
10656 | | /* Bottom */ |
10657 | 0 | if (p_end_y <= 0) { |
10658 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10659 | 0 | "Bottom position of the decoded area (region_y1=%d) should be > 0.\n", |
10660 | 0 | p_end_y); |
10661 | 0 | return OPJ_FALSE; |
10662 | 0 | } else if ((OPJ_UINT32)p_end_y < l_image->y0) { |
10663 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10664 | 0 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n", |
10665 | 0 | p_end_y, l_image->y0); |
10666 | 0 | return OPJ_FALSE; |
10667 | 0 | } |
10668 | 0 | if ((OPJ_UINT32)p_end_y > l_image->y1) { |
10669 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10670 | 0 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n", |
10671 | 0 | p_end_y, l_image->y1); |
10672 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
10673 | 0 | p_image->y1 = l_image->y1; |
10674 | 0 | } else { |
10675 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv(( |
10676 | 0 | OPJ_UINT32)p_end_y - l_cp->ty0, l_cp->tdy); |
10677 | 0 | p_image->y1 = (OPJ_UINT32)p_end_y; |
10678 | 0 | } |
10679 | | /* ----- */ |
10680 | |
|
10681 | 0 | p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; |
10682 | |
|
10683 | 0 | ret = opj_j2k_update_image_dimensions(p_image, p_manager); |
10684 | |
|
10685 | 0 | if (ret) { |
10686 | 0 | opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n", |
10687 | 0 | p_image->x0, p_image->y0, p_image->x1, p_image->y1); |
10688 | 0 | } |
10689 | |
|
10690 | 0 | return ret; |
10691 | 0 | } |
10692 | | |
10693 | | opj_j2k_t* opj_j2k_create_decompress(void) |
10694 | 8.24k | { |
10695 | 8.24k | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
10696 | 8.24k | if (!l_j2k) { |
10697 | 0 | return 00; |
10698 | 0 | } |
10699 | | |
10700 | 8.24k | l_j2k->m_is_decoder = 1; |
10701 | 8.24k | l_j2k->m_cp.m_is_decoder = 1; |
10702 | | /* in the absence of JP2 boxes, consider different bit depth / sign */ |
10703 | | /* per component is allowed */ |
10704 | 8.24k | l_j2k->m_cp.allow_different_bit_depth_sign = 1; |
10705 | | |
10706 | | /* Default to using strict mode. */ |
10707 | 8.24k | l_j2k->m_cp.strict = OPJ_TRUE; |
10708 | | |
10709 | | #ifdef OPJ_DISABLE_TPSOT_FIX |
10710 | | l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
10711 | | #endif |
10712 | | |
10713 | 8.24k | l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1, |
10714 | 8.24k | sizeof(opj_tcp_t)); |
10715 | 8.24k | if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { |
10716 | 0 | opj_j2k_destroy(l_j2k); |
10717 | 0 | return 00; |
10718 | 0 | } |
10719 | | |
10720 | 8.24k | l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1, |
10721 | 8.24k | OPJ_J2K_DEFAULT_HEADER_SIZE); |
10722 | 8.24k | if (! l_j2k->m_specific_param.m_decoder.m_header_data) { |
10723 | 0 | opj_j2k_destroy(l_j2k); |
10724 | 0 | return 00; |
10725 | 0 | } |
10726 | | |
10727 | 8.24k | l_j2k->m_specific_param.m_decoder.m_header_data_size = |
10728 | 8.24k | OPJ_J2K_DEFAULT_HEADER_SIZE; |
10729 | | |
10730 | 8.24k | l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; |
10731 | | |
10732 | 8.24k | l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; |
10733 | | |
10734 | | /* codestream index creation */ |
10735 | 8.24k | l_j2k->cstr_index = opj_j2k_create_cstr_index(); |
10736 | 8.24k | if (!l_j2k->cstr_index) { |
10737 | 0 | opj_j2k_destroy(l_j2k); |
10738 | 0 | return 00; |
10739 | 0 | } |
10740 | | |
10741 | | /* validation list creation */ |
10742 | 8.24k | l_j2k->m_validation_list = opj_procedure_list_create(); |
10743 | 8.24k | if (! l_j2k->m_validation_list) { |
10744 | 0 | opj_j2k_destroy(l_j2k); |
10745 | 0 | return 00; |
10746 | 0 | } |
10747 | | |
10748 | | /* execution list creation */ |
10749 | 8.24k | l_j2k->m_procedure_list = opj_procedure_list_create(); |
10750 | 8.24k | if (! l_j2k->m_procedure_list) { |
10751 | 0 | opj_j2k_destroy(l_j2k); |
10752 | 0 | return 00; |
10753 | 0 | } |
10754 | | |
10755 | 8.24k | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
10756 | 8.24k | if (!l_j2k->m_tp) { |
10757 | 0 | l_j2k->m_tp = opj_thread_pool_create(0); |
10758 | 0 | } |
10759 | 8.24k | if (!l_j2k->m_tp) { |
10760 | 0 | opj_j2k_destroy(l_j2k); |
10761 | 0 | return NULL; |
10762 | 0 | } |
10763 | | |
10764 | 8.24k | return l_j2k; |
10765 | 8.24k | } |
10766 | | |
10767 | | static opj_codestream_index_t* opj_j2k_create_cstr_index(void) |
10768 | 8.24k | { |
10769 | 8.24k | opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) |
10770 | 8.24k | opj_calloc(1, sizeof(opj_codestream_index_t)); |
10771 | 8.24k | if (!cstr_index) { |
10772 | 0 | return NULL; |
10773 | 0 | } |
10774 | | |
10775 | 8.24k | cstr_index->maxmarknum = 100; |
10776 | 8.24k | cstr_index->marknum = 0; |
10777 | 8.24k | cstr_index->marker = (opj_marker_info_t*) |
10778 | 8.24k | opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); |
10779 | 8.24k | if (!cstr_index-> marker) { |
10780 | 0 | opj_free(cstr_index); |
10781 | 0 | return NULL; |
10782 | 0 | } |
10783 | | |
10784 | 8.24k | cstr_index->tile_index = NULL; |
10785 | | |
10786 | 8.24k | return cstr_index; |
10787 | 8.24k | } |
10788 | | |
10789 | | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, |
10790 | | OPJ_UINT32 p_tile_no, |
10791 | | OPJ_UINT32 p_comp_no) |
10792 | 0 | { |
10793 | 0 | opj_cp_t *l_cp = 00; |
10794 | 0 | opj_tcp_t *l_tcp = 00; |
10795 | 0 | opj_tccp_t *l_tccp = 00; |
10796 | | |
10797 | | /* preconditions */ |
10798 | 0 | assert(p_j2k != 00); |
10799 | |
|
10800 | 0 | l_cp = &(p_j2k->m_cp); |
10801 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10802 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10803 | | |
10804 | | /* preconditions again */ |
10805 | 0 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
10806 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
10807 | |
|
10808 | 0 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
10809 | 0 | return 5 + l_tccp->numresolutions; |
10810 | 0 | } else { |
10811 | 0 | return 5; |
10812 | 0 | } |
10813 | 0 | } |
10814 | | |
10815 | | static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10816 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
10817 | 0 | { |
10818 | 0 | OPJ_UINT32 i; |
10819 | 0 | opj_cp_t *l_cp = NULL; |
10820 | 0 | opj_tcp_t *l_tcp = NULL; |
10821 | 0 | opj_tccp_t *l_tccp0 = NULL; |
10822 | 0 | opj_tccp_t *l_tccp1 = NULL; |
10823 | | |
10824 | | /* preconditions */ |
10825 | 0 | assert(p_j2k != 00); |
10826 | |
|
10827 | 0 | l_cp = &(p_j2k->m_cp); |
10828 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10829 | 0 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
10830 | 0 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
10831 | |
|
10832 | 0 | if (l_tccp0->numresolutions != l_tccp1->numresolutions) { |
10833 | 0 | return OPJ_FALSE; |
10834 | 0 | } |
10835 | 0 | if (l_tccp0->cblkw != l_tccp1->cblkw) { |
10836 | 0 | return OPJ_FALSE; |
10837 | 0 | } |
10838 | 0 | if (l_tccp0->cblkh != l_tccp1->cblkh) { |
10839 | 0 | return OPJ_FALSE; |
10840 | 0 | } |
10841 | 0 | if (l_tccp0->cblksty != l_tccp1->cblksty) { |
10842 | 0 | return OPJ_FALSE; |
10843 | 0 | } |
10844 | 0 | if (l_tccp0->qmfbid != l_tccp1->qmfbid) { |
10845 | 0 | return OPJ_FALSE; |
10846 | 0 | } |
10847 | 0 | if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) { |
10848 | 0 | return OPJ_FALSE; |
10849 | 0 | } |
10850 | | |
10851 | 0 | for (i = 0U; i < l_tccp0->numresolutions; ++i) { |
10852 | 0 | if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) { |
10853 | 0 | return OPJ_FALSE; |
10854 | 0 | } |
10855 | 0 | if (l_tccp0->prch[i] != l_tccp1->prch[i]) { |
10856 | 0 | return OPJ_FALSE; |
10857 | 0 | } |
10858 | 0 | } |
10859 | 0 | return OPJ_TRUE; |
10860 | 0 | } |
10861 | | |
10862 | | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10863 | | OPJ_UINT32 p_tile_no, |
10864 | | OPJ_UINT32 p_comp_no, |
10865 | | OPJ_BYTE * p_data, |
10866 | | OPJ_UINT32 * p_header_size, |
10867 | | struct opj_event_mgr * p_manager) |
10868 | 0 | { |
10869 | 0 | OPJ_UINT32 i; |
10870 | 0 | opj_cp_t *l_cp = 00; |
10871 | 0 | opj_tcp_t *l_tcp = 00; |
10872 | 0 | opj_tccp_t *l_tccp = 00; |
10873 | | |
10874 | | /* preconditions */ |
10875 | 0 | assert(p_j2k != 00); |
10876 | 0 | assert(p_header_size != 00); |
10877 | 0 | assert(p_manager != 00); |
10878 | 0 | assert(p_data != 00); |
10879 | |
|
10880 | 0 | l_cp = &(p_j2k->m_cp); |
10881 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10882 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10883 | | |
10884 | | /* preconditions again */ |
10885 | 0 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
10886 | 0 | assert(p_comp_no < (p_j2k->m_private_image->numcomps)); |
10887 | |
|
10888 | 0 | if (*p_header_size < 5) { |
10889 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
10890 | 0 | return OPJ_FALSE; |
10891 | 0 | } |
10892 | | |
10893 | 0 | opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ |
10894 | 0 | ++p_data; |
10895 | |
|
10896 | 0 | opj_write_bytes(p_data, l_tccp->cblkw - 2, 1); /* SPcoc (E) */ |
10897 | 0 | ++p_data; |
10898 | |
|
10899 | 0 | opj_write_bytes(p_data, l_tccp->cblkh - 2, 1); /* SPcoc (F) */ |
10900 | 0 | ++p_data; |
10901 | |
|
10902 | 0 | opj_write_bytes(p_data, l_tccp->cblksty, |
10903 | 0 | 1); /* SPcoc (G) */ |
10904 | 0 | ++p_data; |
10905 | |
|
10906 | 0 | opj_write_bytes(p_data, l_tccp->qmfbid, |
10907 | 0 | 1); /* SPcoc (H) */ |
10908 | 0 | ++p_data; |
10909 | |
|
10910 | 0 | *p_header_size = *p_header_size - 5; |
10911 | |
|
10912 | 0 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
10913 | |
|
10914 | 0 | if (*p_header_size < l_tccp->numresolutions) { |
10915 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
10916 | 0 | return OPJ_FALSE; |
10917 | 0 | } |
10918 | | |
10919 | 0 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
10920 | 0 | opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4), |
10921 | 0 | 1); /* SPcoc (I_i) */ |
10922 | 0 | ++p_data; |
10923 | 0 | } |
10924 | |
|
10925 | 0 | *p_header_size = *p_header_size - l_tccp->numresolutions; |
10926 | 0 | } |
10927 | | |
10928 | 0 | return OPJ_TRUE; |
10929 | 0 | } |
10930 | | |
10931 | | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10932 | | OPJ_UINT32 compno, |
10933 | | OPJ_BYTE * p_header_data, |
10934 | | OPJ_UINT32 * p_header_size, |
10935 | | opj_event_mgr_t * p_manager) |
10936 | 13.2k | { |
10937 | 13.2k | OPJ_UINT32 i, l_tmp; |
10938 | 13.2k | opj_cp_t *l_cp = NULL; |
10939 | 13.2k | opj_tcp_t *l_tcp = NULL; |
10940 | 13.2k | opj_tccp_t *l_tccp = NULL; |
10941 | 13.2k | OPJ_BYTE * l_current_ptr = NULL; |
10942 | | |
10943 | | /* preconditions */ |
10944 | 13.2k | assert(p_j2k != 00); |
10945 | 13.2k | assert(p_manager != 00); |
10946 | 13.2k | assert(p_header_data != 00); |
10947 | | |
10948 | 13.2k | l_cp = &(p_j2k->m_cp); |
10949 | 13.2k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
10950 | 52 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
10951 | 13.2k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
10952 | | |
10953 | | /* precondition again */ |
10954 | 13.2k | assert(compno < p_j2k->m_private_image->numcomps); |
10955 | | |
10956 | 13.2k | l_tccp = &l_tcp->tccps[compno]; |
10957 | 13.2k | l_current_ptr = p_header_data; |
10958 | | |
10959 | | /* make sure room is sufficient */ |
10960 | 13.2k | if (*p_header_size < 5) { |
10961 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
10962 | 0 | return OPJ_FALSE; |
10963 | 0 | } |
10964 | | |
10965 | | /* SPcod (D) / SPcoc (A) */ |
10966 | 13.2k | opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, 1); |
10967 | 13.2k | ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ |
10968 | 13.2k | if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { |
10969 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10970 | 0 | "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n", |
10971 | 0 | l_tccp->numresolutions, OPJ_J2K_MAXRLVLS); |
10972 | 0 | return OPJ_FALSE; |
10973 | 0 | } |
10974 | 13.2k | ++l_current_ptr; |
10975 | | |
10976 | | /* If user wants to remove more resolutions than the codestream contains, return error */ |
10977 | 13.2k | if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { |
10978 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10979 | 0 | "Error decoding component %d.\nThe number of resolutions " |
10980 | 0 | "to remove (%d) is greater or equal than the number " |
10981 | 0 | "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n", |
10982 | 0 | compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions); |
10983 | 0 | p_j2k->m_specific_param.m_decoder.m_state |= |
10984 | 0 | 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ |
10985 | 0 | return OPJ_FALSE; |
10986 | 0 | } |
10987 | | |
10988 | | /* SPcod (E) / SPcoc (B) */ |
10989 | 13.2k | opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1); |
10990 | 13.2k | ++l_current_ptr; |
10991 | 13.2k | l_tccp->cblkw += 2; |
10992 | | |
10993 | | /* SPcod (F) / SPcoc (C) */ |
10994 | 13.2k | opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1); |
10995 | 13.2k | ++l_current_ptr; |
10996 | 13.2k | l_tccp->cblkh += 2; |
10997 | | |
10998 | 13.2k | if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || |
10999 | 13.2k | ((l_tccp->cblkw + l_tccp->cblkh) > 12)) { |
11000 | 11 | opj_event_msg(p_manager, EVT_ERROR, |
11001 | 11 | "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n"); |
11002 | 11 | return OPJ_FALSE; |
11003 | 11 | } |
11004 | | |
11005 | | /* SPcod (G) / SPcoc (D) */ |
11006 | 13.2k | opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1); |
11007 | 13.2k | ++l_current_ptr; |
11008 | 13.2k | if ((l_tccp->cblksty & J2K_CCP_CBLKSTY_HTMIXED) != 0) { |
11009 | | /* We do not support HT mixed mode yet. For conformance, it should be supported.*/ |
11010 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
11011 | 4 | "Error reading SPCod SPCoc element. Unsupported Mixed HT code-block style found\n"); |
11012 | 4 | return OPJ_FALSE; |
11013 | 4 | } |
11014 | | |
11015 | | /* SPcod (H) / SPcoc (E) */ |
11016 | 13.2k | opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1); |
11017 | 13.2k | ++l_current_ptr; |
11018 | | |
11019 | 13.2k | if (l_tccp->qmfbid > 1) { |
11020 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
11021 | 3 | "Error reading SPCod SPCoc element, Invalid transformation found\n"); |
11022 | 3 | return OPJ_FALSE; |
11023 | 3 | } |
11024 | | |
11025 | 13.2k | *p_header_size = *p_header_size - 5; |
11026 | | |
11027 | | /* use custom precinct size ? */ |
11028 | 13.2k | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
11029 | 6.50k | if (*p_header_size < l_tccp->numresolutions) { |
11030 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
11031 | 0 | return OPJ_FALSE; |
11032 | 0 | } |
11033 | | |
11034 | | /* SPcod (I_i) / SPcoc (F_i) */ |
11035 | 17.3k | for (i = 0; i < l_tccp->numresolutions; ++i) { |
11036 | 10.8k | opj_read_bytes(l_current_ptr, &l_tmp, 1); |
11037 | 10.8k | ++l_current_ptr; |
11038 | | /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */ |
11039 | 10.8k | if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) { |
11040 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n"); |
11041 | 2 | return OPJ_FALSE; |
11042 | 2 | } |
11043 | 10.8k | l_tccp->prcw[i] = l_tmp & 0xf; |
11044 | 10.8k | l_tccp->prch[i] = l_tmp >> 4; |
11045 | 10.8k | } |
11046 | | |
11047 | 6.50k | *p_header_size = *p_header_size - l_tccp->numresolutions; |
11048 | 6.75k | } else { |
11049 | | /* set default size for the precinct width and height */ |
11050 | 38.5k | for (i = 0; i < l_tccp->numresolutions; ++i) { |
11051 | 31.7k | l_tccp->prcw[i] = 15; |
11052 | 31.7k | l_tccp->prch[i] = 15; |
11053 | 31.7k | } |
11054 | 6.75k | } |
11055 | | |
11056 | | #ifdef WIP_REMOVE_MSD |
11057 | | /* INDEX >> */ |
11058 | | if (p_j2k->cstr_info && compno == 0) { |
11059 | | OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); |
11060 | | |
11061 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = |
11062 | | l_tccp->cblkh; |
11063 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = |
11064 | | l_tccp->cblkw; |
11065 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions |
11066 | | = l_tccp->numresolutions; |
11067 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = |
11068 | | l_tccp->cblksty; |
11069 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = |
11070 | | l_tccp->qmfbid; |
11071 | | |
11072 | | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw, |
11073 | | l_data_size); |
11074 | | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch, |
11075 | | l_data_size); |
11076 | | } |
11077 | | /* << INDEX */ |
11078 | | #endif |
11079 | | |
11080 | 13.2k | return OPJ_TRUE; |
11081 | 13.2k | } |
11082 | | |
11083 | | static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k) |
11084 | 12.9k | { |
11085 | | /* loop */ |
11086 | 12.9k | OPJ_UINT32 i; |
11087 | 12.9k | opj_cp_t *l_cp = NULL; |
11088 | 12.9k | opj_tcp_t *l_tcp = NULL; |
11089 | 12.9k | opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; |
11090 | 12.9k | OPJ_UINT32 l_prc_size; |
11091 | | |
11092 | | /* preconditions */ |
11093 | 12.9k | assert(p_j2k != 00); |
11094 | | |
11095 | 12.9k | l_cp = &(p_j2k->m_cp); |
11096 | 12.9k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
11097 | 12.9k | ? |
11098 | 49 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11099 | 12.9k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11100 | | |
11101 | 12.9k | l_ref_tccp = &l_tcp->tccps[0]; |
11102 | 12.9k | l_copied_tccp = l_ref_tccp + 1; |
11103 | 12.9k | l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32); |
11104 | | |
11105 | 21.5k | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
11106 | 8.60k | l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; |
11107 | 8.60k | l_copied_tccp->cblkw = l_ref_tccp->cblkw; |
11108 | 8.60k | l_copied_tccp->cblkh = l_ref_tccp->cblkh; |
11109 | 8.60k | l_copied_tccp->cblksty = l_ref_tccp->cblksty; |
11110 | 8.60k | l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; |
11111 | 8.60k | memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size); |
11112 | 8.60k | memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size); |
11113 | 8.60k | ++l_copied_tccp; |
11114 | 8.60k | } |
11115 | 12.9k | } |
11116 | | |
11117 | | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, |
11118 | | OPJ_UINT32 p_tile_no, |
11119 | | OPJ_UINT32 p_comp_no) |
11120 | 0 | { |
11121 | 0 | OPJ_UINT32 l_num_bands; |
11122 | |
|
11123 | 0 | opj_cp_t *l_cp = 00; |
11124 | 0 | opj_tcp_t *l_tcp = 00; |
11125 | 0 | opj_tccp_t *l_tccp = 00; |
11126 | | |
11127 | | /* preconditions */ |
11128 | 0 | assert(p_j2k != 00); |
11129 | |
|
11130 | 0 | l_cp = &(p_j2k->m_cp); |
11131 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
11132 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
11133 | | |
11134 | | /* preconditions again */ |
11135 | 0 | assert(p_tile_no < l_cp->tw * l_cp->th); |
11136 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
11137 | |
|
11138 | 0 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11139 | 0 | (l_tccp->numresolutions * 3 - 2); |
11140 | |
|
11141 | 0 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
11142 | 0 | return 1 + l_num_bands; |
11143 | 0 | } else { |
11144 | 0 | return 1 + 2 * l_num_bands; |
11145 | 0 | } |
11146 | 0 | } |
11147 | | |
11148 | | static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, |
11149 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
11150 | 0 | { |
11151 | 0 | opj_cp_t *l_cp = NULL; |
11152 | 0 | opj_tcp_t *l_tcp = NULL; |
11153 | 0 | opj_tccp_t *l_tccp0 = NULL; |
11154 | 0 | opj_tccp_t *l_tccp1 = NULL; |
11155 | 0 | OPJ_UINT32 l_band_no, l_num_bands; |
11156 | | |
11157 | | /* preconditions */ |
11158 | 0 | assert(p_j2k != 00); |
11159 | |
|
11160 | 0 | l_cp = &(p_j2k->m_cp); |
11161 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
11162 | 0 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
11163 | 0 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
11164 | |
|
11165 | 0 | if (l_tccp0->qntsty != l_tccp1->qntsty) { |
11166 | 0 | return OPJ_FALSE; |
11167 | 0 | } |
11168 | 0 | if (l_tccp0->numgbits != l_tccp1->numgbits) { |
11169 | 0 | return OPJ_FALSE; |
11170 | 0 | } |
11171 | 0 | if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11172 | 0 | l_num_bands = 1U; |
11173 | 0 | } else { |
11174 | 0 | l_num_bands = l_tccp0->numresolutions * 3U - 2U; |
11175 | 0 | if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) { |
11176 | 0 | return OPJ_FALSE; |
11177 | 0 | } |
11178 | 0 | } |
11179 | | |
11180 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11181 | 0 | if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) { |
11182 | 0 | return OPJ_FALSE; |
11183 | 0 | } |
11184 | 0 | } |
11185 | 0 | if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) { |
11186 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11187 | 0 | if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) { |
11188 | 0 | return OPJ_FALSE; |
11189 | 0 | } |
11190 | 0 | } |
11191 | 0 | } |
11192 | 0 | return OPJ_TRUE; |
11193 | 0 | } |
11194 | | |
11195 | | |
11196 | | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
11197 | | OPJ_UINT32 p_tile_no, |
11198 | | OPJ_UINT32 p_comp_no, |
11199 | | OPJ_BYTE * p_data, |
11200 | | OPJ_UINT32 * p_header_size, |
11201 | | struct opj_event_mgr * p_manager) |
11202 | 0 | { |
11203 | 0 | OPJ_UINT32 l_header_size; |
11204 | 0 | OPJ_UINT32 l_band_no, l_num_bands; |
11205 | 0 | OPJ_UINT32 l_expn, l_mant; |
11206 | |
|
11207 | 0 | opj_cp_t *l_cp = 00; |
11208 | 0 | opj_tcp_t *l_tcp = 00; |
11209 | 0 | opj_tccp_t *l_tccp = 00; |
11210 | | |
11211 | | /* preconditions */ |
11212 | 0 | assert(p_j2k != 00); |
11213 | 0 | assert(p_header_size != 00); |
11214 | 0 | assert(p_manager != 00); |
11215 | 0 | assert(p_data != 00); |
11216 | |
|
11217 | 0 | l_cp = &(p_j2k->m_cp); |
11218 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
11219 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
11220 | | |
11221 | | /* preconditions again */ |
11222 | 0 | assert(p_tile_no < l_cp->tw * l_cp->th); |
11223 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
11224 | |
|
11225 | 0 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11226 | 0 | (l_tccp->numresolutions * 3 - 2); |
11227 | |
|
11228 | 0 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
11229 | 0 | l_header_size = 1 + l_num_bands; |
11230 | |
|
11231 | 0 | if (*p_header_size < l_header_size) { |
11232 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
11233 | 0 | return OPJ_FALSE; |
11234 | 0 | } |
11235 | | |
11236 | 0 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
11237 | 0 | 1); /* Sqcx */ |
11238 | 0 | ++p_data; |
11239 | |
|
11240 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11241 | 0 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
11242 | 0 | opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ |
11243 | 0 | ++p_data; |
11244 | 0 | } |
11245 | 0 | } else { |
11246 | 0 | l_header_size = 1 + 2 * l_num_bands; |
11247 | |
|
11248 | 0 | if (*p_header_size < l_header_size) { |
11249 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
11250 | 0 | return OPJ_FALSE; |
11251 | 0 | } |
11252 | | |
11253 | 0 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
11254 | 0 | 1); /* Sqcx */ |
11255 | 0 | ++p_data; |
11256 | |
|
11257 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11258 | 0 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
11259 | 0 | l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant; |
11260 | |
|
11261 | 0 | opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ |
11262 | 0 | p_data += 2; |
11263 | 0 | } |
11264 | 0 | } |
11265 | | |
11266 | 0 | *p_header_size = *p_header_size - l_header_size; |
11267 | |
|
11268 | 0 | return OPJ_TRUE; |
11269 | 0 | } |
11270 | | |
11271 | | static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
11272 | | OPJ_UINT32 p_comp_no, |
11273 | | OPJ_BYTE* p_header_data, |
11274 | | OPJ_UINT32 * p_header_size, |
11275 | | opj_event_mgr_t * p_manager |
11276 | | ) |
11277 | 23.1k | { |
11278 | | /* loop*/ |
11279 | 23.1k | OPJ_UINT32 l_band_no; |
11280 | 23.1k | opj_cp_t *l_cp = 00; |
11281 | 23.1k | opj_tcp_t *l_tcp = 00; |
11282 | 23.1k | opj_tccp_t *l_tccp = 00; |
11283 | 23.1k | OPJ_BYTE * l_current_ptr = 00; |
11284 | 23.1k | OPJ_UINT32 l_tmp, l_num_band; |
11285 | | |
11286 | | /* preconditions*/ |
11287 | 23.1k | assert(p_j2k != 00); |
11288 | 23.1k | assert(p_manager != 00); |
11289 | 23.1k | assert(p_header_data != 00); |
11290 | | |
11291 | 23.1k | l_cp = &(p_j2k->m_cp); |
11292 | | /* come from tile part header or main header ?*/ |
11293 | 23.1k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
11294 | 23.1k | ? |
11295 | 34 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11296 | 23.1k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11297 | | |
11298 | | /* precondition again*/ |
11299 | 23.1k | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
11300 | | |
11301 | 23.1k | l_tccp = &l_tcp->tccps[p_comp_no]; |
11302 | 23.1k | l_current_ptr = p_header_data; |
11303 | | |
11304 | 23.1k | if (*p_header_size < 1) { |
11305 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); |
11306 | 0 | return OPJ_FALSE; |
11307 | 0 | } |
11308 | 23.1k | *p_header_size -= 1; |
11309 | | |
11310 | 23.1k | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* Sqcx */ |
11311 | 23.1k | ++l_current_ptr; |
11312 | | |
11313 | 23.1k | l_tccp->qntsty = l_tmp & 0x1f; |
11314 | 23.1k | l_tccp->numgbits = l_tmp >> 5; |
11315 | 23.1k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11316 | 10.5k | l_num_band = 1; |
11317 | 12.6k | } else { |
11318 | 12.6k | l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? |
11319 | 7.39k | (*p_header_size) : |
11320 | 12.6k | (*p_header_size) / 2; |
11321 | | |
11322 | 12.6k | if (l_num_band > OPJ_J2K_MAXBANDS) { |
11323 | 130 | opj_event_msg(p_manager, EVT_WARNING, |
11324 | 130 | "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " |
11325 | 130 | "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to " |
11326 | 130 | "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, |
11327 | 130 | OPJ_J2K_MAXBANDS); |
11328 | | /*return OPJ_FALSE;*/ |
11329 | 130 | } |
11330 | 12.6k | } |
11331 | | |
11332 | | #ifdef USE_JPWL |
11333 | | if (l_cp->correct) { |
11334 | | |
11335 | | /* if JPWL is on, we check whether there are too many subbands */ |
11336 | | if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { |
11337 | | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
11338 | | "JPWL: bad number of subbands in Sqcx (%d)\n", |
11339 | | l_num_band); |
11340 | | if (!JPWL_ASSUME) { |
11341 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
11342 | | return OPJ_FALSE; |
11343 | | } |
11344 | | /* we try to correct */ |
11345 | | l_num_band = 1; |
11346 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" |
11347 | | "- setting number of bands to %d => HYPOTHESIS!!!\n", |
11348 | | l_num_band); |
11349 | | }; |
11350 | | |
11351 | | }; |
11352 | | #endif /* USE_JPWL */ |
11353 | | |
11354 | 23.1k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
11355 | 54.9k | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
11356 | 47.5k | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPqcx_i */ |
11357 | 47.5k | ++l_current_ptr; |
11358 | 47.5k | if (l_band_no < OPJ_J2K_MAXBANDS) { |
11359 | 44.3k | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3); |
11360 | 44.3k | l_tccp->stepsizes[l_band_no].mant = 0; |
11361 | 44.3k | } |
11362 | 47.5k | } |
11363 | | |
11364 | 7.39k | if (*p_header_size < l_num_band) { |
11365 | 0 | return OPJ_FALSE; |
11366 | 0 | } |
11367 | 7.39k | *p_header_size = *p_header_size - l_num_band; |
11368 | 15.7k | } else { |
11369 | 51.8k | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
11370 | 36.1k | opj_read_bytes(l_current_ptr, &l_tmp, 2); /* SPqcx_i */ |
11371 | 36.1k | l_current_ptr += 2; |
11372 | 36.1k | if (l_band_no < OPJ_J2K_MAXBANDS) { |
11373 | 35.0k | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11); |
11374 | 35.0k | l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; |
11375 | 35.0k | } |
11376 | 36.1k | } |
11377 | | |
11378 | 15.7k | if (*p_header_size < 2 * l_num_band) { |
11379 | 1 | return OPJ_FALSE; |
11380 | 1 | } |
11381 | 15.7k | *p_header_size = *p_header_size - 2 * l_num_band; |
11382 | 15.7k | } |
11383 | | |
11384 | | /* Add Antonin : if scalar_derived -> compute other stepsizes */ |
11385 | 23.1k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11386 | 1.02M | for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) { |
11387 | 1.01M | l_tccp->stepsizes[l_band_no].expn = |
11388 | 1.01M | ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) |
11389 | 1.01M | ? |
11390 | 764k | (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0; |
11391 | 1.01M | l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; |
11392 | 1.01M | } |
11393 | 10.5k | } |
11394 | | |
11395 | 23.1k | return OPJ_TRUE; |
11396 | 23.1k | } |
11397 | | |
11398 | | static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k) |
11399 | 22.6k | { |
11400 | 22.6k | OPJ_UINT32 i; |
11401 | 22.6k | opj_cp_t *l_cp = NULL; |
11402 | 22.6k | opj_tcp_t *l_tcp = NULL; |
11403 | 22.6k | opj_tccp_t *l_ref_tccp = NULL; |
11404 | 22.6k | opj_tccp_t *l_copied_tccp = NULL; |
11405 | 22.6k | OPJ_UINT32 l_size; |
11406 | | |
11407 | | /* preconditions */ |
11408 | 22.6k | assert(p_j2k != 00); |
11409 | | |
11410 | 22.6k | l_cp = &(p_j2k->m_cp); |
11411 | 22.6k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
11412 | 34 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11413 | 22.6k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11414 | | |
11415 | 22.6k | l_ref_tccp = &l_tcp->tccps[0]; |
11416 | 22.6k | l_copied_tccp = l_ref_tccp + 1; |
11417 | 22.6k | l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t); |
11418 | | |
11419 | 50.0k | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
11420 | 27.3k | l_copied_tccp->qntsty = l_ref_tccp->qntsty; |
11421 | 27.3k | l_copied_tccp->numgbits = l_ref_tccp->numgbits; |
11422 | 27.3k | memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size); |
11423 | 27.3k | ++l_copied_tccp; |
11424 | 27.3k | } |
11425 | 22.6k | } |
11426 | | |
11427 | | static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile, |
11428 | | OPJ_INT32 numcomps, FILE* out_stream) |
11429 | 0 | { |
11430 | 0 | if (l_default_tile) { |
11431 | 0 | OPJ_INT32 compno; |
11432 | |
|
11433 | 0 | fprintf(out_stream, "\t default tile {\n"); |
11434 | 0 | fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty); |
11435 | 0 | fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg); |
11436 | 0 | fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers); |
11437 | 0 | fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct); |
11438 | |
|
11439 | 0 | for (compno = 0; compno < numcomps; compno++) { |
11440 | 0 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
11441 | 0 | OPJ_UINT32 resno; |
11442 | 0 | OPJ_INT32 bandno, numbands; |
11443 | | |
11444 | | /* coding style*/ |
11445 | 0 | fprintf(out_stream, "\t\t comp %d {\n", compno); |
11446 | 0 | fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty); |
11447 | 0 | fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions); |
11448 | 0 | fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw); |
11449 | 0 | fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh); |
11450 | 0 | fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty); |
11451 | 0 | fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid); |
11452 | |
|
11453 | 0 | fprintf(out_stream, "\t\t\t preccintsize (w,h)="); |
11454 | 0 | for (resno = 0; resno < l_tccp->numresolutions; resno++) { |
11455 | 0 | fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]); |
11456 | 0 | } |
11457 | 0 | fprintf(out_stream, "\n"); |
11458 | | |
11459 | | /* quantization style*/ |
11460 | 0 | fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty); |
11461 | 0 | fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits); |
11462 | 0 | fprintf(out_stream, "\t\t\t stepsizes (m,e)="); |
11463 | 0 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11464 | 0 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
11465 | 0 | for (bandno = 0; bandno < numbands; bandno++) { |
11466 | 0 | fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant, |
11467 | 0 | l_tccp->stepsizes[bandno].expn); |
11468 | 0 | } |
11469 | 0 | fprintf(out_stream, "\n"); |
11470 | | |
11471 | | /* RGN value*/ |
11472 | 0 | fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift); |
11473 | |
|
11474 | 0 | fprintf(out_stream, "\t\t }\n"); |
11475 | 0 | } /*end of component of default tile*/ |
11476 | 0 | fprintf(out_stream, "\t }\n"); /*end of default tile*/ |
11477 | 0 | } |
11478 | 0 | } |
11479 | | |
11480 | | void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream) |
11481 | 0 | { |
11482 | | /* Check if the flag is compatible with j2k file*/ |
11483 | 0 | if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) { |
11484 | 0 | fprintf(out_stream, "Wrong flag\n"); |
11485 | 0 | return; |
11486 | 0 | } |
11487 | | |
11488 | | /* Dump the image_header */ |
11489 | 0 | if (flag & OPJ_IMG_INFO) { |
11490 | 0 | if (p_j2k->m_private_image) { |
11491 | 0 | j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); |
11492 | 0 | } |
11493 | 0 | } |
11494 | | |
11495 | | /* Dump the codestream info from main header */ |
11496 | 0 | if (flag & OPJ_J2K_MH_INFO) { |
11497 | 0 | if (p_j2k->m_private_image) { |
11498 | 0 | opj_j2k_dump_MH_info(p_j2k, out_stream); |
11499 | 0 | } |
11500 | 0 | } |
11501 | | /* Dump all tile/codestream info */ |
11502 | 0 | if (flag & OPJ_J2K_TCH_INFO) { |
11503 | 0 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
11504 | 0 | OPJ_UINT32 i; |
11505 | 0 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
11506 | 0 | if (p_j2k->m_private_image) { |
11507 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
11508 | 0 | opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps, |
11509 | 0 | out_stream); |
11510 | 0 | ++l_tcp; |
11511 | 0 | } |
11512 | 0 | } |
11513 | 0 | } |
11514 | | |
11515 | | /* Dump the codestream info of the current tile */ |
11516 | 0 | if (flag & OPJ_J2K_TH_INFO) { |
11517 | |
|
11518 | 0 | } |
11519 | | |
11520 | | /* Dump the codestream index from main header */ |
11521 | 0 | if (flag & OPJ_J2K_MH_IND) { |
11522 | 0 | opj_j2k_dump_MH_index(p_j2k, out_stream); |
11523 | 0 | } |
11524 | | |
11525 | | /* Dump the codestream index of the current tile */ |
11526 | 0 | if (flag & OPJ_J2K_TH_IND) { |
11527 | |
|
11528 | 0 | } |
11529 | |
|
11530 | 0 | } |
11531 | | |
11532 | | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream) |
11533 | 0 | { |
11534 | 0 | opj_codestream_index_t* cstr_index = p_j2k->cstr_index; |
11535 | 0 | OPJ_UINT32 it_marker, it_tile, it_tile_part; |
11536 | |
|
11537 | 0 | fprintf(out_stream, "Codestream index from main header: {\n"); |
11538 | |
|
11539 | 0 | fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" |
11540 | 0 | "\t Main header end position=%" PRIi64 "\n", |
11541 | 0 | cstr_index->main_head_start, cstr_index->main_head_end); |
11542 | |
|
11543 | 0 | fprintf(out_stream, "\t Marker list: {\n"); |
11544 | |
|
11545 | 0 | if (cstr_index->marker) { |
11546 | 0 | for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) { |
11547 | 0 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
11548 | 0 | cstr_index->marker[it_marker].type, |
11549 | 0 | cstr_index->marker[it_marker].pos, |
11550 | 0 | cstr_index->marker[it_marker].len); |
11551 | 0 | } |
11552 | 0 | } |
11553 | |
|
11554 | 0 | fprintf(out_stream, "\t }\n"); |
11555 | |
|
11556 | 0 | if (cstr_index->tile_index) { |
11557 | | |
11558 | | /* Simple test to avoid to write empty information*/ |
11559 | 0 | OPJ_UINT32 l_acc_nb_of_tile_part = 0; |
11560 | 0 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
11561 | 0 | l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; |
11562 | | |
11563 | | /* To avoid regenerating expected opj_dump results from the test */ |
11564 | | /* suite when there is a TLM marker present */ |
11565 | 0 | if (cstr_index->tile_index[it_tile].nb_tps && |
11566 | 0 | cstr_index->tile_index[it_tile].tp_index && |
11567 | 0 | cstr_index->tile_index[it_tile].tp_index[0].start_pos > 0 && |
11568 | 0 | cstr_index->tile_index[it_tile].tp_index[0].end_header == 0 && |
11569 | 0 | getenv("OJP_DO_NOT_DISPLAY_TILE_INDEX_IF_TLM") != NULL) { |
11570 | 0 | l_acc_nb_of_tile_part = 0; |
11571 | 0 | break; |
11572 | 0 | } |
11573 | 0 | } |
11574 | |
|
11575 | 0 | if (l_acc_nb_of_tile_part) { |
11576 | 0 | fprintf(out_stream, "\t Tile index: {\n"); |
11577 | |
|
11578 | 0 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
11579 | 0 | OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; |
11580 | |
|
11581 | 0 | fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, |
11582 | 0 | nb_of_tile_part); |
11583 | |
|
11584 | 0 | if (cstr_index->tile_index[it_tile].tp_index) { |
11585 | 0 | for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) { |
11586 | 0 | fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" |
11587 | 0 | PRIi64 ", end_pos=%" PRIi64 ".\n", |
11588 | 0 | it_tile_part, |
11589 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, |
11590 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, |
11591 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); |
11592 | 0 | } |
11593 | 0 | } |
11594 | |
|
11595 | 0 | if (cstr_index->tile_index[it_tile].marker) { |
11596 | 0 | for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ; |
11597 | 0 | it_marker++) { |
11598 | 0 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
11599 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].type, |
11600 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].pos, |
11601 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].len); |
11602 | 0 | } |
11603 | 0 | } |
11604 | 0 | } |
11605 | 0 | fprintf(out_stream, "\t }\n"); |
11606 | 0 | } |
11607 | 0 | } |
11608 | |
|
11609 | 0 | fprintf(out_stream, "}\n"); |
11610 | |
|
11611 | 0 | } |
11612 | | |
11613 | | |
11614 | | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream) |
11615 | 0 | { |
11616 | |
|
11617 | 0 | fprintf(out_stream, "Codestream info from main header: {\n"); |
11618 | |
|
11619 | 0 | fprintf(out_stream, "\t tx0=%" PRIu32 ", ty0=%" PRIu32 "\n", p_j2k->m_cp.tx0, |
11620 | 0 | p_j2k->m_cp.ty0); |
11621 | 0 | fprintf(out_stream, "\t tdx=%" PRIu32 ", tdy=%" PRIu32 "\n", p_j2k->m_cp.tdx, |
11622 | 0 | p_j2k->m_cp.tdy); |
11623 | 0 | fprintf(out_stream, "\t tw=%" PRIu32 ", th=%" PRIu32 "\n", p_j2k->m_cp.tw, |
11624 | 0 | p_j2k->m_cp.th); |
11625 | 0 | opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp, |
11626 | 0 | (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream); |
11627 | 0 | fprintf(out_stream, "}\n"); |
11628 | 0 | } |
11629 | | |
11630 | | void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, |
11631 | | FILE* out_stream) |
11632 | 0 | { |
11633 | 0 | char tab[2]; |
11634 | |
|
11635 | 0 | if (dev_dump_flag) { |
11636 | 0 | fprintf(stdout, "[DEV] Dump an image_header struct {\n"); |
11637 | 0 | tab[0] = '\0'; |
11638 | 0 | } else { |
11639 | 0 | fprintf(out_stream, "Image info {\n"); |
11640 | 0 | tab[0] = '\t'; |
11641 | 0 | tab[1] = '\0'; |
11642 | 0 | } |
11643 | |
|
11644 | 0 | fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0); |
11645 | 0 | fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, |
11646 | 0 | img_header->y1); |
11647 | 0 | fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps); |
11648 | |
|
11649 | 0 | if (img_header->comps) { |
11650 | 0 | OPJ_UINT32 compno; |
11651 | 0 | for (compno = 0; compno < img_header->numcomps; compno++) { |
11652 | 0 | fprintf(out_stream, "%s\t component %d {\n", tab, compno); |
11653 | 0 | j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, |
11654 | 0 | out_stream); |
11655 | 0 | fprintf(out_stream, "%s}\n", tab); |
11656 | 0 | } |
11657 | 0 | } |
11658 | |
|
11659 | 0 | fprintf(out_stream, "}\n"); |
11660 | 0 | } |
11661 | | |
11662 | | void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, |
11663 | | OPJ_BOOL dev_dump_flag, FILE* out_stream) |
11664 | 0 | { |
11665 | 0 | char tab[3]; |
11666 | |
|
11667 | 0 | if (dev_dump_flag) { |
11668 | 0 | fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); |
11669 | 0 | tab[0] = '\0'; |
11670 | 0 | } else { |
11671 | 0 | tab[0] = '\t'; |
11672 | 0 | tab[1] = '\t'; |
11673 | 0 | tab[2] = '\0'; |
11674 | 0 | } |
11675 | |
|
11676 | 0 | fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy); |
11677 | 0 | fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec); |
11678 | 0 | fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd); |
11679 | |
|
11680 | 0 | if (dev_dump_flag) { |
11681 | 0 | fprintf(out_stream, "}\n"); |
11682 | 0 | } |
11683 | 0 | } |
11684 | | |
11685 | | opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) |
11686 | 0 | { |
11687 | 0 | OPJ_UINT32 compno; |
11688 | 0 | OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps; |
11689 | 0 | opj_tcp_t *l_default_tile; |
11690 | 0 | opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1, |
11691 | 0 | sizeof(opj_codestream_info_v2_t)); |
11692 | 0 | if (!cstr_info) { |
11693 | 0 | return NULL; |
11694 | 0 | } |
11695 | | |
11696 | 0 | cstr_info->nbcomps = p_j2k->m_private_image->numcomps; |
11697 | |
|
11698 | 0 | cstr_info->tx0 = p_j2k->m_cp.tx0; |
11699 | 0 | cstr_info->ty0 = p_j2k->m_cp.ty0; |
11700 | 0 | cstr_info->tdx = p_j2k->m_cp.tdx; |
11701 | 0 | cstr_info->tdy = p_j2k->m_cp.tdy; |
11702 | 0 | cstr_info->tw = p_j2k->m_cp.tw; |
11703 | 0 | cstr_info->th = p_j2k->m_cp.th; |
11704 | |
|
11705 | 0 | cstr_info->tile_info = NULL; /* Not fill from the main header*/ |
11706 | |
|
11707 | 0 | l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11708 | |
|
11709 | 0 | cstr_info->m_default_tile_info.csty = l_default_tile->csty; |
11710 | 0 | cstr_info->m_default_tile_info.prg = l_default_tile->prg; |
11711 | 0 | cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; |
11712 | 0 | cstr_info->m_default_tile_info.mct = l_default_tile->mct; |
11713 | |
|
11714 | 0 | cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc( |
11715 | 0 | cstr_info->nbcomps, sizeof(opj_tccp_info_t)); |
11716 | 0 | if (!cstr_info->m_default_tile_info.tccp_info) { |
11717 | 0 | opj_destroy_cstr_info(&cstr_info); |
11718 | 0 | return NULL; |
11719 | 0 | } |
11720 | | |
11721 | 0 | for (compno = 0; compno < numcomps; compno++) { |
11722 | 0 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
11723 | 0 | opj_tccp_info_t *l_tccp_info = & |
11724 | 0 | (cstr_info->m_default_tile_info.tccp_info[compno]); |
11725 | 0 | OPJ_INT32 bandno, numbands; |
11726 | | |
11727 | | /* coding style*/ |
11728 | 0 | l_tccp_info->csty = l_tccp->csty; |
11729 | 0 | l_tccp_info->numresolutions = l_tccp->numresolutions; |
11730 | 0 | l_tccp_info->cblkw = l_tccp->cblkw; |
11731 | 0 | l_tccp_info->cblkh = l_tccp->cblkh; |
11732 | 0 | l_tccp_info->cblksty = l_tccp->cblksty; |
11733 | 0 | l_tccp_info->qmfbid = l_tccp->qmfbid; |
11734 | 0 | if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) { |
11735 | 0 | memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); |
11736 | 0 | memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); |
11737 | 0 | } |
11738 | | |
11739 | | /* quantization style*/ |
11740 | 0 | l_tccp_info->qntsty = l_tccp->qntsty; |
11741 | 0 | l_tccp_info->numgbits = l_tccp->numgbits; |
11742 | |
|
11743 | 0 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11744 | 0 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
11745 | 0 | if (numbands < OPJ_J2K_MAXBANDS) { |
11746 | 0 | for (bandno = 0; bandno < numbands; bandno++) { |
11747 | 0 | l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32) |
11748 | 0 | l_tccp->stepsizes[bandno].mant; |
11749 | 0 | l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32) |
11750 | 0 | l_tccp->stepsizes[bandno].expn; |
11751 | 0 | } |
11752 | 0 | } |
11753 | | |
11754 | | /* RGN value*/ |
11755 | 0 | l_tccp_info->roishift = l_tccp->roishift; |
11756 | 0 | } |
11757 | |
|
11758 | 0 | return cstr_info; |
11759 | 0 | } |
11760 | | |
11761 | | opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) |
11762 | 0 | { |
11763 | 0 | opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) |
11764 | 0 | opj_calloc(1, sizeof(opj_codestream_index_t)); |
11765 | 0 | if (!l_cstr_index) { |
11766 | 0 | return NULL; |
11767 | 0 | } |
11768 | | |
11769 | 0 | l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; |
11770 | 0 | l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; |
11771 | 0 | l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; |
11772 | |
|
11773 | 0 | l_cstr_index->marknum = p_j2k->cstr_index->marknum; |
11774 | 0 | l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum * |
11775 | 0 | sizeof(opj_marker_info_t)); |
11776 | 0 | if (!l_cstr_index->marker) { |
11777 | 0 | opj_free(l_cstr_index); |
11778 | 0 | return NULL; |
11779 | 0 | } |
11780 | | |
11781 | 0 | if (p_j2k->cstr_index->marker) { |
11782 | 0 | memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, |
11783 | 0 | l_cstr_index->marknum * sizeof(opj_marker_info_t)); |
11784 | 0 | } else { |
11785 | 0 | opj_free(l_cstr_index->marker); |
11786 | 0 | l_cstr_index->marker = NULL; |
11787 | 0 | } |
11788 | |
|
11789 | 0 | l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; |
11790 | 0 | l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
11791 | 0 | l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
11792 | 0 | if (!l_cstr_index->tile_index) { |
11793 | 0 | opj_free(l_cstr_index->marker); |
11794 | 0 | opj_free(l_cstr_index); |
11795 | 0 | return NULL; |
11796 | 0 | } |
11797 | | |
11798 | 0 | if (!p_j2k->cstr_index->tile_index) { |
11799 | 0 | opj_free(l_cstr_index->tile_index); |
11800 | 0 | l_cstr_index->tile_index = NULL; |
11801 | 0 | } else { |
11802 | 0 | OPJ_UINT32 it_tile = 0; |
11803 | 0 | for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) { |
11804 | | |
11805 | | /* Tile Marker*/ |
11806 | 0 | l_cstr_index->tile_index[it_tile].marknum = |
11807 | 0 | p_j2k->cstr_index->tile_index[it_tile].marknum; |
11808 | |
|
11809 | 0 | l_cstr_index->tile_index[it_tile].marker = |
11810 | 0 | (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum * |
11811 | 0 | sizeof(opj_marker_info_t)); |
11812 | |
|
11813 | 0 | if (!l_cstr_index->tile_index[it_tile].marker) { |
11814 | 0 | OPJ_UINT32 it_tile_free; |
11815 | |
|
11816 | 0 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
11817 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
11818 | 0 | } |
11819 | |
|
11820 | 0 | opj_free(l_cstr_index->tile_index); |
11821 | 0 | opj_free(l_cstr_index->marker); |
11822 | 0 | opj_free(l_cstr_index); |
11823 | 0 | return NULL; |
11824 | 0 | } |
11825 | | |
11826 | 0 | if (p_j2k->cstr_index->tile_index[it_tile].marker) |
11827 | 0 | memcpy(l_cstr_index->tile_index[it_tile].marker, |
11828 | 0 | p_j2k->cstr_index->tile_index[it_tile].marker, |
11829 | 0 | l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t)); |
11830 | 0 | else { |
11831 | 0 | opj_free(l_cstr_index->tile_index[it_tile].marker); |
11832 | 0 | l_cstr_index->tile_index[it_tile].marker = NULL; |
11833 | 0 | } |
11834 | | |
11835 | | /* Tile part index*/ |
11836 | 0 | l_cstr_index->tile_index[it_tile].nb_tps = |
11837 | 0 | p_j2k->cstr_index->tile_index[it_tile].nb_tps; |
11838 | |
|
11839 | 0 | l_cstr_index->tile_index[it_tile].tp_index = |
11840 | 0 | (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof( |
11841 | 0 | opj_tp_index_t)); |
11842 | |
|
11843 | 0 | if (!l_cstr_index->tile_index[it_tile].tp_index) { |
11844 | 0 | OPJ_UINT32 it_tile_free; |
11845 | |
|
11846 | 0 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
11847 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
11848 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); |
11849 | 0 | } |
11850 | |
|
11851 | 0 | opj_free(l_cstr_index->tile_index); |
11852 | 0 | opj_free(l_cstr_index->marker); |
11853 | 0 | opj_free(l_cstr_index); |
11854 | 0 | return NULL; |
11855 | 0 | } |
11856 | | |
11857 | 0 | if (p_j2k->cstr_index->tile_index[it_tile].tp_index) { |
11858 | 0 | memcpy(l_cstr_index->tile_index[it_tile].tp_index, |
11859 | 0 | p_j2k->cstr_index->tile_index[it_tile].tp_index, |
11860 | 0 | l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t)); |
11861 | 0 | } else { |
11862 | 0 | opj_free(l_cstr_index->tile_index[it_tile].tp_index); |
11863 | 0 | l_cstr_index->tile_index[it_tile].tp_index = NULL; |
11864 | 0 | } |
11865 | | |
11866 | | /* Packet index (NOT USED)*/ |
11867 | 0 | l_cstr_index->tile_index[it_tile].nb_packet = 0; |
11868 | 0 | l_cstr_index->tile_index[it_tile].packet_index = NULL; |
11869 | |
|
11870 | 0 | } |
11871 | 0 | } |
11872 | | |
11873 | 0 | return l_cstr_index; |
11874 | 0 | } |
11875 | | |
11876 | | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) |
11877 | 8.02k | { |
11878 | 8.02k | OPJ_UINT32 it_tile = 0; |
11879 | | |
11880 | 8.02k | p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
11881 | 8.02k | p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
11882 | 8.02k | p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
11883 | 8.02k | if (!p_j2k->cstr_index->tile_index) { |
11884 | 0 | return OPJ_FALSE; |
11885 | 0 | } |
11886 | | |
11887 | 359k | for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) { |
11888 | 351k | p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; |
11889 | 351k | p_j2k->cstr_index->tile_index[it_tile].marknum = 0; |
11890 | 351k | p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) |
11891 | 351k | opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, |
11892 | 351k | sizeof(opj_marker_info_t)); |
11893 | 351k | if (!p_j2k->cstr_index->tile_index[it_tile].marker) { |
11894 | 0 | return OPJ_FALSE; |
11895 | 0 | } |
11896 | 351k | } |
11897 | | |
11898 | 8.02k | return OPJ_TRUE; |
11899 | 8.02k | } |
11900 | | |
11901 | | static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k, |
11902 | | opj_event_mgr_t * p_manager) |
11903 | 6.10k | { |
11904 | 6.10k | OPJ_UINT32 compno; |
11905 | 6.10k | OPJ_BOOL decoded_all_used_components = OPJ_TRUE; |
11906 | | |
11907 | 6.10k | if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) { |
11908 | 0 | for (compno = 0; |
11909 | 0 | compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { |
11910 | 0 | OPJ_UINT32 dec_compno = |
11911 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; |
11912 | 0 | if (p_j2k->m_output_image->comps[dec_compno].data == NULL) { |
11913 | 0 | opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", |
11914 | 0 | dec_compno); |
11915 | 0 | decoded_all_used_components = OPJ_FALSE; |
11916 | 0 | } |
11917 | 0 | } |
11918 | 6.10k | } else { |
11919 | 12.4k | for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) { |
11920 | 6.37k | if (p_j2k->m_output_image->comps[compno].data == NULL) { |
11921 | 63 | opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", |
11922 | 63 | compno); |
11923 | 63 | decoded_all_used_components = OPJ_FALSE; |
11924 | 63 | } |
11925 | 6.37k | } |
11926 | 6.10k | } |
11927 | | |
11928 | 6.10k | if (decoded_all_used_components == OPJ_FALSE) { |
11929 | 38 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n"); |
11930 | 38 | return OPJ_FALSE; |
11931 | 38 | } |
11932 | | |
11933 | 6.06k | return OPJ_TRUE; |
11934 | 6.10k | } |
11935 | | |
11936 | | static int CompareOffT(const void* a, const void* b) |
11937 | 0 | { |
11938 | 0 | const OPJ_OFF_T offA = *(const OPJ_OFF_T*)a; |
11939 | 0 | const OPJ_OFF_T offB = *(const OPJ_OFF_T*)b; |
11940 | 0 | if (offA < offB) { |
11941 | 0 | return -1; |
11942 | 0 | } |
11943 | 0 | if (offA == offB) { |
11944 | 0 | return 0; |
11945 | 0 | } |
11946 | 0 | return 1; |
11947 | 0 | } |
11948 | | |
11949 | | static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
11950 | | opj_stream_private_t *p_stream, |
11951 | | opj_event_mgr_t * p_manager) |
11952 | 7.14k | { |
11953 | 7.14k | OPJ_BOOL l_go_on = OPJ_TRUE; |
11954 | 7.14k | OPJ_UINT32 l_current_tile_no; |
11955 | 7.14k | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
11956 | 7.14k | OPJ_UINT32 l_nb_comps; |
11957 | 7.14k | OPJ_UINT32 nr_tiles = 0; |
11958 | 7.14k | OPJ_OFF_T end_pos = 0; |
11959 | | |
11960 | | /* Particular case for whole single tile decoding */ |
11961 | | /* We can avoid allocating intermediate tile buffers */ |
11962 | 7.14k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
11963 | 7.14k | p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 && |
11964 | 7.14k | p_j2k->m_output_image->x0 == 0 && |
11965 | 7.14k | p_j2k->m_output_image->y0 == 0 && |
11966 | 7.14k | p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx && |
11967 | 7.14k | p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) { |
11968 | 43 | OPJ_UINT32 i; |
11969 | 43 | if (! opj_j2k_read_tile_header(p_j2k, |
11970 | 43 | &l_current_tile_no, |
11971 | 43 | NULL, |
11972 | 43 | &l_tile_x0, &l_tile_y0, |
11973 | 43 | &l_tile_x1, &l_tile_y1, |
11974 | 43 | &l_nb_comps, |
11975 | 43 | &l_go_on, |
11976 | 43 | p_stream, |
11977 | 43 | p_manager)) { |
11978 | 12 | return OPJ_FALSE; |
11979 | 12 | } |
11980 | | |
11981 | 31 | if (!l_go_on || |
11982 | 31 | ! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
11983 | 31 | p_stream, p_manager)) { |
11984 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n"); |
11985 | 3 | return OPJ_FALSE; |
11986 | 3 | } |
11987 | | |
11988 | | /* Transfer TCD data to output image data */ |
11989 | 72 | for (i = 0; i < p_j2k->m_output_image->numcomps; i++) { |
11990 | 44 | opj_image_data_free(p_j2k->m_output_image->comps[i].data); |
11991 | 44 | p_j2k->m_output_image->comps[i].data = |
11992 | 44 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data; |
11993 | 44 | p_j2k->m_output_image->comps[i].resno_decoded = |
11994 | 44 | p_j2k->m_tcd->image->comps[i].resno_decoded; |
11995 | 44 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL; |
11996 | 44 | } |
11997 | | |
11998 | 28 | return OPJ_TRUE; |
11999 | 31 | } |
12000 | | |
12001 | 7.09k | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts = 0; |
12002 | 7.09k | p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts = 0; |
12003 | 7.09k | opj_free(p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset); |
12004 | 7.09k | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset = NULL; |
12005 | | |
12006 | | /* If the area to decode only intersects a subset of tiles, and we have |
12007 | | * valid TLM information, then use it to plan the tilepart offsets to |
12008 | | * seek to. |
12009 | | */ |
12010 | 7.09k | if (!(p_j2k->m_specific_param.m_decoder.m_start_tile_x == 0 && |
12011 | 7.09k | p_j2k->m_specific_param.m_decoder.m_start_tile_y == 0 && |
12012 | 7.09k | p_j2k->m_specific_param.m_decoder.m_end_tile_x == p_j2k->m_cp.tw && |
12013 | 7.09k | p_j2k->m_specific_param.m_decoder.m_end_tile_y == p_j2k->m_cp.th) && |
12014 | 7.09k | !p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid && |
12015 | 7.09k | opj_stream_has_seek(p_stream)) { |
12016 | 0 | OPJ_UINT32 m_num_intersecting_tile_parts = 0; |
12017 | |
|
12018 | 0 | OPJ_UINT32 j; |
12019 | 0 | for (j = 0; j < p_j2k->m_cp.tw * p_j2k->m_cp.th; ++j) { |
12020 | 0 | if (p_j2k->cstr_index->tile_index[j].nb_tps > 0 && |
12021 | 0 | p_j2k->cstr_index->tile_index[j].tp_index[ |
12022 | 0 | p_j2k->cstr_index->tile_index[j].nb_tps - 1].end_pos > end_pos) { |
12023 | 0 | end_pos = p_j2k->cstr_index->tile_index[j].tp_index[ |
12024 | 0 | p_j2k->cstr_index->tile_index[j].nb_tps - 1].end_pos; |
12025 | 0 | } |
12026 | 0 | } |
12027 | |
|
12028 | 0 | for (j = p_j2k->m_specific_param.m_decoder.m_start_tile_y; |
12029 | 0 | j < p_j2k->m_specific_param.m_decoder.m_end_tile_y; ++j) { |
12030 | 0 | OPJ_UINT32 i; |
12031 | 0 | for (i = p_j2k->m_specific_param.m_decoder.m_start_tile_x; |
12032 | 0 | i < p_j2k->m_specific_param.m_decoder.m_end_tile_x; ++i) { |
12033 | 0 | const OPJ_UINT32 tile_number = j * p_j2k->m_cp.tw + i; |
12034 | 0 | m_num_intersecting_tile_parts += |
12035 | 0 | p_j2k->cstr_index->tile_index[tile_number].nb_tps; |
12036 | 0 | } |
12037 | 0 | } |
12038 | |
|
12039 | 0 | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset = |
12040 | 0 | (OPJ_OFF_T*) |
12041 | 0 | opj_malloc(m_num_intersecting_tile_parts * sizeof(OPJ_OFF_T)); |
12042 | 0 | if (m_num_intersecting_tile_parts > 0 && |
12043 | 0 | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset) { |
12044 | 0 | OPJ_UINT32 idx = 0; |
12045 | 0 | for (j = p_j2k->m_specific_param.m_decoder.m_start_tile_y; |
12046 | 0 | j < p_j2k->m_specific_param.m_decoder.m_end_tile_y; ++j) { |
12047 | 0 | OPJ_UINT32 i; |
12048 | 0 | for (i = p_j2k->m_specific_param.m_decoder.m_start_tile_x; |
12049 | 0 | i < p_j2k->m_specific_param.m_decoder.m_end_tile_x; ++i) { |
12050 | 0 | const OPJ_UINT32 tile_number = j * p_j2k->m_cp.tw + i; |
12051 | 0 | OPJ_UINT32 k; |
12052 | 0 | for (k = 0; k < p_j2k->cstr_index->tile_index[tile_number].nb_tps; ++k) { |
12053 | 0 | const OPJ_OFF_T next_tp_sot_pos = |
12054 | 0 | p_j2k->cstr_index->tile_index[tile_number].tp_index[k].start_pos; |
12055 | 0 | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset[idx] = |
12056 | 0 | next_tp_sot_pos; |
12057 | 0 | ++idx; |
12058 | 0 | } |
12059 | 0 | } |
12060 | 0 | } |
12061 | |
|
12062 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts = idx; |
12063 | | |
12064 | | /* Sort by increasing offset */ |
12065 | 0 | qsort(p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset, |
12066 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts, |
12067 | 0 | sizeof(OPJ_OFF_T), |
12068 | 0 | CompareOffT); |
12069 | 0 | } |
12070 | 0 | } |
12071 | | |
12072 | 9.47k | for (;;) { |
12073 | 9.47k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
12074 | 9.47k | p_j2k->m_cp.tcps[0].m_data != NULL) { |
12075 | 0 | l_current_tile_no = 0; |
12076 | 0 | p_j2k->m_current_tile_number = 0; |
12077 | 0 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
12078 | 9.47k | } else { |
12079 | 9.47k | if (! opj_j2k_read_tile_header(p_j2k, |
12080 | 9.47k | &l_current_tile_no, |
12081 | 9.47k | NULL, |
12082 | 9.47k | &l_tile_x0, &l_tile_y0, |
12083 | 9.47k | &l_tile_x1, &l_tile_y1, |
12084 | 9.47k | &l_nb_comps, |
12085 | 9.47k | &l_go_on, |
12086 | 9.47k | p_stream, |
12087 | 9.47k | p_manager)) { |
12088 | 199 | return OPJ_FALSE; |
12089 | 199 | } |
12090 | | |
12091 | 9.27k | if (! l_go_on) { |
12092 | 14 | break; |
12093 | 14 | } |
12094 | 9.27k | } |
12095 | | |
12096 | 9.26k | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
12097 | 9.26k | p_stream, p_manager)) { |
12098 | 759 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", |
12099 | 759 | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
12100 | 759 | return OPJ_FALSE; |
12101 | 759 | } |
12102 | | |
12103 | 8.50k | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", |
12104 | 8.50k | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
12105 | | |
12106 | 8.50k | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
12107 | 8.50k | p_j2k->m_output_image)) { |
12108 | 38 | return OPJ_FALSE; |
12109 | 38 | } |
12110 | | |
12111 | 8.46k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
12112 | 8.46k | !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 && |
12113 | 5.64k | p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 && |
12114 | 5.64k | p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 && |
12115 | 5.64k | p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) { |
12116 | | /* Keep current tcp data */ |
12117 | 8.46k | } else { |
12118 | 8.46k | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
12119 | 8.46k | } |
12120 | | |
12121 | 8.46k | opj_event_msg(p_manager, EVT_INFO, |
12122 | 8.46k | "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
12123 | | |
12124 | 8.46k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
12125 | 8.46k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
12126 | 6.08k | break; |
12127 | 6.08k | } |
12128 | 2.38k | if (++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) { |
12129 | 6 | break; |
12130 | 6 | } |
12131 | 2.37k | if (p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts > 0 && |
12132 | 2.37k | p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts == |
12133 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts) { |
12134 | 0 | opj_stream_seek(p_stream, end_pos + 2, p_manager); |
12135 | 0 | break; |
12136 | 0 | } |
12137 | 2.37k | } |
12138 | | |
12139 | 6.10k | if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { |
12140 | 38 | return OPJ_FALSE; |
12141 | 38 | } |
12142 | | |
12143 | 6.06k | return OPJ_TRUE; |
12144 | 6.10k | } |
12145 | | |
12146 | | /** |
12147 | | * Sets up the procedures to do on decoding data. Developers wanting to extend the library can add their own reading procedures. |
12148 | | */ |
12149 | | static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k, |
12150 | | opj_event_mgr_t * p_manager) |
12151 | 7.14k | { |
12152 | | /* preconditions*/ |
12153 | 7.14k | assert(p_j2k != 00); |
12154 | 7.14k | assert(p_manager != 00); |
12155 | | |
12156 | 7.14k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12157 | 7.14k | (opj_procedure)opj_j2k_decode_tiles, p_manager)) { |
12158 | 0 | return OPJ_FALSE; |
12159 | 0 | } |
12160 | | /* DEVELOPER CORNER, add your custom procedures */ |
12161 | | |
12162 | 7.14k | return OPJ_TRUE; |
12163 | 7.14k | } |
12164 | | |
12165 | | /* |
12166 | | * Read and decode one tile. |
12167 | | */ |
12168 | | static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k, |
12169 | | opj_stream_private_t *p_stream, |
12170 | | opj_event_mgr_t * p_manager) |
12171 | 0 | { |
12172 | 0 | OPJ_BOOL l_go_on = OPJ_TRUE; |
12173 | 0 | OPJ_UINT32 l_current_tile_no; |
12174 | 0 | OPJ_UINT32 l_tile_no_to_dec; |
12175 | 0 | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
12176 | 0 | OPJ_UINT32 l_nb_comps; |
12177 | 0 | OPJ_UINT32 l_nb_tiles; |
12178 | 0 | OPJ_UINT32 i; |
12179 | | |
12180 | | /* Move into the codestream to the first SOT used to decode the desired tile */ |
12181 | 0 | l_tile_no_to_dec = (OPJ_UINT32) |
12182 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; |
12183 | 0 | if (p_j2k->cstr_index->tile_index) |
12184 | 0 | if (p_j2k->cstr_index->tile_index->tp_index) { |
12185 | 0 | if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { |
12186 | | /* the index for this tile has not been built, |
12187 | | * so move to the last SOT read */ |
12188 | 0 | if (!(opj_stream_read_seek(p_stream, |
12189 | 0 | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) { |
12190 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
12191 | 0 | return OPJ_FALSE; |
12192 | 0 | } |
12193 | 0 | } else { |
12194 | 0 | OPJ_OFF_T sot_pos = |
12195 | 0 | p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos; |
12196 | 0 | OPJ_UINT32 l_marker; |
12197 | |
|
12198 | | #if 0 |
12199 | | opj_event_msg(p_manager, EVT_INFO, |
12200 | | "opj_j2k_decode_one_tile(%u): seek to %" PRId64 "\n", |
12201 | | l_tile_no_to_dec, |
12202 | | sot_pos); |
12203 | | #endif |
12204 | 0 | if (!(opj_stream_read_seek(p_stream, |
12205 | 0 | sot_pos, |
12206 | 0 | p_manager))) { |
12207 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
12208 | 0 | return OPJ_FALSE; |
12209 | 0 | } |
12210 | | |
12211 | | /* Try to read 2 bytes (the marker ID) from stream and copy them into the buffer */ |
12212 | 0 | if (opj_stream_read_data(p_stream, |
12213 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
12214 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
12215 | 0 | return OPJ_FALSE; |
12216 | 0 | } |
12217 | | |
12218 | | /* Read 2 bytes from the buffer as the marker ID */ |
12219 | 0 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker, |
12220 | 0 | 2); |
12221 | |
|
12222 | 0 | if (l_marker != J2K_MS_SOT) { |
12223 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Did not get expected SOT marker\n"); |
12224 | 0 | return OPJ_FALSE; |
12225 | 0 | } |
12226 | 0 | } |
12227 | | /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ |
12228 | 0 | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
12229 | 0 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
12230 | 0 | } |
12231 | 0 | } |
12232 | | |
12233 | | /* Reset current tile part number for all tiles, and not only the one */ |
12234 | | /* of interest. */ |
12235 | | /* Not completely sure this is always correct but required for */ |
12236 | | /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */ |
12237 | 0 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
12238 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
12239 | 0 | p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1; |
12240 | 0 | } |
12241 | |
|
12242 | 0 | for (;;) { |
12243 | 0 | if (! opj_j2k_read_tile_header(p_j2k, |
12244 | 0 | &l_current_tile_no, |
12245 | 0 | NULL, |
12246 | 0 | &l_tile_x0, &l_tile_y0, |
12247 | 0 | &l_tile_x1, &l_tile_y1, |
12248 | 0 | &l_nb_comps, |
12249 | 0 | &l_go_on, |
12250 | 0 | p_stream, |
12251 | 0 | p_manager)) { |
12252 | 0 | return OPJ_FALSE; |
12253 | 0 | } |
12254 | | |
12255 | 0 | if (! l_go_on) { |
12256 | 0 | break; |
12257 | 0 | } |
12258 | | |
12259 | 0 | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
12260 | 0 | p_stream, p_manager)) { |
12261 | 0 | return OPJ_FALSE; |
12262 | 0 | } |
12263 | 0 | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", |
12264 | 0 | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
12265 | |
|
12266 | 0 | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
12267 | 0 | p_j2k->m_output_image)) { |
12268 | 0 | return OPJ_FALSE; |
12269 | 0 | } |
12270 | 0 | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
12271 | |
|
12272 | 0 | opj_event_msg(p_manager, EVT_INFO, |
12273 | 0 | "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
12274 | |
|
12275 | 0 | if (l_current_tile_no == l_tile_no_to_dec) { |
12276 | | /* move into the codestream to the first SOT (FIXME or not move?)*/ |
12277 | 0 | if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, |
12278 | 0 | p_manager))) { |
12279 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
12280 | 0 | return OPJ_FALSE; |
12281 | 0 | } |
12282 | 0 | break; |
12283 | 0 | } else { |
12284 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
12285 | 0 | "Tile read, decoded and updated is not the desired one (%d vs %d).\n", |
12286 | 0 | l_current_tile_no + 1, l_tile_no_to_dec + 1); |
12287 | 0 | } |
12288 | |
|
12289 | 0 | } |
12290 | | |
12291 | 0 | if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { |
12292 | 0 | return OPJ_FALSE; |
12293 | 0 | } |
12294 | | |
12295 | 0 | return OPJ_TRUE; |
12296 | 0 | } |
12297 | | |
12298 | | /** |
12299 | | * Sets up the procedures to do on decoding one tile. Developers wanting to extend the library can add their own reading procedures. |
12300 | | */ |
12301 | | static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k, |
12302 | | opj_event_mgr_t * p_manager) |
12303 | 0 | { |
12304 | | /* preconditions*/ |
12305 | 0 | assert(p_j2k != 00); |
12306 | 0 | assert(p_manager != 00); |
12307 | |
|
12308 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12309 | 0 | (opj_procedure)opj_j2k_decode_one_tile, p_manager)) { |
12310 | 0 | return OPJ_FALSE; |
12311 | 0 | } |
12312 | | /* DEVELOPER CORNER, add your custom procedures */ |
12313 | | |
12314 | 0 | return OPJ_TRUE; |
12315 | 0 | } |
12316 | | |
12317 | | static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k, |
12318 | | opj_image_t * p_image) |
12319 | 6.09k | { |
12320 | 6.09k | OPJ_UINT32 compno; |
12321 | | |
12322 | | /* Move data and copy one information from codec to output image*/ |
12323 | 6.09k | if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) { |
12324 | 0 | opj_image_comp_t* newcomps = |
12325 | 0 | (opj_image_comp_t*) opj_malloc( |
12326 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode * |
12327 | 0 | sizeof(opj_image_comp_t)); |
12328 | 0 | if (newcomps == NULL) { |
12329 | 0 | opj_image_destroy(p_j2k->m_private_image); |
12330 | 0 | p_j2k->m_private_image = NULL; |
12331 | 0 | return OPJ_FALSE; |
12332 | 0 | } |
12333 | 0 | for (compno = 0; compno < p_image->numcomps; compno++) { |
12334 | 0 | opj_image_data_free(p_image->comps[compno].data); |
12335 | 0 | p_image->comps[compno].data = NULL; |
12336 | 0 | } |
12337 | 0 | for (compno = 0; |
12338 | 0 | compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { |
12339 | 0 | OPJ_UINT32 src_compno = |
12340 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; |
12341 | 0 | memcpy(&(newcomps[compno]), |
12342 | 0 | &(p_j2k->m_output_image->comps[src_compno]), |
12343 | 0 | sizeof(opj_image_comp_t)); |
12344 | 0 | newcomps[compno].resno_decoded = |
12345 | 0 | p_j2k->m_output_image->comps[src_compno].resno_decoded; |
12346 | 0 | newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data; |
12347 | 0 | p_j2k->m_output_image->comps[src_compno].data = NULL; |
12348 | 0 | } |
12349 | 0 | for (compno = 0; compno < p_image->numcomps; compno++) { |
12350 | 0 | assert(p_j2k->m_output_image->comps[compno].data == NULL); |
12351 | 0 | opj_image_data_free(p_j2k->m_output_image->comps[compno].data); |
12352 | 0 | p_j2k->m_output_image->comps[compno].data = NULL; |
12353 | 0 | } |
12354 | 0 | p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; |
12355 | 0 | opj_free(p_image->comps); |
12356 | 0 | p_image->comps = newcomps; |
12357 | 6.09k | } else { |
12358 | 12.3k | for (compno = 0; compno < p_image->numcomps; compno++) { |
12359 | 6.30k | p_image->comps[compno].resno_decoded = |
12360 | 6.30k | p_j2k->m_output_image->comps[compno].resno_decoded; |
12361 | 6.30k | opj_image_data_free(p_image->comps[compno].data); |
12362 | 6.30k | p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; |
12363 | | #if 0 |
12364 | | char fn[256]; |
12365 | | snprintf(fn, sizeof fn, "/tmp/%d.raw", compno); |
12366 | | FILE *debug = fopen(fn, "wb"); |
12367 | | fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32), |
12368 | | p_image->comps[compno].w * p_image->comps[compno].h, debug); |
12369 | | fclose(debug); |
12370 | | #endif |
12371 | 6.30k | p_j2k->m_output_image->comps[compno].data = NULL; |
12372 | 6.30k | } |
12373 | 6.09k | } |
12374 | 6.09k | return OPJ_TRUE; |
12375 | 6.09k | } |
12376 | | |
12377 | | OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, |
12378 | | opj_stream_private_t * p_stream, |
12379 | | opj_image_t * p_image, |
12380 | | opj_event_mgr_t * p_manager) |
12381 | 7.14k | { |
12382 | 7.14k | if (!p_image) { |
12383 | 0 | return OPJ_FALSE; |
12384 | 0 | } |
12385 | | |
12386 | | /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */ |
12387 | | /* and finally opj_decode_image() without manual setting of comps[].factor */ |
12388 | | /* We could potentially always execute it, if we don't allow people to do */ |
12389 | | /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */ |
12390 | 7.14k | if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 && |
12391 | 7.14k | p_j2k->m_private_image != NULL && |
12392 | 7.14k | p_j2k->m_private_image->numcomps > 0 && |
12393 | 7.14k | p_j2k->m_private_image->comps[0].factor == |
12394 | 0 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce && |
12395 | 7.14k | p_image->numcomps > 0 && |
12396 | 7.14k | p_image->comps[0].factor == 0 && |
12397 | | /* Don't mess with image dimension if the user has allocated it */ |
12398 | 7.14k | p_image->comps[0].data == NULL) { |
12399 | 0 | OPJ_UINT32 it_comp; |
12400 | | |
12401 | | /* Update the comps[].factor member of the output image with the one */ |
12402 | | /* of m_reduce */ |
12403 | 0 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
12404 | 0 | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
12405 | 0 | } |
12406 | 0 | if (!opj_j2k_update_image_dimensions(p_image, p_manager)) { |
12407 | 0 | return OPJ_FALSE; |
12408 | 0 | } |
12409 | 0 | } |
12410 | | |
12411 | 7.14k | if (p_j2k->m_output_image == NULL) { |
12412 | 7.14k | p_j2k->m_output_image = opj_image_create0(); |
12413 | 7.14k | if (!(p_j2k->m_output_image)) { |
12414 | 0 | return OPJ_FALSE; |
12415 | 0 | } |
12416 | 7.14k | } |
12417 | 7.14k | opj_copy_image_header(p_image, p_j2k->m_output_image); |
12418 | | |
12419 | | /* customization of the decoding */ |
12420 | 7.14k | if (!opj_j2k_setup_decoding(p_j2k, p_manager)) { |
12421 | 0 | return OPJ_FALSE; |
12422 | 0 | } |
12423 | | |
12424 | | /* Decode the codestream */ |
12425 | 7.14k | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12426 | 1.04k | opj_image_destroy(p_j2k->m_private_image); |
12427 | 1.04k | p_j2k->m_private_image = NULL; |
12428 | 1.04k | return OPJ_FALSE; |
12429 | 1.04k | } |
12430 | | |
12431 | | /* Move data and copy one information from codec to output image*/ |
12432 | 6.09k | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
12433 | 7.14k | } |
12434 | | |
12435 | | OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k, |
12436 | | opj_stream_private_t *p_stream, |
12437 | | opj_image_t* p_image, |
12438 | | opj_event_mgr_t * p_manager, |
12439 | | OPJ_UINT32 tile_index) |
12440 | 0 | { |
12441 | 0 | OPJ_UINT32 compno; |
12442 | 0 | OPJ_UINT32 l_tile_x, l_tile_y; |
12443 | 0 | opj_image_comp_t* l_img_comp; |
12444 | |
|
12445 | 0 | if (!p_image) { |
12446 | 0 | opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n"); |
12447 | 0 | return OPJ_FALSE; |
12448 | 0 | } |
12449 | | |
12450 | 0 | if (p_image->numcomps < p_j2k->m_private_image->numcomps) { |
12451 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12452 | 0 | "Image has less components than codestream.\n"); |
12453 | 0 | return OPJ_FALSE; |
12454 | 0 | } |
12455 | | |
12456 | 0 | if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) { |
12457 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12458 | 0 | "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, |
12459 | 0 | (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); |
12460 | 0 | return OPJ_FALSE; |
12461 | 0 | } |
12462 | | |
12463 | | /* Compute the dimension of the desired tile*/ |
12464 | 0 | l_tile_x = tile_index % p_j2k->m_cp.tw; |
12465 | 0 | l_tile_y = tile_index / p_j2k->m_cp.tw; |
12466 | |
|
12467 | 0 | p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
12468 | 0 | if (p_image->x0 < p_j2k->m_private_image->x0) { |
12469 | 0 | p_image->x0 = p_j2k->m_private_image->x0; |
12470 | 0 | } |
12471 | 0 | p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
12472 | 0 | if (p_image->x1 > p_j2k->m_private_image->x1) { |
12473 | 0 | p_image->x1 = p_j2k->m_private_image->x1; |
12474 | 0 | } |
12475 | |
|
12476 | 0 | p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
12477 | 0 | if (p_image->y0 < p_j2k->m_private_image->y0) { |
12478 | 0 | p_image->y0 = p_j2k->m_private_image->y0; |
12479 | 0 | } |
12480 | 0 | p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
12481 | 0 | if (p_image->y1 > p_j2k->m_private_image->y1) { |
12482 | 0 | p_image->y1 = p_j2k->m_private_image->y1; |
12483 | 0 | } |
12484 | |
|
12485 | 0 | l_img_comp = p_image->comps; |
12486 | 0 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
12487 | 0 | OPJ_INT32 l_comp_x1, l_comp_y1; |
12488 | |
|
12489 | 0 | l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; |
12490 | |
|
12491 | 0 | l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx); |
12492 | 0 | l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy); |
12493 | 0 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
12494 | 0 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
12495 | |
|
12496 | 0 | l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, |
12497 | 0 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, |
12498 | 0 | (OPJ_INT32)l_img_comp->factor)); |
12499 | 0 | l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, |
12500 | 0 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, |
12501 | 0 | (OPJ_INT32)l_img_comp->factor)); |
12502 | |
|
12503 | 0 | l_img_comp++; |
12504 | 0 | } |
12505 | |
|
12506 | 0 | if (p_image->numcomps > p_j2k->m_private_image->numcomps) { |
12507 | | /* Can happen when calling repeatdly opj_get_decoded_tile() on an |
12508 | | * image with a color palette, where color palette expansion is done |
12509 | | * later in jp2.c */ |
12510 | 0 | for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps; |
12511 | 0 | ++compno) { |
12512 | 0 | opj_image_data_free(p_image->comps[compno].data); |
12513 | 0 | p_image->comps[compno].data = NULL; |
12514 | 0 | } |
12515 | 0 | p_image->numcomps = p_j2k->m_private_image->numcomps; |
12516 | 0 | } |
12517 | | |
12518 | | /* Destroy the previous output image*/ |
12519 | 0 | if (p_j2k->m_output_image) { |
12520 | 0 | opj_image_destroy(p_j2k->m_output_image); |
12521 | 0 | } |
12522 | | |
12523 | | /* Create the output image from the information previously computed*/ |
12524 | 0 | p_j2k->m_output_image = opj_image_create0(); |
12525 | 0 | if (!(p_j2k->m_output_image)) { |
12526 | 0 | return OPJ_FALSE; |
12527 | 0 | } |
12528 | 0 | opj_copy_image_header(p_image, p_j2k->m_output_image); |
12529 | |
|
12530 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index; |
12531 | | |
12532 | | /* customization of the decoding */ |
12533 | 0 | if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) { |
12534 | 0 | return OPJ_FALSE; |
12535 | 0 | } |
12536 | | |
12537 | | /* Decode the codestream */ |
12538 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12539 | 0 | opj_image_destroy(p_j2k->m_private_image); |
12540 | 0 | p_j2k->m_private_image = NULL; |
12541 | 0 | return OPJ_FALSE; |
12542 | 0 | } |
12543 | | |
12544 | | /* Move data and copy one information from codec to output image*/ |
12545 | 0 | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
12546 | 0 | } |
12547 | | |
12548 | | OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, |
12549 | | OPJ_UINT32 res_factor, |
12550 | | opj_event_mgr_t * p_manager) |
12551 | 0 | { |
12552 | 0 | OPJ_UINT32 it_comp; |
12553 | |
|
12554 | 0 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; |
12555 | |
|
12556 | 0 | if (p_j2k->m_private_image) { |
12557 | 0 | if (p_j2k->m_private_image->comps) { |
12558 | 0 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { |
12559 | 0 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { |
12560 | 0 | for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { |
12561 | 0 | OPJ_UINT32 max_res = |
12562 | 0 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; |
12563 | 0 | if (res_factor >= max_res) { |
12564 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12565 | 0 | "Resolution factor is greater than the maximum resolution in the component.\n"); |
12566 | 0 | return OPJ_FALSE; |
12567 | 0 | } |
12568 | 0 | p_j2k->m_private_image->comps[it_comp].factor = res_factor; |
12569 | 0 | } |
12570 | 0 | return OPJ_TRUE; |
12571 | 0 | } |
12572 | 0 | } |
12573 | 0 | } |
12574 | 0 | } |
12575 | | |
12576 | 0 | return OPJ_FALSE; |
12577 | 0 | } |
12578 | | |
12579 | | /* ----------------------------------------------------------------------- */ |
12580 | | |
12581 | | OPJ_BOOL opj_j2k_encoder_set_extra_options( |
12582 | | opj_j2k_t *p_j2k, |
12583 | | const char* const* p_options, |
12584 | | opj_event_mgr_t * p_manager) |
12585 | 0 | { |
12586 | 0 | const char* const* p_option_iter; |
12587 | |
|
12588 | 0 | if (p_options == NULL) { |
12589 | 0 | return OPJ_TRUE; |
12590 | 0 | } |
12591 | | |
12592 | 0 | for (p_option_iter = p_options; *p_option_iter != NULL; ++p_option_iter) { |
12593 | 0 | if (strncmp(*p_option_iter, "PLT=", 4) == 0) { |
12594 | 0 | if (strcmp(*p_option_iter, "PLT=YES") == 0) { |
12595 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_TRUE; |
12596 | 0 | } else if (strcmp(*p_option_iter, "PLT=NO") == 0) { |
12597 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_FALSE; |
12598 | 0 | } else { |
12599 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12600 | 0 | "Invalid value for option: %s.\n", *p_option_iter); |
12601 | 0 | return OPJ_FALSE; |
12602 | 0 | } |
12603 | 0 | } else if (strncmp(*p_option_iter, "TLM=", 4) == 0) { |
12604 | 0 | if (strcmp(*p_option_iter, "TLM=YES") == 0) { |
12605 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE; |
12606 | 0 | } else if (strcmp(*p_option_iter, "TLM=NO") == 0) { |
12607 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_FALSE; |
12608 | 0 | } else { |
12609 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12610 | 0 | "Invalid value for option: %s.\n", *p_option_iter); |
12611 | 0 | return OPJ_FALSE; |
12612 | 0 | } |
12613 | 0 | } else if (strncmp(*p_option_iter, "GUARD_BITS=", strlen("GUARD_BITS=")) == 0) { |
12614 | 0 | OPJ_UINT32 tileno; |
12615 | 0 | opj_cp_t *cp = cp = &(p_j2k->m_cp); |
12616 | |
|
12617 | 0 | int numgbits = atoi(*p_option_iter + strlen("GUARD_BITS=")); |
12618 | 0 | if (numgbits < 0 || numgbits > 7) { |
12619 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12620 | 0 | "Invalid value for option: %s. Should be in [0,7]\n", *p_option_iter); |
12621 | 0 | return OPJ_FALSE; |
12622 | 0 | } |
12623 | | |
12624 | 0 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
12625 | 0 | OPJ_UINT32 i; |
12626 | 0 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
12627 | 0 | for (i = 0; i < p_j2k->m_specific_param.m_encoder.m_nb_comps; i++) { |
12628 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
12629 | 0 | tccp->numgbits = (OPJ_UINT32)numgbits; |
12630 | 0 | } |
12631 | 0 | } |
12632 | 0 | } else { |
12633 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12634 | 0 | "Invalid option: %s.\n", *p_option_iter); |
12635 | 0 | return OPJ_FALSE; |
12636 | 0 | } |
12637 | 0 | } |
12638 | | |
12639 | 0 | return OPJ_TRUE; |
12640 | 0 | } |
12641 | | |
12642 | | /* ----------------------------------------------------------------------- */ |
12643 | | |
12644 | | OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, |
12645 | | opj_stream_private_t *p_stream, |
12646 | | opj_event_mgr_t * p_manager) |
12647 | 0 | { |
12648 | 0 | OPJ_UINT32 i, j; |
12649 | 0 | OPJ_UINT32 l_nb_tiles; |
12650 | 0 | OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size; |
12651 | 0 | OPJ_BYTE * l_current_data = 00; |
12652 | 0 | OPJ_BOOL l_reuse_data = OPJ_FALSE; |
12653 | 0 | opj_tcd_t* p_tcd = 00; |
12654 | | |
12655 | | /* preconditions */ |
12656 | 0 | assert(p_j2k != 00); |
12657 | 0 | assert(p_stream != 00); |
12658 | 0 | assert(p_manager != 00); |
12659 | |
|
12660 | 0 | p_tcd = p_j2k->m_tcd; |
12661 | |
|
12662 | 0 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
12663 | 0 | if (l_nb_tiles == 1) { |
12664 | 0 | l_reuse_data = OPJ_TRUE; |
12665 | 0 | #ifdef __SSE__ |
12666 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
12667 | 0 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
12668 | 0 | if (((size_t)l_img_comp->data & 0xFU) != |
12669 | 0 | 0U) { /* tile data shall be aligned on 16 bytes */ |
12670 | 0 | l_reuse_data = OPJ_FALSE; |
12671 | 0 | } |
12672 | 0 | } |
12673 | 0 | #endif |
12674 | 0 | } |
12675 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
12676 | 0 | if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) { |
12677 | 0 | if (l_current_data) { |
12678 | 0 | opj_free(l_current_data); |
12679 | 0 | } |
12680 | 0 | return OPJ_FALSE; |
12681 | 0 | } |
12682 | | |
12683 | | /* if we only have one tile, then simply set tile component data equal to image component data */ |
12684 | | /* otherwise, allocate the data */ |
12685 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
12686 | 0 | opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; |
12687 | 0 | if (l_reuse_data) { |
12688 | 0 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
12689 | 0 | l_tilec->data = l_img_comp->data; |
12690 | 0 | l_tilec->ownsData = OPJ_FALSE; |
12691 | 0 | } else { |
12692 | 0 | if (! opj_alloc_tile_component_data(l_tilec)) { |
12693 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); |
12694 | 0 | if (l_current_data) { |
12695 | 0 | opj_free(l_current_data); |
12696 | 0 | } |
12697 | 0 | return OPJ_FALSE; |
12698 | 0 | } |
12699 | 0 | } |
12700 | 0 | } |
12701 | 0 | l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd); |
12702 | 0 | if (!l_reuse_data) { |
12703 | 0 | if (l_current_tile_size > l_max_tile_size) { |
12704 | 0 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, |
12705 | 0 | l_current_tile_size); |
12706 | 0 | if (! l_new_current_data) { |
12707 | 0 | if (l_current_data) { |
12708 | 0 | opj_free(l_current_data); |
12709 | 0 | } |
12710 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); |
12711 | 0 | return OPJ_FALSE; |
12712 | 0 | } |
12713 | 0 | l_current_data = l_new_current_data; |
12714 | 0 | l_max_tile_size = l_current_tile_size; |
12715 | 0 | } |
12716 | 0 | if (l_current_data == NULL) { |
12717 | | /* Should not happen in practice, but will avoid Coverity to */ |
12718 | | /* complain about a null pointer dereference */ |
12719 | 0 | assert(0); |
12720 | 0 | return OPJ_FALSE; |
12721 | 0 | } |
12722 | | |
12723 | | /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */ |
12724 | | /* 32 bit components @ 8 bit precision get converted to 8 bit */ |
12725 | | /* 32 bit components @ 16 bit precision get converted to 16 bit */ |
12726 | 0 | opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data); |
12727 | | |
12728 | | /* now copy this data into the tile component */ |
12729 | 0 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data, |
12730 | 0 | l_current_tile_size)) { |
12731 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12732 | 0 | "Size mismatch between tile data and sent data."); |
12733 | 0 | opj_free(l_current_data); |
12734 | 0 | return OPJ_FALSE; |
12735 | 0 | } |
12736 | 0 | } |
12737 | | |
12738 | 0 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
12739 | 0 | if (l_current_data) { |
12740 | 0 | opj_free(l_current_data); |
12741 | 0 | } |
12742 | 0 | return OPJ_FALSE; |
12743 | 0 | } |
12744 | 0 | } |
12745 | | |
12746 | 0 | if (l_current_data) { |
12747 | 0 | opj_free(l_current_data); |
12748 | 0 | } |
12749 | 0 | return OPJ_TRUE; |
12750 | 0 | } |
12751 | | |
12752 | | OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, |
12753 | | opj_stream_private_t *p_stream, |
12754 | | opj_event_mgr_t * p_manager) |
12755 | 0 | { |
12756 | | /* customization of the encoding */ |
12757 | 0 | if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) { |
12758 | 0 | return OPJ_FALSE; |
12759 | 0 | } |
12760 | | |
12761 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12762 | 0 | return OPJ_FALSE; |
12763 | 0 | } |
12764 | | |
12765 | 0 | return OPJ_TRUE; |
12766 | 0 | } |
12767 | | |
12768 | | OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, |
12769 | | opj_stream_private_t *p_stream, |
12770 | | opj_image_t * p_image, |
12771 | | opj_event_mgr_t * p_manager) |
12772 | 0 | { |
12773 | | /* preconditions */ |
12774 | 0 | assert(p_j2k != 00); |
12775 | 0 | assert(p_stream != 00); |
12776 | 0 | assert(p_manager != 00); |
12777 | |
|
12778 | 0 | p_j2k->m_private_image = opj_image_create0(); |
12779 | 0 | if (! p_j2k->m_private_image) { |
12780 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header."); |
12781 | 0 | return OPJ_FALSE; |
12782 | 0 | } |
12783 | 0 | opj_copy_image_header(p_image, p_j2k->m_private_image); |
12784 | | |
12785 | | /* TODO_MSD: Find a better way */ |
12786 | 0 | if (p_image->comps) { |
12787 | 0 | OPJ_UINT32 it_comp; |
12788 | 0 | for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { |
12789 | 0 | if (p_image->comps[it_comp].data) { |
12790 | 0 | p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data; |
12791 | 0 | p_image->comps[it_comp].data = NULL; |
12792 | |
|
12793 | 0 | } |
12794 | 0 | } |
12795 | 0 | } |
12796 | | |
12797 | | /* customization of the validation */ |
12798 | 0 | if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) { |
12799 | 0 | return OPJ_FALSE; |
12800 | 0 | } |
12801 | | |
12802 | | /* validation of the parameters codec */ |
12803 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
12804 | 0 | return OPJ_FALSE; |
12805 | 0 | } |
12806 | | |
12807 | | /* customization of the encoding */ |
12808 | 0 | if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) { |
12809 | 0 | return OPJ_FALSE; |
12810 | 0 | } |
12811 | | |
12812 | | /* write header */ |
12813 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12814 | 0 | return OPJ_FALSE; |
12815 | 0 | } |
12816 | | |
12817 | 0 | return OPJ_TRUE; |
12818 | 0 | } |
12819 | | |
12820 | | static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, |
12821 | | OPJ_UINT32 p_tile_index, |
12822 | | opj_stream_private_t *p_stream, |
12823 | | opj_event_mgr_t * p_manager) |
12824 | 0 | { |
12825 | 0 | (void)p_stream; |
12826 | 0 | if (p_tile_index != p_j2k->m_current_tile_number) { |
12827 | 0 | opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match."); |
12828 | 0 | return OPJ_FALSE; |
12829 | 0 | } |
12830 | | |
12831 | 0 | opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", |
12832 | 0 | p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); |
12833 | |
|
12834 | 0 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; |
12835 | 0 | p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; |
12836 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
12837 | | |
12838 | | /* initialisation before tile encoding */ |
12839 | 0 | if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
12840 | 0 | p_manager)) { |
12841 | 0 | return OPJ_FALSE; |
12842 | 0 | } |
12843 | | |
12844 | 0 | return OPJ_TRUE; |
12845 | 0 | } |
12846 | | |
12847 | | static void opj_get_tile_dimensions(opj_image_t * l_image, |
12848 | | opj_tcd_tilecomp_t * l_tilec, |
12849 | | opj_image_comp_t * l_img_comp, |
12850 | | OPJ_UINT32* l_size_comp, |
12851 | | OPJ_UINT32* l_width, |
12852 | | OPJ_UINT32* l_height, |
12853 | | OPJ_UINT32* l_offset_x, |
12854 | | OPJ_UINT32* l_offset_y, |
12855 | | OPJ_UINT32* l_image_width, |
12856 | | OPJ_UINT32* l_stride, |
12857 | | OPJ_UINT32* l_tile_offset) |
12858 | 0 | { |
12859 | 0 | OPJ_UINT32 l_remaining; |
12860 | 0 | *l_size_comp = l_img_comp->prec >> 3; /* (/8) */ |
12861 | 0 | l_remaining = l_img_comp->prec & 7; /* (%8) */ |
12862 | 0 | if (l_remaining) { |
12863 | 0 | *l_size_comp += 1; |
12864 | 0 | } |
12865 | |
|
12866 | 0 | if (*l_size_comp == 3) { |
12867 | 0 | *l_size_comp = 4; |
12868 | 0 | } |
12869 | |
|
12870 | 0 | *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0); |
12871 | 0 | *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0); |
12872 | 0 | *l_offset_x = opj_uint_ceildiv(l_image->x0, l_img_comp->dx); |
12873 | 0 | *l_offset_y = opj_uint_ceildiv(l_image->y0, l_img_comp->dy); |
12874 | 0 | *l_image_width = opj_uint_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx); |
12875 | 0 | *l_stride = *l_image_width - *l_width; |
12876 | 0 | *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + (( |
12877 | 0 | OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width; |
12878 | 0 | } |
12879 | | |
12880 | | static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data) |
12881 | 0 | { |
12882 | 0 | OPJ_UINT32 i, j, k = 0; |
12883 | |
|
12884 | 0 | for (i = 0; i < p_tcd->image->numcomps; ++i) { |
12885 | 0 | opj_image_t * l_image = p_tcd->image; |
12886 | 0 | OPJ_INT32 * l_src_ptr; |
12887 | 0 | opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i; |
12888 | 0 | opj_image_comp_t * l_img_comp = l_image->comps + i; |
12889 | 0 | OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y, |
12890 | 0 | l_image_width, l_stride, l_tile_offset; |
12891 | |
|
12892 | 0 | opj_get_tile_dimensions(l_image, |
12893 | 0 | l_tilec, |
12894 | 0 | l_img_comp, |
12895 | 0 | &l_size_comp, |
12896 | 0 | &l_width, |
12897 | 0 | &l_height, |
12898 | 0 | &l_offset_x, |
12899 | 0 | &l_offset_y, |
12900 | 0 | &l_image_width, |
12901 | 0 | &l_stride, |
12902 | 0 | &l_tile_offset); |
12903 | |
|
12904 | 0 | l_src_ptr = l_img_comp->data + l_tile_offset; |
12905 | |
|
12906 | 0 | switch (l_size_comp) { |
12907 | 0 | case 1: { |
12908 | 0 | OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; |
12909 | 0 | if (l_img_comp->sgnd) { |
12910 | 0 | for (j = 0; j < l_height; ++j) { |
12911 | 0 | for (k = 0; k < l_width; ++k) { |
12912 | 0 | *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr); |
12913 | 0 | ++l_dest_ptr; |
12914 | 0 | ++l_src_ptr; |
12915 | 0 | } |
12916 | 0 | l_src_ptr += l_stride; |
12917 | 0 | } |
12918 | 0 | } else { |
12919 | 0 | for (j = 0; j < l_height; ++j) { |
12920 | 0 | for (k = 0; k < l_width; ++k) { |
12921 | 0 | *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff); |
12922 | 0 | ++l_dest_ptr; |
12923 | 0 | ++l_src_ptr; |
12924 | 0 | } |
12925 | 0 | l_src_ptr += l_stride; |
12926 | 0 | } |
12927 | 0 | } |
12928 | |
|
12929 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12930 | 0 | } |
12931 | 0 | break; |
12932 | 0 | case 2: { |
12933 | 0 | OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data; |
12934 | 0 | if (l_img_comp->sgnd) { |
12935 | 0 | for (j = 0; j < l_height; ++j) { |
12936 | 0 | for (k = 0; k < l_width; ++k) { |
12937 | 0 | *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++)); |
12938 | 0 | } |
12939 | 0 | l_src_ptr += l_stride; |
12940 | 0 | } |
12941 | 0 | } else { |
12942 | 0 | for (j = 0; j < l_height; ++j) { |
12943 | 0 | for (k = 0; k < l_width; ++k) { |
12944 | 0 | *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff); |
12945 | 0 | } |
12946 | 0 | l_src_ptr += l_stride; |
12947 | 0 | } |
12948 | 0 | } |
12949 | |
|
12950 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12951 | 0 | } |
12952 | 0 | break; |
12953 | 0 | case 4: { |
12954 | 0 | OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data; |
12955 | 0 | for (j = 0; j < l_height; ++j) { |
12956 | 0 | for (k = 0; k < l_width; ++k) { |
12957 | 0 | *(l_dest_ptr++) = *(l_src_ptr++); |
12958 | 0 | } |
12959 | 0 | l_src_ptr += l_stride; |
12960 | 0 | } |
12961 | |
|
12962 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12963 | 0 | } |
12964 | 0 | break; |
12965 | 0 | } |
12966 | 0 | } |
12967 | 0 | } |
12968 | | |
12969 | | static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, |
12970 | | opj_stream_private_t *p_stream, |
12971 | | opj_event_mgr_t * p_manager) |
12972 | 0 | { |
12973 | 0 | OPJ_UINT32 l_nb_bytes_written; |
12974 | 0 | OPJ_BYTE * l_current_data = 00; |
12975 | 0 | OPJ_UINT32 l_tile_size = 0; |
12976 | 0 | OPJ_UINT32 l_available_data; |
12977 | | |
12978 | | /* preconditions */ |
12979 | 0 | assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
12980 | |
|
12981 | 0 | l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; |
12982 | 0 | l_available_data = l_tile_size; |
12983 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; |
12984 | |
|
12985 | 0 | l_nb_bytes_written = 0; |
12986 | 0 | if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written, |
12987 | 0 | l_available_data, p_stream, p_manager)) { |
12988 | 0 | return OPJ_FALSE; |
12989 | 0 | } |
12990 | 0 | l_current_data += l_nb_bytes_written; |
12991 | 0 | l_available_data -= l_nb_bytes_written; |
12992 | |
|
12993 | 0 | l_nb_bytes_written = 0; |
12994 | 0 | if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written, |
12995 | 0 | l_available_data, p_stream, p_manager)) { |
12996 | 0 | return OPJ_FALSE; |
12997 | 0 | } |
12998 | | |
12999 | 0 | l_available_data -= l_nb_bytes_written; |
13000 | 0 | l_nb_bytes_written = l_tile_size - l_available_data; |
13001 | |
|
13002 | 0 | if (opj_stream_write_data(p_stream, |
13003 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, |
13004 | 0 | l_nb_bytes_written, p_manager) != l_nb_bytes_written) { |
13005 | 0 | return OPJ_FALSE; |
13006 | 0 | } |
13007 | | |
13008 | 0 | ++p_j2k->m_current_tile_number; |
13009 | |
|
13010 | 0 | return OPJ_TRUE; |
13011 | 0 | } |
13012 | | |
13013 | | static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, |
13014 | | opj_event_mgr_t * p_manager) |
13015 | 0 | { |
13016 | | /* preconditions */ |
13017 | 0 | assert(p_j2k != 00); |
13018 | 0 | assert(p_manager != 00); |
13019 | | |
13020 | | /* DEVELOPER CORNER, insert your custom procedures */ |
13021 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13022 | 0 | (opj_procedure)opj_j2k_write_eoc, p_manager)) { |
13023 | 0 | return OPJ_FALSE; |
13024 | 0 | } |
13025 | | |
13026 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13027 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13028 | 0 | (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) { |
13029 | 0 | return OPJ_FALSE; |
13030 | 0 | } |
13031 | 0 | } |
13032 | | |
13033 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13034 | 0 | (opj_procedure)opj_j2k_write_epc, p_manager)) { |
13035 | 0 | return OPJ_FALSE; |
13036 | 0 | } |
13037 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13038 | 0 | (opj_procedure)opj_j2k_end_encoding, p_manager)) { |
13039 | 0 | return OPJ_FALSE; |
13040 | 0 | } |
13041 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13042 | 0 | (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) { |
13043 | 0 | return OPJ_FALSE; |
13044 | 0 | } |
13045 | 0 | return OPJ_TRUE; |
13046 | 0 | } |
13047 | | |
13048 | | static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, |
13049 | | opj_event_mgr_t * p_manager) |
13050 | 0 | { |
13051 | | /* preconditions */ |
13052 | 0 | assert(p_j2k != 00); |
13053 | 0 | assert(p_manager != 00); |
13054 | |
|
13055 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
13056 | 0 | (opj_procedure)opj_j2k_build_encoder, p_manager)) { |
13057 | 0 | return OPJ_FALSE; |
13058 | 0 | } |
13059 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
13060 | 0 | (opj_procedure)opj_j2k_encoding_validation, p_manager)) { |
13061 | 0 | return OPJ_FALSE; |
13062 | 0 | } |
13063 | | |
13064 | | /* DEVELOPER CORNER, add your custom validation procedure */ |
13065 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
13066 | 0 | (opj_procedure)opj_j2k_mct_validation, p_manager)) { |
13067 | 0 | return OPJ_FALSE; |
13068 | 0 | } |
13069 | | |
13070 | 0 | return OPJ_TRUE; |
13071 | 0 | } |
13072 | | |
13073 | | static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, |
13074 | | opj_event_mgr_t * p_manager) |
13075 | 0 | { |
13076 | | /* preconditions */ |
13077 | 0 | assert(p_j2k != 00); |
13078 | 0 | assert(p_manager != 00); |
13079 | |
|
13080 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13081 | 0 | (opj_procedure)opj_j2k_init_info, p_manager)) { |
13082 | 0 | return OPJ_FALSE; |
13083 | 0 | } |
13084 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13085 | 0 | (opj_procedure)opj_j2k_write_soc, p_manager)) { |
13086 | 0 | return OPJ_FALSE; |
13087 | 0 | } |
13088 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13089 | 0 | (opj_procedure)opj_j2k_write_siz, p_manager)) { |
13090 | 0 | return OPJ_FALSE; |
13091 | 0 | } |
13092 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13093 | 0 | (opj_procedure)opj_j2k_write_cod, p_manager)) { |
13094 | 0 | return OPJ_FALSE; |
13095 | 0 | } |
13096 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13097 | 0 | (opj_procedure)opj_j2k_write_qcd, p_manager)) { |
13098 | 0 | return OPJ_FALSE; |
13099 | 0 | } |
13100 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13101 | 0 | (opj_procedure)opj_j2k_write_all_coc, p_manager)) { |
13102 | 0 | return OPJ_FALSE; |
13103 | 0 | } |
13104 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13105 | 0 | (opj_procedure)opj_j2k_write_all_qcc, p_manager)) { |
13106 | 0 | return OPJ_FALSE; |
13107 | 0 | } |
13108 | | |
13109 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13110 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13111 | 0 | (opj_procedure)opj_j2k_write_tlm, p_manager)) { |
13112 | 0 | return OPJ_FALSE; |
13113 | 0 | } |
13114 | | |
13115 | 0 | if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) { |
13116 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13117 | 0 | (opj_procedure)opj_j2k_write_poc, p_manager)) { |
13118 | 0 | return OPJ_FALSE; |
13119 | 0 | } |
13120 | 0 | } |
13121 | 0 | } |
13122 | | |
13123 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13124 | 0 | (opj_procedure)opj_j2k_write_regions, p_manager)) { |
13125 | 0 | return OPJ_FALSE; |
13126 | 0 | } |
13127 | | |
13128 | 0 | if (p_j2k->m_cp.comment != 00) { |
13129 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13130 | 0 | (opj_procedure)opj_j2k_write_com, p_manager)) { |
13131 | 0 | return OPJ_FALSE; |
13132 | 0 | } |
13133 | 0 | } |
13134 | | |
13135 | | /* DEVELOPER CORNER, insert your custom procedures */ |
13136 | 0 | if ((p_j2k->m_cp.rsiz & (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) == |
13137 | 0 | (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) { |
13138 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13139 | 0 | (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) { |
13140 | 0 | return OPJ_FALSE; |
13141 | 0 | } |
13142 | 0 | } |
13143 | | /* End of Developer Corner */ |
13144 | | |
13145 | 0 | if (p_j2k->cstr_index) { |
13146 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13147 | 0 | (opj_procedure)opj_j2k_get_end_header, p_manager)) { |
13148 | 0 | return OPJ_FALSE; |
13149 | 0 | } |
13150 | 0 | } |
13151 | | |
13152 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13153 | 0 | (opj_procedure)opj_j2k_create_tcd, p_manager)) { |
13154 | 0 | return OPJ_FALSE; |
13155 | 0 | } |
13156 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13157 | 0 | (opj_procedure)opj_j2k_update_rates, p_manager)) { |
13158 | 0 | return OPJ_FALSE; |
13159 | 0 | } |
13160 | | |
13161 | 0 | return OPJ_TRUE; |
13162 | 0 | } |
13163 | | |
13164 | | static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, |
13165 | | OPJ_BYTE * p_data, |
13166 | | OPJ_UINT32 * p_data_written, |
13167 | | OPJ_UINT32 total_data_size, |
13168 | | opj_stream_private_t *p_stream, |
13169 | | struct opj_event_mgr * p_manager) |
13170 | 0 | { |
13171 | 0 | OPJ_UINT32 l_nb_bytes_written = 0; |
13172 | 0 | OPJ_UINT32 l_current_nb_bytes_written; |
13173 | 0 | OPJ_BYTE * l_begin_data = 00; |
13174 | |
|
13175 | 0 | opj_tcd_t * l_tcd = 00; |
13176 | 0 | opj_cp_t * l_cp = 00; |
13177 | |
|
13178 | 0 | l_tcd = p_j2k->m_tcd; |
13179 | 0 | l_cp = &(p_j2k->m_cp); |
13180 | |
|
13181 | 0 | l_tcd->cur_pino = 0; |
13182 | | |
13183 | | /*Get number of tile parts*/ |
13184 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
13185 | | |
13186 | | /* INDEX >> */ |
13187 | | /* << INDEX */ |
13188 | |
|
13189 | 0 | l_current_nb_bytes_written = 0; |
13190 | 0 | l_begin_data = p_data; |
13191 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, total_data_size, |
13192 | 0 | &l_current_nb_bytes_written, p_stream, |
13193 | 0 | p_manager)) { |
13194 | 0 | return OPJ_FALSE; |
13195 | 0 | } |
13196 | | |
13197 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13198 | 0 | p_data += l_current_nb_bytes_written; |
13199 | 0 | total_data_size -= l_current_nb_bytes_written; |
13200 | |
|
13201 | 0 | if (!OPJ_IS_CINEMA(l_cp->rsiz)) { |
13202 | | #if 0 |
13203 | | for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { |
13204 | | l_current_nb_bytes_written = 0; |
13205 | | opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
13206 | | p_manager); |
13207 | | l_nb_bytes_written += l_current_nb_bytes_written; |
13208 | | p_data += l_current_nb_bytes_written; |
13209 | | total_data_size -= l_current_nb_bytes_written; |
13210 | | |
13211 | | l_current_nb_bytes_written = 0; |
13212 | | opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
13213 | | p_manager); |
13214 | | l_nb_bytes_written += l_current_nb_bytes_written; |
13215 | | p_data += l_current_nb_bytes_written; |
13216 | | total_data_size -= l_current_nb_bytes_written; |
13217 | | } |
13218 | | #endif |
13219 | 0 | if (l_cp->tcps[p_j2k->m_current_tile_number].POC) { |
13220 | 0 | l_current_nb_bytes_written = 0; |
13221 | 0 | opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written, |
13222 | 0 | p_manager); |
13223 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13224 | 0 | p_data += l_current_nb_bytes_written; |
13225 | 0 | total_data_size -= l_current_nb_bytes_written; |
13226 | 0 | } |
13227 | 0 | } |
13228 | |
|
13229 | 0 | l_current_nb_bytes_written = 0; |
13230 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
13231 | 0 | total_data_size, p_stream, p_manager)) { |
13232 | 0 | return OPJ_FALSE; |
13233 | 0 | } |
13234 | | |
13235 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13236 | 0 | * p_data_written = l_nb_bytes_written; |
13237 | | |
13238 | | /* Writing Psot in SOT marker */ |
13239 | 0 | opj_write_bytes(l_begin_data + 6, l_nb_bytes_written, |
13240 | 0 | 4); /* PSOT */ |
13241 | |
|
13242 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13243 | 0 | opj_j2k_update_tlm(p_j2k, l_nb_bytes_written); |
13244 | 0 | } |
13245 | |
|
13246 | 0 | return OPJ_TRUE; |
13247 | 0 | } |
13248 | | |
13249 | | static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, |
13250 | | OPJ_BYTE * p_data, |
13251 | | OPJ_UINT32 * p_data_written, |
13252 | | OPJ_UINT32 total_data_size, |
13253 | | opj_stream_private_t *p_stream, |
13254 | | struct opj_event_mgr * p_manager |
13255 | | ) |
13256 | 0 | { |
13257 | 0 | OPJ_UINT32 tilepartno = 0; |
13258 | 0 | OPJ_UINT32 l_nb_bytes_written = 0; |
13259 | 0 | OPJ_UINT32 l_current_nb_bytes_written; |
13260 | 0 | OPJ_UINT32 l_part_tile_size; |
13261 | 0 | OPJ_UINT32 tot_num_tp; |
13262 | 0 | OPJ_UINT32 pino; |
13263 | |
|
13264 | 0 | OPJ_BYTE * l_begin_data; |
13265 | 0 | opj_tcp_t *l_tcp = 00; |
13266 | 0 | opj_tcd_t * l_tcd = 00; |
13267 | 0 | opj_cp_t * l_cp = 00; |
13268 | |
|
13269 | 0 | l_tcd = p_j2k->m_tcd; |
13270 | 0 | l_cp = &(p_j2k->m_cp); |
13271 | 0 | l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; |
13272 | | |
13273 | | /*Get number of tile parts*/ |
13274 | 0 | tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number); |
13275 | | |
13276 | | /* start writing remaining tile parts */ |
13277 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
13278 | 0 | for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { |
13279 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
13280 | 0 | l_current_nb_bytes_written = 0; |
13281 | 0 | l_part_tile_size = 0; |
13282 | 0 | l_begin_data = p_data; |
13283 | |
|
13284 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, |
13285 | 0 | total_data_size, |
13286 | 0 | &l_current_nb_bytes_written, |
13287 | 0 | p_stream, |
13288 | 0 | p_manager)) { |
13289 | 0 | return OPJ_FALSE; |
13290 | 0 | } |
13291 | | |
13292 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13293 | 0 | p_data += l_current_nb_bytes_written; |
13294 | 0 | total_data_size -= l_current_nb_bytes_written; |
13295 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13296 | |
|
13297 | 0 | l_current_nb_bytes_written = 0; |
13298 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
13299 | 0 | total_data_size, p_stream, p_manager)) { |
13300 | 0 | return OPJ_FALSE; |
13301 | 0 | } |
13302 | | |
13303 | 0 | p_data += l_current_nb_bytes_written; |
13304 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13305 | 0 | total_data_size -= l_current_nb_bytes_written; |
13306 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13307 | | |
13308 | | /* Writing Psot in SOT marker */ |
13309 | 0 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
13310 | 0 | 4); /* PSOT */ |
13311 | |
|
13312 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13313 | 0 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
13314 | 0 | } |
13315 | |
|
13316 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
13317 | 0 | } |
13318 | | |
13319 | 0 | for (pino = 1; pino <= l_tcp->numpocs; ++pino) { |
13320 | 0 | l_tcd->cur_pino = pino; |
13321 | | |
13322 | | /*Get number of tile parts*/ |
13323 | 0 | tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number); |
13324 | 0 | for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { |
13325 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
13326 | 0 | l_current_nb_bytes_written = 0; |
13327 | 0 | l_part_tile_size = 0; |
13328 | 0 | l_begin_data = p_data; |
13329 | |
|
13330 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, |
13331 | 0 | total_data_size, |
13332 | 0 | &l_current_nb_bytes_written, p_stream, |
13333 | 0 | p_manager)) { |
13334 | 0 | return OPJ_FALSE; |
13335 | 0 | } |
13336 | | |
13337 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13338 | 0 | p_data += l_current_nb_bytes_written; |
13339 | 0 | total_data_size -= l_current_nb_bytes_written; |
13340 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13341 | |
|
13342 | 0 | l_current_nb_bytes_written = 0; |
13343 | |
|
13344 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
13345 | 0 | total_data_size, p_stream, p_manager)) { |
13346 | 0 | return OPJ_FALSE; |
13347 | 0 | } |
13348 | | |
13349 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13350 | 0 | p_data += l_current_nb_bytes_written; |
13351 | 0 | total_data_size -= l_current_nb_bytes_written; |
13352 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13353 | | |
13354 | | /* Writing Psot in SOT marker */ |
13355 | 0 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
13356 | 0 | 4); /* PSOT */ |
13357 | |
|
13358 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13359 | 0 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
13360 | 0 | } |
13361 | |
|
13362 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
13363 | 0 | } |
13364 | 0 | } |
13365 | | |
13366 | 0 | *p_data_written = l_nb_bytes_written; |
13367 | |
|
13368 | 0 | return OPJ_TRUE; |
13369 | 0 | } |
13370 | | |
13371 | | static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, |
13372 | | struct opj_stream_private *p_stream, |
13373 | | struct opj_event_mgr * p_manager) |
13374 | 0 | { |
13375 | 0 | OPJ_UINT32 l_tlm_size; |
13376 | 0 | OPJ_OFF_T l_tlm_position, l_current_position; |
13377 | 0 | OPJ_UINT32 size_per_tile_part; |
13378 | | |
13379 | | /* preconditions */ |
13380 | 0 | assert(p_j2k != 00); |
13381 | 0 | assert(p_manager != 00); |
13382 | 0 | assert(p_stream != 00); |
13383 | |
|
13384 | 0 | size_per_tile_part = p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte ? 5 : 6; |
13385 | 0 | l_tlm_size = size_per_tile_part * |
13386 | 0 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts; |
13387 | 0 | l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; |
13388 | 0 | l_current_position = opj_stream_tell(p_stream); |
13389 | |
|
13390 | 0 | if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) { |
13391 | 0 | return OPJ_FALSE; |
13392 | 0 | } |
13393 | | |
13394 | 0 | if (opj_stream_write_data(p_stream, |
13395 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size, |
13396 | 0 | p_manager) != l_tlm_size) { |
13397 | 0 | return OPJ_FALSE; |
13398 | 0 | } |
13399 | | |
13400 | 0 | if (! opj_stream_seek(p_stream, l_current_position, p_manager)) { |
13401 | 0 | return OPJ_FALSE; |
13402 | 0 | } |
13403 | | |
13404 | 0 | return OPJ_TRUE; |
13405 | 0 | } |
13406 | | |
13407 | | static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, |
13408 | | struct opj_stream_private *p_stream, |
13409 | | struct opj_event_mgr * p_manager) |
13410 | 0 | { |
13411 | | /* preconditions */ |
13412 | 0 | assert(p_j2k != 00); |
13413 | 0 | assert(p_manager != 00); |
13414 | 0 | assert(p_stream != 00); |
13415 | |
|
13416 | 0 | OPJ_UNUSED(p_stream); |
13417 | 0 | OPJ_UNUSED(p_manager); |
13418 | |
|
13419 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
13420 | 0 | p_j2k->m_tcd = 00; |
13421 | |
|
13422 | 0 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
13423 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
13424 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; |
13425 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; |
13426 | 0 | } |
13427 | |
|
13428 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
13429 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
13430 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; |
13431 | 0 | } |
13432 | |
|
13433 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; |
13434 | |
|
13435 | 0 | return OPJ_TRUE; |
13436 | 0 | } |
13437 | | |
13438 | | /** |
13439 | | * Destroys the memory associated with the decoding of headers. |
13440 | | */ |
13441 | | static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, |
13442 | | opj_stream_private_t *p_stream, |
13443 | | opj_event_mgr_t * p_manager |
13444 | | ) |
13445 | 0 | { |
13446 | | /* preconditions */ |
13447 | 0 | assert(p_j2k != 00); |
13448 | 0 | assert(p_stream != 00); |
13449 | 0 | assert(p_manager != 00); |
13450 | |
|
13451 | 0 | OPJ_UNUSED(p_stream); |
13452 | 0 | OPJ_UNUSED(p_manager); |
13453 | |
|
13454 | 0 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
13455 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
13456 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; |
13457 | 0 | } |
13458 | |
|
13459 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
13460 | |
|
13461 | 0 | return OPJ_TRUE; |
13462 | 0 | } |
13463 | | |
13464 | | static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, |
13465 | | struct opj_stream_private *p_stream, |
13466 | | struct opj_event_mgr * p_manager) |
13467 | 0 | { |
13468 | 0 | opj_codestream_info_t * l_cstr_info = 00; |
13469 | | |
13470 | | /* preconditions */ |
13471 | 0 | assert(p_j2k != 00); |
13472 | 0 | assert(p_manager != 00); |
13473 | 0 | assert(p_stream != 00); |
13474 | 0 | (void)l_cstr_info; |
13475 | |
|
13476 | 0 | OPJ_UNUSED(p_stream); |
13477 | | |
13478 | | /* TODO mergeV2: check this part which use cstr_info */ |
13479 | | /*l_cstr_info = p_j2k->cstr_info; |
13480 | | |
13481 | | if (l_cstr_info) { |
13482 | | OPJ_UINT32 compno; |
13483 | | 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)); |
13484 | | |
13485 | | l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; |
13486 | | l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; |
13487 | | |
13488 | | l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; |
13489 | | |
13490 | | l_cstr_info->tw = p_j2k->m_cp.tw; |
13491 | | l_cstr_info->th = p_j2k->m_cp.th; |
13492 | | |
13493 | | l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ |
13494 | | /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ |
13495 | | /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ |
13496 | | /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ |
13497 | | |
13498 | | /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; |
13499 | | |
13500 | | l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; |
13501 | | |
13502 | | l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); |
13503 | | |
13504 | | for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { |
13505 | | l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; |
13506 | | } |
13507 | | |
13508 | | l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ |
13509 | | |
13510 | | /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ |
13511 | | |
13512 | | /*l_cstr_info->maxmarknum = 100; |
13513 | | l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); |
13514 | | l_cstr_info->marknum = 0; |
13515 | | }*/ |
13516 | |
|
13517 | 0 | return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp), |
13518 | 0 | &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image, |
13519 | 0 | p_manager); |
13520 | 0 | } |
13521 | | |
13522 | | /** |
13523 | | * Creates a tile-coder encoder. |
13524 | | * |
13525 | | * @param p_stream the stream to write data to. |
13526 | | * @param p_j2k J2K codec. |
13527 | | * @param p_manager the user event manager. |
13528 | | */ |
13529 | | static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, |
13530 | | opj_stream_private_t *p_stream, |
13531 | | opj_event_mgr_t * p_manager |
13532 | | ) |
13533 | 0 | { |
13534 | | /* preconditions */ |
13535 | 0 | assert(p_j2k != 00); |
13536 | 0 | assert(p_manager != 00); |
13537 | 0 | assert(p_stream != 00); |
13538 | |
|
13539 | 0 | OPJ_UNUSED(p_stream); |
13540 | |
|
13541 | 0 | p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); |
13542 | |
|
13543 | 0 | if (! p_j2k->m_tcd) { |
13544 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); |
13545 | 0 | return OPJ_FALSE; |
13546 | 0 | } |
13547 | | |
13548 | 0 | if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp, |
13549 | 0 | p_j2k->m_tp)) { |
13550 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
13551 | 0 | p_j2k->m_tcd = 00; |
13552 | 0 | return OPJ_FALSE; |
13553 | 0 | } |
13554 | | |
13555 | 0 | return OPJ_TRUE; |
13556 | 0 | } |
13557 | | |
13558 | | OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k, |
13559 | | OPJ_UINT32 p_tile_index, |
13560 | | OPJ_BYTE * p_data, |
13561 | | OPJ_UINT32 p_data_size, |
13562 | | opj_stream_private_t *p_stream, |
13563 | | opj_event_mgr_t * p_manager) |
13564 | 0 | { |
13565 | 0 | if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) { |
13566 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13567 | 0 | "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); |
13568 | 0 | return OPJ_FALSE; |
13569 | 0 | } else { |
13570 | 0 | OPJ_UINT32 j; |
13571 | | /* Allocate data */ |
13572 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
13573 | 0 | opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j; |
13574 | |
|
13575 | 0 | if (! opj_alloc_tile_component_data(l_tilec)) { |
13576 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); |
13577 | 0 | return OPJ_FALSE; |
13578 | 0 | } |
13579 | 0 | } |
13580 | | |
13581 | | /* now copy data into the tile component */ |
13582 | 0 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
13583 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13584 | 0 | "Size mismatch between tile data and sent data."); |
13585 | 0 | return OPJ_FALSE; |
13586 | 0 | } |
13587 | 0 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
13588 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13589 | 0 | "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); |
13590 | 0 | return OPJ_FALSE; |
13591 | 0 | } |
13592 | 0 | } |
13593 | | |
13594 | 0 | return OPJ_TRUE; |
13595 | 0 | } |