/src/openjpeg/src/lib/openjp2/j2k.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * The copyright in this software is being made available under the 2-clauses |
3 | | * BSD License, included below. This software may be subject to other third |
4 | | * party and contributor rights, including patent rights, and no such rights |
5 | | * are granted under this license. |
6 | | * |
7 | | * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium |
8 | | * Copyright (c) 2002-2014, Professor Benoit Macq |
9 | | * Copyright (c) 2001-2003, David Janssens |
10 | | * Copyright (c) 2002-2003, Yannick Verschueren |
11 | | * Copyright (c) 2003-2007, Francois-Olivier Devaux |
12 | | * Copyright (c) 2003-2014, Antonin Descampe |
13 | | * Copyright (c) 2005, Herve Drolon, FreeImage Team |
14 | | * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr> |
15 | | * Copyright (c) 2006-2007, Parvatha Elangovan |
16 | | * Copyright (c) 2010-2011, Kaori Hagihara |
17 | | * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France |
18 | | * Copyright (c) 2012, CS Systemes d'Information, France |
19 | | * Copyright (c) 2017, IntoPIX SA <support@intopix.com> |
20 | | * All rights reserved. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the above copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * |
31 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' |
32 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
33 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
34 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
35 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
36 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
37 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
38 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
39 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
40 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
41 | | * POSSIBILITY OF SUCH DAMAGE. |
42 | | */ |
43 | | |
44 | | #include "opj_includes.h" |
45 | | |
46 | | /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ |
47 | | /*@{*/ |
48 | | |
49 | | /** @name Local static functions */ |
50 | | /*@{*/ |
51 | | |
52 | | /** |
53 | | * Sets up the procedures to do on reading header. Developers wanting to extend the library can add their own reading procedures. |
54 | | */ |
55 | | static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, |
56 | | opj_event_mgr_t * p_manager); |
57 | | |
58 | | /** |
59 | | * The read header procedure. |
60 | | */ |
61 | | static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, |
62 | | opj_stream_private_t *p_stream, |
63 | | opj_event_mgr_t * p_manager); |
64 | | |
65 | | /** |
66 | | * The default encoding validation procedure without any extension. |
67 | | * |
68 | | * @param p_j2k the jpeg2000 codec to validate. |
69 | | * @param p_stream the input stream to validate. |
70 | | * @param p_manager the user event manager. |
71 | | * |
72 | | * @return true if the parameters are correct. |
73 | | */ |
74 | | static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, |
75 | | opj_stream_private_t *p_stream, |
76 | | opj_event_mgr_t * p_manager); |
77 | | |
78 | | /** |
79 | | * The default decoding validation procedure without any extension. |
80 | | * |
81 | | * @param p_j2k the jpeg2000 codec to validate. |
82 | | * @param p_stream the input stream to validate. |
83 | | * @param p_manager the user event manager. |
84 | | * |
85 | | * @return true if the parameters are correct. |
86 | | */ |
87 | | static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t * p_j2k, |
88 | | opj_stream_private_t *p_stream, |
89 | | opj_event_mgr_t * p_manager); |
90 | | |
91 | | /** |
92 | | * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters |
93 | | * are valid. Developers wanting to extend the library can add their own validation procedures. |
94 | | */ |
95 | | static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, |
96 | | opj_event_mgr_t * p_manager); |
97 | | |
98 | | /** |
99 | | * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters |
100 | | * are valid. Developers wanting to extend the library can add their own validation procedures. |
101 | | */ |
102 | | static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, |
103 | | opj_event_mgr_t * p_manager); |
104 | | |
105 | | /** |
106 | | * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters |
107 | | * are valid. Developers wanting to extend the library can add their own validation procedures. |
108 | | */ |
109 | | static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, |
110 | | opj_event_mgr_t * p_manager); |
111 | | |
112 | | /** |
113 | | * The mct encoding validation procedure. |
114 | | * |
115 | | * @param p_j2k the jpeg2000 codec to validate. |
116 | | * @param p_stream the input stream to validate. |
117 | | * @param p_manager the user event manager. |
118 | | * |
119 | | * @return true if the parameters are correct. |
120 | | */ |
121 | | static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, |
122 | | opj_stream_private_t *p_stream, |
123 | | opj_event_mgr_t * p_manager); |
124 | | |
125 | | /** |
126 | | * Builds the tcd decoder to use to decode tile. |
127 | | */ |
128 | | static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, |
129 | | opj_stream_private_t *p_stream, |
130 | | opj_event_mgr_t * p_manager); |
131 | | /** |
132 | | * Builds the tcd encoder to use to encode tile. |
133 | | */ |
134 | | static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, |
135 | | opj_stream_private_t *p_stream, |
136 | | opj_event_mgr_t * p_manager); |
137 | | |
138 | | /** |
139 | | * Creates a tile-coder encoder. |
140 | | * |
141 | | * @param p_stream the stream to write data to. |
142 | | * @param p_j2k J2K codec. |
143 | | * @param p_manager the user event manager. |
144 | | */ |
145 | | static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, |
146 | | opj_stream_private_t *p_stream, |
147 | | opj_event_mgr_t * p_manager); |
148 | | |
149 | | /** |
150 | | * Executes the given procedures on the given codec. |
151 | | * |
152 | | * @param p_procedure_list the list of procedures to execute |
153 | | * @param p_j2k the jpeg2000 codec to execute the procedures on. |
154 | | * @param p_stream the stream to execute the procedures on. |
155 | | * @param p_manager the user manager. |
156 | | * |
157 | | * @return true if all the procedures were successfully executed. |
158 | | */ |
159 | | static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, |
160 | | opj_procedure_list_t * p_procedure_list, |
161 | | opj_stream_private_t *p_stream, |
162 | | opj_event_mgr_t * p_manager); |
163 | | |
164 | | /** |
165 | | * Updates the rates of the tcp. |
166 | | * |
167 | | * @param p_stream the stream to write data to. |
168 | | * @param p_j2k J2K codec. |
169 | | * @param p_manager the user event manager. |
170 | | */ |
171 | | static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, |
172 | | opj_stream_private_t *p_stream, |
173 | | opj_event_mgr_t * p_manager); |
174 | | |
175 | | /** |
176 | | * Copies the decoding tile parameters onto all the tile parameters. |
177 | | * Creates also the tile decoder. |
178 | | */ |
179 | | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, |
180 | | opj_stream_private_t *p_stream, |
181 | | opj_event_mgr_t * p_manager); |
182 | | |
183 | | /** |
184 | | * Destroys the memory associated with the decoding of headers. |
185 | | */ |
186 | | static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, |
187 | | opj_stream_private_t *p_stream, |
188 | | opj_event_mgr_t * p_manager); |
189 | | |
190 | | /** |
191 | | * Reads the lookup table containing all the marker, status and action, and returns the handler associated |
192 | | * with the marker value. |
193 | | * @param p_id Marker value to look up |
194 | | * |
195 | | * @return the handler associated with the id. |
196 | | */ |
197 | | static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler( |
198 | | OPJ_UINT32 p_id); |
199 | | |
200 | | /** |
201 | | * Destroys a tile coding parameter structure. |
202 | | * |
203 | | * @param p_tcp the tile coding parameter to destroy. |
204 | | */ |
205 | | static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp); |
206 | | |
207 | | /** |
208 | | * Destroys the data inside a tile coding parameter structure. |
209 | | * |
210 | | * @param p_tcp the tile coding parameter which contain data to destroy. |
211 | | */ |
212 | | static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp); |
213 | | |
214 | | /** |
215 | | * Destroys a coding parameter structure. |
216 | | * |
217 | | * @param p_cp the coding parameter to destroy. |
218 | | */ |
219 | | static void opj_j2k_cp_destroy(opj_cp_t *p_cp); |
220 | | |
221 | | /** |
222 | | * Compare 2 a SPCod/ SPCoc elements, i.e. the coding style of a given component of a tile. |
223 | | * |
224 | | * @param p_j2k J2K codec. |
225 | | * @param p_tile_no Tile number |
226 | | * @param p_first_comp_no The 1st component number to compare. |
227 | | * @param p_second_comp_no The 1st component number to compare. |
228 | | * |
229 | | * @return OPJ_TRUE if SPCdod are equals. |
230 | | */ |
231 | | static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, |
232 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
233 | | |
234 | | /** |
235 | | * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
236 | | * |
237 | | * @param p_j2k J2K codec. |
238 | | * @param p_tile_no FIXME DOC |
239 | | * @param p_comp_no the component number to output. |
240 | | * @param p_data FIXME DOC |
241 | | * @param p_header_size FIXME DOC |
242 | | * @param p_manager the user event manager. |
243 | | * |
244 | | * @return FIXME DOC |
245 | | */ |
246 | | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, |
247 | | OPJ_UINT32 p_tile_no, |
248 | | OPJ_UINT32 p_comp_no, |
249 | | OPJ_BYTE * p_data, |
250 | | OPJ_UINT32 * p_header_size, |
251 | | opj_event_mgr_t * p_manager); |
252 | | |
253 | | /** |
254 | | * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. |
255 | | * |
256 | | * @param p_j2k the J2K codec. |
257 | | * @param p_tile_no the tile index. |
258 | | * @param p_comp_no the component being outputted. |
259 | | * |
260 | | * @return the number of bytes taken by the SPCod element. |
261 | | */ |
262 | | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, |
263 | | OPJ_UINT32 p_tile_no, |
264 | | OPJ_UINT32 p_comp_no); |
265 | | |
266 | | /** |
267 | | * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
268 | | * @param p_j2k the jpeg2000 codec. |
269 | | * @param compno FIXME DOC |
270 | | * @param p_header_data the data contained in the COM box. |
271 | | * @param p_header_size the size of the data contained in the COM marker. |
272 | | * @param p_manager the user event manager. |
273 | | */ |
274 | | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, |
275 | | OPJ_UINT32 compno, |
276 | | OPJ_BYTE * p_header_data, |
277 | | OPJ_UINT32 * p_header_size, |
278 | | opj_event_mgr_t * p_manager); |
279 | | |
280 | | /** |
281 | | * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
282 | | * |
283 | | * @param p_tile_no the tile index. |
284 | | * @param p_comp_no the component being outputted. |
285 | | * @param p_j2k the J2K codec. |
286 | | * |
287 | | * @return the number of bytes taken by the SPCod element. |
288 | | */ |
289 | | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, |
290 | | OPJ_UINT32 p_tile_no, |
291 | | OPJ_UINT32 p_comp_no); |
292 | | |
293 | | /** |
294 | | * Compares 2 SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
295 | | * |
296 | | * @param p_j2k J2K codec. |
297 | | * @param p_tile_no the tile to output. |
298 | | * @param p_first_comp_no the first component number to compare. |
299 | | * @param p_second_comp_no the second component number to compare. |
300 | | * |
301 | | * @return OPJ_TRUE if equals. |
302 | | */ |
303 | | static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, |
304 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
305 | | |
306 | | |
307 | | /** |
308 | | * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
309 | | * |
310 | | * @param p_tile_no the tile to output. |
311 | | * @param p_comp_no the component number to output. |
312 | | * @param p_data the data buffer. |
313 | | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
314 | | * @param p_j2k J2K codec. |
315 | | * @param p_manager the user event manager. |
316 | | * |
317 | | */ |
318 | | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
319 | | OPJ_UINT32 p_tile_no, |
320 | | OPJ_UINT32 p_comp_no, |
321 | | OPJ_BYTE * p_data, |
322 | | OPJ_UINT32 * p_header_size, |
323 | | opj_event_mgr_t * p_manager); |
324 | | |
325 | | /** |
326 | | * Updates the Tile Length Marker. |
327 | | */ |
328 | | static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size); |
329 | | |
330 | | /** |
331 | | * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
332 | | * |
333 | | * @param p_j2k J2K codec. |
334 | | * @param compno the component number to output. |
335 | | * @param p_header_data the data buffer. |
336 | | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
337 | | * @param p_manager the user event manager. |
338 | | * |
339 | | */ |
340 | | static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
341 | | OPJ_UINT32 compno, |
342 | | OPJ_BYTE * p_header_data, |
343 | | OPJ_UINT32 * p_header_size, |
344 | | opj_event_mgr_t * p_manager); |
345 | | |
346 | | /** |
347 | | * Copies the tile component parameters of all the component from the first tile component. |
348 | | * |
349 | | * @param p_j2k the J2k codec. |
350 | | */ |
351 | | static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k); |
352 | | |
353 | | /** |
354 | | * Copies the tile quantization parameters of all the component from the first tile component. |
355 | | * |
356 | | * @param p_j2k the J2k codec. |
357 | | */ |
358 | | static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k); |
359 | | |
360 | | /** |
361 | | * Reads the tiles. |
362 | | */ |
363 | | static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
364 | | opj_stream_private_t *p_stream, |
365 | | opj_event_mgr_t * p_manager); |
366 | | |
367 | | static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, |
368 | | OPJ_UINT32 p_tile_index, |
369 | | opj_stream_private_t *p_stream, |
370 | | opj_event_mgr_t * p_manager); |
371 | | |
372 | | static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, |
373 | | opj_image_t* p_output_image); |
374 | | |
375 | | static void opj_get_tile_dimensions(opj_image_t * l_image, |
376 | | opj_tcd_tilecomp_t * l_tilec, |
377 | | opj_image_comp_t * l_img_comp, |
378 | | OPJ_UINT32* l_size_comp, |
379 | | OPJ_UINT32* l_width, |
380 | | OPJ_UINT32* l_height, |
381 | | OPJ_UINT32* l_offset_x, |
382 | | OPJ_UINT32* l_offset_y, |
383 | | OPJ_UINT32* l_image_width, |
384 | | OPJ_UINT32* l_stride, |
385 | | OPJ_UINT32* l_tile_offset); |
386 | | |
387 | | static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data); |
388 | | |
389 | | static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, |
390 | | opj_stream_private_t *p_stream, |
391 | | opj_event_mgr_t * p_manager); |
392 | | |
393 | | /** |
394 | | * Sets up the procedures to do on writing header. |
395 | | * Developers wanting to extend the library can add their own writing procedures. |
396 | | */ |
397 | | static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, |
398 | | opj_event_mgr_t * p_manager); |
399 | | |
400 | | static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, |
401 | | OPJ_BYTE * p_data, |
402 | | OPJ_UINT32 * p_data_written, |
403 | | OPJ_UINT32 total_data_size, |
404 | | opj_stream_private_t *p_stream, |
405 | | struct opj_event_mgr * p_manager); |
406 | | |
407 | | static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, |
408 | | OPJ_BYTE * p_data, |
409 | | OPJ_UINT32 * p_data_written, |
410 | | OPJ_UINT32 total_data_size, |
411 | | opj_stream_private_t *p_stream, |
412 | | struct opj_event_mgr * p_manager); |
413 | | |
414 | | /** |
415 | | * Gets the offset of the header. |
416 | | * |
417 | | * @param p_stream the stream to write data to. |
418 | | * @param p_j2k J2K codec. |
419 | | * @param p_manager the user event manager. |
420 | | */ |
421 | | static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, |
422 | | opj_stream_private_t *p_stream, |
423 | | opj_event_mgr_t * p_manager); |
424 | | |
425 | | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k); |
426 | | |
427 | | /* |
428 | | * ----------------------------------------------------------------------- |
429 | | * ----------------------------------------------------------------------- |
430 | | * ----------------------------------------------------------------------- |
431 | | */ |
432 | | |
433 | | /** |
434 | | * Writes the SOC marker (Start Of Codestream) |
435 | | * |
436 | | * @param p_stream the stream to write data to. |
437 | | * @param p_j2k J2K codec. |
438 | | * @param p_manager the user event manager. |
439 | | */ |
440 | | static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k, |
441 | | opj_stream_private_t *p_stream, |
442 | | opj_event_mgr_t * p_manager); |
443 | | |
444 | | /** |
445 | | * Reads a SOC marker (Start of Codestream) |
446 | | * @param p_j2k the jpeg2000 file codec. |
447 | | * @param p_stream XXX needs data |
448 | | * @param p_manager the user event manager. |
449 | | */ |
450 | | static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k, |
451 | | opj_stream_private_t *p_stream, |
452 | | opj_event_mgr_t * p_manager); |
453 | | |
454 | | /** |
455 | | * Writes the SIZ marker (image and tile size) |
456 | | * |
457 | | * @param p_j2k J2K codec. |
458 | | * @param p_stream the stream to write data to. |
459 | | * @param p_manager the user event manager. |
460 | | */ |
461 | | static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, |
462 | | opj_stream_private_t *p_stream, |
463 | | opj_event_mgr_t * p_manager); |
464 | | |
465 | | /** |
466 | | * Reads a SIZ marker (image and tile size) |
467 | | * @param p_j2k the jpeg2000 file codec. |
468 | | * @param p_header_data the data contained in the SIZ box. |
469 | | * @param p_header_size the size of the data contained in the SIZ marker. |
470 | | * @param p_manager the user event manager. |
471 | | */ |
472 | | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, |
473 | | OPJ_BYTE * p_header_data, |
474 | | OPJ_UINT32 p_header_size, |
475 | | opj_event_mgr_t * p_manager); |
476 | | |
477 | | /** |
478 | | * Writes the COM marker (comment) |
479 | | * |
480 | | * @param p_stream the stream to write data to. |
481 | | * @param p_j2k J2K codec. |
482 | | * @param p_manager the user event manager. |
483 | | */ |
484 | | static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, |
485 | | opj_stream_private_t *p_stream, |
486 | | opj_event_mgr_t * p_manager); |
487 | | |
488 | | /** |
489 | | * Reads a COM marker (comments) |
490 | | * @param p_j2k the jpeg2000 file codec. |
491 | | * @param p_header_data the data contained in the COM box. |
492 | | * @param p_header_size the size of the data contained in the COM marker. |
493 | | * @param p_manager the user event manager. |
494 | | */ |
495 | | static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, |
496 | | OPJ_BYTE * p_header_data, |
497 | | OPJ_UINT32 p_header_size, |
498 | | opj_event_mgr_t * p_manager); |
499 | | /** |
500 | | * Writes the COD marker (Coding style default) |
501 | | * |
502 | | * @param p_stream the stream to write data to. |
503 | | * @param p_j2k J2K codec. |
504 | | * @param p_manager the user event manager. |
505 | | */ |
506 | | static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, |
507 | | opj_stream_private_t *p_stream, |
508 | | opj_event_mgr_t * p_manager); |
509 | | |
510 | | /** |
511 | | * Reads a COD marker (Coding style defaults) |
512 | | * @param p_header_data the data contained in the COD box. |
513 | | * @param p_j2k the jpeg2000 codec. |
514 | | * @param p_header_size the size of the data contained in the COD marker. |
515 | | * @param p_manager the user event manager. |
516 | | */ |
517 | | static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, |
518 | | OPJ_BYTE * p_header_data, |
519 | | OPJ_UINT32 p_header_size, |
520 | | opj_event_mgr_t * p_manager); |
521 | | |
522 | | /** |
523 | | * Compares 2 COC markers (Coding style component) |
524 | | * |
525 | | * @param p_j2k J2K codec. |
526 | | * @param p_first_comp_no the index of the first component to compare. |
527 | | * @param p_second_comp_no the index of the second component to compare. |
528 | | * |
529 | | * @return OPJ_TRUE if equals |
530 | | */ |
531 | | static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, |
532 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
533 | | |
534 | | /** |
535 | | * Writes the COC marker (Coding style component) |
536 | | * |
537 | | * @param p_j2k J2K codec. |
538 | | * @param p_comp_no the index of the component to output. |
539 | | * @param p_stream the stream to write data to. |
540 | | * @param p_manager the user event manager. |
541 | | */ |
542 | | static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, |
543 | | OPJ_UINT32 p_comp_no, |
544 | | opj_stream_private_t *p_stream, |
545 | | opj_event_mgr_t * p_manager); |
546 | | |
547 | | /** |
548 | | * Writes the COC marker (Coding style component) |
549 | | * |
550 | | * @param p_j2k J2K codec. |
551 | | * @param p_comp_no the index of the component to output. |
552 | | * @param p_data FIXME DOC |
553 | | * @param p_data_written FIXME DOC |
554 | | * @param p_manager the user event manager. |
555 | | */ |
556 | | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, |
557 | | OPJ_UINT32 p_comp_no, |
558 | | OPJ_BYTE * p_data, |
559 | | OPJ_UINT32 * p_data_written, |
560 | | opj_event_mgr_t * p_manager); |
561 | | |
562 | | /** |
563 | | * Gets the maximum size taken by a coc. |
564 | | * |
565 | | * @param p_j2k the jpeg2000 codec to use. |
566 | | */ |
567 | | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k); |
568 | | |
569 | | /** |
570 | | * Reads a COC marker (Coding Style Component) |
571 | | * @param p_header_data the data contained in the COC box. |
572 | | * @param p_j2k the jpeg2000 codec. |
573 | | * @param p_header_size the size of the data contained in the COC marker. |
574 | | * @param p_manager the user event manager. |
575 | | */ |
576 | | static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, |
577 | | OPJ_BYTE * p_header_data, |
578 | | OPJ_UINT32 p_header_size, |
579 | | opj_event_mgr_t * p_manager); |
580 | | |
581 | | /** |
582 | | * Writes the QCD marker (quantization default) |
583 | | * |
584 | | * @param p_j2k J2K codec. |
585 | | * @param p_stream the stream to write data to. |
586 | | * @param p_manager the user event manager. |
587 | | */ |
588 | | static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, |
589 | | opj_stream_private_t *p_stream, |
590 | | opj_event_mgr_t * p_manager); |
591 | | |
592 | | /** |
593 | | * Reads a QCD marker (Quantization defaults) |
594 | | * @param p_header_data the data contained in the QCD box. |
595 | | * @param p_j2k the jpeg2000 codec. |
596 | | * @param p_header_size the size of the data contained in the QCD marker. |
597 | | * @param p_manager the user event manager. |
598 | | */ |
599 | | static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, |
600 | | OPJ_BYTE * p_header_data, |
601 | | OPJ_UINT32 p_header_size, |
602 | | opj_event_mgr_t * p_manager); |
603 | | |
604 | | /** |
605 | | * Compare QCC markers (quantization component) |
606 | | * |
607 | | * @param p_j2k J2K codec. |
608 | | * @param p_first_comp_no the index of the first component to compare. |
609 | | * @param p_second_comp_no the index of the second component to compare. |
610 | | * |
611 | | * @return OPJ_TRUE if equals. |
612 | | */ |
613 | | static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, |
614 | | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
615 | | |
616 | | /** |
617 | | * Writes the QCC marker (quantization component) |
618 | | * |
619 | | * @param p_comp_no the index of the component to output. |
620 | | * @param p_stream the stream to write data to. |
621 | | * @param p_j2k J2K codec. |
622 | | * @param p_manager the user event manager. |
623 | | */ |
624 | | static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, |
625 | | OPJ_UINT32 p_comp_no, |
626 | | opj_stream_private_t *p_stream, |
627 | | opj_event_mgr_t * p_manager); |
628 | | |
629 | | /** |
630 | | * Writes the QCC marker (quantization component) |
631 | | * |
632 | | * @param p_j2k J2K codec. |
633 | | * @param p_comp_no the index of the component to output. |
634 | | * @param p_data FIXME DOC |
635 | | * @param p_data_written the stream to write data to. |
636 | | * @param p_manager the user event manager. |
637 | | */ |
638 | | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, |
639 | | OPJ_UINT32 p_comp_no, |
640 | | OPJ_BYTE * p_data, |
641 | | OPJ_UINT32 * p_data_written, |
642 | | opj_event_mgr_t * p_manager); |
643 | | |
644 | | /** |
645 | | * Gets the maximum size taken by a qcc. |
646 | | */ |
647 | | static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k); |
648 | | |
649 | | /** |
650 | | * Reads a QCC marker (Quantization component) |
651 | | * @param p_header_data the data contained in the QCC box. |
652 | | * @param p_j2k the jpeg2000 codec. |
653 | | * @param p_header_size the size of the data contained in the QCC marker. |
654 | | * @param p_manager the user event manager. |
655 | | */ |
656 | | static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, |
657 | | OPJ_BYTE * p_header_data, |
658 | | OPJ_UINT32 p_header_size, |
659 | | opj_event_mgr_t * p_manager); |
660 | | /** |
661 | | * Writes the POC marker (Progression Order Change) |
662 | | * |
663 | | * @param p_stream the stream to write data to. |
664 | | * @param p_j2k J2K codec. |
665 | | * @param p_manager the user event manager. |
666 | | */ |
667 | | static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, |
668 | | opj_stream_private_t *p_stream, |
669 | | opj_event_mgr_t * p_manager); |
670 | | /** |
671 | | * Writes the POC marker (Progression Order Change) |
672 | | * |
673 | | * @param p_j2k J2K codec. |
674 | | * @param p_data FIXME DOC |
675 | | * @param p_data_written the stream to write data to. |
676 | | * @param p_manager the user event manager. |
677 | | */ |
678 | | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, |
679 | | OPJ_BYTE * p_data, |
680 | | OPJ_UINT32 * p_data_written, |
681 | | opj_event_mgr_t * p_manager); |
682 | | /** |
683 | | * Gets the maximum size taken by the writing of a POC. |
684 | | */ |
685 | | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k); |
686 | | |
687 | | /** |
688 | | * Reads a POC marker (Progression Order Change) |
689 | | * |
690 | | * @param p_header_data the data contained in the POC box. |
691 | | * @param p_j2k the jpeg2000 codec. |
692 | | * @param p_header_size the size of the data contained in the POC marker. |
693 | | * @param p_manager the user event manager. |
694 | | */ |
695 | | static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, |
696 | | OPJ_BYTE * p_header_data, |
697 | | OPJ_UINT32 p_header_size, |
698 | | opj_event_mgr_t * p_manager); |
699 | | |
700 | | /** |
701 | | * Gets the maximum size taken by the toc headers of all the tile parts of any given tile. |
702 | | */ |
703 | | static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k); |
704 | | |
705 | | /** |
706 | | * Gets the maximum size taken by the headers of the SOT. |
707 | | * |
708 | | * @param p_j2k the jpeg2000 codec to use. |
709 | | */ |
710 | | static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k); |
711 | | |
712 | | /** |
713 | | * Reads a CRG marker (Component registration) |
714 | | * |
715 | | * @param p_header_data the data contained in the TLM box. |
716 | | * @param p_j2k the jpeg2000 codec. |
717 | | * @param p_header_size the size of the data contained in the TLM marker. |
718 | | * @param p_manager the user event manager. |
719 | | */ |
720 | | static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, |
721 | | OPJ_BYTE * p_header_data, |
722 | | OPJ_UINT32 p_header_size, |
723 | | opj_event_mgr_t * p_manager); |
724 | | /** |
725 | | * Reads a TLM marker (Tile Length Marker) |
726 | | * |
727 | | * @param p_header_data the data contained in the TLM box. |
728 | | * @param p_j2k the jpeg2000 codec. |
729 | | * @param p_header_size the size of the data contained in the TLM marker. |
730 | | * @param p_manager the user event manager. |
731 | | */ |
732 | | static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, |
733 | | OPJ_BYTE * p_header_data, |
734 | | OPJ_UINT32 p_header_size, |
735 | | opj_event_mgr_t * p_manager); |
736 | | |
737 | | /** |
738 | | * Writes the updated tlm. |
739 | | * |
740 | | * @param p_stream the stream to write data to. |
741 | | * @param p_j2k J2K codec. |
742 | | * @param p_manager the user event manager. |
743 | | */ |
744 | | static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, |
745 | | opj_stream_private_t *p_stream, |
746 | | opj_event_mgr_t * p_manager); |
747 | | |
748 | | /** |
749 | | * Reads a PLM marker (Packet length, main header marker) |
750 | | * |
751 | | * @param p_header_data the data contained in the TLM box. |
752 | | * @param p_j2k the jpeg2000 codec. |
753 | | * @param p_header_size the size of the data contained in the TLM marker. |
754 | | * @param p_manager the user event manager. |
755 | | */ |
756 | | static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, |
757 | | OPJ_BYTE * p_header_data, |
758 | | OPJ_UINT32 p_header_size, |
759 | | opj_event_mgr_t * p_manager); |
760 | | /** |
761 | | * Reads a PLT marker (Packet length, tile-part header) |
762 | | * |
763 | | * @param p_header_data the data contained in the PLT box. |
764 | | * @param p_j2k the jpeg2000 codec. |
765 | | * @param p_header_size the size of the data contained in the PLT marker. |
766 | | * @param p_manager the user event manager. |
767 | | */ |
768 | | static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, |
769 | | OPJ_BYTE * p_header_data, |
770 | | OPJ_UINT32 p_header_size, |
771 | | opj_event_mgr_t * p_manager); |
772 | | |
773 | | /** |
774 | | * Reads a PPM marker (Packed headers, main header) |
775 | | * |
776 | | * @param p_header_data the data contained in the POC box. |
777 | | * @param p_j2k the jpeg2000 codec. |
778 | | * @param p_header_size the size of the data contained in the POC marker. |
779 | | * @param p_manager the user event manager. |
780 | | */ |
781 | | |
782 | | static OPJ_BOOL opj_j2k_read_ppm( |
783 | | opj_j2k_t *p_j2k, |
784 | | OPJ_BYTE * p_header_data, |
785 | | OPJ_UINT32 p_header_size, |
786 | | opj_event_mgr_t * p_manager); |
787 | | |
788 | | /** |
789 | | * Merges all PPM markers read (Packed headers, main header) |
790 | | * |
791 | | * @param p_cp main coding parameters. |
792 | | * @param p_manager the user event manager. |
793 | | */ |
794 | | static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager); |
795 | | |
796 | | /** |
797 | | * Reads a PPT marker (Packed packet headers, tile-part header) |
798 | | * |
799 | | * @param p_header_data the data contained in the PPT box. |
800 | | * @param p_j2k the jpeg2000 codec. |
801 | | * @param p_header_size the size of the data contained in the PPT marker. |
802 | | * @param p_manager the user event manager. |
803 | | */ |
804 | | static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, |
805 | | OPJ_BYTE * p_header_data, |
806 | | OPJ_UINT32 p_header_size, |
807 | | opj_event_mgr_t * p_manager); |
808 | | |
809 | | /** |
810 | | * Merges all PPT markers read (Packed headers, tile-part header) |
811 | | * |
812 | | * @param p_tcp the tile. |
813 | | * @param p_manager the user event manager. |
814 | | */ |
815 | | static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, |
816 | | opj_event_mgr_t * p_manager); |
817 | | |
818 | | |
819 | | /** |
820 | | * Writes the TLM marker (Tile Length Marker) |
821 | | * |
822 | | * @param p_stream the stream to write data to. |
823 | | * @param p_j2k J2K codec. |
824 | | * @param p_manager the user event manager. |
825 | | */ |
826 | | static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, |
827 | | opj_stream_private_t *p_stream, |
828 | | opj_event_mgr_t * p_manager); |
829 | | |
830 | | /** |
831 | | * Writes the SOT marker (Start of tile-part) |
832 | | * |
833 | | * @param p_j2k J2K codec. |
834 | | * @param p_data Output buffer |
835 | | * @param total_data_size Output buffer size |
836 | | * @param p_data_written Number of bytes written into stream |
837 | | * @param p_stream the stream to write data to. |
838 | | * @param p_manager the user event manager. |
839 | | */ |
840 | | static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, |
841 | | OPJ_BYTE * p_data, |
842 | | OPJ_UINT32 total_data_size, |
843 | | OPJ_UINT32 * p_data_written, |
844 | | const opj_stream_private_t *p_stream, |
845 | | opj_event_mgr_t * p_manager); |
846 | | |
847 | | /** |
848 | | * Reads values from a SOT marker (Start of tile-part) |
849 | | * |
850 | | * the j2k decoder state is not affected. No side effects, no checks except for p_header_size. |
851 | | * |
852 | | * @param p_header_data the data contained in the SOT marker. |
853 | | * @param p_header_size the size of the data contained in the SOT marker. |
854 | | * @param p_tile_no Isot. |
855 | | * @param p_tot_len Psot. |
856 | | * @param p_current_part TPsot. |
857 | | * @param p_num_parts TNsot. |
858 | | * @param p_manager the user event manager. |
859 | | */ |
860 | | static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, |
861 | | OPJ_UINT32 p_header_size, |
862 | | OPJ_UINT32* p_tile_no, |
863 | | OPJ_UINT32* p_tot_len, |
864 | | OPJ_UINT32* p_current_part, |
865 | | OPJ_UINT32* p_num_parts, |
866 | | opj_event_mgr_t * p_manager); |
867 | | /** |
868 | | * Reads a SOT marker (Start of tile-part) |
869 | | * |
870 | | * @param p_header_data the data contained in the SOT marker. |
871 | | * @param p_j2k the jpeg2000 codec. |
872 | | * @param p_header_size the size of the data contained in the PPT marker. |
873 | | * @param p_manager the user event manager. |
874 | | */ |
875 | | static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, |
876 | | OPJ_BYTE * p_header_data, |
877 | | OPJ_UINT32 p_header_size, |
878 | | opj_event_mgr_t * p_manager); |
879 | | /** |
880 | | * Writes the SOD marker (Start of data) |
881 | | * |
882 | | * This also writes optional PLT markers (before SOD) |
883 | | * |
884 | | * @param p_j2k J2K codec. |
885 | | * @param p_tile_coder FIXME DOC |
886 | | * @param p_data FIXME DOC |
887 | | * @param p_data_written FIXME DOC |
888 | | * @param total_data_size FIXME DOC |
889 | | * @param p_stream the stream to write data to. |
890 | | * @param p_manager the user event manager. |
891 | | */ |
892 | | static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, |
893 | | opj_tcd_t * p_tile_coder, |
894 | | OPJ_BYTE * p_data, |
895 | | OPJ_UINT32 * p_data_written, |
896 | | OPJ_UINT32 total_data_size, |
897 | | const opj_stream_private_t *p_stream, |
898 | | opj_event_mgr_t * p_manager); |
899 | | |
900 | | /** |
901 | | * Reads a SOD marker (Start Of Data) |
902 | | * |
903 | | * @param p_j2k the jpeg2000 codec. |
904 | | * @param p_stream FIXME DOC |
905 | | * @param p_manager the user event manager. |
906 | | */ |
907 | | static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, |
908 | | opj_stream_private_t *p_stream, |
909 | | opj_event_mgr_t * p_manager); |
910 | | |
911 | | static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size) |
912 | 0 | { |
913 | 0 | if (p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte) { |
914 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
915 | 0 | p_j2k->m_current_tile_number, 1); |
916 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 1; |
917 | 0 | } else { |
918 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
919 | 0 | p_j2k->m_current_tile_number, 2); |
920 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 2; |
921 | 0 | } |
922 | |
|
923 | 0 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
924 | 0 | p_tile_part_size, 4); /* PSOT */ |
925 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; |
926 | 0 | } |
927 | | |
928 | | /** |
929 | | * Writes the RGN marker (Region Of Interest) |
930 | | * |
931 | | * @param p_tile_no the tile to output |
932 | | * @param p_comp_no the component to output |
933 | | * @param nb_comps the number of components |
934 | | * @param p_stream the stream to write data to. |
935 | | * @param p_j2k J2K codec. |
936 | | * @param p_manager the user event manager. |
937 | | */ |
938 | | static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, |
939 | | OPJ_UINT32 p_tile_no, |
940 | | OPJ_UINT32 p_comp_no, |
941 | | OPJ_UINT32 nb_comps, |
942 | | opj_stream_private_t *p_stream, |
943 | | opj_event_mgr_t * p_manager); |
944 | | |
945 | | /** |
946 | | * Reads a RGN marker (Region Of Interest) |
947 | | * |
948 | | * @param p_header_data the data contained in the POC box. |
949 | | * @param p_j2k the jpeg2000 codec. |
950 | | * @param p_header_size the size of the data contained in the POC marker. |
951 | | * @param p_manager the user event manager. |
952 | | */ |
953 | | static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, |
954 | | OPJ_BYTE * p_header_data, |
955 | | OPJ_UINT32 p_header_size, |
956 | | opj_event_mgr_t * p_manager); |
957 | | |
958 | | /** |
959 | | * Writes the EOC marker (End of Codestream) |
960 | | * |
961 | | * @param p_stream the stream to write data to. |
962 | | * @param p_j2k J2K codec. |
963 | | * @param p_manager the user event manager. |
964 | | */ |
965 | | static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, |
966 | | opj_stream_private_t *p_stream, |
967 | | opj_event_mgr_t * p_manager); |
968 | | |
969 | | #if 0 |
970 | | /** |
971 | | * Reads a EOC marker (End Of Codestream) |
972 | | * |
973 | | * @param p_j2k the jpeg2000 codec. |
974 | | * @param p_stream FIXME DOC |
975 | | * @param p_manager the user event manager. |
976 | | */ |
977 | | static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, |
978 | | opj_stream_private_t *p_stream, |
979 | | opj_event_mgr_t * p_manager); |
980 | | #endif |
981 | | |
982 | | /** |
983 | | * Writes the CBD-MCT-MCC-MCO markers (Multi components transform) |
984 | | * |
985 | | * @param p_stream the stream to write data to. |
986 | | * @param p_j2k J2K codec. |
987 | | * @param p_manager the user event manager. |
988 | | */ |
989 | | static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, |
990 | | opj_stream_private_t *p_stream, |
991 | | opj_event_mgr_t * p_manager); |
992 | | |
993 | | /** |
994 | | * Inits the Info |
995 | | * |
996 | | * @param p_stream the stream to write data to. |
997 | | * @param p_j2k J2K codec. |
998 | | * @param p_manager the user event manager. |
999 | | */ |
1000 | | static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, |
1001 | | opj_stream_private_t *p_stream, |
1002 | | opj_event_mgr_t * p_manager); |
1003 | | |
1004 | | /** |
1005 | | Add main header marker information |
1006 | | @param cstr_index Codestream information structure |
1007 | | @param type marker type |
1008 | | @param pos byte offset of marker segment |
1009 | | @param len length of marker segment |
1010 | | */ |
1011 | | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, |
1012 | | OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ; |
1013 | | /** |
1014 | | Add tile header marker information |
1015 | | @param tileno tile index number |
1016 | | @param cstr_index Codestream information structure |
1017 | | @param type marker type |
1018 | | @param pos byte offset of marker segment |
1019 | | @param len length of marker segment |
1020 | | */ |
1021 | | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, |
1022 | | opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, |
1023 | | OPJ_UINT32 len); |
1024 | | |
1025 | | /** |
1026 | | * Reads an unknown marker |
1027 | | * |
1028 | | * @param p_j2k the jpeg2000 codec. |
1029 | | * @param p_stream the stream object to read from. |
1030 | | * @param output_marker FIXME DOC |
1031 | | * @param p_manager the user event manager. |
1032 | | * |
1033 | | * @return true if the marker could be deduced. |
1034 | | */ |
1035 | | static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, |
1036 | | opj_stream_private_t *p_stream, |
1037 | | OPJ_UINT32 *output_marker, |
1038 | | opj_event_mgr_t * p_manager); |
1039 | | |
1040 | | /** |
1041 | | * Writes the MCT marker (Multiple Component Transform) |
1042 | | * |
1043 | | * @param p_j2k J2K codec. |
1044 | | * @param p_mct_record FIXME DOC |
1045 | | * @param p_stream the stream to write data to. |
1046 | | * @param p_manager the user event manager. |
1047 | | */ |
1048 | | static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, |
1049 | | opj_mct_data_t * p_mct_record, |
1050 | | opj_stream_private_t *p_stream, |
1051 | | opj_event_mgr_t * p_manager); |
1052 | | |
1053 | | /** |
1054 | | * Reads a MCT marker (Multiple Component Transform) |
1055 | | * |
1056 | | * @param p_header_data the data contained in the MCT box. |
1057 | | * @param p_j2k the jpeg2000 codec. |
1058 | | * @param p_header_size the size of the data contained in the MCT marker. |
1059 | | * @param p_manager the user event manager. |
1060 | | */ |
1061 | | static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, |
1062 | | OPJ_BYTE * p_header_data, |
1063 | | OPJ_UINT32 p_header_size, |
1064 | | opj_event_mgr_t * p_manager); |
1065 | | |
1066 | | /** |
1067 | | * Writes the MCC marker (Multiple Component Collection) |
1068 | | * |
1069 | | * @param p_j2k J2K codec. |
1070 | | * @param p_mcc_record FIXME DOC |
1071 | | * @param p_stream the stream to write data to. |
1072 | | * @param p_manager the user event manager. |
1073 | | */ |
1074 | | static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, |
1075 | | opj_simple_mcc_decorrelation_data_t * p_mcc_record, |
1076 | | opj_stream_private_t *p_stream, |
1077 | | opj_event_mgr_t * p_manager); |
1078 | | |
1079 | | /** |
1080 | | * Reads a MCC marker (Multiple Component Collection) |
1081 | | * |
1082 | | * @param p_header_data the data contained in the MCC box. |
1083 | | * @param p_j2k the jpeg2000 codec. |
1084 | | * @param p_header_size the size of the data contained in the MCC marker. |
1085 | | * @param p_manager the user event manager. |
1086 | | */ |
1087 | | static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, |
1088 | | OPJ_BYTE * p_header_data, |
1089 | | OPJ_UINT32 p_header_size, |
1090 | | opj_event_mgr_t * p_manager); |
1091 | | |
1092 | | /** |
1093 | | * Writes the MCO marker (Multiple component transformation ordering) |
1094 | | * |
1095 | | * @param p_stream the stream to write data to. |
1096 | | * @param p_j2k J2K codec. |
1097 | | * @param p_manager the user event manager. |
1098 | | */ |
1099 | | static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, |
1100 | | opj_stream_private_t *p_stream, |
1101 | | opj_event_mgr_t * p_manager); |
1102 | | |
1103 | | /** |
1104 | | * Reads a MCO marker (Multiple Component Transform Ordering) |
1105 | | * |
1106 | | * @param p_header_data the data contained in the MCO box. |
1107 | | * @param p_j2k the jpeg2000 codec. |
1108 | | * @param p_header_size the size of the data contained in the MCO marker. |
1109 | | * @param p_manager the user event manager. |
1110 | | */ |
1111 | | static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, |
1112 | | OPJ_BYTE * p_header_data, |
1113 | | OPJ_UINT32 p_header_size, |
1114 | | opj_event_mgr_t * p_manager); |
1115 | | |
1116 | | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, |
1117 | | OPJ_UINT32 p_index); |
1118 | | |
1119 | | static void opj_j2k_read_int16_to_float(const void * p_src_data, |
1120 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1121 | | static void opj_j2k_read_int32_to_float(const void * p_src_data, |
1122 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1123 | | static void opj_j2k_read_float32_to_float(const void * p_src_data, |
1124 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1125 | | static void opj_j2k_read_float64_to_float(const void * p_src_data, |
1126 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1127 | | |
1128 | | static void opj_j2k_read_int16_to_int32(const void * p_src_data, |
1129 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1130 | | static void opj_j2k_read_int32_to_int32(const void * p_src_data, |
1131 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1132 | | static void opj_j2k_read_float32_to_int32(const void * p_src_data, |
1133 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1134 | | static void opj_j2k_read_float64_to_int32(const void * p_src_data, |
1135 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1136 | | |
1137 | | static void opj_j2k_write_float_to_int16(const void * p_src_data, |
1138 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1139 | | static void opj_j2k_write_float_to_int32(const void * p_src_data, |
1140 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1141 | | static void opj_j2k_write_float_to_float(const void * p_src_data, |
1142 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1143 | | static void opj_j2k_write_float_to_float64(const void * p_src_data, |
1144 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1145 | | |
1146 | | /** |
1147 | | * Ends the encoding, i.e. frees memory. |
1148 | | * |
1149 | | * @param p_stream the stream to write data to. |
1150 | | * @param p_j2k J2K codec. |
1151 | | * @param p_manager the user event manager. |
1152 | | */ |
1153 | | static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, |
1154 | | opj_stream_private_t *p_stream, |
1155 | | opj_event_mgr_t * p_manager); |
1156 | | |
1157 | | /** |
1158 | | * Writes the CBD marker (Component bit depth definition) |
1159 | | * |
1160 | | * @param p_stream the stream to write data to. |
1161 | | * @param p_j2k J2K codec. |
1162 | | * @param p_manager the user event manager. |
1163 | | */ |
1164 | | static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, |
1165 | | opj_stream_private_t *p_stream, |
1166 | | opj_event_mgr_t * p_manager); |
1167 | | |
1168 | | /** |
1169 | | * Reads a CBD marker (Component bit depth definition) |
1170 | | * @param p_header_data the data contained in the CBD box. |
1171 | | * @param p_j2k the jpeg2000 codec. |
1172 | | * @param p_header_size the size of the data contained in the CBD marker. |
1173 | | * @param p_manager the user event manager. |
1174 | | */ |
1175 | | static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, |
1176 | | OPJ_BYTE * p_header_data, |
1177 | | OPJ_UINT32 p_header_size, |
1178 | | opj_event_mgr_t * p_manager); |
1179 | | |
1180 | | /** |
1181 | | * Reads a CAP marker (extended capabilities definition). Empty implementation. |
1182 | | * Found in HTJ2K files |
1183 | | * |
1184 | | * @param p_header_data the data contained in the CAP box. |
1185 | | * @param p_j2k the jpeg2000 codec. |
1186 | | * @param p_header_size the size of the data contained in the CAP marker. |
1187 | | * @param p_manager the user event manager. |
1188 | | */ |
1189 | | static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k, |
1190 | | OPJ_BYTE * p_header_data, |
1191 | | OPJ_UINT32 p_header_size, |
1192 | | opj_event_mgr_t * p_manager); |
1193 | | |
1194 | | /** |
1195 | | * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files |
1196 | | * @param p_header_data the data contained in the CPF box. |
1197 | | * @param p_j2k the jpeg2000 codec. |
1198 | | * @param p_header_size the size of the data contained in the CPF marker. |
1199 | | * @param p_manager the user event manager. |
1200 | | */ |
1201 | | static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k, |
1202 | | OPJ_BYTE * p_header_data, |
1203 | | OPJ_UINT32 p_header_size, |
1204 | | opj_event_mgr_t * p_manager); |
1205 | | |
1206 | | |
1207 | | /** |
1208 | | * Writes COC marker for each component. |
1209 | | * |
1210 | | * @param p_stream the stream to write data to. |
1211 | | * @param p_j2k J2K codec. |
1212 | | * @param p_manager the user event manager. |
1213 | | */ |
1214 | | static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k, |
1215 | | opj_stream_private_t *p_stream, |
1216 | | opj_event_mgr_t * p_manager); |
1217 | | |
1218 | | /** |
1219 | | * Writes QCC marker for each component. |
1220 | | * |
1221 | | * @param p_stream the stream to write data to. |
1222 | | * @param p_j2k J2K codec. |
1223 | | * @param p_manager the user event manager. |
1224 | | */ |
1225 | | static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k, |
1226 | | opj_stream_private_t *p_stream, |
1227 | | opj_event_mgr_t * p_manager); |
1228 | | |
1229 | | /** |
1230 | | * Writes regions of interests. |
1231 | | * |
1232 | | * @param p_stream the stream to write data to. |
1233 | | * @param p_j2k J2K codec. |
1234 | | * @param p_manager the user event manager. |
1235 | | */ |
1236 | | static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, |
1237 | | opj_stream_private_t *p_stream, |
1238 | | opj_event_mgr_t * p_manager); |
1239 | | |
1240 | | /** |
1241 | | * Writes EPC ???? |
1242 | | * |
1243 | | * @param p_stream the stream to write data to. |
1244 | | * @param p_j2k J2K codec. |
1245 | | * @param p_manager the user event manager. |
1246 | | */ |
1247 | | static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, |
1248 | | opj_stream_private_t *p_stream, |
1249 | | opj_event_mgr_t * p_manager); |
1250 | | |
1251 | | /** |
1252 | | * Checks the progression order changes values. Tells of the poc given as input are valid. |
1253 | | * A nice message is outputted at errors. |
1254 | | * |
1255 | | * @param p_pocs the progression order changes. |
1256 | | * @param tileno the tile number of interest |
1257 | | * @param p_nb_pocs the number of progression order changes. |
1258 | | * @param p_nb_resolutions the number of resolutions. |
1259 | | * @param numcomps the number of components |
1260 | | * @param numlayers the number of layers. |
1261 | | * @param p_manager the user event manager. |
1262 | | * |
1263 | | * @return true if the pocs are valid. |
1264 | | */ |
1265 | | static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, |
1266 | | OPJ_UINT32 tileno, |
1267 | | OPJ_UINT32 p_nb_pocs, |
1268 | | OPJ_UINT32 p_nb_resolutions, |
1269 | | OPJ_UINT32 numcomps, |
1270 | | OPJ_UINT32 numlayers, |
1271 | | opj_event_mgr_t * p_manager); |
1272 | | |
1273 | | /** |
1274 | | * Gets the number of tile parts used for the given change of progression (if any) and the given tile. |
1275 | | * |
1276 | | * @param cp the coding parameters. |
1277 | | * @param pino the offset of the given poc (i.e. its position in the coding parameter). |
1278 | | * @param tileno the given tile. |
1279 | | * |
1280 | | * @return the number of tile parts. |
1281 | | */ |
1282 | | static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, |
1283 | | OPJ_UINT32 tileno); |
1284 | | |
1285 | | /** |
1286 | | * Calculates the total number of tile parts needed by the encoder to |
1287 | | * encode such an image. If not enough memory is available, then the function return false. |
1288 | | * |
1289 | | * @param p_nb_tiles pointer that will hold the number of tile parts. |
1290 | | * @param cp the coding parameters for the image. |
1291 | | * @param image the image to encode. |
1292 | | * @param p_j2k the p_j2k encoder. |
1293 | | * @param p_manager the user event manager. |
1294 | | * |
1295 | | * @return true if the function was successful, false else. |
1296 | | */ |
1297 | | static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k, |
1298 | | opj_cp_t *cp, |
1299 | | OPJ_UINT32 * p_nb_tiles, |
1300 | | opj_image_t *image, |
1301 | | opj_event_mgr_t * p_manager); |
1302 | | |
1303 | | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream); |
1304 | | |
1305 | | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream); |
1306 | | |
1307 | | static opj_codestream_index_t* opj_j2k_create_cstr_index(void); |
1308 | | |
1309 | | static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp); |
1310 | | |
1311 | | static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp); |
1312 | | |
1313 | | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres); |
1314 | | |
1315 | | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, |
1316 | | opj_image_t *image, opj_event_mgr_t *p_manager); |
1317 | | |
1318 | | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, |
1319 | | opj_event_mgr_t *p_manager); |
1320 | | |
1321 | | static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters, |
1322 | | opj_image_t *image, opj_event_mgr_t *p_manager); |
1323 | | |
1324 | | static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters, |
1325 | | opj_image_t *image, |
1326 | | opj_event_mgr_t *p_manager); |
1327 | | |
1328 | | /** |
1329 | | * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254. |
1330 | | * |
1331 | | * @param p_stream the stream to read data from. |
1332 | | * @param tile_no tile number we're looking for. |
1333 | | * @param p_correction_needed output value. if true, non conformant codestream needs TNsot correction. |
1334 | | * @param p_manager the user event manager. |
1335 | | * |
1336 | | * @return true if the function was successful, false else. |
1337 | | */ |
1338 | | static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t |
1339 | | *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, |
1340 | | opj_event_mgr_t * p_manager); |
1341 | | |
1342 | | /*@}*/ |
1343 | | |
1344 | | /*@}*/ |
1345 | | |
1346 | | /* ----------------------------------------------------------------------- */ |
1347 | | typedef struct j2k_prog_order { |
1348 | | OPJ_PROG_ORDER enum_prog; |
1349 | | char str_prog[5]; |
1350 | | } j2k_prog_order_t; |
1351 | | |
1352 | | static const j2k_prog_order_t j2k_prog_order_list[] = { |
1353 | | {OPJ_CPRL, "CPRL"}, |
1354 | | {OPJ_LRCP, "LRCP"}, |
1355 | | {OPJ_PCRL, "PCRL"}, |
1356 | | {OPJ_RLCP, "RLCP"}, |
1357 | | {OPJ_RPCL, "RPCL"}, |
1358 | | {(OPJ_PROG_ORDER) - 1, ""} |
1359 | | }; |
1360 | | |
1361 | | /** |
1362 | | * FIXME DOC |
1363 | | */ |
1364 | | static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = { |
1365 | | 2, |
1366 | | 4, |
1367 | | 4, |
1368 | | 8 |
1369 | | }; |
1370 | | |
1371 | | typedef void (* opj_j2k_mct_function)(const void * p_src_data, |
1372 | | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1373 | | |
1374 | | static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = { |
1375 | | opj_j2k_read_int16_to_float, |
1376 | | opj_j2k_read_int32_to_float, |
1377 | | opj_j2k_read_float32_to_float, |
1378 | | opj_j2k_read_float64_to_float |
1379 | | }; |
1380 | | |
1381 | | static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = { |
1382 | | opj_j2k_read_int16_to_int32, |
1383 | | opj_j2k_read_int32_to_int32, |
1384 | | opj_j2k_read_float32_to_int32, |
1385 | | opj_j2k_read_float64_to_int32 |
1386 | | }; |
1387 | | |
1388 | | static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = { |
1389 | | opj_j2k_write_float_to_int16, |
1390 | | opj_j2k_write_float_to_int32, |
1391 | | opj_j2k_write_float_to_float, |
1392 | | opj_j2k_write_float_to_float64 |
1393 | | }; |
1394 | | |
1395 | | typedef struct opj_dec_memory_marker_handler { |
1396 | | /** marker value */ |
1397 | | OPJ_UINT32 id; |
1398 | | /** value of the state when the marker can appear */ |
1399 | | OPJ_UINT32 states; |
1400 | | /** action linked to the marker */ |
1401 | | OPJ_BOOL(*handler)(opj_j2k_t *p_j2k, |
1402 | | OPJ_BYTE * p_header_data, |
1403 | | OPJ_UINT32 p_header_size, |
1404 | | opj_event_mgr_t * p_manager); |
1405 | | } |
1406 | | opj_dec_memory_marker_handler_t; |
1407 | | |
1408 | | static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] = |
1409 | | { |
1410 | | {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot}, |
1411 | | {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod}, |
1412 | | {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc}, |
1413 | | {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn}, |
1414 | | {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd}, |
1415 | | {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc}, |
1416 | | {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc}, |
1417 | | {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz}, |
1418 | | {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm}, |
1419 | | {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm}, |
1420 | | {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt}, |
1421 | | {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm}, |
1422 | | {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt}, |
1423 | | {J2K_MS_SOP, 0, 0}, |
1424 | | {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg}, |
1425 | | {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com}, |
1426 | | {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct}, |
1427 | | {J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd}, |
1428 | | {J2K_MS_CAP, J2K_STATE_MH, opj_j2k_read_cap}, |
1429 | | {J2K_MS_CPF, J2K_STATE_MH, opj_j2k_read_cpf}, |
1430 | | {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc}, |
1431 | | {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco}, |
1432 | | #ifdef USE_JPWL |
1433 | | #ifdef TODO_MS /* remove these functions which are not compatible with the v2 API */ |
1434 | | {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, |
1435 | | {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, |
1436 | | {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, |
1437 | | {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, |
1438 | | #endif |
1439 | | #endif /* USE_JPWL */ |
1440 | | #ifdef USE_JPSEC |
1441 | | {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, |
1442 | | {J2K_MS_INSEC, 0, j2k_read_insec} |
1443 | | #endif /* USE_JPSEC */ |
1444 | | {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/ |
1445 | | }; |
1446 | | |
1447 | | static void opj_j2k_read_int16_to_float(const void * p_src_data, |
1448 | | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1449 | 561 | { |
1450 | 561 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1451 | 561 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1452 | 561 | OPJ_UINT32 i; |
1453 | 561 | OPJ_UINT32 l_temp; |
1454 | | |
1455 | 2.78k | for (i = 0; i < p_nb_elem; ++i) { |
1456 | 2.22k | opj_read_bytes(l_src_data, &l_temp, 2); |
1457 | | |
1458 | 2.22k | l_src_data += sizeof(OPJ_INT16); |
1459 | | |
1460 | 2.22k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1461 | 2.22k | } |
1462 | 561 | } |
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 | 407 | { |
1467 | 407 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1468 | 407 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1469 | 407 | OPJ_UINT32 i; |
1470 | 407 | OPJ_UINT32 l_temp; |
1471 | | |
1472 | 1.85k | for (i = 0; i < p_nb_elem; ++i) { |
1473 | 1.44k | opj_read_bytes(l_src_data, &l_temp, 4); |
1474 | | |
1475 | 1.44k | l_src_data += sizeof(OPJ_INT32); |
1476 | | |
1477 | 1.44k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1478 | 1.44k | } |
1479 | 407 | } |
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 | 451 | { |
1484 | 451 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1485 | 451 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1486 | 451 | OPJ_UINT32 i; |
1487 | 451 | OPJ_FLOAT32 l_temp; |
1488 | | |
1489 | 2.03k | for (i = 0; i < p_nb_elem; ++i) { |
1490 | 1.58k | opj_read_float(l_src_data, &l_temp); |
1491 | | |
1492 | 1.58k | l_src_data += sizeof(OPJ_FLOAT32); |
1493 | | |
1494 | 1.58k | *(l_dest_data++) = l_temp; |
1495 | 1.58k | } |
1496 | 451 | } |
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 | 370 | { |
1501 | 370 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1502 | 370 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1503 | 370 | OPJ_UINT32 i; |
1504 | 370 | OPJ_FLOAT64 l_temp; |
1505 | | |
1506 | 1.83k | for (i = 0; i < p_nb_elem; ++i) { |
1507 | 1.46k | opj_read_double(l_src_data, &l_temp); |
1508 | | |
1509 | 1.46k | l_src_data += sizeof(OPJ_FLOAT64); |
1510 | | |
1511 | 1.46k | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1512 | 1.46k | } |
1513 | 370 | } |
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 | 52 | { |
1518 | 52 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1519 | 52 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1520 | 52 | OPJ_UINT32 i; |
1521 | 52 | OPJ_UINT32 l_temp; |
1522 | | |
1523 | 150 | for (i = 0; i < p_nb_elem; ++i) { |
1524 | 98 | opj_read_bytes(l_src_data, &l_temp, 2); |
1525 | | |
1526 | 98 | l_src_data += sizeof(OPJ_INT16); |
1527 | | |
1528 | 98 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1529 | 98 | } |
1530 | 52 | } |
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 | 137 | { |
1535 | 137 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1536 | 137 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1537 | 137 | OPJ_UINT32 i; |
1538 | 137 | OPJ_UINT32 l_temp; |
1539 | | |
1540 | 355 | for (i = 0; i < p_nb_elem; ++i) { |
1541 | 218 | opj_read_bytes(l_src_data, &l_temp, 4); |
1542 | | |
1543 | 218 | l_src_data += sizeof(OPJ_INT32); |
1544 | | |
1545 | 218 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1546 | 218 | } |
1547 | 137 | } |
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 | 452 | { |
1552 | 452 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1553 | 452 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1554 | 452 | OPJ_UINT32 i; |
1555 | 452 | OPJ_FLOAT32 l_temp; |
1556 | | |
1557 | 1.26k | for (i = 0; i < p_nb_elem; ++i) { |
1558 | 811 | opj_read_float(l_src_data, &l_temp); |
1559 | | |
1560 | 811 | l_src_data += sizeof(OPJ_FLOAT32); |
1561 | | |
1562 | 811 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1563 | 811 | } |
1564 | 452 | } |
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 | 147 | { |
1569 | 147 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1570 | 147 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1571 | 147 | OPJ_UINT32 i; |
1572 | 147 | OPJ_FLOAT64 l_temp; |
1573 | | |
1574 | 438 | for (i = 0; i < p_nb_elem; ++i) { |
1575 | 291 | opj_read_double(l_src_data, &l_temp); |
1576 | | |
1577 | 291 | l_src_data += sizeof(OPJ_FLOAT64); |
1578 | | |
1579 | 291 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1580 | 291 | } |
1581 | 147 | } |
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.83k | { |
1932 | 8.83k | OPJ_BYTE l_data [2]; |
1933 | 8.83k | OPJ_UINT32 l_marker; |
1934 | | |
1935 | | /* preconditions */ |
1936 | 8.83k | assert(p_j2k != 00); |
1937 | 8.83k | assert(p_manager != 00); |
1938 | 8.83k | assert(p_stream != 00); |
1939 | | |
1940 | 8.83k | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
1941 | 1 | return OPJ_FALSE; |
1942 | 1 | } |
1943 | | |
1944 | 8.83k | opj_read_bytes(l_data, &l_marker, 2); |
1945 | 8.83k | if (l_marker != J2K_MS_SOC) { |
1946 | 0 | return OPJ_FALSE; |
1947 | 0 | } |
1948 | | |
1949 | | /* Next marker should be a SIZ marker in the main header */ |
1950 | 8.83k | 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.83k | p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; |
1954 | | |
1955 | 8.83k | opj_event_msg(p_manager, EVT_INFO, |
1956 | 8.83k | "Start to read j2k main header (%" PRId64 ").\n", |
1957 | 8.83k | p_j2k->cstr_index->main_head_start); |
1958 | | |
1959 | | /* Add the marker to the codestream index*/ |
1960 | 8.83k | if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, |
1961 | 8.83k | 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.83k | return OPJ_TRUE; |
1966 | 8.83k | } |
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.69k | { |
2080 | 8.69k | OPJ_UINT32 i; |
2081 | 8.69k | OPJ_UINT32 l_nb_comp; |
2082 | 8.69k | OPJ_UINT32 l_nb_comp_remain; |
2083 | 8.69k | OPJ_UINT32 l_remaining_size; |
2084 | 8.69k | OPJ_UINT32 l_nb_tiles; |
2085 | 8.69k | OPJ_UINT32 l_tmp, l_tx1, l_ty1; |
2086 | 8.69k | OPJ_UINT32 l_prec0, l_sgnd0; |
2087 | 8.69k | opj_image_t *l_image = 00; |
2088 | 8.69k | opj_cp_t *l_cp = 00; |
2089 | 8.69k | opj_image_comp_t * l_img_comp = 00; |
2090 | 8.69k | opj_tcp_t * l_current_tile_param = 00; |
2091 | | |
2092 | | /* preconditions */ |
2093 | 8.69k | assert(p_j2k != 00); |
2094 | 8.69k | assert(p_manager != 00); |
2095 | 8.69k | assert(p_header_data != 00); |
2096 | | |
2097 | 8.69k | l_image = p_j2k->m_private_image; |
2098 | 8.69k | l_cp = &(p_j2k->m_cp); |
2099 | | |
2100 | | /* minimum size == 39 - 3 (= minimum component parameter) */ |
2101 | 8.69k | if (p_header_size < 36) { |
2102 | 9 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); |
2103 | 9 | return OPJ_FALSE; |
2104 | 9 | } |
2105 | | |
2106 | 8.68k | l_remaining_size = p_header_size - 36; |
2107 | 8.68k | l_nb_comp = l_remaining_size / 3; |
2108 | 8.68k | l_nb_comp_remain = l_remaining_size % 3; |
2109 | 8.68k | if (l_nb_comp_remain != 0) { |
2110 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); |
2111 | 1 | return OPJ_FALSE; |
2112 | 1 | } |
2113 | | |
2114 | 8.68k | opj_read_bytes(p_header_data, &l_tmp, |
2115 | 8.68k | 2); /* Rsiz (capabilities) */ |
2116 | 8.68k | p_header_data += 2; |
2117 | 8.68k | l_cp->rsiz = (OPJ_UINT16) l_tmp; |
2118 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ |
2119 | 8.68k | p_header_data += 4; |
2120 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ |
2121 | 8.68k | p_header_data += 4; |
2122 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ |
2123 | 8.68k | p_header_data += 4; |
2124 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ |
2125 | 8.68k | p_header_data += 4; |
2126 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, |
2127 | 8.68k | 4); /* XTsiz */ |
2128 | 8.68k | p_header_data += 4; |
2129 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, |
2130 | 8.68k | 4); /* YTsiz */ |
2131 | 8.68k | p_header_data += 4; |
2132 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, |
2133 | 8.68k | 4); /* XT0siz */ |
2134 | 8.68k | p_header_data += 4; |
2135 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, |
2136 | 8.68k | 4); /* YT0siz */ |
2137 | 8.68k | p_header_data += 4; |
2138 | 8.68k | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, |
2139 | 8.68k | 2); /* Csiz */ |
2140 | 8.68k | p_header_data += 2; |
2141 | 8.68k | if (l_tmp < 16385) { |
2142 | 8.67k | l_image->numcomps = (OPJ_UINT16) l_tmp; |
2143 | 8.67k | } else { |
2144 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
2145 | 6 | "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp); |
2146 | 6 | return OPJ_FALSE; |
2147 | 6 | } |
2148 | | |
2149 | 8.67k | if (l_image->numcomps != l_nb_comp) { |
2150 | 29 | opj_event_msg(p_manager, EVT_ERROR, |
2151 | 29 | "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", |
2152 | 29 | l_image->numcomps, l_nb_comp); |
2153 | 29 | return OPJ_FALSE; |
2154 | 29 | } |
2155 | | |
2156 | | /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ |
2157 | | /* testcase issue427-null-image-size.jp2 */ |
2158 | 8.64k | if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { |
2159 | 8 | opj_event_msg(p_manager, EVT_ERROR, |
2160 | 8 | "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64 |
2161 | 8 | ")\n", (OPJ_INT64)l_image->x1 - l_image->x0, |
2162 | 8 | (OPJ_INT64)l_image->y1 - l_image->y0); |
2163 | 8 | return OPJ_FALSE; |
2164 | 8 | } |
2165 | | /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ |
2166 | 8.63k | if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) { |
2167 | 5 | opj_event_msg(p_manager, EVT_ERROR, |
2168 | 5 | "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, |
2169 | 5 | l_cp->tdy); |
2170 | 5 | return OPJ_FALSE; |
2171 | 5 | } |
2172 | | |
2173 | | /* testcase issue427-illegal-tile-offset.jp2 */ |
2174 | 8.63k | l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */ |
2175 | 8.63k | l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */ |
2176 | 8.63k | if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || |
2177 | 8.63k | (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) { |
2178 | 69 | opj_event_msg(p_manager, EVT_ERROR, |
2179 | 69 | "Error with SIZ marker: illegal tile offset\n"); |
2180 | 69 | return OPJ_FALSE; |
2181 | 69 | } |
2182 | 8.56k | if (!p_j2k->dump_state) { |
2183 | 8.56k | OPJ_UINT32 siz_w, siz_h; |
2184 | | |
2185 | 8.56k | siz_w = l_image->x1 - l_image->x0; |
2186 | 8.56k | siz_h = l_image->y1 - l_image->y0; |
2187 | | |
2188 | 8.56k | if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0 |
2189 | 8.56k | && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) { |
2190 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
2191 | 0 | "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w, |
2192 | 0 | p_j2k->ihdr_h, siz_w, siz_h); |
2193 | 0 | return OPJ_FALSE; |
2194 | 0 | } |
2195 | 8.56k | } |
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.56k | l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, |
2240 | 8.56k | sizeof(opj_image_comp_t)); |
2241 | 8.56k | if (l_image->comps == 00) { |
2242 | 30 | l_image->numcomps = 0; |
2243 | 30 | opj_event_msg(p_manager, EVT_ERROR, |
2244 | 30 | "Not enough memory to take in charge SIZ marker\n"); |
2245 | 30 | return OPJ_FALSE; |
2246 | 30 | } |
2247 | | |
2248 | 8.53k | l_img_comp = l_image->comps; |
2249 | | |
2250 | 8.53k | l_prec0 = 0; |
2251 | 8.53k | l_sgnd0 = 0; |
2252 | | /* Read the component information */ |
2253 | 38.9k | for (i = 0; i < l_image->numcomps; ++i) { |
2254 | 30.4k | OPJ_UINT32 tmp; |
2255 | 30.4k | opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */ |
2256 | 30.4k | ++p_header_data; |
2257 | 30.4k | l_img_comp->prec = (tmp & 0x7f) + 1; |
2258 | 30.4k | l_img_comp->sgnd = tmp >> 7; |
2259 | | |
2260 | 30.4k | if (p_j2k->dump_state == 0) { |
2261 | 30.4k | if (i == 0) { |
2262 | 8.53k | l_prec0 = l_img_comp->prec; |
2263 | 8.53k | l_sgnd0 = l_img_comp->sgnd; |
2264 | 21.9k | } else if (!l_cp->allow_different_bit_depth_sign |
2265 | 21.9k | && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) { |
2266 | 60 | opj_event_msg(p_manager, EVT_WARNING, |
2267 | 60 | "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n" |
2268 | 60 | " [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0, |
2269 | 60 | i, l_img_comp->prec, l_img_comp->sgnd); |
2270 | 60 | } |
2271 | | /* TODO: we should perhaps also check against JP2 BPCC values */ |
2272 | 30.4k | } |
2273 | 30.4k | opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */ |
2274 | 30.4k | ++p_header_data; |
2275 | 30.4k | l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2276 | 30.4k | opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */ |
2277 | 30.4k | ++p_header_data; |
2278 | 30.4k | l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2279 | 30.4k | if (l_img_comp->dx < 1 || l_img_comp->dx > 255 || |
2280 | 30.4k | l_img_comp->dy < 1 || l_img_comp->dy > 255) { |
2281 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
2282 | 6 | "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n", |
2283 | 6 | i, l_img_comp->dx, l_img_comp->dy); |
2284 | 6 | return OPJ_FALSE; |
2285 | 6 | } |
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 | 30.4k | if (l_img_comp->prec > 31) { |
2290 | 7 | opj_event_msg(p_manager, EVT_ERROR, |
2291 | 7 | "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n", |
2292 | 7 | i, l_img_comp->prec); |
2293 | 7 | return OPJ_FALSE; |
2294 | 7 | } |
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 | 30.4k | l_img_comp->resno_decoded = |
2325 | 30.4k | 0; /* number of resolution decoded */ |
2326 | 30.4k | l_img_comp->factor = |
2327 | 30.4k | l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ |
2328 | 30.4k | ++l_img_comp; |
2329 | 30.4k | } |
2330 | | |
2331 | 8.52k | 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.52k | l_cp->tw = opj_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx); |
2337 | 8.52k | 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.52k | if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { |
2341 | 18 | opj_event_msg(p_manager, EVT_ERROR, |
2342 | 18 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", |
2343 | 18 | l_cp->tw, l_cp->th); |
2344 | 18 | return OPJ_FALSE; |
2345 | 18 | } |
2346 | 8.50k | l_nb_tiles = l_cp->tw * l_cp->th; |
2347 | | |
2348 | | /* Define the tiles which will be decoded */ |
2349 | 8.50k | 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.50k | } else { |
2361 | 8.50k | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
2362 | 8.50k | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
2363 | 8.50k | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
2364 | 8.50k | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
2365 | 8.50k | } |
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.50k | l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); |
2414 | 8.50k | 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.50k | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = |
2434 | 8.50k | (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); |
2435 | 8.50k | 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.50k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = |
2442 | 8.50k | (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS, |
2443 | 8.50k | sizeof(opj_mct_data_t)); |
2444 | | |
2445 | 8.50k | 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.50k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = |
2451 | 8.50k | OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
2452 | | |
2453 | 8.50k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = |
2454 | 8.50k | (opj_simple_mcc_decorrelation_data_t*) |
2455 | 8.50k | opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, |
2456 | 8.50k | sizeof(opj_simple_mcc_decorrelation_data_t)); |
2457 | | |
2458 | 8.50k | 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.50k | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = |
2464 | 8.50k | OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
2465 | | |
2466 | | /* set up default dc level shift */ |
2467 | 38.8k | for (i = 0; i < l_image->numcomps; ++i) { |
2468 | 30.3k | if (! l_image->comps[i].sgnd) { |
2469 | 23.5k | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 |
2470 | 23.5k | << (l_image->comps[i].prec - 1); |
2471 | 23.5k | } |
2472 | 30.3k | } |
2473 | | |
2474 | 8.50k | l_current_tile_param = l_cp->tcps; |
2475 | 18.9M | for (i = 0; i < l_nb_tiles; ++i) { |
2476 | 18.9M | l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, |
2477 | 18.9M | sizeof(opj_tccp_t)); |
2478 | 18.9M | 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 | 18.9M | ++l_current_tile_param; |
2485 | 18.9M | } |
2486 | | |
2487 | | /*Allocate and initialize some elements of codestrem index*/ |
2488 | 8.50k | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { |
2489 | 0 | return OPJ_FALSE; |
2490 | 0 | } |
2491 | | |
2492 | 8.50k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; |
2493 | 8.50k | opj_image_comp_header_update(l_image, l_cp); |
2494 | | |
2495 | 8.50k | return OPJ_TRUE; |
2496 | 8.50k | } |
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.83k | { |
2569 | | /* preconditions */ |
2570 | 2.83k | assert(p_j2k != 00); |
2571 | 2.83k | assert(p_manager != 00); |
2572 | 2.83k | assert(p_header_data != 00); |
2573 | | |
2574 | 2.83k | OPJ_UNUSED(p_j2k); |
2575 | 2.83k | OPJ_UNUSED(p_header_data); |
2576 | 2.83k | OPJ_UNUSED(p_header_size); |
2577 | 2.83k | OPJ_UNUSED(p_manager); |
2578 | | |
2579 | 2.83k | return OPJ_TRUE; |
2580 | 2.83k | } |
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 | 10.4k | { |
2671 | | /* loop */ |
2672 | 10.4k | OPJ_UINT32 i; |
2673 | 10.4k | OPJ_UINT32 l_tmp; |
2674 | 10.4k | opj_cp_t *l_cp = 00; |
2675 | 10.4k | opj_tcp_t *l_tcp = 00; |
2676 | 10.4k | opj_image_t *l_image = 00; |
2677 | | |
2678 | | /* preconditions */ |
2679 | 10.4k | assert(p_header_data != 00); |
2680 | 10.4k | assert(p_j2k != 00); |
2681 | 10.4k | assert(p_manager != 00); |
2682 | | |
2683 | 10.4k | l_image = p_j2k->m_private_image; |
2684 | 10.4k | l_cp = &(p_j2k->m_cp); |
2685 | | |
2686 | | /* If we are in the first tile-part header of the current tile */ |
2687 | 10.4k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
2688 | 1 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2689 | 10.4k | 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 | 10.4k | l_tcp->cod = 1; |
2703 | | |
2704 | | /* Make sure room is sufficient */ |
2705 | 10.4k | if (p_header_size < 5) { |
2706 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2707 | 2 | return OPJ_FALSE; |
2708 | 2 | } |
2709 | | |
2710 | 10.4k | opj_read_bytes(p_header_data, &l_tcp->csty, 1); /* Scod */ |
2711 | 10.4k | ++p_header_data; |
2712 | | /* Make sure we know how to decode this */ |
2713 | 10.4k | if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | |
2714 | 10.4k | J2K_CP_CSTY_EPH)) != 0U) { |
2715 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n"); |
2716 | 5 | return OPJ_FALSE; |
2717 | 5 | } |
2718 | 10.4k | opj_read_bytes(p_header_data, &l_tmp, 1); /* SGcod (A) */ |
2719 | 10.4k | ++p_header_data; |
2720 | 10.4k | l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; |
2721 | | /* Make sure progression order is valid */ |
2722 | 10.4k | if (l_tcp->prg > OPJ_CPRL) { |
2723 | 453 | opj_event_msg(p_manager, EVT_ERROR, |
2724 | 453 | "Unknown progression order in COD marker\n"); |
2725 | 453 | l_tcp->prg = OPJ_PROG_UNKNOWN; |
2726 | 453 | } |
2727 | 10.4k | opj_read_bytes(p_header_data, &l_tcp->numlayers, 2); /* SGcod (B) */ |
2728 | 10.4k | p_header_data += 2; |
2729 | | |
2730 | 10.4k | if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) { |
2731 | 2 | opj_event_msg(p_manager, EVT_ERROR, |
2732 | 2 | "Invalid number of layers in COD marker : %d not in range [1-65535]\n", |
2733 | 2 | l_tcp->numlayers); |
2734 | 2 | return OPJ_FALSE; |
2735 | 2 | } |
2736 | | |
2737 | | /* If user didn't set a number layer to decode take the max specify in the codestream. */ |
2738 | 10.4k | 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 | 10.4k | } else { |
2741 | 10.4k | l_tcp->num_layers_to_decode = l_tcp->numlayers; |
2742 | 10.4k | } |
2743 | | |
2744 | 10.4k | opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */ |
2745 | 10.4k | ++p_header_data; |
2746 | | |
2747 | 10.4k | if (l_tcp->mct > 1) { |
2748 | 10 | opj_event_msg(p_manager, EVT_ERROR, |
2749 | 10 | "Invalid multiple component transformation\n"); |
2750 | 10 | return OPJ_FALSE; |
2751 | 10 | } |
2752 | | |
2753 | 10.4k | p_header_size -= 5; |
2754 | 48.2k | for (i = 0; i < l_image->numcomps; ++i) { |
2755 | 37.8k | l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; |
2756 | 37.8k | } |
2757 | | |
2758 | 10.4k | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size, |
2759 | 10.4k | p_manager)) { |
2760 | 29 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2761 | 29 | return OPJ_FALSE; |
2762 | 29 | } |
2763 | | |
2764 | 10.3k | if (p_header_size != 0) { |
2765 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
2766 | 3 | return OPJ_FALSE; |
2767 | 3 | } |
2768 | | |
2769 | | /* Apply the coding style to other components of the current tile or the m_default_tcp*/ |
2770 | 10.3k | 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 | 10.3k | return OPJ_TRUE; |
2790 | 10.3k | } |
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 | 233 | { |
2948 | 233 | opj_cp_t *l_cp = NULL; |
2949 | 233 | opj_tcp_t *l_tcp = NULL; |
2950 | 233 | opj_image_t *l_image = NULL; |
2951 | 233 | OPJ_UINT32 l_comp_room; |
2952 | 233 | OPJ_UINT32 l_comp_no; |
2953 | | |
2954 | | /* preconditions */ |
2955 | 233 | assert(p_header_data != 00); |
2956 | 233 | assert(p_j2k != 00); |
2957 | 233 | assert(p_manager != 00); |
2958 | | |
2959 | 233 | l_cp = &(p_j2k->m_cp); |
2960 | 233 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
2961 | 233 | ? |
2962 | 25 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2963 | 233 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
2964 | 233 | l_image = p_j2k->m_private_image; |
2965 | | |
2966 | 233 | l_comp_room = l_image->numcomps <= 256 ? 1 : 2; |
2967 | | |
2968 | | /* make sure room is sufficient*/ |
2969 | 233 | if (p_header_size < l_comp_room + 1) { |
2970 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2971 | 3 | return OPJ_FALSE; |
2972 | 3 | } |
2973 | 230 | p_header_size -= l_comp_room + 1; |
2974 | | |
2975 | 230 | opj_read_bytes(p_header_data, &l_comp_no, |
2976 | 230 | l_comp_room); /* Ccoc */ |
2977 | 230 | p_header_data += l_comp_room; |
2978 | 230 | if (l_comp_no >= l_image->numcomps) { |
2979 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
2980 | 6 | "Error reading COC marker (bad number of components)\n"); |
2981 | 6 | return OPJ_FALSE; |
2982 | 6 | } |
2983 | | |
2984 | 224 | opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty, |
2985 | 224 | 1); /* Scoc */ |
2986 | 224 | ++p_header_data ; |
2987 | | |
2988 | 224 | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
2989 | 224 | p_manager)) { |
2990 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
2991 | 12 | return OPJ_FALSE; |
2992 | 12 | } |
2993 | | |
2994 | 212 | 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 | 211 | return OPJ_TRUE; |
2999 | 212 | } |
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 | 11.3k | { |
3075 | | /* preconditions */ |
3076 | 11.3k | assert(p_header_data != 00); |
3077 | 11.3k | assert(p_j2k != 00); |
3078 | 11.3k | assert(p_manager != 00); |
3079 | | |
3080 | 11.3k | if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size, |
3081 | 11.3k | p_manager)) { |
3082 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
3083 | 3 | return OPJ_FALSE; |
3084 | 3 | } |
3085 | | |
3086 | 11.3k | if (p_header_size != 0) { |
3087 | 13 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
3088 | 13 | return OPJ_FALSE; |
3089 | 13 | } |
3090 | | |
3091 | | /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ |
3092 | 11.3k | opj_j2k_copy_tile_quantization_parameters(p_j2k); |
3093 | | |
3094 | 11.3k | return OPJ_TRUE; |
3095 | 11.3k | } |
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 | 1.08k | { |
3217 | 1.08k | OPJ_UINT32 l_num_comp, l_comp_no; |
3218 | | |
3219 | | /* preconditions */ |
3220 | 1.08k | assert(p_header_data != 00); |
3221 | 1.08k | assert(p_j2k != 00); |
3222 | 1.08k | assert(p_manager != 00); |
3223 | | |
3224 | 1.08k | l_num_comp = p_j2k->m_private_image->numcomps; |
3225 | | |
3226 | 1.08k | if (l_num_comp <= 256) { |
3227 | 279 | if (p_header_size < 1) { |
3228 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3229 | 1 | return OPJ_FALSE; |
3230 | 1 | } |
3231 | 278 | opj_read_bytes(p_header_data, &l_comp_no, 1); |
3232 | 278 | ++p_header_data; |
3233 | 278 | --p_header_size; |
3234 | 804 | } else { |
3235 | 804 | if (p_header_size < 2) { |
3236 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3237 | 1 | return OPJ_FALSE; |
3238 | 1 | } |
3239 | 803 | opj_read_bytes(p_header_data, &l_comp_no, 2); |
3240 | 803 | p_header_data += 2; |
3241 | 803 | p_header_size -= 2; |
3242 | 803 | } |
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 | 1.08k | if (l_comp_no >= p_j2k->m_private_image->numcomps) { |
3271 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
3272 | 4 | "Invalid component number: %d, regarding the number of components %d\n", |
3273 | 4 | l_comp_no, p_j2k->m_private_image->numcomps); |
3274 | 4 | return OPJ_FALSE; |
3275 | 4 | } |
3276 | | |
3277 | 1.07k | if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
3278 | 1.07k | p_manager)) { |
3279 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
3280 | 2 | return OPJ_FALSE; |
3281 | 2 | } |
3282 | | |
3283 | 1.07k | 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 | 1.07k | return OPJ_TRUE; |
3289 | 1.07k | } |
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.21k | { |
3532 | 1.21k | OPJ_UINT32 i, l_nb_comp, l_tmp; |
3533 | 1.21k | opj_image_t * l_image = 00; |
3534 | 1.21k | OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; |
3535 | 1.21k | OPJ_UINT32 l_chunk_size, l_comp_room; |
3536 | | |
3537 | 1.21k | opj_cp_t *l_cp = 00; |
3538 | 1.21k | opj_tcp_t *l_tcp = 00; |
3539 | 1.21k | opj_poc_t *l_current_poc = 00; |
3540 | | |
3541 | | /* preconditions */ |
3542 | 1.21k | assert(p_header_data != 00); |
3543 | 1.21k | assert(p_j2k != 00); |
3544 | 1.21k | assert(p_manager != 00); |
3545 | | |
3546 | 1.21k | l_image = p_j2k->m_private_image; |
3547 | 1.21k | l_nb_comp = l_image->numcomps; |
3548 | 1.21k | if (l_nb_comp <= 256) { |
3549 | 1.16k | l_comp_room = 1; |
3550 | 1.16k | } else { |
3551 | 57 | l_comp_room = 2; |
3552 | 57 | } |
3553 | 1.21k | l_chunk_size = 5 + 2 * l_comp_room; |
3554 | 1.21k | l_current_poc_nb = p_header_size / l_chunk_size; |
3555 | 1.21k | l_current_poc_remaining = p_header_size % l_chunk_size; |
3556 | | |
3557 | 1.21k | if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { |
3558 | 5 | opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n"); |
3559 | 5 | return OPJ_FALSE; |
3560 | 5 | } |
3561 | | |
3562 | 1.21k | l_cp = &(p_j2k->m_cp); |
3563 | 1.21k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
3564 | 55 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
3565 | 1.21k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
3566 | 1.21k | l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; |
3567 | 1.21k | l_current_poc_nb += l_old_poc_nb; |
3568 | | |
3569 | 1.21k | if (l_current_poc_nb >= J2K_MAX_POCS) { |
3570 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb); |
3571 | 2 | return OPJ_FALSE; |
3572 | 2 | } |
3573 | | |
3574 | | /* now poc is in use.*/ |
3575 | 1.21k | l_tcp->POC = 1; |
3576 | | |
3577 | 1.21k | l_current_poc = &l_tcp->pocs[l_old_poc_nb]; |
3578 | 5.41k | for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { |
3579 | 4.20k | opj_read_bytes(p_header_data, &(l_current_poc->resno0), |
3580 | 4.20k | 1); /* RSpoc_i */ |
3581 | 4.20k | ++p_header_data; |
3582 | 4.20k | opj_read_bytes(p_header_data, &(l_current_poc->compno0), |
3583 | 4.20k | l_comp_room); /* CSpoc_i */ |
3584 | 4.20k | p_header_data += l_comp_room; |
3585 | 4.20k | opj_read_bytes(p_header_data, &(l_current_poc->layno1), |
3586 | 4.20k | 2); /* LYEpoc_i */ |
3587 | | /* make sure layer end is in acceptable bounds */ |
3588 | 4.20k | l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers); |
3589 | 4.20k | p_header_data += 2; |
3590 | 4.20k | opj_read_bytes(p_header_data, &(l_current_poc->resno1), |
3591 | 4.20k | 1); /* REpoc_i */ |
3592 | 4.20k | ++p_header_data; |
3593 | 4.20k | opj_read_bytes(p_header_data, &(l_current_poc->compno1), |
3594 | 4.20k | l_comp_room); /* CEpoc_i */ |
3595 | 4.20k | p_header_data += l_comp_room; |
3596 | 4.20k | opj_read_bytes(p_header_data, &l_tmp, |
3597 | 4.20k | 1); /* Ppoc_i */ |
3598 | 4.20k | ++p_header_data; |
3599 | 4.20k | l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; |
3600 | | /* make sure comp is in acceptable bounds */ |
3601 | 4.20k | l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp); |
3602 | 4.20k | ++l_current_poc; |
3603 | 4.20k | } |
3604 | | |
3605 | 1.21k | l_tcp->numpocs = l_current_poc_nb - 1; |
3606 | 1.21k | return OPJ_TRUE; |
3607 | 1.21k | } |
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 | 69 | { |
3623 | 69 | OPJ_UINT32 l_nb_comp; |
3624 | | /* preconditions */ |
3625 | 69 | assert(p_header_data != 00); |
3626 | 69 | assert(p_j2k != 00); |
3627 | 69 | assert(p_manager != 00); |
3628 | | |
3629 | 69 | OPJ_UNUSED(p_header_data); |
3630 | | |
3631 | 69 | l_nb_comp = p_j2k->m_private_image->numcomps; |
3632 | | |
3633 | 69 | if (p_header_size != l_nb_comp * 4) { |
3634 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n"); |
3635 | 2 | return OPJ_FALSE; |
3636 | 2 | } |
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 | 67 | return OPJ_TRUE; |
3649 | 69 | } |
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 | 196 | { |
3665 | 196 | OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, |
3666 | 196 | l_Ptlm_size, l_entry_size, l_num_tileparts; |
3667 | 196 | OPJ_UINT32 i; |
3668 | 196 | opj_j2k_tlm_tile_part_info_t* l_tile_part_infos; |
3669 | 196 | opj_j2k_tlm_info_t* l_tlm; |
3670 | | |
3671 | | /* preconditions */ |
3672 | 196 | assert(p_header_data != 00); |
3673 | 196 | assert(p_j2k != 00); |
3674 | 196 | assert(p_manager != 00); |
3675 | | |
3676 | 196 | l_tlm = &(p_j2k->m_specific_param.m_decoder.m_tlm); |
3677 | | |
3678 | 196 | if (p_header_size < 2) { |
3679 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker.\n"); |
3680 | 2 | return OPJ_FALSE; |
3681 | 2 | } |
3682 | 194 | p_header_size -= 2; |
3683 | | |
3684 | 194 | if (l_tlm->m_is_invalid) { |
3685 | 40 | return OPJ_TRUE; |
3686 | 40 | } |
3687 | | |
3688 | 154 | opj_read_bytes(p_header_data, &l_Ztlm, |
3689 | 154 | 1); /* Ztlm */ |
3690 | 154 | ++p_header_data; |
3691 | 154 | opj_read_bytes(p_header_data, &l_Stlm, |
3692 | 154 | 1); /* Stlm */ |
3693 | 154 | ++p_header_data; |
3694 | | |
3695 | 154 | l_ST = ((l_Stlm >> 4) & 0x3); |
3696 | 154 | if (l_ST == 3) { |
3697 | 3 | l_tlm->m_is_invalid = OPJ_TRUE; |
3698 | 3 | opj_event_msg(p_manager, EVT_WARNING, |
3699 | 3 | "opj_j2k_read_tlm(): ST = 3 is invalid.\n"); |
3700 | 3 | return OPJ_TRUE; |
3701 | 3 | } |
3702 | 151 | l_SP = (l_Stlm >> 6) & 0x1; |
3703 | | |
3704 | 151 | l_Ptlm_size = (l_SP + 1) * 2; |
3705 | 151 | l_entry_size = l_Ptlm_size + l_ST; |
3706 | | |
3707 | 151 | if ((p_header_size % l_entry_size) != 0) { |
3708 | 1 | l_tlm->m_is_invalid = OPJ_TRUE; |
3709 | 1 | opj_event_msg(p_manager, EVT_WARNING, |
3710 | 1 | "opj_j2k_read_tlm(): TLM marker not of expected size.\n"); |
3711 | 1 | return OPJ_TRUE; |
3712 | 1 | } |
3713 | | |
3714 | 150 | l_num_tileparts = p_header_size / l_entry_size; |
3715 | 150 | if (l_num_tileparts == 0) { |
3716 | | /* not totally sure if this is valid... */ |
3717 | 134 | return OPJ_TRUE; |
3718 | 134 | } |
3719 | | |
3720 | | /* Highly unlikely, unless there are gazillions of TLM markers */ |
3721 | 16 | if (l_tlm->m_entries_count > UINT32_MAX - l_num_tileparts || |
3722 | 16 | l_tlm->m_entries_count + l_num_tileparts > UINT32_MAX / sizeof( |
3723 | 16 | 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 | 16 | l_tile_part_infos = (opj_j2k_tlm_tile_part_info_t*)opj_realloc( |
3731 | 16 | l_tlm->m_tile_part_infos, |
3732 | 16 | (l_tlm->m_entries_count + l_num_tileparts) * sizeof( |
3733 | 16 | opj_j2k_tlm_tile_part_info_t)); |
3734 | 16 | 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 | 16 | l_tlm->m_tile_part_infos = l_tile_part_infos; |
3742 | | |
3743 | 226 | for (i = 0; i < l_num_tileparts; ++ i) { |
3744 | 220 | OPJ_UINT32 l_tile_index; |
3745 | 220 | OPJ_UINT32 l_length; |
3746 | | |
3747 | | /* Read Ttlm_i */ |
3748 | 220 | if (l_ST == 0) { |
3749 | 178 | l_tile_index = l_tlm->m_entries_count; |
3750 | 178 | } else { |
3751 | 42 | opj_read_bytes(p_header_data, &l_tile_index, l_ST); |
3752 | 42 | p_header_data += l_ST; |
3753 | 42 | } |
3754 | | |
3755 | 220 | if (l_tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) { |
3756 | 10 | l_tlm->m_is_invalid = OPJ_TRUE; |
3757 | 10 | opj_event_msg(p_manager, EVT_WARNING, |
3758 | 10 | "opj_j2k_read_tlm(): invalid tile number %d\n", |
3759 | 10 | l_tile_index); |
3760 | 10 | return OPJ_TRUE; |
3761 | 10 | } |
3762 | | |
3763 | | /* Read Ptlm_i */ |
3764 | 210 | opj_read_bytes(p_header_data, &l_length, l_Ptlm_size); |
3765 | 210 | p_header_data += l_Ptlm_size; |
3766 | | |
3767 | 210 | l_tile_part_infos[l_tlm->m_entries_count].m_tile_index = |
3768 | 210 | (OPJ_UINT16)l_tile_index; |
3769 | 210 | l_tile_part_infos[l_tlm->m_entries_count].m_length = l_length; |
3770 | 210 | ++l_tlm->m_entries_count; |
3771 | 210 | } |
3772 | | |
3773 | 6 | return OPJ_TRUE; |
3774 | 16 | } |
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 | 7.12k | { |
3790 | | /* preconditions */ |
3791 | 7.12k | assert(p_header_data != 00); |
3792 | 7.12k | assert(p_j2k != 00); |
3793 | 7.12k | assert(p_manager != 00); |
3794 | | |
3795 | 7.12k | OPJ_UNUSED(p_j2k); |
3796 | 7.12k | OPJ_UNUSED(p_header_data); |
3797 | | |
3798 | 7.12k | 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 | 7.12k | return OPJ_TRUE; |
3847 | 7.12k | } |
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 | 506 | { |
3863 | 506 | OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; |
3864 | | |
3865 | | /* preconditions */ |
3866 | 506 | assert(p_header_data != 00); |
3867 | 506 | assert(p_j2k != 00); |
3868 | 506 | assert(p_manager != 00); |
3869 | | |
3870 | 506 | OPJ_UNUSED(p_j2k); |
3871 | | |
3872 | 506 | if (p_header_size < 1) { |
3873 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
3874 | 1 | return OPJ_FALSE; |
3875 | 1 | } |
3876 | | |
3877 | 505 | opj_read_bytes(p_header_data, &l_Zplt, 1); /* Zplt */ |
3878 | 505 | ++p_header_data; |
3879 | 505 | --p_header_size; |
3880 | | |
3881 | 64.6k | for (i = 0; i < p_header_size; ++i) { |
3882 | 64.1k | opj_read_bytes(p_header_data, &l_tmp, 1); /* Iplt_ij */ |
3883 | 64.1k | ++p_header_data; |
3884 | | /* take only the last seven bytes */ |
3885 | 64.1k | l_packet_len |= (l_tmp & 0x7f); |
3886 | 64.1k | if (l_tmp & 0x80) { |
3887 | 18.5k | l_packet_len <<= 7; |
3888 | 45.5k | } else { |
3889 | | /* store packet length and proceed to next packet */ |
3890 | 45.5k | l_packet_len = 0; |
3891 | 45.5k | } |
3892 | 64.1k | } |
3893 | | |
3894 | 505 | if (l_packet_len != 0) { |
3895 | 4 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
3896 | 4 | return OPJ_FALSE; |
3897 | 4 | } |
3898 | | |
3899 | 501 | return OPJ_TRUE; |
3900 | 505 | } |
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 | 500 | { |
3917 | 500 | opj_cp_t *l_cp = 00; |
3918 | 500 | OPJ_UINT32 l_Z_ppm; |
3919 | | |
3920 | | /* preconditions */ |
3921 | 500 | assert(p_header_data != 00); |
3922 | 500 | assert(p_j2k != 00); |
3923 | 500 | assert(p_manager != 00); |
3924 | | |
3925 | | /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */ |
3926 | 500 | if (p_header_size < 2) { |
3927 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
3928 | 2 | return OPJ_FALSE; |
3929 | 2 | } |
3930 | | |
3931 | 498 | l_cp = &(p_j2k->m_cp); |
3932 | 498 | l_cp->ppm = 1; |
3933 | | |
3934 | 498 | opj_read_bytes(p_header_data, &l_Z_ppm, 1); /* Z_ppm */ |
3935 | 498 | ++p_header_data; |
3936 | 498 | --p_header_size; |
3937 | | |
3938 | | /* check allocation needed */ |
3939 | 498 | if (l_cp->ppm_markers == NULL) { /* first PPM marker */ |
3940 | 116 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3941 | 116 | assert(l_cp->ppm_markers_count == 0U); |
3942 | | |
3943 | 116 | l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
3944 | 116 | 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 | 116 | l_cp->ppm_markers_count = l_newCount; |
3949 | 382 | } else if (l_cp->ppm_markers_count <= l_Z_ppm) { |
3950 | 183 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3951 | 183 | opj_ppx *new_ppm_markers; |
3952 | 183 | new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers, |
3953 | 183 | l_newCount * sizeof(opj_ppx)); |
3954 | 183 | 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 | 183 | l_cp->ppm_markers = new_ppm_markers; |
3960 | 183 | memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0, |
3961 | 183 | (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx)); |
3962 | 183 | l_cp->ppm_markers_count = l_newCount; |
3963 | 183 | } |
3964 | | |
3965 | 498 | if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) { |
3966 | | /* clean up to be done on l_cp destruction */ |
3967 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm); |
3968 | 2 | return OPJ_FALSE; |
3969 | 2 | } |
3970 | | |
3971 | 496 | l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
3972 | 496 | 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 | 496 | l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size; |
3978 | 496 | memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size); |
3979 | | |
3980 | 496 | return OPJ_TRUE; |
3981 | 496 | } |
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.54k | { |
3991 | 7.54k | OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining; |
3992 | | |
3993 | | /* preconditions */ |
3994 | 7.54k | assert(p_cp != 00); |
3995 | 7.54k | assert(p_manager != 00); |
3996 | 7.54k | assert(p_cp->ppm_buffer == NULL); |
3997 | | |
3998 | 7.54k | if (p_cp->ppm == 0U) { |
3999 | 7.45k | return OPJ_TRUE; |
4000 | 7.45k | } |
4001 | | |
4002 | 87 | l_ppm_data_size = 0U; |
4003 | 87 | l_N_ppm_remaining = 0U; |
4004 | 8.24k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
4005 | 8.16k | if (p_cp->ppm_markers[i].m_data != |
4006 | 8.16k | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
4007 | 397 | OPJ_UINT32 l_N_ppm; |
4008 | 397 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
4009 | 397 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
4010 | | |
4011 | 397 | if (l_N_ppm_remaining >= l_data_size) { |
4012 | 157 | l_N_ppm_remaining -= l_data_size; |
4013 | 157 | l_data_size = 0U; |
4014 | 240 | } else { |
4015 | 240 | l_data += l_N_ppm_remaining; |
4016 | 240 | l_data_size -= l_N_ppm_remaining; |
4017 | 240 | l_N_ppm_remaining = 0U; |
4018 | 240 | } |
4019 | | |
4020 | 397 | if (l_data_size > 0U) { |
4021 | 697 | do { |
4022 | | /* read Nppm */ |
4023 | 697 | if (l_data_size < 4U) { |
4024 | | /* clean up to be done on l_cp destruction */ |
4025 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); |
4026 | 2 | return OPJ_FALSE; |
4027 | 2 | } |
4028 | 695 | opj_read_bytes(l_data, &l_N_ppm, 4); |
4029 | 695 | l_data += 4; |
4030 | 695 | l_data_size -= 4; |
4031 | | |
4032 | 695 | 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 | 694 | l_ppm_data_size += l_N_ppm; |
4037 | 694 | if (l_data_size >= l_N_ppm) { |
4038 | 617 | l_data_size -= l_N_ppm; |
4039 | 617 | l_data += l_N_ppm; |
4040 | 617 | } else { |
4041 | 77 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
4042 | 77 | l_data_size = 0U; |
4043 | 77 | } |
4044 | 694 | } while (l_data_size > 0U); |
4045 | 240 | } |
4046 | 397 | } |
4047 | 8.16k | } |
4048 | | |
4049 | 84 | if (l_N_ppm_remaining != 0U) { |
4050 | | /* clean up to be done on l_cp destruction */ |
4051 | 54 | opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n"); |
4052 | 54 | return OPJ_FALSE; |
4053 | 54 | } |
4054 | | |
4055 | 30 | p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size); |
4056 | 30 | if (p_cp->ppm_buffer == 00) { |
4057 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); |
4058 | 1 | return OPJ_FALSE; |
4059 | 1 | } |
4060 | 29 | p_cp->ppm_len = l_ppm_data_size; |
4061 | 29 | l_ppm_data_size = 0U; |
4062 | 29 | l_N_ppm_remaining = 0U; |
4063 | 3.07k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
4064 | 3.04k | if (p_cp->ppm_markers[i].m_data != |
4065 | 3.04k | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
4066 | 181 | OPJ_UINT32 l_N_ppm; |
4067 | 181 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
4068 | 181 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
4069 | | |
4070 | 181 | if (l_N_ppm_remaining >= l_data_size) { |
4071 | 24 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
4072 | 24 | l_ppm_data_size += l_data_size; |
4073 | 24 | l_N_ppm_remaining -= l_data_size; |
4074 | 24 | l_data_size = 0U; |
4075 | 157 | } else { |
4076 | 157 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining); |
4077 | 157 | l_ppm_data_size += l_N_ppm_remaining; |
4078 | 157 | l_data += l_N_ppm_remaining; |
4079 | 157 | l_data_size -= l_N_ppm_remaining; |
4080 | 157 | l_N_ppm_remaining = 0U; |
4081 | 157 | } |
4082 | | |
4083 | 181 | if (l_data_size > 0U) { |
4084 | 240 | do { |
4085 | | /* read Nppm */ |
4086 | 240 | 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 | 240 | opj_read_bytes(l_data, &l_N_ppm, 4); |
4092 | 240 | l_data += 4; |
4093 | 240 | l_data_size -= 4; |
4094 | | |
4095 | 240 | if (l_data_size >= l_N_ppm) { |
4096 | 222 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm); |
4097 | 222 | l_ppm_data_size += l_N_ppm; |
4098 | 222 | l_data_size -= l_N_ppm; |
4099 | 222 | l_data += l_N_ppm; |
4100 | 222 | } else { |
4101 | 18 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
4102 | 18 | l_ppm_data_size += l_data_size; |
4103 | 18 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
4104 | 18 | l_data_size = 0U; |
4105 | 18 | } |
4106 | 240 | } while (l_data_size > 0U); |
4107 | 157 | } |
4108 | 181 | opj_free(p_cp->ppm_markers[i].m_data); |
4109 | 181 | p_cp->ppm_markers[i].m_data = NULL; |
4110 | 181 | p_cp->ppm_markers[i].m_data_size = 0U; |
4111 | 181 | } |
4112 | 3.04k | } |
4113 | | |
4114 | 29 | p_cp->ppm_data = p_cp->ppm_buffer; |
4115 | 29 | p_cp->ppm_data_size = p_cp->ppm_len; |
4116 | | |
4117 | 29 | p_cp->ppm_markers_count = 0U; |
4118 | 29 | opj_free(p_cp->ppm_markers); |
4119 | 29 | p_cp->ppm_markers = NULL; |
4120 | | |
4121 | 29 | return OPJ_TRUE; |
4122 | 29 | } |
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 | 137 | { |
4138 | 137 | opj_cp_t *l_cp = 00; |
4139 | 137 | opj_tcp_t *l_tcp = 00; |
4140 | 137 | OPJ_UINT32 l_Z_ppt; |
4141 | | |
4142 | | /* preconditions */ |
4143 | 137 | assert(p_header_data != 00); |
4144 | 137 | assert(p_j2k != 00); |
4145 | 137 | assert(p_manager != 00); |
4146 | | |
4147 | | /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */ |
4148 | 137 | 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 | 136 | l_cp = &(p_j2k->m_cp); |
4154 | 136 | 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 | 135 | l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); |
4161 | 135 | l_tcp->ppt = 1; |
4162 | | |
4163 | 135 | opj_read_bytes(p_header_data, &l_Z_ppt, 1); /* Z_ppt */ |
4164 | 135 | ++p_header_data; |
4165 | 135 | --p_header_size; |
4166 | | |
4167 | | /* check allocation needed */ |
4168 | 135 | if (l_tcp->ppt_markers == NULL) { /* first PPT marker */ |
4169 | 109 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4170 | 109 | assert(l_tcp->ppt_markers_count == 0U); |
4171 | | |
4172 | 109 | l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
4173 | 109 | 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 | 109 | l_tcp->ppt_markers_count = l_newCount; |
4178 | 109 | } else if (l_tcp->ppt_markers_count <= l_Z_ppt) { |
4179 | 14 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4180 | 14 | opj_ppx *new_ppt_markers; |
4181 | 14 | new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers, |
4182 | 14 | l_newCount * sizeof(opj_ppx)); |
4183 | 14 | 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 | 14 | l_tcp->ppt_markers = new_ppt_markers; |
4189 | 14 | memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0, |
4190 | 14 | (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx)); |
4191 | 14 | l_tcp->ppt_markers_count = l_newCount; |
4192 | 14 | } |
4193 | | |
4194 | 135 | if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) { |
4195 | | /* clean up to be done on l_tcp destruction */ |
4196 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt); |
4197 | 1 | return OPJ_FALSE; |
4198 | 1 | } |
4199 | | |
4200 | 134 | l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
4201 | 134 | 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 | 134 | l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size; |
4207 | 134 | memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size); |
4208 | 134 | return OPJ_TRUE; |
4209 | 134 | } |
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 | 8.62k | { |
4219 | 8.62k | OPJ_UINT32 i, l_ppt_data_size; |
4220 | | /* preconditions */ |
4221 | 8.62k | assert(p_tcp != 00); |
4222 | 8.62k | assert(p_manager != 00); |
4223 | | |
4224 | 8.62k | 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 | 8.62k | if (p_tcp->ppt == 0U) { |
4231 | 8.55k | return OPJ_TRUE; |
4232 | 8.55k | } |
4233 | | |
4234 | 69 | l_ppt_data_size = 0U; |
4235 | 2.36k | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4236 | 2.29k | l_ppt_data_size += |
4237 | 2.29k | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4238 | 2.29k | } |
4239 | | |
4240 | 69 | p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size); |
4241 | 69 | 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 | 69 | p_tcp->ppt_len = l_ppt_data_size; |
4246 | 69 | l_ppt_data_size = 0U; |
4247 | 2.36k | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4248 | 2.29k | if (p_tcp->ppt_markers[i].m_data != |
4249 | 2.29k | NULL) { /* standard doesn't seem to require contiguous Zppt */ |
4250 | 69 | memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data, |
4251 | 69 | p_tcp->ppt_markers[i].m_data_size); |
4252 | 69 | l_ppt_data_size += |
4253 | 69 | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4254 | | |
4255 | 69 | opj_free(p_tcp->ppt_markers[i].m_data); |
4256 | 69 | p_tcp->ppt_markers[i].m_data = NULL; |
4257 | 69 | p_tcp->ppt_markers[i].m_data_size = 0U; |
4258 | 69 | } |
4259 | 2.29k | } |
4260 | | |
4261 | 69 | p_tcp->ppt_markers_count = 0U; |
4262 | 69 | opj_free(p_tcp->ppt_markers); |
4263 | 69 | p_tcp->ppt_markers = NULL; |
4264 | | |
4265 | 69 | p_tcp->ppt_data = p_tcp->ppt_buffer; |
4266 | 69 | p_tcp->ppt_data_size = p_tcp->ppt_len; |
4267 | 69 | return OPJ_TRUE; |
4268 | 69 | } |
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 | 12.5k | { |
4423 | | /* preconditions */ |
4424 | 12.5k | assert(p_header_data != 00); |
4425 | 12.5k | assert(p_manager != 00); |
4426 | | |
4427 | | /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ |
4428 | 12.5k | if (p_header_size != 8) { |
4429 | 8 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); |
4430 | 8 | return OPJ_FALSE; |
4431 | 8 | } |
4432 | | |
4433 | 12.5k | opj_read_bytes(p_header_data, p_tile_no, 2); /* Isot */ |
4434 | 12.5k | p_header_data += 2; |
4435 | 12.5k | opj_read_bytes(p_header_data, p_tot_len, 4); /* Psot */ |
4436 | 12.5k | p_header_data += 4; |
4437 | 12.5k | opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */ |
4438 | 12.5k | ++p_header_data; |
4439 | 12.5k | opj_read_bytes(p_header_data, p_num_parts, 1); /* TNsot */ |
4440 | 12.5k | ++p_header_data; |
4441 | 12.5k | return OPJ_TRUE; |
4442 | 12.5k | } |
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.57k | { |
4449 | 9.57k | opj_cp_t *l_cp = 00; |
4450 | 9.57k | opj_tcp_t *l_tcp = 00; |
4451 | 9.57k | OPJ_UINT32 l_tot_len, l_num_parts = 0; |
4452 | 9.57k | OPJ_UINT32 l_current_part; |
4453 | 9.57k | OPJ_UINT32 l_tile_x, l_tile_y; |
4454 | | |
4455 | | /* preconditions */ |
4456 | | |
4457 | 9.57k | assert(p_j2k != 00); |
4458 | 9.57k | assert(p_manager != 00); |
4459 | | |
4460 | 9.57k | if (! opj_j2k_get_sot_values(p_header_data, p_header_size, |
4461 | 9.57k | &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts, |
4462 | 9.57k | p_manager)) { |
4463 | 8 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); |
4464 | 8 | return OPJ_FALSE; |
4465 | 8 | } |
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.56k | l_cp = &(p_j2k->m_cp); |
4472 | | |
4473 | | /* testcase 2.pdf.SIGFPE.706.1112 */ |
4474 | 9.56k | if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { |
4475 | 49 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", |
4476 | 49 | p_j2k->m_current_tile_number); |
4477 | 49 | return OPJ_FALSE; |
4478 | 49 | } |
4479 | | |
4480 | 9.51k | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
4481 | 9.51k | l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; |
4482 | 9.51k | l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; |
4483 | | |
4484 | 9.51k | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 || |
4485 | 9.51k | p_j2k->m_current_tile_number == (OPJ_UINT32) |
4486 | 9.51k | 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.51k | if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) { |
4498 | 74 | opj_event_msg(p_manager, EVT_ERROR, |
4499 | 74 | "Invalid tile part index for tile number %d. " |
4500 | 74 | "Got %d, expected %d\n", |
4501 | 74 | p_j2k->m_current_tile_number, |
4502 | 74 | l_current_part, |
4503 | 74 | l_tcp->m_current_tile_part_number + 1); |
4504 | 74 | return OPJ_FALSE; |
4505 | 74 | } |
4506 | 9.51k | } |
4507 | | |
4508 | 9.44k | 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.44k | if ((l_tot_len != 0) && (l_tot_len < 14)) { |
4543 | 4 | if (l_tot_len == |
4544 | 4 | 12) { /* MSD: Special case for the PHR data which are read by kakadu*/ |
4545 | 1 | opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", |
4546 | 1 | l_tot_len); |
4547 | 3 | } else { |
4548 | 3 | opj_event_msg(p_manager, EVT_ERROR, |
4549 | 3 | "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len); |
4550 | 3 | return OPJ_FALSE; |
4551 | 3 | } |
4552 | 4 | } |
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.44k | if (!l_tot_len) { |
4579 | 6.43k | opj_event_msg(p_manager, EVT_INFO, |
4580 | 6.43k | "Psot value of the current tile-part is equal to zero, " |
4581 | 6.43k | "we assuming it is the last tile-part of the codestream.\n"); |
4582 | 6.43k | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4583 | 6.43k | } |
4584 | | |
4585 | 9.44k | 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 | 2 | opj_event_msg(p_manager, EVT_ERROR, |
4588 | 2 | "In SOT marker, TPSot (%d) is not valid regards to the previous " |
4589 | 2 | "number of tile-part (%d), giving up\n", l_current_part, |
4590 | 2 | l_tcp->m_nb_tile_parts); |
4591 | 2 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4592 | 2 | return OPJ_FALSE; |
4593 | 2 | } |
4594 | | |
4595 | 9.43k | if (l_num_parts != |
4596 | 9.43k | 0) { /* Number of tile-part header is provided by this tile-part header */ |
4597 | 9.30k | 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.30k | if (l_tcp->m_nb_tile_parts) { |
4601 | 35 | 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 | 35 | } |
4610 | 9.30k | 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.30k | l_tcp->m_nb_tile_parts = l_num_parts; |
4619 | 9.30k | } |
4620 | | |
4621 | | /* If know the number of tile part header we will check if we didn't read the last*/ |
4622 | 9.43k | if (l_tcp->m_nb_tile_parts) { |
4623 | 9.30k | if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) { |
4624 | 2.60k | p_j2k->m_specific_param.m_decoder.m_can_decode = |
4625 | 2.60k | 1; /* Process the last tile-part header*/ |
4626 | 2.60k | } |
4627 | 9.30k | } |
4628 | | |
4629 | 9.43k | 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.00k | p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - |
4632 | 3.00k | 12; /* SOT_marker_size = 12 */ |
4633 | 6.43k | } else { |
4634 | | /* FIXME: need to be computed from the number of bytes remaining in the codestream */ |
4635 | 6.43k | p_j2k->m_specific_param.m_decoder.m_sot_length = 0; |
4636 | 6.43k | } |
4637 | | |
4638 | 9.43k | 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.43k | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { |
4642 | 9.43k | p_j2k->m_specific_param.m_decoder.m_skip_data = |
4643 | 9.43k | (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) |
4644 | 9.43k | || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) |
4645 | 9.43k | || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) |
4646 | 9.43k | || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); |
4647 | 9.43k | } 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.43k | { |
4656 | 9.43k | assert(p_j2k->cstr_index->tile_index != 00); |
4657 | 9.43k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = |
4658 | 9.43k | p_j2k->m_current_tile_number; |
4659 | 9.43k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = |
4660 | 9.43k | l_current_part; |
4661 | | |
4662 | 9.43k | if (!p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid && |
4663 | 9.43k | l_num_parts > |
4664 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps) { |
4665 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
4666 | 0 | "SOT marker for tile %u declares more tile-parts than found in TLM marker.", |
4667 | 0 | p_j2k->m_current_tile_number); |
4668 | 0 | p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid = OPJ_TRUE; |
4669 | 0 | } |
4670 | | |
4671 | 9.43k | if (!p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid) { |
4672 | | /* do nothing */ |
4673 | 9.43k | } else if (l_num_parts != 0) { |
4674 | | |
4675 | 9.30k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = |
4676 | 9.30k | l_num_parts; |
4677 | 9.30k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = |
4678 | 9.30k | l_num_parts; |
4679 | | |
4680 | 9.30k | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4681 | 9.27k | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4682 | 9.27k | (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); |
4683 | 9.27k | 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.27k | } else { |
4689 | 34 | opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( |
4690 | 34 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
4691 | 34 | l_num_parts * sizeof(opj_tp_index_t)); |
4692 | 34 | 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 | 34 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4700 | 34 | new_tp_index; |
4701 | 34 | } |
4702 | 9.30k | } else { |
4703 | 132 | /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { |
4704 | | |
4705 | 132 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4706 | 131 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; |
4707 | 131 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4708 | 131 | (opj_tp_index_t*)opj_calloc( |
4709 | 131 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, |
4710 | 131 | sizeof(opj_tp_index_t)); |
4711 | 131 | 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 | 131 | } |
4718 | | |
4719 | 132 | if (l_current_part >= |
4720 | 132 | 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 | 132 | } |
4740 | | |
4741 | 132 | } |
4742 | | |
4743 | 9.43k | } |
4744 | | |
4745 | 9.43k | return OPJ_TRUE; |
4746 | 9.43k | } |
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 | 10.5k | { |
4983 | 10.5k | OPJ_SIZE_T l_current_read_size; |
4984 | 10.5k | opj_codestream_index_t * l_cstr_index = 00; |
4985 | 10.5k | OPJ_BYTE ** l_current_data = 00; |
4986 | 10.5k | opj_tcp_t * l_tcp = 00; |
4987 | 10.5k | OPJ_UINT32 * l_tile_len = 00; |
4988 | 10.5k | OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; |
4989 | | |
4990 | | /* preconditions */ |
4991 | 10.5k | assert(p_j2k != 00); |
4992 | 10.5k | assert(p_manager != 00); |
4993 | 10.5k | assert(p_stream != 00); |
4994 | | |
4995 | 10.5k | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
4996 | | |
4997 | 10.5k | 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.40k | p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)( |
5003 | 6.40k | opj_stream_get_number_byte_left(p_stream) - 2); |
5004 | 6.40k | } else { |
5005 | | /* Check to avoid pass the limit of OPJ_UINT32 */ |
5006 | 4.15k | if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) { |
5007 | 2.68k | p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; |
5008 | 2.68k | } else { |
5009 | | /* MSD: case commented to support empty SOT marker (PHR data) */ |
5010 | 1.47k | } |
5011 | 4.15k | } |
5012 | | |
5013 | 10.5k | l_current_data = &(l_tcp->m_data); |
5014 | 10.5k | l_tile_len = &l_tcp->m_data_size; |
5015 | | |
5016 | | /* Patch to support new PHR data */ |
5017 | 10.5k | 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.05k | if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > |
5021 | 9.05k | opj_stream_get_number_byte_left(p_stream)) { |
5022 | 115 | if (p_j2k->m_cp.strict) { |
5023 | 115 | opj_event_msg(p_manager, EVT_ERROR, |
5024 | 115 | "Tile part length size inconsistent with stream length\n"); |
5025 | 115 | return OPJ_FALSE; |
5026 | 115 | } else { |
5027 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
5028 | 0 | "Tile part length size inconsistent with stream length\n"); |
5029 | 0 | } |
5030 | 115 | } |
5031 | 8.94k | if (p_j2k->m_specific_param.m_decoder.m_sot_length > |
5032 | 8.94k | 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 | 8.94k | 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 | 8.80k | *l_current_data = (OPJ_BYTE*) opj_malloc( |
5046 | 8.80k | p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA); |
5047 | 8.80k | } else { |
5048 | 135 | OPJ_BYTE *l_new_current_data; |
5049 | 135 | if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - |
5050 | 135 | 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 | 135 | l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, |
5058 | 135 | *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length + |
5059 | 135 | OPJ_COMMON_CBLK_DATA_EXTRA); |
5060 | 135 | 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 | 135 | *l_current_data = l_new_current_data; |
5068 | 135 | } |
5069 | | |
5070 | 8.94k | 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 | 8.94k | } else { |
5075 | 1.49k | l_sot_length_pb_detected = OPJ_TRUE; |
5076 | 1.49k | } |
5077 | | |
5078 | | /* Index */ |
5079 | 10.4k | l_cstr_index = p_j2k->cstr_index; |
5080 | 10.4k | { |
5081 | 10.4k | OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; |
5082 | | |
5083 | 10.4k | OPJ_UINT32 l_current_tile_part = |
5084 | 10.4k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; |
5085 | 10.4k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header |
5086 | 10.4k | = |
5087 | 10.4k | l_current_pos; |
5088 | 10.4k | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos |
5089 | 10.4k | = |
5090 | 10.4k | l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; |
5091 | | |
5092 | 10.4k | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
5093 | 10.4k | l_cstr_index, |
5094 | 10.4k | J2K_MS_SOD, |
5095 | 10.4k | l_current_pos, |
5096 | 10.4k | 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 | 10.4k | } |
5103 | | |
5104 | | /* Patch to support new PHR data */ |
5105 | 10.4k | if (!l_sot_length_pb_detected) { |
5106 | 8.94k | l_current_read_size = opj_stream_read_data( |
5107 | 8.94k | p_stream, |
5108 | 8.94k | *l_current_data + *l_tile_len, |
5109 | 8.94k | p_j2k->m_specific_param.m_decoder.m_sot_length, |
5110 | 8.94k | p_manager); |
5111 | 8.94k | } else { |
5112 | 1.49k | l_current_read_size = 0; |
5113 | 1.49k | } |
5114 | | |
5115 | 10.4k | 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 | 10.4k | } else { |
5123 | 10.4k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
5124 | 10.4k | } |
5125 | | |
5126 | 10.4k | *l_tile_len += (OPJ_UINT32)l_current_read_size; |
5127 | | |
5128 | 10.4k | return OPJ_TRUE; |
5129 | 10.4k | } |
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 | 592 | { |
5241 | 592 | OPJ_UINT32 l_nb_comp; |
5242 | 592 | opj_image_t * l_image = 00; |
5243 | | |
5244 | 592 | opj_cp_t *l_cp = 00; |
5245 | 592 | opj_tcp_t *l_tcp = 00; |
5246 | 592 | OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; |
5247 | | |
5248 | | /* preconditions*/ |
5249 | 592 | assert(p_header_data != 00); |
5250 | 592 | assert(p_j2k != 00); |
5251 | 592 | assert(p_manager != 00); |
5252 | | |
5253 | 592 | l_image = p_j2k->m_private_image; |
5254 | 592 | l_nb_comp = l_image->numcomps; |
5255 | | |
5256 | 592 | if (l_nb_comp <= 256) { |
5257 | 277 | l_comp_room = 1; |
5258 | 315 | } else { |
5259 | 315 | l_comp_room = 2; |
5260 | 315 | } |
5261 | | |
5262 | 592 | if (p_header_size != 2 + l_comp_room) { |
5263 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n"); |
5264 | 3 | return OPJ_FALSE; |
5265 | 3 | } |
5266 | | |
5267 | 589 | l_cp = &(p_j2k->m_cp); |
5268 | 589 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
5269 | 44 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
5270 | 589 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5271 | | |
5272 | 589 | opj_read_bytes(p_header_data, &l_comp_no, l_comp_room); /* Crgn */ |
5273 | 589 | p_header_data += l_comp_room; |
5274 | 589 | opj_read_bytes(p_header_data, &l_roi_sty, |
5275 | 589 | 1); /* Srgn */ |
5276 | 589 | ++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 | 589 | if (l_comp_no >= l_nb_comp) { |
5295 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
5296 | 4 | "bad component number in RGN (%d when there are only %d)\n", |
5297 | 4 | l_comp_no, l_nb_comp); |
5298 | 4 | return OPJ_FALSE; |
5299 | 4 | } |
5300 | | |
5301 | 585 | opj_read_bytes(p_header_data, |
5302 | 585 | (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1); /* SPrgn */ |
5303 | 585 | ++p_header_data; |
5304 | | |
5305 | 585 | return OPJ_TRUE; |
5306 | | |
5307 | 589 | } |
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 | 113k | { |
5733 | 113k | OPJ_UINT32 l_unknown_marker; |
5734 | 113k | const opj_dec_memory_marker_handler_t * l_marker_handler; |
5735 | 113k | OPJ_UINT32 l_size_unk = 2; |
5736 | | |
5737 | | /* preconditions*/ |
5738 | 113k | assert(p_j2k != 00); |
5739 | 113k | assert(p_manager != 00); |
5740 | 113k | assert(p_stream != 00); |
5741 | | |
5742 | 113k | opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); |
5743 | | |
5744 | 3.34M | for (;;) { |
5745 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
5746 | 3.34M | if (opj_stream_read_data(p_stream, |
5747 | 3.34M | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
5748 | 160 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
5749 | 160 | return OPJ_FALSE; |
5750 | 160 | } |
5751 | | |
5752 | | /* read 2 bytes as the new marker ID*/ |
5753 | 3.34M | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
5754 | 3.34M | &l_unknown_marker, 2); |
5755 | | |
5756 | 3.34M | if (!(l_unknown_marker < 0xff00)) { |
5757 | | |
5758 | | /* Get the marker handler from the marker ID*/ |
5759 | 1.02M | l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); |
5760 | | |
5761 | 1.02M | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
5762 | 20 | opj_event_msg(p_manager, EVT_ERROR, |
5763 | 20 | "Marker is not compliant with its position\n"); |
5764 | 20 | return OPJ_FALSE; |
5765 | 1.02M | } else { |
5766 | 1.02M | if (l_marker_handler->id != J2K_MS_UNK) { |
5767 | | /* Add the marker to the codestream index*/ |
5768 | 113k | if (l_marker_handler->id != J2K_MS_SOT) { |
5769 | 112k | OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, |
5770 | 112k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, |
5771 | 112k | l_size_unk); |
5772 | 112k | 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 | 112k | } |
5777 | 113k | break; /* next marker is known and well located */ |
5778 | 914k | } else { |
5779 | 914k | l_size_unk += 2; |
5780 | 914k | } |
5781 | 1.02M | } |
5782 | 1.02M | } |
5783 | 3.34M | } |
5784 | | |
5785 | 113k | *output_marker = l_marker_handler->id ; |
5786 | | |
5787 | 113k | return OPJ_TRUE; |
5788 | 113k | } |
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 | 5.18k | { |
5870 | 5.18k | OPJ_UINT32 i; |
5871 | 5.18k | opj_tcp_t *l_tcp = 00; |
5872 | 5.18k | OPJ_UINT32 l_tmp; |
5873 | 5.18k | OPJ_UINT32 l_indix; |
5874 | 5.18k | opj_mct_data_t * l_mct_data; |
5875 | | |
5876 | | /* preconditions */ |
5877 | 5.18k | assert(p_header_data != 00); |
5878 | 5.18k | assert(p_j2k != 00); |
5879 | | |
5880 | 5.18k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
5881 | 7 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
5882 | 5.18k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5883 | | |
5884 | 5.18k | if (p_header_size < 2) { |
5885 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5886 | 2 | return OPJ_FALSE; |
5887 | 2 | } |
5888 | | |
5889 | | /* first marker */ |
5890 | 5.18k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmct */ |
5891 | 5.18k | p_header_data += 2; |
5892 | 5.18k | if (l_tmp != 0) { |
5893 | 558 | opj_event_msg(p_manager, EVT_WARNING, |
5894 | 558 | "Cannot take in charge mct data within multiple MCT records\n"); |
5895 | 558 | return OPJ_TRUE; |
5896 | 558 | } |
5897 | | |
5898 | 4.62k | if (p_header_size <= 6) { |
5899 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
5900 | 1 | return OPJ_FALSE; |
5901 | 1 | } |
5902 | | |
5903 | | /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ |
5904 | 4.62k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Imct */ |
5905 | 4.62k | p_header_data += 2; |
5906 | | |
5907 | 4.62k | l_indix = l_tmp & 0xff; |
5908 | 4.62k | 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.7k | if (l_mct_data->m_index == l_indix) { |
5912 | 3.85k | break; |
5913 | 3.85k | } |
5914 | 14.8k | ++l_mct_data; |
5915 | 14.8k | } |
5916 | | |
5917 | | /* NOT FOUND */ |
5918 | 4.62k | if (i == l_tcp->m_nb_mct_records) { |
5919 | 769 | if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { |
5920 | 36 | opj_mct_data_t *new_mct_records; |
5921 | 36 | l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
5922 | | |
5923 | 36 | new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, |
5924 | 36 | l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
5925 | 36 | 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 | 36 | if (new_mct_records != l_tcp->m_mct_records) { |
5937 | 159 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5938 | 126 | opj_simple_mcc_decorrelation_data_t* l_mcc_record = |
5939 | 126 | &(l_tcp->m_mcc_records[i]); |
5940 | 126 | if (l_mcc_record->m_decorrelation_array) { |
5941 | 31 | l_mcc_record->m_decorrelation_array = |
5942 | 31 | new_mct_records + |
5943 | 31 | (l_mcc_record->m_decorrelation_array - |
5944 | 31 | l_tcp->m_mct_records); |
5945 | 31 | } |
5946 | 126 | if (l_mcc_record->m_offset_array) { |
5947 | 31 | l_mcc_record->m_offset_array = |
5948 | 31 | new_mct_records + |
5949 | 31 | (l_mcc_record->m_offset_array - |
5950 | 31 | l_tcp->m_mct_records); |
5951 | 31 | } |
5952 | 126 | } |
5953 | 33 | } |
5954 | | |
5955 | 36 | l_tcp->m_mct_records = new_mct_records; |
5956 | 36 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5957 | 36 | memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * |
5958 | 36 | sizeof(opj_mct_data_t)); |
5959 | 36 | } |
5960 | | |
5961 | 769 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5962 | 769 | ++l_tcp->m_nb_mct_records; |
5963 | 769 | } |
5964 | | |
5965 | 4.62k | if (l_mct_data->m_data) { |
5966 | 970 | opj_free(l_mct_data->m_data); |
5967 | 970 | l_mct_data->m_data = 00; |
5968 | 970 | l_mct_data->m_data_size = 0; |
5969 | 970 | } |
5970 | | |
5971 | 4.62k | l_mct_data->m_index = l_indix; |
5972 | 4.62k | l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); |
5973 | 4.62k | l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); |
5974 | | |
5975 | 4.62k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymct */ |
5976 | 4.62k | p_header_data += 2; |
5977 | 4.62k | if (l_tmp != 0) { |
5978 | 3.39k | opj_event_msg(p_manager, EVT_WARNING, |
5979 | 3.39k | "Cannot take in charge multiple MCT markers\n"); |
5980 | 3.39k | return OPJ_TRUE; |
5981 | 3.39k | } |
5982 | | |
5983 | 1.23k | p_header_size -= 6; |
5984 | | |
5985 | 1.23k | l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); |
5986 | 1.23k | 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 | 1.23k | memcpy(l_mct_data->m_data, p_header_data, p_header_size); |
5991 | | |
5992 | 1.23k | l_mct_data->m_data_size = p_header_size; |
5993 | | |
5994 | 1.23k | return OPJ_TRUE; |
5995 | 1.23k | } |
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 | 52.7k | { |
6117 | 52.7k | OPJ_UINT32 i, j; |
6118 | 52.7k | OPJ_UINT32 l_tmp; |
6119 | 52.7k | OPJ_UINT32 l_indix; |
6120 | 52.7k | opj_tcp_t * l_tcp; |
6121 | 52.7k | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6122 | 52.7k | opj_mct_data_t * l_mct_data; |
6123 | 52.7k | OPJ_UINT32 l_nb_collections; |
6124 | 52.7k | OPJ_UINT32 l_nb_comps; |
6125 | 52.7k | OPJ_UINT32 l_nb_bytes_by_comp; |
6126 | 52.7k | OPJ_BOOL l_new_mcc = OPJ_FALSE; |
6127 | | |
6128 | | /* preconditions */ |
6129 | 52.7k | assert(p_header_data != 00); |
6130 | 52.7k | assert(p_j2k != 00); |
6131 | 52.7k | assert(p_manager != 00); |
6132 | | |
6133 | 52.7k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
6134 | 13 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
6135 | 52.7k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
6136 | | |
6137 | 52.7k | 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 | 52.7k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmcc */ |
6144 | 52.7k | p_header_data += 2; |
6145 | 52.7k | if (l_tmp != 0) { |
6146 | 2.61k | opj_event_msg(p_manager, EVT_WARNING, |
6147 | 2.61k | "Cannot take in charge multiple data spanning\n"); |
6148 | 2.61k | return OPJ_TRUE; |
6149 | 2.61k | } |
6150 | | |
6151 | 50.1k | 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 | 50.1k | opj_read_bytes(p_header_data, &l_indix, |
6157 | 50.1k | 1); /* Imcc -> no need for other values, take the first */ |
6158 | 50.1k | ++p_header_data; |
6159 | | |
6160 | 50.1k | l_mcc_record = l_tcp->m_mcc_records; |
6161 | | |
6162 | 68.3k | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
6163 | 52.9k | if (l_mcc_record->m_index == l_indix) { |
6164 | 34.7k | break; |
6165 | 34.7k | } |
6166 | 18.1k | ++l_mcc_record; |
6167 | 18.1k | } |
6168 | | |
6169 | | /** NOT FOUND */ |
6170 | 50.1k | if (i == l_tcp->m_nb_mcc_records) { |
6171 | 15.3k | if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { |
6172 | 14 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
6173 | 14 | l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
6174 | | |
6175 | 14 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
6176 | 14 | l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof( |
6177 | 14 | opj_simple_mcc_decorrelation_data_t)); |
6178 | 14 | 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 | 14 | l_tcp->m_mcc_records = new_mcc_records; |
6187 | 14 | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
6188 | 14 | memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records) |
6189 | 14 | * sizeof(opj_simple_mcc_decorrelation_data_t)); |
6190 | 14 | } |
6191 | 15.3k | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
6192 | 15.3k | l_new_mcc = OPJ_TRUE; |
6193 | 15.3k | } |
6194 | 50.1k | l_mcc_record->m_index = l_indix; |
6195 | | |
6196 | | /* only one marker atm */ |
6197 | 50.1k | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymcc */ |
6198 | 50.1k | p_header_data += 2; |
6199 | 50.1k | if (l_tmp != 0) { |
6200 | 12.6k | opj_event_msg(p_manager, EVT_WARNING, |
6201 | 12.6k | "Cannot take in charge multiple data spanning\n"); |
6202 | 12.6k | return OPJ_TRUE; |
6203 | 12.6k | } |
6204 | | |
6205 | 37.4k | opj_read_bytes(p_header_data, &l_nb_collections, |
6206 | 37.4k | 2); /* Qmcc -> number of collections -> 1 */ |
6207 | 37.4k | p_header_data += 2; |
6208 | | |
6209 | 37.4k | if (l_nb_collections > 1) { |
6210 | 1.70k | opj_event_msg(p_manager, EVT_WARNING, |
6211 | 1.70k | "Cannot take in charge multiple collections\n"); |
6212 | 1.70k | return OPJ_TRUE; |
6213 | 1.70k | } |
6214 | | |
6215 | 35.7k | p_header_size -= 7; |
6216 | | |
6217 | 44.3k | for (i = 0; i < l_nb_collections; ++i) { |
6218 | 35.0k | 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 | 35.0k | opj_read_bytes(p_header_data, &l_tmp, |
6224 | 35.0k | 1); /* Xmcci type of component transformation -> array based decorrelation */ |
6225 | 35.0k | ++p_header_data; |
6226 | | |
6227 | 35.0k | if (l_tmp != 1) { |
6228 | 859 | opj_event_msg(p_manager, EVT_WARNING, |
6229 | 859 | "Cannot take in charge collections other than array decorrelation\n"); |
6230 | 859 | return OPJ_TRUE; |
6231 | 859 | } |
6232 | | |
6233 | 34.2k | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
6234 | | |
6235 | 34.2k | p_header_data += 2; |
6236 | 34.2k | p_header_size -= 3; |
6237 | | |
6238 | 34.2k | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
6239 | 34.2k | l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; |
6240 | | |
6241 | 34.2k | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { |
6242 | 7 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6243 | 7 | return OPJ_FALSE; |
6244 | 7 | } |
6245 | | |
6246 | 34.2k | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); |
6247 | | |
6248 | 63.5k | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
6249 | 30.7k | opj_read_bytes(p_header_data, &l_tmp, |
6250 | 30.7k | l_nb_bytes_by_comp); /* Cmccij Component offset*/ |
6251 | 30.7k | p_header_data += l_nb_bytes_by_comp; |
6252 | | |
6253 | 30.7k | if (l_tmp != j) { |
6254 | 1.33k | opj_event_msg(p_manager, EVT_WARNING, |
6255 | 1.33k | "Cannot take in charge collections with indix shuffle\n"); |
6256 | 1.33k | return OPJ_TRUE; |
6257 | 1.33k | } |
6258 | 30.7k | } |
6259 | | |
6260 | 32.8k | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
6261 | 32.8k | p_header_data += 2; |
6262 | | |
6263 | 32.8k | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
6264 | 32.8k | l_nb_comps &= 0x7fff; |
6265 | | |
6266 | 32.8k | if (l_nb_comps != l_mcc_record->m_nb_comps) { |
6267 | 15.1k | opj_event_msg(p_manager, EVT_WARNING, |
6268 | 15.1k | "Cannot take in charge collections without same number of indixes\n"); |
6269 | 15.1k | return OPJ_TRUE; |
6270 | 15.1k | } |
6271 | | |
6272 | 17.6k | 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 | 17.6k | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); |
6278 | | |
6279 | 25.3k | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
6280 | 16.7k | opj_read_bytes(p_header_data, &l_tmp, |
6281 | 16.7k | l_nb_bytes_by_comp); /* Wmccij Component offset*/ |
6282 | 16.7k | p_header_data += l_nb_bytes_by_comp; |
6283 | | |
6284 | 16.7k | if (l_tmp != j) { |
6285 | 9.06k | opj_event_msg(p_manager, EVT_WARNING, |
6286 | 9.06k | "Cannot take in charge collections with indix shuffle\n"); |
6287 | 9.06k | return OPJ_TRUE; |
6288 | 9.06k | } |
6289 | 16.7k | } |
6290 | | |
6291 | 8.60k | opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/ |
6292 | 8.60k | p_header_data += 3; |
6293 | | |
6294 | 8.60k | l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1); |
6295 | 8.60k | l_mcc_record->m_decorrelation_array = 00; |
6296 | 8.60k | l_mcc_record->m_offset_array = 00; |
6297 | | |
6298 | 8.60k | l_indix = l_tmp & 0xff; |
6299 | 8.60k | if (l_indix != 0) { |
6300 | 1.03k | l_mct_data = l_tcp->m_mct_records; |
6301 | 3.85k | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6302 | 3.85k | if (l_mct_data->m_index == l_indix) { |
6303 | 1.02k | l_mcc_record->m_decorrelation_array = l_mct_data; |
6304 | 1.02k | break; |
6305 | 1.02k | } |
6306 | 2.82k | ++l_mct_data; |
6307 | 2.82k | } |
6308 | | |
6309 | 1.03k | if (l_mcc_record->m_decorrelation_array == 00) { |
6310 | 6 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6311 | 6 | return OPJ_FALSE; |
6312 | 6 | } |
6313 | 1.03k | } |
6314 | | |
6315 | 8.59k | l_indix = (l_tmp >> 8) & 0xff; |
6316 | 8.59k | if (l_indix != 0) { |
6317 | 1.04k | l_mct_data = l_tcp->m_mct_records; |
6318 | 3.87k | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6319 | 3.86k | if (l_mct_data->m_index == l_indix) { |
6320 | 1.03k | l_mcc_record->m_offset_array = l_mct_data; |
6321 | 1.03k | break; |
6322 | 1.03k | } |
6323 | 2.82k | ++l_mct_data; |
6324 | 2.82k | } |
6325 | | |
6326 | 1.04k | if (l_mcc_record->m_offset_array == 00) { |
6327 | 9 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6328 | 9 | return OPJ_FALSE; |
6329 | 9 | } |
6330 | 1.04k | } |
6331 | 8.59k | } |
6332 | | |
6333 | 9.24k | if (p_header_size != 0) { |
6334 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
6335 | 1 | return OPJ_FALSE; |
6336 | 1 | } |
6337 | | |
6338 | 9.24k | if (l_new_mcc) { |
6339 | 515 | ++l_tcp->m_nb_mcc_records; |
6340 | 515 | } |
6341 | | |
6342 | 9.24k | return OPJ_TRUE; |
6343 | 9.24k | } |
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 | 59.0k | { |
6422 | 59.0k | OPJ_UINT32 l_tmp, i; |
6423 | 59.0k | OPJ_UINT32 l_nb_stages; |
6424 | 59.0k | opj_tcp_t * l_tcp; |
6425 | 59.0k | opj_tccp_t * l_tccp; |
6426 | 59.0k | opj_image_t * l_image; |
6427 | | |
6428 | | /* preconditions */ |
6429 | 59.0k | assert(p_header_data != 00); |
6430 | 59.0k | assert(p_j2k != 00); |
6431 | 59.0k | assert(p_manager != 00); |
6432 | | |
6433 | 59.0k | l_image = p_j2k->m_private_image; |
6434 | 59.0k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
6435 | 24 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
6436 | 59.0k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
6437 | | |
6438 | 59.0k | 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 | 58.9k | opj_read_bytes(p_header_data, &l_nb_stages, |
6444 | 58.9k | 1); /* Nmco : only one transform stage*/ |
6445 | 58.9k | ++p_header_data; |
6446 | | |
6447 | 58.9k | if (l_nb_stages > 1) { |
6448 | 35.6k | opj_event_msg(p_manager, EVT_WARNING, |
6449 | 35.6k | "Cannot take in charge multiple transformation stages.\n"); |
6450 | 35.6k | return OPJ_TRUE; |
6451 | 35.6k | } |
6452 | | |
6453 | 23.3k | if (p_header_size != l_nb_stages + 1) { |
6454 | 7 | opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n"); |
6455 | 7 | return OPJ_FALSE; |
6456 | 7 | } |
6457 | | |
6458 | 23.3k | l_tccp = l_tcp->tccps; |
6459 | | |
6460 | 60.8k | for (i = 0; i < l_image->numcomps; ++i) { |
6461 | 37.5k | l_tccp->m_dc_level_shift = 0; |
6462 | 37.5k | ++l_tccp; |
6463 | 37.5k | } |
6464 | | |
6465 | 23.3k | if (l_tcp->m_mct_decoding_matrix) { |
6466 | 1.73k | opj_free(l_tcp->m_mct_decoding_matrix); |
6467 | 1.73k | l_tcp->m_mct_decoding_matrix = 00; |
6468 | 1.73k | } |
6469 | | |
6470 | 46.5k | for (i = 0; i < l_nb_stages; ++i) { |
6471 | 23.1k | opj_read_bytes(p_header_data, &l_tmp, 1); |
6472 | 23.1k | ++p_header_data; |
6473 | | |
6474 | 23.1k | if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) { |
6475 | 3 | return OPJ_FALSE; |
6476 | 3 | } |
6477 | 23.1k | } |
6478 | | |
6479 | 23.3k | return OPJ_TRUE; |
6480 | 23.3k | } |
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 | 23.1k | { |
6485 | 23.1k | OPJ_UINT32 i; |
6486 | 23.1k | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6487 | 23.1k | opj_mct_data_t * l_deco_array, * l_offset_array; |
6488 | 23.1k | OPJ_UINT32 l_data_size, l_mct_size, l_offset_size; |
6489 | 23.1k | OPJ_UINT32 l_nb_elem; |
6490 | 23.1k | OPJ_UINT32 * l_offset_data, * l_current_offset_data; |
6491 | 23.1k | opj_tccp_t * l_tccp; |
6492 | | |
6493 | | /* preconditions */ |
6494 | 23.1k | assert(p_tcp != 00); |
6495 | | |
6496 | 23.1k | l_mcc_record = p_tcp->m_mcc_records; |
6497 | | |
6498 | 23.3k | for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) { |
6499 | 22.3k | if (l_mcc_record->m_index == p_index) { |
6500 | 22.1k | break; |
6501 | 22.1k | } |
6502 | 22.3k | } |
6503 | | |
6504 | 23.1k | if (i == p_tcp->m_nb_mcc_records) { |
6505 | | /** element discarded **/ |
6506 | 990 | return OPJ_TRUE; |
6507 | 990 | } |
6508 | | |
6509 | 22.1k | if (l_mcc_record->m_nb_comps != p_image->numcomps) { |
6510 | | /** do not support number of comps != image */ |
6511 | 3.50k | return OPJ_TRUE; |
6512 | 3.50k | } |
6513 | | |
6514 | 18.6k | l_deco_array = l_mcc_record->m_decorrelation_array; |
6515 | | |
6516 | 18.6k | if (l_deco_array) { |
6517 | 1.79k | l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps |
6518 | 1.79k | * p_image->numcomps; |
6519 | 1.79k | if (l_deco_array->m_data_size != l_data_size) { |
6520 | 1 | return OPJ_FALSE; |
6521 | 1 | } |
6522 | | |
6523 | 1.78k | l_nb_elem = p_image->numcomps * p_image->numcomps; |
6524 | 1.78k | l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32); |
6525 | 1.78k | p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
6526 | | |
6527 | 1.78k | if (! p_tcp->m_mct_decoding_matrix) { |
6528 | 0 | return OPJ_FALSE; |
6529 | 0 | } |
6530 | | |
6531 | 1.78k | j2k_mct_read_functions_to_float[l_deco_array->m_element_type]( |
6532 | 1.78k | l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem); |
6533 | 1.78k | } |
6534 | | |
6535 | 18.6k | l_offset_array = l_mcc_record->m_offset_array; |
6536 | | |
6537 | 18.6k | if (l_offset_array) { |
6538 | 790 | l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * |
6539 | 790 | p_image->numcomps; |
6540 | 790 | if (l_offset_array->m_data_size != l_data_size) { |
6541 | 2 | return OPJ_FALSE; |
6542 | 2 | } |
6543 | | |
6544 | 788 | l_nb_elem = p_image->numcomps; |
6545 | 788 | l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32); |
6546 | 788 | l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); |
6547 | | |
6548 | 788 | if (! l_offset_data) { |
6549 | 0 | return OPJ_FALSE; |
6550 | 0 | } |
6551 | | |
6552 | 788 | j2k_mct_read_functions_to_int32[l_offset_array->m_element_type]( |
6553 | 788 | l_offset_array->m_data, l_offset_data, l_nb_elem); |
6554 | | |
6555 | 788 | l_tccp = p_tcp->tccps; |
6556 | 788 | l_current_offset_data = l_offset_data; |
6557 | | |
6558 | 2.20k | for (i = 0; i < p_image->numcomps; ++i) { |
6559 | 1.41k | l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++); |
6560 | 1.41k | ++l_tccp; |
6561 | 1.41k | } |
6562 | | |
6563 | 788 | opj_free(l_offset_data); |
6564 | 788 | } |
6565 | | |
6566 | 18.6k | return OPJ_TRUE; |
6567 | 18.6k | } |
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 | 139 | { |
6644 | 139 | OPJ_UINT32 l_nb_comp, l_num_comp; |
6645 | 139 | OPJ_UINT32 l_comp_def; |
6646 | 139 | OPJ_UINT32 i; |
6647 | 139 | opj_image_comp_t * l_comp = 00; |
6648 | | |
6649 | | /* preconditions */ |
6650 | 139 | assert(p_header_data != 00); |
6651 | 139 | assert(p_j2k != 00); |
6652 | 139 | assert(p_manager != 00); |
6653 | | |
6654 | 139 | l_num_comp = p_j2k->m_private_image->numcomps; |
6655 | | |
6656 | 139 | if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { |
6657 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
6658 | 3 | return OPJ_FALSE; |
6659 | 3 | } |
6660 | | |
6661 | 136 | opj_read_bytes(p_header_data, &l_nb_comp, |
6662 | 136 | 2); /* Ncbd */ |
6663 | 136 | p_header_data += 2; |
6664 | | |
6665 | 136 | if (l_nb_comp != l_num_comp) { |
6666 | 10 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
6667 | 10 | return OPJ_FALSE; |
6668 | 10 | } |
6669 | | |
6670 | 126 | l_comp = p_j2k->m_private_image->comps; |
6671 | 482 | for (i = 0; i < l_num_comp; ++i) { |
6672 | 362 | opj_read_bytes(p_header_data, &l_comp_def, |
6673 | 362 | 1); /* Component bit depth */ |
6674 | 362 | ++p_header_data; |
6675 | 362 | l_comp->sgnd = (l_comp_def >> 7) & 1; |
6676 | 362 | l_comp->prec = (l_comp_def & 0x7f) + 1; |
6677 | | |
6678 | 362 | if (l_comp->prec > 31) { |
6679 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
6680 | 6 | "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 | 6 | i, l_comp->prec); |
6682 | 6 | return OPJ_FALSE; |
6683 | 6 | } |
6684 | 356 | ++l_comp; |
6685 | 356 | } |
6686 | | |
6687 | 120 | return OPJ_TRUE; |
6688 | 126 | } |
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 | 309 | { |
6705 | | /* preconditions */ |
6706 | 309 | assert(p_header_data != 00); |
6707 | 309 | assert(p_j2k != 00); |
6708 | 309 | assert(p_manager != 00); |
6709 | | |
6710 | 309 | (void)p_j2k; |
6711 | 309 | (void)p_header_data; |
6712 | 309 | (void)p_header_size; |
6713 | 309 | (void)p_manager; |
6714 | | |
6715 | 309 | return OPJ_TRUE; |
6716 | 309 | } |
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 | 71 | { |
6731 | | /* preconditions */ |
6732 | 71 | assert(p_header_data != 00); |
6733 | 71 | assert(p_j2k != 00); |
6734 | 71 | assert(p_manager != 00); |
6735 | | |
6736 | 71 | (void)p_j2k; |
6737 | 71 | (void)p_header_data; |
6738 | 71 | (void)p_header_size; |
6739 | 71 | (void)p_manager; |
6740 | | |
6741 | 71 | return OPJ_TRUE; |
6742 | 71 | } |
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.86k | { |
6750 | 8.86k | if (j2k && parameters) { |
6751 | 8.86k | j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; |
6752 | 8.86k | j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; |
6753 | | |
6754 | 8.86k | 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.86k | } |
6761 | 8.86k | } |
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 | } |
6768 | 0 | } |
6769 | | |
6770 | | OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads) |
6771 | 0 | { |
6772 | | /* Currently we pass the thread-pool to the tcd, so we cannot re-set it */ |
6773 | | /* afterwards */ |
6774 | 0 | if (opj_has_thread_support() && j2k->m_tcd == NULL) { |
6775 | 0 | opj_thread_pool_destroy(j2k->m_tp); |
6776 | 0 | j2k->m_tp = NULL; |
6777 | 0 | if (num_threads <= (OPJ_UINT32)INT_MAX) { |
6778 | 0 | j2k->m_tp = opj_thread_pool_create((int)num_threads); |
6779 | 0 | } |
6780 | 0 | if (j2k->m_tp == NULL) { |
6781 | 0 | j2k->m_tp = opj_thread_pool_create(0); |
6782 | 0 | return OPJ_FALSE; |
6783 | 0 | } |
6784 | 0 | return OPJ_TRUE; |
6785 | 0 | } |
6786 | 0 | return OPJ_FALSE; |
6787 | 0 | } |
6788 | | |
6789 | | static int opj_j2k_get_default_thread_count(void) |
6790 | 8.86k | { |
6791 | 8.86k | const char* num_threads_str = getenv("OPJ_NUM_THREADS"); |
6792 | 8.86k | int num_cpus; |
6793 | 8.86k | int num_threads; |
6794 | | |
6795 | 8.86k | if (num_threads_str == NULL || !opj_has_thread_support()) { |
6796 | 8.86k | return 0; |
6797 | 8.86k | } |
6798 | 0 | num_cpus = opj_get_num_cpus(); |
6799 | 0 | if (strcmp(num_threads_str, "ALL_CPUS") == 0) { |
6800 | 0 | return num_cpus; |
6801 | 0 | } |
6802 | 0 | if (num_cpus == 0) { |
6803 | 0 | num_cpus = 32; |
6804 | 0 | } |
6805 | 0 | num_threads = atoi(num_threads_str); |
6806 | 0 | if (num_threads < 0) { |
6807 | 0 | num_threads = 0; |
6808 | 0 | } else if (num_threads > 2 * num_cpus) { |
6809 | 0 | num_threads = 2 * num_cpus; |
6810 | 0 | } |
6811 | 0 | return num_threads; |
6812 | 0 | } |
6813 | | |
6814 | | /* ----------------------------------------------------------------------- */ |
6815 | | /* J2K encoder interface */ |
6816 | | /* ----------------------------------------------------------------------- */ |
6817 | | |
6818 | | opj_j2k_t* opj_j2k_create_compress(void) |
6819 | 0 | { |
6820 | 0 | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
6821 | 0 | if (!l_j2k) { |
6822 | 0 | return NULL; |
6823 | 0 | } |
6824 | | |
6825 | | |
6826 | 0 | l_j2k->m_is_decoder = 0; |
6827 | 0 | l_j2k->m_cp.m_is_decoder = 0; |
6828 | |
|
6829 | 0 | l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc( |
6830 | 0 | OPJ_J2K_DEFAULT_HEADER_SIZE); |
6831 | 0 | if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
6832 | 0 | opj_j2k_destroy(l_j2k); |
6833 | 0 | return NULL; |
6834 | 0 | } |
6835 | | |
6836 | 0 | l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = |
6837 | 0 | OPJ_J2K_DEFAULT_HEADER_SIZE; |
6838 | | |
6839 | | /* validation list creation*/ |
6840 | 0 | l_j2k->m_validation_list = opj_procedure_list_create(); |
6841 | 0 | if (! l_j2k->m_validation_list) { |
6842 | 0 | opj_j2k_destroy(l_j2k); |
6843 | 0 | return NULL; |
6844 | 0 | } |
6845 | | |
6846 | | /* execution list creation*/ |
6847 | 0 | l_j2k->m_procedure_list = opj_procedure_list_create(); |
6848 | 0 | if (! l_j2k->m_procedure_list) { |
6849 | 0 | opj_j2k_destroy(l_j2k); |
6850 | 0 | return NULL; |
6851 | 0 | } |
6852 | | |
6853 | 0 | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
6854 | 0 | if (!l_j2k->m_tp) { |
6855 | 0 | l_j2k->m_tp = opj_thread_pool_create(0); |
6856 | 0 | } |
6857 | 0 | if (!l_j2k->m_tp) { |
6858 | 0 | opj_j2k_destroy(l_j2k); |
6859 | 0 | return NULL; |
6860 | 0 | } |
6861 | | |
6862 | 0 | return l_j2k; |
6863 | 0 | } |
6864 | | |
6865 | | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres) |
6866 | 0 | { |
6867 | 0 | POC[0].tile = 1; |
6868 | 0 | POC[0].resno0 = 0; |
6869 | 0 | POC[0].compno0 = 0; |
6870 | 0 | POC[0].layno1 = 1; |
6871 | 0 | POC[0].resno1 = (OPJ_UINT32)(numres - 1); |
6872 | 0 | POC[0].compno1 = 3; |
6873 | 0 | POC[0].prg1 = OPJ_CPRL; |
6874 | 0 | POC[1].tile = 1; |
6875 | 0 | POC[1].resno0 = (OPJ_UINT32)(numres - 1); |
6876 | 0 | POC[1].compno0 = 0; |
6877 | 0 | POC[1].layno1 = 1; |
6878 | 0 | POC[1].resno1 = (OPJ_UINT32)numres; |
6879 | 0 | POC[1].compno1 = 3; |
6880 | 0 | POC[1].prg1 = OPJ_CPRL; |
6881 | 0 | return 2; |
6882 | 0 | } |
6883 | | |
6884 | | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, |
6885 | | opj_image_t *image, opj_event_mgr_t *p_manager) |
6886 | 0 | { |
6887 | | /* Configure cinema parameters */ |
6888 | 0 | int i; |
6889 | | |
6890 | | /* No tiling */ |
6891 | 0 | parameters->tile_size_on = OPJ_FALSE; |
6892 | 0 | parameters->cp_tdx = 1; |
6893 | 0 | parameters->cp_tdy = 1; |
6894 | | |
6895 | | /* One tile part for each component */ |
6896 | 0 | parameters->tp_flag = 'C'; |
6897 | 0 | parameters->tp_on = 1; |
6898 | | |
6899 | | /* Tile and Image shall be at (0,0) */ |
6900 | 0 | parameters->cp_tx0 = 0; |
6901 | 0 | parameters->cp_ty0 = 0; |
6902 | 0 | parameters->image_offset_x0 = 0; |
6903 | 0 | parameters->image_offset_y0 = 0; |
6904 | | |
6905 | | /* Codeblock size= 32*32 */ |
6906 | 0 | parameters->cblockw_init = 32; |
6907 | 0 | parameters->cblockh_init = 32; |
6908 | | |
6909 | | /* Codeblock style: no mode switch enabled */ |
6910 | 0 | parameters->mode = 0; |
6911 | | |
6912 | | /* No ROI */ |
6913 | 0 | parameters->roi_compno = -1; |
6914 | | |
6915 | | /* No subsampling */ |
6916 | 0 | parameters->subsampling_dx = 1; |
6917 | 0 | parameters->subsampling_dy = 1; |
6918 | | |
6919 | | /* 9-7 transform */ |
6920 | 0 | parameters->irreversible = 1; |
6921 | | |
6922 | | /* Number of layers */ |
6923 | 0 | if (parameters->tcp_numlayers > 1) { |
6924 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6925 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6926 | 0 | "1 single quality layer" |
6927 | 0 | "-> Number of layers forced to 1 (rather than %d)\n" |
6928 | 0 | "-> Rate of the last layer (%3.1f) will be used", |
6929 | 0 | parameters->tcp_numlayers, |
6930 | 0 | parameters->tcp_rates[parameters->tcp_numlayers - 1]); |
6931 | 0 | parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1]; |
6932 | 0 | parameters->tcp_numlayers = 1; |
6933 | 0 | } |
6934 | | |
6935 | | /* Resolution levels */ |
6936 | 0 | switch (parameters->rsiz) { |
6937 | 0 | case OPJ_PROFILE_CINEMA_2K: |
6938 | 0 | if (parameters->numresolution > 6) { |
6939 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6940 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6941 | 0 | "Number of decomposition levels <= 5\n" |
6942 | 0 | "-> Number of decomposition levels forced to 5 (rather than %d)\n", |
6943 | 0 | parameters->numresolution + 1); |
6944 | 0 | parameters->numresolution = 6; |
6945 | 0 | } |
6946 | 0 | break; |
6947 | 0 | case OPJ_PROFILE_CINEMA_4K: |
6948 | 0 | if (parameters->numresolution < 2) { |
6949 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6950 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6951 | 0 | "Number of decomposition levels >= 1 && <= 6\n" |
6952 | 0 | "-> Number of decomposition levels forced to 1 (rather than %d)\n", |
6953 | 0 | parameters->numresolution + 1); |
6954 | 0 | parameters->numresolution = 1; |
6955 | 0 | } else if (parameters->numresolution > 7) { |
6956 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6957 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6958 | 0 | "Number of decomposition levels >= 1 && <= 6\n" |
6959 | 0 | "-> Number of decomposition levels forced to 6 (rather than %d)\n", |
6960 | 0 | parameters->numresolution + 1); |
6961 | 0 | parameters->numresolution = 7; |
6962 | 0 | } |
6963 | 0 | break; |
6964 | 0 | default : |
6965 | 0 | break; |
6966 | 0 | } |
6967 | | |
6968 | | /* Precincts */ |
6969 | 0 | parameters->csty |= J2K_CP_CSTY_PRT; |
6970 | 0 | if (parameters->numresolution == 1) { |
6971 | 0 | parameters->res_spec = 1; |
6972 | 0 | parameters->prcw_init[0] = 128; |
6973 | 0 | parameters->prch_init[0] = 128; |
6974 | 0 | } else { |
6975 | 0 | parameters->res_spec = parameters->numresolution - 1; |
6976 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
6977 | 0 | parameters->prcw_init[i] = 256; |
6978 | 0 | parameters->prch_init[i] = 256; |
6979 | 0 | } |
6980 | 0 | } |
6981 | | |
6982 | | /* The progression order shall be CPRL */ |
6983 | 0 | parameters->prog_order = OPJ_CPRL; |
6984 | | |
6985 | | /* Progression order changes for 4K, disallowed for 2K */ |
6986 | 0 | if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) { |
6987 | 0 | parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC, |
6988 | 0 | parameters->numresolution); |
6989 | 0 | } else { |
6990 | 0 | parameters->numpocs = 0; |
6991 | 0 | } |
6992 | | |
6993 | | /* Limited bit-rate */ |
6994 | 0 | parameters->cp_disto_alloc = 1; |
6995 | 0 | if (parameters->max_cs_size <= 0) { |
6996 | | /* No rate has been introduced, 24 fps is assumed */ |
6997 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
6998 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
6999 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7000 | 0 | "Maximum 1302083 compressed bytes @ 24fps\n" |
7001 | 0 | "As no rate has been given, this limit will be used.\n"); |
7002 | 0 | } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) { |
7003 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7004 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7005 | 0 | "Maximum 1302083 compressed bytes @ 24fps\n" |
7006 | 0 | "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n"); |
7007 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7008 | 0 | } |
7009 | |
|
7010 | 0 | if (parameters->max_comp_size <= 0) { |
7011 | | /* No rate has been introduced, 24 fps is assumed */ |
7012 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7013 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7014 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7015 | 0 | "Maximum 1041666 compressed bytes @ 24fps\n" |
7016 | 0 | "As no rate has been given, this limit will be used.\n"); |
7017 | 0 | } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) { |
7018 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7019 | 0 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
7020 | 0 | "Maximum 1041666 compressed bytes @ 24fps\n" |
7021 | 0 | "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n"); |
7022 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7023 | 0 | } |
7024 | |
|
7025 | 0 | parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * |
7026 | 0 | image->comps[0].h * image->comps[0].prec) / |
7027 | 0 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7028 | 0 | image->comps[0].dy); |
7029 | |
|
7030 | 0 | } |
7031 | | |
7032 | | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, |
7033 | | opj_event_mgr_t *p_manager) |
7034 | 0 | { |
7035 | 0 | OPJ_UINT32 i; |
7036 | | |
7037 | | /* Number of components */ |
7038 | 0 | if (image->numcomps != 3) { |
7039 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7040 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
7041 | 0 | "3 components" |
7042 | 0 | "-> Number of components of input image (%d) is not compliant\n" |
7043 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7044 | 0 | image->numcomps); |
7045 | 0 | return OPJ_FALSE; |
7046 | 0 | } |
7047 | | |
7048 | | /* Bitdepth */ |
7049 | 0 | for (i = 0; i < image->numcomps; i++) { |
7050 | 0 | if ((image->comps[i].prec != 12) | (image->comps[i].sgnd)) { |
7051 | 0 | char signed_str[] = "signed"; |
7052 | 0 | char unsigned_str[] = "unsigned"; |
7053 | 0 | char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; |
7054 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7055 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
7056 | 0 | "Precision of each component shall be 12 bits unsigned" |
7057 | 0 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
7058 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7059 | 0 | i, image->comps[i].prec, tmp_str); |
7060 | 0 | return OPJ_FALSE; |
7061 | 0 | } |
7062 | 0 | } |
7063 | | |
7064 | | /* Image size */ |
7065 | 0 | switch (rsiz) { |
7066 | 0 | case OPJ_PROFILE_CINEMA_2K: |
7067 | 0 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) { |
7068 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7069 | 0 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
7070 | 0 | "width <= 2048 and height <= 1080\n" |
7071 | 0 | "-> Input image size %d x %d is not compliant\n" |
7072 | 0 | "-> Non-profile-3 codestream will be generated\n", |
7073 | 0 | image->comps[0].w, image->comps[0].h); |
7074 | 0 | return OPJ_FALSE; |
7075 | 0 | } |
7076 | 0 | break; |
7077 | 0 | case OPJ_PROFILE_CINEMA_4K: |
7078 | 0 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) { |
7079 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7080 | 0 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
7081 | 0 | "width <= 4096 and height <= 2160\n" |
7082 | 0 | "-> Image size %d x %d is not compliant\n" |
7083 | 0 | "-> Non-profile-4 codestream will be generated\n", |
7084 | 0 | image->comps[0].w, image->comps[0].h); |
7085 | 0 | return OPJ_FALSE; |
7086 | 0 | } |
7087 | 0 | break; |
7088 | 0 | default : |
7089 | 0 | break; |
7090 | 0 | } |
7091 | | |
7092 | 0 | return OPJ_TRUE; |
7093 | 0 | } |
7094 | | |
7095 | | static int opj_j2k_get_imf_max_NL(opj_cparameters_t *parameters, |
7096 | | opj_image_t *image) |
7097 | 0 | { |
7098 | | /* Decomposition levels */ |
7099 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7100 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7101 | 0 | const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) |
7102 | 0 | parameters->cp_tdx : image->x1; |
7103 | 0 | switch (profile) { |
7104 | 0 | case OPJ_PROFILE_IMF_2K: |
7105 | 0 | return 5; |
7106 | 0 | case OPJ_PROFILE_IMF_4K: |
7107 | 0 | return 6; |
7108 | 0 | case OPJ_PROFILE_IMF_8K: |
7109 | 0 | return 7; |
7110 | 0 | case OPJ_PROFILE_IMF_2K_R: { |
7111 | 0 | if (XTsiz >= 2048) { |
7112 | 0 | return 5; |
7113 | 0 | } else if (XTsiz >= 1024) { |
7114 | 0 | return 4; |
7115 | 0 | } |
7116 | 0 | break; |
7117 | 0 | } |
7118 | 0 | case OPJ_PROFILE_IMF_4K_R: { |
7119 | 0 | if (XTsiz >= 4096) { |
7120 | 0 | return 6; |
7121 | 0 | } else if (XTsiz >= 2048) { |
7122 | 0 | return 5; |
7123 | 0 | } else if (XTsiz >= 1024) { |
7124 | 0 | return 4; |
7125 | 0 | } |
7126 | 0 | break; |
7127 | 0 | } |
7128 | 0 | case OPJ_PROFILE_IMF_8K_R: { |
7129 | 0 | if (XTsiz >= 8192) { |
7130 | 0 | return 7; |
7131 | 0 | } else if (XTsiz >= 4096) { |
7132 | 0 | return 6; |
7133 | 0 | } else if (XTsiz >= 2048) { |
7134 | 0 | return 5; |
7135 | 0 | } else if (XTsiz >= 1024) { |
7136 | 0 | return 4; |
7137 | 0 | } |
7138 | 0 | break; |
7139 | 0 | } |
7140 | 0 | default: |
7141 | 0 | break; |
7142 | 0 | } |
7143 | 0 | return -1; |
7144 | 0 | } |
7145 | | |
7146 | | static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters, |
7147 | | opj_image_t *image, opj_event_mgr_t *p_manager) |
7148 | 0 | { |
7149 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7150 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7151 | |
|
7152 | 0 | OPJ_UNUSED(p_manager); |
7153 | | |
7154 | | /* Override defaults set by opj_set_default_encoder_parameters */ |
7155 | 0 | if (parameters->cblockw_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKW && |
7156 | 0 | parameters->cblockh_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKH) { |
7157 | 0 | parameters->cblockw_init = 32; |
7158 | 0 | parameters->cblockh_init = 32; |
7159 | 0 | } |
7160 | | |
7161 | | /* One tile part for each component */ |
7162 | 0 | parameters->tp_flag = 'C'; |
7163 | 0 | parameters->tp_on = 1; |
7164 | |
|
7165 | 0 | if (parameters->prog_order == OPJ_COMP_PARAM_DEFAULT_PROG_ORDER) { |
7166 | 0 | parameters->prog_order = OPJ_CPRL; |
7167 | 0 | } |
7168 | |
|
7169 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7170 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7171 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7172 | | /* 9-7 transform */ |
7173 | 0 | parameters->irreversible = 1; |
7174 | 0 | } |
7175 | | |
7176 | | /* Adjust the number of resolutions if set to its defaults */ |
7177 | 0 | if (parameters->numresolution == OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION && |
7178 | 0 | image->x0 == 0 && |
7179 | 0 | image->y0 == 0) { |
7180 | 0 | const int max_NL = opj_j2k_get_imf_max_NL(parameters, image); |
7181 | 0 | if (max_NL >= 0 && parameters->numresolution > max_NL) { |
7182 | 0 | parameters->numresolution = max_NL + 1; |
7183 | 0 | } |
7184 | | |
7185 | | /* Note: below is generic logic */ |
7186 | 0 | if (!parameters->tile_size_on) { |
7187 | 0 | while (parameters->numresolution > 0) { |
7188 | 0 | if (image->x1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { |
7189 | 0 | parameters->numresolution --; |
7190 | 0 | continue; |
7191 | 0 | } |
7192 | 0 | if (image->y1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { |
7193 | 0 | parameters->numresolution --; |
7194 | 0 | continue; |
7195 | 0 | } |
7196 | 0 | break; |
7197 | 0 | } |
7198 | 0 | } |
7199 | 0 | } |
7200 | | |
7201 | | /* Set defaults precincts */ |
7202 | 0 | if (parameters->csty == 0) { |
7203 | 0 | parameters->csty |= J2K_CP_CSTY_PRT; |
7204 | 0 | if (parameters->numresolution == 1) { |
7205 | 0 | parameters->res_spec = 1; |
7206 | 0 | parameters->prcw_init[0] = 128; |
7207 | 0 | parameters->prch_init[0] = 128; |
7208 | 0 | } else { |
7209 | 0 | int i; |
7210 | 0 | parameters->res_spec = parameters->numresolution - 1; |
7211 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
7212 | 0 | parameters->prcw_init[i] = 256; |
7213 | 0 | parameters->prch_init[i] = 256; |
7214 | 0 | } |
7215 | 0 | } |
7216 | 0 | } |
7217 | 0 | } |
7218 | | |
7219 | | /* Table A.53 from JPEG2000 standard */ |
7220 | | static const OPJ_UINT16 tabMaxSubLevelFromMainLevel[] = { |
7221 | | 15, /* unspecified */ |
7222 | | 1, |
7223 | | 1, |
7224 | | 1, |
7225 | | 2, |
7226 | | 3, |
7227 | | 4, |
7228 | | 5, |
7229 | | 6, |
7230 | | 7, |
7231 | | 8, |
7232 | | 9 |
7233 | | }; |
7234 | | |
7235 | | static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters, |
7236 | | opj_image_t *image, |
7237 | | opj_event_mgr_t *p_manager) |
7238 | 0 | { |
7239 | 0 | OPJ_UINT32 i; |
7240 | 0 | const OPJ_UINT16 rsiz = parameters->rsiz; |
7241 | 0 | const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); |
7242 | 0 | const OPJ_UINT16 mainlevel = OPJ_GET_IMF_MAINLEVEL(rsiz); |
7243 | 0 | const OPJ_UINT16 sublevel = OPJ_GET_IMF_SUBLEVEL(rsiz); |
7244 | 0 | const int NL = parameters->numresolution - 1; |
7245 | 0 | const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) |
7246 | 0 | parameters->cp_tdx : image->x1; |
7247 | 0 | OPJ_BOOL ret = OPJ_TRUE; |
7248 | | |
7249 | | /* Validate mainlevel */ |
7250 | 0 | if (mainlevel > OPJ_IMF_MAINLEVEL_MAX) { |
7251 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7252 | 0 | "IMF profile require mainlevel <= 11.\n" |
7253 | 0 | "-> %d is thus not compliant\n" |
7254 | 0 | "-> Non-IMF codestream will be generated\n", |
7255 | 0 | mainlevel); |
7256 | 0 | ret = OPJ_FALSE; |
7257 | 0 | } else { |
7258 | | /* Validate sublevel */ |
7259 | 0 | assert(sizeof(tabMaxSubLevelFromMainLevel) == |
7260 | 0 | (OPJ_IMF_MAINLEVEL_MAX + 1) * sizeof(tabMaxSubLevelFromMainLevel[0])); |
7261 | 0 | if (sublevel > tabMaxSubLevelFromMainLevel[mainlevel]) { |
7262 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7263 | 0 | "IMF profile require sublevel <= %d for mainlevel = %d.\n" |
7264 | 0 | "-> %d is thus not compliant\n" |
7265 | 0 | "-> Non-IMF codestream will be generated\n", |
7266 | 0 | tabMaxSubLevelFromMainLevel[mainlevel], |
7267 | 0 | mainlevel, |
7268 | 0 | sublevel); |
7269 | 0 | ret = OPJ_FALSE; |
7270 | 0 | } |
7271 | 0 | } |
7272 | | |
7273 | | /* Number of components */ |
7274 | 0 | if (image->numcomps > 3) { |
7275 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7276 | 0 | "IMF profiles require at most 3 components.\n" |
7277 | 0 | "-> Number of components of input image (%d) is not compliant\n" |
7278 | 0 | "-> Non-IMF codestream will be generated\n", |
7279 | 0 | image->numcomps); |
7280 | 0 | ret = OPJ_FALSE; |
7281 | 0 | } |
7282 | |
|
7283 | 0 | if (image->x0 != 0 || image->y0 != 0) { |
7284 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7285 | 0 | "IMF profiles require image origin to be at 0,0.\n" |
7286 | 0 | "-> %d,%d is not compliant\n" |
7287 | 0 | "-> Non-IMF codestream will be generated\n", |
7288 | 0 | image->x0, image->y0 != 0); |
7289 | 0 | ret = OPJ_FALSE; |
7290 | 0 | } |
7291 | |
|
7292 | 0 | if (parameters->cp_tx0 != 0 || parameters->cp_ty0 != 0) { |
7293 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7294 | 0 | "IMF profiles require tile origin to be at 0,0.\n" |
7295 | 0 | "-> %d,%d is not compliant\n" |
7296 | 0 | "-> Non-IMF codestream will be generated\n", |
7297 | 0 | parameters->cp_tx0, parameters->cp_ty0); |
7298 | 0 | ret = OPJ_FALSE; |
7299 | 0 | } |
7300 | |
|
7301 | 0 | if (parameters->tile_size_on) { |
7302 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7303 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7304 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7305 | 0 | if ((OPJ_UINT32)parameters->cp_tdx < image->x1 || |
7306 | 0 | (OPJ_UINT32)parameters->cp_tdy < image->y1) { |
7307 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7308 | 0 | "IMF 2K/4K/8K single tile profiles require tile to be greater or equal to image size.\n" |
7309 | 0 | "-> %d,%d is lesser than %d,%d\n" |
7310 | 0 | "-> Non-IMF codestream will be generated\n", |
7311 | 0 | parameters->cp_tdx, |
7312 | 0 | parameters->cp_tdy, |
7313 | 0 | image->x1, |
7314 | 0 | image->y1); |
7315 | 0 | ret = OPJ_FALSE; |
7316 | 0 | } |
7317 | 0 | } else { |
7318 | 0 | if ((OPJ_UINT32)parameters->cp_tdx >= image->x1 && |
7319 | 0 | (OPJ_UINT32)parameters->cp_tdy >= image->y1) { |
7320 | | /* ok */ |
7321 | 0 | } else if (parameters->cp_tdx == 1024 && |
7322 | 0 | parameters->cp_tdy == 1024) { |
7323 | | /* ok */ |
7324 | 0 | } else if (parameters->cp_tdx == 2048 && |
7325 | 0 | parameters->cp_tdy == 2048 && |
7326 | 0 | (profile == OPJ_PROFILE_IMF_4K || |
7327 | 0 | profile == OPJ_PROFILE_IMF_8K)) { |
7328 | | /* ok */ |
7329 | 0 | } else if (parameters->cp_tdx == 4096 && |
7330 | 0 | parameters->cp_tdy == 4096 && |
7331 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7332 | | /* ok */ |
7333 | 0 | } else { |
7334 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7335 | 0 | "IMF 2K_R/4K_R/8K_R single/multiple tile profiles " |
7336 | 0 | "require tile to be greater or equal to image size,\n" |
7337 | 0 | "or to be (1024,1024), or (2048,2048) for 4K_R/8K_R " |
7338 | 0 | "or (4096,4096) for 8K_R.\n" |
7339 | 0 | "-> %d,%d is non conformant\n" |
7340 | 0 | "-> Non-IMF codestream will be generated\n", |
7341 | 0 | parameters->cp_tdx, |
7342 | 0 | parameters->cp_tdy); |
7343 | 0 | ret = OPJ_FALSE; |
7344 | 0 | } |
7345 | 0 | } |
7346 | 0 | } |
7347 | | |
7348 | | /* Bitdepth */ |
7349 | 0 | for (i = 0; i < image->numcomps; i++) { |
7350 | 0 | if (!(image->comps[i].prec >= 8 && image->comps[i].prec <= 16) || |
7351 | 0 | (image->comps[i].sgnd)) { |
7352 | 0 | char signed_str[] = "signed"; |
7353 | 0 | char unsigned_str[] = "unsigned"; |
7354 | 0 | char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; |
7355 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7356 | 0 | "IMF profiles require precision of each component to b in [8-16] bits unsigned" |
7357 | 0 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
7358 | 0 | "-> Non-IMF codestream will be generated\n", |
7359 | 0 | i, image->comps[i].prec, tmp_str); |
7360 | 0 | ret = OPJ_FALSE; |
7361 | 0 | } |
7362 | 0 | } |
7363 | | |
7364 | | /* Sub-sampling */ |
7365 | 0 | for (i = 0; i < image->numcomps; i++) { |
7366 | 0 | if (i == 0 && image->comps[i].dx != 1) { |
7367 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7368 | 0 | "IMF profiles require XRSiz1 == 1. Here it is set to %d.\n" |
7369 | 0 | "-> Non-IMF codestream will be generated\n", |
7370 | 0 | image->comps[i].dx); |
7371 | 0 | ret = OPJ_FALSE; |
7372 | 0 | } |
7373 | 0 | if (i == 1 && image->comps[i].dx != 1 && image->comps[i].dx != 2) { |
7374 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7375 | 0 | "IMF profiles require XRSiz2 == 1 or 2. Here it is set to %d.\n" |
7376 | 0 | "-> Non-IMF codestream will be generated\n", |
7377 | 0 | image->comps[i].dx); |
7378 | 0 | ret = OPJ_FALSE; |
7379 | 0 | } |
7380 | 0 | if (i > 1 && image->comps[i].dx != image->comps[i - 1].dx) { |
7381 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7382 | 0 | "IMF profiles require XRSiz%d to be the same as XRSiz2. " |
7383 | 0 | "Here it is set to %d instead of %d.\n" |
7384 | 0 | "-> Non-IMF codestream will be generated\n", |
7385 | 0 | i + 1, image->comps[i].dx, image->comps[i - 1].dx); |
7386 | 0 | ret = OPJ_FALSE; |
7387 | 0 | } |
7388 | 0 | if (image->comps[i].dy != 1) { |
7389 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7390 | 0 | "IMF profiles require YRsiz == 1. " |
7391 | 0 | "Here it is set to %d for component %d.\n" |
7392 | 0 | "-> Non-IMF codestream will be generated\n", |
7393 | 0 | image->comps[i].dy, i); |
7394 | 0 | ret = OPJ_FALSE; |
7395 | 0 | } |
7396 | 0 | } |
7397 | | |
7398 | | /* Image size */ |
7399 | 0 | switch (profile) { |
7400 | 0 | case OPJ_PROFILE_IMF_2K: |
7401 | 0 | case OPJ_PROFILE_IMF_2K_R: |
7402 | 0 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1556))) { |
7403 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7404 | 0 | "IMF 2K/2K_R profile require:\n" |
7405 | 0 | "width <= 2048 and height <= 1556\n" |
7406 | 0 | "-> Input image size %d x %d is not compliant\n" |
7407 | 0 | "-> Non-IMF codestream will be generated\n", |
7408 | 0 | image->comps[0].w, image->comps[0].h); |
7409 | 0 | ret = OPJ_FALSE; |
7410 | 0 | } |
7411 | 0 | break; |
7412 | 0 | case OPJ_PROFILE_IMF_4K: |
7413 | 0 | case OPJ_PROFILE_IMF_4K_R: |
7414 | 0 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 3112))) { |
7415 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7416 | 0 | "IMF 4K/4K_R profile require:\n" |
7417 | 0 | "width <= 4096 and height <= 3112\n" |
7418 | 0 | "-> Input image size %d x %d is not compliant\n" |
7419 | 0 | "-> Non-IMF codestream will be generated\n", |
7420 | 0 | image->comps[0].w, image->comps[0].h); |
7421 | 0 | ret = OPJ_FALSE; |
7422 | 0 | } |
7423 | 0 | break; |
7424 | 0 | case OPJ_PROFILE_IMF_8K: |
7425 | 0 | case OPJ_PROFILE_IMF_8K_R: |
7426 | 0 | if (((image->comps[0].w > 8192) | (image->comps[0].h > 6224))) { |
7427 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7428 | 0 | "IMF 8K/8K_R profile require:\n" |
7429 | 0 | "width <= 8192 and height <= 6224\n" |
7430 | 0 | "-> Input image size %d x %d is not compliant\n" |
7431 | 0 | "-> Non-IMF codestream will be generated\n", |
7432 | 0 | image->comps[0].w, image->comps[0].h); |
7433 | 0 | ret = OPJ_FALSE; |
7434 | 0 | } |
7435 | 0 | break; |
7436 | 0 | default : |
7437 | 0 | assert(0); |
7438 | 0 | return OPJ_FALSE; |
7439 | 0 | } |
7440 | | |
7441 | 0 | if (parameters->roi_compno != -1) { |
7442 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7443 | 0 | "IMF profile forbid RGN / region of interest marker.\n" |
7444 | 0 | "-> Compression parameters specify a ROI\n" |
7445 | 0 | "-> Non-IMF codestream will be generated\n"); |
7446 | 0 | ret = OPJ_FALSE; |
7447 | 0 | } |
7448 | |
|
7449 | 0 | if (parameters->cblockw_init != 32 || parameters->cblockh_init != 32) { |
7450 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7451 | 0 | "IMF profile require code block size to be 32x32.\n" |
7452 | 0 | "-> Compression parameters set it to %dx%d.\n" |
7453 | 0 | "-> Non-IMF codestream will be generated\n", |
7454 | 0 | parameters->cblockw_init, |
7455 | 0 | parameters->cblockh_init); |
7456 | 0 | ret = OPJ_FALSE; |
7457 | 0 | } |
7458 | |
|
7459 | 0 | if (parameters->prog_order != OPJ_CPRL) { |
7460 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7461 | 0 | "IMF profile require progression order to be CPRL.\n" |
7462 | 0 | "-> Compression parameters set it to %d.\n" |
7463 | 0 | "-> Non-IMF codestream will be generated\n", |
7464 | 0 | parameters->prog_order); |
7465 | 0 | ret = OPJ_FALSE; |
7466 | 0 | } |
7467 | |
|
7468 | 0 | if (parameters->numpocs != 0) { |
7469 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7470 | 0 | "IMF profile forbid POC markers.\n" |
7471 | 0 | "-> Compression parameters set %d POC.\n" |
7472 | 0 | "-> Non-IMF codestream will be generated\n", |
7473 | 0 | parameters->numpocs); |
7474 | 0 | ret = OPJ_FALSE; |
7475 | 0 | } |
7476 | | |
7477 | | /* Codeblock style: no mode switch enabled */ |
7478 | 0 | if (parameters->mode != 0) { |
7479 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7480 | 0 | "IMF profile forbid mode switch in code block style.\n" |
7481 | 0 | "-> Compression parameters set code block style to %d.\n" |
7482 | 0 | "-> Non-IMF codestream will be generated\n", |
7483 | 0 | parameters->mode); |
7484 | 0 | ret = OPJ_FALSE; |
7485 | 0 | } |
7486 | |
|
7487 | 0 | if (profile == OPJ_PROFILE_IMF_2K || |
7488 | 0 | profile == OPJ_PROFILE_IMF_4K || |
7489 | 0 | profile == OPJ_PROFILE_IMF_8K) { |
7490 | | /* Expect 9-7 transform */ |
7491 | 0 | if (parameters->irreversible != 1) { |
7492 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7493 | 0 | "IMF 2K/4K/8K profiles require 9-7 Irreversible Transform.\n" |
7494 | 0 | "-> Compression parameters set it to reversible.\n" |
7495 | 0 | "-> Non-IMF codestream will be generated\n"); |
7496 | 0 | ret = OPJ_FALSE; |
7497 | 0 | } |
7498 | 0 | } else { |
7499 | | /* Expect 5-3 transform */ |
7500 | 0 | if (parameters->irreversible != 0) { |
7501 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7502 | 0 | "IMF 2K/4K/8K profiles require 5-3 reversible Transform.\n" |
7503 | 0 | "-> Compression parameters set it to irreversible.\n" |
7504 | 0 | "-> Non-IMF codestream will be generated\n"); |
7505 | 0 | ret = OPJ_FALSE; |
7506 | 0 | } |
7507 | 0 | } |
7508 | | |
7509 | | /* Number of layers */ |
7510 | 0 | if (parameters->tcp_numlayers != 1) { |
7511 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7512 | 0 | "IMF 2K/4K/8K profiles require 1 single quality layer.\n" |
7513 | 0 | "-> Number of layers is %d.\n" |
7514 | 0 | "-> Non-IMF codestream will be generated\n", |
7515 | 0 | parameters->tcp_numlayers); |
7516 | 0 | ret = OPJ_FALSE; |
7517 | 0 | } |
7518 | | |
7519 | | /* Decomposition levels */ |
7520 | 0 | switch (profile) { |
7521 | 0 | case OPJ_PROFILE_IMF_2K: |
7522 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7523 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7524 | 0 | "IMF 2K profile requires 1 <= NL <= 5:\n" |
7525 | 0 | "-> Number of decomposition levels is %d.\n" |
7526 | 0 | "-> Non-IMF codestream will be generated\n", |
7527 | 0 | NL); |
7528 | 0 | ret = OPJ_FALSE; |
7529 | 0 | } |
7530 | 0 | break; |
7531 | 0 | case OPJ_PROFILE_IMF_4K: |
7532 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7533 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7534 | 0 | "IMF 4K profile requires 1 <= NL <= 6:\n" |
7535 | 0 | "-> Number of decomposition levels is %d.\n" |
7536 | 0 | "-> Non-IMF codestream will be generated\n", |
7537 | 0 | NL); |
7538 | 0 | ret = OPJ_FALSE; |
7539 | 0 | } |
7540 | 0 | break; |
7541 | 0 | case OPJ_PROFILE_IMF_8K: |
7542 | 0 | if (!(NL >= 1 && NL <= 7)) { |
7543 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7544 | 0 | "IMF 8K profile requires 1 <= NL <= 7:\n" |
7545 | 0 | "-> Number of decomposition levels is %d.\n" |
7546 | 0 | "-> Non-IMF codestream will be generated\n", |
7547 | 0 | NL); |
7548 | 0 | ret = OPJ_FALSE; |
7549 | 0 | } |
7550 | 0 | break; |
7551 | 0 | case OPJ_PROFILE_IMF_2K_R: { |
7552 | 0 | if (XTsiz >= 2048) { |
7553 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7554 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7555 | 0 | "IMF 2K_R profile requires 1 <= NL <= 5 for XTsiz >= 2048:\n" |
7556 | 0 | "-> Number of decomposition levels is %d.\n" |
7557 | 0 | "-> Non-IMF codestream will be generated\n", |
7558 | 0 | NL); |
7559 | 0 | ret = OPJ_FALSE; |
7560 | 0 | } |
7561 | 0 | } else if (XTsiz >= 1024) { |
7562 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7563 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7564 | 0 | "IMF 2K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7565 | 0 | "-> Number of decomposition levels is %d.\n" |
7566 | 0 | "-> Non-IMF codestream will be generated\n", |
7567 | 0 | NL); |
7568 | 0 | ret = OPJ_FALSE; |
7569 | 0 | } |
7570 | 0 | } |
7571 | 0 | break; |
7572 | 0 | } |
7573 | 0 | case OPJ_PROFILE_IMF_4K_R: { |
7574 | 0 | if (XTsiz >= 4096) { |
7575 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7576 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7577 | 0 | "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz >= 4096:\n" |
7578 | 0 | "-> Number of decomposition levels is %d.\n" |
7579 | 0 | "-> Non-IMF codestream will be generated\n", |
7580 | 0 | NL); |
7581 | 0 | ret = OPJ_FALSE; |
7582 | 0 | } |
7583 | 0 | } else if (XTsiz >= 2048) { |
7584 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7585 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7586 | 0 | "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" |
7587 | 0 | "-> Number of decomposition levels is %d.\n" |
7588 | 0 | "-> Non-IMF codestream will be generated\n", |
7589 | 0 | NL); |
7590 | 0 | ret = OPJ_FALSE; |
7591 | 0 | } |
7592 | 0 | } else if (XTsiz >= 1024) { |
7593 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7594 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7595 | 0 | "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7596 | 0 | "-> Number of decomposition levels is %d.\n" |
7597 | 0 | "-> Non-IMF codestream will be generated\n", |
7598 | 0 | NL); |
7599 | 0 | ret = OPJ_FALSE; |
7600 | 0 | } |
7601 | 0 | } |
7602 | 0 | break; |
7603 | 0 | } |
7604 | 0 | case OPJ_PROFILE_IMF_8K_R: { |
7605 | 0 | if (XTsiz >= 8192) { |
7606 | 0 | if (!(NL >= 1 && NL <= 7)) { |
7607 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7608 | 0 | "IMF 4K_R profile requires 1 <= NL <= 7 for XTsiz >= 8192:\n" |
7609 | 0 | "-> Number of decomposition levels is %d.\n" |
7610 | 0 | "-> Non-IMF codestream will be generated\n", |
7611 | 0 | NL); |
7612 | 0 | ret = OPJ_FALSE; |
7613 | 0 | } |
7614 | 0 | } else if (XTsiz >= 4096) { |
7615 | 0 | if (!(NL >= 1 && NL <= 6)) { |
7616 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7617 | 0 | "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz in [4096,8192[:\n" |
7618 | 0 | "-> Number of decomposition levels is %d.\n" |
7619 | 0 | "-> Non-IMF codestream will be generated\n", |
7620 | 0 | NL); |
7621 | 0 | ret = OPJ_FALSE; |
7622 | 0 | } |
7623 | 0 | } else if (XTsiz >= 2048) { |
7624 | 0 | if (!(NL >= 1 && NL <= 5)) { |
7625 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7626 | 0 | "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" |
7627 | 0 | "-> Number of decomposition levels is %d.\n" |
7628 | 0 | "-> Non-IMF codestream will be generated\n", |
7629 | 0 | NL); |
7630 | 0 | ret = OPJ_FALSE; |
7631 | 0 | } |
7632 | 0 | } else if (XTsiz >= 1024) { |
7633 | 0 | if (!(NL >= 1 && NL <= 4)) { |
7634 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7635 | 0 | "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" |
7636 | 0 | "-> Number of decomposition levels is %d.\n" |
7637 | 0 | "-> Non-IMF codestream will be generated\n", |
7638 | 0 | NL); |
7639 | 0 | ret = OPJ_FALSE; |
7640 | 0 | } |
7641 | 0 | } |
7642 | 0 | break; |
7643 | 0 | } |
7644 | 0 | default: |
7645 | 0 | break; |
7646 | 0 | } |
7647 | | |
7648 | 0 | if (parameters->numresolution == 1) { |
7649 | 0 | if (parameters->res_spec != 1 || |
7650 | 0 | parameters->prcw_init[0] != 128 || |
7651 | 0 | parameters->prch_init[0] != 128) { |
7652 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7653 | 0 | "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" |
7654 | 0 | "-> Supplied values are different from that.\n" |
7655 | 0 | "-> Non-IMF codestream will be generated\n"); |
7656 | 0 | ret = OPJ_FALSE; |
7657 | 0 | } |
7658 | 0 | } else { |
7659 | 0 | int i; |
7660 | 0 | for (i = 0; i < parameters->res_spec; i++) { |
7661 | 0 | if (parameters->prcw_init[i] != 256 || |
7662 | 0 | parameters->prch_init[i] != 256) { |
7663 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7664 | 0 | "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" |
7665 | 0 | "-> Supplied values are different from that.\n" |
7666 | 0 | "-> Non-IMF codestream will be generated\n"); |
7667 | 0 | ret = OPJ_FALSE; |
7668 | 0 | } |
7669 | 0 | } |
7670 | 0 | } |
7671 | |
|
7672 | 0 | return ret; |
7673 | 0 | } |
7674 | | |
7675 | | |
7676 | | OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, |
7677 | | opj_cparameters_t *parameters, |
7678 | | opj_image_t *image, |
7679 | | opj_event_mgr_t * p_manager) |
7680 | 0 | { |
7681 | 0 | OPJ_UINT32 i, j, tileno, numpocs_tile; |
7682 | 0 | opj_cp_t *cp = 00; |
7683 | 0 | OPJ_UINT32 cblkw, cblkh; |
7684 | |
|
7685 | 0 | if (!p_j2k || !parameters || ! image) { |
7686 | 0 | return OPJ_FALSE; |
7687 | 0 | } |
7688 | | |
7689 | 0 | if ((parameters->numresolution <= 0) || |
7690 | 0 | (parameters->numresolution > OPJ_J2K_MAXRLVLS)) { |
7691 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7692 | 0 | "Invalid number of resolutions : %d not in range [1,%d]\n", |
7693 | 0 | parameters->numresolution, OPJ_J2K_MAXRLVLS); |
7694 | 0 | return OPJ_FALSE; |
7695 | 0 | } |
7696 | | |
7697 | 0 | if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) { |
7698 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7699 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7700 | 0 | parameters->cblockw_init); |
7701 | 0 | return OPJ_FALSE; |
7702 | 0 | } |
7703 | 0 | if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) { |
7704 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7705 | 0 | "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n", |
7706 | 0 | parameters->cblockh_init); |
7707 | 0 | return OPJ_FALSE; |
7708 | 0 | } |
7709 | 0 | if (parameters->cblockw_init * parameters->cblockh_init > 4096) { |
7710 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7711 | 0 | "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n"); |
7712 | 0 | return OPJ_FALSE; |
7713 | 0 | } |
7714 | 0 | cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
7715 | 0 | cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
7716 | 0 | if (parameters->cblockw_init != (1 << cblkw)) { |
7717 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7718 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7719 | 0 | parameters->cblockw_init); |
7720 | 0 | return OPJ_FALSE; |
7721 | 0 | } |
7722 | 0 | if (parameters->cblockh_init != (1 << cblkh)) { |
7723 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7724 | 0 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", |
7725 | 0 | parameters->cblockh_init); |
7726 | 0 | return OPJ_FALSE; |
7727 | 0 | } |
7728 | | |
7729 | 0 | if (parameters->cp_fixed_alloc) { |
7730 | 0 | if (parameters->cp_matrice == NULL) { |
7731 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7732 | 0 | "cp_fixed_alloc set, but cp_matrice missing\n"); |
7733 | 0 | return OPJ_FALSE; |
7734 | 0 | } |
7735 | | |
7736 | 0 | if (parameters->tcp_numlayers > J2K_TCD_MATRIX_MAX_LAYER_COUNT) { |
7737 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7738 | 0 | "tcp_numlayers when cp_fixed_alloc set should not exceed %d\n", |
7739 | 0 | J2K_TCD_MATRIX_MAX_LAYER_COUNT); |
7740 | 0 | return OPJ_FALSE; |
7741 | 0 | } |
7742 | 0 | if (parameters->numresolution > J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT) { |
7743 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7744 | 0 | "numresolution when cp_fixed_alloc set should not exceed %d\n", |
7745 | 0 | J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT); |
7746 | 0 | return OPJ_FALSE; |
7747 | 0 | } |
7748 | 0 | } |
7749 | | |
7750 | 0 | p_j2k->m_specific_param.m_encoder.m_nb_comps = image->numcomps; |
7751 | | |
7752 | | /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ |
7753 | 0 | cp = &(p_j2k->m_cp); |
7754 | | |
7755 | | /* set default values for cp */ |
7756 | 0 | cp->tw = 1; |
7757 | 0 | cp->th = 1; |
7758 | | |
7759 | | /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */ |
7760 | 0 | if (parameters->rsiz == |
7761 | 0 | OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */ |
7762 | 0 | OPJ_BOOL deprecated_used = OPJ_FALSE; |
7763 | 0 | switch (parameters->cp_cinema) { |
7764 | 0 | case OPJ_CINEMA2K_24: |
7765 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7766 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7767 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7768 | 0 | deprecated_used = OPJ_TRUE; |
7769 | 0 | break; |
7770 | 0 | case OPJ_CINEMA2K_48: |
7771 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7772 | 0 | parameters->max_cs_size = OPJ_CINEMA_48_CS; |
7773 | 0 | parameters->max_comp_size = OPJ_CINEMA_48_COMP; |
7774 | 0 | deprecated_used = OPJ_TRUE; |
7775 | 0 | break; |
7776 | 0 | case OPJ_CINEMA4K_24: |
7777 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
7778 | 0 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
7779 | 0 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
7780 | 0 | deprecated_used = OPJ_TRUE; |
7781 | 0 | break; |
7782 | 0 | case OPJ_OFF: |
7783 | 0 | default: |
7784 | 0 | break; |
7785 | 0 | } |
7786 | 0 | switch (parameters->cp_rsiz) { |
7787 | 0 | case OPJ_CINEMA2K: |
7788 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
7789 | 0 | deprecated_used = OPJ_TRUE; |
7790 | 0 | break; |
7791 | 0 | case OPJ_CINEMA4K: |
7792 | 0 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
7793 | 0 | deprecated_used = OPJ_TRUE; |
7794 | 0 | break; |
7795 | 0 | case OPJ_MCT: |
7796 | 0 | parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT; |
7797 | 0 | deprecated_used = OPJ_TRUE; |
7798 | 0 | case OPJ_STD_RSIZ: |
7799 | 0 | default: |
7800 | 0 | break; |
7801 | 0 | } |
7802 | 0 | if (deprecated_used) { |
7803 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7804 | 0 | "Deprecated fields cp_cinema or cp_rsiz are used\n" |
7805 | 0 | "Please consider using only the rsiz field\n" |
7806 | 0 | "See openjpeg.h documentation for more details\n"); |
7807 | 0 | } |
7808 | 0 | } |
7809 | | |
7810 | | /* If no explicit layers are provided, use lossless settings */ |
7811 | 0 | if (parameters->tcp_numlayers == 0) { |
7812 | 0 | parameters->tcp_numlayers = 1; |
7813 | 0 | parameters->cp_disto_alloc = 1; |
7814 | 0 | parameters->tcp_rates[0] = 0; |
7815 | 0 | } |
7816 | |
|
7817 | 0 | if (parameters->cp_disto_alloc) { |
7818 | | /* Emit warnings if tcp_rates are not decreasing */ |
7819 | 0 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7820 | 0 | OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i]; |
7821 | 0 | OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1]; |
7822 | 0 | if (rate_i_corr <= 1.0) { |
7823 | 0 | rate_i_corr = 1.0; |
7824 | 0 | } |
7825 | 0 | if (rate_i_m_1_corr <= 1.0) { |
7826 | 0 | rate_i_m_1_corr = 1.0; |
7827 | 0 | } |
7828 | 0 | if (rate_i_corr >= rate_i_m_1_corr) { |
7829 | 0 | if (rate_i_corr != parameters->tcp_rates[i] && |
7830 | 0 | rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
7831 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7832 | 0 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
7833 | 0 | "than tcp_rates[%d]=%f (corrected as %f)\n", |
7834 | 0 | i, parameters->tcp_rates[i], rate_i_corr, |
7835 | 0 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
7836 | 0 | } else if (rate_i_corr != parameters->tcp_rates[i]) { |
7837 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7838 | 0 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
7839 | 0 | "than tcp_rates[%d]=%f\n", |
7840 | 0 | i, parameters->tcp_rates[i], rate_i_corr, |
7841 | 0 | i - 1, parameters->tcp_rates[i - 1]); |
7842 | 0 | } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
7843 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7844 | 0 | "tcp_rates[%d]=%f should be strictly lesser " |
7845 | 0 | "than tcp_rates[%d]=%f (corrected as %f)\n", |
7846 | 0 | i, parameters->tcp_rates[i], |
7847 | 0 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
7848 | 0 | } else { |
7849 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7850 | 0 | "tcp_rates[%d]=%f should be strictly lesser " |
7851 | 0 | "than tcp_rates[%d]=%f\n", |
7852 | 0 | i, parameters->tcp_rates[i], |
7853 | 0 | i - 1, parameters->tcp_rates[i - 1]); |
7854 | 0 | } |
7855 | 0 | } |
7856 | 0 | } |
7857 | 0 | } else if (parameters->cp_fixed_quality) { |
7858 | | /* Emit warnings if tcp_distoratio are not increasing */ |
7859 | 0 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7860 | 0 | if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] && |
7861 | 0 | !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 && |
7862 | 0 | parameters->tcp_distoratio[i] == 0)) { |
7863 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7864 | 0 | "tcp_distoratio[%d]=%f should be strictly greater " |
7865 | 0 | "than tcp_distoratio[%d]=%f\n", |
7866 | 0 | i, parameters->tcp_distoratio[i], i - 1, |
7867 | 0 | parameters->tcp_distoratio[i - 1]); |
7868 | 0 | } |
7869 | 0 | } |
7870 | 0 | } |
7871 | | |
7872 | | /* see if max_codestream_size does limit input rate */ |
7873 | 0 | if (parameters->max_cs_size <= 0) { |
7874 | 0 | if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) { |
7875 | 0 | OPJ_FLOAT32 temp_size; |
7876 | 0 | temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
7877 | 0 | image->comps[0].h * image->comps[0].prec) / |
7878 | 0 | ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 * |
7879 | 0 | image->comps[0].dx * image->comps[0].dy)); |
7880 | 0 | if (temp_size > (OPJ_FLOAT32)INT_MAX) { |
7881 | 0 | parameters->max_cs_size = INT_MAX; |
7882 | 0 | } else { |
7883 | 0 | parameters->max_cs_size = (int) floor(temp_size); |
7884 | 0 | } |
7885 | 0 | } else { |
7886 | 0 | parameters->max_cs_size = 0; |
7887 | 0 | } |
7888 | 0 | } else { |
7889 | 0 | OPJ_FLOAT32 temp_rate; |
7890 | 0 | OPJ_BOOL cap = OPJ_FALSE; |
7891 | |
|
7892 | 0 | if (OPJ_IS_IMF(parameters->rsiz) && parameters->max_cs_size > 0 && |
7893 | 0 | parameters->tcp_numlayers == 1 && parameters->tcp_rates[0] == 0) { |
7894 | 0 | parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * |
7895 | 0 | image->comps[0].h * image->comps[0].prec) / |
7896 | 0 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7897 | 0 | image->comps[0].dy); |
7898 | 0 | } |
7899 | |
|
7900 | 0 | temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
7901 | 0 | image->comps[0].h * image->comps[0].prec) / |
7902 | 0 | (((double)parameters->max_cs_size) * 8 * image->comps[0].dx * |
7903 | 0 | image->comps[0].dy)); |
7904 | 0 | for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
7905 | 0 | if (parameters->tcp_rates[i] < temp_rate) { |
7906 | 0 | parameters->tcp_rates[i] = temp_rate; |
7907 | 0 | cap = OPJ_TRUE; |
7908 | 0 | } |
7909 | 0 | } |
7910 | 0 | if (cap) { |
7911 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7912 | 0 | "The desired maximum codestream size has limited\n" |
7913 | 0 | "at least one of the desired quality layers\n"); |
7914 | 0 | } |
7915 | 0 | } |
7916 | |
|
7917 | 0 | if (OPJ_IS_CINEMA(parameters->rsiz) || OPJ_IS_IMF(parameters->rsiz)) { |
7918 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE; |
7919 | 0 | } |
7920 | | |
7921 | | /* Manage profiles and applications and set RSIZ */ |
7922 | | /* set cinema parameters if required */ |
7923 | 0 | if (OPJ_IS_CINEMA(parameters->rsiz)) { |
7924 | 0 | if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K) |
7925 | 0 | || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) { |
7926 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7927 | 0 | "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n"); |
7928 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7929 | 0 | } else { |
7930 | 0 | opj_j2k_set_cinema_parameters(parameters, image, p_manager); |
7931 | 0 | if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) { |
7932 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7933 | 0 | } |
7934 | 0 | } |
7935 | 0 | } else if (OPJ_IS_STORAGE(parameters->rsiz)) { |
7936 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7937 | 0 | "JPEG 2000 Long Term Storage profile not yet supported\n"); |
7938 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7939 | 0 | } else if (OPJ_IS_BROADCAST(parameters->rsiz)) { |
7940 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7941 | 0 | "JPEG 2000 Broadcast profiles not yet supported\n"); |
7942 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7943 | 0 | } else if (OPJ_IS_IMF(parameters->rsiz)) { |
7944 | 0 | opj_j2k_set_imf_parameters(parameters, image, p_manager); |
7945 | 0 | if (!opj_j2k_is_imf_compliant(parameters, image, p_manager)) { |
7946 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7947 | 0 | } |
7948 | 0 | } else if (OPJ_IS_PART2(parameters->rsiz)) { |
7949 | 0 | if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) { |
7950 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7951 | 0 | "JPEG 2000 Part-2 profile defined\n" |
7952 | 0 | "but no Part-2 extension enabled.\n" |
7953 | 0 | "Profile set to NONE.\n"); |
7954 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7955 | 0 | } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) { |
7956 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
7957 | 0 | "Unsupported Part-2 extension enabled\n" |
7958 | 0 | "Profile set to NONE.\n"); |
7959 | 0 | parameters->rsiz = OPJ_PROFILE_NONE; |
7960 | 0 | } |
7961 | 0 | } |
7962 | | |
7963 | | /* |
7964 | | copy user encoding parameters |
7965 | | */ |
7966 | 0 | cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32) |
7967 | 0 | parameters->max_comp_size; |
7968 | 0 | cp->rsiz = parameters->rsiz; |
7969 | 0 | if (parameters->cp_fixed_alloc) { |
7970 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = FIXED_LAYER; |
7971 | 0 | } else if (parameters->cp_fixed_quality) { |
7972 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = |
7973 | 0 | FIXED_DISTORTION_RATIO; |
7974 | 0 | } else { |
7975 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = |
7976 | 0 | RATE_DISTORTION_RATIO; |
7977 | 0 | } |
7978 | |
|
7979 | 0 | if (parameters->cp_fixed_alloc) { |
7980 | 0 | size_t array_size = (size_t)parameters->tcp_numlayers * |
7981 | 0 | (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32); |
7982 | 0 | cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); |
7983 | 0 | if (!cp->m_specific_param.m_enc.m_matrice) { |
7984 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
7985 | 0 | "Not enough memory to allocate copy of user encoding parameters matrix \n"); |
7986 | 0 | return OPJ_FALSE; |
7987 | 0 | } |
7988 | 0 | memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, |
7989 | 0 | array_size); |
7990 | 0 | } |
7991 | | |
7992 | | /* tiles */ |
7993 | 0 | cp->tdx = (OPJ_UINT32)parameters->cp_tdx; |
7994 | 0 | cp->tdy = (OPJ_UINT32)parameters->cp_tdy; |
7995 | | |
7996 | | /* tile offset */ |
7997 | 0 | cp->tx0 = (OPJ_UINT32)parameters->cp_tx0; |
7998 | 0 | cp->ty0 = (OPJ_UINT32)parameters->cp_ty0; |
7999 | | |
8000 | | /* comment string */ |
8001 | 0 | if (parameters->cp_comment) { |
8002 | 0 | cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U); |
8003 | 0 | if (!cp->comment) { |
8004 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8005 | 0 | "Not enough memory to allocate copy of comment string\n"); |
8006 | 0 | return OPJ_FALSE; |
8007 | 0 | } |
8008 | 0 | strcpy(cp->comment, parameters->cp_comment); |
8009 | 0 | } else { |
8010 | | /* Create default comment for codestream */ |
8011 | 0 | const char comment[] = "Created by OpenJPEG version "; |
8012 | 0 | const size_t clen = strlen(comment); |
8013 | 0 | const char *version = opj_version(); |
8014 | | |
8015 | | /* UniPG>> */ |
8016 | | #ifdef USE_JPWL |
8017 | | const size_t cp_comment_buf_size = clen + strlen(version) + 11; |
8018 | | cp->comment = (char*)opj_malloc(cp_comment_buf_size); |
8019 | | if (!cp->comment) { |
8020 | | opj_event_msg(p_manager, EVT_ERROR, |
8021 | | "Not enough memory to allocate comment string\n"); |
8022 | | return OPJ_FALSE; |
8023 | | } |
8024 | | snprintf(cp->comment, cp_comment_buf_size, "%s%s with JPWL", |
8025 | | comment, version); |
8026 | | #else |
8027 | 0 | const size_t cp_comment_buf_size = clen + strlen(version) + 1; |
8028 | 0 | cp->comment = (char*)opj_malloc(cp_comment_buf_size); |
8029 | 0 | if (!cp->comment) { |
8030 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8031 | 0 | "Not enough memory to allocate comment string\n"); |
8032 | 0 | return OPJ_FALSE; |
8033 | 0 | } |
8034 | 0 | snprintf(cp->comment, cp_comment_buf_size, "%s%s", comment, version); |
8035 | 0 | #endif |
8036 | | /* <<UniPG */ |
8037 | 0 | } |
8038 | | |
8039 | | /* |
8040 | | calculate other encoding parameters |
8041 | | */ |
8042 | | |
8043 | 0 | if (parameters->tile_size_on) { |
8044 | 0 | if (cp->tdx == 0) { |
8045 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile width\n"); |
8046 | 0 | return OPJ_FALSE; |
8047 | 0 | } |
8048 | 0 | if (cp->tdy == 0) { |
8049 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile height\n"); |
8050 | 0 | return OPJ_FALSE; |
8051 | 0 | } |
8052 | 0 | cp->tw = opj_uint_ceildiv(image->x1 - cp->tx0, cp->tdx); |
8053 | 0 | cp->th = opj_uint_ceildiv(image->y1 - cp->ty0, cp->tdy); |
8054 | | /* Check that the number of tiles is valid */ |
8055 | 0 | if (cp->tw > 65535 / cp->th) { |
8056 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8057 | 0 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", |
8058 | 0 | cp->tw, cp->th); |
8059 | 0 | return OPJ_FALSE; |
8060 | 0 | } |
8061 | 0 | } else { |
8062 | 0 | cp->tdx = image->x1 - cp->tx0; |
8063 | 0 | cp->tdy = image->y1 - cp->ty0; |
8064 | 0 | } |
8065 | | |
8066 | 0 | if (parameters->tp_on) { |
8067 | 0 | cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag; |
8068 | 0 | cp->m_specific_param.m_enc.m_tp_on = 1; |
8069 | 0 | } |
8070 | |
|
8071 | | #ifdef USE_JPWL |
8072 | | /* |
8073 | | calculate JPWL encoding parameters |
8074 | | */ |
8075 | | |
8076 | | if (parameters->jpwl_epc_on) { |
8077 | | OPJ_INT32 i; |
8078 | | |
8079 | | /* set JPWL on */ |
8080 | | cp->epc_on = OPJ_TRUE; |
8081 | | cp->info_on = OPJ_FALSE; /* no informative technique */ |
8082 | | |
8083 | | /* set EPB on */ |
8084 | | if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { |
8085 | | cp->epb_on = OPJ_TRUE; |
8086 | | |
8087 | | cp->hprot_MH = parameters->jpwl_hprot_MH; |
8088 | | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
8089 | | cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; |
8090 | | cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; |
8091 | | } |
8092 | | /* if tile specs are not specified, copy MH specs */ |
8093 | | if (cp->hprot_TPH[0] == -1) { |
8094 | | cp->hprot_TPH_tileno[0] = 0; |
8095 | | cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; |
8096 | | } |
8097 | | for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { |
8098 | | cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; |
8099 | | cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; |
8100 | | cp->pprot[i] = parameters->jpwl_pprot[i]; |
8101 | | } |
8102 | | } |
8103 | | |
8104 | | /* set ESD writing */ |
8105 | | if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { |
8106 | | cp->esd_on = OPJ_TRUE; |
8107 | | |
8108 | | cp->sens_size = parameters->jpwl_sens_size; |
8109 | | cp->sens_addr = parameters->jpwl_sens_addr; |
8110 | | cp->sens_range = parameters->jpwl_sens_range; |
8111 | | |
8112 | | cp->sens_MH = parameters->jpwl_sens_MH; |
8113 | | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
8114 | | cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; |
8115 | | cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; |
8116 | | } |
8117 | | } |
8118 | | |
8119 | | /* always set RED writing to false: we are at the encoder */ |
8120 | | cp->red_on = OPJ_FALSE; |
8121 | | |
8122 | | } else { |
8123 | | cp->epc_on = OPJ_FALSE; |
8124 | | } |
8125 | | #endif /* USE_JPWL */ |
8126 | | |
8127 | | /* initialize the multiple tiles */ |
8128 | | /* ---------------------------- */ |
8129 | 0 | cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); |
8130 | 0 | if (!cp->tcps) { |
8131 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8132 | 0 | "Not enough memory to allocate tile coding parameters\n"); |
8133 | 0 | return OPJ_FALSE; |
8134 | 0 | } |
8135 | | |
8136 | 0 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
8137 | 0 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
8138 | 0 | const OPJ_BOOL fixed_distoratio = |
8139 | 0 | cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy == |
8140 | 0 | FIXED_DISTORTION_RATIO; |
8141 | 0 | tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers; |
8142 | |
|
8143 | 0 | for (j = 0; j < tcp->numlayers; j++) { |
8144 | 0 | if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) { |
8145 | 0 | if (fixed_distoratio) { |
8146 | 0 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
8147 | 0 | } |
8148 | 0 | tcp->rates[j] = parameters->tcp_rates[j]; |
8149 | 0 | } else { |
8150 | 0 | if (fixed_distoratio) { |
8151 | 0 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
8152 | 0 | } else { |
8153 | 0 | tcp->rates[j] = parameters->tcp_rates[j]; |
8154 | 0 | } |
8155 | 0 | } |
8156 | 0 | if (!fixed_distoratio && |
8157 | 0 | tcp->rates[j] <= 1.0) { |
8158 | 0 | tcp->rates[j] = 0.0; /* force lossless */ |
8159 | 0 | } |
8160 | 0 | } |
8161 | |
|
8162 | 0 | tcp->csty = (OPJ_UINT32)parameters->csty; |
8163 | 0 | tcp->prg = parameters->prog_order; |
8164 | 0 | tcp->mct = (OPJ_UINT32)parameters->tcp_mct; |
8165 | |
|
8166 | 0 | numpocs_tile = 0; |
8167 | 0 | tcp->POC = 0; |
8168 | |
|
8169 | 0 | if (parameters->numpocs) { |
8170 | | /* initialisation of POC */ |
8171 | 0 | for (i = 0; i < parameters->numpocs; i++) { |
8172 | 0 | if (tileno + 1 == parameters->POC[i].tile) { |
8173 | 0 | opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; |
8174 | |
|
8175 | 0 | if (parameters->POC[numpocs_tile].compno0 >= image->numcomps) { |
8176 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8177 | 0 | "Invalid compno0 for POC %d\n", i); |
8178 | 0 | return OPJ_FALSE; |
8179 | 0 | } |
8180 | | |
8181 | 0 | tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; |
8182 | 0 | tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; |
8183 | 0 | tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; |
8184 | 0 | tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; |
8185 | 0 | tcp_poc->compno1 = opj_uint_min(parameters->POC[numpocs_tile].compno1, |
8186 | 0 | image->numcomps); |
8187 | 0 | tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; |
8188 | 0 | tcp_poc->tile = parameters->POC[numpocs_tile].tile; |
8189 | |
|
8190 | 0 | numpocs_tile++; |
8191 | 0 | } |
8192 | 0 | } |
8193 | | |
8194 | 0 | if (numpocs_tile) { |
8195 | | |
8196 | | /* TODO MSD use the return value*/ |
8197 | 0 | opj_j2k_check_poc_val(parameters->POC, tileno, parameters->numpocs, |
8198 | 0 | (OPJ_UINT32)parameters->numresolution, image->numcomps, |
8199 | 0 | (OPJ_UINT32)parameters->tcp_numlayers, p_manager); |
8200 | |
|
8201 | 0 | tcp->POC = 1; |
8202 | 0 | tcp->numpocs = numpocs_tile - 1 ; |
8203 | 0 | } |
8204 | 0 | } else { |
8205 | 0 | tcp->numpocs = 0; |
8206 | 0 | } |
8207 | | |
8208 | 0 | tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); |
8209 | 0 | if (!tcp->tccps) { |
8210 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8211 | 0 | "Not enough memory to allocate tile component coding parameters\n"); |
8212 | 0 | return OPJ_FALSE; |
8213 | 0 | } |
8214 | 0 | if (parameters->mct_data) { |
8215 | |
|
8216 | 0 | OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof( |
8217 | 0 | OPJ_FLOAT32); |
8218 | 0 | OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8219 | 0 | OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data + |
8220 | 0 | lMctSize); |
8221 | |
|
8222 | 0 | if (!lTmpBuf) { |
8223 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8224 | 0 | "Not enough memory to allocate temp buffer\n"); |
8225 | 0 | return OPJ_FALSE; |
8226 | 0 | } |
8227 | | |
8228 | 0 | tcp->mct = 2; |
8229 | 0 | tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8230 | 0 | if (! tcp->m_mct_coding_matrix) { |
8231 | 0 | opj_free(lTmpBuf); |
8232 | 0 | lTmpBuf = NULL; |
8233 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8234 | 0 | "Not enough memory to allocate encoder MCT coding matrix \n"); |
8235 | 0 | return OPJ_FALSE; |
8236 | 0 | } |
8237 | 0 | memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize); |
8238 | 0 | memcpy(lTmpBuf, parameters->mct_data, lMctSize); |
8239 | |
|
8240 | 0 | tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
8241 | 0 | if (! tcp->m_mct_decoding_matrix) { |
8242 | 0 | opj_free(lTmpBuf); |
8243 | 0 | lTmpBuf = NULL; |
8244 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8245 | 0 | "Not enough memory to allocate encoder MCT decoding matrix \n"); |
8246 | 0 | return OPJ_FALSE; |
8247 | 0 | } |
8248 | 0 | if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix), |
8249 | 0 | image->numcomps) == OPJ_FALSE) { |
8250 | 0 | opj_free(lTmpBuf); |
8251 | 0 | lTmpBuf = NULL; |
8252 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8253 | 0 | "Failed to inverse encoder MCT decoding matrix \n"); |
8254 | 0 | return OPJ_FALSE; |
8255 | 0 | } |
8256 | | |
8257 | 0 | tcp->mct_norms = (OPJ_FLOAT64*) |
8258 | 0 | opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); |
8259 | 0 | if (! tcp->mct_norms) { |
8260 | 0 | opj_free(lTmpBuf); |
8261 | 0 | lTmpBuf = NULL; |
8262 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8263 | 0 | "Not enough memory to allocate encoder MCT norms \n"); |
8264 | 0 | return OPJ_FALSE; |
8265 | 0 | } |
8266 | 0 | opj_calculate_norms(tcp->mct_norms, image->numcomps, |
8267 | 0 | tcp->m_mct_decoding_matrix); |
8268 | 0 | opj_free(lTmpBuf); |
8269 | |
|
8270 | 0 | for (i = 0; i < image->numcomps; i++) { |
8271 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8272 | 0 | tccp->m_dc_level_shift = l_dc_shift[i]; |
8273 | 0 | } |
8274 | |
|
8275 | 0 | if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) { |
8276 | | /* free will be handled by opj_j2k_destroy */ |
8277 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n"); |
8278 | 0 | return OPJ_FALSE; |
8279 | 0 | } |
8280 | 0 | } else { |
8281 | 0 | if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */ |
8282 | 0 | if ((image->comps[0].dx != image->comps[1].dx) || |
8283 | 0 | (image->comps[0].dx != image->comps[2].dx) || |
8284 | 0 | (image->comps[0].dy != image->comps[1].dy) || |
8285 | 0 | (image->comps[0].dy != image->comps[2].dy)) { |
8286 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
8287 | 0 | "Cannot perform MCT on components with different sizes. Disabling MCT.\n"); |
8288 | 0 | tcp->mct = 0; |
8289 | 0 | } |
8290 | 0 | } |
8291 | 0 | for (i = 0; i < image->numcomps; i++) { |
8292 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8293 | 0 | opj_image_comp_t * l_comp = &(image->comps[i]); |
8294 | |
|
8295 | 0 | if (! l_comp->sgnd) { |
8296 | 0 | tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); |
8297 | 0 | } |
8298 | 0 | } |
8299 | 0 | } |
8300 | | |
8301 | 0 | for (i = 0; i < image->numcomps; i++) { |
8302 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
8303 | |
|
8304 | 0 | tccp->csty = parameters->csty & |
8305 | 0 | 0x01; /* 0 => one precinct || 1 => custom precinct */ |
8306 | 0 | tccp->numresolutions = (OPJ_UINT32)parameters->numresolution; |
8307 | 0 | tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
8308 | 0 | tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
8309 | 0 | tccp->cblksty = (OPJ_UINT32)parameters->mode; |
8310 | 0 | tccp->qmfbid = parameters->irreversible ? 0 : 1; |
8311 | 0 | tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : |
8312 | 0 | J2K_CCP_QNTSTY_NOQNT; |
8313 | 0 | tccp->numgbits = 2; |
8314 | |
|
8315 | 0 | if ((OPJ_INT32)i == parameters->roi_compno) { |
8316 | 0 | tccp->roishift = parameters->roi_shift; |
8317 | 0 | } else { |
8318 | 0 | tccp->roishift = 0; |
8319 | 0 | } |
8320 | |
|
8321 | 0 | if (parameters->csty & J2K_CCP_CSTY_PRT) { |
8322 | 0 | OPJ_INT32 p = 0, it_res; |
8323 | 0 | assert(tccp->numresolutions > 0); |
8324 | 0 | for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) { |
8325 | 0 | if (p < parameters->res_spec) { |
8326 | |
|
8327 | 0 | if (parameters->prcw_init[p] < 1) { |
8328 | 0 | tccp->prcw[it_res] = 1; |
8329 | 0 | } else { |
8330 | 0 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]); |
8331 | 0 | } |
8332 | |
|
8333 | 0 | if (parameters->prch_init[p] < 1) { |
8334 | 0 | tccp->prch[it_res] = 1; |
8335 | 0 | } else { |
8336 | 0 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]); |
8337 | 0 | } |
8338 | |
|
8339 | 0 | } else { |
8340 | 0 | OPJ_INT32 res_spec = parameters->res_spec; |
8341 | 0 | OPJ_INT32 size_prcw = 0; |
8342 | 0 | OPJ_INT32 size_prch = 0; |
8343 | |
|
8344 | 0 | assert(res_spec > 0); /* issue 189 */ |
8345 | 0 | size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); |
8346 | 0 | size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); |
8347 | | |
8348 | |
|
8349 | 0 | if (size_prcw < 1) { |
8350 | 0 | tccp->prcw[it_res] = 1; |
8351 | 0 | } else { |
8352 | 0 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw); |
8353 | 0 | } |
8354 | |
|
8355 | 0 | if (size_prch < 1) { |
8356 | 0 | tccp->prch[it_res] = 1; |
8357 | 0 | } else { |
8358 | 0 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch); |
8359 | 0 | } |
8360 | 0 | } |
8361 | 0 | p++; |
8362 | | /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ |
8363 | 0 | } /*end for*/ |
8364 | 0 | } else { |
8365 | 0 | for (j = 0; j < tccp->numresolutions; j++) { |
8366 | 0 | tccp->prcw[j] = 15; |
8367 | 0 | tccp->prch[j] = 15; |
8368 | 0 | } |
8369 | 0 | } |
8370 | |
|
8371 | 0 | opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); |
8372 | 0 | } |
8373 | 0 | } |
8374 | | |
8375 | 0 | if (parameters->mct_data) { |
8376 | 0 | opj_free(parameters->mct_data); |
8377 | 0 | parameters->mct_data = 00; |
8378 | 0 | } |
8379 | 0 | return OPJ_TRUE; |
8380 | 0 | } |
8381 | | |
8382 | | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, |
8383 | | OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) |
8384 | 282k | { |
8385 | 282k | assert(cstr_index != 00); |
8386 | | |
8387 | | /* expand the list? */ |
8388 | 282k | if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { |
8389 | 2.31k | opj_marker_info_t *new_marker; |
8390 | 2.31k | cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) |
8391 | 2.31k | cstr_index->maxmarknum); |
8392 | 2.31k | new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, |
8393 | 2.31k | cstr_index->maxmarknum * sizeof(opj_marker_info_t)); |
8394 | 2.31k | if (! new_marker) { |
8395 | 0 | opj_free(cstr_index->marker); |
8396 | 0 | cstr_index->marker = NULL; |
8397 | 0 | cstr_index->maxmarknum = 0; |
8398 | 0 | cstr_index->marknum = 0; |
8399 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */ |
8400 | 0 | return OPJ_FALSE; |
8401 | 0 | } |
8402 | 2.31k | cstr_index->marker = new_marker; |
8403 | 2.31k | } |
8404 | | |
8405 | | /* add the marker */ |
8406 | 282k | cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; |
8407 | 282k | cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; |
8408 | 282k | cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; |
8409 | 282k | cstr_index->marknum++; |
8410 | 282k | return OPJ_TRUE; |
8411 | 282k | } |
8412 | | |
8413 | | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, |
8414 | | opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, |
8415 | | OPJ_UINT32 len) |
8416 | 20.7k | { |
8417 | 20.7k | assert(cstr_index != 00); |
8418 | 20.7k | assert(cstr_index->tile_index != 00); |
8419 | | |
8420 | | /* expand the list? */ |
8421 | 20.7k | if ((cstr_index->tile_index[tileno].marknum + 1) > |
8422 | 20.7k | cstr_index->tile_index[tileno].maxmarknum) { |
8423 | 12 | opj_marker_info_t *new_marker; |
8424 | 12 | cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + |
8425 | 12 | (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum); |
8426 | 12 | new_marker = (opj_marker_info_t *) opj_realloc( |
8427 | 12 | cstr_index->tile_index[tileno].marker, |
8428 | 12 | cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t)); |
8429 | 12 | if (! new_marker) { |
8430 | 0 | opj_free(cstr_index->tile_index[tileno].marker); |
8431 | 0 | cstr_index->tile_index[tileno].marker = NULL; |
8432 | 0 | cstr_index->tile_index[tileno].maxmarknum = 0; |
8433 | 0 | cstr_index->tile_index[tileno].marknum = 0; |
8434 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */ |
8435 | 0 | return OPJ_FALSE; |
8436 | 0 | } |
8437 | 12 | cstr_index->tile_index[tileno].marker = new_marker; |
8438 | 12 | } |
8439 | | |
8440 | | /* add the marker */ |
8441 | 20.7k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type |
8442 | 20.7k | = (OPJ_UINT16)type; |
8443 | 20.7k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos |
8444 | 20.7k | = (OPJ_INT32)pos; |
8445 | 20.7k | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len |
8446 | 20.7k | = (OPJ_INT32)len; |
8447 | 20.7k | cstr_index->tile_index[tileno].marknum++; |
8448 | | |
8449 | 20.7k | if (type == J2K_MS_SOT) { |
8450 | 9.43k | OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; |
8451 | | |
8452 | 9.43k | if (cstr_index->tile_index[tileno].tp_index) { |
8453 | 9.43k | cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; |
8454 | 9.43k | } |
8455 | | |
8456 | 9.43k | } |
8457 | 20.7k | return OPJ_TRUE; |
8458 | 20.7k | } |
8459 | | |
8460 | | /* |
8461 | | * ----------------------------------------------------------------------- |
8462 | | * ----------------------------------------------------------------------- |
8463 | | * ----------------------------------------------------------------------- |
8464 | | */ |
8465 | | |
8466 | | OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k, |
8467 | | opj_stream_private_t *p_stream, |
8468 | | opj_event_mgr_t * p_manager |
8469 | | ) |
8470 | 7.48k | { |
8471 | 7.48k | (void)p_j2k; |
8472 | 7.48k | (void)p_stream; |
8473 | 7.48k | (void)p_manager; |
8474 | 7.48k | return OPJ_TRUE; |
8475 | 7.48k | } |
8476 | | |
8477 | | OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream, |
8478 | | opj_j2k_t* p_j2k, |
8479 | | opj_image_t** p_image, |
8480 | | opj_event_mgr_t* p_manager) |
8481 | 8.83k | { |
8482 | | /* preconditions */ |
8483 | 8.83k | assert(p_j2k != 00); |
8484 | 8.83k | assert(p_stream != 00); |
8485 | 8.83k | assert(p_manager != 00); |
8486 | | |
8487 | | /* create an empty image header */ |
8488 | 8.83k | p_j2k->m_private_image = opj_image_create0(); |
8489 | 8.83k | if (! p_j2k->m_private_image) { |
8490 | 0 | return OPJ_FALSE; |
8491 | 0 | } |
8492 | | |
8493 | | /* customization of the validation */ |
8494 | 8.83k | if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) { |
8495 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8496 | 0 | p_j2k->m_private_image = NULL; |
8497 | 0 | return OPJ_FALSE; |
8498 | 0 | } |
8499 | | |
8500 | | /* validation of the parameters codec */ |
8501 | 8.83k | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
8502 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8503 | 0 | p_j2k->m_private_image = NULL; |
8504 | 0 | return OPJ_FALSE; |
8505 | 0 | } |
8506 | | |
8507 | | /* customization of the encoding */ |
8508 | 8.83k | if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) { |
8509 | 0 | opj_image_destroy(p_j2k->m_private_image); |
8510 | 0 | p_j2k->m_private_image = NULL; |
8511 | 0 | return OPJ_FALSE; |
8512 | 0 | } |
8513 | | |
8514 | | /* read header */ |
8515 | 8.83k | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
8516 | 1.35k | opj_image_destroy(p_j2k->m_private_image); |
8517 | 1.35k | p_j2k->m_private_image = NULL; |
8518 | 1.35k | return OPJ_FALSE; |
8519 | 1.35k | } |
8520 | | |
8521 | 7.48k | *p_image = opj_image_create0(); |
8522 | 7.48k | if (!(*p_image)) { |
8523 | 0 | return OPJ_FALSE; |
8524 | 0 | } |
8525 | | |
8526 | | /* Copy codestream image information to the output image */ |
8527 | 7.48k | opj_copy_image_header(p_j2k->m_private_image, *p_image); |
8528 | | |
8529 | 7.48k | return OPJ_TRUE; |
8530 | 7.48k | } |
8531 | | |
8532 | | static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, |
8533 | | opj_event_mgr_t * p_manager) |
8534 | 8.83k | { |
8535 | | /* preconditions*/ |
8536 | 8.83k | assert(p_j2k != 00); |
8537 | 8.83k | assert(p_manager != 00); |
8538 | | |
8539 | 8.83k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
8540 | 8.83k | (opj_procedure)opj_j2k_read_header_procedure, p_manager)) { |
8541 | 0 | return OPJ_FALSE; |
8542 | 0 | } |
8543 | | |
8544 | | /* DEVELOPER CORNER, add your custom procedures */ |
8545 | 8.83k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
8546 | 8.83k | (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) { |
8547 | 0 | return OPJ_FALSE; |
8548 | 0 | } |
8549 | | |
8550 | 8.83k | return OPJ_TRUE; |
8551 | 8.83k | } |
8552 | | |
8553 | | static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, |
8554 | | opj_event_mgr_t * p_manager) |
8555 | 8.83k | { |
8556 | | /* preconditions*/ |
8557 | 8.83k | assert(p_j2k != 00); |
8558 | 8.83k | assert(p_manager != 00); |
8559 | | |
8560 | 8.83k | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
8561 | 8.83k | (opj_procedure)opj_j2k_build_decoder, p_manager)) { |
8562 | 0 | return OPJ_FALSE; |
8563 | 0 | } |
8564 | 8.83k | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
8565 | 8.83k | (opj_procedure)opj_j2k_decoding_validation, p_manager)) { |
8566 | 0 | return OPJ_FALSE; |
8567 | 0 | } |
8568 | | |
8569 | | /* DEVELOPER CORNER, add your custom validation procedure */ |
8570 | 8.83k | return OPJ_TRUE; |
8571 | 8.83k | } |
8572 | | |
8573 | | static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, |
8574 | | opj_stream_private_t *p_stream, |
8575 | | opj_event_mgr_t * p_manager) |
8576 | 0 | { |
8577 | 0 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8578 | 0 | OPJ_UINT32 i, j; |
8579 | | |
8580 | | /* preconditions */ |
8581 | 0 | assert(p_j2k != 00); |
8582 | 0 | assert(p_stream != 00); |
8583 | 0 | assert(p_manager != 00); |
8584 | |
|
8585 | 0 | OPJ_UNUSED(p_stream); |
8586 | 0 | OPJ_UNUSED(p_manager); |
8587 | |
|
8588 | 0 | if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { |
8589 | 0 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
8590 | 0 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
8591 | |
|
8592 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
8593 | 0 | if (l_tcp->mct == 2) { |
8594 | 0 | opj_tccp_t * l_tccp = l_tcp->tccps; |
8595 | 0 | l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); |
8596 | |
|
8597 | 0 | for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) { |
8598 | 0 | l_is_valid &= !(l_tccp->qmfbid & 1); |
8599 | 0 | ++l_tccp; |
8600 | 0 | } |
8601 | 0 | } |
8602 | 0 | ++l_tcp; |
8603 | 0 | } |
8604 | 0 | } |
8605 | |
|
8606 | 0 | return l_is_valid; |
8607 | 0 | } |
8608 | | |
8609 | | OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) |
8610 | 0 | { |
8611 | 0 | OPJ_UINT32 i; |
8612 | 0 | OPJ_UINT32 l_indix = 1; |
8613 | 0 | opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00; |
8614 | 0 | opj_simple_mcc_decorrelation_data_t * l_mcc_data; |
8615 | 0 | OPJ_UINT32 l_mct_size, l_nb_elem; |
8616 | 0 | OPJ_FLOAT32 * l_data, * l_current_data; |
8617 | 0 | opj_tccp_t * l_tccp; |
8618 | | |
8619 | | /* preconditions */ |
8620 | 0 | assert(p_tcp != 00); |
8621 | |
|
8622 | 0 | if (p_tcp->mct != 2) { |
8623 | 0 | return OPJ_TRUE; |
8624 | 0 | } |
8625 | | |
8626 | 0 | if (p_tcp->m_mct_decoding_matrix) { |
8627 | 0 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
8628 | 0 | opj_mct_data_t *new_mct_records; |
8629 | 0 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8630 | |
|
8631 | 0 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
8632 | 0 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
8633 | 0 | if (! new_mct_records) { |
8634 | 0 | opj_free(p_tcp->m_mct_records); |
8635 | 0 | p_tcp->m_mct_records = NULL; |
8636 | 0 | p_tcp->m_nb_max_mct_records = 0; |
8637 | 0 | p_tcp->m_nb_mct_records = 0; |
8638 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8639 | 0 | return OPJ_FALSE; |
8640 | 0 | } |
8641 | 0 | p_tcp->m_mct_records = new_mct_records; |
8642 | 0 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8643 | |
|
8644 | 0 | memset(l_mct_deco_data, 0, |
8645 | 0 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
8646 | 0 | opj_mct_data_t)); |
8647 | 0 | } |
8648 | 0 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8649 | |
|
8650 | 0 | if (l_mct_deco_data->m_data) { |
8651 | 0 | opj_free(l_mct_deco_data->m_data); |
8652 | 0 | l_mct_deco_data->m_data = 00; |
8653 | 0 | } |
8654 | |
|
8655 | 0 | l_mct_deco_data->m_index = l_indix++; |
8656 | 0 | l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; |
8657 | 0 | l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; |
8658 | 0 | l_nb_elem = p_image->numcomps * p_image->numcomps; |
8659 | 0 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; |
8660 | 0 | l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
8661 | |
|
8662 | 0 | if (! l_mct_deco_data->m_data) { |
8663 | 0 | return OPJ_FALSE; |
8664 | 0 | } |
8665 | | |
8666 | 0 | j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type]( |
8667 | 0 | p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem); |
8668 | |
|
8669 | 0 | l_mct_deco_data->m_data_size = l_mct_size; |
8670 | 0 | ++p_tcp->m_nb_mct_records; |
8671 | 0 | } |
8672 | | |
8673 | 0 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
8674 | 0 | opj_mct_data_t *new_mct_records; |
8675 | 0 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8676 | 0 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
8677 | 0 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
8678 | 0 | if (! new_mct_records) { |
8679 | 0 | opj_free(p_tcp->m_mct_records); |
8680 | 0 | p_tcp->m_mct_records = NULL; |
8681 | 0 | p_tcp->m_nb_max_mct_records = 0; |
8682 | 0 | p_tcp->m_nb_mct_records = 0; |
8683 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8684 | 0 | return OPJ_FALSE; |
8685 | 0 | } |
8686 | 0 | p_tcp->m_mct_records = new_mct_records; |
8687 | 0 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8688 | |
|
8689 | 0 | memset(l_mct_offset_data, 0, |
8690 | 0 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
8691 | 0 | opj_mct_data_t)); |
8692 | |
|
8693 | 0 | if (l_mct_deco_data) { |
8694 | 0 | l_mct_deco_data = l_mct_offset_data - 1; |
8695 | 0 | } |
8696 | 0 | } |
8697 | | |
8698 | 0 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
8699 | |
|
8700 | 0 | if (l_mct_offset_data->m_data) { |
8701 | 0 | opj_free(l_mct_offset_data->m_data); |
8702 | 0 | l_mct_offset_data->m_data = 00; |
8703 | 0 | } |
8704 | |
|
8705 | 0 | l_mct_offset_data->m_index = l_indix++; |
8706 | 0 | l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; |
8707 | 0 | l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; |
8708 | 0 | l_nb_elem = p_image->numcomps; |
8709 | 0 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; |
8710 | 0 | l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
8711 | |
|
8712 | 0 | if (! l_mct_offset_data->m_data) { |
8713 | 0 | return OPJ_FALSE; |
8714 | 0 | } |
8715 | | |
8716 | 0 | l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); |
8717 | 0 | if (! l_data) { |
8718 | 0 | opj_free(l_mct_offset_data->m_data); |
8719 | 0 | l_mct_offset_data->m_data = 00; |
8720 | 0 | return OPJ_FALSE; |
8721 | 0 | } |
8722 | | |
8723 | 0 | l_tccp = p_tcp->tccps; |
8724 | 0 | l_current_data = l_data; |
8725 | |
|
8726 | 0 | for (i = 0; i < l_nb_elem; ++i) { |
8727 | 0 | *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift); |
8728 | 0 | ++l_tccp; |
8729 | 0 | } |
8730 | |
|
8731 | 0 | j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data, |
8732 | 0 | l_mct_offset_data->m_data, l_nb_elem); |
8733 | |
|
8734 | 0 | opj_free(l_data); |
8735 | |
|
8736 | 0 | l_mct_offset_data->m_data_size = l_mct_size; |
8737 | |
|
8738 | 0 | ++p_tcp->m_nb_mct_records; |
8739 | |
|
8740 | 0 | if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { |
8741 | 0 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
8742 | 0 | p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
8743 | 0 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
8744 | 0 | p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof( |
8745 | 0 | opj_simple_mcc_decorrelation_data_t)); |
8746 | 0 | if (! new_mcc_records) { |
8747 | 0 | opj_free(p_tcp->m_mcc_records); |
8748 | 0 | p_tcp->m_mcc_records = NULL; |
8749 | 0 | p_tcp->m_nb_max_mcc_records = 0; |
8750 | 0 | p_tcp->m_nb_mcc_records = 0; |
8751 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
8752 | 0 | return OPJ_FALSE; |
8753 | 0 | } |
8754 | 0 | p_tcp->m_mcc_records = new_mcc_records; |
8755 | 0 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
8756 | 0 | memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * |
8757 | 0 | sizeof(opj_simple_mcc_decorrelation_data_t)); |
8758 | |
|
8759 | 0 | } |
8760 | | |
8761 | 0 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
8762 | 0 | l_mcc_data->m_decorrelation_array = l_mct_deco_data; |
8763 | 0 | l_mcc_data->m_is_irreversible = 1; |
8764 | 0 | l_mcc_data->m_nb_comps = p_image->numcomps; |
8765 | 0 | l_mcc_data->m_index = l_indix++; |
8766 | 0 | l_mcc_data->m_offset_array = l_mct_offset_data; |
8767 | 0 | ++p_tcp->m_nb_mcc_records; |
8768 | |
|
8769 | 0 | return OPJ_TRUE; |
8770 | 0 | } |
8771 | | |
8772 | | static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, |
8773 | | opj_stream_private_t *p_stream, |
8774 | | opj_event_mgr_t * p_manager) |
8775 | 8.83k | { |
8776 | | /* add here initialization of cp |
8777 | | copy paste of setup_decoder */ |
8778 | 8.83k | (void)p_j2k; |
8779 | 8.83k | (void)p_stream; |
8780 | 8.83k | (void)p_manager; |
8781 | 8.83k | return OPJ_TRUE; |
8782 | 8.83k | } |
8783 | | |
8784 | | static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, |
8785 | | opj_stream_private_t *p_stream, |
8786 | | opj_event_mgr_t * p_manager) |
8787 | 0 | { |
8788 | | /* add here initialization of cp |
8789 | | copy paste of setup_encoder */ |
8790 | 0 | (void)p_j2k; |
8791 | 0 | (void)p_stream; |
8792 | 0 | (void)p_manager; |
8793 | 0 | return OPJ_TRUE; |
8794 | 0 | } |
8795 | | |
8796 | | static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, |
8797 | | opj_stream_private_t *p_stream, |
8798 | | opj_event_mgr_t * p_manager) |
8799 | 0 | { |
8800 | 0 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8801 | | |
8802 | | /* preconditions */ |
8803 | 0 | assert(p_j2k != 00); |
8804 | 0 | assert(p_stream != 00); |
8805 | 0 | assert(p_manager != 00); |
8806 | |
|
8807 | 0 | OPJ_UNUSED(p_stream); |
8808 | | |
8809 | | /* STATE checking */ |
8810 | | /* make sure the state is at 0 */ |
8811 | 0 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); |
8812 | | |
8813 | | /* POINTER validation */ |
8814 | | /* make sure a p_j2k codec is present */ |
8815 | 0 | l_is_valid &= (p_j2k->m_procedure_list != 00); |
8816 | | /* make sure a validation list is present */ |
8817 | 0 | l_is_valid &= (p_j2k->m_validation_list != 00); |
8818 | | |
8819 | | /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */ |
8820 | | /* 33 (32) would always fail the check below (if a cast to 64bits was done) */ |
8821 | | /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */ |
8822 | 0 | if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || |
8823 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) { |
8824 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8825 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8826 | 0 | return OPJ_FALSE; |
8827 | 0 | } |
8828 | | |
8829 | 0 | if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 << |
8830 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
8831 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8832 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8833 | 0 | return OPJ_FALSE; |
8834 | 0 | } |
8835 | | |
8836 | 0 | if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 << |
8837 | 0 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
8838 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8839 | 0 | "Number of resolutions is too high in comparison to the size of tiles\n"); |
8840 | 0 | return OPJ_FALSE; |
8841 | 0 | } |
8842 | | |
8843 | | /* PARAMETER VALIDATION */ |
8844 | 0 | return l_is_valid; |
8845 | 0 | } |
8846 | | |
8847 | | static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k, |
8848 | | opj_stream_private_t *p_stream, |
8849 | | opj_event_mgr_t * p_manager |
8850 | | ) |
8851 | 8.83k | { |
8852 | 8.83k | OPJ_BOOL l_is_valid = OPJ_TRUE; |
8853 | | |
8854 | | /* preconditions*/ |
8855 | 8.83k | assert(p_j2k != 00); |
8856 | 8.83k | assert(p_stream != 00); |
8857 | 8.83k | assert(p_manager != 00); |
8858 | | |
8859 | 8.83k | OPJ_UNUSED(p_stream); |
8860 | 8.83k | OPJ_UNUSED(p_manager); |
8861 | | |
8862 | | /* STATE checking */ |
8863 | | /* make sure the state is at 0 */ |
8864 | | #ifdef TODO_MSD |
8865 | | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); |
8866 | | #endif |
8867 | 8.83k | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); |
8868 | | |
8869 | | /* POINTER validation */ |
8870 | | /* make sure a p_j2k codec is present */ |
8871 | | /* make sure a procedure list is present */ |
8872 | 8.83k | l_is_valid &= (p_j2k->m_procedure_list != 00); |
8873 | | /* make sure a validation list is present */ |
8874 | 8.83k | l_is_valid &= (p_j2k->m_validation_list != 00); |
8875 | | |
8876 | | /* PARAMETER VALIDATION */ |
8877 | 8.83k | return l_is_valid; |
8878 | 8.83k | } |
8879 | | |
8880 | | /** Fill p_j2k->cstr_index->tp_index[].start_pos/end_pos fields from TLM marker segments */ |
8881 | | static void opj_j2k_build_tp_index_from_tlm(opj_j2k_t* p_j2k, |
8882 | | opj_event_mgr_t * p_manager) |
8883 | 7.48k | { |
8884 | 7.48k | opj_j2k_tlm_info_t* l_tlm; |
8885 | 7.48k | OPJ_UINT32 i; |
8886 | 7.48k | OPJ_OFF_T l_cur_offset; |
8887 | | |
8888 | 7.48k | assert(p_j2k->cstr_index->main_head_end > 0); |
8889 | 7.48k | assert(p_j2k->cstr_index->nb_of_tiles > 0); |
8890 | 7.48k | assert(p_j2k->cstr_index->tile_index != NULL); |
8891 | | |
8892 | 7.48k | l_tlm = &(p_j2k->m_specific_param.m_decoder.m_tlm); |
8893 | | |
8894 | 7.48k | if (l_tlm->m_entries_count == 0) { |
8895 | 7.48k | l_tlm->m_is_invalid = OPJ_TRUE; |
8896 | 7.48k | return; |
8897 | 7.48k | } |
8898 | | |
8899 | 2 | if (l_tlm->m_is_invalid) { |
8900 | 1 | return; |
8901 | 1 | } |
8902 | | |
8903 | | /* Initial pass to count the number of tile-parts per tile */ |
8904 | 3 | for (i = 0; i < l_tlm->m_entries_count; ++i) { |
8905 | 2 | OPJ_UINT32 l_tile_index_no = l_tlm->m_tile_part_infos[i].m_tile_index; |
8906 | 2 | assert(l_tile_index_no < p_j2k->cstr_index->nb_of_tiles); |
8907 | 2 | p_j2k->cstr_index->tile_index[l_tile_index_no].tileno = l_tile_index_no; |
8908 | 2 | ++p_j2k->cstr_index->tile_index[l_tile_index_no].current_nb_tps; |
8909 | 2 | } |
8910 | | |
8911 | | /* Now check that all tiles have at least one tile-part */ |
8912 | 3 | for (i = 0; i < p_j2k->cstr_index->nb_of_tiles; ++i) { |
8913 | 3 | if (p_j2k->cstr_index->tile_index[i].current_nb_tps == 0) { |
8914 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
8915 | 1 | "opj_j2k_build_tp_index_from_tlm(): tile %d has no " |
8916 | 1 | "registered tile-part in TLM marker segments.\n", i); |
8917 | 1 | goto error; |
8918 | 1 | } |
8919 | 3 | } |
8920 | | |
8921 | | /* Final pass to fill p_j2k->cstr_index */ |
8922 | 0 | l_cur_offset = p_j2k->cstr_index->main_head_end; |
8923 | 0 | for (i = 0; i < l_tlm->m_entries_count; ++i) { |
8924 | 0 | OPJ_UINT32 l_tile_index_no = l_tlm->m_tile_part_infos[i].m_tile_index; |
8925 | 0 | opj_tile_index_t* l_tile_index = & |
8926 | 0 | (p_j2k->cstr_index->tile_index[l_tile_index_no]); |
8927 | 0 | if (!l_tile_index->tp_index) { |
8928 | 0 | l_tile_index->tp_index = (opj_tp_index_t *) opj_calloc( |
8929 | 0 | l_tile_index->current_nb_tps, sizeof(opj_tp_index_t)); |
8930 | 0 | if (! l_tile_index->tp_index) { |
8931 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
8932 | 0 | "opj_j2k_build_tp_index_from_tlm(): tile index allocation failed\n"); |
8933 | 0 | goto error; |
8934 | 0 | } |
8935 | 0 | } |
8936 | | |
8937 | 0 | assert(l_tile_index->nb_tps < l_tile_index->current_nb_tps); |
8938 | 0 | l_tile_index->tp_index[l_tile_index->nb_tps].start_pos = l_cur_offset; |
8939 | | /* We don't know how to set the tp_index[].end_header field, but this is not really needed */ |
8940 | | /* If there would be no markers between SOT and SOD, that would be : */ |
8941 | | /* l_tile_index->tp_index[l_tile_index->nb_tps].end_header = l_cur_offset + 12; */ |
8942 | 0 | l_tile_index->tp_index[l_tile_index->nb_tps].end_pos = l_cur_offset + |
8943 | 0 | l_tlm->m_tile_part_infos[i].m_length; |
8944 | 0 | ++l_tile_index->nb_tps; |
8945 | |
|
8946 | 0 | l_cur_offset += l_tlm->m_tile_part_infos[i].m_length; |
8947 | 0 | } |
8948 | | |
8949 | 0 | return; |
8950 | | |
8951 | 1 | error: |
8952 | 1 | l_tlm->m_is_invalid = OPJ_TRUE; |
8953 | 3 | for (i = 0; i < l_tlm->m_entries_count; ++i) { |
8954 | 2 | OPJ_UINT32 l_tile_index = l_tlm->m_tile_part_infos[i].m_tile_index; |
8955 | 2 | p_j2k->cstr_index->tile_index[l_tile_index].current_nb_tps = 0; |
8956 | 2 | opj_free(p_j2k->cstr_index->tile_index[l_tile_index].tp_index); |
8957 | 2 | p_j2k->cstr_index->tile_index[l_tile_index].tp_index = NULL; |
8958 | 2 | } |
8959 | 1 | } |
8960 | | |
8961 | | static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, |
8962 | | opj_stream_private_t *p_stream, |
8963 | | opj_event_mgr_t * p_manager) |
8964 | 8.83k | { |
8965 | 8.83k | OPJ_UINT32 l_current_marker; |
8966 | 8.83k | OPJ_UINT32 l_marker_size; |
8967 | 8.83k | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
8968 | 8.83k | OPJ_BOOL l_has_siz = 0; |
8969 | 8.83k | OPJ_BOOL l_has_cod = 0; |
8970 | 8.83k | OPJ_BOOL l_has_qcd = 0; |
8971 | | |
8972 | | /* preconditions */ |
8973 | 8.83k | assert(p_stream != 00); |
8974 | 8.83k | assert(p_j2k != 00); |
8975 | 8.83k | assert(p_manager != 00); |
8976 | | |
8977 | | /* We enter in the main header */ |
8978 | 8.83k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; |
8979 | | |
8980 | | /* Try to read the SOC marker, the codestream must begin with SOC marker */ |
8981 | 8.83k | if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) { |
8982 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n"); |
8983 | 1 | return OPJ_FALSE; |
8984 | 1 | } |
8985 | | |
8986 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8987 | 8.83k | if (opj_stream_read_data(p_stream, |
8988 | 8.83k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8989 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
8990 | 1 | return OPJ_FALSE; |
8991 | 1 | } |
8992 | | |
8993 | | /* Read 2 bytes as the new marker ID */ |
8994 | 8.83k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
8995 | 8.83k | &l_current_marker, 2); |
8996 | | |
8997 | | /* Try to read until the SOT is detected */ |
8998 | 169k | while (l_current_marker != J2K_MS_SOT) { |
8999 | | |
9000 | | /* Check if the current marker ID is valid */ |
9001 | 162k | if (l_current_marker < 0xff00) { |
9002 | 171 | opj_event_msg(p_manager, EVT_ERROR, |
9003 | 171 | "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker); |
9004 | 171 | return OPJ_FALSE; |
9005 | 171 | } |
9006 | | |
9007 | | /* Get the marker handler from the marker ID */ |
9008 | 162k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
9009 | | |
9010 | | /* Manage case where marker is unknown */ |
9011 | 162k | if (l_marker_handler->id == J2K_MS_UNK) { |
9012 | 113k | if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) { |
9013 | 180 | opj_event_msg(p_manager, EVT_ERROR, |
9014 | 180 | "Unknown marker has been detected and generated error.\n"); |
9015 | 180 | return OPJ_FALSE; |
9016 | 180 | } |
9017 | | |
9018 | 113k | if (l_current_marker == J2K_MS_SOT) { |
9019 | 748 | break; /* SOT marker is detected main header is completely read */ |
9020 | 112k | } else { /* Get the marker handler from the marker ID */ |
9021 | 112k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
9022 | 112k | } |
9023 | 113k | } |
9024 | | |
9025 | 161k | if (l_marker_handler->id == J2K_MS_SIZ) { |
9026 | | /* Mark required SIZ marker as found */ |
9027 | 8.75k | l_has_siz = 1; |
9028 | 8.75k | } |
9029 | 161k | if (l_marker_handler->id == J2K_MS_COD) { |
9030 | | /* Mark required COD marker as found */ |
9031 | 10.4k | l_has_cod = 1; |
9032 | 10.4k | } |
9033 | 161k | if (l_marker_handler->id == J2K_MS_QCD) { |
9034 | | /* Mark required QCD marker as found */ |
9035 | 11.3k | l_has_qcd = 1; |
9036 | 11.3k | } |
9037 | | |
9038 | | /* Check if the marker is known and if it is the right place to find it */ |
9039 | 161k | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
9040 | 10 | opj_event_msg(p_manager, EVT_ERROR, |
9041 | 10 | "Marker is not compliant with its position\n"); |
9042 | 10 | return OPJ_FALSE; |
9043 | 10 | } |
9044 | | |
9045 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9046 | 161k | if (opj_stream_read_data(p_stream, |
9047 | 161k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9048 | 54 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9049 | 54 | return OPJ_FALSE; |
9050 | 54 | } |
9051 | | |
9052 | | /* read 2 bytes as the marker size */ |
9053 | 161k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
9054 | 161k | 2); |
9055 | 161k | if (l_marker_size < 2) { |
9056 | 12 | opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n"); |
9057 | 12 | return OPJ_FALSE; |
9058 | 12 | } |
9059 | 161k | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
9060 | | |
9061 | | /* Check if the marker size is compatible with the header data size */ |
9062 | 161k | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
9063 | 109 | OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc( |
9064 | 109 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
9065 | 109 | if (! new_header_data) { |
9066 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9067 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
9068 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9069 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
9070 | 0 | return OPJ_FALSE; |
9071 | 0 | } |
9072 | 109 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
9073 | 109 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
9074 | 109 | } |
9075 | | |
9076 | | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
9077 | 161k | if (opj_stream_read_data(p_stream, |
9078 | 161k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
9079 | 161k | p_manager) != l_marker_size) { |
9080 | 198 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9081 | 198 | return OPJ_FALSE; |
9082 | 198 | } |
9083 | | |
9084 | | /* Read the marker segment with the correct marker handler */ |
9085 | 161k | if (!(*(l_marker_handler->handler))(p_j2k, |
9086 | 161k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
9087 | 360 | opj_event_msg(p_manager, EVT_ERROR, |
9088 | 360 | "Marker handler function failed to read the marker segment\n"); |
9089 | 360 | return OPJ_FALSE; |
9090 | 360 | } |
9091 | | |
9092 | | /* Add the marker to the codestream index*/ |
9093 | 161k | if (OPJ_FALSE == opj_j2k_add_mhmarker( |
9094 | 161k | p_j2k->cstr_index, |
9095 | 161k | l_marker_handler->id, |
9096 | 161k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
9097 | 161k | l_marker_size + 4)) { |
9098 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
9099 | 0 | return OPJ_FALSE; |
9100 | 0 | } |
9101 | | |
9102 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
9103 | 161k | if (opj_stream_read_data(p_stream, |
9104 | 161k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9105 | 304 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9106 | 304 | return OPJ_FALSE; |
9107 | 304 | } |
9108 | | |
9109 | | /* read 2 bytes as the new marker ID */ |
9110 | 160k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9111 | 160k | &l_current_marker, 2); |
9112 | 160k | } |
9113 | | |
9114 | 7.54k | if (l_has_siz == 0) { |
9115 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
9116 | 1 | "required SIZ marker not found in main header\n"); |
9117 | 1 | return OPJ_FALSE; |
9118 | 1 | } |
9119 | 7.54k | if (l_has_cod == 0) { |
9120 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
9121 | 4 | "required COD marker not found in main header\n"); |
9122 | 4 | return OPJ_FALSE; |
9123 | 4 | } |
9124 | 7.54k | if (l_has_qcd == 0) { |
9125 | 2 | opj_event_msg(p_manager, EVT_ERROR, |
9126 | 2 | "required QCD marker not found in main header\n"); |
9127 | 2 | return OPJ_FALSE; |
9128 | 2 | } |
9129 | | |
9130 | 7.54k | if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) { |
9131 | 58 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n"); |
9132 | 58 | return OPJ_FALSE; |
9133 | 58 | } |
9134 | | |
9135 | 7.48k | opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); |
9136 | | |
9137 | | /* Position of the last element if the main header */ |
9138 | 7.48k | p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; |
9139 | | |
9140 | | /* Build tile-part index from TLM information */ |
9141 | 7.48k | opj_j2k_build_tp_index_from_tlm(p_j2k, p_manager); |
9142 | | |
9143 | | /* Next step: read a tile-part header */ |
9144 | 7.48k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
9145 | | |
9146 | 7.48k | return OPJ_TRUE; |
9147 | 7.54k | } |
9148 | | |
9149 | | static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, |
9150 | | opj_procedure_list_t * p_procedure_list, |
9151 | | opj_stream_private_t *p_stream, |
9152 | | opj_event_mgr_t * p_manager) |
9153 | 25.1k | { |
9154 | 25.1k | OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *, |
9155 | 25.1k | opj_event_mgr_t *) = 00; |
9156 | 25.1k | OPJ_BOOL l_result = OPJ_TRUE; |
9157 | 25.1k | OPJ_UINT32 l_nb_proc, i; |
9158 | | |
9159 | | /* preconditions*/ |
9160 | 25.1k | assert(p_procedure_list != 00); |
9161 | 25.1k | assert(p_j2k != 00); |
9162 | 25.1k | assert(p_stream != 00); |
9163 | 25.1k | assert(p_manager != 00); |
9164 | | |
9165 | 25.1k | l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); |
9166 | 25.1k | l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *, |
9167 | 25.1k | opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); |
9168 | | |
9169 | 67.9k | for (i = 0; i < l_nb_proc; ++i) { |
9170 | 42.7k | l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager)); |
9171 | 42.7k | ++l_procedure; |
9172 | 42.7k | } |
9173 | | |
9174 | | /* and clear the procedure list at the end.*/ |
9175 | 25.1k | opj_procedure_list_clear(p_procedure_list); |
9176 | 25.1k | return l_result; |
9177 | 25.1k | } |
9178 | | |
9179 | | /* FIXME DOC*/ |
9180 | | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, |
9181 | | opj_stream_private_t *p_stream, |
9182 | | opj_event_mgr_t * p_manager |
9183 | | ) |
9184 | 7.48k | { |
9185 | 7.48k | opj_tcp_t * l_tcp = 00; |
9186 | 7.48k | opj_tcp_t * l_default_tcp = 00; |
9187 | 7.48k | OPJ_UINT32 l_nb_tiles; |
9188 | 7.48k | OPJ_UINT32 i, j; |
9189 | 7.48k | opj_tccp_t *l_current_tccp = 00; |
9190 | 7.48k | OPJ_UINT32 l_tccp_size; |
9191 | 7.48k | OPJ_UINT32 l_mct_size; |
9192 | 7.48k | opj_image_t * l_image; |
9193 | 7.48k | OPJ_UINT32 l_mcc_records_size, l_mct_records_size; |
9194 | 7.48k | opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; |
9195 | 7.48k | opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; |
9196 | 7.48k | OPJ_UINT32 l_offset; |
9197 | | |
9198 | | /* preconditions */ |
9199 | 7.48k | assert(p_j2k != 00); |
9200 | 7.48k | assert(p_stream != 00); |
9201 | 7.48k | assert(p_manager != 00); |
9202 | | |
9203 | 7.48k | OPJ_UNUSED(p_stream); |
9204 | | |
9205 | 7.48k | l_image = p_j2k->m_private_image; |
9206 | 7.48k | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
9207 | 7.48k | l_tcp = p_j2k->m_cp.tcps; |
9208 | 7.48k | l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t); |
9209 | 7.48k | l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
9210 | 7.48k | l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof( |
9211 | 7.48k | OPJ_FLOAT32); |
9212 | | |
9213 | | /* For each tile */ |
9214 | 17.5M | for (i = 0; i < l_nb_tiles; ++i) { |
9215 | | /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/ |
9216 | 17.5M | l_current_tccp = l_tcp->tccps; |
9217 | | /*Copy default coding parameters into the current tile coding parameters*/ |
9218 | 17.5M | memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t)); |
9219 | | /* Initialize some values of the current tile coding parameters*/ |
9220 | 17.5M | l_tcp->cod = 0; |
9221 | 17.5M | l_tcp->ppt = 0; |
9222 | 17.5M | l_tcp->ppt_data = 00; |
9223 | 17.5M | l_tcp->m_current_tile_part_number = -1; |
9224 | | /* Remove memory not owned by this tile in case of early error return. */ |
9225 | 17.5M | l_tcp->m_mct_decoding_matrix = 00; |
9226 | 17.5M | l_tcp->m_nb_max_mct_records = 0; |
9227 | 17.5M | l_tcp->m_mct_records = 00; |
9228 | 17.5M | l_tcp->m_nb_max_mcc_records = 0; |
9229 | 17.5M | l_tcp->m_mcc_records = 00; |
9230 | | /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ |
9231 | 17.5M | l_tcp->tccps = l_current_tccp; |
9232 | | |
9233 | | /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ |
9234 | 17.5M | if (l_default_tcp->m_mct_decoding_matrix) { |
9235 | 293 | l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
9236 | 293 | if (! l_tcp->m_mct_decoding_matrix) { |
9237 | 0 | return OPJ_FALSE; |
9238 | 0 | } |
9239 | 293 | memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix, |
9240 | 293 | l_mct_size); |
9241 | 293 | } |
9242 | | |
9243 | | /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ |
9244 | 17.5M | l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof( |
9245 | 17.5M | opj_mct_data_t); |
9246 | 17.5M | l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); |
9247 | 17.5M | if (! l_tcp->m_mct_records) { |
9248 | 0 | return OPJ_FALSE; |
9249 | 0 | } |
9250 | 17.5M | memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size); |
9251 | | |
9252 | | /* Copy the mct record data from dflt_tile_cp to the current tile*/ |
9253 | 17.5M | l_src_mct_rec = l_default_tcp->m_mct_records; |
9254 | 17.5M | l_dest_mct_rec = l_tcp->m_mct_records; |
9255 | | |
9256 | 17.5M | for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) { |
9257 | | |
9258 | 2.84k | if (l_src_mct_rec->m_data) { |
9259 | | |
9260 | 1.59k | l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); |
9261 | 1.59k | if (! l_dest_mct_rec->m_data) { |
9262 | 0 | return OPJ_FALSE; |
9263 | 0 | } |
9264 | 1.59k | memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data, |
9265 | 1.59k | l_src_mct_rec->m_data_size); |
9266 | 1.59k | } |
9267 | | |
9268 | 2.84k | ++l_src_mct_rec; |
9269 | 2.84k | ++l_dest_mct_rec; |
9270 | | /* Update with each pass to free exactly what has been allocated on early return. */ |
9271 | 2.84k | l_tcp->m_nb_max_mct_records += 1; |
9272 | 2.84k | } |
9273 | | |
9274 | | /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ |
9275 | 17.5M | l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof( |
9276 | 17.5M | opj_simple_mcc_decorrelation_data_t); |
9277 | 17.5M | l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc( |
9278 | 17.5M | l_mcc_records_size); |
9279 | 17.5M | if (! l_tcp->m_mcc_records) { |
9280 | 0 | return OPJ_FALSE; |
9281 | 0 | } |
9282 | 17.5M | memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size); |
9283 | 17.5M | l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records; |
9284 | | |
9285 | | /* Copy the mcc record data from dflt_tile_cp to the current tile*/ |
9286 | 17.5M | l_src_mcc_rec = l_default_tcp->m_mcc_records; |
9287 | 17.5M | l_dest_mcc_rec = l_tcp->m_mcc_records; |
9288 | | |
9289 | 192M | for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) { |
9290 | | |
9291 | 175M | if (l_src_mcc_rec->m_decorrelation_array) { |
9292 | 1.88k | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - |
9293 | 1.88k | l_default_tcp->m_mct_records); |
9294 | 1.88k | l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; |
9295 | 1.88k | } |
9296 | | |
9297 | 175M | if (l_src_mcc_rec->m_offset_array) { |
9298 | 1.59k | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - |
9299 | 1.59k | l_default_tcp->m_mct_records); |
9300 | 1.59k | l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; |
9301 | 1.59k | } |
9302 | | |
9303 | 175M | ++l_src_mcc_rec; |
9304 | 175M | ++l_dest_mcc_rec; |
9305 | 175M | } |
9306 | | |
9307 | | /* Copy all the dflt_tile_compo_cp to the current tile cp */ |
9308 | 17.5M | memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size); |
9309 | | |
9310 | | /* Move to next tile cp*/ |
9311 | 17.5M | ++l_tcp; |
9312 | 17.5M | } |
9313 | | |
9314 | | /* Create the current tile decoder*/ |
9315 | 7.48k | p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE); |
9316 | 7.48k | if (! p_j2k->m_tcd) { |
9317 | 0 | return OPJ_FALSE; |
9318 | 0 | } |
9319 | | |
9320 | 7.48k | if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) { |
9321 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
9322 | 0 | p_j2k->m_tcd = 00; |
9323 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
9324 | 0 | return OPJ_FALSE; |
9325 | 0 | } |
9326 | | |
9327 | 7.48k | return OPJ_TRUE; |
9328 | 7.48k | } |
9329 | | |
9330 | | static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler( |
9331 | | OPJ_UINT32 p_id) |
9332 | 1.31M | { |
9333 | 1.31M | const opj_dec_memory_marker_handler_t *e; |
9334 | 28.6M | for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { |
9335 | 27.6M | if (e->id == p_id) { |
9336 | 285k | break; /* we find a handler corresponding to the marker ID*/ |
9337 | 285k | } |
9338 | 27.6M | } |
9339 | 1.31M | return e; |
9340 | 1.31M | } |
9341 | | |
9342 | | void opj_j2k_destroy(opj_j2k_t *p_j2k) |
9343 | 8.86k | { |
9344 | 8.86k | if (p_j2k == 00) { |
9345 | 0 | return; |
9346 | 0 | } |
9347 | | |
9348 | 8.86k | if (p_j2k->m_is_decoder) { |
9349 | | |
9350 | 8.86k | if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { |
9351 | 8.86k | opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
9352 | 8.86k | opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
9353 | 8.86k | p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; |
9354 | 8.86k | } |
9355 | | |
9356 | 8.86k | if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { |
9357 | 8.86k | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9358 | 8.86k | p_j2k->m_specific_param.m_decoder.m_header_data = 00; |
9359 | 8.86k | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9360 | 8.86k | } |
9361 | | |
9362 | 8.86k | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
9363 | 8.86k | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00; |
9364 | 8.86k | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
9365 | | |
9366 | 8.86k | opj_free(p_j2k->m_specific_param.m_decoder.m_tlm.m_tile_part_infos); |
9367 | 8.86k | p_j2k->m_specific_param.m_decoder.m_tlm.m_tile_part_infos = NULL; |
9368 | | |
9369 | 8.86k | opj_free(p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset); |
9370 | 8.86k | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset = NULL; |
9371 | | |
9372 | 8.86k | } else { |
9373 | |
|
9374 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
9375 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
9376 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; |
9377 | 0 | } |
9378 | |
|
9379 | 0 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
9380 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
9381 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; |
9382 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; |
9383 | 0 | } |
9384 | |
|
9385 | 0 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
9386 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
9387 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; |
9388 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
9389 | 0 | } |
9390 | 0 | } |
9391 | | |
9392 | 8.86k | opj_tcd_destroy(p_j2k->m_tcd); |
9393 | | |
9394 | 8.86k | opj_j2k_cp_destroy(&(p_j2k->m_cp)); |
9395 | 8.86k | memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t)); |
9396 | | |
9397 | 8.86k | opj_procedure_list_destroy(p_j2k->m_procedure_list); |
9398 | 8.86k | p_j2k->m_procedure_list = 00; |
9399 | | |
9400 | 8.86k | opj_procedure_list_destroy(p_j2k->m_validation_list); |
9401 | 8.86k | p_j2k->m_procedure_list = 00; |
9402 | | |
9403 | 8.86k | j2k_destroy_cstr_index(p_j2k->cstr_index); |
9404 | 8.86k | p_j2k->cstr_index = NULL; |
9405 | | |
9406 | 8.86k | opj_image_destroy(p_j2k->m_private_image); |
9407 | 8.86k | p_j2k->m_private_image = NULL; |
9408 | | |
9409 | 8.86k | opj_image_destroy(p_j2k->m_output_image); |
9410 | 8.86k | p_j2k->m_output_image = NULL; |
9411 | | |
9412 | 8.86k | opj_thread_pool_destroy(p_j2k->m_tp); |
9413 | 8.86k | p_j2k->m_tp = NULL; |
9414 | | |
9415 | 8.86k | opj_free(p_j2k); |
9416 | 8.86k | } |
9417 | | |
9418 | | void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind) |
9419 | 8.86k | { |
9420 | 8.86k | if (p_cstr_ind) { |
9421 | | |
9422 | 8.86k | if (p_cstr_ind->marker) { |
9423 | 8.86k | opj_free(p_cstr_ind->marker); |
9424 | 8.86k | p_cstr_ind->marker = NULL; |
9425 | 8.86k | } |
9426 | | |
9427 | 8.86k | if (p_cstr_ind->tile_index) { |
9428 | 8.50k | OPJ_UINT32 it_tile = 0; |
9429 | | |
9430 | 18.9M | for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { |
9431 | | |
9432 | 18.9M | if (p_cstr_ind->tile_index[it_tile].packet_index) { |
9433 | 0 | opj_free(p_cstr_ind->tile_index[it_tile].packet_index); |
9434 | 0 | p_cstr_ind->tile_index[it_tile].packet_index = NULL; |
9435 | 0 | } |
9436 | | |
9437 | 18.9M | if (p_cstr_ind->tile_index[it_tile].tp_index) { |
9438 | 9.40k | opj_free(p_cstr_ind->tile_index[it_tile].tp_index); |
9439 | 9.40k | p_cstr_ind->tile_index[it_tile].tp_index = NULL; |
9440 | 9.40k | } |
9441 | | |
9442 | 18.9M | if (p_cstr_ind->tile_index[it_tile].marker) { |
9443 | 18.9M | opj_free(p_cstr_ind->tile_index[it_tile].marker); |
9444 | 18.9M | p_cstr_ind->tile_index[it_tile].marker = NULL; |
9445 | | |
9446 | 18.9M | } |
9447 | 18.9M | } |
9448 | | |
9449 | 8.50k | opj_free(p_cstr_ind->tile_index); |
9450 | 8.50k | p_cstr_ind->tile_index = NULL; |
9451 | 8.50k | } |
9452 | | |
9453 | 8.86k | opj_free(p_cstr_ind); |
9454 | 8.86k | } |
9455 | 8.86k | } |
9456 | | |
9457 | | static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp) |
9458 | 18.9M | { |
9459 | 18.9M | if (p_tcp == 00) { |
9460 | 0 | return; |
9461 | 0 | } |
9462 | | |
9463 | 18.9M | if (p_tcp->ppt_markers != 00) { |
9464 | 40 | OPJ_UINT32 i; |
9465 | 2.19k | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
9466 | 2.15k | if (p_tcp->ppt_markers[i].m_data != NULL) { |
9467 | 65 | opj_free(p_tcp->ppt_markers[i].m_data); |
9468 | 65 | } |
9469 | 2.15k | } |
9470 | 40 | p_tcp->ppt_markers_count = 0U; |
9471 | 40 | opj_free(p_tcp->ppt_markers); |
9472 | 40 | p_tcp->ppt_markers = NULL; |
9473 | 40 | } |
9474 | | |
9475 | 18.9M | if (p_tcp->ppt_buffer != 00) { |
9476 | 69 | opj_free(p_tcp->ppt_buffer); |
9477 | 69 | p_tcp->ppt_buffer = 00; |
9478 | 69 | } |
9479 | | |
9480 | 18.9M | if (p_tcp->tccps != 00) { |
9481 | 18.9M | opj_free(p_tcp->tccps); |
9482 | 18.9M | p_tcp->tccps = 00; |
9483 | 18.9M | } |
9484 | | |
9485 | 18.9M | if (p_tcp->m_mct_coding_matrix != 00) { |
9486 | 0 | opj_free(p_tcp->m_mct_coding_matrix); |
9487 | 0 | p_tcp->m_mct_coding_matrix = 00; |
9488 | 0 | } |
9489 | | |
9490 | 18.9M | if (p_tcp->m_mct_decoding_matrix != 00) { |
9491 | 352 | opj_free(p_tcp->m_mct_decoding_matrix); |
9492 | 352 | p_tcp->m_mct_decoding_matrix = 00; |
9493 | 352 | } |
9494 | | |
9495 | 18.9M | if (p_tcp->m_mcc_records) { |
9496 | 17.5M | opj_free(p_tcp->m_mcc_records); |
9497 | 17.5M | p_tcp->m_mcc_records = 00; |
9498 | 17.5M | p_tcp->m_nb_max_mcc_records = 0; |
9499 | 17.5M | p_tcp->m_nb_mcc_records = 0; |
9500 | 17.5M | } |
9501 | | |
9502 | 18.9M | if (p_tcp->m_mct_records) { |
9503 | 17.5M | opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; |
9504 | 17.5M | OPJ_UINT32 i; |
9505 | | |
9506 | 17.5M | for (i = 0; i < p_tcp->m_nb_mct_records; ++i) { |
9507 | 3.60k | if (l_mct_data->m_data) { |
9508 | 1.85k | opj_free(l_mct_data->m_data); |
9509 | 1.85k | l_mct_data->m_data = 00; |
9510 | 1.85k | } |
9511 | | |
9512 | 3.60k | ++l_mct_data; |
9513 | 3.60k | } |
9514 | | |
9515 | 17.5M | opj_free(p_tcp->m_mct_records); |
9516 | 17.5M | p_tcp->m_mct_records = 00; |
9517 | 17.5M | } |
9518 | | |
9519 | 18.9M | if (p_tcp->mct_norms != 00) { |
9520 | 0 | opj_free(p_tcp->mct_norms); |
9521 | 0 | p_tcp->mct_norms = 00; |
9522 | 0 | } |
9523 | | |
9524 | 18.9M | opj_j2k_tcp_data_destroy(p_tcp); |
9525 | | |
9526 | 18.9M | } |
9527 | | |
9528 | | static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp) |
9529 | 18.9M | { |
9530 | 18.9M | if (p_tcp->m_data) { |
9531 | 8.80k | opj_free(p_tcp->m_data); |
9532 | 8.80k | p_tcp->m_data = NULL; |
9533 | 8.80k | p_tcp->m_data_size = 0; |
9534 | 8.80k | } |
9535 | 18.9M | } |
9536 | | |
9537 | | static void opj_j2k_cp_destroy(opj_cp_t *p_cp) |
9538 | 8.86k | { |
9539 | 8.86k | OPJ_UINT32 l_nb_tiles; |
9540 | 8.86k | opj_tcp_t * l_current_tile = 00; |
9541 | | |
9542 | 8.86k | if (p_cp == 00) { |
9543 | 0 | return; |
9544 | 0 | } |
9545 | 8.86k | if (p_cp->tcps != 00) { |
9546 | 8.50k | OPJ_UINT32 i; |
9547 | 8.50k | l_current_tile = p_cp->tcps; |
9548 | 8.50k | l_nb_tiles = p_cp->th * p_cp->tw; |
9549 | | |
9550 | 18.9M | for (i = 0U; i < l_nb_tiles; ++i) { |
9551 | 18.9M | opj_j2k_tcp_destroy(l_current_tile); |
9552 | 18.9M | ++l_current_tile; |
9553 | 18.9M | } |
9554 | 8.50k | opj_free(p_cp->tcps); |
9555 | 8.50k | p_cp->tcps = 00; |
9556 | 8.50k | } |
9557 | 8.86k | if (p_cp->ppm_markers != 00) { |
9558 | 87 | OPJ_UINT32 i; |
9559 | 8.65k | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
9560 | 8.56k | if (p_cp->ppm_markers[i].m_data != NULL) { |
9561 | 315 | opj_free(p_cp->ppm_markers[i].m_data); |
9562 | 315 | } |
9563 | 8.56k | } |
9564 | 87 | p_cp->ppm_markers_count = 0U; |
9565 | 87 | opj_free(p_cp->ppm_markers); |
9566 | 87 | p_cp->ppm_markers = NULL; |
9567 | 87 | } |
9568 | 8.86k | opj_free(p_cp->ppm_buffer); |
9569 | 8.86k | p_cp->ppm_buffer = 00; |
9570 | 8.86k | p_cp->ppm_data = |
9571 | 8.86k | NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */ |
9572 | 8.86k | opj_free(p_cp->comment); |
9573 | 8.86k | p_cp->comment = 00; |
9574 | 8.86k | if (! p_cp->m_is_decoder) { |
9575 | 0 | opj_free(p_cp->m_specific_param.m_enc.m_matrice); |
9576 | 0 | p_cp->m_specific_param.m_enc.m_matrice = 00; |
9577 | 0 | } |
9578 | 8.86k | } |
9579 | | |
9580 | | static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t |
9581 | | *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, |
9582 | | opj_event_mgr_t * p_manager) |
9583 | 688 | { |
9584 | 688 | OPJ_BYTE l_header_data[10]; |
9585 | 688 | OPJ_OFF_T l_stream_pos_backup; |
9586 | 688 | OPJ_UINT32 l_current_marker; |
9587 | 688 | OPJ_UINT32 l_marker_size; |
9588 | 688 | OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts; |
9589 | | |
9590 | | /* initialize to no correction needed */ |
9591 | 688 | *p_correction_needed = OPJ_FALSE; |
9592 | | |
9593 | 688 | if (!opj_stream_has_seek(p_stream)) { |
9594 | | /* We can't do much in this case, seek is needed */ |
9595 | 0 | return OPJ_TRUE; |
9596 | 0 | } |
9597 | | |
9598 | 688 | l_stream_pos_backup = opj_stream_tell(p_stream); |
9599 | 688 | if (l_stream_pos_backup == -1) { |
9600 | | /* let's do nothing */ |
9601 | 0 | return OPJ_TRUE; |
9602 | 0 | } |
9603 | | |
9604 | 3.42k | for (;;) { |
9605 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
9606 | 3.42k | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
9607 | | /* assume all is OK */ |
9608 | 53 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9609 | 0 | return OPJ_FALSE; |
9610 | 0 | } |
9611 | 53 | return OPJ_TRUE; |
9612 | 53 | } |
9613 | | |
9614 | | /* Read 2 bytes from buffer as the new marker ID */ |
9615 | 3.37k | opj_read_bytes(l_header_data, &l_current_marker, 2); |
9616 | | |
9617 | 3.37k | if (l_current_marker != J2K_MS_SOT) { |
9618 | | /* assume all is OK */ |
9619 | 358 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9620 | 0 | return OPJ_FALSE; |
9621 | 0 | } |
9622 | 358 | return OPJ_TRUE; |
9623 | 358 | } |
9624 | | |
9625 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9626 | 3.01k | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
9627 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9628 | 2 | return OPJ_FALSE; |
9629 | 2 | } |
9630 | | |
9631 | | /* Read 2 bytes from the buffer as the marker size */ |
9632 | 3.01k | opj_read_bytes(l_header_data, &l_marker_size, 2); |
9633 | | |
9634 | | /* Check marker size for SOT Marker */ |
9635 | 3.01k | if (l_marker_size != 10) { |
9636 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); |
9637 | 1 | return OPJ_FALSE; |
9638 | 1 | } |
9639 | 3.01k | l_marker_size -= 2; |
9640 | | |
9641 | 3.01k | if (opj_stream_read_data(p_stream, l_header_data, l_marker_size, |
9642 | 3.01k | p_manager) != l_marker_size) { |
9643 | 1 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9644 | 1 | return OPJ_FALSE; |
9645 | 1 | } |
9646 | | |
9647 | 3.01k | if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no, |
9648 | 3.01k | &l_tot_len, &l_current_part, &l_num_parts, p_manager)) { |
9649 | 0 | return OPJ_FALSE; |
9650 | 0 | } |
9651 | | |
9652 | 3.01k | if (l_tile_no == tile_no) { |
9653 | | /* we found what we were looking for */ |
9654 | 30 | break; |
9655 | 30 | } |
9656 | | |
9657 | 2.98k | if (l_tot_len < 14U) { |
9658 | | /* last SOT until EOC or invalid Psot value */ |
9659 | | /* assume all is OK */ |
9660 | 15 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9661 | 0 | return OPJ_FALSE; |
9662 | 0 | } |
9663 | 15 | return OPJ_TRUE; |
9664 | 15 | } |
9665 | 2.96k | l_tot_len -= 12U; |
9666 | | /* look for next SOT marker */ |
9667 | 2.96k | if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len), |
9668 | 2.96k | p_manager) != (OPJ_OFF_T)(l_tot_len)) { |
9669 | | /* assume all is OK */ |
9670 | 228 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9671 | 0 | return OPJ_FALSE; |
9672 | 0 | } |
9673 | 228 | return OPJ_TRUE; |
9674 | 228 | } |
9675 | 2.96k | } |
9676 | | |
9677 | | /* check for correction */ |
9678 | 30 | if (l_current_part == l_num_parts) { |
9679 | 23 | *p_correction_needed = OPJ_TRUE; |
9680 | 23 | } |
9681 | | |
9682 | 30 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
9683 | 0 | return OPJ_FALSE; |
9684 | 0 | } |
9685 | 30 | return OPJ_TRUE; |
9686 | 30 | } |
9687 | | |
9688 | | OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, |
9689 | | OPJ_UINT32 * p_tile_index, |
9690 | | OPJ_UINT32 * p_data_size, |
9691 | | OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, |
9692 | | OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, |
9693 | | OPJ_UINT32 * p_nb_comps, |
9694 | | OPJ_BOOL * p_go_on, |
9695 | | opj_stream_private_t *p_stream, |
9696 | | opj_event_mgr_t * p_manager) |
9697 | 9.30k | { |
9698 | 9.30k | OPJ_UINT32 l_current_marker = J2K_MS_SOT; |
9699 | 9.30k | OPJ_UINT32 l_marker_size; |
9700 | 9.30k | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
9701 | 9.30k | opj_tcp_t * l_tcp = NULL; |
9702 | 9.30k | const OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
9703 | | |
9704 | | /* preconditions */ |
9705 | 9.30k | assert(p_stream != 00); |
9706 | 9.30k | assert(p_j2k != 00); |
9707 | 9.30k | assert(p_manager != 00); |
9708 | | |
9709 | | /* Reach the End Of Codestream ?*/ |
9710 | 9.30k | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
9711 | 18 | l_current_marker = J2K_MS_EOC; |
9712 | 18 | } |
9713 | | /* We need to encounter a SOT marker (a new tile-part header) */ |
9714 | 9.28k | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
9715 | 0 | return OPJ_FALSE; |
9716 | 0 | } |
9717 | | |
9718 | | /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ |
9719 | 19.9k | while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) && |
9720 | 19.9k | (l_current_marker != J2K_MS_EOC)) { |
9721 | | |
9722 | 17.7k | if (p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts > 0 && |
9723 | 17.7k | p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts < |
9724 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts) { |
9725 | 0 | OPJ_OFF_T next_tp_sot_pos; |
9726 | |
|
9727 | 0 | next_tp_sot_pos = |
9728 | 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]; |
9729 | 0 | ++p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts; |
9730 | 0 | if (!(opj_stream_read_seek(p_stream, |
9731 | 0 | next_tp_sot_pos, |
9732 | 0 | p_manager))) { |
9733 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
9734 | 0 | return OPJ_FALSE; |
9735 | 0 | } |
9736 | | |
9737 | | /* Try to read 2 bytes (the marker ID) from stream and copy them into the buffer */ |
9738 | 0 | if (opj_stream_read_data(p_stream, |
9739 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9740 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9741 | 0 | return OPJ_FALSE; |
9742 | 0 | } |
9743 | | |
9744 | | /* Read 2 bytes from the buffer as the marker ID */ |
9745 | 0 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9746 | 0 | &l_current_marker, |
9747 | 0 | 2); |
9748 | |
|
9749 | 0 | if (l_current_marker != J2K_MS_SOT) { |
9750 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Did not get expected SOT marker\n"); |
9751 | 0 | return OPJ_FALSE; |
9752 | 0 | } |
9753 | 0 | } |
9754 | | |
9755 | | /* Try to read until the Start Of Data is detected */ |
9756 | 27.9k | while (l_current_marker != J2K_MS_SOD) { |
9757 | | |
9758 | 17.1k | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
9759 | 6.40k | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
9760 | 6.40k | break; |
9761 | 6.40k | } |
9762 | | |
9763 | | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
9764 | 10.6k | if (opj_stream_read_data(p_stream, |
9765 | 10.6k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9766 | 14 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9767 | 14 | return OPJ_FALSE; |
9768 | 14 | } |
9769 | | |
9770 | | /* Read 2 bytes from the buffer as the marker size */ |
9771 | 10.6k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
9772 | 10.6k | 2); |
9773 | | |
9774 | | /* Check marker size (does not include marker ID but includes marker size) */ |
9775 | 10.6k | if (l_marker_size < 2) { |
9776 | 17 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); |
9777 | 17 | return OPJ_FALSE; |
9778 | 17 | } |
9779 | | |
9780 | | /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */ |
9781 | 10.6k | if (l_current_marker == 0x8080 && |
9782 | 10.6k | opj_stream_get_number_byte_left(p_stream) == 0) { |
9783 | 1 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
9784 | 1 | break; |
9785 | 1 | } |
9786 | | |
9787 | | /* Why this condition? FIXME */ |
9788 | 10.6k | if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) { |
9789 | 1.00k | if (p_j2k->m_specific_param.m_decoder.m_sot_length < l_marker_size + 2) { |
9790 | 56 | opj_event_msg(p_manager, EVT_ERROR, |
9791 | 56 | "Sot length is less than marker size + marker ID\n"); |
9792 | 56 | return OPJ_FALSE; |
9793 | 56 | } |
9794 | 947 | p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); |
9795 | 947 | } |
9796 | 10.6k | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
9797 | | |
9798 | | /* Get the marker handler from the marker ID */ |
9799 | 10.6k | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
9800 | | |
9801 | | /* Check if the marker is known and if it is the right place to find it */ |
9802 | 10.6k | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
9803 | 28 | opj_event_msg(p_manager, EVT_ERROR, |
9804 | 28 | "Marker is not compliant with its position\n"); |
9805 | 28 | return OPJ_FALSE; |
9806 | 28 | } |
9807 | | /* FIXME manage case of unknown marker as in the main header ? */ |
9808 | | |
9809 | | /* Check if the marker size is compatible with the header data size */ |
9810 | 10.5k | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
9811 | 50 | OPJ_BYTE *new_header_data = NULL; |
9812 | | /* If we are here, this means we consider this marker as known & we will read it */ |
9813 | | /* Check enough bytes left in stream before allocation */ |
9814 | 50 | if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) { |
9815 | 46 | opj_event_msg(p_manager, EVT_ERROR, |
9816 | 46 | "Marker size inconsistent with stream length\n"); |
9817 | 46 | return OPJ_FALSE; |
9818 | 46 | } |
9819 | 4 | new_header_data = (OPJ_BYTE *) opj_realloc( |
9820 | 4 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
9821 | 4 | if (! new_header_data) { |
9822 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
9823 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
9824 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
9825 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
9826 | 0 | return OPJ_FALSE; |
9827 | 0 | } |
9828 | 4 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
9829 | 4 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
9830 | 4 | } |
9831 | | |
9832 | | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
9833 | 10.5k | if (opj_stream_read_data(p_stream, |
9834 | 10.5k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
9835 | 10.5k | p_manager) != l_marker_size) { |
9836 | 60 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9837 | 60 | return OPJ_FALSE; |
9838 | 60 | } |
9839 | | |
9840 | 10.4k | if (!l_marker_handler->handler) { |
9841 | | /* See issue #175 */ |
9842 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n"); |
9843 | 3 | return OPJ_FALSE; |
9844 | 3 | } |
9845 | | /* Read the marker segment with the correct marker handler */ |
9846 | 10.4k | if (!(*(l_marker_handler->handler))(p_j2k, |
9847 | 10.4k | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
9848 | 155 | opj_event_msg(p_manager, EVT_ERROR, |
9849 | 155 | "Fail to read the current marker segment (%#x)\n", l_current_marker); |
9850 | 155 | return OPJ_FALSE; |
9851 | 155 | } |
9852 | | |
9853 | | /* Add the marker to the codestream index*/ |
9854 | 10.3k | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
9855 | 10.3k | p_j2k->cstr_index, |
9856 | 10.3k | l_marker_handler->id, |
9857 | 10.3k | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
9858 | 10.3k | l_marker_size + 4)) { |
9859 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); |
9860 | 0 | return OPJ_FALSE; |
9861 | 0 | } |
9862 | | |
9863 | | /* Keep the position of the last SOT marker read */ |
9864 | 10.3k | if (l_marker_handler->id == J2K_MS_SOT) { |
9865 | 9.43k | OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 |
9866 | 9.43k | ; |
9867 | 9.43k | if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) { |
9868 | 9.43k | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; |
9869 | 9.43k | } |
9870 | 9.43k | } |
9871 | | |
9872 | 10.3k | if (p_j2k->m_specific_param.m_decoder.m_skip_data) { |
9873 | | /* Skip the rest of the tile part header*/ |
9874 | 326 | if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length, |
9875 | 326 | p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
9876 | 60 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9877 | 60 | return OPJ_FALSE; |
9878 | 60 | } |
9879 | 266 | l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ |
9880 | 9.98k | } else { |
9881 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
9882 | 9.98k | if (opj_stream_read_data(p_stream, |
9883 | 9.98k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9884 | 35 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9885 | 35 | return OPJ_FALSE; |
9886 | 35 | } |
9887 | | /* Read 2 bytes from the buffer as the new marker ID */ |
9888 | 9.95k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9889 | 9.95k | &l_current_marker, 2); |
9890 | 9.95k | } |
9891 | 10.3k | } |
9892 | 17.2k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
9893 | 17.2k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
9894 | 6.40k | break; |
9895 | 6.40k | } |
9896 | | |
9897 | | /* If we didn't skip data before, we need to read the SOD marker*/ |
9898 | 10.8k | if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { |
9899 | | /* Try to read the SOD marker and skip data ? FIXME */ |
9900 | 10.5k | if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { |
9901 | 115 | return OPJ_FALSE; |
9902 | 115 | } |
9903 | | |
9904 | | /* Check if we can use the TLM index to access the next tile-part */ |
9905 | 10.4k | if (!p_j2k->m_specific_param.m_decoder.m_can_decode && |
9906 | 10.4k | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 && |
9907 | 10.4k | p_j2k->m_current_tile_number == (OPJ_UINT32) |
9908 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec && |
9909 | 10.4k | !p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid && |
9910 | 10.4k | opj_stream_has_seek(p_stream)) { |
9911 | 0 | l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; |
9912 | 0 | if (l_tcp->m_nb_tile_parts == |
9913 | 0 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps && |
9914 | 0 | (OPJ_UINT32)l_tcp->m_current_tile_part_number + 1 < l_tcp->m_nb_tile_parts) { |
9915 | 0 | const OPJ_OFF_T next_tp_sot_pos = p_j2k->cstr_index->tile_index[ |
9916 | 0 | p_j2k->m_current_tile_number].tp_index[l_tcp->m_current_tile_part_number + |
9917 | 0 | 1].start_pos; |
9918 | |
|
9919 | 0 | if (next_tp_sot_pos != opj_stream_tell(p_stream)) { |
9920 | | #if 0 |
9921 | | opj_event_msg(p_manager, EVT_INFO, |
9922 | | "opj_j2k_read_tile_header(tile=%u): seek to tile part %u at %" PRId64 "\n", |
9923 | | p_j2k->m_current_tile_number, |
9924 | | l_tcp->m_current_tile_part_number + 1, |
9925 | | next_tp_sot_pos); |
9926 | | #endif |
9927 | |
|
9928 | 0 | if (!(opj_stream_read_seek(p_stream, |
9929 | 0 | next_tp_sot_pos, |
9930 | 0 | p_manager))) { |
9931 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
9932 | 0 | return OPJ_FALSE; |
9933 | 0 | } |
9934 | 0 | } |
9935 | | |
9936 | | /* Try to read 2 bytes (the marker ID) from stream and copy them into the buffer */ |
9937 | 0 | if (opj_stream_read_data(p_stream, |
9938 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9939 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
9940 | 0 | return OPJ_FALSE; |
9941 | 0 | } |
9942 | | |
9943 | | /* Read 2 bytes from the buffer as the marker ID */ |
9944 | 0 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
9945 | 0 | &l_current_marker, |
9946 | 0 | 2); |
9947 | |
|
9948 | 0 | if (l_current_marker != J2K_MS_SOT) { |
9949 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Did not get expected SOT marker\n"); |
9950 | 0 | return OPJ_FALSE; |
9951 | 0 | } |
9952 | | |
9953 | 0 | continue; |
9954 | 0 | } |
9955 | 0 | } |
9956 | | |
9957 | 10.4k | if (p_j2k->m_specific_param.m_decoder.m_can_decode && |
9958 | 10.4k | !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) { |
9959 | | /* Issue 254 */ |
9960 | 688 | OPJ_BOOL l_correction_needed; |
9961 | | |
9962 | 688 | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
9963 | 688 | if (!opj_j2k_need_nb_tile_parts_correction(p_stream, |
9964 | 688 | p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { |
9965 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
9966 | 4 | "opj_j2k_apply_nb_tile_parts_correction error\n"); |
9967 | 4 | return OPJ_FALSE; |
9968 | 4 | } |
9969 | 684 | if (l_correction_needed) { |
9970 | 23 | OPJ_UINT32 l_tile_no; |
9971 | | |
9972 | 23 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
9973 | 23 | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1; |
9974 | | /* correct tiles */ |
9975 | 2.91k | for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { |
9976 | 2.88k | if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) { |
9977 | 36 | p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1; |
9978 | 36 | } |
9979 | 2.88k | } |
9980 | 23 | opj_event_msg(p_manager, EVT_WARNING, |
9981 | 23 | "Non conformant codestream TPsot==TNsot.\n"); |
9982 | 23 | } |
9983 | 684 | } |
9984 | 10.4k | } else { |
9985 | | /* Indicate we will try to read a new tile-part header*/ |
9986 | 266 | p_j2k->m_specific_param.m_decoder.m_skip_data = 0; |
9987 | 266 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
9988 | 266 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
9989 | 266 | } |
9990 | | |
9991 | 10.7k | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
9992 | | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
9993 | 8.45k | if (opj_stream_read_data(p_stream, |
9994 | 8.45k | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
9995 | | |
9996 | | /* Deal with likely non conformant SPOT6 files, where the last */ |
9997 | | /* row of tiles have TPsot == 0 and TNsot == 0, and missing EOC, */ |
9998 | | /* but no other tile-parts were found. */ |
9999 | 26 | if (p_j2k->m_current_tile_number + 1 == l_nb_tiles) { |
10000 | 22 | OPJ_UINT32 l_tile_no; |
10001 | 418 | for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { |
10002 | 398 | if (p_j2k->m_cp.tcps[l_tile_no].m_current_tile_part_number == 0 && |
10003 | 398 | p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts == 0) { |
10004 | 2 | break; |
10005 | 2 | } |
10006 | 398 | } |
10007 | 22 | if (l_tile_no < l_nb_tiles) { |
10008 | 2 | opj_event_msg(p_manager, EVT_INFO, |
10009 | 2 | "Tile %u has TPsot == 0 and TNsot == 0, " |
10010 | 2 | "but no other tile-parts were found. " |
10011 | 2 | "EOC is also missing.\n", |
10012 | 2 | l_tile_no); |
10013 | 2 | p_j2k->m_current_tile_number = l_tile_no; |
10014 | 2 | l_current_marker = J2K_MS_EOC; |
10015 | 2 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
10016 | 2 | break; |
10017 | 2 | } |
10018 | 22 | } |
10019 | | |
10020 | 24 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
10021 | 24 | return OPJ_FALSE; |
10022 | 26 | } |
10023 | | |
10024 | | /* Read 2 bytes from buffer as the new marker ID */ |
10025 | 8.43k | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
10026 | 8.43k | &l_current_marker, 2); |
10027 | 8.43k | } |
10028 | 10.7k | } |
10029 | | |
10030 | | /* Current marker is the EOC marker ?*/ |
10031 | 8.68k | if (l_current_marker == J2K_MS_EOC) { |
10032 | 28 | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { |
10033 | 8 | p_j2k->m_current_tile_number = 0; |
10034 | 8 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
10035 | 8 | } |
10036 | 28 | } |
10037 | | |
10038 | | /* Deal with tiles that have a single tile-part with TPsot == 0 and TNsot == 0 */ |
10039 | 8.68k | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
10040 | 6.42k | l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; |
10041 | | |
10042 | 272k | while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) { |
10043 | 265k | ++p_j2k->m_current_tile_number; |
10044 | 265k | ++l_tcp; |
10045 | 265k | } |
10046 | | |
10047 | 6.42k | if (p_j2k->m_current_tile_number == l_nb_tiles) { |
10048 | 61 | *p_go_on = OPJ_FALSE; |
10049 | 61 | return OPJ_TRUE; |
10050 | 61 | } |
10051 | 6.42k | } |
10052 | | |
10053 | 8.62k | if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number, |
10054 | 8.62k | p_manager)) { |
10055 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n"); |
10056 | 0 | return OPJ_FALSE; |
10057 | 0 | } |
10058 | | /*FIXME ???*/ |
10059 | 8.62k | if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
10060 | 8.62k | p_manager)) { |
10061 | 35 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
10062 | 35 | return OPJ_FALSE; |
10063 | 35 | } |
10064 | | |
10065 | 8.58k | opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", |
10066 | 8.58k | p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw)); |
10067 | | |
10068 | 8.58k | *p_tile_index = p_j2k->m_current_tile_number; |
10069 | 8.58k | *p_go_on = OPJ_TRUE; |
10070 | 8.58k | if (p_data_size) { |
10071 | | /* For internal use in j2k.c, we don't need this */ |
10072 | | /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */ |
10073 | 0 | *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE); |
10074 | 0 | if (*p_data_size == UINT_MAX) { |
10075 | 0 | return OPJ_FALSE; |
10076 | 0 | } |
10077 | 0 | } |
10078 | 8.58k | *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; |
10079 | 8.58k | *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; |
10080 | 8.58k | *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; |
10081 | 8.58k | *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; |
10082 | 8.58k | *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; |
10083 | | |
10084 | 8.58k | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
10085 | | |
10086 | 8.58k | return OPJ_TRUE; |
10087 | 8.58k | } |
10088 | | |
10089 | | OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, |
10090 | | OPJ_UINT32 p_tile_index, |
10091 | | OPJ_BYTE * p_data, |
10092 | | OPJ_UINT32 p_data_size, |
10093 | | opj_stream_private_t *p_stream, |
10094 | | opj_event_mgr_t * p_manager) |
10095 | 8.58k | { |
10096 | 8.58k | OPJ_UINT32 l_current_marker; |
10097 | 8.58k | OPJ_BYTE l_data [2]; |
10098 | 8.58k | opj_tcp_t * l_tcp; |
10099 | 8.58k | opj_image_t* l_image_for_bounds; |
10100 | | |
10101 | | /* preconditions */ |
10102 | 8.58k | assert(p_stream != 00); |
10103 | 8.58k | assert(p_j2k != 00); |
10104 | 8.58k | assert(p_manager != 00); |
10105 | | |
10106 | 8.58k | if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA) |
10107 | 8.58k | || (p_tile_index != p_j2k->m_current_tile_number)) { |
10108 | 0 | return OPJ_FALSE; |
10109 | 0 | } |
10110 | | |
10111 | 8.58k | l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); |
10112 | 8.58k | if (! l_tcp->m_data) { |
10113 | 10 | opj_j2k_tcp_destroy(l_tcp); |
10114 | 10 | return OPJ_FALSE; |
10115 | 10 | } |
10116 | | |
10117 | | /* When using the opj_read_tile_header / opj_decode_tile_data API */ |
10118 | | /* such as in test_tile_decoder, m_output_image is NULL, so fall back */ |
10119 | | /* to the full image dimension. This is a bit surprising that */ |
10120 | | /* opj_set_decode_area() is only used to determine intersecting tiles, */ |
10121 | | /* but full tile decoding is done */ |
10122 | 8.57k | l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image : |
10123 | 8.57k | p_j2k->m_private_image; |
10124 | 8.57k | if (! opj_tcd_decode_tile(p_j2k->m_tcd, |
10125 | 8.57k | l_image_for_bounds->x0, |
10126 | 8.57k | l_image_for_bounds->y0, |
10127 | 8.57k | l_image_for_bounds->x1, |
10128 | 8.57k | l_image_for_bounds->y1, |
10129 | 8.57k | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode, |
10130 | 8.57k | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
10131 | 8.57k | l_tcp->m_data, |
10132 | 8.57k | l_tcp->m_data_size, |
10133 | 8.57k | p_tile_index, |
10134 | 8.57k | p_j2k->cstr_index, p_manager)) { |
10135 | 895 | opj_j2k_tcp_destroy(l_tcp); |
10136 | 895 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
10137 | 895 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n"); |
10138 | 895 | return OPJ_FALSE; |
10139 | 895 | } |
10140 | | |
10141 | | /* p_data can be set to NULL when the call will take care of using */ |
10142 | | /* itself the TCD data. This is typically the case for whole single */ |
10143 | | /* tile decoding optimization. */ |
10144 | 7.68k | if (p_data != NULL) { |
10145 | 0 | if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
10146 | 0 | return OPJ_FALSE; |
10147 | 0 | } |
10148 | | |
10149 | | /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) |
10150 | | * we destroy just the data which will be re-read in read_tile_header*/ |
10151 | | /*opj_j2k_tcp_destroy(l_tcp); |
10152 | | p_j2k->m_tcd->tcp = 0;*/ |
10153 | 0 | opj_j2k_tcp_data_destroy(l_tcp); |
10154 | 0 | } |
10155 | | |
10156 | 7.68k | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
10157 | 7.68k | p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA); |
10158 | | |
10159 | 7.68k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
10160 | 7.68k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
10161 | 5.55k | return OPJ_TRUE; |
10162 | 5.55k | } |
10163 | | |
10164 | 2.12k | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { |
10165 | 2.11k | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
10166 | 33 | opj_event_msg(p_manager, p_j2k->m_cp.strict ? EVT_ERROR : EVT_WARNING, |
10167 | 33 | "Stream too short\n"); |
10168 | 33 | return p_j2k->m_cp.strict ? OPJ_FALSE : OPJ_TRUE; |
10169 | 33 | } |
10170 | 2.07k | opj_read_bytes(l_data, &l_current_marker, 2); |
10171 | | |
10172 | 2.07k | if (l_current_marker == J2K_MS_EOC) { |
10173 | 2 | p_j2k->m_current_tile_number = 0; |
10174 | 2 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
10175 | 2.07k | } else if (l_current_marker != J2K_MS_SOT) { |
10176 | 218 | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
10177 | 40 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
10178 | 40 | opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n"); |
10179 | 40 | return OPJ_TRUE; |
10180 | 40 | } |
10181 | 178 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); |
10182 | 178 | return OPJ_FALSE; |
10183 | 218 | } |
10184 | 2.07k | } |
10185 | | |
10186 | 1.87k | return OPJ_TRUE; |
10187 | 2.12k | } |
10188 | | |
10189 | | static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, |
10190 | | opj_image_t* p_output_image) |
10191 | 7.46k | { |
10192 | 7.46k | OPJ_UINT32 i, j; |
10193 | 7.46k | OPJ_UINT32 l_width_src, l_height_src; |
10194 | 7.46k | OPJ_UINT32 l_width_dest, l_height_dest; |
10195 | 7.46k | OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src; |
10196 | 7.46k | OPJ_SIZE_T l_start_offset_src; |
10197 | 7.46k | OPJ_UINT32 l_start_x_dest, l_start_y_dest; |
10198 | 7.46k | OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest; |
10199 | 7.46k | OPJ_SIZE_T l_start_offset_dest; |
10200 | | |
10201 | 7.46k | opj_image_comp_t * l_img_comp_src = 00; |
10202 | 7.46k | opj_image_comp_t * l_img_comp_dest = 00; |
10203 | | |
10204 | 7.46k | opj_tcd_tilecomp_t * l_tilec = 00; |
10205 | 7.46k | opj_image_t * l_image_src = 00; |
10206 | 7.46k | OPJ_INT32 * l_dest_ptr; |
10207 | | |
10208 | 7.46k | l_tilec = p_tcd->tcd_image->tiles->comps; |
10209 | 7.46k | l_image_src = p_tcd->image; |
10210 | 7.46k | l_img_comp_src = l_image_src->comps; |
10211 | | |
10212 | 7.46k | l_img_comp_dest = p_output_image->comps; |
10213 | | |
10214 | 30.5k | for (i = 0; i < l_image_src->numcomps; |
10215 | 23.1k | i++, ++l_img_comp_dest, ++l_img_comp_src, ++l_tilec) { |
10216 | 23.1k | OPJ_INT32 res_x0, res_x1, res_y0, res_y1; |
10217 | 23.1k | OPJ_UINT32 src_data_stride; |
10218 | 23.1k | const OPJ_INT32* p_src_data; |
10219 | | |
10220 | | /* Copy info from decoded comp image to output image */ |
10221 | 23.1k | l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; |
10222 | | |
10223 | 23.1k | if (p_tcd->whole_tile_decoding) { |
10224 | 14.7k | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
10225 | 14.7k | l_img_comp_src->resno_decoded; |
10226 | 14.7k | res_x0 = l_res->x0; |
10227 | 14.7k | res_y0 = l_res->y0; |
10228 | 14.7k | res_x1 = l_res->x1; |
10229 | 14.7k | res_y1 = l_res->y1; |
10230 | 14.7k | src_data_stride = (OPJ_UINT32)( |
10231 | 14.7k | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 - |
10232 | 14.7k | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0); |
10233 | 14.7k | p_src_data = l_tilec->data; |
10234 | 14.7k | } else { |
10235 | 8.39k | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
10236 | 8.39k | l_img_comp_src->resno_decoded; |
10237 | 8.39k | res_x0 = (OPJ_INT32)l_res->win_x0; |
10238 | 8.39k | res_y0 = (OPJ_INT32)l_res->win_y0; |
10239 | 8.39k | res_x1 = (OPJ_INT32)l_res->win_x1; |
10240 | 8.39k | res_y1 = (OPJ_INT32)l_res->win_y1; |
10241 | 8.39k | src_data_stride = l_res->win_x1 - l_res->win_x0; |
10242 | 8.39k | p_src_data = l_tilec->data_win; |
10243 | 8.39k | } |
10244 | | |
10245 | 23.1k | if (p_src_data == NULL) { |
10246 | | /* Happens for partial component decoding */ |
10247 | 4.25k | continue; |
10248 | 4.25k | } |
10249 | | |
10250 | 18.8k | l_width_src = (OPJ_UINT32)(res_x1 - res_x0); |
10251 | 18.8k | l_height_src = (OPJ_UINT32)(res_y1 - res_y0); |
10252 | | |
10253 | | |
10254 | | /* Current tile component size*/ |
10255 | | /*if (i == 0) { |
10256 | | fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", |
10257 | | res_x0, res_x1, res_y0, res_y1); |
10258 | | }*/ |
10259 | | |
10260 | | |
10261 | | /* Border of the current output component*/ |
10262 | 18.8k | l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor); |
10263 | 18.8k | l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor); |
10264 | 18.8k | l_x1_dest = l_x0_dest + |
10265 | 18.8k | l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */ |
10266 | 18.8k | l_y1_dest = l_y0_dest + l_img_comp_dest->h; |
10267 | | |
10268 | | /*if (i == 0) { |
10269 | | fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", |
10270 | | l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); |
10271 | | }*/ |
10272 | | |
10273 | | /*-----*/ |
10274 | | /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) |
10275 | | * of the input buffer (decoded tile component) which will be move |
10276 | | * in the output buffer. Compute the area of the output buffer (l_start_x_dest, |
10277 | | * l_start_y_dest, l_width_dest, l_height_dest) which will be modified |
10278 | | * by this input area. |
10279 | | * */ |
10280 | 18.8k | assert(res_x0 >= 0); |
10281 | 18.8k | assert(res_x1 >= 0); |
10282 | 18.8k | if (l_x0_dest < (OPJ_UINT32)res_x0) { |
10283 | 3.38k | l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest; |
10284 | 3.38k | l_offset_x0_src = 0; |
10285 | | |
10286 | 3.38k | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
10287 | 3.38k | l_width_dest = l_width_src; |
10288 | 3.38k | l_offset_x1_src = 0; |
10289 | 3.38k | } else { |
10290 | 0 | l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ; |
10291 | 0 | l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest); |
10292 | 0 | } |
10293 | 15.4k | } else { |
10294 | 15.4k | l_start_x_dest = 0U; |
10295 | 15.4k | l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0; |
10296 | | |
10297 | 15.4k | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
10298 | 15.4k | l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src; |
10299 | 15.4k | l_offset_x1_src = 0; |
10300 | 15.4k | } else { |
10301 | 0 | l_width_dest = l_img_comp_dest->w ; |
10302 | 0 | l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest; |
10303 | 0 | } |
10304 | 15.4k | } |
10305 | | |
10306 | 18.8k | if (l_y0_dest < (OPJ_UINT32)res_y0) { |
10307 | 3.50k | l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest; |
10308 | 3.50k | l_offset_y0_src = 0; |
10309 | | |
10310 | 3.50k | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
10311 | 3.50k | l_height_dest = l_height_src; |
10312 | 3.50k | l_offset_y1_src = 0; |
10313 | 3.50k | } else { |
10314 | 0 | l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ; |
10315 | 0 | l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest); |
10316 | 0 | } |
10317 | 15.3k | } else { |
10318 | 15.3k | l_start_y_dest = 0U; |
10319 | 15.3k | l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0; |
10320 | | |
10321 | 15.3k | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
10322 | 15.3k | l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src; |
10323 | 15.3k | l_offset_y1_src = 0; |
10324 | 15.3k | } else { |
10325 | 0 | l_height_dest = l_img_comp_dest->h ; |
10326 | 0 | l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest; |
10327 | 0 | } |
10328 | 15.3k | } |
10329 | | |
10330 | 18.8k | if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) || |
10331 | 18.8k | (l_offset_y1_src < 0)) { |
10332 | 0 | return OPJ_FALSE; |
10333 | 0 | } |
10334 | | /* testcase 2977.pdf.asan.67.2198 */ |
10335 | 18.8k | if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) { |
10336 | 29 | return OPJ_FALSE; |
10337 | 29 | } |
10338 | | /*-----*/ |
10339 | | |
10340 | | /* Compute the input buffer offset */ |
10341 | 18.8k | l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src |
10342 | 18.8k | * (OPJ_SIZE_T)src_data_stride; |
10343 | | |
10344 | | /* Compute the output buffer offset */ |
10345 | 18.8k | l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest |
10346 | 18.8k | * (OPJ_SIZE_T)l_img_comp_dest->w; |
10347 | | |
10348 | | /* Allocate output component buffer if necessary */ |
10349 | 18.8k | if (l_img_comp_dest->data == NULL && |
10350 | 18.8k | l_start_offset_src == 0 && l_start_offset_dest == 0 && |
10351 | 18.8k | src_data_stride == l_img_comp_dest->w && |
10352 | 18.8k | l_width_dest == l_img_comp_dest->w && |
10353 | 18.8k | l_height_dest == l_img_comp_dest->h) { |
10354 | | /* If the final image matches the tile buffer, then borrow it */ |
10355 | | /* directly to save a copy */ |
10356 | 6.43k | if (p_tcd->whole_tile_decoding) { |
10357 | 2.81k | l_img_comp_dest->data = l_tilec->data; |
10358 | 2.81k | l_tilec->data = NULL; |
10359 | 3.62k | } else { |
10360 | 3.62k | l_img_comp_dest->data = l_tilec->data_win; |
10361 | 3.62k | l_tilec->data_win = NULL; |
10362 | 3.62k | } |
10363 | 6.43k | continue; |
10364 | 12.3k | } else if (l_img_comp_dest->data == NULL) { |
10365 | 7.49k | OPJ_SIZE_T l_width = l_img_comp_dest->w; |
10366 | 7.49k | OPJ_SIZE_T l_height = l_img_comp_dest->h; |
10367 | | |
10368 | 7.49k | if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) || |
10369 | 7.49k | l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) { |
10370 | | /* would overflow */ |
10371 | 0 | return OPJ_FALSE; |
10372 | 0 | } |
10373 | 7.49k | l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height * |
10374 | 7.49k | sizeof(OPJ_INT32)); |
10375 | 7.49k | if (! l_img_comp_dest->data) { |
10376 | 0 | return OPJ_FALSE; |
10377 | 0 | } |
10378 | | |
10379 | 7.49k | if (l_img_comp_dest->w != l_width_dest || |
10380 | 7.49k | l_img_comp_dest->h != l_height_dest) { |
10381 | 7.49k | memset(l_img_comp_dest->data, 0, |
10382 | 7.49k | (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32)); |
10383 | 7.49k | } |
10384 | 7.49k | } |
10385 | | |
10386 | | /* Move the output buffer to the first place where we will write*/ |
10387 | 12.3k | l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; |
10388 | | |
10389 | 12.3k | { |
10390 | 12.3k | const OPJ_INT32 * l_src_ptr = p_src_data; |
10391 | 12.3k | l_src_ptr += l_start_offset_src; |
10392 | | |
10393 | 1.93M | for (j = 0; j < l_height_dest; ++j) { |
10394 | 1.92M | memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32)); |
10395 | 1.92M | l_dest_ptr += l_img_comp_dest->w; |
10396 | 1.92M | l_src_ptr += src_data_stride; |
10397 | 1.92M | } |
10398 | 12.3k | } |
10399 | | |
10400 | | |
10401 | 12.3k | } |
10402 | | |
10403 | 7.43k | return OPJ_TRUE; |
10404 | 7.46k | } |
10405 | | |
10406 | | static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image, |
10407 | | opj_event_mgr_t * p_manager) |
10408 | 7.44k | { |
10409 | 7.44k | OPJ_UINT32 it_comp; |
10410 | 7.44k | OPJ_INT32 l_comp_x1, l_comp_y1; |
10411 | 7.44k | opj_image_comp_t* l_img_comp = NULL; |
10412 | | |
10413 | 7.44k | l_img_comp = p_image->comps; |
10414 | 30.2k | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
10415 | 22.7k | OPJ_INT32 l_h, l_w; |
10416 | 22.7k | if (p_image->x0 > (OPJ_UINT32)INT_MAX || |
10417 | 22.7k | p_image->y0 > (OPJ_UINT32)INT_MAX || |
10418 | 22.7k | p_image->x1 > (OPJ_UINT32)INT_MAX || |
10419 | 22.7k | p_image->y1 > (OPJ_UINT32)INT_MAX) { |
10420 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10421 | 0 | "Image coordinates above INT_MAX are not supported\n"); |
10422 | 0 | return OPJ_FALSE; |
10423 | 0 | } |
10424 | | |
10425 | 22.7k | l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx); |
10426 | 22.7k | l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy); |
10427 | 22.7k | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
10428 | 22.7k | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
10429 | | |
10430 | 22.7k | l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) |
10431 | 22.7k | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor); |
10432 | 22.7k | if (l_w < 0) { |
10433 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10434 | 0 | "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", |
10435 | 0 | it_comp, l_w); |
10436 | 0 | return OPJ_FALSE; |
10437 | 0 | } |
10438 | 22.7k | l_img_comp->w = (OPJ_UINT32)l_w; |
10439 | | |
10440 | 22.7k | l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) |
10441 | 22.7k | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor); |
10442 | 22.7k | if (l_h < 0) { |
10443 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10444 | 0 | "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", |
10445 | 0 | it_comp, l_h); |
10446 | 0 | return OPJ_FALSE; |
10447 | 0 | } |
10448 | 22.7k | l_img_comp->h = (OPJ_UINT32)l_h; |
10449 | | |
10450 | 22.7k | l_img_comp++; |
10451 | 22.7k | } |
10452 | | |
10453 | 7.44k | return OPJ_TRUE; |
10454 | 7.44k | } |
10455 | | |
10456 | | OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k, |
10457 | | OPJ_UINT32 numcomps, |
10458 | | const OPJ_UINT32* comps_indices, |
10459 | | opj_event_mgr_t * p_manager) |
10460 | 0 | { |
10461 | 0 | OPJ_UINT32 i; |
10462 | 0 | OPJ_BOOL* already_mapped; |
10463 | |
|
10464 | 0 | if (p_j2k->m_private_image == NULL) { |
10465 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10466 | 0 | "opj_read_header() should be called before " |
10467 | 0 | "opj_set_decoded_components().\n"); |
10468 | 0 | return OPJ_FALSE; |
10469 | 0 | } |
10470 | | |
10471 | 0 | already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL), |
10472 | 0 | p_j2k->m_private_image->numcomps); |
10473 | 0 | if (already_mapped == NULL) { |
10474 | 0 | return OPJ_FALSE; |
10475 | 0 | } |
10476 | | |
10477 | 0 | for (i = 0; i < numcomps; i++) { |
10478 | 0 | if (comps_indices[i] >= p_j2k->m_private_image->numcomps) { |
10479 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10480 | 0 | "Invalid component index: %u\n", |
10481 | 0 | comps_indices[i]); |
10482 | 0 | opj_free(already_mapped); |
10483 | 0 | return OPJ_FALSE; |
10484 | 0 | } |
10485 | 0 | if (already_mapped[comps_indices[i]]) { |
10486 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10487 | 0 | "Component index %u used several times\n", |
10488 | 0 | comps_indices[i]); |
10489 | 0 | opj_free(already_mapped); |
10490 | 0 | return OPJ_FALSE; |
10491 | 0 | } |
10492 | 0 | already_mapped[comps_indices[i]] = OPJ_TRUE; |
10493 | 0 | } |
10494 | 0 | opj_free(already_mapped); |
10495 | |
|
10496 | 0 | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
10497 | 0 | if (numcomps) { |
10498 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = |
10499 | 0 | (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32)); |
10500 | 0 | if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) { |
10501 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
10502 | 0 | return OPJ_FALSE; |
10503 | 0 | } |
10504 | 0 | memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
10505 | 0 | comps_indices, |
10506 | 0 | numcomps * sizeof(OPJ_UINT32)); |
10507 | 0 | } else { |
10508 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL; |
10509 | 0 | } |
10510 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps; |
10511 | |
|
10512 | 0 | return OPJ_TRUE; |
10513 | 0 | } |
10514 | | |
10515 | | |
10516 | | OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k, |
10517 | | opj_image_t* p_image, |
10518 | | OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, |
10519 | | OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, |
10520 | | opj_event_mgr_t * p_manager) |
10521 | 7.48k | { |
10522 | 7.48k | opj_cp_t * l_cp = &(p_j2k->m_cp); |
10523 | 7.48k | opj_image_t * l_image = p_j2k->m_private_image; |
10524 | 7.48k | OPJ_BOOL ret; |
10525 | 7.48k | OPJ_UINT32 it_comp; |
10526 | | |
10527 | 7.48k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
10528 | 7.48k | p_j2k->m_cp.tcps[0].m_data != NULL) { |
10529 | | /* In the case of a single-tiled image whose codestream we have already */ |
10530 | | /* ingested, go on */ |
10531 | 0 | } |
10532 | | /* Check if we are read the main header */ |
10533 | 7.48k | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
10534 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10535 | 0 | "Need to decode the main header before begin to decode the remaining codestream.\n"); |
10536 | 0 | return OPJ_FALSE; |
10537 | 0 | } |
10538 | | |
10539 | | /* Update the comps[].factor member of the output image with the one */ |
10540 | | /* of m_reduce */ |
10541 | 30.3k | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
10542 | 22.8k | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
10543 | 22.8k | } |
10544 | | |
10545 | 7.48k | if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) { |
10546 | 0 | opj_event_msg(p_manager, EVT_INFO, |
10547 | 0 | "No decoded area parameters, set the decoded area to the whole image\n"); |
10548 | |
|
10549 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
10550 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
10551 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
10552 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
10553 | |
|
10554 | 0 | p_image->x0 = l_image->x0; |
10555 | 0 | p_image->y0 = l_image->y0; |
10556 | 0 | p_image->x1 = l_image->x1; |
10557 | 0 | p_image->y1 = l_image->y1; |
10558 | |
|
10559 | 0 | return opj_j2k_update_image_dimensions(p_image, p_manager); |
10560 | 0 | } |
10561 | | |
10562 | | /* ----- */ |
10563 | | /* Check if the positions provided by the user are correct */ |
10564 | | |
10565 | | /* Left */ |
10566 | 7.48k | if (p_start_x < 0) { |
10567 | 13 | opj_event_msg(p_manager, EVT_ERROR, |
10568 | 13 | "Left position of the decoded area (region_x0=%d) should be >= 0.\n", |
10569 | 13 | p_start_x); |
10570 | 13 | return OPJ_FALSE; |
10571 | 7.46k | } else if ((OPJ_UINT32)p_start_x > l_image->x1) { |
10572 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10573 | 0 | "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n", |
10574 | 0 | p_start_x, l_image->x1); |
10575 | 0 | return OPJ_FALSE; |
10576 | 7.46k | } else if ((OPJ_UINT32)p_start_x < l_image->x0) { |
10577 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10578 | 0 | "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n", |
10579 | 0 | p_start_x, l_image->x0); |
10580 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
10581 | 0 | p_image->x0 = l_image->x0; |
10582 | 7.46k | } else { |
10583 | 7.46k | p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - |
10584 | 7.46k | l_cp->tx0) / l_cp->tdx; |
10585 | 7.46k | p_image->x0 = (OPJ_UINT32)p_start_x; |
10586 | 7.46k | } |
10587 | | |
10588 | | /* Up */ |
10589 | 7.46k | if (p_start_y < 0) { |
10590 | 20 | opj_event_msg(p_manager, EVT_ERROR, |
10591 | 20 | "Up position of the decoded area (region_y0=%d) should be >= 0.\n", |
10592 | 20 | p_start_y); |
10593 | 20 | return OPJ_FALSE; |
10594 | 7.44k | } else if ((OPJ_UINT32)p_start_y > l_image->y1) { |
10595 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10596 | 0 | "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n", |
10597 | 0 | p_start_y, l_image->y1); |
10598 | 0 | return OPJ_FALSE; |
10599 | 7.44k | } else if ((OPJ_UINT32)p_start_y < l_image->y0) { |
10600 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10601 | 0 | "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n", |
10602 | 0 | p_start_y, l_image->y0); |
10603 | 0 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
10604 | 0 | p_image->y0 = l_image->y0; |
10605 | 7.44k | } else { |
10606 | 7.44k | p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - |
10607 | 7.44k | l_cp->ty0) / l_cp->tdy; |
10608 | 7.44k | p_image->y0 = (OPJ_UINT32)p_start_y; |
10609 | 7.44k | } |
10610 | | |
10611 | | /* Right */ |
10612 | 7.44k | if (p_end_x <= 0) { |
10613 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
10614 | 1 | "Right position of the decoded area (region_x1=%d) should be > 0.\n", |
10615 | 1 | p_end_x); |
10616 | 1 | return OPJ_FALSE; |
10617 | 7.44k | } else if ((OPJ_UINT32)p_end_x < l_image->x0) { |
10618 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10619 | 0 | "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n", |
10620 | 0 | p_end_x, l_image->x0); |
10621 | 0 | return OPJ_FALSE; |
10622 | 7.44k | } else if ((OPJ_UINT32)p_end_x > l_image->x1) { |
10623 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10624 | 0 | "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n", |
10625 | 0 | p_end_x, l_image->x1); |
10626 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
10627 | 0 | p_image->x1 = l_image->x1; |
10628 | 7.44k | } else { |
10629 | 7.44k | p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv(( |
10630 | 7.44k | OPJ_UINT32)p_end_x - l_cp->tx0, l_cp->tdx); |
10631 | 7.44k | p_image->x1 = (OPJ_UINT32)p_end_x; |
10632 | 7.44k | } |
10633 | | |
10634 | | /* Bottom */ |
10635 | 7.44k | if (p_end_y <= 0) { |
10636 | 1 | opj_event_msg(p_manager, EVT_ERROR, |
10637 | 1 | "Bottom position of the decoded area (region_y1=%d) should be > 0.\n", |
10638 | 1 | p_end_y); |
10639 | 1 | return OPJ_FALSE; |
10640 | 7.44k | } else if ((OPJ_UINT32)p_end_y < l_image->y0) { |
10641 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10642 | 0 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n", |
10643 | 0 | p_end_y, l_image->y0); |
10644 | 0 | return OPJ_FALSE; |
10645 | 0 | } |
10646 | 7.44k | if ((OPJ_UINT32)p_end_y > l_image->y1) { |
10647 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
10648 | 0 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n", |
10649 | 0 | p_end_y, l_image->y1); |
10650 | 0 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
10651 | 0 | p_image->y1 = l_image->y1; |
10652 | 7.44k | } else { |
10653 | 7.44k | p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv(( |
10654 | 7.44k | OPJ_UINT32)p_end_y - l_cp->ty0, l_cp->tdy); |
10655 | 7.44k | p_image->y1 = (OPJ_UINT32)p_end_y; |
10656 | 7.44k | } |
10657 | | /* ----- */ |
10658 | | |
10659 | 7.44k | p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; |
10660 | | |
10661 | 7.44k | ret = opj_j2k_update_image_dimensions(p_image, p_manager); |
10662 | | |
10663 | 7.44k | if (ret) { |
10664 | 7.44k | opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n", |
10665 | 7.44k | p_image->x0, p_image->y0, p_image->x1, p_image->y1); |
10666 | 7.44k | } |
10667 | | |
10668 | 7.44k | return ret; |
10669 | 7.44k | } |
10670 | | |
10671 | | opj_j2k_t* opj_j2k_create_decompress(void) |
10672 | 8.86k | { |
10673 | 8.86k | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
10674 | 8.86k | if (!l_j2k) { |
10675 | 0 | return 00; |
10676 | 0 | } |
10677 | | |
10678 | 8.86k | l_j2k->m_is_decoder = 1; |
10679 | 8.86k | l_j2k->m_cp.m_is_decoder = 1; |
10680 | | /* in the absence of JP2 boxes, consider different bit depth / sign */ |
10681 | | /* per component is allowed */ |
10682 | 8.86k | l_j2k->m_cp.allow_different_bit_depth_sign = 1; |
10683 | | |
10684 | | /* Default to using strict mode. */ |
10685 | 8.86k | l_j2k->m_cp.strict = OPJ_TRUE; |
10686 | | |
10687 | | #ifdef OPJ_DISABLE_TPSOT_FIX |
10688 | | l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
10689 | | #endif |
10690 | | |
10691 | 8.86k | l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1, |
10692 | 8.86k | sizeof(opj_tcp_t)); |
10693 | 8.86k | if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { |
10694 | 0 | opj_j2k_destroy(l_j2k); |
10695 | 0 | return 00; |
10696 | 0 | } |
10697 | | |
10698 | 8.86k | l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1, |
10699 | 8.86k | OPJ_J2K_DEFAULT_HEADER_SIZE); |
10700 | 8.86k | if (! l_j2k->m_specific_param.m_decoder.m_header_data) { |
10701 | 0 | opj_j2k_destroy(l_j2k); |
10702 | 0 | return 00; |
10703 | 0 | } |
10704 | | |
10705 | 8.86k | l_j2k->m_specific_param.m_decoder.m_header_data_size = |
10706 | 8.86k | OPJ_J2K_DEFAULT_HEADER_SIZE; |
10707 | | |
10708 | 8.86k | l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; |
10709 | | |
10710 | 8.86k | l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; |
10711 | | |
10712 | | /* codestream index creation */ |
10713 | 8.86k | l_j2k->cstr_index = opj_j2k_create_cstr_index(); |
10714 | 8.86k | if (!l_j2k->cstr_index) { |
10715 | 0 | opj_j2k_destroy(l_j2k); |
10716 | 0 | return 00; |
10717 | 0 | } |
10718 | | |
10719 | | /* validation list creation */ |
10720 | 8.86k | l_j2k->m_validation_list = opj_procedure_list_create(); |
10721 | 8.86k | if (! l_j2k->m_validation_list) { |
10722 | 0 | opj_j2k_destroy(l_j2k); |
10723 | 0 | return 00; |
10724 | 0 | } |
10725 | | |
10726 | | /* execution list creation */ |
10727 | 8.86k | l_j2k->m_procedure_list = opj_procedure_list_create(); |
10728 | 8.86k | if (! l_j2k->m_procedure_list) { |
10729 | 0 | opj_j2k_destroy(l_j2k); |
10730 | 0 | return 00; |
10731 | 0 | } |
10732 | | |
10733 | 8.86k | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
10734 | 8.86k | if (!l_j2k->m_tp) { |
10735 | 0 | l_j2k->m_tp = opj_thread_pool_create(0); |
10736 | 0 | } |
10737 | 8.86k | if (!l_j2k->m_tp) { |
10738 | 0 | opj_j2k_destroy(l_j2k); |
10739 | 0 | return NULL; |
10740 | 0 | } |
10741 | | |
10742 | 8.86k | return l_j2k; |
10743 | 8.86k | } |
10744 | | |
10745 | | static opj_codestream_index_t* opj_j2k_create_cstr_index(void) |
10746 | 8.86k | { |
10747 | 8.86k | opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) |
10748 | 8.86k | opj_calloc(1, sizeof(opj_codestream_index_t)); |
10749 | 8.86k | if (!cstr_index) { |
10750 | 0 | return NULL; |
10751 | 0 | } |
10752 | | |
10753 | 8.86k | cstr_index->maxmarknum = 100; |
10754 | 8.86k | cstr_index->marknum = 0; |
10755 | 8.86k | cstr_index->marker = (opj_marker_info_t*) |
10756 | 8.86k | opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); |
10757 | 8.86k | if (!cstr_index-> marker) { |
10758 | 0 | opj_free(cstr_index); |
10759 | 0 | return NULL; |
10760 | 0 | } |
10761 | | |
10762 | 8.86k | cstr_index->tile_index = NULL; |
10763 | | |
10764 | 8.86k | return cstr_index; |
10765 | 8.86k | } |
10766 | | |
10767 | | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, |
10768 | | OPJ_UINT32 p_tile_no, |
10769 | | OPJ_UINT32 p_comp_no) |
10770 | 0 | { |
10771 | 0 | opj_cp_t *l_cp = 00; |
10772 | 0 | opj_tcp_t *l_tcp = 00; |
10773 | 0 | opj_tccp_t *l_tccp = 00; |
10774 | | |
10775 | | /* preconditions */ |
10776 | 0 | assert(p_j2k != 00); |
10777 | |
|
10778 | 0 | l_cp = &(p_j2k->m_cp); |
10779 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10780 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10781 | | |
10782 | | /* preconditions again */ |
10783 | 0 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
10784 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
10785 | |
|
10786 | 0 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
10787 | 0 | return 5 + l_tccp->numresolutions; |
10788 | 0 | } else { |
10789 | 0 | return 5; |
10790 | 0 | } |
10791 | 0 | } |
10792 | | |
10793 | | static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10794 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
10795 | 0 | { |
10796 | 0 | OPJ_UINT32 i; |
10797 | 0 | opj_cp_t *l_cp = NULL; |
10798 | 0 | opj_tcp_t *l_tcp = NULL; |
10799 | 0 | opj_tccp_t *l_tccp0 = NULL; |
10800 | 0 | opj_tccp_t *l_tccp1 = NULL; |
10801 | | |
10802 | | /* preconditions */ |
10803 | 0 | assert(p_j2k != 00); |
10804 | |
|
10805 | 0 | l_cp = &(p_j2k->m_cp); |
10806 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10807 | 0 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
10808 | 0 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
10809 | |
|
10810 | 0 | if (l_tccp0->numresolutions != l_tccp1->numresolutions) { |
10811 | 0 | return OPJ_FALSE; |
10812 | 0 | } |
10813 | 0 | if (l_tccp0->cblkw != l_tccp1->cblkw) { |
10814 | 0 | return OPJ_FALSE; |
10815 | 0 | } |
10816 | 0 | if (l_tccp0->cblkh != l_tccp1->cblkh) { |
10817 | 0 | return OPJ_FALSE; |
10818 | 0 | } |
10819 | 0 | if (l_tccp0->cblksty != l_tccp1->cblksty) { |
10820 | 0 | return OPJ_FALSE; |
10821 | 0 | } |
10822 | 0 | if (l_tccp0->qmfbid != l_tccp1->qmfbid) { |
10823 | 0 | return OPJ_FALSE; |
10824 | 0 | } |
10825 | 0 | if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) { |
10826 | 0 | return OPJ_FALSE; |
10827 | 0 | } |
10828 | | |
10829 | 0 | for (i = 0U; i < l_tccp0->numresolutions; ++i) { |
10830 | 0 | if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) { |
10831 | 0 | return OPJ_FALSE; |
10832 | 0 | } |
10833 | 0 | if (l_tccp0->prch[i] != l_tccp1->prch[i]) { |
10834 | 0 | return OPJ_FALSE; |
10835 | 0 | } |
10836 | 0 | } |
10837 | 0 | return OPJ_TRUE; |
10838 | 0 | } |
10839 | | |
10840 | | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10841 | | OPJ_UINT32 p_tile_no, |
10842 | | OPJ_UINT32 p_comp_no, |
10843 | | OPJ_BYTE * p_data, |
10844 | | OPJ_UINT32 * p_header_size, |
10845 | | struct opj_event_mgr * p_manager) |
10846 | 0 | { |
10847 | 0 | OPJ_UINT32 i; |
10848 | 0 | opj_cp_t *l_cp = 00; |
10849 | 0 | opj_tcp_t *l_tcp = 00; |
10850 | 0 | opj_tccp_t *l_tccp = 00; |
10851 | | |
10852 | | /* preconditions */ |
10853 | 0 | assert(p_j2k != 00); |
10854 | 0 | assert(p_header_size != 00); |
10855 | 0 | assert(p_manager != 00); |
10856 | 0 | assert(p_data != 00); |
10857 | |
|
10858 | 0 | l_cp = &(p_j2k->m_cp); |
10859 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
10860 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10861 | | |
10862 | | /* preconditions again */ |
10863 | 0 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
10864 | 0 | assert(p_comp_no < (p_j2k->m_private_image->numcomps)); |
10865 | |
|
10866 | 0 | if (*p_header_size < 5) { |
10867 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
10868 | 0 | return OPJ_FALSE; |
10869 | 0 | } |
10870 | | |
10871 | 0 | opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ |
10872 | 0 | ++p_data; |
10873 | |
|
10874 | 0 | opj_write_bytes(p_data, l_tccp->cblkw - 2, 1); /* SPcoc (E) */ |
10875 | 0 | ++p_data; |
10876 | |
|
10877 | 0 | opj_write_bytes(p_data, l_tccp->cblkh - 2, 1); /* SPcoc (F) */ |
10878 | 0 | ++p_data; |
10879 | |
|
10880 | 0 | opj_write_bytes(p_data, l_tccp->cblksty, |
10881 | 0 | 1); /* SPcoc (G) */ |
10882 | 0 | ++p_data; |
10883 | |
|
10884 | 0 | opj_write_bytes(p_data, l_tccp->qmfbid, |
10885 | 0 | 1); /* SPcoc (H) */ |
10886 | 0 | ++p_data; |
10887 | |
|
10888 | 0 | *p_header_size = *p_header_size - 5; |
10889 | |
|
10890 | 0 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
10891 | |
|
10892 | 0 | if (*p_header_size < l_tccp->numresolutions) { |
10893 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
10894 | 0 | return OPJ_FALSE; |
10895 | 0 | } |
10896 | | |
10897 | 0 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
10898 | 0 | opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4), |
10899 | 0 | 1); /* SPcoc (I_i) */ |
10900 | 0 | ++p_data; |
10901 | 0 | } |
10902 | |
|
10903 | 0 | *p_header_size = *p_header_size - l_tccp->numresolutions; |
10904 | 0 | } |
10905 | | |
10906 | 0 | return OPJ_TRUE; |
10907 | 0 | } |
10908 | | |
10909 | | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, |
10910 | | OPJ_UINT32 compno, |
10911 | | OPJ_BYTE * p_header_data, |
10912 | | OPJ_UINT32 * p_header_size, |
10913 | | opj_event_mgr_t * p_manager) |
10914 | 10.6k | { |
10915 | 10.6k | OPJ_UINT32 i, l_tmp; |
10916 | 10.6k | opj_cp_t *l_cp = NULL; |
10917 | 10.6k | opj_tcp_t *l_tcp = NULL; |
10918 | 10.6k | opj_tccp_t *l_tccp = NULL; |
10919 | 10.6k | OPJ_BYTE * l_current_ptr = NULL; |
10920 | | |
10921 | | /* preconditions */ |
10922 | 10.6k | assert(p_j2k != 00); |
10923 | 10.6k | assert(p_manager != 00); |
10924 | 10.6k | assert(p_header_data != 00); |
10925 | | |
10926 | 10.6k | l_cp = &(p_j2k->m_cp); |
10927 | 10.6k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
10928 | 24 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
10929 | 10.6k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
10930 | | |
10931 | | /* precondition again */ |
10932 | 10.6k | assert(compno < p_j2k->m_private_image->numcomps); |
10933 | | |
10934 | 10.6k | l_tccp = &l_tcp->tccps[compno]; |
10935 | 10.6k | l_current_ptr = p_header_data; |
10936 | | |
10937 | | /* make sure room is sufficient */ |
10938 | 10.6k | if (*p_header_size < 5) { |
10939 | 7 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
10940 | 7 | return OPJ_FALSE; |
10941 | 7 | } |
10942 | | |
10943 | | /* SPcod (D) / SPcoc (A) */ |
10944 | 10.6k | opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, 1); |
10945 | 10.6k | ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ |
10946 | 10.6k | if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { |
10947 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
10948 | 6 | "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n", |
10949 | 6 | l_tccp->numresolutions, OPJ_J2K_MAXRLVLS); |
10950 | 6 | return OPJ_FALSE; |
10951 | 6 | } |
10952 | 10.6k | ++l_current_ptr; |
10953 | | |
10954 | | /* If user wants to remove more resolutions than the codestream contains, return error */ |
10955 | 10.6k | if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { |
10956 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
10957 | 0 | "Error decoding component %d.\nThe number of resolutions " |
10958 | 0 | "to remove (%d) is greater or equal than the number " |
10959 | 0 | "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n", |
10960 | 0 | compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions); |
10961 | 0 | p_j2k->m_specific_param.m_decoder.m_state |= |
10962 | 0 | 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ |
10963 | 0 | return OPJ_FALSE; |
10964 | 0 | } |
10965 | | |
10966 | | /* SPcod (E) / SPcoc (B) */ |
10967 | 10.6k | opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1); |
10968 | 10.6k | ++l_current_ptr; |
10969 | 10.6k | l_tccp->cblkw += 2; |
10970 | | |
10971 | | /* SPcod (F) / SPcoc (C) */ |
10972 | 10.6k | opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1); |
10973 | 10.6k | ++l_current_ptr; |
10974 | 10.6k | l_tccp->cblkh += 2; |
10975 | | |
10976 | 10.6k | if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || |
10977 | 10.6k | ((l_tccp->cblkw + l_tccp->cblkh) > 12)) { |
10978 | 10 | opj_event_msg(p_manager, EVT_ERROR, |
10979 | 10 | "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n"); |
10980 | 10 | return OPJ_FALSE; |
10981 | 10 | } |
10982 | | |
10983 | | /* SPcod (G) / SPcoc (D) */ |
10984 | 10.6k | opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1); |
10985 | 10.6k | ++l_current_ptr; |
10986 | 10.6k | if ((l_tccp->cblksty & J2K_CCP_CBLKSTY_HTMIXED) != 0) { |
10987 | | /* We do not support HT mixed mode yet. For conformance, it should be supported.*/ |
10988 | 2 | opj_event_msg(p_manager, EVT_ERROR, |
10989 | 2 | "Error reading SPCod SPCoc element. Unsupported Mixed HT code-block style found\n"); |
10990 | 2 | return OPJ_FALSE; |
10991 | 2 | } |
10992 | | |
10993 | | /* SPcod (H) / SPcoc (E) */ |
10994 | 10.6k | opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1); |
10995 | 10.6k | ++l_current_ptr; |
10996 | | |
10997 | 10.6k | if (l_tccp->qmfbid > 1) { |
10998 | 6 | opj_event_msg(p_manager, EVT_ERROR, |
10999 | 6 | "Error reading SPCod SPCoc element, Invalid transformation found\n"); |
11000 | 6 | return OPJ_FALSE; |
11001 | 6 | } |
11002 | | |
11003 | 10.6k | *p_header_size = *p_header_size - 5; |
11004 | | |
11005 | | /* use custom precinct size ? */ |
11006 | 10.6k | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
11007 | 5.18k | if (*p_header_size < l_tccp->numresolutions) { |
11008 | 2 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
11009 | 2 | return OPJ_FALSE; |
11010 | 2 | } |
11011 | | |
11012 | | /* SPcod (I_i) / SPcoc (F_i) */ |
11013 | 11.5k | for (i = 0; i < l_tccp->numresolutions; ++i) { |
11014 | 6.39k | opj_read_bytes(l_current_ptr, &l_tmp, 1); |
11015 | 6.39k | ++l_current_ptr; |
11016 | | /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */ |
11017 | 6.39k | if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) { |
11018 | 8 | opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n"); |
11019 | 8 | return OPJ_FALSE; |
11020 | 8 | } |
11021 | 6.38k | l_tccp->prcw[i] = l_tmp & 0xf; |
11022 | 6.38k | l_tccp->prch[i] = l_tmp >> 4; |
11023 | 6.38k | } |
11024 | | |
11025 | 5.17k | *p_header_size = *p_header_size - l_tccp->numresolutions; |
11026 | 5.42k | } else { |
11027 | | /* set default size for the precinct width and height */ |
11028 | 33.4k | for (i = 0; i < l_tccp->numresolutions; ++i) { |
11029 | 28.0k | l_tccp->prcw[i] = 15; |
11030 | 28.0k | l_tccp->prch[i] = 15; |
11031 | 28.0k | } |
11032 | 5.42k | } |
11033 | | |
11034 | | #ifdef WIP_REMOVE_MSD |
11035 | | /* INDEX >> */ |
11036 | | if (p_j2k->cstr_info && compno == 0) { |
11037 | | OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); |
11038 | | |
11039 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = |
11040 | | l_tccp->cblkh; |
11041 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = |
11042 | | l_tccp->cblkw; |
11043 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions |
11044 | | = l_tccp->numresolutions; |
11045 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = |
11046 | | l_tccp->cblksty; |
11047 | | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = |
11048 | | l_tccp->qmfbid; |
11049 | | |
11050 | | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw, |
11051 | | l_data_size); |
11052 | | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch, |
11053 | | l_data_size); |
11054 | | } |
11055 | | /* << INDEX */ |
11056 | | #endif |
11057 | | |
11058 | 10.5k | return OPJ_TRUE; |
11059 | 10.6k | } |
11060 | | |
11061 | | static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k) |
11062 | 10.3k | { |
11063 | | /* loop */ |
11064 | 10.3k | OPJ_UINT32 i; |
11065 | 10.3k | opj_cp_t *l_cp = NULL; |
11066 | 10.3k | opj_tcp_t *l_tcp = NULL; |
11067 | 10.3k | opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; |
11068 | 10.3k | OPJ_UINT32 l_prc_size; |
11069 | | |
11070 | | /* preconditions */ |
11071 | 10.3k | assert(p_j2k != 00); |
11072 | | |
11073 | 10.3k | l_cp = &(p_j2k->m_cp); |
11074 | 10.3k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
11075 | 10.3k | ? |
11076 | 1 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11077 | 10.3k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11078 | | |
11079 | 10.3k | l_ref_tccp = &l_tcp->tccps[0]; |
11080 | 10.3k | l_copied_tccp = l_ref_tccp + 1; |
11081 | 10.3k | l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32); |
11082 | | |
11083 | 37.7k | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
11084 | 27.3k | l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; |
11085 | 27.3k | l_copied_tccp->cblkw = l_ref_tccp->cblkw; |
11086 | 27.3k | l_copied_tccp->cblkh = l_ref_tccp->cblkh; |
11087 | 27.3k | l_copied_tccp->cblksty = l_ref_tccp->cblksty; |
11088 | 27.3k | l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; |
11089 | 27.3k | memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size); |
11090 | 27.3k | memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size); |
11091 | 27.3k | ++l_copied_tccp; |
11092 | 27.3k | } |
11093 | 10.3k | } |
11094 | | |
11095 | | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, |
11096 | | OPJ_UINT32 p_tile_no, |
11097 | | OPJ_UINT32 p_comp_no) |
11098 | 0 | { |
11099 | 0 | OPJ_UINT32 l_num_bands; |
11100 | |
|
11101 | 0 | opj_cp_t *l_cp = 00; |
11102 | 0 | opj_tcp_t *l_tcp = 00; |
11103 | 0 | opj_tccp_t *l_tccp = 00; |
11104 | | |
11105 | | /* preconditions */ |
11106 | 0 | assert(p_j2k != 00); |
11107 | |
|
11108 | 0 | l_cp = &(p_j2k->m_cp); |
11109 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
11110 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
11111 | | |
11112 | | /* preconditions again */ |
11113 | 0 | assert(p_tile_no < l_cp->tw * l_cp->th); |
11114 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
11115 | |
|
11116 | 0 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11117 | 0 | (l_tccp->numresolutions * 3 - 2); |
11118 | |
|
11119 | 0 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
11120 | 0 | return 1 + l_num_bands; |
11121 | 0 | } else { |
11122 | 0 | return 1 + 2 * l_num_bands; |
11123 | 0 | } |
11124 | 0 | } |
11125 | | |
11126 | | static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, |
11127 | | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
11128 | 0 | { |
11129 | 0 | opj_cp_t *l_cp = NULL; |
11130 | 0 | opj_tcp_t *l_tcp = NULL; |
11131 | 0 | opj_tccp_t *l_tccp0 = NULL; |
11132 | 0 | opj_tccp_t *l_tccp1 = NULL; |
11133 | 0 | OPJ_UINT32 l_band_no, l_num_bands; |
11134 | | |
11135 | | /* preconditions */ |
11136 | 0 | assert(p_j2k != 00); |
11137 | |
|
11138 | 0 | l_cp = &(p_j2k->m_cp); |
11139 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
11140 | 0 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
11141 | 0 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
11142 | |
|
11143 | 0 | if (l_tccp0->qntsty != l_tccp1->qntsty) { |
11144 | 0 | return OPJ_FALSE; |
11145 | 0 | } |
11146 | 0 | if (l_tccp0->numgbits != l_tccp1->numgbits) { |
11147 | 0 | return OPJ_FALSE; |
11148 | 0 | } |
11149 | 0 | if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11150 | 0 | l_num_bands = 1U; |
11151 | 0 | } else { |
11152 | 0 | l_num_bands = l_tccp0->numresolutions * 3U - 2U; |
11153 | 0 | if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) { |
11154 | 0 | return OPJ_FALSE; |
11155 | 0 | } |
11156 | 0 | } |
11157 | | |
11158 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11159 | 0 | if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) { |
11160 | 0 | return OPJ_FALSE; |
11161 | 0 | } |
11162 | 0 | } |
11163 | 0 | if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) { |
11164 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11165 | 0 | if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) { |
11166 | 0 | return OPJ_FALSE; |
11167 | 0 | } |
11168 | 0 | } |
11169 | 0 | } |
11170 | 0 | return OPJ_TRUE; |
11171 | 0 | } |
11172 | | |
11173 | | |
11174 | | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
11175 | | OPJ_UINT32 p_tile_no, |
11176 | | OPJ_UINT32 p_comp_no, |
11177 | | OPJ_BYTE * p_data, |
11178 | | OPJ_UINT32 * p_header_size, |
11179 | | struct opj_event_mgr * p_manager) |
11180 | 0 | { |
11181 | 0 | OPJ_UINT32 l_header_size; |
11182 | 0 | OPJ_UINT32 l_band_no, l_num_bands; |
11183 | 0 | OPJ_UINT32 l_expn, l_mant; |
11184 | |
|
11185 | 0 | opj_cp_t *l_cp = 00; |
11186 | 0 | opj_tcp_t *l_tcp = 00; |
11187 | 0 | opj_tccp_t *l_tccp = 00; |
11188 | | |
11189 | | /* preconditions */ |
11190 | 0 | assert(p_j2k != 00); |
11191 | 0 | assert(p_header_size != 00); |
11192 | 0 | assert(p_manager != 00); |
11193 | 0 | assert(p_data != 00); |
11194 | |
|
11195 | 0 | l_cp = &(p_j2k->m_cp); |
11196 | 0 | l_tcp = &l_cp->tcps[p_tile_no]; |
11197 | 0 | l_tccp = &l_tcp->tccps[p_comp_no]; |
11198 | | |
11199 | | /* preconditions again */ |
11200 | 0 | assert(p_tile_no < l_cp->tw * l_cp->th); |
11201 | 0 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
11202 | |
|
11203 | 0 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11204 | 0 | (l_tccp->numresolutions * 3 - 2); |
11205 | |
|
11206 | 0 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
11207 | 0 | l_header_size = 1 + l_num_bands; |
11208 | |
|
11209 | 0 | if (*p_header_size < l_header_size) { |
11210 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
11211 | 0 | return OPJ_FALSE; |
11212 | 0 | } |
11213 | | |
11214 | 0 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
11215 | 0 | 1); /* Sqcx */ |
11216 | 0 | ++p_data; |
11217 | |
|
11218 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11219 | 0 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
11220 | 0 | opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ |
11221 | 0 | ++p_data; |
11222 | 0 | } |
11223 | 0 | } else { |
11224 | 0 | l_header_size = 1 + 2 * l_num_bands; |
11225 | |
|
11226 | 0 | if (*p_header_size < l_header_size) { |
11227 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
11228 | 0 | return OPJ_FALSE; |
11229 | 0 | } |
11230 | | |
11231 | 0 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
11232 | 0 | 1); /* Sqcx */ |
11233 | 0 | ++p_data; |
11234 | |
|
11235 | 0 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
11236 | 0 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
11237 | 0 | l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant; |
11238 | |
|
11239 | 0 | opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ |
11240 | 0 | p_data += 2; |
11241 | 0 | } |
11242 | 0 | } |
11243 | | |
11244 | 0 | *p_header_size = *p_header_size - l_header_size; |
11245 | |
|
11246 | 0 | return OPJ_TRUE; |
11247 | 0 | } |
11248 | | |
11249 | | static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
11250 | | OPJ_UINT32 p_comp_no, |
11251 | | OPJ_BYTE* p_header_data, |
11252 | | OPJ_UINT32 * p_header_size, |
11253 | | opj_event_mgr_t * p_manager |
11254 | | ) |
11255 | 12.4k | { |
11256 | | /* loop*/ |
11257 | 12.4k | OPJ_UINT32 l_band_no; |
11258 | 12.4k | opj_cp_t *l_cp = 00; |
11259 | 12.4k | opj_tcp_t *l_tcp = 00; |
11260 | 12.4k | opj_tccp_t *l_tccp = 00; |
11261 | 12.4k | OPJ_BYTE * l_current_ptr = 00; |
11262 | 12.4k | OPJ_UINT32 l_tmp, l_num_band; |
11263 | | |
11264 | | /* preconditions*/ |
11265 | 12.4k | assert(p_j2k != 00); |
11266 | 12.4k | assert(p_manager != 00); |
11267 | 12.4k | assert(p_header_data != 00); |
11268 | | |
11269 | 12.4k | l_cp = &(p_j2k->m_cp); |
11270 | | /* come from tile part header or main header ?*/ |
11271 | 12.4k | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
11272 | 12.4k | ? |
11273 | 74 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11274 | 12.4k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11275 | | |
11276 | | /* precondition again*/ |
11277 | 12.4k | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
11278 | | |
11279 | 12.4k | l_tccp = &l_tcp->tccps[p_comp_no]; |
11280 | 12.4k | l_current_ptr = p_header_data; |
11281 | | |
11282 | 12.4k | if (*p_header_size < 1) { |
11283 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); |
11284 | 3 | return OPJ_FALSE; |
11285 | 3 | } |
11286 | 12.4k | *p_header_size -= 1; |
11287 | | |
11288 | 12.4k | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* Sqcx */ |
11289 | 12.4k | ++l_current_ptr; |
11290 | | |
11291 | 12.4k | l_tccp->qntsty = l_tmp & 0x1f; |
11292 | 12.4k | l_tccp->numgbits = l_tmp >> 5; |
11293 | 12.4k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11294 | 1.08k | l_num_band = 1; |
11295 | 11.3k | } else { |
11296 | 11.3k | l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? |
11297 | 8.95k | (*p_header_size) : |
11298 | 11.3k | (*p_header_size) / 2; |
11299 | | |
11300 | 11.3k | if (l_num_band > OPJ_J2K_MAXBANDS) { |
11301 | 366 | opj_event_msg(p_manager, EVT_WARNING, |
11302 | 366 | "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " |
11303 | 366 | "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to " |
11304 | 366 | "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, |
11305 | 366 | OPJ_J2K_MAXBANDS); |
11306 | | /*return OPJ_FALSE;*/ |
11307 | 366 | } |
11308 | 11.3k | } |
11309 | | |
11310 | | #ifdef USE_JPWL |
11311 | | if (l_cp->correct) { |
11312 | | |
11313 | | /* if JPWL is on, we check whether there are too many subbands */ |
11314 | | if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { |
11315 | | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
11316 | | "JPWL: bad number of subbands in Sqcx (%d)\n", |
11317 | | l_num_band); |
11318 | | if (!JPWL_ASSUME) { |
11319 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
11320 | | return OPJ_FALSE; |
11321 | | } |
11322 | | /* we try to correct */ |
11323 | | l_num_band = 1; |
11324 | | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" |
11325 | | "- setting number of bands to %d => HYPOTHESIS!!!\n", |
11326 | | l_num_band); |
11327 | | }; |
11328 | | |
11329 | | }; |
11330 | | #endif /* USE_JPWL */ |
11331 | | |
11332 | 12.4k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
11333 | 80.4k | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
11334 | 71.5k | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPqcx_i */ |
11335 | 71.5k | ++l_current_ptr; |
11336 | 71.5k | if (l_band_no < OPJ_J2K_MAXBANDS) { |
11337 | 44.9k | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3); |
11338 | 44.9k | l_tccp->stepsizes[l_band_no].mant = 0; |
11339 | 44.9k | } |
11340 | 71.5k | } |
11341 | | |
11342 | 8.95k | if (*p_header_size < l_num_band) { |
11343 | 0 | return OPJ_FALSE; |
11344 | 0 | } |
11345 | 8.95k | *p_header_size = *p_header_size - l_num_band; |
11346 | 8.95k | } else { |
11347 | 317k | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
11348 | 314k | opj_read_bytes(l_current_ptr, &l_tmp, 2); /* SPqcx_i */ |
11349 | 314k | l_current_ptr += 2; |
11350 | 314k | if (l_band_no < OPJ_J2K_MAXBANDS) { |
11351 | 33.0k | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11); |
11352 | 33.0k | l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; |
11353 | 33.0k | } |
11354 | 314k | } |
11355 | | |
11356 | 3.46k | if (*p_header_size < 2 * l_num_band) { |
11357 | 2 | return OPJ_FALSE; |
11358 | 2 | } |
11359 | 3.46k | *p_header_size = *p_header_size - 2 * l_num_band; |
11360 | 3.46k | } |
11361 | | |
11362 | | /* Add Antonin : if scalar_derived -> compute other stepsizes */ |
11363 | 12.4k | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
11364 | 105k | for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) { |
11365 | 104k | l_tccp->stepsizes[l_band_no].expn = |
11366 | 104k | ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) |
11367 | 104k | ? |
11368 | 73.3k | (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0; |
11369 | 104k | l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; |
11370 | 104k | } |
11371 | 1.08k | } |
11372 | | |
11373 | 12.4k | return OPJ_TRUE; |
11374 | 12.4k | } |
11375 | | |
11376 | | static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k) |
11377 | 11.3k | { |
11378 | 11.3k | OPJ_UINT32 i; |
11379 | 11.3k | opj_cp_t *l_cp = NULL; |
11380 | 11.3k | opj_tcp_t *l_tcp = NULL; |
11381 | 11.3k | opj_tccp_t *l_ref_tccp = NULL; |
11382 | 11.3k | opj_tccp_t *l_copied_tccp = NULL; |
11383 | 11.3k | OPJ_UINT32 l_size; |
11384 | | |
11385 | | /* preconditions */ |
11386 | 11.3k | assert(p_j2k != 00); |
11387 | | |
11388 | 11.3k | l_cp = &(p_j2k->m_cp); |
11389 | 11.3k | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
11390 | 72 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
11391 | 11.3k | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11392 | | |
11393 | 11.3k | l_ref_tccp = &l_tcp->tccps[0]; |
11394 | 11.3k | l_copied_tccp = l_ref_tccp + 1; |
11395 | 11.3k | l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t); |
11396 | | |
11397 | 87.6k | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
11398 | 76.3k | l_copied_tccp->qntsty = l_ref_tccp->qntsty; |
11399 | 76.3k | l_copied_tccp->numgbits = l_ref_tccp->numgbits; |
11400 | 76.3k | memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size); |
11401 | 76.3k | ++l_copied_tccp; |
11402 | 76.3k | } |
11403 | 11.3k | } |
11404 | | |
11405 | | static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile, |
11406 | | OPJ_INT32 numcomps, FILE* out_stream) |
11407 | 0 | { |
11408 | 0 | if (l_default_tile) { |
11409 | 0 | OPJ_INT32 compno; |
11410 | |
|
11411 | 0 | fprintf(out_stream, "\t default tile {\n"); |
11412 | 0 | fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty); |
11413 | 0 | fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg); |
11414 | 0 | fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers); |
11415 | 0 | fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct); |
11416 | |
|
11417 | 0 | for (compno = 0; compno < numcomps; compno++) { |
11418 | 0 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
11419 | 0 | OPJ_UINT32 resno; |
11420 | 0 | OPJ_INT32 bandno, numbands; |
11421 | | |
11422 | | /* coding style*/ |
11423 | 0 | fprintf(out_stream, "\t\t comp %d {\n", compno); |
11424 | 0 | fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty); |
11425 | 0 | fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions); |
11426 | 0 | fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw); |
11427 | 0 | fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh); |
11428 | 0 | fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty); |
11429 | 0 | fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid); |
11430 | |
|
11431 | 0 | fprintf(out_stream, "\t\t\t preccintsize (w,h)="); |
11432 | 0 | for (resno = 0; resno < l_tccp->numresolutions; resno++) { |
11433 | 0 | fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]); |
11434 | 0 | } |
11435 | 0 | fprintf(out_stream, "\n"); |
11436 | | |
11437 | | /* quantization style*/ |
11438 | 0 | fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty); |
11439 | 0 | fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits); |
11440 | 0 | fprintf(out_stream, "\t\t\t stepsizes (m,e)="); |
11441 | 0 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11442 | 0 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
11443 | 0 | for (bandno = 0; bandno < numbands; bandno++) { |
11444 | 0 | fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant, |
11445 | 0 | l_tccp->stepsizes[bandno].expn); |
11446 | 0 | } |
11447 | 0 | fprintf(out_stream, "\n"); |
11448 | | |
11449 | | /* RGN value*/ |
11450 | 0 | fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift); |
11451 | |
|
11452 | 0 | fprintf(out_stream, "\t\t }\n"); |
11453 | 0 | } /*end of component of default tile*/ |
11454 | 0 | fprintf(out_stream, "\t }\n"); /*end of default tile*/ |
11455 | 0 | } |
11456 | 0 | } |
11457 | | |
11458 | | void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream) |
11459 | 0 | { |
11460 | | /* Check if the flag is compatible with j2k file*/ |
11461 | 0 | if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) { |
11462 | 0 | fprintf(out_stream, "Wrong flag\n"); |
11463 | 0 | return; |
11464 | 0 | } |
11465 | | |
11466 | | /* Dump the image_header */ |
11467 | 0 | if (flag & OPJ_IMG_INFO) { |
11468 | 0 | if (p_j2k->m_private_image) { |
11469 | 0 | j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); |
11470 | 0 | } |
11471 | 0 | } |
11472 | | |
11473 | | /* Dump the codestream info from main header */ |
11474 | 0 | if (flag & OPJ_J2K_MH_INFO) { |
11475 | 0 | if (p_j2k->m_private_image) { |
11476 | 0 | opj_j2k_dump_MH_info(p_j2k, out_stream); |
11477 | 0 | } |
11478 | 0 | } |
11479 | | /* Dump all tile/codestream info */ |
11480 | 0 | if (flag & OPJ_J2K_TCH_INFO) { |
11481 | 0 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
11482 | 0 | OPJ_UINT32 i; |
11483 | 0 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
11484 | 0 | if (p_j2k->m_private_image) { |
11485 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
11486 | 0 | opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps, |
11487 | 0 | out_stream); |
11488 | 0 | ++l_tcp; |
11489 | 0 | } |
11490 | 0 | } |
11491 | 0 | } |
11492 | | |
11493 | | /* Dump the codestream info of the current tile */ |
11494 | 0 | if (flag & OPJ_J2K_TH_INFO) { |
11495 | |
|
11496 | 0 | } |
11497 | | |
11498 | | /* Dump the codestream index from main header */ |
11499 | 0 | if (flag & OPJ_J2K_MH_IND) { |
11500 | 0 | opj_j2k_dump_MH_index(p_j2k, out_stream); |
11501 | 0 | } |
11502 | | |
11503 | | /* Dump the codestream index of the current tile */ |
11504 | 0 | if (flag & OPJ_J2K_TH_IND) { |
11505 | |
|
11506 | 0 | } |
11507 | |
|
11508 | 0 | } |
11509 | | |
11510 | | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream) |
11511 | 0 | { |
11512 | 0 | opj_codestream_index_t* cstr_index = p_j2k->cstr_index; |
11513 | 0 | OPJ_UINT32 it_marker, it_tile, it_tile_part; |
11514 | |
|
11515 | 0 | fprintf(out_stream, "Codestream index from main header: {\n"); |
11516 | |
|
11517 | 0 | fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" |
11518 | 0 | "\t Main header end position=%" PRIi64 "\n", |
11519 | 0 | cstr_index->main_head_start, cstr_index->main_head_end); |
11520 | |
|
11521 | 0 | fprintf(out_stream, "\t Marker list: {\n"); |
11522 | |
|
11523 | 0 | if (cstr_index->marker) { |
11524 | 0 | for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) { |
11525 | 0 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
11526 | 0 | cstr_index->marker[it_marker].type, |
11527 | 0 | cstr_index->marker[it_marker].pos, |
11528 | 0 | cstr_index->marker[it_marker].len); |
11529 | 0 | } |
11530 | 0 | } |
11531 | |
|
11532 | 0 | fprintf(out_stream, "\t }\n"); |
11533 | |
|
11534 | 0 | if (cstr_index->tile_index) { |
11535 | | |
11536 | | /* Simple test to avoid to write empty information*/ |
11537 | 0 | OPJ_UINT32 l_acc_nb_of_tile_part = 0; |
11538 | 0 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
11539 | 0 | l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; |
11540 | | |
11541 | | /* To avoid regenerating expected opj_dump results from the test */ |
11542 | | /* suite when there is a TLM marker present */ |
11543 | 0 | if (cstr_index->tile_index[it_tile].nb_tps && |
11544 | 0 | cstr_index->tile_index[it_tile].tp_index && |
11545 | 0 | cstr_index->tile_index[it_tile].tp_index[0].start_pos > 0 && |
11546 | 0 | cstr_index->tile_index[it_tile].tp_index[0].end_header == 0 && |
11547 | 0 | getenv("OJP_DO_NOT_DISPLAY_TILE_INDEX_IF_TLM") != NULL) { |
11548 | 0 | l_acc_nb_of_tile_part = 0; |
11549 | 0 | break; |
11550 | 0 | } |
11551 | 0 | } |
11552 | |
|
11553 | 0 | if (l_acc_nb_of_tile_part) { |
11554 | 0 | fprintf(out_stream, "\t Tile index: {\n"); |
11555 | |
|
11556 | 0 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
11557 | 0 | OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; |
11558 | |
|
11559 | 0 | fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, |
11560 | 0 | nb_of_tile_part); |
11561 | |
|
11562 | 0 | if (cstr_index->tile_index[it_tile].tp_index) { |
11563 | 0 | for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) { |
11564 | 0 | fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" |
11565 | 0 | PRIi64 ", end_pos=%" PRIi64 ".\n", |
11566 | 0 | it_tile_part, |
11567 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, |
11568 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, |
11569 | 0 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); |
11570 | 0 | } |
11571 | 0 | } |
11572 | |
|
11573 | 0 | if (cstr_index->tile_index[it_tile].marker) { |
11574 | 0 | for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ; |
11575 | 0 | it_marker++) { |
11576 | 0 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
11577 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].type, |
11578 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].pos, |
11579 | 0 | cstr_index->tile_index[it_tile].marker[it_marker].len); |
11580 | 0 | } |
11581 | 0 | } |
11582 | 0 | } |
11583 | 0 | fprintf(out_stream, "\t }\n"); |
11584 | 0 | } |
11585 | 0 | } |
11586 | |
|
11587 | 0 | fprintf(out_stream, "}\n"); |
11588 | |
|
11589 | 0 | } |
11590 | | |
11591 | | |
11592 | | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream) |
11593 | 0 | { |
11594 | |
|
11595 | 0 | fprintf(out_stream, "Codestream info from main header: {\n"); |
11596 | |
|
11597 | 0 | fprintf(out_stream, "\t tx0=%" PRIu32 ", ty0=%" PRIu32 "\n", p_j2k->m_cp.tx0, |
11598 | 0 | p_j2k->m_cp.ty0); |
11599 | 0 | fprintf(out_stream, "\t tdx=%" PRIu32 ", tdy=%" PRIu32 "\n", p_j2k->m_cp.tdx, |
11600 | 0 | p_j2k->m_cp.tdy); |
11601 | 0 | fprintf(out_stream, "\t tw=%" PRIu32 ", th=%" PRIu32 "\n", p_j2k->m_cp.tw, |
11602 | 0 | p_j2k->m_cp.th); |
11603 | 0 | opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp, |
11604 | 0 | (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream); |
11605 | 0 | fprintf(out_stream, "}\n"); |
11606 | 0 | } |
11607 | | |
11608 | | void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, |
11609 | | FILE* out_stream) |
11610 | 0 | { |
11611 | 0 | char tab[2]; |
11612 | |
|
11613 | 0 | if (dev_dump_flag) { |
11614 | 0 | fprintf(stdout, "[DEV] Dump an image_header struct {\n"); |
11615 | 0 | tab[0] = '\0'; |
11616 | 0 | } else { |
11617 | 0 | fprintf(out_stream, "Image info {\n"); |
11618 | 0 | tab[0] = '\t'; |
11619 | 0 | tab[1] = '\0'; |
11620 | 0 | } |
11621 | |
|
11622 | 0 | fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0); |
11623 | 0 | fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, |
11624 | 0 | img_header->y1); |
11625 | 0 | fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps); |
11626 | |
|
11627 | 0 | if (img_header->comps) { |
11628 | 0 | OPJ_UINT32 compno; |
11629 | 0 | for (compno = 0; compno < img_header->numcomps; compno++) { |
11630 | 0 | fprintf(out_stream, "%s\t component %d {\n", tab, compno); |
11631 | 0 | j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, |
11632 | 0 | out_stream); |
11633 | 0 | fprintf(out_stream, "%s}\n", tab); |
11634 | 0 | } |
11635 | 0 | } |
11636 | |
|
11637 | 0 | fprintf(out_stream, "}\n"); |
11638 | 0 | } |
11639 | | |
11640 | | void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, |
11641 | | OPJ_BOOL dev_dump_flag, FILE* out_stream) |
11642 | 0 | { |
11643 | 0 | char tab[3]; |
11644 | |
|
11645 | 0 | if (dev_dump_flag) { |
11646 | 0 | fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); |
11647 | 0 | tab[0] = '\0'; |
11648 | 0 | } else { |
11649 | 0 | tab[0] = '\t'; |
11650 | 0 | tab[1] = '\t'; |
11651 | 0 | tab[2] = '\0'; |
11652 | 0 | } |
11653 | |
|
11654 | 0 | fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy); |
11655 | 0 | fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec); |
11656 | 0 | fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd); |
11657 | |
|
11658 | 0 | if (dev_dump_flag) { |
11659 | 0 | fprintf(out_stream, "}\n"); |
11660 | 0 | } |
11661 | 0 | } |
11662 | | |
11663 | | opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) |
11664 | 0 | { |
11665 | 0 | OPJ_UINT32 compno; |
11666 | 0 | OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps; |
11667 | 0 | opj_tcp_t *l_default_tile; |
11668 | 0 | opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1, |
11669 | 0 | sizeof(opj_codestream_info_v2_t)); |
11670 | 0 | if (!cstr_info) { |
11671 | 0 | return NULL; |
11672 | 0 | } |
11673 | | |
11674 | 0 | cstr_info->nbcomps = p_j2k->m_private_image->numcomps; |
11675 | |
|
11676 | 0 | cstr_info->tx0 = p_j2k->m_cp.tx0; |
11677 | 0 | cstr_info->ty0 = p_j2k->m_cp.ty0; |
11678 | 0 | cstr_info->tdx = p_j2k->m_cp.tdx; |
11679 | 0 | cstr_info->tdy = p_j2k->m_cp.tdy; |
11680 | 0 | cstr_info->tw = p_j2k->m_cp.tw; |
11681 | 0 | cstr_info->th = p_j2k->m_cp.th; |
11682 | |
|
11683 | 0 | cstr_info->tile_info = NULL; /* Not fill from the main header*/ |
11684 | |
|
11685 | 0 | l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
11686 | |
|
11687 | 0 | cstr_info->m_default_tile_info.csty = l_default_tile->csty; |
11688 | 0 | cstr_info->m_default_tile_info.prg = l_default_tile->prg; |
11689 | 0 | cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; |
11690 | 0 | cstr_info->m_default_tile_info.mct = l_default_tile->mct; |
11691 | |
|
11692 | 0 | cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc( |
11693 | 0 | cstr_info->nbcomps, sizeof(opj_tccp_info_t)); |
11694 | 0 | if (!cstr_info->m_default_tile_info.tccp_info) { |
11695 | 0 | opj_destroy_cstr_info(&cstr_info); |
11696 | 0 | return NULL; |
11697 | 0 | } |
11698 | | |
11699 | 0 | for (compno = 0; compno < numcomps; compno++) { |
11700 | 0 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
11701 | 0 | opj_tccp_info_t *l_tccp_info = & |
11702 | 0 | (cstr_info->m_default_tile_info.tccp_info[compno]); |
11703 | 0 | OPJ_INT32 bandno, numbands; |
11704 | | |
11705 | | /* coding style*/ |
11706 | 0 | l_tccp_info->csty = l_tccp->csty; |
11707 | 0 | l_tccp_info->numresolutions = l_tccp->numresolutions; |
11708 | 0 | l_tccp_info->cblkw = l_tccp->cblkw; |
11709 | 0 | l_tccp_info->cblkh = l_tccp->cblkh; |
11710 | 0 | l_tccp_info->cblksty = l_tccp->cblksty; |
11711 | 0 | l_tccp_info->qmfbid = l_tccp->qmfbid; |
11712 | 0 | if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) { |
11713 | 0 | memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); |
11714 | 0 | memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); |
11715 | 0 | } |
11716 | | |
11717 | | /* quantization style*/ |
11718 | 0 | l_tccp_info->qntsty = l_tccp->qntsty; |
11719 | 0 | l_tccp_info->numgbits = l_tccp->numgbits; |
11720 | |
|
11721 | 0 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
11722 | 0 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
11723 | 0 | if (numbands < OPJ_J2K_MAXBANDS) { |
11724 | 0 | for (bandno = 0; bandno < numbands; bandno++) { |
11725 | 0 | l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32) |
11726 | 0 | l_tccp->stepsizes[bandno].mant; |
11727 | 0 | l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32) |
11728 | 0 | l_tccp->stepsizes[bandno].expn; |
11729 | 0 | } |
11730 | 0 | } |
11731 | | |
11732 | | /* RGN value*/ |
11733 | 0 | l_tccp_info->roishift = l_tccp->roishift; |
11734 | 0 | } |
11735 | |
|
11736 | 0 | return cstr_info; |
11737 | 0 | } |
11738 | | |
11739 | | opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) |
11740 | 0 | { |
11741 | 0 | opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) |
11742 | 0 | opj_calloc(1, sizeof(opj_codestream_index_t)); |
11743 | 0 | if (!l_cstr_index) { |
11744 | 0 | return NULL; |
11745 | 0 | } |
11746 | | |
11747 | 0 | l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; |
11748 | 0 | l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; |
11749 | 0 | l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; |
11750 | |
|
11751 | 0 | l_cstr_index->marknum = p_j2k->cstr_index->marknum; |
11752 | 0 | l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum * |
11753 | 0 | sizeof(opj_marker_info_t)); |
11754 | 0 | if (!l_cstr_index->marker) { |
11755 | 0 | opj_free(l_cstr_index); |
11756 | 0 | return NULL; |
11757 | 0 | } |
11758 | | |
11759 | 0 | if (p_j2k->cstr_index->marker) { |
11760 | 0 | memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, |
11761 | 0 | l_cstr_index->marknum * sizeof(opj_marker_info_t)); |
11762 | 0 | } else { |
11763 | 0 | opj_free(l_cstr_index->marker); |
11764 | 0 | l_cstr_index->marker = NULL; |
11765 | 0 | } |
11766 | |
|
11767 | 0 | l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; |
11768 | 0 | l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
11769 | 0 | l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
11770 | 0 | if (!l_cstr_index->tile_index) { |
11771 | 0 | opj_free(l_cstr_index->marker); |
11772 | 0 | opj_free(l_cstr_index); |
11773 | 0 | return NULL; |
11774 | 0 | } |
11775 | | |
11776 | 0 | if (!p_j2k->cstr_index->tile_index) { |
11777 | 0 | opj_free(l_cstr_index->tile_index); |
11778 | 0 | l_cstr_index->tile_index = NULL; |
11779 | 0 | } else { |
11780 | 0 | OPJ_UINT32 it_tile = 0; |
11781 | 0 | for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) { |
11782 | | |
11783 | | /* Tile Marker*/ |
11784 | 0 | l_cstr_index->tile_index[it_tile].marknum = |
11785 | 0 | p_j2k->cstr_index->tile_index[it_tile].marknum; |
11786 | |
|
11787 | 0 | l_cstr_index->tile_index[it_tile].marker = |
11788 | 0 | (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum * |
11789 | 0 | sizeof(opj_marker_info_t)); |
11790 | |
|
11791 | 0 | if (!l_cstr_index->tile_index[it_tile].marker) { |
11792 | 0 | OPJ_UINT32 it_tile_free; |
11793 | |
|
11794 | 0 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
11795 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
11796 | 0 | } |
11797 | |
|
11798 | 0 | opj_free(l_cstr_index->tile_index); |
11799 | 0 | opj_free(l_cstr_index->marker); |
11800 | 0 | opj_free(l_cstr_index); |
11801 | 0 | return NULL; |
11802 | 0 | } |
11803 | | |
11804 | 0 | if (p_j2k->cstr_index->tile_index[it_tile].marker) |
11805 | 0 | memcpy(l_cstr_index->tile_index[it_tile].marker, |
11806 | 0 | p_j2k->cstr_index->tile_index[it_tile].marker, |
11807 | 0 | l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t)); |
11808 | 0 | else { |
11809 | 0 | opj_free(l_cstr_index->tile_index[it_tile].marker); |
11810 | 0 | l_cstr_index->tile_index[it_tile].marker = NULL; |
11811 | 0 | } |
11812 | | |
11813 | | /* Tile part index*/ |
11814 | 0 | l_cstr_index->tile_index[it_tile].nb_tps = |
11815 | 0 | p_j2k->cstr_index->tile_index[it_tile].nb_tps; |
11816 | |
|
11817 | 0 | l_cstr_index->tile_index[it_tile].tp_index = |
11818 | 0 | (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof( |
11819 | 0 | opj_tp_index_t)); |
11820 | |
|
11821 | 0 | if (!l_cstr_index->tile_index[it_tile].tp_index) { |
11822 | 0 | OPJ_UINT32 it_tile_free; |
11823 | |
|
11824 | 0 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
11825 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
11826 | 0 | opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); |
11827 | 0 | } |
11828 | |
|
11829 | 0 | opj_free(l_cstr_index->tile_index); |
11830 | 0 | opj_free(l_cstr_index->marker); |
11831 | 0 | opj_free(l_cstr_index); |
11832 | 0 | return NULL; |
11833 | 0 | } |
11834 | | |
11835 | 0 | if (p_j2k->cstr_index->tile_index[it_tile].tp_index) { |
11836 | 0 | memcpy(l_cstr_index->tile_index[it_tile].tp_index, |
11837 | 0 | p_j2k->cstr_index->tile_index[it_tile].tp_index, |
11838 | 0 | l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t)); |
11839 | 0 | } else { |
11840 | 0 | opj_free(l_cstr_index->tile_index[it_tile].tp_index); |
11841 | 0 | l_cstr_index->tile_index[it_tile].tp_index = NULL; |
11842 | 0 | } |
11843 | | |
11844 | | /* Packet index (NOT USED)*/ |
11845 | 0 | l_cstr_index->tile_index[it_tile].nb_packet = 0; |
11846 | 0 | l_cstr_index->tile_index[it_tile].packet_index = NULL; |
11847 | |
|
11848 | 0 | } |
11849 | 0 | } |
11850 | | |
11851 | 0 | return l_cstr_index; |
11852 | 0 | } |
11853 | | |
11854 | | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) |
11855 | 8.50k | { |
11856 | 8.50k | OPJ_UINT32 it_tile = 0; |
11857 | | |
11858 | 8.50k | p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
11859 | 8.50k | p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
11860 | 8.50k | p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
11861 | 8.50k | if (!p_j2k->cstr_index->tile_index) { |
11862 | 0 | return OPJ_FALSE; |
11863 | 0 | } |
11864 | | |
11865 | 18.9M | for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) { |
11866 | 18.9M | p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; |
11867 | 18.9M | p_j2k->cstr_index->tile_index[it_tile].marknum = 0; |
11868 | 18.9M | p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) |
11869 | 18.9M | opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, |
11870 | 18.9M | sizeof(opj_marker_info_t)); |
11871 | 18.9M | if (!p_j2k->cstr_index->tile_index[it_tile].marker) { |
11872 | 0 | return OPJ_FALSE; |
11873 | 0 | } |
11874 | 18.9M | } |
11875 | | |
11876 | 8.50k | return OPJ_TRUE; |
11877 | 8.50k | } |
11878 | | |
11879 | | static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k, |
11880 | | opj_event_mgr_t * p_manager) |
11881 | 5.64k | { |
11882 | 5.64k | OPJ_UINT32 compno; |
11883 | 5.64k | OPJ_BOOL decoded_all_used_components = OPJ_TRUE; |
11884 | | |
11885 | 5.64k | if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) { |
11886 | 0 | for (compno = 0; |
11887 | 0 | compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { |
11888 | 0 | OPJ_UINT32 dec_compno = |
11889 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; |
11890 | 0 | if (p_j2k->m_output_image->comps[dec_compno].data == NULL) { |
11891 | 0 | opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", |
11892 | 0 | dec_compno); |
11893 | 0 | decoded_all_used_components = OPJ_FALSE; |
11894 | 0 | } |
11895 | 0 | } |
11896 | 5.64k | } else { |
11897 | 19.7k | for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) { |
11898 | 14.1k | if (p_j2k->m_output_image->comps[compno].data == NULL) { |
11899 | 2.91k | opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", |
11900 | 2.91k | compno); |
11901 | 2.91k | decoded_all_used_components = OPJ_FALSE; |
11902 | 2.91k | } |
11903 | 14.1k | } |
11904 | 5.64k | } |
11905 | | |
11906 | 5.64k | if (decoded_all_used_components == OPJ_FALSE) { |
11907 | 96 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n"); |
11908 | 96 | return OPJ_FALSE; |
11909 | 96 | } |
11910 | | |
11911 | 5.54k | return OPJ_TRUE; |
11912 | 5.64k | } |
11913 | | |
11914 | | static int CompareOffT(const void* a, const void* b) |
11915 | 0 | { |
11916 | 0 | const OPJ_OFF_T offA = *(const OPJ_OFF_T*)a; |
11917 | 0 | const OPJ_OFF_T offB = *(const OPJ_OFF_T*)b; |
11918 | 0 | if (offA < offB) { |
11919 | 0 | return -1; |
11920 | 0 | } |
11921 | 0 | if (offA == offB) { |
11922 | 0 | return 0; |
11923 | 0 | } |
11924 | 0 | return 1; |
11925 | 0 | } |
11926 | | |
11927 | | static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
11928 | | opj_stream_private_t *p_stream, |
11929 | | opj_event_mgr_t * p_manager) |
11930 | 7.44k | { |
11931 | 7.44k | OPJ_BOOL l_go_on = OPJ_TRUE; |
11932 | 7.44k | OPJ_UINT32 l_current_tile_no; |
11933 | 7.44k | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
11934 | 7.44k | OPJ_UINT32 l_nb_comps; |
11935 | 7.44k | OPJ_UINT32 nr_tiles = 0; |
11936 | 7.44k | OPJ_OFF_T end_pos = 0; |
11937 | | |
11938 | | /* Particular case for whole single tile decoding */ |
11939 | | /* We can avoid allocating intermediate tile buffers */ |
11940 | 7.44k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
11941 | 7.44k | p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 && |
11942 | 7.44k | p_j2k->m_output_image->x0 == 0 && |
11943 | 7.44k | p_j2k->m_output_image->y0 == 0 && |
11944 | 7.44k | p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx && |
11945 | 7.44k | p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) { |
11946 | 8 | OPJ_UINT32 i; |
11947 | 8 | if (! opj_j2k_read_tile_header(p_j2k, |
11948 | 8 | &l_current_tile_no, |
11949 | 8 | NULL, |
11950 | 8 | &l_tile_x0, &l_tile_y0, |
11951 | 8 | &l_tile_x1, &l_tile_y1, |
11952 | 8 | &l_nb_comps, |
11953 | 8 | &l_go_on, |
11954 | 8 | p_stream, |
11955 | 8 | p_manager)) { |
11956 | 1 | return OPJ_FALSE; |
11957 | 1 | } |
11958 | | |
11959 | 7 | if (!l_go_on || |
11960 | 7 | ! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
11961 | 5 | p_stream, p_manager)) { |
11962 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n"); |
11963 | 3 | return OPJ_FALSE; |
11964 | 3 | } |
11965 | | |
11966 | | /* Transfer TCD data to output image data */ |
11967 | 523 | for (i = 0; i < p_j2k->m_output_image->numcomps; i++) { |
11968 | 519 | opj_image_data_free(p_j2k->m_output_image->comps[i].data); |
11969 | 519 | p_j2k->m_output_image->comps[i].data = |
11970 | 519 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data; |
11971 | 519 | p_j2k->m_output_image->comps[i].resno_decoded = |
11972 | 519 | p_j2k->m_tcd->image->comps[i].resno_decoded; |
11973 | 519 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL; |
11974 | 519 | } |
11975 | | |
11976 | 4 | return OPJ_TRUE; |
11977 | 7 | } |
11978 | | |
11979 | 7.43k | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts = 0; |
11980 | 7.43k | p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts = 0; |
11981 | 7.43k | opj_free(p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset); |
11982 | 7.43k | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset = NULL; |
11983 | | |
11984 | | /* If the area to decode only intersects a subset of tiles, and we have |
11985 | | * valid TLM information, then use it to plan the tilepart offsets to |
11986 | | * seek to. |
11987 | | */ |
11988 | 7.43k | if (!(p_j2k->m_specific_param.m_decoder.m_start_tile_x == 0 && |
11989 | 7.43k | p_j2k->m_specific_param.m_decoder.m_start_tile_y == 0 && |
11990 | 7.43k | p_j2k->m_specific_param.m_decoder.m_end_tile_x == p_j2k->m_cp.tw && |
11991 | 7.43k | p_j2k->m_specific_param.m_decoder.m_end_tile_y == p_j2k->m_cp.th) && |
11992 | 7.43k | !p_j2k->m_specific_param.m_decoder.m_tlm.m_is_invalid && |
11993 | 7.43k | opj_stream_has_seek(p_stream)) { |
11994 | 0 | OPJ_UINT32 m_num_intersecting_tile_parts = 0; |
11995 | |
|
11996 | 0 | OPJ_UINT32 j; |
11997 | 0 | for (j = 0; j < p_j2k->m_cp.tw * p_j2k->m_cp.th; ++j) { |
11998 | 0 | if (p_j2k->cstr_index->tile_index[j].nb_tps > 0 && |
11999 | 0 | p_j2k->cstr_index->tile_index[j].tp_index[ |
12000 | 0 | p_j2k->cstr_index->tile_index[j].nb_tps - 1].end_pos > end_pos) { |
12001 | 0 | end_pos = p_j2k->cstr_index->tile_index[j].tp_index[ |
12002 | 0 | p_j2k->cstr_index->tile_index[j].nb_tps - 1].end_pos; |
12003 | 0 | } |
12004 | 0 | } |
12005 | |
|
12006 | 0 | for (j = p_j2k->m_specific_param.m_decoder.m_start_tile_y; |
12007 | 0 | j < p_j2k->m_specific_param.m_decoder.m_end_tile_y; ++j) { |
12008 | 0 | OPJ_UINT32 i; |
12009 | 0 | for (i = p_j2k->m_specific_param.m_decoder.m_start_tile_x; |
12010 | 0 | i < p_j2k->m_specific_param.m_decoder.m_end_tile_x; ++i) { |
12011 | 0 | const OPJ_UINT32 tile_number = j * p_j2k->m_cp.tw + i; |
12012 | 0 | m_num_intersecting_tile_parts += |
12013 | 0 | p_j2k->cstr_index->tile_index[tile_number].nb_tps; |
12014 | 0 | } |
12015 | 0 | } |
12016 | |
|
12017 | 0 | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset = |
12018 | 0 | (OPJ_OFF_T*) |
12019 | 0 | opj_malloc(m_num_intersecting_tile_parts * sizeof(OPJ_OFF_T)); |
12020 | 0 | if (m_num_intersecting_tile_parts > 0 && |
12021 | 0 | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset) { |
12022 | 0 | OPJ_UINT32 idx = 0; |
12023 | 0 | for (j = p_j2k->m_specific_param.m_decoder.m_start_tile_y; |
12024 | 0 | j < p_j2k->m_specific_param.m_decoder.m_end_tile_y; ++j) { |
12025 | 0 | OPJ_UINT32 i; |
12026 | 0 | for (i = p_j2k->m_specific_param.m_decoder.m_start_tile_x; |
12027 | 0 | i < p_j2k->m_specific_param.m_decoder.m_end_tile_x; ++i) { |
12028 | 0 | const OPJ_UINT32 tile_number = j * p_j2k->m_cp.tw + i; |
12029 | 0 | OPJ_UINT32 k; |
12030 | 0 | for (k = 0; k < p_j2k->cstr_index->tile_index[tile_number].nb_tps; ++k) { |
12031 | 0 | const OPJ_OFF_T next_tp_sot_pos = |
12032 | 0 | p_j2k->cstr_index->tile_index[tile_number].tp_index[k].start_pos; |
12033 | 0 | p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset[idx] = |
12034 | 0 | next_tp_sot_pos; |
12035 | 0 | ++idx; |
12036 | 0 | } |
12037 | 0 | } |
12038 | 0 | } |
12039 | |
|
12040 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts = idx; |
12041 | | |
12042 | | /* Sort by increasing offset */ |
12043 | 0 | qsort(p_j2k->m_specific_param.m_decoder.m_intersecting_tile_parts_offset, |
12044 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts, |
12045 | 0 | sizeof(OPJ_OFF_T), |
12046 | 0 | CompareOffT); |
12047 | 0 | } |
12048 | 0 | } |
12049 | | |
12050 | 9.29k | for (;;) { |
12051 | 9.29k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
12052 | 9.29k | p_j2k->m_cp.tcps[0].m_data != NULL) { |
12053 | 0 | l_current_tile_no = 0; |
12054 | 0 | p_j2k->m_current_tile_number = 0; |
12055 | 0 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
12056 | 9.29k | } else { |
12057 | 9.29k | if (! opj_j2k_read_tile_header(p_j2k, |
12058 | 9.29k | &l_current_tile_no, |
12059 | 9.29k | NULL, |
12060 | 9.29k | &l_tile_x0, &l_tile_y0, |
12061 | 9.29k | &l_tile_x1, &l_tile_y1, |
12062 | 9.29k | &l_nb_comps, |
12063 | 9.29k | &l_go_on, |
12064 | 9.29k | p_stream, |
12065 | 9.29k | p_manager)) { |
12066 | 651 | return OPJ_FALSE; |
12067 | 651 | } |
12068 | | |
12069 | 8.64k | if (! l_go_on) { |
12070 | 59 | break; |
12071 | 59 | } |
12072 | 8.64k | } |
12073 | | |
12074 | 8.58k | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
12075 | 8.58k | p_stream, p_manager)) { |
12076 | 1.11k | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", |
12077 | 1.11k | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
12078 | 1.11k | return OPJ_FALSE; |
12079 | 1.11k | } |
12080 | | |
12081 | 7.46k | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", |
12082 | 7.46k | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
12083 | | |
12084 | 7.46k | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
12085 | 7.46k | p_j2k->m_output_image)) { |
12086 | 29 | return OPJ_FALSE; |
12087 | 29 | } |
12088 | | |
12089 | 7.43k | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
12090 | 7.43k | !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 && |
12091 | 3.02k | p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 && |
12092 | 3.02k | p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 && |
12093 | 3.02k | p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) { |
12094 | | /* Keep current tcp data */ |
12095 | 6.23k | } else { |
12096 | 6.23k | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
12097 | 6.23k | } |
12098 | | |
12099 | 7.43k | opj_event_msg(p_manager, EVT_INFO, |
12100 | 7.43k | "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
12101 | | |
12102 | 7.43k | if (opj_stream_get_number_byte_left(p_stream) == 0 |
12103 | 7.43k | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
12104 | 5.57k | break; |
12105 | 5.57k | } |
12106 | 1.86k | if (++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) { |
12107 | 14 | break; |
12108 | 14 | } |
12109 | 1.85k | if (p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts > 0 && |
12110 | 1.85k | p_j2k->m_specific_param.m_decoder.m_idx_intersecting_tile_parts == |
12111 | 0 | p_j2k->m_specific_param.m_decoder.m_num_intersecting_tile_parts) { |
12112 | 0 | opj_stream_seek(p_stream, end_pos + 2, p_manager); |
12113 | 0 | break; |
12114 | 0 | } |
12115 | 1.85k | } |
12116 | | |
12117 | 5.64k | if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { |
12118 | 96 | return OPJ_FALSE; |
12119 | 96 | } |
12120 | | |
12121 | 5.54k | return OPJ_TRUE; |
12122 | 5.64k | } |
12123 | | |
12124 | | /** |
12125 | | * Sets up the procedures to do on decoding data. Developers wanting to extend the library can add their own reading procedures. |
12126 | | */ |
12127 | | static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k, |
12128 | | opj_event_mgr_t * p_manager) |
12129 | 7.44k | { |
12130 | | /* preconditions*/ |
12131 | 7.44k | assert(p_j2k != 00); |
12132 | 7.44k | assert(p_manager != 00); |
12133 | | |
12134 | 7.44k | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12135 | 7.44k | (opj_procedure)opj_j2k_decode_tiles, p_manager)) { |
12136 | 0 | return OPJ_FALSE; |
12137 | 0 | } |
12138 | | /* DEVELOPER CORNER, add your custom procedures */ |
12139 | | |
12140 | 7.44k | return OPJ_TRUE; |
12141 | 7.44k | } |
12142 | | |
12143 | | /* |
12144 | | * Read and decode one tile. |
12145 | | */ |
12146 | | static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k, |
12147 | | opj_stream_private_t *p_stream, |
12148 | | opj_event_mgr_t * p_manager) |
12149 | 0 | { |
12150 | 0 | OPJ_BOOL l_go_on = OPJ_TRUE; |
12151 | 0 | OPJ_UINT32 l_current_tile_no; |
12152 | 0 | OPJ_UINT32 l_tile_no_to_dec; |
12153 | 0 | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
12154 | 0 | OPJ_UINT32 l_nb_comps; |
12155 | 0 | OPJ_UINT32 l_nb_tiles; |
12156 | 0 | OPJ_UINT32 i; |
12157 | | |
12158 | | /* Move into the codestream to the first SOT used to decode the desired tile */ |
12159 | 0 | l_tile_no_to_dec = (OPJ_UINT32) |
12160 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; |
12161 | 0 | if (p_j2k->cstr_index->tile_index) |
12162 | 0 | if (p_j2k->cstr_index->tile_index->tp_index) { |
12163 | 0 | if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { |
12164 | | /* the index for this tile has not been built, |
12165 | | * so move to the last SOT read */ |
12166 | 0 | if (!(opj_stream_read_seek(p_stream, |
12167 | 0 | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) { |
12168 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
12169 | 0 | return OPJ_FALSE; |
12170 | 0 | } |
12171 | 0 | } else { |
12172 | 0 | OPJ_OFF_T sot_pos = |
12173 | 0 | p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos; |
12174 | 0 | OPJ_UINT32 l_marker; |
12175 | |
|
12176 | | #if 0 |
12177 | | opj_event_msg(p_manager, EVT_INFO, |
12178 | | "opj_j2k_decode_one_tile(%u): seek to %" PRId64 "\n", |
12179 | | l_tile_no_to_dec, |
12180 | | sot_pos); |
12181 | | #endif |
12182 | 0 | if (!(opj_stream_read_seek(p_stream, |
12183 | 0 | sot_pos, |
12184 | 0 | p_manager))) { |
12185 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
12186 | 0 | return OPJ_FALSE; |
12187 | 0 | } |
12188 | | |
12189 | | /* Try to read 2 bytes (the marker ID) from stream and copy them into the buffer */ |
12190 | 0 | if (opj_stream_read_data(p_stream, |
12191 | 0 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
12192 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
12193 | 0 | return OPJ_FALSE; |
12194 | 0 | } |
12195 | | |
12196 | | /* Read 2 bytes from the buffer as the marker ID */ |
12197 | 0 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker, |
12198 | 0 | 2); |
12199 | |
|
12200 | 0 | if (l_marker != J2K_MS_SOT) { |
12201 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Did not get expected SOT marker\n"); |
12202 | 0 | return OPJ_FALSE; |
12203 | 0 | } |
12204 | 0 | } |
12205 | | /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ |
12206 | 0 | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
12207 | 0 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
12208 | 0 | } |
12209 | 0 | } |
12210 | | |
12211 | | /* Reset current tile part number for all tiles, and not only the one */ |
12212 | | /* of interest. */ |
12213 | | /* Not completely sure this is always correct but required for */ |
12214 | | /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */ |
12215 | 0 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
12216 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
12217 | 0 | p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1; |
12218 | 0 | } |
12219 | |
|
12220 | 0 | for (;;) { |
12221 | 0 | if (! opj_j2k_read_tile_header(p_j2k, |
12222 | 0 | &l_current_tile_no, |
12223 | 0 | NULL, |
12224 | 0 | &l_tile_x0, &l_tile_y0, |
12225 | 0 | &l_tile_x1, &l_tile_y1, |
12226 | 0 | &l_nb_comps, |
12227 | 0 | &l_go_on, |
12228 | 0 | p_stream, |
12229 | 0 | p_manager)) { |
12230 | 0 | return OPJ_FALSE; |
12231 | 0 | } |
12232 | | |
12233 | 0 | if (! l_go_on) { |
12234 | 0 | break; |
12235 | 0 | } |
12236 | | |
12237 | 0 | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
12238 | 0 | p_stream, p_manager)) { |
12239 | 0 | return OPJ_FALSE; |
12240 | 0 | } |
12241 | 0 | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", |
12242 | 0 | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
12243 | |
|
12244 | 0 | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
12245 | 0 | p_j2k->m_output_image)) { |
12246 | 0 | return OPJ_FALSE; |
12247 | 0 | } |
12248 | 0 | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
12249 | |
|
12250 | 0 | opj_event_msg(p_manager, EVT_INFO, |
12251 | 0 | "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
12252 | |
|
12253 | 0 | if (l_current_tile_no == l_tile_no_to_dec) { |
12254 | | /* move into the codestream to the first SOT (FIXME or not move?)*/ |
12255 | 0 | if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, |
12256 | 0 | p_manager))) { |
12257 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
12258 | 0 | return OPJ_FALSE; |
12259 | 0 | } |
12260 | 0 | break; |
12261 | 0 | } else { |
12262 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
12263 | 0 | "Tile read, decoded and updated is not the desired one (%d vs %d).\n", |
12264 | 0 | l_current_tile_no + 1, l_tile_no_to_dec + 1); |
12265 | 0 | } |
12266 | |
|
12267 | 0 | } |
12268 | | |
12269 | 0 | if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { |
12270 | 0 | return OPJ_FALSE; |
12271 | 0 | } |
12272 | | |
12273 | 0 | return OPJ_TRUE; |
12274 | 0 | } |
12275 | | |
12276 | | /** |
12277 | | * Sets up the procedures to do on decoding one tile. Developers wanting to extend the library can add their own reading procedures. |
12278 | | */ |
12279 | | static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k, |
12280 | | opj_event_mgr_t * p_manager) |
12281 | 0 | { |
12282 | | /* preconditions*/ |
12283 | 0 | assert(p_j2k != 00); |
12284 | 0 | assert(p_manager != 00); |
12285 | |
|
12286 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
12287 | 0 | (opj_procedure)opj_j2k_decode_one_tile, p_manager)) { |
12288 | 0 | return OPJ_FALSE; |
12289 | 0 | } |
12290 | | /* DEVELOPER CORNER, add your custom procedures */ |
12291 | | |
12292 | 0 | return OPJ_TRUE; |
12293 | 0 | } |
12294 | | |
12295 | | static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k, |
12296 | | opj_image_t * p_image) |
12297 | 5.55k | { |
12298 | 5.55k | OPJ_UINT32 compno; |
12299 | | |
12300 | | /* Move data and copy one information from codec to output image*/ |
12301 | 5.55k | if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) { |
12302 | 0 | opj_image_comp_t* newcomps = |
12303 | 0 | (opj_image_comp_t*) opj_malloc( |
12304 | 0 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode * |
12305 | 0 | sizeof(opj_image_comp_t)); |
12306 | 0 | if (newcomps == NULL) { |
12307 | 0 | opj_image_destroy(p_j2k->m_private_image); |
12308 | 0 | p_j2k->m_private_image = NULL; |
12309 | 0 | return OPJ_FALSE; |
12310 | 0 | } |
12311 | 0 | for (compno = 0; compno < p_image->numcomps; compno++) { |
12312 | 0 | opj_image_data_free(p_image->comps[compno].data); |
12313 | 0 | p_image->comps[compno].data = NULL; |
12314 | 0 | } |
12315 | 0 | for (compno = 0; |
12316 | 0 | compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { |
12317 | 0 | OPJ_UINT32 src_compno = |
12318 | 0 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; |
12319 | 0 | memcpy(&(newcomps[compno]), |
12320 | 0 | &(p_j2k->m_output_image->comps[src_compno]), |
12321 | 0 | sizeof(opj_image_comp_t)); |
12322 | 0 | newcomps[compno].resno_decoded = |
12323 | 0 | p_j2k->m_output_image->comps[src_compno].resno_decoded; |
12324 | 0 | newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data; |
12325 | 0 | p_j2k->m_output_image->comps[src_compno].data = NULL; |
12326 | 0 | } |
12327 | 0 | for (compno = 0; compno < p_image->numcomps; compno++) { |
12328 | 0 | assert(p_j2k->m_output_image->comps[compno].data == NULL); |
12329 | 0 | opj_image_data_free(p_j2k->m_output_image->comps[compno].data); |
12330 | 0 | p_j2k->m_output_image->comps[compno].data = NULL; |
12331 | 0 | } |
12332 | 0 | p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; |
12333 | 0 | opj_free(p_image->comps); |
12334 | 0 | p_image->comps = newcomps; |
12335 | 5.55k | } else { |
12336 | 13.9k | for (compno = 0; compno < p_image->numcomps; compno++) { |
12337 | 8.44k | p_image->comps[compno].resno_decoded = |
12338 | 8.44k | p_j2k->m_output_image->comps[compno].resno_decoded; |
12339 | 8.44k | opj_image_data_free(p_image->comps[compno].data); |
12340 | 8.44k | p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; |
12341 | | #if 0 |
12342 | | char fn[256]; |
12343 | | snprintf(fn, sizeof fn, "/tmp/%d.raw", compno); |
12344 | | FILE *debug = fopen(fn, "wb"); |
12345 | | fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32), |
12346 | | p_image->comps[compno].w * p_image->comps[compno].h, debug); |
12347 | | fclose(debug); |
12348 | | #endif |
12349 | 8.44k | p_j2k->m_output_image->comps[compno].data = NULL; |
12350 | 8.44k | } |
12351 | 5.55k | } |
12352 | 5.55k | return OPJ_TRUE; |
12353 | 5.55k | } |
12354 | | |
12355 | | OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, |
12356 | | opj_stream_private_t * p_stream, |
12357 | | opj_image_t * p_image, |
12358 | | opj_event_mgr_t * p_manager) |
12359 | 7.44k | { |
12360 | 7.44k | if (!p_image) { |
12361 | 0 | return OPJ_FALSE; |
12362 | 0 | } |
12363 | | |
12364 | | /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */ |
12365 | | /* and finally opj_decode_image() without manual setting of comps[].factor */ |
12366 | | /* We could potentially always execute it, if we don't allow people to do */ |
12367 | | /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */ |
12368 | 7.44k | if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 && |
12369 | 7.44k | p_j2k->m_private_image != NULL && |
12370 | 7.44k | p_j2k->m_private_image->numcomps > 0 && |
12371 | 7.44k | p_j2k->m_private_image->comps[0].factor == |
12372 | 0 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce && |
12373 | 7.44k | p_image->numcomps > 0 && |
12374 | 7.44k | p_image->comps[0].factor == 0 && |
12375 | | /* Don't mess with image dimension if the user has allocated it */ |
12376 | 7.44k | p_image->comps[0].data == NULL) { |
12377 | 0 | OPJ_UINT32 it_comp; |
12378 | | |
12379 | | /* Update the comps[].factor member of the output image with the one */ |
12380 | | /* of m_reduce */ |
12381 | 0 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
12382 | 0 | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
12383 | 0 | } |
12384 | 0 | if (!opj_j2k_update_image_dimensions(p_image, p_manager)) { |
12385 | 0 | return OPJ_FALSE; |
12386 | 0 | } |
12387 | 0 | } |
12388 | | |
12389 | 7.44k | if (p_j2k->m_output_image == NULL) { |
12390 | 7.44k | p_j2k->m_output_image = opj_image_create0(); |
12391 | 7.44k | if (!(p_j2k->m_output_image)) { |
12392 | 0 | return OPJ_FALSE; |
12393 | 0 | } |
12394 | 7.44k | } |
12395 | 7.44k | opj_copy_image_header(p_image, p_j2k->m_output_image); |
12396 | | |
12397 | | /* customization of the decoding */ |
12398 | 7.44k | if (!opj_j2k_setup_decoding(p_j2k, p_manager)) { |
12399 | 0 | return OPJ_FALSE; |
12400 | 0 | } |
12401 | | |
12402 | | /* Decode the codestream */ |
12403 | 7.44k | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12404 | 1.89k | opj_image_destroy(p_j2k->m_private_image); |
12405 | 1.89k | p_j2k->m_private_image = NULL; |
12406 | 1.89k | return OPJ_FALSE; |
12407 | 1.89k | } |
12408 | | |
12409 | | /* Move data and copy one information from codec to output image*/ |
12410 | 5.55k | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
12411 | 7.44k | } |
12412 | | |
12413 | | OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k, |
12414 | | opj_stream_private_t *p_stream, |
12415 | | opj_image_t* p_image, |
12416 | | opj_event_mgr_t * p_manager, |
12417 | | OPJ_UINT32 tile_index) |
12418 | 0 | { |
12419 | 0 | OPJ_UINT32 compno; |
12420 | 0 | OPJ_UINT32 l_tile_x, l_tile_y; |
12421 | 0 | opj_image_comp_t* l_img_comp; |
12422 | |
|
12423 | 0 | if (!p_image) { |
12424 | 0 | opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n"); |
12425 | 0 | return OPJ_FALSE; |
12426 | 0 | } |
12427 | | |
12428 | 0 | if (p_image->numcomps < p_j2k->m_private_image->numcomps) { |
12429 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12430 | 0 | "Image has less components than codestream.\n"); |
12431 | 0 | return OPJ_FALSE; |
12432 | 0 | } |
12433 | | |
12434 | 0 | if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) { |
12435 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12436 | 0 | "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, |
12437 | 0 | (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); |
12438 | 0 | return OPJ_FALSE; |
12439 | 0 | } |
12440 | | |
12441 | | /* Compute the dimension of the desired tile*/ |
12442 | 0 | l_tile_x = tile_index % p_j2k->m_cp.tw; |
12443 | 0 | l_tile_y = tile_index / p_j2k->m_cp.tw; |
12444 | |
|
12445 | 0 | p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
12446 | 0 | if (p_image->x0 < p_j2k->m_private_image->x0) { |
12447 | 0 | p_image->x0 = p_j2k->m_private_image->x0; |
12448 | 0 | } |
12449 | 0 | p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
12450 | 0 | if (p_image->x1 > p_j2k->m_private_image->x1) { |
12451 | 0 | p_image->x1 = p_j2k->m_private_image->x1; |
12452 | 0 | } |
12453 | |
|
12454 | 0 | p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
12455 | 0 | if (p_image->y0 < p_j2k->m_private_image->y0) { |
12456 | 0 | p_image->y0 = p_j2k->m_private_image->y0; |
12457 | 0 | } |
12458 | 0 | p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
12459 | 0 | if (p_image->y1 > p_j2k->m_private_image->y1) { |
12460 | 0 | p_image->y1 = p_j2k->m_private_image->y1; |
12461 | 0 | } |
12462 | |
|
12463 | 0 | l_img_comp = p_image->comps; |
12464 | 0 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
12465 | 0 | OPJ_INT32 l_comp_x1, l_comp_y1; |
12466 | |
|
12467 | 0 | l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; |
12468 | |
|
12469 | 0 | l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx); |
12470 | 0 | l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy); |
12471 | 0 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
12472 | 0 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
12473 | |
|
12474 | 0 | l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, |
12475 | 0 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, |
12476 | 0 | (OPJ_INT32)l_img_comp->factor)); |
12477 | 0 | l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, |
12478 | 0 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, |
12479 | 0 | (OPJ_INT32)l_img_comp->factor)); |
12480 | |
|
12481 | 0 | l_img_comp++; |
12482 | 0 | } |
12483 | |
|
12484 | 0 | if (p_image->numcomps > p_j2k->m_private_image->numcomps) { |
12485 | | /* Can happen when calling repeatdly opj_get_decoded_tile() on an |
12486 | | * image with a color palette, where color palette expansion is done |
12487 | | * later in jp2.c */ |
12488 | 0 | for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps; |
12489 | 0 | ++compno) { |
12490 | 0 | opj_image_data_free(p_image->comps[compno].data); |
12491 | 0 | p_image->comps[compno].data = NULL; |
12492 | 0 | } |
12493 | 0 | p_image->numcomps = p_j2k->m_private_image->numcomps; |
12494 | 0 | } |
12495 | | |
12496 | | /* Destroy the previous output image*/ |
12497 | 0 | if (p_j2k->m_output_image) { |
12498 | 0 | opj_image_destroy(p_j2k->m_output_image); |
12499 | 0 | } |
12500 | | |
12501 | | /* Create the output image from the information previously computed*/ |
12502 | 0 | p_j2k->m_output_image = opj_image_create0(); |
12503 | 0 | if (!(p_j2k->m_output_image)) { |
12504 | 0 | return OPJ_FALSE; |
12505 | 0 | } |
12506 | 0 | opj_copy_image_header(p_image, p_j2k->m_output_image); |
12507 | |
|
12508 | 0 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index; |
12509 | | |
12510 | | /* customization of the decoding */ |
12511 | 0 | if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) { |
12512 | 0 | return OPJ_FALSE; |
12513 | 0 | } |
12514 | | |
12515 | | /* Decode the codestream */ |
12516 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12517 | 0 | opj_image_destroy(p_j2k->m_private_image); |
12518 | 0 | p_j2k->m_private_image = NULL; |
12519 | 0 | return OPJ_FALSE; |
12520 | 0 | } |
12521 | | |
12522 | | /* Move data and copy one information from codec to output image*/ |
12523 | 0 | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
12524 | 0 | } |
12525 | | |
12526 | | OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, |
12527 | | OPJ_UINT32 res_factor, |
12528 | | opj_event_mgr_t * p_manager) |
12529 | 0 | { |
12530 | 0 | OPJ_UINT32 it_comp; |
12531 | |
|
12532 | 0 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; |
12533 | |
|
12534 | 0 | if (p_j2k->m_private_image) { |
12535 | 0 | if (p_j2k->m_private_image->comps) { |
12536 | 0 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { |
12537 | 0 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { |
12538 | 0 | for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { |
12539 | 0 | OPJ_UINT32 max_res = |
12540 | 0 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; |
12541 | 0 | if (res_factor >= max_res) { |
12542 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12543 | 0 | "Resolution factor is greater than the maximum resolution in the component.\n"); |
12544 | 0 | return OPJ_FALSE; |
12545 | 0 | } |
12546 | 0 | p_j2k->m_private_image->comps[it_comp].factor = res_factor; |
12547 | 0 | } |
12548 | 0 | return OPJ_TRUE; |
12549 | 0 | } |
12550 | 0 | } |
12551 | 0 | } |
12552 | 0 | } |
12553 | | |
12554 | 0 | return OPJ_FALSE; |
12555 | 0 | } |
12556 | | |
12557 | | /* ----------------------------------------------------------------------- */ |
12558 | | |
12559 | | OPJ_BOOL opj_j2k_encoder_set_extra_options( |
12560 | | opj_j2k_t *p_j2k, |
12561 | | const char* const* p_options, |
12562 | | opj_event_mgr_t * p_manager) |
12563 | 0 | { |
12564 | 0 | const char* const* p_option_iter; |
12565 | |
|
12566 | 0 | if (p_options == NULL) { |
12567 | 0 | return OPJ_TRUE; |
12568 | 0 | } |
12569 | | |
12570 | 0 | for (p_option_iter = p_options; *p_option_iter != NULL; ++p_option_iter) { |
12571 | 0 | if (strncmp(*p_option_iter, "PLT=", 4) == 0) { |
12572 | 0 | if (strcmp(*p_option_iter, "PLT=YES") == 0) { |
12573 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_TRUE; |
12574 | 0 | } else if (strcmp(*p_option_iter, "PLT=NO") == 0) { |
12575 | 0 | p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_FALSE; |
12576 | 0 | } else { |
12577 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12578 | 0 | "Invalid value for option: %s.\n", *p_option_iter); |
12579 | 0 | return OPJ_FALSE; |
12580 | 0 | } |
12581 | 0 | } else if (strncmp(*p_option_iter, "TLM=", 4) == 0) { |
12582 | 0 | if (strcmp(*p_option_iter, "TLM=YES") == 0) { |
12583 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE; |
12584 | 0 | } else if (strcmp(*p_option_iter, "TLM=NO") == 0) { |
12585 | 0 | p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_FALSE; |
12586 | 0 | } else { |
12587 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12588 | 0 | "Invalid value for option: %s.\n", *p_option_iter); |
12589 | 0 | return OPJ_FALSE; |
12590 | 0 | } |
12591 | 0 | } else if (strncmp(*p_option_iter, "GUARD_BITS=", strlen("GUARD_BITS=")) == 0) { |
12592 | 0 | OPJ_UINT32 tileno; |
12593 | 0 | opj_cp_t *cp = cp = &(p_j2k->m_cp); |
12594 | |
|
12595 | 0 | int numgbits = atoi(*p_option_iter + strlen("GUARD_BITS=")); |
12596 | 0 | if (numgbits < 0 || numgbits > 7) { |
12597 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12598 | 0 | "Invalid value for option: %s. Should be in [0,7]\n", *p_option_iter); |
12599 | 0 | return OPJ_FALSE; |
12600 | 0 | } |
12601 | | |
12602 | 0 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
12603 | 0 | OPJ_UINT32 i; |
12604 | 0 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
12605 | 0 | for (i = 0; i < p_j2k->m_specific_param.m_encoder.m_nb_comps; i++) { |
12606 | 0 | opj_tccp_t *tccp = &tcp->tccps[i]; |
12607 | 0 | tccp->numgbits = (OPJ_UINT32)numgbits; |
12608 | 0 | } |
12609 | 0 | } |
12610 | 0 | } else { |
12611 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12612 | 0 | "Invalid option: %s.\n", *p_option_iter); |
12613 | 0 | return OPJ_FALSE; |
12614 | 0 | } |
12615 | 0 | } |
12616 | | |
12617 | 0 | return OPJ_TRUE; |
12618 | 0 | } |
12619 | | |
12620 | | /* ----------------------------------------------------------------------- */ |
12621 | | |
12622 | | OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, |
12623 | | opj_stream_private_t *p_stream, |
12624 | | opj_event_mgr_t * p_manager) |
12625 | 0 | { |
12626 | 0 | OPJ_UINT32 i, j; |
12627 | 0 | OPJ_UINT32 l_nb_tiles; |
12628 | 0 | OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size; |
12629 | 0 | OPJ_BYTE * l_current_data = 00; |
12630 | 0 | OPJ_BOOL l_reuse_data = OPJ_FALSE; |
12631 | 0 | opj_tcd_t* p_tcd = 00; |
12632 | | |
12633 | | /* preconditions */ |
12634 | 0 | assert(p_j2k != 00); |
12635 | 0 | assert(p_stream != 00); |
12636 | 0 | assert(p_manager != 00); |
12637 | |
|
12638 | 0 | p_tcd = p_j2k->m_tcd; |
12639 | |
|
12640 | 0 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
12641 | 0 | if (l_nb_tiles == 1) { |
12642 | 0 | l_reuse_data = OPJ_TRUE; |
12643 | 0 | #ifdef __SSE__ |
12644 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
12645 | 0 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
12646 | 0 | if (((size_t)l_img_comp->data & 0xFU) != |
12647 | 0 | 0U) { /* tile data shall be aligned on 16 bytes */ |
12648 | 0 | l_reuse_data = OPJ_FALSE; |
12649 | 0 | } |
12650 | 0 | } |
12651 | 0 | #endif |
12652 | 0 | } |
12653 | 0 | for (i = 0; i < l_nb_tiles; ++i) { |
12654 | 0 | if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) { |
12655 | 0 | if (l_current_data) { |
12656 | 0 | opj_free(l_current_data); |
12657 | 0 | } |
12658 | 0 | return OPJ_FALSE; |
12659 | 0 | } |
12660 | | |
12661 | | /* if we only have one tile, then simply set tile component data equal to image component data */ |
12662 | | /* otherwise, allocate the data */ |
12663 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
12664 | 0 | opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; |
12665 | 0 | if (l_reuse_data) { |
12666 | 0 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
12667 | 0 | l_tilec->data = l_img_comp->data; |
12668 | 0 | l_tilec->ownsData = OPJ_FALSE; |
12669 | 0 | } else { |
12670 | 0 | if (! opj_alloc_tile_component_data(l_tilec)) { |
12671 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); |
12672 | 0 | if (l_current_data) { |
12673 | 0 | opj_free(l_current_data); |
12674 | 0 | } |
12675 | 0 | return OPJ_FALSE; |
12676 | 0 | } |
12677 | 0 | } |
12678 | 0 | } |
12679 | 0 | l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd); |
12680 | 0 | if (!l_reuse_data) { |
12681 | 0 | if (l_current_tile_size > l_max_tile_size) { |
12682 | 0 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, |
12683 | 0 | l_current_tile_size); |
12684 | 0 | if (! l_new_current_data) { |
12685 | 0 | if (l_current_data) { |
12686 | 0 | opj_free(l_current_data); |
12687 | 0 | } |
12688 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); |
12689 | 0 | return OPJ_FALSE; |
12690 | 0 | } |
12691 | 0 | l_current_data = l_new_current_data; |
12692 | 0 | l_max_tile_size = l_current_tile_size; |
12693 | 0 | } |
12694 | 0 | if (l_current_data == NULL) { |
12695 | | /* Should not happen in practice, but will avoid Coverity to */ |
12696 | | /* complain about a null pointer dereference */ |
12697 | 0 | assert(0); |
12698 | 0 | return OPJ_FALSE; |
12699 | 0 | } |
12700 | | |
12701 | | /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */ |
12702 | | /* 32 bit components @ 8 bit precision get converted to 8 bit */ |
12703 | | /* 32 bit components @ 16 bit precision get converted to 16 bit */ |
12704 | 0 | opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data); |
12705 | | |
12706 | | /* now copy this data into the tile component */ |
12707 | 0 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data, |
12708 | 0 | l_current_tile_size)) { |
12709 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
12710 | 0 | "Size mismatch between tile data and sent data."); |
12711 | 0 | opj_free(l_current_data); |
12712 | 0 | return OPJ_FALSE; |
12713 | 0 | } |
12714 | 0 | } |
12715 | | |
12716 | 0 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
12717 | 0 | if (l_current_data) { |
12718 | 0 | opj_free(l_current_data); |
12719 | 0 | } |
12720 | 0 | return OPJ_FALSE; |
12721 | 0 | } |
12722 | 0 | } |
12723 | | |
12724 | 0 | if (l_current_data) { |
12725 | 0 | opj_free(l_current_data); |
12726 | 0 | } |
12727 | 0 | return OPJ_TRUE; |
12728 | 0 | } |
12729 | | |
12730 | | OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, |
12731 | | opj_stream_private_t *p_stream, |
12732 | | opj_event_mgr_t * p_manager) |
12733 | 0 | { |
12734 | | /* customization of the encoding */ |
12735 | 0 | if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) { |
12736 | 0 | return OPJ_FALSE; |
12737 | 0 | } |
12738 | | |
12739 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12740 | 0 | return OPJ_FALSE; |
12741 | 0 | } |
12742 | | |
12743 | 0 | return OPJ_TRUE; |
12744 | 0 | } |
12745 | | |
12746 | | OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, |
12747 | | opj_stream_private_t *p_stream, |
12748 | | opj_image_t * p_image, |
12749 | | opj_event_mgr_t * p_manager) |
12750 | 0 | { |
12751 | | /* preconditions */ |
12752 | 0 | assert(p_j2k != 00); |
12753 | 0 | assert(p_stream != 00); |
12754 | 0 | assert(p_manager != 00); |
12755 | |
|
12756 | 0 | p_j2k->m_private_image = opj_image_create0(); |
12757 | 0 | if (! p_j2k->m_private_image) { |
12758 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header."); |
12759 | 0 | return OPJ_FALSE; |
12760 | 0 | } |
12761 | 0 | opj_copy_image_header(p_image, p_j2k->m_private_image); |
12762 | | |
12763 | | /* TODO_MSD: Find a better way */ |
12764 | 0 | if (p_image->comps) { |
12765 | 0 | OPJ_UINT32 it_comp; |
12766 | 0 | for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { |
12767 | 0 | if (p_image->comps[it_comp].data) { |
12768 | 0 | p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data; |
12769 | 0 | p_image->comps[it_comp].data = NULL; |
12770 | |
|
12771 | 0 | } |
12772 | 0 | } |
12773 | 0 | } |
12774 | | |
12775 | | /* customization of the validation */ |
12776 | 0 | if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) { |
12777 | 0 | return OPJ_FALSE; |
12778 | 0 | } |
12779 | | |
12780 | | /* validation of the parameters codec */ |
12781 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
12782 | 0 | return OPJ_FALSE; |
12783 | 0 | } |
12784 | | |
12785 | | /* customization of the encoding */ |
12786 | 0 | if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) { |
12787 | 0 | return OPJ_FALSE; |
12788 | 0 | } |
12789 | | |
12790 | | /* write header */ |
12791 | 0 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
12792 | 0 | return OPJ_FALSE; |
12793 | 0 | } |
12794 | | |
12795 | 0 | return OPJ_TRUE; |
12796 | 0 | } |
12797 | | |
12798 | | static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, |
12799 | | OPJ_UINT32 p_tile_index, |
12800 | | opj_stream_private_t *p_stream, |
12801 | | opj_event_mgr_t * p_manager) |
12802 | 0 | { |
12803 | 0 | (void)p_stream; |
12804 | 0 | if (p_tile_index != p_j2k->m_current_tile_number) { |
12805 | 0 | opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match."); |
12806 | 0 | return OPJ_FALSE; |
12807 | 0 | } |
12808 | | |
12809 | 0 | opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", |
12810 | 0 | p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); |
12811 | |
|
12812 | 0 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; |
12813 | 0 | p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; |
12814 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
12815 | | |
12816 | | /* initialisation before tile encoding */ |
12817 | 0 | if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
12818 | 0 | p_manager)) { |
12819 | 0 | return OPJ_FALSE; |
12820 | 0 | } |
12821 | | |
12822 | 0 | return OPJ_TRUE; |
12823 | 0 | } |
12824 | | |
12825 | | static void opj_get_tile_dimensions(opj_image_t * l_image, |
12826 | | opj_tcd_tilecomp_t * l_tilec, |
12827 | | opj_image_comp_t * l_img_comp, |
12828 | | OPJ_UINT32* l_size_comp, |
12829 | | OPJ_UINT32* l_width, |
12830 | | OPJ_UINT32* l_height, |
12831 | | OPJ_UINT32* l_offset_x, |
12832 | | OPJ_UINT32* l_offset_y, |
12833 | | OPJ_UINT32* l_image_width, |
12834 | | OPJ_UINT32* l_stride, |
12835 | | OPJ_UINT32* l_tile_offset) |
12836 | 0 | { |
12837 | 0 | OPJ_UINT32 l_remaining; |
12838 | 0 | *l_size_comp = l_img_comp->prec >> 3; /* (/8) */ |
12839 | 0 | l_remaining = l_img_comp->prec & 7; /* (%8) */ |
12840 | 0 | if (l_remaining) { |
12841 | 0 | *l_size_comp += 1; |
12842 | 0 | } |
12843 | |
|
12844 | 0 | if (*l_size_comp == 3) { |
12845 | 0 | *l_size_comp = 4; |
12846 | 0 | } |
12847 | |
|
12848 | 0 | *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0); |
12849 | 0 | *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0); |
12850 | 0 | *l_offset_x = opj_uint_ceildiv(l_image->x0, l_img_comp->dx); |
12851 | 0 | *l_offset_y = opj_uint_ceildiv(l_image->y0, l_img_comp->dy); |
12852 | 0 | *l_image_width = opj_uint_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx); |
12853 | 0 | *l_stride = *l_image_width - *l_width; |
12854 | 0 | *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + (( |
12855 | 0 | OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width; |
12856 | 0 | } |
12857 | | |
12858 | | static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data) |
12859 | 0 | { |
12860 | 0 | OPJ_UINT32 i, j, k = 0; |
12861 | |
|
12862 | 0 | for (i = 0; i < p_tcd->image->numcomps; ++i) { |
12863 | 0 | opj_image_t * l_image = p_tcd->image; |
12864 | 0 | OPJ_INT32 * l_src_ptr; |
12865 | 0 | opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i; |
12866 | 0 | opj_image_comp_t * l_img_comp = l_image->comps + i; |
12867 | 0 | OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y, |
12868 | 0 | l_image_width, l_stride, l_tile_offset; |
12869 | |
|
12870 | 0 | opj_get_tile_dimensions(l_image, |
12871 | 0 | l_tilec, |
12872 | 0 | l_img_comp, |
12873 | 0 | &l_size_comp, |
12874 | 0 | &l_width, |
12875 | 0 | &l_height, |
12876 | 0 | &l_offset_x, |
12877 | 0 | &l_offset_y, |
12878 | 0 | &l_image_width, |
12879 | 0 | &l_stride, |
12880 | 0 | &l_tile_offset); |
12881 | |
|
12882 | 0 | l_src_ptr = l_img_comp->data + l_tile_offset; |
12883 | |
|
12884 | 0 | switch (l_size_comp) { |
12885 | 0 | case 1: { |
12886 | 0 | OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; |
12887 | 0 | if (l_img_comp->sgnd) { |
12888 | 0 | for (j = 0; j < l_height; ++j) { |
12889 | 0 | for (k = 0; k < l_width; ++k) { |
12890 | 0 | *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr); |
12891 | 0 | ++l_dest_ptr; |
12892 | 0 | ++l_src_ptr; |
12893 | 0 | } |
12894 | 0 | l_src_ptr += l_stride; |
12895 | 0 | } |
12896 | 0 | } else { |
12897 | 0 | for (j = 0; j < l_height; ++j) { |
12898 | 0 | for (k = 0; k < l_width; ++k) { |
12899 | 0 | *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff); |
12900 | 0 | ++l_dest_ptr; |
12901 | 0 | ++l_src_ptr; |
12902 | 0 | } |
12903 | 0 | l_src_ptr += l_stride; |
12904 | 0 | } |
12905 | 0 | } |
12906 | |
|
12907 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12908 | 0 | } |
12909 | 0 | break; |
12910 | 0 | case 2: { |
12911 | 0 | OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data; |
12912 | 0 | if (l_img_comp->sgnd) { |
12913 | 0 | for (j = 0; j < l_height; ++j) { |
12914 | 0 | for (k = 0; k < l_width; ++k) { |
12915 | 0 | *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++)); |
12916 | 0 | } |
12917 | 0 | l_src_ptr += l_stride; |
12918 | 0 | } |
12919 | 0 | } else { |
12920 | 0 | for (j = 0; j < l_height; ++j) { |
12921 | 0 | for (k = 0; k < l_width; ++k) { |
12922 | 0 | *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff); |
12923 | 0 | } |
12924 | 0 | l_src_ptr += l_stride; |
12925 | 0 | } |
12926 | 0 | } |
12927 | |
|
12928 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12929 | 0 | } |
12930 | 0 | break; |
12931 | 0 | case 4: { |
12932 | 0 | OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data; |
12933 | 0 | for (j = 0; j < l_height; ++j) { |
12934 | 0 | for (k = 0; k < l_width; ++k) { |
12935 | 0 | *(l_dest_ptr++) = *(l_src_ptr++); |
12936 | 0 | } |
12937 | 0 | l_src_ptr += l_stride; |
12938 | 0 | } |
12939 | |
|
12940 | 0 | p_data = (OPJ_BYTE*) l_dest_ptr; |
12941 | 0 | } |
12942 | 0 | break; |
12943 | 0 | } |
12944 | 0 | } |
12945 | 0 | } |
12946 | | |
12947 | | static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, |
12948 | | opj_stream_private_t *p_stream, |
12949 | | opj_event_mgr_t * p_manager) |
12950 | 0 | { |
12951 | 0 | OPJ_UINT32 l_nb_bytes_written; |
12952 | 0 | OPJ_BYTE * l_current_data = 00; |
12953 | 0 | OPJ_UINT32 l_tile_size = 0; |
12954 | 0 | OPJ_UINT32 l_available_data; |
12955 | | |
12956 | | /* preconditions */ |
12957 | 0 | assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
12958 | |
|
12959 | 0 | l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; |
12960 | 0 | l_available_data = l_tile_size; |
12961 | 0 | l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; |
12962 | |
|
12963 | 0 | l_nb_bytes_written = 0; |
12964 | 0 | if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written, |
12965 | 0 | l_available_data, p_stream, p_manager)) { |
12966 | 0 | return OPJ_FALSE; |
12967 | 0 | } |
12968 | 0 | l_current_data += l_nb_bytes_written; |
12969 | 0 | l_available_data -= l_nb_bytes_written; |
12970 | |
|
12971 | 0 | l_nb_bytes_written = 0; |
12972 | 0 | if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written, |
12973 | 0 | l_available_data, p_stream, p_manager)) { |
12974 | 0 | return OPJ_FALSE; |
12975 | 0 | } |
12976 | | |
12977 | 0 | l_available_data -= l_nb_bytes_written; |
12978 | 0 | l_nb_bytes_written = l_tile_size - l_available_data; |
12979 | |
|
12980 | 0 | if (opj_stream_write_data(p_stream, |
12981 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, |
12982 | 0 | l_nb_bytes_written, p_manager) != l_nb_bytes_written) { |
12983 | 0 | return OPJ_FALSE; |
12984 | 0 | } |
12985 | | |
12986 | 0 | ++p_j2k->m_current_tile_number; |
12987 | |
|
12988 | 0 | return OPJ_TRUE; |
12989 | 0 | } |
12990 | | |
12991 | | static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, |
12992 | | opj_event_mgr_t * p_manager) |
12993 | 0 | { |
12994 | | /* preconditions */ |
12995 | 0 | assert(p_j2k != 00); |
12996 | 0 | assert(p_manager != 00); |
12997 | | |
12998 | | /* DEVELOPER CORNER, insert your custom procedures */ |
12999 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13000 | 0 | (opj_procedure)opj_j2k_write_eoc, p_manager)) { |
13001 | 0 | return OPJ_FALSE; |
13002 | 0 | } |
13003 | | |
13004 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13005 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13006 | 0 | (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) { |
13007 | 0 | return OPJ_FALSE; |
13008 | 0 | } |
13009 | 0 | } |
13010 | | |
13011 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13012 | 0 | (opj_procedure)opj_j2k_write_epc, p_manager)) { |
13013 | 0 | return OPJ_FALSE; |
13014 | 0 | } |
13015 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13016 | 0 | (opj_procedure)opj_j2k_end_encoding, p_manager)) { |
13017 | 0 | return OPJ_FALSE; |
13018 | 0 | } |
13019 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13020 | 0 | (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) { |
13021 | 0 | return OPJ_FALSE; |
13022 | 0 | } |
13023 | 0 | return OPJ_TRUE; |
13024 | 0 | } |
13025 | | |
13026 | | static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, |
13027 | | opj_event_mgr_t * p_manager) |
13028 | 0 | { |
13029 | | /* preconditions */ |
13030 | 0 | assert(p_j2k != 00); |
13031 | 0 | assert(p_manager != 00); |
13032 | |
|
13033 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
13034 | 0 | (opj_procedure)opj_j2k_build_encoder, p_manager)) { |
13035 | 0 | return OPJ_FALSE; |
13036 | 0 | } |
13037 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
13038 | 0 | (opj_procedure)opj_j2k_encoding_validation, p_manager)) { |
13039 | 0 | return OPJ_FALSE; |
13040 | 0 | } |
13041 | | |
13042 | | /* DEVELOPER CORNER, add your custom validation procedure */ |
13043 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
13044 | 0 | (opj_procedure)opj_j2k_mct_validation, p_manager)) { |
13045 | 0 | return OPJ_FALSE; |
13046 | 0 | } |
13047 | | |
13048 | 0 | return OPJ_TRUE; |
13049 | 0 | } |
13050 | | |
13051 | | static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, |
13052 | | opj_event_mgr_t * p_manager) |
13053 | 0 | { |
13054 | | /* preconditions */ |
13055 | 0 | assert(p_j2k != 00); |
13056 | 0 | assert(p_manager != 00); |
13057 | |
|
13058 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13059 | 0 | (opj_procedure)opj_j2k_init_info, p_manager)) { |
13060 | 0 | return OPJ_FALSE; |
13061 | 0 | } |
13062 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13063 | 0 | (opj_procedure)opj_j2k_write_soc, p_manager)) { |
13064 | 0 | return OPJ_FALSE; |
13065 | 0 | } |
13066 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13067 | 0 | (opj_procedure)opj_j2k_write_siz, p_manager)) { |
13068 | 0 | return OPJ_FALSE; |
13069 | 0 | } |
13070 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13071 | 0 | (opj_procedure)opj_j2k_write_cod, p_manager)) { |
13072 | 0 | return OPJ_FALSE; |
13073 | 0 | } |
13074 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13075 | 0 | (opj_procedure)opj_j2k_write_qcd, p_manager)) { |
13076 | 0 | return OPJ_FALSE; |
13077 | 0 | } |
13078 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13079 | 0 | (opj_procedure)opj_j2k_write_all_coc, p_manager)) { |
13080 | 0 | return OPJ_FALSE; |
13081 | 0 | } |
13082 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13083 | 0 | (opj_procedure)opj_j2k_write_all_qcc, p_manager)) { |
13084 | 0 | return OPJ_FALSE; |
13085 | 0 | } |
13086 | | |
13087 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13088 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13089 | 0 | (opj_procedure)opj_j2k_write_tlm, p_manager)) { |
13090 | 0 | return OPJ_FALSE; |
13091 | 0 | } |
13092 | | |
13093 | 0 | if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) { |
13094 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13095 | 0 | (opj_procedure)opj_j2k_write_poc, p_manager)) { |
13096 | 0 | return OPJ_FALSE; |
13097 | 0 | } |
13098 | 0 | } |
13099 | 0 | } |
13100 | | |
13101 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13102 | 0 | (opj_procedure)opj_j2k_write_regions, p_manager)) { |
13103 | 0 | return OPJ_FALSE; |
13104 | 0 | } |
13105 | | |
13106 | 0 | if (p_j2k->m_cp.comment != 00) { |
13107 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13108 | 0 | (opj_procedure)opj_j2k_write_com, p_manager)) { |
13109 | 0 | return OPJ_FALSE; |
13110 | 0 | } |
13111 | 0 | } |
13112 | | |
13113 | | /* DEVELOPER CORNER, insert your custom procedures */ |
13114 | 0 | if ((p_j2k->m_cp.rsiz & (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) == |
13115 | 0 | (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) { |
13116 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13117 | 0 | (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) { |
13118 | 0 | return OPJ_FALSE; |
13119 | 0 | } |
13120 | 0 | } |
13121 | | /* End of Developer Corner */ |
13122 | | |
13123 | 0 | if (p_j2k->cstr_index) { |
13124 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13125 | 0 | (opj_procedure)opj_j2k_get_end_header, p_manager)) { |
13126 | 0 | return OPJ_FALSE; |
13127 | 0 | } |
13128 | 0 | } |
13129 | | |
13130 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13131 | 0 | (opj_procedure)opj_j2k_create_tcd, p_manager)) { |
13132 | 0 | return OPJ_FALSE; |
13133 | 0 | } |
13134 | 0 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
13135 | 0 | (opj_procedure)opj_j2k_update_rates, p_manager)) { |
13136 | 0 | return OPJ_FALSE; |
13137 | 0 | } |
13138 | | |
13139 | 0 | return OPJ_TRUE; |
13140 | 0 | } |
13141 | | |
13142 | | static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, |
13143 | | OPJ_BYTE * p_data, |
13144 | | OPJ_UINT32 * p_data_written, |
13145 | | OPJ_UINT32 total_data_size, |
13146 | | opj_stream_private_t *p_stream, |
13147 | | struct opj_event_mgr * p_manager) |
13148 | 0 | { |
13149 | 0 | OPJ_UINT32 l_nb_bytes_written = 0; |
13150 | 0 | OPJ_UINT32 l_current_nb_bytes_written; |
13151 | 0 | OPJ_BYTE * l_begin_data = 00; |
13152 | |
|
13153 | 0 | opj_tcd_t * l_tcd = 00; |
13154 | 0 | opj_cp_t * l_cp = 00; |
13155 | |
|
13156 | 0 | l_tcd = p_j2k->m_tcd; |
13157 | 0 | l_cp = &(p_j2k->m_cp); |
13158 | |
|
13159 | 0 | l_tcd->cur_pino = 0; |
13160 | | |
13161 | | /*Get number of tile parts*/ |
13162 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
13163 | | |
13164 | | /* INDEX >> */ |
13165 | | /* << INDEX */ |
13166 | |
|
13167 | 0 | l_current_nb_bytes_written = 0; |
13168 | 0 | l_begin_data = p_data; |
13169 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, total_data_size, |
13170 | 0 | &l_current_nb_bytes_written, p_stream, |
13171 | 0 | p_manager)) { |
13172 | 0 | return OPJ_FALSE; |
13173 | 0 | } |
13174 | | |
13175 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13176 | 0 | p_data += l_current_nb_bytes_written; |
13177 | 0 | total_data_size -= l_current_nb_bytes_written; |
13178 | |
|
13179 | 0 | if (!OPJ_IS_CINEMA(l_cp->rsiz)) { |
13180 | | #if 0 |
13181 | | for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { |
13182 | | l_current_nb_bytes_written = 0; |
13183 | | opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
13184 | | p_manager); |
13185 | | l_nb_bytes_written += l_current_nb_bytes_written; |
13186 | | p_data += l_current_nb_bytes_written; |
13187 | | total_data_size -= l_current_nb_bytes_written; |
13188 | | |
13189 | | l_current_nb_bytes_written = 0; |
13190 | | opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
13191 | | p_manager); |
13192 | | l_nb_bytes_written += l_current_nb_bytes_written; |
13193 | | p_data += l_current_nb_bytes_written; |
13194 | | total_data_size -= l_current_nb_bytes_written; |
13195 | | } |
13196 | | #endif |
13197 | 0 | if (l_cp->tcps[p_j2k->m_current_tile_number].POC) { |
13198 | 0 | l_current_nb_bytes_written = 0; |
13199 | 0 | opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written, |
13200 | 0 | p_manager); |
13201 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13202 | 0 | p_data += l_current_nb_bytes_written; |
13203 | 0 | total_data_size -= l_current_nb_bytes_written; |
13204 | 0 | } |
13205 | 0 | } |
13206 | |
|
13207 | 0 | l_current_nb_bytes_written = 0; |
13208 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
13209 | 0 | total_data_size, p_stream, p_manager)) { |
13210 | 0 | return OPJ_FALSE; |
13211 | 0 | } |
13212 | | |
13213 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13214 | 0 | * p_data_written = l_nb_bytes_written; |
13215 | | |
13216 | | /* Writing Psot in SOT marker */ |
13217 | 0 | opj_write_bytes(l_begin_data + 6, l_nb_bytes_written, |
13218 | 0 | 4); /* PSOT */ |
13219 | |
|
13220 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13221 | 0 | opj_j2k_update_tlm(p_j2k, l_nb_bytes_written); |
13222 | 0 | } |
13223 | |
|
13224 | 0 | return OPJ_TRUE; |
13225 | 0 | } |
13226 | | |
13227 | | static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, |
13228 | | OPJ_BYTE * p_data, |
13229 | | OPJ_UINT32 * p_data_written, |
13230 | | OPJ_UINT32 total_data_size, |
13231 | | opj_stream_private_t *p_stream, |
13232 | | struct opj_event_mgr * p_manager |
13233 | | ) |
13234 | 0 | { |
13235 | 0 | OPJ_UINT32 tilepartno = 0; |
13236 | 0 | OPJ_UINT32 l_nb_bytes_written = 0; |
13237 | 0 | OPJ_UINT32 l_current_nb_bytes_written; |
13238 | 0 | OPJ_UINT32 l_part_tile_size; |
13239 | 0 | OPJ_UINT32 tot_num_tp; |
13240 | 0 | OPJ_UINT32 pino; |
13241 | |
|
13242 | 0 | OPJ_BYTE * l_begin_data; |
13243 | 0 | opj_tcp_t *l_tcp = 00; |
13244 | 0 | opj_tcd_t * l_tcd = 00; |
13245 | 0 | opj_cp_t * l_cp = 00; |
13246 | |
|
13247 | 0 | l_tcd = p_j2k->m_tcd; |
13248 | 0 | l_cp = &(p_j2k->m_cp); |
13249 | 0 | l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; |
13250 | | |
13251 | | /*Get number of tile parts*/ |
13252 | 0 | tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number); |
13253 | | |
13254 | | /* start writing remaining tile parts */ |
13255 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
13256 | 0 | for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { |
13257 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
13258 | 0 | l_current_nb_bytes_written = 0; |
13259 | 0 | l_part_tile_size = 0; |
13260 | 0 | l_begin_data = p_data; |
13261 | |
|
13262 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, |
13263 | 0 | total_data_size, |
13264 | 0 | &l_current_nb_bytes_written, |
13265 | 0 | p_stream, |
13266 | 0 | p_manager)) { |
13267 | 0 | return OPJ_FALSE; |
13268 | 0 | } |
13269 | | |
13270 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13271 | 0 | p_data += l_current_nb_bytes_written; |
13272 | 0 | total_data_size -= l_current_nb_bytes_written; |
13273 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13274 | |
|
13275 | 0 | l_current_nb_bytes_written = 0; |
13276 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
13277 | 0 | total_data_size, p_stream, p_manager)) { |
13278 | 0 | return OPJ_FALSE; |
13279 | 0 | } |
13280 | | |
13281 | 0 | p_data += l_current_nb_bytes_written; |
13282 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13283 | 0 | total_data_size -= l_current_nb_bytes_written; |
13284 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13285 | | |
13286 | | /* Writing Psot in SOT marker */ |
13287 | 0 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
13288 | 0 | 4); /* PSOT */ |
13289 | |
|
13290 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13291 | 0 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
13292 | 0 | } |
13293 | |
|
13294 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
13295 | 0 | } |
13296 | | |
13297 | 0 | for (pino = 1; pino <= l_tcp->numpocs; ++pino) { |
13298 | 0 | l_tcd->cur_pino = pino; |
13299 | | |
13300 | | /*Get number of tile parts*/ |
13301 | 0 | tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number); |
13302 | 0 | for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { |
13303 | 0 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
13304 | 0 | l_current_nb_bytes_written = 0; |
13305 | 0 | l_part_tile_size = 0; |
13306 | 0 | l_begin_data = p_data; |
13307 | |
|
13308 | 0 | if (! opj_j2k_write_sot(p_j2k, p_data, |
13309 | 0 | total_data_size, |
13310 | 0 | &l_current_nb_bytes_written, p_stream, |
13311 | 0 | p_manager)) { |
13312 | 0 | return OPJ_FALSE; |
13313 | 0 | } |
13314 | | |
13315 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13316 | 0 | p_data += l_current_nb_bytes_written; |
13317 | 0 | total_data_size -= l_current_nb_bytes_written; |
13318 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13319 | |
|
13320 | 0 | l_current_nb_bytes_written = 0; |
13321 | |
|
13322 | 0 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
13323 | 0 | total_data_size, p_stream, p_manager)) { |
13324 | 0 | return OPJ_FALSE; |
13325 | 0 | } |
13326 | | |
13327 | 0 | l_nb_bytes_written += l_current_nb_bytes_written; |
13328 | 0 | p_data += l_current_nb_bytes_written; |
13329 | 0 | total_data_size -= l_current_nb_bytes_written; |
13330 | 0 | l_part_tile_size += l_current_nb_bytes_written; |
13331 | | |
13332 | | /* Writing Psot in SOT marker */ |
13333 | 0 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
13334 | 0 | 4); /* PSOT */ |
13335 | |
|
13336 | 0 | if (p_j2k->m_specific_param.m_encoder.m_TLM) { |
13337 | 0 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
13338 | 0 | } |
13339 | |
|
13340 | 0 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
13341 | 0 | } |
13342 | 0 | } |
13343 | | |
13344 | 0 | *p_data_written = l_nb_bytes_written; |
13345 | |
|
13346 | 0 | return OPJ_TRUE; |
13347 | 0 | } |
13348 | | |
13349 | | static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, |
13350 | | struct opj_stream_private *p_stream, |
13351 | | struct opj_event_mgr * p_manager) |
13352 | 0 | { |
13353 | 0 | OPJ_UINT32 l_tlm_size; |
13354 | 0 | OPJ_OFF_T l_tlm_position, l_current_position; |
13355 | 0 | OPJ_UINT32 size_per_tile_part; |
13356 | | |
13357 | | /* preconditions */ |
13358 | 0 | assert(p_j2k != 00); |
13359 | 0 | assert(p_manager != 00); |
13360 | 0 | assert(p_stream != 00); |
13361 | |
|
13362 | 0 | size_per_tile_part = p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte ? 5 : 6; |
13363 | 0 | l_tlm_size = size_per_tile_part * |
13364 | 0 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts; |
13365 | 0 | l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; |
13366 | 0 | l_current_position = opj_stream_tell(p_stream); |
13367 | |
|
13368 | 0 | if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) { |
13369 | 0 | return OPJ_FALSE; |
13370 | 0 | } |
13371 | | |
13372 | 0 | if (opj_stream_write_data(p_stream, |
13373 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size, |
13374 | 0 | p_manager) != l_tlm_size) { |
13375 | 0 | return OPJ_FALSE; |
13376 | 0 | } |
13377 | | |
13378 | 0 | if (! opj_stream_seek(p_stream, l_current_position, p_manager)) { |
13379 | 0 | return OPJ_FALSE; |
13380 | 0 | } |
13381 | | |
13382 | 0 | return OPJ_TRUE; |
13383 | 0 | } |
13384 | | |
13385 | | static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, |
13386 | | struct opj_stream_private *p_stream, |
13387 | | struct opj_event_mgr * p_manager) |
13388 | 0 | { |
13389 | | /* preconditions */ |
13390 | 0 | assert(p_j2k != 00); |
13391 | 0 | assert(p_manager != 00); |
13392 | 0 | assert(p_stream != 00); |
13393 | |
|
13394 | 0 | OPJ_UNUSED(p_stream); |
13395 | 0 | OPJ_UNUSED(p_manager); |
13396 | |
|
13397 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
13398 | 0 | p_j2k->m_tcd = 00; |
13399 | |
|
13400 | 0 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
13401 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
13402 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; |
13403 | 0 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; |
13404 | 0 | } |
13405 | |
|
13406 | 0 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
13407 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
13408 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; |
13409 | 0 | } |
13410 | |
|
13411 | 0 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; |
13412 | |
|
13413 | 0 | return OPJ_TRUE; |
13414 | 0 | } |
13415 | | |
13416 | | /** |
13417 | | * Destroys the memory associated with the decoding of headers. |
13418 | | */ |
13419 | | static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, |
13420 | | opj_stream_private_t *p_stream, |
13421 | | opj_event_mgr_t * p_manager |
13422 | | ) |
13423 | 0 | { |
13424 | | /* preconditions */ |
13425 | 0 | assert(p_j2k != 00); |
13426 | 0 | assert(p_stream != 00); |
13427 | 0 | assert(p_manager != 00); |
13428 | |
|
13429 | 0 | OPJ_UNUSED(p_stream); |
13430 | 0 | OPJ_UNUSED(p_manager); |
13431 | |
|
13432 | 0 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
13433 | 0 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
13434 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; |
13435 | 0 | } |
13436 | |
|
13437 | 0 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
13438 | |
|
13439 | 0 | return OPJ_TRUE; |
13440 | 0 | } |
13441 | | |
13442 | | static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, |
13443 | | struct opj_stream_private *p_stream, |
13444 | | struct opj_event_mgr * p_manager) |
13445 | 0 | { |
13446 | 0 | opj_codestream_info_t * l_cstr_info = 00; |
13447 | | |
13448 | | /* preconditions */ |
13449 | 0 | assert(p_j2k != 00); |
13450 | 0 | assert(p_manager != 00); |
13451 | 0 | assert(p_stream != 00); |
13452 | 0 | (void)l_cstr_info; |
13453 | |
|
13454 | 0 | OPJ_UNUSED(p_stream); |
13455 | | |
13456 | | /* TODO mergeV2: check this part which use cstr_info */ |
13457 | | /*l_cstr_info = p_j2k->cstr_info; |
13458 | | |
13459 | | if (l_cstr_info) { |
13460 | | OPJ_UINT32 compno; |
13461 | | 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)); |
13462 | | |
13463 | | l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; |
13464 | | l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; |
13465 | | |
13466 | | l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; |
13467 | | |
13468 | | l_cstr_info->tw = p_j2k->m_cp.tw; |
13469 | | l_cstr_info->th = p_j2k->m_cp.th; |
13470 | | |
13471 | | l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ |
13472 | | /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ |
13473 | | /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ |
13474 | | /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ |
13475 | | |
13476 | | /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; |
13477 | | |
13478 | | l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; |
13479 | | |
13480 | | l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); |
13481 | | |
13482 | | for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { |
13483 | | l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; |
13484 | | } |
13485 | | |
13486 | | l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ |
13487 | | |
13488 | | /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ |
13489 | | |
13490 | | /*l_cstr_info->maxmarknum = 100; |
13491 | | l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); |
13492 | | l_cstr_info->marknum = 0; |
13493 | | }*/ |
13494 | |
|
13495 | 0 | return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp), |
13496 | 0 | &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image, |
13497 | 0 | p_manager); |
13498 | 0 | } |
13499 | | |
13500 | | /** |
13501 | | * Creates a tile-coder encoder. |
13502 | | * |
13503 | | * @param p_stream the stream to write data to. |
13504 | | * @param p_j2k J2K codec. |
13505 | | * @param p_manager the user event manager. |
13506 | | */ |
13507 | | static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, |
13508 | | opj_stream_private_t *p_stream, |
13509 | | opj_event_mgr_t * p_manager |
13510 | | ) |
13511 | 0 | { |
13512 | | /* preconditions */ |
13513 | 0 | assert(p_j2k != 00); |
13514 | 0 | assert(p_manager != 00); |
13515 | 0 | assert(p_stream != 00); |
13516 | |
|
13517 | 0 | OPJ_UNUSED(p_stream); |
13518 | |
|
13519 | 0 | p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); |
13520 | |
|
13521 | 0 | if (! p_j2k->m_tcd) { |
13522 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); |
13523 | 0 | return OPJ_FALSE; |
13524 | 0 | } |
13525 | | |
13526 | 0 | if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp, |
13527 | 0 | p_j2k->m_tp)) { |
13528 | 0 | opj_tcd_destroy(p_j2k->m_tcd); |
13529 | 0 | p_j2k->m_tcd = 00; |
13530 | 0 | return OPJ_FALSE; |
13531 | 0 | } |
13532 | | |
13533 | 0 | return OPJ_TRUE; |
13534 | 0 | } |
13535 | | |
13536 | | OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k, |
13537 | | OPJ_UINT32 p_tile_index, |
13538 | | OPJ_BYTE * p_data, |
13539 | | OPJ_UINT32 p_data_size, |
13540 | | opj_stream_private_t *p_stream, |
13541 | | opj_event_mgr_t * p_manager) |
13542 | 0 | { |
13543 | 0 | if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) { |
13544 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13545 | 0 | "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); |
13546 | 0 | return OPJ_FALSE; |
13547 | 0 | } else { |
13548 | 0 | OPJ_UINT32 j; |
13549 | | /* Allocate data */ |
13550 | 0 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
13551 | 0 | opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j; |
13552 | |
|
13553 | 0 | if (! opj_alloc_tile_component_data(l_tilec)) { |
13554 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); |
13555 | 0 | return OPJ_FALSE; |
13556 | 0 | } |
13557 | 0 | } |
13558 | | |
13559 | | /* now copy data into the tile component */ |
13560 | 0 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
13561 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13562 | 0 | "Size mismatch between tile data and sent data."); |
13563 | 0 | return OPJ_FALSE; |
13564 | 0 | } |
13565 | 0 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
13566 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
13567 | 0 | "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); |
13568 | 0 | return OPJ_FALSE; |
13569 | 0 | } |
13570 | 0 | } |
13571 | | |
13572 | 0 | return OPJ_TRUE; |
13573 | 0 | } |