Debian Codegraph @

converter/other/fiasco/output/matrices.c

raw
1 /*
2 * matrices.c: Output of transitions 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 /* NETPBM: When you call delta_encoding() with last_domain < 4, it
11 crashes. And we have seen it happen.
12 */
13
14 /*
15 * $Date: 2000/06/14 20:50:31 $
16 * $Author: hafner $
17 * $Revision: 5.1 $
18 * $State: Exp $
19 */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24
25 #include "pm_c_util.h"
26
27 #include "types.h"
28 #include "macros.h"
29 #include "error.h"
30
31 #include "wfa.h"
32 #include "bit-io.h"
33 #include "arith.h"
34 #include "misc.h"
35 #include "wfalib.h"
36
37 #include "matrices.h"
38
39 /*****************************************************************************
40
41 prototypes
42
43 *****************************************************************************/
44
45 static unsigned
46 delta_encoding (bool_t use_normal_domains, bool_t use_delta_domains,
47 const wfa_t *wfa, unsigned last_domain, bitfile_t *output);
48 static unsigned
49 column_0_encoding (const wfa_t *wfa, unsigned last_row, bitfile_t *output);
50 static unsigned
51 chroma_encoding (const wfa_t *wfa, bitfile_t *output);
52
53 /*****************************************************************************
54
55 public code
56
57 *****************************************************************************/
58
59 unsigned
60 write_matrices (bool_t use_normal_domains, bool_t use_delta_domains,
61 const wfa_t *wfa, bitfile_t *output)
62 /*
63 * Write transition matrices of 'wfa' to stream 'output'.
64 *
65 * Return value:
66 * number of transitions encoded
67 */
68 {
69 unsigned root_state; /* root of luminance */
70 unsigned total = 0; /* number of transitions */
71
72 root_state = wfa->wfainfo->color
73 ? wfa->tree [wfa->tree [wfa->root_state][0]][0]
74 : wfa->root_state;
75
76 total = column_0_encoding (wfa, root_state, output);
77
78 total += delta_encoding (use_normal_domains, use_delta_domains,
79 wfa, root_state, output);
80
81 if (wfa->wfainfo->color)
82 total += chroma_encoding (wfa, output);
83
84 return total;
85 }
86
87 /*****************************************************************************
88
89 private code
90
91 *****************************************************************************/
92
93 static unsigned
94 delta_encoding (bool_t use_normal_domains, bool_t use_delta_domains,
95 const wfa_t *wfa, unsigned last_domain, bitfile_t *output)
96 /*
97 * Write transition matrices with delta coding to stream 'input'.
98 * 'last_domain' is the maximum state number used as domain image.
99 *
100 * Return value:
101 * number of non-zero matrix elements (WFA edges)
102 */
103 {
104 range_sort_t rs; /* ranges are sorted as in the coder */
105 unsigned max_domain; /* dummy used for recursion */
106 unsigned total = 0;
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 * Compute and write distribution of #edges
126 */
127 {
128 unsigned state, label;
129 unsigned edge;
130 unsigned count [MAXEDGES + 1];
131 unsigned n;
132 unsigned edges = 0;
133 unsigned M = 0;
134 unsigned bits = bits_processed (output);
135
136 for (n = 0; n < MAXEDGES + 1; n++)
137 count [n] = 0;
138
139 for (state = wfa->basis_states; state <= last_domain; state++)
140 for (label = 0; label < MAXLABELS; label++)
141 if (isrange (wfa->tree [state][label]))
142 {
143 for (edge = 0; isedge (wfa->into [state][label][edge]); edge++)
144 ;
145 count [edge]++;
146 edges++;
147 M = MAX(edge, M);
148 }
149 write_rice_code (M, 3, output);
150 for (n = 0; n <= M; n++)
151 /* NETPBM: The following causes a crash when last_domain < 4, because
152 it requests writing of a negative number of bits. And we have seen
153 last_domain = 3. But we have no clue what last_domain means, or
154 even what a rice code is, so we don't know where the error lies.
155 -Bryan 2001.02.09
156 */
157 write_rice_code (count [n], (int) log2 (last_domain) - 2, output);
158
159 /*
160 * Arithmetic coding of values */
161 {
162 unsigned range;
163 model_t *elements = alloc_model (M + 1, 0, 0, count);
164 arith_t *encoder = alloc_encoder (output);
165
166 for (range = 0; range < rs.range_no; range++)
167 if (!rs.range_subdivided [range])
168 {
169 state = rs.range_state [range];
170 label = rs.range_label [range];
171 for (edge = 0; isedge (wfa->into [state][label][edge]); edge++)
172 ;
173
174 encode_symbol (edge, encoder, elements);
175 }
176 free_encoder (encoder);
177 free_model (elements);
178 }
179 debug_message ("delta-#edges: %5d bits. (%5d symbols => %5.2f bps)",
180 bits_processed (output) - bits, edges,
181 edges > 0 ? ((bits_processed (output) - bits) /
182 (double) edges) : 0);
183 }
184
185 /*
186 * Write matrix elements
187 */
188 {
189 unsigned bits = bits_processed (output);
190 u_word_t *mapping1 = Calloc (wfa->states, sizeof (u_word_t));
191 u_word_t *mapping2 = Calloc (wfa->states, sizeof (u_word_t));
192 unsigned range;
193
194 put_bit (output, use_normal_domains);
195 put_bit (output, use_delta_domains);
196
197 /*
198 * Generate array of states which are admitted domains.
199 * When coding intra frames 'mapping1' == 'mapping2' otherwise
200 * 'mapping1' is a list of 'normal' domains which are admitted for
201 * coding intra blocks
202 * 'mapping2' is a list of 'delta' domains which are admitted for
203 * coding the motion compensated prediction error
204 */
205 {
206 unsigned n1, n2, state;
207
208 for (n1 = n2 = state = 0; state < wfa->states; state++)
209 {
210 mapping1 [state] = n1;
211 if (usedomain (state, wfa)
212 && (state < wfa->basis_states || use_delta_domains
213 || !wfa->delta_state [state]))
214 n1++;
215
216 mapping2 [state] = n2;
217 if (usedomain (state, wfa)
218 && (state < wfa->basis_states || use_normal_domains
219 || wfa->delta_state [state]))
220 n2++;
221 }
222 debug_message ("# normal states = %d, # delta states = %d,"
223 " # WFA states = %d", n1, n2, wfa->states);
224 }
225
226 for (range = 0; range < rs.range_no; range++)
227 if (!rs.range_subdivided [range])
228 {
229 unsigned state = rs.range_state [range];
230 unsigned label = rs.range_label [range];
231 unsigned last = 1;
232 u_word_t *mapping;
233 unsigned max_value;
234 unsigned edge;
235 word_t domain;
236
237 if (wfa->delta_state [state] ||
238 wfa->mv_tree [state][label].type != NONE)
239 mapping = mapping2;
240 else
241 mapping = mapping1;
242
243 max_value = mapping [rs.range_max_domain [range]];
244
245 for (edge = 0; isedge (domain = wfa->into [state][label][edge]);
246 edge++)
247 if (domain > 0)
248 {
249 total++;
250 if (max_value - last)
251 {
252 write_bin_code (mapping [domain] - last,
253 max_value - last, output);
254 last = mapping [domain] + 1;
255 }
256 }
257 }
258
259 debug_message ("delta-index: %5d bits. (%5d symbols => %5.2f bps)",
260 bits_processed (output) - bits, total,
261 total > 0 ? ((bits_processed (output) - bits) /
262 (double) total) : 0);
263 Free (mapping1);
264 Free (mapping2);
265 }
266
267 Free (rs.range_state);
268 Free (rs.range_label);
269 Free (rs.range_max_domain);
270 Free (rs.range_subdivided);
271
272 return total;
273 }
274
275 static unsigned
276 column_0_encoding (const wfa_t *wfa, unsigned last_row, bitfile_t *output)
277 /*
278 * Write column 0 of the transition matrices of the 'wfa' to stream 'output'
279 * with quasi arithmetic coding.
280 * All rows from 'wfa->basis_states' up to 'last_row' are decoded.
281 *
282 * Return value:
283 * number of non-zero matrix elements (WFA edges)
284 */
285 {
286 u_word_t high; /* Start of the current code range */
287 u_word_t low; /* End of the current code range */
288 unsigned *prob; /* probability array */
289 unsigned row; /* current matrix row */
290 unsigned label; /* current matrix label */
291 unsigned underflow; /* Underflow bits */
292 unsigned index; /* probability index */
293 unsigned total = 0; /* Number of '1' elements */
294 unsigned bits = bits_processed (output);
295
296 /*
297 * Compute the probability array:
298 * prob[] = { 1/2, 1/2, 1/4, 1/4, 1/4, 1/4,
299 * 1/8, ... , 1/16, ..., 1/(MAXPROB+1)}
300 */
301 {
302 unsigned n;
303 unsigned exp; /* current exponent */
304
305 prob = Calloc (1 << (MAX_PROB + 1), sizeof (unsigned));
306
307 for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
308 for (exp = 0; exp < 1U << n; exp++, index++)
309 prob [index] = n;
310 }
311
312 high = HIGH; /* 1.0 */
313 low = LOW; /* 0.0 */
314 underflow = 0; /* no underflow bits */
315
316 index = 0;
317
318 /*
319 * Encode column 0 with a quasi arithmetic coder (QAC).
320 * Advantage of this QAC with respect to a binary AC:
321 * Instead of using time consuming multiplications and divisions
322 * to compute the probability of the most probable symbol (MPS) and
323 * the range of the interval, a table look up procedure linked
324 * with a shift operation is used for both computations.
325 */
326 for (row = wfa->basis_states; row <= last_row; row++)
327 for (label = 0; label < MAXLABELS; label++)
328 if (isrange (wfa->tree [row][label]))
329 {
330 if (wfa->into [row][label][0] != 0)
331 {
332 /*
333 * encode the MPS '0'
334 */
335 high = high - ((high - low) >> prob [index]) - 1;
336 RESCALE_OUTPUT_INTERVAL;
337
338 if (index < 1020)
339 index++;
340 }
341 else
342 {
343 /*
344 * encode the LPS '1'
345 */
346 low = high - ((high - low) >> prob [index]);
347
348 RESCALE_OUTPUT_INTERVAL;
349
350 total++;
351 index >>= 1;
352 }
353 }
354 /*
355 * Flush the quasi-arithmetic encoder
356 */
357 low = high;
358
359 RESCALE_OUTPUT_INTERVAL;
360
361 OUTPUT_BYTE_ALIGN (output);
362
363 Free (prob);
364
365 debug_message ("delta-state0: %5d bits. (%5d symbols => %5.2f bps)",
366 bits_processed (output) - bits, total,
367 total > 0 ? ((bits_processed (output) - bits) /
368 (double) total) : 0);
369
370 return total;
371 }
372
373 static unsigned
374 chroma_encoding (const wfa_t *wfa, bitfile_t *output)
375 /*
376 * Write transition matrices of 'wfa' states which are part of the
377 * chroma channels Cb and Cr to stream 'output'.
378 *
379 * Return value:
380 * number of non-zero matrix elements (WFA edges)
381 */
382 {
383
384 unsigned domain; /* current domain, counter */
385 unsigned label; /* current label */
386 unsigned total = 0; /* number of '1' elements */
387 u_word_t high; /* Start of the current code range */
388 u_word_t low; /* End of the current code range */
389 unsigned underflow; /* underflow bits */
390 unsigned *prob; /* probability array */
391 unsigned index; /* probability index, counter */
392 unsigned next_index; /* probability of last domain */
393 unsigned row; /* current matrix row */
394 word_t *y_domains;
395 unsigned count = 0; /* number of transitions for part 1 */
396 unsigned bits = bits_processed (output);
397
398 /*
399 * Compute the asymmetric probability array
400 * prob[] = { 1/2, 1/2, 1/4, 1/4, 1/4, 1/4,
401 * 1/8, ... , 1/16, ..., 1/(MAXPROB+1)}
402 */
403 {
404 unsigned n;
405 unsigned exp; /* current exponent */
406
407 prob = Calloc (1 << (MAX_PROB + 1), sizeof (unsigned));
408
409 for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
410 for (exp = 0; exp < 1U << n; exp++, index++)
411 prob [index] = n;
412 }
413
414 high = HIGH; /* 1.0 */
415 low = LOW; /* 0.0 */
416 underflow = 0; /* no underflow bits */
417
418 next_index = index = 0;
419
420 y_domains = compute_hits (wfa->basis_states,
421 wfa->tree [wfa->tree [wfa->root_state][0]][0],
422 wfa->wfainfo->chroma_max_states, wfa);
423
424 /*
425 * First of all, read all matrix columns given in the list 'y_domains'
426 * which note all admitted domains.
427 * These matrix elements are stored with QAC (see column_0_encoding ()).
428 */
429 for (domain = 0; y_domains [domain] != -1; domain++)
430 {
431 bool_t save_index = YES; /* YES: store current prob. index */
432
433 row = wfa->tree [wfa->tree [wfa->root_state][0]][0] + 1;
434 index = next_index;
435
436 for (; row < wfa->states; row++)
437 {
438 for (label = 0; label < MAXLABELS; label++)
439 if (isrange (wfa->tree [row][label]))
440 {
441 unsigned edge;
442 int into;
443 bool_t match; /* approx with current domain found */
444
445 for (match = NO, edge = 0;
446 isedge (into = wfa->into [row][label][edge])
447 && (unsigned) into < row;
448 edge++)
449 if (into == y_domains [domain]
450 && into != wfa->y_state [row][label])
451 match = YES;
452 if (!match)
453 {
454 /*
455 * encode the MPS '0'
456 */
457 high = high - ((high - low) >> prob [index]) - 1;
458
459 RESCALE_OUTPUT_INTERVAL;
460
461 if (index < 1020)
462 index++;
463 }
464 else
465 {
466 /*
467 * encode the LPS '1'
468 */
469 low = high - ((high - low) >> prob [index]);
470
471 RESCALE_OUTPUT_INTERVAL;
472
473 total++;
474 index >>= 1;
475 }
476 }
477 if (save_index)
478 {
479 next_index = index;
480 save_index = NO;
481 }
482 }
483 }
484
485 debug_message ("CbCr_matrix: %5d bits. (%5d symbols => %5.2f bps)",
486 bits_processed (output) - bits, total,
487 total > 0 ? ((bits_processed (output) - bits) /
488 (double) total) : 0);
489 count = total;
490 bits = bits_processed (output);
491
492 /*
493 * Encode the additional column which indicates whether there
494 * are transitions to a state with same spatial coordinates
495 * in the Y component.
496 *
497 * Again, quasi arithmetic coding is used for this task.
498 */
499
500 next_index = index = 0;
501
502 for (row = wfa->tree [wfa->tree [wfa->root_state][0]][0] + 1;
503 row < wfa->states; row++)
504 for (label = 0; label < MAXLABELS; label++)
505 if (!wfa->y_column [row][label])
506 {
507 /*
508 * encode the MPS '0'
509 */
510 high = high - ((high - low) >> prob [index]) - 1;
511
512 RESCALE_OUTPUT_INTERVAL;
513
514 if (index < 1020)
515 index++;
516 }
517 else
518 {
519 /*
520 * encode the LPS '1'
521 */
522 low = high - ((high - low) >> prob [index]);
523
524 RESCALE_OUTPUT_INTERVAL;
525
526 index >>= 1;
527 total++;
528 }
529
530 /*
531 * Flush the quasi-arithmetic encoder
532 */
533 low = high;
534
535 RESCALE_OUTPUT_INTERVAL;
536 OUTPUT_BYTE_ALIGN (output);
537
538 debug_message ("Yreferences: %5d bits. (%5d symbols => %5.2f bps)",
539 bits_processed (output) - bits, total - count,
540 total - count > 0 ? ((bits_processed (output) - bits) /
541 (double) (total - count)) : 0);
542
543 Free (prob);
544 Free (y_domains);
545
546 return total;
547 }