[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 573, Mon Sep 30 14:16:02 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.8 2002-09-30 14:16:02 chl 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 55  Line 56 
56  #include "image/image.h"  #include "image/image.h"
57  #ifdef BFRAMES  #ifdef BFRAMES
58  #include "image/font.h"  #include "image/font.h"
59    #include "motion/sad.h"
60  #endif  #endif
61  #include "motion/motion.h"  #include "motion/motion.h"
62  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
# Line 68  Line 70 
70  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
71  #include "utils/mem_align.h"  #include "utils/mem_align.h"
72    
 #ifdef _SMP  
 #include "motion/smp_motion_est.h"  
 #endif  
73  /*****************************************************************************  /*****************************************************************************
74   * Local macros   * Local macros
75   ****************************************************************************/   ****************************************************************************/
# Line 92  Line 91 
91                                            bool force_inter,                                            bool force_inter,
92                                            bool vol_header);                                            bool vol_header);
93    
 #ifdef BFRAMES  
94  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
95                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
96                                             Bitstream * bs,                                             Bitstream * bs,
97                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
98    
99  /*****************************************************************************  /*****************************************************************************
100   * Local data   * Local data
# Line 209  Line 206 
206    
207          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
208    
209          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
210                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
211    
212          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
# Line 236  Line 233 
233    
234          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
235    
 #ifdef _SMP  
         pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);  
 #endif  
   
236          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
237    
238          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 274  Line 267 
267  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
268          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
269  #endif  #endif
270  #ifdef BFRAMES  
271          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
272          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
273          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
274  #endif  
275          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
276          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
277          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 293  Line 286 
286                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
287                  goto xvid_err_memory3;                  goto xvid_err_memory3;
288  #endif  #endif
289  #ifdef BFRAMES  
290          if (image_create          if (image_create
291                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
292                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 306  Line 299 
299                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
300                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
301                  goto xvid_err_memory3;                  goto xvid_err_memory3;
302  #endif  
303          if (image_create          if (image_create
304                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
305                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 339  Line 332 
332    
333    
334          /* B Frames specific init */          /* B Frames specific init */
 #ifdef BFRAMES  
335    
336          pEnc->global = pParam->global;          pEnc->global = pParam->global;
337          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
338          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
339            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
340          pEnc->bframes = NULL;          pEnc->bframes = NULL;
341    
342          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 417  Line 410 
410          pEnc->queue_tail = 0;          pEnc->queue_tail = 0;
411          pEnc->queue_size = 0;          pEnc->queue_size = 0;
412    
413            pEnc->mbParam.m_stamp = 0;
414    
         pEnc->mbParam.m_seconds = 0;  
         pEnc->mbParam.m_ticks = 0;  
415          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
416  #endif          pEnc->current->stamp = 0;
417            pEnc->reference->stamp = 0;
418    
419          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
420    
# Line 440  Line 433 
433          /*          /*
434           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
435           */           */
436  #ifdef BFRAMES  
437    xvid_err_memory5:    xvid_err_memory5:
438    
439    
# Line 474  Line 467 
467                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
468          }          }
469    
 #endif  
   
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    
 #ifdef BFRAMES  
476          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
477                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
478          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
479                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
480          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
481                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
482    
483          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
484                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 534  Line 523 
523  int  int
524  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
525  {  {
 #ifdef BFRAMES  
526          int i;          int i;
 #endif  
527    
528          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
529    
530          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
531          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
532    
533                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 571  Line 557 
557                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
558    
559          }          }
 #endif  
560    
561          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
562    
# Line 589  Line 574 
574                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
575          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
576                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
577  #ifdef BFRAMES  
578          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
579                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
580          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
581                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
582          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
583                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
584  #endif  
585  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
586          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
587                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 616  Line 601 
601  }  }
602    
603    
604  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
605  {  {
606          pEnc->iFrameNum++;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
607          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;  
608  }  }
 #endif  
609    
610    
611  #ifdef BFRAMES  static __inline void
612  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
613  {  {
614          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
615          {          {
# Line 652  Line 632 
632          pEnc->queue_size++;          pEnc->queue_size++;
633          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
634  }  }
635  #endif  
636    static __inline void
637    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
638    {
639    
640                    pCur->ticks = (int32_t)pCur->stamp % time_base;
641                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
642    
643                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
644    
645    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
646                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
647                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
648                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
649                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
650                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
651    
652    */
653    }
654    
655    
656    
657    
658    
 #ifdef BFRAMES  
659  /*****************************************************************************  /*****************************************************************************
660   * IPB frame encoder entry point   * IPB frame encoder entry point
661   *   *
# Line 672  Line 672 
672  {  {
673          uint16_t x, y;          uint16_t x, y;
674          Bitstream bs;          Bitstream bs;
675          uint32_t bits;          uint32_t bits, mode;
676    
677          int input_valid = 1;          int input_valid = 1;
678    
# Line 750  Line 750 
750                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
751                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
752    
753                            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
754                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
755                          BitstreamPad(&bs);                          BitstreamPad(&bs);
756                          BitstreamPutBits(&bs, 0x7f, 8);                          BitstreamPutBits(&bs, 0x7f, 8);
# Line 822  Line 823 
823    
824                  pFrame->intra = 0;                  pFrame->intra = 0;
825    
826                  BitstreamPutBits(&bs, 0x7f, 8);                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
827                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
828                  BitstreamPad(&bs);                  BitstreamPad(&bs);
829                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
830    
# Line 836  Line 838 
838    
839          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
840    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
841          emms();          emms();
842    
843          // 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 848 
848                  else                  else
849                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
850    
851                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
852                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
853    
854                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
855                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
856    */
857                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
858                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
859    
# Line 864  Line 861 
861                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
862                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
863    
                 pEnc->current->seconds = pEnc->mbParam.m_seconds;  
                 pEnc->current->ticks = pEnc->mbParam.m_ticks;  
   
864                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
865    
866  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
# Line 925  Line 919 
919          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
920                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
921                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
922                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
923                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
924                                           pEnc->mbParam.height) > 30) {                                           pEnc->mbParam.height) > 30) {*/
925                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
926                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
927    
928                  /*                  /*
929                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
930                   */                   */
# Line 975  Line 972 
972                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
973                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
974                   */                   */
975          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
976                  /*                  /*
977                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
978                   */                   */
# Line 1003  Line 1000 
1000                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1001                   */                   */
1002    
                 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);  
   
1003                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1004                          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");
1005                  }                  }
# Line 1018  Line 1011 
1011                  } else {                  } else {
1012                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1013                  }                  }
1014                    if (pEnc->current->quant < 1)
1015                            pEnc->current->quant = 1;
1016    
1017                    if (pEnc->current->quant > 31)
1018                            pEnc->current->quant = 31;
1019    
1020    
1021                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1022                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1023                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1024    
1025    
1026    
1027                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1028                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 1032  Line 1037 
1037                  goto bvop_loop;                  goto bvop_loop;
1038          }          }
1039    
1040            pEnc->iFrameNum++;
1041    
1042          BitstreamPad(&bs);          BitstreamPad(&bs);
1043          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1044    
# Line 1060  Line 1067 
1067                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1068          }          }
1069    
   
1070          stop_global_timer();          stop_global_timer();
1071          write_timer();          write_timer();
1072    
1073          return XVID_ERR_OK;          return XVID_ERR_OK;
1074  }  }
1075    
 #endif  
   
1076    
1077    
1078  /*****************************************************************************  /*****************************************************************************
# Line 1106  Line 1110 
1110    
1111          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1112          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  
1113          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1114    
1115            inc_frame_num(pEnc);
1116    
1117            /* disable alternate scan flag if interlacing is not enabled */
1118            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1119                    !(pEnc->current->global_flags & XVID_INTERLACING))
1120            {
1121                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1122            }
1123    
1124          start_timer();          start_timer();
1125          if (image_input          if (image_input
1126                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
# Line 1239  Line 1248 
1248          DEBUG(temp);          DEBUG(temp);
1249  #endif  #endif
1250    
 #ifdef BFRAMES  
         inc_frame_num(pEnc);  
 #else  
1251          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1252    
1253          stop_global_timer();          stop_global_timer();
1254          write_timer();          write_timer();
# Line 1495  Line 1499 
1499          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1500    
1501          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1502  #ifdef BFRAMES  
1503  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1504          if ((pEnc->global & XVID_GLOBAL_PACKED)) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1505                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1506          }          }
1507  #endif  
1508            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1509          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1510    
1511          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1524  Line 1529 
1529                          stop_prediction_timer();                          stop_prediction_timer();
1530    
1531                          start_timer();                          start_timer();
1532                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1533                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1534                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1535                                    qcoeff[5*64+0]=0;
1536                            }
1537                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1538                          stop_coding_timer();                          stop_coding_timer();
1539                  }                  }
# Line 1545  Line 1555 
1555    
1556    
1557  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1558    #define BFRAME_SKIP_THRESHHOLD 30
1559    
1560  static int  static int
1561  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1559  Line 1570 
1570          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1571    
1572          int iLimit;          int iLimit;
1573          uint32_t x, y;          int x, y, k;
1574          int iSearchRange;          int iSearchRange;
1575          int bIntra;          int bIntra;
1576    
# Line 1568  Line 1579 
1579    
1580          start_timer();          start_timer();
1581          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1582                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1583          stop_edges_timer();          stop_edges_timer();
1584    
1585          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 1595  Line 1605 
1605          start_timer();          start_timer();
1606          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1607                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1608                    if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1609                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1610    
1611          } else {          } else {
1612    
 #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  
1613                  bIntra =                  bIntra =
1614                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1615                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1613  Line 1618 
1618          }          }
1619          stop_motion_timer();          stop_motion_timer();
1620    
1621          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1622    
1623          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1624    
1625          if (vol_header)          if (vol_header)
1626                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1627    
1628    
1629            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1630          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1631    
1632          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1633    
1634          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1635          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1636    
1637          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1663  Line 1666 
1666    
1667                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1668    
1669                                    if (pMB->mode != MODE_NOT_CODED)
1670                                  pMB->cbp =                                  pMB->cbp =
1671                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1672                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1687  Line 1691 
1691                          }                          }
1692    
1693                          start_timer();                          start_timer();
1694    
1695                            /* Finished processing the MB, now check if to CODE or SKIP */
1696    
1697                            if ((pMB->mode == MODE_NOT_CODED) ||
1698                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1699                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1700    
1701    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1702    
1703                                    int bSkip = 1;
1704                                    pMB->mode = MODE_NOT_CODED;
1705    
1706                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1707                                    {
1708                                            int iSAD;
1709                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1710                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1711                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1712                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1713                                            {       bSkip = 0;
1714                                                    break;
1715                                            }
1716                                    }
1717                                    if (!bSkip)
1718                                    {
1719                                            VECTOR predMV;
1720                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1721                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1722                                            pMB->mode = MODE_INTER;
1723                                            pMB->cbp = 0;
1724                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1725                                    }
1726                                    else MBSkip(bs);
1727    
1728                            } else {
1729                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1730                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1731                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1732                                            qcoeff[5*64+0]=0;
1733                                    }
1734                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1735                            }
1736    
1737                          stop_coding_timer();                          stop_coding_timer();
1738                  }                  }
1739          }          }
# Line 1721  Line 1767 
1767    
1768          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1769    
1770    #ifdef FRAMEDROP
1771            /* frame drop code */
1772            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1773            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1774                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1775            {
1776                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1777                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1778    
1779                    BitstreamReset(bs);
1780    
1781                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1782                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1783    
1784                    // copy reference frame details into the current frame
1785                    pEnc->current->quant = pEnc->reference->quant;
1786                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1787                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1788                    pEnc->current->fcode = pEnc->reference->fcode;
1789                    pEnc->current->bcode = pEnc->reference->bcode;
1790                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1791                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1792    
1793            }
1794    #endif
1795    
1796          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1797    
1798          return 0;                                       // inter          return 0;                                       // inter
1799  }  }
1800    
1801    
1802  #ifdef BFRAMES  static __inline void
 static void  
1803  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1804                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1805                     Bitstream * bs,                     Bitstream * bs,
# Line 1737  Line 1808 
1808          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1809          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1810          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1811    
1812          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1813          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
# Line 1758  Line 1827 
1827          // forward          // forward
1828          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1829                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1830                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1831          start_timer();          start_timer();
1832          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,
1833                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1769  Line 1837 
1837          // backward          // backward
1838          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1839                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1840                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1841          start_timer();          start_timer();
1842          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1843                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1778  Line 1845 
1845          stop_inter_timer();          stop_inter_timer();
1846    
1847          start_timer();          start_timer();
1848          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,  
1849            MotionEstimationBVOP(&pEnc->mbParam, frame,
1850                    ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1851                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1852                            pEnc->reference->mbs, f_ref,
1853                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1854                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1855                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1792  Line 1863 
1863             } */             } */
1864    
1865          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1866    
1867            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1868          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1869    
1870          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1803  Line 1876 
1876    
1877    
1878          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;  
   
1879                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1880                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1881                                  &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];  
1882    
1883                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1884                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1885                                  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  
1886                                  continue;                                  continue;
1887                          }                          }
1888    
1889                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1890                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1891                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1892                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1893                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1894                                                                           dct_codes);                                                                           dct_codes);
1895    
1896                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1897                          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);  
1898    
1899                                    mb->cbp =
1900                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1901    
1902                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1903                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1904                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
1905                          }                          }
   
                         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;  
1906                          }                          }
 //                      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);  
1907    
1908  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1909          BFRAME_DEBUG          BFRAME_DEBUG
1910  #endif  #endif
1911                          start_timer();                          start_timer();
1912                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1913                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1914                          stop_coding_timer();                          stop_coding_timer();
1915                  }                  }
1916          }          }
# Line 1886  Line 1929 
1929          }          }
1930  #endif  #endif
1931  }  }
1932  #endif  
1933    
1934    /*      in case internal output is needed somewhere... */
1935    /*      {
1936            FILE *filehandle;
1937            filehandle=fopen("last-b.pgm","wb");
1938            if (filehandle)
1939            {
1940                    fprintf(filehandle,"P5\n\n");           //
1941                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1942                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1943                    fclose(filehandle);
1944                    }
1945            }
1946    */

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

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