506 |
const uint8_t *ReferenceF, *ReferenceB; |
const uint8_t *ReferenceF, *ReferenceB; |
507 |
VECTOR *current; |
VECTOR *current; |
508 |
|
|
509 |
if ( (xf > data->max_dx) | (xf < data->min_dx) |
if ( (xf > data->max_dx) || (xf < data->min_dx) |
510 |
| (yf > data->max_dy) | (yf < data->min_dy) ) return; |
|| (yf > data->max_dy) || (yf < data->min_dy) ) return; |
511 |
|
|
512 |
if (!data->qpel_precision) { |
if (!data->qpel_precision) { |
513 |
ReferenceF = GetReference(xf, yf, data); |
ReferenceF = GetReference(xf, yf, data); |
552 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
553 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
554 |
|
|
555 |
if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
556 |
|
|
557 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
558 |
mvs.x = data->directmvF[k].x + x; |
mvs.x = data->directmvF[k].x + x; |
565 |
data->directmvB[k].y |
data->directmvB[k].y |
566 |
: mvs.y - data->referencemv[k].y); |
: mvs.y - data->referencemv[k].y); |
567 |
|
|
568 |
if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx) |
if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx) |
569 |
| (mvs.y > data->max_dy) | (mvs.y < data->min_dy) |
|| (mvs.y > data->max_dy) || (mvs.y < data->min_dy) |
570 |
| (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx) |
|| (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) |
571 |
| (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; |
572 |
|
|
573 |
if (data->qpel) { |
if (data->qpel) { |
574 |
xcf += mvs.x/2; ycf += mvs.y/2; |
xcf += mvs.x/2; ycf += mvs.y/2; |
1000 |
uint32_t x, y; |
uint32_t x, y; |
1001 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
1002 |
int32_t quant = current->quant, sad00; |
int32_t quant = current->quant, sad00; |
1003 |
|
int skip_thresh = INITIAL_SKIP_THRESH * |
1004 |
|
(current->global_flags & XVID_REDUCED ? 4:1) * |
1005 |
|
(current->global_flags & XVID_MODEDECISION_BITS ? 2:1); |
1006 |
|
|
1007 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
1008 |
int32_t temp[8]; |
int32_t temp[8]; |
1069 |
//initial skip decision |
//initial skip decision |
1070 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1071 |
if (!(current->global_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
if (!(current->global_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
1072 |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * skip_thresh) |
1073 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
1074 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1075 |
continue; |
continue; |
1082 |
current->global_flags & XVID_INTER4V, pMB); |
current->global_flags & XVID_INTER4V, pMB); |
1083 |
|
|
1084 |
/* 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?" */ |
1085 |
if (!(current->global_flags & XVID_GMC)) { |
if (!(current->global_flags & XVID_GMC || current->global_flags & XVID_MODEDECISION_BITS)) { |
1086 |
if ( pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
if ( pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
|
if (!(current->global_flags & XVID_MODEDECISION_BITS)) { |
|
1087 |
if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
1088 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) |
1089 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
|
} else { // BITS mode decision |
|
|
if (pMB->sad16 > 10) |
|
|
SkipMacroblockP(pMB, sad00); // more than 10 bits would be used for this MB - skip |
|
|
|
|
|
} |
|
1090 |
} |
} |
1091 |
} |
} |
1092 |
if (pMB->mode == MODE_INTRA) |
if (pMB->mode == MODE_INTRA) |
1381 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1382 |
} |
} |
1383 |
|
|
1384 |
if (MotionFlags & PMV_QUARTERPELREFINE16) |
if (MotionFlags & PMV_QUARTERPELREFINE16) { |
1385 |
if ((!(MotionFlags & QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { |
|
|
Data->qpel_precision = 1; |
|
1386 |
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, |
1387 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1388 |
|
|
1389 |
|
if ((!(MotionFlags & QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { |
1390 |
|
Data->qpel_precision = 1; |
1391 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1392 |
} |
} |
1393 |
|
} |
1394 |
|
|
1395 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
1396 |
|
|
1822 |
} |
} |
1823 |
} |
} |
1824 |
|
|
1825 |
|
*Data->iMinSAD += Data->lambda16; |
1826 |
skip_sad = *Data->iMinSAD; |
skip_sad = *Data->iMinSAD; |
1827 |
|
|
1828 |
// DIRECT MODE DELTA VECTOR SEARCH. |
// DIRECT MODE DELTA VECTOR SEARCH. |
2149 |
|
|
2150 |
int i, mask; |
int i, mask; |
2151 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
2152 |
MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
2153 |
|
|
2154 |
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
2155 |
|
|
2163 |
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 |
2164 |
|
|
2165 |
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, |
2166 |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, 0); |
2167 |
|
|
2168 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
2169 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
2175 |
pmv[0].x = pmv[0].y = 0; |
pmv[0].x = pmv[0].y = 0; |
2176 |
|
|
2177 |
CheckCandidate32I(0, 0, 255, &i, Data); |
CheckCandidate32I(0, 0, 255, &i, Data); |
2178 |
|
Data->iMinSAD[1] -= 50; |
2179 |
|
Data->iMinSAD[2] -= 50; |
2180 |
|
Data->iMinSAD[3] -= 50; |
2181 |
|
Data->iMinSAD[4] -= 50; |
2182 |
|
|
2183 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) { |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) { |
2184 |
|
|
2185 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
2186 |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
2187 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
2188 |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
2189 |
|
|
2190 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) // diamond only if needed |
2191 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
2192 |
|
} |
2193 |
|
|
2194 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
2195 |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
2198 |
MB->sad16 = Data->iMinSAD[i+1]; |
MB->sad16 = Data->iMinSAD[i+1]; |
2199 |
} |
} |
2200 |
} |
} |
|
} |
|
2201 |
|
|
2202 |
#define INTRA_BIAS 2500 |
#define INTRA_THRESH 2400 |
2203 |
#define INTRA_THRESH 1500 |
#define INTER_THRESH 1100 |
|
#define INTER_THRESH 1400 |
|
2204 |
|
|
2205 |
int |
int |
2206 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
2207 |
FRAMEINFO * const Current, |
const FRAMEINFO * const Current, |
2208 |
MBParam * const pParam, |
const MBParam * const pParam, |
2209 |
int maxIntra, //maximum number if non-I frames |
const int maxIntra, //maximum number if non-I frames |
2210 |
int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
const int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
2211 |
int bCount) // number of B frames in a row |
const int bCount, // number of B frames in a row |
2212 |
|
const int b_thresh) |
2213 |
{ |
{ |
2214 |
uint32_t x, y, intra = 0; |
uint32_t x, y, intra = 0; |
2215 |
int sSAD = 0; |
int sSAD = 0; |
2216 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
2217 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
2218 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh; |
2219 |
|
int s = 0, blocks = 0; |
2220 |
|
|
2221 |
int32_t iMinSAD[5], temp[5]; |
int32_t iMinSAD[5], temp[5]; |
2222 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
2225 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; |
2226 |
Data.iMinSAD = iMinSAD; |
Data.iMinSAD = iMinSAD; |
2227 |
Data.iFcode = Current->fcode; |
Data.iFcode = Current->fcode; |
|
Data.rrv = Current->global_flags & XVID_REDUCED; |
|
2228 |
Data.temp = temp; |
Data.temp = temp; |
2229 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2230 |
|
|
2231 |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
2232 |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 8 * (intraCount - 10) * (intraCount - 10); |
2233 |
else |
else |
2234 |
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 |
2235 |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
2236 |
|
|
2237 |
InterThresh += 400 * (1 - bCount); |
InterThresh -= (350 - 8*b_thresh) * bCount; |
2238 |
if (InterThresh < 300) InterThresh = 300; |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
2239 |
|
|
2240 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2241 |
|
|
2242 |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
2243 |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
2244 |
int i; |
int i; |
2245 |
|
blocks += 4; |
2246 |
|
|
2247 |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
2248 |
|
else { //extrapolation of the vector found for last frame |
2249 |
|
pMBs[x + y * pParam->mb_width].mvs[0].x = |
2250 |
|
(pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount; |
2251 |
|
pMBs[x + y * pParam->mb_width].mvs[0].y = |
2252 |
|
(pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount; |
2253 |
|
} |
2254 |
|
|
2255 |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
2256 |
|
|
2262 |
pParam->edged_width); |
pParam->edged_width); |
2263 |
if (dev + IntraThresh < pMB->sad16) { |
if (dev + IntraThresh < pMB->sad16) { |
2264 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
2265 |
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; |
2266 |
} |
} |
2267 |
} |
} |
2268 |
|
if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++; |
2269 |
|
|
2270 |
sSAD += pMB->sad16; |
sSAD += pMB->sad16; |
2271 |
} |
} |
2272 |
} |
} |
2273 |
} |
} |
2274 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
|
2275 |
// if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP; |
sSAD /= blocks; |
2276 |
|
s = (10*s) / blocks; |
2277 |
|
|
2278 |
|
if (s > 5) sSAD += (s - 4) * (180 - 2*b_thresh); //static block - looks bad when in bframe... |
2279 |
|
|
2280 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2281 |
emms(); |
emms(); |
2282 |
return B_VOP; |
return B_VOP; |
|
|
|
2283 |
} |
} |
2284 |
|
|
2285 |
|
|
2724 |
|
|
2725 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2726 |
if (t != 0) cbp |= 1 << (5 - 4); |
if (t != 0) cbp |= 1 << (5 - 4); |
|
Data->temp[4] = t; |
|
2727 |
|
|
2728 |
if (bits < Data->iMinSAD[0]) { |
if (bits < Data->iMinSAD[0]) { |
2729 |
|
iDcScaler = get_dc_scaler(iQuant, 1); |
2730 |
//chroma V |
//chroma V |
2731 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2732 |
fdct(in); |
fdct(in); |
2737 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2738 |
if (t != 0) cbp |= 1 << (5 - 5); |
if (t != 0) cbp |= 1 << (5 - 5); |
2739 |
|
|
2740 |
Data->temp[5] = t; |
bits += xvid_cbpy_tab[cbp>>2].len; |
2741 |
|
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
bits += t = xvid_cbpy_tab[cbp>>2].len; |
|
|
Data->temp[6] = t; |
|
|
|
|
|
bits += t = mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
|
Data->temp[7] = t; |
|
|
|
|
2742 |
} |
} |
2743 |
} |
} |
|
|
|
2744 |
return bits; |
return bits; |
2745 |
} |
} |