enabled JPP-stream
[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)
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   cachemodel->mhead_model = false;
71   
72   tilepart = target->codeidx->tilepart;
73   numOftiles = get_m( tilepart);
74   numOfelem = get_nmax( tilepart)*numOftiles;
75   cachemodel->tp_model = (bool *)calloc( 1, numOfelem*sizeof(bool));
76   cachemodel->th_model = (bool *)calloc( 1, numOftiles*sizeof(bool));
77   cachemodel->pp_model = (bool **)malloc( target->codeidx->SIZ.Csiz*sizeof(bool *));
78   for( i=0; i<target->codeidx->SIZ.Csiz; i++){
79     precpacket = target->codeidx->precpacket[i];
80     cachemodel->pp_model[i] = (bool *)calloc( 1, get_nmax(precpacket)*get_m(precpacket)*sizeof(bool));
81   }
82   cachemodel->next = NULL;
83   
84   if( cachemodellist){
85     if( cachemodellist->first) // there are one or more entries
86       cachemodellist->last->next = cachemodel;
87     else                   // first entry
88       cachemodellist->first = cachemodel;
89     cachemodellist->last = cachemodel;
90   }
91
92 #ifndef SERVER
93   fprintf( logstream, "local log: cachemodel generated\n");
94 #endif
95
96   return cachemodel; 
97 }
98
99 void print_cachemodel( cachemodel_param_t cachemodel)
100 {
101   target_param_t *target;
102   Byte8_t TPnum; // num of tile parts in each tile
103   Byte8_t Pmax; // max num of packets per tile
104   int i, j, k, n;
105
106   target = cachemodel.target;
107   
108   fprintf( logstream, "target: %s\n", target->filename);
109   fprintf( logstream, "\t main header model: %d\n", cachemodel.mhead_model);
110
111   fprintf( logstream, "\t tile part model:\n");
112   TPnum = get_nmax( target->codeidx->tilepart);
113
114   for( i=0, n=0; i<target->codeidx->SIZ.YTnum; i++){
115     for( j=0; j<target->codeidx->SIZ.XTnum; j++){
116       for( k=0; k<TPnum; k++)
117         fprintf( logstream, "%d", cachemodel.tp_model[n++]);
118       fprintf( logstream, " ");
119     }
120     fprintf( logstream, "\n");
121   }
122
123   fprintf( logstream, "\t tile header and precinct packet model:\n");
124   for( i=0; i<target->codeidx->SIZ.XTnum*target->codeidx->SIZ.YTnum; i++){
125     fprintf( logstream, "\t  tile.%d  %d\n", i, cachemodel.th_model[i]);
126     for( j=0; j<target->codeidx->SIZ.Csiz; j++){
127       fprintf( logstream, "\t   compo.%d: ", j);
128       Pmax = get_nmax( target->codeidx->precpacket[j]);
129       for( k=0; k<Pmax; k++)
130         fprintf( logstream, "%d", cachemodel.pp_model[j][i*Pmax+k]);
131       fprintf( logstream, "\n");
132     }
133   }
134 }
135
136 cachemodel_param_t * search_cachemodel( target_param_t *target, cachemodellist_param_t *cachemodellist)
137 {
138   cachemodel_param_t *foundcachemodel;
139
140   foundcachemodel = cachemodellist->first;
141   
142   while( foundcachemodel != NULL){
143     
144     if( foundcachemodel->target == target)
145       return foundcachemodel;
146       
147     foundcachemodel = foundcachemodel->next;
148   }
149   return NULL;
150 }
151
152 void delete_cachemodellist( cachemodellist_param_t **cachemodellist)
153 {  
154   cachemodel_param_t *cachemodelPtr, *cachemodelNext;
155
156   cachemodelPtr = (*cachemodellist)->first;
157   while( cachemodelPtr != NULL){
158     cachemodelNext=cachemodelPtr->next;
159     delete_cachemodel( &cachemodelPtr);
160     cachemodelPtr=cachemodelNext;
161   }
162   free(*cachemodellist);
163 }
164
165 void delete_cachemodel( cachemodel_param_t **cachemodel)
166 {
167   int i;
168
169   unrefer_target( (*cachemodel)->target);
170   
171   free( (*cachemodel)->tp_model);
172   free( (*cachemodel)->th_model);
173   
174   if( (*cachemodel)->target->codeidx->SIZ.Csiz > 1)
175     for( i=0; i<(*cachemodel)->target->codeidx->SIZ.Csiz; i++)
176       free( (*cachemodel)->pp_model[i]);
177   free( (*cachemodel)->pp_model);
178
179 #ifndef SERVER
180   fprintf( logstream, "local log: cachemodel deleted\n");
181 #endif
182   free( *cachemodel);
183 }