[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.59, Sat Jul 20 22:30:30 2002 UTC revision 1.76.2.6, Sun Sep 29 15:56:21 2002 UTC
# Line 43  Line 43 
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;
         pEnc->last_pframe = 1;  
 #endif  
416    
417          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
418    
# Line 441  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 475  Line 465 
465                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
466          }          }
467    
 #endif  
   
468    xvid_err_memory3:    xvid_err_memory3:
469  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
470          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
471                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
472  #endif  #endif
473    
 #ifdef BFRAMES  
474          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
475                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
476          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
477                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
478          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
479                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
480    
481          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
482                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 535  Line 521 
521  int  int
522  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
523  {  {
 #ifdef BFRAMES  
524          int i;          int i;
 #endif  
525    
526          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
527    
528          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
529          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
530    
531                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 572  Line 555 
555                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
556    
557          }          }
 #endif  
558    
559          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
560    
# Line 590  Line 572 
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
573          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
574                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
575  #ifdef BFRAMES  
576          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
577                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
578          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
579                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
580          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
581                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
582  #endif  
583  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
584          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
585                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 617  Line 599 
599  }  }
600    
601    
602  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
603  {  {
604          pEnc->iFrameNum++;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
605          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;  
606  }  }
 #endif  
607    
608    
609  #ifdef BFRAMES  static __inline void
610  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
611  {  {
612          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
613          {          {
# Line 653  Line 630 
630          pEnc->queue_size++;          pEnc->queue_size++;
631          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
632  }  }
633  #endif  
634    static __inline void
635    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
636    {
637    
638                    pCur->ticks = (int32_t)pCur->stamp % time_base;
639                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
640    
641                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
642    
643    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
644                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
645                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
646                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
647                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
648                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
649    
650    */
651    }
652    
653    
654    
655    
656    
 #ifdef BFRAMES  
657  /*****************************************************************************  /*****************************************************************************
658   * IPB frame encoder entry point   * IPB frame encoder entry point
659   *   *
# Line 673  Line 670 
670  {  {
671          uint16_t x, y;          uint16_t x, y;
672          Bitstream bs;          Bitstream bs;
673          uint32_t bits;          uint32_t bits, mode;
674    
675          int input_valid = 1;          int input_valid = 1;
676    
# Line 751  Line 748 
748                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
749                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
750    
751                            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
752                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
753                          BitstreamPad(&bs);                          BitstreamPad(&bs);
754                          BitstreamPutBits(&bs, 0x7f, 8);                          BitstreamPutBits(&bs, 0x7f, 8);
# Line 823  Line 821 
821    
822                  pFrame->intra = 0;                  pFrame->intra = 0;
823    
824                  BitstreamPutBits(&bs, 0x7f, 8);                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
825                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
826                  BitstreamPad(&bs);                  BitstreamPad(&bs);
827                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
828    
# Line 837  Line 836 
836    
837          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
838    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
839          emms();          emms();
840    
841          // 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 852  Line 846 
846                  else                  else
847                          pEnc->current->quant = pFrame->quant;                          pEnc->current->quant = pFrame->quant;
848    
849                  if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
850                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
851    
852                  if (pEnc->current->quant > 31)                  if (pEnc->current->quant > 31)
853                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
854    */
855                  pEnc->current->global_flags = pFrame->general;                  pEnc->current->global_flags = pFrame->general;
856                  pEnc->current->motion_flags = pFrame->motion;                  pEnc->current->motion_flags = pFrame->motion;
857    
# Line 865  Line 859 
859                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
860                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
861    
                 pEnc->current->seconds = pEnc->mbParam.m_seconds;  
                 pEnc->current->ticks = pEnc->mbParam.m_ticks;  
   
862                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
863    
864  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
# Line 926  Line 917 
917          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
918                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
919                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
920                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
921                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
922                                           pEnc->mbParam.height) > 30) {                                           pEnc->mbParam.height) > 30) {*/
923                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
924                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
925    
926                  /*                  /*
927                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
928                   */                   */
# Line 976  Line 970 
970                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
971                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
972                   */                   */
973          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
974                  /*                  /*
975                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
976                   */                   */
# Line 1004  Line 998 
998                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
999                   */                   */
1000    
                 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);  
   
1001                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1002                          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");
1003                  }                  }
# Line 1019  Line 1009 
1009                  } else {                  } else {
1010                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1011                  }                  }
1012                    if (pEnc->current->quant < 1)
1013                            pEnc->current->quant = 1;
1014    
1015                    if (pEnc->current->quant > 31)
1016                            pEnc->current->quant = 31;
1017    
1018    
1019                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1020                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1021                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1022    
1023    
1024    
1025                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1026                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 1033  Line 1035 
1035                  goto bvop_loop;                  goto bvop_loop;
1036          }          }
1037    
1038            pEnc->iFrameNum++;
1039    
1040          BitstreamPad(&bs);          BitstreamPad(&bs);
1041          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1042    
# Line 1061  Line 1065 
1065                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1066          }          }
1067    
   
1068          stop_global_timer();          stop_global_timer();
1069          write_timer();          write_timer();
1070    
1071          return XVID_ERR_OK;          return XVID_ERR_OK;
1072  }  }
1073    
 #endif  
   
1074    
1075    
1076  /*****************************************************************************  /*****************************************************************************
# Line 1107  Line 1108 
1108    
1109          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1110          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  
1111          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1112    
1113            /* disable alternate scan flag if interlacing is not enabled */
1114            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1115                    !(pEnc->current->global_flags & XVID_INTERLACING))
1116            {
1117                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1118            }
1119    
1120          start_timer();          start_timer();
1121          if (image_input          if (image_input
1122                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
# Line 1240  Line 1244 
1244          DEBUG(temp);          DEBUG(temp);
1245  #endif  #endif
1246    
 #ifdef BFRAMES  
1247          inc_frame_num(pEnc);          inc_frame_num(pEnc);
 #else  
1248          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1249    
1250          stop_global_timer();          stop_global_timer();
1251          write_timer();          write_timer();
# Line 1496  Line 1496 
1496          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1497    
1498          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1499  #ifdef BFRAMES  
1500  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1501          if ((pEnc->global & XVID_GLOBAL_PACKED)) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1502                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1503          }          }
1504  #endif  
1505            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1506          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1507    
1508          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1525  Line 1526 
1526                          stop_prediction_timer();                          stop_prediction_timer();
1527    
1528                          start_timer();                          start_timer();
1529                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1530                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1531                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1532                                    qcoeff[5*64+0]=0;
1533                            }
1534                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1535                          stop_coding_timer();                          stop_coding_timer();
1536                  }                  }
# Line 1546  Line 1552 
1552    
1553    
1554  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1555    #define BFRAME_SKIP_THRESHHOLD 30
1556    
1557  static int  static int
1558  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1560  Line 1567 
1567          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1568    
1569          int iLimit;          int iLimit;
1570          uint32_t x, y;          int x, y, k;
1571          int iSearchRange;          int iSearchRange;
1572          int bIntra;          int bIntra;
1573    
# Line 1569  Line 1576 
1576    
1577          start_timer();          start_timer();
1578          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1579                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1580          stop_edges_timer();          stop_edges_timer();
1581    
1582          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 1596  Line 1602 
1602          start_timer();          start_timer();
1603          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1604                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1605                    if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1606                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1607    
1608          } else {          } else {
1609    
 #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  
1610                  bIntra =                  bIntra =
1611                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1612                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1614  Line 1615 
1615          }          }
1616          stop_motion_timer();          stop_motion_timer();
1617    
1618          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1619    
1620          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1621    
1622          if (vol_header)          if (vol_header)
1623                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1624    
1625    
1626            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1627          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1628    
1629          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1630    
1631          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1632          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1633    
1634          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1664  Line 1663 
1663    
1664                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1665    
1666                                    if (pMB->mode != MODE_NOT_CODED)
1667                                  pMB->cbp =                                  pMB->cbp =
1668                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1669                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1688  Line 1688 
1688                          }                          }
1689    
1690                          start_timer();                          start_timer();
1691    
1692                            /* Finished processing the MB, now check if to CODE or SKIP */
1693    
1694                            if ((pMB->mode == MODE_NOT_CODED) ||
1695                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1696                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1697    
1698    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1699    
1700                                    int bSkip = 1;
1701                                    pMB->mode = MODE_NOT_CODED;
1702    
1703                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1704                                    {
1705                                            int iSAD;
1706                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1707                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1708                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1709                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1710                                            {       bSkip = 0;
1711                                                    break;
1712                                            }
1713                                    }
1714                                    if (!bSkip)
1715                                    {
1716                                            VECTOR predMV;
1717                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1718                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1719                                            pMB->mode = MODE_INTER;
1720                                            pMB->cbp = 0;
1721                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1722                                    }
1723                                    else MBSkip(bs);
1724    
1725                            } else {
1726                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1727                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1728                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1729                                            qcoeff[5*64+0]=0;
1730                                    }
1731                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1732                            }
1733    
1734                          stop_coding_timer();                          stop_coding_timer();
1735                  }                  }
1736          }          }
# Line 1722  Line 1764 
1764    
1765          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1766    
1767          *pBits = BitstreamPos(bs) - *pBits;  #ifdef FRAMEDROP
1768            /* frame drop code */
1769  #ifdef BFRAMES          // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1770          pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase;          if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1771                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1772            {
1773                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1774                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1775    
1776                    BitstreamReset(bs);
1777    
1778                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1779                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1780    
1781                    // copy reference frame details into the current frame
1782                    pEnc->current->quant = pEnc->reference->quant;
1783                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1784                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1785                    pEnc->current->fcode = pEnc->reference->fcode;
1786                    pEnc->current->bcode = pEnc->reference->bcode;
1787                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1788                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1789    
1790          pEnc->last_pframe = pEnc->mbParam.m_ticks;          }
1791  #endif  #endif
1792    
1793            *pBits = BitstreamPos(bs) - *pBits;
1794    
1795          return 0;                                       // inter          return 0;                                       // inter
1796  }  }
1797    
1798    
1799  #ifdef BFRAMES  static __inline void
 static void  
1800  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1801                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1802                     Bitstream * bs,                     Bitstream * bs,
# Line 1743  Line 1805 
1805          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1806          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1807          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1808    
1809          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1810          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
# Line 1764  Line 1824 
1824          // forward          // forward
1825          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1826                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1827                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1828          start_timer();          start_timer();
1829          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,
1830                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1775  Line 1834 
1834          // backward          // backward
1835          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1836                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1837                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1838          start_timer();          start_timer();
1839          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1840                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1784  Line 1842 
1842          stop_inter_timer();          stop_inter_timer();
1843    
1844          start_timer();          start_timer();
1845    
1846          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1847            ((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase,                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1848                                                  pEnc->time_pp,                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1849                                                  pEnc->reference->mbs, f_ref,                                                  pEnc->reference->mbs, f_ref,
1850                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1851                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
# Line 1801  Line 1860 
1860             } */             } */
1861    
1862          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1863    
1864            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1865          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1866    
1867          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1812  Line 1873 
1873    
1874    
1875          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;  
   
1876                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1877                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1878                                  &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];  
1879    
1880                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1881                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1882                                  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  
1883                                  continue;                                  continue;
1884                          }                          }
1885    
1886                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1887                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1888                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1889                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1890                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1891                                                                           dct_codes);                                                                           dct_codes);
1892    
1893                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1894                          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);  
1895    
1896                                    mb->cbp =
1897                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1898    
1899                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1900                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1901                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
1902                          }                          }
   
                         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;  
1903                          }                          }
 //                      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);  
1904    
1905  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1906          BFRAME_DEBUG          BFRAME_DEBUG
1907  #endif  #endif
1908                          start_timer();                          start_timer();
1909                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1910                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1911                          stop_coding_timer();                          stop_coding_timer();
1912                  }                  }
1913          }          }
# Line 1895  Line 1926 
1926          }          }
1927  #endif  #endif
1928  }  }
1929  #endif  
1930    
1931    /*      in case internal output is needed somewhere... */
1932    /*      {
1933            FILE *filehandle;
1934            filehandle=fopen("last-b.pgm","wb");
1935            if (filehandle)
1936            {
1937                    fprintf(filehandle,"P5\n\n");           //
1938                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1939                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1940                    fclose(filehandle);
1941                    }
1942            }
1943    */

Legend:
Removed from v.1.59  
changed lines
  Added in v.1.76.2.6

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