[cvs] / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/encoder.c

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

revision 1.95.2.30, Fri Jun 13 12:11:27 2003 UTC revision 1.95.2.47, Wed Nov 5 16:15:47 2003 UTC
# Line 38  Line 38 
38  #include "image/font.h"  #include "image/font.h"
39  #include "motion/sad.h"  #include "motion/sad.h"
40  #include "motion/motion.h"  #include "motion/motion.h"
41    #include "motion/gmc.h"
42    
43  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
44  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
45  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 115  Line 117 
117          Encoder *pEnc;          Encoder *pEnc;
118      int n;      int n;
119    
120          if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */          if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
121                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
122    
123          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
# Line 224  Line 226 
226          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
227    
228      /* max keyframe interval */      /* max keyframe interval */
229      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ?      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
                 (10 * pEnc->mbParam.fbase) / pEnc->mbParam.fincr : create->max_key_interval;  
230    
231      /* allocate working frame-image memory */      /* allocate working frame-image memory */
232    
# Line 388  Line 389 
389                  image_null(&pEnc->queue[n].image);                  image_null(&pEnc->queue[n].image);
390    
391    
392          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
         {  
393                  if (image_create                  if (image_create
394                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
395                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
396                          goto xvid_err_memory5;                          goto xvid_err_memory5;
   
397          }          }
398    
   
399          /* timestamp stuff */          /* timestamp stuff */
400    
401          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
# Line 422  Line 420 
420    
421    xvid_err_memory5:    xvid_err_memory5:
422    
423          if (pEnc->mbParam.max_bframes > 0) {          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
424          int i;                          image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
   
                 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {  
                         image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,  
425                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
426                  }                  }
427    
428                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
429    
430    xvid_err_memory4:    xvid_err_memory4:
431    
# Line 444  Line 439 
439    
440                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
441                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
442                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
443                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
444                  }                  }
445    
446                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 537  Line 529 
529          int i;          int i;
530    
531          /* B Frames specific */          /* B Frames specific */
         if (pEnc->mbParam.max_bframes > 0) {  
   
532                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
   
533                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
534                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
535                  }                  }
                 xvid_free(pEnc->queue);  
         }  
536    
537            xvid_free(pEnc->queue);
538    
539          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
540    
# Line 557  Line 545 
545    
546                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
547                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
548                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
549                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
550                  }                  }
551    
# Line 739  Line 725 
725    
726              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
727              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
728                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
729              }              }
730          }          }
731    
# Line 769  Line 755 
755      }      }
756    
757      /* call plugins */      /* call plugins */
758      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
759          emms();          emms();
760          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
761              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
# Line 865  Line 851 
851          int type;          int type;
852          Bitstream bs;          Bitstream bs;
853    
854          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))     /* v1.x.x */
855                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
856    
857          xFrame->out_flags = 0;          xFrame->out_flags = 0;
# Line 902  Line 888 
888    
889                  if (xFrame->quant_intra_matrix)                  if (xFrame->quant_intra_matrix)
890                  {                  {
891                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
892                          q->frame.quant_intra_matrix = q->quant_intra_matrix;                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
893                  }                  }
894    
895                  if (xFrame->quant_inter_matrix)                  if (xFrame->quant_inter_matrix)
896                  {                  {
897                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
898                          q->frame.quant_inter_matrix = q->quant_inter_matrix;                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
899                  }                  }
900    
# Line 1008  Line 994 
994                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
995    
996                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
997                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
998                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
999                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1000    
1001                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1002                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 1096  Line 1084 
1084    
1085          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1086                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1087                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                          "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1088          }          }
1089    
1090          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1175  Line 1163 
1163                  }                  }
1164    
1165                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1166                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1167                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1168                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1169          type = P_VOP;          type = P_VOP;
1170      }      }
1171    
# Line 1194  Line 1184 
1184                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1185                  }                  }
1186    
1187                    pEnc->iFrameNum = 1;
1188    
1189                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1190                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1191                    switch(frame->par) {
1192                    case XVID_PAR_11_VGA:
1193                    case XVID_PAR_43_PAL:
1194                    case XVID_PAR_43_NTSC:
1195                    case XVID_PAR_169_PAL:
1196                    case XVID_PAR_169_NTSC:
1197                    case XVID_PAR_EXT:
1198                            pEnc->mbParam.par = frame->par;
1199                            break;
1200                    default:
1201                            pEnc->mbParam.par = XVID_PAR_EXT;
1202                            break;
1203                    }
1204                    pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1;
1205                    pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1;
1206    
1207          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1208                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
# Line 1250  Line 1256 
1256           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1257           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1258    
1259  done_flush:  /*done_flush:*/
1260    
1261      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1262    
# Line 1282  Line 1288 
1288  {  {
1289      unsigned int i,j;      unsigned int i,j;
1290      int quant = frame->quant;      int quant = frame->quant;
1291        if (quant > 31)
1292                    frame->quant = quant = 31;
1293            else if (quant < 1)
1294                    frame->quant = quant = 1;
1295    
1296      for (j=0; j<pParam->mb_height; j++)      for (j=0; j<pParam->mb_height; j++)
1297      for (i=0; i<pParam->mb_width; i++) {      for (i=0; i<pParam->mb_width; i++) {
# Line 1289  Line 1299 
1299          quant += pMB->dquant;          quant += pMB->dquant;
1300          if (quant > 31)          if (quant > 31)
1301                          quant = 31;                          quant = 31;
1302                  if (quant < 1)                  else if (quant < 1)
1303                          quant = 1;                          quant = 1;
1304          pMB->quant = quant;          pMB->quant = quant;
1305      }      }
# Line 1353  Line 1363 
1363    
1364          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1365    
1366          BitstreamPadAlways(bs);          BitstreamPad(bs);
1367    
1368          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1369    
1370          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
# Line 1392  Line 1403 
1403          }          }
1404          emms();          emms();
1405    
1406  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1407  #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1408      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1409    
1410          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1411          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1412    
1413            pEnc->current->is_edged = 0; /* not edged */
1414            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1415    
1416          return 1;                                       /* intra */          return 1;                                       /* intra */
1417  }  }
1418    
# Line 1428  Line 1434 
1434          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1435          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1436    
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
1437          int iLimit;          int iLimit;
1438          int x, y, k;          int x, y, k;
1439          int iSearchRange;          int iSearchRange;
1440          int bIntra, skip_possible;          int bIntra=0, skip_possible;
1441            FRAMEINFO *const current = pEnc->current;
1442            FRAMEINFO *const reference = pEnc->reference;
1443            MBParam * const pParam = &pEnc->mbParam;
1444            int mb_width = pParam->mb_width;
1445            int mb_height = pParam->mb_height;
1446    
         /* IMAGE *pCurrent = &pEnc->current->image; */  
         IMAGE *pRef = &pEnc->reference->image;  
1447    
1448          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))          /* IMAGE *pCurrent = &current->image; */
1449            IMAGE *pRef = &reference->image;
1450    
1451            if ((current->vop_flags & XVID_VOP_REDUCED))
1452          {          {
1453                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
1454                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pParam->height + 31) / 32;
1455          }          }
1456    
1457    
1458            if (!reference->is_edged) {
1459          start_timer();          start_timer();
1460          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1461                                     pEnc->mbParam.width, pEnc->mbParam.height);                                             pParam->width, pParam->height);
1462          stop_edges_timer();          stop_edges_timer();
1463                    reference->is_edged = 1;
1464            }
1465    
1466          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1467          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1468          pEnc->current->fcode = pEnc->mbParam.m_fcode;          current->fcode = pParam->m_fcode;
1469    
1470          if (!force_inter)          if (!force_inter)
1471                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
1472          else          else
1473                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1474    
1475          if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1476                    if (reference->is_interpolated != current->rounding_type) {
1477                  start_timer();                  start_timer();
1478                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1479                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                            &pEnc->vInterHV, pParam->edged_width,
1480                                                    pEnc->mbParam.edged_height,                                                            pParam->edged_height,
1481                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1482                                                    pEnc->current->rounding_type);                                                            current->rounding_type);
1483                  stop_inter_timer();                  stop_inter_timer();
1484                            reference->is_interpolated = current->rounding_type;
1485                    }
1486          }          }
1487    
1488          pEnc->current->coding_type = P_VOP;          current->coding_type = P_VOP;
1489    
1490    
1491      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, current);
1492    
1493          start_timer();          start_timer();
1494          bIntra = MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1495                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          {       int gmcval;
1496                           iLimit);                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1497                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1498    
1499                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1500                            gmcval = GlobalMotionEstRefine(&current->warp,
1501                                                                    current->mbs, pParam,
1502                                                                    current, reference,
1503                                                                    &current->image,
1504                                                                    &reference->image,
1505                                                                    &pEnc->vInterH,
1506                                                                    &pEnc->vInterV,
1507                                                                    &pEnc->vInterHV);
1508                            gmcval += /*current->quant */ 2 * (int)(pParam->mb_width*pParam->mb_height);
1509                    }
1510    
1511                    gmcval = globalSAD(&current->warp, pParam, current->mbs,
1512                                                            current,
1513                                                            &reference->image,
1514                                                            &current->image,
1515                                                            pEnc->vGMC.y);
1516                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1517    
1518    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1519                    generate_GMCparameters( 3, 3, &current->warp,
1520                                    pParam->width, pParam->height,
1521                                    &current->new_gmc_data);
1522    
1523                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1524                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1525                    {
1526                            current->coding_type = S_VOP;
1527    
1528                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1529                                    pParam->mb_width, pParam->mb_height,
1530                                    pParam->edged_width, pParam->edged_width/2,
1531                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1532                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1533    
1534          stop_motion_timer();                  } else {
1535    
1536          if (bIntra == 1) return FrameCodeI(pEnc, bs);                          generate_GMCimage(&current->new_gmc_data, &reference->image,
1537                                    pParam->mb_width, pParam->mb_height,
1538                                    pParam->edged_width, pParam->edged_width/2,
1539                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1540                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1541                    }
1542            }
1543    
1544          if ( ( pEnc->current->vol_flags & XVID_VOL_GMC )          bIntra =
1545                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )                  MotionEstimation(&pEnc->mbParam, current, reference,
1546          {                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1547                  pEnc->current->coding_type = S_VOP;                                           &pEnc->vGMC, iLimit);
1548    
                 generate_GMCparameters( 2, 16, &pEnc->current->warp,  
                                         pEnc->mbParam.width, pEnc->mbParam.height,  
                                         &pEnc->current->gmc_data);  
   
                 generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,  
                                 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,  
                                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,  
                                 pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0,  
                                 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);  
1549    
1550          }          stop_motion_timer();
1551    
1552          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1553    
1554            set_timecodes(current,reference,pParam->fbase);
1555          if (vol_header)          if (vol_header)
1556          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1557                  BitstreamPadAlways(bs);                  BitstreamPad(bs);
1558          }          }
1559    
1560          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1);
1561    
1562          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1563                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1564    
1565    
1566          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1567                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1568                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1569                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &current->mbs[x + y * pParam->mb_width];
   
 /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */  
 /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */  
1570    
1571                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1572    
1573                          if (bIntra) {                          if (bIntra) {
1574                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1575                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1576                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1577    
1578                                  start_timer();                                  start_timer();
1579                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1580                                  stop_prediction_timer();                                  stop_prediction_timer();
1581    
1582                                  pEnc->current->sStat.kblks++;                                  current->sStat.kblks++;
1583    
1584                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1585                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1586                                            qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1587                                            qcoeff[5*64+0]=0;
1588                                    }
1589                                    MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1590                                  stop_coding_timer();                                  stop_coding_timer();
1591                                  continue;                                  continue;
1592                          }                          }
1593    
                         if (pEnc->current->coding_type == S_VOP) {  
   
                                 int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->mbParam.edged_width, 65536);  
   
                                 if (pEnc->current->motion_flags & XVID_ME_CHROMA16) {  
                                         iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
   
                                         iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
                                 }  
   
                                 if (iSAD <= pMB->sad16) {               /* mode decision GMC */  
   
                                         if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))  
                                                 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;  
                                         else  
                                                 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
                                         pMB->mode = MODE_INTER;  
                                         pMB->mcsel = 1;  
                                         pMB->sad16 = iSAD;  
                                 } else {  
                                         pMB->mcsel = 0;  
                                 }  
                         } else {  
                                 pMB->mcsel = 0; /* just a precaution */  
                         }  
   
1594                          start_timer();                          start_timer();
1595                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1596                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1597                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1598                                                                   &pEnc->current->image,                                                                   &current->image,
1599                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1600                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1601                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1602                                                                   (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1603                                                                   (pEnc->current->vop_flags & XVID_VOP_REDUCED),                                                                   (current->vop_flags & XVID_VOP_REDUCED),
1604                                                                   pEnc->current->rounding_type);                                                                   current->rounding_type);
1605    
1606                          stop_comp_timer();                          stop_comp_timer();
1607    
# Line 1592  Line 1613 
1613    
1614                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->mode != MODE_NOT_CODED)
1615                          {       pMB->cbp =                          {       pMB->cbp =
1616                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
1617                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1618                          }                          }
1619    
1620                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1621                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1622                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1623                                  pEnc->current->sStat.mblks++;                                  current->sStat.mblks++;
1624                          }  else {                          }  else {
1625                                  pEnc->current->sStat.ublks++;                                  current->sStat.ublks++;
1626                          }                          }
1627    
1628                          start_timer();                          start_timer();
# Line 1611  Line 1632 
1632                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1633                                                          (pMB->dquant == 0);                                                          (pMB->dquant == 0);
1634    
1635                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1636                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1637                          else if (pEnc->current->coding_type == P_VOP) {                          else if (current->coding_type == P_VOP) {
1638                                  if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))
1639                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1640                                  else                                  else
1641                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
# Line 1624  Line 1645 
1645    
1646  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1647    
1648                                  if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */                                  if (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */
1649                                  {                                  {
1650                                          int bSkip = 1;                                          int bSkip = 1;
1651    
1652                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1653                                          {                                          {
1654                                                  int iSAD;                                                  int iSAD;
1655                                                  iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1656                                                                          pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1657                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);                                                                  pParam->edged_width,BFRAME_SKIP_THRESHHOLD);
1658                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1659                                                  {       bSkip = 0;                                                  {       bSkip = 0;
1660                                                          break;                                                          break;
# Line 1641  Line 1662 
1662                                          }                                          }
1663    
1664                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1665                                                  if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                                  if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1666                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1667                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1668                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1669                                                  }                                                  }
1670                                                  else {                                                  else {
1671                                                          VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1672                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1673                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1674                                                  }                                                  }
1675                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1676                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1677                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1678                                                  stop_coding_timer();                                                  stop_coding_timer();
1679    
1680                                                  continue;       /* next MB */                                                  continue;       /* next MB */
# Line 1668  Line 1689 
1689                          }                          }
1690                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1691    
1692                          if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE))                          if ((current->vop_flags & XVID_VOP_GREYSCALE))
1693                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1694                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1695                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1696                          }                          }
1697    
1698                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1699                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1700                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1701                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1702                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1703                          } else {                          } else {
1704                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1705                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1706                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1707                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
# Line 1691  Line 1712 
1712                          {       int k;                          {       int k;
1713                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1714                                  {                                  {
1715                                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1716                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k);
1717                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1718                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1719                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1720                                          } else {                                          } else {
1721                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k);
1722                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1723                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1724                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
# Line 1706  Line 1727 
1727                                  }                                  }
1728                          }                          }
1729    
1730                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1731                          stop_coding_timer();                          stop_coding_timer();
1732    
1733                  }                  }
1734          }          }
1735    
1736          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))          if ((current->vop_flags & XVID_VOP_REDUCED))
1737          {          {
1738                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&current->image, pParam->edged_width,
1739                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          current->mbs, mb_width, mb_height, pParam->mb_width,
1740                          16, 0);                          16, 0);
1741          }          }
1742    
1743          emms();          emms();
1744    
1745          if (pEnc->current->sStat.iMvCount == 0)          if (current->sStat.iMvCount == 0)
1746                  pEnc->current->sStat.iMvCount = 1;                  current->sStat.iMvCount = 1;
1747    
1748          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);          fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);
1749    
1750          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pParam->m_fcode);
1751    
1752          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1753          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0)  )))     /* maximum search range 128 */          && (pParam->m_fcode <= (3 +  (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0)  ))) /* maximum search range 128 */
1754          {          {
1755                  pEnc->mbParam.m_fcode++;                  pParam->m_fcode++;
1756                  iSearchRange *= 2;                  iSearchRange *= 2;
1757          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1758                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1759                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1760                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) )))    /* minimum search range 16 */                             && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) )))        /* minimum search range 16 */
1761          {          {
1762                  pEnc->mbParam.m_fcode--;                  pParam->m_fcode--;
1763                  iSearchRange /= 2;                  iSearchRange /= 2;
1764          }          }
1765    
# Line 1746  Line 1767 
1767    
1768          /* frame drop code */          /* frame drop code */
1769  #if 0  #if 0
1770          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1771  #endif  #endif
1772          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          if (current->sStat.kblks + current->sStat.mblks <
1773                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100)
1774          {          {
1775                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = 0;
1776                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1777    
1778                  BitstreamReset(bs);                  BitstreamReset(bs);
1779    
1780                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
1781                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0);
1782    
1783                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1784                  pEnc->current->quant = pEnc->reference->quant;                  current->quant = reference->quant;
1785                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  current->motion_flags = reference->motion_flags;
1786                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1787                  pEnc->current->fcode = pEnc->reference->fcode;                  current->fcode = reference->fcode;
1788                  pEnc->current->bcode = pEnc->reference->bcode;                  current->bcode = reference->bcode;
1789                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1790                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1791          }          }
1792    
1793            pEnc->current->is_edged = 0; /* not edged */
1794            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1795    
1796            /* what was this frame's interpolated reference will become
1797                    forward (past) reference in b-frame coding */
1798    
1799            image_swap(&pEnc->vInterH, &pEnc->f_refh);
1800            image_swap(&pEnc->vInterV, &pEnc->f_refv);
1801            image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1802    
1803    
1804          /* XXX: debug          /* XXX: debug
1805          {          {
1806                  char s[100];                  char s[100];
1807                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1808                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
1809                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1810                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1811    
1812                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1813                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
1814                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1815                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1816          }          }
1817          */          */
1818    
1819  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
 #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1820    
1821      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      current->length = (BitstreamPos(bs) - bits) / 8;
1822    
1823          return 0;                                       /* inter */          return 0;                                       /* inter */
1824  }  }
# Line 1829  Line 1852 
1852  #endif  #endif
1853    
1854          /* forward  */          /* forward  */
1855            if (!pEnc->reference->is_edged) {
1856          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1857                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1858                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1859                    pEnc->current->is_edged = 1;
1860            }
1861    
1862            if (pEnc->reference->is_interpolated != 0) {
1863          start_timer();          start_timer();
1864          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1865                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1866                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1867          stop_inter_timer();          stop_inter_timer();
1868                    pEnc->reference->is_interpolated = 0;
1869            }
1870    
1871          /* backward */          /* backward */
1872            if (!pEnc->current->is_edged) {
1873          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1874                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1875                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1876                    pEnc->current->is_edged = 1;
1877            }
1878    
1879            if (pEnc->current->is_interpolated != 0) {
1880          start_timer();          start_timer();
1881          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1882                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1883                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1884          stop_inter_timer();          stop_inter_timer();
1885                    pEnc->current->is_interpolated = 0;
1886            }
1887    
1888          start_timer();          start_timer();
1889          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
# Line 1873  Line 1910 
1910          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1911                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1912                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
                         int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0;  
1913    
1914                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1915                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1900  Line 1936 
1936                                  }                                  }
1937                          }                          }
1938    
1939  #ifdef BFRAMES_DEC_DEBUG                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the
1940          BFRAME_DEBUG                           * coding function for BFrames, that's why we don't zero teh DC
1941  #endif                           * coeffs */
1942                            if ((frame->vop_flags & XVID_VOP_GREYSCALE))
1943                                    mb->cbp &= 0x3C;
1944    
1945                          start_timer();                          start_timer();
1946                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1947                                                   &frame->sStat, direction);                                                   &frame->sStat);
1948                          stop_coding_timer();                          stop_coding_timer();
1949                  }                  }
1950          }          }
# Line 1914  Line 1953 
1953    
1954          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1955    
1956      BitstreamPadAlways(bs);      BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1957          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1958    
1959  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.30  
changed lines
  Added in v.1.95.2.47

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