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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
37 #include "jp2k_encoder.h"
38 #include "j2kheader_manager.h"
39 #include "imgreg_manager.h"
40 #include "opj_inttypes.h"
44 #include "fcgi_stdio.h"
45 #define logstream FCGI_stdout
47 #define FCGI_stdout stdout
48 #define FCGI_stderr stderr
49 #define logstream stderr
54 * search a message by class_id
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
62 message_param_t * search_message(Byte8_t class_id, Byte8_t in_class_id,
63 Byte8_t csn, message_param_t *msg);
66 * reconstruct j2k codestream from JPT- (in future, JPP-) stream
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
76 Byte_t * recons_codestream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
77 Byte8_t csn, int fw, int fh, Byte8_t *codelen);
79 Byte_t * recons_j2k(msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn,
80 int fw, int fh, Byte8_t *j2klen)
82 Byte_t *j2kstream = NULL;
88 j2kstream = recons_codestream(msgqueue, jpipstream, csn, fw, fh, j2klen);
93 Byte_t * add_emptyboxstream(placeholder_param_t *phld, Byte_t *jp2stream,
95 Byte_t * add_msgstream(message_param_t *message, Byte_t *origstream,
96 Byte_t *j2kstream, Byte8_t *j2klen);
98 Byte_t * recons_jp2(msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn,
101 message_param_t *ptr;
102 Byte_t *jp2stream = NULL;
103 Byte_t *codestream = NULL;
105 Byte8_t jp2cDBoxOffset = 0, jp2cDBoxlen = 0;
113 ptr = msgqueue->first;
114 while ((ptr = search_message(METADATA_MSG, (Byte8_t) - 1, csn, ptr)) != NULL) {
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;
121 jp2stream = add_emptyboxstream(ptr->phld, jp2stream,
122 jp2len); /* header only */
125 jp2stream = add_msgstream(ptr, jpipstream, jp2stream, jp2len);
129 codestream = recons_codestream(msgqueue, jpipstream, csn, 0, 0, &codelen);
131 if (jp2cDBoxOffset != 0 && codelen <= jp2cDBoxlen) {
132 memcpy(jp2stream + jp2cDBoxOffset, codestream, codelen);
135 opj_free(codestream);
140 OPJ_BOOL isJPPstream(Byte8_t csn, msgqueue_param_t *msgqueue);
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);
147 Byte_t * add_EOC(Byte_t *j2kstream, Byte8_t *j2klen);
149 Byte_t * recons_codestream(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
150 Byte8_t csn, int fw, int fh, Byte8_t *codelen)
152 if (isJPPstream(csn, msgqueue)) {
153 return recons_codestream_from_JPPstream(msgqueue, jpipstream, csn, fw, fh,
156 return recons_codestream_from_JPTstream(msgqueue, jpipstream, csn, fw, fh,
161 OPJ_BOOL isJPPstream(Byte8_t csn, msgqueue_param_t *msgqueue)
163 message_param_t *msg;
165 msg = msgqueue->first;
167 if (msg->csn == csn) {
168 if (msg->class_id <= 2) {
170 } else if (msg->class_id == 4 || msg->class_id == 5) {
177 fprintf(FCGI_stderr, "Error, message of csn %" PRId64 " not found\n", csn);
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,
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)
192 Byte_t *j2kstream = NULL;
193 Byte8_t last_tileID, tileID;
196 message_param_t *ptr;
197 SIZmarker_param_t SIZ;
198 OPJ_SIZE_T mindeclev;
201 j2kstream = add_mainhead_msgstream(msgqueue, jpipstream, j2kstream, csn,
204 if (!get_mainheader_from_j2kstream(j2kstream, &SIZ, NULL)) {
208 if (fw <= 0 || fh <= 0) {
211 mindeclev = (OPJ_SIZE_T)comp_decomplev(fw, fh, (int)SIZ.Xsiz, (int)SIZ.Ysiz);
214 last_tileID = get_last_tileID(msgqueue, csn, OPJ_FALSE);
216 for (tileID = 0; tileID <= last_tileID; tileID++) {
220 ptr = msgqueue->first;
221 while ((ptr = search_message(TILE_MSG, tileID, csn, ptr)) != NULL) {
222 if (ptr->bin_offset == binOffset) {
224 j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
225 binOffset += ptr->length;
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) {
234 j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
235 binOffset += ptr->length;
241 j2kstream = add_emptytilestream(tileID, j2kstream, j2klen);
245 j2kstream = add_EOC(j2kstream, j2klen);
250 Byte_t * add_SOTmkr(Byte_t *j2kstream, Byte8_t *j2klen);
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);
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)
260 Byte_t *j2kstream = NULL;
261 Byte8_t tileID, last_tileID;
265 message_param_t *ptr;
266 SIZmarker_param_t SIZ;
267 CODmarker_param_t COD;
268 int max_reslev, mindeclev;
271 j2kstream = add_mainhead_msgstream(msgqueue, jpipstream, j2kstream, csn,
274 if (!get_mainheader_from_j2kstream(j2kstream, &SIZ, &COD)) {
278 if (fw == 0 || fh == 0) {
281 mindeclev = comp_decomplev(fw, fh, (int)SIZ.Xsiz, (int)SIZ.Ysiz);
285 last_tileID = get_last_tileID(msgqueue, csn, OPJ_TRUE);
287 for (tileID = 0; tileID <= last_tileID; tileID++) {
289 ptr = msgqueue->first;
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);
298 binOffset += ptr->length;
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);
309 j2kstream = add_emptytilestream(tileID, j2kstream, j2klen);
313 if (max_reslev < COD.numOfdecomp)
314 if (!modify_mainheader(j2kstream, max_reslev, SIZ, COD, j2klen)) {
319 j2kstream = add_EOC(j2kstream, j2klen);
325 Byte_t * add_mainhead_msgstream(msgqueue_param_t *msgqueue, Byte_t *origstream,
326 Byte_t *j2kstream, Byte8_t csn, Byte8_t *j2klen)
328 message_param_t *ptr;
331 ptr = msgqueue->first;
334 while ((ptr = search_message(MAINHEADER_MSG, (Byte8_t) - 1, csn,
336 if (ptr->bin_offset == binOffset) {
337 j2kstream = add_msgstream(ptr, origstream, j2kstream, j2klen);
338 binOffset += ptr->length;
345 Byte_t * add_SOTmkr(Byte_t *j2kstream, Byte8_t *j2klen)
348 const Byte2_t SOT = 0x90ff;
350 buf = (Byte_t *)opj_malloc((*j2klen) + 2);
352 memcpy(buf, j2kstream, *j2klen);
353 memcpy(buf + (*j2klen), &SOT, 2);
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);
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);
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);
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);
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);
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)
394 switch (COD.prog_order) {
396 return recons_LRCPbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
397 COD, mindeclev, max_reslev, j2klen);
399 return recons_RLCPbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
400 COD, mindeclev, max_reslev, j2klen);
402 return recons_RPCLbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
403 COD, mindeclev, max_reslev, j2klen);
405 return recons_PCRLbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
406 COD, mindeclev, max_reslev, j2klen);
408 return recons_CPRLbitstream(msgqueue, jpipstream, j2kstream, csn, tileID, SIZ,
409 COD, mindeclev, max_reslev, j2klen);
411 fprintf(FCGI_stderr, "Error, progression order not supported\n");
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,
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);
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)
431 int r, p, c, l, numOfprcts;
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);
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);
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)
456 int r, p, c, l, numOfprcts;
458 for (r = 0; r <= (COD.numOfdecomp - mindeclev); r++) {
459 if (COD.Scod & 0x01) {
460 numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
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);
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);
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)
486 int r, p, c, numOfprcts;
489 for (r = 0, seqID = 0; r <= (COD.numOfdecomp - mindeclev); r++) {
491 if (COD.Scod & 0x01) {
492 numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
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);
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)
512 int r, p, c, min_numOfprcts, numOfprcts, min_numOfres;
515 min_numOfres = COD.numOfdecomp - mindeclev + 1;
517 if (COD.Scod & 0x01) {
519 for (r = 0; r < min_numOfres; r++) {
520 numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
522 if (numOfprcts < min_numOfprcts || min_numOfprcts == 0) {
523 min_numOfprcts = numOfprcts;
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);
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)
547 int r, p, c, min_numOfprcts, numOfprcts, min_numOfres;
550 min_numOfres = COD.numOfdecomp - mindeclev + 1;
552 if (COD.Scod & 0x01) {
554 for (r = 0; r < min_numOfres; r++) {
555 numOfprcts = comp_numOfprcts(tileID, SIZ, COD, r);
557 if (numOfprcts < min_numOfprcts || min_numOfprcts == 0) {
558 min_numOfprcts = numOfprcts;
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);
576 int comp_numOfprcts(Byte8_t tileID, SIZmarker_param_t SIZ,
577 CODmarker_param_t COD, int r)
580 Byte4_t XTsiz, YTsiz;
582 XTsiz = get_tile_XSiz(SIZ, (Byte4_t)tileID, COD.numOfdecomp - r);
583 YTsiz = get_tile_YSiz(SIZ, (Byte4_t)tileID, COD.numOfdecomp - r);
585 ret = (int)(ceil((double)XTsiz / (double)COD.XPsiz[r]) * ceil((double)YTsiz /
586 (double)COD.YPsiz[r]));
591 Byte_t * add_padding(Byte8_t padding, Byte_t *j2kstream, Byte8_t *j2klen);
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)
598 Byte8_t seqID, precID, binOffset;
599 message_param_t *ptr;
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);
607 ptr = msgqueue->first;
609 foundPrec = OPJ_FALSE;
612 while ((ptr = search_message(PRECINCT_MSG, precID, csn, ptr)) != NULL) {
613 if (ptr->bin_offset == binOffset) {
615 j2kstream = add_msgstream(ptr, jpipstream, j2kstream, j2klen);
616 foundPrec = OPJ_TRUE;
617 if (*max_reslev < res_idx) {
618 *max_reslev = res_idx;
623 binOffset += ptr->length;
628 if (!foundPrec && COD.Scod & 0x01) {
629 j2kstream = add_padding(1, j2kstream, j2klen);
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)
641 Byte8_t precID, binOffset;
642 message_param_t *ptr;
645 precID = comp_precinct_id((int)tileID, comp_idx, (int)seqID, (int)SIZ.Csiz,
646 (int)SIZ.XTnum * (int)SIZ.YTnum);
648 ptr = msgqueue->first;
650 foundPrec = OPJ_FALSE;
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);
656 foundPrec = OPJ_TRUE;
657 binOffset += ptr->length;
658 if (*max_reslev < res_idx) {
659 *max_reslev = res_idx;
662 if (ptr->last_byte) {
668 if (!foundPrec && COD.Scod & 0x01) {
669 j2kstream = add_padding(COD.numOflayers, j2kstream, j2klen);
675 Byte8_t comp_seqID(Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD,
683 for (rr = 0; rr < r; rr++) {
684 seqID += (Byte8_t)comp_numOfprcts(tileID, SIZ, COD, rr);
692 Byte8_t get_last_tileID(msgqueue_param_t *msgqueue, Byte8_t csn,
693 OPJ_BOOL isjppstream)
695 Byte8_t last_tileID = 0;
696 message_param_t *msg;
698 msg = msgqueue->first;
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;
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;
717 message_param_t * search_message(Byte8_t class_id, Byte8_t in_class_id,
718 Byte8_t csn, message_param_t *msg)
720 while (msg != NULL) {
721 if (in_class_id == (Byte8_t) - 1) {
722 if (msg->class_id == class_id && msg->csn == csn) {
726 if (msg->class_id == class_id && msg->in_class_id == in_class_id &&
737 Byte_t * gene_msgstream(message_param_t *message, Byte_t *stream,
739 Byte_t * gene_emptytilestream(const Byte8_t tileID, Byte8_t *length);
741 Byte_t * add_msgstream(message_param_t *message, Byte_t *origstream,
742 Byte_t *j2kstream, Byte8_t *j2klen)
752 newstream = gene_msgstream(message, origstream, &newlen);
754 buf = (Byte_t *)opj_malloc((*j2klen) + newlen);
756 memcpy(buf, j2kstream, *j2klen);
757 memcpy(buf + (*j2klen), newstream, newlen);
770 Byte_t * add_emptyboxstream(placeholder_param_t *phld, Byte_t *jp2stream,
777 if (phld->OrigBHlen == 8) {
778 newlen = big4(phld->OrigBH);
780 newlen = big8(phld->OrigBH + 8);
783 newstream = (Byte_t *)opj_malloc(newlen);
784 memset(newstream, 0, newlen);
785 memcpy(newstream, phld->OrigBH, phld->OrigBHlen);
787 buf = (Byte_t *)opj_malloc((*jp2len) + newlen);
789 memcpy(buf, jp2stream, *jp2len);
790 memcpy(buf + (*jp2len), newstream, newlen);
802 Byte_t * add_emptytilestream(const Byte8_t tileID, Byte_t *j2kstream,
809 newstream = gene_emptytilestream(tileID, &newlen);
811 buf = (Byte_t *)opj_malloc((*j2klen) + newlen);
813 memcpy(buf, j2kstream, *j2klen);
814 memcpy(buf + (*j2klen), newstream, newlen);
826 Byte_t * add_padding(Byte8_t padding, Byte_t *j2kstream, Byte8_t *j2klen)
830 buf = (Byte_t *)opj_malloc((*j2klen) + padding);
832 memcpy(buf, j2kstream, *j2klen);
833 memset(buf + (*j2klen), 0, padding);
844 Byte_t * add_EOC(Byte_t *j2kstream, Byte8_t *j2klen)
846 Byte2_t EOC = 0xd9ff;
850 buf = (Byte_t *)opj_malloc((*j2klen) + 2);
852 memcpy(buf, j2kstream, *j2klen);
853 memcpy(buf + (*j2klen), &EOC, 2);
864 Byte_t * gene_msgstream(message_param_t *message, Byte_t *stream,
873 *length = message->length;
874 buf = (Byte_t *)opj_malloc(*length);
875 memcpy(buf, stream + message->res_offset, *length);
880 Byte_t * gene_emptytilestream(const Byte8_t tileID, Byte8_t *length)
883 const Byte2_t SOT = 0x90ff;
884 const Byte2_t Lsot = 0xa << 8;
886 const Byte4_t Psot = 0xe << 24;
887 const Byte_t TPsot = 0, TNsot = 1;
888 const Byte2_t SOD = 0x93ff;
891 buf = (Byte_t *)opj_malloc(*length);
893 Isot = (Byte2_t)((((Byte2_t)tileID) << 8) | ((((Byte2_t)tileID) & 0xf0) >> 8));
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);
906 Byte_t * recons_j2kmainhead(msgqueue_param_t *msgqueue, Byte_t *jpipstream,
907 Byte8_t csn, Byte8_t *j2klen)
910 return add_mainhead_msgstream(msgqueue, jpipstream, NULL, csn, j2klen);