[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.40, Tue Jan 21 22:05:44 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 = -1;
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;
732    
733                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
734                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
735                          pFrame->intra = 0;                          pFrame->intra = 0;
736    
737    
738                          emms();                          emms();
739    
740                            if (pResult) {
741                                    pResult->quant = pEnc->current->quant;
742                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
743                                    pResult->kblks = pEnc->current->sStat.kblks;
744                                    pResult->mblks = pEnc->current->sStat.mblks;
745                                    pResult->ublks = pEnc->current->sStat.ublks;
746                            }
747    
748                          return XVID_ERR_OK;                          return XVID_ERR_OK;
749                  }                  }
750    
# Line 726  Line 756 
756                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
757                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
758    
759                  BitstreamPad(&bs);                  BitstreamPadAlways(&bs);
760                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
761                  pFrame->intra = 0;                  pFrame->intra = 2;
762    
763                    if (pResult) {
764                            pResult->quant = pEnc->current->quant;
765                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
766                            pResult->kblks = pEnc->current->sStat.kblks;
767                            pResult->mblks = pEnc->current->sStat.mblks;
768                            pResult->ublks = pEnc->current->sStat.ublks;
769                    }
770    
771                  if (input_valid)                  if (input_valid)
772                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
# Line 748  Line 786 
786                     indentical to the future-referece frame.                     indentical to the future-referece frame.
787                  */                  */
788    
789                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
790                          int tmp;                          int tmp;
791    
792                          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",
793                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
794                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
795    
                         BitstreamPad(&bs);  
796    
797                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
798                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
799    
800                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
801                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
802    
803                            BitstreamPadAlways(&bs);
804                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
805                          pFrame->intra = 0;                          pFrame->intra = 4;
806    
807                            if (pResult) {
808                                    pResult->quant = pEnc->current->quant;
809                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
810                                    pResult->kblks = pEnc->current->sStat.kblks;
811                                    pResult->mblks = pEnc->current->sStat.mblks;
812                                    pResult->ublks = pEnc->current->sStat.ublks;
813                            }
814    
815                          if (input_valid)                          if (input_valid)
816                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
# Line 780  Line 827 
827          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->bframenum_dx50bvop != -1)
828          {          {
829    
830                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
831                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
832    
833                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
834                          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");
835                  }                  }
836    
# Line 795  Line 842 
842    
843          } else if (input_valid) {          } else if (input_valid) {
844    
845                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
846    
847                  start_timer();                  start_timer();
848                  if (image_input                  if (image_input
# Line 821  Line 868 
868    
869          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
870    
871                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
872    
873                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
874                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
# Line 838  Line 885 
885                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
886                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
887    
888                          BitstreamPutBits(&bs, 0x7f, 8);                  //      BitstreamPutBits(&bs, 0x7f, 8);
889                          pFrame->intra = 0;                          pFrame->intra = 5;
890    
891                            if (pResult) {
892                                    /*
893                                     * We must decide what to put there because i know some apps
894                                     * are storing statistics about quantizers and just do
895                                     * stats[quant]++ or stats[quant-1]++
896                                     * transcode is one of these app with its 2pass module
897                                     */
898    
899                                    /*
900                                     * For now i prefer 31 than 0 that could lead to a segfault
901                                     * in transcode
902                                     */
903                                    pResult->quant = 31;
904    
905                                    pResult->hlength = 0;
906                                    pResult->kblks = 0;
907                                    pResult->mblks = 0;
908                                    pResult->ublks = 0;
909                            }
910    
911                    } else {
912    
913                            if (pResult) {
914                                    pResult->quant = pEnc->current->quant;
915                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
916                                    pResult->kblks = pEnc->current->sStat.kblks;
917                                    pResult->mblks = pEnc->current->sStat.mblks;
918                                    pResult->ublks = pEnc->current->sStat.ublks;
919                            }
920    
921                  }                  }
922    
923                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
924    
925                  emms();                  emms();
926    
927                  return XVID_ERR_OK;                  return XVID_ERR_OK;
928          }          }
929    
# Line 854  Line 934 
934          // 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
935          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
936          {          {
937                    mode = intra2coding_type(pFrame->intra);
938                  if (pFrame->quant == 0)                  if (pFrame->quant == 0)
939                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
940                  else                  else
# Line 881  Line 962 
962    
963                  emms();                  emms();
964    
965                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
966                          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,
967                                  "%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);
968                  }                  }
# Line 925  Line 1006 
1006          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1007           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
1008           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1009            pEnc->iFrameNum++;
1010    
1011            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1012                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1013                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
1014            {
1015                    mode = I_VOP;
1016            }else{
1017                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1018                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1019                                            (mode < 0) ? pEnc->iFrameNum : 0,
1020                                            bframes_count++);
1021            }
1022    
1023          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++))) {  
   
1024                  /*                  /*
1025                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
1026                   */                   */
# Line 943  Line 1029 
1029                  else                  else
1030                          pEnc->mbParam.m_quarterpel = 0;                          pEnc->mbParam.m_quarterpel = 0;
1031    
1032                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1033    
1034                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1035                            if (pFrame->quant_intra_matrix != NULL)
1036                                    set_intra_matrix(pFrame->quant_intra_matrix);
1037                            if (pFrame->quant_inter_matrix != NULL)
1038                                    set_inter_matrix(pFrame->quant_inter_matrix);
1039                    }
1040    
1041    
1042                  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",
1043                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1044                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1045    
1046                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1047                          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");
1048                  }                  }
1049    
1050                  // 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
1051    
1052                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1053    
1054                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
1055                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1056    
1057                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1058                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1059                                  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");
1060                          }                          }
1061                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
1062                          bframes_count = 0;                          bframes_count = 0;
   
1063                          pFrame->intra = 0;                          pFrame->intra = 0;
1064    
1065                  } else {                  } else {
# Line 978  Line 1073 
1073    
1074                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1075    
1076                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1077                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1078                          input_valid = 0;                          input_valid = 0;
1079                          goto ipvop_loop;                          goto ipvop_loop;
# Line 988  Line 1083 
1083                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1084                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1085                   */                   */
1086          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {          } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1087                  /*                  /*
1088                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1089                   */                   */
# Line 997  Line 1092 
1092                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1093                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1094    
1095                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1096                          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");
1097                  }                  }
1098    
# Line 1006  Line 1101 
1101                  pFrame->intra = 0;                  pFrame->intra = 0;
1102                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1103    
1104                  if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1105                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1106                          input_valid = 0;                          input_valid = 0;
1107                          goto ipvop_loop;                          goto ipvop_loop;
1108                  }                  }
1109    
1110          } else {          } else {        /* mode == B_VOP */
1111                  /*                  /*
1112                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1113                   */                   */
1114    
1115                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1116                          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");
1117                  }                  }
1118    
1119                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1120                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1121                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1122                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1123                  } else {                  } else {
1124                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1125                  }                  }
1126    
1127                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
1128                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
1129                    else if (pEnc->current->quant > 31)
                 if (pEnc->current->quant > 31)  
1130                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
1131    
   
1132                          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",
1133                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1134                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1135    
   
   
1136                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1137                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1138                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1139    
1140                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1141    
1142                  pFrame->intra = 0;  // bframe report by koepi
1143                    pFrame->intra = 2;
1144                  pFrame->length = 0;                  pFrame->length = 0;
1145    
1146                  input_valid = 0;                  input_valid = 0;
1147                  goto bvop_loop;                  goto bvop_loop;
1148          }          }
1149    
1150          pEnc->iFrameNum++;          BitstreamPadAlways(&bs);
   
         BitstreamPad(&bs);  
1151          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1152    
1153          if (pResult) {          if (pResult) {
1154                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1155                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1156                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1157                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1158                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1159          }          }
1160    
1161          emms();          emms();
# Line 1075  Line 1166 
1166                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1167                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1168    
1169          snprintf(temp, 127, "PSNR: %f\n", psnr);          printf("PSNR: %f\n", psnr);
1170          DEBUG(temp);  //      DEBUG(temp);
1171  #endif  #endif
1172    
1173          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
# Line 1124  Line 1215 
1215          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1216          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1217    
1218          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1219    
1220          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1221          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
# Line 1227  Line 1318 
1318    
1319          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1320                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == 0)
1321                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1322                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1323                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1324                  } else {                  } else {
1325                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 1242  Line 1333 
1333    
1334          }          }
1335    
1336          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1337          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1338          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1339          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1340    
1341          if (pResult) {          if (pResult) {
1342                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1343                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1344                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1345                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1346                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1347          }          }
1348    
1349          emms();          emms();
# Line 1268  Line 1359 
1359                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1360    
1361          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1362          DEBUG(temp);  //      DEBUG(temp);
1363  #endif  #endif
1364    
1365          pEnc->iFrameNum++;          pEnc->iFrameNum++;
# Line 1435  Line 1526 
1526    
1527          if (intra) {          if (intra) {
1528                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1529                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1530                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1531                  }                  }
1532                  return;                  return;
# Line 1510  Line 1601 
1601                     Bitstream * bs,                     Bitstream * bs,
1602                     uint32_t * pBits)                     uint32_t * pBits)
1603  {  {
1604            int mb_width = pEnc->mbParam.mb_width;
1605            int mb_height = pEnc->mbParam.mb_height;
1606    
1607          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1608          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1609    
1610          uint16_t x, y;          uint16_t x, y;
1611    
1612            if ((pEnc->current->global_flags & XVID_REDUCED))
1613            {
1614                    mb_width = (pEnc->mbParam.width + 31) / 32;
1615                    mb_height = (pEnc->mbParam.height + 31) / 32;
1616    
1617                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1618                    /* XXX: setedges is overkill */
1619                    start_timer();
1620                    image_setedges(&pEnc->current->image,
1621                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1622                            pEnc->mbParam.width, pEnc->mbParam.height);
1623                    stop_edges_timer();
1624            }
1625    
1626          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1627          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1628          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1524  Line 1631 
1631    
1632          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1633    
 #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));  
   
1634          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1635    
1636            BitstreamPadAlways(bs);
1637          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1638    
1639          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1640    
1641          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1642          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1643          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1644    
1645          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1646                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1647                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1648                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1649    
# Line 1561  Line 1662 
1662                                  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 */
1663                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1664                          }                          }
1665                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1666                          stop_coding_timer();                          stop_coding_timer();
1667                  }                  }
1668    
1669            if ((pEnc->current->global_flags & XVID_REDUCED))
1670            {
1671                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1672                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1673                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1674            }
1675          emms();          emms();
1676    
1677          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1678          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1679          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1680    
1681          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
# Line 1584  Line 1689 
1689  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1690  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1691    
1692    
1693    /* FrameCodeP also handles S(GMC)-VOPs */
1694  static int  static int
1695  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1696                     Bitstream * bs,                     Bitstream * bs,
# Line 1596  Line 1703 
1703          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1704          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1705    
1706            int mb_width = pEnc->mbParam.mb_width;
1707            int mb_height = pEnc->mbParam.mb_height;
1708    
1709          int iLimit;          int iLimit;
1710          int x, y, k;          int x, y, k;
1711          int iSearchRange;          int iSearchRange;
# Line 1604  Line 1714 
1714          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1715          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1716    
1717            if ((pEnc->current->global_flags & XVID_REDUCED))
1718            {
1719                    mb_width = (pEnc->mbParam.width + 31) / 32;
1720                    mb_height = (pEnc->mbParam.height + 31) / 32;
1721            }
1722    
1723    
1724          start_timer();          start_timer();
1725          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1726                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height);
# Line 1615  Line 1732 
1732          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1733    
1734          if (!force_inter)          if (!force_inter)
1735                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1736          else          else
1737                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1738    
1739          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1740                  start_timer();                  start_timer();
# Line 1631  Line 1746 
1746                  stop_inter_timer();                  stop_inter_timer();
1747          }          }
1748    
         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  
1749                  pEnc->current->coding_type = P_VOP;                  pEnc->current->coding_type = P_VOP;
1750    
1751          start_timer();          start_timer();
1752          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1753                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1754          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 {  
   
1755                  bIntra =                  bIntra =
1756                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1757                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1758                           iLimit);                           iLimit);
1759          }  
1760          stop_motion_timer();          stop_motion_timer();
1761    
1762          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1763    
1764          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )          if ( ( pEnc->current->global_flags & XVID_GMC )
1765                          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) ) )
1766            {
1767                    pEnc->current->coding_type = S_VOP;
1768    
1769                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1770                                            pEnc->mbParam.width, pEnc->mbParam.height,
1771                                            &pEnc->current->gmc_data);
1772    
1773                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1774                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1775                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1776                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1777                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1778    
1779          if (vol_header)          }
                 BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);  
   
1780    
1781          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1782            if (vol_header)
1783            {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1784                    BitstreamPadAlways(bs);
1785            }
1786    
1787          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1788    
1789          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1790    
1791          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1792                  pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1793    
1794          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1795                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1796                    for (x = 0; x < mb_width; x++) {
1797                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1798                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1799    
1800    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1801    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1802    
1803                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1804    
1805                          if (!bIntra) {                          if (bIntra) {
1806                                    CodeIntraMB(pEnc, pMB);
1807                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1808                                                                      dct_codes, qcoeff);
1809    
1810                                    start_timer();
1811                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1812                                    stop_prediction_timer();
1813    
1814                                    pEnc->current->sStat.kblks++;
1815    
1816                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1817                                    stop_coding_timer();
1818                                    continue;
1819                            }
1820    
1821                            if (pEnc->current->coding_type == S_VOP) {
1822    
1823                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1824                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1825                                            pEnc->mbParam.edged_width, 65536);
1826    
1827                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1828                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1829                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1830    
1831                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1832                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1833                                    }
1834    
1835                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1836    
1837                                            if (pEnc->mbParam.m_quarterpel)
1838                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1839                                            else
1840                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1841    
1842                                            pMB->mode = MODE_INTER;
1843                                            pMB->mcsel = 1;
1844                                            pMB->sad16 = iSAD;
1845                                    } else {
1846                                            pMB->mcsel = 0;
1847                                    }
1848                            } else {
1849                                    pMB->mcsel = 0; /* just a precaution */
1850                            }
1851    
1852                                  start_timer();                                  start_timer();
1853                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1854                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1855                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1856                                                                     &pEnc->current->image,
1857                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1858                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1859                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1860                                                                           pEnc->mbParam.m_quarterpel,                                                                           pEnc->mbParam.m_quarterpel,
1861                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1862                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1863    
1864                                  stop_comp_timer();                                  stop_comp_timer();
1865    
1866                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1708  Line 1878 
1878                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1879    
1880                                  if (pMB->mode != MODE_NOT_CODED)                                  if (pMB->mode != MODE_NOT_CODED)
1881                                          pMB->cbp =                          {       pMB->cbp =
1882                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1883                                                                                    dct_codes, qcoeff);                                                                                    dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1884                          }                          }
1885    
1886                          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 ||  
1887                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1888                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1889                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1890                          }  else {                          }  else {
1891                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1892                          }                          }
1893    
1894                          start_timer();                          start_timer();
1895    
1896                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1897    
1898                          skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1899                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == NO_CHANGE);
1900    
1901                            if (pEnc->current->coding_type == S_VOP)
1902                                    skip_possible &= (pMB->mcsel == 1);
1903                            else if (pEnc->current->coding_type == P_VOP) {
1904                          if(pEnc->mbParam.m_quarterpel)                          if(pEnc->mbParam.m_quarterpel)
1905                          {       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) );
                         }  
1906                          else                          else
1907                          {       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) );
1908                          }                          }
1909    
1910                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1911    
1912  /* 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;  
1913    
1914                                  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 */
1915                                    {
1916                                            int bSkip = 1;
1917    
1918                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1919                                          {                                          {
1920                                                  int iSAD;                                                  int iSAD;
# Line 1763  Line 1927 
1927                                                  }                                                  }
1928                                          }                                          }
1929    
1930                                  if (!bSkip)                                          if (!bSkip) {   /* no SKIP, but trivial block */
                                 {  
                                         VECTOR predMV;  
1931                                          if(pEnc->mbParam.m_quarterpel) {                                          if(pEnc->mbParam.m_quarterpel) {
1932                                                  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);
1933                                                  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;
1934                                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1935                                          }                                          }
1936                                          else {                                          else {
1937                                                  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);
1938                                                  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;
1939                                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1940                                          }                                          }
1941                                          pMB->mode = MODE_INTER;                                          pMB->mode = MODE_INTER;
1942                                          pMB->cbp = 0;                                          pMB->cbp = 0;
1943                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1944                                                    stop_coding_timer();
1945    
1946                                                    continue;       /* next MB */
1947                                  }                                  }
1948                                  else                                  }
1949                                  {                                  /* do SKIP */
1950    
1951                                          pMB->mode = MODE_NOT_CODED;                                          pMB->mode = MODE_NOT_CODED;
1952                                          MBSkip(bs);                                          MBSkip(bs);
1953                                    stop_coding_timer();
1954                                    continue;       /* next MB */
1955                                  }                                  }
1956                            /* ordinary case: normal coded INTER/INTER4V block */
1957    
                         } else {  
1958                                  if (pEnc->current->global_flags & XVID_GREYSCALE)                                  if (pEnc->current->global_flags & XVID_GREYSCALE)
1959                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1960                                          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 */
1961                                          qcoeff[5*64+0]=0;                                          qcoeff[5*64+0]=0;
1962                                  }                                  }
1963                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1964                            if(pEnc->mbParam.m_quarterpel) {
1965                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1966                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1967                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1968                                    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);
1969                            } else {
1970                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1971                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1972                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1973                                    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);
1974                            }
1975    
1976    
1977                            if (pMB->mode == MODE_INTER4V)
1978                            {       int k;
1979                                    for (k=1;k<4;k++)
1980                                    {
1981                                            if(pEnc->mbParam.m_quarterpel) {
1982                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1983                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1984                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1985                                    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);
1986                                            } else {
1987                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1988                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1989                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1990                                    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);
1991                                            }
1992    
1993                                    }
1994                          }                          }
1995    
1996                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1997                          stop_coding_timer();                          stop_coding_timer();
1998    
1999                  }                  }
2000          }          }
2001    
2002            if ((pEnc->current->global_flags & XVID_REDUCED))
2003            {
2004                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2005                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2006                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2007            }
2008    
2009          emms();          emms();
2010    
2011          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
2012                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2013          }          }
2014    
2015          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2016                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2017    
2018          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2019    
2020          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2021    
# Line 1818  Line 2025 
2025                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2026                  iSearchRange *= 2;                  iSearchRange *= 2;
2027          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2028                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2029                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2030                          && (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
2031          {          {
2032                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2033                  iSearchRange /= 2;                  iSearchRange /= 2;
2034          }          }
2035    
2036          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2037    
 #ifdef FRAMEDROP  
2038          /* frame drop code */          /* frame drop code */
2039          // 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);
2040          if (pEnc->sStat.kblks + pEnc->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2041                  (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2042          {          {
2043                  pEnc->sStat.kblks = pEnc->sStat.mblks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2044                  pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;                  pEnc->current->sStat.ublks = mb_width * mb_height;
2045    
2046                  BitstreamReset(bs);                  BitstreamReset(bs);
2047    
# Line 1850  Line 2056 
2056                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
2057                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
2058                  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);
2059                  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);
2060            }
2061    
2062            /* XXX: debug
2063            {
2064                    char s[100];
2065                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2066                    image_dump_yuvpgm(&pEnc->current->image,
2067                            pEnc->mbParam.edged_width,
2068                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2069    
2070                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2071                    image_dump_yuvpgm(&pEnc->reference->image,
2072                            pEnc->mbParam.edged_width,
2073                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2074          }          }
2075  #endif          */
2076    
2077    
2078          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2079    
# Line 1861  Line 2081 
2081  }  }
2082    
2083    
2084  static __inline void  static void
2085  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2086                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2087                     Bitstream * bs,                     Bitstream * bs,
2088                     uint32_t * pBits)                     uint32_t * pBits)
2089  {  {
2090          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2091          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2092          uint32_t x, y;          uint32_t x, y;
2093    
2094          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
# Line 1881  Line 2101 
2101                  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); \
2102          }          }
2103    
2104            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2105    
2106          if (!first){          if (!first){
2107                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2108          }          }
# Line 1933  Line 2155 
2155    
2156          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2157    
2158          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2159          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2160          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2161          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2162    
2163    
2164          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2165                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2166                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2167                          int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2168    
2169                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2170                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1974  Line 2196 
2196  #endif  #endif
2197                          start_timer();                          start_timer();
2198                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2199                                                   &pEnc->sStat, direction);                                                   &frame->sStat, direction);
2200                          stop_coding_timer();                          stop_coding_timer();
2201                  }                  }
2202          }          }
# Line 1993  Line 2215 
2215          }          }
2216  #endif  #endif
2217  }  }
   
   
 /*      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.40

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