81 |
static __inline uint32_t |
static __inline uint32_t |
82 |
d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) |
d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) |
83 |
{ |
{ |
84 |
int xb, yb; |
int bits; |
85 |
x = qpel ? x<<1 : x; |
const int q = (1 << (iFcode - 1)) - 1; |
86 |
y = qpel ? y<<1 : y; |
|
87 |
|
x <<= qpel; |
88 |
|
y <<= qpel; |
89 |
if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
90 |
|
|
91 |
x -= pred.x; |
x -= pred.x; |
92 |
y -= pred.y; |
bits = (x != 0 ? iFcode:0); |
93 |
|
x = abs(x); |
94 |
if (x) { |
x += q; |
|
x = ABS(x); |
|
|
x += (1 << (iFcode - 1)) - 1; |
|
95 |
x >>= (iFcode - 1); |
x >>= (iFcode - 1); |
96 |
if (x > 32) x = 32; |
bits += mvtab[x]; |
97 |
xb = mvtab[x] + iFcode; |
|
98 |
} else xb = 1; |
y -= pred.y; |
99 |
|
bits += (y != 0 ? iFcode:0); |
100 |
if (y) { |
y = abs(y); |
101 |
y = ABS(y); |
y += q; |
|
y += (1 << (iFcode - 1)) - 1; |
|
102 |
y >>= (iFcode - 1); |
y >>= (iFcode - 1); |
103 |
if (y > 32) y = 32; |
bits += mvtab[y]; |
104 |
yb = mvtab[y] + iFcode; |
|
105 |
} else yb = 1; |
return bits; |
|
return xb + yb; |
|
106 |
} |
} |
107 |
|
|
108 |
static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data) |
static int32_t ChromaSAD2(const int fx, const int fy, const int bx, const int by, |
109 |
|
const SearchData * const data) |
110 |
{ |
{ |
111 |
int sad; |
int sad; |
112 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
114 |
* f_refv = data->RefQ + 8, |
* f_refv = data->RefQ + 8, |
115 |
* b_refu = data->RefQ + 16, |
* b_refu = data->RefQ + 16, |
116 |
* b_refv = data->RefQ + 24; |
* b_refv = data->RefQ + 24; |
117 |
|
int offset = (fx>>1) + (fy>>1)*stride; |
118 |
|
|
119 |
switch (((fx & 1) << 1) | (fy & 1)) { |
switch (((fx & 1) << 1) | (fy & 1)) { |
120 |
case 0: |
case 0: |
121 |
fx = fx / 2; fy = fy / 2; |
f_refu = (uint8_t*)data->RefP[4] + offset; |
122 |
f_refu = (uint8_t*)data->RefCU + fy * stride + fx, stride; |
f_refv = (uint8_t*)data->RefP[5] + offset; |
|
f_refv = (uint8_t*)data->RefCV + fy * stride + fx, stride; |
|
123 |
break; |
break; |
124 |
case 1: |
case 1: |
125 |
fx = fx / 2; fy = (fy - 1) / 2; |
interpolate8x8_halfpel_v(f_refu, data->RefP[4] + offset, stride, data->rounding); |
126 |
interpolate8x8_halfpel_v(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
interpolate8x8_halfpel_v(f_refv, data->RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_v(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
|
127 |
break; |
break; |
128 |
case 2: |
case 2: |
129 |
fx = (fx - 1) / 2; fy = fy / 2; |
interpolate8x8_halfpel_h(f_refu, data->RefP[4] + offset, stride, data->rounding); |
130 |
interpolate8x8_halfpel_h(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
interpolate8x8_halfpel_h(f_refv, data->RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_h(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
|
131 |
break; |
break; |
132 |
default: |
default: |
133 |
fx = (fx - 1) / 2; fy = (fy - 1) / 2; |
interpolate8x8_halfpel_hv(f_refu, data->RefP[4] + offset, stride, data->rounding); |
134 |
interpolate8x8_halfpel_hv(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
interpolate8x8_halfpel_hv(f_refv, data->RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_hv(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
|
135 |
break; |
break; |
136 |
} |
} |
137 |
|
|
138 |
|
offset = (bx>>1) + (by>>1)*stride; |
139 |
switch (((bx & 1) << 1) | (by & 1)) { |
switch (((bx & 1) << 1) | (by & 1)) { |
140 |
case 0: |
case 0: |
141 |
bx = bx / 2; by = by / 2; |
b_refu = (uint8_t*)data->b_RefP[4] + offset; |
142 |
b_refu = (uint8_t*)data->b_RefCU + by * stride + bx, stride; |
b_refv = (uint8_t*)data->b_RefP[5] + offset; |
|
b_refv = (uint8_t*)data->b_RefCV + by * stride + bx, stride; |
|
143 |
break; |
break; |
144 |
case 1: |
case 1: |
145 |
bx = bx / 2; by = (by - 1) / 2; |
interpolate8x8_halfpel_v(b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
146 |
interpolate8x8_halfpel_v(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
interpolate8x8_halfpel_v(b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_v(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
|
147 |
break; |
break; |
148 |
case 2: |
case 2: |
149 |
bx = (bx - 1) / 2; by = by / 2; |
interpolate8x8_halfpel_h(b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
150 |
interpolate8x8_halfpel_h(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
interpolate8x8_halfpel_h(b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_h(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
|
151 |
break; |
break; |
152 |
default: |
default: |
153 |
bx = (bx - 1) / 2; by = (by - 1) / 2; |
interpolate8x8_halfpel_hv(b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
154 |
interpolate8x8_halfpel_hv(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
interpolate8x8_halfpel_hv(b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_hv(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
|
155 |
break; |
break; |
156 |
} |
} |
157 |
|
|
161 |
return sad; |
return sad; |
162 |
} |
} |
163 |
|
|
|
|
|
164 |
static int32_t |
static int32_t |
165 |
ChromaSAD(int dx, int dy, const SearchData * const data) |
ChromaSAD(const int dx, const int dy, const SearchData * const data) |
166 |
{ |
{ |
167 |
int sad; |
int sad; |
168 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
169 |
|
int offset = (dx>>1) + (dy>>1)*stride; |
170 |
|
|
171 |
if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently |
if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently |
172 |
data->temp[5] = dx; data->temp[6] = dy; // backup |
data->temp[5] = dx; data->temp[6] = dy; // backup |
173 |
|
|
174 |
switch (((dx & 1) << 1) | (dy & 1)) { |
switch (((dx & 1) << 1) | (dy & 1)) { |
175 |
case 0: |
case 0: |
176 |
dx = dx / 2; dy = dy / 2; |
sad = sad8(data->CurU, data->RefP[4] + offset, stride); |
177 |
sad = sad8(data->CurU, data->RefCU + dy * stride + dx, stride); |
sad += sad8(data->CurV, data->RefP[5] + offset, stride); |
|
sad += sad8(data->CurV, data->RefCV + dy * stride + dx, stride); |
|
178 |
break; |
break; |
179 |
case 1: |
case 1: |
180 |
dx = dx / 2; dy = (dy - 1) / 2; |
sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + stride, stride); |
181 |
sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + (dy+1) * stride + dx, stride); |
sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + stride, stride); |
|
sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + (dy+1) * stride + dx, stride); |
|
182 |
break; |
break; |
183 |
case 2: |
case 2: |
184 |
dx = (dx - 1) / 2; dy = dy / 2; |
sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + 1, stride); |
185 |
sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + dy * stride + dx+1, stride); |
sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + 1, stride); |
|
sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + dy * stride + dx+1, stride); |
|
186 |
break; |
break; |
187 |
default: |
default: |
188 |
dx = (dx - 1) / 2; dy = (dy - 1) / 2; |
interpolate8x8_halfpel_hv(data->RefQ, data->RefP[4] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_hv(data->RefQ, data->RefCU + dy * stride + dx, stride, data->rounding); |
|
189 |
sad = sad8(data->CurU, data->RefQ, stride); |
sad = sad8(data->CurU, data->RefQ, stride); |
190 |
|
|
191 |
interpolate8x8_halfpel_hv(data->RefQ, data->RefCV + dy * stride + dx, stride, data->rounding); |
interpolate8x8_halfpel_hv(data->RefQ, data->RefP[5] + offset, stride, data->rounding); |
192 |
sad += sad8(data->CurV, data->RefQ, stride); |
sad += sad8(data->CurV, data->RefQ, stride); |
193 |
break; |
break; |
194 |
} |
} |
200 |
GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data) |
GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data) |
201 |
{ |
{ |
202 |
// dir : 0 = forward, 1 = backward |
// dir : 0 = forward, 1 = backward |
203 |
switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) { |
const uint8_t *const *const direction = ( dir == 0 ? data->RefP : data->b_RefP ); |
204 |
case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); |
const int picture = ((x&1)<<1) | (y&1); |
205 |
case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; |
206 |
case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); |
return direction[picture] + offset; |
|
case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
|
|
case 4 : return data->bRef + x/2 + (y/2)*(data->iEdgedWidth); |
|
|
case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
|
|
case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); |
|
|
default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
|
|
} |
|
207 |
} |
} |
208 |
|
|
209 |
// this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate |
// this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate |
210 |
static __inline const uint8_t * |
static __inline const uint8_t * |
211 |
GetReference(const int x, const int y, const SearchData * const data) |
GetReference(const int x, const int y, const SearchData * const data) |
212 |
{ |
{ |
213 |
switch ( ((x&1)<<1) | (y&1) ) { |
const int picture = ((x&1)<<1) | (y&1); |
214 |
case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); |
const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; |
215 |
case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
return data->RefP[picture] + offset; |
|
case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
|
|
default : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); //case 2 |
|
|
} |
|
216 |
} |
} |
217 |
|
|
218 |
static uint8_t * |
static uint8_t * |
229 |
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); |
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); |
230 |
ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
231 |
switch( ((x&1)<<1) + (y&1) ) { |
switch( ((x&1)<<1) + (y&1) ) { |
232 |
case 0: // pure halfpel position |
case 3: // x and y in qpel resolution - the "corners" (top left/right and |
233 |
return (uint8_t *) ref1; |
// bottom left/right) during qpel refinement |
234 |
|
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
235 |
|
ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); |
236 |
|
ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data); |
237 |
|
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
238 |
|
ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
239 |
|
ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
240 |
|
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
241 |
break; |
break; |
242 |
|
|
243 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
252 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
253 |
break; |
break; |
254 |
|
|
255 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // pure halfpel position |
256 |
// bottom left/right) during qpel refinement |
return (uint8_t *) ref1; |
257 |
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
|
|
ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); |
|
|
ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data); |
|
|
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
|
|
ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
|
|
ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
|
|
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
|
|
break; |
|
258 |
} |
} |
259 |
return Reference; |
return Reference; |
260 |
} |
} |
299 |
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8); |
300 |
break; |
break; |
301 |
|
|
302 |
case 0: // pure halfpel position |
default: // pure halfpel position |
303 |
return (uint8_t *) ref1; |
return (uint8_t *) ref1; |
304 |
} |
} |
305 |
return Reference; |
return Reference; |
351 |
data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; } |
data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; } |
352 |
if (data->temp[4] < data->iMinSAD[4]) { |
if (data->temp[4] < data->iMinSAD[4]) { |
353 |
data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; } |
data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; } |
|
|
|
354 |
} |
} |
355 |
|
|
356 |
static void |
static void |
358 |
{ |
{ |
359 |
int32_t sad; uint32_t t; |
int32_t sad; uint32_t t; |
360 |
const uint8_t * Reference; |
const uint8_t * Reference; |
361 |
|
VECTOR * current; |
362 |
|
|
363 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
364 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
365 |
|
|
366 |
if (!data->qpel_precision) Reference = GetReference(x, y, data); |
if (!data->qpel_precision) { |
367 |
else Reference = Interpolate8x8qpel(x, y, 0, 0, data); |
Reference = GetReference(x, y, data); |
368 |
|
current = data->currentMV; |
369 |
|
} else { // x and y are in 1/4 precision |
370 |
|
Reference = Interpolate8x8qpel(x, y, 0, 0, data); |
371 |
|
current = data->currentQMV; |
372 |
|
} |
373 |
|
|
374 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
375 |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
378 |
|
|
379 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
380 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
381 |
data->currentMV->x = x; data->currentMV->y = y; |
current->x = x; current->y = y; |
382 |
*dir = Direction; |
*dir = Direction; |
383 |
} |
} |
384 |
} |
} |
385 |
|
|
|
|
|
386 |
static void |
static void |
387 |
CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
388 |
{ |
{ |
389 |
uint32_t t; |
uint32_t t; |
390 |
const uint8_t * Reference; |
const uint8_t * Reference; |
391 |
|
|
392 |
if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value |
if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero even value |
393 |
(x > data->max_dx) || (x < data->min_dx) |
(x > data->max_dx) || (x < data->min_dx) |
394 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
395 |
|
|
424 |
uint32_t t; |
uint32_t t; |
425 |
VECTOR * current; |
VECTOR * current; |
426 |
|
|
427 |
if ( (x > data->max_dx) | ( x < data->min_dx) |
if ( (x > data->max_dx) || ( x < data->min_dx) |
428 |
| (y > data->max_dy) | (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
429 |
|
|
430 |
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value |
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value |
431 |
|
|
463 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
464 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
465 |
|
|
466 |
sad = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*(data->iEdgedWidth), |
467 |
data->iEdgedWidth, data->temp+1); |
data->iEdgedWidth, data->temp+1); |
468 |
|
|
469 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
490 |
const uint8_t *ReferenceF, *ReferenceB; |
const uint8_t *ReferenceF, *ReferenceB; |
491 |
VECTOR *current; |
VECTOR *current; |
492 |
|
|
493 |
if ( (xf > data->max_dx) | (xf < data->min_dx) |
if ((xf > data->max_dx) || (xf < data->min_dx) || |
494 |
| (yf > data->max_dy) | (yf < data->min_dy) ) return; |
(yf > data->max_dy) || (yf < data->min_dy)) |
495 |
|
return; |
496 |
|
|
497 |
if (!data->qpel_precision) { |
if (!data->qpel_precision) { |
498 |
ReferenceF = GetReference(xf, yf, data); |
ReferenceF = GetReference(xf, yf, data); |
537 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
538 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
539 |
|
|
540 |
if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
541 |
|
|
542 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
543 |
mvs.x = data->directmvF[k].x + x; |
mvs.x = data->directmvF[k].x + x; |
550 |
data->directmvB[k].y |
data->directmvB[k].y |
551 |
: mvs.y - data->referencemv[k].y); |
: mvs.y - data->referencemv[k].y); |
552 |
|
|
553 |
if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx) |
if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) || |
554 |
| (mvs.y > data->max_dy) | (mvs.y < data->min_dy) |
(mvs.y > data->max_dy) || (mvs.y < data->min_dy) || |
555 |
| (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx) |
(b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) || |
556 |
| (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return; |
(b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) |
557 |
|
return; |
558 |
|
|
559 |
if (data->qpel) { |
if (data->qpel) { |
560 |
xcf += mvs.x/2; ycf += mvs.y/2; |
xcf += mvs.x/2; ycf += mvs.y/2; |
596 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
597 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
598 |
|
|
599 |
if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
600 |
|
|
601 |
mvs.x = data->directmvF[0].x + x; |
mvs.x = data->directmvF[0].x + x; |
602 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
608 |
data->directmvB[0].y |
data->directmvB[0].y |
609 |
: mvs.y - data->referencemv[0].y); |
: mvs.y - data->referencemv[0].y); |
610 |
|
|
611 |
if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx) |
if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx) |
612 |
| (mvs.y > data->max_dy) | (mvs.y < data->min_dy) |
|| (mvs.y > data->max_dy) || (mvs.y < data->min_dy) |
613 |
| (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx) |
|| (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) |
614 |
| (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return; |
|| (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return; |
615 |
|
|
616 |
if (data->qpel) { |
if (data->qpel) { |
617 |
xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); |
xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); |
645 |
CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
646 |
{ |
{ |
647 |
|
|
648 |
static int16_t in[64], coeff[64]; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
649 |
int32_t bits = 0, sum; |
int32_t bits = 0, sum; |
650 |
VECTOR * current; |
VECTOR * current; |
651 |
const uint8_t * ptr; |
const uint8_t * ptr; |
683 |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
684 |
|
|
685 |
//chroma U |
//chroma U |
686 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCU, 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
687 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
688 |
fdct(in); |
fdct(in); |
689 |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
695 |
|
|
696 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
697 |
//chroma V |
//chroma V |
698 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCV, 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
699 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
700 |
fdct(in); |
fdct(in); |
701 |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
707 |
} |
} |
708 |
} |
} |
709 |
|
|
710 |
bits += cbpy_tab[15-(cbp>>2)].len; |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
711 |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
712 |
|
|
713 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
730 |
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
731 |
{ |
{ |
732 |
|
|
733 |
static int16_t in[64], coeff[64]; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
734 |
int32_t sum, bits; |
int32_t sum, bits; |
735 |
VECTOR * current; |
VECTOR * current; |
736 |
const uint8_t * ptr; |
const uint8_t * ptr; |
936 |
const uint32_t stride, const uint32_t iQuant, int rrv) |
const uint32_t stride, const uint32_t iQuant, int rrv) |
937 |
|
|
938 |
{ |
{ |
939 |
|
int offset = (x + y*stride)*8; |
940 |
if(!rrv) { |
if(!rrv) { |
941 |
uint32_t sadC = sad8(current->u + x*8 + y*stride*8, |
uint32_t sadC = sad8(current->u + offset, |
942 |
reference->u + x*8 + y*stride*8, stride); |
reference->u + offset, stride); |
943 |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
944 |
sadC += sad8(current->v + (x + y*stride)*8, |
sadC += sad8(current->v + offset, |
945 |
reference->v + (x + y*stride)*8, stride); |
reference->v + offset, stride); |
946 |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
947 |
return 1; |
return 1; |
948 |
|
|
949 |
} else { |
} else { |
950 |
uint32_t sadC = sad16(current->u + x*16 + y*stride*16, |
uint32_t sadC = sad16(current->u + 2*offset, |
951 |
reference->u + x*16 + y*stride*16, stride, 256*4096); |
reference->u + 2*offset, stride, 256*4096); |
952 |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
953 |
sadC += sad16(current->v + (x + y*stride)*16, |
sadC += sad16(current->v + 2*offset, |
954 |
reference->v + (x + y*stride)*16, stride, 256*4096); |
reference->v + 2*offset, stride, 256*4096); |
955 |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
956 |
return 1; |
return 1; |
957 |
} |
} |
982 |
uint32_t mb_width = pParam->mb_width; |
uint32_t mb_width = pParam->mb_width; |
983 |
uint32_t mb_height = pParam->mb_height; |
uint32_t mb_height = pParam->mb_height; |
984 |
const uint32_t iEdgedWidth = pParam->edged_width; |
const uint32_t iEdgedWidth = pParam->edged_width; |
985 |
const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->global_flags); |
const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags); |
986 |
|
|
987 |
uint32_t x, y; |
uint32_t x, y; |
988 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
989 |
int32_t quant = current->quant, sad00; |
int32_t quant = current->quant, sad00; |
990 |
|
int skip_thresh = \ |
991 |
|
INITIAL_SKIP_THRESH * \ |
992 |
|
(current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \ |
993 |
|
(current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1); |
994 |
|
|
995 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
996 |
int32_t temp[8]; |
int32_t temp[8]; |
997 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
998 |
VECTOR currentQMV[5]; |
VECTOR currentQMV[5]; |
999 |
int32_t iMinSAD[5]; |
int32_t iMinSAD[5]; |
1000 |
|
DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE); |
1001 |
SearchData Data; |
SearchData Data; |
1002 |
memset(&Data, 0, sizeof(SearchData)); |
memset(&Data, 0, sizeof(SearchData)); |
1003 |
Data.iEdgedWidth = iEdgedWidth; |
Data.iEdgedWidth = iEdgedWidth; |
1007 |
Data.temp = temp; |
Data.temp = temp; |
1008 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
1009 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
1010 |
Data.qpel = pParam->m_quarterpel; |
Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); |
1011 |
Data.chroma = MotionFlags & PMV_CHROMA16; |
Data.chroma = MotionFlags & XVID_ME_CHROMA16; |
1012 |
Data.rrv = current->global_flags & XVID_REDUCED; |
Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0); |
1013 |
|
Data.dctSpace = dct_space; |
1014 |
|
|
1015 |
if ((current->global_flags & XVID_REDUCED)) { |
if ((current->vop_flags & XVID_VOP_REDUCED)) { |
1016 |
mb_width = (pParam->width + 31) / 32; |
mb_width = (pParam->width + 31) / 32; |
1017 |
mb_height = (pParam->height + 31) / 32; |
mb_height = (pParam->height + 31) / 32; |
1018 |
Data.qpel = 0; |
Data.qpel = 0; |
1045 |
|
|
1046 |
sad00 = pMB->sad16; |
sad00 = pMB->sad16; |
1047 |
|
|
1048 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
pMB->dquant = NO_CHANGE; |
|
|
} else { |
|
|
if (pMB->dquant != NO_CHANGE) { |
|
1049 |
quant += DQtab[pMB->dquant]; |
quant += DQtab[pMB->dquant]; |
1050 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
1051 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
1052 |
} |
} |
|
} |
|
1053 |
pMB->quant = current->quant; |
pMB->quant = current->quant; |
1054 |
|
|
1055 |
//initial skip decision |
//initial skip decision |
1056 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1057 |
if (!(current->global_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
if (!(current->vol_flags & XVID_VOL_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
1058 |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) |
1059 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
1060 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1061 |
continue; |
continue; |
1063 |
} |
} |
1064 |
|
|
1065 |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1066 |
y, MotionFlags, current->global_flags, pMB->quant, |
y, MotionFlags, current->vol_flags, pMB->quant, |
1067 |
&Data, pParam, pMBs, reference->mbs, |
&Data, pParam, pMBs, reference->mbs, |
1068 |
current->global_flags & XVID_INTER4V, pMB); |
current->vop_flags & XVID_VOP_INTER4V, pMB); |
1069 |
|
|
1070 |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
1071 |
if (!(current->global_flags & XVID_GMC)) { |
if (!(current->vol_flags & XVID_VOL_GMC || current->vop_flags & XVID_VOP_MODEDECISION_BITS)) { |
1072 |
if ( pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
|
if (!(current->global_flags & XVID_MODEDECISION_BITS)) { |
|
1073 |
if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
1074 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) |
1075 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
|
} else { // BITS mode decision |
|
|
if (pMB->sad16 > 10) |
|
|
SkipMacroblockP(pMB, sad00); // more than 10 bits would be used for this MB - skip |
|
|
|
|
|
} |
|
1076 |
} |
} |
1077 |
} |
} |
1078 |
if (pMB->mode == MODE_INTRA) |
if (pMB->mode == MODE_INTRA) |
1080 |
} |
} |
1081 |
} |
} |
1082 |
|
|
1083 |
if (current->global_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ |
if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
1084 |
{ |
{ |
1085 |
current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
1086 |
} |
} |
1155 |
const int x, const int y, |
const int x, const int y, |
1156 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1157 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1158 |
const uint32_t GlobalFlags) |
const uint32_t VopFlags) |
1159 |
{ |
{ |
1160 |
|
|
1161 |
int mode = MODE_INTER; |
int mode = MODE_INTER; |
1162 |
|
|
1163 |
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision |
if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision |
|
// int intra = 0; |
|
1164 |
int sad; |
int sad; |
1165 |
int InterBias = MV16_INTER_BIAS; |
int InterBias = MV16_INTER_BIAS; |
1166 |
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
1167 |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { |
1168 |
mode = 0; //inter |
mode = MODE_INTER; |
1169 |
sad = Data->iMinSAD[0]; |
sad = Data->iMinSAD[0]; |
1170 |
} else { |
} else { |
1171 |
mode = MODE_INTER4V; |
mode = MODE_INTER4V; |
1193 |
dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) + |
dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) + |
1194 |
dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth); |
dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth); |
1195 |
|
|
1196 |
if (deviation < (sad - InterBias)) return MODE_INTRA;// intra |
if (deviation < (sad - InterBias)) return MODE_INTRA; |
1197 |
} |
} |
1198 |
return mode; |
return mode; |
1199 |
|
|
1202 |
int bits, intra, i; |
int bits, intra, i; |
1203 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
1204 |
Data->lambda16 = iQuant; |
Data->lambda16 = iQuant; |
1205 |
Data->lambda8 = pParam->m_quant_type; |
Data->lambda8 = (pParam->vol_flags & XVID_VOL_MPEGQUANT)?1:0; |
1206 |
|
|
1207 |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
1208 |
for (i = 0; i < 5; i++) { |
for (i = 0; i < 5; i++) { |
1214 |
if (bits == 0) return MODE_INTER; // quick stop |
if (bits == 0) return MODE_INTER; // quick stop |
1215 |
|
|
1216 |
if (inter4v) { |
if (inter4v) { |
1217 |
int inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
1218 |
if (inter4v < bits) { Data->iMinSAD[0] = bits = inter4v; mode = MODE_INTER4V; } |
if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; } |
1219 |
} |
} |
1220 |
|
|
|
|
|
1221 |
intra = CountMBBitsIntra(Data); |
intra = CountMBBitsIntra(Data); |
1222 |
|
|
1223 |
if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; } |
if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; } |
1235 |
const int x, |
const int x, |
1236 |
const int y, |
const int y, |
1237 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1238 |
const uint32_t GlobalFlags, |
const uint32_t VopFlags, |
1239 |
const uint32_t iQuant, |
const uint32_t iQuant, |
1240 |
SearchData * const Data, |
SearchData * const Data, |
1241 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1259 |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1260 |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1261 |
|
|
1262 |
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i; |
1263 |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i; |
1264 |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i; |
1265 |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i; |
1266 |
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1267 |
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1268 |
|
|
1269 |
Data->lambda16 = lambda_vec16[iQuant]; |
Data->lambda16 = lambda_vec16[iQuant]; |
1270 |
Data->lambda8 = lambda_vec8[iQuant]; |
Data->lambda8 = lambda_vec8[iQuant]; |
1271 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1272 |
|
|
1273 |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
if (pMB->dquant != 0) inter4v = 0; |
1274 |
|
|
1275 |
for(i = 0; i < 5; i++) |
memset(Data->currentMV, 0, 5*sizeof(VECTOR)); |
|
Data->currentMV[i].x = Data->currentMV[i].y = 0; |
|
1276 |
|
|
1277 |
if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1278 |
else Data->predMV = pmv[0]; |
else Data->predMV = pmv[0]; |
1284 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
1285 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1286 |
|
|
1287 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) || (x | y)) { |
if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) || (x | y)) { |
1288 |
threshA = Data->temp[0]; // that's where we keep this SAD atm |
threshA = Data->temp[0]; // that's where we keep this SAD atm |
1289 |
if (threshA < 512) threshA = 512; |
if (threshA < 512) threshA = 512; |
1290 |
else if (threshA > 1024) threshA = 1024; |
else if (threshA > 1024) threshA = 1024; |
1310 |
if ((Data->iMinSAD[0] <= threshA) || |
if ((Data->iMinSAD[0] <= threshA) || |
1311 |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
1312 |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) { |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) { |
1313 |
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) inter4v = 0; } |
if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) inter4v = 0; } |
1314 |
else { |
else { |
1315 |
|
|
1316 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
1317 |
if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; |
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch; |
1318 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
1319 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1320 |
|
|
1321 |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
1324 |
note that this search is/might be done in halfpel positions, |
note that this search is/might be done in halfpel positions, |
1325 |
which makes it more different than the diamond above */ |
which makes it more different than the diamond above */ |
1326 |
|
|
1327 |
if (MotionFlags & PMV_EXTSEARCH16) { |
if (MotionFlags & XVID_ME_EXTSEARCH16) { |
1328 |
int32_t bSAD; |
int32_t bSAD; |
1329 |
VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0]; |
VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0]; |
1330 |
if (Data->rrv) { |
if (Data->rrv) { |
1355 |
} |
} |
1356 |
} |
} |
1357 |
|
|
1358 |
if (MotionFlags & PMV_HALFPELREFINE16) |
if (MotionFlags & XVID_ME_HALFPELREFINE16) |
|
if ((!(MotionFlags & HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant) |
|
1359 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1360 |
|
|
1361 |
for(i = 0; i < 5; i++) { |
for(i = 0; i < 5; i++) { |
1363 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1364 |
} |
} |
1365 |
|
|
1366 |
if (MotionFlags & PMV_QUARTERPELREFINE16) |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
1367 |
if ((!(MotionFlags & QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { |
|
|
Data->qpel_precision = 1; |
|
1368 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1369 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1370 |
|
Data->qpel_precision = 1; |
1371 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1372 |
} |
} |
1373 |
|
|
1374 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
|
|
|
|
if (inter4v && (!(GlobalFlags & XVID_MODEDECISION_BITS) || |
|
|
(!(MotionFlags & QUARTERPELREFINE8_BITS)) || (!(MotionFlags & HALFPELREFINE8_BITS)) || |
|
|
((!(MotionFlags & EXTSEARCH_BITS)) && (!(MotionFlags&PMV_EXTSEARCH8)) ))) { |
|
|
// if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop |
|
1375 |
|
|
1376 |
|
if (inter4v) { |
1377 |
SearchData Data8; |
SearchData Data8; |
1378 |
memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data |
memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data |
1379 |
|
|
1382 |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
1383 |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
1384 |
|
|
1385 |
if ((Data->chroma) && (!(GlobalFlags & XVID_MODEDECISION_BITS))) { |
if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) { |
1386 |
// chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it |
// chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it |
1387 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
1388 |
const int div = 1 + Data->qpel; |
const int div = Data->qpel ? 2 : 1; |
1389 |
const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs; |
const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs; |
1390 |
|
|
1391 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
1398 |
} |
} |
1399 |
} |
} |
1400 |
|
|
1401 |
inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, GlobalFlags); |
inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, VopFlags); |
1402 |
|
|
1403 |
if (Data->rrv) { |
if (Data->rrv) { |
1404 |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
1457 |
|
|
1458 |
*(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; |
*(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; |
1459 |
|
|
1460 |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8|PMV_QUARTERPELREFINE8)) { |
if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { |
|
if (Data->rrv) i = 2; else i = 1; |
|
1461 |
|
|
1462 |
Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
if (Data->rrv) i = 16; else i = 8; |
|
Data->RefH = OldData->RefH + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
|
|
Data->RefV = OldData->RefV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
|
|
Data->RefHV = OldData->RefHV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
|
1463 |
|
|
1464 |
Data->Cur = OldData->Cur + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1465 |
|
Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1466 |
|
Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1467 |
|
Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1468 |
|
|
1469 |
|
Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1470 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1471 |
|
|
1472 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
1475 |
if (!Data->rrv) CheckCandidate = CheckCandidate8; |
if (!Data->rrv) CheckCandidate = CheckCandidate8; |
1476 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1477 |
|
|
1478 |
if (MotionFlags & PMV_EXTSEARCH8 && (!(MotionFlags & EXTSEARCH_BITS))) { |
if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) { |
1479 |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
1480 |
|
|
1481 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1482 |
if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch; |
if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch; |
1483 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; |
1484 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1485 |
|
|
1486 |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255); |
1491 |
} |
} |
1492 |
} |
} |
1493 |
|
|
1494 |
if (MotionFlags & PMV_HALFPELREFINE8) { |
if (MotionFlags & XVID_ME_HALFPELREFINE8) { |
1495 |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
1496 |
|
|
1497 |
SubpelRefine(Data); // perform halfpel refine of current best vector |
SubpelRefine(Data); // perform halfpel refine of current best vector |
1502 |
} |
} |
1503 |
} |
} |
1504 |
|
|
1505 |
if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE8) { |
if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) { |
1506 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1507 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
1508 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1600 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1601 |
Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache |
Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache |
1602 |
|
|
1603 |
Data->Ref = pRef->y + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
1604 |
Data->RefH = pRefH + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; |
1605 |
Data->RefV = pRefV + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; |
1606 |
Data->RefHV = pRefHV + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
1607 |
Data->RefCU = pRef->u + (x + y * Data->iEdgedWidth/2) * 8; |
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
1608 |
Data->RefCV = pRef->v + (x + y * Data->iEdgedWidth/2) * 8; |
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1609 |
|
|
1610 |
Data->predMV = *predMV; |
Data->predMV = *predMV; |
1611 |
|
|
1626 |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
1627 |
} |
} |
1628 |
|
|
1629 |
if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; |
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch; |
1630 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
1631 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1632 |
|
|
1633 |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
1687 |
|
|
1688 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
1689 |
dy += Data->directmvF[k].y / div; |
dy += Data->directmvF[k].y / div; |
1690 |
dx += Data->directmvF[0].x / div; |
dx += Data->directmvF[k].x / div; |
1691 |
b_dy += Data->directmvB[0].y / div; |
b_dy += Data->directmvB[k].y / div; |
1692 |
b_dx += Data->directmvB[0].x / div; |
b_dx += Data->directmvB[k].x / div; |
1693 |
} |
} |
1694 |
|
|
1695 |
dy = (dy >> 3) + roundtab_76[dy & 0xf]; |
dy = (dy >> 3) + roundtab_76[dy & 0xf]; |
1709 |
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
1710 |
stride); |
stride); |
1711 |
|
|
1712 |
if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped |
if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { |
1713 |
|
pMB->mode = MODE_DIRECT_NONE_MV; //skipped |
1714 |
|
for (k = 0; k < 4; k++) { |
1715 |
|
pMB->qmvs[k] = pMB->mvs[k]; |
1716 |
|
pMB->b_qmvs[k] = pMB->b_mvs[k]; |
1717 |
|
} |
1718 |
|
} |
1719 |
} |
} |
1720 |
|
|
1721 |
static __inline uint32_t |
static __inline uint32_t |
1743 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1744 |
|
|
1745 |
*Data->iMinSAD = 256*4096; |
*Data->iMinSAD = 256*4096; |
1746 |
Data->Ref = f_Ref->y + k; |
Data->RefP[0] = f_Ref->y + k; |
1747 |
Data->RefH = f_RefH + k; |
Data->RefP[2] = f_RefH + k; |
1748 |
Data->RefV = f_RefV + k; |
Data->RefP[1] = f_RefV + k; |
1749 |
Data->RefHV = f_RefHV + k; |
Data->RefP[3] = f_RefHV + k; |
1750 |
Data->bRef = b_Ref->y + k; |
Data->b_RefP[0] = b_Ref->y + k; |
1751 |
Data->bRefH = b_RefH + k; |
Data->b_RefP[2] = b_RefH + k; |
1752 |
Data->bRefV = b_RefV + k; |
Data->b_RefP[1] = b_RefV + k; |
1753 |
Data->bRefHV = b_RefHV + k; |
Data->b_RefP[3] = b_RefHV + k; |
1754 |
Data->RefCU = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
1755 |
Data->RefCV = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
1756 |
Data->b_RefCU = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
1757 |
Data->b_RefCV = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
1758 |
|
|
1759 |
k = Data->qpel ? 4 : 2; |
k = Data->qpel ? 4 : 2; |
1760 |
Data->max_dx = k * (pParam->width - x * 16); |
Data->max_dx = k * (pParam->width - x * 16); |
1804 |
} |
} |
1805 |
} |
} |
1806 |
|
|
1807 |
|
*Data->iMinSAD += Data->lambda16; |
1808 |
skip_sad = *Data->iMinSAD; |
skip_sad = *Data->iMinSAD; |
1809 |
|
|
1810 |
// DIRECT MODE DELTA VECTOR SEARCH. |
// DIRECT MODE DELTA VECTOR SEARCH. |
1811 |
// This has to be made more effective, but at the moment I'm happy it's running at all |
// This has to be made more effective, but at the moment I'm happy it's running at all |
1812 |
|
|
1813 |
if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; |
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch; |
1814 |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
1815 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1816 |
|
|
1817 |
MainSearchPtr(0, 0, Data, 255); |
MainSearchPtr(0, 0, Data, 255); |
1885 |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
1886 |
|
|
1887 |
i = (x + y * fData->iEdgedWidth) * 16; |
i = (x + y * fData->iEdgedWidth) * 16; |
|
bData.bRef = fData->Ref = f_Ref->y + i; |
|
|
bData.bRefH = fData->RefH = f_RefH + i; |
|
|
bData.bRefV = fData->RefV = f_RefV + i; |
|
|
bData.bRefHV = fData->RefHV = f_RefHV + i; |
|
|
bData.Ref = fData->bRef = b_Ref->y + i; |
|
|
bData.RefH = fData->bRefH = b_RefH + i; |
|
|
bData.RefV = fData->bRefV = b_RefV + i; |
|
|
bData.RefHV = fData->bRefHV = b_RefHV + i; |
|
|
bData.b_RefCU = fData->RefCU = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
|
|
bData.b_RefCV = fData->RefCV = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
|
|
bData.RefCU = fData->b_RefCU = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
|
|
bData.RefCV = fData->b_RefCV = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
|
1888 |
|
|
1889 |
|
bData.b_RefP[0] = fData->RefP[0] = f_Ref->y + i; |
1890 |
|
bData.b_RefP[2] = fData->RefP[2] = f_RefH + i; |
1891 |
|
bData.b_RefP[1] = fData->RefP[1] = f_RefV + i; |
1892 |
|
bData.b_RefP[3] = fData->RefP[3] = f_RefHV + i; |
1893 |
|
bData.RefP[0] = fData->b_RefP[0] = b_Ref->y + i; |
1894 |
|
bData.RefP[2] = fData->b_RefP[2] = b_RefH + i; |
1895 |
|
bData.RefP[1] = fData->b_RefP[1] = b_RefV + i; |
1896 |
|
bData.RefP[3] = fData->b_RefP[3] = b_RefHV + i; |
1897 |
|
bData.b_RefP[4] = fData->RefP[4] = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
1898 |
|
bData.b_RefP[5] = fData->RefP[5] = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
1899 |
|
bData.RefP[4] = fData->b_RefP[4] = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
1900 |
|
bData.RefP[5] = fData->b_RefP[5] = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
1901 |
|
|
1902 |
bData.bpredMV = fData->predMV = *f_predMV; |
bData.bpredMV = fData->predMV = *f_predMV; |
1903 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
2020 |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
2021 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
2022 |
Data.lambda16 = lambda_vec16[frame->quant]; |
Data.lambda16 = lambda_vec16[frame->quant]; |
2023 |
Data.qpel = pParam->m_quarterpel; |
Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL; |
2024 |
Data.rounding = 0; |
Data.rounding = 0; |
2025 |
Data.chroma = frame->motion_flags & PMV_CHROMA8; |
Data.chroma = frame->motion_flags & XVID_ME_CHROMA8; |
2026 |
Data.temp = temp; |
Data.temp = temp; |
2027 |
|
|
2028 |
Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose) |
Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose) |
2130 |
{ |
{ |
2131 |
|
|
2132 |
int i, mask; |
int i, mask; |
2133 |
|
int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0; |
2134 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
2135 |
MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
2136 |
|
|
2137 |
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
2138 |
|
|
2146 |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
2147 |
|
|
2148 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
2149 |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0); |
2150 |
|
|
2151 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
2152 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16; |
2153 |
|
|
2154 |
pmv[1].x = EVEN(pMB->mvs[0].x); |
pmv[1].x = EVEN(pMB->mvs[0].x); |
2155 |
pmv[1].y = EVEN(pMB->mvs[0].y); |
pmv[1].y = EVEN(pMB->mvs[0].y); |
2159 |
|
|
2160 |
CheckCandidate32I(0, 0, 255, &i, Data); |
CheckCandidate32I(0, 0, 255, &i, Data); |
2161 |
|
|
2162 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) { |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) { |
2163 |
|
|
2164 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
2165 |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
2166 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
2167 |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
2168 |
|
|
2169 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) // diamond only if needed |
2170 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
2171 |
|
} |
2172 |
|
|
2173 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
2174 |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
2177 |
MB->sad16 = Data->iMinSAD[i+1]; |
MB->sad16 = Data->iMinSAD[i+1]; |
2178 |
} |
} |
2179 |
} |
} |
|
} |
|
2180 |
|
|
2181 |
#define INTRA_BIAS 2500 |
#define INTRA_THRESH 1800 |
2182 |
#define INTRA_THRESH 1500 |
#define INTER_THRESH 1200 |
|
#define INTER_THRESH 1400 |
|
2183 |
|
|
2184 |
int |
int |
2185 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
2186 |
FRAMEINFO * const Current, |
const FRAMEINFO * const Current, |
2187 |
MBParam * const pParam, |
const MBParam * const pParam, |
2188 |
int maxIntra, //maximum number if non-I frames |
const int maxIntra, //maximum number if non-I frames |
2189 |
int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
const int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
2190 |
int bCount) // number of B frames in a row |
const int bCount, // number of B frames in a row |
2191 |
|
const int b_thresh) |
2192 |
{ |
{ |
2193 |
uint32_t x, y, intra = 0; |
uint32_t x, y, intra = 0; |
2194 |
int sSAD = 0; |
int sSAD = 0; |
2195 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
2196 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
2197 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh; |
2198 |
|
int s = 0, blocks = 0; |
2199 |
|
|
2200 |
int32_t iMinSAD[5], temp[5]; |
int32_t iMinSAD[5], temp[5]; |
2201 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
2204 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; |
2205 |
Data.iMinSAD = iMinSAD; |
Data.iMinSAD = iMinSAD; |
2206 |
Data.iFcode = Current->fcode; |
Data.iFcode = Current->fcode; |
|
Data.rrv = Current->global_flags & XVID_REDUCED; |
|
2207 |
Data.temp = temp; |
Data.temp = temp; |
2208 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2209 |
|
|
2210 |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
2211 |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
2212 |
else |
else |
2213 |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
2214 |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
2215 |
|
|
2216 |
InterThresh += 400 * (1 - bCount); |
InterThresh -= (350 - 8*b_thresh) * bCount; |
2217 |
if (InterThresh < 300) InterThresh = 300; |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
2218 |
|
|
2219 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2220 |
|
|
2221 |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
2222 |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
2223 |
int i; |
int i; |
2224 |
|
blocks += 4; |
2225 |
|
|
2226 |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
2227 |
|
else { //extrapolation of the vector found for last frame |
2228 |
|
pMBs[x + y * pParam->mb_width].mvs[0].x = |
2229 |
|
(pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount; |
2230 |
|
pMBs[x + y * pParam->mb_width].mvs[0].y = |
2231 |
|
(pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount; |
2232 |
|
} |
2233 |
|
|
2234 |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
2235 |
|
|
2241 |
pParam->edged_width); |
pParam->edged_width); |
2242 |
if (dev + IntraThresh < pMB->sad16) { |
if (dev + IntraThresh < pMB->sad16) { |
2243 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
2244 |
if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP; |
if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP; |
2245 |
} |
} |
2246 |
} |
} |
2247 |
|
if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++; |
2248 |
|
|
2249 |
sSAD += pMB->sad16; |
sSAD += pMB->sad16; |
2250 |
} |
} |
2251 |
} |
} |
2252 |
} |
} |
2253 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
|
2254 |
// if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP; |
sSAD /= blocks; |
2255 |
|
s = (10*s) / blocks; |
2256 |
|
|
2257 |
|
if (s > 4) sSAD += (s - 2) * (60 - 2*b_thresh); //static block - looks bad when in bframe... |
2258 |
|
|
2259 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2260 |
emms(); |
emms(); |
2261 |
return B_VOP; |
return B_VOP; |
|
|
|
2262 |
} |
} |
2263 |
|
|
2264 |
|
|
2308 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
2309 |
continue; |
continue; |
2310 |
|
|
2311 |
if ( ( (ABS(mv.x - (pMB-1)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-1)->mvs[0].y) < deltay) ) |
if ( ( (abs(mv.x - (pMB-1)->mvs[0].x) < deltax) && (abs(mv.y - (pMB-1)->mvs[0].y) < deltay) ) |
2312 |
&& ( (ABS(mv.x - (pMB+1)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+1)->mvs[0].y) < deltay) ) |
&& ( (abs(mv.x - (pMB+1)->mvs[0].x) < deltax) && (abs(mv.y - (pMB+1)->mvs[0].y) < deltay) ) |
2313 |
&& ( (ABS(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-MBw)->mvs[0].y) < deltay) ) |
&& ( (abs(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB-MBw)->mvs[0].y) < deltay) ) |
2314 |
&& ( (ABS(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) ) |
&& ( (abs(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) ) |
2315 |
MBmask[mbnum]=1; |
MBmask[mbnum]=1; |
2316 |
} |
} |
2317 |
|
|
2386 |
continue; |
continue; |
2387 |
|
|
2388 |
oldnum++; |
oldnum++; |
2389 |
meanx += ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ); |
meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ); |
2390 |
meany += ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ); |
meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ); |
2391 |
} |
} |
2392 |
|
|
2393 |
if (4*meanx > oldnum) /* better fit than 0.25 is useless */ |
if (4*meanx > oldnum) /* better fit than 0.25 is useless */ |
2414 |
if (!MBmask[mbnum]) |
if (!MBmask[mbnum]) |
2415 |
continue; |
continue; |
2416 |
|
|
2417 |
if ( ( ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx ) |
if ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx ) |
2418 |
|| ( ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) ) |
|| ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) ) |
2419 |
MBmask[mbnum]=0; |
MBmask[mbnum]=0; |
2420 |
else |
else |
2421 |
num++; |
num++; |
2469 |
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) |
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) |
2470 |
return 0; //quick stop |
return 0; //quick stop |
2471 |
|
|
2472 |
if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search |
if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search |
2473 |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
2474 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
2475 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
2487 |
} |
} |
2488 |
} |
} |
2489 |
|
|
2490 |
if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
2491 |
|
|
2492 |
if (MotionFlags&HALFPELREFINE16_BITS) SubpelRefine(Data); |
if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data); |
2493 |
|
|
2494 |
if (Data->qpel) { |
if (Data->qpel) { |
2495 |
if (MotionFlags&(EXTSEARCH_BITS | HALFPELREFINE16_BITS)) { // there was halfpel-precision search |
if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { // there was halfpel-precision search |
2496 |
for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { |
for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { |
2497 |
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match |
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match |
2498 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
2503 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
2504 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
2505 |
} |
} |
2506 |
if (MotionFlags&QUARTERPELREFINE16_BITS) SubpelRefine(Data); |
if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data); |
2507 |
} |
} |
2508 |
|
|
2509 |
if (MotionFlags&CHECKPREDICTION_BITS) { //let's check vector equal to prediction |
if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { //let's check vector equal to prediction |
2510 |
VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; |
VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; |
2511 |
if (!(Data->predMV.x == v->x && Data->predMV.y == v->y)) |
if (!(Data->predMV.x == v->x && Data->predMV.y == v->y)) |
2512 |
CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data); |
CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data); |
2526 |
int cbp = 0, bits = 0, t = 0, i, iDirection; |
int cbp = 0, bits = 0, t = 0, i, iDirection; |
2527 |
SearchData Data2, *Data8 = &Data2; |
SearchData Data2, *Data8 = &Data2; |
2528 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
2529 |
int16_t in[64], coeff[64]; |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
2530 |
|
|
2531 |
memcpy(Data8, Data, sizeof(SearchData)); |
memcpy(Data8, Data, sizeof(SearchData)); |
2532 |
CheckCandidate = CheckCandidateBits8; |
CheckCandidate = CheckCandidateBits8; |
2536 |
Data8->currentMV = Data->currentMV + i + 1; |
Data8->currentMV = Data->currentMV + i + 1; |
2537 |
Data8->currentQMV = Data->currentQMV + i + 1; |
Data8->currentQMV = Data->currentQMV + i + 1; |
2538 |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2539 |
Data8->Ref = Data->Ref + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2540 |
Data8->RefH = Data->RefH + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2541 |
Data8->RefV = Data->RefV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2542 |
Data8->RefHV = Data->RefHV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2543 |
|
|
2544 |
if(Data->qpel) { |
if(Data->qpel) { |
2545 |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
2558 |
|
|
2559 |
Data8->qpel_precision = Data8->qpel; |
Data8->qpel_precision = Data8->qpel; |
2560 |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
2561 |
if (Data8->qpel) { |
{ |
2562 |
if (!(Data8->currentQMV->x == backup[i+1].x && Data8->currentQMV->y == backup[i+1].y)) |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
2563 |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
if (!( (v->x == backup[i+1].x) && (v->y == backup[i+1].y) )) |
|
} else { |
|
|
if (!(Data8->currentMV->x == backup[i+1].x && Data8->currentMV->y == backup[i+1].y)) |
|
2564 |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
2565 |
} |
} |
2566 |
|
|
2567 |
if (Data8->qpel) { |
if (Data8->qpel) { |
2568 |
if (MotionFlags&HALFPELREFINE8_BITS || (MotionFlags&PMV_EXTSEARCH8 && MotionFlags&EXTSEARCH_BITS)) { // halfpixel motion search follows |
if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { // halfpixel motion search follows |
2569 |
int32_t s = *Data8->iMinSAD; |
int32_t s = *Data8->iMinSAD; |
2570 |
Data8->currentMV->x = Data8->currentQMV->x/2; |
Data8->currentMV->x = Data8->currentQMV->x/2; |
2571 |
Data8->currentMV->y = Data8->currentQMV->y/2; |
Data8->currentMV->y = Data8->currentQMV->y/2; |
2576 |
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
2577 |
CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8); |
CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8); |
2578 |
|
|
2579 |
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) |
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) |
2580 |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2581 |
|
|
2582 |
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); |
2583 |
|
|
2584 |
if(s > *Data8->iMinSAD) { //we have found a better match |
if(s > *Data8->iMinSAD) { //we have found a better match |
2585 |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
2591 |
pParam->width, pParam->height, Data8->iFcode, 1, 0); |
pParam->width, pParam->height, Data8->iFcode, 1, 0); |
2592 |
|
|
2593 |
} |
} |
2594 |
if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
2595 |
|
|
2596 |
} else // not qpel |
} else // not qpel |
2597 |
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement |
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement |
2598 |
|
|
2599 |
//checking vector equal to predicion |
//checking vector equal to predicion |
2600 |
if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) { |
if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) { |
2601 |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
2602 |
if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y)) |
if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y)) |
2603 |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
2630 |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
2631 |
|
|
2632 |
//chroma U |
//chroma U |
2633 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCU, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2634 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
2635 |
fdct(in); |
fdct(in); |
2636 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
2642 |
|
|
2643 |
if (bits < *Data->iMinSAD) { // still possible |
if (bits < *Data->iMinSAD) { // still possible |
2644 |
//chroma V |
//chroma V |
2645 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCV, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2646 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
2647 |
fdct(in); |
fdct(in); |
2648 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
2651 |
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
2652 |
cbp |= 1 << (5 - 5); |
cbp |= 1 << (5 - 5); |
2653 |
} |
} |
2654 |
bits += cbpy_tab[15-(cbp>>2)].len; |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
2655 |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
2656 |
} |
} |
2657 |
} |
} |
2664 |
CountMBBitsIntra(const SearchData * const Data) |
CountMBBitsIntra(const SearchData * const Data) |
2665 |
{ |
{ |
2666 |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
2667 |
int cbp = 0, i, t, dc = 0, b_dc = 1024; |
int cbp = 0, i, t, dc = 1024, b_dc; |
2668 |
const uint32_t iQuant = Data->lambda16; |
const uint32_t iQuant = Data->lambda16; |
2669 |
int16_t in[64], coeff[64]; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
2670 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, 1);; |
2671 |
|
|
2672 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, 1); |
|
|
|
|
2673 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2674 |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
2675 |
fdct(in); |
fdct(in); |
2676 |
b_dc = dc; |
b_dc = in[0]; |
2677 |
dc = in[0]; |
in[0] -= dc; |
2678 |
in[0] -= b_dc; |
dc = b_dc; |
2679 |
if (Data->lambda8 == 0) quant_intra_c(coeff, in, iQuant, iDcScaler); |
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
2680 |
else quant4_intra_c(coeff, in, iQuant, iDcScaler); |
else quant4_intra(coeff, in, iQuant, iDcScaler); |
|
|
|
|
b_dc = dc; |
|
|
dc = coeff[0]; |
|
|
if (i != 0) coeff[0] -= b_dc; |
|
2681 |
|
|
2682 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;; |
2683 |
Data->temp[i] = t; |
Data->temp[i] = t; |
2686 |
} |
} |
2687 |
|
|
2688 |
if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma |
if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma |
2689 |
uint32_t iDcScaler = get_dc_scaler(iQuant, 0); |
iDcScaler = get_dc_scaler(iQuant, 0); |
2690 |
//chroma U |
//chroma U |
2691 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
2692 |
fdct(in); |
fdct(in); |
2696 |
|
|
2697 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2698 |
if (t != 0) cbp |= 1 << (5 - 4); |
if (t != 0) cbp |= 1 << (5 - 4); |
|
Data->temp[4] = t; |
|
2699 |
|
|
2700 |
if (bits < Data->iMinSAD[0]) { |
if (bits < Data->iMinSAD[0]) { |
2701 |
//chroma V |
//chroma V |
2708 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2709 |
if (t != 0) cbp |= 1 << (5 - 5); |
if (t != 0) cbp |= 1 << (5 - 5); |
2710 |
|
|
2711 |
Data->temp[5] = t; |
bits += xvid_cbpy_tab[cbp>>2].len; |
2712 |
|
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
bits += t = cbpy_tab[cbp>>2].len; |
|
|
Data->temp[6] = t; |
|
|
|
|
|
bits += t = mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
|
Data->temp[7] = t; |
|
|
|
|
2713 |
} |
} |
2714 |
} |
} |
|
|
|
2715 |
return bits; |
return bits; |
2716 |
} |
} |