[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.45, Sat Jun 22 07:23:10 2002 UTC revision 1.76.2.39, Wed Jan 15 14:36:06 2003 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37   *      20.06.2002 bframe patch   *      20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
# Line 51  Line 53 
53  #include "global.h"  #include "global.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "image/image.h"  #include "image/image.h"
56    #include "image/font.h"
57    #include "motion/sad.h"
58  #include "motion/motion.h"  #include "motion/motion.h"
59  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
60  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 68  Line 72 
72   ****************************************************************************/   ****************************************************************************/
73    
74  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
75  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
76    
77  /*****************************************************************************  /*****************************************************************************
78   * Local function prototypes   * Local function prototypes
# Line 84  Line 88 
88                                            bool force_inter,                                            bool force_inter,
89                                            bool vol_header);                                            bool vol_header);
90    
 #ifdef BFRAMES  
91  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
92                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
93                                             Bitstream * bs,                                             Bitstream * bs,
94                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
95    
96  /*****************************************************************************  /*****************************************************************************
97   * Local data   * Local data
# Line 104  Line 106 
106  };  };
107    
108    
 static void __inline  
 image_null(IMAGE * image)  
 {  
         image->y = image->u = image->v = NULL;  
 }  
   
   
109  /*****************************************************************************  /*****************************************************************************
110   * Encoder creation   * Encoder creation
111   *   *
# Line 201  Line 196 
196    
197          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
198    
199          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
200                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
201    
   
202          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
203          if (pEnc == NULL)          if (pEnc == NULL)
204                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
# Line 229  Line 223 
223    
224          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
225    
226          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
227    
228          /* Fill rate control parameters */          /* Fill rate control parameters */
229    
230          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
231    
232          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
233          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
234    
235          /* try to allocate frame memory */          /* try to allocate frame memory */
236    
# Line 263  Line 257 
257  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
258          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
259  #endif  #endif
260  #ifdef BFRAMES  
261          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
262          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
263          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
264  #endif  
265          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
266          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
267          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 282  Line 276 
276                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
277                  goto xvid_err_memory3;                  goto xvid_err_memory3;
278  #endif  #endif
279  #ifdef BFRAMES  
280          if (image_create          if (image_create
281                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
282                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 295  Line 289 
289                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
290                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
291                  goto xvid_err_memory3;                  goto xvid_err_memory3;
292  #endif  
293          if (image_create          if (image_create
294                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
295                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 325  Line 319 
319                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
320                  goto xvid_err_memory3;                  goto xvid_err_memory3;
321    
322    /* Create full bitplane for GMC, this might be wasteful */
323            if (image_create
324                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
325                     pEnc->mbParam.edged_height) < 0)
326                    goto xvid_err_memory3;
327    
328    
329          /* B Frames specific init */          /* B Frames specific init */
 #ifdef BFRAMES  
330    
331          pEnc->packed = pParam->packed;          pEnc->mbParam.global = pParam->global;
332          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
333          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
334            pEnc->mbParam.bquant_offset = pParam->bquant_offset;
335            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
336          pEnc->bframes = NULL;          pEnc->bframes = NULL;
337    
338          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 375  Line 375 
375          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
376          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
377          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
378            pEnc->bframenum_dx50bvop = -1;
379    
380          pEnc->queue = NULL;          pEnc->queue = NULL;
381    
# Line 405  Line 406 
406          pEnc->queue_tail = 0;          pEnc->queue_tail = 0;
407          pEnc->queue_size = 0;          pEnc->queue_size = 0;
408    
409            pEnc->mbParam.m_stamp = 0;
410    
411          pEnc->mbParam.m_seconds = 0;          pEnc->m_framenum = 0;
412          pEnc->mbParam.m_ticks = 0;          pEnc->current->stamp = 0;
413  #endif          pEnc->reference->stamp = 0;
414    
415          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
416    
# Line 427  Line 429 
429          /*          /*
430           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
431           */           */
432  #ifdef BFRAMES  
433    xvid_err_memory5:    xvid_err_memory5:
434    
435    
# Line 461  Line 463 
463                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
464          }          }
465    
 #endif  
   
466    xvid_err_memory3:    xvid_err_memory3:
467  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
468          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
469                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
470  #endif  #endif
471    
 #ifdef BFRAMES  
472          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
473                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
474          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
475                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
476          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
477                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
478    
479          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
480                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 493  Line 491 
491          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
492                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
493    
494    /* destroy GMC image */
495            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
496                                      pEnc->mbParam.edged_height);
497    
498    
499    xvid_err_memory2:    xvid_err_memory2:
500          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
501          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 521  Line 524 
524  int  int
525  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
526  {  {
 #ifdef BFRAMES  
527          int i;          int i;
 #endif  
528    
529          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
530    
531          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
532          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
533    
534                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 558  Line 558 
558                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
559    
560          }          }
 #endif  
561    
562          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
563    
# Line 576  Line 575 
575                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
576          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
577                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
578  #ifdef BFRAMES  
579          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
580                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
581          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
582                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
583          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
584                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
585  #endif  
586  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
587          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
588                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 603  Line 602 
602  }  }
603    
604    
605  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
606  {  {
607          pEnc->iFrameNum++;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
608          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
         if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {  
                 pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;  
                 pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;  
         }  
609  }  }
 #endif  
610    
611    
612  #ifdef BFRAMES  static __inline void
613  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
614  {  {
615          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
616          {          {
617                  DPRINTF("FATAL: QUEUE FULL");                  DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
618                  return;                  return;
619          }          }
620    
621          DPRINTF("*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",          DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
622                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
623                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
624    
# Line 633  Line 626 
626          start_timer();          start_timer();
627          if (image_input          if (image_input
628                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
629                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
630                  return;                  return;
631          stop_conv_timer();          stop_conv_timer();
632    
633          pEnc->queue_size++;          pEnc->queue_size++;
634          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
635  }  }
636  #endif  
637    static __inline void
638    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
639    {
640    
641                    pCur->ticks = (int32_t)pCur->stamp % time_base;
642                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
643    
644                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
645    
646    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
647                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
648                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
649                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
650                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
651                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
652    
653    */
654    }
655    
656    
657    
658    /* convert pFrame->intra to coding_type */
659    static int intra2coding_type(int intra)
660    {
661            if (intra < 0)  return -1;
662            if (intra == 1) return I_VOP;
663            if (intra == 2) return B_VOP;
664            if (intra == 3) return S_VOP;
665    
666            return P_VOP;
667    }
668    
669    
670    
 #ifdef BFRAMES  
671  /*****************************************************************************  /*****************************************************************************
672   * IPB frame encoder entry point   * IPB frame encoder entry point
673   *   *
# Line 661  Line 685 
685          uint16_t x, y;          uint16_t x, y;
686          Bitstream bs;          Bitstream bs;
687          uint32_t bits;          uint32_t bits;
688            int mode;
689    
690          int input_valid = 1;          int input_valid = 1;
691            int bframes_count = 0;
692    
693  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
694          float psnr;          float psnr;
# Line 693  Line 719 
719                           * frame as a pframe                           * frame as a pframe
720                           */                           */
721    
722                          DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
723                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
724                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
725    
726                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
727                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
728    
729                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
730    
731                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
732                            bframes_count = 0;
733    
734                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
735                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
736                            if(pEnc->current->coding_type == P_VOP)
737                          pFrame->intra = 0;                          pFrame->intra = 0;
738                            else
739                                    pFrame->intra = 3;
740    
741                            emms();
742    
743                            if (pResult) {
744                                    pResult->quant = pEnc->current->quant;
745                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
746                                    pResult->kblks = pEnc->current->sStat.kblks;
747                                    pResult->mblks = pEnc->current->sStat.mblks;
748                                    pResult->ublks = pEnc->current->sStat.ublks;
749                            }
750    
751                          return XVID_ERR_OK;                          return XVID_ERR_OK;
752                  }                  }
753    
754    
755                  DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
756                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
757                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
758    
759                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
760                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
761    
762                  BitstreamPad(&bs);                  BitstreamPadAlways(&bs);
763                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
764                  pFrame->intra = 0;                  pFrame->intra = 2;
765    
766                    if (pResult) {
767                            pResult->quant = pEnc->current->quant;
768                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
769                            pResult->kblks = pEnc->current->sStat.kblks;
770                            pResult->mblks = pEnc->current->sStat.mblks;
771                            pResult->ublks = pEnc->current->sStat.ublks;
772                    }
773    
774                  if (input_valid)                  if (input_valid)
775                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
776    
777                    emms();
778    
779                  return XVID_ERR_OK;                  return XVID_ERR_OK;
780          }          }
781    
782          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
783                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
784    
785                  if (pEnc->packed) {                  /* write an empty marker to the bitstream.
786    
787                       for divx5 decoder compatibility, this marker must consist
788                       of a not-coded p-vop, with a time_base of zero, and time_increment
789                       indentical to the future-referece frame.
790                    */
791    
792                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
793                            int tmp;
794    
795                          DPRINTF("*** 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",
796                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
797                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
798    
799    
800                            tmp = pEnc->current->seconds;
801                            pEnc->current->seconds = 0; /* force time_base = 0 */
802    
803                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
804                          BitstreamPad(&bs);                          pEnc->current->seconds = tmp;
                         BitstreamPutBits(&bs, 0x7f, 8);  
805    
806                            BitstreamPadAlways(&bs);
807                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
808                          pFrame->intra = 0;                          pFrame->intra = 4;
809    
810                            if (pResult) {
811                                    pResult->quant = pEnc->current->quant;
812                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
813                                    pResult->kblks = pEnc->current->sStat.kblks;
814                                    pResult->mblks = pEnc->current->sStat.mblks;
815                                    pResult->ublks = pEnc->current->sStat.ublks;
816                            }
817    
818                          if (input_valid)                          if (input_valid)
819                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
820    
821                            emms();
822    
823                          return XVID_ERR_OK;                          return XVID_ERR_OK;
824                  }                  }
825          }          }
# Line 756  Line 827 
827    
828  bvop_loop:  bvop_loop:
829    
830          if (input_valid) {          if (pEnc->bframenum_dx50bvop != -1)
831            {
832    
833                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
834                    SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
835    
836                  SWAP(pEnc->current, pEnc->reference);                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
837                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
838                    }
839    
840                    if (input_valid)
841                    {
842                            queue_image(pEnc, pFrame);
843                            input_valid = 0;
844                    }
845    
846            } else if (input_valid) {
847    
848                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
849    
850                  start_timer();                  start_timer();
851                  if (image_input                  if (image_input
852                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
853                          pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
854                    {
855                            emms();
856                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
857                    }
858                  stop_conv_timer();                  stop_conv_timer();
859    
860                  // queue input frame, and dequue next image                  // queue input frame, and dequue next image
# Line 781  Line 871 
871    
872          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
873    
874                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
875    
876                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
877                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
878                  pEnc->queue_size--;                  pEnc->queue_size--;
879    
880          } else if (BitstreamPos(&bs) == 0) {          } else {
881    
882                    /* if nothing was encoded, write an 'ignore this frame' flag
883                       to the bitstream */
884    
885                  DPRINTF("*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  if (BitstreamPos(&bs) == 0) {
886    
887                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
888                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
889                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
890    
891                    //      BitstreamPutBits(&bs, 0x7f, 8);
892                            pFrame->intra = 5;
893    
894                  pFrame->intra = 0;                          if (pResult) {
895                                    /*
896                                     * We must decide what to put there because i know some apps
897                                     * are storing statistics about quantizers and just do
898                                     * stats[quant]++ or stats[quant-1]++
899                                     * transcode is one of these app with its 2pass module
900                                     */
901    
902                  BitstreamPutBits(&bs, 0x7f, 8);                                  /*
903                  BitstreamPad(&bs);                                   * For now i prefer 31 than 0 that could lead to a segfault
904                  pFrame->length = BitstreamLength(&bs);                                   * in transcode
905                                     */
906                                    pResult->quant = 31;
907    
908                  return XVID_ERR_OK;                                  pResult->hlength = 0;
909                                    pResult->kblks = 0;
910                                    pResult->mblks = 0;
911                                    pResult->ublks = 0;
912                            }
913    
914          } else {          } else {
915    
916                            if (pResult) {
917                                    pResult->quant = pEnc->current->quant;
918                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
919                                    pResult->kblks = pEnc->current->sStat.kblks;
920                                    pResult->mblks = pEnc->current->sStat.mblks;
921                                    pResult->ublks = pEnc->current->sStat.ublks;
922                            }
923    
924                    }
925    
926                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
927    
928                    emms();
929    
930                  return XVID_ERR_OK;                  return XVID_ERR_OK;
931          }          }
932    
933          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
934    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
 //$$    SWAP(pEnc->current, pEnc->reference);  
   
935          emms();          emms();
936    
937            // only inc frame num, adapt quant, etc. if we havent seen it before
938            if (pEnc->bframenum_dx50bvop < 0 )
939            {
940                    mode = intra2coding_type(pFrame->intra);
941          if (pFrame->quant == 0)          if (pFrame->quant == 0)
942                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
943          else          else
944                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
945    
946          if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
947                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
948    
949          if (pEnc->current->quant > 31)          if (pEnc->current->quant > 31)
950                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
951    */
952          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
953          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
954          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
955          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
956          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
957          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
958    
959  //$$$   start_timer();                  inc_frame_num(pEnc);
 //$$$   if (image_input  
 //$$$           (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
 //$$$            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))  
 //$$$           return XVID_ERR_FORMAT;  
 //$$$   stop_conv_timer();  
960    
961  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
962          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 852  Line 965 
965    
966          emms();          emms();
967    
968                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
969                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
970                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
971                    }
972    
973          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974           * Luminance masking           * Luminance masking
975           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 881  Line 999 
999                          }                          }
1000    
1001  #undef OFFSET  #undef OFFSET
   
1002                  }                  }
1003    
1004                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1005          }          }
1006    
1007            }
1008    
1009          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
1011           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1012            pEnc->iFrameNum++;
1013    
1014            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1015                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1016                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
1017            {
1018                    mode = I_VOP;
1019            }else{
1020                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1021                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1022                                            (mode < 0) ? pEnc->iFrameNum : 0,
1023                                            bframes_count++);
1024            }
1025    
1026          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          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) {  
1027                  /*                  /*
1028                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
1029                   */                   */
1030                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1031                            pEnc->mbParam.m_quarterpel = 1;
1032                    else
1033                            pEnc->mbParam.m_quarterpel = 0;
1034    
1035                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1036    
1037                  DPRINTF("*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1038                            if (pFrame->quant_intra_matrix != NULL)
1039                                    set_intra_matrix(pFrame->quant_intra_matrix);
1040                            if (pFrame->quant_inter_matrix != NULL)
1041                                    set_inter_matrix(pFrame->quant_inter_matrix);
1042                    }
1043    
1044    
1045                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1046                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1047                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1048    
1049                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1050                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1051                    }
1052    
1053                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
1054    
1055                    if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1056    
1057                            pEnc->bframenum_tail--;
1058                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1059    
1060                            SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1061                            if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1062                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1063                            }
1064                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
1065                            bframes_count = 0;
1066    
1067                            if(pEnc->current->coding_type == P_VOP)
1068                                    pFrame->intra = 0;
1069                            else
1070                                    pFrame->intra = 3;
1071    
1072    
1073                    } else {
1074    
1075                            FrameCodeI(pEnc, &bs, &bits);
1076                            bframes_count = 0;
1077                  pFrame->intra = 1;                  pFrame->intra = 1;
                 pEnc->flush_bframes = 1;  
1078    
1079                  inc_frame_num(pEnc);                          pEnc->bframenum_dx50bvop = -1;
1080                    }
1081    
1082                  if (pEnc->packed) {                  pEnc->flush_bframes = 1;
1083                          BitstreamPad(&bs);  
1084                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1085                            BitstreamPadAlways(&bs);
1086                          input_valid = 0;                          input_valid = 0;
1087                          goto ipvop_loop;                          goto ipvop_loop;
1088                  }                  }
# Line 923  Line 1091 
1091                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1092                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1093                   */                   */
1094          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1095                  /*                  /*
1096                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1097                   */                   */
1098    
1099                  DPRINTF("*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1100                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1101                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1102    
1103                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1104                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1105                    }
1106    
1107                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1108                    bframes_count = 0;
1109                    if(pEnc->current->coding_type == P_VOP)
1110                  pFrame->intra = 0;                  pFrame->intra = 0;
1111                    else
1112                            pFrame->intra = 3;
1113                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1114    
1115                  inc_frame_num(pEnc);                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1116                            BitstreamPadAlways(&bs);
                 if (pEnc->packed) {  
                         BitstreamPad(&bs);  
1117                          input_valid = 0;                          input_valid = 0;
1118                          goto ipvop_loop;                          goto ipvop_loop;
1119                  }                  }
1120    
1121          } else {          } else {        /* mode == B_VOP */
1122                  /*                  /*
1123                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1124                   */                   */
1125    
1126                  DPRINTF("*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1127                                  pEnc->bframenum_head, pEnc->bframenum_tail,                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1128                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                  }
1129    
1130                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1131                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1132                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1133                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1134                  } else {                  } else {
1135                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1136                  }                  }
1137    
1138                    if (pEnc->current->quant < 1)
1139                            pEnc->current->quant = 1;
1140                    else if (pEnc->current->quant > 31)
1141                pEnc->current->quant = 31;
1142    
1143                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1144                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1145                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1146    
1147                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1148                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1149                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1150    
1151                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1152    
1153                  pFrame->intra = 0;  // bframe report by koepi
1154                    pFrame->intra = 2;
1155                  pFrame->length = 0;                  pFrame->length = 0;
1156    
                 inc_frame_num(pEnc);  
   
1157                  input_valid = 0;                  input_valid = 0;
1158                  goto bvop_loop;                  goto bvop_loop;
1159          }          }
1160    
1161          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1162          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1163    
1164          if (pResult) {          if (pResult) {
1165                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1166                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1167                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1168                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1169                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1170          }          }
1171    
1172          emms();          emms();
# Line 995  Line 1177 
1177                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1178                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1179    
1180          snprintf(temp, 127, "PSNR: %f\n", psnr);          printf("PSNR: %f\n", psnr);
1181          DEBUG(temp);  //      DEBUG(temp);
1182  #endif  #endif
1183    
1184          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
# Line 1004  Line 1186 
1186                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1187          }          }
1188    
   
1189          stop_global_timer();          stop_global_timer();
1190          write_timer();          write_timer();
1191    
1192            emms();
1193          return XVID_ERR_OK;          return XVID_ERR_OK;
1194  }  }
1195    
 #endif  
   
1196    
1197    
1198  /*****************************************************************************  /*****************************************************************************
# Line 1046  Line 1226 
1226          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1227          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1228    
1229          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1230    
1231          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1232          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  
1233          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1234    
1235            inc_frame_num(pEnc);
1236    
1237            /* disable alternate scan flag if interlacing is not enabled */
1238            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1239                    !(pEnc->current->global_flags & XVID_INTERLACING))
1240            {
1241                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1242            }
1243    
1244          start_timer();          start_timer();
1245          if (image_input          if (image_input
1246                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1247                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1248                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1249          stop_conv_timer();          stop_conv_timer();
1250    
# Line 1078  Line 1263 
1263                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1264          }          }
1265    
1266            if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1267                    pEnc->mbParam.m_quarterpel = 1;
1268            else
1269                    pEnc->mbParam.m_quarterpel = 0;
1270    
1271          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1272                  int *temp_dquants =                  int *temp_dquants =
1273                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 1139  Line 1329 
1329    
1330          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1331                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == 0)
1332                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1333                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1334                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1335                  } else {                  } else {
1336                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 1154  Line 1344 
1344    
1345          }          }
1346    
1347          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1348          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1349          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1350          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1351    
1352          if (pResult) {          if (pResult) {
1353                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1354                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1355                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1356                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1357                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1358          }          }
1359    
1360          emms();          emms();
# Line 1180  Line 1370 
1370                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1371    
1372          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1373          DEBUG(temp);  //      DEBUG(temp);
1374  #endif  #endif
1375    
 #ifdef BFRAMES  
         inc_frame_num(pEnc);  
 #else  
1376          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1377    
1378          stop_global_timer();          stop_global_timer();
1379          write_timer();          write_timer();
# Line 1264  Line 1449 
1449                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1450                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1451                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1452                          VECTOR pred[4];                          VECTOR pred;
1453                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1454                          int vec;                          int vec;
1455    
1456                          pMB->mode =                          pMB->mode =
# Line 1286  Line 1470 
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    
1473                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
                                                         0, pred, dummy);  
1474    
1475                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1476                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1477                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1478                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1479                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1480                                  }                                  }
1481                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1482                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1306  Line 1489 
1489                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1490                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1491    
1492                                          get_pmvdata(pEnc->current->mbs, x, y,                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
                                                                 pEnc->mbParam.mb_width, vec, pred, dummy);  
1493    
1494                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1495                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1496                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1497                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1498                                  }                                  }
1499                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1500                          {                          {
# Line 1355  Line 1537 
1537    
1538          if (intra) {          if (intra) {
1539                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1540                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1541                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1542                  }                  }
1543                  return;                  return;
# Line 1430  Line 1612 
1612                     Bitstream * bs,                     Bitstream * bs,
1613                     uint32_t * pBits)                     uint32_t * pBits)
1614  {  {
1615            int mb_width = pEnc->mbParam.mb_width;
1616            int mb_height = pEnc->mbParam.mb_height;
1617    
1618          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1619          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1620    
1621          uint16_t x, y;          uint16_t x, y;
1622    
1623            if ((pEnc->current->global_flags & XVID_REDUCED))
1624            {
1625                    mb_width = (pEnc->mbParam.width + 31) / 32;
1626                    mb_height = (pEnc->mbParam.height + 31) / 32;
1627    
1628                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1629                    /* XXX: setedges is overkill */
1630                    start_timer();
1631                    image_setedges(&pEnc->current->image,
1632                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1633                            pEnc->mbParam.width, pEnc->mbParam.height);
1634                    stop_edges_timer();
1635            }
1636    
1637          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1638          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1639          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1640            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1641          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1642    
1643          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1644  #ifdef BFRAMES  
1645  #define DIVX501B481P "DivX501b481p"          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1646          if (pEnc->packed) {  
1647                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));          BitstreamPadAlways(bs);
         }  
 #endif  
1648          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1649    
1650          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1651    
1652          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1653          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1654          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1655    
1656          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1657                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1658                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1659                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1660    
# Line 1471  Line 1668 
1668                          stop_prediction_timer();                          stop_prediction_timer();
1669    
1670                          start_timer();                          start_timer();
1671                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1672                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1673                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1674                                    qcoeff[5*64+0]=0;
1675                            }
1676                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1677                          stop_coding_timer();                          stop_coding_timer();
1678                  }                  }
1679    
1680            if ((pEnc->current->global_flags & XVID_REDUCED))
1681            {
1682                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1683                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1684                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1685            }
1686          emms();          emms();
1687    
1688          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1689          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1690          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1691    
1692          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
# Line 1492  Line 1698 
1698    
1699    
1700  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1701    #define BFRAME_SKIP_THRESHHOLD 30
1702    
1703    
1704    /* FrameCodeP also handles S(GMC)-VOPs */
1705  static int  static int
1706  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1707                     Bitstream * bs,                     Bitstream * bs,
# Line 1505  Line 1714 
1714          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1715          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1716    
1717            int mb_width = pEnc->mbParam.mb_width;
1718            int mb_height = pEnc->mbParam.mb_height;
1719    
1720          int iLimit;          int iLimit;
1721          uint32_t x, y;          int x, y, k;
1722          int iSearchRange;          int iSearchRange;
1723          int bIntra;          int bIntra, skip_possible;
1724    
1725          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1726          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1727    
1728            if ((pEnc->current->global_flags & XVID_REDUCED))
1729            {
1730                    mb_width = (pEnc->mbParam.width + 31) / 32;
1731                    mb_height = (pEnc->mbParam.height + 31) / 32;
1732            }
1733    
1734    
1735          start_timer();          start_timer();
1736          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1737                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1738          stop_edges_timer();          stop_edges_timer();
1739    
1740          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1741          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1742            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1743          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1744    
1745          if (!force_inter)          if (!force_inter)
1746                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1747          else          else
1748                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1749    
1750          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1751                  start_timer();                  start_timer();
1752                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1753                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1754                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1755                                                      pEnc->mbParam.m_quarterpel,
1756                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1757                  stop_inter_timer();                  stop_inter_timer();
1758          }          }
1759    
1760            pEnc->current->coding_type = P_VOP;
1761    
1762          start_timer();          start_timer();
1763          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1764                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1765          } else {          else
1766                  bIntra =                  bIntra =
1767                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1768                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1769                                                           iLimit);                                                           iLimit);
1770          }  
1771          stop_motion_timer();          stop_motion_timer();
1772    
1773          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1774    
1775          pEnc->current->coding_type = P_VOP;          if ( ( pEnc->current->global_flags & XVID_GMC )
1776                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1777            {
1778                    pEnc->current->coding_type = S_VOP;
1779    
1780                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1781                                            pEnc->mbParam.width, pEnc->mbParam.height,
1782                                            &pEnc->current->gmc_data);
1783    
1784                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1785                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1786                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1787                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1788                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1789    
1790            }
1791    
1792            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1793          if (vol_header)          if (vol_header)
1794                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1795                    BitstreamPadAlways(bs);
1796            }
1797    
1798          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1799    
1800          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1801    
1802          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1803          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;  
1804    
1805          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1806                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1807                    for (x = 0; x < mb_width; x++) {
1808                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1809                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1810    
1811    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1812    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1813    
1814                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1815    
1816                          if (!bIntra) {                          if (bIntra) {
1817                                    CodeIntraMB(pEnc, pMB);
1818                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1819                                                                      dct_codes, qcoeff);
1820    
1821                                    start_timer();
1822                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1823                                    stop_prediction_timer();
1824    
1825                                    pEnc->current->sStat.kblks++;
1826    
1827                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1828                                    stop_coding_timer();
1829                                    continue;
1830                            }
1831    
1832                            if (pEnc->current->coding_type == S_VOP) {
1833    
1834                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1835                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1836                                            pEnc->mbParam.edged_width, 65536);
1837    
1838                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1839                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1840                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1841    
1842                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1843                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1844                                    }
1845    
1846                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1847    
1848                                            if (pEnc->mbParam.m_quarterpel)
1849                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1850                                            else
1851                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1852    
1853                                            pMB->mode = MODE_INTER;
1854                                            pMB->mcsel = 1;
1855                                            pMB->sad16 = iSAD;
1856                                    } else {
1857                                            pMB->mcsel = 0;
1858                                    }
1859                            } else {
1860                                    pMB->mcsel = 0; /* just a precaution */
1861                            }
1862    
1863                                  start_timer();                                  start_timer();
1864                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1865                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1866                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1867                                                                     &pEnc->current->image,
1868                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1869                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1870                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1871                                                                     pEnc->mbParam.m_quarterpel,
1872                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1873                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1874    
1875                                  stop_comp_timer();                                  stop_comp_timer();
1876    
1877                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1600  Line 1888 
1888    
1889                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1890    
1891                                  pMB->cbp =                          if (pMB->mode != MODE_NOT_CODED)
1892                            {       pMB->cbp =
1893                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1894                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1895                          }                          }
1896    
1897                          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 ||  
1898                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1899                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1900                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1901                          } else {                          } else {
1902                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1903                          }                          }
1904    
1905                          start_timer();                          start_timer();
1906                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1907                            /* Finished processing the MB, now check if to CODE or SKIP */
1908    
1909                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1910                                                            (pMB->dquant == NO_CHANGE);
1911    
1912                            if (pEnc->current->coding_type == S_VOP)
1913                                    skip_possible &= (pMB->mcsel == 1);
1914                            else if (pEnc->current->coding_type == P_VOP) {
1915                                    if (pEnc->mbParam.m_quarterpel)
1916                                            skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1917                                    else
1918                                            skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
1919                            }
1920    
1921                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1922    
1923    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1924    
1925                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1926                                    {
1927                                            int bSkip = 1;
1928    
1929                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1930                                            {
1931                                                    int iSAD;
1932                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1933                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1934                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1935                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1936                                                    {       bSkip = 0;
1937                                                            break;
1938                                                    }
1939                                            }
1940    
1941                                            if (!bSkip) {   /* no SKIP, but trivial block */
1942                                                    if(pEnc->mbParam.m_quarterpel) {
1943                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1944                                                            pMB->pmvs[0].x = - predMV.x;
1945                                                            pMB->pmvs[0].y = - predMV.y;
1946                                                    }
1947                                                    else {
1948                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1949                                                            pMB->pmvs[0].x = - predMV.x;
1950                                                            pMB->pmvs[0].y = - predMV.y;
1951                                                    }
1952                                                    pMB->mode = MODE_INTER;
1953                                                    pMB->cbp = 0;
1954                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1955                                                    stop_coding_timer();
1956    
1957                                                    continue;       /* next MB */
1958                                            }
1959                                    }
1960                                    /* do SKIP */
1961    
1962                                    pMB->mode = MODE_NOT_CODED;
1963                                    MBSkip(bs);
1964                                    stop_coding_timer();
1965                                    continue;       /* next MB */
1966                            }
1967                            /* ordinary case: normal coded INTER/INTER4V block */
1968    
1969                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1970                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1971                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1972                                    qcoeff[5*64+0]=0;
1973                            }
1974    
1975                            if(pEnc->mbParam.m_quarterpel) {
1976                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1977                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1978                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1979                                    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);
1980                            } else {
1981                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1982                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1983                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1984                                    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);
1985                            }
1986    
1987    
1988                            if (pMB->mode == MODE_INTER4V)
1989                            {       int k;
1990                                    for (k=1;k<4;k++)
1991                                    {
1992                                            if(pEnc->mbParam.m_quarterpel) {
1993                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1994                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1995                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1996                                    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);
1997                                            } else {
1998                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1999                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
2000                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
2001                                    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);
2002                                            }
2003    
2004                                    }
2005                            }
2006    
2007                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2008                          stop_coding_timer();                          stop_coding_timer();
2009    
2010                    }
2011                  }                  }
2012    
2013            if ((pEnc->current->global_flags & XVID_REDUCED))
2014            {
2015                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2016                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2017                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2018          }          }
2019    
2020          emms();          emms();
# Line 1635  Line 2023 
2023                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2024          }          }
2025    
2026          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2027                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2028    
2029          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2030    
2031          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2032    
2033          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2034                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
2035          {          {
2036                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2037                  iSearchRange *= 2;                  iSearchRange *= 2;
2038          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2039                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2040                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2041                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
2042          {          {
2043                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2044                  iSearchRange /= 2;                  iSearchRange /= 2;
2045          }          }
2046    
2047          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2048    
2049            /* frame drop code */
2050            DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
2051            if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2052                    (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2053            {
2054                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2055                    pEnc->current->sStat.ublks = mb_width * mb_height;
2056    
2057                    BitstreamReset(bs);
2058    
2059                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2060                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2061    
2062                    // copy reference frame details into the current frame
2063                    pEnc->current->quant = pEnc->reference->quant;
2064                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
2065                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
2066                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
2067                    pEnc->current->fcode = pEnc->reference->fcode;
2068                    pEnc->current->bcode = pEnc->reference->bcode;
2069                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
2070                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2071            }
2072    
2073            /* XXX: debug
2074            {
2075                    char s[100];
2076                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2077                    image_dump_yuvpgm(&pEnc->current->image,
2078                            pEnc->mbParam.edged_width,
2079                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2080    
2081                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2082                    image_dump_yuvpgm(&pEnc->reference->image,
2083                            pEnc->mbParam.edged_width,
2084                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2085            }
2086            */
2087    
2088    
2089          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2090    
# Line 1664  Line 2092 
2092  }  }
2093    
2094    
 #ifdef BFRAMES  
2095  static void  static void
2096  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2097                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2098                     Bitstream * bs,                     Bitstream * bs,
2099                     uint32_t * pBits)                     uint32_t * pBits)
2100  {  {
2101          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2102          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2103          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
2104    
2105          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2106          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2107    
2108    #ifdef BFRAMES_DEC_DEBUG
2109            FILE *fp;
2110            static char first=0;
2111    #define BFRAME_DEBUG    if (!first && fp){ \
2112                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2113            }
2114    
2115            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2116    
2117            if (!first){
2118                    fp=fopen("C:\\XVIDDBGE.TXT","w");
2119            }
2120    #endif
2121    
2122            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2123    
2124          // forward          // forward
2125          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2126                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2127                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2128          start_timer();          start_timer();
2129          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,
2130                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2131                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2132          stop_inter_timer();          stop_inter_timer();
2133    
2134          // backward          // backward
2135          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2136                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2137                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2138          start_timer();          start_timer();
2139          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2140                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2141                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2142          stop_inter_timer();          stop_inter_timer();
2143    
2144          start_timer();          start_timer();
2145          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,  
2146            MotionEstimationBVOP(&pEnc->mbParam, frame,
2147                    ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
2148                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
2149                            pEnc->reference->mbs, f_ref,
2150                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2151                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2152                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
2153    
2154    
# Line 1717  Line 2160 
2160             } */             } */
2161    
2162          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2163    
2164            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2165          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
2166    
2167          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2168    
2169          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2170          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2171          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2172          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2173    
2174    
2175          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;  
   
2176                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2177                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2178                                  &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];  
2179    
2180                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2181                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2182                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
2183                                  continue;                                  continue;
2184                          }                          }
2185    
2186                            if (mb->mode != MODE_DIRECT_NONE_MV) {
2187                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2188                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2189                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2190                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
2191                                                                           dct_codes);                                                                           dct_codes);
2192    
2193                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2194                          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);  
   
2195    
2196                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  mb->cbp =
2197                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mode = 5;   // skipped  
                         }  
2198    
2199                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2200                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2201                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
2202                          }                          }
   
                         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;  
2203                          }                          }
 //          printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  
2204    
2205    #ifdef BFRAMES_DEC_DEBUG
2206            BFRAME_DEBUG
2207    #endif
2208                          start_timer();                          start_timer();
2209                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2210                                                   &pEnc->sStat);                                                   &frame->sStat, direction);
2211                          stop_coding_timer();                          stop_coding_timer();
2212                  }                  }
2213          }          }
# Line 1794  Line 2217 
2217          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
2218    
2219          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2220    
2221    #ifdef BFRAMES_DEC_DEBUG
2222            if (!first){
2223                    first=1;
2224                    if (fp)
2225                            fclose(fp);
2226  }  }
2227  #endif  #endif
2228    }

Legend:
Removed from v.1.45  
changed lines
  Added in v.1.76.2.39

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