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