667 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
668 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
669 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
670 |
bits += data->temp[i] = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, i, 0); |
bits += data->temp[i] = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, i); |
671 |
} |
} |
672 |
|
|
673 |
bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
bits += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
674 |
|
|
675 |
|
if (data->temp[0] + t < data->iMinSAD[1]) { |
676 |
|
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; } |
677 |
|
if (data->temp[1] < data->iMinSAD[2]) { |
678 |
|
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
679 |
|
if (data->temp[2] < data->iMinSAD[3]) { |
680 |
|
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
681 |
|
if (data->temp[3] < data->iMinSAD[4]) { |
682 |
|
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
683 |
|
|
684 |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
|
685 |
|
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
686 |
|
|
687 |
if (bits >= data->iMinSAD[0]) return; |
if (bits >= data->iMinSAD[0]) return; |
688 |
|
|
693 |
//chroma U |
//chroma U |
694 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
695 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
696 |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 4, 0); |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 4); |
697 |
if (bits >= data->iMinSAD[0]) return; |
if (bits >= data->iMinSAD[0]) return; |
698 |
|
|
699 |
//chroma V |
//chroma V |
700 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
701 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
702 |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5, 0); |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5); |
703 |
|
|
704 |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
705 |
|
|
706 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
707 |
data->iMinSAD[0] = bits; |
data->iMinSAD[0] = bits; |
709 |
*dir = Direction; |
*dir = Direction; |
710 |
} |
} |
711 |
|
|
|
if (data->temp[0] + t < data->iMinSAD[1]) { |
|
|
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; } |
|
|
if (data->temp[1] < data->iMinSAD[2]) { |
|
|
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
|
|
if (data->temp[2] < data->iMinSAD[3]) { |
|
|
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
|
|
if (data->temp[3] < data->iMinSAD[4]) { |
|
|
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
|
|
|
|
712 |
} |
} |
713 |
|
|
714 |
static void |
static void |
715 |
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
716 |
{ |
{ |
733 |
} |
} |
734 |
|
|
735 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
736 |
bits = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5, 0); |
bits = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5); |
737 |
bits += d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
738 |
|
|
739 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
740 |
data->temp[0] = cbp; |
data->temp[0] = cbp; |
1152 |
//initial skip decision |
//initial skip decision |
1153 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1154 |
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 */ |
1155 |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * skip_thresh) |
if (pMB->dquant == NO_CHANGE && sad00 < quant * skip_thresh) |
1156 |
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)) { |
1157 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1158 |
continue; |
continue; |
1294 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
1295 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1296 |
|
|
1297 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) || (x | y)) { |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (x | y)) { |
1298 |
threshA = Data->temp[0]; // that's where we keep this SAD atm |
threshA = Data->temp[0]; // that's where we keep this SAD atm |
1299 |
if (threshA < 512) threshA = 512; |
if (threshA < 512) threshA = 512; |
1300 |
else if (threshA > 1024) threshA = 1024; |
else if (threshA > 1024) threshA = 1024; |
1373 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1374 |
} |
} |
1375 |
|
|
1376 |
if (MotionFlags & PMV_QUARTERPELREFINE16) { |
if (Data->qpel) { |
|
|
|
1377 |
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, |
1378 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1379 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1380 |
|
if (MotionFlags & PMV_QUARTERPELREFINE16) |
1381 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1382 |
} |
} |
1383 |
|
|
1384 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)) |
if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30) |
1385 |
inter4v = 0; |
inter4v = 0; |
1386 |
|
|
1387 |
if (inter4v) { |
if (inter4v) { |
1777 |
CheckCandidate(0, 0, 255, &k, Data); |
CheckCandidate(0, 0, 255, &k, Data); |
1778 |
|
|
1779 |
// initial (fast) skip decision |
// initial (fast) skip decision |
1780 |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) { |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) { |
1781 |
//possible skip |
//possible skip |
1782 |
if (Data->chroma) { |
if (Data->chroma) { |
1783 |
pMB->mode = MODE_DIRECT_NONE_MV; |
pMB->mode = MODE_DIRECT_NONE_MV; |
2161 |
} |
} |
2162 |
} |
} |
2163 |
|
|
2164 |
#define INTRA_THRESH 1800 |
#define INTRA_THRESH 1700 |
2165 |
#define INTER_THRESH 1200 |
#define INTER_THRESH 1200 |
2166 |
|
|
2167 |
int |
int |
2190 |
Data.temp = temp; |
Data.temp = temp; |
2191 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2192 |
|
|
2193 |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
if (intraCount != 0) { |
2194 |
|
if (intraCount < 10) // we're right after an I frame |
2195 |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
2196 |
else |
else |
2197 |
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 |
2198 |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
2199 |
|
} |
2200 |
|
|
2201 |
InterThresh -= (350 - 8*b_thresh) * bCount; |
InterThresh -= (350 - 8*b_thresh) * bCount; |
2202 |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
2237 |
} |
} |
2238 |
|
|
2239 |
sSAD /= blocks; |
sSAD /= blocks; |
|
s = (10*s) / blocks; |
|
2240 |
|
|
2241 |
if (s > 4) sSAD += (s - 2) * (60 - 2*b_thresh); //static block - looks bad when in bframe... |
if (b_thresh < 20) { |
2242 |
|
s = (10*s) / blocks; |
2243 |
|
if (s > 4) sSAD += (s - 2) * (40 - 2*b_thresh); //static block - looks bad when in bframe... |
2244 |
|
} |
2245 |
|
|
2246 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2247 |
emms(); |
emms(); |
2452 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
2453 |
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
2454 |
|
|
|
//checking if this vector is perfect. if it is, we stop. |
|
|
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) |
|
|
return 0; //quick stop |
|
|
|
|
2455 |
if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search |
if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search |
2456 |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
2457 |
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, |
2464 |
} else { // not qpel |
} else { // not qpel |
2465 |
|
|
2466 |
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
|
//checking if this vector is perfect. if it is, we stop. |
|
|
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) { |
|
|
return 0; //inter |
|
|
} |
|
2467 |
} |
} |
2468 |
|
|
2469 |
if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
2493 |
return Data->iMinSAD[0]; |
return Data->iMinSAD[0]; |
2494 |
} |
} |
2495 |
|
|
|
|
|
2496 |
static int |
static int |
2497 |
CountMBBitsInter4v(const SearchData * const Data, |
CountMBBitsInter4v(const SearchData * const Data, |
2498 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
2534 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
2535 |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
2536 |
|
|
2537 |
*Data8->iMinSAD += t; |
*Data8->iMinSAD += BITS_MULT*t; |
2538 |
|
|
2539 |
Data8->qpel_precision = Data8->qpel; |
Data8->qpel_precision = Data8->qpel; |
2540 |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
2612 |
|
|
2613 |
} // /for all luma blocks |
} // /for all luma blocks |
2614 |
|
|
2615 |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
2616 |
|
|
2617 |
// let's check chroma |
// let's check chroma |
2618 |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
2621 |
//chroma U |
//chroma U |
2622 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2623 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
2624 |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 4, 0); |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 4); |
2625 |
|
|
2626 |
if (bits >= *Data->iMinSAD) return bits; |
if (bits >= *Data->iMinSAD) return bits; |
2627 |
|
|
2628 |
//chroma V |
//chroma V |
2629 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2630 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
2631 |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 5, 0); |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 5); |
2632 |
|
|
2633 |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
2634 |
|
|
2635 |
return bits; |
return bits; |
2636 |
} |
} |
2637 |
|
|
|
|
|
2638 |
static int |
static int |
2639 |
CountMBBitsIntra(const SearchData * const Data) |
CountMBBitsIntra(const SearchData * const Data) |
2640 |
{ |
{ |
2641 |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
int bits = BITS_MULT*1; //this one is ac/dc prediction flag bit |
2642 |
int cbp = 0, i, t, dc = 1024, b_dc; |
int cbp = 0, i, dc = 0; |
2643 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
|
uint32_t iDcScaler = get_dc_scaler(Data->iQuant, 1); |
|
2644 |
|
|
2645 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
2646 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2647 |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
2648 |
fdct(in); |
bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, i, &dc); |
2649 |
b_dc = in[0]; |
|
|
in[0] -= dc; |
|
|
dc = b_dc; |
|
|
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
|
|
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
|
|
|
|
|
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len; |
|
|
Data->temp[i] = t; |
|
|
if (t != 0) cbp |= 1 << (5 - i); |
|
2650 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
2651 |
} |
} |
2652 |
|
|
2653 |
bits += xvid_cbpy_tab[cbp>>2].len; |
bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len; |
|
|
|
|
iDcScaler = get_dc_scaler(Data->iQuant, 0); |
|
2654 |
|
|
2655 |
//chroma U |
//chroma U |
2656 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
2657 |
fdct(in); |
bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, 4, &dc); |
|
in[0] -= 1024; |
|
|
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
|
|
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
|
|
|
|
|
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
|
|
if (t != 0) cbp |= 1 << (5 - 4); |
|
2658 |
|
|
2659 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
2660 |
|
|
2661 |
//chroma V |
//chroma V |
2662 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2663 |
fdct(in); |
bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, 5, &dc); |
|
in[0] -= 1024; |
|
|
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
|
|
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
|
|
|
|
|
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
|
|
if (t != 0) cbp |= 1 << (5 - 5); |
|
2664 |
|
|
2665 |
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
2666 |
|
|
2667 |
return bits; |
return bits; |
2668 |
} |
} |