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)) |
511 |
|
return; |
512 |
|
|
513 |
if (!data->qpel_precision) { |
if (!data->qpel_precision) { |
514 |
ReferenceF = GetReference(xf, yf, data); |
ReferenceF = GetReference(xf, yf, data); |
553 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
554 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
555 |
|
|
556 |
if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
557 |
|
|
558 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
559 |
mvs.x = data->directmvF[k].x + x; |
mvs.x = data->directmvF[k].x + x; |
566 |
data->directmvB[k].y |
data->directmvB[k].y |
567 |
: mvs.y - data->referencemv[k].y); |
: mvs.y - data->referencemv[k].y); |
568 |
|
|
569 |
if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx) |
if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) || |
570 |
| (mvs.y > data->max_dy) | (mvs.y < data->min_dy) |
(mvs.y > data->max_dy) || (mvs.y < data->min_dy) || |
571 |
| (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx) |
(b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) || |
572 |
| (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) ) |
573 |
|
return; |
574 |
|
|
575 |
if (data->qpel) { |
if (data->qpel) { |
576 |
xcf += mvs.x/2; ycf += mvs.y/2; |
xcf += mvs.x/2; ycf += mvs.y/2; |
1001 |
|
|
1002 |
uint32_t x, y; |
uint32_t x, y; |
1003 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
1004 |
int32_t sad00; |
int32_t quant = current->quant, sad00; |
1005 |
|
int skip_thresh = \ |
1006 |
|
INITIAL_SKIP_THRESH * \ |
1007 |
|
(current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \ |
1008 |
|
(current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1); |
1009 |
|
|
1010 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
1011 |
int32_t temp[8]; |
int32_t temp[8]; |
1058 |
|
|
1059 |
sad00 = pMB->sad16; |
sad00 = pMB->sad16; |
1060 |
|
|
1061 |
|
if (pMB->dquant != 0) { |
1062 |
|
quant += DQtab[pMB->dquant]; |
1063 |
|
if (quant > 31) quant = 31; |
1064 |
|
else if (quant < 1) quant = 1; |
1065 |
|
} |
1066 |
|
|
1067 |
|
pMB->quant = current->quant; |
1068 |
|
|
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->vol_flags & XVID_VOL_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
if (!(current->vol_flags & XVID_VOL_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
1072 |
if (pMB->dquant == 0 && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
if (pMB->dquant == 0 && 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->vop_flags & XVID_VOP_INTER4V, pMB); |
current->vop_flags & XVID_VOP_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->vol_flags & XVID_VOL_GMC)) { |
if (!(current->vol_flags & XVID_VOL_GMC || current->vop_flags & XVID_VOP_MODEDECISION_BITS)) { |
1086 |
if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
|
if (!(current->vop_flags & XVID_VOP_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 & XVID_ME_QUARTERPELREFINE16) |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
1385 |
if ((!(MotionFlags & XVID_ME_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 & XVID_ME_QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { |
1390 |
|
Data->qpel_precision = 1; |
1391 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1392 |
} |
} |
1393 |
|
} |
1394 |
|
|
1395 |
if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
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. |
2063 |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
2064 |
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
2065 |
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |
2066 |
|
pMB->quant = frame->quant; |
2067 |
|
|
2068 |
/* direct search comes first, because it (1) checks for SKIP-mode |
/* direct search comes first, because it (1) checks for SKIP-mode |
2069 |
and (2) sets very good predictions for forward and backward search */ |
and (2) sets very good predictions for forward and backward search */ |
2148 |
{ |
{ |
2149 |
|
|
2150 |
int i, mask; |
int i, mask; |
2151 |
|
int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0; |
2152 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
2153 |
MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
2154 |
|
|
2155 |
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
2156 |
|
|
2164 |
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 |
2165 |
|
|
2166 |
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, |
2167 |
pParam->width, pParam->height, Data->iFcode - (pParam->vol_flags&XVID_VOL_QUARTERPEL?1:0), 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0); |
2168 |
|
|
2169 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
2170 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
2176 |
pmv[0].x = pmv[0].y = 0; |
pmv[0].x = pmv[0].y = 0; |
2177 |
|
|
2178 |
CheckCandidate32I(0, 0, 255, &i, Data); |
CheckCandidate32I(0, 0, 255, &i, Data); |
2179 |
|
Data->iMinSAD[1] -= 50; |
2180 |
|
Data->iMinSAD[2] -= 50; |
2181 |
|
Data->iMinSAD[3] -= 50; |
2182 |
|
Data->iMinSAD[4] -= 50; |
2183 |
|
|
2184 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) { |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) { |
2185 |
|
|
2186 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
2187 |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
2188 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
2189 |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
2190 |
|
|
2191 |
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 |
2192 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
2193 |
|
} |
2194 |
|
|
2195 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
2196 |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
2199 |
MB->sad16 = Data->iMinSAD[i+1]; |
MB->sad16 = Data->iMinSAD[i+1]; |
2200 |
} |
} |
2201 |
} |
} |
|
} |
|
2202 |
|
|
2203 |
#define INTRA_BIAS 2500 |
#define INTRA_THRESH 2400 |
2204 |
#define INTRA_THRESH 1500 |
#define INTER_THRESH 1100 |
|
#define INTER_THRESH 1400 |
|
2205 |
|
|
2206 |
int |
int |
2207 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
2208 |
FRAMEINFO * const Current, |
const FRAMEINFO * const Current, |
2209 |
MBParam * const pParam, |
const MBParam * const pParam, |
2210 |
int maxIntra, //maximum number if non-I frames |
const int maxIntra, //maximum number if non-I frames |
2211 |
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 |
2212 |
int bCount) // number of B frames in a row |
const int bCount, // number of B frames in a row |
2213 |
|
const int b_thresh) |
2214 |
{ |
{ |
2215 |
uint32_t x, y, intra = 0; |
uint32_t x, y, intra = 0; |
2216 |
int sSAD = 0; |
int sSAD = 0; |
2217 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
2218 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
2219 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh; |
2220 |
|
int s = 0, blocks = 0; |
2221 |
|
|
2222 |
int32_t iMinSAD[5], temp[5]; |
int32_t iMinSAD[5], temp[5]; |
2223 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
2226 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; |
2227 |
Data.iMinSAD = iMinSAD; |
Data.iMinSAD = iMinSAD; |
2228 |
Data.iFcode = Current->fcode; |
Data.iFcode = Current->fcode; |
|
Data.rrv = Current->vop_flags & XVID_VOP_REDUCED; |
|
2229 |
Data.temp = temp; |
Data.temp = temp; |
2230 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2231 |
|
|
2232 |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
2233 |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 8 * (intraCount - 10) * (intraCount - 10); |
2234 |
else |
else |
2235 |
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 |
2236 |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
2237 |
|
|
2238 |
InterThresh += 400 * (1 - bCount); |
InterThresh -= (350 - 8*b_thresh) * bCount; |
2239 |
if (InterThresh < 300) InterThresh = 300; |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
2240 |
|
|
2241 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2242 |
|
|
2243 |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
2244 |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
2245 |
int i; |
int i; |
2246 |
|
blocks += 4; |
2247 |
|
|
2248 |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
2249 |
|
else { //extrapolation of the vector found for last frame |
2250 |
|
pMBs[x + y * pParam->mb_width].mvs[0].x = |
2251 |
|
(pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount; |
2252 |
|
pMBs[x + y * pParam->mb_width].mvs[0].y = |
2253 |
|
(pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount; |
2254 |
|
} |
2255 |
|
|
2256 |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
2257 |
|
|
2263 |
pParam->edged_width); |
pParam->edged_width); |
2264 |
if (dev + IntraThresh < pMB->sad16) { |
if (dev + IntraThresh < pMB->sad16) { |
2265 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
2266 |
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; |
2267 |
} |
} |
2268 |
} |
} |
2269 |
|
if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++; |
2270 |
|
|
2271 |
sSAD += pMB->sad16; |
sSAD += pMB->sad16; |
2272 |
} |
} |
2273 |
} |
} |
2274 |
} |
} |
2275 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
|
2276 |
// if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP; |
sSAD /= blocks; |
2277 |
|
s = (10*s) / blocks; |
2278 |
|
|
2279 |
|
if (s > 5) sSAD += (s - 4) * (180 - 2*b_thresh); //static block - looks bad when in bframe... |
2280 |
|
|
2281 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2282 |
emms(); |
emms(); |
2283 |
return B_VOP; |
return B_VOP; |
|
|
|
2284 |
} |
} |
2285 |
|
|
2286 |
|
|
2725 |
|
|
2726 |
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; |
2727 |
if (t != 0) cbp |= 1 << (5 - 4); |
if (t != 0) cbp |= 1 << (5 - 4); |
|
Data->temp[4] = t; |
|
2728 |
|
|
2729 |
if (bits < Data->iMinSAD[0]) { |
if (bits < Data->iMinSAD[0]) { |
2730 |
|
iDcScaler = get_dc_scaler(iQuant, 1); |
2731 |
//chroma V |
//chroma V |
2732 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2733 |
fdct(in); |
fdct(in); |
2738 |
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; |
2739 |
if (t != 0) cbp |= 1 << (5 - 5); |
if (t != 0) cbp |= 1 << (5 - 5); |
2740 |
|
|
2741 |
Data->temp[5] = t; |
bits += cbpy_tab[cbp>>2].len; |
2742 |
|
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
bits += t = cbpy_tab[cbp>>2].len; |
|
|
Data->temp[6] = t; |
|
|
|
|
|
bits += t = mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
|
Data->temp[7] = t; |
|
|
|
|
2743 |
} |
} |
2744 |
} |
} |
|
|
|
2745 |
return bits; |
return bits; |
2746 |
} |
} |