[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 1566, Sun Dec 5 13:56:13 2004 UTC revision 1574, Fri Dec 10 05:37:11 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.110 2004-12-05 13:56:13 syskin Exp $   * $Id: encoder.c,v 1.116 2004-12-10 05:37:11 syskin Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 1479  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                  }                  }
# Line 1503  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;  
         int skip_possible;  
1538          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1539          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1540          MBParam * const pParam = &pEnc->mbParam;          MBParam * const pParam = &pEnc->mbParam;
# Line 1529  Line 1542 
1542          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1543          int coded = 1;          int coded = 1;
1544    
   
         /* IMAGE *pCurrent = &current->image; */  
1545          IMAGE *pRef = &reference->image;          IMAGE *pRef = &reference->image;
1546    
1547          if (!reference->is_edged) {          if (!reference->is_edged) {
# Line 1558  Line 1569 
1569                  }                  }
1570          }          }
1571    
1572            current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1573                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1574    
1575          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1576    
1577          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 1626  Line 1640 
1640    
1641          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1642    
         current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =  
                 current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;  
   
   
1643          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1644                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1645                          MACROBLOCK *pMB =                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1646                                  &current->mbs[x + y * pParam->mb_width];                          int skip_possible;
   
                         int bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);  
1647    
1648                          if (bIntra) {                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1649                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1650                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1651                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
# Line 1648  Line 1656 
1656    
1657                                  current->sStat.kblks++;                                  current->sStat.kblks++;
1658    
                                 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;  
                                 }  
1659                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1660                                  stop_coding_timer();                                  stop_coding_timer();
1661                                  continue;                                  continue;
# Line 1673  Line 1676 
1676    
1677                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1678    
1679                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1680                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,  
1681                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1682                          }                          }
1683    
# Line 1695  Line 1697 
1697    
1698                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1699    
1700                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                                                         (pMB->dquant == 0);  
1701    
1702                          if (current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1703                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1704                          else if (current->coding_type == P_VOP) {                          else { /* PVOP */
1705                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1706                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                                                                  pMB->qmvs : pMB->mvs;
1707                                  else                                  skip_possible &= ((mv->x|mv->y) == 0);
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
1708                          }                          }
1709    
1710                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
   
1711  /* 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 */
   
                                 if (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */  
                                 {  
1712                                          int bSkip = 1;                                          int bSkip = 1;
1713    
1714                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1715                                          {  
1716                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1717                                                  int iSAD;                                                  int iSAD;
1718                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1719                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1720                                                                  pParam->edged_width,BFRAME_SKIP_THRESHHOLD);                                                                                  pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1721                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1722                                                  {       bSkip = 0;                                                          bSkip = 0; /* could not SKIP */
1723                                                          break;                                                          if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
                                                 }  
                                         }  
   
                                         if (!bSkip) {   /* no SKIP, but trivial block */  
                                                 if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
1724                                                          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);
1725                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1726                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1727                                                  }                                                          } else {
                                                 else {  
1728                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1729                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1730                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1731                                                  }                                                  }
1732                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1733                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1734                                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                                                          break;
1735                                                  stop_coding_timer();                                                  }
   
                                                 continue;       /* next MB */  
1736                                          }                                          }
1737                                  }                                  }
                                 /* do SKIP */  
1738    
1739                                    if (bSkip) {
1740                                            /* do SKIP */
1741                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1742                                  MBSkip(bs);                                  MBSkip(bs);
1743                                  stop_coding_timer();                                  stop_coding_timer();
1744                                  continue;       /* next MB */                                  continue;       /* next MB */
1745                          }                          }
                         /* ordinary case: normal coded INTER/INTER4V block */  
   
                         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;  
                         }  
   
                         if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.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);  
                         } else {  
                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->mvs[0].y - predMV.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);  
                         }  
   
   
                         if (pMB->mode == MODE_INTER4V)  
                         {       int k;  
                                 for (k=1;k<4;k++)  
                                 {  
                                         if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.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);  
                                         } else {  
                                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->mvs[k].y - predMV.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);  
                                         }  
   
                                 }  
1746                          }                          }
1747    
1748                            /* ordinary case: normal coded INTER/INTER4V block */
1749                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1750                          stop_coding_timer();                          stop_coding_timer();
   
1751                  }                  }
1752          }          }
1753    
1754          emms();          emms();
1755            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;  
1756    
1757          /* frame drop code */          /* frame drop code */
1758  #if 0  #if 0
# Line 1979  Line 1909 
1909                                          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,
1910                                                                                          NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);                                                                                          NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
1911                                  }                                  }
   
1912                                  continue;                                  continue;
1913                          }                          }
1914    
1915                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                          mb->quant = frame->quant;
1916    
1917                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1918                                    /* we have to motion-compensate, transfer etc,
1919                                            because there might be blocks to code */
1920    
1921                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1922                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1923                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1924                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1925                                                                           dct_codes);                                                                           dct_codes);
1926    
                                 if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;  
                                 mb->quant = frame->quant;  
   
                                 if (mb->mode != MODE_DIRECT_NONE_MV)  
1927                                          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 */  
                                 }  
1928                          }                          }
1929    
1930                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the                          if (mb->mode == MODE_DIRECT_NO4V)
1931                           * coding function for BFrames, that's why we don't zero teh DC                                  mb->mode = MODE_DIRECT;
1932                           * coeffs */  
1933                          if ((frame->vop_flags & XVID_VOP_GREYSCALE))                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
1934                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
1935                            else
1936                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
1937                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
1938                                  mb->cbp &= 0x3C;                                  mb->cbp &= 0x3C;
1939    
1940                          start_timer();                          start_timer();
# Line 2017  Line 1946 
1946    
1947          emms();          emms();
1948    
         /* TODO: dynamic fcode/bcode ??? */  
   
1949          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1950          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1951    

Legend:
Removed from v.1566  
changed lines
  Added in v.1574

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