31 |
#include <assert.h> |
#include <assert.h> |
32 |
#include <stdio.h> |
#include <stdio.h> |
33 |
#include <stdlib.h> |
#include <stdlib.h> |
34 |
|
#include <string.h> // memcpy |
35 |
|
|
36 |
#include "../encoder.h" |
#include "../encoder.h" |
37 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
53 |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
54 |
|
|
55 |
static __inline int |
static __inline int |
56 |
d_mv_bits(int x, int y, 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) |
57 |
{ |
{ |
58 |
int xb, yb; |
int xb, yb; |
59 |
if (qpel) { x *= 2; y *= 2;} |
if (qpel) { x *= 2; y *= 2;} |
60 |
else if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
else if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
61 |
|
x -= pred.x; |
62 |
|
y -= pred.y; |
63 |
|
|
64 |
if (x == 0) xb = 1; |
if (x) { |
|
else { |
|
65 |
if (x < 0) x = -x; |
if (x < 0) x = -x; |
66 |
x += (1 << (iFcode - 1)) - 1; |
x += (1 << (iFcode - 1)) - 1; |
67 |
x >>= (iFcode - 1); |
x >>= (iFcode - 1); |
68 |
if (x > 32) x = 32; |
if (x > 32) x = 32; |
69 |
xb = mvtab[x] + iFcode; |
xb = mvtab[x] + iFcode; |
70 |
} |
} else xb = 1; |
71 |
|
|
72 |
if (y == 0) yb = 1; |
if (y) { |
|
else { |
|
73 |
if (y < 0) y = -y; |
if (y < 0) y = -y; |
74 |
y += (1 << (iFcode - 1)) - 1; |
y += (1 << (iFcode - 1)) - 1; |
75 |
y >>= (iFcode - 1); |
y >>= (iFcode - 1); |
76 |
if (y > 32) y = 32; |
if (y > 32) y = 32; |
77 |
yb = mvtab[y] + iFcode; |
yb = mvtab[y] + iFcode; |
78 |
} |
} else yb = 1; |
79 |
return xb + yb; |
return xb + yb; |
80 |
} |
} |
81 |
|
|
83 |
ChromaSAD(int dx, int dy, const SearchData * const data) |
ChromaSAD(int dx, int dy, const SearchData * const data) |
84 |
{ |
{ |
85 |
int sad; |
int sad; |
86 |
|
const uint32_t stride = data->iEdgedWidth/2; |
87 |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
88 |
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
89 |
|
|
90 |
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 |
91 |
|
data->temp[5] = dx; data->temp[6] = dy; // backup |
92 |
|
|
93 |
switch (((dx & 1) << 1) | (dy & 1)) { |
switch (((dx & 1) << 1) | (dy & 1)) { |
94 |
case 0: |
case 0: |
95 |
sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
dx = dx / 2; dy = dy / 2; |
96 |
sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
sad = sad8(data->CurU, data->RefCU + dy * stride + dx, stride); |
97 |
|
sad += sad8(data->CurV, data->RefCV + dy * stride + dx, stride); |
98 |
break; |
break; |
99 |
case 1: |
case 1: |
100 |
dx = dx / 2; dy = (dy - 1) / 2; |
dx = dx / 2; dy = (dy - 1) / 2; |
101 |
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + (dy+1) * stride + dx, stride); |
102 |
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + (dy+1) * stride + dx, stride); |
103 |
break; |
break; |
104 |
case 2: |
case 2: |
105 |
dx = (dx - 1) / 2; dy = dy / 2; |
dx = (dx - 1) / 2; dy = dy / 2; |
106 |
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + dy * stride + dx+1, stride); |
107 |
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + dy * stride + dx+1, stride); |
108 |
break; |
break; |
109 |
default: |
default: |
110 |
dx = (dx - 1) / 2; dy = (dy - 1) / 2; |
dx = (dx - 1) / 2; dy = (dy - 1) / 2; |
111 |
interpolate8x8_halfpel_hv(data->RefQ, |
interpolate8x8_halfpel_hv(data->RefQ, data->RefCU + dy * stride + dx, stride, data->rounding); |
112 |
data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
sad = sad8(data->CurU, data->RefQ, stride); |
113 |
data->rounding); |
|
114 |
sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2); |
interpolate8x8_halfpel_hv(data->RefQ, data->RefCV + dy * stride + dx, stride, data->rounding); |
115 |
interpolate8x8_halfpel_hv(data->RefQ, |
sad += sad8(data->CurV, data->RefQ, stride); |
|
data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
|
|
data->rounding); |
|
|
sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2); |
|
116 |
break; |
break; |
117 |
} |
} |
118 |
data->temp[5] = dx; data->temp[6] = dy; data->temp[7] = sad; //backup |
data->temp[7] = sad; //backup, part 2 |
119 |
return sad; |
return sad; |
120 |
} |
} |
121 |
|
|
122 |
static __inline const uint8_t * |
static __inline const uint8_t * |
123 |
GetReference(const int x, const int y, const int dir, const SearchData * const data) |
GetReferenceB(const int x, const int y, const int dir, const SearchData * const data) |
124 |
{ |
{ |
125 |
// dir : 0 = forward, 1 = backward |
// dir : 0 = forward, 1 = backward |
126 |
switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) { |
switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) { |
135 |
} |
} |
136 |
} |
} |
137 |
|
|
138 |
|
// this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate |
139 |
|
static __inline const uint8_t * |
140 |
|
GetReference(const int x, const int y, const SearchData * const data) |
141 |
|
{ |
142 |
|
switch ( ((x&1)<<1) | (y&1) ) { |
143 |
|
case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); |
144 |
|
case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
145 |
|
case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); |
146 |
|
default : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
static uint8_t * |
static uint8_t * |
151 |
Interpolate8x8qpel(const int x, const int y, const int block, const int dir, const SearchData * const data) |
Interpolate8x8qpel(const int x, const int y, const int block, const int dir, const SearchData * const data) |
152 |
{ |
{ |
158 |
const int halfpel_y = y/2; |
const int halfpel_y = y/2; |
159 |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
160 |
|
|
161 |
ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases |
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); |
162 |
ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
163 |
switch( ((x&1)<<1) + (y&1) ) { |
switch( ((x&1)<<1) + (y&1) ) { |
164 |
case 0: // pure halfpel position |
case 0: // pure halfpel position |
165 |
Reference = (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data); |
return (uint8_t *) ref1; |
|
Reference += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
|
166 |
break; |
break; |
167 |
|
|
168 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
169 |
ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data); |
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
170 |
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
171 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
172 |
break; |
break; |
173 |
|
|
174 |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
175 |
ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data); |
ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); |
176 |
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
177 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
178 |
break; |
break; |
179 |
|
|
180 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // x and y in qpel resolution - the "corners" (top left/right and |
181 |
// bottom left/right) during qpel refinement |
// bottom left/right) during qpel refinement |
182 |
ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data); |
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
183 |
ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data); |
ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); |
184 |
ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data); |
ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data); |
185 |
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
186 |
ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
187 |
ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
202 |
const int halfpel_y = y/2; |
const int halfpel_y = y/2; |
203 |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
204 |
|
|
205 |
ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases |
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); |
206 |
switch( ((x&1)<<1) + (y&1) ) { |
switch( ((x&1)<<1) + (y&1) ) { |
207 |
case 0: // pure halfpel position |
case 0: // pure halfpel position |
208 |
return (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data); |
return (uint8_t *) ref1; |
209 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
210 |
ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data); |
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
211 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
212 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8); |
213 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8); |
215 |
break; |
break; |
216 |
|
|
217 |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
218 |
ref2 = GetReference(x - halfpel_x, halfpel_y, dir, data); |
ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); |
219 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
220 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8); |
221 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8); |
224 |
|
|
225 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // x and y in qpel resolution - the "corners" (top left/right and |
226 |
// bottom left/right) during qpel refinement |
// bottom left/right) during qpel refinement |
227 |
ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data); |
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
228 |
ref3 = GetReference(x - halfpel_x, halfpel_y, dir, data); |
ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); |
229 |
ref4 = GetReference(x - halfpel_x, y - halfpel_y, dir, data); |
ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data); |
230 |
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
231 |
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); |
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); |
232 |
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); |
253 |
xc = x/2; yc = y/2; //for chroma sad |
xc = x/2; yc = y/2; //for chroma sad |
254 |
current = data->currentQMV; |
current = data->currentQMV; |
255 |
} else { |
} else { |
256 |
Reference = GetReference(x, y, 0, data); |
Reference = GetReference(x, y, data); |
257 |
current = data->currentMV; |
current = data->currentMV; |
258 |
xc = x; yc = y; |
xc = x; yc = y; |
259 |
} |
} |
260 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
261 |
|
|
262 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
263 |
|
|
292 |
( x > data->max_dx) || ( x < data->min_dx) |
( x > data->max_dx) || ( x < data->min_dx) |
293 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
294 |
|
|
295 |
Reference = GetReference(x, y, 0, data); |
Reference = GetReference(x, y, data); |
296 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, 0, 1); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1); |
297 |
|
|
298 |
data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
299 |
|
|
333 |
Reference = Interpolate16x16qpel(x, y, 0, data); |
Reference = Interpolate16x16qpel(x, y, 0, data); |
334 |
current = data->currentQMV; |
current = data->currentQMV; |
335 |
} else { |
} else { |
336 |
Reference = GetReference(x, y, 0, data); |
Reference = GetReference(x, y, data); |
337 |
current = data->currentMV; |
current = data->currentMV; |
338 |
} |
} |
339 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, |
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
340 |
data->qpel && !data->qpel_precision && !data->rrv, data->rrv); |
data->qpel && !data->qpel_precision, data->rrv); |
341 |
|
|
342 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
343 |
sad += (data->lambda16 * t * sad)/1000; |
sad += (data->lambda16 * t * sad)/1000; |
349 |
} |
} |
350 |
|
|
351 |
static void |
static void |
352 |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
353 |
{ |
{ |
354 |
// maximum speed - for P/B/I decision |
// maximum speed - for P/B/I decision |
|
int32_t sad; |
|
355 |
|
|
356 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
357 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
358 |
|
|
359 |
sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
data->temp[0] = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
360 |
data->iEdgedWidth, 256*4096); |
data->iEdgedWidth, data->temp+1); |
361 |
|
if (data->temp[0] < *(data->iMinSAD)) { |
362 |
if (sad < *(data->iMinSAD)) { |
*(data->iMinSAD) = data->temp[0]; |
|
*(data->iMinSAD) = sad; |
|
363 |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
364 |
*dir = Direction; } |
*dir = Direction; } |
365 |
} |
if (data->temp[1] < data->iMinSAD[1]) { |
366 |
|
data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } |
367 |
|
if (data->temp[2] < data->iMinSAD[2]) { |
368 |
|
data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; } |
369 |
|
if (data->temp[3] < data->iMinSAD[3]) { |
370 |
|
data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; } |
371 |
|
if (data->temp[4] < data->iMinSAD[4]) { |
372 |
|
data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; } |
373 |
|
|
374 |
|
} |
375 |
|
|
376 |
static void |
static void |
377 |
CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data) |
390 |
current = data->currentQMV; |
current = data->currentQMV; |
391 |
ReferenceB = Interpolate16x16qpel(xb, yb, 1, data); |
ReferenceB = Interpolate16x16qpel(xb, yb, 1, data); |
392 |
} else { |
} else { |
393 |
ReferenceF = GetReference(xf, yf, 0, data); |
ReferenceF = GetReference(xf, yf, data); |
394 |
xb = data->currentMV[1].x; yb = data->currentMV[1].y; |
xb = data->currentMV[1].x; yb = data->currentMV[1].y; |
395 |
ReferenceB = GetReference(xb, yb, 1, data); |
ReferenceB = GetReferenceB(xb, yb, 1, data); |
396 |
current = data->currentMV; |
current = data->currentMV; |
397 |
} |
} |
398 |
|
|
399 |
t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0) |
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0) |
400 |
+ d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
401 |
|
|
402 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
403 |
sad += (data->lambda16 * t * sad)/1000; |
sad += (data->lambda16 * t * sad)/1000; |
416 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
417 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
418 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
419 |
|
const VECTOR zeroMV={0,0}; |
420 |
|
|
421 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
422 |
|
|
449 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
450 |
} |
} |
451 |
|
|
452 |
sad += (data->lambda16 * d_mv_bits(x, y, 1, 0, 0) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)/1000; |
453 |
|
|
454 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
455 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
464 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
465 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
466 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
467 |
|
const VECTOR zeroMV = {0,0}; |
468 |
|
|
469 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
470 |
|
|
491 |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
492 |
|
|
493 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
494 |
sad += (data->lambda16 * d_mv_bits(x, y, 1, 0, 0) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)/1000; |
495 |
|
|
496 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
497 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
509 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
510 |
|
|
511 |
if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data); |
if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data); |
512 |
else Reference = GetReference(x, y, 0, data); |
else Reference = GetReference(x, y, data); |
513 |
|
|
514 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
515 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
516 |
|
|
517 |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
518 |
|
|
682 |
backupMV = *(data->currentQMV); |
backupMV = *(data->currentQMV); |
683 |
else backupMV = *(data->currentMV); |
else backupMV = *(data->currentMV); |
684 |
|
|
685 |
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0); |
CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0); |
686 |
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0); |
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0); |
|
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0); |
|
|
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0); |
|
|
|
|
|
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0); |
|
687 |
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0); |
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0); |
688 |
|
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0); |
689 |
CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0); |
CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0); |
690 |
CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0); |
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0); |
691 |
|
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0); |
692 |
|
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0); |
693 |
} |
} |
694 |
|
|
695 |
static __inline int |
static __inline int |
1014 |
if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1015 |
else Data->predMV = pmv[0]; |
else Data->predMV = pmv[0]; |
1016 |
|
|
1017 |
i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode, 0, 0); |
i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0); |
1018 |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
1019 |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
1020 |
Data->iMinSAD[2] = pMB->sad8[1]; |
Data->iMinSAD[2] = pMB->sad8[1]; |
1100 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1101 |
} |
} |
1102 |
|
|
1103 |
if((!Data->rrv) && (pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
if((Data->qpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
1104 |
|
|
1105 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1106 |
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, |
1144 |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
1145 |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
1146 |
} |
} |
1147 |
|
|
1148 |
if (!(inter4v) || |
if (!(inter4v) || |
1149 |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
1150 |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
1190 |
|
|
1191 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1192 |
Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
1193 |
if (block != 0) i = d_mv_bits( Data->currentQMV->x - Data->predMV.x, |
if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y, |
1194 |
Data->currentQMV->y - Data->predMV.y, Data->iFcode, 0, 0); |
Data->predMV, Data->iFcode, 0, 0); |
1195 |
|
|
1196 |
} else { |
} else { |
1197 |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
1198 |
if (block != 0) { |
if (block != 0) { |
1199 |
if (block != 0) i = d_mv_bits( Data->currentMV->x - Data->predMV.x, |
if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y, |
1200 |
Data->currentMV->y - Data->predMV.y, Data->iFcode, 0, Data->rrv); |
Data->predMV, Data->iFcode, 0, Data->rrv); |
1201 |
} |
} |
1202 |
} |
} |
1203 |
|
|
1247 |
} |
} |
1248 |
} |
} |
1249 |
|
|
1250 |
if(!Data->rrv && Data->qpel) { |
if(Data->qpel) { |
1251 |
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
1252 |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
1253 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1345 |
|
|
1346 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
1347 |
|
|
1348 |
int i, iDirection, mask; |
int i, iDirection = 255, mask; |
1349 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
1350 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1351 |
*Data->iMinSAD = MV_MAX_ERROR; |
*Data->iMinSAD = MV_MAX_ERROR; |
1370 |
CheckCandidate = CheckCandidate16no4v; |
CheckCandidate = CheckCandidate16no4v; |
1371 |
|
|
1372 |
// main loop. checking all predictions |
// main loop. checking all predictions |
1373 |
for (i = 0; i < 8; i++) { |
for (i = 0; i < 7; i++) { |
1374 |
if (!(mask = make_mask(pmv, i)) ) continue; |
if (!(mask = make_mask(pmv, i)) ) continue; |
1375 |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
1376 |
} |
} |
1381 |
MainSearchPtr = AdvDiamondSearch; |
MainSearchPtr = AdvDiamondSearch; |
1382 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1383 |
|
|
1384 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
1385 |
|
|
1386 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1387 |
|
|
1388 |
if (Data->qpel) { |
if (Data->qpel && *Data->iMinSAD < *best_sad + 300) { |
1389 |
Data->currentQMV->x = 2*Data->currentMV->x; |
Data->currentQMV->x = 2*Data->currentMV->x; |
1390 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
1391 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1395 |
} |
} |
1396 |
|
|
1397 |
// three bits are needed to code backward mode. four for forward |
// three bits are needed to code backward mode. four for forward |
1398 |
// we treat the bits just like they were vector's |
|
1399 |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
1400 |
else *Data->iMinSAD += 3 * Data->lambda16; |
else *Data->iMinSAD += 3 * Data->lambda16; |
1401 |
|
|
1413 |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
1414 |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
1415 |
} |
} |
1416 |
if (mode_current == MODE_FORWARD) |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
1417 |
pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV; |
else pMB->b_mvs[0] = *Data->currentMV; |
|
else |
|
|
pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
|
|
|
|
1418 |
} |
} |
1419 |
|
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
1420 |
|
else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
1421 |
|
|
1422 |
} |
} |
1423 |
|
|
1552 |
} |
} |
1553 |
} |
} |
1554 |
|
|
|
|
|
1555 |
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
1556 |
else CheckCandidate = CheckCandidateDirectno4v; |
else CheckCandidate = CheckCandidateDirectno4v; |
1557 |
|
|
1578 |
|
|
1579 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1580 |
|
|
1581 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V || Data->qpel) pMB->mode = MODE_DIRECT; |
|
pMB->mode = MODE_DIRECT; |
|
1582 |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
1583 |
|
|
1584 |
pMB->pmvs[3] = *Data->currentMV; |
pMB->pmvs[3] = *Data->currentMV; |
1695 |
} while (!(iDirection)); |
} while (!(iDirection)); |
1696 |
|
|
1697 |
if (fData->qpel) { |
if (fData->qpel) { |
1698 |
|
if (*fData->iMinSAD > *best_sad + 500) return; |
1699 |
CheckCandidate = CheckCandidateInt; |
CheckCandidate = CheckCandidateInt; |
1700 |
fData->qpel_precision = bData.qpel_precision = 1; |
fData->qpel_precision = bData.qpel_precision = 1; |
1701 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0); |
1705 |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
1706 |
fData->currentQMV[1].y = 2 * fData->currentMV[1].y; |
fData->currentQMV[1].y = 2 * fData->currentMV[1].y; |
1707 |
SubpelRefine(fData); |
SubpelRefine(fData); |
1708 |
|
if (*fData->iMinSAD > *best_sad + 300) return; |
1709 |
fData->currentQMV[2] = fData->currentQMV[0]; |
fData->currentQMV[2] = fData->currentQMV[0]; |
1710 |
SubpelRefine(&bData); |
SubpelRefine(&bData); |
1711 |
} |
} |
1712 |
|
|
1713 |
*fData->iMinSAD += (2+2) * fData->lambda16; // two bits are needed to code interpolate mode. |
*fData->iMinSAD += (2+3) * fData->lambda16; // two bits are needed to code interpolate mode. |
1714 |
|
|
1715 |
if (*fData->iMinSAD < *best_sad) { |
if (*fData->iMinSAD < *best_sad) { |
1716 |
*best_sad = *fData->iMinSAD; |
*best_sad = *fData->iMinSAD; |
1836 |
MODE_BACKWARD, &Data); |
MODE_BACKWARD, &Data); |
1837 |
|
|
1838 |
// interpolate search comes last, because it uses data from forward and backward as prediction |
// interpolate search comes last, because it uses data from forward and backward as prediction |
|
|
|
1839 |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
1840 |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
1841 |
&frame->image, |
&frame->image, |
1884 |
free(qimage); |
free(qimage); |
1885 |
} |
} |
1886 |
|
|
1887 |
static __inline int |
static __inline void |
1888 |
MEanalyzeMB ( const uint8_t * const pRef, |
MEanalyzeMB ( const uint8_t * const pRef, |
1889 |
const uint8_t * const pCur, |
const uint8_t * const pCur, |
1890 |
const int x, |
const int x, |
1891 |
const int y, |
const int y, |
1892 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1893 |
const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMBs, |
|
MACROBLOCK * const pMB, |
|
1894 |
SearchData * const Data) |
SearchData * const Data) |
1895 |
{ |
{ |
1896 |
|
|
1897 |
int i = 255, mask; |
int i, mask; |
1898 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
1899 |
*(Data->iMinSAD) = MV_MAX_ERROR; |
MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width]; |
1900 |
|
|
1901 |
|
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
1902 |
|
|
1903 |
//median is only used as prediction. it doesn't have to be real |
//median is only used as prediction. it doesn't have to be real |
1904 |
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
1905 |
else |
else |
1906 |
if (x == 1) //left macroblock does not have any vector now |
if (x == 1) //left macroblock does not have any vector now |
1907 |
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
1908 |
else if (y == 1) // top macroblock don't have it's vector |
else if (y == 1) // top macroblock doesn't have it's vector |
1909 |
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
1910 |
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 |
1911 |
|
|
1912 |
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, |
1913 |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, 0); |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv); |
1914 |
|
|
1915 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
1916 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
1921 |
pmv[2].y = EVEN(Data->predMV.y); |
pmv[2].y = EVEN(Data->predMV.y); |
1922 |
pmv[0].x = pmv[0].y = 0; |
pmv[0].x = pmv[0].y = 0; |
1923 |
|
|
1924 |
CheckCandidate16no4vI(0, 0, 255, &i, Data); |
CheckCandidate32I(0, 0, 255, &i, Data); |
1925 |
|
|
1926 |
//early skip for 0,0 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) { |
|
if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) { |
|
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
|
|
pMB->mode = MODE_NOT_CODED; |
|
|
return 0; |
|
|
} |
|
1927 |
|
|
1928 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
1929 |
CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data); |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
1930 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
1931 |
CheckCandidate16no4vI(pmv[2].x, pmv[2].y, mask, &i, Data); |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
1932 |
|
|
1933 |
if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 6) // diamond only if needed |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
1934 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
1935 |
|
|
1936 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
for (i = 0; i < 4; i++) { |
1937 |
pMB->mode = MODE_INTER; |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1) * pParam->mb_width)]; |
1938 |
return *(Data->iMinSAD); |
MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i]; |
1939 |
|
MB->mode = MODE_INTER; |
1940 |
|
MB->sad16 = Data->iMinSAD[i+1]; |
1941 |
|
} |
1942 |
|
} |
1943 |
} |
} |
1944 |
|
|
1945 |
#define INTRA_THRESH 1350 |
#define INTRA_BIAS 2500 |
1946 |
#define INTER_THRESH 1200 |
#define INTRA_THRESH 1500 |
1947 |
|
#define INTER_THRESH 1400 |
1948 |
|
|
1949 |
|
|
1950 |
int |
int |
1960 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
1961 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
1962 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
1963 |
|
const VECTOR zeroMV = {0,0}; |
1964 |
|
|
1965 |
VECTOR currentMV; |
int32_t iMinSAD[5], temp[5]; |
1966 |
int32_t iMinSAD; |
VECTOR currentMV[5]; |
1967 |
SearchData Data; |
SearchData Data; |
1968 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
1969 |
Data.currentMV = ¤tMV; |
Data.currentMV = currentMV; |
1970 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = iMinSAD; |
1971 |
Data.iFcode = Current->fcode; |
Data.iFcode = Current->fcode; |
1972 |
CheckCandidate = CheckCandidate16no4vI; |
Data.rrv = Current->global_flags & XVID_REDUCED; |
1973 |
|
Data.temp = temp; |
1974 |
|
CheckCandidate = CheckCandidate32I; |
1975 |
|
|
1976 |
if (intraCount < 10) // we're right after an I frame |
if (intraCount < 10) // we're right after an I frame |
1977 |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
1979 |
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 |
1980 |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
1981 |
|
|
|
|
|
1982 |
InterThresh += 400 * (1 - bCount); |
InterThresh += 400 * (1 - bCount); |
1983 |
if (InterThresh < 200) InterThresh = 200; |
if (InterThresh < 300) InterThresh = 300; |
1984 |
|
|
1985 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
1986 |
|
|
1987 |
for (y = 1; y < pParam->mb_height-1; y++) { |
for (y = 1; y < pParam->mb_height-1; y+=2) { |
1988 |
for (x = 1; x < pParam->mb_width-1; x++) { |
for (x = 1; x < pParam->mb_width-1; x+=2) { |
1989 |
int sad, dev; |
int i; |
|
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
|
1990 |
|
|
1991 |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
|
pParam, pMBs, pMB, &Data); |
|
1992 |
|
|
1993 |
if (sad > IntraThresh) { |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
1994 |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
|
1995 |
|
for (i = 0; i < 4; i++) { |
1996 |
|
int dev; |
1997 |
|
MACROBLOCK *pMB = &pMBs[x+(i&1) + y+(i>>1) * pParam->mb_width]; |
1998 |
|
if (pMB->sad16 > IntraThresh) { |
1999 |
|
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1))* pParam->edged_width) * 16, |
2000 |
pParam->edged_width); |
pParam->edged_width); |
2001 |
if (dev + IntraThresh < sad) { |
if (dev + IntraThresh < pMB->sad16) { |
2002 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
2003 |
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; |
2004 |
} |
} |
2005 |
} |
} |
2006 |
sSAD += sad; |
sSAD += pMB->sad16; |
2007 |
|
} |
2008 |
} |
} |
2009 |
} |
} |
2010 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
2011 |
|
if (sSAD > IntraThresh + INTRA_BIAS ) return I_VOP; |
2012 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2013 |
emms(); |
emms(); |
2014 |
return B_VOP; |
return B_VOP; |
2040 |
} |
} |
2041 |
|
|
2042 |
|
|
2043 |
static __inline VECTOR |
static VECTOR |
2044 |
GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) |
GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) |
2045 |
{ |
{ |
2046 |
|
|
2079 |
max_x = gmc.x + step; |
max_x = gmc.x + step; |
2080 |
min_y = gmc.y - step; |
min_y = gmc.y - step; |
2081 |
max_y = gmc.y + step; |
max_y = gmc.y + step; |
|
|
|
2082 |
} |
} |
2083 |
|
|
2084 |
if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) |
if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) |
2085 |
gmc.x = gmc.y = 0; //no camara pan, no GMC |
gmc.x = gmc.y = 0; //no camara pan, no GMC |
2086 |
|
|
2087 |
// step2: let's refine camera panning using gradiend-descent approach. |
// step2: let's refine camera panning using gradiend-descent approach |
2088 |
// TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond) |
// TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond) |
2089 |
bestcount = 0; |
bestcount = 0; |
2090 |
CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam); |
CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam); |