/src/opencv/3rdparty/openjpeg/openjp2/t2.c
Line | Count | Source |
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, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR |
15 | | * Copyright (c) 2012, CS Systemes d'Information, France |
16 | | * Copyright (c) 2017, IntoPIX SA <support@intopix.com> |
17 | | * All rights reserved. |
18 | | * |
19 | | * Redistribution and use in source and binary forms, with or without |
20 | | * modification, are permitted provided that the following conditions |
21 | | * are met: |
22 | | * 1. Redistributions of source code must retain the above copyright |
23 | | * notice, this list of conditions and the following disclaimer. |
24 | | * 2. Redistributions in binary form must reproduce the above copyright |
25 | | * notice, this list of conditions and the following disclaimer in the |
26 | | * documentation and/or other materials provided with the distribution. |
27 | | * |
28 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' |
29 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
30 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
31 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
32 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
33 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
34 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
35 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
36 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | * POSSIBILITY OF SUCH DAMAGE. |
39 | | */ |
40 | | |
41 | | #include "opj_includes.h" |
42 | | #include "opj_common.h" |
43 | | |
44 | | |
45 | | /** @defgroup T2 T2 - Implementation of a tier-2 coding */ |
46 | | /*@{*/ |
47 | | |
48 | | /** @name Local static functions */ |
49 | | /*@{*/ |
50 | | |
51 | | static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n); |
52 | | |
53 | | static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio); |
54 | | /** |
55 | | Variable length code for signalling delta Zil (truncation point) |
56 | | @param bio Bit Input/Output component |
57 | | @param n delta Zil |
58 | | */ |
59 | | static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n); |
60 | | static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio); |
61 | | |
62 | | /** |
63 | | Encode a packet of a tile to a destination buffer |
64 | | @param tileno Number of the tile encoded |
65 | | @param tile Tile for which to write the packets |
66 | | @param tcp Tile coding parameters |
67 | | @param pi Packet identity |
68 | | @param dest Destination buffer |
69 | | @param p_data_written FIXME DOC |
70 | | @param len Length of the destination buffer |
71 | | @param cstr_info Codestream information structure |
72 | | @param p_t2_mode If == THRESH_CALC In Threshold calculation ,If == FINAL_PASS Final pass |
73 | | @param p_manager the user event manager |
74 | | @return |
75 | | */ |
76 | | static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno, |
77 | | opj_tcd_tile_t *tile, |
78 | | opj_tcp_t *tcp, |
79 | | opj_pi_iterator_t *pi, |
80 | | OPJ_BYTE *dest, |
81 | | OPJ_UINT32 * p_data_written, |
82 | | OPJ_UINT32 len, |
83 | | opj_codestream_info_t *cstr_info, |
84 | | J2K_T2_MODE p_t2_mode, |
85 | | opj_event_mgr_t *p_manager); |
86 | | |
87 | | /** |
88 | | Decode a packet of a tile from a source buffer |
89 | | @param t2 T2 handle |
90 | | @param tile Tile for which to write the packets |
91 | | @param tcp Tile coding parameters |
92 | | @param pi Packet identity |
93 | | @param src Source buffer |
94 | | @param data_read FIXME DOC |
95 | | @param max_length FIXME DOC |
96 | | @param pack_info Packet information |
97 | | @param p_manager the user event manager |
98 | | |
99 | | @return FIXME DOC |
100 | | */ |
101 | | static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* t2, |
102 | | opj_tcd_tile_t *tile, |
103 | | opj_tcp_t *tcp, |
104 | | opj_pi_iterator_t *pi, |
105 | | OPJ_BYTE *src, |
106 | | OPJ_UINT32 * data_read, |
107 | | OPJ_UINT32 max_length, |
108 | | opj_packet_info_t *pack_info, |
109 | | opj_event_mgr_t *p_manager); |
110 | | |
111 | | static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2, |
112 | | opj_tcd_tile_t *p_tile, |
113 | | opj_tcp_t *p_tcp, |
114 | | opj_pi_iterator_t *p_pi, |
115 | | OPJ_BYTE *p_src, |
116 | | OPJ_UINT32 * p_data_read, |
117 | | OPJ_UINT32 p_max_length, |
118 | | opj_packet_info_t *p_pack_info, |
119 | | opj_event_mgr_t *p_manager); |
120 | | |
121 | | static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2, |
122 | | opj_tcd_tile_t *p_tile, |
123 | | opj_tcp_t *p_tcp, |
124 | | opj_pi_iterator_t *p_pi, |
125 | | OPJ_BOOL * p_is_data_present, |
126 | | OPJ_BYTE *p_src_data, |
127 | | OPJ_UINT32 * p_data_read, |
128 | | OPJ_UINT32 p_max_length, |
129 | | opj_packet_info_t *p_pack_info, |
130 | | opj_event_mgr_t *p_manager); |
131 | | |
132 | | static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2, |
133 | | opj_tcd_tile_t *p_tile, |
134 | | opj_pi_iterator_t *p_pi, |
135 | | OPJ_BYTE *p_src_data, |
136 | | OPJ_UINT32 * p_data_read, |
137 | | OPJ_UINT32 p_max_length, |
138 | | opj_packet_info_t *pack_info, |
139 | | opj_event_mgr_t *p_manager); |
140 | | |
141 | | static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2, |
142 | | opj_tcd_tile_t *p_tile, |
143 | | opj_pi_iterator_t *p_pi, |
144 | | OPJ_UINT32 * p_data_read, |
145 | | OPJ_UINT32 p_max_length, |
146 | | opj_packet_info_t *pack_info, |
147 | | opj_event_mgr_t *p_manager); |
148 | | |
149 | | /** |
150 | | @param cblk |
151 | | @param index |
152 | | @param cblksty |
153 | | @param first |
154 | | */ |
155 | | static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk, |
156 | | OPJ_UINT32 index, |
157 | | OPJ_UINT32 cblksty, |
158 | | OPJ_UINT32 first); |
159 | | |
160 | | /*@}*/ |
161 | | |
162 | | /*@}*/ |
163 | | |
164 | | /* ----------------------------------------------------------------------- */ |
165 | | |
166 | | /* #define RESTART 0x04 */ |
167 | | static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n) |
168 | 0 | { |
169 | 0 | while (--n >= 0) { |
170 | 0 | opj_bio_putbit(bio, 1); |
171 | 0 | } |
172 | 0 | opj_bio_putbit(bio, 0); |
173 | 0 | } |
174 | | |
175 | | static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio) |
176 | 693k | { |
177 | 693k | OPJ_UINT32 n = 0; |
178 | 980k | while (opj_bio_read(bio, 1)) { |
179 | 286k | ++n; |
180 | 286k | } |
181 | 693k | return n; |
182 | 693k | } |
183 | | |
184 | | static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) |
185 | 0 | { |
186 | 0 | if (n == 1) { |
187 | 0 | opj_bio_putbit(bio, 0); |
188 | 0 | } else if (n == 2) { |
189 | 0 | opj_bio_write(bio, 2, 2); |
190 | 0 | } else if (n <= 5) { |
191 | 0 | opj_bio_write(bio, 0xc | (n - 3), 4); |
192 | 0 | } else if (n <= 36) { |
193 | 0 | opj_bio_write(bio, 0x1e0 | (n - 6), 9); |
194 | 0 | } else if (n <= 164) { |
195 | 0 | opj_bio_write(bio, 0xff80 | (n - 37), 16); |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | | static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio) |
200 | 693k | { |
201 | 693k | OPJ_UINT32 n; |
202 | 693k | if (!opj_bio_read(bio, 1)) { |
203 | 251k | return 1; |
204 | 251k | } |
205 | 442k | if (!opj_bio_read(bio, 1)) { |
206 | 115k | return 2; |
207 | 115k | } |
208 | 326k | if ((n = opj_bio_read(bio, 2)) != 3) { |
209 | 105k | return (3 + n); |
210 | 105k | } |
211 | 220k | if ((n = opj_bio_read(bio, 5)) != 31) { |
212 | 218k | return (6 + n); |
213 | 218k | } |
214 | 2.11k | return (37 + opj_bio_read(bio, 7)); |
215 | 220k | } |
216 | | |
217 | | /* ----------------------------------------------------------------------- */ |
218 | | |
219 | | OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2, |
220 | | OPJ_UINT32 p_tile_no, |
221 | | opj_tcd_tile_t *p_tile, |
222 | | OPJ_UINT32 p_maxlayers, |
223 | | OPJ_BYTE *p_dest, |
224 | | OPJ_UINT32 * p_data_written, |
225 | | OPJ_UINT32 p_max_len, |
226 | | opj_codestream_info_t *cstr_info, |
227 | | opj_tcd_marker_info_t* p_marker_info, |
228 | | OPJ_UINT32 p_tp_num, |
229 | | OPJ_INT32 p_tp_pos, |
230 | | OPJ_UINT32 p_pino, |
231 | | J2K_T2_MODE p_t2_mode, |
232 | | opj_event_mgr_t *p_manager) |
233 | 0 | { |
234 | 0 | OPJ_BYTE *l_current_data = p_dest; |
235 | 0 | OPJ_UINT32 l_nb_bytes = 0; |
236 | 0 | OPJ_UINT32 compno; |
237 | 0 | OPJ_UINT32 poc; |
238 | 0 | opj_pi_iterator_t *l_pi = 00; |
239 | 0 | opj_pi_iterator_t *l_current_pi = 00; |
240 | 0 | opj_image_t *l_image = p_t2->image; |
241 | 0 | opj_cp_t *l_cp = p_t2->cp; |
242 | 0 | opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no]; |
243 | 0 | OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K) ? 2 : 1; |
244 | 0 | OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? |
245 | 0 | l_image->numcomps : 1; |
246 | 0 | OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; |
247 | |
|
248 | 0 | l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode, p_manager); |
249 | 0 | if (!l_pi) { |
250 | 0 | return OPJ_FALSE; |
251 | 0 | } |
252 | | |
253 | 0 | * p_data_written = 0; |
254 | |
|
255 | 0 | if (p_t2_mode == THRESH_CALC) { /* Calculating threshold */ |
256 | 0 | l_current_pi = l_pi; |
257 | |
|
258 | 0 | for (compno = 0; compno < l_max_comp; ++compno) { |
259 | 0 | OPJ_UINT32 l_comp_len = 0; |
260 | 0 | l_current_pi = l_pi; |
261 | |
|
262 | 0 | for (poc = 0; poc < pocno ; ++poc) { |
263 | 0 | OPJ_UINT32 l_tp_num = compno; |
264 | | |
265 | | /* TODO MSD : check why this function cannot fail (cf. v1) */ |
266 | 0 | opj_pi_create_encode(l_pi, l_cp, p_tile_no, poc, l_tp_num, p_tp_pos, p_t2_mode); |
267 | |
|
268 | 0 | if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { |
269 | | /* TODO ADE : add an error */ |
270 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
271 | 0 | return OPJ_FALSE; |
272 | 0 | } |
273 | 0 | while (opj_pi_next(l_current_pi)) { |
274 | 0 | if (l_current_pi->layno < p_maxlayers) { |
275 | 0 | l_nb_bytes = 0; |
276 | |
|
277 | 0 | if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi, |
278 | 0 | l_current_data, &l_nb_bytes, |
279 | 0 | p_max_len, cstr_info, |
280 | 0 | p_t2_mode, |
281 | 0 | p_manager)) { |
282 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
283 | 0 | return OPJ_FALSE; |
284 | 0 | } |
285 | | |
286 | 0 | l_comp_len += l_nb_bytes; |
287 | 0 | l_current_data += l_nb_bytes; |
288 | 0 | p_max_len -= l_nb_bytes; |
289 | |
|
290 | 0 | * p_data_written += l_nb_bytes; |
291 | 0 | } |
292 | 0 | } |
293 | | |
294 | 0 | if (l_cp->m_specific_param.m_enc.m_max_comp_size) { |
295 | 0 | if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) { |
296 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
297 | 0 | return OPJ_FALSE; |
298 | 0 | } |
299 | 0 | } |
300 | | |
301 | 0 | ++l_current_pi; |
302 | 0 | } |
303 | 0 | } |
304 | 0 | } else { /* t2_mode == FINAL_PASS */ |
305 | 0 | opj_pi_create_encode(l_pi, l_cp, p_tile_no, p_pino, p_tp_num, p_tp_pos, |
306 | 0 | p_t2_mode); |
307 | |
|
308 | 0 | l_current_pi = &l_pi[p_pino]; |
309 | 0 | if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { |
310 | | /* TODO ADE : add an error */ |
311 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
312 | 0 | return OPJ_FALSE; |
313 | 0 | } |
314 | | |
315 | 0 | if (p_marker_info && p_marker_info->need_PLT) { |
316 | | /* One time use intended */ |
317 | 0 | assert(p_marker_info->packet_count == 0); |
318 | 0 | assert(p_marker_info->p_packet_size == NULL); |
319 | |
|
320 | 0 | p_marker_info->p_packet_size = (OPJ_UINT32*) opj_malloc( |
321 | 0 | opj_get_encoding_packet_count(l_image, l_cp, p_tile_no) * sizeof(OPJ_UINT32)); |
322 | 0 | if (p_marker_info->p_packet_size == NULL) { |
323 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
324 | 0 | return OPJ_FALSE; |
325 | 0 | } |
326 | 0 | } |
327 | | |
328 | 0 | while (opj_pi_next(l_current_pi)) { |
329 | 0 | if (l_current_pi->layno < p_maxlayers) { |
330 | 0 | l_nb_bytes = 0; |
331 | |
|
332 | 0 | if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi, |
333 | 0 | l_current_data, &l_nb_bytes, p_max_len, |
334 | 0 | cstr_info, p_t2_mode, p_manager)) { |
335 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
336 | 0 | return OPJ_FALSE; |
337 | 0 | } |
338 | | |
339 | 0 | l_current_data += l_nb_bytes; |
340 | 0 | p_max_len -= l_nb_bytes; |
341 | |
|
342 | 0 | * p_data_written += l_nb_bytes; |
343 | |
|
344 | 0 | if (p_marker_info && p_marker_info->need_PLT) { |
345 | 0 | p_marker_info->p_packet_size[p_marker_info->packet_count] = l_nb_bytes; |
346 | 0 | p_marker_info->packet_count ++; |
347 | 0 | } |
348 | | |
349 | | /* INDEX >> */ |
350 | 0 | if (cstr_info) { |
351 | 0 | if (cstr_info->index_write) { |
352 | 0 | opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no]; |
353 | 0 | opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; |
354 | 0 | if (!cstr_info->packno) { |
355 | 0 | info_PK->start_pos = info_TL->end_header + 1; |
356 | 0 | } else { |
357 | 0 | info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC) && |
358 | 0 | info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - |
359 | 0 | 1].end_pos + 1; |
360 | 0 | } |
361 | 0 | info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1; |
362 | 0 | info_PK->end_ph_pos += info_PK->start_pos - |
363 | 0 | 1; /* End of packet header which now only represents the distance |
364 | | to start of packet is incremented by value of start of packet*/ |
365 | 0 | } |
366 | |
|
367 | 0 | cstr_info->packno++; |
368 | 0 | } |
369 | | /* << INDEX */ |
370 | 0 | ++p_tile->packno; |
371 | 0 | } |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
376 | |
|
377 | 0 | return OPJ_TRUE; |
378 | 0 | } |
379 | | |
380 | | /* see issue 80 */ |
381 | | #if 0 |
382 | | #define JAS_FPRINTF fprintf |
383 | | #else |
384 | | /* issue 290 */ |
385 | | static void opj_null_jas_fprintf(FILE* file, const char * format, ...) |
386 | 33.9M | { |
387 | 33.9M | (void)file; |
388 | 33.9M | (void)format; |
389 | 33.9M | } |
390 | 33.9M | #define JAS_FPRINTF opj_null_jas_fprintf |
391 | | #endif |
392 | | |
393 | | OPJ_BOOL opj_t2_decode_packets(opj_tcd_t* tcd, |
394 | | opj_t2_t *p_t2, |
395 | | OPJ_UINT32 p_tile_no, |
396 | | opj_tcd_tile_t *p_tile, |
397 | | OPJ_BYTE *p_src, |
398 | | OPJ_UINT32 * p_data_read, |
399 | | OPJ_UINT32 p_max_len, |
400 | | opj_codestream_index_t *p_cstr_index, |
401 | | opj_event_mgr_t *p_manager) |
402 | 2.62k | { |
403 | 2.62k | OPJ_BYTE *l_current_data = p_src; |
404 | 2.62k | opj_pi_iterator_t *l_pi = 00; |
405 | 2.62k | OPJ_UINT32 pino; |
406 | 2.62k | opj_image_t *l_image = p_t2->image; |
407 | 2.62k | opj_cp_t *l_cp = p_t2->cp; |
408 | 2.62k | opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]); |
409 | 2.62k | OPJ_UINT32 l_nb_bytes_read; |
410 | 2.62k | OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; |
411 | 2.62k | opj_pi_iterator_t *l_current_pi = 00; |
412 | | #ifdef TODO_MSD |
413 | | OPJ_UINT32 curtp = 0; |
414 | | OPJ_UINT32 tp_start_packno; |
415 | | #endif |
416 | 2.62k | opj_packet_info_t *l_pack_info = 00; |
417 | 2.62k | opj_image_comp_t* l_img_comp = 00; |
418 | | |
419 | 2.62k | OPJ_ARG_NOT_USED(p_cstr_index); |
420 | | |
421 | | #ifdef TODO_MSD |
422 | | if (p_cstr_index) { |
423 | | l_pack_info = p_cstr_index->tile_index[p_tile_no].packet; |
424 | | } |
425 | | #endif |
426 | | |
427 | | /* create a packet iterator */ |
428 | 2.62k | l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no, p_manager); |
429 | 2.62k | if (!l_pi) { |
430 | 1 | return OPJ_FALSE; |
431 | 1 | } |
432 | | |
433 | | |
434 | 2.62k | l_current_pi = l_pi; |
435 | | |
436 | 11.1k | for (pino = 0; pino <= l_tcp->numpocs; ++pino) { |
437 | | |
438 | | /* if the resolution needed is too low, one dim of the tilec could be equal to zero |
439 | | * and no packets are used to decode this resolution and |
440 | | * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions |
441 | | * and no l_img_comp->resno_decoded are computed |
442 | | */ |
443 | 8.59k | OPJ_BOOL* first_pass_failed = NULL; |
444 | | |
445 | 8.59k | if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { |
446 | | /* TODO ADE : add an error */ |
447 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
448 | 0 | return OPJ_FALSE; |
449 | 0 | } |
450 | | |
451 | 8.59k | first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL)); |
452 | 8.59k | if (!first_pass_failed) { |
453 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
454 | 0 | return OPJ_FALSE; |
455 | 0 | } |
456 | 8.59k | memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL)); |
457 | | |
458 | 13.7M | while (opj_pi_next(l_current_pi)) { |
459 | 13.6M | OPJ_BOOL skip_packet = OPJ_FALSE; |
460 | 13.6M | JAS_FPRINTF(stderr, |
461 | 13.6M | "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n", |
462 | 13.6M | l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, |
463 | 13.6M | l_current_pi->precno, l_current_pi->layno); |
464 | | |
465 | | /* If the packet layer is greater or equal than the maximum */ |
466 | | /* number of layers, skip the packet */ |
467 | 13.6M | if (l_current_pi->layno >= l_tcp->num_layers_to_decode) { |
468 | 0 | skip_packet = OPJ_TRUE; |
469 | 0 | } |
470 | | /* If the packet resolution number is greater than the minimum */ |
471 | | /* number of resolution allowed, skip the packet */ |
472 | 13.6M | else if (l_current_pi->resno >= |
473 | 13.6M | p_tile->comps[l_current_pi->compno].minimum_num_resolutions) { |
474 | 0 | skip_packet = OPJ_TRUE; |
475 | 13.6M | } else { |
476 | | /* If no precincts of any band intersects the area of interest, */ |
477 | | /* skip the packet */ |
478 | 13.6M | OPJ_UINT32 bandno; |
479 | 13.6M | opj_tcd_tilecomp_t *tilec = &p_tile->comps[l_current_pi->compno]; |
480 | 13.6M | opj_tcd_resolution_t *res = &tilec->resolutions[l_current_pi->resno]; |
481 | | |
482 | 13.6M | skip_packet = OPJ_TRUE; |
483 | 13.7M | for (bandno = 0; bandno < res->numbands; ++bandno) { |
484 | 13.7M | opj_tcd_band_t* band = &res->bands[bandno]; |
485 | 13.7M | opj_tcd_precinct_t* prec = &band->precincts[l_current_pi->precno]; |
486 | | |
487 | 13.7M | if (opj_tcd_is_subband_area_of_interest(tcd, |
488 | 13.7M | l_current_pi->compno, |
489 | 13.7M | l_current_pi->resno, |
490 | 13.7M | band->bandno, |
491 | 13.7M | (OPJ_UINT32)prec->x0, |
492 | 13.7M | (OPJ_UINT32)prec->y0, |
493 | 13.7M | (OPJ_UINT32)prec->x1, |
494 | 13.7M | (OPJ_UINT32)prec->y1)) { |
495 | 13.6M | skip_packet = OPJ_FALSE; |
496 | 13.6M | break; |
497 | 13.6M | } |
498 | 13.7M | } |
499 | | /* |
500 | | printf("packet cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d -> %s\n", |
501 | | l_current_pi->compno, l_current_pi->resno, |
502 | | l_current_pi->precno, l_current_pi->layno, skip_packet ? "skipped" : "kept"); |
503 | | */ |
504 | 13.6M | } |
505 | 13.6M | if (!skip_packet) { |
506 | 13.6M | l_nb_bytes_read = 0; |
507 | | |
508 | 13.6M | first_pass_failed[l_current_pi->compno] = OPJ_FALSE; |
509 | | |
510 | 13.6M | if (! opj_t2_decode_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data, |
511 | 13.6M | &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) { |
512 | 34 | opj_pi_destroy(l_pi, l_nb_pocs); |
513 | 34 | opj_free(first_pass_failed); |
514 | 34 | return OPJ_FALSE; |
515 | 34 | } |
516 | | |
517 | 13.6M | l_img_comp = &(l_image->comps[l_current_pi->compno]); |
518 | 13.6M | l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, |
519 | 13.6M | l_img_comp->resno_decoded); |
520 | 13.6M | } else { |
521 | 1.70k | l_nb_bytes_read = 0; |
522 | 1.70k | if (! opj_t2_skip_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data, |
523 | 1.70k | &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) { |
524 | 0 | opj_pi_destroy(l_pi, l_nb_pocs); |
525 | 0 | opj_free(first_pass_failed); |
526 | 0 | return OPJ_FALSE; |
527 | 0 | } |
528 | 1.70k | } |
529 | | |
530 | 13.6M | if (first_pass_failed[l_current_pi->compno]) { |
531 | 0 | l_img_comp = &(l_image->comps[l_current_pi->compno]); |
532 | 0 | if (l_img_comp->resno_decoded == 0) { |
533 | 0 | l_img_comp->resno_decoded = |
534 | 0 | p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1; |
535 | 0 | } |
536 | 0 | } |
537 | | |
538 | 13.6M | l_current_data += l_nb_bytes_read; |
539 | 13.6M | p_max_len -= l_nb_bytes_read; |
540 | | |
541 | | /* INDEX >> */ |
542 | | #ifdef TODO_MSD |
543 | | if (p_cstr_info) { |
544 | | opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no]; |
545 | | opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno]; |
546 | | tp_start_packno = 0; |
547 | | if (!p_cstr_info->packno) { |
548 | | info_PK->start_pos = info_TL->end_header + 1; |
549 | | } else if (info_TL->packet[p_cstr_info->packno - 1].end_pos >= |
550 | | (OPJ_INT32) |
551 | | p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos) { /* New tile part */ |
552 | | info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - |
553 | | tp_start_packno; /* Number of packets in previous tile-part */ |
554 | | tp_start_packno = p_cstr_info->packno; |
555 | | curtp++; |
556 | | info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header + 1; |
557 | | } else { |
558 | | info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on && |
559 | | info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - |
560 | | 1].end_pos + 1; |
561 | | } |
562 | | info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1; |
563 | | info_PK->end_ph_pos += info_PK->start_pos - |
564 | | 1; /* End of packet header which now only represents the distance */ |
565 | | ++p_cstr_info->packno; |
566 | | } |
567 | | #endif |
568 | | /* << INDEX */ |
569 | 13.6M | } |
570 | 8.55k | ++l_current_pi; |
571 | | |
572 | 8.55k | opj_free(first_pass_failed); |
573 | 8.55k | } |
574 | | /* INDEX >> */ |
575 | | #ifdef TODO_MSD |
576 | | if |
577 | | (p_cstr_info) { |
578 | | p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - |
579 | | tp_start_packno; /* Number of packets in last tile-part */ |
580 | | } |
581 | | #endif |
582 | | /* << INDEX */ |
583 | | |
584 | | /* don't forget to release pi */ |
585 | 2.58k | opj_pi_destroy(l_pi, l_nb_pocs); |
586 | 2.58k | *p_data_read = (OPJ_UINT32)(l_current_data - p_src); |
587 | 2.58k | return OPJ_TRUE; |
588 | 2.62k | } |
589 | | |
590 | | /* ----------------------------------------------------------------------- */ |
591 | | |
592 | | /** |
593 | | * Creates a Tier 2 handle |
594 | | * |
595 | | * @param p_image Source or destination image |
596 | | * @param p_cp Image coding parameters. |
597 | | * @return a new T2 handle if successful, NULL otherwise. |
598 | | */ |
599 | | opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp) |
600 | 2.62k | { |
601 | | /* create the t2 structure */ |
602 | 2.62k | opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1, sizeof(opj_t2_t)); |
603 | 2.62k | if (!l_t2) { |
604 | 0 | return NULL; |
605 | 0 | } |
606 | | |
607 | 2.62k | l_t2->image = p_image; |
608 | 2.62k | l_t2->cp = p_cp; |
609 | | |
610 | 2.62k | return l_t2; |
611 | 2.62k | } |
612 | | |
613 | | void opj_t2_destroy(opj_t2_t *t2) |
614 | 2.62k | { |
615 | 2.62k | if (t2) { |
616 | 2.62k | opj_free(t2); |
617 | 2.62k | } |
618 | 2.62k | } |
619 | | |
620 | | static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* p_t2, |
621 | | opj_tcd_tile_t *p_tile, |
622 | | opj_tcp_t *p_tcp, |
623 | | opj_pi_iterator_t *p_pi, |
624 | | OPJ_BYTE *p_src, |
625 | | OPJ_UINT32 * p_data_read, |
626 | | OPJ_UINT32 p_max_length, |
627 | | opj_packet_info_t *p_pack_info, |
628 | | opj_event_mgr_t *p_manager) |
629 | 13.6M | { |
630 | 13.6M | OPJ_BOOL l_read_data; |
631 | 13.6M | OPJ_UINT32 l_nb_bytes_read = 0; |
632 | 13.6M | OPJ_UINT32 l_nb_total_bytes_read = 0; |
633 | | |
634 | 13.6M | *p_data_read = 0; |
635 | | |
636 | 13.6M | if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src, |
637 | 13.6M | &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) { |
638 | 7 | return OPJ_FALSE; |
639 | 7 | } |
640 | | |
641 | 13.6M | p_src += l_nb_bytes_read; |
642 | 13.6M | l_nb_total_bytes_read += l_nb_bytes_read; |
643 | 13.6M | p_max_length -= l_nb_bytes_read; |
644 | | |
645 | | /* we should read data for the packet */ |
646 | 13.6M | if (l_read_data) { |
647 | 375k | l_nb_bytes_read = 0; |
648 | | |
649 | 375k | if (! opj_t2_read_packet_data(p_t2, p_tile, p_pi, p_src, &l_nb_bytes_read, |
650 | 375k | p_max_length, p_pack_info, p_manager)) { |
651 | 27 | return OPJ_FALSE; |
652 | 27 | } |
653 | | |
654 | 375k | l_nb_total_bytes_read += l_nb_bytes_read; |
655 | 375k | } |
656 | | |
657 | 13.6M | *p_data_read = l_nb_total_bytes_read; |
658 | | |
659 | 13.6M | return OPJ_TRUE; |
660 | 13.6M | } |
661 | | |
662 | | static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno, |
663 | | opj_tcd_tile_t * tile, |
664 | | opj_tcp_t * tcp, |
665 | | opj_pi_iterator_t *pi, |
666 | | OPJ_BYTE *dest, |
667 | | OPJ_UINT32 * p_data_written, |
668 | | OPJ_UINT32 length, |
669 | | opj_codestream_info_t *cstr_info, |
670 | | J2K_T2_MODE p_t2_mode, |
671 | | opj_event_mgr_t *p_manager) |
672 | 0 | { |
673 | 0 | OPJ_UINT32 bandno, cblkno; |
674 | 0 | OPJ_BYTE* c = dest; |
675 | 0 | OPJ_UINT32 l_nb_bytes; |
676 | 0 | OPJ_UINT32 compno = pi->compno; /* component value */ |
677 | 0 | OPJ_UINT32 resno = pi->resno; /* resolution level value */ |
678 | 0 | OPJ_UINT32 precno = pi->precno; /* precinct value */ |
679 | 0 | OPJ_UINT32 layno = pi->layno; /* quality layer value */ |
680 | 0 | OPJ_UINT32 l_nb_blocks; |
681 | 0 | opj_tcd_band_t *band = 00; |
682 | 0 | opj_tcd_cblk_enc_t* cblk = 00; |
683 | 0 | opj_tcd_pass_t *pass = 00; |
684 | |
|
685 | 0 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; |
686 | 0 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; |
687 | |
|
688 | 0 | opj_bio_t *bio = 00; /* BIO component */ |
689 | | #ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION |
690 | | OPJ_BOOL packet_empty = OPJ_TRUE; |
691 | | #else |
692 | 0 | OPJ_BOOL packet_empty = OPJ_FALSE; |
693 | 0 | #endif |
694 | |
|
695 | | #ifdef DEBUG_VERBOSE |
696 | | if (p_t2_mode == FINAL_PASS) { |
697 | | fprintf(stderr, |
698 | | "encode packet compono=%d, resno=%d, precno=%d, layno=%d\n", |
699 | | compno, resno, precno, layno); |
700 | | } |
701 | | #endif |
702 | | |
703 | | /* <SOP 0xff91> */ |
704 | 0 | if (tcp->csty & J2K_CP_CSTY_SOP) { |
705 | 0 | if (length < 6) { |
706 | 0 | if (p_t2_mode == FINAL_PASS) { |
707 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
708 | 0 | "opj_t2_encode_packet(): only %u bytes remaining in " |
709 | 0 | "output buffer. %u needed.\n", |
710 | 0 | length, 6); |
711 | 0 | } |
712 | 0 | return OPJ_FALSE; |
713 | 0 | } |
714 | 0 | c[0] = 255; |
715 | 0 | c[1] = 145; |
716 | 0 | c[2] = 0; |
717 | 0 | c[3] = 4; |
718 | | #if 0 |
719 | | c[4] = (tile->packno % 65536) / 256; |
720 | | c[5] = (tile->packno % 65536) % 256; |
721 | | #else |
722 | 0 | c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */ |
723 | 0 | c[5] = tile->packno & 0xff; |
724 | 0 | #endif |
725 | 0 | c += 6; |
726 | 0 | length -= 6; |
727 | 0 | } |
728 | | /* </SOP> */ |
729 | | |
730 | 0 | if (!layno) { |
731 | 0 | band = res->bands; |
732 | |
|
733 | 0 | for (bandno = 0; bandno < res->numbands; ++bandno, ++band) { |
734 | 0 | opj_tcd_precinct_t *prc; |
735 | | |
736 | | /* Skip empty bands */ |
737 | 0 | if (opj_tcd_is_band_empty(band)) { |
738 | 0 | continue; |
739 | 0 | } |
740 | | |
741 | | /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1294 */ |
742 | | /* but likely not a proper fix. */ |
743 | 0 | if (precno >= res->pw * res->ph) { |
744 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
745 | 0 | "opj_t2_encode_packet(): accessing precno=%u >= %u\n", |
746 | 0 | precno, res->pw * res->ph); |
747 | 0 | return OPJ_FALSE; |
748 | 0 | } |
749 | | |
750 | 0 | prc = &band->precincts[precno]; |
751 | 0 | opj_tgt_reset(prc->incltree); |
752 | 0 | opj_tgt_reset(prc->imsbtree); |
753 | |
|
754 | 0 | l_nb_blocks = prc->cw * prc->ch; |
755 | 0 | for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { |
756 | 0 | cblk = &prc->cblks.enc[cblkno]; |
757 | |
|
758 | 0 | cblk->numpasses = 0; |
759 | 0 | opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps); |
760 | 0 | } |
761 | 0 | } |
762 | 0 | } |
763 | | |
764 | 0 | bio = opj_bio_create(); |
765 | 0 | if (!bio) { |
766 | | /* FIXME event manager error callback */ |
767 | 0 | return OPJ_FALSE; |
768 | 0 | } |
769 | 0 | opj_bio_init_enc(bio, c, length); |
770 | |
|
771 | | #ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION |
772 | | /* WARNING: this code branch is disabled, since it has been reported that */ |
773 | | /* such packets cause decoding issues with cinema J2K hardware */ |
774 | | /* decoders: https://groups.google.com/forum/#!topic/openjpeg/M7M_fLX_Bco */ |
775 | | |
776 | | /* Check if the packet is empty */ |
777 | | /* Note: we could also skip that step and always write a packet header */ |
778 | | band = res->bands; |
779 | | for (bandno = 0; bandno < res->numbands; ++bandno, ++band) { |
780 | | opj_tcd_precinct_t *prc; |
781 | | /* Skip empty bands */ |
782 | | if (opj_tcd_is_band_empty(band)) { |
783 | | continue; |
784 | | } |
785 | | |
786 | | prc = &band->precincts[precno]; |
787 | | l_nb_blocks = prc->cw * prc->ch; |
788 | | cblk = prc->cblks.enc; |
789 | | for (cblkno = 0; cblkno < l_nb_blocks; cblkno++, ++cblk) { |
790 | | opj_tcd_layer_t *layer = &cblk->layers[layno]; |
791 | | |
792 | | /* if cblk not included, go to the next cblk */ |
793 | | if (!layer->numpasses) { |
794 | | continue; |
795 | | } |
796 | | packet_empty = OPJ_FALSE; |
797 | | break; |
798 | | } |
799 | | if (!packet_empty) { |
800 | | break; |
801 | | } |
802 | | } |
803 | | #endif |
804 | 0 | opj_bio_putbit(bio, packet_empty ? 0 : 1); /* Empty header bit */ |
805 | | |
806 | | /* Writing Packet header */ |
807 | 0 | band = res->bands; |
808 | 0 | for (bandno = 0; !packet_empty && |
809 | 0 | bandno < res->numbands; ++bandno, ++band) { |
810 | 0 | opj_tcd_precinct_t *prc; |
811 | | |
812 | | /* Skip empty bands */ |
813 | 0 | if (opj_tcd_is_band_empty(band)) { |
814 | 0 | continue; |
815 | 0 | } |
816 | | |
817 | | /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1297 */ |
818 | | /* but likely not a proper fix. */ |
819 | 0 | if (precno >= res->pw * res->ph) { |
820 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
821 | 0 | "opj_t2_encode_packet(): accessing precno=%u >= %u\n", |
822 | 0 | precno, res->pw * res->ph); |
823 | 0 | return OPJ_FALSE; |
824 | 0 | } |
825 | | |
826 | 0 | prc = &band->precincts[precno]; |
827 | 0 | l_nb_blocks = prc->cw * prc->ch; |
828 | 0 | cblk = prc->cblks.enc; |
829 | |
|
830 | 0 | for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { |
831 | 0 | opj_tcd_layer_t *layer = &cblk->layers[layno]; |
832 | |
|
833 | 0 | if (!cblk->numpasses && layer->numpasses) { |
834 | 0 | opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno); |
835 | 0 | } |
836 | |
|
837 | 0 | ++cblk; |
838 | 0 | } |
839 | |
|
840 | 0 | cblk = prc->cblks.enc; |
841 | 0 | for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) { |
842 | 0 | opj_tcd_layer_t *layer = &cblk->layers[layno]; |
843 | 0 | OPJ_UINT32 increment = 0; |
844 | 0 | OPJ_UINT32 nump = 0; |
845 | 0 | OPJ_UINT32 len = 0, passno; |
846 | 0 | OPJ_UINT32 l_nb_passes; |
847 | | |
848 | | /* cblk inclusion bits */ |
849 | 0 | if (!cblk->numpasses) { |
850 | 0 | opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1)); |
851 | 0 | } else { |
852 | 0 | opj_bio_putbit(bio, layer->numpasses != 0); |
853 | 0 | } |
854 | | |
855 | | /* if cblk not included, go to the next cblk */ |
856 | 0 | if (!layer->numpasses) { |
857 | 0 | ++cblk; |
858 | 0 | continue; |
859 | 0 | } |
860 | | |
861 | | /* if first instance of cblk --> zero bit-planes information */ |
862 | 0 | if (!cblk->numpasses) { |
863 | 0 | cblk->numlenbits = 3; |
864 | 0 | opj_tgt_encode(bio, prc->imsbtree, cblkno, 999); |
865 | 0 | } |
866 | | |
867 | | /* number of coding passes included */ |
868 | 0 | opj_t2_putnumpasses(bio, layer->numpasses); |
869 | 0 | l_nb_passes = cblk->numpasses + layer->numpasses; |
870 | 0 | pass = cblk->passes + cblk->numpasses; |
871 | | |
872 | | /* computation of the increase of the length indicator and insertion in the header */ |
873 | 0 | for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { |
874 | 0 | ++nump; |
875 | 0 | len += pass->len; |
876 | |
|
877 | 0 | if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { |
878 | 0 | increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, |
879 | 0 | opj_int_floorlog2((OPJ_INT32)len) + 1 |
880 | 0 | - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump))); |
881 | 0 | len = 0; |
882 | 0 | nump = 0; |
883 | 0 | } |
884 | |
|
885 | 0 | ++pass; |
886 | 0 | } |
887 | 0 | opj_t2_putcommacode(bio, (OPJ_INT32)increment); |
888 | | |
889 | | /* computation of the new Length indicator */ |
890 | 0 | cblk->numlenbits += increment; |
891 | |
|
892 | 0 | pass = cblk->passes + cblk->numpasses; |
893 | | /* insertion of the codeword segment length */ |
894 | 0 | for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { |
895 | 0 | nump++; |
896 | 0 | len += pass->len; |
897 | |
|
898 | 0 | if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { |
899 | 0 | opj_bio_write(bio, (OPJ_UINT32)len, |
900 | 0 | cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump)); |
901 | 0 | len = 0; |
902 | 0 | nump = 0; |
903 | 0 | } |
904 | 0 | ++pass; |
905 | 0 | } |
906 | |
|
907 | 0 | ++cblk; |
908 | 0 | } |
909 | 0 | } |
910 | | |
911 | 0 | if (!opj_bio_flush(bio)) { |
912 | 0 | opj_bio_destroy(bio); |
913 | 0 | return OPJ_FALSE; /* modified to eliminate longjmp !! */ |
914 | 0 | } |
915 | | |
916 | 0 | l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio); |
917 | 0 | c += l_nb_bytes; |
918 | 0 | length -= l_nb_bytes; |
919 | |
|
920 | 0 | opj_bio_destroy(bio); |
921 | | |
922 | | /* <EPH 0xff92> */ |
923 | 0 | if (tcp->csty & J2K_CP_CSTY_EPH) { |
924 | 0 | if (length < 2) { |
925 | 0 | if (p_t2_mode == FINAL_PASS) { |
926 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
927 | 0 | "opj_t2_encode_packet(): only %u bytes remaining in " |
928 | 0 | "output buffer. %u needed.\n", |
929 | 0 | length, 2); |
930 | 0 | } |
931 | 0 | return OPJ_FALSE; |
932 | 0 | } |
933 | 0 | c[0] = 255; |
934 | 0 | c[1] = 146; |
935 | 0 | c += 2; |
936 | 0 | length -= 2; |
937 | 0 | } |
938 | | /* </EPH> */ |
939 | | |
940 | | /* << INDEX */ |
941 | | /* End of packet header position. Currently only represents the distance to start of packet |
942 | | Will be updated later by incrementing with packet start value*/ |
943 | 0 | if (cstr_info && cstr_info->index_write) { |
944 | 0 | opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; |
945 | 0 | info_PK->end_ph_pos = (OPJ_INT32)(c - dest); |
946 | 0 | } |
947 | | /* INDEX >> */ |
948 | | |
949 | | /* Writing the packet body */ |
950 | 0 | band = res->bands; |
951 | 0 | for (bandno = 0; !packet_empty && bandno < res->numbands; bandno++, ++band) { |
952 | 0 | opj_tcd_precinct_t *prc; |
953 | | |
954 | | /* Skip empty bands */ |
955 | 0 | if (opj_tcd_is_band_empty(band)) { |
956 | 0 | continue; |
957 | 0 | } |
958 | | |
959 | 0 | prc = &band->precincts[precno]; |
960 | 0 | l_nb_blocks = prc->cw * prc->ch; |
961 | 0 | cblk = prc->cblks.enc; |
962 | |
|
963 | 0 | for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { |
964 | 0 | opj_tcd_layer_t *layer = &cblk->layers[layno]; |
965 | |
|
966 | 0 | if (!layer->numpasses) { |
967 | 0 | ++cblk; |
968 | 0 | continue; |
969 | 0 | } |
970 | | |
971 | 0 | if (layer->len > length) { |
972 | 0 | if (p_t2_mode == FINAL_PASS) { |
973 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
974 | 0 | "opj_t2_encode_packet(): only %u bytes remaining in " |
975 | 0 | "output buffer. %u needed.\n", |
976 | 0 | length, layer->len); |
977 | 0 | } |
978 | 0 | return OPJ_FALSE; |
979 | 0 | } |
980 | | |
981 | 0 | if (p_t2_mode == FINAL_PASS) { |
982 | 0 | memcpy(c, layer->data, layer->len); |
983 | 0 | } |
984 | 0 | cblk->numpasses += layer->numpasses; |
985 | 0 | c += layer->len; |
986 | 0 | length -= layer->len; |
987 | | |
988 | | /* << INDEX */ |
989 | 0 | if (cstr_info && cstr_info->index_write) { |
990 | 0 | opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; |
991 | 0 | info_PK->disto += layer->disto; |
992 | 0 | if (cstr_info->D_max < info_PK->disto) { |
993 | 0 | cstr_info->D_max = info_PK->disto; |
994 | 0 | } |
995 | 0 | } |
996 | |
|
997 | 0 | ++cblk; |
998 | | /* INDEX >> */ |
999 | 0 | } |
1000 | 0 | } |
1001 | | |
1002 | 0 | assert(c >= dest); |
1003 | 0 | * p_data_written += (OPJ_UINT32)(c - dest); |
1004 | |
|
1005 | 0 | return OPJ_TRUE; |
1006 | 0 | } |
1007 | | |
1008 | | static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2, |
1009 | | opj_tcd_tile_t *p_tile, |
1010 | | opj_tcp_t *p_tcp, |
1011 | | opj_pi_iterator_t *p_pi, |
1012 | | OPJ_BYTE *p_src, |
1013 | | OPJ_UINT32 * p_data_read, |
1014 | | OPJ_UINT32 p_max_length, |
1015 | | opj_packet_info_t *p_pack_info, |
1016 | | opj_event_mgr_t *p_manager) |
1017 | 1.70k | { |
1018 | 1.70k | OPJ_BOOL l_read_data; |
1019 | 1.70k | OPJ_UINT32 l_nb_bytes_read = 0; |
1020 | 1.70k | OPJ_UINT32 l_nb_total_bytes_read = 0; |
1021 | | |
1022 | 1.70k | *p_data_read = 0; |
1023 | | |
1024 | 1.70k | if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src, |
1025 | 1.70k | &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) { |
1026 | 0 | return OPJ_FALSE; |
1027 | 0 | } |
1028 | | |
1029 | 1.70k | p_src += l_nb_bytes_read; |
1030 | 1.70k | l_nb_total_bytes_read += l_nb_bytes_read; |
1031 | 1.70k | p_max_length -= l_nb_bytes_read; |
1032 | | |
1033 | | /* we should read data for the packet */ |
1034 | 1.70k | if (l_read_data) { |
1035 | 331 | l_nb_bytes_read = 0; |
1036 | | |
1037 | 331 | if (! opj_t2_skip_packet_data(p_t2, p_tile, p_pi, &l_nb_bytes_read, |
1038 | 331 | p_max_length, p_pack_info, p_manager)) { |
1039 | 0 | return OPJ_FALSE; |
1040 | 0 | } |
1041 | | |
1042 | 331 | l_nb_total_bytes_read += l_nb_bytes_read; |
1043 | 331 | } |
1044 | 1.70k | *p_data_read = l_nb_total_bytes_read; |
1045 | | |
1046 | 1.70k | return OPJ_TRUE; |
1047 | 1.70k | } |
1048 | | |
1049 | | |
1050 | | static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2, |
1051 | | opj_tcd_tile_t *p_tile, |
1052 | | opj_tcp_t *p_tcp, |
1053 | | opj_pi_iterator_t *p_pi, |
1054 | | OPJ_BOOL * p_is_data_present, |
1055 | | OPJ_BYTE *p_src_data, |
1056 | | OPJ_UINT32 * p_data_read, |
1057 | | OPJ_UINT32 p_max_length, |
1058 | | opj_packet_info_t *p_pack_info, |
1059 | | opj_event_mgr_t *p_manager) |
1060 | | |
1061 | 13.6M | { |
1062 | | /* loop */ |
1063 | 13.6M | OPJ_UINT32 bandno, cblkno; |
1064 | 13.6M | OPJ_UINT32 l_nb_code_blocks; |
1065 | 13.6M | OPJ_UINT32 l_remaining_length; |
1066 | 13.6M | OPJ_UINT32 l_header_length; |
1067 | 13.6M | OPJ_UINT32 * l_modified_length_ptr = 00; |
1068 | 13.6M | OPJ_BYTE *l_current_data = p_src_data; |
1069 | 13.6M | opj_cp_t *l_cp = p_t2->cp; |
1070 | 13.6M | opj_bio_t *l_bio = 00; /* BIO component */ |
1071 | 13.6M | opj_tcd_band_t *l_band = 00; |
1072 | 13.6M | opj_tcd_cblk_dec_t* l_cblk = 00; |
1073 | 13.6M | opj_tcd_resolution_t* l_res = |
1074 | 13.6M | &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; |
1075 | | |
1076 | 13.6M | OPJ_BYTE *l_header_data = 00; |
1077 | 13.6M | OPJ_BYTE **l_header_data_start = 00; |
1078 | | |
1079 | 13.6M | OPJ_UINT32 l_present; |
1080 | | |
1081 | 13.6M | if (p_pi->layno == 0) { |
1082 | 2.55M | l_band = l_res->bands; |
1083 | | |
1084 | | /* reset tagtrees */ |
1085 | 5.12M | for (bandno = 0; bandno < l_res->numbands; ++bandno) { |
1086 | 2.56M | if (!opj_tcd_is_band_empty(l_band)) { |
1087 | 2.56M | opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; |
1088 | 2.56M | if (!(p_pi->precno < (l_band->precincts_data_size / sizeof( |
1089 | 2.56M | opj_tcd_precinct_t)))) { |
1090 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct\n"); |
1091 | 0 | return OPJ_FALSE; |
1092 | 0 | } |
1093 | | |
1094 | | |
1095 | 2.56M | opj_tgt_reset(l_prc->incltree); |
1096 | 2.56M | opj_tgt_reset(l_prc->imsbtree); |
1097 | 2.56M | l_cblk = l_prc->cblks.dec; |
1098 | | |
1099 | 2.56M | l_nb_code_blocks = l_prc->cw * l_prc->ch; |
1100 | 7.10M | for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { |
1101 | 4.54M | l_cblk->numsegs = 0; |
1102 | 4.54M | l_cblk->real_num_segs = 0; |
1103 | 4.54M | ++l_cblk; |
1104 | 4.54M | } |
1105 | 2.56M | } |
1106 | | |
1107 | 2.56M | ++l_band; |
1108 | 2.56M | } |
1109 | 2.55M | } |
1110 | | |
1111 | | /* SOP markers */ |
1112 | | |
1113 | 13.6M | if (p_tcp->csty & J2K_CP_CSTY_SOP) { |
1114 | | /* SOP markers are allowed (i.e. optional), just warn */ |
1115 | 130k | if (p_max_length < 6) { |
1116 | 117k | opj_event_msg(p_manager, EVT_WARNING, |
1117 | 117k | "Not enough space for expected SOP marker\n"); |
1118 | 117k | } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) { |
1119 | 11.9k | opj_event_msg(p_manager, EVT_WARNING, "Expected SOP marker\n"); |
1120 | 11.9k | } else { |
1121 | 1.01k | l_current_data += 6; |
1122 | 1.01k | } |
1123 | | |
1124 | | /** TODO : check the Nsop value */ |
1125 | 130k | } |
1126 | | |
1127 | | /* |
1128 | | When the marker PPT/PPM is used the packet header are store in PPT/PPM marker |
1129 | | This part deal with this characteristic |
1130 | | step 1: Read packet header in the saved structure |
1131 | | step 2: Return to codestream for decoding |
1132 | | */ |
1133 | | |
1134 | 13.6M | l_bio = opj_bio_create(); |
1135 | 13.6M | if (! l_bio) { |
1136 | 0 | return OPJ_FALSE; |
1137 | 0 | } |
1138 | | |
1139 | 13.6M | if (l_cp->ppm == 1) { /* PPM */ |
1140 | 1.01k | l_header_data_start = &l_cp->ppm_data; |
1141 | 1.01k | l_header_data = *l_header_data_start; |
1142 | 1.01k | l_modified_length_ptr = &(l_cp->ppm_len); |
1143 | | |
1144 | 13.6M | } else if (p_tcp->ppt == 1) { /* PPT */ |
1145 | 117 | l_header_data_start = &(p_tcp->ppt_data); |
1146 | 117 | l_header_data = *l_header_data_start; |
1147 | 117 | l_modified_length_ptr = &(p_tcp->ppt_len); |
1148 | 13.6M | } else { /* Normal Case */ |
1149 | 13.6M | l_header_data_start = &(l_current_data); |
1150 | 13.6M | l_header_data = *l_header_data_start; |
1151 | 13.6M | l_remaining_length = (OPJ_UINT32)(p_src_data + p_max_length - l_header_data); |
1152 | 13.6M | l_modified_length_ptr = &(l_remaining_length); |
1153 | 13.6M | } |
1154 | | |
1155 | 13.6M | opj_bio_init_dec(l_bio, l_header_data, *l_modified_length_ptr); |
1156 | | |
1157 | 13.6M | l_present = opj_bio_read(l_bio, 1); |
1158 | 13.6M | JAS_FPRINTF(stderr, "present=%d \n", l_present); |
1159 | 13.6M | if (!l_present) { |
1160 | | /* TODO MSD: no test to control the output of this function*/ |
1161 | 13.3M | opj_bio_inalign(l_bio); |
1162 | 13.3M | l_header_data += opj_bio_numbytes(l_bio); |
1163 | 13.3M | opj_bio_destroy(l_bio); |
1164 | | |
1165 | | /* EPH markers */ |
1166 | 13.3M | if (p_tcp->csty & J2K_CP_CSTY_EPH) { |
1167 | | /* EPH markers are required */ |
1168 | 27 | if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - |
1169 | 27 | *l_header_data_start)) < 2U) { |
1170 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
1171 | 0 | "Not enough space for required EPH marker\n"); |
1172 | 0 | return OPJ_FALSE; |
1173 | 27 | } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { |
1174 | 0 | opj_event_msg(p_manager, EVT_ERROR, "Expected EPH marker\n"); |
1175 | 0 | return OPJ_FALSE; |
1176 | 27 | } else { |
1177 | 27 | l_header_data += 2; |
1178 | 27 | } |
1179 | 27 | } |
1180 | | |
1181 | 13.3M | l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start); |
1182 | 13.3M | *l_modified_length_ptr -= l_header_length; |
1183 | 13.3M | *l_header_data_start += l_header_length; |
1184 | | |
1185 | | /* << INDEX */ |
1186 | | /* End of packet header position. Currently only represents the distance to start of packet |
1187 | | Will be updated later by incrementing with packet start value */ |
1188 | 13.3M | if (p_pack_info) { |
1189 | 0 | p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); |
1190 | 0 | } |
1191 | | /* INDEX >> */ |
1192 | | |
1193 | 13.3M | * p_is_data_present = OPJ_FALSE; |
1194 | 13.3M | *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data); |
1195 | 13.3M | return OPJ_TRUE; |
1196 | 13.3M | } |
1197 | | |
1198 | 376k | l_band = l_res->bands; |
1199 | 770k | for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band) { |
1200 | 394k | opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]); |
1201 | | |
1202 | 394k | if (opj_tcd_is_band_empty(l_band)) { |
1203 | 4.61k | continue; |
1204 | 4.61k | } |
1205 | | |
1206 | 389k | l_nb_code_blocks = l_prc->cw * l_prc->ch; |
1207 | 389k | l_cblk = l_prc->cblks.dec; |
1208 | 5.98M | for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) { |
1209 | 5.60M | OPJ_UINT32 l_included, l_increment, l_segno; |
1210 | 5.60M | OPJ_INT32 n; |
1211 | | |
1212 | | /* if cblk not yet included before --> inclusion tagtree */ |
1213 | 5.60M | if (!l_cblk->numsegs) { |
1214 | 5.25M | l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, |
1215 | 5.25M | (OPJ_INT32)(p_pi->layno + 1)); |
1216 | | /* else one bit */ |
1217 | 5.25M | } else { |
1218 | 349k | l_included = opj_bio_read(l_bio, 1); |
1219 | 349k | } |
1220 | | |
1221 | | /* if cblk not included */ |
1222 | 5.60M | if (!l_included) { |
1223 | 4.90M | l_cblk->numnewpasses = 0; |
1224 | 4.90M | ++l_cblk; |
1225 | 4.90M | JAS_FPRINTF(stderr, "included=%d \n", l_included); |
1226 | 4.90M | continue; |
1227 | 4.90M | } |
1228 | | |
1229 | | /* if cblk not yet included --> zero-bitplane tagtree */ |
1230 | 693k | if (!l_cblk->numsegs) { |
1231 | 479k | OPJ_UINT32 i = 0; |
1232 | | |
1233 | 2.80M | while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) { |
1234 | 2.32M | ++i; |
1235 | 2.32M | } |
1236 | 479k | l_cblk->Mb = (OPJ_UINT32)l_band->numbps; |
1237 | 479k | if ((OPJ_UINT32)l_band->numbps + 1 < i) { |
1238 | | /* Not totally sure what we should do in that situation, |
1239 | | * but that avoids the integer overflow of |
1240 | | * https://github.com/uclouvain/openjpeg/pull/1488 |
1241 | | * while keeping the regression test suite happy. |
1242 | | */ |
1243 | 8.74k | l_cblk->numbps = (OPJ_UINT32)(l_band->numbps + 1 - (int)i); |
1244 | 470k | } else { |
1245 | 470k | l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i; |
1246 | 470k | } |
1247 | 479k | l_cblk->numlenbits = 3; |
1248 | 479k | } |
1249 | | |
1250 | | /* number of coding passes */ |
1251 | 693k | l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio); |
1252 | 693k | l_increment = opj_t2_getcommacode(l_bio); |
1253 | | |
1254 | | /* length indicator increment */ |
1255 | 693k | l_cblk->numlenbits += l_increment; |
1256 | 693k | l_segno = 0; |
1257 | | |
1258 | 693k | if (!l_cblk->numsegs) { |
1259 | 479k | if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) { |
1260 | 0 | opj_bio_destroy(l_bio); |
1261 | 0 | return OPJ_FALSE; |
1262 | 0 | } |
1263 | 479k | } else { |
1264 | 213k | l_segno = l_cblk->numsegs - 1; |
1265 | 213k | if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) { |
1266 | 12.1k | ++l_segno; |
1267 | 12.1k | if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { |
1268 | 0 | opj_bio_destroy(l_bio); |
1269 | 0 | return OPJ_FALSE; |
1270 | 0 | } |
1271 | 12.1k | } |
1272 | 213k | } |
1273 | 693k | n = (OPJ_INT32)l_cblk->numnewpasses; |
1274 | | |
1275 | 693k | if ((p_tcp->tccps[p_pi->compno].cblksty & J2K_CCP_CBLKSTY_HT) != 0) |
1276 | 8.47k | do { |
1277 | 8.47k | OPJ_UINT32 bit_number; |
1278 | 8.47k | l_cblk->segs[l_segno].numnewpasses = l_segno == 0 ? 1 : (OPJ_UINT32)n; |
1279 | 8.47k | bit_number = l_cblk->numlenbits + opj_uint_floorlog2( |
1280 | 8.47k | l_cblk->segs[l_segno].numnewpasses); |
1281 | 8.47k | if (bit_number > 32) { |
1282 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
1283 | 0 | "Invalid bit number %d in opj_t2_read_packet_header()\n", |
1284 | 0 | bit_number); |
1285 | 0 | opj_bio_destroy(l_bio); |
1286 | 0 | return OPJ_FALSE; |
1287 | 0 | } |
1288 | 8.47k | l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number); |
1289 | 8.47k | JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n", |
1290 | 8.47k | l_included, l_cblk->segs[l_segno].numnewpasses, l_increment, |
1291 | 8.47k | l_cblk->segs[l_segno].newlen); |
1292 | | |
1293 | 8.47k | n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses; |
1294 | 8.47k | if (n > 0) { |
1295 | 678 | ++l_segno; |
1296 | | |
1297 | 678 | if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { |
1298 | 0 | opj_bio_destroy(l_bio); |
1299 | 0 | return OPJ_FALSE; |
1300 | 0 | } |
1301 | 678 | } |
1302 | 8.47k | } while (n > 0); |
1303 | 685k | else |
1304 | 848k | do { |
1305 | 848k | OPJ_UINT32 bit_number; |
1306 | 848k | l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)( |
1307 | 848k | l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n); |
1308 | 848k | bit_number = l_cblk->numlenbits + opj_uint_floorlog2( |
1309 | 848k | l_cblk->segs[l_segno].numnewpasses); |
1310 | 848k | if (bit_number > 32) { |
1311 | 4 | opj_event_msg(p_manager, EVT_ERROR, |
1312 | 4 | "Invalid bit number %d in opj_t2_read_packet_header()\n", |
1313 | 4 | bit_number); |
1314 | 4 | opj_bio_destroy(l_bio); |
1315 | 4 | return OPJ_FALSE; |
1316 | 4 | } |
1317 | 848k | l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number); |
1318 | 848k | JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n", |
1319 | 848k | l_included, l_cblk->segs[l_segno].numnewpasses, l_increment, |
1320 | 848k | l_cblk->segs[l_segno].newlen); |
1321 | | |
1322 | 848k | n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses; |
1323 | 848k | if (n > 0) { |
1324 | 162k | ++l_segno; |
1325 | | |
1326 | 162k | if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { |
1327 | 0 | opj_bio_destroy(l_bio); |
1328 | 0 | return OPJ_FALSE; |
1329 | 0 | } |
1330 | 162k | } |
1331 | 848k | } while (n > 0); |
1332 | | |
1333 | 693k | ++l_cblk; |
1334 | 693k | } |
1335 | 389k | } |
1336 | | |
1337 | 376k | if (!opj_bio_inalign(l_bio)) { |
1338 | 0 | opj_bio_destroy(l_bio); |
1339 | 0 | return OPJ_FALSE; |
1340 | 0 | } |
1341 | | |
1342 | 376k | l_header_data += opj_bio_numbytes(l_bio); |
1343 | 376k | opj_bio_destroy(l_bio); |
1344 | | |
1345 | | /* EPH markers */ |
1346 | 376k | if (p_tcp->csty & J2K_CP_CSTY_EPH) { |
1347 | | /* EPH markers are required */ |
1348 | 1.09k | if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - |
1349 | 1.09k | *l_header_data_start)) < 2U) { |
1350 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
1351 | 0 | "Not enough space for required EPH marker\n"); |
1352 | 0 | return OPJ_FALSE; |
1353 | 1.09k | } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { |
1354 | 3 | opj_event_msg(p_manager, EVT_ERROR, "Expected EPH marker\n"); |
1355 | 3 | return OPJ_FALSE; |
1356 | 1.08k | } else { |
1357 | 1.08k | l_header_data += 2; |
1358 | 1.08k | } |
1359 | 1.09k | } |
1360 | | |
1361 | 376k | l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start); |
1362 | 376k | JAS_FPRINTF(stderr, "hdrlen=%d \n", l_header_length); |
1363 | 376k | if (!l_header_length) { |
1364 | 0 | return OPJ_FALSE; |
1365 | 0 | } |
1366 | 376k | JAS_FPRINTF(stderr, "packet body\n"); |
1367 | 376k | *l_modified_length_ptr -= l_header_length; |
1368 | 376k | *l_header_data_start += l_header_length; |
1369 | | |
1370 | | /* << INDEX */ |
1371 | | /* End of packet header position. Currently only represents the distance to start of packet |
1372 | | Will be updated later by incrementing with packet start value */ |
1373 | 376k | if (p_pack_info) { |
1374 | 0 | p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); |
1375 | 0 | } |
1376 | | /* INDEX >> */ |
1377 | | |
1378 | 376k | *p_is_data_present = OPJ_TRUE; |
1379 | 376k | *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data); |
1380 | | |
1381 | 376k | return OPJ_TRUE; |
1382 | 376k | } |
1383 | | |
1384 | | static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2, |
1385 | | opj_tcd_tile_t *p_tile, |
1386 | | opj_pi_iterator_t *p_pi, |
1387 | | OPJ_BYTE *p_src_data, |
1388 | | OPJ_UINT32 * p_data_read, |
1389 | | OPJ_UINT32 p_max_length, |
1390 | | opj_packet_info_t *pack_info, |
1391 | | opj_event_mgr_t* p_manager) |
1392 | 375k | { |
1393 | 375k | OPJ_UINT32 bandno, cblkno; |
1394 | 375k | OPJ_UINT32 l_nb_code_blocks; |
1395 | 375k | OPJ_BYTE *l_current_data = p_src_data; |
1396 | 375k | opj_tcd_band_t *l_band = 00; |
1397 | 375k | opj_tcd_cblk_dec_t* l_cblk = 00; |
1398 | 375k | opj_tcd_resolution_t* l_res = |
1399 | 375k | &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; |
1400 | 375k | OPJ_BOOL partial_buffer = OPJ_FALSE; |
1401 | | |
1402 | 375k | OPJ_ARG_NOT_USED(p_t2); |
1403 | 375k | OPJ_ARG_NOT_USED(pack_info); |
1404 | | |
1405 | 375k | l_band = l_res->bands; |
1406 | 769k | for (bandno = 0; bandno < l_res->numbands; ++bandno) { |
1407 | 393k | opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; |
1408 | | |
1409 | 393k | if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) { |
1410 | 3.61k | ++l_band; |
1411 | 3.61k | continue; |
1412 | 3.61k | } |
1413 | | |
1414 | 389k | l_nb_code_blocks = l_prc->cw * l_prc->ch; |
1415 | 389k | l_cblk = l_prc->cblks.dec; |
1416 | | |
1417 | 5.98M | for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno, ++l_cblk) { |
1418 | 5.59M | opj_tcd_seg_t *l_seg = 00; |
1419 | | |
1420 | 5.59M | if (!l_cblk->numnewpasses) { |
1421 | | /* nothing to do */ |
1422 | 4.90M | continue; |
1423 | 4.90M | } |
1424 | | |
1425 | 692k | if (partial_buffer || l_cblk->corrupted) { |
1426 | | /* if a previous segment in this packet couldn't be decoded, |
1427 | | * or if this code block was corrupted in a previous layer, |
1428 | | * then mark it as corrupted. |
1429 | | */ |
1430 | 0 | l_cblk->numchunks = 0; |
1431 | 0 | l_cblk->corrupted = OPJ_TRUE; |
1432 | 0 | continue; |
1433 | 0 | } |
1434 | | |
1435 | 692k | if (!l_cblk->numsegs) { |
1436 | 478k | l_seg = l_cblk->segs; |
1437 | 478k | ++l_cblk->numsegs; |
1438 | 478k | } else { |
1439 | 213k | l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; |
1440 | | |
1441 | 213k | if (l_seg->numpasses == l_seg->maxpasses) { |
1442 | 12.1k | ++l_seg; |
1443 | 12.1k | ++l_cblk->numsegs; |
1444 | 12.1k | } |
1445 | 213k | } |
1446 | | |
1447 | 853k | do { |
1448 | | /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */ |
1449 | 853k | if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) < |
1450 | 853k | (OPJ_SIZE_T)l_current_data) || |
1451 | 853k | (l_current_data + l_seg->newlen > p_src_data + p_max_length) || |
1452 | 853k | (partial_buffer)) { |
1453 | 27 | if (p_t2->cp->strict) { |
1454 | 27 | opj_event_msg(p_manager, EVT_ERROR, |
1455 | 27 | "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", |
1456 | 27 | l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, |
1457 | 27 | p_pi->compno); |
1458 | 27 | return OPJ_FALSE; |
1459 | 27 | } else { |
1460 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
1461 | 0 | "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", |
1462 | 0 | l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, |
1463 | 0 | p_pi->compno); |
1464 | | /* skip this codeblock (and following ones in this |
1465 | | * packet) since it is a partial read |
1466 | | */ |
1467 | 0 | partial_buffer = OPJ_TRUE; |
1468 | 0 | l_cblk->corrupted = OPJ_TRUE; |
1469 | 0 | l_cblk->numchunks = 0; |
1470 | 0 | break; |
1471 | 0 | } |
1472 | 27 | } |
1473 | | |
1474 | | #ifdef USE_JPWL |
1475 | | /* we need here a j2k handle to verify if making a check to |
1476 | | the validity of cblocks parameters is selected from user (-W) */ |
1477 | | |
1478 | | /* let's check that we are not exceeding */ |
1479 | | if ((l_cblk->len + l_seg->newlen) > 8192) { |
1480 | | opj_event_msg(p_manager, EVT_WARNING, |
1481 | | "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", |
1482 | | l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); |
1483 | | if (!JPWL_ASSUME) { |
1484 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
1485 | | return OPJ_FALSE; |
1486 | | } |
1487 | | l_seg->newlen = 8192 - l_cblk->len; |
1488 | | opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n", |
1489 | | l_seg->newlen); |
1490 | | break; |
1491 | | }; |
1492 | | |
1493 | | #endif /* USE_JPWL */ |
1494 | | |
1495 | 853k | if (l_cblk->numchunks == l_cblk->numchunksalloc) { |
1496 | 639k | OPJ_UINT32 l_numchunksalloc = l_cblk->numchunksalloc * 2 + 1; |
1497 | 639k | opj_tcd_seg_data_chunk_t* l_chunks = |
1498 | 639k | (opj_tcd_seg_data_chunk_t*)opj_realloc(l_cblk->chunks, |
1499 | 639k | l_numchunksalloc * sizeof(opj_tcd_seg_data_chunk_t)); |
1500 | 639k | if (l_chunks == NULL) { |
1501 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
1502 | 0 | "cannot allocate opj_tcd_seg_data_chunk_t* array"); |
1503 | 0 | return OPJ_FALSE; |
1504 | 0 | } |
1505 | 639k | l_cblk->chunks = l_chunks; |
1506 | 639k | l_cblk->numchunksalloc = l_numchunksalloc; |
1507 | 639k | } |
1508 | | |
1509 | 853k | l_cblk->chunks[l_cblk->numchunks].data = l_current_data; |
1510 | 853k | l_cblk->chunks[l_cblk->numchunks].len = l_seg->newlen; |
1511 | 853k | l_cblk->numchunks ++; |
1512 | | |
1513 | 853k | l_current_data += l_seg->newlen; |
1514 | 853k | l_seg->len += l_seg->newlen; |
1515 | 853k | l_seg->numpasses += l_seg->numnewpasses; |
1516 | 853k | l_cblk->numnewpasses -= l_seg->numnewpasses; |
1517 | | |
1518 | 853k | l_seg->real_num_passes = l_seg->numpasses; |
1519 | | |
1520 | 853k | if (l_cblk->numnewpasses > 0) { |
1521 | 161k | ++l_seg; |
1522 | 161k | ++l_cblk->numsegs; |
1523 | 161k | } |
1524 | 853k | } while (l_cblk->numnewpasses > 0); |
1525 | | |
1526 | 691k | l_cblk->real_num_segs = l_cblk->numsegs; |
1527 | | |
1528 | 691k | } /* next code_block */ |
1529 | | |
1530 | 389k | ++l_band; |
1531 | 389k | } |
1532 | | |
1533 | | // return the number of bytes read |
1534 | 375k | if (partial_buffer) { |
1535 | 0 | *(p_data_read) = p_max_length; |
1536 | 375k | } else { |
1537 | 375k | *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data); |
1538 | 375k | } |
1539 | | |
1540 | 375k | return OPJ_TRUE; |
1541 | 375k | } |
1542 | | |
1543 | | static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2, |
1544 | | opj_tcd_tile_t *p_tile, |
1545 | | opj_pi_iterator_t *p_pi, |
1546 | | OPJ_UINT32 * p_data_read, |
1547 | | OPJ_UINT32 p_max_length, |
1548 | | opj_packet_info_t *pack_info, |
1549 | | opj_event_mgr_t *p_manager) |
1550 | 331 | { |
1551 | 331 | OPJ_UINT32 bandno, cblkno; |
1552 | 331 | OPJ_UINT32 l_nb_code_blocks; |
1553 | 331 | opj_tcd_band_t *l_band = 00; |
1554 | 331 | opj_tcd_cblk_dec_t* l_cblk = 00; |
1555 | 331 | opj_tcd_resolution_t* l_res = |
1556 | 331 | &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; |
1557 | | |
1558 | 331 | OPJ_ARG_NOT_USED(p_t2); |
1559 | 331 | OPJ_ARG_NOT_USED(pack_info); |
1560 | | |
1561 | 331 | *p_data_read = 0; |
1562 | 331 | l_band = l_res->bands; |
1563 | | |
1564 | 1.32k | for (bandno = 0; bandno < l_res->numbands; ++bandno) { |
1565 | 993 | opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; |
1566 | | |
1567 | 993 | if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) { |
1568 | 993 | ++l_band; |
1569 | 993 | continue; |
1570 | 993 | } |
1571 | | |
1572 | 0 | l_nb_code_blocks = l_prc->cw * l_prc->ch; |
1573 | 0 | l_cblk = l_prc->cblks.dec; |
1574 | |
|
1575 | 0 | for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { |
1576 | 0 | opj_tcd_seg_t *l_seg = 00; |
1577 | |
|
1578 | 0 | if (!l_cblk->numnewpasses) { |
1579 | | /* nothing to do */ |
1580 | 0 | ++l_cblk; |
1581 | 0 | continue; |
1582 | 0 | } |
1583 | | |
1584 | 0 | if (!l_cblk->numsegs) { |
1585 | 0 | l_seg = l_cblk->segs; |
1586 | 0 | ++l_cblk->numsegs; |
1587 | 0 | } else { |
1588 | 0 | l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; |
1589 | |
|
1590 | 0 | if (l_seg->numpasses == l_seg->maxpasses) { |
1591 | 0 | ++l_seg; |
1592 | 0 | ++l_cblk->numsegs; |
1593 | 0 | } |
1594 | 0 | } |
1595 | |
|
1596 | 0 | do { |
1597 | | /* Check possible overflow then size */ |
1598 | 0 | if (((*p_data_read + l_seg->newlen) < (*p_data_read)) || |
1599 | 0 | ((*p_data_read + l_seg->newlen) > p_max_length)) { |
1600 | 0 | if (p_t2->cp->strict) { |
1601 | 0 | opj_event_msg(p_manager, EVT_ERROR, |
1602 | 0 | "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", |
1603 | 0 | l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, |
1604 | 0 | p_pi->compno); |
1605 | 0 | return OPJ_FALSE; |
1606 | 0 | } else { |
1607 | 0 | opj_event_msg(p_manager, EVT_WARNING, |
1608 | 0 | "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", |
1609 | 0 | l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, |
1610 | 0 | p_pi->compno); |
1611 | |
|
1612 | 0 | *p_data_read = p_max_length; |
1613 | 0 | return OPJ_TRUE; |
1614 | 0 | } |
1615 | 0 | } |
1616 | | |
1617 | | #ifdef USE_JPWL |
1618 | | /* we need here a j2k handle to verify if making a check to |
1619 | | the validity of cblocks parameters is selected from user (-W) */ |
1620 | | |
1621 | | /* let's check that we are not exceeding */ |
1622 | | if ((l_cblk->len + l_seg->newlen) > 8192) { |
1623 | | opj_event_msg(p_manager, EVT_WARNING, |
1624 | | "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", |
1625 | | l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); |
1626 | | if (!JPWL_ASSUME) { |
1627 | | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
1628 | | return -999; |
1629 | | } |
1630 | | l_seg->newlen = 8192 - l_cblk->len; |
1631 | | opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n", |
1632 | | l_seg->newlen); |
1633 | | break; |
1634 | | }; |
1635 | | |
1636 | | #endif /* USE_JPWL */ |
1637 | 0 | JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, |
1638 | 0 | l_seg->newlen); |
1639 | 0 | *(p_data_read) += l_seg->newlen; |
1640 | |
|
1641 | 0 | l_seg->numpasses += l_seg->numnewpasses; |
1642 | 0 | l_cblk->numnewpasses -= l_seg->numnewpasses; |
1643 | 0 | if (l_cblk->numnewpasses > 0) { |
1644 | 0 | ++l_seg; |
1645 | 0 | ++l_cblk->numsegs; |
1646 | 0 | } |
1647 | 0 | } while (l_cblk->numnewpasses > 0); |
1648 | | |
1649 | 0 | ++l_cblk; |
1650 | 0 | } |
1651 | | |
1652 | 0 | ++l_band; |
1653 | 0 | } |
1654 | | |
1655 | 331 | return OPJ_TRUE; |
1656 | 331 | } |
1657 | | |
1658 | | |
1659 | | static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk, |
1660 | | OPJ_UINT32 index, |
1661 | | OPJ_UINT32 cblksty, |
1662 | | OPJ_UINT32 first) |
1663 | 655k | { |
1664 | 655k | opj_tcd_seg_t* seg = 00; |
1665 | 655k | OPJ_UINT32 l_nb_segs = index + 1; |
1666 | | |
1667 | 655k | if (l_nb_segs > cblk->m_current_max_segs) { |
1668 | 9.08k | opj_tcd_seg_t* new_segs; |
1669 | 9.08k | OPJ_UINT32 l_m_current_max_segs = cblk->m_current_max_segs + |
1670 | 9.08k | OPJ_J2K_DEFAULT_NB_SEGS; |
1671 | | |
1672 | 9.08k | new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, |
1673 | 9.08k | l_m_current_max_segs * sizeof(opj_tcd_seg_t)); |
1674 | 9.08k | if (! new_segs) { |
1675 | | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */ |
1676 | 0 | return OPJ_FALSE; |
1677 | 0 | } |
1678 | 9.08k | cblk->segs = new_segs; |
1679 | 9.08k | memset(new_segs + cblk->m_current_max_segs, |
1680 | 9.08k | 0, OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t)); |
1681 | 9.08k | cblk->m_current_max_segs = l_m_current_max_segs; |
1682 | 9.08k | } |
1683 | | |
1684 | 655k | seg = &cblk->segs[index]; |
1685 | 655k | opj_tcd_reinit_segment(seg); |
1686 | | |
1687 | 655k | if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { |
1688 | 44.1k | seg->maxpasses = 1; |
1689 | 611k | } else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { |
1690 | 252k | if (first) { |
1691 | 111k | seg->maxpasses = 10; |
1692 | 141k | } else { |
1693 | 141k | seg->maxpasses = (((seg - 1)->maxpasses == 1) || |
1694 | 79.4k | ((seg - 1)->maxpasses == 10)) ? 2 : 1; |
1695 | 141k | } |
1696 | 358k | } else { |
1697 | | /* See paragraph "B.10.6 Number of coding passes" of the standard. |
1698 | | * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37, |
1699 | | * Mb being the maximum number of bit-planes available for the |
1700 | | * representation of coefficients in the sub-band */ |
1701 | 358k | seg->maxpasses = 109; |
1702 | 358k | } |
1703 | | |
1704 | 655k | return OPJ_TRUE; |
1705 | 655k | } |