[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/xvidcore/src/encoder.c revision 252, Sun Jun 30 10:46:29 2002 UTC branches/dev-api-3/xvidcore/src/encoder.c revision 658, Tue Nov 19 13:04:35 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37   *      20.06.2002 bframe patch   *      20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
41   *   *
42   *  $Id: encoder.c,v 1.51 2002-06-30 10:46:29 suxen_drol Exp $   *  $Id: encoder.c,v 1.76.2.19 2002-11-19 13:03:57 syskin Exp $
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
46    
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <stdio.h>  #include <stdio.h>
49  #include <math.h>  #include <math.h>
# Line 51  Line 54 
54  #include "global.h"  #include "global.h"
55  #include "utils/timer.h"  #include "utils/timer.h"
56  #include "image/image.h"  #include "image/image.h"
 #ifdef BFRAMES  
57  #include "image/font.h"  #include "image/font.h"
58  #endif  #include "motion/sad.h"
59  #include "motion/motion.h"  #include "motion/motion.h"
60  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
61  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 87  Line 89 
89                                            bool force_inter,                                            bool force_inter,
90                                            bool vol_header);                                            bool vol_header);
91    
 #ifdef BFRAMES  
92  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
93                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
94                                             Bitstream * bs,                                             Bitstream * bs,
95                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
96    
97  /*****************************************************************************  /*****************************************************************************
98   * Local data   * Local data
# Line 107  Line 107 
107  };  };
108    
109    
 static void __inline  
 image_null(IMAGE * image)  
 {  
         image->y = image->u = image->v = NULL;  
 }  
   
   
110  /*****************************************************************************  /*****************************************************************************
111   * Encoder creation   * Encoder creation
112   *   *
# Line 204  Line 197 
197    
198          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
199    
200          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
201                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
202    
   
203          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
204          if (pEnc == NULL)          if (pEnc == NULL)
205                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
# Line 266  Line 258 
258  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
259          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
260  #endif  #endif
261  #ifdef BFRAMES  
262          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
263          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
264          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
265  #endif  
266          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
267          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
268          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 285  Line 277 
277                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
278                  goto xvid_err_memory3;                  goto xvid_err_memory3;
279  #endif  #endif
280  #ifdef BFRAMES  
281          if (image_create          if (image_create
282                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
283                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 298  Line 290 
290                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
291                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
292                  goto xvid_err_memory3;                  goto xvid_err_memory3;
293  #endif  
294          if (image_create          if (image_create
295                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
296                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 331  Line 323 
323    
324    
325          /* B Frames specific init */          /* B Frames specific init */
 #ifdef BFRAMES  
326    
327          pEnc->global = pParam->global;          pEnc->global = pParam->global;
328          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
329          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
330            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
331          pEnc->bframes = NULL;          pEnc->bframes = NULL;
332    
333          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 409  Line 401 
401          pEnc->queue_tail = 0;          pEnc->queue_tail = 0;
402          pEnc->queue_size = 0;          pEnc->queue_size = 0;
403    
404            pEnc->mbParam.m_stamp = 0;
405    
         pEnc->mbParam.m_seconds = 0;  
         pEnc->mbParam.m_ticks = 0;  
406          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
407  #endif          pEnc->current->stamp = 0;
408            pEnc->reference->stamp = 0;
409    
410          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
411    
# Line 432  Line 424 
424          /*          /*
425           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
426           */           */
427  #ifdef BFRAMES  
428    xvid_err_memory5:    xvid_err_memory5:
429    
430    
# Line 466  Line 458 
458                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
459          }          }
460    
 #endif  
   
461    xvid_err_memory3:    xvid_err_memory3:
462  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
463          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
464                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
465  #endif  #endif
466    
 #ifdef BFRAMES  
467          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
468                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
469          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
470                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
471          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
472                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
473    
474          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
475                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 526  Line 514 
514  int  int
515  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
516  {  {
 #ifdef BFRAMES  
517          int i;          int i;
 #endif  
518    
519          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
520    
521          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
522          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
523    
524                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 563  Line 548 
548                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
549    
550          }          }
 #endif  
551    
552          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
553    
# Line 581  Line 565 
565                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
566          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
567                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
568  #ifdef BFRAMES  
569          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
570                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
571          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
573          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
574                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
575  #endif  
576  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
577          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
578                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 608  Line 592 
592  }  }
593    
594    
595  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
596  {  {
597          pEnc->iFrameNum++;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
598          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
   
         pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;  
         pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;  
599  }  }
 #endif  
600    
601    
602  #ifdef BFRAMES  static __inline void
603  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
604  {  {
605          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
606          {          {
# Line 637  Line 616 
616          start_timer();          start_timer();
617          if (image_input          if (image_input
618                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
619                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
620                  return;                  return;
621          stop_conv_timer();          stop_conv_timer();
622    
623          pEnc->queue_size++;          pEnc->queue_size++;
624          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
625  }  }
626  #endif  
627    static __inline void
628    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
629    {
630    
631                    pCur->ticks = (int32_t)pCur->stamp % time_base;
632                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
633    
634                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
635    
636    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
637                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
638                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
639                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
640                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
641                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
642    
643    */
644    }
645    
646    
647    
648    
649    
 #ifdef BFRAMES  
650  /*****************************************************************************  /*****************************************************************************
651   * IPB frame encoder entry point   * IPB frame encoder entry point
652   *   *
# Line 664  Line 663 
663  {  {
664          uint16_t x, y;          uint16_t x, y;
665          Bitstream bs;          Bitstream bs;
666          uint32_t bits;          uint32_t bits, mode;
667    
668          int input_valid = 1;          int input_valid = 1;
669            int bframes_count = 0;
670    
671  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
672          float psnr;          float psnr;
# Line 707  Line 707 
707                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
708    
709                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
710                            bframes_count = 0;
711    
712                          BitstreamPad(&bs);                          BitstreamPad(&bs);
713                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
714                          pFrame->intra = 0;                          pFrame->intra = 0;
715    
716                            emms();
717    
718                          return XVID_ERR_OK;                          return XVID_ERR_OK;
719                  }                  }
720    
# Line 730  Line 733 
733                  if (input_valid)                  if (input_valid)
734                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
735    
736                    emms();
737    
738                  return XVID_ERR_OK;                  return XVID_ERR_OK;
739          }          }
740    
741          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
742                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
743    
744                    /* write an empty marker to the bitstream.
745    
746                       for divx5 decoder compatibility, this marker must consist
747                       of a not-coded p-vop, with a time_base of zero, and time_increment
748                       indentical to the future-referece frame.
749                    */
750    
751                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
752                            int tmp;
753    
754                          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",
755                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
756                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
757    
                         BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);  
758                          BitstreamPad(&bs);                          BitstreamPad(&bs);
759                          BitstreamPutBits(&bs, 0x7f, 8);  
760                            tmp = pEnc->current->seconds;
761                            pEnc->current->seconds = 0; /* force time_base = 0 */
762                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
763                            pEnc->current->seconds = tmp;
764    
765                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
766                          pFrame->intra = 0;                          pFrame->intra = 0;
# Line 752  Line 768 
768                          if (input_valid)                          if (input_valid)
769                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
770    
771                            emms();
772    
773                          return XVID_ERR_OK;                          return XVID_ERR_OK;
774                  }                  }
775          }          }
# Line 782  Line 800 
800                  start_timer();                  start_timer();
801                  if (image_input                  if (image_input
802                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
803                          pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
804                    {
805                            emms();
806                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
807                    }
808                  stop_conv_timer();                  stop_conv_timer();
809    
810                  // queue input frame, and dequue next image                  // queue input frame, and dequue next image
# Line 806  Line 827 
827                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
828                  pEnc->queue_size--;                  pEnc->queue_size--;
829    
830          } else if (BitstreamPos(&bs) == 0) {          } else {
831    
832                    /* if nothing was encoded, write an 'ignore this frame' flag
833                       to the bitstream */
834    
835                    if (BitstreamPos(&bs) == 0) {
836    
837                  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",
838                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
839                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
840    
                 pFrame->intra = 0;  
   
841                  BitstreamPutBits(&bs, 0x7f, 8);                  BitstreamPutBits(&bs, 0x7f, 8);
842                  BitstreamPad(&bs);                          pFrame->intra = 0;
843                  pFrame->length = BitstreamLength(&bs);                  }
   
                 return XVID_ERR_OK;  
   
         } else {  
844    
845                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
846                    emms();
847                  return XVID_ERR_OK;                  return XVID_ERR_OK;
848          }          }
849    
850          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
851    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
852          emms();          emms();
853    
854          // only inc frame num, adapt quant, etc. if we havent seen it before          // only inc frame num, adapt quant, etc. if we havent seen it before
# Line 843  Line 859 
859                  else                  else
860                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
861    
862                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
863                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
864    
865                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
866                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
867    */
868                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
869                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
870    
# Line 856  Line 872 
872                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
873                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
874    
                 pEnc->current->seconds = pEnc->mbParam.m_seconds;  
                 pEnc->current->ticks = pEnc->mbParam.m_ticks;  
   
875                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
876    
877  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
# Line 870  Line 883 
883    
884                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
885                          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,
886                                  "%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);
887                  }                  }
888    
889          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 917  Line 930 
930          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
931                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
932                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
933                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || 2 == (mode = MEanalysis(&pEnc->reference->image, pEnc->current,
934                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,                                                                          &pEnc->mbParam, pEnc->iMaxKeyInterval,
935                                           pEnc->mbParam.height) > 30) {                                                                          (pFrame->intra < 0) ? pEnc->iFrameNum : 0,
936                                                                            bframes_count++))) {
937    
938                  /*                  /*
939                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
940                   */                   */
941                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
942                            pEnc->mbParam.m_quarterpel = 1;
943                    else
944                            pEnc->mbParam.m_quarterpel = 0;
945    
946                  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",
947                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
# Line 944  Line 963 
963                                  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");
964                          }                          }
965                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
966                            bframes_count = 0;
967    
968                          pFrame->intra = 0;                          pFrame->intra = 0;
969    
970                  } else {                  } else {
971    
972                          FrameCodeI(pEnc, &bs, &bits);                          FrameCodeI(pEnc, &bs, &bits);
973                            bframes_count = 0;
974                          pFrame->intra = 1;                          pFrame->intra = 1;
975    
976                          pEnc->bframenum_dx50bvop = -1;                          pEnc->bframenum_dx50bvop = -1;
# Line 958  Line 979 
979                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
980    
981                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
982                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
983                          input_valid = 0;                          input_valid = 0;
984                          goto ipvop_loop;                          goto ipvop_loop;
985                  }                  }
# Line 967  Line 988 
988                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
989                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
990                   */                   */
991          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
992                  /*                  /*
993                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
994                   */                   */
# Line 981  Line 1002 
1002                  }                  }
1003    
1004                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1005                    bframes_count = 0;
1006                  pFrame->intra = 0;                  pFrame->intra = 0;
1007                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1008    
1009                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1010                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1011                          input_valid = 0;                          input_valid = 0;
1012                          goto ipvop_loop;                          goto ipvop_loop;
1013                  }                  }
# Line 995  Line 1017 
1017                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1018                   */                   */
1019    
                 DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
   
1020                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1021                          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");
1022                  }                  }
# Line 1010  Line 1028 
1028                  } else {                  } else {
1029                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1030                  }                  }
1031                    if (pEnc->current->quant < 1)
1032                            pEnc->current->quant = 1;
1033    
1034                    if (pEnc->current->quant > 31)
1035                            pEnc->current->quant = 31;
1036    
1037    
1038                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1039                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1040                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1041    
1042    
1043    
1044                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1045                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 1024  Line 1054 
1054                  goto bvop_loop;                  goto bvop_loop;
1055          }          }
1056    
1057            pEnc->iFrameNum++;
1058    
1059          BitstreamPad(&bs);          BitstreamPad(&bs);
1060          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1061    
# Line 1052  Line 1084 
1084                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1085          }          }
1086    
   
1087          stop_global_timer();          stop_global_timer();
1088          write_timer();          write_timer();
1089    
1090            emms();
1091          return XVID_ERR_OK;          return XVID_ERR_OK;
1092  }  }
1093    
 #endif  
   
1094    
1095    
1096  /*****************************************************************************  /*****************************************************************************
# Line 1098  Line 1128 
1128    
1129          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1130          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
 #ifdef BFRAMES  
         pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
 #endif  
1131          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1132    
1133            inc_frame_num(pEnc);
1134    
1135            /* disable alternate scan flag if interlacing is not enabled */
1136            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1137                    !(pEnc->current->global_flags & XVID_INTERLACING))
1138            {
1139                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1140            }
1141    
1142          start_timer();          start_timer();
1143          if (image_input          if (image_input
1144                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1145                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1146                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1147          stop_conv_timer();          stop_conv_timer();
1148    
# Line 1126  Line 1161 
1161                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1162          }          }
1163    
1164            if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1165                    pEnc->mbParam.m_quarterpel = 1;
1166            else
1167                    pEnc->mbParam.m_quarterpel = 0;
1168    
1169          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1170                  int *temp_dquants =                  int *temp_dquants =
1171                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 1231  Line 1271 
1271          DEBUG(temp);          DEBUG(temp);
1272  #endif  #endif
1273    
 #ifdef BFRAMES  
         inc_frame_num(pEnc);  
 #else  
1274          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1275    
1276          stop_global_timer();          stop_global_timer();
1277          write_timer();          write_timer();
# Line 1312  Line 1347 
1347                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1348                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1349                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1350                          VECTOR pred[4];                          VECTOR pred;
1351                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1352                          int vec;                          int vec;
1353    
1354                          pMB->mode =                          pMB->mode =
# Line 1334  Line 1368 
1368                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1369                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1370    
1371                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
                                                         0, pred, dummy);  
1372    
1373                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1374                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1375                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1376                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1377                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1378                                  }                                  }
1379                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1380                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1354  Line 1387 
1387                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1388                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1389    
1390                                          get_pmvdata(pEnc->current->mbs, x, y,                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
                                                                 pEnc->mbParam.mb_width, vec, pred, dummy);  
1391    
1392                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1393                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1394                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1395                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1396                                  }                                  }
1397                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1398                          {                          {
# Line 1487  Line 1519 
1519          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1520          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1521          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1522            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1523          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1524    
1525          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1526  #ifdef BFRAMES  
1527  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1528          if ((pEnc->global & XVID_GLOBAL_PACKED)) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1529                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1530          }          }
1531  #endif  
1532    #define XVID_ID "XviD" XVID_BS_VERSION
1533            BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID));
1534    
1535            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1536          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1537    
1538          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1519  Line 1556 
1556                          stop_prediction_timer();                          stop_prediction_timer();
1557    
1558                          start_timer();                          start_timer();
1559                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1560                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1561                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1562                                    qcoeff[5*64+0]=0;
1563                            }
1564                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1565                          stop_coding_timer();                          stop_coding_timer();
1566                  }                  }
# Line 1540  Line 1582 
1582    
1583    
1584  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1585    #define BFRAME_SKIP_THRESHHOLD 30
1586    
1587  static int  static int
1588  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1554  Line 1597 
1597          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1598    
1599          int iLimit;          int iLimit;
1600          uint32_t x, y;          int x, y, k;
1601          int iSearchRange;          int iSearchRange;
1602          int bIntra;          int bIntra, skip_possible;
1603    
1604          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1605          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1606    
1607          start_timer();          start_timer();
1608          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1609                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1610          stop_edges_timer();          stop_edges_timer();
1611    
1612          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1613          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1614            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1615          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1616    
1617          if (!force_inter)          if (!force_inter)
# Line 1583  Line 1626 
1626                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1627                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1628                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1629                                                      pEnc->mbParam.m_quarterpel,
1630                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1631                  stop_inter_timer();                  stop_inter_timer();
1632          }          }
1633    
1634            if (pEnc->current->global_flags & XVID_GMC) {
1635    //              printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);
1636                    DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);
1637                    pEnc->current->coding_type = S_VOP;
1638            } else
1639                    pEnc->current->coding_type = P_VOP;
1640    
1641          start_timer();          start_timer();
1642          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1643                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1644            if (bIntra == 0) {
1645                            pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current);
1646                            MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1647                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1648                    }
1649    
1650          } else {          } else {
1651    
1652                  bIntra =                  bIntra =
1653                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1654                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1598  Line 1656 
1656          }          }
1657          stop_motion_timer();          stop_motion_timer();
1658    
1659          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1660                  return FrameCodeI(pEnc, bs, pBits);  
1661          }          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )
1662                            pEnc->current->coding_type = P_VOP;             /* no global motion -> no GMC */
1663    
         pEnc->current->coding_type = P_VOP;  
1664    
1665          if (vol_header)          if (vol_header)
1666                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1667    
1668    
1669            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1670          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1671    
1672          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1673    
1674          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1675          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1676    
1677          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1631  Line 1689 
1689                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1690                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1691                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1692                                                                             pEnc->mbParam.m_quarterpel,
1693                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1694                                  stop_comp_timer();                                  stop_comp_timer();
1695    
# Line 1648  Line 1707 
1707    
1708                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1709    
1710                                    if (pMB->mode != MODE_NOT_CODED)
1711                                  pMB->cbp =                                  pMB->cbp =
1712                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1713                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1672  Line 1732 
1732                          }                          }
1733    
1734                          start_timer();                          start_timer();
1735    
1736                            /* Finished processing the MB, now check if to CODE or SKIP */
1737    
1738                            skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &
1739                                                            (pMB->dquant == NO_CHANGE);
1740    
1741                            if(pEnc->mbParam.m_quarterpel)
1742                            {       skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y);
1743                            }
1744                            else
1745                            {       skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y);
1746                            }
1747    
1748                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1749    
1750    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1751                                    int bSkip = 1;
1752    
1753                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1754                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1755                                            {
1756                                                    int iSAD;
1757                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1758                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1759                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1760                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1761                                                    {       bSkip = 0;
1762                                                            break;
1763                                                    }
1764                                            }
1765    
1766                                    if (!bSkip)
1767                                    {
1768                                            VECTOR predMV;
1769                                            if(pEnc->mbParam.m_quarterpel) {
1770                                                    predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1771                                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  /* with GMC, qmvs doesn't have to be (0,0)! */
1772                                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1773                                            }
1774                                            else {
1775                                                    predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1776                                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */
1777                                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1778                                            }
1779                                            pMB->mode = MODE_INTER;
1780                                            pMB->cbp = 0;
1781                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1782                                    }
1783                                    else
1784                                    {
1785                                            pMB->mode = MODE_NOT_CODED;
1786                                            MBSkip(bs);
1787                                    }
1788    
1789                            } else {
1790                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1791                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1792                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1793                                            qcoeff[5*64+0]=0;
1794                                    }
1795                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1796                            }
1797    
1798                          stop_coding_timer();                          stop_coding_timer();
1799                  }                  }
1800          }          }
# Line 1691  Line 1813 
1813          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1814    
1815          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1816                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
1817          {          {
1818                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1819                  iSearchRange *= 2;                  iSearchRange *= 2;
1820          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1821                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->sStat.fMvPrevSigma >= 0)
1822                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1823                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
1824          {          {
1825                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1826                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1706  Line 1828 
1828    
1829          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1830    
1831    #ifdef FRAMEDROP
1832            /* frame drop code */
1833            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1834            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1835                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1836            {
1837                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1838                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1839    
1840                    BitstreamReset(bs);
1841    
1842                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1843                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1844    
1845                    // copy reference frame details into the current frame
1846                    pEnc->current->quant = pEnc->reference->quant;
1847                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1848                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1849                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1850                    pEnc->current->fcode = pEnc->reference->fcode;
1851                    pEnc->current->bcode = pEnc->reference->bcode;
1852                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1853                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1854    
1855            }
1856    #endif
1857    
1858          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1859    
1860          return 0;                                       // inter          return 0;                                       // inter
1861  }  }
1862    
1863    
1864  #ifdef BFRAMES  static __inline void
 static void  
1865  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1866                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1867                     Bitstream * bs,                     Bitstream * bs,
# Line 1722  Line 1870 
1870          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1871          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1872          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1873    
1874          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1875          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1876    
1877    #ifdef BFRAMES_DEC_DEBUG
1878            FILE *fp;
1879            static char first=0;
1880    #define BFRAME_DEBUG    if (!first && fp){ \
1881                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1882            }
1883    
1884            if (!first){
1885                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1886            }
1887    #endif
1888    
1889            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
1890    
1891          // forward          // forward
1892          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1893                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1894                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1895          start_timer();          start_timer();
1896          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,
1897                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1898                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
1899          stop_inter_timer();          stop_inter_timer();
1900    
1901          // backward          // backward
1902          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1903                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1904                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1905          start_timer();          start_timer();
1906          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1907                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1908                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
1909          stop_inter_timer();          stop_inter_timer();
1910    
1911          start_timer();          start_timer();
1912          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,  
1913            MotionEstimationBVOP(&pEnc->mbParam, frame,
1914                    ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1915                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1916                            pEnc->reference->mbs, f_ref,
1917                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1918                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
1919                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
1920    
1921    
# Line 1765  Line 1927 
1927             } */             } */
1928    
1929          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1930    
1931            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1932          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1933    
1934          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1776  Line 1940 
1940    
1941    
1942          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;  
   
1943                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1944                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1945                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                          int direction = pEnc->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];  
1946    
1947                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1948                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1949                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
1950                                  continue;                                  continue;
1951                          }                          }
1952    
1953                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1954                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1955                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1956                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1957                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1958                                                                           dct_codes);                                                                           dct_codes);
1959    
1960                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1961                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp =  
                                 MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,  
                                                                   qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
   
1962    
1963                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  mb->cbp =
1964                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mode = 5;   // skipped  
                         }  
1965    
1966                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1967                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1968                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
1969                          }                          }
   
                         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;  
1970                          }                          }
 //                      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);  
1971    
1972    #ifdef BFRAMES_DEC_DEBUG
1973            BFRAME_DEBUG
1974    #endif
1975                          start_timer();                          start_timer();
1976                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1977                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1978                          stop_coding_timer();                          stop_coding_timer();
1979                  }                  }
1980          }          }
# Line 1842  Line 1984 
1984          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1985    
1986          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1987    
1988    #ifdef BFRAMES_DEC_DEBUG
1989            if (!first){
1990                    first=1;
1991                    if (fp)
1992                            fclose(fp);
1993  }  }
1994  #endif  #endif
1995    }
1996    
1997    
1998    /*      in case internal output is needed somewhere... */
1999    /*      {
2000            FILE *filehandle;
2001            filehandle=fopen("last-b.pgm","wb");
2002            if (filehandle)
2003            {
2004                    fprintf(filehandle,"P5\n\n");           //
2005                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
2006                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
2007                    fclose(filehandle);
2008                    }
2009            }
2010    */

Legend:
Removed from v.252  
changed lines
  Added in v.658

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