Debian Codegraph @

converter/other/fiasco/codec/prediction.c

raw
1 /*
2 * prediction.c: Range image prediction with MC or ND
3 *
4 * Written by: Ullrich Hafner
5 * Michael Unger
6 *
7 * This file is part of FIASCO (Fractal Image And Sequence COdec)
8 * Copyright (C) 1994-2000 Ullrich Hafner
9 */
10
11 /*
12 * $Date: 2000/06/14 20:50:51 $
13 * $Author: hafner $
14 * $Revision: 5.1 $
15 * $State: Exp $
16 */
17
18 #include "config.h"
19
20 #include <string.h>
21
22 #include "types.h"
23 #include "macros.h"
24 #include "error.h"
25
26 #include "cwfa.h"
27 #include "ip.h"
28 #include "control.h"
29 #include "misc.h"
30 #include "subdivide.h"
31 #include "bintree.h"
32 #include "domain-pool.h"
33 #include "approx.h"
34 #include "wfalib.h"
35 #include "mwfa.h"
36 #include "prediction.h"
37
38 #include "decoder.h"
39
40
41 /*****************************************************************************
42
43 local variables
44
45 *****************************************************************************/
46
47 typedef struct state_data
48 {
49 real_t final_distribution;
50 byte_t level_of_state;
51 byte_t domain_type;
52
53 real_t *images_of_state;
54 real_t *inner_products;
55 real_t *ip_states_state [MAXLEVEL];
56
57 word_t tree [MAXLABELS];
58 mv_t mv_tree [MAXLABELS];
59 word_t y_state [MAXLABELS];
60 byte_t y_column [MAXLABELS];
61 byte_t prediction [MAXLABELS];
62
63 u_word_t x [MAXLABELS];
64 u_word_t y [MAXLABELS];
65
66 real_t weight [MAXLABELS][MAXEDGES + 1];
67 word_t int_weight [MAXLABELS][MAXEDGES + 1];
68 word_t into [MAXLABELS][MAXEDGES + 1];
69 } state_data_t;
70
71 /*****************************************************************************
72
73 prototypes
74
75 *****************************************************************************/
76
77 static real_t
78 nd_prediction (real_t max_costs, real_t price, unsigned band, int y_state,
79 range_t *range, wfa_t *wfa, coding_t *c);
80 static real_t
81 mc_prediction (real_t max_costs, real_t price, unsigned band, int y_state,
82 range_t *range, wfa_t *wfa, coding_t *c);
83 static state_data_t *
84 store_state_data (unsigned from, unsigned to, unsigned max_level,
85 wfa_t *wfa, coding_t *c);
86 static void
87 restore_state_data (unsigned from, unsigned to, unsigned max_level,
88 state_data_t *data, wfa_t *wfa, coding_t *c);
89
90 /*****************************************************************************
91
92 public code
93
94 *****************************************************************************/
95
96 real_t
97 predict_range (real_t max_costs, real_t price, range_t *range, wfa_t *wfa,
98 coding_t *c, unsigned band, int y_state, unsigned states,
99 const tree_t *tree_model, const tree_t *p_tree_model,
100 const void *domain_model, const void *d_domain_model,
101 const void *coeff_model, const void *d_coeff_model)
102 {
103 unsigned state; /* counter */
104 void *rec_domain_model; /* domain model after recursion */
105 void *rec_d_domain_model; /* p domain model after recursion */
106 void *rec_coeff_model; /* coeff model after recursion */
107 void *rec_d_coeff_model; /* d coeff model after recursion */
108 tree_t rec_tree_model; /* tree_model after '' */
109 tree_t rec_p_tree_model; /* p_tree_model after '' */
110 unsigned rec_states; /* wfa->states after '' */
111 real_t *rec_pixels; /* c->pixels after '' */
112 state_data_t *rec_state_data; /* state_data struct after '' */
113 real_t costs; /* current approximation costs */
114 unsigned level; /* counter */
115 state_data_t *sd; /* pointer to state_data field */
116
117 /*
118 * Store WFA data from state 'lc_states' to 'wfa->states' - 1 and
119 * current state of probability models.
120 */
121 rec_domain_model = c->domain_pool->model;
122 rec_d_domain_model = c->d_domain_pool->model;
123 rec_coeff_model = c->coeff->model;
124 rec_d_coeff_model = c->d_coeff->model;
125 rec_tree_model = c->tree;
126 rec_p_tree_model = c->p_tree;
127 rec_states = wfa->states;
128 rec_pixels = c->pixels;
129 rec_state_data = store_state_data (states, rec_states - 1,
130 c->options.lc_max_level, wfa, c);
131
132 /*
133 * Restore probability models to the state before the recursive subdivision
134 * has been started.
135 */
136 wfa->states = states;
137 c->tree = *tree_model;
138 c->p_tree = *p_tree_model;
139 c->domain_pool->model = c->domain_pool->model_duplicate (domain_model);
140 c->d_domain_pool->model = c->d_domain_pool->model_duplicate (d_domain_model);
141 c->coeff->model = c->coeff->model_duplicate (c->coeff, coeff_model);
142 c->d_coeff->model = c->d_coeff->model_duplicate (c->d_coeff,
143 d_coeff_model);
144
145 if (c->mt->frame_type == I_FRAME)
146 costs = nd_prediction (max_costs, price, band, y_state, range, wfa, c);
147 else
148 costs = mc_prediction (max_costs, price, band, y_state, range, wfa, c);
149
150 c->pixels = rec_pixels;
151
152 if (costs < MAXCOSTS)
153 {
154 /*
155 * Free the memory used by the state_data struct
156 */
157 for (state = states; state < rec_states; state++)
158 {
159 sd = &rec_state_data [state - states];
160 for (level = c->options.images_level + 1;
161 level <= c->options.lc_max_level; level++)
162 if (sd->ip_states_state [level] != NULL)
163 Free (sd->ip_states_state [level]);
164 if (sd->images_of_state != NULL)
165 Free (sd->images_of_state);
166 if (sd->inner_products != NULL)
167 Free (sd->inner_products);
168 }
169 if (states < rec_states)
170 Free (rec_state_data);
171 c->domain_pool->model_free (rec_domain_model);
172 c->d_domain_pool->model_free (rec_d_domain_model);
173 c->coeff->model_free (rec_coeff_model);
174 c->d_coeff->model_free (rec_d_coeff_model);
175
176 costs = (range->tree_bits + range->matrix_bits + range->weights_bits
177 + range->mv_tree_bits + range->mv_coord_bits
178 + range->nd_tree_bits + range->nd_weights_bits) * price
179 + range->err;
180 }
181 else
182 {
183 /*
184 * Restore WFA to state before function was called
185 */
186 c->domain_pool->model_free (c->domain_pool->model);
187 c->d_domain_pool->model_free (c->d_domain_pool->model);
188 c->coeff->model_free (c->coeff->model);
189 c->d_coeff->model_free (c->d_coeff->model);
190
191 c->domain_pool->model = rec_domain_model;
192 c->d_domain_pool->model = rec_d_domain_model;
193 c->coeff->model = rec_coeff_model;
194 c->d_coeff->model = rec_d_coeff_model;
195 c->tree = rec_tree_model;
196 c->p_tree = rec_p_tree_model;
197
198 range->prediction = NO;
199
200 if (wfa->states != states)
201 remove_states (states, wfa);
202 restore_state_data (states, rec_states - 1, c->options.lc_max_level,
203 rec_state_data, wfa, c);
204 costs = MAXCOSTS;
205 }
206
207 return costs;
208 }
209
210 void
211 clear_norms_table (unsigned level, const wfa_info_t *wi, motion_t *mt)
212 /*
213 * Clear norms arrays.
214 *
215 * No return value.
216 */
217 {
218 unsigned range_size = wi->half_pixel
219 ? square (wi->search_range)
220 : square (2 * wi->search_range);
221
222 if (level > wi->p_min_level)
223 {
224 memset (mt->mc_forward_norms [level], 0, range_size * sizeof(real_t));
225 memset (mt->mc_backward_norms [level], 0, range_size * sizeof(real_t));
226 }
227 }
228
229 void
230 update_norms_table (unsigned level, const wfa_info_t *wi, motion_t *mt)
231 /*
232 * Norms table of levels larger than the bottom level are computed
233 * by summing up previously calculated displacement costs of lower levels.
234 *
235 * No return value.
236 */
237 {
238 unsigned range_size = wi->half_pixel
239 ? square (wi->search_range)
240 : square (2 * wi->search_range);
241
242 if (level > wi->p_min_level)
243 {
244 unsigned index; /* index of motion vector */
245
246 for (index = 0; index < range_size; index++)
247 mt->mc_forward_norms [level][index]
248 += mt->mc_forward_norms [level - 1][index];
249 if (mt->frame_type == B_FRAME)
250 for (index = 0; index < range_size; index++)
251 mt->mc_backward_norms [level][index]
252 += mt->mc_backward_norms [level - 1][index];
253 }
254 }
255
256 /*****************************************************************************
257
258 private code
259
260 *****************************************************************************/
261
262 static real_t
263 mc_prediction (real_t max_costs, real_t price, unsigned band, int y_state,
264 range_t *range, wfa_t *wfa, coding_t *c)
265 {
266 real_t costs; /* current approximation costs */
267 range_t prange = *range;
268 unsigned width = width_of_level (range->level);
269 unsigned height = height_of_level (range->level);
270 word_t *mcpe = Calloc (width * height, sizeof (word_t));
271
272 /*
273 * If we are at the bottom level of the mc tree:
274 * Fill in the norms table
275 */
276 if (prange.level == wfa->wfainfo->p_min_level)
277 fill_norms_table (prange.x, prange.y, prange.level, wfa->wfainfo, c->mt);
278 /*
279 * Predict 'range' with motion compensation according to frame type.
280 * MCPE is returned in 'c->mcpe'
281 */
282 if (c->mt->frame_type == P_FRAME)
283 find_P_frame_mc (mcpe, price, &prange, wfa->wfainfo, c->mt);
284 else
285 find_B_frame_mc (mcpe, price, &prange, wfa->wfainfo, c->mt);
286
287 costs = (prange.mv_tree_bits + prange.mv_coord_bits) * price;
288
289 if (costs < max_costs) /* motion vector not too expensive */
290 {
291 unsigned last_state; /* last WFA state before recursion */
292 real_t *ipi [MAXSTATES]; /* inner products pointers */
293 unsigned state;
294 real_t mvt, mvc;
295
296 c->pixels = Calloc (width * height, sizeof (real_t));
297 cut_to_bintree (c->pixels, mcpe, width, height, 0, 0, width, height);
298
299 /*
300 * Approximate MCPE recursively.
301 */
302 last_state = wfa->states - 1;
303 for (state = 0; state <= last_state; state++)
304 if (need_image (state, wfa))
305 {
306 ipi [state] = c->ip_images_state[state];
307 c->ip_images_state[state]
308 = Calloc (size_of_tree (c->products_level), sizeof (real_t));
309 }
310
311 mvc = prange.mv_coord_bits;
312 mvt = prange.mv_tree_bits;
313
314 prange.image = 0;
315 prange.address = 0;
316 prange.tree_bits = 0;
317 prange.matrix_bits = 0;
318 prange.weights_bits = 0;
319 prange.mv_coord_bits = 0;
320 prange.mv_tree_bits = 0;
321 prange.nd_weights_bits = 0;
322 prange.nd_tree_bits = 0;
323
324 compute_ip_images_state (prange.image, prange.address, prange.level,
325 1, 0, wfa, c);
326 costs += subdivide (max_costs - costs, band, y_state, &prange,
327 wfa, c, NO, YES);
328
329 if (costs < max_costs) /* use motion compensation */
330 {
331 unsigned img, adr; /* temp. values */
332
333 img = range->image;
334 adr = range->address;
335 *range = prange;
336 range->image = img;
337 range->address = adr;
338 range->mv_coord_bits = mvc;
339 range->mv_tree_bits = mvt;
340 range->prediction = YES;
341
342 for (state = last_state + 1; state < wfa->states; state++)
343 if (need_image (state, wfa))
344 memset (c->ip_images_state [state], 0,
345 size_of_tree (c->products_level) * sizeof (real_t));
346
347 costs = (range->tree_bits + range->matrix_bits + range->weights_bits
348 + range->mv_tree_bits + range->mv_coord_bits
349 + range->nd_tree_bits + range->nd_weights_bits) * price
350 + range->err;
351 }
352 else
353 costs = MAXCOSTS;
354
355 for (state = 0; state <= last_state; state++)
356 if (need_image (state, wfa))
357 {
358 Free (c->ip_images_state[state]);
359 c->ip_images_state[state] = ipi [state];
360 }
361 Free (c->pixels);
362 }
363 else
364 costs = MAXCOSTS;
365
366 Free (mcpe);
367
368 return costs;
369 }
370
371 static real_t
372 nd_prediction (real_t max_costs, real_t price, unsigned band, int y_state,
373 range_t *range, wfa_t *wfa, coding_t *c)
374 {
375 real_t costs; /* current approximation costs */
376 range_t lrange = *range;
377
378 /*
379 * Predict 'range' with DC component approximation
380 */
381 {
382 real_t x = get_ip_image_state (range->image, range->address,
383 range->level, 0, c);
384 real_t y = get_ip_state_state (0, 0, range->level, c);
385 real_t w = btor (rtob (x / y, c->coeff->dc_rpf), c->coeff->dc_rpf);
386 word_t s [2] = {0, -1};
387
388 lrange.into [0] = 0;
389 lrange.into [1] = NO_EDGE;
390 lrange.weight [0] = w;
391 lrange.mv_coord_bits = 0;
392 lrange.mv_tree_bits = 0;
393 lrange.nd_tree_bits = tree_bits (LEAF, lrange.level, &c->p_tree);
394 lrange.nd_weights_bits = 0;
395 lrange.tree_bits = 0;
396 lrange.matrix_bits = 0;
397 lrange.weights_bits = c->coeff->bits (&w, s, range->level, c->coeff);
398 }
399 costs = price * (lrange.weights_bits + lrange.nd_tree_bits);
400
401 /*
402 * Recursive approximation of difference image
403 */
404 if (costs < max_costs)
405 {
406 unsigned state;
407 range_t rrange; /* range: recursive subdivision */
408 unsigned last_state; /* last WFA state before recursion */
409 real_t *ipi [MAXSTATES]; /* inner products pointers */
410 unsigned width = width_of_level (range->level);
411 unsigned height = height_of_level (range->level);
412 real_t *pixels;
413
414 /*
415 * Generate difference image original - approximation
416 */
417 {
418 unsigned n;
419 real_t *src, *dst; /* pointers to image data */
420 real_t w = - lrange.weight [0] * c->images_of_state [0][0];
421
422 src = c->pixels + range->address * size_of_level (range->level);
423 dst = c->pixels = pixels = Calloc (width * height, sizeof (real_t));
424
425 for (n = width * height; n; n--)
426 *dst++ = *src++ + w;
427 }
428
429 /*
430 * Approximate difference recursively.
431 */
432 rrange = *range;
433 rrange.tree_bits = 0;
434 rrange.matrix_bits = 0;
435 rrange.weights_bits = 0;
436 rrange.mv_coord_bits = 0;
437 rrange.mv_tree_bits = 0;
438 rrange.nd_tree_bits = 0;
439 rrange.nd_weights_bits = 0;
440 rrange.image = 0;
441 rrange.address = 0;
442
443 last_state = wfa->states - 1;
444 for (state = 0; state <= last_state; state++)
445 if (need_image (state, wfa))
446 {
447 ipi [state] = c->ip_images_state[state];
448 c->ip_images_state[state]
449 = Calloc (size_of_tree (c->products_level), sizeof (real_t));
450 }
451
452 compute_ip_images_state (rrange.image, rrange.address, rrange.level,
453 1, 0, wfa, c);
454
455 costs += subdivide (max_costs - costs, band, y_state, &rrange, wfa, c,
456 NO, YES);
457
458 Free (pixels);
459
460 if (costs < max_costs && ischild (rrange.tree)) /* use prediction */
461 {
462 unsigned img, adr;
463 unsigned edge;
464
465 img = range->image;
466 adr = range->address;
467 *range = rrange;
468 range->image = img;
469 range->address = adr;
470 range->nd_tree_bits += lrange.nd_tree_bits;
471 range->nd_weights_bits += lrange.weights_bits;
472
473 for (edge = 0; isedge (lrange.into [edge]); edge++)
474 {
475 range->into [edge] = lrange.into [edge];
476 range->weight [edge] = lrange.weight [edge];
477 }
478 range->into [edge] = NO_EDGE;
479 range->prediction = edge;
480
481 for (state = last_state + 1; state < wfa->states; state++)
482 if (need_image (state, wfa))
483 memset (c->ip_images_state [state], 0,
484 size_of_tree (c->products_level) * sizeof (real_t));
485 }
486 else
487 costs = MAXCOSTS;
488
489 for (state = 0; state <= last_state; state++)
490 if (need_image (state, wfa))
491 {
492 Free (c->ip_images_state [state]);
493 c->ip_images_state [state] = ipi [state];
494 }
495 }
496 else
497 costs = MAXCOSTS;
498
499 return costs;
500 }
501
502 static state_data_t *
503 store_state_data (unsigned from, unsigned to, unsigned max_level,
504 wfa_t *wfa, coding_t *c)
505 /*
506 * Save and remove all states starting from state 'from'.
507 *
508 * Return value:
509 * pointer to array of state_data structs
510 */
511 {
512 state_data_t *data; /* array of savestates */
513 state_data_t *sd; /* pointer to current savestates */
514 unsigned state, label, level;
515
516 if (to < from)
517 return NULL; /* nothing to do */
518
519 data = Calloc (to - from + 1, sizeof (state_data_t));
520
521 for (state = from; state <= to; state++)
522 {
523 sd = &data [state - from];
524
525 sd->final_distribution = wfa->final_distribution [state];
526 sd->level_of_state = wfa->level_of_state [state];
527 sd->domain_type = wfa->domain_type [state];
528 sd->images_of_state = c->images_of_state [state];
529 sd->inner_products = c->ip_images_state [state];
530
531 wfa->domain_type [state] = 0;
532 c->images_of_state [state] = NULL;
533 c->ip_images_state [state] = NULL;
534
535 for (label = 0; label < MAXLABELS; label++)
536 {
537 sd->tree [label] = wfa->tree [state][label];
538 sd->y_state [label] = wfa->y_state [state][label];
539 sd->y_column [label] = wfa->y_column [state][label];
540 sd->mv_tree [label] = wfa->mv_tree [state][label];
541 sd->x [label] = wfa->x [state][label];
542 sd->y [label] = wfa->y [state][label];
543 sd->prediction [label] = wfa->prediction [state][label];
544
545 memcpy (sd->weight [label], wfa->weight [state][label],
546 sizeof (real_t) * (MAXEDGES + 1));
547 memcpy (sd->int_weight [label], wfa->int_weight [state][label],
548 sizeof (word_t) * (MAXEDGES + 1));
549 memcpy (sd->into [label], wfa->into [state][label],
550 sizeof (word_t) * (MAXEDGES + 1));
551
552 wfa->into [state][label][0] = NO_EDGE;
553 wfa->tree [state][label] = RANGE;
554 wfa->y_state [state][label] = RANGE;
555 }
556 for (level = c->options.images_level + 1; level <= max_level;
557 level++)
558 {
559 sd->ip_states_state [level] = c->ip_states_state [state][level];
560 c->ip_states_state [state][level] = NULL;
561 }
562 }
563
564 return data;
565 }
566
567 static void
568 restore_state_data (unsigned from, unsigned to, unsigned max_level,
569 state_data_t *data, wfa_t *wfa, coding_t *c)
570 /*
571 * Restore all state data starting from state 'from'.
572 *
573 * No return value.
574 */
575 {
576 state_data_t *sd; /* pointer to state_data item */
577 unsigned state, label, level;
578
579 if (to < from)
580 return; /* nothing to do */
581
582 for (state = from; state <= to; state++)
583 {
584 sd = &data [state - from];
585
586 wfa->final_distribution [state] = sd->final_distribution;
587 wfa->level_of_state [state] = sd->level_of_state;
588 wfa->domain_type [state] = sd->domain_type;
589
590 if (c->images_of_state [state] != NULL)
591 Free (c->images_of_state [state]);
592 c->images_of_state [state] = sd->images_of_state;
593 if (c->ip_images_state [state] != NULL)
594 Free (c->ip_images_state [state]);
595 c->ip_images_state [state] = sd->inner_products;
596
597 for (label = 0; label < MAXLABELS; label++)
598 {
599 wfa->tree [state][label] = sd->tree [label];
600 wfa->y_state [state][label] = sd->y_state [label];
601 wfa->y_column [state][label] = sd->y_column [label];
602 wfa->mv_tree [state][label] = sd->mv_tree [label];
603 wfa->x [state][label] = sd->x [label];
604 wfa->y [state][label] = sd->y [label];
605 wfa->prediction [state][label] = sd->prediction [label];
606
607 memcpy (wfa->weight [state][label], sd->weight [label],
608 sizeof(real_t) * (MAXEDGES + 1));
609 memcpy (wfa->int_weight [state][label], sd->int_weight [label],
610 sizeof(word_t) * (MAXEDGES + 1));
611 memcpy (wfa->into [state][label], sd->into [label],
612 sizeof(word_t) * (MAXEDGES + 1));
613 }
614 for (level = c->options.images_level + 1; level <= max_level;
615 level++)
616 {
617 if (c->ip_states_state [state][level] != NULL)
618 Free (c->ip_states_state [state][level]);
619 c->ip_states_state [state][level] = sd->ip_states_state [level];
620 }
621 }
622
623 Free (data);
624 wfa->states = to + 1;
625 }