/src/jasper/src/libjasper/jpc/jpc_t2enc.c
Line | Count | Source |
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 | 2.56M | { |
94 | 2.56M | assert(n >= 0); |
95 | | |
96 | 8.04M | while (--n >= 0) { |
97 | 5.48M | if (jpc_bitstream_putbit(out, 1) == EOF) { |
98 | 0 | return -1; |
99 | 0 | } |
100 | 5.48M | } |
101 | 2.56M | if (jpc_bitstream_putbit(out, 0) == EOF) { |
102 | 0 | return -1; |
103 | 0 | } |
104 | 2.56M | return 0; |
105 | 2.56M | } |
106 | | |
107 | | static int jpc_putnumnewpasses(jpc_bitstream_t *out, int n) |
108 | 2.56M | { |
109 | 2.56M | int ret; |
110 | | |
111 | 2.56M | if (n <= 0) { |
112 | 0 | return -1; |
113 | 2.56M | } else if (n == 1) { |
114 | 280k | ret = jpc_bitstream_putbit(out, 0); |
115 | 2.28M | } else if (n == 2) { |
116 | 83.7k | ret = jpc_bitstream_putbits(out, 2, 2); |
117 | 2.20M | } else if (n <= 5) { |
118 | 621k | ret = jpc_bitstream_putbits(out, 4, 0xc | (n - 3)); |
119 | 1.57M | } else if (n <= 36) { |
120 | 1.47M | ret = jpc_bitstream_putbits(out, 9, 0x1e0 | (n - 6)); |
121 | 1.47M | } else if (n <= 164) { |
122 | 104k | ret = jpc_bitstream_putbits(out, 16, 0xff80 | (n - 37)); |
123 | 104k | } 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 | 2.56M | return (ret != EOF) ? 0 : (-1); |
131 | 2.56M | } |
132 | | |
133 | | int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out) |
134 | 1.91k | { |
135 | 1.91k | jpc_enc_tile_t *tile; |
136 | 1.91k | jpc_pi_t *pi; |
137 | | |
138 | 1.91k | tile = enc->curtile; |
139 | | |
140 | 1.91k | jpc_init_t2state(enc, false); |
141 | 1.91k | pi = tile->pi; |
142 | 1.91k | jpc_pi_init(pi); |
143 | | |
144 | 1.91k | if (!jpc_pi_next(pi)) { |
145 | 15.9k | for (;;) { |
146 | 15.9k | if (jpc_enc_encpkt(enc, out, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi), |
147 | 15.9k | jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) { |
148 | 0 | return -1; |
149 | 0 | } |
150 | 15.9k | if (jpc_pi_next(pi)) { |
151 | 1.91k | break; |
152 | 1.91k | } |
153 | 15.9k | } |
154 | 1.91k | } |
155 | | |
156 | 1.91k | return 0; |
157 | 1.91k | } |
158 | | |
159 | | int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, unsigned compno, unsigned lvlno, unsigned prcno, unsigned lyrno) |
160 | 459k | { |
161 | 459k | jpc_enc_tcmpt_t *comp; |
162 | 459k | jpc_enc_rlvl_t *lvl; |
163 | 459k | jpc_enc_band_t *band; |
164 | 459k | jpc_enc_band_t *endbands; |
165 | 459k | jpc_enc_cblk_t *cblk; |
166 | 459k | jpc_enc_cblk_t *endcblks; |
167 | 459k | jpc_bitstream_t *outb; |
168 | 459k | jpc_enc_pass_t *pass; |
169 | 459k | jpc_enc_pass_t *startpass; |
170 | 459k | jpc_enc_pass_t *lastpass; |
171 | 459k | jpc_enc_pass_t *endpass; |
172 | 459k | jpc_enc_pass_t *endpasses; |
173 | 459k | int i; |
174 | 459k | int ret; |
175 | 459k | jpc_tagtreenode_t *leaf; |
176 | 459k | int t1; |
177 | 459k | int t2; |
178 | 459k | jpc_enc_tile_t *tile; |
179 | 459k | jpc_enc_prc_t *prc; |
180 | 459k | jpc_enc_cp_t *cp; |
181 | 459k | jpc_ms_t *ms; |
182 | | |
183 | 459k | JAS_LOGDEBUGF(10, "encoding packet begin %d %d %d %d\n", compno, lvlno, |
184 | 459k | prcno, lyrno); |
185 | | |
186 | 459k | tile = enc->curtile; |
187 | 459k | cp = enc->cp; |
188 | | |
189 | 459k | 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 | 459k | if (!(outb = jpc_bitstream_sopen(out, "w+"))) { |
201 | 0 | return -1; |
202 | 0 | } |
203 | | |
204 | 459k | if (jpc_bitstream_putbit(outb, 1) == EOF) { |
205 | 0 | goto error_close; |
206 | 0 | } |
207 | 459k | JAS_LOGDEBUGF(10, "present.\n"); |
208 | | |
209 | 459k | comp = &tile->tcmpts[compno]; |
210 | 459k | lvl = &comp->rlvls[lvlno]; |
211 | 459k | endbands = &lvl->bands[lvl->numbands]; |
212 | 1.68M | for (band = lvl->bands; band != endbands; ++band) { |
213 | 1.22M | if (!band->data) { |
214 | 25.7k | continue; |
215 | 25.7k | } |
216 | 1.20M | prc = &band->prcs[prcno]; |
217 | 1.20M | if (!prc->cblks) { |
218 | 0 | continue; |
219 | 0 | } |
220 | | |
221 | 1.20M | endcblks = &prc->cblks[prc->numcblks]; |
222 | 18.8M | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
223 | 17.6M | if (!lyrno) { |
224 | 17.6M | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
225 | 17.6M | jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); |
226 | 17.6M | } |
227 | 17.6M | pass = cblk->curpass; |
228 | 17.6M | const bool included = (pass && pass->lyrno == lyrno); |
229 | 17.6M | if (included && (!cblk->numencpasses)) { |
230 | 2.56M | assert(pass->lyrno == lyrno); |
231 | 2.56M | leaf = jpc_tagtree_getleaf(prc->incltree, |
232 | 2.56M | cblk - prc->cblks); |
233 | 2.56M | jpc_tagtree_setvalue(prc->incltree, leaf, pass->lyrno); |
234 | 2.56M | } |
235 | 17.6M | } |
236 | | |
237 | 1.20M | endcblks = &prc->cblks[prc->numcblks]; |
238 | 18.8M | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
239 | 17.6M | pass = cblk->curpass; |
240 | 17.6M | const bool included = (pass && pass->lyrno == lyrno); |
241 | 17.6M | if (!cblk->numencpasses) { |
242 | 17.6M | leaf = jpc_tagtree_getleaf(prc->incltree, |
243 | 17.6M | cblk - prc->cblks); |
244 | 17.6M | if (jpc_tagtree_encode(prc->incltree, leaf, lyrno + 1, outb) < |
245 | 17.6M | 0) { |
246 | 0 | goto error_close; |
247 | 0 | } |
248 | 17.6M | } else { |
249 | 0 | if (jpc_bitstream_putbit(outb, included) == EOF) { |
250 | 0 | goto error_close; |
251 | 0 | } |
252 | 0 | } |
253 | 17.6M | JAS_LOGDEBUGF(10, "included=%d ", included); |
254 | 17.6M | if (!included) { |
255 | 15.0M | continue; |
256 | 15.0M | } |
257 | 2.56M | if (!cblk->numencpasses) { |
258 | 2.56M | i = 1; |
259 | 2.56M | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
260 | 10.5M | for (;;) { |
261 | 10.5M | if ((ret = jpc_tagtree_encode(prc->nlibtree, leaf, i, |
262 | 10.5M | outb)) < 0) { |
263 | 0 | goto error_close; |
264 | 0 | } |
265 | 10.5M | if (ret) { |
266 | 2.56M | break; |
267 | 2.56M | } |
268 | 7.93M | ++i; |
269 | 7.93M | } |
270 | 2.56M | assert(leaf->known_ && i == leaf->value_ + 1); |
271 | 2.56M | } |
272 | | |
273 | 2.56M | endpasses = &cblk->passes[cblk->numpasses]; |
274 | 2.56M | startpass = pass; |
275 | 2.56M | endpass = startpass; |
276 | 30.1M | while (endpass != endpasses && endpass->lyrno == lyrno){ |
277 | 27.5M | ++endpass; |
278 | 27.5M | } |
279 | 2.56M | const unsigned numnewpasses = endpass - startpass; |
280 | 2.56M | if (jpc_putnumnewpasses(outb, numnewpasses)) { |
281 | 0 | goto error_close; |
282 | 0 | } |
283 | 2.56M | JAS_LOGDEBUGF(10, "numnewpasses=%d ", numnewpasses); |
284 | | |
285 | 2.56M | lastpass = endpass - 1; |
286 | 2.56M | unsigned n = startpass->start; |
287 | 2.56M | unsigned passcount = 1; |
288 | 2.56M | unsigned maxadjust = 0; |
289 | 30.1M | for (pass = startpass; pass != endpass; ++pass) { |
290 | 27.5M | if (pass->term || pass == lastpass) { |
291 | 2.56M | unsigned datalen = pass->end - n; |
292 | 2.56M | t1 = jpc_int_firstone(datalen) + 1; |
293 | 2.56M | t2 = cblk->numlenbits + jpc_floorlog2(passcount); |
294 | 2.56M | const unsigned adjust = JAS_MAX(t1 - t2, 0); |
295 | 2.56M | maxadjust = JAS_MAX(adjust, maxadjust); |
296 | 2.56M | n += datalen; |
297 | 2.56M | passcount = 1; |
298 | 25.0M | } else { |
299 | 25.0M | ++passcount; |
300 | 25.0M | } |
301 | 27.5M | } |
302 | 2.56M | if (jpc_putcommacode(outb, maxadjust)) { |
303 | 0 | goto error_close; |
304 | 0 | } |
305 | 2.56M | cblk->numlenbits += maxadjust; |
306 | | |
307 | 2.56M | lastpass = endpass - 1; |
308 | 2.56M | n = startpass->start; |
309 | 2.56M | passcount = 1; |
310 | 30.1M | for (pass = startpass; pass != endpass; ++pass) { |
311 | 27.5M | if (pass->term || pass == lastpass) { |
312 | 2.56M | unsigned datalen = pass->end - n; |
313 | 2.56M | assert(jpc_int_firstone(datalen) < cblk->numlenbits + |
314 | 2.56M | (int)jpc_floorlog2(passcount)); |
315 | 2.56M | if (jpc_bitstream_putbits(outb, cblk->numlenbits + |
316 | 2.56M | jpc_floorlog2(passcount), datalen) == EOF) { |
317 | 0 | goto error_close; |
318 | 0 | } |
319 | 2.56M | n += datalen; |
320 | 2.56M | passcount = 1; |
321 | 25.0M | } else { |
322 | 25.0M | ++passcount; |
323 | 25.0M | } |
324 | 27.5M | } |
325 | 2.56M | } |
326 | 1.20M | } |
327 | | |
328 | 459k | jpc_bitstream_outalign(outb, 0); |
329 | 459k | jpc_bitstream_close(outb); |
330 | | |
331 | 459k | 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 | 459k | comp = &tile->tcmpts[compno]; |
342 | 459k | lvl = &comp->rlvls[lvlno]; |
343 | 459k | endbands = &lvl->bands[lvl->numbands]; |
344 | 1.68M | for (band = lvl->bands; band != endbands; ++band) { |
345 | 1.22M | if (!band->data) { |
346 | 25.7k | continue; |
347 | 25.7k | } |
348 | 1.20M | prc = &band->prcs[prcno]; |
349 | 1.20M | if (!prc->cblks) { |
350 | 0 | continue; |
351 | 0 | } |
352 | 1.20M | endcblks = &prc->cblks[prc->numcblks]; |
353 | 18.8M | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
354 | 17.6M | pass = cblk->curpass; |
355 | | |
356 | 17.6M | if (!pass) { |
357 | 13.2M | continue; |
358 | 13.2M | } |
359 | 4.44M | if (pass->lyrno != lyrno) { |
360 | 1.87M | assert(pass->lyrno > lyrno); |
361 | 1.87M | continue; |
362 | 1.87M | } |
363 | | |
364 | 2.56M | endpasses = &cblk->passes[cblk->numpasses]; |
365 | 2.56M | startpass = pass; |
366 | 2.56M | endpass = startpass; |
367 | 30.1M | while (endpass != endpasses && endpass->lyrno == lyrno){ |
368 | 27.5M | ++endpass; |
369 | 27.5M | } |
370 | 2.56M | lastpass = endpass - 1; |
371 | 2.56M | const unsigned numnewpasses = endpass - startpass; |
372 | | |
373 | 2.56M | jas_stream_seek(cblk->stream, startpass->start, SEEK_SET); |
374 | 2.56M | assert(jas_stream_tell(cblk->stream) == startpass->start); |
375 | 2.56M | if (jas_stream_copy(out, cblk->stream, lastpass->end - |
376 | 2.56M | startpass->start)) { |
377 | 0 | return -1; |
378 | 0 | } |
379 | 2.56M | cblk->curpass = (endpass != endpasses) ? endpass : 0; |
380 | 2.56M | cblk->numencpasses += numnewpasses; |
381 | | |
382 | 2.56M | } |
383 | 1.20M | } |
384 | | |
385 | 459k | JAS_LOGDEBUGF(10, "encoding packet end\n"); |
386 | | |
387 | 459k | return 0; |
388 | | |
389 | 0 | error_close: |
390 | 0 | jpc_bitstream_close(outb); |
391 | 0 | return -1; |
392 | 459k | } |
393 | | |
394 | | void jpc_save_t2state(jpc_enc_t *enc) |
395 | 51.1k | { |
396 | | /* stream pos in embedded T1 stream may be wrong since not saved/restored! */ |
397 | | |
398 | 51.1k | jpc_enc_tcmpt_t *comp; |
399 | 51.1k | jpc_enc_tcmpt_t *endcomps; |
400 | 51.1k | jpc_enc_rlvl_t *lvl; |
401 | 51.1k | jpc_enc_rlvl_t *endlvls; |
402 | 51.1k | jpc_enc_band_t *band; |
403 | 51.1k | jpc_enc_band_t *endbands; |
404 | 51.1k | jpc_enc_cblk_t *cblk; |
405 | 51.1k | jpc_enc_cblk_t *endcblks; |
406 | 51.1k | jpc_enc_tile_t *tile; |
407 | 51.1k | unsigned prcno; |
408 | 51.1k | jpc_enc_prc_t *prc; |
409 | | |
410 | 51.1k | tile = enc->curtile; |
411 | | |
412 | 51.1k | endcomps = &tile->tcmpts[tile->numtcmpts]; |
413 | 122k | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
414 | 71.3k | endlvls = &comp->rlvls[comp->numrlvls]; |
415 | 499k | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
416 | 428k | if (!lvl->bands) { |
417 | 0 | continue; |
418 | 0 | } |
419 | 428k | endbands = &lvl->bands[lvl->numbands]; |
420 | 1.56M | for (band = lvl->bands; band != endbands; ++band) { |
421 | 1.14M | if (!band->data) { |
422 | 19.7k | continue; |
423 | 19.7k | } |
424 | 2.24M | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
425 | 1.12M | if (!prc->cblks) { |
426 | 0 | continue; |
427 | 0 | } |
428 | 1.12M | jpc_tagtree_copy(prc->savincltree, prc->incltree); |
429 | 1.12M | jpc_tagtree_copy(prc->savnlibtree, prc->nlibtree); |
430 | 1.12M | endcblks = &prc->cblks[prc->numcblks]; |
431 | 17.7M | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
432 | 16.5M | cblk->savedcurpass = cblk->curpass; |
433 | 16.5M | cblk->savednumencpasses = cblk->numencpasses; |
434 | 16.5M | cblk->savednumlenbits = cblk->numlenbits; |
435 | 16.5M | } |
436 | 1.12M | } |
437 | 1.12M | } |
438 | 428k | } |
439 | 71.3k | } |
440 | | |
441 | 51.1k | } |
442 | | |
443 | | void jpc_restore_t2state(jpc_enc_t *enc) |
444 | 51.1k | { |
445 | | |
446 | 51.1k | jpc_enc_tcmpt_t *comp; |
447 | 51.1k | jpc_enc_tcmpt_t *endcomps; |
448 | 51.1k | jpc_enc_rlvl_t *lvl; |
449 | 51.1k | jpc_enc_rlvl_t *endlvls; |
450 | 51.1k | jpc_enc_band_t *band; |
451 | 51.1k | jpc_enc_band_t *endbands; |
452 | 51.1k | jpc_enc_cblk_t *cblk; |
453 | 51.1k | jpc_enc_cblk_t *endcblks; |
454 | 51.1k | jpc_enc_tile_t *tile; |
455 | 51.1k | unsigned prcno; |
456 | 51.1k | jpc_enc_prc_t *prc; |
457 | | |
458 | 51.1k | tile = enc->curtile; |
459 | | |
460 | 51.1k | endcomps = &tile->tcmpts[tile->numtcmpts]; |
461 | 122k | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
462 | 71.3k | endlvls = &comp->rlvls[comp->numrlvls]; |
463 | 499k | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
464 | 428k | if (!lvl->bands) { |
465 | 0 | continue; |
466 | 0 | } |
467 | 428k | endbands = &lvl->bands[lvl->numbands]; |
468 | 1.56M | for (band = lvl->bands; band != endbands; ++band) { |
469 | 1.14M | if (!band->data) { |
470 | 19.7k | continue; |
471 | 19.7k | } |
472 | 2.24M | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
473 | 1.12M | if (!prc->cblks) { |
474 | 0 | continue; |
475 | 0 | } |
476 | 1.12M | jpc_tagtree_copy(prc->incltree, prc->savincltree); |
477 | 1.12M | jpc_tagtree_copy(prc->nlibtree, prc->savnlibtree); |
478 | 1.12M | endcblks = &prc->cblks[prc->numcblks]; |
479 | 17.7M | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
480 | 16.5M | cblk->curpass = cblk->savedcurpass; |
481 | 16.5M | cblk->numencpasses = cblk->savednumencpasses; |
482 | 16.5M | cblk->numlenbits = cblk->savednumlenbits; |
483 | 16.5M | } |
484 | 1.12M | } |
485 | 1.12M | } |
486 | 428k | } |
487 | 71.3k | } |
488 | 51.1k | } |
489 | | |
490 | | void jpc_init_t2state(jpc_enc_t *enc, bool raflag) |
491 | 3.83k | { |
492 | | /* It is assumed that band->numbps and cblk->numbps precomputed */ |
493 | | |
494 | 3.83k | jpc_enc_tcmpt_t *comp; |
495 | 3.83k | jpc_enc_tcmpt_t *endcomps; |
496 | 3.83k | jpc_enc_rlvl_t *lvl; |
497 | 3.83k | jpc_enc_rlvl_t *endlvls; |
498 | 3.83k | jpc_enc_band_t *band; |
499 | 3.83k | jpc_enc_band_t *endbands; |
500 | 3.83k | jpc_enc_cblk_t *cblk; |
501 | 3.83k | jpc_enc_cblk_t *endcblks; |
502 | 3.83k | jpc_enc_pass_t *pass; |
503 | 3.83k | jpc_enc_pass_t *endpasses; |
504 | 3.83k | jpc_tagtreenode_t *leaf; |
505 | 3.83k | jpc_enc_tile_t *tile; |
506 | 3.83k | unsigned prcno; |
507 | 3.83k | jpc_enc_prc_t *prc; |
508 | | |
509 | 3.83k | tile = enc->curtile; |
510 | | |
511 | 3.83k | endcomps = &tile->tcmpts[tile->numtcmpts]; |
512 | 9.14k | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
513 | 5.30k | endlvls = &comp->rlvls[comp->numrlvls]; |
514 | 37.1k | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
515 | 31.8k | if (!lvl->bands) { |
516 | 0 | continue; |
517 | 0 | } |
518 | 31.8k | endbands = &lvl->bands[lvl->numbands]; |
519 | 116k | for (band = lvl->bands; band != endbands; ++band) { |
520 | 84.9k | if (!band->data) { |
521 | 5.90k | continue; |
522 | 5.90k | } |
523 | 158k | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
524 | 79.0k | if (!prc->cblks) { |
525 | 0 | continue; |
526 | 0 | } |
527 | 79.0k | jpc_tagtree_reset(prc->incltree); |
528 | 79.0k | jpc_tagtree_reset(prc->nlibtree); |
529 | 79.0k | endcblks = &prc->cblks[prc->numcblks]; |
530 | 1.15M | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
531 | 1.07M | if (jas_stream_rewind(cblk->stream)) { |
532 | 0 | assert(0); |
533 | 0 | } |
534 | 1.07M | cblk->curpass = (cblk->numpasses > 0) ? cblk->passes : 0; |
535 | 1.07M | cblk->numencpasses = 0; |
536 | 1.07M | cblk->numlenbits = 3; |
537 | 1.07M | cblk->numimsbs = band->numbps - cblk->numbps; |
538 | 1.07M | assert(cblk->numimsbs >= 0); |
539 | 1.07M | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
540 | 1.07M | jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); |
541 | | |
542 | 1.07M | if (raflag) { |
543 | 536k | endpasses = &cblk->passes[cblk->numpasses]; |
544 | 3.77M | for (pass = cblk->passes; pass != endpasses; ++pass) { |
545 | 3.23M | pass->lyrno = 0; |
546 | 3.23M | } |
547 | 536k | } |
548 | 1.07M | } |
549 | 79.0k | } |
550 | 79.0k | } |
551 | 31.8k | } |
552 | 5.30k | } |
553 | | |
554 | 3.83k | } |
555 | | |
556 | | jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile) |
557 | 1.92k | { |
558 | 1.92k | jpc_pi_t *pi; |
559 | 1.92k | unsigned compno; |
560 | 1.92k | jpc_picomp_t *picomp; |
561 | 1.92k | jpc_pirlvl_t *pirlvl; |
562 | 1.92k | jpc_enc_tcmpt_t *tcomp; |
563 | 1.92k | unsigned rlvlno; |
564 | 1.92k | jpc_enc_rlvl_t *rlvl; |
565 | 1.92k | unsigned prcno; |
566 | 1.92k | unsigned *prclyrno; |
567 | | |
568 | 1.92k | if (!(pi = jpc_pi_create0())) { |
569 | 0 | return 0; |
570 | 0 | } |
571 | 1.92k | pi->pktno = -1; |
572 | 1.92k | pi->numcomps = cp->numcmpts; |
573 | 1.92k | if (!(pi->picomps = jas_alloc2(pi->numcomps, sizeof(jpc_picomp_t)))) { |
574 | 0 | jpc_pi_destroy(pi); |
575 | 0 | return 0; |
576 | 0 | } |
577 | 4.60k | for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno, |
578 | 2.67k | ++picomp) { |
579 | 2.67k | picomp->pirlvls = 0; |
580 | 2.67k | } |
581 | | |
582 | 1.92k | for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; |
583 | 4.60k | compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
584 | 2.67k | picomp->numrlvls = tcomp->numrlvls; |
585 | 2.67k | if (!(picomp->pirlvls = jas_alloc2(picomp->numrlvls, |
586 | 2.67k | sizeof(jpc_pirlvl_t)))) { |
587 | 0 | jpc_pi_destroy(pi); |
588 | 0 | return 0; |
589 | 0 | } |
590 | 18.7k | for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
591 | 18.7k | picomp->numrlvls; ++rlvlno, ++pirlvl) { |
592 | 16.0k | pirlvl->prclyrnos = 0; |
593 | 16.0k | } |
594 | 2.67k | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
595 | 18.7k | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
596 | | /* XXX sizeof(long) should be sizeof different type */ |
597 | 16.0k | pirlvl->numprcs = rlvl->numprcs; |
598 | 16.0k | if (rlvl->numprcs) { |
599 | 16.0k | if (!(pirlvl->prclyrnos = jas_alloc2(pirlvl->numprcs, |
600 | 16.0k | sizeof(long)))) { |
601 | 0 | jpc_pi_destroy(pi); |
602 | 0 | return 0; |
603 | 0 | } |
604 | 16.0k | } else { |
605 | 0 | pirlvl->prclyrnos = 0; |
606 | 0 | } |
607 | 16.0k | } |
608 | 2.67k | } |
609 | | |
610 | 1.92k | pi->maxrlvls = 0; |
611 | 1.92k | for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; |
612 | 4.60k | compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
613 | 2.67k | picomp->hsamp = cp->ccps[compno].sampgrdstepx; |
614 | 2.67k | picomp->vsamp = cp->ccps[compno].sampgrdstepy; |
615 | 2.67k | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
616 | 18.7k | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
617 | 16.0k | pirlvl->prcwidthexpn = rlvl->prcwidthexpn; |
618 | 16.0k | pirlvl->prcheightexpn = rlvl->prcheightexpn; |
619 | 16.0k | for (prcno = 0, prclyrno = pirlvl->prclyrnos; |
620 | 32.1k | prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { |
621 | 16.0k | *prclyrno = 0; |
622 | 16.0k | } |
623 | 16.0k | pirlvl->numhprcs = rlvl->numhprcs; |
624 | 16.0k | } |
625 | 2.67k | if (pi->maxrlvls < tcomp->numrlvls) { |
626 | 1.92k | pi->maxrlvls = tcomp->numrlvls; |
627 | 1.92k | } |
628 | 2.67k | } |
629 | | |
630 | 1.92k | pi->numlyrs = tile->numlyrs; |
631 | 1.92k | pi->xstart = tile->tlx; |
632 | 1.92k | pi->ystart = tile->tly; |
633 | 1.92k | pi->xend = tile->brx; |
634 | 1.92k | pi->yend = tile->bry; |
635 | | |
636 | 1.92k | pi->picomp = 0; |
637 | 1.92k | pi->pirlvl = 0; |
638 | 1.92k | pi->x = 0; |
639 | 1.92k | pi->y = 0; |
640 | 1.92k | pi->compno = 0; |
641 | 1.92k | pi->rlvlno = 0; |
642 | 1.92k | pi->prcno = 0; |
643 | 1.92k | pi->lyrno = 0; |
644 | 1.92k | pi->xstep = 0; |
645 | 1.92k | pi->ystep = 0; |
646 | | |
647 | 1.92k | pi->pchgno = -1; |
648 | | |
649 | 1.92k | pi->defaultpchg.prgord = tile->prg; |
650 | 1.92k | pi->defaultpchg.compnostart = 0; |
651 | 1.92k | pi->defaultpchg.compnoend = pi->numcomps; |
652 | 1.92k | pi->defaultpchg.rlvlnostart = 0; |
653 | 1.92k | pi->defaultpchg.rlvlnoend = pi->maxrlvls; |
654 | 1.92k | pi->defaultpchg.lyrnoend = pi->numlyrs; |
655 | 1.92k | pi->pchg = 0; |
656 | | |
657 | 1.92k | pi->valid = 0; |
658 | | |
659 | 1.92k | return pi; |
660 | 1.92k | } |