[trunk] updated copyright and added copyright notice required by ISO, in each file...
[openjpeg.git] / src / lib / openjpip / cachemodel_manager.c
1 /*
2  * $Id$
3  *
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  * 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 #include "opj_inttypes.h"
36
37 #ifdef SERVER
38 #include "fcgi_stdio.h"
39 #define logstream FCGI_stdout
40 #else
41 #define FCGI_stdout stdout
42 #define FCGI_stderr stderr
43 #define logstream stderr
44 #endif /*SERVER*/
45
46
47 cachemodellist_param_t * gene_cachemodellist(void)
48 {
49   cachemodellist_param_t *cachemodellist;
50
51   cachemodellist = (cachemodellist_param_t *)opj_malloc( sizeof(cachemodellist_param_t));
52   
53   cachemodellist->first = NULL;
54   cachemodellist->last  = NULL;
55
56   return cachemodellist;
57 }
58
59 cachemodel_param_t * gene_cachemodel( cachemodellist_param_t *cachemodellist, target_param_t *target, OPJ_BOOL reqJPP)
60 {
61   cachemodel_param_t *cachemodel;
62   faixbox_param_t *tilepart;
63   faixbox_param_t *precpacket;
64   size_t numOfelem;
65   Byte8_t numOftiles;
66   int i;
67
68   cachemodel = (cachemodel_param_t *)opj_malloc( sizeof(cachemodel_param_t));
69
70   refer_target( target, &cachemodel->target);
71   
72   if( reqJPP){
73     if( target->jppstream)
74       cachemodel->jppstream = OPJ_TRUE;
75     else
76       cachemodel->jppstream = OPJ_FALSE;
77   } else{ /* reqJPT */
78     if( target->jptstream)
79       cachemodel->jppstream = OPJ_FALSE;
80     else
81       cachemodel->jppstream = OPJ_TRUE;
82   }
83
84   cachemodel->mhead_model = OPJ_FALSE;
85   
86   tilepart = target->codeidx->tilepart;
87   numOftiles = get_m( tilepart);
88   numOfelem = get_nmax( tilepart)*numOftiles;
89   cachemodel->tp_model = (OPJ_BOOL *)opj_calloc( 1, numOfelem*sizeof(OPJ_BOOL));
90   cachemodel->th_model = (OPJ_BOOL *)opj_calloc( 1, numOftiles*sizeof(OPJ_BOOL));
91   cachemodel->pp_model = (OPJ_BOOL **)opj_malloc( target->codeidx->SIZ.Csiz*sizeof(OPJ_BOOL *));
92   for( i=0; i<target->codeidx->SIZ.Csiz; i++){
93     precpacket = target->codeidx->precpacket[i];
94     cachemodel->pp_model[i] = (OPJ_BOOL *)opj_calloc( 1, get_nmax(precpacket)*get_m(precpacket)*sizeof(OPJ_BOOL));
95   }
96   cachemodel->next = NULL;
97   
98   if( cachemodellist){
99     if( cachemodellist->first) /* there are one or more entries */
100       cachemodellist->last->next = cachemodel;
101     else                   /* first entry */
102       cachemodellist->first = cachemodel;
103     cachemodellist->last = cachemodel;
104   }
105
106 #ifndef SERVER
107   fprintf( logstream, "local log: cachemodel generated\n");
108 #endif
109
110   return cachemodel; 
111 }
112
113 void print_cachemodel( cachemodel_param_t cachemodel)
114 {
115   target_param_t *target;
116   Byte8_t TPnum; /* num of tile parts in each tile */
117   Byte8_t Pmax; /* max num of packets per tile */
118   Byte8_t i, j, k;
119   int n; /* FIXME: Is this large enough ? */
120
121   target = cachemodel.target;
122   
123   fprintf( logstream, "target: %s\n", target->targetname);
124   fprintf( logstream, "\t main header model: %d\n", cachemodel.mhead_model);
125
126   fprintf( logstream, "\t tile part model:\n");
127   TPnum = get_nmax( target->codeidx->tilepart);
128
129   for( i=0, n=0; i<target->codeidx->SIZ.YTnum; i++){
130     for( j=0; j<target->codeidx->SIZ.XTnum; j++){
131       for( k=0; k<TPnum; k++)
132         fprintf( logstream, "%d", cachemodel.tp_model[n++]);
133       fprintf( logstream, " ");
134     }
135     fprintf( logstream, "\n");
136   }
137
138   fprintf( logstream, "\t tile header and precinct packet model:\n");
139   for( i=0; i<target->codeidx->SIZ.XTnum*target->codeidx->SIZ.YTnum; i++){
140     fprintf( logstream, "\t  tile.%" PRIu64 "  %d\n", i, cachemodel.th_model[i]);
141     for( j=0; j<target->codeidx->SIZ.Csiz; j++){
142       fprintf( logstream, "\t   compo.%" PRIu64 ": ", j);
143       Pmax = get_nmax( target->codeidx->precpacket[j]);
144       for( k=0; k<Pmax; k++)
145         fprintf( logstream, "%d", cachemodel.pp_model[j][i*Pmax+k]);
146       fprintf( logstream, "\n");
147     }
148   }
149 }
150
151 cachemodel_param_t * search_cachemodel( target_param_t *target, cachemodellist_param_t *cachemodellist)
152 {
153   cachemodel_param_t *foundcachemodel;
154
155   foundcachemodel = cachemodellist->first;
156   
157   while( foundcachemodel != NULL){
158     
159     if( foundcachemodel->target == target)
160       return foundcachemodel;
161       
162     foundcachemodel = foundcachemodel->next;
163   }
164   return NULL;
165 }
166
167 void delete_cachemodellist( cachemodellist_param_t **cachemodellist)
168 {  
169   cachemodel_param_t *cachemodelPtr, *cachemodelNext;
170
171   cachemodelPtr = (*cachemodellist)->first;
172   while( cachemodelPtr != NULL){
173     cachemodelNext=cachemodelPtr->next;
174     delete_cachemodel( &cachemodelPtr);
175     cachemodelPtr=cachemodelNext;
176   }
177   opj_free(*cachemodellist);
178 }
179
180 void delete_cachemodel( cachemodel_param_t **cachemodel)
181 {
182   int i;
183
184   unrefer_target( (*cachemodel)->target);
185   
186   opj_free( (*cachemodel)->tp_model);
187   opj_free( (*cachemodel)->th_model);
188   
189   for( i=0; i<(*cachemodel)->target->codeidx->SIZ.Csiz; i++)
190     opj_free( (*cachemodel)->pp_model[i]);
191   opj_free( (*cachemodel)->pp_model);
192
193 #ifndef SERVER
194   fprintf( logstream, "local log: cachemodel deleted\n");
195 #endif
196   opj_free( *cachemodel);
197 }
198
199 OPJ_BOOL is_allsent( cachemodel_param_t cachemodel)
200 {
201   target_param_t *target;
202   Byte8_t TPnum; /* num of tile parts in each tile */
203   Byte8_t Pmax; /* max num of packets per tile */
204   Byte8_t i, j, k;
205   int n; /* FIXME: is this large enough ? */
206
207   target = cachemodel.target;
208   
209   if( !cachemodel.mhead_model)
210     return OPJ_FALSE;
211
212   TPnum = get_nmax( target->codeidx->tilepart);
213
214   if( cachemodel.jppstream){
215     for( i=0; i<target->codeidx->SIZ.XTnum*target->codeidx->SIZ.YTnum; i++){
216       if( !cachemodel.th_model[i])
217         return OPJ_FALSE;
218       
219       for( j=0; j<target->codeidx->SIZ.Csiz; j++){
220         Pmax = get_nmax( target->codeidx->precpacket[j]);
221         for( k=0; k<Pmax; k++)
222           if( !cachemodel.pp_model[j][i*Pmax+k])
223             return OPJ_FALSE;
224       }
225     }
226     return OPJ_TRUE;
227   }
228   else{
229     for( i=0, n=0; i<target->codeidx->SIZ.YTnum; i++)
230       for( j=0; j<target->codeidx->SIZ.XTnum; j++)
231         for( k=0; k<TPnum; k++)
232           if( !cachemodel.tp_model[n++])
233             return OPJ_FALSE;
234     return OPJ_TRUE;
235   }
236 }