[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.37, Mon Jan 13 14:33:24 2003 UTC revision 1.96, Tue Mar 4 10:55:21 2003 UTC
# Line 26  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
  ****************************************************************************/  
   
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              MinChen <chenm001@163.com>  
  *  20.06.2002 bframe patch  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
29   *  $Id$   *  $Id$
30   *   *
31   ****************************************************************************/   ****************************************************************************/
# Line 129  Line 116 
116  {  {
117          Encoder *pEnc;          Encoder *pEnc;
118          int i;          int i;
   
119          pParam->handle = NULL;          pParam->handle = NULL;
120    
121          ENC_CHECK(pParam);          ENC_CHECK(pParam);
# Line 229  Line 215 
215    
216          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
217    
218          pEnc->iFrameNum = 0;          pEnc->iFrameNum = -1;
219          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
220    
221          /* try to allocate frame memory */          /* try to allocate frame memory */
# Line 254  Line 240 
240    
241          /* try to allocate image memory */          /* try to allocate image memory */
242    
243  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
244          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
 #endif  
245    
246          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
247          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 270  Line 255 
255          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
256          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
257    
258  #ifdef _DEBUG_PSNR          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
259          if (image_create          {       if (image_create
260                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
261                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
262                  goto xvid_err_memory3;                  goto xvid_err_memory3;
263  #endif          }
264    
265          if (image_create          if (image_create
266                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
# Line 326  Line 311 
311                  goto xvid_err_memory3;                  goto xvid_err_memory3;
312    
313    
         /* B Frames specific init */  
314    
315          pEnc->mbParam.global = pParam->global;          pEnc->mbParam.global = pParam->global;
316    
317            /* B Frames specific init */
318          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
319          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
320          pEnc->mbParam.bquant_offset = pParam->bquant_offset;          pEnc->mbParam.bquant_offset = pParam->bquant_offset;
# Line 379  Line 365 
365    
366          pEnc->queue = NULL;          pEnc->queue = NULL;
367    
   
368          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
369                  int n;                  int n;
370    
# Line 432  Line 417 
417    
418    xvid_err_memory5:    xvid_err_memory5:
419    
   
420          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
421    
422                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 464  Line 448 
448          }          }
449    
450    xvid_err_memory3:    xvid_err_memory3:
451  #ifdef _DEBUG_PSNR  
452          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
453            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
454                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
455  #endif          }
456    
457          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
458                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 539  Line 524 
524                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
525          }          }
526    
   
527          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
528    
529                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 583  Line 567 
567          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
568                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
569    
570  #ifdef _DEBUG_PSNR          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
571          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
573  #endif          }
574    
575          /* Encoder structure */          /* Encoder structure */
576    
# Line 604  Line 588 
588    
589  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
590  {  {
591          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
592          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
593  }  }
594    
# Line 630  Line 614 
614                  return;                  return;
615          stop_conv_timer();          stop_conv_timer();
616    
617            if ((pFrame->general & XVID_CHROMAOPT)) {
618                    image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
619                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
620            }
621    
622          pEnc->queue_size++;          pEnc->queue_size++;
623          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
624  }  }
# Line 641  Line 630 
630                  pCur->ticks = (int32_t)pCur->stamp % time_base;                  pCur->ticks = (int32_t)pCur->stamp % time_base;
631                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
632    
633                  //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */
634    
635  /*              fprintf(stderr,"WriteVop:   %d - %d \n",  /*              fprintf(stderr,"WriteVop:   %d - %d \n",
636                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
# Line 684  Line 673 
673          uint16_t x, y;          uint16_t x, y;
674          Bitstream bs;          Bitstream bs;
675          uint32_t bits;          uint32_t bits;
676          int mode;          int mode = -1; /* Just to shut up compiler warning */
677    
678          int input_valid = 1;          int input_valid = 1;
679          int bframes_count = 0;          int bframes_count = 0;
680    
 #ifdef _DEBUG_PSNR  
         float psnr;  
         char temp[128];  
 #endif  
   
681          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
682          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
683          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
# Line 734  Line 718 
718                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
719                          pFrame->intra = 0;                          pFrame->intra = 0;
720    
721    
722                          emms();                          emms();
723    
724                            if (pResult) {
725                                    pResult->quant = pEnc->current->quant;
726                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
727                                    pResult->kblks = pEnc->current->sStat.kblks;
728                                    pResult->mblks = pEnc->current->sStat.mblks;
729                                    pResult->ublks = pEnc->current->sStat.ublks;
730                            }
731    
732                          return XVID_ERR_OK;                          return XVID_ERR_OK;
733                  }                  }
734    
# Line 751  Line 744 
744                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
745                  pFrame->intra = 2;                  pFrame->intra = 2;
746    
747                    if (pResult) {
748                            pResult->quant = pEnc->current->quant;
749                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
750                            pResult->kblks = pEnc->current->sStat.kblks;
751                            pResult->mblks = pEnc->current->sStat.mblks;
752                            pResult->ublks = pEnc->current->sStat.ublks;
753                    }
754    
755                  if (input_valid)                  if (input_valid)
756                          queue_image(pEnc, pFrame);                          queue_image(pEnc, pFrame);
757    
# Line 763  Line 764 
764                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
765    
766                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
   
767                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
768                     of a not-coded p-vop, with a time_base of zero, and time_increment                     of a not-coded p-vop, with a time_base of zero, and time_increment
769                     indentical to the future-referece frame.                     indentical to the future-referece frame.
# Line 776  Line 776 
776                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
777                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
778    
   
779                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
780                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
781    
# Line 787  Line 786 
786                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
787                          pFrame->intra = 4;                          pFrame->intra = 4;
788    
789                            if (pResult) {
790                                    pResult->quant = pEnc->current->quant;
791                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
792                                    pResult->kblks = pEnc->current->sStat.kblks;
793                                    pResult->mblks = pEnc->current->sStat.mblks;
794                                    pResult->ublks = pEnc->current->sStat.ublks;
795                            }
796    
797                          if (input_valid)                          if (input_valid)
798                                  queue_image(pEnc, pFrame);                                  queue_image(pEnc, pFrame);
799    
# Line 829  Line 836 
836                  }                  }
837                  stop_conv_timer();                  stop_conv_timer();
838    
839                  // queue input frame, and dequue next image                  if ((pFrame->general & XVID_CHROMAOPT)) {
840                            image_chroma_optimize(&pEnc->current->image,
841                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
842                    }
843    
844                    /* queue input frame, and dequue next image */
845                  if (pEnc->queue_size > 0)                  if (pEnc->queue_size > 0)
846                  {                  {
847                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
# Line 860  Line 872 
872                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
873                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
874    
875                            /* That disabled line of code was supposed to inform VirtualDub
876                             * that the frame was a dummy delay frame - now disabled (thx god :-)
877                             */
878                  //      BitstreamPutBits(&bs, 0x7f, 8);                  //      BitstreamPutBits(&bs, 0x7f, 8);
879                          pFrame->intra = 5;                          pFrame->intra = 5;
880    
881                            if (pResult) {
882                                    /*
883                                     * We must decide what to put there because i know some apps
884                                     * are storing statistics about quantizers and just do
885                                     * stats[quant]++ or stats[quant-1]++
886                                     * transcode is one of these app with its 2pass module
887                                     */
888    
889                                    /*
890                                     * For now i prefer 31 than 0 that could lead to a segfault
891                                     * in transcode
892                                     */
893                                    pResult->quant = 31;
894    
895                                    pResult->hlength = 0;
896                                    pResult->kblks = 0;
897                                    pResult->mblks = 0;
898                                    pResult->ublks = 0;
899                            }
900                    } else {
901    
902                            if (pResult) {
903                                    pResult->quant = pEnc->current->quant;
904                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
905                                    pResult->kblks = pEnc->current->sStat.kblks;
906                                    pResult->mblks = pEnc->current->sStat.mblks;
907                                    pResult->ublks = pEnc->current->sStat.ublks;
908                            }
909    
910                  }                  }
911    
912                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
913    
914                  emms();                  emms();
915    
916                  return XVID_ERR_OK;                  return XVID_ERR_OK;
917          }          }
918    
# Line 873  Line 920 
920    
921          emms();          emms();
922    
923          // 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 */
924          if (pEnc->bframenum_dx50bvop < 0 )          if (pEnc->bframenum_dx50bvop < 0 )
925          {          {
926                  mode = intra2coding_type(pFrame->intra);                  mode = intra2coding_type(pFrame->intra);
# Line 897  Line 944 
944    
945                  inc_frame_num(pEnc);                  inc_frame_num(pEnc);
946    
947  #ifdef _DEBUG_PSNR                  if (pFrame->general & XVID_EXTRASTATS)
948                  image_copy(&pEnc->sOriginal, &pEnc->current->image,                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
949                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
950  #endif                  }
951    
952                  emms();                  emms();
953    
# Line 936  Line 983 
983    
984                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
985                                  }                                  }
   
986  #undef OFFSET  #undef OFFSET
987                          }                          }
   
988                          xvid_free(temp_dquants);                          xvid_free(temp_dquants);
989                  }                  }
   
990          }          }
991    
992          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 952  Line 996 
996    
997          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
998                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
999                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) {
         {  
1000                  mode = I_VOP;                  mode = I_VOP;
1001          }else{          }else{
1002                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1003                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1004                                          (mode < 0) ? pEnc->iFrameNum : 0,                                          (/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0,
1005                                          bframes_count++);                                          bframes_count++, pFrame->bframe_threshold);
1006          }          }
1007    
1008          if (mode == I_VOP) {          if (mode == I_VOP) {
# Line 989  Line 1032 
1032                          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");
1033                  }                  }
1034    
1035                  // 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 */
1036    
1037                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1038    
# Line 1002  Line 1045 
1045                          }                          }
1046                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
1047                          bframes_count = 0;                          bframes_count = 0;
   
1048                          pFrame->intra = 0;                          pFrame->intra = 0;
1049    
1050                  } else {                  } else {
# Line 1026  Line 1068 
1068                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1069                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1070                   */                   */
1071          } else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1072                  /*                  /*
1073                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1074                   */                   */
# Line 1082  Line 1124 
1124    
1125                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1126    
1127  // bframe report by koepi                  /* bframe report by koepi */
1128                  pFrame->intra = 2;                  pFrame->intra = 2;
1129                  pFrame->length = 0;                  pFrame->length = 0;
1130    
# Line 1099  Line 1141 
1141                  pResult->kblks = pEnc->current->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1142                  pResult->mblks = pEnc->current->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1143                  pResult->ublks = pEnc->current->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
         }  
1144    
1145          emms();                  if (pFrame->general & XVID_EXTRASTATS)
1146                    {       pResult->sse_y =
1147  #ifdef _DEBUG_PSNR                                  plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
         psnr =  
                 image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
1148                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1149                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1150    
1151          printf("PSNR: %f\n", psnr);                          pResult->sse_u =
1152  //      DEBUG(temp);                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1153  #endif                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1154                                                       pEnc->mbParam.height/2);
1155    
1156                            pResult->sse_v =
1157                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1158                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1159                                                       pEnc->mbParam.height/2);
1160                    }
1161            }
1162    
1163            emms();
1164    
1165          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1166                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
# Line 1146  Line 1195 
1195          uint32_t bits;          uint32_t bits;
1196          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1197    
 #ifdef _DEBUG_PSNR  
1198          float psnr;          float psnr;
1199          uint8_t temp[128];          char temp[128];
 #endif  
1200    
1201          start_global_timer();          start_global_timer();
1202    
# Line 1180  Line 1227 
1227                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1228          stop_conv_timer();          stop_conv_timer();
1229    
1230  #ifdef _DEBUG_PSNR          if ((pFrame->general & XVID_CHROMAOPT)) {
1231          image_copy(&pEnc->sOriginal, &pEnc->current->image,                  image_chroma_optimize(&pEnc->current->image,
1232                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1233            }
1234    
1235            if (pFrame->general & XVID_EXTRASTATS)
1236            {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1237                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1238  #endif          }
1239    
1240          emms();          emms();
1241    
# Line 1260  Line 1312 
1312          }          }
1313    
1314          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1315                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == -1)
1316                          || ((pEnc->mbParam.iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1317                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1318                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
# Line 1276  Line 1328 
1328    
1329          }          }
1330    
1331  //      BitstreamPutBits(&bs, 0xFFFF, 16);          /* Relic from OpenDivX - now disabled
1332  //      BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1333            BitstreamPutBits(&bs, 0xFFFF, 16);
1334            */
1335    
1336          BitstreamPadAlways(&bs);          BitstreamPadAlways(&bs);
1337          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1338    
# Line 1295  Line 1350 
1350                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1351                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1352          }          }
1353  #ifdef _DEBUG_PSNR          if (pFrame->general & XVID_EXTRASTATS)
1354            {
1355          psnr =          psnr =
1356                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1357                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1358                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1359    
1360          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1361  //      DEBUG(temp);          }
 #endif  
1362    
1363          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1364    
# Line 1368  Line 1423 
1423                  return;                  return;
1424          }          }
1425    
1426          pEnc->current->fcode =          pEnc->current->fcode = (hint->rawhints) ?
1427                  (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,                  (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
                                                                                                                                  FCODEBITS);  
1428    
1429          length = pEnc->current->fcode + 5;          length = pEnc->current->fcode + 5;
1430          high = 1 << (length - 1);          high = 1 << (length - 1);
# Line 1385  Line 1439 
1439                          VECTOR tmp;                          VECTOR tmp;
1440                          int vec;                          int vec;
1441    
1442                          pMB->mode =                          pMB->mode =     (hint->rawhints) ?
1443                                  (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,                                  (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
                                                                                                                                   MODEBITS);  
1444    
1445                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1446                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1447    
1448                          if (pMB->mode == MODE_INTER) {                          if (pMB->mode == MODE_INTER) {
1449                                  tmp.x =                                  tmp.x = (hint->rawhints) ?
1450                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,                                          bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
1451                                                                                                                                                    length);                                  tmp.y = (hint->rawhints) ?
1452                                  tmp.y =                                          bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
                                         (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,  
                                                                                                                                                   length);  
1453                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1454                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1455    
# Line 1412  Line 1463 
1463                                  }                                  }
1464                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1465                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1466                                          tmp.x =                                          tmp.x = (hint->rawhints) ?
1467                                                  (hint->rawhints) ? bhint->mvs[vec].                                                  bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
1468                                                  x : BitstreamGetBits(&bs, length);                                          tmp.y = (hint->rawhints) ?
1469                                          tmp.y =                                                  bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
                                                 (hint->rawhints) ? bhint->mvs[vec].  
                                                 y : BitstreamGetBits(&bs, length);  
1470                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1471                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1472    
# Line 1428  Line 1477 
1477                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1478                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1479                                  }                                  }
1480                          } else                          // intra / stuffing / not_coded                          } else                          /* intra / stuffing / not_coded */
1481                          {                          {
1482                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1483                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
# Line 1565  Line 1614 
1614                          pEnc->mbParam.width, pEnc->mbParam.height);                          pEnc->mbParam.width, pEnc->mbParam.height);
1615                  stop_edges_timer();                  stop_edges_timer();
1616          }          }
   
1617          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1618          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1619          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1625  Line 1673 
1673                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1674          }          }
1675    
1676          return 1;                                       // intra          return 1;                                       /* intra */
1677  }  }
1678    
1679    
# Line 1663  Line 1711 
1711                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
1712          }          }
1713    
   
1714          start_timer();          start_timer();
1715          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1716                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height);
# Line 1767  Line 1814 
1814                                          pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                          pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1815                                          pEnc->mbParam.edged_width, 65536);                                          pEnc->mbParam.edged_width, 65536);
1816    
1817                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1818                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1819                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1820    
1821                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1822                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1823                                    }
1824    
1825                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1826    
1827                                          if (pEnc->mbParam.m_quarterpel)                                          if (pEnc->mbParam.m_quarterpel)
# Line 1955  Line 2010 
2010          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2011    
2012          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2013                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */
2014          {          {
2015                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2016                  iSearchRange *= 2;                  iSearchRange *= 2;
2017          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2018                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2019                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2020                          && (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 */
2021          {          {
2022                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2023                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1983  Line 2038 
2038                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2039                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2040    
2041                  // copy reference frame details into the current frame                  /* copy reference frame details into the current frame */
2042                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
2043                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
2044                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
# Line 2012  Line 2067 
2067    
2068          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2069    
2070          return 0;                                       // inter          return 0;                                       /* inter */
2071  }  }
2072    
2073    
# Line 2045  Line 2100 
2100    
2101          frame->quarterpel =  pEnc->mbParam.m_quarterpel;          frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2102    
2103          // forward          /* forward  */
2104          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2105                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2106                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
# Line 2055  Line 2110 
2110                                            pEnc->mbParam.m_quarterpel, 0);                                            pEnc->mbParam.m_quarterpel, 0);
2111          stop_inter_timer();          stop_inter_timer();
2112    
2113          // backward          /* backward */
2114          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2115                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2116                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
# Line 2068  Line 2123 
2123          start_timer();          start_timer();
2124    
2125          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2126                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2127                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2128                          pEnc->reference->mbs, f_ref,                          pEnc->reference->mbs, f_ref,
2129                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2130                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
# Line 2077  Line 2132 
2132    
2133    
2134          stop_motion_timer();          stop_motion_timer();
2135            /*
2136          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
            {  
2137             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
2138             } */          }
2139            */
2140    
2141          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2142    
# Line 2101  Line 2156 
2156                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2157                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2158    
2159                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2160                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2161                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */
2162                                  continue;                                  continue;
2163                          }                          }
2164    
# Line 2122  Line 2177 
2177    
2178                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2179                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2180                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2181                                  }                                  }
2182                          }                          }
2183    
# Line 2138  Line 2193 
2193    
2194          emms();          emms();
2195    
2196          // TODO: dynamic fcode/bcode ???          /* TODO: dynamic fcode/bcode ??? */
2197    
2198          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2199    

Legend:
Removed from v.1.76.2.37  
changed lines
  Added in v.1.96

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