| 1 |
/* |
| 2 |
* write.c: Output of WFA files |
| 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/07/18 15:44:59 $ |
| 12 |
* $Author: hafner $ |
| 13 |
* $Revision: 5.3 $ |
| 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 "cwfa.h" |
| 24 |
#include "image.h" |
| 25 |
#include "misc.h" |
| 26 |
#include "bit-io.h" |
| 27 |
#include "rpf.h" |
| 28 |
|
| 29 |
#include "tree.h" |
| 30 |
#include "matrices.h" |
| 31 |
#include "weights.h" |
| 32 |
#include "mc.h" |
| 33 |
#include "nd.h" |
| 34 |
#include "write.h" |
| 35 |
|
| 36 |
/***************************************************************************** |
| 37 |
|
| 38 |
prototypes |
| 39 |
|
| 40 |
*****************************************************************************/ |
| 41 |
|
| 42 |
static void |
| 43 |
write_tiling (const tiling_t *tiling, bitfile_t *output); |
| 44 |
|
| 45 |
/***************************************************************************** |
| 46 |
|
| 47 |
public code |
| 48 |
|
| 49 |
|
| 50 |
*****************************************************************************/ |
| 51 |
|
| 52 |
|
| 53 |
void |
| 54 |
write_next_wfa (const wfa_t *wfa, const coding_t *c, bitfile_t *output) |
| 55 |
/* |
| 56 |
* Write 'wfa' to stream 'output'. If the first frame should be written |
| 57 |
* then also store the header information of the coding struct 'c'. |
| 58 |
* |
| 59 |
* No return value. |
| 60 |
*/ |
| 61 |
{ |
| 62 |
unsigned edges = 0; /* number of transitions */ |
| 63 |
unsigned bits; |
| 64 |
|
| 65 |
debug_message ("--------------------------------------" |
| 66 |
"--------------------------------------"); |
| 67 |
|
| 68 |
if (c->mt->number == 0) /* first WFA */ |
| 69 |
write_header (wfa->wfainfo, output); |
| 70 |
|
| 71 |
bits = bits_processed (output); |
| 72 |
|
| 73 |
/* |
| 74 |
* Frame header information |
| 75 |
*/ |
| 76 |
{ |
| 77 |
const int rice_k = 8; /* parameter of Rice Code */ |
| 78 |
|
| 79 |
write_rice_code (wfa->states, rice_k, output); |
| 80 |
write_rice_code (c->mt->frame_type, rice_k, output); |
| 81 |
write_rice_code (c->mt->number, rice_k, output); |
| 82 |
} |
| 83 |
|
| 84 |
OUTPUT_BYTE_ALIGN (output); |
| 85 |
|
| 86 |
debug_message ("frame-header: %5d bits.", bits_processed (output) - bits); |
| 87 |
|
| 88 |
if (c->tiling->exponent) /* write tiling permutation */ |
| 89 |
{ |
| 90 |
put_bit (output, 1); |
| 91 |
write_tiling (c->tiling, output); |
| 92 |
} |
| 93 |
else |
| 94 |
put_bit (output, 0); |
| 95 |
|
| 96 |
OUTPUT_BYTE_ALIGN (output); |
| 97 |
|
| 98 |
write_tree (wfa, output); |
| 99 |
|
| 100 |
if (c->options.prediction) /* write nondeterministic approx. */ |
| 101 |
{ |
| 102 |
put_bit (output, 1); |
| 103 |
write_nd (wfa, output); |
| 104 |
} |
| 105 |
else |
| 106 |
put_bit (output, 0); |
| 107 |
|
| 108 |
if (c->mt->frame_type != I_FRAME) /* write motion compensation info */ |
| 109 |
write_mc (c->mt->frame_type, wfa, output); |
| 110 |
|
| 111 |
edges = write_matrices (c->options.normal_domains, |
| 112 |
c->options.delta_domains, wfa, output); |
| 113 |
|
| 114 |
if (edges) /* found at least one approximation */ |
| 115 |
write_weights (edges, wfa, output); |
| 116 |
|
| 117 |
debug_message ("--------------------------------------" |
| 118 |
"--------------------------------------"); |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
write_header (const wfa_info_t *wi, bitfile_t *output) |
| 123 |
/* |
| 124 |
* Write the header information describing the type of 'wfa' |
| 125 |
* to stream 'output'. |
| 126 |
* |
| 127 |
* No return value. |
| 128 |
*/ |
| 129 |
{ |
| 130 |
const unsigned rice_k = 8; /* parameter of Rice Code */ |
| 131 |
unsigned bits = bits_processed (output); |
| 132 |
const char *text; /* next character to write */ |
| 133 |
|
| 134 |
/* |
| 135 |
* Write magic number and name of initial basis |
| 136 |
*/ |
| 137 |
for (text = FIASCO_MAGIC; *text; text++) |
| 138 |
put_bits (output, *text, 8); |
| 139 |
put_bits (output, '\n', 8); |
| 140 |
for (text = wi->basis_name; *text; text++) |
| 141 |
put_bits (output, *text, 8); |
| 142 |
put_bits (output, *text, 8); |
| 143 |
|
| 144 |
write_rice_code (FIASCO_BINFILE_RELEASE, rice_k, output); |
| 145 |
|
| 146 |
write_rice_code (HEADER_TITLE, rice_k, output); |
| 147 |
for (text = wi->title; |
| 148 |
text && *text && text - wi->title < MAXSTRLEN - 2; |
| 149 |
text++) |
| 150 |
put_bits (output, *text, 8); |
| 151 |
put_bits (output, 0, 8); |
| 152 |
|
| 153 |
write_rice_code (HEADER_COMMENT, rice_k, output); |
| 154 |
for (text = wi->comment; |
| 155 |
text && *text && text - wi->comment < MAXSTRLEN - 2; |
| 156 |
text++) |
| 157 |
put_bits (output, *text, 8); |
| 158 |
put_bits (output, 0, 8); |
| 159 |
|
| 160 |
write_rice_code (HEADER_END, rice_k, output); |
| 161 |
|
| 162 |
write_rice_code (wi->max_states, rice_k, output); |
| 163 |
put_bit (output, wi->color ? 1 : 0); |
| 164 |
write_rice_code (wi->width, rice_k, output); |
| 165 |
write_rice_code (wi->height, rice_k, output); |
| 166 |
if (wi->color) |
| 167 |
write_rice_code (wi->chroma_max_states, rice_k, output); |
| 168 |
write_rice_code (wi->p_min_level, rice_k, output); |
| 169 |
write_rice_code (wi->p_max_level, rice_k, output); |
| 170 |
write_rice_code (wi->frames, rice_k, output); |
| 171 |
write_rice_code (wi->smoothing, rice_k, output); |
| 172 |
|
| 173 |
put_bits (output, wi->rpf->mantissa_bits - 2, 3); |
| 174 |
put_bits (output, wi->rpf->range_e, 2); |
| 175 |
if (wi->rpf->mantissa_bits != wi->dc_rpf->mantissa_bits || |
| 176 |
wi->rpf->range != wi->dc_rpf->range) |
| 177 |
{ |
| 178 |
put_bit (output, YES); |
| 179 |
put_bits (output, wi->dc_rpf->mantissa_bits - 2, 3); |
| 180 |
put_bits (output, wi->dc_rpf->range_e, 2); |
| 181 |
} |
| 182 |
else |
| 183 |
put_bit (output, NO); |
| 184 |
if (wi->rpf->mantissa_bits != wi->d_rpf->mantissa_bits || |
| 185 |
wi->rpf->range != wi->d_rpf->range) |
| 186 |
{ |
| 187 |
put_bit (output, YES); |
| 188 |
put_bits (output, wi->d_rpf->mantissa_bits - 2, 3); |
| 189 |
put_bits (output, wi->d_rpf->range_e, 2); |
| 190 |
} |
| 191 |
else |
| 192 |
put_bit (output, NO); |
| 193 |
if (wi->dc_rpf->mantissa_bits != wi->d_dc_rpf->mantissa_bits || |
| 194 |
wi->dc_rpf->range != wi->d_dc_rpf->range) |
| 195 |
{ |
| 196 |
put_bit (output, YES); |
| 197 |
put_bits (output, wi->d_dc_rpf->mantissa_bits - 2, 3); |
| 198 |
put_bits (output, wi->d_dc_rpf->range_e, 2); |
| 199 |
} |
| 200 |
else |
| 201 |
put_bit (output, NO); |
| 202 |
|
| 203 |
if (wi->frames > 1) /* motion compensation stuff */ |
| 204 |
{ |
| 205 |
write_rice_code (wi->fps, rice_k, output); |
| 206 |
write_rice_code (wi->search_range, rice_k, output); |
| 207 |
put_bit (output, wi->half_pixel ? 1 : 0); |
| 208 |
put_bit (output, wi->B_as_past_ref ? 1 : 0); |
| 209 |
} |
| 210 |
|
| 211 |
OUTPUT_BYTE_ALIGN (output); |
| 212 |
debug_message ("header: %d bits.", bits_processed (output) - bits); |
| 213 |
} |
| 214 |
|
| 215 |
/***************************************************************************** |
| 216 |
|
| 217 |
private code |
| 218 |
|
| 219 |
*****************************************************************************/ |
| 220 |
|
| 221 |
static void |
| 222 |
write_tiling (const tiling_t *tiling, bitfile_t *output) |
| 223 |
/* |
| 224 |
* Write image tiling information given by 'tiling' to stream 'output'. |
| 225 |
* |
| 226 |
* No return value. |
| 227 |
*/ |
| 228 |
{ |
| 229 |
const unsigned rice_k = 8; /* parameter of Rice Code */ |
| 230 |
unsigned bits = bits_processed (output); |
| 231 |
|
| 232 |
write_rice_code (tiling->exponent, rice_k, output); |
| 233 |
if (tiling->method == FIASCO_TILING_VARIANCE_ASC |
| 234 |
|| tiling->method == FIASCO_TILING_VARIANCE_DSC) |
| 235 |
{ |
| 236 |
unsigned tile; /* current image tile */ |
| 237 |
|
| 238 |
put_bit (output, 1); |
| 239 |
for (tile = 0; tile < 1U << tiling->exponent; tile++) |
| 240 |
if (tiling->vorder [tile] != -1) /* image tile is visible */ |
| 241 |
put_bits (output, tiling->vorder [tile], tiling->exponent); |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
put_bit (output, 0); |
| 246 |
put_bit (output, tiling->method == FIASCO_TILING_SPIRAL_ASC); |
| 247 |
} |
| 248 |
|
| 249 |
debug_message ("tiling: %4d bits.", bits_processed (output) - bits); |
| 250 |
} |