[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.58, Fri Jul 19 15:02:38 2002 UTC revision 1.96, Tue Mar 4 10:55:21 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 92  Line 75 
75                                            bool force_inter,                                            bool force_inter,
76                                            bool vol_header);                                            bool vol_header);
77    
 #ifdef BFRAMES  
78  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
79                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
80                                             Bitstream * bs,                                             Bitstream * bs,
81                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
82    
83  /*****************************************************************************  /*****************************************************************************
84   * Local data   * Local data
# Line 112  Line 93 
93  };  };
94    
95    
 static void __inline  
 image_null(IMAGE * image)  
 {  
         image->y = image->u = image->v = NULL;  
 }  
   
   
96  /*****************************************************************************  /*****************************************************************************
97   * Encoder creation   * Encoder creation
98   *   *
# Line 142  Line 116 
116  {  {
117          Encoder *pEnc;          Encoder *pEnc;
118          int i;          int i;
   
119          pParam->handle = NULL;          pParam->handle = NULL;
120    
121          ENC_CHECK(pParam);          ENC_CHECK(pParam);
# Line 209  Line 182 
182    
183          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
184    
185          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
186                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
187    
188          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
# Line 236  Line 209 
209    
210          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
211    
212  #ifdef _SMP          pEnc->fMvPrevSigma = -1;
         pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);  
 #endif  
   
         pEnc->sStat.fMvPrevSigma = -1;  
213    
214          /* Fill rate control parameters */          /* Fill rate control parameters */
215    
216          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
217    
218          pEnc->iFrameNum = 0;          pEnc->iFrameNum = -1;
219          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
220    
221          /* try to allocate frame memory */          /* try to allocate frame memory */
222    
# Line 271  Line 240 
240    
241          /* try to allocate image memory */          /* try to allocate image memory */
242    
243  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
244          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
245  #endif  
 #ifdef BFRAMES  
246          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
247          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
248          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
249  #endif  
250          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
251          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
252          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 287  Line 255 
255          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
256          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
257    
258  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
259          if (image_create          {       if (image_create
260                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
261                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
262                  goto xvid_err_memory3;                  goto xvid_err_memory3;
263  #endif          }
264  #ifdef BFRAMES  
265          if (image_create          if (image_create
266                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
267                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 306  Line 274 
274                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
275                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
276                  goto xvid_err_memory3;                  goto xvid_err_memory3;
277  #endif  
278          if (image_create          if (image_create
279                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
280                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 336  Line 304 
304                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
305                  goto xvid_err_memory3;                  goto xvid_err_memory3;
306    
307    /* Create full bitplane for GMC, this might be wasteful */
308            if (image_create
309                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
310                     pEnc->mbParam.edged_height) < 0)
311                    goto xvid_err_memory3;
312    
313    
314    
315          /* B Frames specific init */          pEnc->mbParam.global = pParam->global;
 #ifdef BFRAMES  
316    
317          pEnc->global = pParam->global;          /* B Frames specific init */
318          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
319          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
320            pEnc->mbParam.bquant_offset = pParam->bquant_offset;
321            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
322          pEnc->bframes = NULL;          pEnc->bframes = NULL;
323    
324          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 390  Line 365 
365    
366          pEnc->queue = NULL;          pEnc->queue = NULL;
367    
   
368          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
369                  int n;                  int n;
370    
# Line 417  Line 391 
391          pEnc->queue_tail = 0;          pEnc->queue_tail = 0;
392          pEnc->queue_size = 0;          pEnc->queue_size = 0;
393    
394            pEnc->mbParam.m_stamp = 0;
395    
         pEnc->mbParam.m_seconds = 0;  
         pEnc->mbParam.m_ticks = 0;  
396          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
397          pEnc->last_pframe = 1;          pEnc->current->stamp = 0;
398  #endif          pEnc->reference->stamp = 0;
399    
400          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
401    
# Line 441  Line 414 
414          /*          /*
415           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
416           */           */
 #ifdef BFRAMES  
   xvid_err_memory5:  
417    
418      xvid_err_memory5:
419    
420          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
421    
# Line 475  Line 447 
447                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
448          }          }
449    
 #endif  
   
450    xvid_err_memory3:    xvid_err_memory3:
451  #ifdef _DEBUG_PSNR  
452          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
453            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
454                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
455  #endif          }
456    
 #ifdef BFRAMES  
457          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
458                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
459          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
460                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
461          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
462                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
463    
464          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
465                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 507  Line 476 
476          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
477                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
478    
479    /* destroy GMC image */
480            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
481                                      pEnc->mbParam.edged_height);
482    
483    
484    xvid_err_memory2:    xvid_err_memory2:
485          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
486          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 535  Line 509 
509  int  int
510  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
511  {  {
 #ifdef BFRAMES  
512          int i;          int i;
 #endif  
513    
514          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
515    
516          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
517          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
518    
519                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 553  Line 524 
524                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
525          }          }
526    
   
527          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
528    
529                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 572  Line 542 
542                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
543    
544          }          }
 #endif  
545    
546          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
547    
# Line 590  Line 559 
559                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
560          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
561                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
562  #ifdef BFRAMES  
563          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
564                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
565          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
566                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
567          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
568                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
569  #endif  
570  #ifdef _DEBUG_PSNR          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
571          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
573  #endif          }
574    
575          /* Encoder structure */          /* Encoder structure */
576    
# Line 617  Line 586 
586  }  }
587    
588    
589  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
590  {  {
591          pEnc->iFrameNum++;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
592          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;  
593  }  }
 #endif  
594    
595    
596  #ifdef BFRAMES  static __inline void
597  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
598  {  {
599          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
600          {          {
# Line 646  Line 610 
610          start_timer();          start_timer();
611          if (image_input          if (image_input
612                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
613                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
614                  return;                  return;
615          stop_conv_timer();          stop_conv_timer();
616    
617            if ((pFrame->general & XVID_CHROMAOPT)) {
618                    image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
619                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
620            }
621    
622          pEnc->queue_size++;          pEnc->queue_size++;
623          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
624  }  }
625  #endif  
626    static __inline void
627    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
628    {
629    
630                    pCur->ticks = (int32_t)pCur->stamp % time_base;
631                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
632    
633                    /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */
634    
635    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
636                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
637                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
638                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
639                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
640                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
641    
642    */
643    }
644    
645    
646    
647    /* convert pFrame->intra to coding_type */
648    static int intra2coding_type(int intra)
649    {
650            if (intra < 0)  return -1;
651            if (intra == 1) return I_VOP;
652            if (intra == 2) return B_VOP;
653    
654            return P_VOP;
655    }
656    
657    
658    
 #ifdef BFRAMES  
659  /*****************************************************************************  /*****************************************************************************
660   * IPB frame encoder entry point   * IPB frame encoder entry point
661   *   *
# Line 674  Line 673 
673          uint16_t x, y;          uint16_t x, y;
674          Bitstream bs;          Bitstream bs;
675          uint32_t bits;          uint32_t bits;
676            int mode = -1; /* Just to shut up compiler warning */
677    
678          int input_valid = 1;          int input_valid = 1;
679            int bframes_count = 0;
 #ifdef _DEBUG_PSNR  
         float psnr;  
         char temp[128];  
 #endif  
680    
681          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
682          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
# Line 711  Line 707 
707                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
708    
709                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
710                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
711    
712                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
713    
714                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
715                            bframes_count = 0;
716    
717                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
718                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
719                          pFrame->intra = 0;                          pFrame->intra = 0;
720    
721    
722                            emms();
723    
724                            if (pResult) {
725                                    pResult->quant = pEnc->current->quant;
726                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
727                                    pResult->kblks = pEnc->current->sStat.kblks;
728                                    pResult->mblks = pEnc->current->sStat.mblks;
729                                    pResult->ublks = pEnc->current->sStat.ublks;
730                            }
731    
732                          return XVID_ERR_OK;                          return XVID_ERR_OK;
733                  }                  }
734    
# Line 732  Line 740 
740                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
741                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
742    
743                  BitstreamPad(&bs);                  BitstreamPadAlways(&bs);
744                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
745                  pFrame->intra = 0;                  pFrame->intra = 2;
746    
747                    if (pResult) {
748                            pResult->quant = pEnc->current->quant;
749                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
750                            pResult->kblks = pEnc->current->sStat.kblks;
751                            pResult->mblks = pEnc->current->sStat.mblks;
752                            pResult->ublks = pEnc->current->sStat.ublks;
753                    }
754    
755                  if (input_valid)                  if (input_valid)
756                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
757    
758                    emms();
759    
760                  return XVID_ERR_OK;                  return XVID_ERR_OK;
761          }          }
762    
763          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
764                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
765    
766                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  /* write an empty marker to the bitstream.
767                       for divx5 decoder compatibility, this marker must consist
768                       of a not-coded p-vop, with a time_base of zero, and time_increment
769                       indentical to the future-referece frame.
770                    */
771    
772                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
773                            int tmp;
774    
775                          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",
776                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
777                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
778    
779                            tmp = pEnc->current->seconds;
780                            pEnc->current->seconds = 0; /* force time_base = 0 */
781    
782                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
783                          BitstreamPad(&bs);                          pEnc->current->seconds = tmp;
                         BitstreamPutBits(&bs, 0x7f, 8);  
784    
785                            BitstreamPadAlways(&bs);
786                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
787                          pFrame->intra = 0;                          pFrame->intra = 4;
788    
789                            if (pResult) {
790                                    pResult->quant = pEnc->current->quant;
791                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
792                                    pResult->kblks = pEnc->current->sStat.kblks;
793                                    pResult->mblks = pEnc->current->sStat.mblks;
794                                    pResult->ublks = pEnc->current->sStat.ublks;
795                            }
796    
797                          if (input_valid)                          if (input_valid)
798                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
799    
800                            emms();
801    
802                          return XVID_ERR_OK;                          return XVID_ERR_OK;
803                  }                  }
804          }          }
# Line 771  Line 809 
809          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->bframenum_dx50bvop != -1)
810          {          {
811    
812                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
813                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
814    
815                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
816                          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");
817                  }                  }
818    
# Line 786  Line 824 
824    
825          } else if (input_valid) {          } else if (input_valid) {
826    
827                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
828    
829                  start_timer();                  start_timer();
830                  if (image_input                  if (image_input
831                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
832                          pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
833                    {
834                            emms();
835                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
836                    }
837                  stop_conv_timer();                  stop_conv_timer();
838    
839                  // queue input frame, and dequue next image                  if ((pFrame->general & XVID_CHROMAOPT)) {
840                            image_chroma_optimize(&pEnc->current->image,
841                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
842                    }
843    
844                    /* queue input frame, and dequue next image */
845                  if (pEnc->queue_size > 0)                  if (pEnc->queue_size > 0)
846                  {                  {
847                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
# Line 809  Line 855 
855    
856          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
857    
858                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
859    
860                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
861                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
862                  pEnc->queue_size--;                  pEnc->queue_size--;
863    
864          } else if (BitstreamPos(&bs) == 0) {          } else {
865    
866                    /* if nothing was encoded, write an 'ignore this frame' flag
867                       to the bitstream */
868    
869                    if (BitstreamPos(&bs) == 0) {
870    
871                  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",
872                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
873                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
874    
875                  pFrame->intra = 0;                          /* That disabled line of code was supposed to inform VirtualDub
876                             * that the frame was a dummy delay frame - now disabled (thx god :-)
877                             */
878                            //BitstreamPutBits(&bs, 0x7f, 8);
879                            pFrame->intra = 5;
880    
881                  BitstreamPutBits(&bs, 0x7f, 8);                          if (pResult) {
882                  BitstreamPad(&bs);                                  /*
883                  pFrame->length = BitstreamLength(&bs);                                   * We must decide what to put there because i know some apps
884                                     * are storing statistics about quantizers and just do
885                                     * stats[quant]++ or stats[quant-1]++
886                                     * transcode is one of these app with its 2pass module
887                                     */
888    
889                  return XVID_ERR_OK;                                  /*
890                                     * For now i prefer 31 than 0 that could lead to a segfault
891                                     * in transcode
892                                     */
893                                    pResult->quant = 31;
894    
895                                    pResult->hlength = 0;
896                                    pResult->kblks = 0;
897                                    pResult->mblks = 0;
898                                    pResult->ublks = 0;
899                            }
900          } else {          } else {
901    
902                            if (pResult) {
903                                    pResult->quant = pEnc->current->quant;
904                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
905                                    pResult->kblks = pEnc->current->sStat.kblks;
906                                    pResult->mblks = pEnc->current->sStat.mblks;
907                                    pResult->ublks = pEnc->current->sStat.ublks;
908                            }
909    
910                    }
911    
912                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
913    
914                    emms();
915    
916                  return XVID_ERR_OK;                  return XVID_ERR_OK;
917          }          }
918    
919          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
920    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
921          emms();          emms();
922    
923          // 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 */
924          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
925          {          {
926                    mode = intra2coding_type(pFrame->intra);
927                  if (pFrame->quant == 0)                  if (pFrame->quant == 0)
928                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
929                  else                  else
930                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
931    
932                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
933                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
934    
935                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
936                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
937    */
938                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
939                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
940    
# Line 865  Line 942 
942                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
943                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
944    
                 pEnc->current->seconds = pEnc->mbParam.m_seconds;  
                 pEnc->current->ticks = pEnc->mbParam.m_ticks;  
   
945                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
946    
947  #ifdef _DEBUG_PSNR                  if (pFrame->general & XVID_EXTRASTATS)
948                  image_copy(&pEnc->sOriginal, &pEnc->current->image,                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
949                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
950  #endif                  }
951    
952                  emms();                  emms();
953    
954                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
955                          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,
956                                  "%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);
957                  }                  }
958    
959          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 909  Line 983 
983    
984                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
985                                  }                                  }
   
986  #undef OFFSET  #undef OFFSET
987                          }                          }
   
988                          xvid_free(temp_dquants);                          xvid_free(temp_dquants);
989                  }                  }
   
990          }          }
991    
992          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
994           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
995            pEnc->iFrameNum++;
996    
997            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
998                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
999                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) {
1000                    mode = I_VOP;
1001            } else {
1002                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1003                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1004                                            (/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0,
1005                                            bframes_count++, pFrame->bframe_threshold);
1006            }
1007    
1008          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) {  
1009                  /*                  /*
1010                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
1011                   */                   */
1012                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1013                            pEnc->mbParam.m_quarterpel = 1;
1014                    else
1015                            pEnc->mbParam.m_quarterpel = 0;
1016    
1017                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1018    
1019                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1020                            if (pFrame->quant_intra_matrix != NULL)
1021                                    set_intra_matrix(pFrame->quant_intra_matrix);
1022                            if (pFrame->quant_inter_matrix != NULL)
1023                                    set_inter_matrix(pFrame->quant_inter_matrix);
1024                    }
1025    
1026    
1027                  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",
1028                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1029                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1030    
1031                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1032                          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");
1033                  }                  }
1034    
1035                  // 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 */
1036    
1037                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1038    
1039                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
1040                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1041    
1042                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1043                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1044                                  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");
1045                          }                          }
1046                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
1047                            bframes_count = 0;
1048                          pFrame->intra = 0;                          pFrame->intra = 0;
1049    
1050                  } else {                  } else {
1051    
1052                          FrameCodeI(pEnc, &bs, &bits);                          FrameCodeI(pEnc, &bs, &bits);
1053                            bframes_count = 0;
1054                          pFrame->intra = 1;                          pFrame->intra = 1;
1055    
1056                          pEnc->bframenum_dx50bvop = -1;                          pEnc->bframenum_dx50bvop = -1;
# Line 966  Line 1058 
1058    
1059                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1060    
1061                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1062                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1063                          input_valid = 0;                          input_valid = 0;
1064                          goto ipvop_loop;                          goto ipvop_loop;
1065                  }                  }
# Line 976  Line 1068 
1068                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1069                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1070                   */                   */
1071          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1072                  /*                  /*
1073                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1074                   */                   */
# Line 985  Line 1077 
1077                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1078                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1079    
1080                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1081                          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");
1082                  }                  }
1083    
1084                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1085                    bframes_count = 0;
1086                  pFrame->intra = 0;                  pFrame->intra = 0;
1087                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1088    
1089                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1090                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1091                          input_valid = 0;                          input_valid = 0;
1092                          goto ipvop_loop;                          goto ipvop_loop;
1093                  }                  }
1094    
1095          } else {          } else {        /* mode == B_VOP */
1096                  /*                  /*
1097                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1098                   */                   */
1099    
1100                  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)) {  
1101                          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");
1102                  }                  }
1103    
1104                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1105                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1106                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1107                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1108                  } else {                  } else {
1109                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1110                  }                  }
1111    
1112                    if (pEnc->current->quant < 1)
1113                            pEnc->current->quant = 1;
1114                    else if (pEnc->current->quant > 31)
1115                            pEnc->current->quant = 31;
1116    
1117                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1118                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1119                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1120    
1121                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1122                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1123                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1124    
1125                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1126    
1127                  pFrame->intra = 0;                  /* bframe report by koepi */
1128                    pFrame->intra = 2;
1129                  pFrame->length = 0;                  pFrame->length = 0;
1130    
1131                  input_valid = 0;                  input_valid = 0;
1132                  goto bvop_loop;                  goto bvop_loop;
1133          }          }
1134    
1135          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1136          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1137    
1138          if (pResult) {          if (pResult) {
1139                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1140                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1141                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1142                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1143                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1144          }  
1145                    if (pFrame->general & XVID_EXTRASTATS)
1146          emms();                  {       pResult->sse_y =
1147                                    plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
 #ifdef _DEBUG_PSNR  
         psnr =  
                 image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
1148                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1149                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1150    
1151          snprintf(temp, 127, "PSNR: %f\n", psnr);                          pResult->sse_u =
1152          DEBUG(temp);                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1153  #endif                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1154                                                       pEnc->mbParam.height/2);
1155    
1156                            pResult->sse_v =
1157                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1158                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1159                                                       pEnc->mbParam.height/2);
1160                    }
1161            }
1162    
1163            emms();
1164    
1165          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1166                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1167                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1168          }          }
1169    
   
1170          stop_global_timer();          stop_global_timer();
1171          write_timer();          write_timer();
1172    
1173            emms();
1174          return XVID_ERR_OK;          return XVID_ERR_OK;
1175  }  }
1176    
 #endif  
   
1177    
1178    
1179  /*****************************************************************************  /*****************************************************************************
# Line 1091  Line 1195 
1195          uint32_t bits;          uint32_t bits;
1196          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1197    
 #ifdef _DEBUG_PSNR  
1198          float psnr;          float psnr;
1199          uint8_t temp[128];          char temp[128];
 #endif  
1200    
1201          start_global_timer();          start_global_timer();
1202    
# Line 1103  Line 1205 
1205          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1206          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1207    
1208          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1209    
1210          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1211          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  
1212          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1213    
1214            inc_frame_num(pEnc);
1215    
1216            /* disable alternate scan flag if interlacing is not enabled */
1217            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1218                    !(pEnc->current->global_flags & XVID_INTERLACING))
1219            {
1220                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1221            }
1222    
1223          start_timer();          start_timer();
1224          if (image_input          if (image_input
1225                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1226                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1227                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1228          stop_conv_timer();          stop_conv_timer();
1229    
1230  #ifdef _DEBUG_PSNR          if ((pFrame->general & XVID_CHROMAOPT)) {
1231          image_copy(&pEnc->sOriginal, &pEnc->current->image,                  image_chroma_optimize(&pEnc->current->image,
1232                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1233            }
1234    
1235            if (pFrame->general & XVID_EXTRASTATS)
1236            {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1237                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1238  #endif          }
1239    
1240          emms();          emms();
1241    
# Line 1135  Line 1247 
1247                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1248          }          }
1249    
1250            if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1251                    pEnc->mbParam.m_quarterpel = 1;
1252            else
1253                    pEnc->mbParam.m_quarterpel = 0;
1254    
1255          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1256                  int *temp_dquants =                  int *temp_dquants =
1257                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 1195  Line 1312 
1312          }          }
1313    
1314          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1315                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == -1)
1316                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1317                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1318                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1319                  } else {                  } else {
1320                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 1211  Line 1328 
1328    
1329          }          }
1330    
1331            /* Relic from OpenDivX - now disabled
1332          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1333          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1334          BitstreamPad(&bs);          */
1335    
1336            BitstreamPadAlways(&bs);
1337          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1338    
1339          if (pResult) {          if (pResult) {
1340                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1341                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1342                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1343                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1344                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1345          }          }
1346    
1347          emms();          emms();
# Line 1230  Line 1350 
1350                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1351                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1352          }          }
1353  #ifdef _DEBUG_PSNR          if (pFrame->general & XVID_EXTRASTATS)
1354            {
1355          psnr =          psnr =
1356                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1357                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1358                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1359    
1360          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1361          DEBUG(temp);          }
 #endif  
1362    
 #ifdef BFRAMES  
         inc_frame_num(pEnc);  
 #else  
1363          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1364    
1365          stop_global_timer();          stop_global_timer();
1366          write_timer();          write_timer();
# Line 1308  Line 1423 
1423                  return;                  return;
1424          }          }
1425    
1426          pEnc->current->fcode =          pEnc->current->fcode = (hint->rawhints) ?
1427                  (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,                  (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
                                                                                                                                  FCODEBITS);  
1428    
1429          length = pEnc->current->fcode + 5;          length = pEnc->current->fcode + 5;
1430          high = 1 << (length - 1);          high = 1 << (length - 1);
# Line 1325  Line 1439 
1439                          VECTOR tmp;                          VECTOR tmp;
1440                          int vec;                          int vec;
1441    
1442                          pMB->mode =                          pMB->mode =     (hint->rawhints) ?
1443                                  (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,                                  (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
                                                                                                                                   MODEBITS);  
1444    
1445                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1446                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1447    
1448                          if (pMB->mode == MODE_INTER) {                          if (pMB->mode == MODE_INTER) {
1449                                  tmp.x =                                  tmp.x = (hint->rawhints) ?
1450                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,                                          bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
1451                                                                                                                                                    length);                                  tmp.y = (hint->rawhints) ?
1452                                  tmp.y =                                          bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
                                         (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,  
                                                                                                                                                   length);  
1453                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1454                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1455    
# Line 1352  Line 1463 
1463                                  }                                  }
1464                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1465                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1466                                          tmp.x =                                          tmp.x = (hint->rawhints) ?
1467                                                  (hint->rawhints) ? bhint->mvs[vec].                                                  bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
1468                                                  x : BitstreamGetBits(&bs, length);                                          tmp.y = (hint->rawhints) ?
1469                                          tmp.y =                                                  bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
                                                 (hint->rawhints) ? bhint->mvs[vec].  
                                                 y : BitstreamGetBits(&bs, length);  
1470                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1471                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1472    
# Line 1368  Line 1477 
1477                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1478                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1479                                  }                                  }
1480                          } else                          // intra / stuffing / not_coded                          } else                          /* intra / stuffing / not_coded */
1481                          {                          {
1482                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1483                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
# Line 1409  Line 1518 
1518    
1519          if (intra) {          if (intra) {
1520                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1521                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1522                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1523                  }                  }
1524                  return;                  return;
# Line 1484  Line 1593 
1593                     Bitstream * bs,                     Bitstream * bs,
1594                     uint32_t * pBits)                     uint32_t * pBits)
1595  {  {
1596            int mb_width = pEnc->mbParam.mb_width;
1597            int mb_height = pEnc->mbParam.mb_height;
1598    
1599          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1600          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1601    
1602          uint16_t x, y;          uint16_t x, y;
1603    
1604            if ((pEnc->current->global_flags & XVID_REDUCED))
1605            {
1606                    mb_width = (pEnc->mbParam.width + 31) / 32;
1607                    mb_height = (pEnc->mbParam.height + 31) / 32;
1608    
1609                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1610                    /* XXX: setedges is overkill */
1611                    start_timer();
1612                    image_setedges(&pEnc->current->image,
1613                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1614                            pEnc->mbParam.width, pEnc->mbParam.height);
1615                    stop_edges_timer();
1616            }
1617          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1618          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1619          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1620            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1621          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1622    
1623          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1624  #ifdef BFRAMES  
1625  #define DIVX501B481P "DivX501b481p"          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1626          if ((pEnc->global & XVID_GLOBAL_PACKED)) {  
1627                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));          BitstreamPadAlways(bs);
         }  
 #endif  
1628          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1629    
1630          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1631    
1632          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1633          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1634          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1635    
1636          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1637                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1638                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1639                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1640    
# Line 1525  Line 1648 
1648                          stop_prediction_timer();                          stop_prediction_timer();
1649    
1650                          start_timer();                          start_timer();
1651                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1652                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1653                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1654                                    qcoeff[5*64+0]=0;
1655                            }
1656                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1657                          stop_coding_timer();                          stop_coding_timer();
1658                  }                  }
1659    
1660            if ((pEnc->current->global_flags & XVID_REDUCED))
1661            {
1662                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1663                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1664                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1665            }
1666          emms();          emms();
1667    
1668          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1669          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1670          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1671    
1672          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1673                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1674          }          }
1675    
1676          return 1;                                       // intra          return 1;                                       /* intra */
1677  }  }
1678    
1679    
1680  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1681    #define BFRAME_SKIP_THRESHHOLD 30
1682    
1683    
1684    /* FrameCodeP also handles S(GMC)-VOPs */
1685  static int  static int
1686  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1687                     Bitstream * bs,                     Bitstream * bs,
# Line 1559  Line 1694 
1694          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1695          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1696    
1697            int mb_width = pEnc->mbParam.mb_width;
1698            int mb_height = pEnc->mbParam.mb_height;
1699    
1700          int iLimit;          int iLimit;
1701          uint32_t x, y;          int x, y, k;
1702          int iSearchRange;          int iSearchRange;
1703          int bIntra;          int bIntra, skip_possible;
1704    
1705          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1706          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1707    
1708            if ((pEnc->current->global_flags & XVID_REDUCED))
1709            {
1710                    mb_width = (pEnc->mbParam.width + 31) / 32;
1711                    mb_height = (pEnc->mbParam.height + 31) / 32;
1712            }
1713    
1714          start_timer();          start_timer();
1715          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1716                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1717          stop_edges_timer();          stop_edges_timer();
1718    
1719          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1720          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1721            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1722          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1723    
1724          if (!force_inter)          if (!force_inter)
1725                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1726          else          else
1727                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1728    
1729          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1730                  start_timer();                  start_timer();
1731                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1732                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1733                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1734                                                      pEnc->mbParam.m_quarterpel,
1735                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1736                  stop_inter_timer();                  stop_inter_timer();
1737          }          }
1738    
1739            pEnc->current->coding_type = P_VOP;
1740    
1741          start_timer();          start_timer();
1742          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1743                  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);  
1744          else          else
 #endif  
1745                  bIntra =                  bIntra =
1746                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1747                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1748                           iLimit);                           iLimit);
1749    
         }  
1750          stop_motion_timer();          stop_motion_timer();
1751    
1752          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1753    
1754          pEnc->current->coding_type = P_VOP;          if ( ( pEnc->current->global_flags & XVID_GMC )
1755                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1756            {
1757                    pEnc->current->coding_type = S_VOP;
1758    
1759                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1760                                            pEnc->mbParam.width, pEnc->mbParam.height,
1761                                            &pEnc->current->gmc_data);
1762    
1763                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1764                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1765                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1766                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1767                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1768    
1769            }
1770    
1771            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1772          if (vol_header)          if (vol_header)
1773                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1774                    BitstreamPadAlways(bs);
1775            }
1776    
1777          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1778    
1779          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1780    
1781          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1782          pEnc->sStat.iMvSum = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
         pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1783    
1784          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1785                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1786                    for (x = 0; x < mb_width; x++) {
1787                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1788                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1789    
1790    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1791    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1792    
1793                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1794    
1795                          if (!bIntra) {                          if (bIntra) {
1796                                    CodeIntraMB(pEnc, pMB);
1797                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1798                                                                      dct_codes, qcoeff);
1799    
1800                                    start_timer();
1801                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1802                                    stop_prediction_timer();
1803    
1804                                    pEnc->current->sStat.kblks++;
1805    
1806                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1807                                    stop_coding_timer();
1808                                    continue;
1809                            }
1810    
1811                            if (pEnc->current->coding_type == S_VOP) {
1812    
1813                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1814                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1815                                            pEnc->mbParam.edged_width, 65536);
1816    
1817                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1818                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1819                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1820    
1821                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1822                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1823                                    }
1824    
1825                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1826    
1827                                            if (pEnc->mbParam.m_quarterpel)
1828                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1829                                            else
1830                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1831    
1832                                            pMB->mode = MODE_INTER;
1833                                            pMB->mcsel = 1;
1834                                            pMB->sad16 = iSAD;
1835                                    } else {
1836                                            pMB->mcsel = 0;
1837                                    }
1838                            } else {
1839                                    pMB->mcsel = 0; /* just a precaution */
1840                            }
1841    
1842                                  start_timer();                                  start_timer();
1843                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1844                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1845                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1846                                                                     &pEnc->current->image,
1847                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1848                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1849                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1850                                                                     pEnc->mbParam.m_quarterpel,
1851                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1852                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1853    
1854                                  stop_comp_timer();                                  stop_comp_timer();
1855    
1856                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1664  Line 1867 
1867    
1868                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1869    
1870                                  pMB->cbp =                          if (pMB->mode != MODE_NOT_CODED)
1871                            {       pMB->cbp =
1872                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1873                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1874                          }                          }
1875    
1876                          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 ||  
1877                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1878                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1879                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1880                          } else {                          } else {
1881                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1882                          }                          }
1883    
1884                          start_timer();                          start_timer();
1885                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1886                            /* Finished processing the MB, now check if to CODE or SKIP */
1887    
1888                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1889                                                            (pMB->dquant == NO_CHANGE);
1890    
1891                            if (pEnc->current->coding_type == S_VOP)
1892                                    skip_possible &= (pMB->mcsel == 1);
1893                            else if (pEnc->current->coding_type == P_VOP) {
1894                                    if (pEnc->mbParam.m_quarterpel)
1895                                            skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1896                                    else
1897                                            skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
1898                            }
1899    
1900                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1901    
1902    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1903    
1904                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1905                                    {
1906                                            int bSkip = 1;
1907    
1908                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1909                                            {
1910                                                    int iSAD;
1911                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1912                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1913                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1914                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1915                                                    {       bSkip = 0;
1916                                                            break;
1917                                                    }
1918                                            }
1919    
1920                                            if (!bSkip) {   /* no SKIP, but trivial block */
1921                                                    if(pEnc->mbParam.m_quarterpel) {
1922                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1923                                                            pMB->pmvs[0].x = - predMV.x;
1924                                                            pMB->pmvs[0].y = - predMV.y;
1925                                                    }
1926                                                    else {
1927                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1928                                                            pMB->pmvs[0].x = - predMV.x;
1929                                                            pMB->pmvs[0].y = - predMV.y;
1930                                                    }
1931                                                    pMB->mode = MODE_INTER;
1932                                                    pMB->cbp = 0;
1933                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1934                                                    stop_coding_timer();
1935    
1936                                                    continue;       /* next MB */
1937                                            }
1938                                    }
1939                                    /* do SKIP */
1940    
1941                                    pMB->mode = MODE_NOT_CODED;
1942                                    MBSkip(bs);
1943                                    stop_coding_timer();
1944                                    continue;       /* next MB */
1945                            }
1946                            /* ordinary case: normal coded INTER/INTER4V block */
1947    
1948                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1949                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1950                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1951                                    qcoeff[5*64+0]=0;
1952                            }
1953    
1954                            if(pEnc->mbParam.m_quarterpel) {
1955                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1956                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1957                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1958                                    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);
1959                            } else {
1960                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1961                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1962                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1963                                    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);
1964                            }
1965    
1966    
1967                            if (pMB->mode == MODE_INTER4V)
1968                            {       int k;
1969                                    for (k=1;k<4;k++)
1970                                    {
1971                                            if(pEnc->mbParam.m_quarterpel) {
1972                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1973                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1974                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1975                                    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);
1976                                            } else {
1977                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1978                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1979                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1980                                    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);
1981                                            }
1982    
1983                                    }
1984                            }
1985    
1986                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1987                          stop_coding_timer();                          stop_coding_timer();
1988    
1989                    }
1990                  }                  }
1991    
1992            if ((pEnc->current->global_flags & XVID_REDUCED))
1993            {
1994                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1995                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1996                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1997          }          }
1998    
1999          emms();          emms();
# Line 1699  Line 2002 
2002                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2003          }          }
2004    
2005          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2006                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2007    
2008          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2009    
2010          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2011    
2012          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2013                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */
2014          {          {
2015                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2016                  iSearchRange *= 2;                  iSearchRange *= 2;
2017          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2018                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2019                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2020                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16                             && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel)))      /* minimum search range 16 */
2021          {          {
2022                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2023                  iSearchRange /= 2;                  iSearchRange /= 2;
2024          }          }
2025    
2026          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2027    
2028            /* frame drop code */
2029            DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
2030            if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2031                    (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2032            {
2033                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2034                    pEnc->current->sStat.ublks = mb_width * mb_height;
2035    
2036                    BitstreamReset(bs);
2037    
2038                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2039                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2040    
2041                    /* copy reference frame details into the current frame */
2042                    pEnc->current->quant = pEnc->reference->quant;
2043                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
2044                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
2045                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
2046                    pEnc->current->fcode = pEnc->reference->fcode;
2047                    pEnc->current->bcode = pEnc->reference->bcode;
2048                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
2049                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2050            }
2051    
2052            /* XXX: debug
2053            {
2054                    char s[100];
2055                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2056                    image_dump_yuvpgm(&pEnc->current->image,
2057                            pEnc->mbParam.edged_width,
2058                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2059    
2060                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2061                    image_dump_yuvpgm(&pEnc->reference->image,
2062                            pEnc->mbParam.edged_width,
2063                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2064            }
2065            */
2066    
         *pBits = BitstreamPos(bs) - *pBits;  
2067    
2068          pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase;          *pBits = BitstreamPos(bs) - *pBits;
2069    
2070          pEnc->last_pframe = pEnc->mbParam.m_ticks;          return 0;                                       /* inter */
         return 0;                                       // inter  
2071  }  }
2072    
2073    
 #ifdef BFRAMES  
2074  static void  static void
2075  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2076                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2077                     Bitstream * bs,                     Bitstream * bs,
2078                     uint32_t * pBits)                     uint32_t * pBits)
2079  {  {
2080          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2081          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2082          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
2083    
2084          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2085          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
# Line 1754  Line 2091 
2091                  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); \
2092          }          }
2093    
2094            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2095    
2096          if (!first){          if (!first){
2097                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2098          }          }
2099  #endif  #endif
2100    
2101          // forward          frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2102    
2103            /* forward  */
2104          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2105                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2106                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2107          start_timer();          start_timer();
2108          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2109                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2110                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2111          stop_inter_timer();          stop_inter_timer();
2112    
2113          // backward          /* backward */
2114          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2115                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2116                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2117          start_timer();          start_timer();
2118          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2119                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2120                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2121          stop_inter_timer();          stop_inter_timer();
2122    
2123          start_timer();          start_timer();
2124    
2125          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2126            ((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase,                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2127                                                  pEnc->time_pp,                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2128                                                  pEnc->reference->mbs, f_ref,                                                  pEnc->reference->mbs, f_ref,
2129                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2130                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2131                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
2132    
2133    
2134          stop_motion_timer();          stop_motion_timer();
2135            /*
2136          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
            {  
2137             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
2138             } */          }
2139            */
2140    
2141          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2142    
2143            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2144          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
2145    
2146          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2147    
2148          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2149          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2150          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2151          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2152    
2153    
2154          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;  
   
2155                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2156                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2157                                  &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];  
2158    
2159                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2160                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2161                                  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  
2162                                  continue;                                  continue;
2163                          }                          }
2164    
2165                            if (mb->mode != MODE_DIRECT_NONE_MV) {
2166                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2167                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2168                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2169                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
2170                                                                           dct_codes);                                                                           dct_codes);
2171    
2172                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2173                          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);  
2174    
2175                                    mb->cbp =
2176                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
2177    
2178                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2179                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2180                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2181                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {  
                                 mb->pmvs[0].x = mb->mvs[0].x - forward.x;  
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
2182                          }                          }
2183    
                         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;  
                         }  
 //                      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);  
   
2184  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2185          BFRAME_DEBUG          BFRAME_DEBUG
2186  #endif  #endif
2187                          start_timer();                          start_timer();
2188                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2189                                                   &pEnc->sStat);                                                   &frame->sStat, direction);
2190                          stop_coding_timer();                          stop_coding_timer();
2191                  }                  }
2192          }          }
2193    
2194          emms();          emms();
2195    
2196          // TODO: dynamic fcode/bcode ???          /* TODO: dynamic fcode/bcode ??? */
2197    
2198          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2199    
# Line 1893  Line 2205 
2205          }          }
2206  #endif  #endif
2207  }  }
 #endif  

Legend:
Removed from v.1.58  
changed lines
  Added in v.1.96

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