[svn] / trunk / xvidcore / src / motion / estimation_bvop.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/motion/estimation_bvop.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1567, Wed Dec 8 12:43:48 2004 UTC revision 1913, Sat Dec 18 16:02:08 2010 UTC
# Line 4  Line 4 
4   *  - Motion Estimation for B-VOPs  -   *  - Motion Estimation for B-VOPs  -
5   *   *
6   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>   *  Copyright(C) 2002 Christoph Lampert <gruel@web.de>
7   *               2002 Michael Militzer <michael@xvid.org>   *               2002-2010 Michael Militzer <michael@xvid.org>
8   *               2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>   *               2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>
9   *   *
10   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
# Line 21  Line 21 
21   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   * $Id: estimation_bvop.c,v 1.18 2004-12-08 12:43:48 syskin Exp $   * $Id: estimation_bvop.c,v 1.27 2010-12-18 16:02:00 Isibaar Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 141  Line 141 
141                   + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision);                   + d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision);
142    
143          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
144          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t);
145    
146          if (data->chroma && sad < *data->iMinSAD)          if (data->chroma && sad < *data->iMinSAD)
147                  sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],                  sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],
# Line 207  Line 207 
207                  if (sad > *(data->iMinSAD)) return;                  if (sad > *(data->iMinSAD)) return;
208          }          }
209    
210          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0) * sad)>>10;          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0));
211    
212          if (data->chroma && sad < *data->iMinSAD)          if (data->chroma && sad < *data->iMinSAD)
213                  sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],                  sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
# Line 265  Line 265 
265    
266  done:  done:
267          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
268          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0) * sad)>>10;          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0));
269    
270          if (data->chroma && sad < *data->iMinSAD)          if (data->chroma && sad < *data->iMinSAD)
271                  sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],                  sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
# Line 304  Line 304 
304                                          data->qpel^data->qpel_precision);                                          data->qpel^data->qpel_precision);
305    
306          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
307          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t);
308    
309          if (data->chroma && sad < *data->iMinSAD)          if (data->chroma && sad < *data->iMinSAD)
310                  sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],                  sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
# Line 829  Line 829 
829    
830          pMB->sad16 = best_sad;          pMB->sad16 = best_sad;
831          pMB->mode = mode;          pMB->mode = mode;
832            pMB->cbp = 63;
833    
834          switch (mode) {          switch (mode) {
835    
# Line 964  Line 965 
965  {  {
966          uint32_t i, j;          uint32_t i, j;
967          int32_t best_sad = 256*4096;          int32_t best_sad = 256*4096;
         int32_t sad2;  
968          uint32_t skip_sad;          uint32_t skip_sad;
969            int fb_thresh;
970          const MACROBLOCK * const b_mbs = b_reference->mbs;          const MACROBLOCK * const b_mbs = b_reference->mbs;
971    
972          VECTOR f_predMV, b_predMV;          VECTOR f_predMV, b_predMV;
# Line 985  Line 985 
985          Data_d.rounding = 0;          Data_d.rounding = 0;
986          Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;          Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
987          Data_d.iQuant = frame->quant;          Data_d.iQuant = frame->quant;
988            Data_d.quant_sq = frame->quant*frame->quant;
989          Data_d.dctSpace = dct_space;          Data_d.dctSpace = dct_space;
990          Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);          Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
991          Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices;          Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
# Line 1028  Line 1029 
1029    
1030                          if (pMB->mode == MODE_DIRECT_NONE_MV) {                          if (pMB->mode == MODE_DIRECT_NONE_MV) {
1031                                  pMB->sad16 = best_sad;                                  pMB->sad16 = best_sad;
1032                                    pMB->cbp = 0;
1033                                  continue;                                  continue;
1034                          }                          }
1035    
# Line 1037  Line 1039 
1039                          SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB,                          SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB,
1040                                                  &b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]);                                                  &b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]);
1041    
1042                          sad2 = best_sad;                          if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP)
1043                                    fb_thresh = best_sad;
1044                            else
1045                                    fb_thresh = best_sad + (best_sad>>1);
1046    
1047                          if (Data_f.iMinSAD[0] < 2*sad2+2000)                          if (Data_f.iMinSAD[0] <= fb_thresh)
1048                                  SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f);                                  SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f);
1049    
1050                          if (Data_b.iMinSAD[0] < 2*sad2+2000)                          if (Data_b.iMinSAD[0] <= fb_thresh)
1051                                  SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b);                                  SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b);
1052    
1053                          SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad,                          SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad,
1054                                                                    &Data_i, Data_f.currentMV[0], Data_b.currentMV[0]);                                                                    &Data_i, Data_f.currentMV[0], Data_b.currentMV[0]);
1055    
1056                          if (((Data_i.iMinSAD[0] < 2*best_sad+2000) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE))                          if (((Data_i.iMinSAD[0] < best_sad +(best_sad>>3)) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE))
1057                                  || Data_i.iMinSAD[0] <= best_sad)                                  || Data_i.iMinSAD[0] <= best_sad)
1058    
1059                                  SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i);                                  SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i);
1060    
1061                          if ((Data_d.iMinSAD[0] <= 2*best_sad) && (!frame->motion_flags&XVID_ME_SKIP_DELTASEARCH))                          if (Data_d.iMinSAD[0] <= 2*best_sad)
1062                                    if ((!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH) && (best_sad > 750))
1063                                            || (best_sad > 1000))
1064    
1065                                  SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d);                                  SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d);
1066    
1067                          /* final skip decision */                          /* final skip decision */
# Line 1066  Line 1074 
1074    
1075                                  if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */                                  if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */
1076                                          pMB->sad16 = skip_sad;                                          pMB->sad16 = skip_sad;
1077                                            pMB->cbp = 0;
1078                                          continue;                                          continue;
1079                                  }                                  }
1080                          }                          }
1081    
1082                          if (frame->vop_flags & XVID_VOP_RD_BVOP)                          if (frame->vop_flags & XVID_VOP_RD_BVOP)
1083                                  ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i,                                  ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i,
1084                                          pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, pParam, i, j);                                          pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad);
1085                          else                          else
1086                                  ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV);                                  ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV);
1087    
# Line 1084  Line 1093 
1093          frame->fcode = getMinFcode(MVmaxF);          frame->fcode = getMinFcode(MVmaxF);
1094          frame->bcode = getMinFcode(MVmaxB);          frame->bcode = getMinFcode(MVmaxB);
1095  }  }
1096    
1097    
1098    
1099    void
1100    SMPMotionEstimationBVOP(SMPData * h)
1101    {
1102            Encoder *pEnc = (Encoder *) h->pEnc;
1103    
1104            const MBParam * const pParam = &pEnc->mbParam;
1105            const FRAMEINFO * const frame = h->current;
1106            const int32_t time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);
1107            const int32_t time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);
1108            /* forward (past) reference */
1109            const IMAGE * const f_ref = &pEnc->reference->image;
1110            const IMAGE * const f_refH = &pEnc->f_refh;
1111            const IMAGE * const f_refV = &pEnc->f_refv;
1112            const IMAGE * const f_refHV = &pEnc->f_refhv;
1113            /* backward (future) reference */
1114            const FRAMEINFO * const b_reference = pEnc->current;
1115            const IMAGE * const b_ref = &pEnc->current->image;
1116            const IMAGE * const b_refH = &pEnc->vInterH;
1117            const IMAGE * const b_refV = &pEnc->vInterV;
1118            const IMAGE * const b_refHV = &pEnc->vInterHV;
1119    
1120            int y_row = h->y_row;
1121            int y_step = h->y_step;
1122            int start_y = h->start_y;
1123            int stop_y = h->stop_y;
1124            int * complete_count_self = h->complete_count_self;
1125            const int * complete_count_above = h->complete_count_above;
1126            int max_mbs;
1127            int current_mb = 0;
1128    
1129            int32_t i, j;
1130            int32_t best_sad = 256*4096;
1131            uint32_t skip_sad;
1132            int fb_thresh;
1133            const MACROBLOCK * const b_mbs = b_reference->mbs;
1134    
1135            VECTOR f_predMV, b_predMV;
1136    
1137            int MVmaxF = 0, MVmaxB = 0;
1138            const int32_t TRB = time_pp - time_bp;
1139            const int32_t TRD = time_pp;
1140            DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE);
1141    
1142            /* some pre-inintialized data for the rest of the search */
1143            SearchData Data_d, Data_f, Data_b, Data_i;
1144            memset(&Data_d, 0, sizeof(SearchData));
1145    
1146            Data_d.iEdgedWidth = pParam->edged_width;
1147            Data_d.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;
1148            Data_d.rounding = 0;
1149            Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
1150            Data_d.iQuant = frame->quant;
1151            Data_d.quant_sq = frame->quant*frame->quant;
1152            Data_d.dctSpace = dct_space;
1153            Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
1154            Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices;
1155    
1156            Data_d.RefQ = h->RefQ;
1157    
1158            memcpy(&Data_f, &Data_d, sizeof(SearchData));
1159            memcpy(&Data_b, &Data_d, sizeof(SearchData));
1160            memcpy(&Data_i, &Data_d, sizeof(SearchData));
1161    
1162            Data_f.iFcode = Data_i.iFcode = frame->fcode;
1163            Data_b.iFcode = Data_i.bFcode = frame->bcode;
1164    
1165            max_mbs = 0;
1166    
1167            for (j = (start_y+y_row); j < stop_y; j += y_step) {
1168                    if (j == start_y) max_mbs = pParam->mb_width; /* we can process all blocks of the first row */
1169    
1170                    f_predMV = b_predMV = zeroMV;   /* prediction is reset at left boundary */
1171    
1172                    for (i = 0; i < (int) pParam->mb_width; i++) {
1173                            MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
1174                            const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
1175                            pMB->mode = -1;
1176    
1177                            initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i,
1178                                              i, j, f_ref, f_refH->y, f_refV->y, f_refHV->y,
1179                                              b_ref, b_refH->y, b_refV->y, b_refHV->y,
1180                                              &frame->image, b_mb);
1181    
1182                            if (current_mb >= max_mbs) {
1183                                    /* we ME-ed all macroblocks we safely could. grab next portion */
1184                                    int above_count = *complete_count_above; /* sync point */
1185                                    if (above_count == pParam->mb_width) {
1186                                            /* full line above is ready */
1187                                            above_count = pParam->mb_width+1;
1188                                            if (j < stop_y-y_step) {
1189                                                    /* this is not last line, grab a portion of MBs from the next line too */
1190                                                    above_count += MAX(0, complete_count_above[1] - 1);
1191                                            }
1192                                    }
1193    
1194                                    max_mbs = current_mb + above_count - i - 1;
1195    
1196                                    if (current_mb >= max_mbs) {
1197                                            /* current workload is zero */
1198                                            i--;
1199                                            sched_yield();
1200                                            continue;
1201                                    }
1202                            }
1203    
1204    /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
1205                            if (b_reference->coding_type != S_VOP)
1206                                    if (b_mb->mode == MODE_NOT_CODED) {
1207                                            pMB->mode = MODE_NOT_CODED;
1208                                            pMB->mvs[0] = pMB->b_mvs[0] = zeroMV;
1209                                            pMB->sad16 = 0;
1210                                            *complete_count_self = i+1;
1211                                            current_mb++;
1212                                            continue;
1213                                    }
1214    
1215    /* direct search comes first, because it (1) checks for SKIP-mode
1216            and (2) sets very good predictions for forward and backward search */
1217                            skip_sad = SearchDirect_initial(i, j, frame->motion_flags, TRB, TRD, pParam, pMB,
1218                                                                                            b_mb, &best_sad, &Data_d);
1219    
1220                            if (pMB->mode == MODE_DIRECT_NONE_MV) {
1221                                    pMB->sad16 = best_sad;
1222                                    pMB->cbp = 0;
1223                                    *complete_count_self = i+1;
1224                                    current_mb++;
1225                                    continue;
1226                            }
1227    
1228                            SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB,
1229                                                    &f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]);
1230    
1231                            SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB,
1232                                                    &b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]);
1233    
1234                            if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP)
1235                                    fb_thresh = best_sad;
1236                            else
1237                                    fb_thresh = best_sad + (best_sad>>1);
1238    
1239                            if (Data_f.iMinSAD[0] <= fb_thresh)
1240                                    SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f);
1241    
1242                            if (Data_b.iMinSAD[0] <= fb_thresh)
1243                                    SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b);
1244    
1245                            SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad,
1246                                                                      &Data_i, Data_f.currentMV[0], Data_b.currentMV[0]);
1247    
1248                            if (((Data_i.iMinSAD[0] < best_sad +(best_sad>>3)) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE))
1249                                    || Data_i.iMinSAD[0] <= best_sad)
1250    
1251                                    SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i);
1252    
1253                            if (Data_d.iMinSAD[0] <= 2*best_sad)
1254                                    if ((!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH) && (best_sad > 750))
1255                                            || (best_sad > 1000))
1256    
1257                                            SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d);
1258    
1259                            /* final skip decision */
1260                            if ( (skip_sad < 2 * Data_d.iQuant * MAX_SAD00_FOR_SKIP )
1261                                    && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) {
1262    
1263                                    Data_d.chromaSAD = 0; /* green light for chroma check */
1264    
1265                                    SkipDecisionB(pMB, &Data_d);
1266    
1267                                    if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */
1268                                            pMB->sad16 = skip_sad;
1269                                            pMB->cbp = 0;
1270                                            *complete_count_self = i+1;
1271                                            current_mb++;
1272                                            continue;
1273                                    }
1274                            }
1275    
1276                            if (frame->vop_flags & XVID_VOP_RD_BVOP)
1277                                    ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i,
1278                                            pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad);
1279                            else
1280                                    ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV);
1281    
1282                            *complete_count_self = i+1;
1283                            current_mb++;
1284                            maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel);
1285                    }
1286    
1287                    complete_count_self++;
1288                    complete_count_above++;
1289            }
1290    
1291            h->minfcode = getMinFcode(MVmaxF);
1292            h->minbcode = getMinFcode(MVmaxB);
1293    }

Legend:
Removed from v.1567  
changed lines
  Added in v.1913

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4