| 1 |
/* |
| 2 |
* mc.c: Input of motion compensation |
| 3 |
* |
| 4 |
* written by: Michael Unger |
| 5 |
* Ullrich Hafner |
| 6 |
* |
| 7 |
* This file is part of FIASCO (Fractal Image And Sequence COdec) |
| 8 |
* Copyright (C) 1994-2000 Ullrich Hafner |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* $Date: 2000/06/14 20:50:13 $ |
| 13 |
* $Author: hafner $ |
| 14 |
* $Revision: 5.1 $ |
| 15 |
* $State: Exp $ |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include "config.h" |
| 20 |
|
| 21 |
#include "types.h" |
| 22 |
#include "macros.h" |
| 23 |
#include "error.h" |
| 24 |
|
| 25 |
#include "wfa.h" |
| 26 |
#include "bit-io.h" |
| 27 |
#include "misc.h" |
| 28 |
#include "mvcode.h" |
| 29 |
|
| 30 |
#include "mc.h" |
| 31 |
|
| 32 |
/***************************************************************************** |
| 33 |
|
| 34 |
local variables |
| 35 |
|
| 36 |
*****************************************************************************/ |
| 37 |
|
| 38 |
typedef struct huff_node |
| 39 |
{ |
| 40 |
int code_index; /* leaf if index >= 0 */ |
| 41 |
struct huff_node *left; /* follow if '0' bit read */ |
| 42 |
struct huff_node *right; /* follow if '1' bit read */ |
| 43 |
int index_set [34]; |
| 44 |
} huff_node_t; |
| 45 |
|
| 46 |
/***************************************************************************** |
| 47 |
|
| 48 |
prototypes |
| 49 |
|
| 50 |
*****************************************************************************/ |
| 51 |
|
| 52 |
static void |
| 53 |
decode_mc_tree (frame_type_e frame_type, unsigned max_state, |
| 54 |
wfa_t *wfa, bitfile_t *input); |
| 55 |
static void |
| 56 |
decode_mc_coords (unsigned max_state, wfa_t *wfa, bitfile_t *input); |
| 57 |
static int |
| 58 |
get_mv (int f_code, huff_node_t *hn, bitfile_t *input); |
| 59 |
static huff_node_t * |
| 60 |
create_huff_tree (void); |
| 61 |
static void |
| 62 |
create_huff_node (huff_node_t *hn, int bits_processed); |
| 63 |
|
| 64 |
/***************************************************************************** |
| 65 |
|
| 66 |
public code |
| 67 |
|
| 68 |
*****************************************************************************/ |
| 69 |
|
| 70 |
void |
| 71 |
read_mc (frame_type_e frame_type, wfa_t *wfa, bitfile_t *input) |
| 72 |
/* |
| 73 |
* Read motion compensation information of the 'input' stream. |
| 74 |
* Depending on 'frame_type' different decoding methods are used. |
| 75 |
* |
| 76 |
* No return value. |
| 77 |
* |
| 78 |
* Side effects: |
| 79 |
* 'wfa->mv_tree' is filled with the decoded values. |
| 80 |
*/ |
| 81 |
{ |
| 82 |
unsigned max_state = wfa->wfainfo->color |
| 83 |
? wfa->tree [wfa->tree [wfa->root_state][0]][0] |
| 84 |
: wfa->states; |
| 85 |
|
| 86 |
decode_mc_tree (frame_type, max_state, wfa, input); |
| 87 |
decode_mc_coords (max_state, wfa, input); |
| 88 |
} |
| 89 |
|
| 90 |
/***************************************************************************** |
| 91 |
|
| 92 |
private code |
| 93 |
|
| 94 |
*****************************************************************************/ |
| 95 |
|
| 96 |
static void |
| 97 |
decode_mc_tree (frame_type_e frame_type, unsigned max_state, |
| 98 |
wfa_t *wfa, bitfile_t *input) |
| 99 |
/* |
| 100 |
* Read tree of motion compensation decisions of the 'input' stream. |
| 101 |
* Depending on 'frame_type' different decoding methods are used. |
| 102 |
* 'max_state' is the last state with motion compensation infos. |
| 103 |
* |
| 104 |
* No return value. |
| 105 |
* |
| 106 |
* Side effects: |
| 107 |
* 'wfa->mv_tree' is filled with decoded values. |
| 108 |
*/ |
| 109 |
{ |
| 110 |
unsigned state; /* current state */ |
| 111 |
unsigned *queue; /* states in breadth first order */ |
| 112 |
unsigned last; /* last node |
| 113 |
(update for each new node) */ |
| 114 |
|
| 115 |
/* |
| 116 |
* Traverse tree in breadth first order (starting at level |
| 117 |
* 'wfa->wfainfo->p_max_level'). Use a queue to store the children |
| 118 |
* of each node ('last' is the next free queue element). |
| 119 |
*/ |
| 120 |
queue = Calloc (MAXSTATES, sizeof (unsigned)); |
| 121 |
for (last = 0, state = wfa->basis_states; state < max_state; state++) |
| 122 |
if (wfa->level_of_state [state] - 1 == (int) wfa->wfainfo->p_max_level) |
| 123 |
queue [last++] = state; /* init level 'p_max_level' */ |
| 124 |
|
| 125 |
if (frame_type == P_FRAME) |
| 126 |
{ |
| 127 |
unsigned label; /* current label */ |
| 128 |
unsigned current; /* current node to process */ |
| 129 |
|
| 130 |
for (current = 0; current < last; current++) |
| 131 |
for (label = 0; label < MAXLABELS; label++) |
| 132 |
{ |
| 133 |
state = queue[current]; |
| 134 |
if (wfa->x [state][label] /* process visible states only */ |
| 135 |
+ width_of_level (wfa->level_of_state [state] - 1) |
| 136 |
<= wfa->wfainfo->width |
| 137 |
&& |
| 138 |
wfa->y [state][label] |
| 139 |
+ height_of_level (wfa->level_of_state [state] - 1) |
| 140 |
<= wfa->wfainfo->height) |
| 141 |
{ |
| 142 |
wfa->mv_tree [state][label].type |
| 143 |
= get_bit (input) ? NONE : FORWARD; |
| 144 |
} |
| 145 |
else |
| 146 |
wfa->mv_tree [state][label].type = NONE; |
| 147 |
if (wfa->mv_tree [state][label].type == NONE && |
| 148 |
!isrange (wfa->tree [state][label]) && |
| 149 |
wfa->level_of_state [state] - 1 >= |
| 150 |
(int) wfa->wfainfo->p_min_level) |
| 151 |
queue [last++] = wfa->tree [state][label]; /* append child */ |
| 152 |
} |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
unsigned label; /* current label */ |
| 157 |
unsigned current; /* current node to process */ |
| 158 |
|
| 159 |
for (current = 0; current < last; current++) |
| 160 |
for (label = 0; label < MAXLABELS; label++) |
| 161 |
{ |
| 162 |
state = queue[current]; |
| 163 |
if (wfa->x [state][label] /* process visible states only */ |
| 164 |
+ width_of_level (wfa->level_of_state [state] - 1) |
| 165 |
> wfa->wfainfo->width |
| 166 |
|| |
| 167 |
wfa->y [state][label] |
| 168 |
+ height_of_level (wfa->level_of_state [state] - 1) |
| 169 |
> wfa->wfainfo->height) |
| 170 |
wfa->mv_tree[state][label].type = NONE; |
| 171 |
else if (get_bit (input)) /* 1 */ |
| 172 |
wfa->mv_tree[state][label].type = NONE; |
| 173 |
else if (get_bit (input)) /* 01 */ |
| 174 |
wfa->mv_tree[state][label].type = INTERPOLATED; |
| 175 |
else if (get_bit (input)) /* 001 */ |
| 176 |
wfa->mv_tree[state][label].type = BACKWARD; |
| 177 |
else /* 000 */ |
| 178 |
wfa->mv_tree[state][label].type = FORWARD; |
| 179 |
if (wfa->mv_tree[state][label].type == NONE && |
| 180 |
!isrange (wfa->tree[state][label]) && |
| 181 |
wfa->level_of_state[state] - 1 |
| 182 |
>= (int) wfa->wfainfo->p_min_level) |
| 183 |
queue[last++] = wfa->tree[state][label]; /* append child */ |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
INPUT_BYTE_ALIGN (input); |
| 188 |
Free (queue); |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
decode_mc_coords (unsigned max_state, wfa_t *wfa, bitfile_t *input) |
| 193 |
/* |
| 194 |
* Read motion vector coordinates of the 'input' stream. They are stored |
| 195 |
* with the static Huffman code of the MPEG and H.263 standards. |
| 196 |
* 'max_state' is the last state with motion compensation infos. |
| 197 |
* |
| 198 |
* No return value. |
| 199 |
* |
| 200 |
* Side effects: |
| 201 |
* 'wfa->mv_tree' is filled with decoded values. |
| 202 |
*/ |
| 203 |
{ |
| 204 |
unsigned label; /* current label */ |
| 205 |
unsigned state; /* current state */ |
| 206 |
mv_t *mv; /* current motion vector */ |
| 207 |
static huff_node_t *huff_mv_root = NULL; /* root of huffman tree */ |
| 208 |
|
| 209 |
if (huff_mv_root == NULL) |
| 210 |
huff_mv_root = create_huff_tree (); |
| 211 |
|
| 212 |
for (state = wfa->basis_states; state < max_state; state++) |
| 213 |
for (label = 0; label < MAXLABELS; label++) |
| 214 |
{ |
| 215 |
mv = &wfa->mv_tree[state][label]; |
| 216 |
switch (mv->type) |
| 217 |
{ |
| 218 |
case NONE: |
| 219 |
break; |
| 220 |
case FORWARD: |
| 221 |
mv->fx = get_mv (1, huff_mv_root, input); |
| 222 |
mv->fy = get_mv (1, huff_mv_root, input); |
| 223 |
break; |
| 224 |
case BACKWARD: |
| 225 |
mv->bx = get_mv (1, huff_mv_root, input); |
| 226 |
mv->by = get_mv (1, huff_mv_root, input); |
| 227 |
break; |
| 228 |
case INTERPOLATED: |
| 229 |
mv->fx = get_mv (1, huff_mv_root, input); |
| 230 |
mv->fy = get_mv (1, huff_mv_root, input); |
| 231 |
mv->bx = get_mv (1, huff_mv_root, input); |
| 232 |
mv->by = get_mv (1, huff_mv_root, input); |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
INPUT_BYTE_ALIGN (input); |
| 238 |
} |
| 239 |
|
| 240 |
static int |
| 241 |
get_mv (int f_code, huff_node_t *hn, bitfile_t *input) |
| 242 |
/* |
| 243 |
* Decode next motion vector component in bitstream |
| 244 |
* by traversing the huffman tree. |
| 245 |
*/ |
| 246 |
{ |
| 247 |
int vlc_code, vlc_code_magnitude, residual, diffvec; |
| 248 |
|
| 249 |
while (hn->code_index < 0) |
| 250 |
{ |
| 251 |
if (hn->code_index == -2) |
| 252 |
error ("wrong huffman code !"); |
| 253 |
if (get_bit (input)) |
| 254 |
hn = hn->right; |
| 255 |
else |
| 256 |
hn = hn->left; |
| 257 |
} |
| 258 |
vlc_code = hn->code_index - 16; |
| 259 |
if (vlc_code == 0 || f_code == 1) |
| 260 |
return vlc_code; |
| 261 |
|
| 262 |
vlc_code_magnitude = abs (vlc_code) - 1; |
| 263 |
if (f_code <= 1) |
| 264 |
residual = 0; |
| 265 |
else |
| 266 |
residual = get_bits (input, f_code - 1); |
| 267 |
diffvec = (vlc_code_magnitude << (f_code - 1)) + residual + 1; |
| 268 |
|
| 269 |
return vlc_code > 0 ? diffvec : - diffvec; |
| 270 |
} |
| 271 |
|
| 272 |
static huff_node_t * |
| 273 |
create_huff_tree (void) |
| 274 |
/* |
| 275 |
* Construct huffman tree from code table |
| 276 |
*/ |
| 277 |
{ |
| 278 |
unsigned i; |
| 279 |
huff_node_t *huff_root = Calloc (1, sizeof (huff_node_t)); |
| 280 |
|
| 281 |
/* |
| 282 |
* The nodes' index set contains indices of all codewords that are |
| 283 |
* still decodable by traversing further down from the node. |
| 284 |
* (The root node has the full index set.) |
| 285 |
*/ |
| 286 |
|
| 287 |
for (i = 0; i < 33; i++) |
| 288 |
huff_root->index_set [i] = i; |
| 289 |
huff_root->index_set [i] = -1; /* end marker */ |
| 290 |
|
| 291 |
create_huff_node (huff_root, 0); |
| 292 |
|
| 293 |
return huff_root; |
| 294 |
} |
| 295 |
|
| 296 |
static void |
| 297 |
create_huff_node (huff_node_t *hn, int bits_processed) |
| 298 |
/* |
| 299 |
* Create one node in the huffman tree |
| 300 |
*/ |
| 301 |
{ |
| 302 |
int lind = 0; /* next index of left huff_node */ |
| 303 |
int rind = 0; /* next index of right huff_node */ |
| 304 |
int code_len, i, ind; |
| 305 |
|
| 306 |
hn->code_index = -1; |
| 307 |
if (hn->index_set [0] < 0) /* empty index set ? */ |
| 308 |
{ |
| 309 |
hn->code_index = -2; /* error */ |
| 310 |
return; |
| 311 |
} |
| 312 |
hn->left = Calloc (1, sizeof (huff_node_t)); |
| 313 |
hn->right = Calloc (1, sizeof (huff_node_t)); |
| 314 |
|
| 315 |
for (i = 0; (ind = hn->index_set[i]) >= 0; i++) |
| 316 |
{ |
| 317 |
code_len = mv_code_table[ind][1]; |
| 318 |
if (code_len == bits_processed) /* generate leaf */ |
| 319 |
{ |
| 320 |
hn->code_index = ind; |
| 321 |
Free (hn->left); |
| 322 |
Free (hn->right); |
| 323 |
return; |
| 324 |
} |
| 325 |
if (mv_code_table[ind][0] & (1 << (code_len - 1 - bits_processed))) |
| 326 |
hn->right->index_set[rind++] = ind; |
| 327 |
else |
| 328 |
hn->left->index_set[lind++] = ind; |
| 329 |
} |
| 330 |
hn->right->index_set[rind] = -1; /* set end markers */ |
| 331 |
hn->left->index_set[lind] = -1; |
| 332 |
create_huff_node (hn->left, bits_processed + 1); |
| 333 |
create_huff_node (hn->right, bits_processed + 1); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|