/* * $Id$ * * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium * Copyright (c) 2002-2011, Professor Benoit Macq * Copyright (c) 2010-2011, Kaori Hagihara * Copyright (c) 2011, Lucian Corlaciu, GSoC * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include "jp2k_encoder.h" #include "j2kheader_manager.h" #include "imgreg_manager.h" #ifdef SERVER #include "fcgi_stdio.h" #define logstream FCGI_stdout #else #define FCGI_stdout stdout #define FCGI_stderr stderr #define logstream stderr #endif //SERVER /** * search a message by class_id * * @param[in] class_id class identifiers * @param[in] in_class_id in-class identifiers, -1 means any * @param[in] csn codestream number * @param[in] msg first message pointer of the searching list * @return found message pointer */ message_param_t * search_message( Byte8_t class_id, Byte8_t in_class_id, Byte8_t csn, message_param_t *msg); /** * reconstruct j2k codestream from JPT- (in future, JPP-) stream * * @param[in] msgqueue message queue pointer * @param[in] jpipstream original JPT- JPP- stream * @param[in] csn codestream number * @param[in] fw reconstructing image frame width * @param[in] fh reconstructing image frame height * @param[out] codelen codestream length * @return generated reconstructed j2k codestream */ Byte_t * recons_codestream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *codelen); Byte_t * recons_j2k( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *j2klen) { Byte_t *j2kstream = NULL; if( !msgqueue) return NULL; j2kstream = recons_codestream( msgqueue, jpipstream, csn, fw, fh, j2klen); return j2kstream; } Byte_t * add_emptyboxstream( placeholder_param_t *phld, Byte_t *jp2stream, Byte8_t *jp2len); Byte_t * add_msgstream( message_param_t *message, Byte_t *origstream, Byte_t *j2kstream, Byte8_t *j2klen); Byte_t * recons_jp2( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, Byte8_t *jp2len) { message_param_t *ptr; Byte_t *jp2stream = NULL; Byte_t *codestream = NULL; Byte8_t codelen; Byte8_t jp2cDBoxOffset = 0, jp2cDBoxlen = 0; *jp2len = 0; if( !msgqueue) return NULL; ptr = msgqueue->first; while(( ptr = search_message( METADATA_MSG, -1, csn, ptr))!=NULL){ if( ptr->phld){ if( strncmp( (char *)ptr->phld->OrigBH+4, "jp2c", 4) == 0){ jp2cDBoxOffset = *jp2len + ptr->phld->OrigBHlen; jp2stream = add_emptyboxstream( ptr->phld, jp2stream, jp2len); // header only jp2cDBoxlen = *jp2len - jp2cDBoxOffset; } else jp2stream = add_emptyboxstream( ptr->phld, jp2stream, jp2len); // header only } jp2stream = add_msgstream( ptr, jpipstream, jp2stream, jp2len); ptr = ptr->next; } codestream = recons_codestream( msgqueue, jpipstream, csn, 0, 0, &codelen); if( jp2cDBoxOffset != 0 && codelen <= jp2cDBoxlen) memcpy( jp2stream+jp2cDBoxOffset, codestream, codelen); free( codestream); return jp2stream; } bool isJPPstream( Byte8_t csn, msgqueue_param_t *msgqueue); Byte_t * recons_codestream_from_JPTstream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *j2klen); Byte_t * recons_codestream_from_JPPstream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *j2klen); Byte_t * add_EOC( Byte_t *j2kstream, Byte8_t *j2klen); Byte_t * recons_codestream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *codelen) { if( isJPPstream( csn, msgqueue)) return recons_codestream_from_JPPstream( msgqueue, jpipstream, csn, fw, fh, codelen); else return recons_codestream_from_JPTstream( msgqueue, jpipstream, csn, fw, fh, codelen); } bool isJPPstream( Byte8_t csn, msgqueue_param_t *msgqueue) { message_param_t *msg; msg = msgqueue->first; while( msg){ if( msg->csn == csn){ if( msg->class_id <= 2) return true; else if( msg->class_id == 4 || msg->class_id == 5) return false; } msg = msg->next; } fprintf( FCGI_stderr, "Error, message of csn %lld not found\n", csn); return false; } Byte_t * add_mainhead_msgstream( msgqueue_param_t *msgqueue, Byte_t *origstream, Byte_t *j2kstream, Byte8_t csn, Byte8_t *j2klen); Byte8_t get_last_tileID( msgqueue_param_t *msgqueue, Byte8_t csn, bool isJPPstream); Byte_t * add_emptytilestream( const Byte8_t tileID, Byte_t *j2kstream, Byte8_t *j2klen); Byte_t * recons_codestream_from_JPTstream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *j2klen) { Byte_t *j2kstream = NULL; Byte8_t last_tileID, tileID; bool found; Byte8_t binOffset; message_param_t *ptr; SIZmarker_param_t SIZ; int mindeclev; *j2klen = 0; j2kstream = add_mainhead_msgstream( msgqueue, jpipstream, j2kstream, csn, j2klen); if( !get_mainheader_from_j2kstream( j2kstream, &SIZ, NULL)) return j2kstream; if( fw <= 0 || fh <= 0) mindeclev = 0; else mindeclev = comp_decomplev( fw, fh, SIZ.Xsiz, SIZ.Ysiz); last_tileID = get_last_tileID( msgqueue, csn, false); for( tileID=0; tileID <= last_tileID; tileID++){ found = false; binOffset = 0; ptr = msgqueue->first; while(( ptr = search_message( TILE_MSG, tileID, csn, ptr))!=NULL){ if( ptr->bin_offset == binOffset){ found = true; j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); binOffset += ptr->length; } ptr = ptr->next; } ptr = msgqueue->first; while(( ptr = search_message( EXT_TILE_MSG, tileID, csn, ptr))!=NULL){ if( ptr->aux > mindeclev){ if( ptr->bin_offset == binOffset){ found = true; j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); binOffset += ptr->length; } } ptr = ptr->next; } if(!found) j2kstream = add_emptytilestream( tileID, j2kstream, j2klen); } j2kstream = add_EOC( j2kstream, j2klen); return j2kstream; } Byte_t * recons_RPCLbitstream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte_t *j2kstream, Byte8_t csn, Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev, int *max_reslev, Byte8_t *j2klen); Byte_t * recons_codestream_from_JPPstream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn, int fw, int fh, Byte8_t *j2klen) { Byte_t *j2kstream = NULL; Byte8_t tileID, last_tileID; Byte8_t SOToffset; bool foundTH; Byte8_t binOffset; message_param_t *ptr; SIZmarker_param_t SIZ; CODmarker_param_t COD; int max_reslev, mindeclev; *j2klen = 0; j2kstream = add_mainhead_msgstream( msgqueue, jpipstream, j2kstream, csn, j2klen); if( !get_mainheader_from_j2kstream( j2kstream, &SIZ, &COD)) return j2kstream; if( COD.prog_order != RPCL){ fprintf( FCGI_stderr, "Error, Only RPCL order supported\n"); return j2kstream; } if( fw == 0 || fh == 0) mindeclev = 0; else mindeclev = comp_decomplev( fw, fh, SIZ.Xsiz, SIZ.Ysiz); max_reslev = -1; last_tileID = get_last_tileID( msgqueue, csn, true); for( tileID=0; tileID <= last_tileID; tileID++){ ptr = msgqueue->first; binOffset = 0; foundTH = false; SOToffset = *j2klen; while(( ptr = search_message( TILE_HEADER_MSG, tileID, csn, ptr))!=NULL){ if( ptr->bin_offset == binOffset){ j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); foundTH = true; binOffset += ptr->length; } ptr = ptr->next; } if( foundTH){ j2kstream = recons_RPCLbitstream( msgqueue, jpipstream, j2kstream, csn, tileID, SIZ, COD, mindeclev, &max_reslev, j2klen); modify_tileheader( j2kstream, SOToffset, (max_reslevfirst; binOffset = 0; while(( ptr = search_message( MAINHEADER_MSG, -1, csn, ptr))!=NULL){ if( ptr->bin_offset == binOffset){ j2kstream = add_msgstream( ptr, origstream, j2kstream, j2klen); binOffset += ptr->length; } ptr = ptr->next; } return j2kstream; } Byte_t * add_padding( Byte8_t padding, Byte_t *j2kstream, Byte8_t *j2klen); Byte_t * recons_RPCLbitstream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte_t *j2kstream, Byte8_t csn, Byte8_t tileID, SIZmarker_param_t SIZ, CODmarker_param_t COD, int mindeclev, int *max_reslev, Byte8_t *j2klen) { int r, p, c; bool foundPrec; Byte8_t binOffset, precID, seqID; Byte4_t XTsiz, YTsiz; message_param_t *ptr; for( r=0, seqID=0; r<=(COD.numOfdecomp-mindeclev); r++){ XTsiz = get_tile_XSiz( SIZ, tileID, COD.numOfdecomp-r); YTsiz = get_tile_YSiz( SIZ, tileID, COD.numOfdecomp-r); for( p=0; pfirst; binOffset = 0; foundPrec = false; while(( ptr = search_message( PRECINCT_MSG, precID, csn, ptr))!=NULL){ if( ptr->bin_offset == binOffset){ j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); foundPrec = true; binOffset += ptr->length; if( *max_reslev < r) *max_reslev = r; } ptr = ptr->next; } if(!foundPrec) j2kstream = add_padding( 1, j2kstream, j2klen); } } } return j2kstream; } Byte8_t get_last_tileID( msgqueue_param_t *msgqueue, Byte8_t csn, bool isJPPstream) { Byte8_t last_tileID = 0; message_param_t *msg; msg = msgqueue->first; while( msg){ if( isJPPstream){ if((msg->class_id == TILE_HEADER_MSG) && msg->csn == csn && last_tileID < msg->in_class_id) last_tileID = msg->in_class_id; } else{ if((msg->class_id == TILE_MSG || msg->class_id == EXT_TILE_MSG) && msg->csn == csn && last_tileID < msg->in_class_id) last_tileID = msg->in_class_id; } msg = msg->next; } return last_tileID; } message_param_t * search_message( Byte8_t class_id, Byte8_t in_class_id, Byte8_t csn, message_param_t *msg) { while( msg != NULL){ if( in_class_id == -1){ if( msg->class_id == class_id && msg->csn == csn) return msg; } else{ if( msg->class_id == class_id && msg->in_class_id == in_class_id && msg->csn == csn) return msg; } msg = msg->next; } return NULL; } Byte_t * gene_msgstream( message_param_t *message, Byte_t *stream, Byte8_t *length); Byte_t * gene_emptytilestream( const Byte8_t tileID, Byte8_t *length); Byte_t * add_msgstream( message_param_t *message, Byte_t *origstream, Byte_t *j2kstream, Byte8_t *j2klen) { Byte_t *newstream; Byte8_t newlen; Byte_t *buf; if( !message) return NULL; newstream = gene_msgstream( message, origstream, &newlen); buf = (Byte_t *)malloc(( *j2klen)+newlen); memcpy( buf, j2kstream, *j2klen); memcpy( buf+(*j2klen), newstream, newlen); *j2klen += newlen; free( newstream); if(j2kstream) free(j2kstream); return buf; } Byte_t * add_emptyboxstream( placeholder_param_t *phld, Byte_t *jp2stream, Byte8_t *jp2len) { Byte_t *newstream; Byte8_t newlen; Byte_t *buf; if( phld->OrigBHlen == 8) newlen = big4(phld->OrigBH); else newlen = big8(phld->OrigBH+8); newstream = (Byte_t *)malloc( newlen); memset( newstream, 0, newlen); memcpy( newstream, phld->OrigBH, phld->OrigBHlen); buf = (Byte_t *)malloc(( *jp2len)+newlen); memcpy( buf, jp2stream, *jp2len); memcpy( buf+(*jp2len), newstream, newlen); *jp2len += newlen; free( newstream); if(jp2stream) free(jp2stream); return buf; } Byte_t * add_emptytilestream( const Byte8_t tileID, Byte_t *j2kstream, Byte8_t *j2klen) { Byte_t *newstream; Byte8_t newlen; Byte_t *buf; newstream = gene_emptytilestream( tileID, &newlen); buf = (Byte_t *)malloc(( *j2klen)+newlen); memcpy( buf, j2kstream, *j2klen); memcpy( buf+(*j2klen), newstream, newlen); *j2klen += newlen; free( newstream); if(j2kstream) free(j2kstream); return buf; } Byte_t * add_padding( Byte8_t padding, Byte_t *j2kstream, Byte8_t *j2klen) { Byte_t *buf; buf = (Byte_t *)malloc(( *j2klen)+padding); memcpy( buf, j2kstream, *j2klen); memset( buf+(*j2klen), 0, padding); *j2klen += padding; if(j2kstream) free(j2kstream); return buf; } Byte_t * add_EOC( Byte_t *j2kstream, Byte8_t *j2klen) { Byte2_t EOC = 0xd9ff; Byte_t *buf; buf = (Byte_t *)malloc(( *j2klen)+2); memcpy( buf, j2kstream, *j2klen); memcpy( buf+(*j2klen), &EOC, 2); *j2klen += 2; if(j2kstream) free(j2kstream); return buf; } Byte_t * gene_msgstream( message_param_t *message, Byte_t *stream, Byte8_t *length) { Byte_t *buf; if( !message) return NULL; *length = message->length; buf = (Byte_t *)malloc( *length); memcpy( buf, stream+message->res_offset, *length); return buf; } Byte_t * gene_emptytilestream( const Byte8_t tileID, Byte8_t *length) { Byte_t *buf; const Byte2_t SOT = 0x90ff; const Byte2_t Lsot = 0xa << 8; Byte2_t Isot; const Byte4_t Psot = 0xe << 24; const Byte_t TPsot = 0, TNsot = 1; const Byte2_t SOD = 0x93ff; *length = 14; buf = (Byte_t *)malloc(*length); Isot = (((Byte2_t)tileID) << 8) | ((((Byte2_t)tileID) & 0xf0) >> 8); memcpy( buf, &SOT, 2); memcpy( buf+2, &Lsot, 2); memcpy( buf+4, &Isot, 2); memcpy( buf+6, &Psot, 4); memcpy( buf+10, &TPsot, 1); memcpy( buf+11, &TNsot, 1); memcpy( buf+12, &SOD, 2); return buf; }