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 |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
676 |
|
|
677 |
if (bits >= data->iMinSAD[0]) return; |
if (bits >= data->iMinSAD[0]) return; |
678 |
|
|
683 |
//chroma U |
//chroma U |
684 |
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); |
685 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
686 |
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); |
687 |
if (bits >= data->iMinSAD[0]) return; |
if (bits >= data->iMinSAD[0]) return; |
688 |
|
|
689 |
//chroma V |
//chroma V |
690 |
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); |
691 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
692 |
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); |
693 |
|
|
694 |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
695 |
|
|
696 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
697 |
data->iMinSAD[0] = bits; |
data->iMinSAD[0] = bits; |
731 |
} |
} |
732 |
|
|
733 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
734 |
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); |
735 |
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); |
736 |
|
|
737 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
738 |
data->temp[0] = cbp; |
data->temp[0] = cbp; |
1150 |
//initial skip decision |
//initial skip decision |
1151 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1152 |
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 */ |
1153 |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * skip_thresh) |
if (pMB->dquant == NO_CHANGE && sad00 < quant * skip_thresh) |
1154 |
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)) { |
1155 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1156 |
continue; |
continue; |
1371 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1372 |
} |
} |
1373 |
|
|
1374 |
if (MotionFlags & PMV_QUARTERPELREFINE16) { |
if (Data->qpel) { |
|
|
|
1375 |
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, |
1376 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1377 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1378 |
|
if (MotionFlags & PMV_QUARTERPELREFINE16) |
1379 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1380 |
} |
} |
1381 |
|
|
1382 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)) |
if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30) |
1383 |
inter4v = 0; |
inter4v = 0; |
1384 |
|
|
1385 |
if (inter4v) { |
if (inter4v) { |
1775 |
CheckCandidate(0, 0, 255, &k, Data); |
CheckCandidate(0, 0, 255, &k, Data); |
1776 |
|
|
1777 |
// initial (fast) skip decision |
// initial (fast) skip decision |
1778 |
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)) { |
1779 |
//possible skip |
//possible skip |
1780 |
if (Data->chroma) { |
if (Data->chroma) { |
1781 |
pMB->mode = MODE_DIRECT_NONE_MV; |
pMB->mode = MODE_DIRECT_NONE_MV; |
2159 |
} |
} |
2160 |
} |
} |
2161 |
|
|
2162 |
#define INTRA_THRESH 1800 |
#define INTRA_THRESH 1700 |
2163 |
#define INTER_THRESH 1200 |
#define INTER_THRESH 1200 |
2164 |
|
|
2165 |
int |
int |
2188 |
Data.temp = temp; |
Data.temp = temp; |
2189 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2190 |
|
|
2191 |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
if (intraCount != 0) { |
2192 |
|
if (intraCount < 10) // we're right after an I frame |
2193 |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
2194 |
else |
else |
2195 |
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 |
2196 |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
2197 |
|
} |
2198 |
|
|
2199 |
InterThresh -= (350 - 8*b_thresh) * bCount; |
InterThresh -= (350 - 8*b_thresh) * bCount; |
2200 |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
2235 |
} |
} |
2236 |
|
|
2237 |
sSAD /= blocks; |
sSAD /= blocks; |
|
s = (10*s) / blocks; |
|
2238 |
|
|
2239 |
if (s > 4) sSAD += (s - 2) * (60 - 2*b_thresh); //static block - looks bad when in bframe... |
if (b_thresh < 20) { |
2240 |
|
s = (10*s) / blocks; |
2241 |
|
if (s > 4) sSAD += (s - 2) * (40 - 2*b_thresh); //static block - looks bad when in bframe... |
2242 |
|
} |
2243 |
|
|
2244 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2245 |
emms(); |
emms(); |
2450 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
2451 |
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
2452 |
|
|
|
//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 |
|
|
|
|
2453 |
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 |
2454 |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
2455 |
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, |
2462 |
} else { // not qpel |
} else { // not qpel |
2463 |
|
|
2464 |
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 |
|
|
} |
|
2465 |
} |
} |
2466 |
|
|
2467 |
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); |
2491 |
return Data->iMinSAD[0]; |
return Data->iMinSAD[0]; |
2492 |
} |
} |
2493 |
|
|
|
|
|
2494 |
static int |
static int |
2495 |
CountMBBitsInter4v(const SearchData * const Data, |
CountMBBitsInter4v(const SearchData * const Data, |
2496 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
2532 |
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, |
2533 |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
2534 |
|
|
2535 |
*Data8->iMinSAD += t; |
*Data8->iMinSAD += BITS_MULT*t; |
2536 |
|
|
2537 |
Data8->qpel_precision = Data8->qpel; |
Data8->qpel_precision = Data8->qpel; |
2538 |
// 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) |
2610 |
|
|
2611 |
} // /for all luma blocks |
} // /for all luma blocks |
2612 |
|
|
2613 |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
2614 |
|
|
2615 |
// let's check chroma |
// let's check chroma |
2616 |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
2619 |
//chroma U |
//chroma U |
2620 |
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); |
2621 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
2622 |
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); |
2623 |
|
|
2624 |
if (bits >= *Data->iMinSAD) return bits; |
if (bits >= *Data->iMinSAD) return bits; |
2625 |
|
|
2626 |
//chroma V |
//chroma V |
2627 |
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); |
2628 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
2629 |
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); |
2630 |
|
|
2631 |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
2632 |
|
|
2633 |
return bits; |
return bits; |
2634 |
} |
} |
2635 |
|
|
|
|
|
2636 |
static int |
static int |
2637 |
CountMBBitsIntra(const SearchData * const Data) |
CountMBBitsIntra(const SearchData * const Data) |
2638 |
{ |
{ |
2639 |
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 |
2640 |
int cbp = 0, i, t, dc = 1024, b_dc; |
int cbp = 0, i, dc = 0; |
2641 |
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); |
|
2642 |
|
|
2643 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
2644 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2645 |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
2646 |
fdct(in); |
bits += Block_CalcBitsIntra(coeff, in, Data->iQuant, Data->quant_type, &cbp, i, &dc); |
2647 |
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); |
|
2648 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
2649 |
} |
} |
2650 |
|
|
2651 |
bits += xvid_cbpy_tab[cbp>>2].len; |
bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len; |
|
|
|
|
iDcScaler = get_dc_scaler(Data->iQuant, 0); |
|
2652 |
|
|
2653 |
//chroma U |
//chroma U |
2654 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
2655 |
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); |
|
2656 |
|
|
2657 |
if (bits >= Data->iMinSAD[0]) return bits; |
if (bits >= Data->iMinSAD[0]) return bits; |
2658 |
|
|
2659 |
//chroma V |
//chroma V |
2660 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2661 |
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); |
|
2662 |
|
|
2663 |
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
2664 |
|
|
2665 |
return bits; |
return bits; |
2666 |
} |
} |