2 * $Id: msgqueue_manager.c 53 2011-05-09 16:55:39Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, 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.
34 #include <sys/types.h>
39 #include "msgqueue_manager.h"
40 #include "metadata_manager.h"
41 #include "index_manager.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
52 msgqueue_param_t * gene_msgqueue( bool stateless, cachemodel_param_t *cachemodel)
54 msgqueue_param_t *msgqueue;
56 msgqueue = (msgqueue_param_t *)malloc( sizeof(msgqueue_param_t));
58 msgqueue->first = NULL;
59 msgqueue->last = NULL;
61 msgqueue->stateless = stateless;
62 msgqueue->cachemodel = cachemodel;
67 void delete_msgqueue( msgqueue_param_t **msgqueue)
69 message_param_t *ptr, *next;
74 ptr = (*msgqueue)->first;
81 if( (*msgqueue)->stateless && (*msgqueue)->cachemodel)
82 delete_cachemodel( &((*msgqueue)->cachemodel));
87 void print_msgqueue( msgqueue_param_t *msgqueue)
90 char *message_class[] = { "Precinct", "Ext-Prec", "TileHead", "non", "Tile", "Ext-Tile", "Main", "non", "Meta"};
95 fprintf( logstream, "message queue:\n");
96 ptr = msgqueue->first;
99 fprintf( logstream, "\t class_id: %lld %s\n", ptr->class_id, message_class[ptr->class_id]);
100 fprintf( logstream, "\t in_class_id: %lld\n", ptr->in_class_id );
101 fprintf( logstream, "\t csn: %lld\n", ptr->csn );
102 fprintf( logstream, "\t bin_offset: %#llx\n", ptr->bin_offset );
103 fprintf( logstream, "\t length: %#llx\n", ptr->length );
105 fprintf( logstream, "\t aux: %lld\n", ptr->aux );
106 fprintf( logstream, "\t last_byte: %d\n", ptr->last_byte );
108 print_placeholder( ptr->phld);
110 fprintf( logstream, "\t res_offset: %#llx\n", ptr->res_offset );
111 fprintf( logstream, "\n");
117 void enqueue_message( message_param_t *msg, msgqueue_param_t *msgqueue);
119 void enqueue_mainheader( msgqueue_param_t *msgqueue)
121 cachemodel_param_t *cachemodel;
122 target_param_t *target;
123 index_param_t *codeidx;
124 message_param_t *msg;
126 cachemodel = msgqueue->cachemodel;
127 target = cachemodel->target;
128 codeidx = target->codeidx;
130 msg = (message_param_t *)malloc( sizeof(message_param_t));
132 msg->last_byte = true;
133 msg->in_class_id = 0;
134 msg->class_id = MAINHEADER_MSG;
135 msg->csn = target->csn;
137 msg->length = codeidx->mhead_length;
138 msg->aux = 0; // non exist
139 msg->res_offset = codeidx->offset;
143 enqueue_message( msg, msgqueue);
145 cachemodel->mhead_model = true;
148 void enqueue_tileheader( int tile_id, msgqueue_param_t *msgqueue)
150 cachemodel_param_t *cachemodel;
151 target_param_t *target;
152 index_param_t *codeidx;
153 message_param_t *msg;
155 cachemodel = msgqueue->cachemodel;
156 target = cachemodel->target;
157 codeidx = target->codeidx;
159 if( !cachemodel->th_model[ tile_id]){
160 msg = (message_param_t *)malloc( sizeof(message_param_t));
161 msg->last_byte = true;
162 msg->in_class_id = tile_id;
163 msg->class_id = TILE_HEADER_MSG;
164 msg->csn = target->csn;
166 msg->length = codeidx->tileheader[tile_id]->tlen-2; // SOT marker segment is removed
167 msg->aux = 0; // non exist
168 msg->res_offset = codeidx->offset + get_elemOff( codeidx->tilepart, 0, tile_id) + 2; // skip SOT marker seg
172 enqueue_message( msg, msgqueue);
173 cachemodel->th_model[ tile_id] = true;
177 void enqueue_tile( int tile_id, int level, msgqueue_param_t *msgqueue)
179 cachemodel_param_t *cachemodel;
180 target_param_t *target;
182 Byte8_t numOftparts; // num of tile parts par tile
184 index_param_t *codeidx;
185 faixbox_param_t *tilepart;
186 message_param_t *msg;
187 Byte8_t binOffset, binLength, class_id;
190 cachemodel = msgqueue->cachemodel;
191 target = cachemodel->target;
192 codeidx = target->codeidx;
193 tilepart = codeidx->tilepart;
195 numOftparts = get_nmax( tilepart);
196 numOftiles = get_m( tilepart);
198 class_id = (numOftparts==1) ? TILE_MSG : EXT_TILE_MSG;
200 if( tile_id < 0 || numOftiles <= tile_id){
201 fprintf( FCGI_stderr, "Error, Invalid tile-id %d\n", tile_id);
205 tp_model = &cachemodel->tp_model[ tile_id*numOftparts];
208 for( i=0; i<numOftparts-level; i++){
209 binLength = get_elemLen( tilepart, i, tile_id);
212 msg = (message_param_t *)malloc( sizeof(message_param_t));
214 msg->last_byte = (i==numOftparts-1);
215 msg->in_class_id = tile_id;
216 msg->class_id = class_id;
217 msg->csn = target->csn;
218 msg->bin_offset = binOffset;
219 msg->length = binLength;
220 msg->aux = numOftparts-i;
221 msg->res_offset = codeidx->offset+get_elemOff( tilepart, i, tile_id)/*-1*/;
225 enqueue_message( msg, msgqueue);
229 binOffset += binLength;
233 void enqueue_precinct( int seq_id, int tile_id, int comp_id, int layers, msgqueue_param_t *msgqueue)
235 cachemodel_param_t *cachemodel;
236 index_param_t *codeidx;
237 faixbox_param_t *precpacket;
238 message_param_t *msg;
239 Byte8_t nmax, binOffset, binLength;
240 int layer_id, numOflayers;
242 cachemodel = msgqueue->cachemodel;
243 codeidx = cachemodel->target->codeidx;
244 precpacket = codeidx->precpacket[ comp_id];
245 numOflayers = codeidx->COD.numOflayers;
247 nmax = get_nmax(precpacket);
249 layers = numOflayers;
252 for( layer_id = 0; layer_id < layers; layer_id++){
254 binLength = get_elemLen( precpacket, seq_id*numOflayers+layer_id, tile_id);
256 if( !cachemodel->pp_model[comp_id][ tile_id*nmax+seq_id*numOflayers+layer_id]){
258 msg = (message_param_t *)malloc( sizeof(message_param_t));
259 msg->last_byte = (layer_id == (numOflayers-1));
260 msg->in_class_id = comp_precinct_id( tile_id, comp_id, seq_id, codeidx->SIZ.Csiz, codeidx->SIZ.XTnum * codeidx->SIZ.YTnum);
261 msg->class_id = PRECINCT_MSG;
262 msg->csn = cachemodel->target->csn;
263 msg->bin_offset = binOffset;
264 msg->length = binLength;
266 msg->res_offset = codeidx->offset+get_elemOff( precpacket, seq_id*numOflayers+layer_id, tile_id);
270 enqueue_message( msg, msgqueue);
272 cachemodel->pp_model[comp_id][ tile_id*nmax+seq_id*numOflayers+layer_id] = true;
274 binOffset += binLength;
278 Byte8_t comp_precinct_id( int t, int c, int s, int num_components, int num_tiles)
280 return t + (c + s * num_components ) * num_tiles;
283 void enqueue_box( int meta_id, boxlist_param_t *boxlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset);
284 void enqueue_phld( int meta_id, placeholderlist_param_t *phldlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset);
285 void enqueue_boxcontents( int meta_id, boxcontents_param_t *boxcontents, msgqueue_param_t *msgqueue, Byte8_t *binOffset);
287 void enqueue_metadata( int meta_id, msgqueue_param_t *msgqueue)
289 metadatalist_param_t *metadatalist;
290 metadata_param_t *metadata;
293 metadatalist = msgqueue->cachemodel->target->codeidx->metadatalist;
294 metadata = search_metadata( meta_id, metadatalist);
297 fprintf( FCGI_stderr, "Error: metadata-bin %d not found\n", meta_id);
302 if( metadata->boxlist)
303 enqueue_box( meta_id, metadata->boxlist, msgqueue, &binOffset);
305 if( metadata->placeholderlist)
306 enqueue_phld( meta_id, metadata->placeholderlist, msgqueue, &binOffset);
308 if( metadata->boxcontents)
309 enqueue_boxcontents( meta_id, metadata->boxcontents, msgqueue, &binOffset);
311 msgqueue->last->last_byte = true;
314 message_param_t * gene_metamsg( int meta_id, Byte8_t binoffset, Byte8_t length, Byte8_t res_offset, placeholder_param_t *phld, Byte8_t csn);
316 void enqueue_box( int meta_id, boxlist_param_t *boxlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset)
319 message_param_t *msg;
321 box = boxlist->first;
323 msg = gene_metamsg( meta_id, *binOffset, box->length, box->offset, NULL, msgqueue->cachemodel->target->csn);
324 enqueue_message( msg, msgqueue);
326 *binOffset += box->length;
331 void enqueue_phld( int meta_id, placeholderlist_param_t *phldlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset)
333 placeholder_param_t *phld;
334 message_param_t *msg;
336 phld = phldlist->first;
338 msg = gene_metamsg( meta_id, *binOffset, phld->LBox, 0, phld, msgqueue->cachemodel->target->csn);
339 enqueue_message( msg, msgqueue);
341 *binOffset += phld->LBox;
346 void enqueue_boxcontents( int meta_id, boxcontents_param_t *boxcontents, msgqueue_param_t *msgqueue, Byte8_t *binOffset)
348 message_param_t *msg;
350 msg = gene_metamsg( meta_id, *binOffset, boxcontents->length, boxcontents->offset, NULL, msgqueue->cachemodel->target->csn);
351 enqueue_message( msg, msgqueue);
353 *binOffset += boxcontents->length;
356 message_param_t * gene_metamsg( int meta_id, Byte8_t binOffset, Byte8_t length, Byte8_t res_offset, placeholder_param_t *phld, Byte8_t csn)
358 message_param_t *msg;
360 msg = (message_param_t *)malloc( sizeof(message_param_t));
362 msg->last_byte = false;
363 msg->in_class_id = meta_id;
364 msg->class_id = METADATA_MSG;
366 msg->bin_offset = binOffset;
367 msg->length = length;
368 msg->aux = 0; // non exist
369 msg->res_offset = res_offset;
376 void enqueue_message( message_param_t *msg, msgqueue_param_t *msgqueue)
379 msgqueue->last->next = msg;
381 msgqueue->first = msg;
383 msgqueue->last = msg;
387 void emit_bin_id_vbas( Byte_t bb, Byte_t c, Byte8_t in_class_id);
388 void emit_vbas( Byte8_t code);
389 void emit_body( message_param_t *msg, int fd);
390 void emit_placeholder( placeholder_param_t *phld);
392 void emit_stream_from_msgqueue( msgqueue_param_t *msgqueue)
394 message_param_t *msg;
395 Byte8_t class_id, csn;
401 msg = msgqueue->first;
405 if( msg->csn == csn){
406 if( msg->class_id == class_id)
410 class_id = msg->class_id;
415 class_id = msg->class_id;
419 c = msg->last_byte ? 1 : 0;
421 emit_bin_id_vbas( bb, c, msg->in_class_id);
424 emit_vbas( class_id);
428 emit_vbas( msg->bin_offset);
429 emit_vbas (msg->length);
431 if( msg->class_id%2) // Aux is present only if the id is odd
432 emit_vbas( msg->aux);
435 emit_placeholder( msg->phld);
437 emit_body( msg, msgqueue->cachemodel->target->fd);
443 void emit_vbas_with_bytelen( Byte8_t code, int bytelength);
444 void print_binarycode( Byte8_t n, int segmentlen);
446 void emit_bin_id_vbas( Byte_t bb, Byte_t c, Byte8_t in_class_id)
451 // A.2.3 In-class identifiers
452 // 7k-3bits, where k is the number of bytes in the VBAS
454 tmp = in_class_id >> 4;
460 in_class_id |= (((bb & 3) << 5) | (c & 1) << 4) << ((bytelength-1)*7);
462 emit_vbas_with_bytelen( in_class_id, bytelength);
465 void emit_vbas( Byte8_t code)
475 emit_vbas_with_bytelen( code, bytelength);
478 void emit_vbas_with_bytelen( Byte8_t code, int bytelength)
485 seg = ( code >> (n*7)) & 0x7f;
488 fputc(( Byte4_t)seg, FCGI_stdout);
493 void emit_body( message_param_t *msg, int fd)
497 if( lseek( fd, msg->res_offset, SEEK_SET)==-1){
498 fprintf( FCGI_stderr, "Error: fseek in emit_body()\n");
502 data = (Byte_t *)malloc( msg->length);
503 if( read( fd, data, msg->length) != msg->length){
505 fprintf( FCGI_stderr, "Error: fread in emit_body()\n");
509 if( fwrite( data, msg->length, 1, FCGI_stdout) < 1){
511 fprintf( FCGI_stderr, "Error: fwrite in emit_body()\n");
517 void emit_bigendian_bytes( Byte8_t code, int bytelength);
519 void emit_placeholder( placeholder_param_t *phld)
521 emit_bigendian_bytes( phld->LBox, 4);
522 if( fwrite( phld->TBox, 4, 1, FCGI_stdout) < 1){
523 fprintf( FCGI_stderr, "Error: fwrite in emit_placeholder()\n");
526 emit_bigendian_bytes( phld->Flags, 4);
527 emit_bigendian_bytes( phld->OrigID, 8);
529 if( fwrite( phld->OrigBH, phld->OrigBHlen, 1, FCGI_stdout) < 1){
530 fprintf( FCGI_stderr, "Error: fwrite in emit_placeholder()\n");
535 void emit_bigendian_bytes( Byte8_t code, int bytelength)
542 seg = ( code >> (n*8)) & 0xff;
543 fputc(( Byte4_t)seg, FCGI_stdout);
548 void print_binarycode( Byte8_t n, int segmentlen)
554 buf[i++] = n%2 ? '1' : '0';
557 for( j=segmentlen-1; j>=i; j--)
560 for( j=i-1, k=0; j>=0; j--, k++){
562 if( !((k+1)%segmentlen))
568 Byte_t * parse_bin_id_vbas( Byte_t *streamptr, Byte_t *bb, Byte_t *c, Byte8_t *in_class_id);
569 Byte_t * parse_vbas( Byte_t *streamptr, Byte8_t *elem);
571 void parse_JPIPstream( Byte_t *JPIPstream, Byte8_t streamlen, Byte8_t offset, msgqueue_param_t *msgqueue)
573 Byte_t *ptr; // stream pointer
574 message_param_t *msg;
576 Byte8_t class_id, csn;
578 class_id = -1; // dummy
581 while( ptr-JPIPstream < streamlen){
582 msg = (message_param_t *)malloc( sizeof(message_param_t));
584 ptr = parse_bin_id_vbas( ptr, &bb, &c, &msg->in_class_id);
586 msg->last_byte = c == 1 ? true : false;
589 ptr = parse_vbas( ptr, &class_id);
591 msg->class_id = class_id;
594 ptr = parse_vbas( ptr, &csn);
597 ptr = parse_vbas( ptr, &msg->bin_offset);
598 ptr = parse_vbas( ptr, &msg->length);
600 if( msg->class_id%2) // Aux is present only if the id is odd
601 ptr = parse_vbas( ptr, &msg->aux);
605 msg->res_offset = ptr-JPIPstream+offset;
610 msgqueue->last->next = msg;
612 msgqueue->first = msg;
613 msgqueue->last = msg;
619 void parse_metadata( metadata_param_t *metadata, message_param_t *msg, Byte_t *stream);
621 void parse_metamsg( msgqueue_param_t *msgqueue, Byte_t *stream, Byte8_t streamlen, metadatalist_param_t *metadatalist)
623 message_param_t *msg;
625 if( metadatalist == NULL)
628 msg = msgqueue->first;
630 if( msg->class_id == METADATA_MSG){
631 metadata_param_t *metadata = gene_metadata( msg->in_class_id, NULL, NULL, NULL);
632 insert_metadata_into_list( metadata, metadatalist);
633 parse_metadata( metadata, msg, stream+msg->res_offset);
639 placeholder_param_t * parse_phld( Byte_t *datastream, Byte8_t metalength);
641 void parse_metadata( metadata_param_t *metadata, message_param_t *msg, Byte_t *datastream)
643 char *boxtype = (char *)(datastream+4);
647 if( strncmp( boxtype, "phld", 4) == 0){
648 if( !metadata->placeholderlist)
649 metadata->placeholderlist = gene_placeholderlist();
651 placeholder_param_t *phld = parse_phld( datastream, msg->length);
653 insert_placeholder_into_list( phld, metadata->placeholderlist);
655 else if( isalpha(boxtype[0]) && isalpha(boxtype[1]) &&
656 (isalnum(boxtype[2])||isblank(boxtype[2])) &&
657 (isalpha(boxtype[3])||isblank(boxtype[3]))){
658 if( !metadata->boxlist)
659 metadata->boxlist = gene_boxlist();
661 box_param_t *box = gene_boxbyOffinStream( datastream, msg->res_offset);
662 insert_box_into_list( box, metadata->boxlist);
665 metadata->boxcontents = gene_boxcontents( msg->res_offset, msg->length);
668 placeholder_param_t * parse_phld( Byte_t *datastream, Byte8_t metalength)
670 placeholder_param_t *phld;
672 phld = (placeholder_param_t *)malloc( sizeof(placeholder_param_t));
674 phld->LBox = big4( datastream);
675 strcpy( phld->TBox, "phld");
676 phld->Flags = big4( datastream+8);
677 phld->OrigID = big8( datastream+12);
678 phld->OrigBHlen = metalength - 20;
679 phld->OrigBH = (Byte_t *)malloc(phld->OrigBHlen);
680 memcpy( phld->OrigBH, datastream+20, phld->OrigBHlen);
686 Byte_t * parse_bin_id_vbas( Byte_t *streamptr, Byte_t *bb, Byte_t *c, Byte8_t *in_class_id)
694 *bb = (code >> 5) & 3;
695 *c = (code >> 4) & 1;
697 *in_class_id = code & 15;
701 *in_class_id = (*in_class_id << 7) | (code & 0x7f);
706 Byte_t * parse_vbas( Byte_t *streamptr, Byte8_t *elem)
715 *elem = (*elem << 7) | (code & 0x7f);
721 void delete_message_in_msgqueue( message_param_t **msg, msgqueue_param_t *msgqueue)
723 message_param_t *ptr;
728 if( *msg == msgqueue->first)
729 msgqueue->first = (*msg)->next;
731 ptr = msgqueue->first;
732 while( ptr->next != *msg){
736 ptr->next = (*msg)->next;
738 if( *msg == msgqueue->last)
739 msgqueue->last = ptr;