[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.57, Fri Jul 19 14:56:00 2002 UTC revision 1.99.2.1, Sat May 3 23:24:30 2003 UTC
# Line 26  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
  ****************************************************************************/  
   
 /*****************************************************************************  
  *  
  *  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()  
  *  
29   *  $Id$   *  $Id$
30   *   *
31   ****************************************************************************/   ****************************************************************************/
# Line 53  Line 40 
40  #include "global.h"  #include "global.h"
41  #include "utils/timer.h"  #include "utils/timer.h"
42  #include "image/image.h"  #include "image/image.h"
 #ifdef BFRAMES  
43  #include "image/font.h"  #include "image/font.h"
44  #endif  #include "motion/sad.h"
45  #include "motion/motion.h"  #include "motion/motion.h"
46  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
47  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 68  Line 54 
54  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
55  #include "utils/mem_align.h"  #include "utils/mem_align.h"
56    
 #ifdef _SMP  
 #include "motion/smp_motion_est.h"  
 #endif  
57  /*****************************************************************************  /*****************************************************************************
58   * Local macros   * Local macros
59   ****************************************************************************/   ****************************************************************************/
60    
61  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
62  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)   { _T_ tmp = A; A = B; B = tmp; }
63    
64  /*****************************************************************************  /*****************************************************************************
65   * Local function prototypes   * Local function prototypes
# Line 90  Line 73 
73                                            Bitstream * bs,                                            Bitstream * bs,
74                                            uint32_t * pBits,                                            uint32_t * pBits,
75                                            bool force_inter,                                            bool force_inter,
76                                            bool vol_header);                                            bool vol_header,
77                                              int interpolate);
78    
 #ifdef BFRAMES  
79  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
80                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
81                                             Bitstream * bs,                                             Bitstream * bs,
82                                             uint32_t * pBits);                                             uint32_t * pBits,
83  #endif                                             int interpolate_forward,
84                                               int interpolate_backward);
85    
86  /*****************************************************************************  /*****************************************************************************
87   * Local data   * Local data
# Line 112  Line 96 
96  };  };
97    
98    
 static void __inline  
 image_null(IMAGE * image)  
 {  
         image->y = image->u = image->v = NULL;  
 }  
   
   
99  /*****************************************************************************  /*****************************************************************************
100   * Encoder creation   * Encoder creation
101   *   *
# Line 142  Line 119 
119  {  {
120          Encoder *pEnc;          Encoder *pEnc;
121          int i;          int i;
   
122          pParam->handle = NULL;          pParam->handle = NULL;
123    
124          ENC_CHECK(pParam);          ENC_CHECK(pParam);
# Line 209  Line 185 
185    
186          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
187    
188          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
189                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
190    
191          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
# Line 236  Line 212 
212    
213          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
214    
215  #ifdef _SMP          pEnc->fMvPrevSigma = -1;
         pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);  
 #endif  
   
         pEnc->sStat.fMvPrevSigma = -1;  
216    
217          /* Fill rate control parameters */          /* Fill rate control parameters */
218    
219          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
220    
221          pEnc->iFrameNum = 0;          pEnc->iFrameNum = -1;
222          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
223    
224          /* try to allocate frame memory */          /* try to allocate frame memory */
225    
# Line 271  Line 243 
243    
244          /* try to allocate image memory */          /* try to allocate image memory */
245    
246  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
247          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
248  #endif  
 #ifdef BFRAMES  
249          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
250          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
251          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
252  #endif  
253          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
254          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
255          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 287  Line 258 
258          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
259          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
260    
261  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
262          if (image_create          {       if (image_create
263                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
264                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
265                  goto xvid_err_memory3;                  goto xvid_err_memory3;
266  #endif          }
267  #ifdef BFRAMES  
268          if (image_create          if (image_create
269                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
270                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 306  Line 277 
277                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
278                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
279                  goto xvid_err_memory3;                  goto xvid_err_memory3;
280  #endif  
281          if (image_create          if (image_create
282                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
283                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 336  Line 307 
307                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
308                  goto xvid_err_memory3;                  goto xvid_err_memory3;
309    
310    /* Create full bitplane for GMC, this might be wasteful */
311            if (image_create
312                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
313                     pEnc->mbParam.edged_height) < 0)
314                    goto xvid_err_memory3;
315    
316    
         /* B Frames specific init */  
 #ifdef BFRAMES  
317    
318          pEnc->global = pParam->global;          pEnc->mbParam.global = pParam->global;
319    
320            /* B Frames specific init */
321          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
322          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
323            pEnc->mbParam.bquant_offset = pParam->bquant_offset;
324            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
325          pEnc->bframes = NULL;          pEnc->bframes = NULL;
326    
327          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 390  Line 368 
368    
369          pEnc->queue = NULL;          pEnc->queue = NULL;
370    
   
371          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
372                  int n;                  int n;
373    
# Line 417  Line 394 
394          pEnc->queue_tail = 0;          pEnc->queue_tail = 0;
395          pEnc->queue_size = 0;          pEnc->queue_size = 0;
396    
397            pEnc->mbParam.m_stamp = 0;
398    
         pEnc->mbParam.m_seconds = 0;  
         pEnc->mbParam.m_ticks = 0;  
399          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
400          pEnc->last_pframe = 1;          pEnc->current->stamp = 0;
401  #endif          pEnc->reference->stamp = 0;
402    
403          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
404    
# Line 441  Line 417 
417          /*          /*
418           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
419           */           */
 #ifdef BFRAMES  
   xvid_err_memory5:  
420    
421      xvid_err_memory5:
422    
423          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
424    
# Line 475  Line 450 
450                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
451          }          }
452    
 #endif  
   
453    xvid_err_memory3:    xvid_err_memory3:
454  #ifdef _DEBUG_PSNR  
455          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
456            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
457                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
458  #endif          }
459    
 #ifdef BFRAMES  
460          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
461                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
462          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
463                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
464          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
465                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
466    
467          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
468                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 507  Line 479 
479          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
480                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
481    
482    /* destroy GMC image */
483            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
484                                      pEnc->mbParam.edged_height);
485    
486    
487    xvid_err_memory2:    xvid_err_memory2:
488          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
489          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 535  Line 512 
512  int  int
513  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
514  {  {
 #ifdef BFRAMES  
515          int i;          int i;
 #endif  
516    
517          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
518    
519          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
520          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
521    
522                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 553  Line 527 
527                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
528          }          }
529    
   
530          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
531    
532                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 572  Line 545 
545                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
546    
547          }          }
 #endif  
548    
549          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
550    
# Line 590  Line 562 
562                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
563          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
564                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
565  #ifdef BFRAMES  
566          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
567                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
568          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
569                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
570          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
571                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
572  #endif  
573  #ifdef _DEBUG_PSNR          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
574          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
575                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
576  #endif          }
577    
578          /* Encoder structure */          /* Encoder structure */
579    
# Line 617  Line 589 
589  }  }
590    
591    
592  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
593  {  {
594          pEnc->iFrameNum++;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
595          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
   
         pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;  
         pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;  
596  }  }
 #endif  
597    
598    
599  #ifdef BFRAMES  static __inline void
600  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
601  {  {
602          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
603          {          {
# Line 646  Line 613 
613          start_timer();          start_timer();
614          if (image_input          if (image_input
615                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
616                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
617                  return;                  return;
618          stop_conv_timer();          stop_conv_timer();
619    
620            if ((pFrame->general & XVID_CHROMAOPT)) {
621                    image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
622                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
623            }
624    
625          pEnc->queue_size++;          pEnc->queue_size++;
626          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
627  }  }
628  #endif  
629    static __inline void
630    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
631    {
632    
633                    pCur->ticks = (int32_t)pCur->stamp % time_base;
634                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
635    
636                    /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */
637    
638    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
639                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
640                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
641                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
642                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
643                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
644    
645    */
646    }
647    
648    
649    
650    /* convert pFrame->intra to coding_type */
651    static int intra2coding_type(int intra)
652    {
653            if (intra < 0)  return -1;
654            if (intra == 1) return I_VOP;
655            if (intra == 2) return B_VOP;
656    
657            return P_VOP;
658    }
659    
660    
661    
 #ifdef BFRAMES  
662  /*****************************************************************************  /*****************************************************************************
663   * IPB frame encoder entry point   * IPB frame encoder entry point
664   *   *
# Line 674  Line 676 
676          uint16_t x, y;          uint16_t x, y;
677          Bitstream bs;          Bitstream bs;
678          uint32_t bits;          uint32_t bits;
679            int mode = -1; /* Just to shut up compiler warning */
680    
681          int input_valid = 1;          int input_valid = 1;
682            int bframes_count = 0;
683  #ifdef _DEBUG_PSNR          int interpolate_forward = 1;
684          float psnr;          int interpolate_backward = 1;
         char temp[128];  
 #endif  
685    
686          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
687          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
# Line 711  Line 712 
712                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
713    
714                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
715                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
716    
717                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
718    
719                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          if (pEnc->mbParam.m_quarterpel)
720                                    pEnc->current->global_flags |= XVID_QUARTERPEL;
721                            else
722                                    pEnc->current->global_flags &= ~XVID_QUARTERPEL;
723    
724                          BitstreamPad(&bs);                          FrameCodeP(pEnc, &bs, &bits, 1, 0, interpolate_backward);
725                            bframes_count = 0;
726                            interpolate_forward = 0;
727                            interpolate_backward = 1;
728    
729                            BitstreamPadAlways(&bs);
730                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
731                          pFrame->intra = 0;                          pFrame->intra = 0;
732    
733    
734                            emms();
735    
736                            if (pResult) {
737                                    pResult->quant = pEnc->current->quant;
738                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
739                                    pResult->kblks = pEnc->current->sStat.kblks;
740                                    pResult->mblks = pEnc->current->sStat.mblks;
741                                    pResult->ublks = pEnc->current->sStat.ublks;
742                                    pResult->iblks = pEnc->current->sStat.iblks;
743                                    pResult->qblks = pEnc->current->sStat.qblks;
744                            }
745    
746                          return XVID_ERR_OK;                          return XVID_ERR_OK;
747                  }                  }
748    
# Line 729  Line 751 
751                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
752                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
753    
754                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  if (pEnc->mbParam.m_quarterpel)
755                            pEnc->current->global_flags |= XVID_QUARTERPEL;
756                    else
757                            pEnc->current->global_flags &= ~XVID_QUARTERPEL;
758    
759                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs,
760                                       &bits, interpolate_forward, interpolate_backward);
761                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
762    
763                  BitstreamPad(&bs);                  interpolate_forward = 0;
764                    interpolate_backward = 0;
765    
766                    BitstreamPadAlways(&bs);
767                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
768                  pFrame->intra = 0;                  pFrame->intra = 2;
769    
770                    if (pResult) {
771                            pResult->quant = pEnc->current->quant;
772                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
773                            pResult->kblks = pEnc->current->sStat.kblks;
774                            pResult->mblks = pEnc->current->sStat.mblks;
775                            pResult->ublks = pEnc->current->sStat.ublks;
776                            pResult->iblks = pEnc->current->sStat.iblks;
777                            pResult->qblks = pEnc->current->sStat.qblks;
778                    }
779    
780                    emms();
781    
782                    if (pFrame->quant == 0) {
783                            RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
784                                                              pFrame->length, pFrame->intra);
785                    }
786    
787                  if (input_valid)                  if (input_valid)
788                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
789    
790                    emms();
791    
792                  return XVID_ERR_OK;                  return XVID_ERR_OK;
793          }          }
794    
795          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
796                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
797    
798                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  /* write an empty marker to the bitstream.
799                       for divx5 decoder compatibility, this marker must consist
800                       of a not-coded p-vop, with a time_base of zero, and time_increment
801                       indentical to the future-referece frame.
802                    */
803    
804                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
805                            int tmp;
806    
807                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
808                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
809                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
810    
811                            tmp = pEnc->current->seconds;
812                            pEnc->current->seconds = 0; /* force time_base = 0 */
813    
814                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
815                          BitstreamPad(&bs);                          pEnc->current->seconds = tmp;
                         BitstreamPutBits(&bs, 0x7f, 8);  
816    
817                            BitstreamPadAlways(&bs);
818                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
819                          pFrame->intra = 0;                          pFrame->intra = 4;
820    
821                            if (pResult) {
822                                    pResult->quant = pEnc->current->quant;
823                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
824                                    pResult->kblks = pEnc->current->sStat.kblks;
825                                    pResult->mblks = pEnc->current->sStat.mblks;
826                                    pResult->ublks = pEnc->current->sStat.ublks;
827                                    pResult->iblks = pEnc->current->sStat.iblks;
828                                    pResult->qblks = pEnc->current->sStat.qblks;
829                            }
830    
831                            emms();
832    
833                            if (pFrame->quant == 0) {
834                                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
835                                                                      pFrame->length, pFrame->intra);
836                            }
837    
838                          if (input_valid)                          if (input_valid)
839                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
840    
841                            emms();
842    
843                          return XVID_ERR_OK;                          return XVID_ERR_OK;
844                  }                  }
845          }          }
# Line 771  Line 850 
850          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->bframenum_dx50bvop != -1)
851          {          {
852    
853                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
854                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
855    
856                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
857                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
858                  }                  }
859    
# Line 786  Line 865 
865    
866          } else if (input_valid) {          } else if (input_valid) {
867    
868                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
869    
870                  start_timer();                  start_timer();
871                  if (image_input                  if (image_input
872                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
873                          pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
874                    {
875                            emms();
876                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
877                    }
878                  stop_conv_timer();                  stop_conv_timer();
879    
880                  // queue input frame, and dequue next image                  if ((pFrame->general & XVID_CHROMAOPT)) {
881                            image_chroma_optimize(&pEnc->current->image,
882                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
883                    }
884    
885                    /* queue input frame, and dequue next image */
886                  if (pEnc->queue_size > 0)                  if (pEnc->queue_size > 0)
887                  {                  {
888                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
# Line 809  Line 896 
896    
897          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
898    
899                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
900    
901                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
902                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
903                  pEnc->queue_size--;                  pEnc->queue_size--;
904    
905          } else if (BitstreamPos(&bs) == 0) {          } else {
906    
907                    /* if nothing was encoded, write an 'ignore this frame' flag
908                       to the bitstream */
909    
910                    if (BitstreamPos(&bs) == 0) {
911    
912                  DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
913                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
914                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
915    
916                  pFrame->intra = 0;                          /* That disabled line of code was supposed to inform VirtualDub
917                             * that the frame was a dummy delay frame - now disabled (thx god :-)
918                             */
919                            //BitstreamPutBits(&bs, 0x7f, 8);
920                            pFrame->intra = 5;
921    
922                  BitstreamPutBits(&bs, 0x7f, 8);                          if (pResult) {
923                  BitstreamPad(&bs);                                  /*
924                  pFrame->length = BitstreamLength(&bs);                                   * We must decide what to put there because i know some apps
925                                     * are storing statistics about quantizers and just do
926                                     * stats[quant]++ or stats[quant-1]++
927                                     * transcode is one of these app with its 2pass module
928                                     */
929    
930                  return XVID_ERR_OK;                                  /*
931                                     * For now i prefer 31 than 0 that could lead to a segfault
932                                     * in transcode
933                                     */
934                                    pResult->quant = 31;
935    
936                                    pResult->hlength = 0;
937                                    pResult->kblks = 0;
938                                    pResult->mblks = 0;
939                                    pResult->ublks = 0;
940                                    pResult->iblks = 0;
941                                    pResult->qblks = 0;
942                            }
943          } else {          } else {
944    
945                            if (pResult) {
946                                    pResult->quant = pEnc->current->quant;
947                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
948                                    pResult->kblks = pEnc->current->sStat.kblks;
949                                    pResult->mblks = pEnc->current->sStat.mblks;
950                                    pResult->ublks = pEnc->current->sStat.ublks;
951                                    pResult->iblks = pEnc->current->sStat.iblks;
952                                    pResult->qblks = pEnc->current->sStat.qblks;
953                            }
954    
955                    }
956    
957                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
958    
959                    emms();
960    
961                  return XVID_ERR_OK;                  return XVID_ERR_OK;
962          }          }
963    
964          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
965    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
966          emms();          emms();
967    
968          // 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 */
969          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
970          {          {
971                    mode = intra2coding_type(pFrame->intra);
972                  if (pFrame->quant == 0)                  if (pFrame->quant == 0)
973                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
974                  else                  else
975                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
976    
977                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
978                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
979    
980                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
981                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
982    */
983                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
984                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
985    
# Line 865  Line 987 
987                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
988                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
989    
                 pEnc->current->seconds = pEnc->mbParam.m_seconds;  
                 pEnc->current->ticks = pEnc->mbParam.m_ticks;  
   
990                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
991    
992  #ifdef _DEBUG_PSNR                  if (pFrame->general & XVID_EXTRASTATS)
993                  image_copy(&pEnc->sOriginal, &pEnc->current->image,                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
994                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
995  #endif                  }
996    
997                  emms();                  emms();
998    
999                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1000                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1001                                  "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);                                  "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
1002                  }                  }
1003    
1004          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 909  Line 1028 
1028    
1029                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1030                                  }                                  }
   
1031  #undef OFFSET  #undef OFFSET
1032                          }                          }
   
1033                          xvid_free(temp_dquants);                          xvid_free(temp_dquants);
1034                  }                  }
   
1035          }          }
1036    
1037          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
1039           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1040            pEnc->iFrameNum++;
1041    
1042            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1043                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1044                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) {
1045                    mode = I_VOP;
1046            } else {
1047                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1048                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1049                                            (/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0,
1050                                            bframes_count++, pFrame->bframe_threshold);
1051            }
1052    
1053          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (mode == I_VOP) {
                 (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&  
                  pEnc->iFrameNum >= pEnc->iMaxKeyInterval)  
                 || image_mad(&pEnc->reference->image, &pEnc->current->image,  
                                          pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                          pEnc->mbParam.height) > 30) {  
1054                  /*                  /*
1055                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
1056                   */                   */
1057                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1058                            pEnc->mbParam.m_quarterpel = 1;
1059                    else
1060                            pEnc->mbParam.m_quarterpel = 0;
1061    
1062                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1063    
1064                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1065                            if (pFrame->quant_intra_matrix != NULL)
1066                                    set_intra_matrix(pFrame->quant_intra_matrix);
1067                            if (pFrame->quant_inter_matrix != NULL)
1068                                    set_inter_matrix(pFrame->quant_inter_matrix);
1069                    }
1070    
1071    
1072                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1073                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1074                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1075    
1076                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1077                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1078                  }                  }
1079    
1080                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe                  /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */
1081    
1082                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1083    
1084                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
1085                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1086    
1087                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1088                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1089                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1090                          }                          }
                         FrameCodeP(pEnc, &bs, &bits, 1, 0);  
1091    
1092                            if (pEnc->mbParam.m_quarterpel)
1093                                    pEnc->current->global_flags |= XVID_QUARTERPEL;
1094                            else
1095                                    pEnc->current->global_flags &= ~XVID_QUARTERPEL;
1096    
1097                            FrameCodeP(pEnc, &bs, &bits, 1, 0, interpolate_backward);
1098                            bframes_count = 0;
1099                          pFrame->intra = 0;                          pFrame->intra = 0;
1100                            interpolate_forward = 0;
1101                            interpolate_backward = 1;
1102    
1103                  } else {                  } else {
1104    
1105                            if (pEnc->mbParam.m_quarterpel)
1106                                    pEnc->current->global_flags |= XVID_QUARTERPEL;
1107                            else
1108                                    pEnc->current->global_flags &= ~XVID_QUARTERPEL;
1109    
1110                          FrameCodeI(pEnc, &bs, &bits);                          FrameCodeI(pEnc, &bs, &bits);
1111                            bframes_count = 0;
1112                            interpolate_forward = 1;
1113                            interpolate_backward = 1;
1114                          pFrame->intra = 1;                          pFrame->intra = 1;
1115    
1116                          pEnc->bframenum_dx50bvop = -1;                          pEnc->bframenum_dx50bvop = -1;
# Line 966  Line 1118 
1118    
1119                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1120    
1121                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1122                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1123                          input_valid = 0;                          input_valid = 0;
1124                          goto ipvop_loop;                          goto ipvop_loop;
1125                  }                  }
# Line 976  Line 1128 
1128                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1129                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1130                   */                   */
1131          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1132                  /*                  /*
1133                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1134                   */                   */
# Line 985  Line 1137 
1137                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1138                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1139    
1140                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1141                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1142                  }                  }
1143    
1144                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  if (pEnc->mbParam.m_quarterpel)
1145                            pEnc->current->global_flags |= XVID_QUARTERPEL;
1146                    else
1147                            pEnc->current->global_flags &= ~XVID_QUARTERPEL;
1148    
1149                    FrameCodeP(pEnc, &bs, &bits, 1, 0, interpolate_backward);
1150                    bframes_count = 0;
1151                  pFrame->intra = 0;                  pFrame->intra = 0;
1152                    interpolate_forward = 0;
1153                    interpolate_backward = 1;
1154                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1155    
1156                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1157                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1158                          input_valid = 0;                          input_valid = 0;
1159                          goto ipvop_loop;                          goto ipvop_loop;
1160                  }                  }
1161    
1162          } else {          } else {        /* mode == B_VOP */
1163                  /*                  /*
1164                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1165                   */                   */
1166    
1167                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
   
                 if ((pEnc->global & XVID_GLOBAL_DEBUG)) {  
1168                          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");
1169                  }                  }
1170    
1171                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1172                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1173                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1174                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1175                  } else {                  } else {
1176                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1177                  }                  }
1178    
1179                    if (pEnc->current->quant < 1)
1180                            pEnc->current->quant = 1;
1181                    else if (pEnc->current->quant > 31)
1182                            pEnc->current->quant = 31;
1183    
1184                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1185                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1186                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1187    
1188                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1189                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1190                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1191    
1192                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1193    
1194                  pFrame->intra = 0;                  /* bframe report by koepi */
1195                    pFrame->intra = 2;
1196                  pFrame->length = 0;                  pFrame->length = 0;
1197    
1198                  input_valid = 0;                  input_valid = 0;
1199                  goto bvop_loop;                  goto bvop_loop;
1200          }          }
1201    
1202          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1203          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1204    
1205          if (pResult) {          if (pResult) {
1206                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1207                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1208                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1209                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1210                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1211          }                  pResult->iblks = pEnc->current->sStat.iblks;
1212                    pResult->qblks = pEnc->current->sStat.qblks;
1213          emms();  
1214                    if (pFrame->general & XVID_EXTRASTATS)
1215  #ifdef _DEBUG_PSNR                  {       pResult->sse_y =
1216          psnr =                                  plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
                 image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
1217                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1218                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1219    
1220          snprintf(temp, 127, "PSNR: %f\n", psnr);                          pResult->sse_u =
1221          DEBUG(temp);                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1222  #endif                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1223                                                       pEnc->mbParam.height/2);
1224    
1225                            pResult->sse_v =
1226                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1227                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1228                                                       pEnc->mbParam.height/2);
1229                    }
1230            }
1231    
1232            emms();
1233    
1234          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1235                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1236                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1237          }          }
1238    
   
1239          stop_global_timer();          stop_global_timer();
1240          write_timer();          write_timer();
1241    
1242            emms();
1243          return XVID_ERR_OK;          return XVID_ERR_OK;
1244  }  }
1245    
 #endif  
   
1246    
1247    
1248  /*****************************************************************************  /*****************************************************************************
# Line 1090  Line 1263 
1263          Bitstream bs;          Bitstream bs;
1264          uint32_t bits;          uint32_t bits;
1265          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1266            unsigned int old_qpel;
1267    
 #ifdef _DEBUG_PSNR  
1268          float psnr;          float psnr;
1269          uint8_t temp[128];          char temp[128];
 #endif  
1270    
1271          start_global_timer();          start_global_timer();
1272    
# Line 1103  Line 1275 
1275          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1276          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1277    
1278          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1279    
1280          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1281          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
 #ifdef BFRAMES  
         pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
 #endif  
1282          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1283    
1284            inc_frame_num(pEnc);
1285    
1286            /* disable alternate scan flag if interlacing is not enabled */
1287            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1288                    !(pEnc->current->global_flags & XVID_INTERLACING))
1289            {
1290                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1291            }
1292    
1293          start_timer();          start_timer();
1294          if (image_input          if (image_input
1295                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1296                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1297                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1298          stop_conv_timer();          stop_conv_timer();
1299    
1300  #ifdef _DEBUG_PSNR          if ((pFrame->general & XVID_CHROMAOPT)) {
1301          image_copy(&pEnc->sOriginal, &pEnc->current->image,                  image_chroma_optimize(&pEnc->current->image,
1302                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1303            }
1304    
1305            if (pFrame->general & XVID_EXTRASTATS)
1306            {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1307                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1308  #endif          }
1309    
1310          emms();          emms();
1311    
# Line 1135  Line 1317 
1317                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1318          }          }
1319    
1320            old_qpel = pEnc->mbParam.m_quarterpel;
1321    
1322            if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1323                    pEnc->mbParam.m_quarterpel = 1;
1324            else
1325                    pEnc->mbParam.m_quarterpel = 0;
1326    
1327          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1328                  int *temp_dquants =                  int *temp_dquants =
1329                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 1195  Line 1384 
1384          }          }
1385    
1386          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1387                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == -1)
1388                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1389                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1390                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1391                  } else {                  } else {
1392                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          if (old_qpel != pEnc->mbParam.m_quarterpel)
1393                                    pEnc->mbParam.m_quarterpel = old_qpel;
1394                            pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header, 1);
1395                  }                  }
1396          } else {          } else {
1397                  if (pFrame->intra == 1) {                  if (pFrame->intra == 1) {
1398                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1399                  } else {                  } else {
1400                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          if (old_qpel != pEnc->mbParam.m_quarterpel)
1401                                    pEnc->mbParam.m_quarterpel = old_qpel;
1402                            pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header, 1);
1403                  }                  }
1404    
1405          }          }
1406    
1407            /* Relic from OpenDivX - now disabled
1408          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1409          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1410          BitstreamPad(&bs);          */
1411    
1412            BitstreamPadAlways(&bs);
1413          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1414    
1415          if (pResult) {          if (pResult) {
1416                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1417                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1418                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1419                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1420                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1421                    pResult->iblks = pEnc->current->sStat.iblks;
1422                    pResult->qblks = pEnc->current->sStat.qblks;
1423          }          }
1424    
1425          emms();          emms();
# Line 1230  Line 1428 
1428                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1429                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1430          }          }
1431  #ifdef _DEBUG_PSNR          if (pFrame->general & XVID_EXTRASTATS)
1432            {
1433          psnr =          psnr =
1434                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1435                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1436                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1437    
1438          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1439          DEBUG(temp);          }
 #endif  
1440    
 #ifdef BFRAMES  
         inc_frame_num(pEnc);  
 #else  
1441          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1442    
1443          stop_global_timer();          stop_global_timer();
1444          write_timer();          write_timer();
# Line 1308  Line 1501 
1501                  return;                  return;
1502          }          }
1503    
1504          pEnc->current->fcode =          pEnc->current->fcode = (hint->rawhints) ?
1505                  (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,                  (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
                                                                                                                                  FCODEBITS);  
1506    
1507          length = pEnc->current->fcode + 5;          length = pEnc->current->fcode + 5;
1508          high = 1 << (length - 1);          high = 1 << (length - 1);
# Line 1325  Line 1517 
1517                          VECTOR tmp;                          VECTOR tmp;
1518                          int vec;                          int vec;
1519    
1520                          pMB->mode =                          pMB->mode =     (hint->rawhints) ?
1521                                  (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,                                  (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
                                                                                                                                   MODEBITS);  
1522    
1523                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1524                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1525    
1526                          if (pMB->mode == MODE_INTER) {                          if (pMB->mode == MODE_INTER) {
1527                                  tmp.x =                                  tmp.x = (hint->rawhints) ?
1528                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,                                          bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
1529                                                                                                                                                    length);                                  tmp.y = (hint->rawhints) ?
1530                                  tmp.y =                                          bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
                                         (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,  
                                                                                                                                                   length);  
1531                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1532                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1533    
# Line 1352  Line 1541 
1541                                  }                                  }
1542                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1543                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1544                                          tmp.x =                                          tmp.x = (hint->rawhints) ?
1545                                                  (hint->rawhints) ? bhint->mvs[vec].                                                  bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
1546                                                  x : BitstreamGetBits(&bs, length);                                          tmp.y = (hint->rawhints) ?
1547                                          tmp.y =                                                  bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
                                                 (hint->rawhints) ? bhint->mvs[vec].  
                                                 y : BitstreamGetBits(&bs, length);  
1548                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1549                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1550    
# Line 1368  Line 1555 
1555                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1556                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1557                                  }                                  }
1558                          } else                          // intra / stuffing / not_coded                          } else                          /* intra / stuffing / not_coded */
1559                          {                          {
1560                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1561                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
# Line 1409  Line 1596 
1596    
1597          if (intra) {          if (intra) {
1598                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1599                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1600                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1601                  }                  }
1602                  return;                  return;
# Line 1484  Line 1671 
1671                     Bitstream * bs,                     Bitstream * bs,
1672                     uint32_t * pBits)                     uint32_t * pBits)
1673  {  {
1674            int mb_width = pEnc->mbParam.mb_width;
1675            int mb_height = pEnc->mbParam.mb_height;
1676    
1677          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1678          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1679    
1680          uint16_t x, y;          uint16_t x, y;
1681    
1682            if ((pEnc->current->global_flags & XVID_REDUCED))
1683            {
1684                    mb_width = (pEnc->mbParam.width + 31) / 32;
1685                    mb_height = (pEnc->mbParam.height + 31) / 32;
1686    
1687                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1688                    /* XXX: setedges is overkill */
1689                    start_timer();
1690                    image_setedges(&pEnc->current->image,
1691                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1692                            pEnc->mbParam.width, pEnc->mbParam.height);
1693                    stop_edges_timer();
1694            }
1695          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1696          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1697          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1698            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1699          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1700    
1701          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1702  #ifdef BFRAMES  
1703  #define DIVX501B481P "DivX501b481p"          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1704          if ((pEnc->global & XVID_GLOBAL_PACKED)) {  
1705                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));          BitstreamPadAlways(bs);
         }  
 #endif  
1706          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1707    
1708          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1709    
1710          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1711          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1712          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1713            pEnc->current->sStat.iblks = pEnc->current->sStat.qblks = 0;
1714    
1715          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1716                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1717                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1718                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1719    
# Line 1525  Line 1727 
1727                          stop_prediction_timer();                          stop_prediction_timer();
1728    
1729                          start_timer();                          start_timer();
1730                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1731                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1732                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1733                                    qcoeff[5*64+0]=0;
1734                            }
1735                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1736                          stop_coding_timer();                          stop_coding_timer();
1737                  }                  }
1738    
1739            if ((pEnc->current->global_flags & XVID_REDUCED))
1740            {
1741                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1742                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1743                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1744            }
1745          emms();          emms();
1746    
1747          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1748          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1749          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1750    
1751          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1752                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1753          }          }
1754    
1755          return 1;                                       // intra          return 1;                                       /* intra */
1756  }  }
1757    
1758    
1759  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1760    #define BFRAME_SKIP_THRESHHOLD 30
1761    
1762    
1763    /* FrameCodeP also handles S(GMC)-VOPs */
1764  static int  static int
1765  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1766                     Bitstream * bs,                     Bitstream * bs,
1767                     uint32_t * pBits,                     uint32_t * pBits,
1768                     bool force_inter,                     bool force_inter,
1769                     bool vol_header)                     bool vol_header,
1770                       int interpolate)
1771  {  {
1772          float fSigma;          float fSigma;
1773    
1774          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1775          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1776    
1777            int mb_width = pEnc->mbParam.mb_width;
1778            int mb_height = pEnc->mbParam.mb_height;
1779    
1780          int iLimit;          int iLimit;
1781          uint32_t x, y;          int x, y, k;
1782          int iSearchRange;          int iSearchRange;
1783          int bIntra;          int bIntra, skip_possible;
1784            IMAGE *refh, *refv, *refhv;
1785    
1786          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1787          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1788    
1789          start_timer();          if ((pEnc->current->global_flags & XVID_REDUCED))
1790          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          {
1791                                     pEnc->mbParam.width, pEnc->mbParam.height,                  mb_width = (pEnc->mbParam.width + 31) / 32;
1792                                     pEnc->current->global_flags & XVID_INTERLACING);                  mb_height = (pEnc->mbParam.height + 31) / 32;
1793          stop_edges_timer();          }
1794    
1795          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1796          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1797            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1798          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1799    
1800          if (!force_inter)          if (!force_inter)
1801                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1802          else          else
1803                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1804    
1805            if((interpolate) || (pEnc->current->rounding_type != 0)) {
1806                    start_timer();
1807                    image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1808                                               pEnc->mbParam.width, pEnc->mbParam.height);
1809                    stop_edges_timer();
1810    
1811          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1812                  start_timer();                  start_timer();
1813                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1814                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1815                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1816                                                              pEnc->mbParam.m_quarterpel,
1817                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1818                  stop_inter_timer();                  stop_inter_timer();
1819          }          }
1820    
1821                    refh = &pEnc->vInterH;
1822                    refv = &pEnc->vInterV;
1823                    refhv = &pEnc->vInterHV;
1824            }
1825            else {
1826                    refh = &pEnc->f_refh;
1827                    refv = &pEnc->f_refv;
1828                    refhv = &pEnc->f_refhv;
1829            }
1830    
1831            pEnc->current->coding_type = P_VOP;
1832    
1833          start_timer();          start_timer();
1834          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1835                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
         } else {  
   
 #ifdef _SMP  
         if (pEnc->mbParam.num_threads > 1)  
                 bIntra =  
                         SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                                                  &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                                  iLimit);  
1836          else          else
 #endif  
1837                  bIntra =                  bIntra =
1838                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1839                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                   refh, refv, refhv,
1840                           iLimit);                           iLimit);
1841    
         }  
1842          stop_motion_timer();          stop_motion_timer();
1843    
1844          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1845    
1846          pEnc->current->coding_type = P_VOP;          if ( ( pEnc->current->global_flags & XVID_GMC )
1847                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1848            {
1849                    pEnc->current->coding_type = S_VOP;
1850    
1851                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1852                                            pEnc->mbParam.width, pEnc->mbParam.height,
1853                                            &pEnc->current->gmc_data);
1854    
1855                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1856                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1857                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1858                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1859                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1860    
1861            }
1862    
1863            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1864          if (vol_header)          if (vol_header)
1865                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1866                    BitstreamPadAlways(bs);
1867            }
1868    
1869          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1870    
1871          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1872    
1873          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1874          pEnc->sStat.iMvSum = 0;          pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1875          pEnc->sStat.iMvCount = 0;          pEnc->current->sStat.iblks = pEnc->current->sStat.qblks = 0;
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1876    
1877          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1878                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1879                    for (x = 0; x < mb_width; x++) {
1880                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1881                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1882    
1883    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1884    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1885    
1886                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1887    
1888                          if (!bIntra) {                          if (bIntra) {
1889                                    CodeIntraMB(pEnc, pMB);
1890                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1891                                                                      dct_codes, qcoeff);
1892    
1893                                    start_timer();
1894                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1895                                    stop_prediction_timer();
1896    
1897                                    pEnc->current->sStat.kblks++;
1898    
1899                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1900                                    stop_coding_timer();
1901                                    continue;
1902                            }
1903    
1904                            if (pEnc->current->coding_type == S_VOP) {
1905    
1906                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1907                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1908                                            pEnc->mbParam.edged_width, 65536);
1909    
1910                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1911                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1912                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1913    
1914                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1915                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1916                                    }
1917    
1918                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1919    
1920                                            if (pEnc->mbParam.m_quarterpel)
1921                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1922                                            else
1923                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1924    
1925                                            pMB->mode = MODE_INTER;
1926                                            pMB->mcsel = 1;
1927                                            pMB->sad16 = iSAD;
1928                                    } else {
1929                                            pMB->mcsel = 0;
1930                                    }
1931                            } else {
1932                                    pMB->mcsel = 0; /* just a precaution */
1933                            }
1934    
1935                                  start_timer();                                  start_timer();
1936                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1937                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                   refh, refv,
1938                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   refhv, &pEnc->vGMC,
1939                                                                     &pEnc->current->image,
1940                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1941                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1942                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1943                                                                     pEnc->mbParam.m_quarterpel,
1944                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1945                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1946    
1947                                  stop_comp_timer();                                  stop_comp_timer();
1948    
1949                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1664  Line 1960 
1960    
1961                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1962    
1963                                  pMB->cbp =                          if (pMB->mode != MODE_NOT_CODED)
1964                            {       pMB->cbp =
1965                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1966                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1967                          }                          }
1968    
1969                          start_timer();                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
   
                         if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {  
                                 pEnc->sStat.kblks++;  
                         } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||  
1970                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1971                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1972                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1973                          } else {                          } else {
1974                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1975                            }
1976    
1977                            if(pEnc->mbParam.m_quarterpel) {
1978                                    for (k = 0; k < ((pMB->mode == MODE_INTER4V) ? 4 : 1); k++) {
1979                                            if (((pMB->qmvs[k].x % 4) != 0) || ((pMB->qmvs[k].y % 4) != 0))
1980                                                    pEnc->current->sStat.iblks++;
1981    
1982                                            if (((pMB->qmvs[k].x % 4) & 1) || ((pMB->qmvs[k].y % 4) & 1))
1983                                                    pEnc->current->sStat.qblks++;
1984                                    }
1985                          }                          }
1986    
1987                          start_timer();                          start_timer();
1988                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1989                            /* Finished processing the MB, now check if to CODE or SKIP */
1990    
1991                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1992                                                            (pMB->dquant == NO_CHANGE);
1993    
1994                            if (pEnc->current->coding_type == S_VOP)
1995                                    skip_possible &= (pMB->mcsel == 1);
1996                            else if (pEnc->current->coding_type == P_VOP) {
1997                                    if (pEnc->mbParam.m_quarterpel)
1998                                            skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1999                                    else
2000                                            skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
2001                            }
2002    
2003                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
2004    
2005    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
2006    
2007                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
2008                                    {
2009                                            int bSkip = 1;
2010    
2011                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
2012                                            {
2013                                                    int iSAD;
2014                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
2015                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
2016                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
2017                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
2018                                                    {       bSkip = 0;
2019                                                            break;
2020                                                    }
2021                                            }
2022    
2023                                            if (!bSkip) {   /* no SKIP, but trivial block */
2024                                                    if(pEnc->mbParam.m_quarterpel) {
2025                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2026                                                            pMB->pmvs[0].x = - predMV.x;
2027                                                            pMB->pmvs[0].y = - predMV.y;
2028                                                    }
2029                                                    else {
2030                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2031                                                            pMB->pmvs[0].x = - predMV.x;
2032                                                            pMB->pmvs[0].y = - predMV.y;
2033                                                    }
2034                                                    pMB->mode = MODE_INTER;
2035                                                    pMB->cbp = 0;
2036                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2037                                                    stop_coding_timer();
2038    
2039                                                    continue;       /* next MB */
2040                                            }
2041                                    }
2042                                    /* do SKIP */
2043    
2044                                    pMB->mode = MODE_NOT_CODED;
2045                                    MBSkip(bs);
2046                                    stop_coding_timer();
2047                                    continue;       /* next MB */
2048                            }
2049                            /* ordinary case: normal coded INTER/INTER4V block */
2050    
2051                            if (pEnc->current->global_flags & XVID_GREYSCALE)
2052                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
2053                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
2054                                    qcoeff[5*64+0]=0;
2055                            }
2056    
2057                            if(pEnc->mbParam.m_quarterpel) {
2058                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2059                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
2060                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
2061                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
2062                            } else {
2063                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2064                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
2065                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
2066                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
2067                            }
2068    
2069    
2070                            if (pMB->mode == MODE_INTER4V)
2071                            {       int k;
2072                                    for (k=1;k<4;k++)
2073                                    {
2074                                            if(pEnc->mbParam.m_quarterpel) {
2075                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
2076                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
2077                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
2078                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
2079                                            } else {
2080                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
2081                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
2082                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
2083                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
2084                                            }
2085    
2086                                    }
2087                            }
2088    
2089                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2090                          stop_coding_timer();                          stop_coding_timer();
2091    
2092                    }
2093                  }                  }
2094    
2095            if ((pEnc->current->global_flags & XVID_REDUCED))
2096            {
2097                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2098                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2099                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2100          }          }
2101    
2102          emms();          emms();
# Line 1699  Line 2105 
2105                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2106          }          }
2107    
2108          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2109                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2110    
2111          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2112    
2113          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2114    
2115          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2116                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */
2117          {          {
2118                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2119                  iSearchRange *= 2;                  iSearchRange *= 2;
2120          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2121                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2122                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2123                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16                             && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel)))      /* minimum search range 16 */
2124          {          {
2125                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2126                  iSearchRange /= 2;                  iSearchRange /= 2;
2127          }          }
2128    
2129          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2130    
2131          *pBits = BitstreamPos(bs) - *pBits;          /* frame drop code */
2132            DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
2133            if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2134                    (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2135            {
2136                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2137                    pEnc->current->sStat.ublks = mb_width * mb_height;
2138    
2139                    BitstreamReset(bs);
2140    
2141                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2142                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2143    
2144                    /* copy reference frame details into the current frame */
2145                    pEnc->current->quant = pEnc->reference->quant;
2146                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
2147                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
2148                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
2149                    pEnc->current->fcode = pEnc->reference->fcode;
2150                    pEnc->current->bcode = pEnc->reference->bcode;
2151                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
2152                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2153            }
2154    
2155          pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase;          /* XXX: debug
2156          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);          {
2157                    char s[100];
2158                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2159                    image_dump_yuvpgm(&pEnc->current->image,
2160                            pEnc->mbParam.edged_width,
2161                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2162    
2163          pEnc->last_pframe = pEnc->mbParam.m_ticks;                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2164          return 0;                                       // inter                  image_dump_yuvpgm(&pEnc->reference->image,
2165                            pEnc->mbParam.edged_width,
2166                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2167            }
2168            */
2169    
2170    
2171            *pBits = BitstreamPos(bs) - *pBits;
2172    
2173            return 0;                                       /* inter */
2174  }  }
2175    
2176    
 #ifdef BFRAMES  
2177  static void  static void
2178  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2179                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2180                     Bitstream * bs,                     Bitstream * bs,
2181                     uint32_t * pBits)                     uint32_t * pBits,
2182                       int interpolate_forward,
2183                       int interpolate_backward)
2184  {  {
2185          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2186          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2187          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
2188    
2189          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2190          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
# Line 1755  Line 2196 
2196                  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); \
2197          }          }
2198    
2199            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2200    
2201          if (!first){          if (!first){
2202                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2203          }          }
2204  #endif  #endif
2205    
2206          // forward          frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2207    
2208            if ((interpolate_forward) || ((pEnc->mbParam.m_rounding_type != 1) && interpolate_backward)) {
2209    
2210                    /* forward  */
2211          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2212                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2213                                     pEnc->mbParam.height,                                             pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2214          start_timer();          start_timer();
2215          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  image_interpolate(f_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2216                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2217                                            0);                                                    pEnc->mbParam.m_quarterpel, 0);
2218          stop_inter_timer();          stop_inter_timer();
2219            }
2220    
2221            if (interpolate_backward) {
2222    
2223          // backward                  /* backward */
2224          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2225                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2226                                     pEnc->mbParam.height,                                             pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2227          start_timer();          start_timer();
2228          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2229                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2230                                            0);                                                    pEnc->mbParam.m_quarterpel, 0);
2231          stop_inter_timer();          stop_inter_timer();
2232            }
2233    
2234          start_timer();          start_timer();
         fprintf(stderr,"m_ticks =%d\n",(int32_t)pEnc->mbParam.m_ticks+1);  
         MotionEstimationBVOP(&pEnc->mbParam, frame,  
           ((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase,  
                                                 pEnc->time_pp,  
                                                 pEnc->reference->mbs, f_ref,  
                                                  &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,  
                                                  pEnc->current->mbs, b_ref, &pEnc->vInterH,  
                                                  &pEnc->vInterV, &pEnc->vInterHV);  
2235    
2236            MotionEstimationBVOP(&pEnc->mbParam, frame,
2237                                                     ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2238                                                     ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2239                                                     pEnc->reference->mbs, f_ref, &pEnc->vInterH,
2240                                                     &pEnc->vInterV, &pEnc->vInterHV,
2241                                                     pEnc->current, b_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv);
2242    
2243          stop_motion_timer();          stop_motion_timer();
2244            /*
2245          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
            {  
2246             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
2247             } */          }
2248            */
2249    
2250          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2251    
2252            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2253          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
2254    
2255          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2256    
2257          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2258          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2259          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2260          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2261            frame->sStat.iblks = frame->sStat.qblks = 0;
2262    
2263    
2264          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;  
   
2265                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2266                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2267                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
                         MACROBLOCK *b_mb =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
2268    
2269                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2270                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2271                                  mb->mvs[0].x = 0;                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */
                                 mb->mvs[0].y = 0;  
   
                                 mb->cbp = 0;  
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
2272                                  continue;                                  continue;
2273                          }                          }
2274    
2275                            if (mb->mode != MODE_DIRECT_NONE_MV) {
2276                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2277                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->vInterH, &pEnc->vInterV,
2278                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->vInterHV, b_ref, &pEnc->f_refh,
2279                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refv, &pEnc->f_refhv,
2280                                                                           dct_codes);                                                                           dct_codes);
2281    
2282                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2283                          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);  
   
2284    
2285                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  mb->cbp =
2286                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mode = MODE_DIRECT_NONE_MV; // skipped  
                         }  
2287    
2288                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2289                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2290                                  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;  
2291                          }                          }
   
                         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;  
2292                          }                          }
 //                      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);  
2293    
2294  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2295          BFRAME_DEBUG          BFRAME_DEBUG
2296  #endif  #endif
2297                          start_timer();                          start_timer();
2298                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2299                                                   &pEnc->sStat);                                                   &frame->sStat, direction);
2300                          stop_coding_timer();                          stop_coding_timer();
2301                  }                  }
2302          }          }
2303    
2304          emms();          emms();
2305    
2306          // TODO: dynamic fcode/bcode ???          /* TODO: dynamic fcode/bcode ??? */
2307    
2308          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2309    
# Line 1895  Line 2315 
2315          }          }
2316  #endif  #endif
2317  }  }
 #endif  

Legend:
Removed from v.1.57  
changed lines
  Added in v.1.99.2.1

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