80b98c7ec97c5dbf7369719ee4de614880e189c3
[openjpeg.git] / src / lib / openjpip / jp2k_encoder.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * Copyright (c) 2011,      Lucian Corlaciu, GSoC
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36 #include <assert.h>
37 #include "jp2k_encoder.h"
38 #include "j2kheader_manager.h"
39 #include "imgreg_manager.h"
40 #include "opj_inttypes.h"
41
42
43 #ifdef SERVER
44 #include "fcgi_stdio.h"
45 #define logstream FCGI_stdout
46 #else
47 #define FCGI_stdout stdout
48 #define FCGI_stderr stderr
49 #define logstream stderr
50 #endif /*SERVER*/
51
52
53 /**
54  * search a message by class_id
55  *
56  * @param[in] class_id    class identifiers
57  * @param[in] in_class_id in-class identifiers, -1 means any
58  * @param[in] csn         codestream number
59  * @param[in] msg         first message pointer of the searching list
60  * @return                found message pointer
61  */
62 message_param_t * search_message(Byte8_t class_id, Byte8_t in_class_id,
63                                  Byte8_t csn, message_param_t *msg);
64
65 /**
66  * reconstruct j2k codestream from JPT- (in future, JPP-) stream
67  *
68  * @param[in]  msgqueue   message queue pointer
69  * @param[in]  jpipstream original JPT- JPP- stream
70  * @param[in]  csn        codestream number
71  * @param[in]  fw         reconstructing image frame width
72  * @param[in]  fh         reconstructing image frame height
73  * @param[out] codelen   codestream length
74  * @return               generated reconstructed j2k codestream
75  */
76 Byte_t * recons_codestream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
77                            Byte8_t csn, int fw, int fh, Byte8_t *codelen);
78
79 Byte_t * recons_j2k(msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn,
80                     int fw, int fh, Byte8_t *j2klen)
81 {
82     Byte_t *j2kstream = NULL;
83
84     if (!msgqueue) {
85         return NULL;
86     }
87
88     j2kstream = recons_codestream(msgqueue, jpipstream, csn, fw, fh, j2klen);
89
90     return j2kstream;
91 }
92
93 Byte_t * add_emptyboxstream(placeholder_param_t *phld, Byte_t *jp2stream,
94                             Byte8_t *jp2len);
95 Byte_t * add_msgstream(message_param_t *message, Byte_t *origstream,
96                        Byte_t *j2kstream, Byte8_t *j2klen);
97
98 Byte_t * recons_jp2(msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn,
99                     Byte8_t *jp2len)
100 {
101     message_param_t *ptr;
102     Byte_t *jp2stream = NULL;
103     Byte_t *codestream = NULL;
104     Byte8_t codelen;
105     Byte8_t jp2cDBoxOffset = 0, jp2cDBoxlen = 0;
106
107     *jp2len = 0;
108
109     if (!msgqueue) {
110         return NULL;
111     }
112
113     ptr = msgqueue->first;
114     while ((ptr = search_message(METADATA_MSG, (Byte8_t) - 1, csn, ptr)) != NULL) {
115         if (ptr->phld) {
116             if (strncmp((char *)ptr->phld->OrigBH + 4, "jp2c", 4) == 0) {
117                 jp2cDBoxOffset = *jp2len + ptr->phld->OrigBHlen;
118                 jp2stream = add_emptyboxstream(ptr->phld, jp2stream, jp2len);  /* header only */
119                 jp2cDBoxlen = *jp2len - jp2cDBoxOffset;
120             } else {
121                 jp2stream = add_emptyboxstream(ptr->phld, jp2stream,
122                                                jp2len);    /* header only */
123             }
124         }
125         jp2stream = add_msgstream(ptr, jpipstream, jp2stream, jp2len);
126         ptr = ptr->next;
127     }
128
129     codestream = recons_codestream(msgqueue, jpipstream, csn, 0, 0, &codelen);
130
131     if (jp2cDBoxOffset != 0 && codelen <= jp2cDBoxlen) {
132         memcpy(jp2stream + jp2cDBoxOffset, codestream, codelen);
133     }
134
135     opj_free(codestream);
136
137     return jp2stream;
138 }
139
140 OPJ_BOOL isJPPstream(Byte8_t csn, msgqueue_param_t *msgqueue);
141
142 Byte_t * recons_codestream_from_JPTstream(msgqueue_param_t *msgqueue,
143         Byte_t *jpipstream, Byte8_t csn, int fw, int fh,  Byte8_t *j2klen);
144 Byte_t * recons_codestream_from_JPPstream(msgqueue_param_t *msgqueue,
145         Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *j2klen);
146
147 Byte_t * add_EOC(Byte_t *j2kstream, Byte8_t *j2klen);
148
149 Byte_t * recons_codestream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
150                            Byte8_t csn, int fw, int fh, Byte8_t *codelen)
151 {
152     if (isJPPstream(csn, msgqueue)) {
153         return recons_codestream_from_JPPstream(msgqueue, jpipstream, csn, fw, fh,
154                                                 codelen);
155     } else {
156         return recons_codestream_from_JPTstream(msgqueue, jpipstream, csn, fw, fh,
157                                                 codelen);
158     }
159 }
160
161 OPJ_BOOL isJPPstream(Byte8_t csn, msgqueue_param_t *msgqueue)
162 {
163     message_param_t *msg;
164
165     msg = msgqueue->first;
166     while (msg) {
167         if (msg->csn == csn) {
168             if (msg->class_id <= 2) {
169                 return OPJ_TRUE;
170             } else if (msg->class_id == 4 || msg->class_id == 5) {
171                 return OPJ_FALSE;
172             }
173         }
174         msg = msg->next;
175     }
176
177     fprintf(FCGI_stderr, "Error, message of csn %" PRId64 " not found\n", csn);
178
179     return OPJ_FALSE;
180 }
181
182 Byte_t * add_mainhead_msgstream(msgqueue_param_t *msgqueue, Byte_t *origstream,
183                                 Byte_t *j2kstream, Byte8_t csn, Byte8_t *j2klen);
184 Byte8_t get_last_tileID(msgqueue_param_t *msgqueue, Byte8_t csn,
185                         OPJ_BOOL isJPPstream);
186 Byte_t * add_emptytilestream(const Byte8_t tileID, Byte_t *j2kstream,
187                              Byte8_t *j2klen);
188
189 Byte_t * recons_codestream_from_JPTstream(msgqueue_param_t *msgqueue,
190         Byte_t *jpipstream, Byte8_t csn, int fw, int fh,  Byte8_t *j2klen)
191 {
192     Byte_t *j2kstream = NULL;
193     Byte8_t last_tileID, tileID;
194     OPJ_BOOL found;
195     Byte8_t binOffset;
196     message_param_t *ptr;
197     SIZmarker_param_t SIZ;
198     OPJ_SIZE_T mindeclev;
199
200     *j2klen = 0;
201     j2kstream = add_mainhead_msgstream(msgqueue, jpipstream, j2kstream, csn,
202                                        j2klen);
203
204     if (!get_mainheader_from_j2kstream(j2kstream, &SIZ, NULL)) {
205         return j2kstream;
206     }
207
208     if (fw <= 0 || fh <= 0) {
209         mindeclev = 0;
210     } else {
211         mindeclev = (OPJ_SIZE_T)comp_decomplev(fw, fh, (int)SIZ.Xsiz, (int)SIZ.Ysiz);
212     }
213
214     last_tileID = get_last_tileID(msgqueue, csn, OPJ_FALSE);
215
216     for (tileID = 0; tileID <= last_tileID; tileID++) {
217         found = OPJ_FALSE;
218         binOffset = 0;
219
220         ptr = msgqueue->first;
221         while ((ptr = search_message(TILE_MSG, tileID, csn, ptr)) != NULL) {
222             if (ptr->bin_offset == binOffset) {
223                 found = OPJ_TRUE;
224                 j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
225                 binOffset += ptr->length;
226             }
227             ptr = ptr->next;
228         }
229         ptr = msgqueue->first;
230         while ((ptr = search_message(EXT_TILE_MSG, tileID, csn, ptr)) != NULL) {
231             if (ptr->aux > mindeclev) { /* FIXME: pointer comparison ? */
232                 if (ptr->bin_offset == binOffset) {
233                     found = OPJ_TRUE;
234                     j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
235                     binOffset += ptr->length;
236                 }
237             }
238             ptr = ptr->next;
239         }
240         if (!found) {
241             j2kstream = add_emptytilestream(tileID, j2kstream, j2klen);
242         }
243     }
244
245     j2kstream = add_EOC(j2kstream, j2klen);
246
247     return j2kstream;
248 }
249
250 Byte_t * add_SOTmkr(Byte_t *j2kstream, Byte8_t *j2klen);
251
252 Byte_t * recons_bitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
253                           Byte_t *j2kstream, Byte8_t csn,
254                           Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
255                           int *max_reslev, Byte8_t *j2klen);
256
257 Byte_t * recons_codestream_from_JPPstream(msgqueue_param_t *msgqueue,
258         Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *j2klen)
259 {
260     Byte_t *j2kstream = NULL;
261     Byte8_t tileID, last_tileID;
262     Byte8_t SOToffset;
263     OPJ_BOOL foundTH;
264     Byte8_t binOffset;
265     message_param_t *ptr;
266     SIZmarker_param_t SIZ;
267     CODmarker_param_t COD;
268     int max_reslev, mindeclev;
269
270     *j2klen = 0;
271     j2kstream = add_mainhead_msgstream(msgqueue, jpipstream, j2kstream, csn,
272                                        j2klen);
273
274     if (!get_mainheader_from_j2kstream(j2kstream, &SIZ, &COD)) {
275         return j2kstream;
276     }
277
278     if (fw == 0 || fh == 0) {
279         mindeclev = 0;
280     } else {
281         mindeclev = comp_decomplev(fw, fh, (int)SIZ.Xsiz, (int)SIZ.Ysiz);
282     }
283
284     max_reslev = -1;
285     last_tileID = get_last_tileID(msgqueue, csn, OPJ_TRUE);
286
287     for (tileID = 0; tileID <= last_tileID; tileID++) {
288
289         ptr = msgqueue->first;
290         binOffset = 0;
291         foundTH = OPJ_FALSE;
292         SOToffset = *j2klen;
293         while ((ptr = search_message(TILE_HEADER_MSG, tileID, csn, ptr)) != NULL) {
294             if (ptr->bin_offset == binOffset) {
295                 j2kstream = add_SOTmkr(j2kstream, j2klen);
296                 j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
297                 foundTH = OPJ_TRUE;
298                 binOffset += ptr->length;
299             }
300             ptr = ptr->next;
301         }
302
303         if (foundTH) {
304             j2kstream = recons_bitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
305                                          COD, mindeclev, &max_reslev, j2klen);
306             modify_tileheader(j2kstream, SOToffset,
307                               (max_reslev < COD.numOfdecomp ? max_reslev : -1), SIZ.Csiz, j2klen);
308         } else {
309             j2kstream = add_emptytilestream(tileID, j2kstream, j2klen);
310         }
311     }
312
313     if (max_reslev < COD.numOfdecomp)
314         if (!modify_mainheader(j2kstream, max_reslev, SIZ, COD, j2klen)) {
315             delete_COD(COD);
316             return j2kstream;
317         }
318
319     j2kstream = add_EOC(j2kstream, j2klen);
320     delete_COD(COD);
321
322     return j2kstream;
323 }
324
325 Byte_t * add_mainhead_msgstream(msgqueue_param_t *msgqueue, Byte_t *origstream,
326                                 Byte_t *j2kstream, Byte8_t csn, Byte8_t *j2klen)
327 {
328     message_param_t *ptr;
329     Byte8_t binOffset;
330
331     ptr = msgqueue->first;
332     binOffset = 0;
333
334     while ((ptr = search_message(MAINHEADER_MSG, (Byte8_t) - 1, csn,
335                                  ptr)) != NULL) {
336         if (ptr->bin_offset == binOffset) {
337             j2kstream = add_msgstream(ptr, origstream, j2kstream, j2klen);
338             binOffset += ptr->length;
339         }
340         ptr = ptr->next;
341     }
342     return j2kstream;
343 }
344
345 Byte_t * add_SOTmkr(Byte_t *j2kstream, Byte8_t *j2klen)
346 {
347     Byte_t *buf;
348     const Byte2_t SOT = 0x90ff;
349
350     buf = (Byte_t *)opj_malloc((*j2klen) + 2);
351
352     memcpy(buf, j2kstream, *j2klen);
353     memcpy(buf + (*j2klen), &SOT, 2);
354
355     *j2klen += 2;
356
357     if (j2kstream) {
358         opj_free(j2kstream);
359     }
360
361     return buf;
362 }
363
364 Byte_t * recons_LRCPbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
365                               Byte_t *j2kstream, Byte8_t csn,
366                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
367                               int *max_reslev, Byte8_t *j2klen);
368
369 Byte_t * recons_RLCPbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
370                               Byte_t *j2kstream, Byte8_t csn,
371                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
372                               int *max_reslev, Byte8_t *j2klen);
373
374 Byte_t * recons_RPCLbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
375                               Byte_t *j2kstream, Byte8_t csn,
376                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
377                               int *max_reslev, Byte8_t *j2klen);
378
379 Byte_t * recons_PCRLbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
380                               Byte_t *j2kstream, Byte8_t csn,
381                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
382                               int *max_reslev, Byte8_t *j2klen);
383
384 Byte_t * recons_CPRLbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
385                               Byte_t *j2kstream, Byte8_t csn,
386                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
387                               int *max_reslev, Byte8_t *j2klen);
388
389 Byte_t * recons_bitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
390                           Byte_t *j2kstream, Byte8_t csn,
391                           Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
392                           int *max_reslev, Byte8_t *j2klen)
393 {
394     switch (COD.prog_order) {
395     case OPJ_LRCP:
396         return recons_LRCPbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
397                                     COD, mindeclev, max_reslev, j2klen);
398     case OPJ_RLCP:
399         return recons_RLCPbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
400                                     COD, mindeclev, max_reslev, j2klen);
401     case OPJ_RPCL:
402         return recons_RPCLbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
403                                     COD, mindeclev, max_reslev, j2klen);
404     case OPJ_PCRL:
405         return recons_PCRLbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
406                                     COD, mindeclev, max_reslev, j2klen);
407     case OPJ_CPRL:
408         return recons_CPRLbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
409                                     COD, mindeclev, max_reslev, j2klen);
410     default:
411         fprintf(FCGI_stderr, "Error, progression order not supported\n");
412     }
413     return j2kstream;
414 }
415
416 int comp_numOfprcts(Byte8_t tileID, SIZmarker_param_t SIZ,
417                     CODmarker_param_t COD, int r);
418 Byte8_t comp_seqID(Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD,
419                    int r, int p);
420
421 Byte_t * recons_packet(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
422                        Byte_t *j2kstream, Byte8_t csn,
423                        Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int *max_reslev,
424                        int comp_idx, int res_idx, int prct_idx, int lay_idx, Byte8_t *j2klen);
425
426 Byte_t * recons_LRCPbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
427                               Byte_t *j2kstream, Byte8_t csn,
428                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
429                               int *max_reslev, Byte8_t *j2klen)
430 {
431     int r, p, c, l, numOfprcts;
432
433     for (l = 0; l < COD.numOflayers; l++)
434         for (r = 0; r <= (COD.numOfdecomp - mindeclev); r++) {
435             if (COD.Scod & 0x01) {
436                 numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
437             } else {
438                 numOfprcts = 1;
439             }
440
441             for (c = 0; c < SIZ.Csiz; c++)
442                 for (p = 0; p < numOfprcts; p++) {
443                     j2kstream = recons_packet(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
444                                               COD, max_reslev, c, r, p, l, j2klen);
445                 }
446         }
447
448     return j2kstream;
449 }
450
451 Byte_t * recons_RLCPbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
452                               Byte_t *j2kstream, Byte8_t csn,
453                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
454                               int *max_reslev, Byte8_t *j2klen)
455 {
456     int r, p, c, l, numOfprcts;
457
458     for (r = 0; r <= (COD.numOfdecomp - mindeclev); r++) {
459         if (COD.Scod & 0x01) {
460             numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
461         } else {
462             numOfprcts = 1;
463         }
464
465         for (l = 0; l < COD.numOflayers; l++)
466             for (c = 0; c < SIZ.Csiz; c++)
467                 for (p = 0; p < numOfprcts; p++) {
468                     j2kstream = recons_packet(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
469                                               COD, max_reslev, c, r, p, l, j2klen);
470                 }
471     }
472
473     return j2kstream;
474 }
475
476 Byte_t * recons_precinct(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
477                          Byte_t *j2kstream, Byte8_t csn,
478                          Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int *max_reslev,
479                          int comp_idx, int res_idx, Byte8_t seqID, Byte8_t *j2klen);
480
481 Byte_t * recons_RPCLbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
482                               Byte_t *j2kstream, Byte8_t csn,
483                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
484                               int *max_reslev, Byte8_t *j2klen)
485 {
486     int r, p, c, numOfprcts;
487     Byte8_t seqID;
488
489     for (r = 0, seqID = 0; r <= (COD.numOfdecomp - mindeclev); r++) {
490
491         if (COD.Scod & 0x01) {
492             numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
493         } else {
494             numOfprcts = 1;
495         }
496
497         for (p = 0; p < numOfprcts; p++, seqID++)
498             for (c = 0; c < SIZ.Csiz; c++) {
499                 j2kstream = recons_precinct(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
500                                             COD, max_reslev, c, r, seqID, j2klen);
501             }
502     }
503
504     return j2kstream;
505 }
506
507 Byte_t * recons_PCRLbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
508                               Byte_t *j2kstream, Byte8_t csn,
509                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
510                               int *max_reslev, Byte8_t *j2klen)
511 {
512     int r, p, c, min_numOfprcts, numOfprcts, min_numOfres;
513     Byte8_t seqID;
514
515     min_numOfres = COD.numOfdecomp - mindeclev + 1;
516
517     if (COD.Scod & 0x01) {
518         min_numOfprcts = 0;
519         for (r = 0; r < min_numOfres; r++) {
520             numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
521
522             if (numOfprcts < min_numOfprcts || min_numOfprcts == 0) {
523                 min_numOfprcts = numOfprcts;
524             }
525         }
526     } else {
527         min_numOfprcts = 1;
528     }
529
530     for (p = 0; p < min_numOfprcts; p++)
531         for (c = 0; c < SIZ.Csiz; c++)
532             for (r = 0; r < min_numOfres; r++) {
533                 seqID = comp_seqID(tileID, SIZ, COD, r, p);
534                 j2kstream = recons_precinct(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
535                                             COD, max_reslev, c, r, seqID, j2klen);
536             }
537
538     return j2kstream;
539 }
540
541
542 Byte_t * recons_CPRLbitstream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
543                               Byte_t *j2kstream, Byte8_t csn,
544                               Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev,
545                               int *max_reslev, Byte8_t *j2klen)
546 {
547     int r, p, c, min_numOfprcts, numOfprcts, min_numOfres;
548     Byte8_t seqID;
549
550     min_numOfres = COD.numOfdecomp - mindeclev + 1;
551
552     if (COD.Scod & 0x01) {
553         min_numOfprcts = 0;
554         for (r = 0; r < min_numOfres; r++) {
555             numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
556
557             if (numOfprcts < min_numOfprcts || min_numOfprcts == 0) {
558                 min_numOfprcts = numOfprcts;
559             }
560         }
561     } else {
562         min_numOfprcts = 1;
563     }
564
565     for (c = 0; c < SIZ.Csiz; c++)
566         for (p = 0; p < min_numOfprcts; p++)
567             for (r = 0; r < min_numOfres; r++) {
568                 seqID = comp_seqID(tileID, SIZ, COD, r, p);
569                 j2kstream = recons_precinct(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
570                                             COD, max_reslev, c, r, seqID, j2klen);
571             }
572
573     return j2kstream;
574 }
575
576 int comp_numOfprcts(Byte8_t tileID, SIZmarker_param_t SIZ,
577                     CODmarker_param_t COD, int r)
578 {
579     int ret;
580     Byte4_t XTsiz, YTsiz;
581
582     XTsiz = get_tile_XSiz(SIZ, (Byte4_t)tileID, COD.numOfdecomp - r);
583     YTsiz = get_tile_YSiz(SIZ, (Byte4_t)tileID, COD.numOfdecomp - r);
584
585     ret = (int)(ceil((double)XTsiz / (double)COD.XPsiz[r]) * ceil((double)YTsiz /
586                 (double)COD.YPsiz[r]));
587     assert(ret >= 0);
588     return ret;
589 }
590
591 Byte_t * add_padding(Byte8_t padding, Byte_t *j2kstream, Byte8_t *j2klen);
592
593 Byte_t * recons_packet(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
594                        Byte_t *j2kstream, Byte8_t csn,
595                        Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int *max_reslev,
596                        int comp_idx, int res_idx, int prct_idx, int lay_idx, Byte8_t *j2klen)
597 {
598     Byte8_t seqID, precID, binOffset;
599     message_param_t *ptr;
600     OPJ_BOOL foundPrec;
601     int l;
602
603     seqID = comp_seqID(tileID, SIZ, COD, res_idx, prct_idx);
604     precID = comp_precinct_id((int)tileID, comp_idx, (int)seqID, (int)SIZ.Csiz,
605                               (int)SIZ.XTnum * (int)SIZ.YTnum);
606
607     ptr = msgqueue->first;
608     binOffset = 0;
609     foundPrec = OPJ_FALSE;
610     l = 0;
611
612     while ((ptr = search_message(PRECINCT_MSG, precID, csn, ptr)) != NULL) {
613         if (ptr->bin_offset == binOffset) {
614             if (lay_idx == l) {
615                 j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
616                 foundPrec = OPJ_TRUE;
617                 if (*max_reslev < res_idx) {
618                     *max_reslev = res_idx;
619                 }
620
621                 break;
622             }
623             binOffset += ptr->length;
624             l++;
625         }
626         ptr = ptr->next;
627     }
628     if (!foundPrec && COD.Scod & 0x01) {
629         j2kstream = add_padding(1, j2kstream, j2klen);
630     }
631
632     return j2kstream;
633 }
634
635
636 Byte_t * recons_precinct(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
637                          Byte_t *j2kstream, Byte8_t csn,
638                          Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int *max_reslev,
639                          int comp_idx, int res_idx, Byte8_t seqID, Byte8_t *j2klen)
640 {
641     Byte8_t precID, binOffset;
642     message_param_t *ptr;
643     OPJ_BOOL foundPrec;
644
645     precID = comp_precinct_id((int)tileID, comp_idx, (int)seqID, (int)SIZ.Csiz,
646                               (int)SIZ.XTnum * (int)SIZ.YTnum);
647
648     ptr = msgqueue->first;
649     binOffset = 0;
650     foundPrec = OPJ_FALSE;
651
652     while ((ptr = search_message(PRECINCT_MSG, precID, csn, ptr)) != NULL) {
653         if (ptr->bin_offset == binOffset) {
654             j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
655
656             foundPrec = OPJ_TRUE;
657             binOffset += ptr->length;
658             if (*max_reslev < res_idx) {
659                 *max_reslev = res_idx;
660             }
661
662             if (ptr->last_byte) {
663                 break;
664             }
665         }
666         ptr = ptr->next;
667     }
668     if (!foundPrec && COD.Scod & 0x01) {
669         j2kstream = add_padding(COD.numOflayers, j2kstream, j2klen);
670     }
671
672     return j2kstream;
673 }
674
675 Byte8_t comp_seqID(Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD,
676                    int r, int p)
677 {
678     Byte8_t seqID = 0;
679     int rr;
680     assert(p >= 0);
681     assert(r >= 0);
682
683     for (rr = 0; rr < r; rr++) {
684         seqID += (Byte8_t)comp_numOfprcts(tileID, SIZ, COD, rr);
685     }
686
687     seqID += (Byte8_t)p;
688
689     return seqID;
690 }
691
692 Byte8_t get_last_tileID(msgqueue_param_t *msgqueue, Byte8_t csn,
693                         OPJ_BOOL isjppstream)
694 {
695     Byte8_t last_tileID = 0;
696     message_param_t *msg;
697
698     msg = msgqueue->first;
699     while (msg) {
700         if (isjppstream) {
701             if ((msg->class_id == TILE_HEADER_MSG) && msg->csn == csn &&
702                     last_tileID < msg->in_class_id) {
703                 last_tileID = msg->in_class_id;
704             }
705         } else {
706             if ((msg->class_id == TILE_MSG || msg->class_id == EXT_TILE_MSG) &&
707                     msg->csn == csn && last_tileID < msg->in_class_id) {
708                 last_tileID = msg->in_class_id;
709             }
710         }
711         msg = msg->next;
712     }
713     return last_tileID;
714 }
715
716
717 message_param_t * search_message(Byte8_t class_id, Byte8_t in_class_id,
718                                  Byte8_t csn, message_param_t *msg)
719 {
720     while (msg != NULL) {
721         if (in_class_id == (Byte8_t) - 1) {
722             if (msg->class_id == class_id && msg->csn == csn) {
723                 return msg;
724             }
725         } else {
726             if (msg->class_id == class_id && msg->in_class_id == in_class_id &&
727                     msg->csn == csn) {
728                 return msg;
729             }
730         }
731         msg = msg->next;
732     }
733     return NULL;
734 }
735
736
737 Byte_t * gene_msgstream(message_param_t *message, Byte_t *stream,
738                         Byte8_t *length);
739 Byte_t * gene_emptytilestream(const Byte8_t tileID, Byte8_t *length);
740
741 Byte_t * add_msgstream(message_param_t *message, Byte_t *origstream,
742                        Byte_t *j2kstream, Byte8_t *j2klen)
743 {
744     Byte_t *newstream;
745     Byte8_t newlen;
746     Byte_t *buf;
747
748     if (!message) {
749         return NULL;
750     }
751
752     newstream = gene_msgstream(message, origstream, &newlen);
753
754     buf = (Byte_t *)opj_malloc((*j2klen) + newlen);
755
756     memcpy(buf, j2kstream, *j2klen);
757     memcpy(buf + (*j2klen), newstream, newlen);
758
759     *j2klen += newlen;
760
761     opj_free(newstream);
762     if (j2kstream) {
763         opj_free(j2kstream);
764     }
765
766     return buf;
767 }
768
769
770 Byte_t * add_emptyboxstream(placeholder_param_t *phld, Byte_t *jp2stream,
771                             Byte8_t *jp2len)
772 {
773     Byte_t *newstream;
774     Byte8_t newlen;
775     Byte_t *buf;
776
777     if (phld->OrigBHlen == 8) {
778         newlen = big4(phld->OrigBH);
779     } else {
780         newlen = big8(phld->OrigBH + 8);
781     }
782
783     newstream = (Byte_t *)opj_malloc(newlen);
784     memset(newstream, 0, newlen);
785     memcpy(newstream, phld->OrigBH, phld->OrigBHlen);
786
787     buf = (Byte_t *)opj_malloc((*jp2len) + newlen);
788
789     memcpy(buf, jp2stream, *jp2len);
790     memcpy(buf + (*jp2len), newstream, newlen);
791
792     *jp2len += newlen;
793
794     opj_free(newstream);
795     if (jp2stream) {
796         opj_free(jp2stream);
797     }
798
799     return buf;
800 }
801
802 Byte_t * add_emptytilestream(const Byte8_t tileID, Byte_t *j2kstream,
803                              Byte8_t *j2klen)
804 {
805     Byte_t *newstream;
806     Byte8_t newlen;
807     Byte_t *buf;
808
809     newstream = gene_emptytilestream(tileID, &newlen);
810
811     buf = (Byte_t *)opj_malloc((*j2klen) + newlen);
812
813     memcpy(buf, j2kstream, *j2klen);
814     memcpy(buf + (*j2klen), newstream, newlen);
815
816     *j2klen += newlen;
817
818     opj_free(newstream);
819     if (j2kstream) {
820         opj_free(j2kstream);
821     }
822
823     return buf;
824 }
825
826 Byte_t * add_padding(Byte8_t padding, Byte_t *j2kstream, Byte8_t *j2klen)
827 {
828     Byte_t *buf;
829
830     buf = (Byte_t *)opj_malloc((*j2klen) + padding);
831
832     memcpy(buf, j2kstream, *j2klen);
833     memset(buf + (*j2klen), 0, padding);
834
835     *j2klen += padding;
836
837     if (j2kstream) {
838         opj_free(j2kstream);
839     }
840
841     return buf;
842 }
843
844 Byte_t * add_EOC(Byte_t *j2kstream, Byte8_t *j2klen)
845 {
846     Byte2_t EOC = 0xd9ff;
847
848     Byte_t *buf;
849
850     buf = (Byte_t *)opj_malloc((*j2klen) + 2);
851
852     memcpy(buf, j2kstream, *j2klen);
853     memcpy(buf + (*j2klen), &EOC, 2);
854
855     *j2klen += 2;
856
857     if (j2kstream) {
858         opj_free(j2kstream);
859     }
860
861     return buf;
862 }
863
864 Byte_t * gene_msgstream(message_param_t *message, Byte_t *stream,
865                         Byte8_t *length)
866 {
867     Byte_t *buf;
868
869     if (!message) {
870         return NULL;
871     }
872
873     *length = message->length;
874     buf = (Byte_t *)opj_malloc(*length);
875     memcpy(buf, stream + message->res_offset,  *length);
876
877     return buf;
878 }
879
880 Byte_t * gene_emptytilestream(const Byte8_t tileID, Byte8_t *length)
881 {
882     Byte_t *buf;
883     const Byte2_t SOT = 0x90ff;
884     const Byte2_t Lsot = 0xa << 8;
885     Byte2_t Isot;
886     const Byte4_t Psot = 0xe << 24;
887     const Byte_t TPsot = 0, TNsot = 1;
888     const Byte2_t SOD = 0x93ff;
889
890     *length = 14;
891     buf = (Byte_t *)opj_malloc(*length);
892
893     Isot = (Byte2_t)((((Byte2_t)tileID) << 8) | ((((Byte2_t)tileID) & 0xf0) >> 8));
894
895     memcpy(buf, &SOT, 2);
896     memcpy(buf + 2, &Lsot, 2);
897     memcpy(buf + 4, &Isot, 2);
898     memcpy(buf + 6, &Psot, 4);
899     memcpy(buf + 10, &TPsot, 1);
900     memcpy(buf + 11, &TNsot, 1);
901     memcpy(buf + 12, &SOD, 2);
902
903     return buf;
904 }
905
906 Byte_t * recons_j2kmainhead(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
907                             Byte8_t csn, Byte8_t *j2klen)
908 {
909     *j2klen = 0;
910     return add_mainhead_msgstream(msgqueue, jpipstream, NULL, csn, j2klen);
911 }