[cvs] / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/encoder.c

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

revision 1.76.2.20, Tue Nov 19 13:21:25 2002 UTC revision 1.77, Wed Sep 4 21:16:02 2002 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6     *  Copyright(C) 2002 Michael Militzer
7     *
8   *  This program is an implementation of a part of one or more MPEG-4   *  This program is an implementation of a part of one or more MPEG-4
9   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
10   *  to use this software module in hardware or software products are   *  to use this software module in hardware or software products are
# Line 28  Line 30 
30   *   *
31   ****************************************************************************/   ****************************************************************************/
32    
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              MinChen <chenm001@163.com>  
  *  20.06.2002 bframe patch  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
  *  $Id$  
  *  
  ****************************************************************************/  
   
   
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <stdio.h>  #include <stdio.h>
35  #include <math.h>  #include <math.h>
# Line 54  Line 40 
40  #include "global.h"  #include "global.h"
41  #include "utils/timer.h"  #include "utils/timer.h"
42  #include "image/image.h"  #include "image/image.h"
43    #ifdef BFRAMES
44  #include "image/font.h"  #include "image/font.h"
45  #include "motion/sad.h"  #include "motion/sad.h"
46    #endif
47  #include "motion/motion.h"  #include "motion/motion.h"
48  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
49  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 68  Line 56 
56  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
57  #include "utils/mem_align.h"  #include "utils/mem_align.h"
58    
59    #ifdef _SMP
60    #include "motion/smp_motion_est.h"
61    #endif
62  /*****************************************************************************  /*****************************************************************************
63   * Local macros   * Local macros
64   ****************************************************************************/   ****************************************************************************/
# Line 89  Line 80 
80                                            bool force_inter,                                            bool force_inter,
81                                            bool vol_header);                                            bool vol_header);
82    
83    #ifdef BFRAMES
84  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
85                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
86                                             Bitstream * bs,                                             Bitstream * bs,
87                                             uint32_t * pBits);                                             uint32_t * pBits);
88    #endif
89    
90  /*****************************************************************************  /*****************************************************************************
91   * Local data   * Local data
# Line 107  Line 100 
100  };  };
101    
102    
103    static void __inline
104    image_null(IMAGE * image)
105    {
106            image->y = image->u = image->v = NULL;
107    }
108    
109    
110  /*****************************************************************************  /*****************************************************************************
111   * Encoder creation   * Encoder creation
112   *   *
# Line 224  Line 224 
224    
225          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
226    
227    #ifdef _SMP
228            pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);
229    #endif
230    
231          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
232    
233          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 258  Line 262 
262  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
263          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
264  #endif  #endif
265    #ifdef BFRAMES
266          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
267          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
268          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
269    #endif
270          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
271          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
272          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 277  Line 281 
281                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
282                  goto xvid_err_memory3;                  goto xvid_err_memory3;
283  #endif  #endif
284    #ifdef BFRAMES
285          if (image_create          if (image_create
286                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
287                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 290  Line 294 
294                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
295                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
296                  goto xvid_err_memory3;                  goto xvid_err_memory3;
297    #endif
298          if (image_create          if (image_create
299                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
300                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 323  Line 327 
327    
328    
329          /* B Frames specific init */          /* B Frames specific init */
330    #ifdef BFRAMES
331    
332          pEnc->global = pParam->global;          pEnc->global = pParam->global;
333          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
334          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
         pEnc->bquant_offset = pParam->bquant_offset;  
335          pEnc->frame_drop_ratio = pParam->frame_drop_ratio;          pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
336          pEnc->bframes = NULL;          pEnc->bframes = NULL;
337    
# Line 402  Line 406 
406          pEnc->queue_tail = 0;          pEnc->queue_tail = 0;
407          pEnc->queue_size = 0;          pEnc->queue_size = 0;
408    
         pEnc->mbParam.m_stamp = 0;  
409    
410            pEnc->mbParam.m_seconds = 0;
411            pEnc->mbParam.m_ticks = 0;
412          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
413          pEnc->current->stamp = 0;          pEnc->last_pframe = 0;
414          pEnc->reference->stamp = 0;          pEnc->last_sync = 0;
415    #endif
416    
417          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
418    
# Line 425  Line 431 
431          /*          /*
432           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
433           */           */
434    #ifdef BFRAMES
435    xvid_err_memory5:    xvid_err_memory5:
436    
437    
# Line 459  Line 465 
465                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
466          }          }
467    
468    #endif
469    
470    xvid_err_memory3:    xvid_err_memory3:
471  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
472          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
473                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
474  #endif  #endif
475    
476    #ifdef BFRAMES
477          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
478                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
479          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
480                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
481          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
482                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
483    #endif
484    
485          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
486                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 515  Line 525 
525  int  int
526  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
527  {  {
528    #ifdef BFRAMES
529          int i;          int i;
530    #endif
531    
532          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
533    
534          /* B Frames specific */          /* B Frames specific */
535    #ifdef BFRAMES
536          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
537    
538                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 549  Line 562 
562                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
563    
564          }          }
565    #endif
566    
567          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
568    
# Line 566  Line 580 
580                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
581          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
582                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
583    #ifdef BFRAMES
584          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
585                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
586          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
587                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
588          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
589                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
590    #endif
591  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
592          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
593                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 593  Line 607 
607  }  }
608    
609    
610  static __inline void inc_frame_num(Encoder * pEnc)  void inc_frame_num(Encoder * pEnc)
611  {  {
612          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
613          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;  
614    #ifdef BFRAMES
615            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
616            if (pEnc->mbParam.m_ticks < pEnc->last_sync)
617                    pEnc->mbParam.m_seconds = 1;            // more than 1 second since last I or P is not supported.
618            else
619                    pEnc->mbParam.m_seconds = 0;
620    
621            if (pEnc->current->coding_type != B_VOP)
622                    pEnc->last_sync = pEnc->mbParam.m_ticks;
623    #else
624    
625            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
626            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
627    
628    #endif
629    
630  }  }
631    
632    
633  static __inline void  #ifdef BFRAMES
634  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
635  {  {
636          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
637          {          {
# Line 617  Line 647 
647          start_timer();          start_timer();
648          if (image_input          if (image_input
649                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
650                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
651                  return;                  return;
652          stop_conv_timer();          stop_conv_timer();
653    
654          pEnc->queue_size++;          pEnc->queue_size++;
655          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
656  }  }
657    #endif
 static __inline void  
 set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)  
 {  
   
                 pCur->ticks = (int32_t)pCur->stamp % time_base;  
                 pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;  
   
                 //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable  
   
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
                         ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));  
                 fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",  
                         pCur->coding_type, pCur->stamp, pRef->stamp, time_base);  
                 fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",  
                         pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);  
   
 */  
 }  
   
   
   
658    
659    
660    #ifdef BFRAMES
661  /*****************************************************************************  /*****************************************************************************
662   * IPB frame encoder entry point   * IPB frame encoder entry point
663   *   *
# Line 664  Line 674 
674  {  {
675          uint16_t x, y;          uint16_t x, y;
676          Bitstream bs;          Bitstream bs;
677          uint32_t bits, mode;          uint32_t bits;
678    
679          int input_valid = 1;          int input_valid = 1;
         int bframes_count = 0;  
680    
681  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
682          float psnr;          float psnr;
# Line 708  Line 717 
717                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
718    
719                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
                         bframes_count = 0;  
720    
721                          BitstreamPad(&bs);                          BitstreamPad(&bs);
722                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
723                          pFrame->intra = 0;                          pFrame->intra = 0;
724    
                         emms();  
   
725                          return XVID_ERR_OK;                          return XVID_ERR_OK;
726                  }                  }
727    
# Line 729  Line 735 
735    
736                  BitstreamPad(&bs);                  BitstreamPad(&bs);
737                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
738                  pFrame->intra = 2;                  pFrame->intra = 0;
739    
740                  if (input_valid)                  if (input_valid)
741                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
742    
                 emms();  
   
743                  return XVID_ERR_OK;                  return XVID_ERR_OK;
744          }          }
745    
746          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
747                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
748    
                 /* write an empty marker to the bitstream.  
   
                    for divx5 decoder compatibility, this marker must consist  
                    of a not-coded p-vop, with a time_base of zero, and time_increment  
                    indentical to the future-referece frame.  
                 */  
   
749                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
                         int tmp;  
750    
751                          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",
752                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
753                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
754    
                         BitstreamPad(&bs);  
   
                         tmp = pEnc->current->seconds;  
                         pEnc->current->seconds = 0; /* force time_base = 0 */  
755                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
756                          pEnc->current->seconds = tmp;                          BitstreamPad(&bs);
757                            BitstreamPutBits(&bs, 0x7f, 8);
758    
759                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
760                          pFrame->intra = 4;                          pFrame->intra = 0;
761    
762                          if (input_valid)                          if (input_valid)
763                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
764    
                         emms();  
   
765                          return XVID_ERR_OK;                          return XVID_ERR_OK;
766                  }                  }
767          }          }
# Line 801  Line 792 
792                  start_timer();                  start_timer();
793                  if (image_input                  if (image_input
794                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
795                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))                          pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
                 {  
                         emms();  
796                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
                 }  
797                  stop_conv_timer();                  stop_conv_timer();
798    
799                  // queue input frame, and dequue next image                  // queue input frame, and dequue next image
# Line 828  Line 816 
816                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
817                  pEnc->queue_size--;                  pEnc->queue_size--;
818    
819          } else {          } else if (BitstreamPos(&bs) == 0) {
   
                 /* if nothing was encoded, write an 'ignore this frame' flag  
                    to the bitstream */  
   
                 if (BitstreamPos(&bs) == 0) {  
820    
821                          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",
822                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
823                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
824    
825                          BitstreamPutBits(&bs, 0x7f, 8);                  pFrame->intra = 0;
826                          pFrame->intra = 5;  
827                  }                  BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
828                    BitstreamPad(&bs);
829                    pFrame->length = BitstreamLength(&bs);
830    
831                    return XVID_ERR_OK;
832    
833            } else {
834    
835                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 emms();  
836                  return XVID_ERR_OK;                  return XVID_ERR_OK;
837          }          }
838    
839          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
840    
841            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842             * Well there was a separation here so i put it in ANSI C
843             * comment style :-)
844             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
845    
846          emms();          emms();
847    
848          // 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 873  Line 866 
866                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
867                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
868    
869                    pEnc->current->seconds = pEnc->mbParam.m_seconds;
870                    pEnc->current->ticks = pEnc->mbParam.m_ticks;
871    
872                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
873    
874  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
# Line 884  Line 880 
880    
881                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
882                          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,
883                                  "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);                                  "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
884                  }                  }
885    
886          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 927  Line 923 
923           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
924           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
925    
926    
927          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
928                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
929                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
930                  || 2 == (mode = MEanalysis(&pEnc->reference->image, pEnc->current,                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
931                                                                          &pEnc->mbParam, pEnc->iMaxKeyInterval,                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
932                                                                          (pFrame->intra < 0) ? pEnc->iFrameNum : 0,                                           pEnc->mbParam.height) > 30) {
                                                                         bframes_count++))) {  
   
933                  /*                  /*
934                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
935                   */                   */
                 if ((pEnc->current->global_flags & XVID_QUARTERPEL))  
                         pEnc->mbParam.m_quarterpel = 1;  
                 else  
                         pEnc->mbParam.m_quarterpel = 0;  
936    
937                  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",
938                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
# Line 963  Line 954 
954                                  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");
955                          }                          }
956                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
                         bframes_count = 0;  
957    
958                          pFrame->intra = 0;                          pFrame->intra = 0;
959    
960                  } else {                  } else {
961    
962                          FrameCodeI(pEnc, &bs, &bits);                          FrameCodeI(pEnc, &bs, &bits);
                         bframes_count = 0;  
963                          pFrame->intra = 1;                          pFrame->intra = 1;
964    
965                          pEnc->bframenum_dx50bvop = -1;                          pEnc->bframenum_dx50bvop = -1;
# Line 979  Line 968 
968                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
969    
970                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
971                          BitstreamPadAlways(&bs);                          BitstreamPad(&bs);
972                          input_valid = 0;                          input_valid = 0;
973                          goto ipvop_loop;                          goto ipvop_loop;
974                  }                  }
# Line 988  Line 977 
977                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
978                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
979                   */                   */
980          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
 //      } else if (pFrame->intra == 0 || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {  
981                  /*                  /*
982                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
983                   */                   */
# Line 1003  Line 991 
991                  }                  }
992    
993                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
                 bframes_count = 0;  
994                  pFrame->intra = 0;                  pFrame->intra = 0;
995                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
996    
997                  if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
998                          BitstreamPadAlways(&bs);                          BitstreamPad(&bs);
999                          input_valid = 0;                          input_valid = 0;
1000                          goto ipvop_loop;                          goto ipvop_loop;
1001                  }                  }
# Line 1023  Line 1010 
1010                  }                  }
1011    
1012                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1013                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant =
1014                                  pEnc->bquant_ratio) / 2) + pEnc->bquant_offset)/100;                                  ((pEnc->reference->quant +
1015                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1016                  } else {                  } else {
1017                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1018                  }                  }
   
1019                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
1020                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
1021                  else if (pEnc->current->quant > 31)  
1022                    if (pEnc->current->quant > 31)
1023              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1024    
1025    
1026                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1027                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1028                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1029    
1030    
1031    
1032                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1033                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1034                  SWAP(pEnc->current, pEnc->reference);                  SWAP(pEnc->current, pEnc->reference);
1035    
1036                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1037    
1038  // bframe report by koepi                  pFrame->intra = 0;
                 pFrame->intra = 2;  
1039                  pFrame->length = 0;                  pFrame->length = 0;
1040    
1041                  input_valid = 0;                  input_valid = 0;
# Line 1083  Line 1072 
1072                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1073          }          }
1074    
1075    
1076          stop_global_timer();          stop_global_timer();
1077          write_timer();          write_timer();
1078    
         emms();  
1079          return XVID_ERR_OK;          return XVID_ERR_OK;
1080  }  }
1081    
1082    #endif
1083    
1084    
1085    
1086  /*****************************************************************************  /*****************************************************************************
# Line 1127  Line 1118 
1118    
1119          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1120          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1121            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1122            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1123          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1124    
         inc_frame_num(pEnc);  
   
         /* disable alternate scan flag if interlacing is not enabled */  
         if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&  
                 !(pEnc->current->global_flags & XVID_INTERLACING))  
         {  
                 pEnc->current->global_flags -= XVID_ALTERNATESCAN;  
         }  
   
1125          start_timer();          start_timer();
1126          if (image_input          if (image_input
1127                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1128                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1129                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1130          stop_conv_timer();          stop_conv_timer();
1131    
# Line 1160  Line 1144 
1144                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1145          }          }
1146    
         if ((pEnc->current->global_flags & XVID_QUARTERPEL))  
                 pEnc->mbParam.m_quarterpel = 1;  
         else  
                 pEnc->mbParam.m_quarterpel = 0;  
   
1147          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1148                  int *temp_dquants =                  int *temp_dquants =
1149                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 1270  Line 1249 
1249          DEBUG(temp);          DEBUG(temp);
1250  #endif  #endif
1251    
1252            inc_frame_num(pEnc);
1253          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1254    
1255          stop_global_timer();          stop_global_timer();
# Line 1518  Line 1498 
1498          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1499          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1500          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
         pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;  
1501          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1502    
1503          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1504    #ifdef BFRAMES
1505  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1506          if ((pEnc->global & XVID_GLOBAL_PACKED)) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1507                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1508          }          }
1509    #endif
 #define XVID_ID "XviD" XVID_BS_VERSION  
         BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID));  
   
         set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);  
1510          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1511    
1512          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1581  Line 1556 
1556    
1557    
1558  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1559  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 16
1560    
1561  static int  static int
1562  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1598  Line 1573 
1573          int iLimit;          int iLimit;
1574          int x, y, k;          int x, y, k;
1575          int iSearchRange;          int iSearchRange;
1576          int bIntra, skip_possible;          int bIntra;
1577    
1578          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1579          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1580    
1581          start_timer();          start_timer();
1582          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1583                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height,
1584                                       pEnc->current->global_flags & XVID_INTERLACING);
1585          stop_edges_timer();          stop_edges_timer();
1586    
1587          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1588          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
         pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;  
1589          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1590    
1591          if (!force_inter)          if (!force_inter)
# Line 1625  Line 1600 
1600                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1601                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1602                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
                                                   pEnc->mbParam.m_quarterpel,  
1603                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1604                  stop_inter_timer();                  stop_inter_timer();
1605          }          }
1606    
         if (pEnc->current->global_flags & XVID_GMC) {  
 //              printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);  
                 DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);  
                 pEnc->current->coding_type = S_VOP;  
         } else  
                 pEnc->current->coding_type = P_VOP;  
   
1607          start_timer();          start_timer();
1608          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1609                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
         if (bIntra == 0) {  
                         pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current);  
                         MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                                                                                         &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);  
                 }  
   
1610          } else {          } else {
1611    
1612    #ifdef _SMP
1613            if (pEnc->mbParam.num_threads > 1)
1614                    bIntra =
1615                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1616                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1617                                                     iLimit);
1618            else
1619    #endif
1620                  bIntra =                  bIntra =
1621                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1622                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1623                           iLimit);                           iLimit);
1624    
1625          }          }
1626          stop_motion_timer();          stop_motion_timer();
1627    
1628          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) {
1629                    return FrameCodeI(pEnc, bs, pBits);
1630          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )          }
                         pEnc->current->coding_type = P_VOP;             /* no global motion -> no GMC */  
1631    
1632            pEnc->current->coding_type = P_VOP;
1633    
1634          if (vol_header)          if (vol_header)
1635                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1636    
   
         set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);  
1637          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1638    
1639          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1640    
1641          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =          pEnc->sStat.iTextBits = 0;
1642            pEnc->sStat.iMvSum = 0;
1643            pEnc->sStat.iMvCount = 0;
1644                  pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;                  pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1645    
1646          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1688  Line 1658 
1658                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1659                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1660                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
                                                                          pEnc->mbParam.m_quarterpel,  
1661                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1662                                  stop_comp_timer();                                  stop_comp_timer();
1663    
# Line 1706  Line 1675 
1675    
1676                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1677    
                                 if (pMB->mode != MODE_NOT_CODED)  
1678                                          pMB->cbp =                                          pMB->cbp =
1679                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1680                                                                                    dct_codes, qcoeff);                                                                                    dct_codes, qcoeff);
# Line 1734  Line 1702 
1702    
1703                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1704    
1705                          skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &                          if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1706                                                          (pMB->dquant == NO_CHANGE);                                  pMB->mvs[0].y == 0) {
   
                         if(pEnc->mbParam.m_quarterpel)  
                         {       skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y);  
                         }  
                         else  
                         {       skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y);  
                         }  
1707    
1708                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {  /* This is a candidate for SKIPping, but check intermediate B-frames first */
1709    
1710  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */  #ifdef BFRAMES
1711                                    int iSAD=BFRAME_SKIP_THRESHHOLD;
1712                                  int bSkip = 1;                                  int bSkip = 1;
1713    
                                 if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */  
1714                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1715                                          {                                          {
                                                 int iSAD;  
1716                                                  iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                  iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1717                                                                          pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                                          pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1718                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1719                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                          if (iSAD >= BFRAME_SKIP_THRESHHOLD)
1720                                                  {       bSkip = 0;                                                  {       bSkip = 0;
1721                                                          break;                                                          break;
1722                                                  }                                                  }
1723                                          }                                          }
   
1724                                  if (!bSkip)                                  if (!bSkip)
1725                                  {                                  {
1726                                          VECTOR predMV;                                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1727                                          if(pEnc->mbParam.m_quarterpel) {                                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1728                                                  predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1729                                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  /* with GMC, qmvs doesn't have to be (0,0)! */                                                  qcoeff[5*64+0]=0;
                                                 pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;  
                                         }  
                                         else {  
                                                 predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
                                                 pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */  
                                                 pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;  
1730                                          }                                          }
                                         pMB->mode = MODE_INTER;  
                                         pMB->cbp = 0;  
1731                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1732                                            pMB->cbp = 0x80;                /* trick! so cbp!=0, but still nothing is written to bs */
1733                                  }                                  }
1734                                  else                                  else
                                 {  
                                         pMB->mode = MODE_NOT_CODED;  
1735                                          MBSkip(bs);                                          MBSkip(bs);
1736                                  }  
1737    #else
1738                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1739    
1740    #endif
1741    
1742                          } else {                          } else {
1743                                  if (pEnc->current->global_flags & XVID_GREYSCALE)                                  if (pEnc->current->global_flags & XVID_GREYSCALE)
# Line 1812  Line 1766 
1766          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1767    
1768          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1769                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128
1770          {          {
1771                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1772                  iSearchRange *= 2;                  iSearchRange *= 2;
1773          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1774                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->sStat.fMvPrevSigma >= 0)
1775                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1776                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16
1777          {          {
1778                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1779                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1827  Line 1781 
1781    
1782          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1783    
1784  #ifdef FRAMEDROP  #ifdef BFRAMES
1785          /* frame drop code */          /* frame drop code */
1786          // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);          // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1787          if (pEnc->sStat.kblks + pEnc->sStat.mblks <          if (pEnc->sStat.kblks + pEnc->sStat.mblks <
# Line 1837  Line 1791 
1791                  pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;                  pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1792    
1793                  BitstreamReset(bs);                  BitstreamReset(bs);
   
                 set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);  
1794                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1795    
1796                  // copy reference frame details into the current frame                  // copy reference frame details into the current frame
1797                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
1798                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
1799                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
                 pEnc->current->quarterpel =  pEnc->reference->quarterpel;  
1800                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1801                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1802                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
# Line 1856  Line 1807 
1807    
1808          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1809    
1810    #ifdef BFRAMES
1811            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1812                            (int32_t)pEnc->mbParam.fbase;
1813            pEnc->last_pframe = pEnc->current->ticks;
1814    #endif
1815    
1816          return 0;                                       // inter          return 0;                                       // inter
1817  }  }
1818    
1819    
1820  static __inline void  #ifdef BFRAMES
1821    static void
1822  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1823                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1824                     Bitstream * bs,                     Bitstream * bs,
# Line 1869  Line 1827 
1827          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1828          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1829          uint32_t x, y;          uint32_t x, y;
1830            VECTOR forward;
1831            VECTOR backward;
1832    
1833          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1834          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
# Line 1885  Line 1845 
1845          }          }
1846  #endif  #endif
1847    
         frame->quarterpel =  pEnc->mbParam.m_quarterpel;  
   
1848          // forward          // forward
1849          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1850                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1851                                     pEnc->mbParam.height);                                     pEnc->mbParam.height,
1852                                       frame->global_flags & XVID_INTERLACING);
1853          start_timer();          start_timer();
1854          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,
1855                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1856                                            pEnc->mbParam.m_quarterpel, 0);                                            0);
1857          stop_inter_timer();          stop_inter_timer();
1858    
1859          // backward          // backward
1860          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1861                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1862                                     pEnc->mbParam.height);                                     pEnc->mbParam.height,
1863                                       frame->global_flags & XVID_INTERLACING);
1864          start_timer();          start_timer();
1865          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1866                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1867                                            pEnc->mbParam.m_quarterpel, 0);                                            0);
1868          stop_inter_timer();          stop_inter_timer();
1869    
1870          start_timer();          start_timer();
   
1871          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1872                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp                  ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1873                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp                                                  pEnc->time_pp,
1874                          pEnc->reference->mbs, f_ref,                          pEnc->reference->mbs, f_ref,
1875                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1876                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1877                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
1878    
1879    
# Line 1926  Line 1885 
1885             } */             } */
1886    
1887          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
   
         set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);  
1888          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1889    
1890          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1939  Line 1896 
1896    
1897    
1898          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1899                    // reset prediction
1900    
1901                    forward.x = 0;
1902                    forward.y = 0;
1903                    backward.x = 0;
1904                    backward.y = 0;
1905    
1906                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1907                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK *f_mb =
1908                          int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1909                            MACROBLOCK *b_mb =
1910                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1911                            MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1912    
1913                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1914                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1915                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;                                  mb->mvs[0].x = 0;
1916                                    mb->mvs[0].y = 0;
1917    
1918                                    mb->cbp = 0;
1919    #ifdef BFRAMES_DEC_DEBUG
1920            BFRAME_DEBUG
1921    #endif
1922                                  continue;                                  continue;
1923                          }                          }
1924    
                         if (mb->mode != MODE_DIRECT_NONE_MV) {  
1925                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1926                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1927                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1928                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1929                                                                           dct_codes);                                                                           dct_codes);
1930    
                                 if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;  
1931                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
   
1932                                  mb->cbp =                                  mb->cbp =
1933                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1934                            //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1935    
1936                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1937                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                  && (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) {
1938                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
1939                                  }                                  }
1940    
1941    /* update predictors for forward and backward vectors */
1942                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1943                                    mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1944                                    mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1945                                    forward.x = mb->mvs[0].x;
1946                                    forward.y = mb->mvs[0].y;
1947                            }
1948    
1949                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
1950                                    mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1951                                    mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1952                                    backward.x = mb->b_mvs[0].x;
1953                                    backward.y = mb->b_mvs[0].y;
1954                          }                          }
1955    
1956    //                      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);
1957    
1958  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1959          BFRAME_DEBUG          BFRAME_DEBUG
1960  #endif  #endif
1961                          start_timer();                          start_timer();
1962                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1963                                                   &pEnc->sStat, direction);                                                   &pEnc->sStat);
1964                          stop_coding_timer();                          stop_coding_timer();
1965                  }                  }
1966          }          }
# Line 1992  Line 1979 
1979          }          }
1980  #endif  #endif
1981  }  }
1982    #endif
1983    
1984    
1985  /*      in case internal output is needed somewhere... */  /*      in case internal output is needed somewhere... */

Legend:
Removed from v.1.76.2.20  
changed lines
  Added in v.1.77

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