[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.19, Tue Nov 19 13:03:57 2002 UTC revision 1.76.2.36, Sat Jan 11 20:37:46 2003 UTC
# Line 43  Line 43 
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
   
46  #include <stdlib.h>  #include <stdlib.h>
47  #include <stdio.h>  #include <stdio.h>
48  #include <math.h>  #include <math.h>
# Line 73  Line 72 
72   ****************************************************************************/   ****************************************************************************/
73    
74  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
75  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
76    
77  /*****************************************************************************  /*****************************************************************************
78   * Local function prototypes   * Local function prototypes
# Line 224  Line 223 
223    
224          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
225    
226          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
227    
228          /* Fill rate control parameters */          /* Fill rate control parameters */
229    
230          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
231    
232          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
233          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
234    
235          /* try to allocate frame memory */          /* try to allocate frame memory */
236    
# Line 320  Line 319 
319                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
320                  goto xvid_err_memory3;                  goto xvid_err_memory3;
321    
322    /* Create full bitplane for GMC, this might be wasteful */
323            if (image_create
324                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
325                     pEnc->mbParam.edged_height) < 0)
326                    goto xvid_err_memory3;
327    
328    
329          /* B Frames specific init */          /* B Frames specific init */
330    
331          pEnc->global = pParam->global;          pEnc->mbParam.global = pParam->global;
332          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
333          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
334          pEnc->frame_drop_ratio = pParam->frame_drop_ratio;          pEnc->mbParam.bquant_offset = pParam->bquant_offset;
335            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
336          pEnc->bframes = NULL;          pEnc->bframes = NULL;
337    
338          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 486  Line 491 
491          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
492                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
493    
494    /* destroy GMC image */
495            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
496                                      pEnc->mbParam.edged_height);
497    
498    
499    xvid_err_memory2:    xvid_err_memory2:
500          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
501          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 645  Line 655 
655    
656    
657    
658    /* convert pFrame->intra to coding_type */
659    static int intra2coding_type(int intra)
660    {
661            if (intra < 0)          return -1;
662            if (intra == 1)         return I_VOP;
663            if (intra == 2)         return B_VOP;
664    
665            return P_VOP;
666    }
667    
668    
669    
670  /*****************************************************************************  /*****************************************************************************
# Line 663  Line 683 
683  {  {
684          uint16_t x, y;          uint16_t x, y;
685          Bitstream bs;          Bitstream bs;
686          uint32_t bits, mode;          uint32_t bits;
687            int mode;
688    
689          int input_valid = 1;          int input_valid = 1;
690          int bframes_count = 0;          int bframes_count = 0;
# Line 702  Line 723 
723                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
724    
725                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
726                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
727    
728                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
729    
730                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
731                          bframes_count = 0;                          bframes_count = 0;
# Line 728  Line 749 
749    
750                  BitstreamPad(&bs);                  BitstreamPad(&bs);
751                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
752                  pFrame->intra = 0;                  pFrame->intra = 2;
753    
754                  if (input_valid)                  if (input_valid)
755                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
# Line 748  Line 769 
769                     indentical to the future-referece frame.                     indentical to the future-referece frame.
770                  */                  */
771    
772                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
773                          int tmp;                          int tmp;
774    
775                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
# Line 763  Line 784 
784                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
785    
786                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
787                          pFrame->intra = 0;                          pFrame->intra = 4;
788    
789                          if (input_valid)                          if (input_valid)
790                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
# Line 780  Line 801 
801          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->bframenum_dx50bvop != -1)
802          {          {
803    
804                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
805                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
806    
807                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
808                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
809                  }                  }
810    
# Line 795  Line 816 
816    
817          } else if (input_valid) {          } else if (input_valid) {
818    
819                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
820    
821                  start_timer();                  start_timer();
822                  if (image_input                  if (image_input
# Line 821  Line 842 
842    
843          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
844    
845                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
846    
847                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
848                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
# Line 839  Line 860 
860                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
861    
862                          BitstreamPutBits(&bs, 0x7f, 8);                          BitstreamPutBits(&bs, 0x7f, 8);
863                          pFrame->intra = 0;                          pFrame->intra = 5;
864                  }                  }
865    
866                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
# Line 854  Line 875 
875          // 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
876          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
877          {          {
878                    mode = intra2coding_type(pFrame->intra);
879                  if (pFrame->quant == 0)                  if (pFrame->quant == 0)
880                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
881                  else                  else
# Line 881  Line 903 
903    
904                  emms();                  emms();
905    
906                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
907                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
908                                  "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);                                  "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
909                  }                  }
# Line 925  Line 947 
947          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
949           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
950            pEnc->iFrameNum++;
951    
952            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
953                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
954                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
955            {
956                    mode = I_VOP;
957            }else{
958                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
959                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
960                                            (mode < 0) ? pEnc->iFrameNum : 0,
961                                            bframes_count++);
962            }
963    
964          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (mode == I_VOP) {
                 (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&  
                  pEnc->iFrameNum >= pEnc->iMaxKeyInterval)  
                 || 2 == (mode = MEanalysis(&pEnc->reference->image, pEnc->current,  
                                                                         &pEnc->mbParam, pEnc->iMaxKeyInterval,  
                                                                         (pFrame->intra < 0) ? pEnc->iFrameNum : 0,  
                                                                         bframes_count++))) {  
   
965                  /*                  /*
966                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
967                   */                   */
# Line 943  Line 970 
970                  else                  else
971                          pEnc->mbParam.m_quarterpel = 0;                          pEnc->mbParam.m_quarterpel = 0;
972    
973                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
974    
975                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
976                            if (pFrame->quant_intra_matrix != NULL)
977                                    set_intra_matrix(pFrame->quant_intra_matrix);
978                            if (pFrame->quant_inter_matrix != NULL)
979                                    set_inter_matrix(pFrame->quant_inter_matrix);
980                    }
981    
982    
983                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
984                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
985                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
986    
987                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
988                          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");
989                  }                  }
990    
991                  // 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
992    
993                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
994    
995                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
996                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
997    
998                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
999                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1000                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1001                          }                          }
1002                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
# Line 978  Line 1015 
1015    
1016                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1017    
1018                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1019                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1020                          input_valid = 0;                          input_valid = 0;
1021                          goto ipvop_loop;                          goto ipvop_loop;
# Line 988  Line 1025 
1025                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1026                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1027                   */                   */
1028          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {          } else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1029                  /*                  /*
1030                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1031                   */                   */
# Line 997  Line 1034 
1034                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1035                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1036    
1037                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1038                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1039                  }                  }
1040    
# Line 1006  Line 1043 
1043                  pFrame->intra = 0;                  pFrame->intra = 0;
1044                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1045    
1046                  if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1047                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1048                          input_valid = 0;                          input_valid = 0;
1049                          goto ipvop_loop;                          goto ipvop_loop;
1050                  }                  }
1051    
1052          } else {          } else {        /* mode == B_VOP */
1053                  /*                  /*
1054                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1055                   */                   */
1056    
1057                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1058                          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");
1059                  }                  }
1060    
1061                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1062                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1063                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1064                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1065                  } else {                  } else {
1066                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1067                  }                  }
1068    
1069                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
1070                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
1071                    else if (pEnc->current->quant > 31)
                 if (pEnc->current->quant > 31)  
1072                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
1073    
   
1074                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1075                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1076                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1077    
   
   
1078                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1079                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1080                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1081    
1082                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1083    
1084                  pFrame->intra = 0;  // bframe report by koepi
1085                    pFrame->intra = 2;
1086                  pFrame->length = 0;                  pFrame->length = 0;
1087    
1088                  input_valid = 0;                  input_valid = 0;
1089                  goto bvop_loop;                  goto bvop_loop;
1090          }          }
1091    
         pEnc->iFrameNum++;  
   
1092          BitstreamPad(&bs);          BitstreamPad(&bs);
1093          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1094    
1095          if (pResult) {          if (pResult) {
1096                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1097                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1098                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1099                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1100                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1101          }          }
1102    
1103          emms();          emms();
# Line 1075  Line 1108 
1108                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1109                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1110    
1111          snprintf(temp, 127, "PSNR: %f\n", psnr);          printf("PSNR: %f\n", psnr);
1112          DEBUG(temp);  //      DEBUG(temp);
1113  #endif  #endif
1114    
1115          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
# Line 1124  Line 1157 
1157          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1158          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1159    
1160          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1161    
1162          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1163          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
# Line 1227  Line 1260 
1260    
1261          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1262                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == 0)
1263                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1264                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1265                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1266                  } else {                  } else {
1267                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 1249  Line 1282 
1282    
1283          if (pResult) {          if (pResult) {
1284                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1285                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1286                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1287                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1288                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1289          }          }
1290    
1291          emms();          emms();
# Line 1268  Line 1301 
1301                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1302    
1303          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1304          DEBUG(temp);  //      DEBUG(temp);
1305  #endif  #endif
1306    
1307          pEnc->iFrameNum++;          pEnc->iFrameNum++;
# Line 1510  Line 1543 
1543                     Bitstream * bs,                     Bitstream * bs,
1544                     uint32_t * pBits)                     uint32_t * pBits)
1545  {  {
1546            int mb_width = pEnc->mbParam.mb_width;
1547            int mb_height = pEnc->mbParam.mb_height;
1548    
1549          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1550          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1551    
1552          uint16_t x, y;          uint16_t x, y;
1553    
1554            if ((pEnc->current->global_flags & XVID_REDUCED))
1555            {
1556                    mb_width = (pEnc->mbParam.width + 31) / 32;
1557                    mb_height = (pEnc->mbParam.height + 31) / 32;
1558    
1559                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1560                    /* XXX: setedges is overkill */
1561                    start_timer();
1562                    image_setedges(&pEnc->current->image,
1563                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1564                            pEnc->mbParam.width, pEnc->mbParam.height);
1565                    stop_edges_timer();
1566            }
1567    
1568          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1569          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1570          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1524  Line 1573 
1573    
1574          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1575    
 #define DIVX501B481P "DivX501b481p"  
         if ((pEnc->global & XVID_GLOBAL_PACKED)) {  
                 BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));  
         }  
   
 #define XVID_ID "XviD" XVID_BS_VERSION  
         BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID));  
   
1576          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1577          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1578    
1579          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1580    
1581          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1582          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1583          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1584    
1585          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1586                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1587                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1588                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1589    
# Line 1561  Line 1602 
1602                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1603                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1604                          }                          }
1605                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1606                          stop_coding_timer();                          stop_coding_timer();
1607                  }                  }
1608    
1609            if ((pEnc->current->global_flags & XVID_REDUCED))
1610            {
1611                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1612                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1613                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1614            }
1615          emms();          emms();
1616    
1617          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1618          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1619          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1620    
1621          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
# Line 1584  Line 1629 
1629  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1630  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1631    
1632    
1633    /* FrameCodeP also handles S(GMC)-VOPs */
1634  static int  static int
1635  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1636                     Bitstream * bs,                     Bitstream * bs,
# Line 1596  Line 1643 
1643          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1644          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1645    
1646            int mb_width = pEnc->mbParam.mb_width;
1647            int mb_height = pEnc->mbParam.mb_height;
1648    
1649          int iLimit;          int iLimit;
1650          int x, y, k;          int x, y, k;
1651          int iSearchRange;          int iSearchRange;
# Line 1604  Line 1654 
1654          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1655          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1656    
1657            if ((pEnc->current->global_flags & XVID_REDUCED))
1658            {
1659                    mb_width = (pEnc->mbParam.width + 31) / 32;
1660                    mb_height = (pEnc->mbParam.height + 31) / 32;
1661            }
1662    
1663    
1664          start_timer();          start_timer();
1665          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1666                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height);
# Line 1615  Line 1672 
1672          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1673    
1674          if (!force_inter)          if (!force_inter)
1675                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1676          else          else
1677                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1678    
1679          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1680                  start_timer();                  start_timer();
# Line 1631  Line 1686 
1686                  stop_inter_timer();                  stop_inter_timer();
1687          }          }
1688    
         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  
1689                  pEnc->current->coding_type = P_VOP;                  pEnc->current->coding_type = P_VOP;
1690    
1691          start_timer();          start_timer();
1692          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1693                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1694          if (bIntra == 0) {          else
                         pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current);  
                         MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                                                                                         &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);  
                 }  
   
         } else {  
   
1695                  bIntra =                  bIntra =
1696                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1697                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1698                           iLimit);                           iLimit);
1699          }  
1700          stop_motion_timer();          stop_motion_timer();
1701    
1702          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1703    
1704          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )          if ( ( pEnc->current->global_flags & XVID_GMC )
1705                          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) ) )
1706            {
1707                    pEnc->current->coding_type = S_VOP;
1708    
1709                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1710                                            pEnc->mbParam.width, pEnc->mbParam.height,
1711                                            &pEnc->current->gmc_data);
1712    
1713                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1714                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1715                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1716                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1717                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1718    
1719            }
1720          if (vol_header)          if (vol_header)
1721                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1722    
   
1723          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1724          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1725    
1726          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1727    
1728          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1729                  pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1730    
1731          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1732                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1733                    for (x = 0; x < mb_width; x++) {
1734                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1735                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1736    
1737    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1738    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1739    
1740                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1741    
1742                          if (!bIntra) {                          if (bIntra) {
1743                                    CodeIntraMB(pEnc, pMB);
1744                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1745                                                                      dct_codes, qcoeff);
1746    
1747                                    start_timer();
1748                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1749                                    stop_prediction_timer();
1750    
1751                                    pEnc->current->sStat.kblks++;
1752    
1753                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1754                                    stop_coding_timer();
1755                                    continue;
1756                            }
1757    
1758                            if (pEnc->current->coding_type == S_VOP) {
1759    
1760                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1761                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1762                                            pEnc->mbParam.edged_width, 65536);
1763    
1764                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1765    
1766                                            if (pEnc->mbParam.m_quarterpel)
1767                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1768                                            else
1769                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1770    
1771                                            pMB->mode = MODE_INTER;
1772                                            pMB->mcsel = 1;
1773                                            pMB->sad16 = iSAD;
1774                                    } else {
1775                                            pMB->mcsel = 0;
1776                                    }
1777                            } else {
1778                                    pMB->mcsel = 0; /* just a precaution */
1779                            }
1780    
1781                                  start_timer();                                  start_timer();
1782                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1783                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1784                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1785                                                                     &pEnc->current->image,
1786                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1787                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1788                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1789                                                                           pEnc->mbParam.m_quarterpel,                                                                           pEnc->mbParam.m_quarterpel,
1790                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1791                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1792    
1793                                  stop_comp_timer();                                  stop_comp_timer();
1794    
1795                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1708  Line 1807 
1807                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1808    
1809                                  if (pMB->mode != MODE_NOT_CODED)                                  if (pMB->mode != MODE_NOT_CODED)
1810                                          pMB->cbp =                          {       pMB->cbp =
1811                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1812                                                                                    dct_codes, qcoeff);                                                                                    dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1813                          }                          }
1814    
1815                          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->sStat.kblks++;  
                         } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||  
1816                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1817                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1818                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1819                          }  else {                          }  else {
1820                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1821                          }                          }
1822    
1823                          start_timer();                          start_timer();
1824    
1825                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1826    
1827                          skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1828                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == NO_CHANGE);
1829    
1830                            if (pEnc->current->coding_type == S_VOP)
1831                                    skip_possible &= (pMB->mcsel == 1);
1832                            else if (pEnc->current->coding_type == P_VOP) {
1833                          if(pEnc->mbParam.m_quarterpel)                          if(pEnc->mbParam.m_quarterpel)
1834                          {       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) );
                         }  
1835                          else                          else
1836                          {       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) );
1837                          }                          }
1838    
1839                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1840    
1841  /* 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;  
1842    
1843                                  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 */
1844                                    {
1845                                            int bSkip = 1;
1846    
1847                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1848                                          {                                          {
1849                                                  int iSAD;                                                  int iSAD;
# Line 1763  Line 1856 
1856                                                  }                                                  }
1857                                          }                                          }
1858    
1859                                  if (!bSkip)                                          if (!bSkip) {   /* no SKIP, but trivial block */
                                 {  
                                         VECTOR predMV;  
1860                                          if(pEnc->mbParam.m_quarterpel) {                                          if(pEnc->mbParam.m_quarterpel) {
1861                                                  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);
1862                                                  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;
1863                                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1864                                          }                                          }
1865                                          else {                                          else {
1866                                                  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);
1867                                                  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;
1868                                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1869                                          }                                          }
1870                                          pMB->mode = MODE_INTER;                                          pMB->mode = MODE_INTER;
1871                                          pMB->cbp = 0;                                          pMB->cbp = 0;
1872                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1873                                                    stop_coding_timer();
1874    
1875                                                    continue;       /* next MB */
1876                                  }                                  }
1877                                  else                                  }
1878                                  {                                  /* do SKIP */
1879    
1880                                          pMB->mode = MODE_NOT_CODED;                                          pMB->mode = MODE_NOT_CODED;
1881                                          MBSkip(bs);                                          MBSkip(bs);
1882                                    stop_coding_timer();
1883                                    continue;       /* next MB */
1884                                  }                                  }
1885                            /* ordinary case: normal coded INTER/INTER4V block */
1886    
                         } else {  
1887                                  if (pEnc->current->global_flags & XVID_GREYSCALE)                                  if (pEnc->current->global_flags & XVID_GREYSCALE)
1888                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1889                                          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 */
1890                                          qcoeff[5*64+0]=0;                                          qcoeff[5*64+0]=0;
1891                                  }                                  }
1892                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1893                            if(pEnc->mbParam.m_quarterpel) {
1894                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1895                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1896                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1897                                    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);
1898                            } else {
1899                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1900                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1901                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1902                                    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);
1903                            }
1904    
1905    
1906                            if (pMB->mode == MODE_INTER4V)
1907                            {       int k;
1908                                    for (k=1;k<4;k++)
1909                                    {
1910                                            if(pEnc->mbParam.m_quarterpel) {
1911                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1912                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1913                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1914                                    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);
1915                                            } else {
1916                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1917                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1918                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1919                                    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);
1920                                            }
1921    
1922                                    }
1923                          }                          }
1924    
1925                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1926                          stop_coding_timer();                          stop_coding_timer();
1927    
1928                  }                  }
1929          }          }
1930    
1931            if ((pEnc->current->global_flags & XVID_REDUCED))
1932            {
1933                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1934                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1935                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1936            }
1937    
1938          emms();          emms();
1939    
1940          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1941                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1942          }          }
1943    
1944          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
1945                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
1946    
1947          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
1948    
1949          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1950    
# Line 1818  Line 1954 
1954                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1955                  iSearchRange *= 2;                  iSearchRange *= 2;
1956          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1957                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1958                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1959                          && (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
1960          {          {
1961                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1962                  iSearchRange /= 2;                  iSearchRange /= 2;
1963          }          }
1964    
1965          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1966    
 #ifdef FRAMEDROP  
1967          /* frame drop code */          /* frame drop code */
1968          // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
1969          if (pEnc->sStat.kblks + pEnc->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
1970                  (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
1971          {          {
1972                  pEnc->sStat.kblks = pEnc->sStat.mblks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
1973                  pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;                  pEnc->current->sStat.ublks = mb_width * mb_height;
1974    
1975                  BitstreamReset(bs);                  BitstreamReset(bs);
1976    
# Line 1850  Line 1985 
1985                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1986                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1987                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1988                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1989            }
1990    
1991            /* XXX: debug
1992            {
1993                    char s[100];
1994                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1995                    image_dump_yuvpgm(&pEnc->current->image,
1996                            pEnc->mbParam.edged_width,
1997                            pEnc->mbParam.width, pEnc->mbParam.height, s);
1998    
1999                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2000                    image_dump_yuvpgm(&pEnc->reference->image,
2001                            pEnc->mbParam.edged_width,
2002                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2003          }          }
2004  #endif          */
2005    
2006    
2007          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2008    
# Line 1861  Line 2010 
2010  }  }
2011    
2012    
2013  static __inline void  static void
2014  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2015                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2016                     Bitstream * bs,                     Bitstream * bs,
2017                     uint32_t * pBits)                     uint32_t * pBits)
2018  {  {
2019          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2020          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2021          uint32_t x, y;          uint32_t x, y;
2022    
2023          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
# Line 1881  Line 2030 
2030                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2031          }          }
2032    
2033            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2034    
2035          if (!first){          if (!first){
2036                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2037          }          }
# Line 1933  Line 2084 
2084    
2085          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2086    
2087          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2088          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2089          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2090          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2091    
2092    
2093          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2094                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2095                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2096                          int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2097    
2098                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2099                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1974  Line 2125 
2125  #endif  #endif
2126                          start_timer();                          start_timer();
2127                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2128                                                   &pEnc->sStat, direction);                                                   &frame->sStat, direction);
2129                          stop_coding_timer();                          stop_coding_timer();
2130                  }                  }
2131          }          }
# Line 1993  Line 2144 
2144          }          }
2145  #endif  #endif
2146  }  }
   
   
 /*      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.19  
changed lines
  Added in v.1.76.2.36

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