[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.76, Tue Sep 3 17:25:18 2002 UTC revision 1.76.2.37, Mon Jan 13 14:33:24 2003 UTC
# Line 43  Line 43 
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
   
46  #include <stdlib.h>  #include <stdlib.h>
47  #include <stdio.h>  #include <stdio.h>
48  #include <math.h>  #include <math.h>
# Line 54  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"
 #ifdef BFRAMES  
56  #include "image/font.h"  #include "image/font.h"
57  #include "motion/sad.h"  #include "motion/sad.h"
 #endif  
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 70  Line 67 
67  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
68  #include "utils/mem_align.h"  #include "utils/mem_align.h"
69    
 #ifdef _SMP  
 #include "motion/smp_motion_est.h"  
 #endif  
70  /*****************************************************************************  /*****************************************************************************
71   * Local macros   * Local macros
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 94  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 114  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 238  Line 223 
223    
224          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
225    
226  #ifdef _SMP          pEnc->fMvPrevSigma = -1;
         pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);  
 #endif  
   
         pEnc->sStat.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 276  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 295  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 308  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 338  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->global = pParam->global;          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->frame_drop_ratio = pParam->frame_drop_ratio;          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 420  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    
         pEnc->mbParam.m_seconds = 0;  
         pEnc->mbParam.m_ticks = 0;  
411          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
412          pEnc->last_pframe = 0;          pEnc->current->stamp = 0;
413          pEnc->last_sync = 0;          pEnc->reference->stamp = 0;
 #endif  
414    
415          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
416    
# Line 445  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 479  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 511  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 539  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 576  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 594  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 621  Line 602 
602  }  }
603    
604    
605  void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
606  {  {
607          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
608            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
 #ifdef BFRAMES  
         pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;  
         if (pEnc->mbParam.m_ticks < pEnc->last_sync)  
                 pEnc->mbParam.m_seconds = 1;            // more than 1 second since last I or P is not supported.  
         else  
                 pEnc->mbParam.m_seconds = 0;  
   
         if (pEnc->current->coding_type != B_VOP)  
                 pEnc->last_sync = pEnc->mbParam.m_ticks;  
 #else  
   
         pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;  
         pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;  
   
 #endif  
   
609  }  }
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          {          {
# Line 661  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    
665            return P_VOP;
666    }
667    
668    
669    
 #ifdef BFRAMES  
670  /*****************************************************************************  /*****************************************************************************
671   * IPB frame encoder entry point   * IPB frame encoder entry point
672   *   *
# Line 689  Line 684 
684          uint16_t x, y;          uint16_t x, y;
685          Bitstream bs;          Bitstream bs;
686          uint32_t bits;          uint32_t bits;
687            int mode;
688    
689          int input_valid = 1;          int input_valid = 1;
690            int bframes_count = 0;
691    
692  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
693          float psnr;          float psnr;
# Line 726  Line 723 
723                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
724    
725                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
726                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
727    
728                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
729    
730                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
731                            bframes_count = 0;
732    
733                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
734                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
735                          pFrame->intra = 0;                          pFrame->intra = 0;
736    
737                            emms();
738    
739                          return XVID_ERR_OK;                          return XVID_ERR_OK;
740                  }                  }
741    
# Line 747  Line 747 
747                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
748                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
749    
750                  BitstreamPad(&bs);                  BitstreamPadAlways(&bs);
751                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
752                  pFrame->intra = 0;                  pFrame->intra = 2;
753    
754                  if (input_valid)                  if (input_valid)
755                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
756    
757                    emms();
758    
759                  return XVID_ERR_OK;                  return XVID_ERR_OK;
760          }          }
761    
762          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
763                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
764    
765                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  /* write an empty marker to the bitstream.
766    
767                       for divx5 decoder compatibility, this marker must consist
768                       of a not-coded p-vop, with a time_base of zero, and time_increment
769                       indentical to the future-referece frame.
770                    */
771    
772                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
773                            int tmp;
774    
775                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
776                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
777                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
778    
779    
780                            tmp = pEnc->current->seconds;
781                            pEnc->current->seconds = 0; /* force time_base = 0 */
782    
783                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
784                          BitstreamPad(&bs);                          pEnc->current->seconds = tmp;
                         BitstreamPutBits(&bs, 0x7f, 8);  
785    
786                            BitstreamPadAlways(&bs);
787                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
788                          pFrame->intra = 0;                          pFrame->intra = 4;
789    
790                          if (input_valid)                          if (input_valid)
791                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
792    
793                            emms();
794    
795                          return XVID_ERR_OK;                          return XVID_ERR_OK;
796                  }                  }
797          }          }
# Line 786  Line 802 
802          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->bframenum_dx50bvop != -1)
803          {          {
804    
805                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
806                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
807    
808                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
809                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
810                  }                  }
811    
# Line 801  Line 817 
817    
818          } else if (input_valid) {          } else if (input_valid) {
819    
820                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
821    
822                  start_timer();                  start_timer();
823                  if (image_input                  if (image_input
824                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
825                          pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
826                    {
827                            emms();
828                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
829                    }
830                  stop_conv_timer();                  stop_conv_timer();
831    
832                  // queue input frame, and dequue next image                  // queue input frame, and dequue next image
# Line 824  Line 843 
843    
844          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
845    
846                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
847    
848                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
849                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
850                  pEnc->queue_size--;                  pEnc->queue_size--;
851    
852          } else if (BitstreamPos(&bs) == 0) {          } else {
853    
854                    /* if nothing was encoded, write an 'ignore this frame' flag
855                       to the bitstream */
856    
857                    if (BitstreamPos(&bs) == 0) {
858    
859                  DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
860                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
861                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
862    
863                  pFrame->intra = 0;                  //      BitstreamPutBits(&bs, 0x7f, 8);
864                            pFrame->intra = 5;
865                  BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP                  }
                 BitstreamPad(&bs);  
                 pFrame->length = BitstreamLength(&bs);  
   
                 return XVID_ERR_OK;  
   
         } else {  
866    
867                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
868                    emms();
869                  return XVID_ERR_OK;                  return XVID_ERR_OK;
870          }          }
871    
872          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
873    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
874          emms();          emms();
875    
876          // only inc frame num, adapt quant, etc. if we havent seen it before          // only inc frame num, adapt quant, etc. if we havent seen it before
877          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
878          {          {
879                    mode = intra2coding_type(pFrame->intra);
880                  if (pFrame->quant == 0)                  if (pFrame->quant == 0)
881                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
882                  else                  else
# Line 880  Line 895 
895                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
896                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
897    
                 pEnc->current->seconds = pEnc->mbParam.m_seconds;  
                 pEnc->current->ticks = pEnc->mbParam.m_ticks;  
   
898                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
899    
900  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
# Line 892  Line 904 
904    
905                  emms();                  emms();
906    
907                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
908                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
909                                  "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);                                  "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
910                  }                  }
911    
912          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 936  Line 948 
948          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
950           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
951            pEnc->iFrameNum++;
952    
953            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
954                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
955                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
956            {
957                    mode = I_VOP;
958            }else{
959                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
960                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
961                                            (mode < 0) ? pEnc->iFrameNum : 0,
962                                            bframes_count++);
963            }
964    
965          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (mode == I_VOP) {
                 (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&  
                  pEnc->iFrameNum >= pEnc->iMaxKeyInterval)  
                 || image_mad(&pEnc->reference->image, &pEnc->current->image,  
                                          pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                          pEnc->mbParam.height) > 30) {  
966                  /*                  /*
967                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
968                   */                   */
969                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
970                            pEnc->mbParam.m_quarterpel = 1;
971                    else
972                            pEnc->mbParam.m_quarterpel = 0;
973    
974                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
975    
976                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
977                            if (pFrame->quant_intra_matrix != NULL)
978                                    set_intra_matrix(pFrame->quant_intra_matrix);
979                            if (pFrame->quant_inter_matrix != NULL)
980                                    set_inter_matrix(pFrame->quant_inter_matrix);
981                    }
982    
983    
984                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
985                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
986                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
987    
988                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
989                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
990                  }                  }
991    
992                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
993    
994                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
995    
996                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
997                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
998    
999                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1000                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1001                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1002                          }                          }
1003                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
1004                            bframes_count = 0;
1005    
1006                          pFrame->intra = 0;                          pFrame->intra = 0;
1007    
1008                  } else {                  } else {
1009    
1010                          FrameCodeI(pEnc, &bs, &bits);                          FrameCodeI(pEnc, &bs, &bits);
1011                            bframes_count = 0;
1012                          pFrame->intra = 1;                          pFrame->intra = 1;
1013    
1014                          pEnc->bframenum_dx50bvop = -1;                          pEnc->bframenum_dx50bvop = -1;
# Line 981  Line 1016 
1016    
1017                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1018    
1019                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1020                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1021                          input_valid = 0;                          input_valid = 0;
1022                          goto ipvop_loop;                          goto ipvop_loop;
1023                  }                  }
# Line 991  Line 1026 
1026                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1027                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1028                   */                   */
1029          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1030                  /*                  /*
1031                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1032                   */                   */
# Line 1000  Line 1035 
1035                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1036                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1037    
1038                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1039                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1040                  }                  }
1041    
1042                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1043                    bframes_count = 0;
1044                  pFrame->intra = 0;                  pFrame->intra = 0;
1045                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1046    
1047                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1048                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1049                          input_valid = 0;                          input_valid = 0;
1050                          goto ipvop_loop;                          goto ipvop_loop;
1051                  }                  }
1052    
1053          } else {          } else {        /* mode == B_VOP */
1054                  /*                  /*
1055                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1056                   */                   */
1057    
1058                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1059                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1060                  }                  }
1061    
1062                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1063                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1064                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1065                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1066                  } else {                  } else {
1067                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1068                  }                  }
1069    
1070                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
1071                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
1072                    else if (pEnc->current->quant > 31)
                 if (pEnc->current->quant > 31)  
1073                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
1074    
   
1075                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1076                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1077                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1078    
   
   
1079                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1080                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1081                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1082    
1083                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1084    
1085                  pFrame->intra = 0;  // bframe report by koepi
1086                    pFrame->intra = 2;
1087                  pFrame->length = 0;                  pFrame->length = 0;
1088    
1089                  input_valid = 0;                  input_valid = 0;
1090                  goto bvop_loop;                  goto bvop_loop;
1091          }          }
1092    
1093          pEnc->iFrameNum++;          BitstreamPadAlways(&bs);
   
         BitstreamPad(&bs);  
1094          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1095    
1096          if (pResult) {          if (pResult) {
1097                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1098                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1099                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1100                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1101                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1102          }          }
1103    
1104          emms();          emms();
# Line 1077  Line 1109 
1109                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1110                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1111    
1112          snprintf(temp, 127, "PSNR: %f\n", psnr);          printf("PSNR: %f\n", psnr);
1113          DEBUG(temp);  //      DEBUG(temp);
1114  #endif  #endif
1115    
1116          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
# Line 1086  Line 1118 
1118                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1119          }          }
1120    
   
1121          stop_global_timer();          stop_global_timer();
1122          write_timer();          write_timer();
1123    
1124            emms();
1125          return XVID_ERR_OK;          return XVID_ERR_OK;
1126  }  }
1127    
 #endif  
   
1128    
1129    
1130  /*****************************************************************************  /*****************************************************************************
# Line 1128  Line 1158 
1158          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1159          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1160    
1161          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1162    
1163          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1164          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
         pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
1165          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1166    
1167            inc_frame_num(pEnc);
1168    
1169            /* disable alternate scan flag if interlacing is not enabled */
1170            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1171                    !(pEnc->current->global_flags & XVID_INTERLACING))
1172            {
1173                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1174            }
1175    
1176          start_timer();          start_timer();
1177          if (image_input          if (image_input
1178                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1179                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1180                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1181          stop_conv_timer();          stop_conv_timer();
1182    
# Line 1158  Line 1195 
1195                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1196          }          }
1197    
1198            if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1199                    pEnc->mbParam.m_quarterpel = 1;
1200            else
1201                    pEnc->mbParam.m_quarterpel = 0;
1202    
1203          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1204                  int *temp_dquants =                  int *temp_dquants =
1205                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 1219  Line 1261 
1261    
1262          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1263                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == 0)
1264                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1265                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1266                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1267                  } else {                  } else {
1268                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 1234  Line 1276 
1276    
1277          }          }
1278    
1279          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1280          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1281          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1282          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1283    
1284          if (pResult) {          if (pResult) {
1285                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1286                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1287                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1288                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1289                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1290          }          }
1291    
1292          emms();          emms();
# Line 1260  Line 1302 
1302                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1303    
1304          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1305          DEBUG(temp);  //      DEBUG(temp);
1306  #endif  #endif
1307    
         inc_frame_num(pEnc);  
1308          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1309    
1310          stop_global_timer();          stop_global_timer();
# Line 1428  Line 1469 
1469    
1470          if (intra) {          if (intra) {
1471                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1472                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1473                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1474                  }                  }
1475                  return;                  return;
# Line 1503  Line 1544 
1544                     Bitstream * bs,                     Bitstream * bs,
1545                     uint32_t * pBits)                     uint32_t * pBits)
1546  {  {
1547            int mb_width = pEnc->mbParam.mb_width;
1548            int mb_height = pEnc->mbParam.mb_height;
1549    
1550          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1551          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1552    
1553          uint16_t x, y;          uint16_t x, y;
1554    
1555            if ((pEnc->current->global_flags & XVID_REDUCED))
1556            {
1557                    mb_width = (pEnc->mbParam.width + 31) / 32;
1558                    mb_height = (pEnc->mbParam.height + 31) / 32;
1559    
1560                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1561                    /* XXX: setedges is overkill */
1562                    start_timer();
1563                    image_setedges(&pEnc->current->image,
1564                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1565                            pEnc->mbParam.width, pEnc->mbParam.height);
1566                    stop_edges_timer();
1567            }
1568    
1569          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1570          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1571          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1572            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1573          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1574    
1575          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1576  #ifdef BFRAMES  
1577  #define DIVX501B481P "DivX501b481p"          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1578          if ((pEnc->global & XVID_GLOBAL_PACKED)) {  
1579                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));          BitstreamPadAlways(bs);
         }  
 #endif  
1580          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1581    
1582          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1583    
1584          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1585          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1586          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1587    
1588          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1589                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1590                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1591                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1592    
# Line 1549  Line 1605 
1605                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1606                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1607                          }                          }
1608                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1609                          stop_coding_timer();                          stop_coding_timer();
1610                  }                  }
1611    
1612            if ((pEnc->current->global_flags & XVID_REDUCED))
1613            {
1614                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1615                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1616                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1617            }
1618          emms();          emms();
1619    
1620          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1621          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1622          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1623    
1624          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
# Line 1570  Line 1630 
1630    
1631    
1632  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1633  #define BFRAME_SKIP_THRESHHOLD 16  #define BFRAME_SKIP_THRESHHOLD 30
1634    
1635    
1636    /* FrameCodeP also handles S(GMC)-VOPs */
1637  static int  static int
1638  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1639                     Bitstream * bs,                     Bitstream * bs,
# Line 1584  Line 1646 
1646          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1647          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1648    
1649            int mb_width = pEnc->mbParam.mb_width;
1650            int mb_height = pEnc->mbParam.mb_height;
1651    
1652          int iLimit;          int iLimit;
1653          int x, y, k;          int x, y, k;
1654          int iSearchRange;          int iSearchRange;
1655          int bIntra;          int bIntra, skip_possible;
1656    
1657          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1658          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1659    
1660            if ((pEnc->current->global_flags & XVID_REDUCED))
1661            {
1662                    mb_width = (pEnc->mbParam.width + 31) / 32;
1663                    mb_height = (pEnc->mbParam.height + 31) / 32;
1664            }
1665    
1666    
1667          start_timer();          start_timer();
1668          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1669                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1670          stop_edges_timer();          stop_edges_timer();
1671    
1672          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1673          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1674            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1675          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1676    
1677          if (!force_inter)          if (!force_inter)
1678                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1679          else          else
1680                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1681    
1682          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1683                  start_timer();                  start_timer();
1684                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1685                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1686                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1687                                                      pEnc->mbParam.m_quarterpel,
1688                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1689                  stop_inter_timer();                  stop_inter_timer();
1690          }          }
1691    
1692            pEnc->current->coding_type = P_VOP;
1693    
1694          start_timer();          start_timer();
1695          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1696                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
         } else {  
   
 #ifdef _SMP  
         if (pEnc->mbParam.num_threads > 1)  
                 bIntra =  
                         SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                                                  &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                                  iLimit);  
1697          else          else
 #endif  
1698                  bIntra =                  bIntra =
1699                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1700                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1701                           iLimit);                           iLimit);
1702    
         }  
1703          stop_motion_timer();          stop_motion_timer();
1704    
1705          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1706    
1707          pEnc->current->coding_type = P_VOP;          if ( ( pEnc->current->global_flags & XVID_GMC )
1708                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1709            {
1710                    pEnc->current->coding_type = S_VOP;
1711    
1712                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1713                                            pEnc->mbParam.width, pEnc->mbParam.height,
1714                                            &pEnc->current->gmc_data);
1715    
1716                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1717                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1718                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1719                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1720                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1721    
1722            }
1723    
1724            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1725          if (vol_header)          if (vol_header)
1726                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1727                    BitstreamPadAlways(bs);
1728            }
1729    
1730          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1731    
1732          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1733    
1734          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1735          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;  
1736    
1737          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1738                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1739                    for (x = 0; x < mb_width; x++) {
1740                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1741                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1742    
1743    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1744    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1745    
1746                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1747    
1748                          if (!bIntra) {                          if (bIntra) {
1749                                    CodeIntraMB(pEnc, pMB);
1750                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1751                                                                      dct_codes, qcoeff);
1752    
1753                                    start_timer();
1754                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1755                                    stop_prediction_timer();
1756    
1757                                    pEnc->current->sStat.kblks++;
1758    
1759                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1760                                    stop_coding_timer();
1761                                    continue;
1762                            }
1763    
1764                            if (pEnc->current->coding_type == S_VOP) {
1765    
1766                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1767                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1768                                            pEnc->mbParam.edged_width, 65536);
1769    
1770                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1771    
1772                                            if (pEnc->mbParam.m_quarterpel)
1773                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1774                                            else
1775                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1776    
1777                                            pMB->mode = MODE_INTER;
1778                                            pMB->mcsel = 1;
1779                                            pMB->sad16 = iSAD;
1780                                    } else {
1781                                            pMB->mcsel = 0;
1782                                    }
1783                            } else {
1784                                    pMB->mcsel = 0; /* just a precaution */
1785                            }
1786    
1787                                  start_timer();                                  start_timer();
1788                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1789                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1790                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1791                                                                     &pEnc->current->image,
1792                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1793                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1794                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1795                                                                     pEnc->mbParam.m_quarterpel,
1796                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1797                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1798    
1799                                  stop_comp_timer();                                  stop_comp_timer();
1800    
1801                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1689  Line 1812 
1812    
1813                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1814    
1815                                  pMB->cbp =                          if (pMB->mode != MODE_NOT_CODED)
1816                            {       pMB->cbp =
1817                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1818                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1819                          }                          }
1820    
1821                          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 ||  
1822                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1823                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1824                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1825                          }  else {                          }  else {
1826                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1827                          }                          }
1828    
1829                          start_timer();                          start_timer();
1830    
1831                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1832    
1833                          if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1834                                  pMB->mvs[0].y == 0) {                                                          (pMB->dquant == NO_CHANGE);
1835    
1836                            if (pEnc->current->coding_type == S_VOP)
1837                                    skip_possible &= (pMB->mcsel == 1);
1838                            else if (pEnc->current->coding_type == P_VOP) {
1839                                    if (pEnc->mbParam.m_quarterpel)
1840                                            skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1841                                    else
1842                                            skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
1843                            }
1844    
1845                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1846    
1847  /* This is a candidate for SKIPping, but check intermediate B-frames first */  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1848    
1849  #ifdef BFRAMES                                  if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1850                                  int iSAD=BFRAME_SKIP_THRESHHOLD;                                  {
1851                                  int bSkip=1;                                  int bSkip=1;
1852    
1853                                  for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                  for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1854                                  {                                  {
1855                                                    int iSAD;
1856                                          iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                          iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1857                                                                  pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                                  pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1858                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1859                                          if (iSAD >= BFRAME_SKIP_THRESHHOLD)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1860                                          {       bSkip = 0;                                          {       bSkip = 0;
1861                                                  break;                                                  break;
1862                                          }                                          }
1863                                  }                                  }
1864                                  if (!bSkip)  
1865                                  {                                          if (!bSkip) {   /* no SKIP, but trivial block */
1866                                                    if(pEnc->mbParam.m_quarterpel) {
1867                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1868                                                            pMB->pmvs[0].x = - predMV.x;
1869                                                            pMB->pmvs[0].y = - predMV.y;
1870                                                    }
1871                                                    else {
1872                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1873                                                            pMB->pmvs[0].x = - predMV.x;
1874                                                            pMB->pmvs[0].y = - predMV.y;
1875                                                    }
1876                                                    pMB->mode = MODE_INTER;
1877                                                    pMB->cbp = 0;
1878                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1879                                                    stop_coding_timer();
1880    
1881                                                    continue;       /* next MB */
1882                                            }
1883                                    }
1884                                    /* do SKIP */
1885    
1886                                    pMB->mode = MODE_NOT_CODED;
1887                                    MBSkip(bs);
1888                                    stop_coding_timer();
1889                                    continue;       /* next MB */
1890                            }
1891                            /* ordinary case: normal coded INTER/INTER4V block */
1892    
1893                                          if (pEnc->current->global_flags & XVID_GREYSCALE)                                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1894                                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1895                                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */                                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1896                                                  qcoeff[5*64+0]=0;                                                  qcoeff[5*64+0]=0;
1897                                          }                                          }
                                         MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
                                         pMB->cbp = 0x80;                /* trick! so cbp!=0, but still nothing is written to bs */  
                                 }  
                                 else  
                                         MBSkip(bs);  
1898    
1899  #else                          if(pEnc->mbParam.m_quarterpel) {
1900                                          MBSkip(bs);     /* without B-frames, no precautions are needed */                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1901                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1902                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1903                                    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);
1904                            } else {
1905                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1906                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1907                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1908                                    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);
1909                            }
1910    
 #endif  
1911    
1912                            if (pMB->mode == MODE_INTER4V)
1913                            {       int k;
1914                                    for (k=1;k<4;k++)
1915                                    {
1916                                            if(pEnc->mbParam.m_quarterpel) {
1917                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1918                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1919                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1920                                    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);
1921                          } else {                          } else {
1922                                  if (pEnc->current->global_flags & XVID_GREYSCALE)                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1923                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1924                                          qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1925                                          qcoeff[5*64+0]=0;                                  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);
1926                                            }
1927    
1928                                  }                                  }
                                 MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1929                          }                          }
1930    
1931                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1932                          stop_coding_timer();                          stop_coding_timer();
1933    
1934                    }
1935                  }                  }
1936    
1937            if ((pEnc->current->global_flags & XVID_REDUCED))
1938            {
1939                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1940                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1941                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1942          }          }
1943    
1944          emms();          emms();
# Line 1772  Line 1947 
1947                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1948          }          }
1949    
1950          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
1951                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
1952    
1953          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
1954    
1955          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1956    
1957          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1958                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
1959          {          {
1960                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1961                  iSearchRange *= 2;                  iSearchRange *= 2;
1962          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1963                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1964                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1965                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
1966          {          {
1967                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1968                  iSearchRange /= 2;                  iSearchRange /= 2;
1969          }          }
1970    
1971          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1972    
 #ifdef BFRAMES  
1973          /* frame drop code */          /* frame drop code */
1974          // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
1975          if (pEnc->sStat.kblks + pEnc->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
1976                  (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
1977          {          {
1978                  pEnc->sStat.kblks = pEnc->sStat.mblks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
1979                  pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;                  pEnc->current->sStat.ublks = mb_width * mb_height;
1980    
1981                  BitstreamReset(bs);                  BitstreamReset(bs);
1982    
1983                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1984                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1985    
1986                  // copy reference frame details into the current frame                  // copy reference frame details into the current frame
1987                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
1988                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
1989                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
1990                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1991                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1992                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1993                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1994                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1995            }
1996    
1997            /* XXX: debug
1998            {
1999                    char s[100];
2000                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2001                    image_dump_yuvpgm(&pEnc->current->image,
2002                            pEnc->mbParam.edged_width,
2003                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2004    
2005                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2006                    image_dump_yuvpgm(&pEnc->reference->image,
2007                            pEnc->mbParam.edged_width,
2008                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2009          }          }
2010  #endif          */
2011    
         *pBits = BitstreamPos(bs) - *pBits;  
2012    
2013  #ifdef BFRAMES          *pBits = BitstreamPos(bs) - *pBits;
         pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %  
                         (int32_t)pEnc->mbParam.fbase;  
         pEnc->last_pframe = pEnc->current->ticks;  
 #endif  
2014    
2015          return 0;                                       // inter          return 0;                                       // inter
2016  }  }
2017    
2018    
 #ifdef BFRAMES  
2019  static void  static void
2020  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2021                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2022                     Bitstream * bs,                     Bitstream * bs,
2023                     uint32_t * pBits)                     uint32_t * pBits)
2024  {  {
2025          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2026          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2027          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
2028    
2029          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2030          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
# Line 1854  Line 2036 
2036                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2037          }          }
2038    
2039            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2040    
2041          if (!first){          if (!first){
2042                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2043          }          }
2044  #endif  #endif
2045    
2046            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2047    
2048          // forward          // forward
2049          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2050                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2051                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2052          start_timer();          start_timer();
2053          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,
2054                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2055                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2056          stop_inter_timer();          stop_inter_timer();
2057    
2058          // backward          // backward
2059          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2060                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2061                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2062          start_timer();          start_timer();
2063          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2064                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2065                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2066          stop_inter_timer();          stop_inter_timer();
2067    
2068          start_timer();          start_timer();
2069    
2070          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2071                  ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
2072                                                  pEnc->time_pp,                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
2073                                                  pEnc->reference->mbs, f_ref,                                                  pEnc->reference->mbs, f_ref,
2074                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2075                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2076                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
2077    
2078    
# Line 1899  Line 2084 
2084             } */             } */
2085    
2086          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2087    
2088            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2089          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
2090    
2091          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2092    
2093          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2094          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2095          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2096          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2097    
2098    
2099          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;  
   
2100                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2101                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2102                                  &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];  
2103    
2104                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2105                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2106                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
   
                                 mb->cbp = 0;  
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
2107                                  continue;                                  continue;
2108                          }                          }
2109    
2110                            if (mb->mode != MODE_DIRECT_NONE_MV) {
2111                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2112                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2113                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2114                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
2115                                                                           dct_codes);                                                                           dct_codes);
2116    
2117                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2118                          mb->quant = frame->quant;                          mb->quant = frame->quant;
2119    
2120                          mb->cbp =                          mb->cbp =
2121                                  MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);                                  MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
2122    
2123                          if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                          if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2124                                  && (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2125                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
2126                          }                          }
   
 /* update predictors for forward and backward vectors */  
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {  
                                 mb->pmvs[0].x = mb->mvs[0].x - forward.x;  
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
                         }  
   
                         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;  
2127                          }                          }
2128    
 //                      DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y);  
   
2129  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2130          BFRAME_DEBUG          BFRAME_DEBUG
2131  #endif  #endif
2132                          start_timer();                          start_timer();
2133                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2134                                                   &pEnc->sStat);                                                   &frame->sStat, direction);
2135                          stop_coding_timer();                          stop_coding_timer();
2136                  }                  }
2137          }          }
# Line 1993  Line 2150 
2150          }          }
2151  #endif  #endif
2152  }  }
 #endif  
   
   
 /*      in case internal output is needed somewhere... */  
 /*      {  
         FILE *filehandle;  
         filehandle=fopen("last-b.pgm","wb");  
         if (filehandle)  
         {  
                 fprintf(filehandle,"P5\n\n");           //  
                 fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);  
                 fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);  
                 fclose(filehandle);  
                 }  
         }  
 */  

Legend:
Removed from v.1.76  
changed lines
  Added in v.1.76.2.37

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