Debian Codegraph @

converter/other/fiasco/codec/mwfa.c

raw
1 /*
2 * mwfa.c: Initialization of MWFA coder
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:51 $
13 * $Author: hafner $
14 * $Revision: 5.1 $
15 * $State: Exp $
16 */
17
18 #include "config.h"
19
20 #include <ctype.h>
21 #include <string.h>
22
23 #include "pm_c_util.h"
24
25 #include "types.h"
26 #include "macros.h"
27 #include "error.h"
28
29 #include "misc.h"
30 #include "cwfa.h"
31 #include "image.h"
32 #include "mvcode.h"
33 #include "motion.h"
34 #include "mwfa.h"
35
36
37 static const unsigned local_range = 6;
38
39 /*****************************************************************************
40
41 prototypes
42
43 *****************************************************************************/
44
45 static void
46 get_mcpe (word_t *mcpe, const image_t *original,
47 unsigned x0, unsigned y0, unsigned width, unsigned height,
48 const word_t *mcblock1, const word_t *mcblock2);
49 static real_t
50 mcpe_norm (const image_t *original, unsigned x0, unsigned y0, unsigned width,
51 unsigned height, const word_t *mcblock1, const word_t *mcblock2);
52 static real_t
53 find_best_mv (real_t price, const image_t *original, const image_t *reference,
54 unsigned x0, unsigned y0, unsigned width, unsigned height,
55 real_t *bits, int *mx, int *my, const real_t *mc_norms,
56 const wfa_info_t *wi, const motion_t *mt);
57 static real_t
58 find_second_mv (real_t price, const image_t *original,
59 const image_t *reference, const word_t *mcblock1,
60 unsigned xr, unsigned yr, unsigned width, unsigned height,
61 real_t *bits, int *mx, int *my, const wfa_info_t *wi,
62 const motion_t *mt);
63
64 /*****************************************************************************
65
66 public code
67
68 *****************************************************************************/
69
70 motion_t *
71 alloc_motion (const wfa_info_t *wi)
72 /*
73 * Motion structure constructor.
74 * Allocate memory for the motion structure and
75 * fill in default values specified by 'wi'.
76 *
77 * Return value:
78 * pointer to the new option structure or NULL on error
79 */
80 {
81 int dx; /* motion vector coordinate */
82 unsigned level;
83 unsigned range_size = wi->half_pixel
84 ? square (wi->search_range)
85 : square (2 * wi->search_range);
86 motion_t *mt = Calloc (1, sizeof (motion_t));
87
88 mt->original = NULL;
89 mt->past = NULL;
90 mt->future = NULL;
91 mt->xbits = Calloc (2 * wi->search_range, sizeof (real_t));
92 mt->ybits = Calloc (2 * wi->search_range, sizeof (real_t));
93
94 for (dx = -wi->search_range; dx < (int) wi->search_range; dx++)
95 {
96 mt->xbits [dx + wi->search_range]
97 = mt->ybits [dx + wi->search_range]
98 = mv_code_table [dx + wi->search_range][1];
99 }
100
101 mt->mc_forward_norms = Calloc (MAXLEVEL, sizeof (real_t *));
102 mt->mc_backward_norms = Calloc (MAXLEVEL, sizeof (real_t *));
103
104 for (level = wi->p_min_level; level <= wi->p_max_level; level++)
105 {
106 mt->mc_forward_norms [level] = Calloc (range_size, sizeof (real_t));
107 mt->mc_backward_norms [level] = Calloc (range_size, sizeof (real_t));
108 }
109
110 return mt;
111 }
112
113 void
114 free_motion (motion_t *mt)
115 /*
116 * Motion struct destructor:
117 * Free memory of 'motion' struct.
118 *
119 * No return value.
120 *
121 * Side effects:
122 * structure 'motion' is discarded.
123 */
124 {
125 unsigned level;
126
127 Free (mt->xbits);
128 Free (mt->ybits);
129 for (level = 0; level < MAXLEVEL; level++)
130 {
131 if (mt->mc_forward_norms [level])
132 Free (mt->mc_forward_norms [level]);
133 if (mt->mc_backward_norms [level])
134 Free (mt->mc_backward_norms [level]);
135 }
136 Free (mt->mc_forward_norms);
137 Free (mt->mc_backward_norms);
138 Free (mt);
139 }
140
141 void
142 subtract_mc (image_t *image, const image_t *past, const image_t *future,
143 const wfa_t *wfa)
144 /*
145 * Subtract motion compensation from chrom channels of 'image'.
146 * Reference frames are given by 'past' and 'future'.
147 *
148 * No return values.
149 */
150 {
151 unsigned state, label;
152 word_t *mcblock1 = Calloc (size_of_level (wfa->wfainfo->p_max_level),
153 sizeof (word_t));
154 word_t *mcblock2 = Calloc (size_of_level (wfa->wfainfo->p_max_level),
155 sizeof (word_t));
156
157 for (state = wfa->basis_states; state < wfa->states; state++)
158 for (label = 0; label < MAXLABELS; label++)
159 if (wfa->mv_tree [state][label].type != NONE)
160 {
161 color_e band; /* current color band */
162 unsigned width, height; /* size of mcblock */
163 unsigned offset; /* remaining pixels in original */
164
165 width = width_of_level (wfa->level_of_state [state] - 1);
166 height = height_of_level (wfa->level_of_state [state] - 1);
167 offset = image->width - width;
168
169 switch (wfa->mv_tree [state][label].type)
170 {
171 case FORWARD:
172 for (band = first_band (image->color) + 1;
173 band <= last_band (image->color); band++)
174 {
175 unsigned y; /* row of block */
176 word_t *mc1; /* pixel in MC block 1 */
177 word_t *orig; /* pixel in original image */
178
179 extract_mc_block (mcblock1, width, height,
180 past->pixels [band], past->width,
181 wfa->wfainfo->half_pixel,
182 wfa->x [state][label],
183 wfa->y [state][label],
184 (wfa->mv_tree [state][label].fx / 2)
185 * 2,
186 (wfa->mv_tree [state][label].fy / 2)
187 * 2);
188 mc1 = mcblock1;
189 orig = image->pixels [band] + wfa->x [state][label]
190 + wfa->y [state][label] * image->width;
191
192 for (y = height; y; y--)
193 {
194 unsigned x; /* column of block */
195
196 for (x = width; x; x--)
197 *orig++ -= *mc1++;
198
199 orig += offset;
200 }
201 }
202 break;
203 case BACKWARD:
204 for (band = first_band (image->color) + 1;
205 band <= last_band (image->color); band++)
206 {
207 unsigned y; /* row of block */
208 word_t *mc1; /* pixel in MC block 1 */
209 word_t *orig; /* pixel in original image */
210
211 extract_mc_block (mcblock1, width, height,
212 future->pixels [band], future->width,
213 wfa->wfainfo->half_pixel,
214 wfa->x [state][label],
215 wfa->y [state][label],
216 (wfa->mv_tree [state][label].bx / 2)
217 * 2,
218 (wfa->mv_tree [state][label].by / 2)
219 * 2);
220 mc1 = mcblock1;
221 orig = image->pixels [band] + wfa->x [state][label]
222 + wfa->y [state][label] * image->width;
223
224 for (y = height; y; y--)
225 {
226 unsigned x; /* column of block */
227
228 for (x = width; x; x--)
229 *orig++ -= *mc1++;
230
231 orig += offset;
232 }
233 }
234 break;
235 case INTERPOLATED:
236 for (band = first_band (image->color) + 1;
237 band <= last_band (image->color); band++)
238 {
239 unsigned y; /* row of block */
240 word_t *mc1; /* pixel in MC block 1 */
241 word_t *mc2; /* pixel in MC block 2 */
242 word_t *orig; /* pixel in original image */
243
244 extract_mc_block (mcblock1, width, height,
245 past->pixels [band], past->width,
246 wfa->wfainfo->half_pixel,
247 wfa->x [state][label],
248 wfa->y [state][label],
249 (wfa->mv_tree[state][label].fx / 2)
250 * 2,
251 (wfa->mv_tree[state][label].fy / 2)
252 * 2);
253 extract_mc_block (mcblock2, width, height,
254 future->pixels [band], future->width,
255 wfa->wfainfo->half_pixel,
256 wfa->x [state][label],
257 wfa->y [state][label],
258 (wfa->mv_tree[state][label].bx / 2)
259 * 2,
260 (wfa->mv_tree[state][label].by / 2)
261 * 2);
262 mc1 = mcblock1;
263 mc2 = mcblock2;
264 orig = image->pixels [band] + wfa->x [state][label]
265 + wfa->y [state][label] * image->width;
266
267 for (y = height; y; y--)
268 {
269 unsigned x; /* column of block */
270
271 for (x = width; x; x--)
272 *orig++ -= (*mc1++ + *mc2++) / 2;
273
274 orig += offset;
275 }
276 }
277 break;
278 default:
279 break;
280 }
281 }
282
283 Free (mcblock1);
284 Free (mcblock2);
285 }
286
287 void
288 find_P_frame_mc (word_t *mcpe, real_t price, range_t *range,
289 const wfa_info_t *wi, const motion_t *mt)
290 /*
291 * Determine best motion vector for P-frame.
292 *
293 * No return value.
294 *
295 * Side effects:
296 * range->mvt_bits (# of mv-tree bits)
297 * range->mvxybits (# of bits for vector components)
298 * mt->mcpe (MCPE in scan-order)
299 */
300 {
301 unsigned width = width_of_level (range->level);
302 unsigned height = height_of_level (range->level);
303 word_t *mcblock = Calloc (width * height, sizeof (word_t));
304
305 range->mv_tree_bits = 1;
306 range->mv.type = FORWARD;
307
308 /*
309 * Find best matching forward prediction
310 */
311 find_best_mv (price, mt->original, mt->past, range->x, range->y,
312 width, height, &range->mv_coord_bits, &range->mv.fx,
313 &range->mv.fy, mt->mc_forward_norms [range->level], wi, mt);
314
315 /*
316 * Compute MCPE
317 */
318 extract_mc_block (mcblock, width, height, mt->past->pixels [GRAY],
319 mt->past->width, wi->half_pixel, range->x, range->y,
320 range->mv.fx, range->mv.fy);
321 get_mcpe (mcpe, mt->original, range->x, range->y, width, height,
322 mcblock, NULL);
323
324 Free (mcblock);
325 }
326
327 void
328 find_B_frame_mc (word_t *mcpe, real_t price, range_t *range,
329 const wfa_info_t *wi, const motion_t *mt)
330 /*
331 * Determines best motion compensation for B-frame.
332 * Steps:
333 * 1) find best forward motion vector
334 * 2) find best backward motion vector
335 * 3) try both motion vectors together (interpolation)
336 * 4) choose best mode (FORWARD, BACKWARD or INTERPOLATED)
337 * Bitcodes:
338 * FORWARD 000
339 * BACKWARD 001
340 * INTERPOLATED 01
341 *
342 * Return values:
343 * range->mvt_bits (# of mv-tree bits)
344 * range->mvxybits (# of bits for vector components)
345 * mt->mcpe (MCPE in scan-order)
346 */
347 {
348 mc_type_e mctype; /* type of motion compensation */
349 real_t forward_costs; /* costs of FORWARD mc */
350 real_t backward_costs; /* costs of BACKWARD mc */
351 real_t interp_costs; /* costs of INTERPOLATED mc */
352 real_t forward_bits; /* bits for FORWARD mc */
353 real_t backward_bits; /* bits for BACKWARD mc */
354 real_t interp_bits; /* bits for INTERPOLATED mc */
355 int fx, fy; /* coordinates FORWARD mc */
356 int bx, by; /* coordinates BACKWARD mc */
357 int ifx, ify; /* coordinates forw. INTERPOLATED mc */
358 int ibx, iby; /* coordinates back. INTERPOLATED mc */
359 unsigned width = width_of_level (range->level);
360 unsigned height = height_of_level (range->level);
361 word_t *mcblock1 = Calloc (width * height, sizeof (word_t));
362 word_t *mcblock2 = Calloc (width * height, sizeof (word_t));
363
364 /*
365 * Forward interpolation: use past frame as reference
366 */
367 forward_costs = find_best_mv (price, mt->original, mt->past,
368 range->x, range->y, width, height,
369 &forward_bits, &fx, &fy,
370 mt->mc_forward_norms [range->level], wi, mt)
371 + 3 * price; /* code 000 */
372
373 /*
374 * Backward interpolation: use future frame as reference
375 */
376 backward_costs = find_best_mv (price, mt->original, mt->future,
377 range->x, range->y, width, height,
378 &backward_bits, &bx, &by,
379 mt->mc_backward_norms [range->level], wi, mt)
380 + 3 * price; /* code 001 */
381
382 /*
383 * Bidirectional interpolation: use both past and future frame as reference
384 */
385 if (wi->cross_B_search)
386 {
387 real_t icosts1; /* costs interpolation alternative 1 */
388 real_t icosts2; /* costs interpolation alternative 2 */
389 real_t ibackward_bits; /* additional bits alternative 1 */
390 real_t iforward_bits; /* additional bits alternative 1 */
391
392 /*
393 * Alternative 1: keep forward mv and vary backward mv locally
394 */
395 extract_mc_block (mcblock1, width, height, mt->past->pixels [GRAY],
396 mt->past->width, wi->half_pixel,
397 range->x, range->y, fx, fy);
398
399 ibx = bx; /* start with backward coordinates */
400 iby = by;
401 icosts1 = find_second_mv (price, mt->original, mt->future,
402 mcblock1, range->x, range->y, width, height,
403 &ibackward_bits, &ibx, &iby, wi, mt)
404 + (forward_bits + 2) * price; /* code 01 */
405
406 /*
407 * Alternative 2: Keep backward mv and vary forward mv locally
408 */
409 extract_mc_block (mcblock1, width, height, mt->future->pixels [GRAY],
410 mt->future->width, wi->half_pixel,
411 range->x, range->y, bx, by);
412
413 ifx = fx;
414 ify = fy;
415 icosts2 = find_second_mv (price, mt->original, mt->past,
416 mcblock1, range->x, range->y, width, height,
417 &iforward_bits, &ifx, &ify, wi, mt)
418 + (backward_bits + 2) * price; /* code 01 */
419
420 /*
421 * Choose best alternative
422 */
423 if (icosts1 < icosts2)
424 {
425 ifx = fx;
426 ify = fy;
427 interp_bits = forward_bits + ibackward_bits;
428 interp_costs = icosts1;
429 }
430 else
431 {
432 ibx = bx;
433 iby = by;
434 interp_bits = iforward_bits + backward_bits;
435 interp_costs = icosts2;
436 }
437 }
438 else /* local exhaustive search */
439 {
440 /*
441 * Keep forward and backward mv because of time constraints
442 */
443
444 ifx = fx;
445 ify = fy;
446 ibx = bx;
447 iby = by;
448 interp_bits = forward_bits + backward_bits;
449
450 extract_mc_block (mcblock1, width, height, mt->past->pixels [GRAY],
451 mt->past->width, wi->half_pixel,
452 range->x, range->y, fx, fy);
453 extract_mc_block (mcblock2, width, height, mt->future->pixels [GRAY],
454 mt->future->width, wi->half_pixel,
455 range->x, range->y, bx, by);
456 interp_costs = mcpe_norm (mt->original, range->x, range->y,
457 width, height, mcblock1, mcblock2)
458 + (interp_bits + 2) * price; /* code 01 */
459 }
460
461 /*
462 * Choose alternative with smallest costs
463 */
464 if (forward_costs <= interp_costs)
465 {
466 if (forward_costs <= backward_costs)
467 mctype = FORWARD;
468 else
469 mctype = BACKWARD;
470 }
471 else
472 {
473 if (backward_costs <= interp_costs)
474 mctype = BACKWARD;
475 else
476 mctype = INTERPOLATED;
477 }
478
479 switch (mctype)
480 {
481 case FORWARD:
482 range->mv_tree_bits = 3;
483 range->mv_coord_bits = forward_bits;
484 range->mv.type = FORWARD;
485 range->mv.fx = fx;
486 range->mv.fy = fy;
487 extract_mc_block (mcblock1, width, height, mt->past->pixels [GRAY],
488 mt->past->width, wi->half_pixel,
489 range->x, range->y, range->mv.fx, range->mv.fy);
490 get_mcpe (mcpe, mt->original, range->x, range->y, width, height,
491 mcblock1, NULL);
492 break;
493 case BACKWARD:
494 range->mv_tree_bits = 3;
495 range->mv_coord_bits = backward_bits;
496 range->mv.type = BACKWARD;
497 range->mv.bx = bx;
498 range->mv.by = by;
499 extract_mc_block (mcblock1, width, height, mt->future->pixels [GRAY],
500 mt->future->width, wi->half_pixel,
501 range->x, range->y, range->mv.bx, range->mv.by);
502 get_mcpe (mcpe, mt->original, range->x, range->y, width, height,
503 mcblock1, NULL);
504 break;
505 case INTERPOLATED:
506 range->mv_tree_bits = 2;
507 range->mv_coord_bits = interp_bits;
508 range->mv.type = INTERPOLATED;
509 range->mv.fx = ifx;
510 range->mv.fy = ify;
511 range->mv.bx = ibx;
512 range->mv.by = iby;
513 extract_mc_block (mcblock1, width, height, mt->past->pixels [GRAY],
514 mt->past->width, wi->half_pixel,
515 range->x, range->y, range->mv.fx, range->mv.fy);
516 extract_mc_block (mcblock2, width, height, mt->future->pixels [GRAY],
517 mt->future->width, wi->half_pixel,
518 range->x, range->y, range->mv.bx, range->mv.by);
519 get_mcpe (mcpe, mt->original, range->x, range->y, width, height,
520 mcblock1, mcblock2);
521 break;
522 default:
523 break;
524 }
525
526 Free (mcblock1);
527 Free (mcblock2);
528 }
529
530 void
531 fill_norms_table (unsigned x0, unsigned y0, unsigned level,
532 const wfa_info_t *wi, motion_t *mt)
533 /*
534 * Compute norms of difference images for all possible displacements
535 * in 'mc_forward_norm' and 'mc_backward_norm'.
536 *
537 * No return value.
538 *
539 * Side effects:
540 * 'mt->mc_backward_norms' are computed
541 * 'mt->mc_forward_norms' are computed
542 */
543 {
544 int mx, my; /* coordinates of motion vector */
545 unsigned sr; /* mv search range +-'sr' pixels */
546 unsigned index = 0; /* index of motion vector */
547 unsigned width = width_of_level (level);
548 unsigned height = height_of_level (level);
549 word_t *mcblock = Calloc (width * height, sizeof (word_t));
550
551 sr = wi->half_pixel ? wi->search_range / 2 : wi->search_range;
552
553 for (my = -sr; my < (int) sr; my++)
554 for (mx = -sr; mx < (int) sr; mx++, index++)
555 {
556 if ((int) x0 + mx < 0 || /* block outside visible area */
557 x0 + mx + width > mt->original->width ||
558 (int) y0 + my < 0 ||
559 y0 + my + height > mt->original->height)
560 {
561 mt->mc_forward_norms [level][index] = 0.0;
562 mt->mc_backward_norms [level][index] = 0.0;
563 }
564 else
565 {
566 extract_mc_block (mcblock, width, height, mt->past->pixels [GRAY],
567 mt->past->width, wi->half_pixel,
568 x0, y0, mx, my);
569 mt->mc_forward_norms [level][index]
570 = mcpe_norm (mt->original, x0, y0, width, height,
571 mcblock, NULL);
572
573 if (mt->frame_type == B_FRAME)
574 {
575 extract_mc_block (mcblock, width, height,
576 mt->future->pixels [GRAY],
577 mt->future->width, wi->half_pixel,
578 x0, y0, mx, my);
579 mt->mc_backward_norms[level][index]
580 = mcpe_norm (mt->original, x0, y0, width, height,
581 mcblock, NULL);
582 }
583 }
584 }
585
586 Free (mcblock);
587 }
588
589 /*****************************************************************************
590
591 private code
592
593 *****************************************************************************/
594
595 static void
596 get_mcpe (word_t *mcpe, const image_t *original, unsigned x0, unsigned y0,
597 unsigned width, unsigned height, const word_t *mcblock1,
598 const word_t *mcblock2)
599 /*
600 * Compute MCPE image 'original' - reference. The reference is either
601 * composed of 'mcblock1' or of ('mcblock1' + 'mcblock2') / 2 (if
602 * 'mcblock2' != NULL). Coordinates of original block are given by
603 * 'x0', 'y0', 'width', and 'height'.
604 *
605 * No return value.
606 *
607 * Side effects:
608 * 'mcpe []' is filled with the delta image
609 */
610 {
611 const word_t *oblock; /* pointer to original image */
612
613 assert (mcpe);
614
615 oblock = original->pixels [GRAY] + y0 * original->width + x0;
616
617 if (mcblock2 != NULL) /* interpolated prediction */
618 {
619 unsigned x, y; /* current coordinates */
620
621 for (y = height; y; y--)
622 {
623 for (x = width; x; x--)
624 *mcpe++ = *oblock++ - (*mcblock1++ + *mcblock2++) / 2;
625
626 oblock += original->width - width;
627 }
628 }
629 else /* forward or backward prediction */
630 {
631 unsigned x, y; /* current coordinates */
632
633 for (y = height; y; y--)
634 {
635 for (x = width; x; x--)
636 *mcpe++ = *oblock++ - *mcblock1++;
637
638 oblock += original->width - width;
639 }
640 }
641 }
642
643 static real_t
644 mcpe_norm (const image_t *original, unsigned x0, unsigned y0, unsigned width,
645 unsigned height, const word_t *mcblock1, const word_t *mcblock2)
646 /*
647 * Compute norm of motion compensation prediction error.
648 * Coordinates of 'original' block are given by ('x0', 'y0')
649 * and 'width', 'height'.
650 * Reference blocks are stored in 'mcimage1' and 'mcimage2'.
651 *
652 * Return value:
653 * square of norm of difference image
654 */
655 {
656 unsigned n;
657 real_t norm = 0;
658 word_t *mcpe = Calloc (width * height, sizeof (word_t));
659 word_t *ptr = mcpe;
660
661 get_mcpe (mcpe, original, x0, y0, width, height, mcblock1, mcblock2);
662
663 for (n = height * width; n; n--, ptr++)
664 norm += square (*ptr / 16);
665
666 Free (mcpe);
667
668 return norm;
669 }
670
671 static real_t
672 find_best_mv (real_t price, const image_t *original, const image_t *reference,
673 unsigned x0, unsigned y0, unsigned width, unsigned height,
674 real_t *bits, int *mx, int *my, const real_t *mc_norms,
675 const wfa_info_t *wi, const motion_t *mt)
676 /*
677 * Find best matching motion vector in image 'reference' to predict
678 * the block ('x0', 'y0') of size 'width'x'height in image 'original'.
679 *
680 * Return values:
681 * prediction costs
682 *
683 * Side effects:
684 * 'mx', 'my' coordinates of motion vector
685 * 'bits' number of bits to encode mv
686 */
687 {
688 unsigned sr; /* mv search range +/- 'sr' pixels */
689 unsigned index; /* index of motion vector */
690 int x, y; /* coordinates of motion vector */
691 real_t costs; /* costs arising if mv is chosen */
692 real_t mincosts = MAXCOSTS; /* best costs so far */
693 unsigned bitshift; /* half_pixel coordinates multiplier */
694
695 *mx = *my = 0;
696
697 /*
698 * Find best fitting motion vector:
699 * Use exhaustive search in the interval x,y +- sr (no halfpixel accuracy)
700 * or x,y +- sr/2 (halfpixel accuracy)
701 */
702 sr = wi->half_pixel ? wi->search_range / 2 : wi->search_range;
703 bitshift = (wi->half_pixel ? 2 : 1); /* bit0 reserved for halfpixel pred. */
704
705 for (index = 0, y = -sr; y < (int) sr; y++)
706 for (x = -sr; x < (int) sr; x++, index++)
707 if ((int) x0 + x >= 0 && (int) y0 + y >= 0 &&
708 x0 + x + width <= original->width &&
709 y0 + y + height <= original->height)
710 {
711 /*
712 * Block is inside visible area.
713 * Compare current costs with 'mincosts'
714 */
715 costs = mc_norms [index]
716 + (mt->xbits [(x + sr) * bitshift]
717 + mt->ybits [(y + sr) * bitshift]) * price;
718
719 if (costs < mincosts)
720 {
721 mincosts = costs;
722 *mx = x * bitshift;
723 *my = y * bitshift;
724 }
725 }
726
727 /*
728 * Halfpixel prediction:
729 * Compare all nine combinations (-1, y), (0, y), (+1, y) for y = -1,0,+1
730 */
731 if (wi->half_pixel)
732 {
733 int rx, ry; /* halfpixel refinement */
734 unsigned bestrx, bestry; /* coordinates of best mv */
735 word_t *mcblock = Calloc (width * height, sizeof (word_t));
736
737 bestrx = bestry = 0;
738 for (rx = -1; rx <= 1; rx++)
739 for (ry = -1; ry <= 1; ry++)
740 {
741 /*
742 * Check if the new motion vector is in allowed area
743 */
744 if (rx == 0 && ry == 0) /* already tested */
745 continue;
746 if ((int) x0 + (*mx / 2) + rx < 0 || /* outside visible area */
747 x0 + (*mx / 2) + rx + width > original->width ||
748 (int) y0 + (*my / 2) + ry < 0 ||
749 y0 + (*my / 2) + ry + height > original->height)
750 continue;
751 if (*mx + rx < (int) -sr || *mx + rx >= (int) sr ||
752 *my + ry < (int) -sr || *my + ry >= (int) sr)
753 continue; /* out of bounds */
754
755 /*
756 * Compute costs of new motion compensation
757 */
758 extract_mc_block (mcblock, width, height,
759 reference->pixels [GRAY],
760 reference->width, wi->half_pixel,
761 x0, y0, *mx + rx, *my + ry);
762 costs = mcpe_norm (mt->original, x0, y0, width, height, mcblock,
763 NULL)
764 + (mt->xbits [*mx + rx + sr * bitshift]
765 + mt->ybits [*my + ry + sr * bitshift]) * price;
766 if (costs < mincosts)
767 {
768 bestrx = rx;
769 bestry = ry;
770 mincosts = costs;
771 }
772 }
773
774 *mx += bestrx;
775 *my += bestry;
776
777 Free (mcblock);
778 } /* halfpixel */
779
780 *bits = mt->xbits [*mx + sr * bitshift] + mt->ybits [*my + sr * bitshift];
781
782 return mincosts;
783 }
784
785 static real_t
786 find_second_mv (real_t price, const image_t *original,
787 const image_t *reference, const word_t *mcblock1,
788 unsigned xr, unsigned yr, unsigned width, unsigned height,
789 real_t *bits, int *mx, int *my, const wfa_info_t *wi,
790 const motion_t *mt)
791 /*
792 * Search local area (*mx,*my) for best additional mv.
793 * Overwrite mt->tmpblock.
794 * TODO check sr = search_range
795 *
796 * Return values:
797 * prediction costs
798 *
799 * Side effects:
800 * 'mx','my' coordinates of mv
801 * 'bits' number of bits to encode mv
802 */
803 {
804 real_t mincosts = MAXCOSTS; /* best costs so far */
805 unsigned sr; /* MV search range +/- 'sr' pixels */
806 int x, y; /* coordinates of motion vector */
807 int y0, y1, x0, x1; /* start/end coord. of search range */
808 unsigned bitshift; /* half_pixel coordinates multiplier */
809 word_t *mcblock2 = Calloc (width * height, sizeof (word_t));
810
811 sr = wi->search_range;
812
813 y0 = MAX((int) -sr, *my - (int) local_range);
814 y1 = MIN((int) sr, *my + (int) local_range);
815 x0 = MAX((int) -sr, *mx - (int) local_range);
816 x1 = MIN((int) sr, *mx + (int) local_range);
817
818 *mx = *my = 0;
819
820 bitshift = (wi->half_pixel ? 2 : 1); /* bit0 reserved for halfpixel pred. */
821
822
823 for (y = y0; y < y1; y++)
824 for (x = x0; x < x1; x++)
825 {
826 real_t costs; /* costs arising if mv is chosen */
827
828 /*
829 * Test each mv ('x', 'y') in the given search range:
830 * Get the new motion compensation image from 'reference' and compute
831 * the norm of the motion compensation prediction error
832 * 'original' - 0.5 * ('firstmc' + 'reference')
833 */
834 if ((int) (xr * bitshift) + x < 0 || /* outside visible area */
835 xr * bitshift + x > (original->width - width) * bitshift ||
836 (int) (yr * bitshift) + y < 0 ||
837 yr * bitshift + y > (original->height - height) * bitshift)
838 continue;
839
840 extract_mc_block (mcblock2, width, height,
841 reference->pixels [GRAY], reference->width,
842 wi->half_pixel, x0, y0, x, y);
843
844 costs = mcpe_norm (mt->original, x0, y0, width, height,
845 mcblock1, mcblock2)
846 + (mt->xbits [x + sr] + mt->ybits [y + sr]) * price;
847
848 if (costs < mincosts)
849 {
850 mincosts = costs;
851 *mx = x;
852 *my = y;
853 }
854 }
855
856 *bits = mt->xbits [*mx + sr] + mt->ybits [*my + sr];
857
858 Free (mcblock2);
859
860 return mincosts;
861 }