Debian Codegraph @

converter/other/fiasco/input/matrices.c

raw
1 /*
2 * matrices.c: Input of transition matrices
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/06/14 20:50:13 $
12 * $Author: hafner $
13 * $Revision: 5.1 $
14 * $State: Exp $
15 */
16
17 #include "config.h"
18
19 #include "types.h"
20 #include "macros.h"
21 #include "error.h"
22
23 #include "bit-io.h"
24 #include "arith.h"
25 #include "misc.h"
26 #include "wfalib.h"
27
28 #include "matrices.h"
29
30 #if STDC_HEADERS
31 # include <stdlib.h>
32 #endif /* not STDC_HEADERS */
33
34 /*****************************************************************************
35
36 prototypes
37
38 *****************************************************************************/
39
40 static unsigned
41 delta_decoding (wfa_t *wfa, unsigned last_domain, bitfile_t *input);
42 static unsigned
43 column_0_decoding (wfa_t *wfa, unsigned last_row, bitfile_t *input);
44 static unsigned
45 chroma_decoding (wfa_t *wfa, bitfile_t *input);
46 static void
47 compute_y_state (int state, int y_state, wfa_t *wfa);
48
49 /*****************************************************************************
50
51 public code
52
53 *****************************************************************************/
54
55 unsigned
56 read_matrices (wfa_t *wfa, bitfile_t *input)
57 /*
58 * Read transitions of WFA given from the stream 'input'.
59 *
60 * Return value:
61 * number of edges
62 *
63 * Side effects:
64 * 'wfa->into' is filled with decoded values
65 */
66 {
67 unsigned total; /* total number of edges in the WFA */
68 unsigned root_state = wfa->wfainfo->color
69 ? wfa->tree [wfa->tree [wfa->root_state][0]][0]
70 : wfa->root_state;
71
72 total = column_0_decoding (wfa, root_state, input);
73 total += delta_decoding (wfa, root_state, input);
74 if (wfa->wfainfo->color)
75 total += chroma_decoding (wfa, input);
76
77 return total;
78 }
79
80 /*****************************************************************************
81
82 private code
83
84 *****************************************************************************/
85
86 static unsigned
87 delta_decoding (wfa_t *wfa, unsigned last_domain, bitfile_t *input)
88 /*
89 * Read transition matrices which are encoded with delta coding
90 * from stream 'input'.
91 * 'last_domain' is the maximum state number used as domain image.
92 *
93 * Return value:
94 * number of non-zero matrix elements (WFA edges)
95 *
96 * Side effects:
97 * 'wfa->into' is filled with decoded values
98 */
99 {
100 range_sort_t rs; /* ranges are sorted as in the coder */
101 unsigned max_domain; /* dummy used for recursion */
102 unsigned range;
103 unsigned count [MAXEDGES + 1];
104 unsigned state, label;
105 unsigned *n_edges; /* number of elements per row */
106 unsigned total = 0; /* total number of decoded edges */
107
108 /*
109 * Generate a list of range blocks.
110 * The order is the same as in the coder.
111 */
112 rs.range_state = Calloc ((last_domain + 1) * MAXLABELS,
113 sizeof (u_word_t));
114 rs.range_label = Calloc ((last_domain + 1) * MAXLABELS,
115 sizeof (byte_t));
116 rs.range_max_domain = Calloc ((last_domain + 1) * MAXLABELS,
117 sizeof (u_word_t));
118 rs.range_subdivided = Calloc ((last_domain + 1) * MAXLABELS,
119 sizeof (bool_t));
120 rs.range_no = 0;
121 max_domain = wfa->basis_states - 1;
122 sort_ranges (last_domain, &max_domain, &rs, wfa);
123
124 /*
125 * Get row statistics
126 */
127 {
128 arith_t *decoder;
129 model_t *elements;
130 unsigned max_edges = read_rice_code (3, input);
131
132 /*
133 * Get the probability array of the number of edges distribution
134 * and allocate the corresponding model.
135 */
136 {
137 unsigned edge;
138
139 for (edge = 0; edge <= max_edges; edge++)
140 count [edge] = read_rice_code ((int) log2 (last_domain) - 2,
141 input);
142 elements = alloc_model (max_edges + 1, 0, 0, count);
143 }
144
145 /*
146 * Get number of elements per matrix row
147 */
148 {
149 unsigned row;
150
151 n_edges = Calloc (wfa->states, sizeof (unsigned));
152 decoder = alloc_decoder (input);
153 for (row = range = 0; range < rs.range_no; range++)
154 if (!rs.range_subdivided [range])
155 {
156 state = rs.range_state [range];
157 label = rs.range_label [range];
158
159 n_edges [row++]
160 = decode_symbol (decoder, elements)
161 - (isedge (wfa->into [state][label][0]) ? 1 : 0);
162 }
163
164 free_decoder (decoder);
165 free_model (elements);
166 }
167 }
168
169 /*
170 * Get matrix elements
171 */
172 {
173 unsigned row;
174 u_word_t *mapping1 = Calloc (wfa->states, sizeof (word_t));
175 u_word_t *mapping_coder1 = Calloc (wfa->states, sizeof (word_t));
176 u_word_t *mapping2 = Calloc (wfa->states, sizeof (word_t));
177 u_word_t *mapping_coder2 = Calloc (wfa->states, sizeof (word_t));
178 bool_t use_normal_domains = get_bit (input);
179 bool_t use_delta_domains = get_bit (input);
180
181 /*
182 * Generate array of states which are admitted domains.
183 * When coding intra frames 'mapping1' == 'mapping2' otherwise
184 * 'mapping1' is a list of 'normal' domains which are admitted for
185 * coding intra blocks
186 * 'mapping2' is a list of 'delta' domains which are admitted for
187 * coding the motion compensated prediction error
188 */
189 {
190 unsigned n1, n2, state;
191
192 for (n1 = n2 = state = 0; state < wfa->states; state++)
193 {
194 mapping1 [n1] = state;
195 mapping_coder1 [state] = n1;
196 if (usedomain (state, wfa)
197 && (state < wfa->basis_states
198 || use_delta_domains || !wfa->delta_state [state]))
199 n1++;
200
201 mapping2 [n2] = state;
202 mapping_coder2 [state] = n2;
203 if (usedomain (state, wfa)
204 && (state < wfa->basis_states || use_normal_domains
205 || wfa->delta_state [state]))
206 n2++;
207 }
208 }
209
210 for (row = 0, range = 0; range < rs.range_no; range++)
211 if (!rs.range_subdivided [range])
212 {
213 u_word_t *mapping;
214 u_word_t *mapping_coder;
215 unsigned max_value;
216 unsigned edge;
217 unsigned state = rs.range_state [range];
218 unsigned label = rs.range_label [range];
219 unsigned last = 1;
220
221 if (wfa->delta_state [state] ||
222 wfa->mv_tree [state][label].type != NONE)
223 {
224 mapping = mapping2;
225 mapping_coder = mapping_coder2;
226 }
227 else
228 {
229 mapping = mapping1;
230 mapping_coder = mapping_coder1;
231 }
232 max_value = mapping_coder [rs.range_max_domain [range]];
233 for (edge = n_edges [row]; edge; edge--)
234 {
235 unsigned domain;
236
237 if (max_value - last)
238 domain = read_bin_code (max_value - last, input) + last;
239 else
240 domain = max_value;
241 append_edge (state, mapping [domain], -1, label, wfa);
242 last = domain + 1;
243 total++;
244 }
245 row++;
246 }
247 Free (mapping1);
248 Free (mapping_coder1);
249 Free (mapping2);
250 Free (mapping_coder2);
251 }
252
253 Free (n_edges);
254 Free (rs.range_state);
255 Free (rs.range_label);
256 Free (rs.range_max_domain);
257 Free (rs.range_subdivided);
258
259 return total;
260 }
261
262 static unsigned
263 column_0_decoding (wfa_t *wfa, unsigned last_row, bitfile_t *input)
264 /*
265 * Read column 0 of the transition matrices of the 'wfa' which are coded
266 * with quasi arithmetic coding from stream 'input'.
267 * All rows from 'wfa->basis_states' up to 'last_row' are decoded.
268 *
269 * Return value:
270 * number of non-zero matrix elements (WFA edges)
271 *
272 * Side effects:
273 * 'wfa->into' is filled with decoded values
274 */
275 {
276 unsigned row; /* current matrix row */
277 unsigned total = 0; /* total number of edges in col 0 */
278 unsigned *prob_ptr; /* pointer to current probability */
279 unsigned *last; /* pointer to minimum probability */
280 unsigned *first; /* pointer to maximum probability */
281 unsigned *new_prob_ptr; /* ptr to probability of last domain */
282 unsigned *prob; /* probability array */
283 u_word_t high; /* Start of the current code range */
284 u_word_t low; /* End of the current code range */
285 u_word_t code; /* The present input code value */
286 word_t *is_leaf; /* pointer to the tree structure */
287
288 /*
289 * Compute the asymmetric probability array
290 * prob[] = { 1/2, 1/2, 1/4, 1/4, 1/4, 1/4,
291 * 1/8, ... , 1/16, ..., 1/(MAXPROB+1)}
292 */
293 {
294 unsigned n;
295 unsigned index; /* probability index */
296 unsigned exp; /* current exponent */
297
298 prob = Calloc (1 << (MAX_PROB + 1), sizeof (unsigned));
299
300 for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
301 for (exp = 0; exp < 1U << n; exp++, index++)
302 prob [index] = n;
303 }
304
305 first = prob_ptr = new_prob_ptr = prob;
306 last = first + 1020;
307
308 is_leaf = wfa->tree [wfa->basis_states]; /* use pointer arithmetics ... */
309
310 high = HIGH; /* 1.0 */
311 low = LOW; /* 0.0 */
312 code = get_bits (input, 16);
313
314 /*
315 * Decode column 0 with a quasi arithmetic coder (QAC).
316 * Advantage of this QAC with respect to a binary AC:
317 * Instead of using time consuming multiplications and divisions
318 * to compute the probability of the most probable symbol (MPS) and
319 * the range of the interval, a table look up procedure linked
320 * with a shift operation is used for both computations.
321 *
322 * Loops and array accesses have been removed
323 * to make real time decoding possible.
324 */
325 for (row = wfa->basis_states; row <= last_row; row++)
326 {
327 unsigned count; /* value in the current interval */
328
329 /*
330 * Read label 0 element
331 */
332 if (isrange (*is_leaf++)) /* valid matrix index */
333 {
334 count = high - ((high - low) >> *prob_ptr);
335 if (code < count)
336 {
337 if (prob_ptr < last) /* model update */
338 prob_ptr++;
339 /*
340 * Decode the MPS '0'
341 */
342 high = count - 1;
343
344 RESCALE_INPUT_INTERVAL;
345 }
346 else
347 {
348 prob_ptr = ((prob_ptr - first) >> 1) + first; /* model update */
349 /*
350 * Decode the LPS '1'
351 */
352 low = count;
353
354 RESCALE_INPUT_INTERVAL;
355 /*
356 * Restore the transition (weight = -1)
357 */
358 append_edge (row, 0, -1, 0, wfa);
359 total++;
360 }
361 }
362 /*
363 * Read label 1 element
364 */
365 if (isrange (*is_leaf++)) /* valid matrix index */
366 {
367 count = high - ((high - low) >> *prob_ptr);
368 if (code < count)
369 {
370 if (prob_ptr < last)
371 prob_ptr++; /* model update */
372 /*
373 * Decode the MPS '0'
374 */
375 high = count - 1;
376
377 RESCALE_INPUT_INTERVAL;
378 }
379 else
380 {
381 prob_ptr = ((prob_ptr - first) >> 1) + first; /* model update */
382 /*
383 * Decode the LPS '1'
384 */
385 low = count;
386
387 RESCALE_INPUT_INTERVAL;
388 /*
389 * Restore the transition (weight = -1)
390 */
391 append_edge (row, 0, -1, 1, wfa);
392 total++;
393 }
394 }
395 }
396
397 INPUT_BYTE_ALIGN (input);
398
399 Free (prob);
400
401 return total;
402 }
403
404 static unsigned
405 chroma_decoding (wfa_t *wfa, bitfile_t *input)
406 /*
407 * Read transition matrices of 'wfa' states which are part of the
408 * chroma channels Cb and Cr from stream 'input'.
409 *
410 * Return value:
411 * number of non-zero matrix elements (WFA edges)
412 *
413 * Side effects:
414 * 'wfa->into' is filled with decoded values
415 */
416 {
417 unsigned domain; /* current domain, counter */
418 unsigned total = 0; /* total number of chroma edges */
419 unsigned *prob_ptr; /* pointer to current probability */
420 unsigned *last; /* pointer to minimum probability */
421 unsigned *first; /* pointer to maximum probability */
422 unsigned *new_prob_ptr; /* ptr to probability of last domain */
423 unsigned *prob; /* probability array */
424 u_word_t high; /* Start of the current code range */
425 u_word_t low; /* End of the current code range */
426 u_word_t code; /* The present input code value */
427 word_t *y_domains; /* domain images corresponding to Y */
428 int save_index; /* YES: store current probability */
429
430 /*
431 * Compute the asymmetric probability array
432 * prob[] = { 1/2, 1/2, 1/4, 1/4, 1/4, 1/4,
433 * 1/8, ... , 1/16, ..., 1/(MAXPROB+1)}
434 */
435 {
436 unsigned n;
437 unsigned index; /* probability index */
438 unsigned exp; /* current exponent */
439
440 prob = Calloc (1 << (MAX_PROB + 1), sizeof (unsigned));
441
442 for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
443 for (exp = 0; exp < 1U << n; exp++, index++)
444 prob [index] = n;
445 }
446
447 high = HIGH; /* 1.0 */
448 low = LOW; /* 0.0 */
449 code = get_bits (input, 16);
450
451 /*
452 * Compute list of admitted domains
453 */
454 y_domains = compute_hits (wfa->basis_states,
455 wfa->tree [wfa->tree [wfa->root_state][0]][0],
456 wfa->wfainfo->chroma_max_states, wfa);
457
458 first = prob_ptr = new_prob_ptr = prob;
459 last = first + 1020;
460
461 /*
462 * First of all, read all matrix columns given in the list 'y_domains'
463 * which note all admitted domains.
464 * These matrix elements are stored with QAC (see column_0_decoding ()).
465 */
466 for (domain = 0; y_domains [domain] != -1; domain++)
467 {
468 unsigned row = wfa->tree [wfa->tree [wfa->root_state][0]][0] + 1;
469 word_t *is_leaf = wfa->tree [row];
470
471 prob_ptr = new_prob_ptr;
472 save_index = YES;
473
474 for (; row < wfa->states; row++)
475 {
476 unsigned count; /* value in the current interval */
477 /*
478 * Read label 0 element
479 */
480 if (isrange (*is_leaf++)) /* valid matrix index */
481 {
482 count = high - ((high - low) >> *prob_ptr);
483 if (code < count)
484 {
485 if (prob_ptr < last)
486 prob_ptr++;
487 /*
488 * Decode the MPS '0'
489 */
490 high = count - 1;
491
492 RESCALE_INPUT_INTERVAL;
493 }
494 else
495 {
496 prob_ptr = ((prob_ptr - first) >> 1) + first;
497 /*
498 * Decode the LPS '1'
499 */
500 low = count;
501
502 RESCALE_INPUT_INTERVAL;
503 /*
504 * Restore the transition (weight = -1)
505 */
506 append_edge (row, y_domains [domain], -1, 0, wfa);
507 total++;
508 }
509 }
510 /*
511 * Read label 1 element
512 */
513 if (isrange (*is_leaf++)) /* valid matrix index */
514 {
515 count = high - ((high - low) >> *prob_ptr);
516 if (code < count)
517 {
518 if (prob_ptr < last)
519 prob_ptr++;
520 /*
521 * Decode the MPS '0'
522 */
523 high = count - 1;
524
525 RESCALE_INPUT_INTERVAL;
526 }
527 else
528 {
529 prob_ptr = ((prob_ptr - first) >> 1) + first;
530 /*
531 * Decode the LPS '1'
532 */
533 low = count;
534
535 RESCALE_INPUT_INTERVAL;
536 /*
537 * Restore the transition (weight = -1)
538 */
539 append_edge (row, y_domains [domain], -1, 1, wfa);
540 total++;
541 }
542 }
543 if (save_index)
544 {
545 save_index = NO;
546 new_prob_ptr = prob_ptr;
547 }
548 }
549 }
550
551 Free (y_domains);
552
553 compute_y_state (wfa->tree [wfa->tree [wfa->root_state][0]][1],
554 wfa->tree [wfa->tree [wfa->root_state][0]][0], wfa);
555 compute_y_state (wfa->tree [wfa->tree [wfa->root_state][1]][0],
556 wfa->tree [wfa->tree [wfa->root_state][0]][0], wfa);
557
558 first = prob_ptr = new_prob_ptr = prob;
559
560 /*
561 * Decode the additional column which indicates whether there
562 * are transitions to a state with same spatial coordinates
563 * in the Y component.
564 *
565 * Again, quasi arithmetic decoding is used for this task.
566 */
567 {
568 unsigned row;
569
570 for (row = wfa->tree [wfa->tree [wfa->root_state][0]][0] + 1;
571 row < wfa->states; row++)
572 {
573 int label; /* current label */
574
575 for (label = 0; label < MAXLABELS; label++)
576 {
577 u_word_t count = high - ((high - low) >> *prob_ptr);
578
579 if (code < count)
580 {
581 if (prob_ptr < last)
582 prob_ptr++;
583 /*
584 * Decode the MPS '0'
585 */
586 high = count - 1;
587
588 RESCALE_INPUT_INTERVAL;
589 }
590 else
591 {
592 prob_ptr = ((prob_ptr - first) >> 1) + first;
593 /*
594 * Decode the LPS '1'
595 */
596 low = count;
597
598 RESCALE_INPUT_INTERVAL;
599 /*
600 * Restore the transition (weight = -1)
601 */
602 append_edge (row, wfa->y_state [row][label], -1, label, wfa);
603 total++;
604 }
605 }
606 }
607 }
608
609 INPUT_BYTE_ALIGN (input);
610
611 Free (prob);
612
613 return total;
614 }
615
616 static void
617 compute_y_state (int state, int y_state, wfa_t *wfa)
618 /*
619 * Compute the 'wfa->y_state' array which denotes those states of
620 * the Y band that have the same spatial coordinates as the corresponding
621 * states of the Cb and Cr bands.
622 * The current root of the Y tree is given by 'y_state'.
623 * The current root of the tree of the chroma channel is given by 'state'.
624 *
625 * No return value.
626 *
627 * Side effects:
628 * 'wfa->y_state' is filled with the generated tree structure.
629 */
630 {
631 unsigned label;
632
633 for (label = 0; label < MAXLABELS; label++)
634 if (isrange (y_state))
635 wfa->y_state [state][label] = RANGE;
636 else
637 {
638 wfa->y_state [state][label] = wfa->tree [y_state][label];
639 if (!isrange (wfa->tree [state][label]))
640 compute_y_state (wfa->tree [state][label],
641 wfa->y_state [state][label], wfa);
642 }
643
644 }
645
646