Debian Codegraph @

converter/other/fiasco/input/read.c

raw
1 /*
2 * read.c: Input of WFA files
3 *
4 * Written by: Ullrich Hafner
5 *
6 * This file is part of FIASCO (Fractal Image And Sequence COdec)
7 * Copyright (C) 1994-2000 Ullrich Hafner
8 */
9
10 /*
11 * $Date: 2000/07/18 15:44:58 $
12 * $Author: hafner $
13 * $Revision: 5.4 $
14 * $State: Exp $
15 */
16
17 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
18 #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */
19 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
20
21 #include "config.h"
22
23 #include <stdio.h>
24
25 #include <string.h>
26
27 #include "nstring.h"
28
29 #include "types.h"
30 #include "macros.h"
31 #include "error.h"
32
33 #include "wfa.h"
34 #include "misc.h"
35 #include "rpf.h"
36 #include "bit-io.h"
37 #include "wfalib.h"
38
39 #include "tree.h"
40 #include "matrices.h"
41 #include "weights.h"
42 #include "nd.h"
43 #include "mc.h"
44 #include "basis.h"
45 #include "read.h"
46
47 /*****************************************************************************
48
49 prototypes
50
51 *****************************************************************************/
52
53 static void
54 read_tiling (tiling_t *tiling, unsigned image_width, unsigned image_height,
55 unsigned image_level, bitfile_t *input);
56
57 /*****************************************************************************
58
59 public code
60
61 *****************************************************************************/
62
63 bitfile_t *
64 open_wfa (const char *filename, wfa_info_t *wi)
65 /*
66 * Open WFA file 'filename' and read header information.
67 *
68 * Return value:
69 * Pointer to input stream (fileposition: first WFA frame)
70 *
71 * Side effects:
72 * The values of the header of 'filename' are copied to 'wfainfo'.
73 *
74 */
75 {
76 bitfile_t *input; /* pointer to WFA bitfile */
77
78 assert (filename && wi);
79
80 wi->wfa_name = strdup (filename);
81
82 /*
83 * Check whether 'filename' is a regular WFA file
84 */
85 {
86 unsigned n;
87 const char *str;
88
89 if (!(input = open_bitfile (filename, "FIASCO_DATA", READ_ACCESS)))
90 file_error (filename);
91
92 for (str = FIASCO_MAGIC, n = strlen (FIASCO_MAGIC); n; n--)
93 if (get_bits (input, 8) != (unsigned) *str++)
94 error ("Input file %s is not a valid FIASCO file!", filename);
95 get_bits (input, 8); /* fetch newline */
96 }
97
98 /*
99 * Read WFA header information
100 */
101 {
102 char basis_name [MAXSTRLEN]; /* temp. buffer */
103 const unsigned rice_k = 8; /* parameter of Rice Code */
104 char *str = basis_name;
105
106 while ((*str++ = get_bits (input, 8)) != 0
107 && str < basis_name + MAXSTRLEN)
108 ;
109 if (str == basis_name + MAXSTRLEN)
110 error ("Input file %s is not a valid FIASCO file!", filename);
111
112 {
113 wi->release = read_rice_code (rice_k, input);
114
115 if (wi->release > FIASCO_BINFILE_RELEASE)
116 error ("Can't decode FIASCO files of file format release `%d'."
117 "\nCurrent file format release is `%d'.", wi->release,
118 FIASCO_BINFILE_RELEASE);
119 }
120
121 if (wi->release > 1)
122 {
123 header_type_e type;
124
125 while ((type = read_rice_code (rice_k, input)) != HEADER_END)
126 {
127 char buffer [MAXSTRLEN];
128 unsigned n = 0;
129
130 switch (type)
131 {
132 case HEADER_TITLE:
133 while ((buffer [n++] = get_bits (input, 8)))
134 ;
135 wi->title = strdup (buffer);
136 break;
137 case HEADER_COMMENT:
138 while ((buffer [n++] = get_bits (input, 8)))
139 ;
140 wi->comment = strdup (buffer);
141 break;
142 default: /* should not happen */
143 break;
144 }
145 }
146 }
147
148 wi->basis_name = strdup (basis_name);
149 wi->max_states = read_rice_code (rice_k, input);
150 wi->color = get_bit (input) ? YES : NO;
151 wi->width = read_rice_code (rice_k, input);
152 wi->height = read_rice_code (rice_k, input);
153
154 /*
155 * Compute bintree level
156 */
157 {
158 unsigned lx = log2 (wi->width - 1) + 1;
159 unsigned ly = log2 (wi->height - 1) + 1;
160
161 wi->level = MAX(lx, ly) * 2 - ((ly == lx + 1) ? 1 : 0);
162 }
163 wi->chroma_max_states = wi->color ? read_rice_code (rice_k, input) : -1;
164 wi->p_min_level = read_rice_code (rice_k, input);
165 wi->p_max_level = read_rice_code (rice_k, input);
166 wi->frames = read_rice_code (rice_k, input);
167 wi->smoothing = read_rice_code (rice_k, input);
168
169 /*
170 * Read RPF models from disk
171 */
172 {
173 unsigned mantissa;
174 fiasco_rpf_range_e range;
175
176 mantissa = get_bits (input, 3) + 2;
177 range = get_bits (input, 2);
178 wi->rpf = alloc_rpf (mantissa, range);
179
180 if (get_bit (input)) /* different DC model */
181 {
182 mantissa = get_bits (input, 3) + 2;
183 range = get_bits (input, 2);
184 wi->dc_rpf = alloc_rpf (mantissa, range);
185 }
186 else /* use same model for DC coefficients */
187 wi->dc_rpf = alloc_rpf (wi->rpf->mantissa_bits,
188 wi->rpf->range_e);
189
190 if (get_bit (input)) /* different delta model */
191 {
192 mantissa = get_bits (input, 3) + 2;
193 range = get_bits (input, 2);
194 wi->d_rpf = alloc_rpf (mantissa, range);
195 }
196 else
197 wi->d_rpf = alloc_rpf (wi->rpf->mantissa_bits,
198 wi->rpf->range_e);
199
200 if (get_bit (input)) /* different DC delta model */
201 {
202 mantissa = get_bits (input, 3) + 2;
203 range = get_bits (input, 2);
204 wi->d_dc_rpf = alloc_rpf (mantissa, range);
205 }
206 else
207 wi->d_dc_rpf = alloc_rpf (wi->dc_rpf->mantissa_bits,
208 wi->dc_rpf->range_e);
209 }
210
211 if (wi->frames > 1) /* motion compensation stuff */
212 {
213 wi->fps = read_rice_code (rice_k, input);
214 wi->search_range = read_rice_code (rice_k, input);
215 wi->half_pixel = get_bit (input) ? YES : NO;
216 wi->B_as_past_ref = get_bit (input) ? YES : NO;
217 }
218 }
219
220 INPUT_BYTE_ALIGN (input);
221
222 return input;
223 }
224
225 void
226 read_basis (const char *filename, wfa_t *wfa)
227 /*
228 * Read WFA initial basis 'filename' and fill 'wfa' struct.
229 *
230 * No return value.
231 *
232 * Side effects:
233 * wfa->into, wfa->weights, wfa->final_distribution, wfa->basis_states
234 * wfa->domain_type wfa->wfainfo->basis_name, are filled with the
235 * values of the WFA basis.
236 */
237 {
238 FILE *input; /* ASCII WFA initial basis file */
239
240 assert (filename && wfa);
241
242 if (!wfa->wfainfo->basis_name ||
243 !streq (wfa->wfainfo->basis_name, filename))
244 {
245 if (wfa->wfainfo->basis_name)
246 Free (wfa->wfainfo->basis_name);
247 wfa->wfainfo->basis_name = strdup (filename);
248 }
249
250 if (get_linked_basis (filename, wfa))
251 return; /* basis is linked with executable */
252
253 /*
254 * Check whether 'wfa_name' is a regular ASCII WFA initial basis file
255 */
256 {
257 char magic [MAXSTRLEN]; /* WFA magic number */
258
259 if (!(input = open_file (filename, "FIASCO_DATA", READ_ACCESS)))
260 file_error(filename);
261
262 if (fscanf (input, MAXSTRLEN_SCANF, magic) != 1)
263 error ("Format error: ASCII FIASCO initial basis file %s", filename);
264 else if (!streq (FIASCO_BASIS_MAGIC, magic))
265 error ("Input file %s is not an ASCII FIASCO initial basis!",
266 filename);
267 }
268
269 /*
270 * WFA ASCII format:
271 *
272 * Note: State 0 is assumed to be the constant function f(x, y) = 128.
273 * Don't define any transitions of state 0 in an initial basis.
274 *
275 * Header:
276 * type |description
277 * ----------------+-----------
278 * string |MAGIC Number "Wfa"
279 * int |Number of basis states 'N'
280 * bool_t-array[N] |use vector in linear combinations,
281 * |0: don't use vector (auxiliary state)
282 * |1: use vector in linear combinations
283 * float-array[N] |final distribution of every state
284 *
285 * Transitions:
286 *
287 * <state 1> current state
288 * <label> <into> <weight> transition 1 of current state
289 * <label> <into> <weight> transition 2 of current state
290 * ...
291 * <-1> last transition marker
292 * <state 2>
293 * ...
294 * <-1> last transition marker
295 * <state N>
296 * ...
297 *
298 * <-1> last transition marker
299 * <-1> last state marker
300 */
301 {
302 unsigned state;
303
304 if (fscanf (input ,"%u", &wfa->basis_states) != 1)
305 error ("Format error: ASCII FIASCO initial basis file %s", filename);
306
307 /*
308 * State 0 is assumed to be the constant function f(x, y) = 128.
309 */
310 wfa->domain_type [0] = USE_DOMAIN_MASK;
311 wfa->final_distribution [0] = 128;
312 wfa->states = wfa->basis_states;
313 wfa->basis_states++;
314
315 append_edge (0, 0, 1.0, 0, wfa);
316 append_edge (0, 0, 1.0, 1, wfa);
317
318 for (state = 1; state < wfa->basis_states; state++)
319 wfa->domain_type [state]
320 = read_int (input) ? USE_DOMAIN_MASK : AUXILIARY_MASK;
321
322 for (state = 1; state < wfa->basis_states; state++)
323 wfa->final_distribution[state] = read_real (input);
324
325 /*
326 * Read transitions
327 */
328 for (state = 1; state < wfa->basis_states; state++)
329 {
330 unsigned domain;
331 int label;
332 real_t weight;
333
334 if (read_int (input) != (int) state)
335 error ("Format error: ASCII FIASCO initial basis file %s",
336 filename);
337
338 while((label = read_int (input)) != -1)
339 {
340 domain = read_int (input);
341 weight = read_real (input);
342 append_edge (state, domain, weight, label, wfa);
343 }
344 }
345 }
346
347 fclose (input);
348 }
349
350 unsigned
351 read_next_wfa (wfa_t *wfa, bitfile_t *input)
352 /*
353 * Read next WFA frame of the WFA stream 'input'.
354 * WFA header information has to be already present in the 'wfainfo' struct.
355 * (i.e. open_wfa must be called first!)
356 *
357 * No return value.
358 *
359 * Side effects:
360 * wfa->into, wfa->weights, wfa->final_distribution, wfa->states
361 * wfa->x, wfa->y, wfa->level_of_state, wfa->domain_type
362 * mt->type, mt->number are filled with the values of the WFA file.
363 */
364 {
365 tiling_t tiling; /* tiling information */
366 unsigned frame_number; /* current frame number */
367
368 assert (wfa && input);
369
370 /*
371 * Frame header information
372 */
373 {
374 const unsigned rice_k = 8; /* parameter of Rice Code */
375
376 wfa->states = read_rice_code (rice_k, input);
377 wfa->frame_type = read_rice_code (rice_k, input);
378 frame_number = read_rice_code (rice_k, input);
379 }
380
381 if (wfa->wfainfo->release > 1) /* no alignment in version 1 */
382 {
383 INPUT_BYTE_ALIGN (input);
384 }
385
386 /*
387 * Read image tiling info
388 */
389 if (get_bit (input)) /* tiling performed ? */
390 read_tiling (&tiling, wfa->wfainfo->width, wfa->wfainfo->height,
391 wfa->wfainfo->level, input);
392 else
393 tiling.exponent = 0;
394
395 INPUT_BYTE_ALIGN (input);
396
397 read_tree (wfa, &tiling, input);
398
399 /*
400 * Compute domain pool.
401 * Large images have not been used because of image tiling.
402 */
403 {
404 unsigned state;
405
406 for (state = wfa->basis_states; state < wfa->states; state++)
407 if ((!wfa->wfainfo->color
408 || (int) state <= wfa->tree [wfa->tree [wfa->root_state][0]][0])
409 &&
410 (!tiling.exponent ||
411 wfa->level_of_state [state] <= (wfa->wfainfo->level
412 - tiling.exponent))
413 && ((wfa->x [state][0]
414 + width_of_level (wfa->level_of_state [state]))
415 <= wfa->wfainfo->width)
416 && ((wfa->y [state][0]
417 + height_of_level (wfa->level_of_state [state]))
418 <= wfa->wfainfo->height))
419 wfa->domain_type [state] = USE_DOMAIN_MASK;
420 else
421 wfa->domain_type [state] = 0;
422 }
423
424 if (tiling.exponent)
425 Free (tiling.vorder);
426
427 if (get_bit (input)) /* nondeterministic prediction used */
428 read_nd (wfa, input);
429
430 if (wfa->frame_type != I_FRAME) /* motion compensation used */
431 read_mc (wfa->frame_type, wfa, input);
432
433 locate_delta_images (wfa);
434
435 /*
436 * Read linear combinations (coefficients and indices)
437 */
438 {
439 unsigned edges = read_matrices (wfa, input);
440
441 if (edges)
442 read_weights (edges, wfa, input);
443 }
444
445 /*
446 * Compute final distribution of all states
447 */
448 {
449 unsigned state;
450
451 for (state = wfa->basis_states; state <= wfa->states; state++)
452 wfa->final_distribution[state]
453 = compute_final_distribution (state, wfa);
454 }
455
456 return frame_number;
457 }
458
459 /*****************************************************************************
460
461 private code
462
463 *****************************************************************************/
464
465 static void
466 read_tiling (tiling_t *tiling, unsigned image_width, unsigned image_height,
467 unsigned image_level, bitfile_t *input)
468 /*
469 * Read image tiling information from the given file 'input'
470 * and store parameters in struct 'tiling'.
471 *
472 * No return value.
473 */
474 {
475 const unsigned rice_k = 8; /* parameter of Rice Code */
476
477 tiling->exponent = read_rice_code (rice_k, input);
478
479 if (get_bit (input)) /* variance order */
480 {
481 unsigned tile; /* current image tile */
482 unsigned x0, y0; /* NW corner of image tile */
483 unsigned width, height; /* size of image tile */
484
485 tiling->vorder = Calloc (1 << tiling->exponent, sizeof (int));
486 for (tile = 0; tile < 1U << tiling->exponent; tile++)
487 {
488 locate_subimage (image_level, image_level - tiling->exponent, tile,
489 &x0, &y0, &width, &height);
490 if (x0 < image_width && y0 < image_height)
491 tiling->vorder [tile] = get_bits (input, tiling->exponent);
492 else
493 tiling->vorder [tile] = -1;
494 }
495 }
496 else /* spiral order */
497 {
498 tiling->vorder = Calloc (1 << tiling->exponent, sizeof (int));
499 compute_spiral (tiling->vorder, image_width, image_height,
500 tiling->exponent, get_bit (input) ? YES : NO);
501 }
502 }
503
504
505