[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.53, Wed Jul 10 19:16:32 2002 UTC revision 1.76.2.1, Mon Sep 23 20:36:01 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37   *      20.06.2002 bframe patch   *      20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
# Line 41  Line 43 
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
46    
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <stdio.h>  #include <stdio.h>
49  #include <math.h>  #include <math.h>
# Line 53  Line 56 
56  #include "image/image.h"  #include "image/image.h"
57  #ifdef BFRAMES  #ifdef BFRAMES
58  #include "image/font.h"  #include "image/font.h"
59    #include "motion/sad.h"
60  #endif  #endif
61  #include "motion/motion.h"  #include "motion/motion.h"
62  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
# Line 66  Line 70 
70  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
71  #include "utils/mem_align.h"  #include "utils/mem_align.h"
72    
 #ifdef _SMP  
 #include "motion/smp_motion_est.h"  
 #endif  
73  /*****************************************************************************  /*****************************************************************************
74   * Local macros   * Local macros
75   ****************************************************************************/   ****************************************************************************/
# Line 90  Line 91 
91                                            bool force_inter,                                            bool force_inter,
92                                            bool vol_header);                                            bool vol_header);
93    
 #ifdef BFRAMES  
94  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
95                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
96                                             Bitstream * bs,                                             Bitstream * bs,
97                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
98    
99  /*****************************************************************************  /*****************************************************************************
100   * Local data   * Local data
# Line 207  Line 206 
206    
207          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
208    
209          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
210                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
211    
   
212          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
213          if (pEnc == NULL)          if (pEnc == NULL)
214                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
# Line 269  Line 267 
267  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
268          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
269  #endif  #endif
270  #ifdef BFRAMES  
271          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
272          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
273          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
274  #endif  
275          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
276          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
277          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 288  Line 286 
286                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
287                  goto xvid_err_memory3;                  goto xvid_err_memory3;
288  #endif  #endif
289  #ifdef BFRAMES  
290          if (image_create          if (image_create
291                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
292                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 301  Line 299 
299                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
300                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
301                  goto xvid_err_memory3;                  goto xvid_err_memory3;
302  #endif  
303          if (image_create          if (image_create
304                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
305                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 334  Line 332 
332    
333    
334          /* B Frames specific init */          /* B Frames specific init */
 #ifdef BFRAMES  
335    
336          pEnc->global = pParam->global;          pEnc->global = pParam->global;
337          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
338          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
339            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
340          pEnc->bframes = NULL;          pEnc->bframes = NULL;
341    
342          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 416  Line 414 
414          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
415          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
416          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
417  #endif          pEnc->last_pframe = 0;
418            pEnc->last_sync = 0;
419    
420          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
421    
# Line 435  Line 434 
434          /*          /*
435           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
436           */           */
437  #ifdef BFRAMES  
438    xvid_err_memory5:    xvid_err_memory5:
439    
440    
# Line 469  Line 468 
468                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
469          }          }
470    
 #endif  
   
471    xvid_err_memory3:    xvid_err_memory3:
472  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
473          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
474                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
475  #endif  #endif
476    
 #ifdef BFRAMES  
477          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
478                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
479          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
480                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
481          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
482                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
483    
484          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
485                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 529  Line 524 
524  int  int
525  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
526  {  {
 #ifdef BFRAMES  
527          int i;          int i;
 #endif  
528    
529          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
530    
531          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
532          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
533    
534                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 566  Line 558 
558                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
559    
560          }          }
 #endif  
561    
562          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
563    
# Line 584  Line 575 
575                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
576          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
577                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
578  #ifdef BFRAMES  
579          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
580                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
581          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
582                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
583          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
584                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
585  #endif  
586  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
587          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
588                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 611  Line 602 
602  }  }
603    
604    
605  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
606  {  {
         pEnc->iFrameNum++;  
607          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
608    
         pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;  
609          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
610            if (pEnc->mbParam.m_ticks < pEnc->last_sync)
611                    pEnc->mbParam.m_seconds = 1;
612                                    // more than 1 second since last I or P is not supported.
613            else
614                    pEnc->mbParam.m_seconds = 0;
615    
616  }  }
 #endif  
617    
618    
619  #ifdef BFRAMES  static __inline void
620  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
621  {  {
622          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
623          {          {
# Line 647  Line 640 
640          pEnc->queue_size++;          pEnc->queue_size++;
641          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
642  }  }
 #endif  
643    
644    
645  #ifdef BFRAMES  
646  /*****************************************************************************  /*****************************************************************************
647   * IPB frame encoder entry point   * IPB frame encoder entry point
648   *   *
# Line 817  Line 809 
809    
810                  pFrame->intra = 0;                  pFrame->intra = 0;
811    
812                  BitstreamPutBits(&bs, 0x7f, 8);                  BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
813                  BitstreamPad(&bs);                  BitstreamPad(&bs);
814                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
815    
# Line 831  Line 823 
823    
824          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
825    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
826          emms();          emms();
827    
828          // only inc frame num, adapt quant, etc. if we havent seen it before          // only inc frame num, adapt quant, etc. if we havent seen it before
# Line 846  Line 833 
833                  else                  else
834                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
835    
836                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
837                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
838    
839                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
840                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
841    */
842                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
843                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
844    
# Line 998  Line 985 
985                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
986                   */                   */
987    
                 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);  
   
988                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
989                          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");
990                  }                  }
# Line 1013  Line 996 
996                  } else {                  } else {
997                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
998                  }                  }
999                    if (pEnc->current->quant < 1)
1000                            pEnc->current->quant = 1;
1001    
1002                    if (pEnc->current->quant > 31)
1003                            pEnc->current->quant = 31;
1004    
1005    
1006                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1007                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1008                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1009    
1010    
1011    
1012                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1013                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 1027  Line 1022 
1022                  goto bvop_loop;                  goto bvop_loop;
1023          }          }
1024    
1025            pEnc->iFrameNum++;
1026    
1027          BitstreamPad(&bs);          BitstreamPad(&bs);
1028          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1029    
# Line 1062  Line 1059 
1059          return XVID_ERR_OK;          return XVID_ERR_OK;
1060  }  }
1061    
 #endif  
   
1062    
1063    
1064  /*****************************************************************************  /*****************************************************************************
# Line 1101  Line 1096 
1096    
1097          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1098          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
 #ifdef BFRAMES  
1099          pEnc->current->seconds = pEnc->mbParam.m_seconds;          pEnc->current->seconds = pEnc->mbParam.m_seconds;
1100          pEnc->current->ticks = pEnc->mbParam.m_ticks;          pEnc->current->ticks = pEnc->mbParam.m_ticks;
 #endif  
1101          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1102    
1103          start_timer();          start_timer();
# Line 1234  Line 1227 
1227          DEBUG(temp);          DEBUG(temp);
1228  #endif  #endif
1229    
 #ifdef BFRAMES  
1230          inc_frame_num(pEnc);          inc_frame_num(pEnc);
 #else  
1231          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1232    
1233          stop_global_timer();          stop_global_timer();
1234          write_timer();          write_timer();
# Line 1490  Line 1479 
1479          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1480    
1481          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1482  #ifdef BFRAMES  
1483  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1484          if ((pEnc->global & XVID_GLOBAL_PACKED)) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1485                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1486          }          }
 #endif  
1487          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1488    
1489          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1519  Line 1507 
1507                          stop_prediction_timer();                          stop_prediction_timer();
1508    
1509                          start_timer();                          start_timer();
1510                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1511                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1512                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1513                                    qcoeff[5*64+0]=0;
1514                            }
1515                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1516                          stop_coding_timer();                          stop_coding_timer();
1517                  }                  }
# Line 1531  Line 1524 
1524          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1525          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1526    
1527            pEnc->last_pframe = pEnc->current->ticks;
1528            pEnc->last_sync = pEnc->current->ticks;
1529    
1530          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1531                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1532          }          }
# Line 1540  Line 1536 
1536    
1537    
1538  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1539    #define BFRAME_SKIP_THRESHHOLD 30
1540    
1541  static int  static int
1542  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1554  Line 1551 
1551          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1552    
1553          int iLimit;          int iLimit;
1554          uint32_t x, y;          int x, y, k;
1555          int iSearchRange;          int iSearchRange;
1556          int bIntra;          int bIntra;
1557    
# Line 1590  Line 1587 
1587          start_timer();          start_timer();
1588          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1589                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1590          } else {                  if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1591                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1592    
1593  #ifdef _SMP          } else {
                 if (NUMTHREADS > 1)  
                         bIntra =  
                                 SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                                                          &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                                          iLimit);  
                 else  
 #endif  
1594    
1595                  bIntra =                  bIntra =
1596                           MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                           MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1597                                                      &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                      &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1598                                                      iLimit);                                                      iLimit);
1599    
   
1600          }          }
1601          stop_motion_timer();          stop_motion_timer();
1602    
1603          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1604    
1605          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1606    
# Line 1623  Line 1611 
1611    
1612          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1613    
1614          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1615          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1616    
1617          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1660  Line 1646 
1646    
1647                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1648    
1649                                    if (pMB->mode != MODE_NOT_CODED)
1650                                  pMB->cbp =                                  pMB->cbp =
1651                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1652                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1684  Line 1671 
1671                          }                          }
1672    
1673                          start_timer();                          start_timer();
1674    
1675                            /* Finished processing the MB, now check if to CODE or SKIP */
1676    
1677                            if ((pMB->mode == MODE_NOT_CODED) ||
1678                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1679                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1680    
1681    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1682    
1683                                    int bSkip = 1;
1684                                    pMB->mode = MODE_NOT_CODED;
1685    
1686                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1687                                    {
1688                                            int iSAD;
1689                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1690                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1691                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1692                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1693                                            {       bSkip = 0;
1694                                                    break;
1695                                            }
1696                                    }
1697                                    if (!bSkip)
1698                                    {
1699                                            VECTOR predMV;
1700                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1701                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1702                                            pMB->mode = MODE_INTER;
1703                                            pMB->cbp = 0;
1704                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1705                                    }
1706                                    else MBSkip(bs);
1707    
1708                            } else {
1709                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1710                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1711                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1712                                            qcoeff[5*64+0]=0;
1713                                    }
1714                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1715                            }
1716    
1717                          stop_coding_timer();                          stop_coding_timer();
1718                  }                  }
1719          }          }
# Line 1718  Line 1747 
1747    
1748          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1749    
1750    #ifdef BFRAMES
1751            /* frame drop code */
1752            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1753            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1754                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1755            {
1756                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1757                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1758    
1759                    BitstreamReset(bs);
1760                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1761    
1762                    // copy reference frame details into the current frame
1763                    pEnc->current->quant = pEnc->reference->quant;
1764                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1765                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1766                    pEnc->current->fcode = pEnc->reference->fcode;
1767                    pEnc->current->bcode = pEnc->reference->bcode;
1768                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1769                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1770    
1771            }
1772    #endif
1773    
1774          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1775    
1776            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1777                            (int32_t)pEnc->mbParam.fbase;
1778            pEnc->last_pframe = pEnc->current->ticks;
1779    
1780          return 0;                                       // inter          return 0;                                       // inter
1781  }  }
1782    
1783    
1784  #ifdef BFRAMES  static __inline void
 static void  
1785  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1786                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1787                     Bitstream * bs,                     Bitstream * bs,
# Line 1734  Line 1790 
1790          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1791          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1792          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1793    
1794          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1795          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1796    
1797    #ifdef BFRAMES_DEC_DEBUG
1798            FILE *fp;
1799            static char first=0;
1800    #define BFRAME_DEBUG    if (!first && fp){ \
1801                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1802            }
1803    
1804            if (!first){
1805                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1806            }
1807    #endif
1808    
1809          // forward          // forward
1810          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1811                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
# Line 1763  Line 1829 
1829          stop_inter_timer();          stop_inter_timer();
1830    
1831          start_timer();          start_timer();
1832          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1833                    ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1834                                                    pEnc->time_pp,
1835                                                    pEnc->reference->mbs, f_ref,
1836                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1837                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1838                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1788  Line 1857 
1857    
1858    
1859          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 // reset prediction  
   
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
   
1860                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1861                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
                                 &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK *b_mb =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
1862    
1863                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1864                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1865                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
1866                                  continue;                                  continue;
1867                          }                          }
1868    
1869                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1870                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1871                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1872                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1873                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1874                                                                           dct_codes);                                                                           dct_codes);
1875    
1876                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1877                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp =  
                                 MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,  
                                                                   qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
   
1878    
1879                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  mb->cbp =
1880                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mode = 5;   // skipped  
                         }  
1881    
1882                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1883                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1884                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
1885                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {  
                                 mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;  
                                 mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
1886                          }                          }
 //                      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);  
1887    
1888    #ifdef BFRAMES_DEC_DEBUG
1889            BFRAME_DEBUG
1890    #endif
1891                          start_timer();                          start_timer();
1892                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1893                                                   &pEnc->sStat);                                                   &pEnc->sStat);
# Line 1854  Line 1900 
1900          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1901    
1902          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1903    
1904    #ifdef BFRAMES_DEC_DEBUG
1905            if (!first){
1906                    first=1;
1907                    if (fp)
1908                            fclose(fp);
1909  }  }
1910  #endif  #endif
1911    }
1912    
1913    
1914    /*      in case internal output is needed somewhere... */
1915    /*      {
1916            FILE *filehandle;
1917            filehandle=fopen("last-b.pgm","wb");
1918            if (filehandle)
1919            {
1920                    fprintf(filehandle,"P5\n\n");           //
1921                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1922                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1923                    fclose(filehandle);
1924                    }
1925            }
1926    */

Legend:
Removed from v.1.53  
changed lines
  Added in v.1.76.2.1

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