about summary refs log tree commit diff
path: root/converter/other/fiasco/output/write.c
blob: d6faee26f16d3b660fbcaf9ad24e1c93804c7eab (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
/*
 *  write.c:        Output of WFA files
 *
 *  Written by:     Ullrich Hafner
 *      
 *  This file is part of FIASCO (Fractal Image And Sequence COdec)
 *  Copyright (C) 1994-2000 Ullrich Hafner
 */

/*
 *  $Date: 2000/07/18 15:44:59 $
 *  $Author: hafner $
 *  $Revision: 5.3 $
 *  $State: Exp $
 */

#include "config.h"

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

#include "cwfa.h"
#include "image.h"
#include "misc.h"
#include "bit-io.h"
#include "rpf.h"

#include "tree.h"
#include "matrices.h"
#include "weights.h"
#include "mc.h"
#include "nd.h"
#include "write.h"
 
/*****************************************************************************

                prototypes
  
*****************************************************************************/

static void
write_tiling (const tiling_t *tiling, bitfile_t *output);

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

                public code

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


void
write_next_wfa (const wfa_t *wfa, const coding_t *c, bitfile_t *output)
/*
 *  Write 'wfa' to stream 'output'. If the first frame should be written
 *  then also store the header information of the coding struct 'c'.
 *
 *  No return value.
 */
{
   unsigned edges = 0;          /* number of transitions */
   unsigned bits;
   
   debug_message ("--------------------------------------"
          "--------------------------------------");

   if (c->mt->number == 0)              /* first WFA */
      write_header (wfa->wfainfo, output);
  
   bits = bits_processed (output);
   
   /*
    *  Frame header information
    */
   {
      const int rice_k = 8;     /* parameter of Rice Code */

      write_rice_code (wfa->states, rice_k, output);      
      write_rice_code (c->mt->frame_type, rice_k, output); 
      write_rice_code (c->mt->number, rice_k, output);     
   }
   
   OUTPUT_BYTE_ALIGN (output);

   debug_message ("frame-header: %5d bits.", bits_processed (output) - bits);

   if (c->tiling->exponent)     /* write tiling permutation */
   {
      put_bit (output, 1);
      write_tiling (c->tiling, output);
   }
   else
      put_bit (output, 0);

   OUTPUT_BYTE_ALIGN (output);

   write_tree (wfa, output);

   if (c->options.prediction)       /* write nondeterministic approx. */
   {
      put_bit (output, 1); 
      write_nd (wfa, output);
   }
   else
      put_bit (output, 0);

   if (c->mt->frame_type != I_FRAME)    /* write motion compensation info */
      write_mc (c->mt->frame_type, wfa, output);
   
   edges = write_matrices (c->options.normal_domains,
               c->options.delta_domains, wfa, output);

   if (edges)               /* found at least one approximation */
      write_weights (edges, wfa, output);

   debug_message ("--------------------------------------"
          "--------------------------------------");
}

void
write_header (const wfa_info_t *wi, bitfile_t *output)
/*
 *  Write the header information describing the type of 'wfa'
 *  to stream 'output'.
 *
 *  No return value.
 */
{
   const unsigned rice_k = 8;       /* parameter of Rice Code */
   unsigned   bits   = bits_processed (output);
   const char *text;            /* next character to write */

   /*
    *  Write magic number and name of initial basis
    */
   for (text = FIASCO_MAGIC; *text; text++)
      put_bits (output, *text, 8);
   put_bits (output, '\n', 8);
   for (text = wi->basis_name; *text; text++)
      put_bits (output, *text, 8);
   put_bits (output, *text, 8);
   
   write_rice_code (FIASCO_BINFILE_RELEASE, rice_k, output);

   write_rice_code (HEADER_TITLE, rice_k, output);
   for (text = wi->title;
    text && *text && text - wi->title < MAXSTRLEN - 2;
    text++)
      put_bits (output, *text, 8);
   put_bits (output, 0, 8);
   
   write_rice_code (HEADER_COMMENT, rice_k, output);
   for (text = wi->comment;
    text && *text && text - wi->comment < MAXSTRLEN - 2;
    text++)
      put_bits (output, *text, 8);
   put_bits (output, 0, 8);
   
   write_rice_code (HEADER_END, rice_k, output);
   
   write_rice_code (wi->max_states, rice_k, output); 
   put_bit (output, wi->color ? 1 : 0); 
   write_rice_code (wi->width, rice_k, output);
   write_rice_code (wi->height, rice_k, output);
   if (wi->color)
      write_rice_code (wi->chroma_max_states, rice_k, output); 
   write_rice_code (wi->p_min_level, rice_k, output); 
   write_rice_code (wi->p_max_level, rice_k, output); 
   write_rice_code (wi->frames, rice_k, output);
   write_rice_code (wi->smoothing, rice_k, output);

   put_bits (output, wi->rpf->mantissa_bits - 2, 3);
   put_bits (output, wi->rpf->range_e, 2);
   if (wi->rpf->mantissa_bits != wi->dc_rpf->mantissa_bits ||
       wi->rpf->range != wi->dc_rpf->range)
   {
      put_bit (output, YES);
      put_bits (output, wi->dc_rpf->mantissa_bits - 2, 3);
      put_bits (output, wi->dc_rpf->range_e, 2);
   }
   else
      put_bit (output, NO);
   if (wi->rpf->mantissa_bits != wi->d_rpf->mantissa_bits ||
       wi->rpf->range != wi->d_rpf->range)
   {
      put_bit (output, YES);
      put_bits (output, wi->d_rpf->mantissa_bits - 2, 3);
      put_bits (output, wi->d_rpf->range_e, 2);
   }
   else
      put_bit (output, NO);
   if (wi->dc_rpf->mantissa_bits != wi->d_dc_rpf->mantissa_bits ||
       wi->dc_rpf->range != wi->d_dc_rpf->range)
   {
      put_bit (output, YES);
      put_bits (output, wi->d_dc_rpf->mantissa_bits - 2, 3);
      put_bits (output, wi->d_dc_rpf->range_e, 2);
   }
   else
      put_bit (output, NO);

   if (wi->frames > 1)          /* motion compensation stuff */
   {
      write_rice_code (wi->fps, rice_k, output); 
      write_rice_code (wi->search_range, rice_k, output); 
      put_bit (output, wi->half_pixel ? 1 : 0); 
      put_bit (output, wi->B_as_past_ref ? 1 : 0);
   }

   OUTPUT_BYTE_ALIGN (output);
   debug_message ("header:         %d bits.", bits_processed (output) - bits);
}

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

                private code
  
*****************************************************************************/

static void
write_tiling (const tiling_t *tiling, bitfile_t *output)
/*
 *  Write image tiling information given by 'tiling' to stream 'output'.
 *
 *  No return value.
 */
{
   const unsigned rice_k = 8;       /* parameter of Rice Code */
   unsigned       bits   = bits_processed (output);
   
   write_rice_code (tiling->exponent, rice_k, output);
   if (tiling->method == FIASCO_TILING_VARIANCE_ASC
       || tiling->method == FIASCO_TILING_VARIANCE_DSC)
   {
      unsigned tile;            /* current image tile */

      put_bit (output, 1);      
      for (tile = 0; tile < 1U << tiling->exponent; tile++)
     if (tiling->vorder [tile] != -1) /* image tile is visible */
        put_bits (output, tiling->vorder [tile], tiling->exponent);
   }
   else
   {
      put_bit (output, 0);      
      put_bit (output, tiling->method == FIASCO_TILING_SPIRAL_ASC);
   }

   debug_message ("tiling:        %4d bits.", bits_processed (output) - bits);
}