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

Diff of /trunk/xvidcore/src/encoder.c

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

revision 1501, Sat Jul 17 11:42:46 2004 UTC revision 1573, Fri Dec 10 04:10:12 2004 UTC
# 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: encoder.c,v 1.106 2004-07-17 11:42:46 edgomez Exp $   * $Id: encoder.c,v 1.115 2004-12-10 04:10:12 syskin Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 103  Line 103 
103          if (*base > 65535 || *inc > 65535) {          if (*base > 65535 || *inc > 65535) {
104                  int *biggest;                  int *biggest;
105                  int *other;                  int *other;
106                    float div;
107    
108                  if (*base > *inc) {                  if (*base > *inc) {
109                          biggest = base;                          biggest = base;
# Line 112  Line 113 
113                          other = base;                          other = base;
114                  }                  }
115    
116                  float div = ((float)*biggest)/((float)65535);                  div = ((float)*biggest)/((float)65535);
117                  *biggest = (int)(((float)*biggest)/div);                  *biggest = (int)(((float)*biggest)/div);
118                  *other = (int)(((float)*other)/div);                  *other = (int)(((float)*other)/div);
119          }          }
# Line 1316  Line 1317 
1317    
1318                  /* prevent vol/vop misuse */                  /* prevent vol/vop misuse */
1319    
                 if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
                         pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;  
   
1320                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1321                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1322    
# Line 1446  Line 1444 
1444    
1445          uint16_t x, y;          uint16_t x, y;
1446    
         if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
   
                 /* 16x16->8x8 downsample requires 1 additional edge pixel*/  
                 /* XXX: setedges is overkill */  
                 start_timer();  
                 image_setedges(&pEnc->current->image,  
                         pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,  
                         pEnc->mbParam.width, pEnc->mbParam.height, 0);  
                 stop_edges_timer();  
         }  
   
1447          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1448          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1449          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
# Line 1495  Line 1479 
1479                          stop_prediction_timer();                          stop_prediction_timer();
1480    
1481                          start_timer();                          start_timer();
                         if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)  
                         {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                 qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
1482                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1483                          stop_coding_timer();                          stop_coding_timer();
1484                  }                  }
1485    
         if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,  
                         16, 0);  
         }  
1486          emms();          emms();
1487    
1488          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
# Line 1525  Line 1498 
1498          return 1;                                       /* intra */          return 1;                                       /* intra */
1499  }  }
1500    
1501    static __inline void
1502    updateFcode(Statistics * sStat, Encoder * pEnc)
1503    {
1504            float fSigma;
1505            int iSearchRange;
1506    
1507  #define INTRA_THRESHOLD 0.5          if (sStat->iMvCount == 0)
1508  #define BFRAME_SKIP_THRESHHOLD 30                  sStat->iMvCount = 1;
1509    
1510            fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
1511    
1512            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1513    
1514            if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1515                    pEnc->mbParam.m_fcode++;
1516    
1517            else if ((5.0 * fSigma < iSearchRange)
1518                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1519                               && (pEnc->mbParam.m_fcode >= 2) )
1520                    pEnc->mbParam.m_fcode--;
1521    
1522            pEnc->fMvPrevSigma = fSigma;
1523    }
1524    
1525    #define BFRAME_SKIP_THRESHHOLD 30
1526    
1527  /* FrameCodeP also handles S(GMC)-VOPs */  /* FrameCodeP also handles S(GMC)-VOPs */
1528  static int  static int
1529  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1530                     Bitstream * bs)                     Bitstream * bs)
1531  {  {
         float fSigma;  
1532          int bits = BitstreamPos(bs);          int bits = BitstreamPos(bs);
1533    
1534          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1535          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1536    
1537          int x, y, k;          int x, y, k;
         int iSearchRange;  
1538          int skip_possible;          int skip_possible;
1539          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1540          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
# Line 1551  Line 1543 
1543          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1544          int coded = 1;          int coded = 1;
1545    
   
         /* IMAGE *pCurrent = &current->image; */  
1546          IMAGE *pRef = &reference->image;          IMAGE *pRef = &reference->image;
1547    
         if ((current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 mb_width = (pParam->width + 31) / 32;  
                 mb_height = (pParam->height + 31) / 32;  
         }  
   
   
1548          if (!reference->is_edged) {          if (!reference->is_edged) {
1549                  start_timer();                  start_timer();
1550                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
# Line 1587  Line 1570 
1570                  }                  }
1571          }          }
1572    
1573            current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1574                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1575    
1576          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1577    
1578          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
# Line 1655  Line 1641 
1641    
1642          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1643    
         current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =  
                 current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;  
   
   
1644          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1645                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1646                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1647                                  &current->mbs[x + y * pParam->mb_width];                                  &current->mbs[x + y * pParam->mb_width];
1648    
1649                          int bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
   
                         if (bIntra) {  
1650                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1651                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1652                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
# Line 1677  Line 1657 
1657    
1658                                  current->sStat.kblks++;                                  current->sStat.kblks++;
1659    
                                 if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)  
                                 {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                         qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                         qcoeff[5*64+0]=0;  
                                 }  
1660                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1661                                  stop_coding_timer();                                  stop_coding_timer();
1662                                  continue;                                  continue;
# Line 1696  Line 1671 
1671                                                                   pParam->height,                                                                   pParam->height,
1672                                                                   pParam->edged_width,                                                                   pParam->edged_width,
1673                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
                                                                  (current->vop_flags & XVID_VOP_REDUCED),  
1674                                                                   current->rounding_type);                                                                   current->rounding_type);
1675    
1676                          stop_comp_timer();                          stop_comp_timer();
1677    
1678                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1679    
1680                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1681                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,  
1682                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1683                          }                          }
1684    
# Line 1785  Line 1758 
1758                          }                          }
1759                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1760    
                         if ((current->vop_flags & XVID_VOP_GREYSCALE))  
                         {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                 qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
   
1761                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1762                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1763                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
# Line 1829  Line 1796 
1796                  }                  }
1797          }          }
1798    
         if ((current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 image_deblock_rrv(&current->image, pParam->edged_width,  
                         current->mbs, mb_width, mb_height, pParam->mb_width,  
                         16, 0);  
         }  
   
1799          emms();          emms();
1800            updateFcode(&current->sStat, pEnc);
         if (current->sStat.iMvCount == 0)  
                 current->sStat.iMvCount = 1;  
   
         fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);  
   
         iSearchRange = 1 << (3 + pParam->m_fcode);  
   
         if ((fSigma > iSearchRange / 3)  
                 && (pParam->m_fcode <= (3 +  (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0)  ))) /* maximum search range 128 */  
         {  
                 pParam->m_fcode++;  
                 iSearchRange *= 2;  
         } else if ((fSigma < iSearchRange / 6)  
                            && (pEnc->fMvPrevSigma >= 0)  
                            && (pEnc->fMvPrevSigma < iSearchRange / 6)  
                            && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) )))        /* minimum search range 16 */  
         {  
                 pParam->m_fcode--;  
                 iSearchRange /= 2;  
         }  
   
         pEnc->fMvPrevSigma = fSigma;  
1801    
1802          /* frame drop code */          /* frame drop code */
1803  #if 0  #if 0
# Line 1944  Line 1882 
1882                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1883          }          }
1884    
         /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */  
   
1885          if (!first){          if (!first){
1886                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1887          }          }
# Line 2016  Line 1952 
1952                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1953                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1954                                          MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,                                          MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1955                                                                                          NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0);                                                                                          NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
1956                                  }                                  }
   
1957                                  continue;                                  continue;
1958                          }                          }
1959    
1960                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                          mb->quant = frame->quant;
1961    
1962                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1963                                    /* we have to motion-compensate, transfer etc,
1964                                            because there might be blocks to code */
1965    
1966                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1967                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1968                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1969                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1970                                                                           dct_codes);                                                                           dct_codes);
1971    
                                 if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;  
                                 mb->quant = frame->quant;  
   
                                 if (mb->mode != MODE_DIRECT_NONE_MV)  
1972                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
   
                                 if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)  
                                         && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {  
                                         mb->mode = MODE_DIRECT_NONE_MV; /* skipped */  
                                 }  
1973                          }                          }
1974    
1975                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the                          if (mb->mode == MODE_DIRECT_NO4V)
1976                           * coding function for BFrames, that's why we don't zero teh DC                                  mb->mode = MODE_DIRECT;
1977                           * coeffs */  
1978                          if ((frame->vop_flags & XVID_VOP_GREYSCALE))                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
1979                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
1980                            else
1981                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
1982                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
1983                                  mb->cbp &= 0x3C;                                  mb->cbp &= 0x3C;
1984    
1985                          start_timer();                          start_timer();
# Line 2056  Line 1991 
1991    
1992          emms();          emms();
1993    
         /* TODO: dynamic fcode/bcode ??? */  
   
1994          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1995          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1996    

Legend:
Removed from v.1501  
changed lines
  Added in v.1573

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