[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.20, Tue Nov 19 13:21:25 2002 UTC revision 1.92, Sat Feb 15 18:48:15 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 255  Line 254 
254    
255          /* try to allocate image memory */          /* try to allocate image memory */
256    
257  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
258          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
 #endif  
259    
260          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
261          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 271  Line 269 
269          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
270          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
271    
272  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
273          if (image_create          {       if (image_create
274                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
275                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
276                  goto xvid_err_memory3;                  goto xvid_err_memory3;
277  #endif          }
278    
279          if (image_create          if (image_create
280                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
# Line 320  Line 318 
318                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
319                  goto xvid_err_memory3;                  goto xvid_err_memory3;
320    
321    /* Create full bitplane for GMC, this might be wasteful */
322            if (image_create
323                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
324                     pEnc->mbParam.edged_height) < 0)
325                    goto xvid_err_memory3;
326    
327    
328    
329          /* B Frames specific init */          pEnc->mbParam.global = pParam->global;
330    
331          pEnc->global = pParam->global;          /* B Frames specific init */
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->bquant_offset = pParam->bquant_offset;          pEnc->mbParam.bquant_offset = pParam->bquant_offset;
335          pEnc->frame_drop_ratio = pParam->frame_drop_ratio;          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 460  Line 464 
464          }          }
465    
466    xvid_err_memory3:    xvid_err_memory3:
467  #ifdef _DEBUG_PSNR  
468          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
469            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
470                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
471  #endif          }
472    
473          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
474                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 487  Line 492 
492          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
493                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
494    
495    /* destroy GMC image */
496            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
497                                      pEnc->mbParam.edged_height);
498    
499    
500    xvid_err_memory2:    xvid_err_memory2:
501          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
502          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 574  Line 584 
584          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
585                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
586    
587  #ifdef _DEBUG_PSNR          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
588          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
589                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
590  #endif          }
591    
592          /* Encoder structure */          /* Encoder structure */
593    
# Line 621  Line 631 
631                  return;                  return;
632          stop_conv_timer();          stop_conv_timer();
633    
634            if ((pFrame->general & XVID_CHROMAOPT)) {
635                    image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
636                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
637            }
638    
639          pEnc->queue_size++;          pEnc->queue_size++;
640          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
641  }  }
# Line 646  Line 661 
661    
662    
663    
664    /* convert pFrame->intra to coding_type */
665    static int intra2coding_type(int intra)
666    {
667            if (intra < 0)  return -1;
668            if (intra == 1) return I_VOP;
669            if (intra == 2) return B_VOP;
670    
671            return P_VOP;
672    }
673    
674    
675    
676  /*****************************************************************************  /*****************************************************************************
# Line 664  Line 689 
689  {  {
690          uint16_t x, y;          uint16_t x, y;
691          Bitstream bs;          Bitstream bs;
692          uint32_t bits, mode;          uint32_t bits;
693            int mode;
694    
695          int input_valid = 1;          int input_valid = 1;
696          int bframes_count = 0;          int bframes_count = 0;
697    
 #ifdef _DEBUG_PSNR  
         float psnr;  
         char temp[128];  
 #endif  
   
698          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
699          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
700          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
# Line 703  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;                          bframes_count = 0;
733    
734                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
735                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
736                          pFrame->intra = 0;                          pFrame->intra = 0;
737    
738    
739                          emms();                          emms();
740    
741                            if (pResult) {
742                                    pResult->quant = pEnc->current->quant;
743                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
744                                    pResult->kblks = pEnc->current->sStat.kblks;
745                                    pResult->mblks = pEnc->current->sStat.mblks;
746                                    pResult->ublks = pEnc->current->sStat.ublks;
747                            }
748    
749                          return XVID_ERR_OK;                          return XVID_ERR_OK;
750                  }                  }
751    
# Line 727  Line 757 
757                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
758                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
759    
760                  BitstreamPad(&bs);                  BitstreamPadAlways(&bs);
761                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
762                  pFrame->intra = 2;                  pFrame->intra = 2;
763    
764                    if (pResult) {
765                            pResult->quant = pEnc->current->quant;
766                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
767                            pResult->kblks = pEnc->current->sStat.kblks;
768                            pResult->mblks = pEnc->current->sStat.mblks;
769                            pResult->ublks = pEnc->current->sStat.ublks;
770                    }
771    
772                  if (input_valid)                  if (input_valid)
773                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
774    
# Line 749  Line 787 
787                     indentical to the future-referece frame.                     indentical to the future-referece frame.
788                  */                  */
789    
790                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
791                          int tmp;                          int tmp;
792    
793                          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",
794                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
795                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
796    
                         BitstreamPad(&bs);  
797    
798                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
799                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
800    
801                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
802                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
803    
804                            BitstreamPadAlways(&bs);
805                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
806                          pFrame->intra = 4;                          pFrame->intra = 4;
807    
808                            if (pResult) {
809                                    pResult->quant = pEnc->current->quant;
810                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
811                                    pResult->kblks = pEnc->current->sStat.kblks;
812                                    pResult->mblks = pEnc->current->sStat.mblks;
813                                    pResult->ublks = pEnc->current->sStat.ublks;
814                            }
815    
816                          if (input_valid)                          if (input_valid)
817                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
818    
# Line 781  Line 828 
828          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->bframenum_dx50bvop != -1)
829          {          {
830    
831                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
832                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
833    
834                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
835                          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");
836                  }                  }
837    
# Line 796  Line 843 
843    
844          } else if (input_valid) {          } else if (input_valid) {
845    
846                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
847    
848                  start_timer();                  start_timer();
849                  if (image_input                  if (image_input
# Line 808  Line 855 
855                  }                  }
856                  stop_conv_timer();                  stop_conv_timer();
857    
858                    if ((pFrame->general & XVID_CHROMAOPT)) {
859                            image_chroma_optimize(&pEnc->current->image,
860                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
861                    }
862    
863                  // queue input frame, and dequue next image                  // queue input frame, and dequue next image
864                  if (pEnc->queue_size > 0)                  if (pEnc->queue_size > 0)
865                  {                  {
# Line 822  Line 874 
874    
875          } else if (pEnc->queue_size > 0) {          } else if (pEnc->queue_size > 0) {
876    
877                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
878    
879                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
880                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
# Line 839  Line 891 
891                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
892                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
893    
894                          BitstreamPutBits(&bs, 0x7f, 8);                  //      BitstreamPutBits(&bs, 0x7f, 8);
895                          pFrame->intra = 5;                          pFrame->intra = 5;
896    
897                            if (pResult) {
898                                    /*
899                                     * We must decide what to put there because i know some apps
900                                     * are storing statistics about quantizers and just do
901                                     * stats[quant]++ or stats[quant-1]++
902                                     * transcode is one of these app with its 2pass module
903                                     */
904    
905                                    /*
906                                     * For now i prefer 31 than 0 that could lead to a segfault
907                                     * in transcode
908                                     */
909                                    pResult->quant = 31;
910    
911                                    pResult->hlength = 0;
912                                    pResult->kblks = 0;
913                                    pResult->mblks = 0;
914                                    pResult->ublks = 0;
915                            }
916    
917                    } else {
918    
919                            if (pResult) {
920                                    pResult->quant = pEnc->current->quant;
921                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
922                                    pResult->kblks = pEnc->current->sStat.kblks;
923                                    pResult->mblks = pEnc->current->sStat.mblks;
924                                    pResult->ublks = pEnc->current->sStat.ublks;
925                            }
926    
927                  }                  }
928    
929                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
930    
931                  emms();                  emms();
932    
933                  return XVID_ERR_OK;                  return XVID_ERR_OK;
934          }          }
935    
# Line 855  Line 940 
940          // 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
941          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
942          {          {
943                    mode = intra2coding_type(pFrame->intra);
944                  if (pFrame->quant == 0)                  if (pFrame->quant == 0)
945                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
946                  else                  else
# Line 875  Line 961 
961    
962                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
963    
964  #ifdef _DEBUG_PSNR                  if (pFrame->general & XVID_EXTRASTATS)
965                  image_copy(&pEnc->sOriginal, &pEnc->current->image,                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
966                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
967  #endif                  }
968    
969                  emms();                  emms();
970    
971                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
972                          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,
973                                  "%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);
974                  }                  }
# Line 926  Line 1012 
1012          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1013           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
1014           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1015            pEnc->iFrameNum++;
1016    
1017          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1018                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1019                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
1020                  || 2 == (mode = MEanalysis(&pEnc->reference->image, pEnc->current,          {
1021                                                                          &pEnc->mbParam, pEnc->iMaxKeyInterval,                  mode = I_VOP;
1022                                                                          (pFrame->intra < 0) ? pEnc->iFrameNum : 0,          }else{
1023                                                                          bframes_count++))) {                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1024                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1025                                            (mode < 0) ? pEnc->iFrameNum : 0,
1026                                            bframes_count++);
1027            }
1028    
1029            if (mode == I_VOP) {
1030                  /*                  /*
1031                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
1032                   */                   */
# Line 943  Line 1035 
1035                  else                  else
1036                          pEnc->mbParam.m_quarterpel = 0;                          pEnc->mbParam.m_quarterpel = 0;
1037    
1038                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1039    
1040                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1041                            if (pFrame->quant_intra_matrix != NULL)
1042                                    set_intra_matrix(pFrame->quant_intra_matrix);
1043                            if (pFrame->quant_inter_matrix != NULL)
1044                                    set_inter_matrix(pFrame->quant_inter_matrix);
1045                    }
1046    
1047    
1048                  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",
1049                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1050                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1051    
1052                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1053                          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");
1054                  }                  }
1055    
1056                  // 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
1057    
1058                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1059    
1060                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
1061                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1062    
1063                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1064                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1065                                  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");
1066                          }                          }
1067                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
1068                          bframes_count = 0;                          bframes_count = 0;
   
1069                          pFrame->intra = 0;                          pFrame->intra = 0;
1070    
1071                  } else {                  } else {
# Line 978  Line 1079 
1079    
1080                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1081    
1082                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1083                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1084                          input_valid = 0;                          input_valid = 0;
1085                          goto ipvop_loop;                          goto ipvop_loop;
# Line 988  Line 1089 
1089                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1090                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1091                   */                   */
1092          } 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) {
 //      } else if (pFrame->intra == 0 || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {  
1093                  /*                  /*
1094                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1095                   */                   */
# Line 998  Line 1098 
1098                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1099                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1100    
1101                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1102                          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");
1103                  }                  }
1104    
# Line 1007  Line 1107 
1107                  pFrame->intra = 0;                  pFrame->intra = 0;
1108                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1109    
1110                  if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1111                          BitstreamPadAlways(&bs);                          BitstreamPadAlways(&bs);
1112                          input_valid = 0;                          input_valid = 0;
1113                          goto ipvop_loop;                          goto ipvop_loop;
1114                  }                  }
1115    
1116          } else {          } else {        /* mode == B_VOP */
1117                  /*                  /*
1118                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1119                   */                   */
1120    
1121                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1122                          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");
1123                  }                  }
1124    
1125                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1126                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1127                                  pEnc->bquant_ratio) / 2) + pEnc->bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1128    
1129                  } else {                  } else {
1130                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
# Line 1040  Line 1140 
1140                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1141    
1142                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1143                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1144                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1145    
1146                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1147    
# Line 1053  Line 1153 
1153                  goto bvop_loop;                  goto bvop_loop;
1154          }          }
1155    
1156          pEnc->iFrameNum++;          BitstreamPadAlways(&bs);
   
         BitstreamPad(&bs);  
1157          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1158    
1159          if (pResult) {          if (pResult) {
1160                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1161                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1162                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1163                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1164                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1165          }  
1166                    if (pFrame->general & XVID_EXTRASTATS)
1167          emms();                  {       pResult->sse_y =
1168                                    plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
 #ifdef _DEBUG_PSNR  
         psnr =  
                 image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
1169                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1170                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1171    
1172          snprintf(temp, 127, "PSNR: %f\n", psnr);                          pResult->sse_u =
1173          DEBUG(temp);                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1174  #endif                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1175                                                       pEnc->mbParam.height/2);
1176    
1177                            pResult->sse_v =
1178                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1179                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1180                                                       pEnc->mbParam.height/2);
1181                    }
1182            }
1183    
1184            emms();
1185    
1186          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1187                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
# Line 1111  Line 1216 
1216          uint32_t bits;          uint32_t bits;
1217          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1218    
 #ifdef _DEBUG_PSNR  
1219          float psnr;          float psnr;
1220          uint8_t temp[128];          uint8_t temp[128];
 #endif  
1221    
1222          start_global_timer();          start_global_timer();
1223    
# Line 1123  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 1145  Line 1248 
1248                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1249          stop_conv_timer();          stop_conv_timer();
1250    
1251  #ifdef _DEBUG_PSNR          if ((pFrame->general & XVID_CHROMAOPT)) {
1252          image_copy(&pEnc->sOriginal, &pEnc->current->image,                  image_chroma_optimize(&pEnc->current->image,
1253                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1254            }
1255    
1256            if (pFrame->general & XVID_EXTRASTATS)
1257            {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1258                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1259  #endif          }
1260    
1261          emms();          emms();
1262    
# Line 1225  Line 1333 
1333          }          }
1334    
1335          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1336                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == -1)
1337                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1338                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1339                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1340                  } else {                  } else {
1341                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 1241  Line 1349 
1349    
1350          }          }
1351    
1352          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1353          BitstreamPutBits(&bs, 0xFFFF, 16);  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1354          BitstreamPad(&bs);          BitstreamPadAlways(&bs);
1355          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1356    
1357          if (pResult) {          if (pResult) {
1358                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1359                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1360                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1361                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1362                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1363          }          }
1364    
1365          emms();          emms();
# Line 1260  Line 1368 
1368                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1369                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1370          }          }
1371  #ifdef _DEBUG_PSNR          if (pFrame->general & XVID_EXTRASTATS)
1372            {
1373          psnr =          psnr =
1374                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1375                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1376                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1377    
1378          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1379          DEBUG(temp);          }
 #endif  
1380    
1381          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1382    
# Line 1434  Line 1542 
1542    
1543          if (intra) {          if (intra) {
1544                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1545                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1546                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1547                  }                  }
1548                  return;                  return;
# Line 1509  Line 1617 
1617                     Bitstream * bs,                     Bitstream * bs,
1618                     uint32_t * pBits)                     uint32_t * pBits)
1619  {  {
1620            int mb_width = pEnc->mbParam.mb_width;
1621            int mb_height = pEnc->mbParam.mb_height;
1622    
1623          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1624          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1625    
1626          uint16_t x, y;          uint16_t x, y;
1627    
1628            if ((pEnc->current->global_flags & XVID_REDUCED))
1629            {
1630                    mb_width = (pEnc->mbParam.width + 31) / 32;
1631                    mb_height = (pEnc->mbParam.height + 31) / 32;
1632    
1633                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1634                    /* XXX: setedges is overkill */
1635                    start_timer();
1636                    image_setedges(&pEnc->current->image,
1637                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1638                            pEnc->mbParam.width, pEnc->mbParam.height);
1639                    stop_edges_timer();
1640            }
1641    
1642          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1643          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1644          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1523  Line 1647 
1647    
1648          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1649    
 #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));  
   
1650          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1651    
1652            BitstreamPadAlways(bs);
1653          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1654    
1655          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1656    
1657          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1658          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1659          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1660    
1661          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1662                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1663                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1664                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1665    
# Line 1560  Line 1678 
1678                                  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 */
1679                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1680                          }                          }
1681                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1682                          stop_coding_timer();                          stop_coding_timer();
1683                  }                  }
1684    
1685            if ((pEnc->current->global_flags & XVID_REDUCED))
1686            {
1687                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1688                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1689                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1690            }
1691          emms();          emms();
1692    
1693          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1694          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1695          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1696    
1697          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
# Line 1583  Line 1705 
1705  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1706  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1707    
1708    
1709    /* FrameCodeP also handles S(GMC)-VOPs */
1710  static int  static int
1711  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1712                     Bitstream * bs,                     Bitstream * bs,
# Line 1595  Line 1719 
1719          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1720          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1721    
1722            int mb_width = pEnc->mbParam.mb_width;
1723            int mb_height = pEnc->mbParam.mb_height;
1724    
1725          int iLimit;          int iLimit;
1726          int x, y, k;          int x, y, k;
1727          int iSearchRange;          int iSearchRange;
# Line 1603  Line 1730 
1730          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1731          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1732    
1733            if ((pEnc->current->global_flags & XVID_REDUCED))
1734            {
1735                    mb_width = (pEnc->mbParam.width + 31) / 32;
1736                    mb_height = (pEnc->mbParam.height + 31) / 32;
1737            }
1738    
1739    
1740          start_timer();          start_timer();
1741          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1742                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height);
# Line 1614  Line 1748 
1748          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1749    
1750          if (!force_inter)          if (!force_inter)
1751                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1752          else          else
1753                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1754    
1755          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1756                  start_timer();                  start_timer();
# Line 1630  Line 1762 
1762                  stop_inter_timer();                  stop_inter_timer();
1763          }          }
1764    
         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  
1765                  pEnc->current->coding_type = P_VOP;                  pEnc->current->coding_type = P_VOP;
1766    
1767          start_timer();          start_timer();
1768          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1769                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1770          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 {  
   
1771                  bIntra =                  bIntra =
1772                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1773                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1774                           iLimit);                           iLimit);
1775          }  
1776          stop_motion_timer();          stop_motion_timer();
1777    
1778          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1779    
1780          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )          if ( ( pEnc->current->global_flags & XVID_GMC )
1781                          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) ) )
1782            {
1783                    pEnc->current->coding_type = S_VOP;
1784    
1785                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1786                                            pEnc->mbParam.width, pEnc->mbParam.height,
1787                                            &pEnc->current->gmc_data);
1788    
1789                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1790                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1791                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1792                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1793                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1794    
1795          if (vol_header)          }
                 BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);  
   
1796    
1797          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1798            if (vol_header)
1799            {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1800                    BitstreamPadAlways(bs);
1801            }
1802    
1803          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1804    
1805          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1806    
1807          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1808                  pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1809    
1810          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1811                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1812                    for (x = 0; x < mb_width; x++) {
1813                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1814                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1815    
1816    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1817    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1818    
1819                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1820    
1821                          if (!bIntra) {                          if (bIntra) {
1822                                    CodeIntraMB(pEnc, pMB);
1823                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1824                                                                      dct_codes, qcoeff);
1825    
1826                                    start_timer();
1827                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1828                                    stop_prediction_timer();
1829    
1830                                    pEnc->current->sStat.kblks++;
1831    
1832                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1833                                    stop_coding_timer();
1834                                    continue;
1835                            }
1836    
1837                            if (pEnc->current->coding_type == S_VOP) {
1838    
1839                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1840                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1841                                            pEnc->mbParam.edged_width, 65536);
1842    
1843                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1844                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1845                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1846    
1847                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1848                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1849                                    }
1850    
1851                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1852    
1853                                            if (pEnc->mbParam.m_quarterpel)
1854                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1855                                            else
1856                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1857    
1858                                            pMB->mode = MODE_INTER;
1859                                            pMB->mcsel = 1;
1860                                            pMB->sad16 = iSAD;
1861                                    } else {
1862                                            pMB->mcsel = 0;
1863                                    }
1864                            } else {
1865                                    pMB->mcsel = 0; /* just a precaution */
1866                            }
1867    
1868                                  start_timer();                                  start_timer();
1869                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1870                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1871                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1872                                                                     &pEnc->current->image,
1873                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1874                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1875                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1876                                                                           pEnc->mbParam.m_quarterpel,                                                                           pEnc->mbParam.m_quarterpel,
1877                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1878                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1879    
1880                                  stop_comp_timer();                                  stop_comp_timer();
1881    
1882                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1707  Line 1894 
1894                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1895    
1896                                  if (pMB->mode != MODE_NOT_CODED)                                  if (pMB->mode != MODE_NOT_CODED)
1897                                          pMB->cbp =                          {       pMB->cbp =
1898                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1899                                                                                    dct_codes, qcoeff);                                                                                    dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1900                          }                          }
1901    
1902                          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 ||  
1903                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1904                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1905                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1906                          }  else {                          }  else {
1907                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1908                          }                          }
1909    
1910                          start_timer();                          start_timer();
1911    
1912                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1913    
1914                          skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1915                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == NO_CHANGE);
1916    
1917                            if (pEnc->current->coding_type == S_VOP)
1918                                    skip_possible &= (pMB->mcsel == 1);
1919                            else if (pEnc->current->coding_type == P_VOP) {
1920                          if(pEnc->mbParam.m_quarterpel)                          if(pEnc->mbParam.m_quarterpel)
1921                          {       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) );
                         }  
1922                          else                          else
1923                          {       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) );
1924                          }                          }
1925    
1926                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1927    
1928  /* 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;  
1929    
1930                                  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 */
1931                                    {
1932                                            int bSkip = 1;
1933    
1934                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1935                                          {                                          {
1936                                                  int iSAD;                                                  int iSAD;
# Line 1762  Line 1943 
1943                                                  }                                                  }
1944                                          }                                          }
1945    
1946                                  if (!bSkip)                                          if (!bSkip) {   /* no SKIP, but trivial block */
                                 {  
                                         VECTOR predMV;  
1947                                          if(pEnc->mbParam.m_quarterpel) {                                          if(pEnc->mbParam.m_quarterpel) {
1948                                                  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);
1949                                                  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;
1950                                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1951                                          }                                          }
1952                                          else {                                          else {
1953                                                  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);
1954                                                  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;
1955                                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1956                                          }                                          }
1957                                          pMB->mode = MODE_INTER;                                          pMB->mode = MODE_INTER;
1958                                          pMB->cbp = 0;                                          pMB->cbp = 0;
1959                                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1960                                                    stop_coding_timer();
1961    
1962                                                    continue;       /* next MB */
1963                                  }                                  }
1964                                  else                                  }
1965                                  {                                  /* do SKIP */
1966    
1967                                          pMB->mode = MODE_NOT_CODED;                                          pMB->mode = MODE_NOT_CODED;
1968                                          MBSkip(bs);                                          MBSkip(bs);
1969                                    stop_coding_timer();
1970                                    continue;       /* next MB */
1971                                  }                                  }
1972                            /* ordinary case: normal coded INTER/INTER4V block */
1973    
                         } else {  
1974                                  if (pEnc->current->global_flags & XVID_GREYSCALE)                                  if (pEnc->current->global_flags & XVID_GREYSCALE)
1975                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1976                                          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 */
1977                                          qcoeff[5*64+0]=0;                                          qcoeff[5*64+0]=0;
1978                                  }                                  }
1979                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1980                            if(pEnc->mbParam.m_quarterpel) {
1981                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1982                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1983                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1984                                    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);
1985                            } else {
1986                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1987                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1988                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1989                                    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);
1990                            }
1991    
1992    
1993                            if (pMB->mode == MODE_INTER4V)
1994                            {       int k;
1995                                    for (k=1;k<4;k++)
1996                                    {
1997                                            if(pEnc->mbParam.m_quarterpel) {
1998                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1999                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
2000                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
2001                                    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);
2002                                            } else {
2003                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
2004                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
2005                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
2006                                    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);
2007                          }                          }
2008    
2009                                    }
2010                            }
2011    
2012                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2013                          stop_coding_timer();                          stop_coding_timer();
2014    
2015                  }                  }
2016          }          }
2017    
2018            if ((pEnc->current->global_flags & XVID_REDUCED))
2019            {
2020                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2021                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2022                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2023            }
2024    
2025          emms();          emms();
2026    
2027          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
2028                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2029          }          }
2030    
2031          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2032                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2033    
2034          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2035    
2036          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2037    
# Line 1817  Line 2041 
2041                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2042                  iSearchRange *= 2;                  iSearchRange *= 2;
2043          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2044                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2045                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2046                          && (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
2047          {          {
2048                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2049                  iSearchRange /= 2;                  iSearchRange /= 2;
2050          }          }
2051    
2052          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2053    
 #ifdef FRAMEDROP  
2054          /* frame drop code */          /* frame drop code */
2055          // 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);
2056          if (pEnc->sStat.kblks + pEnc->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2057                  (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2058          {          {
2059                  pEnc->sStat.kblks = pEnc->sStat.mblks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2060                  pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;                  pEnc->current->sStat.ublks = mb_width * mb_height;
2061    
2062                  BitstreamReset(bs);                  BitstreamReset(bs);
2063    
# Line 1849  Line 2072 
2072                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
2073                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
2074                  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);
2075                  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);
2076            }
2077    
2078            /* XXX: debug
2079            {
2080                    char s[100];
2081                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2082                    image_dump_yuvpgm(&pEnc->current->image,
2083                            pEnc->mbParam.edged_width,
2084                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2085    
2086                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2087                    image_dump_yuvpgm(&pEnc->reference->image,
2088                            pEnc->mbParam.edged_width,
2089                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2090          }          }
2091  #endif          */
2092    
2093    
2094          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2095    
# Line 1860  Line 2097 
2097  }  }
2098    
2099    
2100  static __inline void  static void
2101  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2102                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2103                     Bitstream * bs,                     Bitstream * bs,
2104                     uint32_t * pBits)                     uint32_t * pBits)
2105  {  {
2106          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2107          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2108          uint32_t x, y;          uint32_t x, y;
2109    
2110          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
# Line 1880  Line 2117 
2117                  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); \
2118          }          }
2119    
2120            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2121    
2122          if (!first){          if (!first){
2123                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2124          }          }
# Line 1932  Line 2171 
2171    
2172          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2173    
2174          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2175          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2176          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2177          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2178    
2179    
2180          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2181                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2182                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2183                          int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2184    
2185                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2186                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1973  Line 2212 
2212  #endif  #endif
2213                          start_timer();                          start_timer();
2214                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2215                                                   &pEnc->sStat, direction);                                                   &frame->sStat, direction);
2216                          stop_coding_timer();                          stop_coding_timer();
2217                  }                  }
2218          }          }
# Line 1992  Line 2231 
2231          }          }
2232  #endif  #endif
2233  }  }
   
   
 /*      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.20  
changed lines
  Added in v.1.92

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