32 |
#include <stdio.h> |
#include <stdio.h> |
33 |
#include <stdlib.h> |
#include <stdlib.h> |
34 |
#include <string.h> // memcpy |
#include <string.h> // memcpy |
35 |
|
#include <math.h> // lrint |
36 |
|
|
37 |
#include "../encoder.h" |
#include "../encoder.h" |
38 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
53 |
#define CHECK_CANDIDATE(X,Y,D) { \ |
#define CHECK_CANDIDATE(X,Y,D) { \ |
54 |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
55 |
|
|
56 |
static __inline int |
static __inline uint32_t |
57 |
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) |
58 |
{ |
{ |
59 |
int xb, yb; |
int xb, yb; |
60 |
if (qpel) { x *= 2; y *= 2;} |
x = qpel ? x<<1 : x; |
61 |
else if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
y = qpel ? y<<1 : y; |
62 |
|
if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
63 |
|
|
64 |
x -= pred.x; |
x -= pred.x; |
65 |
y -= pred.y; |
y -= pred.y; |
66 |
|
|
67 |
if (x) { |
if (x) { |
68 |
if (x < 0) x = -x; |
x = ABS(x); |
69 |
x += (1 << (iFcode - 1)) - 1; |
x += (1 << (iFcode - 1)) - 1; |
70 |
x >>= (iFcode - 1); |
x >>= (iFcode - 1); |
71 |
if (x > 32) x = 32; |
if (x > 32) x = 32; |
73 |
} else xb = 1; |
} else xb = 1; |
74 |
|
|
75 |
if (y) { |
if (y) { |
76 |
if (y < 0) y = -y; |
y = ABS(y); |
77 |
y += (1 << (iFcode - 1)) - 1; |
y += (1 << (iFcode - 1)) - 1; |
78 |
y >>= (iFcode - 1); |
y >>= (iFcode - 1); |
79 |
if (y > 32) y = 32; |
if (y > 32) y = 32; |
82 |
return xb + yb; |
return xb + yb; |
83 |
} |
} |
84 |
|
|
85 |
|
static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data) |
86 |
|
{ |
87 |
|
int sad; |
88 |
|
const uint32_t stride = data->iEdgedWidth/2; |
89 |
|
uint8_t * f_refu = data->RefQ, |
90 |
|
* f_refv = data->RefQ + 8, |
91 |
|
* b_refu = data->RefQ + 16, |
92 |
|
* b_refv = data->RefQ + 24; |
93 |
|
|
94 |
|
switch (((fx & 1) << 1) | (fy & 1)) { |
95 |
|
case 0: |
96 |
|
fx = fx / 2; fy = fy / 2; |
97 |
|
f_refu = (uint8_t*)data->RefCU + fy * stride + fx, stride; |
98 |
|
f_refv = (uint8_t*)data->RefCV + fy * stride + fx, stride; |
99 |
|
break; |
100 |
|
case 1: |
101 |
|
fx = fx / 2; fy = (fy - 1) / 2; |
102 |
|
interpolate8x8_halfpel_v(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
103 |
|
interpolate8x8_halfpel_v(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
104 |
|
break; |
105 |
|
case 2: |
106 |
|
fx = (fx - 1) / 2; fy = fy / 2; |
107 |
|
interpolate8x8_halfpel_h(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
108 |
|
interpolate8x8_halfpel_h(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
109 |
|
break; |
110 |
|
default: |
111 |
|
fx = (fx - 1) / 2; fy = (fy - 1) / 2; |
112 |
|
interpolate8x8_halfpel_hv(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
113 |
|
interpolate8x8_halfpel_hv(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
114 |
|
break; |
115 |
|
} |
116 |
|
|
117 |
|
switch (((bx & 1) << 1) | (by & 1)) { |
118 |
|
case 0: |
119 |
|
bx = bx / 2; by = by / 2; |
120 |
|
b_refu = (uint8_t*)data->b_RefCU + by * stride + bx, stride; |
121 |
|
b_refv = (uint8_t*)data->b_RefCV + by * stride + bx, stride; |
122 |
|
break; |
123 |
|
case 1: |
124 |
|
bx = bx / 2; by = (by - 1) / 2; |
125 |
|
interpolate8x8_halfpel_v(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
126 |
|
interpolate8x8_halfpel_v(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
127 |
|
break; |
128 |
|
case 2: |
129 |
|
bx = (bx - 1) / 2; by = by / 2; |
130 |
|
interpolate8x8_halfpel_h(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
131 |
|
interpolate8x8_halfpel_h(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
132 |
|
break; |
133 |
|
default: |
134 |
|
bx = (bx - 1) / 2; by = (by - 1) / 2; |
135 |
|
interpolate8x8_halfpel_hv(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
136 |
|
interpolate8x8_halfpel_hv(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
137 |
|
break; |
138 |
|
} |
139 |
|
|
140 |
|
sad = sad8bi(data->CurU, b_refu, f_refu, stride); |
141 |
|
sad += sad8bi(data->CurV, b_refv, f_refv, stride); |
142 |
|
|
143 |
|
return sad; |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
static int32_t |
static int32_t |
148 |
ChromaSAD(int dx, int dy, const SearchData * const data) |
ChromaSAD(int dx, int dy, const SearchData * const data) |
149 |
{ |
{ |
150 |
int sad; |
int sad; |
151 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
|
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
|
|
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
|
152 |
|
|
153 |
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 |
154 |
data->temp[5] = dx; data->temp[6] = dy; // backup |
data->temp[5] = dx; data->temp[6] = dy; // backup |
183 |
} |
} |
184 |
|
|
185 |
static __inline const uint8_t * |
static __inline const uint8_t * |
186 |
GetReferenceB(const int x, const int y, const int dir, const SearchData * const data) |
GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data) |
187 |
{ |
{ |
188 |
// dir : 0 = forward, 1 = backward |
// dir : 0 = forward, 1 = backward |
189 |
switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) { |
switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) { |
204 |
{ |
{ |
205 |
switch ( ((x&1)<<1) | (y&1) ) { |
switch ( ((x&1)<<1) | (y&1) ) { |
206 |
case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); |
case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); |
207 |
|
case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
208 |
case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
209 |
case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); |
default : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); //case 2 |
|
default : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
|
210 |
} |
} |
211 |
} |
} |
212 |
|
|
213 |
static uint8_t * |
static uint8_t * |
214 |
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 uint32_t block, const uint32_t dir, const SearchData * const data) |
215 |
{ |
{ |
216 |
// create or find a qpel-precision reference picture; return pointer to it |
// create or find a qpel-precision reference picture; return pointer to it |
217 |
uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir; |
uint8_t * Reference = data->RefQ + 16*dir; |
218 |
const int32_t iEdgedWidth = data->iEdgedWidth; |
const uint32_t iEdgedWidth = data->iEdgedWidth; |
219 |
const uint32_t rounding = data->rounding; |
const uint32_t rounding = data->rounding; |
220 |
const int halfpel_x = x/2; |
const int halfpel_x = x/2; |
221 |
const int halfpel_y = y/2; |
const int halfpel_y = y/2; |
255 |
} |
} |
256 |
|
|
257 |
static uint8_t * |
static uint8_t * |
258 |
Interpolate16x16qpel(const int x, const int y, const int dir, const SearchData * const data) |
Interpolate16x16qpel(const int x, const int y, const uint32_t dir, const SearchData * const data) |
259 |
{ |
{ |
260 |
// create or find a qpel-precision reference picture; return pointer to it |
// create or find a qpel-precision reference picture; return pointer to it |
261 |
uint8_t * Reference = (uint8_t *)data->RefQ + 16*dir; |
uint8_t * Reference = data->RefQ + 16*dir; |
262 |
const int32_t iEdgedWidth = data->iEdgedWidth; |
const uint32_t iEdgedWidth = data->iEdgedWidth; |
263 |
const uint32_t rounding = data->rounding; |
const uint32_t rounding = data->rounding; |
264 |
const int halfpel_x = x/2; |
const int halfpel_x = x/2; |
265 |
const int halfpel_y = y/2; |
const int halfpel_y = y/2; |
267 |
|
|
268 |
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); |
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); |
269 |
switch( ((x&1)<<1) + (y&1) ) { |
switch( ((x&1)<<1) + (y&1) ) { |
270 |
case 0: // pure halfpel position |
case 3: // x and y in qpel resolution - the "corners" (top left/right and |
271 |
return (uint8_t *) ref1; |
// bottom left/right) during qpel refinement |
272 |
|
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
273 |
|
ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); |
274 |
|
ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data); |
275 |
|
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
276 |
|
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); |
277 |
|
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); |
278 |
|
interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding); |
279 |
|
break; |
280 |
|
|
281 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
282 |
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); |
283 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
294 |
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); |
295 |
break; |
break; |
296 |
|
|
297 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
case 0: // pure halfpel position |
298 |
// bottom left/right) during qpel refinement |
return (uint8_t *) ref1; |
|
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); |
|
|
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding); |
|
|
break; |
|
299 |
} |
} |
300 |
return Reference; |
return Reference; |
301 |
} |
} |
305 |
static void |
static void |
306 |
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
307 |
{ |
{ |
308 |
int t, xc, yc; |
int xc, yc; |
309 |
const uint8_t * Reference; |
const uint8_t * Reference; |
310 |
VECTOR * current; |
VECTOR * current; |
311 |
|
int32_t sad; uint32_t t; |
312 |
|
|
313 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
314 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
315 |
|
|
316 |
if (data->qpel_precision) { // x and y are in 1/4 precision |
if (!data->qpel_precision) { |
|
Reference = Interpolate16x16qpel(x, y, 0, data); |
|
|
xc = x/2; yc = y/2; //for chroma sad |
|
|
current = data->currentQMV; |
|
|
} else { |
|
317 |
Reference = GetReference(x, y, data); |
Reference = GetReference(x, y, data); |
318 |
current = data->currentMV; |
current = data->currentMV; |
319 |
xc = x; yc = y; |
xc = x; yc = y; |
320 |
|
} else { // x and y are in 1/4 precision |
321 |
|
Reference = Interpolate16x16qpel(x, y, 0, data); |
322 |
|
xc = x/2; yc = y/2; //for chroma sad |
323 |
|
current = data->currentQMV; |
324 |
} |
} |
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
|
325 |
|
|
326 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
327 |
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
328 |
|
|
329 |
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
sad += (data->lambda16 * t * sad)>>10; |
330 |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; |
331 |
|
|
332 |
if (data->chroma) data->temp[0] += ChromaSAD(xc, yc, data); |
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
333 |
|
(yc >> 1) + roundtab_79[yc & 0x3], data); |
334 |
|
|
335 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (sad < data->iMinSAD[0]) { |
336 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = sad; |
337 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
338 |
*dir = Direction; } |
*dir = Direction; |
339 |
|
} |
340 |
|
|
341 |
if (data->temp[1] < data->iMinSAD[1]) { |
if (data->temp[1] < data->iMinSAD[1]) { |
342 |
data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; } |
data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; } |
350 |
} |
} |
351 |
|
|
352 |
static void |
static void |
353 |
|
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
354 |
|
{ |
355 |
|
int32_t sad; uint32_t t; |
356 |
|
const uint8_t * Reference; |
357 |
|
|
358 |
|
if ( (x > data->max_dx) || (x < data->min_dx) |
359 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
360 |
|
|
361 |
|
if (!data->qpel_precision) Reference = GetReference(x, y, data); |
362 |
|
else Reference = Interpolate16x16qpel(x, y, 0, data); |
363 |
|
|
364 |
|
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
365 |
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
366 |
|
|
367 |
|
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; |
368 |
|
|
369 |
|
if (sad < *(data->iMinSAD)) { |
370 |
|
*(data->iMinSAD) = sad; |
371 |
|
data->currentMV->x = x; data->currentMV->y = y; |
372 |
|
*dir = Direction; |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
static void |
378 |
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) |
379 |
{ |
{ |
380 |
int t; |
uint32_t t; |
381 |
const uint8_t * Reference; |
const uint8_t * Reference; |
382 |
|
|
383 |
if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value |
if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value |
389 |
|
|
390 |
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); |
391 |
|
|
392 |
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
data->temp[0] += (data->lambda16 * t * data->temp[0]) >> 10; |
393 |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; |
394 |
|
|
395 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (data->temp[0] < data->iMinSAD[0]) { |
396 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = data->temp[0]; |
410 |
static void |
static void |
411 |
CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
412 |
{ |
{ |
413 |
int32_t sad; |
int32_t sad, xc, yc; |
414 |
const uint8_t * Reference; |
const uint8_t * Reference; |
415 |
int t; |
uint32_t t; |
416 |
VECTOR * current; |
VECTOR * current; |
417 |
|
|
418 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if ( (x > data->max_dx) | ( x < data->min_dx) |
419 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
| (y > data->max_dy) | (y < data->min_dy) ) return; |
420 |
|
|
421 |
if (data->rrv) |
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value |
|
if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) ) return; //non-zero integer value |
|
422 |
|
|
423 |
if (data->qpel_precision) { // x and y are in 1/4 precision |
if (data->qpel_precision) { // x and y are in 1/4 precision |
424 |
Reference = Interpolate16x16qpel(x, y, 0, data); |
Reference = Interpolate16x16qpel(x, y, 0, data); |
425 |
current = data->currentQMV; |
current = data->currentQMV; |
426 |
|
xc = x/2; yc = y/2; |
427 |
} else { |
} else { |
428 |
Reference = GetReference(x, y, data); |
Reference = GetReference(x, y, data); |
429 |
current = data->currentMV; |
current = data->currentMV; |
430 |
|
xc = x; yc = y; |
431 |
} |
} |
432 |
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
433 |
data->qpel && !data->qpel_precision, data->rrv); |
data->qpel^data->qpel_precision, data->rrv); |
434 |
|
|
435 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
436 |
sad += (data->lambda16 * t * sad)/1000; |
sad += (data->lambda16 * t * sad)>>10; |
437 |
|
|
438 |
|
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
439 |
|
(yc >> 1) + roundtab_79[yc & 0x3], data); |
440 |
|
|
441 |
|
|
442 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
443 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
444 |
current->x = x; current->y = y; |
current->x = x; current->y = y; |
445 |
*dir = Direction; } |
*dir = Direction; |
446 |
|
} |
447 |
} |
} |
448 |
|
|
449 |
static void |
static void |
450 |
CheckCandidate32I(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) |
451 |
{ |
{ |
452 |
// maximum speed - for P/B/I decision |
// maximum speed - for P/B/I decision |
453 |
|
int32_t sad; |
454 |
|
|
455 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
456 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
457 |
|
|
458 |
data->temp[0] = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
sad = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
459 |
data->iEdgedWidth, data->temp+1); |
data->iEdgedWidth, data->temp+1); |
460 |
if (data->temp[0] < *(data->iMinSAD)) { |
|
461 |
*(data->iMinSAD) = data->temp[0]; |
if (sad < *(data->iMinSAD)) { |
462 |
|
*(data->iMinSAD) = sad; |
463 |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
464 |
*dir = Direction; } |
*dir = Direction; |
465 |
|
} |
466 |
if (data->temp[1] < data->iMinSAD[1]) { |
if (data->temp[1] < data->iMinSAD[1]) { |
467 |
data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } |
data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } |
468 |
if (data->temp[2] < data->iMinSAD[2]) { |
if (data->temp[2] < data->iMinSAD[2]) { |
477 |
static void |
static void |
478 |
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) |
479 |
{ |
{ |
480 |
int32_t sad; |
int32_t sad, xb, yb, xcf, ycf, xcb, ycb; |
481 |
int xb, yb, t; |
uint32_t t; |
482 |
const uint8_t *ReferenceF, *ReferenceB; |
const uint8_t *ReferenceF, *ReferenceB; |
483 |
VECTOR *current; |
VECTOR *current; |
484 |
|
|
485 |
if (( xf > data->max_dx) || ( xf < data->min_dx) |
if ( (xf > data->max_dx) | (xf < data->min_dx) |
486 |
|| ( yf > data->max_dy) || (yf < data->min_dy)) return; |
| (yf > data->max_dy) | (yf < data->min_dy) ) return; |
487 |
|
|
488 |
if (data->qpel_precision) { |
if (!data->qpel_precision) { |
|
ReferenceF = Interpolate16x16qpel(xf, yf, 0, data); |
|
|
xb = data->currentQMV[1].x; yb = data->currentQMV[1].y; |
|
|
current = data->currentQMV; |
|
|
ReferenceB = Interpolate16x16qpel(xb, yb, 1, data); |
|
|
} else { |
|
489 |
ReferenceF = GetReference(xf, yf, data); |
ReferenceF = GetReference(xf, yf, data); |
490 |
xb = data->currentMV[1].x; yb = data->currentMV[1].y; |
xb = data->currentMV[1].x; yb = data->currentMV[1].y; |
491 |
ReferenceB = GetReferenceB(xb, yb, 1, data); |
ReferenceB = GetReferenceB(xb, yb, 1, data); |
492 |
current = data->currentMV; |
current = data->currentMV; |
493 |
|
xcf = xf; ycf = yf; |
494 |
|
xcb = xb; ycb = yb; |
495 |
|
} else { |
496 |
|
ReferenceF = Interpolate16x16qpel(xf, yf, 0, data); |
497 |
|
xb = data->currentQMV[1].x; yb = data->currentQMV[1].y; |
498 |
|
current = data->currentQMV; |
499 |
|
ReferenceB = Interpolate16x16qpel(xb, yb, 1, data); |
500 |
|
xcf = xf/2; ycf = yf/2; |
501 |
|
xcb = xb/2; ycb = yb/2; |
502 |
} |
} |
503 |
|
|
504 |
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0) |
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
505 |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
506 |
|
|
507 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
508 |
sad += (data->lambda16 * t * sad)/1000; |
sad += (data->lambda16 * t * sad)>>10; |
509 |
|
|
510 |
|
if (data->chroma) sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
511 |
|
(ycf >> 1) + roundtab_79[ycf & 0x3], |
512 |
|
(xcb >> 1) + roundtab_79[xcb & 0x3], |
513 |
|
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
514 |
|
|
515 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
516 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
517 |
current->x = xf; current->y = yf; |
current->x = xf; current->y = yf; |
518 |
*dir = Direction; } |
*dir = Direction; |
519 |
|
} |
520 |
} |
} |
521 |
|
|
522 |
static void |
static void |
523 |
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
524 |
{ |
{ |
525 |
int32_t sad = 0; |
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
526 |
int k; |
uint32_t k; |
527 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
528 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
529 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
|
const VECTOR zeroMV={0,0}; |
|
530 |
|
|
531 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return; |
532 |
|
|
533 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
534 |
mvs.x = data->directmvF[k].x + x; |
mvs.x = data->directmvF[k].x + x; |
541 |
data->directmvB[k].y |
data->directmvB[k].y |
542 |
: mvs.y - data->referencemv[k].y); |
: mvs.y - data->referencemv[k].y); |
543 |
|
|
544 |
if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx ) |
if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx) |
545 |
|| ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy ) |
| (mvs.y > data->max_dy) | (mvs.y < data->min_dy) |
546 |
|| ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx ) |
| (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx) |
547 |
|| ( 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; |
548 |
|
|
549 |
if (!data->qpel) { |
if (data->qpel) { |
550 |
mvs.x *= 2; mvs.y *= 2; |
xcf += mvs.x/2; ycf += mvs.y/2; |
551 |
b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway |
xcb += b_mvs.x/2; ycb += b_mvs.y/2; |
552 |
|
} else { |
553 |
|
xcf += mvs.x; ycf += mvs.y; |
554 |
|
xcb += b_mvs.x; ycb += b_mvs.y; |
555 |
|
mvs.x *= 2; mvs.y *= 2; //we move to qpel precision anyway |
556 |
|
b_mvs.x *= 2; b_mvs.y *= 2; |
557 |
} |
} |
558 |
|
|
559 |
ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); |
ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); |
560 |
ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); |
ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); |
561 |
|
|
562 |
sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
563 |
ReferenceF, ReferenceB, |
ReferenceF, ReferenceB, data->iEdgedWidth); |
|
data->iEdgedWidth); |
|
564 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
565 |
} |
} |
566 |
|
|
567 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
568 |
|
|
569 |
|
if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
570 |
|
(ycf >> 3) + roundtab_76[ycf & 0xf], |
571 |
|
(xcb >> 3) + roundtab_76[xcb & 0xf], |
572 |
|
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
573 |
|
|
574 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
575 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
576 |
data->currentMV->x = x; data->currentMV->y = y; |
data->currentMV->x = x; data->currentMV->y = y; |
577 |
*dir = Direction; } |
*dir = Direction; |
578 |
|
} |
579 |
} |
} |
580 |
|
|
581 |
static void |
static void |
582 |
CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
583 |
{ |
{ |
584 |
int32_t sad; |
int32_t sad, xcf, ycf, xcb, ycb; |
585 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
586 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
587 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
|
const VECTOR zeroMV = {0,0}; |
|
588 |
|
|
589 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return; |
590 |
|
|
591 |
mvs.x = data->directmvF[0].x + x; |
mvs.x = data->directmvF[0].x + x; |
592 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
598 |
data->directmvB[0].y |
data->directmvB[0].y |
599 |
: mvs.y - data->referencemv[0].y); |
: mvs.y - data->referencemv[0].y); |
600 |
|
|
601 |
if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx ) |
if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx) |
602 |
|| ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy ) |
| (mvs.y > data->max_dy) | (mvs.y < data->min_dy) |
603 |
|| ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx ) |
| (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx) |
604 |
|| ( 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; |
605 |
|
|
606 |
if (!data->qpel) { |
if (data->qpel) { |
607 |
mvs.x *= 2; mvs.y *= 2; |
xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); |
608 |
b_mvs.x *= 2; b_mvs.y *= 2; //we move to qpel precision anyway |
xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2); |
|
} |
|
609 |
ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data); |
ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data); |
610 |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
611 |
|
} else { |
612 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
xcf = 4*mvs.x; ycf = 4*mvs.y; |
613 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)/1000; |
xcb = 4*b_mvs.x; ycb = 4*b_mvs.y; |
614 |
|
ReferenceF = GetReference(mvs.x, mvs.y, data); |
615 |
if (sad < *(data->iMinSAD)) { |
ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data); |
|
*(data->iMinSAD) = sad; |
|
|
data->currentMV->x = x; data->currentMV->y = y; |
|
|
*dir = Direction; } |
|
616 |
} |
} |
617 |
|
|
618 |
static void |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
619 |
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
|
{ |
|
|
int32_t sad; int t; |
|
|
const uint8_t * Reference; |
|
|
|
|
|
if (( x > data->max_dx) || ( x < data->min_dx) |
|
|
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|
|
|
|
|
if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data); |
|
|
else Reference = GetReference(x, y, data); |
|
|
|
|
|
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
|
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
|
620 |
|
|
621 |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
622 |
|
(ycf >> 3) + roundtab_76[ycf & 0xf], |
623 |
|
(xcb >> 3) + roundtab_76[xcb & 0xf], |
624 |
|
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
625 |
|
|
626 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
627 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
628 |
data->currentMV->x = x; data->currentMV->y = y; |
data->currentMV->x = x; data->currentMV->y = y; |
629 |
*dir = Direction; } |
*dir = Direction; |
630 |
|
} |
631 |
} |
} |
632 |
|
|
633 |
/* CHECK_CANDIATE FUNCTIONS END */ |
/* CHECK_CANDIATE FUNCTIONS END */ |
651 |
|
|
652 |
/* now we're doing diagonal checks near our candidate */ |
/* now we're doing diagonal checks near our candidate */ |
653 |
|
|
654 |
if (iDirection) { //checking if anything found |
if (iDirection) { //if anything found |
655 |
bDirection = iDirection; |
bDirection = iDirection; |
656 |
iDirection = 0; |
iDirection = 0; |
657 |
x = data->currentMV->x; y = data->currentMV->y; |
x = data->currentMV->x; y = data->currentMV->y; |
777 |
|
|
778 |
/* MAINSEARCH FUNCTIONS END */ |
/* MAINSEARCH FUNCTIONS END */ |
779 |
|
|
|
/* HALFPELREFINE COULD BE A MAINSEARCH FUNCTION, BUT THERE IS NO NEED FOR IT */ |
|
|
|
|
780 |
static void |
static void |
781 |
SubpelRefine(const SearchData * const data) |
SubpelRefine(const SearchData * const data) |
782 |
{ |
{ |
783 |
/* Do a half-pel or q-pel refinement */ |
/* Do a half-pel or q-pel refinement */ |
784 |
VECTOR backupMV; |
const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV; |
785 |
int iDirection; //not needed |
int iDirection; //only needed because macro expects it |
786 |
|
|
787 |
if (data->qpel_precision) |
CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0); |
788 |
backupMV = *(data->currentQMV); |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0); |
789 |
else backupMV = *(data->currentMV); |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, 0); |
790 |
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, 0); |
791 |
CHECK_CANDIDATE(backupMV.x, backupMV.y - 1, 0); |
CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, 0); |
792 |
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y - 1, 0); |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, 0); |
793 |
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y, 0); |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, 0); |
794 |
CHECK_CANDIDATE(backupMV.x + 1, backupMV.y + 1, 0); |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, 0); |
|
CHECK_CANDIDATE(backupMV.x, backupMV.y + 1, 0); |
|
|
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y + 1, 0); |
|
|
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y, 0); |
|
|
CHECK_CANDIDATE(backupMV.x - 1, backupMV.y - 1, 0); |
|
795 |
} |
} |
796 |
|
|
797 |
static __inline int |
static __inline int |
798 |
SkipDecisionP(const IMAGE * current, const IMAGE * reference, |
SkipDecisionP(const IMAGE * current, const IMAGE * reference, |
799 |
const int x, const int y, |
const int x, const int y, |
800 |
const uint32_t iEdgedWidth, const uint32_t iQuant, int rrv) |
const uint32_t stride, const uint32_t iQuant, int rrv) |
801 |
|
|
802 |
{ |
{ |
803 |
/* keep repeating checks for all b-frames before this P frame, |
if(!rrv) { |
804 |
to make sure that SKIP is possible (todo) |
uint32_t sadC = sad8(current->u + x*8 + y*stride*8, |
805 |
how: if skip is not possible set sad00 to a very high value */ |
reference->u + x*8 + y*stride*8, stride); |
|
if(rrv) { |
|
|
uint32_t sadC = sad16(current->u + x*16 + y*(iEdgedWidth/2)*16, |
|
|
reference->u + x*16 + y*(iEdgedWidth/2)*16, iEdgedWidth/2, 256*4096); |
|
|
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
|
|
sadC += sad16(current->v + (x + y*(iEdgedWidth/2))*16, |
|
|
reference->v + (x + y*(iEdgedWidth/2))*16, iEdgedWidth/2, 256*4096); |
|
|
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
|
|
return 1; |
|
|
} else { |
|
|
uint32_t sadC = sad8(current->u + x*8 + y*(iEdgedWidth/2)*8, |
|
|
reference->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2); |
|
806 |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
807 |
sadC += sad8(current->v + (x + y*(iEdgedWidth/2))*8, |
sadC += sad8(current->v + (x + y*stride)*8, |
808 |
reference->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2); |
reference->v + (x + y*stride)*8, stride); |
809 |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; |
810 |
return 1; |
return 1; |
811 |
|
|
812 |
|
} else { |
813 |
|
uint32_t sadC = sad16(current->u + x*16 + y*stride*16, |
814 |
|
reference->u + x*16 + y*stride*16, stride, 256*4096); |
815 |
|
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
816 |
|
sadC += sad16(current->v + (x + y*stride)*16, |
817 |
|
reference->v + (x + y*stride)*16, stride, 256*4096); |
818 |
|
if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; |
819 |
|
return 1; |
820 |
} |
} |
821 |
} |
} |
822 |
|
|
824 |
SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad) |
SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad) |
825 |
{ |
{ |
826 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
827 |
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = zeroMV; |
828 |
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = zeroMV; |
|
|
|
|
pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0; |
|
|
pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0; |
|
|
|
|
829 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
830 |
} |
} |
831 |
|
|
842 |
const IMAGE *const pCurrent = ¤t->image; |
const IMAGE *const pCurrent = ¤t->image; |
843 |
const IMAGE *const pRef = &reference->image; |
const IMAGE *const pRef = &reference->image; |
844 |
|
|
|
const VECTOR zeroMV = { 0, 0 }; |
|
|
|
|
845 |
uint32_t mb_width = pParam->mb_width; |
uint32_t mb_width = pParam->mb_width; |
846 |
uint32_t mb_height = pParam->mb_height; |
uint32_t mb_height = pParam->mb_height; |
847 |
|
const uint32_t iEdgedWidth = pParam->edged_width; |
848 |
|
|
849 |
uint32_t x, y; |
uint32_t x, y; |
850 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
851 |
int32_t InterBias, quant = current->quant, sad00; |
int32_t InterBias, quant = current->quant, sad00; |
|
uint8_t *qimage; |
|
852 |
|
|
853 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
854 |
int32_t temp[8]; |
int32_t temp[8]; |
857 |
int32_t iMinSAD[5]; |
int32_t iMinSAD[5]; |
858 |
SearchData Data; |
SearchData Data; |
859 |
memset(&Data, 0, sizeof(SearchData)); |
memset(&Data, 0, sizeof(SearchData)); |
860 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = iEdgedWidth; |
861 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; |
862 |
Data.currentQMV = currentQMV; |
Data.currentQMV = currentQMV; |
863 |
Data.iMinSAD = iMinSAD; |
Data.iMinSAD = iMinSAD; |
865 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
866 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
867 |
Data.qpel = pParam->m_quarterpel; |
Data.qpel = pParam->m_quarterpel; |
868 |
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
Data.chroma = current->motion_flags & PMV_CHROMA16; |
869 |
Data.rrv = current->global_flags & XVID_REDUCED; |
Data.rrv = current->global_flags & XVID_REDUCED; |
870 |
|
|
871 |
if ((current->global_flags & XVID_REDUCED)) { |
if ((current->global_flags & XVID_REDUCED)) { |
874 |
Data.qpel = Data.chroma = 0; |
Data.qpel = Data.chroma = 0; |
875 |
} |
} |
876 |
|
|
877 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose) |
|
return 1; // allocate some mem for qpel interpolated blocks |
|
|
// somehow this is dirty since I think we shouldn't use malloc outside |
|
|
// encoder_create() - so please fix me! |
|
|
Data.RefQ = qimage; |
|
878 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
879 |
|
|
880 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
881 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
882 |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
883 |
|
|
884 |
if (Data.rrv) pMB->sad16 = |
if (!Data.rrv) pMB->sad16 = |
885 |
sad32v_c(pCurrent->y + (x + y * pParam->edged_width) * 32, |
sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16, |
886 |
pRef->y + (x + y * pParam->edged_width) * 32, |
pRef->y + (x + y * iEdgedWidth) * 16, |
887 |
pParam->edged_width, pMB->sad8 ); |
pParam->edged_width, pMB->sad8 ); |
888 |
|
|
889 |
else pMB->sad16 = |
else pMB->sad16 = |
890 |
sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16, |
sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32, |
891 |
pRef->y + (x + y * pParam->edged_width) * 16, |
pRef->y + (x + y * iEdgedWidth) * 32, |
892 |
pParam->edged_width, pMB->sad8 ); |
pParam->edged_width, pMB->sad8 ); |
893 |
|
|
894 |
if (Data.chroma) { |
if (Data.chroma) { |
895 |
pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8, |
Data.temp[7] = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, |
896 |
pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2); |
pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2) |
897 |
|
+ sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, |
898 |
pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8, |
pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2); |
899 |
pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2); |
pMB->sad16 += Data.temp[7]; |
900 |
} |
} |
901 |
|
|
902 |
sad00 = pMB->sad16; //if no gmc; else sad00 = (..) |
sad00 = pMB->sad16; |
903 |
|
|
904 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
905 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
|
pMB->quant = current->quant; |
|
906 |
} else { |
} else { |
907 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
908 |
quant += DQtab[pMB->dquant]; |
quant += DQtab[pMB->dquant]; |
909 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
910 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
911 |
} |
} |
|
pMB->quant = quant; |
|
912 |
} |
} |
913 |
|
pMB->quant = current->quant; |
914 |
|
|
915 |
//initial skip decision |
//initial skip decision |
916 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
917 |
if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */ |
if (!(current->global_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
918 |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
919 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant, Data.rrv)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
920 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
921 |
continue; |
continue; |
922 |
} |
} |
928 |
current->global_flags & XVID_INTER4V, pMB); |
current->global_flags & XVID_INTER4V, pMB); |
929 |
|
|
930 |
/* 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?" */ |
931 |
if (current->coding_type == P_VOP) { |
if (!(current->global_flags & XVID_GMC)) { |
932 |
if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
933 |
&& ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1)) ) |
&& ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1)) ) |
934 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant, Data.rrv)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
935 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
936 |
continue; |
continue; |
937 |
} |
} |
947 |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
948 |
|
|
949 |
if (Data.chroma) InterBias += 50; // to compensate bigger SAD |
if (Data.chroma) InterBias += 50; // to compensate bigger SAD |
950 |
if (Data.rrv) InterBias *= 4; //?? |
if (Data.rrv) InterBias *= 4; |
951 |
|
|
952 |
if (InterBias < pMB->sad16) { |
if (InterBias < pMB->sad16) { |
953 |
int32_t deviation; |
int32_t deviation; |
954 |
if (Data.rrv) { |
if (!Data.rrv) |
955 |
deviation = dev16(pCurrent->y + (x + y * pParam->edged_width) * 32, |
deviation = dev16(pCurrent->y + (x + y * iEdgedWidth) * 16, iEdgedWidth); |
956 |
pParam->edged_width) |
else { |
957 |
+ dev16(pCurrent->y + (x + y * pParam->edged_width) * 32 + 16, |
deviation = dev16(pCurrent->y + (x + y * iEdgedWidth) * 32, iEdgedWidth) |
958 |
pParam->edged_width) |
+ dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16, iEdgedWidth) |
959 |
+ dev16(pCurrent->y + (x + y * pParam->edged_width) * 32 + 16 * pParam->edged_width, |
+ dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16 * iEdgedWidth, iEdgedWidth) |
960 |
pParam->edged_width) |
+ dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16 * (iEdgedWidth+1), iEdgedWidth); |
961 |
+ dev16(pCurrent->y + (x + y * pParam->edged_width) * 32 + 16 * (pParam->edged_width+1), |
} |
|
pParam->edged_width); |
|
|
} else |
|
|
deviation = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
|
|
pParam->edged_width); |
|
|
|
|
962 |
if (deviation < (pMB->sad16 - InterBias)) { |
if (deviation < (pMB->sad16 - InterBias)) { |
963 |
if (++iIntra >= iLimit) { free(qimage); return 1; } |
if (++iIntra >= iLimit) return 1; |
964 |
|
SkipMacroblockP(pMB, 0); //same thing |
965 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = |
|
|
pMB->mvs[3] = zeroMV; |
|
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = |
|
|
pMB->qmvs[3] = zeroMV; |
|
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = |
|
|
pMB->sad8[3] = 0; |
|
966 |
} |
} |
967 |
} |
} |
968 |
} |
} |
969 |
} |
} |
|
free(qimage); |
|
970 |
|
|
971 |
if (current->coding_type == S_VOP) /* first GMC step only for S(GMC)-VOPs */ |
if (current->global_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ |
972 |
current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode ); |
{ |
973 |
else |
current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
974 |
current->GMC_MV = zeroMV; |
} |
975 |
|
|
976 |
return 0; |
return 0; |
977 |
} |
} |
978 |
|
|
979 |
|
|
|
#define PMV_HALFPEL16 (PMV_HALFPELDIAMOND16|PMV_HALFPELREFINE16) |
|
|
|
|
980 |
static __inline int |
static __inline int |
981 |
make_mask(const VECTOR * const pmv, const int i) |
make_mask(const VECTOR * const pmv, const int i) |
982 |
{ |
{ |
984 |
for (j = 0; j < i; j++) { |
for (j = 0; j < i; j++) { |
985 |
if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already |
if (MVequal(pmv[i], pmv[j])) return 0; // same vector has been checked already |
986 |
if (pmv[i].x == pmv[j].x) { |
if (pmv[i].x == pmv[j].x) { |
987 |
if (pmv[i].y == pmv[j].y + iDiamondSize) { mask &= ~4; continue; } |
if (pmv[i].y == pmv[j].y + iDiamondSize) mask &= ~4; |
988 |
if (pmv[i].y == pmv[j].y - iDiamondSize) { mask &= ~8; continue; } |
else if (pmv[i].y == pmv[j].y - iDiamondSize) mask &= ~8; |
989 |
} else |
} else |
990 |
if (pmv[i].y == pmv[j].y) { |
if (pmv[i].y == pmv[j].y) { |
991 |
if (pmv[i].x == pmv[j].x + iDiamondSize) { mask &= ~1; continue; } |
if (pmv[i].x == pmv[j].x + iDiamondSize) mask &= ~1; |
992 |
if (pmv[i].x == pmv[j].x - iDiamondSize) { mask &= ~2; continue; } |
else if (pmv[i].x == pmv[j].x - iDiamondSize) mask &= ~2; |
993 |
} |
} |
994 |
} |
} |
995 |
return mask; |
return mask; |
1015 |
else pmv[4].x = pmv[4].y = 0; |
else pmv[4].x = pmv[4].y = 0; |
1016 |
|
|
1017 |
// [1] median prediction |
// [1] median prediction |
1018 |
if (rrv) { //median is in halfzero-precision |
pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y); |
|
pmv[1].x = RRV_MV_SCALEUP(pmv[0].x); |
|
|
pmv[1].y = RRV_MV_SCALEUP(pmv[0].y); |
|
|
} else { pmv[1].x = EVEN(pmv[0].x); pmv[1].y = EVEN(pmv[0].y); } |
|
1019 |
|
|
1020 |
pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask |
pmv[0].x = pmv[0].y = 0; // [0] is zero; not used in the loop (checked before) but needed here for make_mask |
1021 |
|
|
1030 |
if (rrv) { |
if (rrv) { |
1031 |
int i; |
int i; |
1032 |
for (i = 0; i < 7; i++) { |
for (i = 0; i < 7; i++) { |
1033 |
pmv[i].x = RRV_MV_SCALEDOWN(pmv[i].x); |
pmv[i].x = RRV_MV_SCALEUP(pmv[i].x); |
1034 |
pmv[i].x = RRV_MV_SCALEUP(pmv[i].x); // a trick |
pmv[i].y = RRV_MV_SCALEUP(pmv[i].y); |
1035 |
} |
} |
1036 |
} |
} |
1037 |
} |
} |
1060 |
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, |
1061 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
1062 |
|
|
1063 |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)() |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); |
1064 |
|
|
1065 |
Data->temp[5] = Data->temp[7] = 256*4096; // to reset chroma-sad cache |
Data->temp[5] = Data->temp[6] = 0; // chroma-sad cache |
1066 |
if (Data->rrv) i = 2; else i = 1; |
i = Data->rrv ? 2 : 1; |
1067 |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i; |
1068 |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1069 |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1084 |
for(i = 0; i < 5; i++) |
for(i = 0; i < 5; i++) |
1085 |
Data->currentMV[i].x = Data->currentMV[i].y = 0; |
Data->currentMV[i].x = Data->currentMV[i].y = 0; |
1086 |
|
|
1087 |
if (pParam->m_quarterpel) 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); |
1088 |
else Data->predMV = pmv[0]; |
else Data->predMV = pmv[0]; |
1089 |
|
|
1090 |
i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0); |
i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0); |
1091 |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10); |
1092 |
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)) >> 10); |
1093 |
Data->iMinSAD[2] = pMB->sad8[1]; |
Data->iMinSAD[2] = pMB->sad8[1]; |
1094 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
1095 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1096 |
|
|
1097 |
if ((x == 0) && (y == 0)) threshA = 512; |
if (x | y) { |
|
else { |
|
1098 |
threshA = Data->temp[0]; // that's when we keep this SAD atm |
threshA = Data->temp[0]; // that's when we keep this SAD atm |
1099 |
if (threshA < 512) threshA = 512; |
if (threshA < 512) threshA = 512; |
1100 |
if (threshA > 1024) threshA = 1024; } |
else if (threshA > 1024) threshA = 1024; |
1101 |
|
} else threshA = 512; |
1102 |
|
|
1103 |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
1104 |
prevMBs + x + y * pParam->mb_width, Data->rrv); |
prevMBs + x + y * pParam->mb_width, Data->rrv); |
1105 |
|
|
1106 |
if (Data->rrv) CheckCandidate = CheckCandidate32; |
if (!Data->rrv) { |
1107 |
else if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16; |
if (inter4v | Data->chroma) CheckCandidate = CheckCandidate16; |
1108 |
else CheckCandidate = CheckCandidate16no4v; //for extra speed |
else CheckCandidate = CheckCandidate16no4v; //for extra speed |
1109 |
|
} else CheckCandidate = CheckCandidate32; |
1110 |
|
|
1111 |
/* main loop. checking all predictions */ |
/* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/ |
1112 |
|
|
1113 |
for (i = 1; i < 7; i++) { |
for (i = 1; i < 7; i++) { |
1114 |
if (!(mask = make_mask(pmv, i)) ) continue; |
if (!(mask = make_mask(pmv, i)) ) continue; |
1118 |
|
|
1119 |
if ((Data->iMinSAD[0] <= threshA) || |
if ((Data->iMinSAD[0] <= threshA) || |
1120 |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
1121 |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) { |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) |
1122 |
inter4v = 0; |
inter4v = 0; |
1123 |
} else { |
else { |
1124 |
|
|
1125 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
1126 |
if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; |
if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; |
1153 |
} |
} |
1154 |
|
|
1155 |
backupMV = Data->currentMV[0]; |
backupMV = Data->currentMV[0]; |
1156 |
if (!MotionFlags & PMV_HALFPELREFINE16 || Data->rrv) startMV.x = startMV.y = 0; |
if (MotionFlags & PMV_HALFPELREFINE16 && !Data->rrv) startMV.x = startMV.y = 1; |
1157 |
else startMV.x = startMV.y = 1; |
else startMV.x = startMV.y = 0; |
1158 |
if (!(MVequal(startMV, backupMV))) { |
if (!(MVequal(startMV, backupMV))) { |
1159 |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
1160 |
|
|
1174 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1175 |
} |
} |
1176 |
|
|
1177 |
if((Data->qpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE16) { |
|
|
|
1178 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1179 |
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, |
1180 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1185 |
if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0; |
if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0; |
1186 |
if (inter4v) { |
if (inter4v) { |
1187 |
SearchData Data8; |
SearchData Data8; |
1188 |
Data8.iFcode = Data->iFcode; |
memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data |
1189 |
Data8.lambda8 = Data->lambda8; |
|
|
Data8.iEdgedWidth = Data->iEdgedWidth; |
|
|
Data8.RefQ = Data->RefQ; |
|
|
Data8.qpel = Data->qpel; |
|
|
Data8.rrv = Data->rrv; |
|
1190 |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
1191 |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
1192 |
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); |
1193 |
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); |
1194 |
|
|
1195 |
if (Data->chroma) { |
if (Data->chroma) { |
1196 |
int sumx, sumy, dx, dy; |
int sumx = 0, sumy = 0; |
1197 |
|
const int div = 1 + Data->qpel; |
1198 |
|
const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs; |
1199 |
|
|
1200 |
if(pParam->m_quarterpel) { |
for (i = 0; i < 4; i++) { |
1201 |
sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2; |
sumx += mv[i].x / div; |
1202 |
sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2; |
sumy += mv[i].y / div; |
|
} else { |
|
|
sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
|
|
sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
|
1203 |
} |
} |
|
dx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
|
|
dy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
|
1204 |
|
|
1205 |
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
Data->iMinSAD[1] += ChromaSAD( (sumx >> 3) + roundtab_76[sumx & 0xf], |
1206 |
|
(sumy >> 3) + roundtab_76[sumy & 0xf], Data); |
1207 |
} |
} |
1208 |
} |
} |
1209 |
|
|
1217 |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
1218 |
// INTER MODE |
// INTER MODE |
1219 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1220 |
pMB->mvs[0] = pMB->mvs[1] |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
1221 |
= pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
|
|
|
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = |
|
|
pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
|
1222 |
|
|
1223 |
if(pParam->m_quarterpel) { |
if(Data->qpel) { |
1224 |
pMB->qmvs[0] = pMB->qmvs[1] |
pMB->qmvs[0] = pMB->qmvs[1] |
1225 |
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
1226 |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
1252 |
Data->currentMV = OldData->currentMV + 1 + block; |
Data->currentMV = OldData->currentMV + 1 + block; |
1253 |
Data->currentQMV = OldData->currentQMV + 1 + block; |
Data->currentQMV = OldData->currentQMV + 1 + block; |
1254 |
|
|
1255 |
if(pParam->m_quarterpel) { |
if(Data->qpel) { |
1256 |
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); |
1257 |
if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y, |
if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y, |
1258 |
Data->predMV, Data->iFcode, 0, 0); |
Data->predMV, Data->iFcode, 0, 0); |
|
|
|
1259 |
} else { |
} else { |
1260 |
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); |
|
if (block != 0) { |
|
1261 |
if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y, |
if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y, |
1262 |
Data->predMV, Data->iFcode, 0, Data->rrv); |
Data->predMV, Data->iFcode, 0, Data->rrv); |
1263 |
} |
} |
|
} |
|
1264 |
|
|
1265 |
*(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100; |
*(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; |
1266 |
|
|
1267 |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) { |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8|PMV_QUARTERPELREFINE8)) { |
1268 |
if (Data->rrv) i = 2; else i = 1; |
if (Data->rrv) i = 2; else i = 1; |
1269 |
|
|
1270 |
Data->Ref = OldData->Ref + i*8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1271 |
Data->RefH = OldData->RefH + i*8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefH = OldData->RefH + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1272 |
Data->RefV = OldData->RefV + i*8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefV = OldData->RefV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1273 |
Data->RefHV = OldData->RefHV + i*8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefHV = OldData->RefHV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1274 |
|
|
1275 |
Data->Cur = OldData->Cur + i*8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->Cur = OldData->Cur + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1276 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1277 |
|
|
1278 |
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, |
1279 |
pParam->width, pParam->height, OldData->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
1280 |
|
|
1281 |
if (Data->rrv) CheckCandidate = CheckCandidate16no4v; |
if (!Data->rrv) CheckCandidate = CheckCandidate8; |
1282 |
else CheckCandidate = CheckCandidate8; |
else CheckCandidate = CheckCandidate16no4v; |
1283 |
|
|
1284 |
if (MotionFlags & PMV_EXTSEARCH8) { |
if (MotionFlags & PMV_EXTSEARCH8) { |
1285 |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
1308 |
} |
} |
1309 |
} |
} |
1310 |
|
|
1311 |
if(Data->qpel) { |
if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE8) { |
|
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
|
|
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
|
1312 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1313 |
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, |
1314 |
pParam->width, pParam->height, OldData->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1315 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1316 |
} |
} |
1317 |
} |
} |
|
} |
|
1318 |
|
|
1319 |
if (Data->rrv) { |
if (Data->rrv) { |
1320 |
Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x); |
Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x); |
1324 |
if(Data->qpel) { |
if(Data->qpel) { |
1325 |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x; |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x; |
1326 |
pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y; |
pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y; |
1327 |
pMB->qmvs[block] = *(Data->currentQMV); |
pMB->qmvs[block] = *Data->currentQMV; |
1328 |
} else { |
} else { |
1329 |
pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x; |
pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x; |
1330 |
pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y; |
pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y; |
1331 |
} |
} |
1332 |
|
|
1333 |
pMB->mvs[block] = *(Data->currentMV); |
pMB->mvs[block] = *Data->currentMV; |
1334 |
pMB->sad8[block] = 4 * (*Data->iMinSAD); |
pMB->sad8[block] = 4 * *Data->iMinSAD; |
1335 |
} |
} |
1336 |
|
|
1337 |
/* B-frames code starts here */ |
/* motion estimation for B-frames */ |
1338 |
|
|
1339 |
static __inline VECTOR |
static __inline VECTOR |
1340 |
ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode) |
ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode) |
1341 |
{ |
{ |
1342 |
/* the stupidiest function ever */ |
/* the stupidiest function ever */ |
1343 |
if (mode == MODE_FORWARD) return pMB->mvs[0]; |
return (mode == MODE_FORWARD ? pMB->mvs[0] : pMB->b_mvs[0]); |
|
else return pMB->b_mvs[0]; |
|
1344 |
} |
} |
1345 |
|
|
1346 |
static void __inline |
static void __inline |
1373 |
pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y); |
pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y); |
1374 |
} else pmv[5].x = pmv[5].y = 0; |
} else pmv[5].x = pmv[5].y = 0; |
1375 |
|
|
1376 |
if ((x != 0)&&(y != 0)) { |
if (x != 0 && y != 0) { |
1377 |
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); |
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); |
1378 |
pmv[6].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y); |
pmv[6].x = EVEN(pmv[6].x); pmv[6].y = EVEN(pmv[6].y); |
1379 |
} else pmv[6].x = pmv[6].y = 0; |
} else pmv[6].x = pmv[6].y = 0; |
|
|
|
|
// more? |
|
1380 |
} |
} |
1381 |
|
|
1382 |
|
|
1383 |
/* search backward or forward, for b-frames */ |
/* search backward or forward */ |
1384 |
static void |
static void |
1385 |
SearchBF( const uint8_t * const pRef, |
SearchBF( const IMAGE * const pRef, |
1386 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1387 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
1388 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
1398 |
SearchData * const Data) |
SearchData * const Data) |
1399 |
{ |
{ |
1400 |
|
|
|
const int32_t iEdgedWidth = pParam->edged_width; |
|
|
|
|
1401 |
int i, iDirection = 255, mask; |
int i, iDirection = 255, mask; |
1402 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
1403 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1404 |
*Data->iMinSAD = MV_MAX_ERROR; |
*Data->iMinSAD = MV_MAX_ERROR; |
1405 |
Data->iFcode = iFcode; |
Data->iFcode = iFcode; |
1406 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1407 |
|
Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache |
1408 |
|
|
1409 |
Data->Ref = pRef + (x + y * iEdgedWidth) * 16; |
Data->Ref = pRef->y + (x + y * Data->iEdgedWidth) * 16; |
1410 |
Data->RefH = pRefH + (x + y * iEdgedWidth) * 16; |
Data->RefH = pRefH + (x + y * Data->iEdgedWidth) * 16; |
1411 |
Data->RefV = pRefV + (x + y * iEdgedWidth) * 16; |
Data->RefV = pRefV + (x + y * Data->iEdgedWidth) * 16; |
1412 |
Data->RefHV = pRefHV + (x + y * iEdgedWidth) * 16; |
Data->RefHV = pRefHV + (x + y * Data->iEdgedWidth) * 16; |
1413 |
|
Data->RefCU = pRef->u + (x + y * Data->iEdgedWidth/2) * 8; |
1414 |
|
Data->RefCV = pRef->v + (x + y * Data->iEdgedWidth/2) * 8; |
1415 |
|
|
1416 |
Data->predMV = *predMV; |
Data->predMV = *predMV; |
1417 |
|
|
1420 |
|
|
1421 |
pmv[0] = Data->predMV; |
pmv[0] = Data->predMV; |
1422 |
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
1423 |
|
|
1424 |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
1425 |
|
|
1426 |
Data->currentMV->x = Data->currentMV->y = 0; |
Data->currentMV->x = Data->currentMV->y = 0; |
1432 |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
1433 |
} |
} |
1434 |
|
|
1435 |
if (MotionFlags & PMV_USESQUARES16) |
if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; |
1436 |
MainSearchPtr = SquareSearch; |
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; |
|
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) |
|
|
MainSearchPtr = AdvDiamondSearch; |
|
1437 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1438 |
|
|
1439 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
1471 |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
1472 |
else pMB->b_mvs[0] = *Data->currentMV; |
else pMB->b_mvs[0] = *Data->currentMV; |
1473 |
} |
} |
1474 |
|
|
1475 |
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
1476 |
else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
|
|
|
1477 |
} |
} |
1478 |
|
|
1479 |
static void |
static void |
1481 |
const IMAGE * const f_Ref, |
const IMAGE * const f_Ref, |
1482 |
const IMAGE * const b_Ref, |
const IMAGE * const b_Ref, |
1483 |
MACROBLOCK * const pMB, |
MACROBLOCK * const pMB, |
|
const uint32_t quant, |
|
1484 |
const uint32_t x, const uint32_t y, |
const uint32_t x, const uint32_t y, |
1485 |
const SearchData * const Data) |
const SearchData * const Data) |
1486 |
{ |
{ |
1487 |
int dx, dy, b_dx, b_dy; |
int dx = 0, dy = 0, b_dx = 0, b_dy = 0; |
1488 |
uint32_t sum; |
int32_t sum; |
1489 |
|
const int div = 1 + Data->qpel; |
1490 |
|
int k; |
1491 |
|
const uint32_t stride = Data->iEdgedWidth/2; |
1492 |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
|
if (Data->qpel) { |
|
|
dy = Data->directmvF[0].y/2 + Data->directmvF[1].y/2 + |
|
|
Data->directmvF[2].y/2 + Data->directmvF[3].y/2; |
|
|
|
|
|
dx = Data->directmvF[0].x/2 + Data->directmvF[1].x/2 + |
|
|
Data->directmvF[2].x/2 + Data->directmvF[3].x/2; |
|
|
|
|
|
b_dy = Data->directmvB[0].y/2 + Data->directmvB[1].y/2 + |
|
|
Data->directmvB[2].y/2 + Data->directmvB[3].y/2; |
|
1493 |
|
|
1494 |
b_dx = Data->directmvB[0].x/2 + Data->directmvB[1].x/2 + |
for (k = 0; k < 4; k++) { |
1495 |
Data->directmvB[2].x/2 + Data->directmvB[3].x/2; |
dy += Data->directmvF[k].y / div; |
1496 |
|
dx += Data->directmvF[0].x / div; |
1497 |
} else { |
b_dy += Data->directmvB[0].y / div; |
1498 |
dy = Data->directmvF[0].y + Data->directmvF[1].y + |
b_dx += Data->directmvB[0].x / div; |
|
Data->directmvF[2].y + Data->directmvF[3].y; |
|
|
|
|
|
dx = Data->directmvF[0].x + Data->directmvF[1].x + |
|
|
Data->directmvF[2].x + Data->directmvF[3].x; |
|
|
|
|
|
b_dy = Data->directmvB[0].y + Data->directmvB[1].y + |
|
|
Data->directmvB[2].y + Data->directmvB[3].y; |
|
|
|
|
|
b_dx = Data->directmvB[0].x + Data->directmvB[1].x + |
|
|
Data->directmvB[2].x + Data->directmvB[3].x; |
|
1499 |
} |
} |
1500 |
|
|
|
|
|
1501 |
dy = (dy >> 3) + roundtab_76[dy & 0xf]; |
dy = (dy >> 3) + roundtab_76[dy & 0xf]; |
1502 |
dx = (dx >> 3) + roundtab_76[dx & 0xf]; |
dx = (dx >> 3) + roundtab_76[dx & 0xf]; |
1503 |
b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; |
b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; |
1504 |
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
1505 |
|
|
1506 |
sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2), |
sum = sad8bi(pCur->u + 8 * x + 8 * y * stride, |
1507 |
f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
f_Ref->u + (y*8 + dy/2) * stride + x*8 + dx/2, |
1508 |
b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
1509 |
Data->iEdgedWidth/2); |
stride); |
1510 |
sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2), |
|
1511 |
f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; //no skip |
1512 |
b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
|
1513 |
Data->iEdgedWidth/2); |
sum += sad8bi(pCur->v + 8*x + 8 * y * stride, |
1514 |
|
f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2, |
1515 |
|
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
1516 |
|
stride); |
1517 |
|
|
1518 |
if (sum < 2*MAX_CHROMA_SAD_FOR_SKIP * quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped |
if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped |
1519 |
} |
} |
1520 |
|
|
|
|
|
|
|
|
1521 |
static __inline uint32_t |
static __inline uint32_t |
1522 |
SearchDirect(const IMAGE * const f_Ref, |
SearchDirect(const IMAGE * const f_Ref, |
1523 |
const uint8_t * const f_RefH, |
const uint8_t * const f_RefH, |
1539 |
|
|
1540 |
{ |
{ |
1541 |
int32_t skip_sad; |
int32_t skip_sad; |
1542 |
int k; |
int k = (x + Data->iEdgedWidth*y) * 16; |
|
|
|
1543 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1544 |
|
|
1545 |
*Data->iMinSAD = 256*4096; |
*Data->iMinSAD = 256*4096; |
1546 |
|
Data->Ref = f_Ref->y + k; |
1547 |
|
Data->RefH = f_RefH + k; |
1548 |
|
Data->RefV = f_RefV + k; |
1549 |
|
Data->RefHV = f_RefHV + k; |
1550 |
|
Data->bRef = b_Ref->y + k; |
1551 |
|
Data->bRefH = b_RefH + k; |
1552 |
|
Data->bRefV = b_RefV + k; |
1553 |
|
Data->bRefHV = b_RefHV + k; |
1554 |
|
Data->RefCU = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
1555 |
|
Data->RefCV = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
1556 |
|
Data->b_RefCU = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
1557 |
|
Data->b_RefCV = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
1558 |
|
|
1559 |
|
k = Data->qpel ? 4 : 2; |
1560 |
|
Data->max_dx = k * (pParam->width - x * 16); |
1561 |
|
Data->max_dy = k * (pParam->height - y * 16); |
1562 |
|
Data->min_dx = -k * (16 + x * 16); |
1563 |
|
Data->min_dy = -k * (16 + y * 16); |
1564 |
|
|
1565 |
Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16; |
Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs; |
1566 |
Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16; |
Data->qpel_precision = 0; |
|
Data->RefV = f_RefV + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefHV = f_RefHV + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->bRef = b_Ref->y + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->bRefH = b_RefH + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->bRefV = b_RefV + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->bRefHV = b_RefHV + (x + Data->iEdgedWidth*y) * 16; |
|
|
|
|
|
Data->max_dx = 2 * pParam->width - 2 * (x) * 16; |
|
|
Data->max_dy = 2 * pParam->height - 2 * (y) * 16; |
|
|
Data->min_dx = -(2 * 16 + 2 * (x) * 16); |
|
|
Data->min_dy = -(2 * 16 + 2 * (y) * 16); |
|
|
if (Data->qpel) { //we measure in qpixels |
|
|
Data->max_dx *= 2; |
|
|
Data->max_dy *= 2; |
|
|
Data->min_dx *= 2; |
|
|
Data->min_dy *= 2; |
|
|
Data->referencemv = b_mb->qmvs; |
|
|
} else Data->referencemv = b_mb->mvs; |
|
|
Data->qpel_precision = 0; // it's a trick. it's 1 not 0, but we need 0 here |
|
1567 |
|
|
1568 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
1569 |
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
1571 |
pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD); |
pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD); |
1572 |
pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD; |
pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD; |
1573 |
|
|
1574 |
if ( ( pMB->b_mvs[k].x > Data->max_dx ) || ( pMB->b_mvs[k].x < Data->min_dx ) |
if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx) |
1575 |
|| ( pMB->b_mvs[k].y > Data->max_dy ) || ( pMB->b_mvs[k].y < Data->min_dy )) { |
| (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) { |
1576 |
|
|
1577 |
*best_sad = 256*4096; // in that case, we won't use direct mode |
*best_sad = 256*4096; // in that case, we won't use direct mode |
1578 |
pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV" |
pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV" |
1588 |
} |
} |
1589 |
} |
} |
1590 |
|
|
1591 |
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v; |
|
else CheckCandidate = CheckCandidateDirectno4v; |
|
1592 |
|
|
1593 |
(*CheckCandidate)(0, 0, 255, &k, Data); |
(*CheckCandidate)(0, 0, 255, &k, Data); |
1594 |
|
|
1595 |
// initial (fast) skip decision |
// initial (fast) skip decision |
1596 |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH*2) { |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) { |
1597 |
SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data->chroma, Data); //possible skip - checking chroma |
//possible skip |
1598 |
|
if (Data->chroma) { |
1599 |
|
pMB->mode = MODE_DIRECT_NONE_MV; |
1600 |
|
return *Data->iMinSAD; // skip. |
1601 |
|
} else { |
1602 |
|
SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data); |
1603 |
if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip. |
if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip. |
1604 |
} |
} |
1605 |
|
} |
1606 |
|
|
1607 |
skip_sad = *Data->iMinSAD; |
skip_sad = *Data->iMinSAD; |
1608 |
|
|
1619 |
|
|
1620 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1621 |
|
|
1622 |
if (b_mb->mode == MODE_INTER4V || Data->qpel) pMB->mode = MODE_DIRECT; |
if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT; |
1623 |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
1624 |
|
|
1625 |
pMB->pmvs[3] = *Data->currentMV; |
pMB->pmvs[3] = *Data->currentMV; |
1651 |
return skip_sad; |
return skip_sad; |
1652 |
} |
} |
1653 |
|
|
|
|
|
1654 |
static void |
static void |
1655 |
SearchInterpolate(const uint8_t * const f_Ref, |
SearchInterpolate(const IMAGE * const f_Ref, |
1656 |
const uint8_t * const f_RefH, |
const uint8_t * const f_RefH, |
1657 |
const uint8_t * const f_RefV, |
const uint8_t * const f_RefV, |
1658 |
const uint8_t * const f_RefHV, |
const uint8_t * const f_RefHV, |
1659 |
const uint8_t * const b_Ref, |
const IMAGE * const b_Ref, |
1660 |
const uint8_t * const b_RefH, |
const uint8_t * const b_RefH, |
1661 |
const uint8_t * const b_RefV, |
const uint8_t * const b_RefV, |
1662 |
const uint8_t * const b_RefHV, |
const uint8_t * const b_RefHV, |
1684 |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
1685 |
|
|
1686 |
i = (x + y * fData->iEdgedWidth) * 16; |
i = (x + y * fData->iEdgedWidth) * 16; |
1687 |
bData.bRef = fData->Ref = f_Ref + i; |
bData.bRef = fData->Ref = f_Ref->y + i; |
1688 |
bData.bRefH = fData->RefH = f_RefH + i; |
bData.bRefH = fData->RefH = f_RefH + i; |
1689 |
bData.bRefV = fData->RefV = f_RefV + i; |
bData.bRefV = fData->RefV = f_RefV + i; |
1690 |
bData.bRefHV = fData->RefHV = f_RefHV + i; |
bData.bRefHV = fData->RefHV = f_RefHV + i; |
1691 |
bData.Ref = fData->bRef = b_Ref + i; |
bData.Ref = fData->bRef = b_Ref->y + i; |
1692 |
bData.RefH = fData->bRefH = b_RefH + i; |
bData.RefH = fData->bRefH = b_RefH + i; |
1693 |
bData.RefV = fData->bRefV = b_RefV + i; |
bData.RefV = fData->bRefV = b_RefV + i; |
1694 |
bData.RefHV = fData->bRefHV = b_RefHV + i; |
bData.RefHV = fData->bRefHV = b_RefHV + i; |
1695 |
|
bData.b_RefCU = fData->RefCU = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
1696 |
|
bData.b_RefCV = fData->RefCV = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
1697 |
|
bData.RefCU = fData->b_RefCU = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
1698 |
|
bData.RefCV = fData->b_RefCV = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
1699 |
|
|
1700 |
|
|
1701 |
bData.bpredMV = fData->predMV = *f_predMV; |
bData.bpredMV = fData->predMV = *f_predMV; |
1702 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
1717 |
|
|
1718 |
CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData); |
CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData); |
1719 |
|
|
1720 |
//diamond. I wish we could use normal mainsearch functions (square, advdiamond) |
//diamond |
|
|
|
1721 |
do { |
do { |
1722 |
iDirection = 255; |
iDirection = 255; |
1723 |
// forward MV moves |
// forward MV moves |
1738 |
|
|
1739 |
} while (!(iDirection)); |
} while (!(iDirection)); |
1740 |
|
|
1741 |
|
//qpel refinement |
1742 |
if (fData->qpel) { |
if (fData->qpel) { |
1743 |
if (*fData->iMinSAD > *best_sad + 500) return; |
if (*fData->iMinSAD > *best_sad + 500) return; |
1744 |
CheckCandidate = CheckCandidateInt; |
CheckCandidate = CheckCandidateInt; |
1800 |
int32_t best_sad; |
int32_t best_sad; |
1801 |
uint32_t skip_sad; |
uint32_t skip_sad; |
1802 |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
|
static const VECTOR zeroMV={0,0}; |
|
1803 |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
1804 |
|
|
1805 |
VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/ |
VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/ |
1806 |
|
|
1807 |
const int32_t TRB = time_pp - time_bp; |
const int32_t TRB = time_pp - time_bp; |
1808 |
const int32_t TRD = time_pp; |
const int32_t TRD = time_pp; |
|
uint8_t * qimage; |
|
1809 |
|
|
1810 |
// some pre-inintialized data for the rest of the search |
// some pre-inintialized data for the rest of the search |
1811 |
|
|
1813 |
int32_t iMinSAD; |
int32_t iMinSAD; |
1814 |
VECTOR currentMV[3]; |
VECTOR currentMV[3]; |
1815 |
VECTOR currentQMV[3]; |
VECTOR currentQMV[3]; |
1816 |
|
int32_t temp[8]; |
1817 |
memset(&Data, 0, sizeof(SearchData)); |
memset(&Data, 0, sizeof(SearchData)); |
1818 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
1819 |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
1820 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
1821 |
Data.lambda16 = lambda_vec16[frame->quant]; |
Data.lambda16 = lambda_vec16[frame->quant]; |
|
Data.chroma = frame->quant; |
|
1822 |
Data.qpel = pParam->m_quarterpel; |
Data.qpel = pParam->m_quarterpel; |
1823 |
Data.rounding = 0; |
Data.rounding = 0; |
1824 |
|
Data.chroma = frame->motion_flags & PMV_CHROMA8; |
1825 |
|
Data.temp = temp; |
1826 |
|
|
1827 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose) |
|
return; // allocate some mem for qpel interpolated blocks |
|
|
// somehow this is dirty since I think we shouldn't use malloc outside |
|
|
// encoder_create() - so please fix me! |
|
|
Data.RefQ = qimage; |
|
|
|
|
1828 |
// note: i==horizontal, j==vertical |
// note: i==horizontal, j==vertical |
1829 |
for (j = 0; j < pParam->mb_height; j++) { |
for (j = 0; j < pParam->mb_height; j++) { |
1830 |
|
|
1842 |
} |
} |
1843 |
|
|
1844 |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
1845 |
|
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
1846 |
|
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |
1847 |
pMB->quant = frame->quant; |
pMB->quant = frame->quant; |
1848 |
|
|
1849 |
/* direct search comes first, because it (1) checks for SKIP-mode |
/* direct search comes first, because it (1) checks for SKIP-mode |
1862 |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
1863 |
|
|
1864 |
// forward search |
// forward search |
1865 |
SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1866 |
&frame->image, i, j, |
&frame->image, i, j, |
1867 |
frame->motion_flags, |
frame->motion_flags, |
1868 |
frame->fcode, pParam, |
frame->fcode, pParam, |
1870 |
MODE_FORWARD, &Data); |
MODE_FORWARD, &Data); |
1871 |
|
|
1872 |
// backward search |
// backward search |
1873 |
SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1874 |
&frame->image, i, j, |
&frame->image, i, j, |
1875 |
frame->motion_flags, |
frame->motion_flags, |
1876 |
frame->bcode, pParam, |
frame->bcode, pParam, |
1878 |
MODE_BACKWARD, &Data); |
MODE_BACKWARD, &Data); |
1879 |
|
|
1880 |
// 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 |
1881 |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1882 |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1883 |
&frame->image, |
&frame->image, |
1884 |
i, j, |
i, j, |
1885 |
frame->fcode, frame->bcode, |
frame->fcode, frame->bcode, |
1892 |
// final skip decision |
// final skip decision |
1893 |
if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP*2) |
if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP*2) |
1894 |
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) |
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) |
1895 |
SkipDecisionB(&frame->image, f_ref, b_ref, pMB,frame->quant, i, j, &Data); |
SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data); |
1896 |
|
|
1897 |
switch (pMB->mode) { |
switch (pMB->mode) { |
1898 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1899 |
f_count++; |
f_count++; |
1900 |
if (Data.qpel) f_predMV = pMB->qmvs[0]; |
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
|
else f_predMV = pMB->mvs[0]; |
|
1901 |
break; |
break; |
1902 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
1903 |
b_count++; |
b_count++; |
1904 |
if (Data.qpel) b_predMV = pMB->b_qmvs[0]; |
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
|
else b_predMV = pMB->b_mvs[0]; |
|
1905 |
break; |
break; |
1906 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
1907 |
i_count++; |
i_count++; |
1908 |
if (Data.qpel) { |
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
1909 |
f_predMV = pMB->qmvs[0]; |
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
|
b_predMV = pMB->b_qmvs[0]; |
|
|
} else { |
|
|
f_predMV = pMB->mvs[0]; |
|
|
b_predMV = pMB->b_mvs[0]; |
|
|
} |
|
1910 |
break; |
break; |
1911 |
case MODE_DIRECT: |
case MODE_DIRECT: |
1912 |
case MODE_DIRECT_NO4V: |
case MODE_DIRECT_NO4V: |
1916 |
} |
} |
1917 |
} |
} |
1918 |
} |
} |
|
free(qimage); |
|
1919 |
} |
} |
1920 |
|
|
1921 |
static __inline void |
static __inline void |
1968 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
1969 |
|
|
1970 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
1971 |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1) * pParam->mb_width)]; |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
1972 |
MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i]; |
MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i]; |
1973 |
MB->mode = MODE_INTER; |
MB->mode = MODE_INTER; |
1974 |
MB->sad16 = Data->iMinSAD[i+1]; |
MB->sad16 = Data->iMinSAD[i+1]; |
1980 |
#define INTRA_THRESH 1500 |
#define INTRA_THRESH 1500 |
1981 |
#define INTER_THRESH 1400 |
#define INTER_THRESH 1400 |
1982 |
|
|
|
|
|
1983 |
int |
int |
1984 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
1985 |
FRAMEINFO * const Current, |
FRAMEINFO * const Current, |
1986 |
MBParam * const pParam, |
MBParam * const pParam, |
1987 |
int maxIntra, //maximum number if non-I frames |
int maxIntra, //maximum number if non-I frames |
1988 |
int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
1989 |
int bCount) // number if B frames in a row |
int bCount) // number of B frames in a row |
1990 |
{ |
{ |
1991 |
uint32_t x, y, intra = 0; |
uint32_t x, y, intra = 0; |
1992 |
int sSAD = 0; |
int sSAD = 0; |
1993 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
1994 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
1995 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
|
const VECTOR zeroMV = {0,0}; |
|
1996 |
|
|
1997 |
int32_t iMinSAD[5], temp[5]; |
int32_t iMinSAD[5], temp[5]; |
1998 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
2005 |
Data.temp = temp; |
Data.temp = temp; |
2006 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2007 |
|
|
2008 |
if (intraCount < 10) // we're right after an I frame |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
2009 |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
2010 |
else |
else |
2011 |
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 |
2026 |
|
|
2027 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
2028 |
int dev; |
int dev; |
2029 |
MACROBLOCK *pMB = &pMBs[x+(i&1) + y+(i>>1) * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width]; |
2030 |
if (pMB->sad16 > IntraThresh) { |
if (pMB->sad16 > IntraThresh) { |
2031 |
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1))* pParam->edged_width) * 16, |
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1))* pParam->edged_width) * 16, |
2032 |
pParam->edged_width); |
pParam->edged_width); |
2040 |
} |
} |
2041 |
} |
} |
2042 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
2043 |
if (sSAD > IntraThresh + INTRA_BIAS ) return I_VOP; |
// if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP; |
2044 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2045 |
emms(); |
emms(); |
2046 |
return B_VOP; |
return B_VOP; |
2047 |
|
|
2048 |
} |
} |
2049 |
|
|
2050 |
static void |
|
2051 |
CheckGMC(int x, int y, const int dir, int * iDirection, |
static WARPPOINTS |
2052 |
const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC, |
GlobalMotionEst(const MACROBLOCK * const pMBs, |
2053 |
const MBParam * const pParam) |
const MBParam * const pParam, |
2054 |
|
const FRAMEINFO * const current, |
2055 |
|
const FRAMEINFO * const reference, |
2056 |
|
const IMAGE * const pRefH, |
2057 |
|
const IMAGE * const pRefV, |
2058 |
|
const IMAGE * const pRefHV ) |
2059 |
{ |
{ |
|
uint32_t mx, my, a, count = 0; |
|
2060 |
|
|
2061 |
for (my = 1; my < pParam->mb_height-1; my++) |
const int deltax=8; // upper bound for difference between a MV and it's neighbour MVs |
2062 |
for (mx = 1; mx < pParam->mb_width-1; mx++) { |
const int deltay=8; |
2063 |
VECTOR mv; |
const int grad=512; // lower bound for deviation in MB |
|
const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; |
|
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue; |
|
|
mv = pMB->mvs[0]; |
|
|
a = ABS(mv.x - x) + ABS(mv.y - y); |
|
|
if (a < 6) count += 6 - a; |
|
|
} |
|
2064 |
|
|
2065 |
if (count > *bestcount) { |
WARPPOINTS gmc; |
2066 |
*bestcount = count; |
|
2067 |
*iDirection = dir; |
uint32_t mx, my; |
2068 |
GMC->x = x; GMC->y = y; |
|
2069 |
|
int MBh = pParam->mb_height; |
2070 |
|
int MBw = pParam->mb_width; |
2071 |
|
|
2072 |
|
int *MBmask= calloc(MBh*MBw,sizeof(int)); |
2073 |
|
double DtimesF[4] = { 0.,0., 0., 0. }; |
2074 |
|
double sol[4] = { 0., 0., 0., 0. }; |
2075 |
|
double a,b,c,n,denom; |
2076 |
|
double meanx,meany; |
2077 |
|
int num,oldnum; |
2078 |
|
|
2079 |
|
if (!MBmask) { fprintf(stderr,"Mem error\n"); return gmc;} |
2080 |
|
|
2081 |
|
// filter mask of all blocks |
2082 |
|
|
2083 |
|
for (my = 1; my < MBh-1; my++) |
2084 |
|
for (mx = 1; mx < MBw-1; mx++) |
2085 |
|
{ |
2086 |
|
const int mbnum = mx + my * MBw; |
2087 |
|
const MACROBLOCK *pMB = &pMBs[mbnum]; |
2088 |
|
const VECTOR mv = pMB->mvs[0]; |
2089 |
|
|
2090 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
2091 |
|
continue; |
2092 |
|
|
2093 |
|
if ( ( (ABS(mv.x - (pMB-1)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-1)->mvs[0].y) < deltay) ) |
2094 |
|
&& ( (ABS(mv.x - (pMB+1)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+1)->mvs[0].y) < deltay) ) |
2095 |
|
&& ( (ABS(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-MBw)->mvs[0].y) < deltay) ) |
2096 |
|
&& ( (ABS(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) ) |
2097 |
|
MBmask[mbnum]=1; |
2098 |
} |
} |
2099 |
|
|
2100 |
|
for (my = 1; my < MBh-1; my++) |
2101 |
|
for (mx = 1; mx < MBw-1; mx++) |
2102 |
|
{ |
2103 |
|
const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx; |
2104 |
|
|
2105 |
|
const int mbnum = mx + my * MBw; |
2106 |
|
if (!MBmask[mbnum]) |
2107 |
|
continue; |
2108 |
|
|
2109 |
|
if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= grad ) |
2110 |
|
MBmask[mbnum] = 0; |
2111 |
|
if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= grad ) |
2112 |
|
MBmask[mbnum] = 0; |
2113 |
|
|
2114 |
} |
} |
2115 |
|
|
2116 |
|
emms(); |
2117 |
|
|
2118 |
static VECTOR |
do { /* until convergence */ |
|
GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) |
|
|
{ |
|
2119 |
|
|
2120 |
uint32_t count, bestcount = 0; |
a = b = c = n = 0; |
2121 |
int x, y; |
DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.; |
2122 |
VECTOR gmc = {0,0}; |
for (my = 0; my < MBh; my++) |
2123 |
int step, min_x, max_x, min_y, max_y; |
for (mx = 0; mx < MBw; mx++) |
2124 |
uint32_t mx, my; |
{ |
2125 |
int iDirection, bDirection; |
const int mbnum = mx + my * MBw; |
2126 |
|
const MACROBLOCK *pMB = &pMBs[mbnum]; |
2127 |
|
const VECTOR mv = pMB->mvs[0]; |
2128 |
|
|
2129 |
min_x = min_y = -32<<iFcode; |
if (!MBmask[mbnum]) |
2130 |
max_x = max_y = 32<<iFcode; |
continue; |
2131 |
|
|
2132 |
//step1: let's find a rough camera panning |
n++; |
2133 |
for (step = 32; step >= 2; step /= 2) { |
a += 16*mx+8; |
2134 |
bestcount = 0; |
b += 16*my+8; |
2135 |
for (y = min_y; y <= max_y; y += step) |
c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8); |
2136 |
for (x = min_x ; x <= max_x; x += step) { |
|
2137 |
count = 0; |
DtimesF[0] += (double)mv.x; |
2138 |
//for all macroblocks |
DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8); |
2139 |
for (my = 1; my < pParam->mb_height-1; my++) |
DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8); |
2140 |
for (mx = 1; mx < pParam->mb_width-1; mx++) { |
DtimesF[3] += (double)mv.y; |
2141 |
const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; |
} |
2142 |
VECTOR mv; |
|
2143 |
|
denom = a*a+b*b-c*n; |
2144 |
|
|
2145 |
|
/* Solve the system: sol = (D'*E*D)^{-1} D'*E*F */ |
2146 |
|
/* D'*E*F has been calculated in the same loop as matrix */ |
2147 |
|
|
2148 |
|
sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2]; |
2149 |
|
sol[1] = a*DtimesF[0] - n*DtimesF[1] + b*DtimesF[3]; |
2150 |
|
sol[2] = b*DtimesF[0] - n*DtimesF[2] - a*DtimesF[3]; |
2151 |
|
sol[3] = b*DtimesF[1] - a*DtimesF[2] - c*DtimesF[3]; |
2152 |
|
|
2153 |
|
sol[0] /= denom; |
2154 |
|
sol[1] /= denom; |
2155 |
|
sol[2] /= denom; |
2156 |
|
sol[3] /= denom; |
2157 |
|
|
2158 |
|
meanx = meany = 0.; |
2159 |
|
oldnum = 0; |
2160 |
|
for (my = 0; my < MBh; my++) |
2161 |
|
for (mx = 0; mx < MBw; mx++) |
2162 |
|
{ |
2163 |
|
const int mbnum = mx + my * MBw; |
2164 |
|
const MACROBLOCK *pMB = &pMBs[mbnum]; |
2165 |
|
const VECTOR mv = pMB->mvs[0]; |
2166 |
|
|
2167 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
if (!MBmask[mbnum]) |
2168 |
continue; |
continue; |
2169 |
|
|
2170 |
mv = pMB->mvs[0]; |
oldnum++; |
2171 |
if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step ) /* GMC translation is always halfpel-res */ |
meanx += ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ); |
2172 |
count++; |
meany += ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ); |
2173 |
} |
} |
2174 |
if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; } |
|
2175 |
} |
if (4*meanx > oldnum) /* better fit than 0.25 is useless */ |
2176 |
min_x = gmc.x - step; |
meanx /= oldnum; |
2177 |
max_x = gmc.x + step; |
else |
2178 |
min_y = gmc.y - step; |
meanx = 0.25; |
2179 |
max_y = gmc.y + step; |
|
2180 |
|
if (4*meany > oldnum) |
2181 |
|
meany /= oldnum; |
2182 |
|
else |
2183 |
|
meany = 0.25; |
2184 |
|
|
2185 |
|
/* fprintf(stderr,"sol = (%8.5f, %8.5f, %8.5f, %8.5f)\n",sol[0],sol[1],sol[2],sol[3]); |
2186 |
|
fprintf(stderr,"meanx = %8.5f meany = %8.5f %d\n",meanx,meany, oldnum); |
2187 |
|
*/ |
2188 |
|
num = 0; |
2189 |
|
for (my = 0; my < MBh; my++) |
2190 |
|
for (mx = 0; mx < MBw; mx++) |
2191 |
|
{ |
2192 |
|
const int mbnum = mx + my * MBw; |
2193 |
|
const MACROBLOCK *pMB = &pMBs[mbnum]; |
2194 |
|
const VECTOR mv = pMB->mvs[0]; |
2195 |
|
|
2196 |
|
if (!MBmask[mbnum]) |
2197 |
|
continue; |
2198 |
|
|
2199 |
|
if ( ( ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx ) |
2200 |
|
|| ( ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) ) |
2201 |
|
MBmask[mbnum]=0; |
2202 |
|
else |
2203 |
|
num++; |
2204 |
} |
} |
2205 |
|
|
2206 |
if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) |
} while ( (oldnum != num) && (num>=4) ); |
|
gmc.x = gmc.y = 0; //no camara pan, no GMC |
|
2207 |
|
|
2208 |
// step2: let's refine camera panning using gradiend-descent approach |
if (num < 4) |
2209 |
// TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond) |
{ |
2210 |
bestcount = 0; |
gmc.duv[0].x= gmc.duv[0].y= gmc.duv[1].x= gmc.duv[1].y= gmc.duv[2].x= gmc.duv[2].y=0; |
2211 |
CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam); |
} else { |
|
do { |
|
|
x = gmc.x; y = gmc.y; |
|
|
bDirection = iDirection; iDirection = 0; |
|
|
if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
|
|
if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
|
|
if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam); |
|
|
if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
|
2212 |
|
|
2213 |
} while (iDirection); |
gmc.duv[0].x=(int)(sol[0]+0.5); |
2214 |
|
gmc.duv[0].y=(int)(sol[3]+0.5); |
2215 |
|
|
2216 |
if (pParam->m_quarterpel) { |
gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5); |
2217 |
gmc.x *= 2; |
gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5); |
2218 |
gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */ |
|
2219 |
|
gmc.duv[2].x=0; |
2220 |
|
gmc.duv[2].y=0; |
2221 |
} |
} |
2222 |
|
// fprintf(stderr,"wp1 = ( %4d, %4d) wp2 = ( %4d, %4d) \n", gmc.duv[0].x, gmc.duv[0].y, gmc.duv[1].x, gmc.duv[1].y); |
2223 |
|
|
2224 |
|
free(MBmask); |
2225 |
|
|
2226 |
return gmc; |
return gmc; |
2227 |
} |
} |