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

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

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

revision 1065, Wed Jun 11 12:37:41 2003 UTC revision 1216, Wed Nov 19 15:42:38 2003 UTC
# Line 21  Line 21 
21   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   * $Id: encoder.c,v 1.95.2.29 2003-06-11 12:37:41 suxen_drol Exp $   * $Id: encoder.c,v 1.95.2.54 2003-11-19 15:42:38 syskin Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 38  Line 38 
38  #include "image/font.h"  #include "image/font.h"
39  #include "motion/sad.h"  #include "motion/sad.h"
40  #include "motion/motion.h"  #include "motion/motion.h"
41    #include "motion/gmc.h"
42    
43  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
44  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
45  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 115  Line 117 
117          Encoder *pEnc;          Encoder *pEnc;
118      int n;      int n;
119    
120          if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */          if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
121                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
122    
123          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
# Line 186  Line 188 
188          pcreate.zones = pEnc->zones;          pcreate.zones = pEnc->zones;
189          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
190          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
191                    pcreate.mb_width = pEnc->mbParam.mb_width;
192                    pcreate.mb_height = pEnc->mbParam.mb_height;
193          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
194          pcreate.fbase = pEnc->mbParam.fbase;          pcreate.fbase = pEnc->mbParam.fbase;
195          pcreate.param = create->plugins[n].param;          pcreate.param = create->plugins[n].param;
# Line 224  Line 228 
228          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
229    
230      /* max keyframe interval */      /* max keyframe interval */
231      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ?          pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
                 (10 * pEnc->mbParam.fbase) / pEnc->mbParam.fincr : create->max_key_interval;  
232    
233      /* allocate working frame-image memory */      /* allocate working frame-image memory */
234    
# Line 262  Line 265 
265          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
266          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
267          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
268          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
269    
270          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
271          if (image_create          if (image_create
# Line 308  Line 309 
309                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
310                  goto xvid_err_memory3;                  goto xvid_err_memory3;
311          if (image_create          if (image_create
                 (&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create  
312                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
313                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
314                  goto xvid_err_memory3;                  goto xvid_err_memory3;
         if (image_create  
                 (&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
315    
316  /* Create full bitplane for GMC, this might be wasteful */  /* Create full bitplane for GMC, this might be wasteful */
317          if (image_create          if (image_create
# Line 388  Line 381 
381                  image_null(&pEnc->queue[n].image);                  image_null(&pEnc->queue[n].image);
382    
383    
384          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
         {  
385                  if (image_create                  if (image_create
386                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
387                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
388                          goto xvid_err_memory5;                          goto xvid_err_memory5;
   
389          }          }
390    
   
391          /* timestamp stuff */          /* timestamp stuff */
392    
393          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
# Line 422  Line 412 
412    
413    xvid_err_memory5:    xvid_err_memory5:
414    
415          if (pEnc->mbParam.max_bframes > 0) {          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
416          int i;                          image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
   
                 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {  
                         image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,  
417                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
418                  }                  }
419    
420                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
421    
422    xvid_err_memory4:    xvid_err_memory4:
423    
# Line 444  Line 431 
431    
432                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
433                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
434                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
435                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
436                  }                  }
437    
438                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 478  Line 462 
462                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
463          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
464                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
465          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
466                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
467    
468  /* destroy GMC image */  /* destroy GMC image */
469          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
# Line 537  Line 517 
517          int i;          int i;
518    
519          /* B Frames specific */          /* B Frames specific */
         if (pEnc->mbParam.max_bframes > 0) {  
   
520                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
   
521                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
522                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
523                  }                  }
                 xvid_free(pEnc->queue);  
         }  
524    
525            xvid_free(pEnc->queue);
526    
527          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
528    
# Line 557  Line 533 
533    
534                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
535                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
536                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
537                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
538                  }                  }
539    
# Line 577  Line 551 
551                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
552          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
553                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
554          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
555                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
   
556          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
557                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
558          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
# Line 662  Line 631 
631      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
632      data.fincr = frame->fincr;      data.fincr = frame->fincr;
633      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
634            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
635            data.bquant_offset = pEnc->mbParam.bquant_offset;
636    
637      for (i=0; i<3; i++) {      for (i=0; i<3; i++) {
638          data.min_quant[i] = pEnc->mbParam.min_quant[i];          data.min_quant[i] = pEnc->mbParam.min_quant[i];
# Line 687  Line 658 
658      data.frame_num = frame->frame_num;      data.frame_num = frame->frame_num;
659    
660      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
661          data.type = XVID_TYPE_AUTO;                  data.type = *type;
662          data.quant = 0;                  data.quant = *quant;
663    
664                    data.vol_flags = frame->vol_flags;
665                    data.vop_flags = frame->vop_flags;
666                    data.motion_flags = frame->motion_flags;
667    
668            } else if (opt == XVID_PLG_FRAME) {
669                    data.type = coding2type(frame->coding_type);
670                    data.quant = frame->quant;
671    
672                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
673              data.dquant = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
# Line 696  Line 675 
675                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height);
676          }          }
677    
         data.vol_flags = frame->vol_flags;  
         data.vop_flags = frame->vop_flags;  
         data.motion_flags = frame->motion_flags;  
   
678      } else { /* XVID_PLG_AFTER */      } else { /* XVID_PLG_AFTER */
679          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
680              data.original.csp = XVID_CSP_USER;              data.original.csp = XVID_CSP_USER;
# Line 739  Line 714 
714    
715              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
716              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
717                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;                                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
718              }              }
719          }          }
720    
# Line 769  Line 744 
744      }      }
745    
746      /* call plugins */      /* call plugins */
747      for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
748          emms();          emms();
749          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
750              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
# Line 784  Line 759 
759          *type = data.type;          *type = data.type;
760          *quant = data.quant > 0 ? data.quant : 2;   /* default */          *quant = data.quant > 0 ? data.quant : 2;   /* default */
761    
762                    frame->vol_flags = data.vol_flags;
763                    frame->vop_flags = data.vop_flags;
764                    frame->motion_flags = data.motion_flags;
765    
766            } else if (opt == XVID_PLG_FRAME) {
767    
768          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
769              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
770              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
# Line 795  Line 776 
776                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
777              }              }
778          }          }
779                    frame->mbs[0].quant = data.quant; /* BEFORE2 will not affect the quant in stats */
         frame->vol_flags = data.vol_flags;  
         frame->vop_flags = data.vop_flags;  
         frame->motion_flags = data.motion_flags;  
780      }      }
781    
782    
783  }  }
784    
785    
# Line 865  Line 845 
845          int type;          int type;
846          Bitstream bs;          Bitstream bs;
847    
848          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))     /* v1.x.x */
849                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
850    
851          xFrame->out_flags = 0;          xFrame->out_flags = 0;
# Line 902  Line 882 
882    
883                  if (xFrame->quant_intra_matrix)                  if (xFrame->quant_intra_matrix)
884                  {                  {
885                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
886                          q->frame.quant_intra_matrix = q->quant_intra_matrix;                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
887                  }                  }
888    
889                  if (xFrame->quant_inter_matrix)                  if (xFrame->quant_inter_matrix)
890                  {                  {
891                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
892                          q->frame.quant_inter_matrix = q->quant_inter_matrix;                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
893                  }                  }
894    
# Line 963  Line 943 
943                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
944                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
945    
946                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
947                          BitstreamPad(&bs);                          BitstreamPad(&bs);
948                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
949    
# Line 1008  Line 988 
988                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
989    
990                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
991                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
992                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
993                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
994    
995                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
996                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 1083  Line 1065 
1065                  }else{                  }else{
1066                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1067                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1068                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1069                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1070                  }                  }
1071          }          }
1072    
# Line 1096  Line 1079 
1079    
1080          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1081                  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,
1082                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                          "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1083          }          }
1084    
1085          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1175  Line 1158 
1158                  }                  }
1159    
1160                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1161                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1162                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1163                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1164          type = P_VOP;          type = P_VOP;
1165      }      }
1166    
# Line 1194  Line 1179 
1179                          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");
1180                  }                  }
1181    
1182                    pEnc->iFrameNum = 1;
1183    
1184                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1185                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1186                    switch(frame->par) {
1187                    case XVID_PAR_11_VGA:
1188                    case XVID_PAR_43_PAL:
1189                    case XVID_PAR_43_NTSC:
1190                    case XVID_PAR_169_PAL:
1191                    case XVID_PAR_169_NTSC:
1192                    case XVID_PAR_EXT:
1193                            pEnc->mbParam.par = frame->par;
1194                            break;
1195                    default:
1196                            pEnc->mbParam.par = XVID_PAR_EXT;
1197                            break;
1198                    }
1199                    pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1;
1200                    pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1;
1201    
1202          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1203                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
# Line 1250  Line 1251 
1251           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1252           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1253    
1254  done_flush:  /*done_flush:*/
1255    
1256      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1257    
# Line 1280  Line 1281 
1281    
1282  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1283  {  {
1284      unsigned int i,j;          unsigned int i;
1285      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1286            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1287            if (quant > 31)
1288                    frame->quant = quant = 31;
1289            else if (quant < 1)
1290                    frame->quant = quant = 1;
1291    
1292      for (j=0; j<pParam->mb_height; j++)          for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
     for (i=0; i<pParam->mb_width; i++) {  
         MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];  
1293          quant += pMB->dquant;          quant += pMB->dquant;
1294          if (quant > 31)          if (quant > 31)
1295                          quant = 31;                          quant = 31;
1296                  if (quant < 1)                  else if (quant < 1)
1297                          quant = 1;                          quant = 1;
1298          pMB->quant = quant;          pMB->quant = quant;
1299                    pMB++;
1300      }      }
1301  }  }
1302    
# Line 1347  Line 1352 
1352          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1353          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1354    
1355            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1356    
1357      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1358    
1359          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1360    
1361          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1362    
1363          BitstreamPadAlways(bs);          BitstreamPad(bs);
1364          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1365            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1366    
1367          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1368          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
# Line 1392  Line 1400 
1400          }          }
1401          emms();          emms();
1402    
1403  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1404  #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1405      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1406    
1407          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1408          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1409    
1410            pEnc->current->is_edged = 0; /* not edged */
1411            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1412    
1413          return 1;                                       /* intra */          return 1;                                       /* intra */
1414  }  }
1415    
# Line 1428  Line 1431 
1431          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1432          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1433    
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
1434          int iLimit;          int iLimit;
1435          int x, y, k;          int x, y, k;
1436          int iSearchRange;          int iSearchRange;
1437          int bIntra, skip_possible;          int bIntra=0, skip_possible;
1438            FRAMEINFO *const current = pEnc->current;
1439            FRAMEINFO *const reference = pEnc->reference;
1440            MBParam * const pParam = &pEnc->mbParam;
1441            int mb_width = pParam->mb_width;
1442            int mb_height = pParam->mb_height;
1443    
         /* IMAGE *pCurrent = &pEnc->current->image; */  
         IMAGE *pRef = &pEnc->reference->image;  
1444    
1445          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))          /* IMAGE *pCurrent = &current->image; */
1446            IMAGE *pRef = &reference->image;
1447    
1448            if ((current->vop_flags & XVID_VOP_REDUCED))
1449          {          {
1450                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
1451                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pParam->height + 31) / 32;
1452          }          }
1453    
1454    
1455            if (!reference->is_edged) {
1456          start_timer();          start_timer();
1457          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1458                                     pEnc->mbParam.width, pEnc->mbParam.height);                                             pParam->width, pParam->height);
1459          stop_edges_timer();          stop_edges_timer();
1460                    reference->is_edged = 1;
1461            }
1462    
1463          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1464          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1465          pEnc->current->fcode = pEnc->mbParam.m_fcode;          current->fcode = pParam->m_fcode;
1466    
1467          if (!force_inter)          if (!force_inter)
1468                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
1469          else          else
1470                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1471    
1472          if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1473                    if (reference->is_interpolated != current->rounding_type) {
1474                  start_timer();                  start_timer();
1475                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1476                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                            &pEnc->vInterHV, pParam->edged_width,
1477                                                    pEnc->mbParam.edged_height,                                                            pParam->edged_height,
1478                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1479                                                    pEnc->current->rounding_type);                                                            current->rounding_type);
1480                  stop_inter_timer();                  stop_inter_timer();
1481                            reference->is_interpolated = current->rounding_type;
1482                    }
1483          }          }
1484    
1485          pEnc->current->coding_type = P_VOP;          current->coding_type = P_VOP;
1486    
1487            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1488    
1489      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          SetMacroblockQuants(&pEnc->mbParam, current);
1490    
1491          start_timer();          start_timer();
1492          bIntra = MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1493                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          {       int gmcval;
1494                           iLimit);                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1495                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1496    
1497                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1498                            gmcval = GlobalMotionEstRefine(&current->warp,
1499                                                                                       current->mbs, pParam,
1500                                                                                       current, reference,
1501                                                                                       &current->image,
1502                                                                                       &reference->image,
1503                                                                                       &pEnc->vInterH,
1504                                                                                       &pEnc->vInterV,
1505                                                                                       &pEnc->vInterHV);
1506                    } else {
1507                            gmcval = globalSAD(&current->warp, pParam, current->mbs,
1508                                                               current,
1509                                                               &reference->image,
1510                                                               &current->image,
1511                                                               pEnc->vGMC.y);
1512                    }
1513    
1514                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1515    
1516                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1517                    generate_GMCparameters( 3, 3, &current->warp,
1518                                    pParam->width, pParam->height,
1519                                    &current->new_gmc_data);
1520    
1521                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1522                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1523                    {
1524                            current->coding_type = S_VOP;
1525    
1526                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1527                                    pParam->mb_width, pParam->mb_height,
1528                                    pParam->edged_width, pParam->edged_width/2,
1529                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1530                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1531    
1532          stop_motion_timer();                  } else {
1533    
1534          if (bIntra == 1) return FrameCodeI(pEnc, bs);                          generate_GMCimage(&current->new_gmc_data, &reference->image,
1535                                    pParam->mb_width, pParam->mb_height,
1536                                    pParam->edged_width, pParam->edged_width/2,
1537                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1538                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1539                    }
1540            }
1541    
1542          if ( ( pEnc->current->vol_flags & XVID_VOL_GMC )          bIntra =
1543                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )                  MotionEstimation(&pEnc->mbParam, current, reference,
1544          {                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1545                  pEnc->current->coding_type = S_VOP;                                           &pEnc->vGMC, iLimit);
1546    
                 generate_GMCparameters( 2, 16, &pEnc->current->warp,  
                                         pEnc->mbParam.width, pEnc->mbParam.height,  
                                         &pEnc->current->gmc_data);  
   
                 generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,  
                                 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,  
                                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,  
                                 pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0,  
                                 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);  
1547    
1548          }          stop_motion_timer();
1549    
1550          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1551    
1552            set_timecodes(current,reference,pParam->fbase);
1553          if (vol_header)          if (vol_header)
1554          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1555                  BitstreamPadAlways(bs);                  BitstreamPad(bs);
1556          }          }
1557    
1558          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1559    
1560          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1561                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1562    
1563    
1564          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1565                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1566                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1567                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &current->mbs[x + y * pParam->mb_width];
   
 /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */  
 /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */  
1568    
1569                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1570    
1571                          if (bIntra) {                          if (bIntra) {
1572                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1573                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1574                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1575    
1576                                  start_timer();                                  start_timer();
1577                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1578                                  stop_prediction_timer();                                  stop_prediction_timer();
1579    
1580                                  pEnc->current->sStat.kblks++;                                  current->sStat.kblks++;
1581    
1582                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1583                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1584                                            qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1585                                            qcoeff[5*64+0]=0;
1586                                    }
1587                                    MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1588                                  stop_coding_timer();                                  stop_coding_timer();
1589                                  continue;                                  continue;
1590                          }                          }
1591    
                         if (pEnc->current->coding_type == S_VOP) {  
   
                                 int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->mbParam.edged_width, 65536);  
   
                                 if (pEnc->current->motion_flags & XVID_ME_CHROMA16) {  
                                         iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
   
                                         iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
                                 }  
   
                                 if (iSAD <= pMB->sad16) {               /* mode decision GMC */  
   
                                         if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))  
                                                 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;  
                                         else  
                                                 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
                                         pMB->mode = MODE_INTER;  
                                         pMB->mcsel = 1;  
                                         pMB->sad16 = iSAD;  
                                 } else {  
                                         pMB->mcsel = 0;  
                                 }  
                         } else {  
                                 pMB->mcsel = 0; /* just a precaution */  
                         }  
   
1592                          start_timer();                          start_timer();
1593                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1594                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1595                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1596                                                                   &pEnc->current->image,                                                                   &current->image,
1597                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1598                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1599                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1600                                                                   (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1601                                                                   (pEnc->current->vop_flags & XVID_VOP_REDUCED),                                                                   (current->vop_flags & XVID_VOP_REDUCED),
1602                                                                   pEnc->current->rounding_type);                                                                   current->rounding_type);
1603    
1604                          stop_comp_timer();                          stop_comp_timer();
1605    
# Line 1592  Line 1611 
1611    
1612                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->mode != MODE_NOT_CODED)
1613                          {       pMB->cbp =                          {       pMB->cbp =
1614                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
1615                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1616                          }                          }
1617    
1618                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1619                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1620                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1621                                  pEnc->current->sStat.mblks++;                                  current->sStat.mblks++;
1622                          }  else {                          }  else {
1623                                  pEnc->current->sStat.ublks++;                                  current->sStat.ublks++;
1624                          }                          }
1625    
1626                          start_timer();                          start_timer();
# Line 1611  Line 1630 
1630                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1631                                                          (pMB->dquant == 0);                                                          (pMB->dquant == 0);
1632    
1633                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1634                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1635                          else if (pEnc->current->coding_type == P_VOP) {                          else if (current->coding_type == P_VOP) {
1636                                  if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))
1637                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1638                                  else                                  else
1639                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
# Line 1624  Line 1643 
1643    
1644  /* 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 */
1645    
1646                                  if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */                                  if (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */
1647                                  {                                  {
1648                                          int bSkip = 1;                                          int bSkip = 1;
1649    
1650                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1651                                          {                                          {
1652                                                  int iSAD;                                                  int iSAD;
1653                                                  iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1654                                                                          pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1655                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);                                                                  pParam->edged_width,BFRAME_SKIP_THRESHHOLD);
1656                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1657                                                  {       bSkip = 0;                                                  {       bSkip = 0;
1658                                                          break;                                                          break;
# Line 1641  Line 1660 
1660                                          }                                          }
1661    
1662                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1663                                                  if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                                  if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1664                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1665                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1666                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1667                                                  }                                                  }
1668                                                  else {                                                  else {
1669                                                          VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1670                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1671                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1672                                                  }                                                  }
1673                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1674                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1675                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1676                                                  stop_coding_timer();                                                  stop_coding_timer();
1677    
1678                                                  continue;       /* next MB */                                                  continue;       /* next MB */
# Line 1668  Line 1687 
1687                          }                          }
1688                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1689    
1690                          if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE))                          if ((current->vop_flags & XVID_VOP_GREYSCALE))
1691                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1692                                  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 */
1693                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1694                          }                          }
1695    
1696                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1697                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1698                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1699                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1700                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1701                          } else {                          } else {
1702                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1703                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1704                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1705                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
# Line 1691  Line 1710 
1710                          {       int k;                          {       int k;
1711                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1712                                  {                                  {
1713                                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1714                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k);
1715                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1716                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1717                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1718                                          } else {                                          } else {
1719                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k);
1720                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1721                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1722                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
# Line 1706  Line 1725 
1725                                  }                                  }
1726                          }                          }
1727    
1728                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1729                          stop_coding_timer();                          stop_coding_timer();
1730    
1731                  }                  }
1732          }          }
1733    
1734          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))          if ((current->vop_flags & XVID_VOP_REDUCED))
1735          {          {
1736                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&current->image, pParam->edged_width,
1737                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          current->mbs, mb_width, mb_height, pParam->mb_width,
1738                          16, 0);                          16, 0);
1739          }          }
1740    
1741          emms();          emms();
1742    
1743          if (pEnc->current->sStat.iMvCount == 0)          if (current->sStat.iMvCount == 0)
1744                  pEnc->current->sStat.iMvCount = 1;                  current->sStat.iMvCount = 1;
1745    
1746          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);          fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);
1747    
1748          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pParam->m_fcode);
1749    
1750          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1751          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0)  )))     /* maximum search range 128 */                  && (pParam->m_fcode <= (3 +  (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0)  ))) /* maximum search range 128 */
1752          {          {
1753                  pEnc->mbParam.m_fcode++;                  pParam->m_fcode++;
1754                  iSearchRange *= 2;                  iSearchRange *= 2;
1755          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1756                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1757                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1758                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) )))    /* minimum search range 16 */                             && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) )))        /* minimum search range 16 */
1759          {          {
1760                  pEnc->mbParam.m_fcode--;                  pParam->m_fcode--;
1761                  iSearchRange /= 2;                  iSearchRange /= 2;
1762          }          }
1763    
# Line 1746  Line 1765 
1765    
1766          /* frame drop code */          /* frame drop code */
1767  #if 0  #if 0
1768          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1769  #endif  #endif
1770          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          if (current->sStat.kblks + current->sStat.mblks <
1771                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100)
1772          {          {
1773                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = 0;
1774                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1775    
1776                  BitstreamReset(bs);                  BitstreamReset(bs);
1777    
1778                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
1779                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1780    
1781                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1782                  pEnc->current->quant = pEnc->reference->quant;                  current->quant = reference->quant;
1783                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  current->motion_flags = reference->motion_flags;
1784                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1785                  pEnc->current->fcode = pEnc->reference->fcode;                  current->fcode = reference->fcode;
1786                  pEnc->current->bcode = pEnc->reference->bcode;                  current->bcode = reference->bcode;
1787                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1788                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1789          }          }
1790    
1791            pEnc->current->is_edged = 0; /* not edged */
1792            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1793    
1794            /* what was this frame's interpolated reference will become
1795                    forward (past) reference in b-frame coding */
1796    
1797            image_swap(&pEnc->vInterH, &pEnc->f_refh);
1798            image_swap(&pEnc->vInterV, &pEnc->f_refv);
1799            image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1800    
1801    
1802          /* XXX: debug          /* XXX: debug
1803          {          {
1804                  char s[100];                  char s[100];
1805                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1806                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
1807                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1808                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1809    
1810                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1811                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
1812                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1813                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1814          }          }
1815          */          */
1816    
1817  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
 #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1818    
1819      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          current->length = (BitstreamPos(bs) - bits) / 8;
1820    
1821          return 0;                                       /* inter */          return 0;                                       /* inter */
1822  }  }
# Line 1829  Line 1850 
1850  #endif  #endif
1851    
1852          /* forward  */          /* forward  */
1853            if (!pEnc->reference->is_edged) {
1854          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1855                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1856                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1857                    pEnc->current->is_edged = 1;
1858            }
1859    
1860            if (pEnc->reference->is_interpolated != 0) {
1861          start_timer();          start_timer();
1862          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1863                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1864                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1865          stop_inter_timer();          stop_inter_timer();
1866                    pEnc->reference->is_interpolated = 0;
1867            }
1868    
1869          /* backward */          /* backward */
1870            if (!pEnc->current->is_edged) {
1871          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1872                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1873                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1874                    pEnc->current->is_edged = 1;
1875            }
1876    
1877            if (pEnc->current->is_interpolated != 0) {
1878          start_timer();          start_timer();
1879          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1880                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1881                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1882          stop_inter_timer();          stop_inter_timer();
1883                    pEnc->current->is_interpolated = 0;
1884            }
1885    
1886            frame->coding_type = B_VOP;
1887            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1888    
1889          start_timer();          start_timer();
1890          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
# Line 1858  Line 1896 
1896                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
1897          stop_motion_timer();          stop_motion_timer();
1898    
         frame->coding_type = B_VOP;  
   
1899          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1900          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
1901    
1902          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1903          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
# Line 1873  Line 1909 
1909          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1910                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1911                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
                         int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0;  
1912    
1913                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1914                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1915                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1916                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1917                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0);
1918                                    }
1919    
1920                                  continue;                                  continue;
1921                          }                          }
1922    
# Line 1900  Line 1939 
1939                                  }                                  }
1940                          }                          }
1941    
1942  #ifdef BFRAMES_DEC_DEBUG                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the
1943          BFRAME_DEBUG                           * coding function for BFrames, that's why we don't zero teh DC
1944  #endif                           * coeffs */
1945                            if ((frame->vop_flags & XVID_VOP_GREYSCALE))
1946                                    mb->cbp &= 0x3C;
1947    
1948                          start_timer();                          start_timer();
1949                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1950                                                   &frame->sStat, direction);                                                   &frame->sStat);
1951                          stop_coding_timer();                          stop_coding_timer();
1952                  }                  }
1953          }          }
# Line 1914  Line 1956 
1956    
1957          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1958    
1959      BitstreamPadAlways(bs);          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1960          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1961    
1962  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1065  
changed lines
  Added in v.1216

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