/src/jasper/src/libjasper/jpc/jpc_t2enc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1999-2000 Image Power, Inc. and the University of |
3 | | * British Columbia. |
4 | | * Copyright (c) 2001-2003 Michael David Adams. |
5 | | * All rights reserved. |
6 | | */ |
7 | | |
8 | | /* __START_OF_JASPER_LICENSE__ |
9 | | * |
10 | | * JasPer License Version 2.0 |
11 | | * |
12 | | * Copyright (c) 2001-2006 Michael David Adams |
13 | | * Copyright (c) 1999-2000 Image Power, Inc. |
14 | | * Copyright (c) 1999-2000 The University of British Columbia |
15 | | * |
16 | | * All rights reserved. |
17 | | * |
18 | | * Permission is hereby granted, free of charge, to any person (the |
19 | | * "User") obtaining a copy of this software and associated documentation |
20 | | * files (the "Software"), to deal in the Software without restriction, |
21 | | * including without limitation the rights to use, copy, modify, merge, |
22 | | * publish, distribute, and/or sell copies of the Software, and to permit |
23 | | * persons to whom the Software is furnished to do so, subject to the |
24 | | * following conditions: |
25 | | * |
26 | | * 1. The above copyright notices and this permission notice (which |
27 | | * includes the disclaimer below) shall be included in all copies or |
28 | | * substantial portions of the Software. |
29 | | * |
30 | | * 2. The name of a copyright holder shall not be used to endorse or |
31 | | * promote products derived from the Software without specific prior |
32 | | * written permission. |
33 | | * |
34 | | * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS |
35 | | * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
36 | | * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS |
37 | | * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
38 | | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
39 | | * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO |
40 | | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL |
41 | | * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING |
42 | | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
43 | | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
44 | | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE |
45 | | * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE |
46 | | * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. |
47 | | * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS |
48 | | * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL |
49 | | * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS |
50 | | * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE |
51 | | * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE |
52 | | * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL |
53 | | * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, |
54 | | * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL |
55 | | * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH |
56 | | * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, |
57 | | * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH |
58 | | * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY |
59 | | * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. |
60 | | * |
61 | | * __END_OF_JASPER_LICENSE__ |
62 | | */ |
63 | | |
64 | | /* |
65 | | * Tier 2 Encoder |
66 | | * |
67 | | * $Id$ |
68 | | */ |
69 | | |
70 | | /******************************************************************************\ |
71 | | * Includes. |
72 | | \******************************************************************************/ |
73 | | |
74 | | #include "jpc_t2enc.h" |
75 | | #include "jpc_t2cod.h" |
76 | | #include "jpc_tagtree.h" |
77 | | #include "jpc_enc.h" |
78 | | #include "jpc_math.h" |
79 | | |
80 | | #include "jasper/jas_malloc.h" |
81 | | #include "jasper/jas_math.h" |
82 | | #include "jasper/jas_debug.h" |
83 | | |
84 | | #include <stdio.h> |
85 | | #include <stdlib.h> |
86 | | #include <assert.h> |
87 | | |
88 | | /******************************************************************************\ |
89 | | * Code. |
90 | | \******************************************************************************/ |
91 | | |
92 | | static int jpc_putcommacode(jpc_bitstream_t *out, int n) |
93 | 0 | { |
94 | 0 | assert(n >= 0); |
95 | | |
96 | 0 | while (--n >= 0) { |
97 | 0 | if (jpc_bitstream_putbit(out, 1) == EOF) { |
98 | 0 | return -1; |
99 | 0 | } |
100 | 0 | } |
101 | 0 | if (jpc_bitstream_putbit(out, 0) == EOF) { |
102 | 0 | return -1; |
103 | 0 | } |
104 | 0 | return 0; |
105 | 0 | } |
106 | | |
107 | | static int jpc_putnumnewpasses(jpc_bitstream_t *out, int n) |
108 | 0 | { |
109 | 0 | int ret; |
110 | |
|
111 | 0 | if (n <= 0) { |
112 | 0 | return -1; |
113 | 0 | } else if (n == 1) { |
114 | 0 | ret = jpc_bitstream_putbit(out, 0); |
115 | 0 | } else if (n == 2) { |
116 | 0 | ret = jpc_bitstream_putbits(out, 2, 2); |
117 | 0 | } else if (n <= 5) { |
118 | 0 | ret = jpc_bitstream_putbits(out, 4, 0xc | (n - 3)); |
119 | 0 | } else if (n <= 36) { |
120 | 0 | ret = jpc_bitstream_putbits(out, 9, 0x1e0 | (n - 6)); |
121 | 0 | } else if (n <= 164) { |
122 | 0 | ret = jpc_bitstream_putbits(out, 16, 0xff80 | (n - 37)); |
123 | 0 | } else { |
124 | | /* The standard has no provision for encoding a larger value. |
125 | | In practice, however, it is highly unlikely that this |
126 | | limitation will ever be encountered. */ |
127 | 0 | return -1; |
128 | 0 | } |
129 | | |
130 | 0 | return (ret != EOF) ? 0 : (-1); |
131 | 0 | } |
132 | | |
133 | | int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out) |
134 | 0 | { |
135 | 0 | jpc_enc_tile_t *tile; |
136 | 0 | jpc_pi_t *pi; |
137 | |
|
138 | 0 | tile = enc->curtile; |
139 | |
|
140 | 0 | jpc_init_t2state(enc, false); |
141 | 0 | pi = tile->pi; |
142 | 0 | jpc_pi_init(pi); |
143 | |
|
144 | 0 | if (!jpc_pi_next(pi)) { |
145 | 0 | for (;;) { |
146 | 0 | if (jpc_enc_encpkt(enc, out, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi), |
147 | 0 | jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) { |
148 | 0 | return -1; |
149 | 0 | } |
150 | 0 | if (jpc_pi_next(pi)) { |
151 | 0 | break; |
152 | 0 | } |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, unsigned compno, unsigned lvlno, unsigned prcno, unsigned lyrno) |
160 | 0 | { |
161 | 0 | jpc_enc_tcmpt_t *comp; |
162 | 0 | jpc_enc_rlvl_t *lvl; |
163 | 0 | jpc_enc_band_t *band; |
164 | 0 | jpc_enc_band_t *endbands; |
165 | 0 | jpc_enc_cblk_t *cblk; |
166 | 0 | jpc_enc_cblk_t *endcblks; |
167 | 0 | jpc_bitstream_t *outb; |
168 | 0 | jpc_enc_pass_t *pass; |
169 | 0 | jpc_enc_pass_t *startpass; |
170 | 0 | jpc_enc_pass_t *lastpass; |
171 | 0 | jpc_enc_pass_t *endpass; |
172 | 0 | jpc_enc_pass_t *endpasses; |
173 | 0 | int i; |
174 | 0 | int ret; |
175 | 0 | jpc_tagtreenode_t *leaf; |
176 | 0 | int t1; |
177 | 0 | int t2; |
178 | 0 | jpc_enc_tile_t *tile; |
179 | 0 | jpc_enc_prc_t *prc; |
180 | 0 | jpc_enc_cp_t *cp; |
181 | 0 | jpc_ms_t *ms; |
182 | |
|
183 | 0 | JAS_LOGDEBUGF(10, "encoding packet begin %d %d %d %d\n", compno, lvlno, |
184 | 0 | prcno, lyrno); |
185 | |
|
186 | 0 | tile = enc->curtile; |
187 | 0 | cp = enc->cp; |
188 | |
|
189 | 0 | if (cp->tcp.csty & JPC_COD_SOP) { |
190 | 0 | if (!(ms = jpc_ms_create(JPC_MS_SOP))) { |
191 | 0 | return -1; |
192 | 0 | } |
193 | 0 | ms->parms.sop.seqno = jpc_pi_getind(tile->pi); |
194 | 0 | if (jpc_putms(out, enc->cstate, ms)) { |
195 | 0 | return -1; |
196 | 0 | } |
197 | 0 | jpc_ms_destroy(ms); |
198 | 0 | } |
199 | | |
200 | 0 | if (!(outb = jpc_bitstream_sopen(out, "w+"))) { |
201 | 0 | return -1; |
202 | 0 | } |
203 | | |
204 | 0 | if (jpc_bitstream_putbit(outb, 1) == EOF) { |
205 | 0 | goto error_close; |
206 | 0 | } |
207 | 0 | JAS_LOGDEBUGF(10, "present.\n"); |
208 | |
|
209 | 0 | comp = &tile->tcmpts[compno]; |
210 | 0 | lvl = &comp->rlvls[lvlno]; |
211 | 0 | endbands = &lvl->bands[lvl->numbands]; |
212 | 0 | for (band = lvl->bands; band != endbands; ++band) { |
213 | 0 | if (!band->data) { |
214 | 0 | continue; |
215 | 0 | } |
216 | 0 | prc = &band->prcs[prcno]; |
217 | 0 | if (!prc->cblks) { |
218 | 0 | continue; |
219 | 0 | } |
220 | | |
221 | 0 | endcblks = &prc->cblks[prc->numcblks]; |
222 | 0 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
223 | 0 | if (!lyrno) { |
224 | 0 | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
225 | 0 | jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); |
226 | 0 | } |
227 | 0 | pass = cblk->curpass; |
228 | 0 | const bool included = (pass && pass->lyrno == lyrno); |
229 | 0 | if (included && (!cblk->numencpasses)) { |
230 | 0 | assert(pass->lyrno == lyrno); |
231 | 0 | leaf = jpc_tagtree_getleaf(prc->incltree, |
232 | 0 | cblk - prc->cblks); |
233 | 0 | jpc_tagtree_setvalue(prc->incltree, leaf, pass->lyrno); |
234 | 0 | } |
235 | 0 | } |
236 | | |
237 | 0 | endcblks = &prc->cblks[prc->numcblks]; |
238 | 0 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
239 | 0 | pass = cblk->curpass; |
240 | 0 | const bool included = (pass && pass->lyrno == lyrno); |
241 | 0 | if (!cblk->numencpasses) { |
242 | 0 | leaf = jpc_tagtree_getleaf(prc->incltree, |
243 | 0 | cblk - prc->cblks); |
244 | 0 | if (jpc_tagtree_encode(prc->incltree, leaf, lyrno + 1, outb) < |
245 | 0 | 0) { |
246 | 0 | goto error_close; |
247 | 0 | } |
248 | 0 | } else { |
249 | 0 | if (jpc_bitstream_putbit(outb, included) == EOF) { |
250 | 0 | goto error_close; |
251 | 0 | } |
252 | 0 | } |
253 | 0 | JAS_LOGDEBUGF(10, "included=%d ", included); |
254 | 0 | if (!included) { |
255 | 0 | continue; |
256 | 0 | } |
257 | 0 | if (!cblk->numencpasses) { |
258 | 0 | i = 1; |
259 | 0 | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
260 | 0 | for (;;) { |
261 | 0 | if ((ret = jpc_tagtree_encode(prc->nlibtree, leaf, i, |
262 | 0 | outb)) < 0) { |
263 | 0 | goto error_close; |
264 | 0 | } |
265 | 0 | if (ret) { |
266 | 0 | break; |
267 | 0 | } |
268 | 0 | ++i; |
269 | 0 | } |
270 | 0 | assert(leaf->known_ && i == leaf->value_ + 1); |
271 | 0 | } |
272 | | |
273 | 0 | endpasses = &cblk->passes[cblk->numpasses]; |
274 | 0 | startpass = pass; |
275 | 0 | endpass = startpass; |
276 | 0 | while (endpass != endpasses && endpass->lyrno == lyrno){ |
277 | 0 | ++endpass; |
278 | 0 | } |
279 | 0 | const unsigned numnewpasses = endpass - startpass; |
280 | 0 | if (jpc_putnumnewpasses(outb, numnewpasses)) { |
281 | 0 | goto error_close; |
282 | 0 | } |
283 | 0 | JAS_LOGDEBUGF(10, "numnewpasses=%d ", numnewpasses); |
284 | |
|
285 | 0 | lastpass = endpass - 1; |
286 | 0 | unsigned n = startpass->start; |
287 | 0 | unsigned passcount = 1; |
288 | 0 | unsigned maxadjust = 0; |
289 | 0 | for (pass = startpass; pass != endpass; ++pass) { |
290 | 0 | if (pass->term || pass == lastpass) { |
291 | 0 | unsigned datalen = pass->end - n; |
292 | 0 | t1 = jpc_int_firstone(datalen) + 1; |
293 | 0 | t2 = cblk->numlenbits + jpc_floorlog2(passcount); |
294 | 0 | const unsigned adjust = JAS_MAX(t1 - t2, 0); |
295 | 0 | maxadjust = JAS_MAX(adjust, maxadjust); |
296 | 0 | n += datalen; |
297 | 0 | passcount = 1; |
298 | 0 | } else { |
299 | 0 | ++passcount; |
300 | 0 | } |
301 | 0 | } |
302 | 0 | if (jpc_putcommacode(outb, maxadjust)) { |
303 | 0 | goto error_close; |
304 | 0 | } |
305 | 0 | cblk->numlenbits += maxadjust; |
306 | |
|
307 | 0 | lastpass = endpass - 1; |
308 | 0 | n = startpass->start; |
309 | 0 | passcount = 1; |
310 | 0 | for (pass = startpass; pass != endpass; ++pass) { |
311 | 0 | if (pass->term || pass == lastpass) { |
312 | 0 | unsigned datalen = pass->end - n; |
313 | 0 | assert(jpc_int_firstone(datalen) < cblk->numlenbits + |
314 | 0 | (int)jpc_floorlog2(passcount)); |
315 | 0 | if (jpc_bitstream_putbits(outb, cblk->numlenbits + |
316 | 0 | jpc_floorlog2(passcount), datalen) == EOF) { |
317 | 0 | goto error_close; |
318 | 0 | } |
319 | 0 | n += datalen; |
320 | 0 | passcount = 1; |
321 | 0 | } else { |
322 | 0 | ++passcount; |
323 | 0 | } |
324 | 0 | } |
325 | 0 | } |
326 | 0 | } |
327 | | |
328 | 0 | jpc_bitstream_outalign(outb, 0); |
329 | 0 | jpc_bitstream_close(outb); |
330 | |
|
331 | 0 | if (cp->tcp.csty & JPC_COD_EPH) { |
332 | 0 | if (!(ms = jpc_ms_create(JPC_MS_EPH))) { |
333 | 0 | return -1; |
334 | 0 | } |
335 | 0 | if (jpc_putms(out, enc->cstate, ms)) { |
336 | 0 | return -1; |
337 | 0 | } |
338 | 0 | jpc_ms_destroy(ms); |
339 | 0 | } |
340 | | |
341 | 0 | comp = &tile->tcmpts[compno]; |
342 | 0 | lvl = &comp->rlvls[lvlno]; |
343 | 0 | endbands = &lvl->bands[lvl->numbands]; |
344 | 0 | for (band = lvl->bands; band != endbands; ++band) { |
345 | 0 | if (!band->data) { |
346 | 0 | continue; |
347 | 0 | } |
348 | 0 | prc = &band->prcs[prcno]; |
349 | 0 | if (!prc->cblks) { |
350 | 0 | continue; |
351 | 0 | } |
352 | 0 | endcblks = &prc->cblks[prc->numcblks]; |
353 | 0 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
354 | 0 | pass = cblk->curpass; |
355 | |
|
356 | 0 | if (!pass) { |
357 | 0 | continue; |
358 | 0 | } |
359 | 0 | if (pass->lyrno != lyrno) { |
360 | 0 | assert(pass->lyrno > lyrno); |
361 | 0 | continue; |
362 | 0 | } |
363 | | |
364 | 0 | endpasses = &cblk->passes[cblk->numpasses]; |
365 | 0 | startpass = pass; |
366 | 0 | endpass = startpass; |
367 | 0 | while (endpass != endpasses && endpass->lyrno == lyrno){ |
368 | 0 | ++endpass; |
369 | 0 | } |
370 | 0 | lastpass = endpass - 1; |
371 | 0 | const unsigned numnewpasses = endpass - startpass; |
372 | |
|
373 | 0 | jas_stream_seek(cblk->stream, startpass->start, SEEK_SET); |
374 | 0 | assert(jas_stream_tell(cblk->stream) == startpass->start); |
375 | 0 | if (jas_stream_copy(out, cblk->stream, lastpass->end - |
376 | 0 | startpass->start)) { |
377 | 0 | return -1; |
378 | 0 | } |
379 | 0 | cblk->curpass = (endpass != endpasses) ? endpass : 0; |
380 | 0 | cblk->numencpasses += numnewpasses; |
381 | |
|
382 | 0 | } |
383 | 0 | } |
384 | | |
385 | 0 | JAS_LOGDEBUGF(10, "encoding packet end\n"); |
386 | |
|
387 | 0 | return 0; |
388 | | |
389 | 0 | error_close: |
390 | 0 | jpc_bitstream_close(outb); |
391 | 0 | return -1; |
392 | 0 | } |
393 | | |
394 | | void jpc_save_t2state(jpc_enc_t *enc) |
395 | 0 | { |
396 | | /* stream pos in embedded T1 stream may be wrong since not saved/restored! */ |
397 | |
|
398 | 0 | jpc_enc_tcmpt_t *comp; |
399 | 0 | jpc_enc_tcmpt_t *endcomps; |
400 | 0 | jpc_enc_rlvl_t *lvl; |
401 | 0 | jpc_enc_rlvl_t *endlvls; |
402 | 0 | jpc_enc_band_t *band; |
403 | 0 | jpc_enc_band_t *endbands; |
404 | 0 | jpc_enc_cblk_t *cblk; |
405 | 0 | jpc_enc_cblk_t *endcblks; |
406 | 0 | jpc_enc_tile_t *tile; |
407 | 0 | unsigned prcno; |
408 | 0 | jpc_enc_prc_t *prc; |
409 | |
|
410 | 0 | tile = enc->curtile; |
411 | |
|
412 | 0 | endcomps = &tile->tcmpts[tile->numtcmpts]; |
413 | 0 | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
414 | 0 | endlvls = &comp->rlvls[comp->numrlvls]; |
415 | 0 | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
416 | 0 | if (!lvl->bands) { |
417 | 0 | continue; |
418 | 0 | } |
419 | 0 | endbands = &lvl->bands[lvl->numbands]; |
420 | 0 | for (band = lvl->bands; band != endbands; ++band) { |
421 | 0 | if (!band->data) { |
422 | 0 | continue; |
423 | 0 | } |
424 | 0 | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
425 | 0 | if (!prc->cblks) { |
426 | 0 | continue; |
427 | 0 | } |
428 | 0 | jpc_tagtree_copy(prc->savincltree, prc->incltree); |
429 | 0 | jpc_tagtree_copy(prc->savnlibtree, prc->nlibtree); |
430 | 0 | endcblks = &prc->cblks[prc->numcblks]; |
431 | 0 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
432 | 0 | cblk->savedcurpass = cblk->curpass; |
433 | 0 | cblk->savednumencpasses = cblk->numencpasses; |
434 | 0 | cblk->savednumlenbits = cblk->numlenbits; |
435 | 0 | } |
436 | 0 | } |
437 | 0 | } |
438 | 0 | } |
439 | 0 | } |
440 | |
|
441 | 0 | } |
442 | | |
443 | | void jpc_restore_t2state(jpc_enc_t *enc) |
444 | 0 | { |
445 | |
|
446 | 0 | jpc_enc_tcmpt_t *comp; |
447 | 0 | jpc_enc_tcmpt_t *endcomps; |
448 | 0 | jpc_enc_rlvl_t *lvl; |
449 | 0 | jpc_enc_rlvl_t *endlvls; |
450 | 0 | jpc_enc_band_t *band; |
451 | 0 | jpc_enc_band_t *endbands; |
452 | 0 | jpc_enc_cblk_t *cblk; |
453 | 0 | jpc_enc_cblk_t *endcblks; |
454 | 0 | jpc_enc_tile_t *tile; |
455 | 0 | unsigned prcno; |
456 | 0 | jpc_enc_prc_t *prc; |
457 | |
|
458 | 0 | tile = enc->curtile; |
459 | |
|
460 | 0 | endcomps = &tile->tcmpts[tile->numtcmpts]; |
461 | 0 | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
462 | 0 | endlvls = &comp->rlvls[comp->numrlvls]; |
463 | 0 | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
464 | 0 | if (!lvl->bands) { |
465 | 0 | continue; |
466 | 0 | } |
467 | 0 | endbands = &lvl->bands[lvl->numbands]; |
468 | 0 | for (band = lvl->bands; band != endbands; ++band) { |
469 | 0 | if (!band->data) { |
470 | 0 | continue; |
471 | 0 | } |
472 | 0 | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
473 | 0 | if (!prc->cblks) { |
474 | 0 | continue; |
475 | 0 | } |
476 | 0 | jpc_tagtree_copy(prc->incltree, prc->savincltree); |
477 | 0 | jpc_tagtree_copy(prc->nlibtree, prc->savnlibtree); |
478 | 0 | endcblks = &prc->cblks[prc->numcblks]; |
479 | 0 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
480 | 0 | cblk->curpass = cblk->savedcurpass; |
481 | 0 | cblk->numencpasses = cblk->savednumencpasses; |
482 | 0 | cblk->numlenbits = cblk->savednumlenbits; |
483 | 0 | } |
484 | 0 | } |
485 | 0 | } |
486 | 0 | } |
487 | 0 | } |
488 | 0 | } |
489 | | |
490 | | void jpc_init_t2state(jpc_enc_t *enc, bool raflag) |
491 | 0 | { |
492 | | /* It is assumed that band->numbps and cblk->numbps precomputed */ |
493 | |
|
494 | 0 | jpc_enc_tcmpt_t *comp; |
495 | 0 | jpc_enc_tcmpt_t *endcomps; |
496 | 0 | jpc_enc_rlvl_t *lvl; |
497 | 0 | jpc_enc_rlvl_t *endlvls; |
498 | 0 | jpc_enc_band_t *band; |
499 | 0 | jpc_enc_band_t *endbands; |
500 | 0 | jpc_enc_cblk_t *cblk; |
501 | 0 | jpc_enc_cblk_t *endcblks; |
502 | 0 | jpc_enc_pass_t *pass; |
503 | 0 | jpc_enc_pass_t *endpasses; |
504 | 0 | jpc_tagtreenode_t *leaf; |
505 | 0 | jpc_enc_tile_t *tile; |
506 | 0 | unsigned prcno; |
507 | 0 | jpc_enc_prc_t *prc; |
508 | |
|
509 | 0 | tile = enc->curtile; |
510 | |
|
511 | 0 | endcomps = &tile->tcmpts[tile->numtcmpts]; |
512 | 0 | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
513 | 0 | endlvls = &comp->rlvls[comp->numrlvls]; |
514 | 0 | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
515 | 0 | if (!lvl->bands) { |
516 | 0 | continue; |
517 | 0 | } |
518 | 0 | endbands = &lvl->bands[lvl->numbands]; |
519 | 0 | for (band = lvl->bands; band != endbands; ++band) { |
520 | 0 | if (!band->data) { |
521 | 0 | continue; |
522 | 0 | } |
523 | 0 | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
524 | 0 | if (!prc->cblks) { |
525 | 0 | continue; |
526 | 0 | } |
527 | 0 | jpc_tagtree_reset(prc->incltree); |
528 | 0 | jpc_tagtree_reset(prc->nlibtree); |
529 | 0 | endcblks = &prc->cblks[prc->numcblks]; |
530 | 0 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
531 | 0 | if (jas_stream_rewind(cblk->stream)) { |
532 | 0 | assert(0); |
533 | 0 | } |
534 | 0 | cblk->curpass = (cblk->numpasses > 0) ? cblk->passes : 0; |
535 | 0 | cblk->numencpasses = 0; |
536 | 0 | cblk->numlenbits = 3; |
537 | 0 | cblk->numimsbs = band->numbps - cblk->numbps; |
538 | 0 | assert(cblk->numimsbs >= 0); |
539 | 0 | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
540 | 0 | jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); |
541 | |
|
542 | 0 | if (raflag) { |
543 | 0 | endpasses = &cblk->passes[cblk->numpasses]; |
544 | 0 | for (pass = cblk->passes; pass != endpasses; ++pass) { |
545 | 0 | pass->lyrno = 0; |
546 | 0 | } |
547 | 0 | } |
548 | 0 | } |
549 | 0 | } |
550 | 0 | } |
551 | 0 | } |
552 | 0 | } |
553 | |
|
554 | 0 | } |
555 | | |
556 | | jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile) |
557 | 0 | { |
558 | 0 | jpc_pi_t *pi; |
559 | 0 | unsigned compno; |
560 | 0 | jpc_picomp_t *picomp; |
561 | 0 | jpc_pirlvl_t *pirlvl; |
562 | 0 | jpc_enc_tcmpt_t *tcomp; |
563 | 0 | unsigned rlvlno; |
564 | 0 | jpc_enc_rlvl_t *rlvl; |
565 | 0 | unsigned prcno; |
566 | 0 | unsigned *prclyrno; |
567 | |
|
568 | 0 | if (!(pi = jpc_pi_create0())) { |
569 | 0 | return 0; |
570 | 0 | } |
571 | 0 | pi->pktno = -1; |
572 | 0 | pi->numcomps = cp->numcmpts; |
573 | 0 | if (!(pi->picomps = jas_alloc2(pi->numcomps, sizeof(jpc_picomp_t)))) { |
574 | 0 | jpc_pi_destroy(pi); |
575 | 0 | return 0; |
576 | 0 | } |
577 | 0 | for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno, |
578 | 0 | ++picomp) { |
579 | 0 | picomp->pirlvls = 0; |
580 | 0 | } |
581 | |
|
582 | 0 | for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; |
583 | 0 | compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
584 | 0 | picomp->numrlvls = tcomp->numrlvls; |
585 | 0 | if (!(picomp->pirlvls = jas_alloc2(picomp->numrlvls, |
586 | 0 | sizeof(jpc_pirlvl_t)))) { |
587 | 0 | jpc_pi_destroy(pi); |
588 | 0 | return 0; |
589 | 0 | } |
590 | 0 | for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
591 | 0 | picomp->numrlvls; ++rlvlno, ++pirlvl) { |
592 | 0 | pirlvl->prclyrnos = 0; |
593 | 0 | } |
594 | 0 | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
595 | 0 | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
596 | | /* XXX sizeof(long) should be sizeof different type */ |
597 | 0 | pirlvl->numprcs = rlvl->numprcs; |
598 | 0 | if (rlvl->numprcs) { |
599 | 0 | if (!(pirlvl->prclyrnos = jas_alloc2(pirlvl->numprcs, |
600 | 0 | sizeof(long)))) { |
601 | 0 | jpc_pi_destroy(pi); |
602 | 0 | return 0; |
603 | 0 | } |
604 | 0 | } else { |
605 | 0 | pirlvl->prclyrnos = 0; |
606 | 0 | } |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | 0 | pi->maxrlvls = 0; |
611 | 0 | for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; |
612 | 0 | compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
613 | 0 | picomp->hsamp = cp->ccps[compno].sampgrdstepx; |
614 | 0 | picomp->vsamp = cp->ccps[compno].sampgrdstepy; |
615 | 0 | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
616 | 0 | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
617 | 0 | pirlvl->prcwidthexpn = rlvl->prcwidthexpn; |
618 | 0 | pirlvl->prcheightexpn = rlvl->prcheightexpn; |
619 | 0 | for (prcno = 0, prclyrno = pirlvl->prclyrnos; |
620 | 0 | prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { |
621 | 0 | *prclyrno = 0; |
622 | 0 | } |
623 | 0 | pirlvl->numhprcs = rlvl->numhprcs; |
624 | 0 | } |
625 | 0 | if (pi->maxrlvls < tcomp->numrlvls) { |
626 | 0 | pi->maxrlvls = tcomp->numrlvls; |
627 | 0 | } |
628 | 0 | } |
629 | |
|
630 | 0 | pi->numlyrs = tile->numlyrs; |
631 | 0 | pi->xstart = tile->tlx; |
632 | 0 | pi->ystart = tile->tly; |
633 | 0 | pi->xend = tile->brx; |
634 | 0 | pi->yend = tile->bry; |
635 | |
|
636 | 0 | pi->picomp = 0; |
637 | 0 | pi->pirlvl = 0; |
638 | 0 | pi->x = 0; |
639 | 0 | pi->y = 0; |
640 | 0 | pi->compno = 0; |
641 | 0 | pi->rlvlno = 0; |
642 | 0 | pi->prcno = 0; |
643 | 0 | pi->lyrno = 0; |
644 | 0 | pi->xstep = 0; |
645 | 0 | pi->ystep = 0; |
646 | |
|
647 | 0 | pi->pchgno = -1; |
648 | |
|
649 | 0 | pi->defaultpchg.prgord = tile->prg; |
650 | 0 | pi->defaultpchg.compnostart = 0; |
651 | 0 | pi->defaultpchg.compnoend = pi->numcomps; |
652 | 0 | pi->defaultpchg.rlvlnostart = 0; |
653 | 0 | pi->defaultpchg.rlvlnoend = pi->maxrlvls; |
654 | 0 | pi->defaultpchg.lyrnoend = pi->numlyrs; |
655 | 0 | pi->pchg = 0; |
656 | |
|
657 | 0 | pi->valid = 0; |
658 | |
|
659 | 0 | return pi; |
660 | 0 | } |