[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 313, Thu Jul 18 23:52:40 2002 UTC branches/dev-api-3/xvidcore/src/encoder.c revision 631, Thu Nov 7 10:31:03 2002 UTC
# Line 39  Line 39 
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.56 2002-07-18 23:52:40 chl Exp $   *  $Id: encoder.c,v 1.76.2.16 2002-11-07 10:28:15 suxen_drol 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 53  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 68  Line 68 
68  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
69  #include "utils/mem_align.h"  #include "utils/mem_align.h"
70    
 #ifdef _SMP  
 #include "motion/smp_motion_est.h"  
 #endif  
71  /*****************************************************************************  /*****************************************************************************
72   * Local macros   * Local macros
73   ****************************************************************************/   ****************************************************************************/
# Line 92  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 112  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 209  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);
# Line 236  Line 224 
224    
225          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
226    
 #ifdef _SMP  
         pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);  
 #endif  
   
227          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
228    
229          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 274  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 293  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 306  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 339  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 417  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 440  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 474  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 534  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 571  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 589  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 616  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 645  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 672  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    
# Line 720  Line 711 
711                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
712                          pFrame->intra = 0;                          pFrame->intra = 0;
713    
714                            emms();
715    
716                          return XVID_ERR_OK;                          return XVID_ERR_OK;
717                  }                  }
718    
# Line 738  Line 731 
731                  if (input_valid)                  if (input_valid)
732                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
733    
734                    emms();
735    
736                  return XVID_ERR_OK;                  return XVID_ERR_OK;
737          }          }
738    
739          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
740                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
741    
742                    /* write an empty marker to the bitstream.
743    
744                       for divx5 decoder compatibility, this marker must consist
745                       of a not-coded p-vop, with a time_base of zero, and time_increment
746                       indentical to the future-referece frame.
747                    */
748    
749                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
750                            int tmp;
751    
752                          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",
753                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
754                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
755    
                         BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);  
756                          BitstreamPad(&bs);                          BitstreamPad(&bs);
757                          BitstreamPutBits(&bs, 0x7f, 8);  
758                            tmp = pEnc->current->seconds;
759                            pEnc->current->seconds = 0; /* force time_base = 0 */
760                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
761                            pEnc->current->seconds = tmp;
762    
763                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
764                          pFrame->intra = 0;                          pFrame->intra = 0;
# Line 760  Line 766 
766                          if (input_valid)                          if (input_valid)
767                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
768    
769                            emms();
770    
771                          return XVID_ERR_OK;                          return XVID_ERR_OK;
772                  }                  }
773          }          }
# Line 790  Line 798 
798                  start_timer();                  start_timer();
799                  if (image_input                  if (image_input
800                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
801                          pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
802                    {
803                            emms();
804                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
805                    }
806                  stop_conv_timer();                  stop_conv_timer();
807    
808                  // queue input frame, and dequue next image                  // queue input frame, and dequue next image
# Line 814  Line 825 
825                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
826                  pEnc->queue_size--;                  pEnc->queue_size--;
827    
828          } else if (BitstreamPos(&bs) == 0) {          } else {
829    
830                    /* if nothing was encoded, write an 'ignore this frame' flag
831                       to the bitstream */
832    
833                    if (BitstreamPos(&bs) == 0) {
834    
835                  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",
836                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
837                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
838    
                 pFrame->intra = 0;  
   
839                  BitstreamPutBits(&bs, 0x7f, 8);                  BitstreamPutBits(&bs, 0x7f, 8);
840                  BitstreamPad(&bs);                          pFrame->intra = 0;
841                  pFrame->length = BitstreamLength(&bs);                  }
   
                 return XVID_ERR_OK;  
   
         } else {  
842    
843                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
844                    emms();
845                  return XVID_ERR_OK;                  return XVID_ERR_OK;
846          }          }
847    
848          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
849    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
850          emms();          emms();
851    
852          // 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 851  Line 857 
857                  else                  else
858                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
859    
860                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
861                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
862    
863                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
864                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
865    */
866                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
867                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
868    
# Line 864  Line 870 
870                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
871                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
872    
                 pEnc->current->seconds = pEnc->mbParam.m_seconds;  
                 pEnc->current->ticks = pEnc->mbParam.m_ticks;  
   
873                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
874    
875  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
# Line 878  Line 881 
881    
882                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
883                          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,
884                                  "%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);
885                  }                  }
886    
887          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 925  Line 928 
928          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
929                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
930                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
931                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
932                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
933                                           pEnc->mbParam.height) > 30) {                                           pEnc->mbParam.height) > 30) {*/
934                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
935                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
936    
937                  /*                  /*
938                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
939                   */                   */
# Line 966  Line 972 
972                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
973    
974                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
975                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
976                          input_valid = 0;                          input_valid = 0;
977                          goto ipvop_loop;                          goto ipvop_loop;
978                  }                  }
# Line 975  Line 981 
981                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
982                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
983                   */                   */
984          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
985                  /*                  /*
986                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
987                   */                   */
# Line 993  Line 999 
999                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1000    
1001                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1002                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1003                          input_valid = 0;                          input_valid = 0;
1004                          goto ipvop_loop;                          goto ipvop_loop;
1005                  }                  }
# Line 1003  Line 1009 
1009                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1010                   */                   */
1011    
                 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);  
   
1012                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1013                          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");
1014                  }                  }
# Line 1018  Line 1020 
1020                  } else {                  } else {
1021                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1022                  }                  }
1023                    if (pEnc->current->quant < 1)
1024                            pEnc->current->quant = 1;
1025    
1026                    if (pEnc->current->quant > 31)
1027                            pEnc->current->quant = 31;
1028    
1029    
1030                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1031                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1032                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1033    
1034    
1035    
1036                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1037                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 1032  Line 1046 
1046                  goto bvop_loop;                  goto bvop_loop;
1047          }          }
1048    
1049            pEnc->iFrameNum++;
1050    
1051          BitstreamPad(&bs);          BitstreamPad(&bs);
1052          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1053    
# Line 1060  Line 1076 
1076                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1077          }          }
1078    
   
1079          stop_global_timer();          stop_global_timer();
1080          write_timer();          write_timer();
1081    
1082            emms();
1083          return XVID_ERR_OK;          return XVID_ERR_OK;
1084  }  }
1085    
 #endif  
   
1086    
1087    
1088  /*****************************************************************************  /*****************************************************************************
# Line 1106  Line 1120 
1120    
1121          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1122          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  
1123          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1124    
1125            inc_frame_num(pEnc);
1126    
1127            /* disable alternate scan flag if interlacing is not enabled */
1128            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1129                    !(pEnc->current->global_flags & XVID_INTERLACING))
1130            {
1131                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1132            }
1133    
1134          start_timer();          start_timer();
1135          if (image_input          if (image_input
1136                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1137                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1138                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1139          stop_conv_timer();          stop_conv_timer();
1140    
# Line 1134  Line 1153 
1153                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1154          }          }
1155    
1156            if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1157                    pEnc->mbParam.m_quarterpel = 1;
1158            else
1159                    pEnc->mbParam.m_quarterpel = 0;
1160    
1161          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1162                  int *temp_dquants =                  int *temp_dquants =
1163                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 1239  Line 1263 
1263          DEBUG(temp);          DEBUG(temp);
1264  #endif  #endif
1265    
 #ifdef BFRAMES  
         inc_frame_num(pEnc);  
 #else  
1266          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1267    
1268          stop_global_timer();          stop_global_timer();
1269          write_timer();          write_timer();
# Line 1492  Line 1511 
1511          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1512          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1513          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1514            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1515          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1516    
1517          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1518  #ifdef BFRAMES  
1519  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1520          if ((pEnc->global & XVID_GLOBAL_PACKED)) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1521                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1522          }          }
1523  #endif  
1524    #define XVID_ID "XviD" XVID_BS_VERSION
1525            BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID));
1526    
1527            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1528          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1529    
1530          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1524  Line 1548 
1548                          stop_prediction_timer();                          stop_prediction_timer();
1549    
1550                          start_timer();                          start_timer();
1551                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1552                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1553                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1554                                    qcoeff[5*64+0]=0;
1555                            }
1556                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1557                          stop_coding_timer();                          stop_coding_timer();
1558                  }                  }
# Line 1545  Line 1574 
1574    
1575    
1576  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1577    #define BFRAME_SKIP_THRESHHOLD 30
1578    
1579  static int  static int
1580  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1559  Line 1589 
1589          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1590    
1591          int iLimit;          int iLimit;
1592          uint32_t x, y;          int x, y, k;
1593          int iSearchRange;          int iSearchRange;
1594          int bIntra;          int bIntra, skip_possible;
1595    
1596          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1597          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1598    
1599          start_timer();          start_timer();
1600          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1601                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1602          stop_edges_timer();          stop_edges_timer();
1603    
1604          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1605          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1606            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1607          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1608    
1609          if (!force_inter)          if (!force_inter)
# Line 1588  Line 1618 
1618                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1619                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1620                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1621                                                      pEnc->mbParam.m_quarterpel,
1622                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1623                  stop_inter_timer();                  stop_inter_timer();
1624          }          }
1625    
1626            if (pEnc->current->global_flags & XVID_GMC) {
1627    //              printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);
1628                    DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);
1629                    pEnc->current->coding_type = S_VOP;
1630            } else
1631                    pEnc->current->coding_type = P_VOP;
1632    
1633          start_timer();          start_timer();
1634          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1635                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1636            if (bIntra == 0) {
1637                            pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current);
1638                            MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1639                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1640                    }
1641    
1642          } else {          } else {
1643    
 #ifdef _SMP  
         if (pEnc->mbParam.num_threads > 1)  
                 bIntra =  
                         SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                                                  &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                                  iLimit);  
         else  
 #endif  
1644                  bIntra =                  bIntra =
1645                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1646                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1647                           iLimit);                           iLimit);
   
1648          }          }
1649          stop_motion_timer();          stop_motion_timer();
1650    
1651          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1652                  return FrameCodeI(pEnc, bs, pBits);  
1653          }          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )
1654                            pEnc->current->coding_type = P_VOP;             /* no global motion -> no GMC */
1655    
         pEnc->current->coding_type = P_VOP;  
1656    
1657          if (vol_header)          if (vol_header)
1658                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1659    
1660    
1661            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1662          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1663    
1664          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1665    
1666          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1667          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1668    
1669          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1646  Line 1681 
1681                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1682                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1683                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1684                                                                             pEnc->mbParam.m_quarterpel,
1685                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1686                                  stop_comp_timer();                                  stop_comp_timer();
1687    
# Line 1663  Line 1699 
1699    
1700                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1701    
1702                                    if (pMB->mode != MODE_NOT_CODED)
1703                                  pMB->cbp =                                  pMB->cbp =
1704                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1705                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1687  Line 1724 
1724                          }                          }
1725    
1726                          start_timer();                          start_timer();
1727    
1728                            /* Finished processing the MB, now check if to CODE or SKIP */
1729    
1730                            skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &
1731                                                            (pMB->dquant == NO_CHANGE);
1732    
1733                            if(pEnc->mbParam.m_quarterpel)
1734                            {       skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y);
1735                            }
1736                            else
1737                            {       skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y);
1738                            }
1739    
1740                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1741    
1742    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1743                                    int bSkip = 1;
1744    
1745                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1746                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1747                                            {
1748                                                    int iSAD;
1749                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1750                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1751                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1752                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1753                                                    {       bSkip = 0;
1754                                                            break;
1755                                                    }
1756                                            }
1757    
1758                                    if (!bSkip)
1759                                    {
1760                                            VECTOR predMV;
1761                                            if(pEnc->mbParam.m_quarterpel) {
1762                                                    predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1763                                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  /* with GMC, qmvs doesn't have to be (0,0)! */
1764                                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1765                                            }
1766                                            else {
1767                                                    predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1768                                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */
1769                                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1770                                            }
1771                                            pMB->mode = MODE_INTER;
1772                                            pMB->cbp = 0;
1773                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1774                                    }
1775                                    else
1776                                    {
1777                                            pMB->mode = MODE_NOT_CODED;
1778                                            MBSkip(bs);
1779                                    }
1780    
1781                            } else {
1782                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1783                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1784                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1785                                            qcoeff[5*64+0]=0;
1786                                    }
1787                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1788                            }
1789    
1790                          stop_coding_timer();                          stop_coding_timer();
1791                  }                  }
1792          }          }
# Line 1706  Line 1805 
1805          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1806    
1807          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1808                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
1809          {          {
1810                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1811                  iSearchRange *= 2;                  iSearchRange *= 2;
1812          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1813                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->sStat.fMvPrevSigma >= 0)
1814                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1815                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
1816          {          {
1817                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1818                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1721  Line 1820 
1820    
1821          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1822    
1823    #ifdef FRAMEDROP
1824            /* frame drop code */
1825            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1826            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1827                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1828            {
1829                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1830                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1831    
1832                    BitstreamReset(bs);
1833    
1834                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1835                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1836    
1837                    // copy reference frame details into the current frame
1838                    pEnc->current->quant = pEnc->reference->quant;
1839                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1840                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1841                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1842                    pEnc->current->fcode = pEnc->reference->fcode;
1843                    pEnc->current->bcode = pEnc->reference->bcode;
1844                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1845                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1846    
1847            }
1848    #endif
1849    
1850          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1851    
1852          return 0;                                       // inter          return 0;                                       // inter
1853  }  }
1854    
1855    
1856  #ifdef BFRAMES  static __inline void
 static void  
1857  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1858                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1859                     Bitstream * bs,                     Bitstream * bs,
# Line 1737  Line 1862 
1862          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1863          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1864          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1865    
1866          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1867          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
# Line 1755  Line 1878 
1878          }          }
1879  #endif  #endif
1880    
1881            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
1882    
1883          // forward          // forward
1884          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1885                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1886                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1887          start_timer();          start_timer();
1888          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,
1889                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1890                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
1891          stop_inter_timer();          stop_inter_timer();
1892    
1893          // backward          // backward
1894          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1895                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1896                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1897          start_timer();          start_timer();
1898          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1899                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1900                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
1901          stop_inter_timer();          stop_inter_timer();
1902    
1903          start_timer();          start_timer();
1904          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,  
1905            MotionEstimationBVOP(&pEnc->mbParam, frame,
1906                    ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1907                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1908                            pEnc->reference->mbs, f_ref,
1909                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1910                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1911                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1792  Line 1919 
1919             } */             } */
1920    
1921          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1922    
1923            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1924          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1925    
1926          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1803  Line 1932 
1932    
1933    
1934          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;  
   
1935                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1936                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1937                                  &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];  
1938    
1939                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1940                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1941                                  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  
1942                                  continue;                                  continue;
1943                          }                          }
1944    
1945                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1946                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1947                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1948                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1949                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1950                                                                           dct_codes);                                                                           dct_codes);
1951    
1952                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1953                          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);  
1954    
1955                                    mb->cbp =
1956                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1957    
1958                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1959                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1960                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
1961                          }                          }
   
                         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;  
1962                          }                          }
 //                      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);  
1963    
1964  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1965          BFRAME_DEBUG          BFRAME_DEBUG
1966  #endif  #endif
1967                          start_timer();                          start_timer();
1968                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1969                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1970                          stop_coding_timer();                          stop_coding_timer();
1971                  }                  }
1972          }          }
# Line 1886  Line 1985 
1985          }          }
1986  #endif  #endif
1987  }  }
1988  #endif  
1989    
1990    /*      in case internal output is needed somewhere... */
1991    /*      {
1992            FILE *filehandle;
1993            filehandle=fopen("last-b.pgm","wb");
1994            if (filehandle)
1995            {
1996                    fprintf(filehandle,"P5\n\n");           //
1997                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1998                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1999                    fclose(filehandle);
2000                    }
2001            }
2002    */

Legend:
Removed from v.313  
changed lines
  Added in v.631

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