about summary refs log tree commit diff
path: root/converter/other/fiasco/input/matrices.c
blob: d20a27ebaad3414316458f22c3728d730835c545 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
 *  matrices.c:		Input of transition matrices
 *
 *  Written by:		Ullrich Hafner
 *
 *  This file is part of FIASCO (Fractal Image And Sequence COdec)
 *  Copyright (C) 1994-2000 Ullrich Hafner
 */

/*
 *  $Date: 2000/06/14 20:50:13 $
 *  $Author: hafner $
 *  $Revision: 5.1 $
 *  $State: Exp $
 */

#include "config.h"

#include "types.h"
#include "macros.h"
#include "error.h"

#include "bit-io.h"
#include "arith.h"
#include "misc.h"
#include "wfalib.h"

#include "matrices.h"

#if STDC_HEADERS
#	include <stdlib.h>
#endif /* not STDC_HEADERS */

/*****************************************************************************

				prototypes

*****************************************************************************/

static unsigned
delta_decoding (wfa_t *wfa, unsigned last_domain, bitfile_t *input);
static unsigned
column_0_decoding (wfa_t *wfa, unsigned last_row, bitfile_t *input);
static unsigned
chroma_decoding (wfa_t *wfa, bitfile_t *input);
static void
compute_y_state (int state, int y_state, wfa_t *wfa);

/*****************************************************************************

				public code

*****************************************************************************/

unsigned
read_matrices (wfa_t *wfa, bitfile_t *input)
/*
 *  Read transitions of WFA given from the stream 'input'.
 *
 *  Return value:
 *	number of edges
 *
 *  Side effects:
 *	'wfa->into' is filled with decoded values
 */
{
   unsigned total;			/* total number of edges in the WFA */
   unsigned root_state = wfa->wfainfo->color
			 ? wfa->tree [wfa->tree [wfa->root_state][0]][0]
			 : wfa->root_state;

   total  = column_0_decoding (wfa, root_state, input);
   total += delta_decoding (wfa, root_state, input);
   if (wfa->wfainfo->color)
      total += chroma_decoding (wfa, input);

   return total;
}

/*****************************************************************************

				private code

*****************************************************************************/

static unsigned
delta_decoding (wfa_t *wfa, unsigned last_domain, bitfile_t *input)
/*
 *  Read transition matrices which are encoded with delta coding
 *  from stream 'input'.
 *  'last_domain' is the maximum state number used as domain image.
 *
 *  Return value:
 *	number of non-zero matrix elements (WFA edges)
 *
 *  Side effects:
 *	'wfa->into' is filled with decoded values
 */
{
   range_sort_t	 rs;			/* ranges are sorted as in the coder */
   unsigned	 max_domain;		/* dummy used for recursion */
   unsigned	 range;
   unsigned	 count [MAXEDGES + 1];
   unsigned 	 state, label;
   unsigned	*n_edges;		/* number of elements per row */
   unsigned	 total = 0;		/* total number of decoded edges */

   /*
    *  Generate a list of range blocks.
    *  The order is the same as in the coder.
    */
   rs.range_state      = Calloc ((last_domain + 1) * MAXLABELS,
				 sizeof (u_word_t));
   rs.range_label      = Calloc ((last_domain + 1) * MAXLABELS,
				 sizeof (byte_t));
   rs.range_max_domain = Calloc ((last_domain + 1) * MAXLABELS,
				 sizeof (u_word_t));
   rs.range_subdivided = Calloc ((last_domain + 1) * MAXLABELS,
				 sizeof (bool_t));
   rs.range_no	       = 0;
   max_domain 	       = wfa->basis_states - 1;
   sort_ranges (last_domain, &max_domain, &rs, wfa);

   /*
    *  Get row statistics
    */
   {
      arith_t  *decoder;
      model_t  *elements;
      unsigned 	max_edges = read_rice_code (3, input);

      /*
       *  Get the probability array of the number of edges distribution
       *  and allocate the corresponding model.
       */
      {
	 unsigned edge;

	 for (edge = 0; edge <= max_edges; edge++)
	    count [edge] = read_rice_code ((int) log2 (last_domain) - 2,
					   input);
	 elements = alloc_model (max_edges + 1, 0, 0, count);
      }

      /*
       *  Get number of elements per matrix row
       */
      {
	 unsigned row;

	 n_edges = Calloc (wfa->states, sizeof (unsigned));
	 decoder = alloc_decoder (input);
	 for (row = range = 0; range < rs.range_no; range++)
	    if (!rs.range_subdivided [range])
	    {
	       state = rs.range_state [range];
	       label = rs.range_label [range];

	       n_edges [row++]
		  = decode_symbol (decoder, elements)
		  - (isedge (wfa->into [state][label][0]) ? 1 : 0);
	    }

	 free_decoder (decoder);
	 free_model (elements);
      }
   }

   /*
    *  Get matrix elements
    */
   {
      unsigned row;
      u_word_t *mapping1           = Calloc (wfa->states, sizeof (word_t));
      u_word_t *mapping_coder1     = Calloc (wfa->states, sizeof (word_t));
      u_word_t *mapping2           = Calloc (wfa->states, sizeof (word_t));
      u_word_t *mapping_coder2     = Calloc (wfa->states, sizeof (word_t));
      bool_t	use_normal_domains = get_bit (input);
      bool_t	use_delta_domains  = get_bit (input);

      /*
       *  Generate array of states which are admitted domains.
       *  When coding intra frames 'mapping1' == 'mapping2' otherwise
       *  'mapping1' is a list of 'normal' domains which are admitted for
       *             coding intra blocks
       *  'mapping2' is a list of 'delta' domains which are admitted for
       *             coding the motion compensated prediction error
       */
      {
	 unsigned n1, n2, state;

	 for (n1 = n2 = state = 0; state < wfa->states; state++)
	 {
	    mapping1 [n1] = state;
	    mapping_coder1 [state] = n1;
	    if (usedomain (state, wfa)
		&& (state < wfa->basis_states
		    || use_delta_domains || !wfa->delta_state [state]))
	       n1++;

	    mapping2 [n2] = state;
	    mapping_coder2 [state] = n2;
	    if (usedomain (state, wfa)
		&& (state < wfa->basis_states || use_normal_domains
		    || wfa->delta_state [state]))
	       n2++;
	 }
      }

      for (row = 0, range = 0; range < rs.range_no; range++)
	 if (!rs.range_subdivided [range])
	 {
	    u_word_t *mapping;
	    u_word_t *mapping_coder;
	    unsigned  max_value;
	    unsigned  edge;
	    unsigned  state = rs.range_state [range];
	    unsigned  label = rs.range_label [range];
	    unsigned  last  = 1;

	    if (wfa->delta_state [state] ||
		wfa->mv_tree [state][label].type != NONE)
	    {
	       mapping 	     = mapping2;
	       mapping_coder = mapping_coder2;
	    }
	    else
	    {
	       mapping 	     = mapping1;
	       mapping_coder = mapping_coder1;
	    }
	    max_value = mapping_coder [rs.range_max_domain [range]];
	    for (edge = n_edges [row]; edge; edge--)
	    {
	       unsigned domain;

	       if (max_value - last)
		  domain = read_bin_code (max_value - last, input) + last;
	       else
		  domain = max_value;
	       append_edge (state, mapping [domain], -1, label, wfa);
	       last = domain + 1;
	       total++;
	    }
	    row++;
	 }
      Free (mapping1);
      Free (mapping_coder1);
      Free (mapping2);
      Free (mapping_coder2);
   }

   Free (n_edges);
   Free (rs.range_state);
   Free (rs.range_label);
   Free (rs.range_max_domain);
   Free (rs.range_subdivided);

   return total;
}

static unsigned
column_0_decoding (wfa_t *wfa, unsigned last_row, bitfile_t *input)
/*
 *  Read column 0 of the transition matrices of the 'wfa' which are coded
 *  with quasi arithmetic coding from stream 'input'.
 *  All rows from 'wfa->basis_states' up to 'last_row' are decoded.
 *
 *  Return value:
 *	number of non-zero matrix elements (WFA edges)
 *
 *  Side effects:
 *	'wfa->into' is filled with decoded values
 */
{
   unsigned  row;			/* current matrix row */
   unsigned  total = 0;			/* total number of edges in col 0 */
   unsigned *prob_ptr;			/* pointer to current probability */
   unsigned *last;			/* pointer to minimum probability */
   unsigned *first;			/* pointer to maximum probability */
   unsigned *new_prob_ptr;		/* ptr to probability of last domain */
   unsigned *prob;			/* probability array */
   u_word_t  high;			/* Start of the current code range */
   u_word_t  low;			/* End of the current code range */
   u_word_t  code;			/* The present input code value */
   word_t   *is_leaf;			/* pointer to the tree structure */

   /*
    *  Compute the asymmetric probability array
    *  prob[] = { 1/2, 1/2, 1/4, 1/4, 1/4, 1/4,
    *             1/8, ... , 1/16, ..., 1/(MAXPROB+1)}
    */
   {
      unsigned n;
      unsigned index;			/* probability index */
      unsigned exp;			/* current exponent */

      prob = Calloc (1 << (MAX_PROB + 1), sizeof (unsigned));

      for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
	 for (exp = 0; exp < 1U << n; exp++, index++)
	    prob [index] = n;
   }

   first = prob_ptr = new_prob_ptr = prob;
   last  = first + 1020;

   is_leaf = wfa->tree [wfa->basis_states]; /* use pointer arithmetics ... */

   high = HIGH;				/* 1.0 */
   low  = LOW;				/* 0.0 */
   code = get_bits (input, 16);

   /*
    *  Decode column 0 with a quasi arithmetic coder (QAC).
    *  Advantage of this QAC with respect to a binary AC:
    *  Instead of using time consuming multiplications and divisions
    *  to compute the probability of the most probable symbol (MPS) and
    *  the range of the interval, a table look up procedure linked
    *  with a shift operation is used for both computations.
    *
    *  Loops and array accesses have been removed
    *  to make real time decoding possible.
    */
   for (row = wfa->basis_states; row <= last_row; row++)
   {
      unsigned count;			/* value in the current interval */

      /*
       *  Read label 0 element
       */
      if (isrange (*is_leaf++))		/* valid matrix index */
      {
	 count = high - ((high - low) >> *prob_ptr);
	 if (code < count)
	 {
	    if (prob_ptr < last)	/* model update */
	       prob_ptr++;
	    /*
	     *  Decode the MPS '0'
	     */
	    high = count - 1;

	    RESCALE_INPUT_INTERVAL;
	 }
	 else
	 {
	    prob_ptr = ((prob_ptr - first) >> 1) + first; /* model update */
	    /*
	     *  Decode the LPS '1'
	     */
	    low = count;

	    RESCALE_INPUT_INTERVAL;
	    /*
	     *  Restore the transition (weight = -1)
	     */
	    append_edge (row, 0, -1, 0, wfa);
	    total++;
	 }
      }
      /*
       *  Read label 1 element
       */
      if (isrange (*is_leaf++)) /* valid matrix index */
      {
	 count = high - ((high - low) >> *prob_ptr);
	 if (code < count)
	 {
	    if (prob_ptr < last)
	       prob_ptr++;		/* model update */
	    /*
	     *  Decode the MPS '0'
	     */
	    high = count - 1;

	    RESCALE_INPUT_INTERVAL;
	 }
	 else
	 {
	    prob_ptr = ((prob_ptr - first) >> 1) + first; /* model update */
	    /*
	     *  Decode the LPS '1'
	     */
	    low = count;

	    RESCALE_INPUT_INTERVAL;
	    /*
	     *  Restore the transition (weight = -1)
	     */
	    append_edge (row, 0, -1, 1, wfa);
	    total++;
	 }
      }
   }

   INPUT_BYTE_ALIGN (input);

   Free (prob);

   return total;
}

static unsigned
chroma_decoding (wfa_t *wfa, bitfile_t *input)
/*
 *  Read transition matrices of 'wfa' states which are part of the
 *  chroma channels Cb and Cr from stream 'input'.
 *
 *  Return value:
 *	number of non-zero matrix elements (WFA edges)
 *
 *  Side effects:
 *	'wfa->into' is filled with decoded values
 */
{
   unsigned  domain;			/* current domain, counter */
   unsigned  total = 0;			/* total number of chroma edges */
   unsigned *prob_ptr;			/* pointer to current probability */
   unsigned *last;			/* pointer to minimum probability */
   unsigned *first;			/* pointer to maximum probability */
   unsigned *new_prob_ptr;		/* ptr to probability of last domain */
   unsigned *prob;			/* probability array */
   u_word_t  high;			/* Start of the current code range */
   u_word_t  low;			/* End of the current code range */
   u_word_t  code;			/* The present input code value */
   word_t   *y_domains;			/* domain images corresponding to Y */
   int	     save_index;		/* YES: store current probability */

   /*
    *  Compute the asymmetric probability array
    *  prob[] = { 1/2, 1/2, 1/4, 1/4, 1/4, 1/4,
    *                     1/8, ... , 1/16, ..., 1/(MAXPROB+1)}
    */
   {
      unsigned n;
      unsigned index;			/* probability index */
      unsigned exp;			/* current exponent */

      prob = Calloc (1 << (MAX_PROB + 1), sizeof (unsigned));

      for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
	 for (exp = 0; exp < 1U << n; exp++, index++)
	    prob [index] = n;
   }

   high = HIGH;				/* 1.0 */
   low  = LOW;				/* 0.0 */
   code = get_bits (input, 16);

   /*
    *  Compute list of admitted domains
    */
   y_domains = compute_hits (wfa->basis_states,
			     wfa->tree [wfa->tree [wfa->root_state][0]][0],
			     wfa->wfainfo->chroma_max_states, wfa);

   first = prob_ptr = new_prob_ptr = prob;
   last  = first + 1020;

   /*
    *  First of all, read all matrix columns given in the list 'y_domains'
    *  which note all admitted domains.
    *  These matrix elements are stored with QAC (see column_0_decoding ()).
    */
   for (domain = 0; y_domains [domain] != -1; domain++)
   {
      unsigned 	row	= wfa->tree [wfa->tree [wfa->root_state][0]][0] + 1;
      word_t   *is_leaf = wfa->tree [row];

      prob_ptr   = new_prob_ptr;
      save_index = YES;

      for (; row < wfa->states; row++)
      {
	 unsigned count;		/* value in the current interval */
	 /*
	  *  Read label 0 element
	  */
	 if (isrange (*is_leaf++)) 	/* valid matrix index */
	 {
	    count = high - ((high - low) >> *prob_ptr);
	    if (code < count)
	    {
	       if (prob_ptr < last)
		  prob_ptr++;
	       /*
		*  Decode the MPS '0'
		*/
	       high = count - 1;

	       RESCALE_INPUT_INTERVAL;
	    }
	    else
	    {
	       prob_ptr = ((prob_ptr - first) >> 1) + first;
	       /*
		*  Decode the LPS '1'
		*/
	       low = count;

	       RESCALE_INPUT_INTERVAL;
	       /*
		*  Restore the transition (weight = -1)
		*/
	       append_edge (row, y_domains [domain], -1, 0, wfa);
	       total++;
	    }
	 }
	 /*
	  *  Read label 1 element
	  */
	 if (isrange (*is_leaf++)) /* valid matrix index */
	 {
	    count = high - ((high - low) >> *prob_ptr);
	    if (code < count)
	    {
	       if (prob_ptr < last)
		  prob_ptr++;
	       /*
		*  Decode the MPS '0'
		*/
	       high = count - 1;

	       RESCALE_INPUT_INTERVAL;
	    }
	    else
	    {
	       prob_ptr = ((prob_ptr - first) >> 1) + first;
	       /*
		*  Decode the LPS '1'
		*/
	       low = count;

	       RESCALE_INPUT_INTERVAL;
	       /*
		*  Restore the transition (weight = -1)
		*/
	       append_edge (row, y_domains [domain], -1, 1, wfa);
	       total++;
	    }
	 }
	 if (save_index)
	 {
	    save_index 	 = NO;
	    new_prob_ptr = prob_ptr;
	 }
      }
   }

   Free (y_domains);

   compute_y_state (wfa->tree [wfa->tree [wfa->root_state][0]][1],
		    wfa->tree [wfa->tree [wfa->root_state][0]][0], wfa);
   compute_y_state (wfa->tree [wfa->tree [wfa->root_state][1]][0],
		    wfa->tree [wfa->tree [wfa->root_state][0]][0], wfa);

   first = prob_ptr = new_prob_ptr = prob;

   /*
    *  Decode the additional column which indicates whether there
    *  are transitions to a state with same spatial coordinates
    *  in the Y component.
    *
    *  Again, quasi arithmetic decoding is used for this task.
    */
   {
      unsigned 	row;

      for (row = wfa->tree [wfa->tree [wfa->root_state][0]][0] + 1;
	   row < wfa->states; row++)
      {
	 int label;			/* current label */

	 for (label = 0; label < MAXLABELS; label++)
	 {
	    u_word_t count = high - ((high - low) >> *prob_ptr);

	    if (code < count)
	    {
	       if (prob_ptr < last)
		  prob_ptr++;
	       /*
		*  Decode the MPS '0'
		*/
	       high = count - 1;

	       RESCALE_INPUT_INTERVAL;
	    }
	    else
	    {
	       prob_ptr = ((prob_ptr - first) >> 1) + first;
	       /*
		*  Decode the LPS '1'
		*/
	       low = count;

	       RESCALE_INPUT_INTERVAL;
	       /*
		*  Restore the transition (weight = -1)
		*/
	       append_edge (row, wfa->y_state [row][label], -1, label, wfa);
	       total++;
	    }
	 }
      }
   }

   INPUT_BYTE_ALIGN (input);

   Free (prob);

   return total;
}

static void
compute_y_state (int state, int y_state, wfa_t *wfa)
/*
 *  Compute the 'wfa->y_state' array which denotes those states of
 *  the Y band that have the same spatial coordinates as the corresponding
 *  states of the Cb and Cr bands.
 *  The current root of the Y tree is given by 'y_state'.
 *  The current root of the tree of the chroma channel is given by 'state'.
 *
 *  No return value.
 *
 *  Side effects:
 *	'wfa->y_state' is filled with the generated tree structure.
 */
{
   unsigned label;

   for (label = 0; label < MAXLABELS; label++)
      if (isrange (y_state))
	 wfa->y_state [state][label] = RANGE;
      else
      {
	 wfa->y_state [state][label] = wfa->tree [y_state][label];
	 if (!isrange (wfa->tree [state][label]))
	    compute_y_state (wfa->tree [state][label],
			     wfa->y_state [state][label], wfa);
      }

}