340 |
} |
} |
341 |
|
|
342 |
static void |
static void |
343 |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
344 |
{ |
{ |
345 |
// maximum speed - for P/B/I decision |
// maximum speed - for P/B/I decision |
|
int32_t sad; |
|
346 |
|
|
347 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
348 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
349 |
|
|
350 |
sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
data->temp[0] = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
351 |
data->iEdgedWidth, 256*4096); |
data->iEdgedWidth, data->temp+1); |
352 |
|
if (data->temp[0] < *(data->iMinSAD)) { |
353 |
if (sad < *(data->iMinSAD)) { |
*(data->iMinSAD) = data->temp[0]; |
|
*(data->iMinSAD) = sad; |
|
354 |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
355 |
*dir = Direction; } |
*dir = Direction; } |
356 |
|
if (data->temp[1] < data->iMinSAD[1]) { |
357 |
|
data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } |
358 |
|
if (data->temp[2] < data->iMinSAD[2]) { |
359 |
|
data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; } |
360 |
|
if (data->temp[3] < data->iMinSAD[3]) { |
361 |
|
data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; } |
362 |
|
if (data->temp[4] < data->iMinSAD[4]) { |
363 |
|
data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; } |
364 |
|
|
365 |
} |
} |
366 |
|
|
367 |
|
|
1406 |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
1407 |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
1408 |
} |
} |
1409 |
if (mode_current == MODE_FORWARD) |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
1410 |
pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV; |
else pMB->b_mvs[0] = *Data->currentMV; |
|
else |
|
|
pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
|
|
|
|
1411 |
} |
} |
1412 |
|
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
1413 |
|
else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
1414 |
|
|
1415 |
} |
} |
1416 |
|
|
1545 |
} |
} |
1546 |
} |
} |
1547 |
|
|
|
|
|
1548 |
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
1549 |
else CheckCandidate = CheckCandidateDirectno4v; |
else CheckCandidate = CheckCandidateDirectno4v; |
1550 |
|
|
1571 |
|
|
1572 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1573 |
|
|
1574 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT; |
|
pMB->mode = MODE_DIRECT; |
|
1575 |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
1576 |
|
|
1577 |
pMB->pmvs[3] = *Data->currentMV; |
pMB->pmvs[3] = *Data->currentMV; |
1827 |
MODE_BACKWARD, &Data); |
MODE_BACKWARD, &Data); |
1828 |
|
|
1829 |
// 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 |
|
|
|
1830 |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
1831 |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
1832 |
&frame->image, |
&frame->image, |
1875 |
free(qimage); |
free(qimage); |
1876 |
} |
} |
1877 |
|
|
1878 |
static __inline int |
static __inline void |
1879 |
MEanalyzeMB ( const uint8_t * const pRef, |
MEanalyzeMB ( const uint8_t * const pRef, |
1880 |
const uint8_t * const pCur, |
const uint8_t * const pCur, |
1881 |
const int x, |
const int x, |
1882 |
const int y, |
const int y, |
1883 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1884 |
const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMBs, |
|
MACROBLOCK * const pMB, |
|
1885 |
SearchData * const Data) |
SearchData * const Data) |
1886 |
{ |
{ |
1887 |
|
|
1888 |
int i = 255, mask; |
int i, mask; |
1889 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
1890 |
*(Data->iMinSAD) = MV_MAX_ERROR; |
MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width]; |
1891 |
|
|
1892 |
|
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
1893 |
|
|
1894 |
//median is only used as prediction. it doesn't have to be real |
//median is only used as prediction. it doesn't have to be real |
1895 |
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
1896 |
else |
else |
1897 |
if (x == 1) //left macroblock does not have any vector now |
if (x == 1) //left macroblock does not have any vector now |
1898 |
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
1899 |
else if (y == 1) // top macroblock don't have it's vector |
else if (y == 1) // top macroblock doesn't have it's vector |
1900 |
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
1901 |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
1902 |
|
|
1903 |
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, |
1904 |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, 0); |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv); |
1905 |
|
|
1906 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
1907 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
1912 |
pmv[2].y = EVEN(Data->predMV.y); |
pmv[2].y = EVEN(Data->predMV.y); |
1913 |
pmv[0].x = pmv[0].y = 0; |
pmv[0].x = pmv[0].y = 0; |
1914 |
|
|
1915 |
CheckCandidate16no4vI(0, 0, 255, &i, Data); |
CheckCandidate32I(0, 0, 255, &i, Data); |
1916 |
|
|
1917 |
//early skip for 0,0 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) { |
|
if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) { |
|
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
|
|
pMB->mode = MODE_NOT_CODED; |
|
|
return 0; |
|
|
} |
|
1918 |
|
|
1919 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
1920 |
CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data); |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
1921 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
1922 |
CheckCandidate16no4vI(pmv[2].x, pmv[2].y, mask, &i, Data); |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
1923 |
|
|
1924 |
if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 6) // diamond only if needed |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
1925 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
1926 |
|
|
1927 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
for (i = 0; i < 4; i++) { |
1928 |
pMB->mode = MODE_INTER; |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1) * pParam->mb_width)]; |
1929 |
return *(Data->iMinSAD); |
MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i]; |
1930 |
|
MB->mode = MODE_INTER; |
1931 |
|
MB->sad16 = Data->iMinSAD[i+1]; |
1932 |
|
} |
1933 |
|
} |
1934 |
} |
} |
1935 |
|
|
1936 |
#define INTRA_THRESH 1350 |
#define INTRA_BIAS 2500 |
1937 |
#define INTER_THRESH 1200 |
#define INTRA_THRESH 1500 |
1938 |
|
#define INTER_THRESH 1400 |
1939 |
|
|
1940 |
|
|
1941 |
int |
int |
1951 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
1952 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
1953 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
1954 |
|
const VECTOR zeroMV = {0,0}; |
1955 |
|
|
1956 |
VECTOR currentMV; |
int32_t iMinSAD[5], temp[5]; |
1957 |
int32_t iMinSAD; |
VECTOR currentMV[5]; |
1958 |
SearchData Data; |
SearchData Data; |
1959 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
1960 |
Data.currentMV = ¤tMV; |
Data.currentMV = currentMV; |
1961 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = iMinSAD; |
1962 |
Data.iFcode = Current->fcode; |
Data.iFcode = Current->fcode; |
1963 |
CheckCandidate = CheckCandidate16no4vI; |
Data.rrv = Current->global_flags & XVID_REDUCED; |
1964 |
|
Data.temp = temp; |
1965 |
|
CheckCandidate = CheckCandidate32I; |
1966 |
|
|
1967 |
if (intraCount < 10) // we're right after an I frame |
if (intraCount < 10) // we're right after an I frame |
1968 |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
1970 |
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 |
1971 |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
1972 |
|
|
|
|
|
1973 |
InterThresh += 400 * (1 - bCount); |
InterThresh += 400 * (1 - bCount); |
1974 |
if (InterThresh < 200) InterThresh = 200; |
if (InterThresh < 300) InterThresh = 300; |
1975 |
|
|
1976 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
1977 |
|
|
1978 |
for (y = 1; y < pParam->mb_height-1; y++) { |
for (y = 1; y < pParam->mb_height-1; y+=2) { |
1979 |
for (x = 1; x < pParam->mb_width-1; x++) { |
for (x = 1; x < pParam->mb_width-1; x+=2) { |
1980 |
int sad, dev; |
int i; |
|
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
|
1981 |
|
|
1982 |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
|
pParam, pMBs, pMB, &Data); |
|
1983 |
|
|
1984 |
if (sad > IntraThresh) { |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
1985 |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
|
1986 |
|
for (i = 0; i < 4; i++) { |
1987 |
|
int dev; |
1988 |
|
MACROBLOCK *pMB = &pMBs[x+(i&1) + y+(i>>1) * pParam->mb_width]; |
1989 |
|
if (pMB->sad16 > IntraThresh) { |
1990 |
|
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1))* pParam->edged_width) * 16, |
1991 |
pParam->edged_width); |
pParam->edged_width); |
1992 |
if (dev + IntraThresh < sad) { |
if (dev + IntraThresh < pMB->sad16) { |
1993 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1994 |
if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP; |
if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP; |
1995 |
} |
} |
1996 |
} |
} |
1997 |
sSAD += sad; |
sSAD += pMB->sad16; |
1998 |
|
} |
1999 |
} |
} |
2000 |
} |
} |
2001 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
2002 |
|
if (sSAD > IntraThresh + INTRA_BIAS ) return I_VOP; |
2003 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2004 |
emms(); |
emms(); |
2005 |
return B_VOP; |
return B_VOP; |