Fix a warning reported on the continuous dashboard for linux.
[openjpeg.git] / applications / jpip / libopenjpip / cachemodel_manager.c
1 /*
2  * $Id$
3  *
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  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "cachemodel_manager.h"
34 #include "faixbox_manager.h"
35
36 #ifdef SERVER
37 #include "fcgi_stdio.h"
38 #define logstream FCGI_stdout
39 #else
40 #define FCGI_stdout stdout
41 #define FCGI_stderr stderr
42 #define logstream stderr
43 #endif //SERVER
44
45
46 cachemodellist_param_t * gene_cachemodellist()
47 {
48   cachemodellist_param_t *cachemodellist;
49
50   cachemodellist = (cachemodellist_param_t *)malloc( sizeof(cachemodellist_param_t));
51   
52   cachemodellist->first = NULL;
53   cachemodellist->last  = NULL;
54
55   return cachemodellist;
56 }
57
58 cachemodel_param_t * gene_cachemodel( cachemodellist_param_t *cachemodellist, target_param_t *target, bool reqJPP)
59 {
60   cachemodel_param_t *cachemodel;
61   faixbox_param_t *tilepart;
62   faixbox_param_t *precpacket;
63   size_t numOfelem;
64   Byte8_t numOftiles;
65   int i;
66
67   cachemodel = (cachemodel_param_t *)malloc( sizeof(cachemodel_param_t));
68
69   refer_target( target, &cachemodel->target);
70   
71   if( reqJPP){
72     if( target->jppstream)
73       cachemodel->jppstream = true;
74     else
75       cachemodel->jppstream = false;
76   } else{ // reqJPT
77     if( target->jptstream)
78       cachemodel->jppstream = false;
79     else
80       cachemodel->jppstream = true;
81   }
82
83   cachemodel->mhead_model = false;
84   
85   tilepart = target->codeidx->tilepart;
86   numOftiles = get_m( tilepart);
87   numOfelem = get_nmax( tilepart)*numOftiles;
88   cachemodel->tp_model = (bool *)calloc( 1, numOfelem*sizeof(bool));
89   cachemodel->th_model = (bool *)calloc( 1, numOftiles*sizeof(bool));
90   cachemodel->pp_model = (bool **)malloc( target->codeidx->SIZ.Csiz*sizeof(bool *));
91   for( i=0; i<target->codeidx->SIZ.Csiz; i++){
92     precpacket = target->codeidx->precpacket[i];
93     cachemodel->pp_model[i] = (bool *)calloc( 1, get_nmax(precpacket)*get_m(precpacket)*sizeof(bool));
94   }
95   cachemodel->next = NULL;
96   
97   if( cachemodellist){
98     if( cachemodellist->first) // there are one or more entries
99       cachemodellist->last->next = cachemodel;
100     else                   // first entry
101       cachemodellist->first = cachemodel;
102     cachemodellist->last = cachemodel;
103   }
104
105 #ifndef SERVER
106   fprintf( logstream, "local log: cachemodel generated\n");
107 #endif
108
109   return cachemodel; 
110 }
111
112 void print_cachemodel( cachemodel_param_t cachemodel)
113 {
114   target_param_t *target;
115   Byte8_t TPnum; // num of tile parts in each tile
116   Byte8_t Pmax; // max num of packets per tile
117   int i, j, k, n;
118
119   target = cachemodel.target;
120   
121   fprintf( logstream, "target: %s\n", target->targetname);
122   fprintf( logstream, "\t main header model: %d\n", cachemodel.mhead_model);
123
124   fprintf( logstream, "\t tile part model:\n");
125   TPnum = get_nmax( target->codeidx->tilepart);
126
127   for( i=0, n=0; i<target->codeidx->SIZ.YTnum; i++){
128     for( j=0; j<target->codeidx->SIZ.XTnum; j++){
129       for( k=0; k<TPnum; k++)
130         fprintf( logstream, "%d", cachemodel.tp_model[n++]);
131       fprintf( logstream, " ");
132     }
133     fprintf( logstream, "\n");
134   }
135
136   fprintf( logstream, "\t tile header and precinct packet model:\n");
137   for( i=0; i<target->codeidx->SIZ.XTnum*target->codeidx->SIZ.YTnum; i++){
138     fprintf( logstream, "\t  tile.%d  %d\n", i, cachemodel.th_model[i]);
139     for( j=0; j<target->codeidx->SIZ.Csiz; j++){
140       fprintf( logstream, "\t   compo.%d: ", j);
141       Pmax = get_nmax( target->codeidx->precpacket[j]);
142       for( k=0; k<Pmax; k++)
143         fprintf( logstream, "%d", cachemodel.pp_model[j][i*Pmax+k]);
144       fprintf( logstream, "\n");
145     }
146   }
147 }
148
149 cachemodel_param_t * search_cachemodel( target_param_t *target, cachemodellist_param_t *cachemodellist)
150 {
151   cachemodel_param_t *foundcachemodel;
152
153   foundcachemodel = cachemodellist->first;
154   
155   while( foundcachemodel != NULL){
156     
157     if( foundcachemodel->target == target)
158       return foundcachemodel;
159       
160     foundcachemodel = foundcachemodel->next;
161   }
162   return NULL;
163 }
164
165 void delete_cachemodellist( cachemodellist_param_t **cachemodellist)
166 {  
167   cachemodel_param_t *cachemodelPtr, *cachemodelNext;
168
169   cachemodelPtr = (*cachemodellist)->first;
170   while( cachemodelPtr != NULL){
171     cachemodelNext=cachemodelPtr->next;
172     delete_cachemodel( &cachemodelPtr);
173     cachemodelPtr=cachemodelNext;
174   }
175   free(*cachemodellist);
176 }
177
178 void delete_cachemodel( cachemodel_param_t **cachemodel)
179 {
180   int i;
181
182   unrefer_target( (*cachemodel)->target);
183   
184   free( (*cachemodel)->tp_model);
185   free( (*cachemodel)->th_model);
186   
187   for( i=0; i<(*cachemodel)->target->codeidx->SIZ.Csiz; i++)
188     free( (*cachemodel)->pp_model[i]);
189   free( (*cachemodel)->pp_model);
190
191 #ifndef SERVER
192   fprintf( logstream, "local log: cachemodel deleted\n");
193 #endif
194   free( *cachemodel);
195 }
196
197 bool is_allsent( cachemodel_param_t cachemodel)
198 {
199   target_param_t *target;
200   Byte8_t TPnum; // num of tile parts in each tile
201   Byte8_t Pmax; // max num of packets per tile
202   int i, j, k, n;
203
204   target = cachemodel.target;
205   
206   if( !cachemodel.mhead_model)
207     return false;
208
209   TPnum = get_nmax( target->codeidx->tilepart);
210
211   if( cachemodel.jppstream){
212     for( i=0; i<target->codeidx->SIZ.XTnum*target->codeidx->SIZ.YTnum; i++){
213       if( !cachemodel.th_model[i])
214         return false;
215       
216       for( j=0; j<target->codeidx->SIZ.Csiz; j++){
217         Pmax = get_nmax( target->codeidx->precpacket[j]);
218         for( k=0; k<Pmax; k++)
219           if( !cachemodel.pp_model[j][i*Pmax+k])
220             return false;
221       }
222     }
223     return true;
224   }
225   else{
226     for( i=0, n=0; i<target->codeidx->SIZ.YTnum; i++)
227       for( j=0; j<target->codeidx->SIZ.XTnum; j++)
228         for( k=0; k<TPnum; k++)
229           if( !cachemodel.tp_model[n++])
230             return false;
231     return true;
232   }
233 }