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

Diff of /xvidcore/src/encoder.c

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

revision 1.76.2.34, Sun Jan 5 16:18:47 2003 UTC revision 1.100, Wed May 28 16:51:08 2003 UTC
# Line 26  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
  ****************************************************************************/  
   
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              MinChen <chenm001@163.com>  
  *  20.06.2002 bframe patch  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
29   *  $Id$   *  $Id$
30   *   *
31   ****************************************************************************/   ****************************************************************************/
32    
   
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <stdio.h>  #include <stdio.h>
35  #include <math.h>  #include <math.h>
# Line 130  Line 116 
116  {  {
117          Encoder *pEnc;          Encoder *pEnc;
118          int i;          int i;
   
119          pParam->handle = NULL;          pParam->handle = NULL;
120    
121          ENC_CHECK(pParam);          ENC_CHECK(pParam);
# Line 198  Line 183 
183          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
184    
185          if (pParam->max_key_interval <= 0)          if (pParam->max_key_interval <= 0)
186                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fbase / pParam->fincr;
187    
188          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
189          if (pEnc == NULL)          if (pEnc == NULL)
# Line 230  Line 215 
215    
216          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
217    
218          pEnc->iFrameNum = 0;          pEnc->iFrameNum = -1;
219          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
220    
221          /* try to allocate frame memory */          /* try to allocate frame memory */
# Line 255  Line 240 
240    
241          /* try to allocate image memory */          /* try to allocate image memory */
242    
243  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
244          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
 #endif  
245    
246          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
247          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 271  Line 255 
255          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
256          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
257    
258  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
259          if (image_create          {       if (image_create
260                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
261                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
262                  goto xvid_err_memory3;                  goto xvid_err_memory3;
263  #endif          }
264    
265          if (image_create          if (image_create
266                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
# Line 320  Line 304 
304                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
305                  goto xvid_err_memory3;                  goto xvid_err_memory3;
306    
307    /* Create full bitplane for GMC, this might be wasteful */
308            if (image_create
309                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
310                     pEnc->mbParam.edged_height) < 0)
311                    goto xvid_err_memory3;
312    
313    
         /* B Frames specific init */  
314    
315          pEnc->mbParam.global = pParam->global;          pEnc->mbParam.global = pParam->global;
316    
317            /* B Frames specific init */
318          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
319          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
320          pEnc->mbParam.bquant_offset = pParam->bquant_offset;          pEnc->mbParam.bquant_offset = pParam->bquant_offset;
# Line 375  Line 365 
365    
366          pEnc->queue = NULL;          pEnc->queue = NULL;
367    
   
368          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
369                  int n;                  int n;
370    
# Line 428  Line 417 
417    
418    xvid_err_memory5:    xvid_err_memory5:
419    
   
420          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
421    
422                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 460  Line 448 
448          }          }
449    
450    xvid_err_memory3:    xvid_err_memory3:
451  #ifdef _DEBUG_PSNR  
452          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
453            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
454                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
455  #endif          }
456    
457          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
458                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 487  Line 476 
476          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
477                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
478    
479    /* destroy GMC image */
480            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
481                                      pEnc->mbParam.edged_height);
482    
483    
484    xvid_err_memory2:    xvid_err_memory2:
485          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
486          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 530  Line 524 
524                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
525          }          }
526    
   
527          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
528    
529                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 574  Line 567 
567          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
568                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
569    
570  #ifdef _DEBUG_PSNR          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
571          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
573  #endif          }
574    
575          /* Encoder structure */          /* Encoder structure */
576    
# Line 595  Line 588 
588    
589  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
590  {  {
591          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
592          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
593  }  }
594    
# Line 621  Line 614 
614                  return;                  return;
615          stop_conv_timer();          stop_conv_timer();
616    
617            if ((pFrame->general & XVID_CHROMAOPT)) {
618                    image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
619                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
620            }
621    
622          pEnc->queue_size++;          pEnc->queue_size++;
623          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
624  }  }
# Line 632  Line 630 
630                  pCur->ticks = (int32_t)pCur->stamp % time_base;                  pCur->ticks = (int32_t)pCur->stamp % time_base;
631                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
632    
633                  //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */
634    
635  /*              fprintf(stderr,"WriteVop:   %d - %d \n",  /*              fprintf(stderr,"WriteVop:   %d - %d \n",
636                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
# Line 675  Line 673 
673          uint16_t x, y;          uint16_t x, y;
674          Bitstream bs;          Bitstream bs;
675          uint32_t bits;          uint32_t bits;
676          int mode;          int mode = -1; /* Just to shut up compiler warning */
677    
678          int input_valid = 1;          int input_valid = 1;
679          int bframes_count = 0;          int bframes_count = 0;
680    
 #ifdef _DEBUG_PSNR  
         float psnr;  
         char temp[128];  
 #endif  
   
681          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
682          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
683          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
# Line 721  Line 714 
714                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
715                          bframes_count = 0;                          bframes_count = 0;
716    
717                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
718                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
719                          pFrame->intra = 0;                          pFrame->intra = 0;
720    
721    
722                          emms();                          emms();
723    
724                            if (pResult) {
725                                    pResult->quant = pEnc->current->quant;
726                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
727                                    pResult->kblks = pEnc->current->sStat.kblks;
728                                    pResult->mblks = pEnc->current->sStat.mblks;
729                                    pResult->ublks = pEnc->current->sStat.ublks;
730                            }
731    
732                          return XVID_ERR_OK;                          return XVID_ERR_OK;
733                  }                  }
734    
# Line 738  Line 740 
740                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
741                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
742    
743                  BitstreamPad(&bs);                  BitstreamPadAlways(&bs);
744                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
745                  pFrame->intra = 2;                  pFrame->intra = 2;
746    
747                    if (pResult) {
748                            pResult->quant = pEnc->current->quant;
749                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
750                            pResult->kblks = pEnc->current->sStat.kblks;
751                            pResult->mblks = pEnc->current->sStat.mblks;
752                            pResult->ublks = pEnc->current->sStat.ublks;
753                    }
754    
755                    emms();
756    
757                    if (pFrame->quant == 0) {
758                            RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
759                                                              pFrame->length, pFrame->intra);
760                    }
761    
762                  if (input_valid)                  if (input_valid)
763                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
764    
# Line 754  Line 771 
771                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
772    
773                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
   
774                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
775                     of a not-coded p-vop, with a time_base of zero, and time_increment                     of a not-coded p-vop, with a time_base of zero, and time_increment
776                     indentical to the future-referece frame.                     indentical to the future-referece frame.
# Line 767  Line 783 
783                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
784                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
785    
                         BitstreamPad(&bs);  
   
786                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
787                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
788    
789                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
790                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
791    
792                            BitstreamPadAlways(&bs);
793                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
794                          pFrame->intra = 4;                          pFrame->intra = 4;
795    
796                            if (pResult) {
797                                    pResult->quant = pEnc->current->quant;
798                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
799                                    pResult->kblks = pEnc->current->sStat.kblks;
800                                    pResult->mblks = pEnc->current->sStat.mblks;
801                                    pResult->ublks = pEnc->current->sStat.ublks;
802                            }
803    
804                            emms();
805    
806                            if (pFrame->quant == 0) {
807                                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
808                                                                      pFrame->length, pFrame->intra);
809                            }
810    
811                          if (input_valid)                          if (input_valid)
812                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
813    
# Line 819  Line 850 
850                  }                  }
851                  stop_conv_timer();                  stop_conv_timer();
852    
853                  // queue input frame, and dequue next image                  if ((pFrame->general & XVID_CHROMAOPT)) {
854                            image_chroma_optimize(&pEnc->current->image,
855                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
856                    }
857    
858                    /* queue input frame, and dequue next image */
859                  if (pEnc->queue_size > 0)                  if (pEnc->queue_size > 0)
860                  {                  {
861                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
# Line 850  Line 886 
886                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
887                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
888    
889                          BitstreamPutBits(&bs, 0x7f, 8);                          /* That disabled line of code was supposed to inform VirtualDub
890                             * that the frame was a dummy delay frame - now disabled (thx god :-)
891                             */
892                            //BitstreamPutBits(&bs, 0x7f, 8);
893                          pFrame->intra = 5;                          pFrame->intra = 5;
894    
895                            if (pResult) {
896                                    /*
897                                     * We must decide what to put there because i know some apps
898                                     * are storing statistics about quantizers and just do
899                                     * stats[quant]++ or stats[quant-1]++
900                                     * transcode is one of these app with its 2pass module
901                                     */
902    
903                                    /*
904                                     * For now i prefer 31 than 0 that could lead to a segfault
905                                     * in transcode
906                                     */
907                                    pResult->quant = 31;
908    
909                                    pResult->hlength = 0;
910                                    pResult->kblks = 0;
911                                    pResult->mblks = 0;
912                                    pResult->ublks = 0;
913                            }
914                    } else {
915    
916                            if (pResult) {
917                                    pResult->quant = pEnc->current->quant;
918                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
919                                    pResult->kblks = pEnc->current->sStat.kblks;
920                                    pResult->mblks = pEnc->current->sStat.mblks;
921                                    pResult->ublks = pEnc->current->sStat.ublks;
922                            }
923    
924                  }                  }
925    
926                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
927    
928                  emms();                  emms();
929    
930                  return XVID_ERR_OK;                  return XVID_ERR_OK;
931          }          }
932    
# Line 863  Line 934 
934    
935          emms();          emms();
936    
937          // 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 */
938          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
939          {          {
940                  mode = intra2coding_type(pFrame->intra);                  mode = intra2coding_type(pFrame->intra);
# Line 887  Line 958 
958    
959                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
960    
961  #ifdef _DEBUG_PSNR                  if (pFrame->general & XVID_EXTRASTATS)
962                  image_copy(&pEnc->sOriginal, &pEnc->current->image,                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
963                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
964  #endif                  }
965    
966                  emms();                  emms();
967    
# Line 926  Line 997 
997    
998                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
999                                  }                                  }
   
1000  #undef OFFSET  #undef OFFSET
1001                          }                          }
   
1002                          xvid_free(temp_dquants);                          xvid_free(temp_dquants);
1003                  }                  }
   
1004          }          }
1005    
1006          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 942  Line 1010 
1010    
1011          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1012                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1013                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) {
         {  
1014                  mode = I_VOP;                  mode = I_VOP;
1015          }else{          }else{
1016                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1017                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1018                                          (mode < 0) ? pEnc->iFrameNum : 0,                                          (/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0,
1019                                          bframes_count++);                                          bframes_count++, pFrame->bframe_threshold);
1020          }          }
1021    
1022          if (mode == I_VOP) {          if (mode == I_VOP) {
# Line 979  Line 1046 
1046                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1047                  }                  }
1048    
1049                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe                  /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */
1050    
1051                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1052    
# Line 992  Line 1059 
1059                          }                          }
1060                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
1061                          bframes_count = 0;                          bframes_count = 0;
   
1062                          pFrame->intra = 0;                          pFrame->intra = 0;
1063    
1064                  } else {                  } else {
# Line 1016  Line 1082 
1082                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1083                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1084                   */                   */
1085          } else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1086                  /*                  /*
1087                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1088                   */                   */
# Line 1072  Line 1138 
1138    
1139                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1140    
1141  // bframe report by koepi                  /* bframe report by koepi */
1142                  pFrame->intra = 2;                  pFrame->intra = 2;
1143                  pFrame->length = 0;                  pFrame->length = 0;
1144    
# Line 1080  Line 1146 
1146                  goto bvop_loop;                  goto bvop_loop;
1147          }          }
1148    
1149          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1150          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1151    
1152          if (pResult) {          if (pResult) {
# Line 1089  Line 1155 
1155                  pResult->kblks = pEnc->current->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1156                  pResult->mblks = pEnc->current->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1157                  pResult->ublks = pEnc->current->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
         }  
1158    
1159          emms();                  if (pFrame->general & XVID_EXTRASTATS)
1160                    {       pResult->sse_y =
1161  #ifdef _DEBUG_PSNR                                  plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
         psnr =  
                 image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
1162                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1163                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1164    
1165          snprintf(temp, 127, "PSNR: %f\n", psnr);                          pResult->sse_u =
1166          DEBUG(temp);                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1167  #endif                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1168                                                       pEnc->mbParam.height/2);
1169    
1170                            pResult->sse_v =
1171                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1172                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1173                                                       pEnc->mbParam.height/2);
1174                    }
1175            }
1176    
1177            emms();
1178    
1179          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1180                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
# Line 1136  Line 1209 
1209          uint32_t bits;          uint32_t bits;
1210          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1211    
 #ifdef _DEBUG_PSNR  
1212          float psnr;          float psnr;
1213          uint8_t temp[128];          char temp[128];
 #endif  
1214    
1215          start_global_timer();          start_global_timer();
1216    
# Line 1170  Line 1241 
1241                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1242          stop_conv_timer();          stop_conv_timer();
1243    
1244  #ifdef _DEBUG_PSNR          if ((pFrame->general & XVID_CHROMAOPT)) {
1245          image_copy(&pEnc->sOriginal, &pEnc->current->image,                  image_chroma_optimize(&pEnc->current->image,
1246                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1247            }
1248    
1249            if (pFrame->general & XVID_EXTRASTATS)
1250            {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1251                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1252  #endif          }
1253    
1254          emms();          emms();
1255    
# Line 1250  Line 1326 
1326          }          }
1327    
1328          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1329                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == -1)
1330                          || ((pEnc->mbParam.iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1331                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1332                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
# Line 1266  Line 1342 
1342    
1343          }          }
1344    
1345            /* Relic from OpenDivX - now disabled
1346          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1347          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1348          BitstreamPad(&bs);          */
1349    
1350            BitstreamPadAlways(&bs);
1351          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1352    
1353          if (pResult) {          if (pResult) {
# Line 1285  Line 1364 
1364                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1365                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1366          }          }
1367  #ifdef _DEBUG_PSNR          if (pFrame->general & XVID_EXTRASTATS)
1368            {
1369          psnr =          psnr =
1370                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1371                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1372                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1373    
1374          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1375          DEBUG(temp);          }
 #endif  
1376    
1377          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1378    
# Line 1358  Line 1437 
1437                  return;                  return;
1438          }          }
1439    
1440          pEnc->current->fcode =          pEnc->current->fcode = (hint->rawhints) ?
1441                  (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,                  (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
                                                                                                                                  FCODEBITS);  
1442    
1443          length = pEnc->current->fcode + 5;          length = pEnc->current->fcode + 5;
1444          high = 1 << (length - 1);          high = 1 << (length - 1);
# Line 1375  Line 1453 
1453                          VECTOR tmp;                          VECTOR tmp;
1454                          int vec;                          int vec;
1455    
1456                          pMB->mode =                          pMB->mode =     (hint->rawhints) ?
1457                                  (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,                                  (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
                                                                                                                                   MODEBITS);  
1458    
1459                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1460                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1461    
1462                          if (pMB->mode == MODE_INTER) {                          if (pMB->mode == MODE_INTER) {
1463                                  tmp.x =                                  tmp.x = (hint->rawhints) ?
1464                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,                                          bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
1465                                                                                                                                                    length);                                  tmp.y = (hint->rawhints) ?
1466                                  tmp.y =                                          bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
                                         (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,  
                                                                                                                                                   length);  
1467                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1468                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1469    
# Line 1402  Line 1477 
1477                                  }                                  }
1478                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1479                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1480                                          tmp.x =                                          tmp.x = (hint->rawhints) ?
1481                                                  (hint->rawhints) ? bhint->mvs[vec].                                                  bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
1482                                                  x : BitstreamGetBits(&bs, length);                                          tmp.y = (hint->rawhints) ?
1483                                          tmp.y =                                                  bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
                                                 (hint->rawhints) ? bhint->mvs[vec].  
                                                 y : BitstreamGetBits(&bs, length);  
1484                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1485                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1486    
# Line 1418  Line 1491 
1491                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1492                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1493                                  }                                  }
1494                          } else                          // intra / stuffing / not_coded                          } else                          /* intra / stuffing / not_coded */
1495                          {                          {
1496                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1497                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
# Line 1459  Line 1532 
1532    
1533          if (intra) {          if (intra) {
1534                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1535                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1536                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1537                  }                  }
1538                  return;                  return;
# Line 1555  Line 1628 
1628                          pEnc->mbParam.width, pEnc->mbParam.height);                          pEnc->mbParam.width, pEnc->mbParam.height);
1629                  stop_edges_timer();                  stop_edges_timer();
1630          }          }
   
1631          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1632          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1633          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1565  Line 1637 
1637          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1638    
1639          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1640    
1641            BitstreamPadAlways(bs);
1642          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1643    
1644          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1613  Line 1687 
1687                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1688          }          }
1689    
1690          return 1;                                       // intra          return 1;                                       /* intra */
1691  }  }
1692    
1693    
1694  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1695  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1696    
1697    
1698    /* FrameCodeP also handles S(GMC)-VOPs */
1699  static int  static int
1700  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1701                     Bitstream * bs,                     Bitstream * bs,
# Line 1649  Line 1725 
1725                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
1726          }          }
1727    
   
1728          start_timer();          start_timer();
1729          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1730                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height);
# Line 1675  Line 1750 
1750                  stop_inter_timer();                  stop_inter_timer();
1751          }          }
1752    
         if (pEnc->current->global_flags & XVID_GMC) {  
 //              printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);  
                 DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);  
                 pEnc->current->coding_type = S_VOP;  
         } else  
1753                  pEnc->current->coding_type = P_VOP;                  pEnc->current->coding_type = P_VOP;
1754    
1755          start_timer();          start_timer();
1756          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1757                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1758          else          else
   
1759                  bIntra =                  bIntra =
1760                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1761                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1696  Line 1765 
1765    
1766          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1767    
1768          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )          if ( ( pEnc->current->global_flags & XVID_GMC )
1769                          pEnc->current->coding_type = P_VOP;             /* no global motion -> no GMC */                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1770            {
1771                    pEnc->current->coding_type = S_VOP;
1772    
1773                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1774                                            pEnc->mbParam.width, pEnc->mbParam.height,
1775                                            &pEnc->current->gmc_data);
1776    
1777                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1778                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1779                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1780                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1781                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1782    
1783          if (vol_header)          }
                 BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);  
   
1784    
1785          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1786            if (vol_header)
1787            {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1788                    BitstreamPadAlways(bs);
1789            }
1790    
1791          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1792    
1793          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1712  Line 1795 
1795          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1796                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1797    
1798    
1799          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1800                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1801                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1802                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1803    
1804    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1805    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1806    
1807                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1808    
1809                          if (!bIntra) {                          if (bIntra) {
1810                                    CodeIntraMB(pEnc, pMB);
1811                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1812                                                                      dct_codes, qcoeff);
1813    
1814                                    start_timer();
1815                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1816                                    stop_prediction_timer();
1817    
1818                                    pEnc->current->sStat.kblks++;
1819    
1820                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1821                                    stop_coding_timer();
1822                                    continue;
1823                            }
1824    
1825                            if (pEnc->current->coding_type == S_VOP) {
1826    
1827                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1828                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1829                                            pEnc->mbParam.edged_width, 65536);
1830    
1831                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1832                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1833                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1834    
1835                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1836                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1837                                    }
1838    
1839                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1840    
1841                                            if (pEnc->mbParam.m_quarterpel)
1842                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1843                                            else
1844                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1845    
1846                                            pMB->mode = MODE_INTER;
1847                                            pMB->mcsel = 1;
1848                                            pMB->sad16 = iSAD;
1849                                    } else {
1850                                            pMB->mcsel = 0;
1851                                    }
1852                            } else {
1853                                    pMB->mcsel = 0; /* just a precaution */
1854                            }
1855    
1856                                  start_timer();                                  start_timer();
1857                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1858                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1859                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1860                                                                     &pEnc->current->image,
1861                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1862                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1863                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1864                                                                           pEnc->mbParam.m_quarterpel,                                                                           pEnc->mbParam.m_quarterpel,
1865                                                                           (pEnc->current->global_flags & XVID_REDUCED),                                                                           (pEnc->current->global_flags & XVID_REDUCED),
1866                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1867    
1868                                  stop_comp_timer();                                  stop_comp_timer();
1869    
1870                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1747  Line 1882 
1882                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1883    
1884                                  if (pMB->mode != MODE_NOT_CODED)                                  if (pMB->mode != MODE_NOT_CODED)
1885                                          pMB->cbp =                          {       pMB->cbp =
1886                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1887                                                                                    dct_codes, qcoeff);                                                                                    dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1888                          }                          }
1889    
1890                          start_timer();                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
   
                         if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {  
                                 pEnc->current->sStat.kblks++;  
                         } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||  
1891                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1892                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1893                                  pEnc->current->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
# Line 1774  Line 1899 
1899    
1900                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1901    
1902                          skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1903                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == NO_CHANGE);
1904    
1905                            if (pEnc->current->coding_type == S_VOP)
1906                                    skip_possible &= (pMB->mcsel == 1);
1907                            else if (pEnc->current->coding_type == P_VOP) {
1908                          if(pEnc->mbParam.m_quarterpel)                          if(pEnc->mbParam.m_quarterpel)
1909                          {       skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y);                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
                         }  
1910                          else                          else
1911                          {       skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y);                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
1912                          }                          }
1913    
1914                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1915    
1916  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
                                 int bSkip = 1;  
1917    
1918                                  if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */                                  if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1919                                    {
1920                                            int bSkip = 1;
1921    
1922                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1923                                          {                                          {
1924                                                  int iSAD;                                                  int iSAD;
# Line 1802  Line 1931 
1931                                                  }                                                  }
1932                                          }                                          }
1933    
1934                                  if (!bSkip)                                          if (!bSkip) {   /* no SKIP, but trivial block */
                                 {  
                                         VECTOR predMV;  
1935                                          if(pEnc->mbParam.m_quarterpel) {                                          if(pEnc->mbParam.m_quarterpel) {
1936                                                  predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1937                                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  /* with GMC, qmvs doesn't have to be (0,0)! */                                                          pMB->pmvs[0].x = - predMV.x;
1938                                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1939                                          }                                          }
1940                                          else {                                          else {
1941                                                  predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1942                                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */                                                          pMB->pmvs[0].x = - predMV.x;
1943                                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1944                                          }                                          }
1945                                          pMB->mode = MODE_INTER;                                          pMB->mode = MODE_INTER;
1946                                          pMB->cbp = 0;                                          pMB->cbp = 0;
1947                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1948                                                    stop_coding_timer();
1949    
1950                                                    continue;       /* next MB */
1951                                  }                                  }
1952                                  else                                  }
1953                                  {                                  /* do SKIP */
1954    
1955                                          pMB->mode = MODE_NOT_CODED;                                          pMB->mode = MODE_NOT_CODED;
1956                                          MBSkip(bs);                                          MBSkip(bs);
1957                                    stop_coding_timer();
1958                                    continue;       /* next MB */
1959                                  }                                  }
1960                            /* ordinary case: normal coded INTER/INTER4V block */
1961    
                         } else {  
1962                                  if (pEnc->current->global_flags & XVID_GREYSCALE)                                  if (pEnc->current->global_flags & XVID_GREYSCALE)
1963                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1964                                          qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */                                          qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1965                                          qcoeff[5*64+0]=0;                                          qcoeff[5*64+0]=0;
1966                                  }                                  }
1967                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);  
1968                            if(pEnc->mbParam.m_quarterpel) {
1969                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1970                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1971                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1972                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1973                            } else {
1974                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1975                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1976                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1977                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1978                            }
1979    
1980    
1981                            if (pMB->mode == MODE_INTER4V)
1982                            {       int k;
1983                                    for (k=1;k<4;k++)
1984                                    {
1985                                            if(pEnc->mbParam.m_quarterpel) {
1986                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1987                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1988                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1989                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1990                                            } else {
1991                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1992                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1993                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1994                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1995                          }                          }
1996    
1997                                    }
1998                            }
1999    
2000                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2001                          stop_coding_timer();                          stop_coding_timer();
2002    
2003                  }                  }
2004          }          }
2005    
# Line 1859  Line 2024 
2024          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2025    
2026          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2027                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */
2028          {          {
2029                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2030                  iSearchRange *= 2;                  iSearchRange *= 2;
2031          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2032                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2033                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2034                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16                             && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel)))      /* minimum search range 16 */
2035          {          {
2036                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2037                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1875  Line 2040 
2040          pEnc->fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2041    
2042          /* frame drop code */          /* frame drop code */
2043          // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
2044          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2045                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2046          {          {
# Line 1887  Line 2052 
2052                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2053                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2054    
2055                  // copy reference frame details into the current frame                  /* copy reference frame details into the current frame */
2056                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
2057                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
2058                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
# Line 1916  Line 2081 
2081    
2082          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2083    
2084          return 0;                                       // inter          return 0;                                       /* inter */
2085  }  }
2086    
2087    
# Line 1949  Line 2114 
2114    
2115          frame->quarterpel =  pEnc->mbParam.m_quarterpel;          frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2116    
2117          // forward          /* forward  */
2118          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2119                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2120                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
# Line 1959  Line 2124 
2124                                            pEnc->mbParam.m_quarterpel, 0);                                            pEnc->mbParam.m_quarterpel, 0);
2125          stop_inter_timer();          stop_inter_timer();
2126    
2127          // backward          /* backward */
2128          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2129                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2130                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
# Line 1972  Line 2137 
2137          start_timer();          start_timer();
2138    
2139          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2140                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2141                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2142                          pEnc->reference->mbs, f_ref,                          pEnc->reference->mbs, f_ref,
2143                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2144                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
# Line 1981  Line 2146 
2146    
2147    
2148          stop_motion_timer();          stop_motion_timer();
2149            /*
2150          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
            {  
2151             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
2152             } */          }
2153            */
2154    
2155          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2156    
# Line 2005  Line 2170 
2170                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2171                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2172    
2173                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2174                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2175                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */
2176                                  continue;                                  continue;
2177                          }                          }
2178    
# Line 2026  Line 2191 
2191    
2192                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2193                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2194                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2195                                  }                                  }
2196                          }                          }
2197    
# Line 2042  Line 2207 
2207    
2208          emms();          emms();
2209    
2210          // TODO: dynamic fcode/bcode ???          /* TODO: dynamic fcode/bcode ??? */
2211    
2212          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2213    
# Line 2054  Line 2219 
2219          }          }
2220  #endif  #endif
2221  }  }
   
   
 /*      in case internal output is needed somewhere... */  
 /*      {  
         FILE *filehandle;  
         filehandle=fopen("last-b.pgm","wb");  
         if (filehandle)  
         {  
                 fprintf(filehandle,"P5\n\n");           //  
                 fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);  
                 fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);  
                 fclose(filehandle);  
                 }  
         }  
 */  

Legend:
Removed from v.1.76.2.34  
changed lines
  Added in v.1.100

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