[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

revision 642, Mon Nov 11 20:53:34 2002 UTC revision 786, Mon Jan 13 23:52:50 2003 UTC
# Line 39  Line 39 
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
41   *   *
42   *  $Id: encoder.c,v 1.76.2.17 2002-11-11 20:53:34 Isibaar Exp $   *  $Id: encoder.c,v 1.76.2.38 2003-01-13 23:52:50 edgomez Exp $
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            if (intra == 3) return S_VOP;
665    
666            return P_VOP;
667    }
668    
669    
670    
671  /*****************************************************************************  /*****************************************************************************
# Line 663  Line 684 
684  {  {
685          uint16_t x, y;          uint16_t x, y;
686          Bitstream bs;          Bitstream bs;
687          uint32_t bits, mode;          uint32_t bits;
688            int mode;
689    
690          int input_valid = 1;          int input_valid = 1;
691            int bframes_count = 0;
692    
693  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
694          float psnr;          float psnr;
# Line 701  Line 724 
724                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
725    
726                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
727                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
728    
729                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
730    
731                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
732                            bframes_count = 0;
733    
734                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
735                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
736                            if(pEnc->current->coding_type == P_VOP)
737                          pFrame->intra = 0;                          pFrame->intra = 0;
738                            else
739                                    pFrame->intra = 3;
740    
741                          emms();                          emms();
742    
743                            if (pResult) {
744                                    pResult->quant = pEnc->current->quant;
745                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
746                                    pResult->kblks = pEnc->current->sStat.kblks;
747                                    pResult->mblks = pEnc->current->sStat.mblks;
748                                    pResult->ublks = pEnc->current->sStat.ublks;
749                            }
750    
751                          return XVID_ERR_OK;                          return XVID_ERR_OK;
752                  }                  }
753    
# Line 724  Line 759 
759                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
760                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
761    
762                  BitstreamPad(&bs);                  BitstreamPadAlways(&bs);
763                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
764                  pFrame->intra = 0;                  pFrame->intra = 2;
765    
766                    if (pResult) {
767                            pResult->quant = pEnc->current->quant;
768                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
769                            pResult->kblks = pEnc->current->sStat.kblks;
770                            pResult->mblks = pEnc->current->sStat.mblks;
771                            pResult->ublks = pEnc->current->sStat.ublks;
772                    }
773    
774                  if (input_valid)                  if (input_valid)
775                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
# Line 746  Line 789 
789                     indentical to the future-referece frame.                     indentical to the future-referece frame.
790                  */                  */
791    
792                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
793                          int tmp;                          int tmp;
794    
795                          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",
796                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
797                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
798    
                         BitstreamPad(&bs);  
799    
800                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
801                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
802    
803                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
804                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
805    
806                            BitstreamPadAlways(&bs);
807                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
808                          pFrame->intra = 0;                          pFrame->intra = 4;
809    
810                            if (pResult) {
811                                    pResult->quant = pEnc->current->quant;
812                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
813                                    pResult->kblks = pEnc->current->sStat.kblks;
814                                    pResult->mblks = pEnc->current->sStat.mblks;
815                                    pResult->ublks = pEnc->current->sStat.ublks;
816                            }
817    
818                          if (input_valid)                          if (input_valid)
819                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
# Line 778  Line 830 
830          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->bframenum_dx50bvop != -1)
831          {          {
832    
833                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
834                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
835    
836                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
837                          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");
838                  }                  }
839    
# Line 793  Line 845 
845    
846          } else if (input_valid) {          } else if (input_valid) {
847    
848                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
849    
850                  start_timer();                  start_timer();
851                  if (image_input                  if (image_input
# Line 819  Line 871 
871    
872          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
873    
874                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
875    
876                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
877                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
# Line 836  Line 888 
888                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
889                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
890    
891                          BitstreamPutBits(&bs, 0x7f, 8);                  //      BitstreamPutBits(&bs, 0x7f, 8);
892                          pFrame->intra = 0;                          pFrame->intra = 5;
893    
894                            if (pResult) {
895                                    /*
896                                     * We must decide what to put there because i know some apps
897                                     * are storing statistics about quantizers and just do
898                                     * stats[quant]++ or stats[quant-1]++
899                                     * transcode is one of these app with its 2pass module
900                                     */
901    
902                                    /*
903                                     * For now i prefer 31 than 0 that could lead to a segfault
904                                     * in transcode
905                                     */
906                                    pResult->quant = 31;
907    
908                                    pResult->hlength = 0;
909                                    pResult->kblks = 0;
910                                    pResult->mblks = 0;
911                                    pResult->ublks = 0;
912                            }
913    
914                    } else {
915    
916                            if (pResult) {
917                                    pResult->quant = pEnc->current->quant;
918                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
919                                    pResult->kblks = pEnc->current->sStat.kblks;
920                                    pResult->mblks = pEnc->current->sStat.mblks;
921                                    pResult->ublks = pEnc->current->sStat.ublks;
922                            }
923    
924                  }                  }
925    
926                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
927    
928                  emms();                  emms();
929    
930                  return XVID_ERR_OK;                  return XVID_ERR_OK;
931          }          }
932    
# Line 852  Line 937 
937          // only inc frame num, adapt quant, etc. if we havent seen it before          // only inc frame num, adapt quant, etc. if we havent seen it before
938          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
939          {          {
940                    mode = intra2coding_type(pFrame->intra);
941                  if (pFrame->quant == 0)                  if (pFrame->quant == 0)
942                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
943                  else                  else
# Line 879  Line 965 
965    
966                  emms();                  emms();
967    
968                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
969                          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,
970                                  "%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);
971                  }                  }
# Line 923  Line 1009 
1009          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
1011           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1012            pEnc->iFrameNum++;
1013    
1014            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1015                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1016                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
1017            {
1018                    mode = I_VOP;
1019            }else{
1020                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1021                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1022                                            (mode < 0) ? pEnc->iFrameNum : 0,
1023                                            bframes_count++);
1024            }
1025    
1026          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)  
                 || /*image_mad(&pEnc->reference->image, &pEnc->current->image,  
                                          pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                          pEnc->mbParam.height) > 30) {*/  
                         2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,  
                                          &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {  
   
1027                  /*                  /*
1028                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
1029                   */                   */
1030                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1031                            pEnc->mbParam.m_quarterpel = 1;
1032                    else
1033                            pEnc->mbParam.m_quarterpel = 0;
1034    
1035                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1036    
1037                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1038                            if (pFrame->quant_intra_matrix != NULL)
1039                                    set_intra_matrix(pFrame->quant_intra_matrix);
1040                            if (pFrame->quant_inter_matrix != NULL)
1041                                    set_inter_matrix(pFrame->quant_inter_matrix);
1042                    }
1043    
1044    
1045                  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",
1046                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1047                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1048    
1049                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1050                          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");
1051                  }                  }
1052    
1053                  // 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
1054    
1055                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1056    
1057                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
1058                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1059    
1060                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1061                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1062                                  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");
1063                          }                          }
1064                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
1065                            bframes_count = 0;
1066    
1067                            if(pEnc->current->coding_type == P_VOP)
1068                          pFrame->intra = 0;                          pFrame->intra = 0;
1069                            else
1070                                    pFrame->intra = 3;
1071    
1072    
1073                  } else {                  } else {
1074    
1075                          FrameCodeI(pEnc, &bs, &bits);                          FrameCodeI(pEnc, &bs, &bits);
1076                            bframes_count = 0;
1077                          pFrame->intra = 1;                          pFrame->intra = 1;
1078    
1079                          pEnc->bframenum_dx50bvop = -1;                          pEnc->bframenum_dx50bvop = -1;
# Line 971  Line 1081 
1081    
1082                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1083    
1084                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1085                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1086                          input_valid = 0;                          input_valid = 0;
1087                          goto ipvop_loop;                          goto ipvop_loop;
# Line 981  Line 1091 
1091                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1092                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1093                   */                   */
1094          } 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) {
   
1095                  /*                  /*
1096                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1097                   */                   */
# Line 991  Line 1100 
1100                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1101                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1102    
1103                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1104                          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");
1105                  }                  }
1106    
1107                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1108                    bframes_count = 0;
1109                    if(pEnc->current->coding_type == P_VOP)
1110                  pFrame->intra = 0;                  pFrame->intra = 0;
1111                    else
1112                            pFrame->intra = 3;
1113                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1114    
1115                  if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1116                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1117                          input_valid = 0;                          input_valid = 0;
1118                          goto ipvop_loop;                          goto ipvop_loop;
1119                  }                  }
1120    
1121          } else {          } else {        /* mode == B_VOP */
1122                  /*                  /*
1123                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1124                   */                   */
1125    
1126                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1127                          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");
1128                  }                  }
1129    
1130                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1131                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1132                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1133                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1134                  } else {                  } else {
1135                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1136                  }                  }
1137    
1138                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
1139                          pEnc->current->quant = 1;                          pEnc->current->quant = 1;
1140                    else if (pEnc->current->quant > 31)
                 if (pEnc->current->quant > 31)  
1141                          pEnc->current->quant = 31;                          pEnc->current->quant = 31;
1142    
   
1143                          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",
1144                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1145                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1146    
   
   
1147                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1148                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1149                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1150    
1151                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1152    
1153                  pFrame->intra = 0;  // bframe report by koepi
1154                    pFrame->intra = 2;
1155                  pFrame->length = 0;                  pFrame->length = 0;
1156    
1157                  input_valid = 0;                  input_valid = 0;
1158                  goto bvop_loop;                  goto bvop_loop;
1159          }          }
1160    
1161          pEnc->iFrameNum++;          BitstreamPadAlways(&bs);
   
         BitstreamPad(&bs);  
1162          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1163    
1164          if (pResult) {          if (pResult) {
1165                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1166                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1167                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1168                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1169                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1170          }          }
1171    
1172          emms();          emms();
# Line 1068  Line 1177 
1177                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1178                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1179    
1180          snprintf(temp, 127, "PSNR: %f\n", psnr);          printf("PSNR: %f\n", psnr);
1181          DEBUG(temp);  //      DEBUG(temp);
1182  #endif  #endif
1183    
1184          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
# Line 1117  Line 1226 
1226          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1227          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1228    
1229          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1230    
1231          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1232          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
# Line 1220  Line 1329 
1329    
1330          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1331                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == 0)
1332                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1333                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1334                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1335                  } else {                  } else {
1336                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 1235  Line 1344 
1344    
1345          }          }
1346    
1347          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1348          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1349          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1350          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1351    
1352          if (pResult) {          if (pResult) {
1353                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1354                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1355                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1356                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1357                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1358          }          }
1359    
1360          emms();          emms();
# Line 1261  Line 1370 
1370                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1371    
1372          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1373          DEBUG(temp);  //      DEBUG(temp);
1374  #endif  #endif
1375    
1376          pEnc->iFrameNum++;          pEnc->iFrameNum++;
# Line 1428  Line 1537 
1537    
1538          if (intra) {          if (intra) {
1539                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1540                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1541                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1542                  }                  }
1543                  return;                  return;
# Line 1503  Line 1612 
1612                     Bitstream * bs,                     Bitstream * bs,
1613                     uint32_t * pBits)                     uint32_t * pBits)
1614  {  {
1615            int mb_width = pEnc->mbParam.mb_width;
1616            int mb_height = pEnc->mbParam.mb_height;
1617    
1618          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1619          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1620    
1621          uint16_t x, y;          uint16_t x, y;
1622    
1623            if ((pEnc->current->global_flags & XVID_REDUCED))
1624            {
1625                    mb_width = (pEnc->mbParam.width + 31) / 32;
1626                    mb_height = (pEnc->mbParam.height + 31) / 32;
1627    
1628                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1629                    /* XXX: setedges is overkill */
1630                    start_timer();
1631                    image_setedges(&pEnc->current->image,
1632                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1633                            pEnc->mbParam.width, pEnc->mbParam.height);
1634                    stop_edges_timer();
1635            }
1636    
1637          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1638          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1639          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1517  Line 1642 
1642    
1643          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1644    
 #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));  
   
1645          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1646    
1647            BitstreamPadAlways(bs);
1648          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1649    
1650          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1651    
1652          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1653          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1654          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1655    
1656          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1657                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1658                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1659                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1660    
# Line 1554  Line 1673 
1673                                  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 */
1674                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1675                          }                          }
1676                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1677                          stop_coding_timer();                          stop_coding_timer();
1678                  }                  }
1679    
1680            if ((pEnc->current->global_flags & XVID_REDUCED))
1681            {
1682                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1683                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1684                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1685            }
1686          emms();          emms();
1687    
1688          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1689          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1690          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1691    
1692          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
# Line 1577  Line 1700 
1700  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1701  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1702    
1703    
1704    /* FrameCodeP also handles S(GMC)-VOPs */
1705  static int  static int
1706  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1707                     Bitstream * bs,                     Bitstream * bs,
# Line 1589  Line 1714 
1714          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1715          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1716    
1717            int mb_width = pEnc->mbParam.mb_width;
1718            int mb_height = pEnc->mbParam.mb_height;
1719    
1720          int iLimit;          int iLimit;
1721          int x, y, k;          int x, y, k;
1722          int iSearchRange;          int iSearchRange;
# Line 1597  Line 1725 
1725          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1726          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1727    
1728            if ((pEnc->current->global_flags & XVID_REDUCED))
1729            {
1730                    mb_width = (pEnc->mbParam.width + 31) / 32;
1731                    mb_height = (pEnc->mbParam.height + 31) / 32;
1732            }
1733    
1734    
1735          start_timer();          start_timer();
1736          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1737                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height);
# Line 1608  Line 1743 
1743          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1744    
1745          if (!force_inter)          if (!force_inter)
1746                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1747          else          else
1748                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1749    
1750          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1751                  start_timer();                  start_timer();
# Line 1624  Line 1757 
1757                  stop_inter_timer();                  stop_inter_timer();
1758          }          }
1759    
         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  
1760                  pEnc->current->coding_type = P_VOP;                  pEnc->current->coding_type = P_VOP;
1761    
1762          start_timer();          start_timer();
1763          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1764                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1765          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 {  
   
1766                  bIntra =                  bIntra =
1767                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1768                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1769                           iLimit);                           iLimit);
1770          }  
1771          stop_motion_timer();          stop_motion_timer();
1772    
1773          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1774    
1775          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )          if ( ( pEnc->current->global_flags & XVID_GMC )
1776                          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) ) )
1777            {
1778                    pEnc->current->coding_type = S_VOP;
1779    
1780          if (vol_header)                  generate_GMCparameters( 2, 16, &pEnc->current->warp,
1781                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                                          pEnc->mbParam.width, pEnc->mbParam.height,
1782                                            &pEnc->current->gmc_data);
1783    
1784                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1785                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1786                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1787                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1788                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1789    
1790            }
1791    
1792          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1793            if (vol_header)
1794            {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1795                    BitstreamPadAlways(bs);
1796            }
1797    
1798          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1799    
1800          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1801    
1802          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1803                  pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1804    
1805          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1806                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1807                    for (x = 0; x < mb_width; x++) {
1808                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1809                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1810    
1811    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1812    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1813    
1814                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1815    
1816                          if (!bIntra) {                          if (bIntra) {
1817                                    CodeIntraMB(pEnc, pMB);
1818                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1819                                                                      dct_codes, qcoeff);
1820    
1821                                    start_timer();
1822                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1823                                    stop_prediction_timer();
1824    
1825                                    pEnc->current->sStat.kblks++;
1826    
1827                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1828                                    stop_coding_timer();
1829                                    continue;
1830                            }
1831    
1832                            if (pEnc->current->coding_type == S_VOP) {
1833    
1834                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1835                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1836                                            pEnc->mbParam.edged_width, 65536);
1837    
1838                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1839    
1840                                            if (pEnc->mbParam.m_quarterpel)
1841                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1842                                            else
1843                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1844    
1845                                            pMB->mode = MODE_INTER;
1846                                            pMB->mcsel = 1;
1847                                            pMB->sad16 = iSAD;
1848                                    } else {
1849                                            pMB->mcsel = 0;
1850                                    }
1851                            } else {
1852                                    pMB->mcsel = 0; /* just a precaution */
1853                            }
1854    
1855                                  start_timer();                                  start_timer();
1856                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1857                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1858                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1859                                                                     &pEnc->current->image,
1860                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1861                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1862                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1863                                                                           pEnc->mbParam.m_quarterpel,                                                                           pEnc->mbParam.m_quarterpel,
1864                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1865                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1866    
1867                                  stop_comp_timer();                                  stop_comp_timer();
1868    
1869                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1701  Line 1881 
1881                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1882    
1883                                  if (pMB->mode != MODE_NOT_CODED)                                  if (pMB->mode != MODE_NOT_CODED)
1884                                          pMB->cbp =                          {       pMB->cbp =
1885                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1886                                                                                    dct_codes, qcoeff);                                                                                    dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1887                          }                          }
1888    
1889                          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 ||  
1890                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1891                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1892                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1893                          }  else {                          }  else {
1894                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1895                          }                          }
1896    
1897                          start_timer();                          start_timer();
1898    
1899                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1900    
1901                          skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1902                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == NO_CHANGE);
1903    
1904                            if (pEnc->current->coding_type == S_VOP)
1905                                    skip_possible &= (pMB->mcsel == 1);
1906                            else if (pEnc->current->coding_type == P_VOP) {
1907                          if(pEnc->mbParam.m_quarterpel)                          if(pEnc->mbParam.m_quarterpel)
1908                          {       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) );
                         }  
1909                          else                          else
1910                          {       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) );
1911                          }                          }
1912    
1913                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1914    
1915  /* 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;  
1916    
1917                                  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 */
1918                                    {
1919                                            int bSkip = 1;
1920    
1921                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1922                                          {                                          {
1923                                                  int iSAD;                                                  int iSAD;
# Line 1756  Line 1930 
1930                                                  }                                                  }
1931                                          }                                          }
1932    
1933                                  if (!bSkip)                                          if (!bSkip) {   /* no SKIP, but trivial block */
                                 {  
                                         VECTOR predMV;  
1934                                          if(pEnc->mbParam.m_quarterpel) {                                          if(pEnc->mbParam.m_quarterpel) {
1935                                                  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);
1936                                                  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;
1937                                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1938                                          }                                          }
1939                                          else {                                          else {
1940                                                  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);
1941                                                  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;
1942                                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1943                                          }                                          }
1944                                          pMB->mode = MODE_INTER;                                          pMB->mode = MODE_INTER;
1945                                          pMB->cbp = 0;                                          pMB->cbp = 0;
1946                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1947                                                    stop_coding_timer();
1948    
1949                                                    continue;       /* next MB */
1950                                  }                                  }
1951                                  else                                  }
1952                                  {                                  /* do SKIP */
1953    
1954                                          pMB->mode = MODE_NOT_CODED;                                          pMB->mode = MODE_NOT_CODED;
1955                                          MBSkip(bs);                                          MBSkip(bs);
1956                                    stop_coding_timer();
1957                                    continue;       /* next MB */
1958                                  }                                  }
1959                            /* ordinary case: normal coded INTER/INTER4V block */
1960    
                         } else {  
1961                                  if (pEnc->current->global_flags & XVID_GREYSCALE)                                  if (pEnc->current->global_flags & XVID_GREYSCALE)
1962                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1963                                          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 */
1964                                          qcoeff[5*64+0]=0;                                          qcoeff[5*64+0]=0;
1965                                  }                                  }
1966                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1967                            if(pEnc->mbParam.m_quarterpel) {
1968                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1969                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1970                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1971                                    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);
1972                            } else {
1973                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1974                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1975                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1976                                    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);
1977                          }                          }
1978    
1979    
1980                            if (pMB->mode == MODE_INTER4V)
1981                            {       int k;
1982                                    for (k=1;k<4;k++)
1983                                    {
1984                                            if(pEnc->mbParam.m_quarterpel) {
1985                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1986                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1987                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1988                                    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);
1989                                            } else {
1990                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1991                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1992                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1993                                    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);
1994                                            }
1995    
1996                                    }
1997                            }
1998    
1999                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2000                          stop_coding_timer();                          stop_coding_timer();
2001    
2002                  }                  }
2003          }          }
2004    
2005            if ((pEnc->current->global_flags & XVID_REDUCED))
2006            {
2007                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2008                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2009                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2010            }
2011    
2012          emms();          emms();
2013    
2014          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
2015                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2016          }          }
2017    
2018          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2019                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2020    
2021          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2022    
2023          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2024    
# Line 1811  Line 2028 
2028                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2029                  iSearchRange *= 2;                  iSearchRange *= 2;
2030          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2031                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2032                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2033                          && (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
2034          {          {
2035                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2036                  iSearchRange /= 2;                  iSearchRange /= 2;
2037          }          }
2038    
2039          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2040    
 #ifdef FRAMEDROP  
2041          /* frame drop code */          /* frame drop code */
2042          // 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);
2043          if (pEnc->sStat.kblks + pEnc->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2044                  (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2045          {          {
2046                  pEnc->sStat.kblks = pEnc->sStat.mblks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2047                  pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;                  pEnc->current->sStat.ublks = mb_width * mb_height;
2048    
2049                  BitstreamReset(bs);                  BitstreamReset(bs);
2050    
# Line 1843  Line 2059 
2059                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
2060                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
2061                  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);
2062                  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);
2063            }
2064    
2065            /* XXX: debug
2066            {
2067                    char s[100];
2068                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2069                    image_dump_yuvpgm(&pEnc->current->image,
2070                            pEnc->mbParam.edged_width,
2071                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2072    
2073                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2074                    image_dump_yuvpgm(&pEnc->reference->image,
2075                            pEnc->mbParam.edged_width,
2076                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2077          }          }
2078  #endif          */
2079    
2080    
2081          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2082    
# Line 1854  Line 2084 
2084  }  }
2085    
2086    
2087  static __inline void  static void
2088  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2089                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2090                     Bitstream * bs,                     Bitstream * bs,
2091                     uint32_t * pBits)                     uint32_t * pBits)
2092  {  {
2093          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2094          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2095          uint32_t x, y;          uint32_t x, y;
2096    
2097          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
# Line 1874  Line 2104 
2104                  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); \
2105          }          }
2106    
2107            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2108    
2109          if (!first){          if (!first){
2110                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2111          }          }
# Line 1908  Line 2140 
2140                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
2141                          pEnc->reference->mbs, f_ref,                          pEnc->reference->mbs, f_ref,
2142                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2143                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2144                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
2145    
2146    
# Line 1926  Line 2158 
2158    
2159          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2160    
2161          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2162          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2163          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2164          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2165    
2166    
2167          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2168                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2169                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2170                          int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2171    
2172                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2173                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1967  Line 2199 
2199  #endif  #endif
2200                          start_timer();                          start_timer();
2201                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2202                                                   &pEnc->sStat, direction);                                                   &frame->sStat, direction);
2203                          stop_coding_timer();                          stop_coding_timer();
2204                  }                  }
2205          }          }
# Line 1986  Line 2218 
2218          }          }
2219  #endif  #endif
2220  }  }
   
   
 /*      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.642  
changed lines
  Added in v.786

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