[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 317, Fri Jul 19 14:56:00 2002 UTC revision 402, Wed Sep 4 21:16:02 2002 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6     *  Copyright(C) 2002 Michael Militzer
7     *
8   *  This program is an implementation of a part of one or more MPEG-4   *  This program is an implementation of a part of one or more MPEG-4
9   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
10   *  to use this software module in hardware or software products are   *  to use this software module in hardware or software products are
# Line 28  Line 30 
30   *   *
31   ****************************************************************************/   ****************************************************************************/
32    
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              MinChen <chenm001@163.com>  
  *  20.06.2002 bframe patch  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
  *  $Id: encoder.c,v 1.57 2002-07-19 14:56:00 chl Exp $  
  *  
  ****************************************************************************/  
   
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <stdio.h>  #include <stdio.h>
35  #include <math.h>  #include <math.h>
# Line 55  Line 42 
42  #include "image/image.h"  #include "image/image.h"
43  #ifdef BFRAMES  #ifdef BFRAMES
44  #include "image/font.h"  #include "image/font.h"
45    #include "motion/sad.h"
46  #endif  #endif
47  #include "motion/motion.h"  #include "motion/motion.h"
48  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
# Line 209  Line 197 
197    
198          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
199    
200          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
201                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
202    
203          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
# Line 344  Line 332 
332          pEnc->global = pParam->global;          pEnc->global = pParam->global;
333          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
334          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
335            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
336          pEnc->bframes = NULL;          pEnc->bframes = NULL;
337    
338          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 421  Line 410 
410          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
411          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
412          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
413          pEnc->last_pframe = 1;          pEnc->last_pframe = 0;
414            pEnc->last_sync = 0;
415  #endif  #endif
416    
417          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
# Line 617  Line 607 
607  }  }
608    
609    
 #ifdef BFRAMES  
610  void inc_frame_num(Encoder * pEnc)  void inc_frame_num(Encoder * pEnc)
611  {  {
         pEnc->iFrameNum++;  
612          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
613    
614    #ifdef BFRAMES
615            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
616            if (pEnc->mbParam.m_ticks < pEnc->last_sync)
617                    pEnc->mbParam.m_seconds = 1;            // more than 1 second since last I or P is not supported.
618            else
619                    pEnc->mbParam.m_seconds = 0;
620    
621            if (pEnc->current->coding_type != B_VOP)
622                    pEnc->last_sync = pEnc->mbParam.m_ticks;
623    #else
624    
625          pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;          pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
626          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
627  }  
628  #endif  #endif
629    
630    }
631    
632    
633  #ifdef BFRAMES  #ifdef BFRAMES
634  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
# Line 823  Line 824 
824    
825                  pFrame->intra = 0;                  pFrame->intra = 0;
826    
827                  BitstreamPutBits(&bs, 0x7f, 8);                  BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
828                  BitstreamPad(&bs);                  BitstreamPad(&bs);
829                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
830    
# Line 852  Line 853 
853                  else                  else
854                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
855    
856                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
857                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
858    
859                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
860                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
861    */
862                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
863                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
864    
# Line 1004  Line 1005 
1005                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1006                   */                   */
1007    
                 DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
   
1008                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1009                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1010                  }                  }
# Line 1019  Line 1016 
1016                  } else {                  } else {
1017                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1018                  }                  }
1019                    if (pEnc->current->quant < 1)
1020                            pEnc->current->quant = 1;
1021    
1022                    if (pEnc->current->quant > 31)
1023                            pEnc->current->quant = 31;
1024    
1025    
1026                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1027                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1028                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1029    
1030    
1031    
1032                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1033                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 1033  Line 1042 
1042                  goto bvop_loop;                  goto bvop_loop;
1043          }          }
1044    
1045            pEnc->iFrameNum++;
1046    
1047          BitstreamPad(&bs);          BitstreamPad(&bs);
1048          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1049    
# Line 1107  Line 1118 
1118    
1119          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1120          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
 #ifdef BFRAMES  
1121          pEnc->current->seconds = pEnc->mbParam.m_seconds;          pEnc->current->seconds = pEnc->mbParam.m_seconds;
1122          pEnc->current->ticks = pEnc->mbParam.m_ticks;          pEnc->current->ticks = pEnc->mbParam.m_ticks;
 #endif  
1123          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1124    
1125          start_timer();          start_timer();
# Line 1240  Line 1249 
1249          DEBUG(temp);          DEBUG(temp);
1250  #endif  #endif
1251    
 #ifdef BFRAMES  
1252          inc_frame_num(pEnc);          inc_frame_num(pEnc);
 #else  
1253          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1254    
1255          stop_global_timer();          stop_global_timer();
1256          write_timer();          write_timer();
# Line 1525  Line 1530 
1530                          stop_prediction_timer();                          stop_prediction_timer();
1531    
1532                          start_timer();                          start_timer();
1533                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1534                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1535                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1536                                    qcoeff[5*64+0]=0;
1537                            }
1538                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1539                          stop_coding_timer();                          stop_coding_timer();
1540                  }                  }
# Line 1546  Line 1556 
1556    
1557    
1558  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1559    #define BFRAME_SKIP_THRESHHOLD 16
1560    
1561  static int  static int
1562  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1560  Line 1571 
1571          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1572    
1573          int iLimit;          int iLimit;
1574          uint32_t x, y;          int x, y, k;
1575          int iSearchRange;          int iSearchRange;
1576          int bIntra;          int bIntra;
1577    
# Line 1688  Line 1699 
1699                          }                          }
1700    
1701                          start_timer();                          start_timer();
1702    
1703                            /* Finished processing the MB, now check if to CODE or SKIP */
1704    
1705                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1706                                    pMB->mvs[0].y == 0) {
1707    
1708    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1709    
1710    #ifdef BFRAMES
1711                                    int iSAD=BFRAME_SKIP_THRESHHOLD;
1712                                    int bSkip=1;
1713    
1714                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1715                                    {
1716                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1717                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1718                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1719                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD)
1720                                            {       bSkip = 0;
1721                                                    break;
1722                                            }
1723                                    }
1724                                    if (!bSkip)
1725                                    {
1726                                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1727                                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1728                                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1729                                                    qcoeff[5*64+0]=0;
1730                                            }
1731                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1732                                            pMB->cbp = 0x80;                /* trick! so cbp!=0, but still nothing is written to bs */
1733                                    }
1734                                    else
1735                                            MBSkip(bs);
1736    
1737    #else
1738                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1739    
1740    #endif
1741    
1742                            } else {
1743                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1744                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1745                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1746                                            qcoeff[5*64+0]=0;
1747                                    }
1748                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1749                            }
1750    
1751                          stop_coding_timer();                          stop_coding_timer();
1752                  }                  }
1753          }          }
# Line 1722  Line 1781 
1781    
1782          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1783    
1784    #ifdef BFRAMES
1785            /* frame drop code */
1786            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1787            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1788                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1789            {
1790                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1791                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1792    
1793                    BitstreamReset(bs);
1794                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1795    
1796                    // copy reference frame details into the current frame
1797                    pEnc->current->quant = pEnc->reference->quant;
1798                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1799                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1800                    pEnc->current->fcode = pEnc->reference->fcode;
1801                    pEnc->current->bcode = pEnc->reference->bcode;
1802                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1803                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1804    
1805            }
1806    #endif
1807    
1808          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1809    
1810          pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase;  #ifdef BFRAMES
1811          fprintf(stderr,"fbase=%d last_p=%d ticks=%d time_pp = %d\n",pEnc->mbParam.fbase,pEnc->last_pframe,pEnc->mbParam.m_ticks,pEnc->time_pp);          pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1812                            (int32_t)pEnc->mbParam.fbase;
1813            pEnc->last_pframe = pEnc->current->ticks;
1814    #endif
1815    
         pEnc->last_pframe = pEnc->mbParam.m_ticks;  
1816          return 0;                                       // inter          return 0;                                       // inter
1817  }  }
1818    
# Line 1783  Line 1868 
1868          stop_inter_timer();          stop_inter_timer();
1869    
1870          start_timer();          start_timer();
         fprintf(stderr,"m_ticks =%d\n",(int32_t)pEnc->mbParam.m_ticks+1);  
1871          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1872            ((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase,                  ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1873                                                  pEnc->time_pp,                                                  pEnc->time_pp,
1874                                                  pEnc->reference->mbs, f_ref,                                                  pEnc->reference->mbs, f_ref,
1875                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
# Line 1846  Line 1930 
1930    
1931                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1932                          mb->cbp =                          mb->cbp =
1933                                  MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,                                  MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                                                   qcoeff);  
1934                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1935    
1936                            if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1937                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  && (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) {
                                 && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {  
1938                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
1939                          }                          }
1940    
1941    /* update predictors for forward and backward vectors */
1942                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1943                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1944                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;
# Line 1869  Line 1952 
1952                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1953                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1954                          }                          }
1955    
1956  //                      DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y);  //                      DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y);
1957    
1958  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
# Line 1896  Line 1980 
1980  #endif  #endif
1981  }  }
1982  #endif  #endif
1983    
1984    
1985    /*      in case internal output is needed somewhere... */
1986    /*      {
1987            FILE *filehandle;
1988            filehandle=fopen("last-b.pgm","wb");
1989            if (filehandle)
1990            {
1991                    fprintf(filehandle,"P5\n\n");           //
1992                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1993                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1994                    fclose(filehandle);
1995                    }
1996            }
1997    */

Legend:
Removed from v.317  
changed lines
  Added in v.402

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