1 |
/***************************************************************************** |
// 30.10.2002 corrected qpel chroma rounding |
2 |
* |
// 04.10.2002 added qpel support to MBMotionCompensation |
3 |
* XVID MPEG-4 VIDEO CODEC |
// 01.05.2002 updated MBMotionCompensationBVOP |
4 |
* - Motion Compensation related code - |
// 14.04.2002 bframe compensation |
|
* |
|
|
* Copyright(C) 2002 Peter Ross <pross@xvid.org> |
|
|
* 2003 Christoph Lampert <gruel@web.de> |
|
|
* |
|
|
* This program is free software ; you can redistribute it and/or modify |
|
|
* it under the terms of the GNU General Public License as published by |
|
|
* the Free Software Foundation ; either version 2 of the License, or |
|
|
* (at your option) any later version. |
|
|
* |
|
|
* This program is distributed in the hope that it will be useful, |
|
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
* GNU General Public License for more details. |
|
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with this program ; if not, write to the Free Software |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
|
* |
|
|
* $Id$ |
|
|
* |
|
|
****************************************************************************/ |
|
5 |
|
|
6 |
#include <stdio.h> |
#include <stdio.h> |
7 |
|
|
8 |
#include "../encoder.h" |
#include "../encoder.h" |
9 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
10 |
#include "../image/interpolate8x8.h" |
#include "../image/interpolate8x8.h" |
|
#include "../image/qpel.h" |
|
11 |
#include "../image/reduced.h" |
#include "../image/reduced.h" |
12 |
#include "../utils/timer.h" |
#include "../utils/timer.h" |
13 |
#include "motion.h" |
#include "motion.h" |
14 |
|
|
15 |
|
#ifndef ABS |
16 |
|
#define ABS(X) (((X)>0)?(X):-(X)) |
17 |
|
#endif |
18 |
|
#ifndef SIGN |
19 |
|
#define SIGN(X) (((X)>0)?1:-1) |
20 |
|
#endif |
21 |
|
|
22 |
#ifndef RSHIFT |
#ifndef RSHIFT |
23 |
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) |
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) |
24 |
#endif |
#endif |
29 |
#endif |
#endif |
30 |
|
|
31 |
|
|
32 |
/* This is borrowed from bitstream.c until we find a common solution */ |
/* This is borrowed from decoder.c */ |
33 |
|
static __inline int gmc_sanitize(int value, int quarterpel, int fcode) |
34 |
|
{ |
35 |
|
int length = 1 << (fcode+4); |
36 |
|
|
37 |
|
// if (quarterpel) value *= 2; |
38 |
|
|
39 |
|
if (value < -length) |
40 |
|
return -length; |
41 |
|
else if (value >= length) |
42 |
|
return length-1; |
43 |
|
else return value; |
44 |
|
} |
45 |
|
|
46 |
|
/* And this is borrowed from bitstream.c until we find a common solution */ |
47 |
|
|
48 |
static uint32_t __inline |
static uint32_t __inline |
49 |
log2bin(uint32_t value) |
log2bin(uint32_t value) |
89 |
|
|
90 |
if(quarterpel) { |
if(quarterpel) { |
91 |
if ((dx&3) | (dy&3)) { |
if ((dx&3) | (dy&3)) { |
|
#if defined(ARCH_IS_IA32) /* new_interpolate is only faster on x86 (MMX) machines */ |
|
|
new_interpolate16x16_quarterpel(tmp - y * stride - x, |
|
|
(uint8_t *) ref, tmp + 32, |
|
|
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
|
|
#else |
|
92 |
interpolate16x16_quarterpel(tmp - y * stride - x, |
interpolate16x16_quarterpel(tmp - y * stride - x, |
93 |
(uint8_t *) ref, tmp + 32, |
(uint8_t *) ref, tmp + 32, |
94 |
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
|
#endif |
|
95 |
ptr = tmp; |
ptr = tmp; |
96 |
} else ptr = ref + (y + dy/4)*stride + x + dx/4; /* fullpixel position */ |
} else ptr = ref + (y + dy/4)*stride + x + dx/4; // fullpixel position |
97 |
|
|
98 |
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
99 |
|
|
106 |
transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8, |
transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8, |
107 |
ptr + 8*stride + 8, stride); |
ptr + 8*stride + 8, stride); |
108 |
|
|
109 |
} else { /* reduced_resolution */ |
} else { //reduced_resolution |
110 |
|
|
111 |
x *= 2; y *= 2; |
x *= 2; y *= 2; |
112 |
|
|
151 |
|
|
152 |
if(quarterpel) { |
if(quarterpel) { |
153 |
if ((dx&3) | (dy&3)) { |
if ((dx&3) | (dy&3)) { |
|
#if defined(ARCH_IS_IA32) /* new_interpolate is only faster on x86 (MMX) machines */ |
|
|
new_interpolate8x8_quarterpel(tmp - y*stride - x, |
|
|
(uint8_t *) ref, tmp + 32, |
|
|
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
|
|
#else |
|
154 |
interpolate8x8_quarterpel(tmp - y*stride - x, |
interpolate8x8_quarterpel(tmp - y*stride - x, |
155 |
(uint8_t *) ref, tmp + 32, |
(uint8_t *) ref, tmp + 32, |
156 |
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
|
#endif |
|
157 |
ptr = tmp; |
ptr = tmp; |
158 |
} else ptr = ref + (y + dy/4)*stride + x + dx/4; /* fullpixel position */ |
} else ptr = ref + (y + dy/4)*stride + x + dx/4; // fullpixel position |
159 |
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
160 |
|
|
161 |
transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride); |
transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride); |
162 |
|
|
163 |
} else { /* reduced_resolution */ |
} else { //reduced_resolution |
164 |
|
|
165 |
x *= 2; y *= 2; |
x *= 2; y *= 2; |
166 |
|
|
323 |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
324 |
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
325 |
|
|
326 |
} else { /* mode == MODE_INTER4V */ |
} else { // mode == MODE_INTER4V |
327 |
int k, sumx = 0, sumy = 0; |
int k, sumx = 0, sumy = 0; |
328 |
const VECTOR * const mvs = (quarterpel ? mb->qmvs : mb->mvs); |
const VECTOR * const mvs = (quarterpel ? mb->qmvs : mb->mvs); |
329 |
|
|
370 |
const uint32_t edged_width = pParam->edged_width; |
const uint32_t edged_width = pParam->edged_width; |
371 |
int32_t dx, dy, b_dx, b_dy, sumx, sumy, b_sumx, b_sumy; |
int32_t dx, dy, b_dx, b_dy, sumx, sumy, b_sumx, b_sumy; |
372 |
int k; |
int k; |
373 |
const int quarterpel = pParam->vol_flags & XVID_VOL_QUARTERPEL; |
const int quarterpel = pParam->m_quarterpel; |
374 |
const uint8_t * ptr1, * ptr2; |
const uint8_t * ptr1, * ptr2; |
375 |
uint8_t * const tmp = f_refv->u; |
uint8_t * const tmp = f_refv->u; |
376 |
const VECTOR * const fmvs = (quarterpel ? mb->qmvs : mb->mvs); |
const VECTOR * const fmvs = (quarterpel ? mb->qmvs : mb->mvs); |
421 |
(uint8_t *) f_ref->y, tmp + 32, |
(uint8_t *) f_ref->y, tmp + 32, |
422 |
tmp + 64, tmp + 96, 16*i, 16*j, dx, dy, edged_width, 0); |
tmp + 64, tmp + 96, 16*i, 16*j, dx, dy, edged_width, 0); |
423 |
ptr1 = tmp; |
ptr1 = tmp; |
424 |
} else ptr1 = f_ref->y + (16*j + dy/4)*edged_width + 16*i + dx/4; /* fullpixel position */ |
} else ptr1 = f_ref->y + (16*j + dy/4)*edged_width + 16*i + dx/4; // fullpixel position |
425 |
|
|
426 |
if ((b_dx&3) | (b_dy&3)) { |
if ((b_dx&3) | (b_dy&3)) { |
427 |
interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width + 16, |
interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width + 16, |
428 |
(uint8_t *) b_ref->y, tmp + 32, |
(uint8_t *) b_ref->y, tmp + 32, |
429 |
tmp + 64, tmp + 96, 16*i, 16*j, b_dx, b_dy, edged_width, 0); |
tmp + 64, tmp + 96, 16*i, 16*j, b_dx, b_dy, edged_width, 0); |
430 |
ptr2 = tmp + 16; |
ptr2 = tmp + 16; |
431 |
} else ptr2 = b_ref->y + (16*j + b_dy/4)*edged_width + 16*i + b_dx/4; /* fullpixel position */ |
} else ptr2 = b_ref->y + (16*j + b_dy/4)*edged_width + 16*i + b_dx/4; // fullpixel position |
432 |
|
|
433 |
b_dx /= 2; |
b_dx /= 2; |
434 |
b_dy /= 2; |
b_dy /= 2; |
457 |
|
|
458 |
break; |
break; |
459 |
|
|
460 |
default: /* MODE_DIRECT (or MODE_DIRECT_NONE_MV in case of bframes decoding) */ |
default: // MODE_DIRECT (or MODE_DIRECT_NONE_MV in case of bframes decoding) |
461 |
sumx = sumy = b_sumx = b_sumy = 0; |
sumx = sumy = b_sumx = b_sumy = 0; |
462 |
|
|
463 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
507 |
break; |
break; |
508 |
} |
} |
509 |
|
|
510 |
/* v block-based chroma interpolation for direct and interpolate modes */ |
// uv block-based chroma interpolation for direct and interpolate modes |
511 |
transfer_8to16sub2(&dct_codes[4 * 64], |
transfer_8to16sub2(&dct_codes[4 * 64], |
512 |
cur->u + (j * 8) * edged_width / 2 + (i * 8), |
cur->u + (j * 8) * edged_width / 2 + (i * 8), |
513 |
interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j, |
interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j, |
524 |
dx, dy, edged_width / 2, 0), |
dx, dy, edged_width / 2, 0), |
525 |
edged_width / 2); |
edged_width / 2); |
526 |
} |
} |
527 |
|
|
528 |
|
|
529 |
|
|
530 |
|
void generate_GMCparameters( const int num_wp, const int res, |
531 |
|
const WARPPOINTS *const warp, |
532 |
|
const int width, const int height, |
533 |
|
GMC_DATA *const gmc) |
534 |
|
{ |
535 |
|
const int du0 = warp->duv[0].x; |
536 |
|
const int dv0 = warp->duv[0].y; |
537 |
|
const int du1 = warp->duv[1].x; |
538 |
|
const int dv1 = warp->duv[1].y; |
539 |
|
const int du2 = warp->duv[2].x; |
540 |
|
const int dv2 = warp->duv[2].y; |
541 |
|
|
542 |
|
gmc->W = width; |
543 |
|
gmc->H = height; |
544 |
|
|
545 |
|
gmc->rho = 4 - log2bin(res-1); // = {3,2,1,0} for res={2,4,8,16} |
546 |
|
|
547 |
|
gmc->alpha = log2bin(gmc->W-1); |
548 |
|
gmc->Ws = (1 << gmc->alpha); |
549 |
|
|
550 |
|
gmc->dxF = 16*gmc->Ws + RDIV( 8*gmc->Ws*du1, gmc->W ); |
551 |
|
gmc->dxG = RDIV( 8*gmc->Ws*dv1, gmc->W ); |
552 |
|
gmc->Fo = (res*du0 + 1) << (gmc->alpha+gmc->rho-1); |
553 |
|
gmc->Go = (res*dv0 + 1) << (gmc->alpha+gmc->rho-1); |
554 |
|
|
555 |
|
if (num_wp==2) { |
556 |
|
gmc->dyF = -gmc->dxG; |
557 |
|
gmc->dyG = gmc->dxF; |
558 |
|
} else if (num_wp==3) { |
559 |
|
gmc->beta = log2bin(gmc->H-1); |
560 |
|
gmc->Hs = (1 << gmc->beta); |
561 |
|
gmc->dyF = RDIV( 8*gmc->Hs*du2, gmc->H ); |
562 |
|
gmc->dyG = 16*gmc->Hs + RDIV( 8*gmc->Hs*dv2, gmc->H ); |
563 |
|
if (gmc->beta > gmc->alpha) { |
564 |
|
gmc->dxF <<= (gmc->beta - gmc->alpha); |
565 |
|
gmc->dxG <<= (gmc->beta - gmc->alpha); |
566 |
|
gmc->alpha = gmc->beta; |
567 |
|
gmc->Ws = 1<< gmc->beta; |
568 |
|
} else { |
569 |
|
gmc->dyF <<= gmc->alpha - gmc->beta; |
570 |
|
gmc->dyG <<= gmc->alpha - gmc->beta; |
571 |
|
} |
572 |
|
} |
573 |
|
|
574 |
|
gmc->cFo = gmc->dxF + gmc->dyF + (1 << (gmc->alpha+gmc->rho+1)); |
575 |
|
gmc->cFo += 16*gmc->Ws*(du0-1); |
576 |
|
|
577 |
|
gmc->cGo = gmc->dxG + gmc->dyG + (1 << (gmc->alpha+gmc->rho+1)); |
578 |
|
gmc->cGo += 16*gmc->Ws*(dv0-1); |
579 |
|
} |
580 |
|
|
581 |
|
void |
582 |
|
generate_GMCimage( const GMC_DATA *const gmc_data, // [input] precalculated data |
583 |
|
const IMAGE *const pRef, // [input] |
584 |
|
const int mb_width, |
585 |
|
const int mb_height, |
586 |
|
const int stride, |
587 |
|
const int stride2, |
588 |
|
const int fcode, // [input] some parameters... |
589 |
|
const int32_t quarterpel, // [input] for rounding avgMV |
590 |
|
const int reduced_resolution, // [input] ignored |
591 |
|
const int32_t rounding, // [input] for rounding image data |
592 |
|
MACROBLOCK *const pMBs, // [output] average motion vectors |
593 |
|
IMAGE *const pGMC) // [output] full warped image |
594 |
|
{ |
595 |
|
|
596 |
|
unsigned int mj,mi; |
597 |
|
VECTOR avgMV; |
598 |
|
|
599 |
|
for (mj = 0; mj < (unsigned int)mb_height; mj++) |
600 |
|
for (mi = 0; mi < (unsigned int)mb_width; mi++) { |
601 |
|
|
602 |
|
avgMV = generate_GMCimageMB(gmc_data, pRef, mi, mj, |
603 |
|
stride, stride2, quarterpel, rounding, pGMC); |
604 |
|
|
605 |
|
pMBs[mj*mb_width+mi].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode); |
606 |
|
pMBs[mj*mb_width+mi].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode); |
607 |
|
pMBs[mj*mb_width+mi].mcsel = 0; /* until mode decision */ |
608 |
|
} |
609 |
|
} |
610 |
|
|
611 |
|
|
612 |
|
|
613 |
|
#define MLT(i) (((16-(i))<<16) + (i)) |
614 |
|
static const uint32_t MTab[16] = { |
615 |
|
MLT( 0), MLT( 1), MLT( 2), MLT( 3), MLT( 4), MLT( 5), MLT( 6), MLT(7), |
616 |
|
MLT( 8), MLT( 9), MLT(10), MLT(11), MLT(12), MLT(13), MLT(14), MLT(15) |
617 |
|
}; |
618 |
|
#undef MLT |
619 |
|
|
620 |
|
VECTOR generate_GMCimageMB( const GMC_DATA *const gmc_data, |
621 |
|
const IMAGE *const pRef, |
622 |
|
const int mi, const int mj, |
623 |
|
const int stride, |
624 |
|
const int stride2, |
625 |
|
const int quarterpel, |
626 |
|
const int rounding, |
627 |
|
IMAGE *const pGMC) |
628 |
|
{ |
629 |
|
const int W = gmc_data->W; |
630 |
|
const int H = gmc_data->H; |
631 |
|
|
632 |
|
const int rho = gmc_data->rho; |
633 |
|
const int alpha = gmc_data->alpha; |
634 |
|
|
635 |
|
const int rounder = ( 128 - (rounding<<(rho+rho)) ) << 16; |
636 |
|
|
637 |
|
const int dxF = gmc_data->dxF; |
638 |
|
const int dyF = gmc_data->dyF; |
639 |
|
const int dxG = gmc_data->dxG; |
640 |
|
const int dyG = gmc_data->dyG; |
641 |
|
|
642 |
|
uint8_t *dstY, *dstU, *dstV; |
643 |
|
|
644 |
|
int I,J; |
645 |
|
VECTOR avgMV = {0,0}; |
646 |
|
|
647 |
|
int32_t Fj, Gj; |
648 |
|
|
649 |
|
dstY = &pGMC->y[(mj*16)*stride+mi*16] + 16; |
650 |
|
|
651 |
|
Fj = gmc_data->Fo + dyF*mj*16 + dxF*mi*16; |
652 |
|
Gj = gmc_data->Go + dyG*mj*16 + dxG*mi*16; |
653 |
|
|
654 |
|
for (J = 16; J > 0; --J) { |
655 |
|
int32_t Fi, Gi; |
656 |
|
|
657 |
|
Fi = Fj; Fj += dyF; |
658 |
|
Gi = Gj; Gj += dyG; |
659 |
|
for (I = -16; I < 0; ++I) { |
660 |
|
int32_t F, G; |
661 |
|
uint32_t ri, rj; |
662 |
|
|
663 |
|
F = ( Fi >> (alpha+rho) ) << rho; Fi += dxF; |
664 |
|
G = ( Gi >> (alpha+rho) ) << rho; Gi += dxG; |
665 |
|
|
666 |
|
avgMV.x += F; |
667 |
|
avgMV.y += G; |
668 |
|
|
669 |
|
ri = MTab[F&15]; |
670 |
|
rj = MTab[G&15]; |
671 |
|
|
672 |
|
F >>= 4; |
673 |
|
G >>= 4; |
674 |
|
|
675 |
|
if (F < -1) F = -1; |
676 |
|
else if (F > W) F = W; |
677 |
|
if (G< -1) G=-1; |
678 |
|
else if (G>H) G=H; |
679 |
|
|
680 |
|
{ // MMX-like bilinear... |
681 |
|
const int offset = G*stride + F; |
682 |
|
uint32_t f0, f1; |
683 |
|
f0 = pRef->y[ offset +0 ]; |
684 |
|
f0 |= pRef->y[ offset +1 ] << 16; |
685 |
|
f1 = pRef->y[ offset+stride +0 ]; |
686 |
|
f1 |= pRef->y[ offset+stride +1 ] << 16; |
687 |
|
f0 = (ri*f0)>>16; |
688 |
|
f1 = (ri*f1) & 0x0fff0000; |
689 |
|
f0 |= f1; |
690 |
|
f0 = ( rj*f0 + rounder ) >> 24; |
691 |
|
|
692 |
|
dstY[I] = (uint8_t)f0; |
693 |
|
} |
694 |
|
} |
695 |
|
|
696 |
|
dstY += stride; |
697 |
|
} |
698 |
|
|
699 |
|
dstU = &pGMC->u[(mj*8)*stride2+mi*8] + 8; |
700 |
|
dstV = &pGMC->v[(mj*8)*stride2+mi*8] + 8; |
701 |
|
|
702 |
|
Fj = gmc_data->cFo + dyF*4 *mj*8 + dxF*4 *mi*8; |
703 |
|
Gj = gmc_data->cGo + dyG*4 *mj*8 + dxG*4 *mi*8; |
704 |
|
|
705 |
|
for (J = 8; J > 0; --J) { |
706 |
|
int32_t Fi, Gi; |
707 |
|
Fi = Fj; Fj += 4*dyF; |
708 |
|
Gi = Gj; Gj += 4*dyG; |
709 |
|
|
710 |
|
for (I = -8; I < 0; ++I) { |
711 |
|
int32_t F, G; |
712 |
|
uint32_t ri, rj; |
713 |
|
|
714 |
|
F = ( Fi >> (alpha+rho+2) ) << rho; Fi += 4*dxF; |
715 |
|
G = ( Gi >> (alpha+rho+2) ) << rho; Gi += 4*dxG; |
716 |
|
|
717 |
|
ri = MTab[F&15]; |
718 |
|
rj = MTab[G&15]; |
719 |
|
|
720 |
|
F >>= 4; |
721 |
|
G >>= 4; |
722 |
|
|
723 |
|
if (F < -1) F=-1; |
724 |
|
else if (F >= W/2) F = W/2; |
725 |
|
if (G < -1) G = -1; |
726 |
|
else if (G >= H/2) G = H/2; |
727 |
|
|
728 |
|
{ |
729 |
|
const int offset = G*stride2 + F; |
730 |
|
uint32_t f0, f1; |
731 |
|
|
732 |
|
f0 = pRef->u[ offset +0 ]; |
733 |
|
f0 |= pRef->u[ offset +1 ] << 16; |
734 |
|
f1 = pRef->u[ offset+stride2 +0 ]; |
735 |
|
f1 |= pRef->u[ offset+stride2 +1 ] << 16; |
736 |
|
f0 = (ri*f0)>>16; |
737 |
|
f1 = (ri*f1) & 0x0fff0000; |
738 |
|
f0 |= f1; |
739 |
|
f0 = ( rj*f0 + rounder ) >> 24; |
740 |
|
|
741 |
|
dstU[I] = (uint8_t)f0; |
742 |
|
|
743 |
|
|
744 |
|
f0 = pRef->v[ offset +0 ]; |
745 |
|
f0 |= pRef->v[ offset +1 ] << 16; |
746 |
|
f1 = pRef->v[ offset+stride2 +0 ]; |
747 |
|
f1 |= pRef->v[ offset+stride2 +1 ] << 16; |
748 |
|
f0 = (ri*f0)>>16; |
749 |
|
f1 = (ri*f1) & 0x0fff0000; |
750 |
|
f0 |= f1; |
751 |
|
f0 = ( rj*f0 + rounder ) >> 24; |
752 |
|
|
753 |
|
dstV[I] = (uint8_t)f0; |
754 |
|
} |
755 |
|
} |
756 |
|
dstU += stride2; |
757 |
|
dstV += stride2; |
758 |
|
} |
759 |
|
|
760 |
|
|
761 |
|
avgMV.x -= 16*((256*mi+120)<<4); // 120 = 15*16/2 |
762 |
|
avgMV.y -= 16*((256*mj+120)<<4); |
763 |
|
|
764 |
|
avgMV.x = RSHIFT( avgMV.x, (4+7-quarterpel) ); |
765 |
|
avgMV.y = RSHIFT( avgMV.y, (4+7-quarterpel) ); |
766 |
|
|
767 |
|
return avgMV; |
768 |
|
} |
769 |
|
|
770 |
|
|
771 |
|
|
772 |
|
#ifdef OLD_GRUEL_GMC |
773 |
|
void |
774 |
|
generate_GMCparameters( const int num_wp, // [input]: number of warppoints |
775 |
|
const int res, // [input]: resolution |
776 |
|
const WARPPOINTS *const warp, // [input]: warp points |
777 |
|
const int width, const int height, |
778 |
|
GMC_DATA *const gmc) // [output] precalculated parameters |
779 |
|
{ |
780 |
|
|
781 |
|
/* We follow mainly two sources: The original standard, which is ugly, and the |
782 |
|
thesis from Andreas Dehnhardt, which is much nicer. |
783 |
|
|
784 |
|
Notation is: indices are written next to the variable, |
785 |
|
primes in the standard are denoted by a suffix 'p'. |
786 |
|
types are "c"=constant, "i"=input parameter, "f"=calculated, then fixed, |
787 |
|
"o"=output data, " "=other, "u" = unused, "p"=calc for every pixel |
788 |
|
|
789 |
|
type | variable name | ISO name (TeX-style) | value or range | usage |
790 |
|
------------------------------------------------------------------------------------- |
791 |
|
c | H | H | [16 , ?] | image width (w/o edges) |
792 |
|
c | W | W | [16 , ?] | image height (w/o edges) |
793 |
|
|
794 |
|
c | i0 | i_0 | 0 | ref. point #1, X |
795 |
|
c | j0 | j_0 | 0 | ref. point #1, Y |
796 |
|
c | i1 | i_1 | W | ref. point #2, X |
797 |
|
c | j1 | j_1 | 0 | ref. point #2, Y |
798 |
|
cu | i2 | i_2 | 0 | ref. point #3, X |
799 |
|
cu | i2 | j_2 | H | ref. point #3, Y |
800 |
|
|
801 |
|
i | du0 | du[0] | [-16863,16863] | warp vector #1, Y |
802 |
|
i | dv0 | dv[0] | [-16863,16863] | warp vector #1, Y |
803 |
|
i | du1 | du[1] | [-16863,16863] | warp vector #2, Y |
804 |
|
i | dv1 | dv[1] | [-16863,16863] | warp vector #2, Y |
805 |
|
iu | du2 | du[2] | [-16863,16863] | warp vector #3, Y |
806 |
|
iu | dv2 | dv[2] | [-16863,16863] | warp vector #3, Y |
807 |
|
|
808 |
|
i | s | s | {2,4,8,16} | interpol. resolution |
809 |
|
f | sigma | - | log2(s) | X / s == X >> sigma |
810 |
|
f | r | r | =16/s | complementary res. |
811 |
|
f | rho | \rho | log2(r) | X / r == X >> rho |
812 |
|
|
813 |
|
f | i0s | i'_0 | | |
814 |
|
f | j0s | j'_0 | | |
815 |
|
f | i1s | i'_1 | | |
816 |
|
f | j1s | j'_1 | | |
817 |
|
f | i2s | i'_2 | | |
818 |
|
f | j2s | j'_2 | | |
819 |
|
|
820 |
|
f | alpha | \alpha | | 2^{alpha-1} < W <= 2^alpha |
821 |
|
f | beta | \beta | | 2^{beta-1} < H <= 2^beta |
822 |
|
|
823 |
|
f | Ws | W' | W = 2^{alpha} | scaled width |
824 |
|
f | Hs | H' | W = 2^{beta} | scaled height |
825 |
|
|
826 |
|
f | i1ss | i''_1 | "virtual sprite stuff" |
827 |
|
f | j1ss | j''_1 | "virtual sprite stuff" |
828 |
|
f | i2ss | i''_2 | "virtual sprite stuff" |
829 |
|
f | j2ss | j''_2 | "virtual sprite stuff" |
830 |
|
*/ |
831 |
|
|
832 |
|
/* Some calculations are disabled because we only use 2 warppoints at the moment */ |
833 |
|
|
834 |
|
int du0 = warp->duv[0].x; |
835 |
|
int dv0 = warp->duv[0].y; |
836 |
|
int du1 = warp->duv[1].x; |
837 |
|
int dv1 = warp->duv[1].y; |
838 |
|
// int du2 = warp->duv[2].x; |
839 |
|
// int dv2 = warp->duv[2].y; |
840 |
|
|
841 |
|
gmc->num_wp = num_wp; |
842 |
|
|
843 |
|
gmc->s = res; /* scaling parameters 2,4,8 or 16 */ |
844 |
|
gmc->sigma = log2bin(res-1); /* log2bin(15)=4, log2bin(16)=5, log2bin(17)=5 */ |
845 |
|
gmc->r = 16/res; |
846 |
|
gmc->rho = 4 - gmc->sigma; /* = log2bin(r-1) */ |
847 |
|
|
848 |
|
gmc->W = width; |
849 |
|
gmc->H = height; /* fixed reference coordinates */ |
850 |
|
|
851 |
|
gmc->alpha = log2bin(gmc->W-1); |
852 |
|
gmc->Ws= 1<<gmc->alpha; |
853 |
|
|
854 |
|
// gmc->beta = log2bin(gmc->H-1); |
855 |
|
// gmc->Hs= 1<<gmc->beta; |
856 |
|
|
857 |
|
// printf("du0=%d dv0=%d du1=%d dv1=%d s=%d sigma=%d W=%d alpha=%d, Ws=%d, rho=%d\n",du0,dv0,du1,dv1,gmc->s,gmc->sigma,gmc->W,gmc->alpha,gmc->Ws,gmc->rho); |
858 |
|
|
859 |
|
/* i2s is only needed for num_wp >= 3, etc. */ |
860 |
|
/* the 's' values are in 1/s pel resolution */ |
861 |
|
gmc->i0s = res/2 * ( du0 ); |
862 |
|
gmc->j0s = res/2 * ( dv0 ); |
863 |
|
gmc->i1s = res/2 * (2*width + du1 + du0 ); |
864 |
|
gmc->j1s = res/2 * ( dv1 + dv0 ); |
865 |
|
// gmc->i2s = res/2 * ( du2 + du0 ); |
866 |
|
// gmc->j2s = res/2 * (2*height + dv2 + dv0 ); |
867 |
|
|
868 |
|
/* i2s and i2ss are only needed for num_wp == 3, etc. */ |
869 |
|
|
870 |
|
/* the 'ss' values are in 1/16 pel resolution */ |
871 |
|
gmc->i1ss = 16*gmc->Ws + ROUNDED_DIV(((gmc->W-gmc->Ws)*(gmc->r*gmc->i0s) + gmc->Ws*(gmc->r*gmc->i1s - 16*gmc->W)),gmc->W); |
872 |
|
gmc->j1ss = ROUNDED_DIV( ((gmc->W - gmc->Ws)*(gmc->r*gmc->j0s) + gmc->Ws*gmc->r*gmc->j1s) ,gmc->W ); |
873 |
|
|
874 |
|
// gmc->i2ss = ROUNDED_DIV( ((gmc->H - gmc->Hs)*(gmc->r*gmc->i0s) + gmc->Hs*(gmc->r*gmc->i2s)), gmc->H); |
875 |
|
// gmc->j2ss = 16*gmc->Hs + ROUNDED_DIV( ((gmc->H-gmc->Hs)*(gmc->r*gmc->j0s) + gmc->Ws*(gmc->r*gmc->j2s - 16*gmc->H)), gmc->H); |
876 |
|
|
877 |
|
return; |
878 |
|
} |
879 |
|
|
880 |
|
void |
881 |
|
generate_GMCimage( const GMC_DATA *const gmc_data, // [input] precalculated data |
882 |
|
const IMAGE *const pRef, // [input] |
883 |
|
const int mb_width, |
884 |
|
const int mb_height, |
885 |
|
const int stride, |
886 |
|
const int stride2, |
887 |
|
const int fcode, // [input] some parameters... |
888 |
|
const int32_t quarterpel, // [input] for rounding avgMV |
889 |
|
const int reduced_resolution, // [input] ignored |
890 |
|
const int32_t rounding, // [input] for rounding image data |
891 |
|
MACROBLOCK *const pMBs, // [output] average motion vectors |
892 |
|
IMAGE *const pGMC) // [output] full warped image |
893 |
|
{ |
894 |
|
|
895 |
|
unsigned int mj,mi; |
896 |
|
VECTOR avgMV; |
897 |
|
|
898 |
|
for (mj = 0;mj < mb_height; mj++) |
899 |
|
for (mi = 0;mi < mb_width; mi++) { |
900 |
|
|
901 |
|
avgMV = generate_GMCimageMB(gmc_data, pRef, mi, mj, |
902 |
|
stride, stride2, quarterpel, rounding, pGMC); |
903 |
|
|
904 |
|
pMBs[mj*mb_width+mi].amv.x = gmc_sanitize(avgMV.x, quarterpel, fcode); |
905 |
|
pMBs[mj*mb_width+mi].amv.y = gmc_sanitize(avgMV.y, quarterpel, fcode); |
906 |
|
pMBs[mj*mb_width+mi].mcsel = 0; /* until mode decision */ |
907 |
|
} |
908 |
|
} |
909 |
|
|
910 |
|
|
911 |
|
VECTOR generate_GMCimageMB( const GMC_DATA *const gmc_data, /* [input] all precalc data */ |
912 |
|
const IMAGE *const pRef, /* [input] */ |
913 |
|
const int mi, const int mj, /* [input] MB position */ |
914 |
|
const int stride, /* [input] Lumi stride */ |
915 |
|
const int stride2, /* [input] chroma stride */ |
916 |
|
const int quarterpel, /* [input] for rounding of avgMV */ |
917 |
|
const int rounding, /* [input] for rounding of imgae data */ |
918 |
|
IMAGE *const pGMC) /* [outut] generate image */ |
919 |
|
|
920 |
|
/* |
921 |
|
type | variable name | ISO name (TeX-style) | value or range | usage |
922 |
|
------------------------------------------------------------------------------------- |
923 |
|
p | F | F(i,j) | | pelwise motion vector X in s-th pel |
924 |
|
p | G | G(i,j) | | pelwise motion vector Y in s-th pel |
925 |
|
p | Fc | F_c(i,j) | | |
926 |
|
p | Gc | G_c(i,j) | | same for chroma |
927 |
|
|
928 |
|
p | Y00 | Y_{00} | [0,255*s*s] | first: 4 neighbouring Y-values |
929 |
|
p | Y01 | Y_{01} | [0,255] | at fullpel position, around the |
930 |
|
p | Y10 | Y_{10} | [0,255*s] | position where pelweise MV points to |
931 |
|
p | Y11 | Y_{11} | [0,255] | later: bilinear interpol Y-values in Y00 |
932 |
|
|
933 |
|
p | C00 | C_{00} | [0,255*s*s] | same for chroma Cb and Cr |
934 |
|
p | C01 | C_{01} | [0,255] | |
935 |
|
p | C10 | C_{10} | [0,255*s] | |
936 |
|
p | C11 | C_{11} | [0,255] | |
937 |
|
|
938 |
|
*/ |
939 |
|
{ |
940 |
|
const int W = gmc_data->W; |
941 |
|
const int H = gmc_data->H; |
942 |
|
|
943 |
|
const int s = gmc_data->s; |
944 |
|
const int sigma = gmc_data->sigma; |
945 |
|
|
946 |
|
const int r = gmc_data->r; |
947 |
|
const int rho = gmc_data->rho; |
948 |
|
|
949 |
|
const int i0s = gmc_data->i0s; |
950 |
|
const int j0s = gmc_data->j0s; |
951 |
|
|
952 |
|
const int i1ss = gmc_data->i1ss; |
953 |
|
const int j1ss = gmc_data->j1ss; |
954 |
|
// const int i2ss = gmc_data->i2ss; |
955 |
|
// const int j2ss = gmc_data->j2ss; |
956 |
|
|
957 |
|
const int alpha = gmc_data->alpha; |
958 |
|
const int Ws = gmc_data->Ws; |
959 |
|
|
960 |
|
// const int beta = gmc_data->beta; |
961 |
|
// const int Hs = gmc_data->Hs; |
962 |
|
|
963 |
|
int I,J; |
964 |
|
VECTOR avgMV = {0,0}; |
965 |
|
|
966 |
|
for (J=16*mj;J<16*(mj+1);J++) |
967 |
|
for (I=16*mi;I<16*(mi+1);I++) |
968 |
|
{ |
969 |
|
int F= i0s + ( ((-r*i0s+i1ss)*I + (r*j0s-j1ss)*J + (1<<(alpha+rho-1))) >> (alpha+rho) ); |
970 |
|
int G= j0s + ( ((-r*j0s+j1ss)*I + (-r*i0s+i1ss)*J + (1<<(alpha+rho-1))) >> (alpha+rho) ); |
971 |
|
|
972 |
|
/* this naive implementation (with lots of multiplications) isn't slower (rather faster) than |
973 |
|
working incremental. Don't ask me why... maybe the whole this is memory bound? */ |
974 |
|
|
975 |
|
const int ri= F & (s-1); // fractional part of pelwise MV X |
976 |
|
const int rj= G & (s-1); // fractional part of pelwise MV Y |
977 |
|
|
978 |
|
int Y00,Y01,Y10,Y11; |
979 |
|
|
980 |
|
/* unclipped values are used for avgMV */ |
981 |
|
avgMV.x += F-(I<<sigma); /* shift position to 1/s-pel, as the MV is */ |
982 |
|
avgMV.y += G-(J<<sigma); /* TODO: don't do this (of course) */ |
983 |
|
|
984 |
|
F >>= sigma; |
985 |
|
G >>= sigma; |
986 |
|
|
987 |
|
/* clip values to be in range. Since we have edges, clip to 1 less than lower boundary |
988 |
|
this way positions F+1/G+1 are still right */ |
989 |
|
|
990 |
|
if (F< -1) |
991 |
|
F=-1; |
992 |
|
else if (F>W) |
993 |
|
F=W; /* W or W-1 doesn't matter, so save 1 subtract ;-) */ |
994 |
|
if (G< -1) |
995 |
|
G=-1; |
996 |
|
else if (G>H) |
997 |
|
G=H; /* dito */ |
998 |
|
|
999 |
|
Y00 = pRef->y[ G*stride + F ]; // Lumi values |
1000 |
|
Y01 = pRef->y[ G*stride + F+1 ]; |
1001 |
|
Y10 = pRef->y[ G*stride + F+stride ]; |
1002 |
|
Y11 = pRef->y[ G*stride + F+stride+1 ]; |
1003 |
|
|
1004 |
|
/* bilinear interpolation */ |
1005 |
|
Y00 = ((s-ri)*Y00 + ri*Y01); |
1006 |
|
Y10 = ((s-ri)*Y10 + ri*Y11); |
1007 |
|
Y00 = ((s-rj)*Y00 + rj*Y10 + s*s/2 - rounding ) >> (sigma+sigma); |
1008 |
|
|
1009 |
|
pGMC->y[J*stride+I] = (uint8_t)Y00; /* output 1 Y-pixel */ |
1010 |
|
} |
1011 |
|
|
1012 |
|
|
1013 |
|
/* doing chroma _here_ is even more stupid and slow, because won't be used until Compensation and |
1014 |
|
most likely not even then (only if the block really _is_ GMC) |
1015 |
|
*/ |
1016 |
|
|
1017 |
|
for (J=8*mj;J<8*(mj+1);J++) /* this plays the role of j_c,i_c in the standard */ |
1018 |
|
for (I=8*mi;I<8*(mi+1);I++) /* For I_c we have to use I_c = 4*i_c+1 ! */ |
1019 |
|
{ |
1020 |
|
/* same positions for both chroma components, U=Cb and V=Cr */ |
1021 |
|
int Fc=((-r*i0s+i1ss)*(4*I+1) + (r*j0s-j1ss)*(4*J+1) +2*Ws*r*i0s |
1022 |
|
-16*Ws +(1<<(alpha+rho+1)))>>(alpha+rho+2); |
1023 |
|
int Gc=((-r*j0s+j1ss)*(4*I+1) +(-r*i0s+i1ss)*(4*J+1) +2*Ws*r*j0s |
1024 |
|
-16*Ws +(1<<(alpha+rho+1))) >>(alpha+rho+2); |
1025 |
|
|
1026 |
|
const int ri= Fc & (s-1); // fractional part of pelwise MV X |
1027 |
|
const int rj= Gc & (s-1); // fractional part of pelwise MV Y |
1028 |
|
|
1029 |
|
int C00,C01,C10,C11; |
1030 |
|
|
1031 |
|
Fc >>= sigma; |
1032 |
|
Gc >>= sigma; |
1033 |
|
|
1034 |
|
if (Fc< -1) |
1035 |
|
Fc=-1; |
1036 |
|
else if (Fc>=W/2) |
1037 |
|
Fc=W/2; /* W or W-1 doesn't matter, so save 1 subtraction ;-) */ |
1038 |
|
if (Gc< -1) |
1039 |
|
Gc=-1; |
1040 |
|
else if (Gc>=H/2) |
1041 |
|
Gc=H/2; /* dito */ |
1042 |
|
|
1043 |
|
/* now calculate U data */ |
1044 |
|
C00 = pRef->u[ Gc*stride2 + Fc ]; // chroma-value Cb |
1045 |
|
C01 = pRef->u[ Gc*stride2 + Fc+1 ]; |
1046 |
|
C10 = pRef->u[ (Gc+1)*stride2 + Fc ]; |
1047 |
|
C11 = pRef->u[ (Gc+1)*stride2 + Fc+1 ]; |
1048 |
|
|
1049 |
|
/* bilinear interpolation */ |
1050 |
|
C00 = ((s-ri)*C00 + ri*C01); |
1051 |
|
C10 = ((s-ri)*C10 + ri*C11); |
1052 |
|
C00 = ((s-rj)*C00 + rj*C10 + s*s/2 - rounding ) >> (sigma+sigma); |
1053 |
|
|
1054 |
|
pGMC->u[J*stride2+I] = (uint8_t)C00; /* output 1 U-pixel */ |
1055 |
|
|
1056 |
|
/* now calculate V data */ |
1057 |
|
C00 = pRef->v[ Gc*stride2 + Fc ]; // chroma-value Cr |
1058 |
|
C01 = pRef->v[ Gc*stride2 + Fc+1 ]; |
1059 |
|
C10 = pRef->v[ (Gc+1)*stride2 + Fc ]; |
1060 |
|
C11 = pRef->v[ (Gc+1)*stride2 + Fc+1 ]; |
1061 |
|
|
1062 |
|
/* bilinear interpolation */ |
1063 |
|
C00 = ((s-ri)*C00 + ri*C01); |
1064 |
|
C10 = ((s-ri)*C10 + ri*C11); |
1065 |
|
C00 = ((s-rj)*C00 + rj*C10 + s*s/2 - rounding ) >> (sigma+sigma); |
1066 |
|
|
1067 |
|
pGMC->v[J*stride2+I] = (uint8_t)C00; /* output 1 V-pixel */ |
1068 |
|
} |
1069 |
|
|
1070 |
|
/* The average vector is rounded from 1/s-pel to 1/2 or 1/4 using the '//' operator*/ |
1071 |
|
|
1072 |
|
avgMV.x = RSHIFT( avgMV.x, (sigma+7-quarterpel) ); |
1073 |
|
avgMV.y = RSHIFT( avgMV.y, (sigma+7-quarterpel) ); |
1074 |
|
|
1075 |
|
/* ^^^^ this is the way MS Reference Software does it */ |
1076 |
|
|
1077 |
|
return avgMV; /* clipping to fcode area is done outside! */ |
1078 |
|
} |
1079 |
|
|
1080 |
|
#endif |