[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.95.2.25, Thu May 22 16:34:47 2003 UTC revision 1.95.2.55, Thu Nov 20 12:07:19 2003 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002     Michael Militzer <isibaar@xvid.org>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *                         2002-2003 Peter Ross <pross@xvid.org>
8   *  to use this software module in hardware or software products are   *                         2002   Daniel Smith <danielsmith@astroboymail.com>
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 43  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"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
47  #include "utils/emms.h"  #include "utils/emms.h"
48  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "quant/adapt_quant.h"  
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
# Line 121  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 192  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 211  Line 209 
209      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
210              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
211                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
212                    if (pEnc->temp_dquants==NULL)
213                            goto xvid_err_memory1a;
214      }      }
     /* XXX: error checking */  
215    
216          /* bframes */          /* bframes */
217          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
# Line 229  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;  
   
         /* Bitrate allocator defaults  
   
         if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))  
                 create->min_quantizer = 1;  
   
         if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))  
                 create->max_quantizer = 31;  
   
         if (create->max_quantizer < create->min_quantizer)  
                 create->max_quantizer = create->min_quantizer; */  
   
232    
233      /* allocate working frame-image memory */      /* allocate working frame-image memory */
234    
# Line 279  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 325  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 405  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 439  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 461  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 495  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 515  Line 478 
478          xvid_free(pEnc->current);          xvid_free(pEnc->current);
479          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
480    
481      xvid_err_memory1a:
482      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
483              xvid_free(pEnc->temp_dquants);              xvid_free(pEnc->temp_dquants);
484      }      }
# Line 553  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 573  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 593  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 678  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 703  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 712  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    
678          /* todo: [vol,vop,motion]_flags*/          } 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;
681              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
# Line 753  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 783  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 798  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 809  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          /* todo: [vol,vop,motion]_flags*/                  frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
     }  
780  }  }
781    
782    
783    }
784    
785    
786  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
# Line 831  Line 798 
798      pEnc->m_framenum--; /* debug ticker */      pEnc->m_framenum--; /* debug ticker */
799  }  }
800    
801    static __inline void
802    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
803    {
804            if (pMB->cbp == 0) {
805                    /* we want to code dquant but the quantizer value will not be used yet
806                            let's find out if we can postpone dquant to next MB
807                    */
808                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
809                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
810                            return;
811                    } else {
812                            MACROBLOCK * next = pMB + 1;
813                            const MACROBLOCK * prev = pMB - 1;
814                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
815                                    /* mode allows dquant change in the future */
816                                    if (abs(next->quant - prev->quant) <= 2) {
817                                            /* quant change is not out of range */
818                                            pMB->quant = prev->quant;
819                                            pMB->dquant = 0;
820                                            next->dquant = next->quant - prev->quant;
821                                            return;
822                                    }
823                    }
824            }
825            /* couldn't skip this dquant */
826            pMB->mode = MODE_INTER_Q;
827    }
828    
829    
830    
831  static __inline void  static __inline void
# Line 876  Line 871 
871          int type;          int type;
872          Bitstream bs;          Bitstream bs;
873    
874          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 */
875                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
876    
877          xFrame->out_flags = 0;          xFrame->out_flags = 0;
# Line 913  Line 908 
908    
909                  if (xFrame->quant_intra_matrix)                  if (xFrame->quant_intra_matrix)
910                  {                  {
911                          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));
912                          q->frame.quant_intra_matrix = q->quant_intra_matrix;                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
913                  }                  }
914    
915                  if (xFrame->quant_inter_matrix)                  if (xFrame->quant_inter_matrix)
916                  {                  {
917                          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));
918                          q->frame.quant_inter_matrix = q->quant_inter_matrix;                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
919                  }                  }
920    
# Line 974  Line 969 
969                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
970                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
971    
972                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
973                          BitstreamPad(&bs);                          BitstreamPad(&bs);
974                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
975    
# Line 1019  Line 1014 
1014                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1015    
1016                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
1017                  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;
1018                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1019                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1020    
1021                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1022                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 1048  Line 1045 
1045                  goto done;      /* nothing to encode yet; encoder lag */                  goto done;      /* nothing to encode yet; encoder lag */
1046          }          }
1047    
1048          // the current FRAME becomes the reference          /* the current FRAME becomes the reference */
1049          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1050    
1051          // remove frame from encoding-queue (head), and move it into the current          /* remove frame from encoding-queue (head), and move it into the current */
1052          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1053          frame = &pEnc->queue[pEnc->queue_head].frame;          frame = &pEnc->queue[pEnc->queue_head].frame;
1054          pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);          pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
# Line 1094  Line 1091 
1091                  }else{                  }else{
1092                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1093                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1094                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1095                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1096                  }                  }
1097          }          }
1098    
# Line 1107  Line 1105 
1105    
1106          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1107                  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,
1108                          "%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);
1109          }          }
1110    
1111          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1167  Line 1165 
1165    
1166      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1167    
1168                  // place this frame back on the encoding-queue (head)                  /* place this frame back on the encoding-queue (head) */
1169                  // we will deal with it next time                  /* we will deal with it next time */
1170          dec_frame_num(pEnc);          dec_frame_num(pEnc);
1171          pEnc->iFrameNum--;          pEnc->iFrameNum--;
1172    
# Line 1176  Line 1174 
1174          pEnc->queue_size++;          pEnc->queue_size++;
1175                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1176    
1177                  // grab the last frame from the bframe-queue                  /* grab the last frame from the bframe-queue */
1178    
1179                  pEnc->bframenum_tail--;                  pEnc->bframenum_tail--;
1180                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 1186  Line 1184 
1184                  }                  }
1185    
1186                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1187                  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;
1188                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1189                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1190          type = P_VOP;          type = P_VOP;
1191      }      }
1192    
# Line 1205  Line 1205 
1205                          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");
1206                  }                  }
1207    
1208                    pEnc->iFrameNum = 1;
1209    
1210                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1211                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1212                    switch(frame->par) {
1213                    case XVID_PAR_11_VGA:
1214                    case XVID_PAR_43_PAL:
1215                    case XVID_PAR_43_NTSC:
1216                    case XVID_PAR_169_PAL:
1217                    case XVID_PAR_169_NTSC:
1218                    case XVID_PAR_EXT:
1219                            pEnc->mbParam.par = frame->par;
1220                            break;
1221                    default:
1222                            pEnc->mbParam.par = XVID_PAR_EXT;
1223                            break;
1224                    }
1225                    pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1;
1226                    pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1;
1227    
1228          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1229                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
# Line 1238  Line 1254 
1254           * encode this frame as an p-vop           * encode this frame as an p-vop
1255       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1256    
1257      } else { // (type == P_VOP || type == S_VOP)          } else { /* (type == P_VOP || type == S_VOP) */
1258    
1259                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1260                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
# Line 1261  Line 1277 
1277           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1278           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1279    
1280  done_flush:  /*done_flush:*/
1281    
1282      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1283    
# Line 1291  Line 1307 
1307    
1308  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1309  {  {
1310      unsigned int i,j;          unsigned int i;
1311      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1312            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1313            if (quant > 31)
1314                    frame->quant = quant = 31;
1315            else if (quant < 1)
1316                    frame->quant = quant = 1;
1317    
1318      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];  
1319          quant += pMB->dquant;          quant += pMB->dquant;
1320          if (quant > 31)          if (quant > 31)
1321                          quant = 31;                          quant = 31;
1322                  if (quant < 1)                  else if (quant < 1)
1323                          quant = 1;                          quant = 1;
1324          pMB->quant = quant;          pMB->quant = quant;
1325                    pMB++;
1326      }      }
1327  }  }
1328    
# Line 1358  Line 1378 
1378          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1379          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1380    
1381            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1382    
1383      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1384    
1385          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1386    
1387          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1388    
1389          BitstreamPadAlways(bs);          BitstreamPad(bs);
1390          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1391            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1392    
1393          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1394          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
# Line 1403  Line 1426 
1426          }          }
1427          emms();          emms();
1428    
1429  /* 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() */
1430  #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  
1431      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1432    
1433          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1434          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1435    
1436      /* XXX: hinted me          pEnc->current->is_edged = 0; /* not edged */
1437          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
                 HintedMEGet(pEnc, 1);  
         }*/  
1438    
1439          return 1;                                       /* intra */          return 1;                                       /* intra */
1440  }  }
# Line 1444  Line 1457 
1457          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1458          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1459    
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
1460          int iLimit;          int iLimit;
1461          int x, y, k;          int x, y, k;
1462          int iSearchRange;          int iSearchRange;
1463          int bIntra, skip_possible;          int bIntra=0, skip_possible;
1464            FRAMEINFO *const current = pEnc->current;
1465            FRAMEINFO *const reference = pEnc->reference;
1466            MBParam * const pParam = &pEnc->mbParam;
1467            int mb_width = pParam->mb_width;
1468            int mb_height = pParam->mb_height;
1469    
         /* IMAGE *pCurrent = &pEnc->current->image; */  
         IMAGE *pRef = &pEnc->reference->image;  
1470    
1471          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))          /* IMAGE *pCurrent = &current->image; */
1472            IMAGE *pRef = &reference->image;
1473    
1474            if ((current->vop_flags & XVID_VOP_REDUCED))
1475          {          {
1476                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
1477                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pParam->height + 31) / 32;
1478          }          }
1479    
1480    
1481            if (!reference->is_edged) {
1482          start_timer();          start_timer();
1483          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1484                                     pEnc->mbParam.width, pEnc->mbParam.height);                                             pParam->width, pParam->height);
1485          stop_edges_timer();          stop_edges_timer();
1486                    reference->is_edged = 1;
1487            }
1488    
1489          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1490          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1491          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          current->fcode = pParam->m_fcode;
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
1492    
1493          if (!force_inter)          if (!force_inter)
1494                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
1495          else          else
1496                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1497    
1498          if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1499                    if (reference->is_interpolated != current->rounding_type) {
1500                  start_timer();                  start_timer();
1501                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1502                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                            &pEnc->vInterHV, pParam->edged_width,
1503                                                    pEnc->mbParam.edged_height,                                                            pParam->edged_height,
1504                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1505                                                    pEnc->current->rounding_type);                                                            current->rounding_type);
1506                  stop_inter_timer();                  stop_inter_timer();
1507                            reference->is_interpolated = current->rounding_type;
1508                    }
1509          }          }
1510    
1511          pEnc->current->coding_type = P_VOP;          current->coding_type = P_VOP;
1512    
1513            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1514    
1515      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          SetMacroblockQuants(&pEnc->mbParam, current);
1516    
1517          start_timer();          start_timer();
1518          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1519                  HintedMESet(pEnc, &bIntra);          {       int gmcval;
1520          else*/                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1521                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1522    
1523                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1524                            gmcval = GlobalMotionEstRefine(&current->warp,
1525                                                                                       current->mbs, pParam,
1526                                                                                       current, reference,
1527                                                                                       &current->image,
1528                                                                                       &reference->image,
1529                                                                                       &pEnc->vInterH,
1530                                                                                       &pEnc->vInterV,
1531                                                                                       &pEnc->vInterHV);
1532                    } else {
1533                            gmcval = globalSAD(&current->warp, pParam, current->mbs,
1534                                                               current,
1535                                                               &reference->image,
1536                                                               &current->image,
1537                                                               pEnc->vGMC.y);
1538                    }
1539    
1540                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1541    
1542                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1543                    generate_GMCparameters( 3, 3, &current->warp,
1544                                    pParam->width, pParam->height,
1545                                    &current->new_gmc_data);
1546    
1547                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1548                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1549                    {
1550                            current->coding_type = S_VOP;
1551    
1552                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1553                                    pParam->mb_width, pParam->mb_height,
1554                                    pParam->edged_width, pParam->edged_width/2,
1555                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1556                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1557    
1558                    } else {
1559    
1560                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1561                                    pParam->mb_width, pParam->mb_height,
1562                                    pParam->edged_width, pParam->edged_width/2,
1563                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1564                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1565                    }
1566            }
1567    
1568                  bIntra =                  bIntra =
1569                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                  MotionEstimation(&pEnc->mbParam, current, reference,
1570                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1571                           iLimit);                                           &pEnc->vGMC, iLimit);
1572    
1573    
1574          stop_motion_timer();          stop_motion_timer();
1575    
1576          if (bIntra == 1) return FrameCodeI(pEnc, bs);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1577    
1578          if ( ( pEnc->current->vol_flags & XVID_VOL_GMC )          set_timecodes(current,reference,pParam->fbase);
                 && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )  
         {  
                 pEnc->current->coding_type = S_VOP;  
   
                 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);  
   
         }  
   
         set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);  
1579          if (vol_header)          if (vol_header)
1580          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1581                  BitstreamPadAlways(bs);                  BitstreamPad(bs);
1582          }          }
1583    
1584          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1585    
1586          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1587                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1588    
1589    
1590          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1591                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1592                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1593                                  &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 */  
1594    
1595                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1596    
1597                          if (bIntra) {                          if (bIntra) {
1598                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1599                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1600                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1601    
1602                                  start_timer();                                  start_timer();
1603                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1604                                  stop_prediction_timer();                                  stop_prediction_timer();
1605    
1606                                  pEnc->current->sStat.kblks++;                                  current->sStat.kblks++;
1607    
1608                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1609                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1610                                            qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1611                                            qcoeff[5*64+0]=0;
1612                                    }
1613                                    MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1614                                  stop_coding_timer();                                  stop_coding_timer();
1615                                  continue;                                  continue;
1616                          }                          }
1617    
                         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 */  
                         }  
   
1618                          start_timer();                          start_timer();
1619                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1620                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1621                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1622                                                                   &pEnc->current->image,                                                                   &current->image,
1623                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1624                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1625                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1626                                                                   (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1627                                                                   (pEnc->current->vop_flags & XVID_VOP_REDUCED),                                                                   (current->vop_flags & XVID_VOP_REDUCED),
1628                                                                   pEnc->current->rounding_type);                                                                   current->rounding_type);
1629    
1630                          stop_comp_timer();                          stop_comp_timer();
1631    
                         if (pMB->dquant != 0) {  
                 pMB->mode = MODE_INTER_Q;  
                         }  
   
1632                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1633    
1634                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->mode != MODE_NOT_CODED)
1635                          {       pMB->cbp =                          {       pMB->cbp =
1636                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
1637                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1638                          }                          }
1639    
1640                            if (pMB->dquant != 0)
1641                                    MBSetDquant(pMB, x, y, &pEnc->mbParam);
1642    
1643    
1644                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1645                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1646                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1647                                  pEnc->current->sStat.mblks++;                                  current->sStat.mblks++;
1648                          }  else {                          }  else {
1649                                  pEnc->current->sStat.ublks++;                                  current->sStat.ublks++;
1650                          }                          }
1651    
1652                          start_timer();                          start_timer();
# Line 1632  Line 1656 
1656                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1657                                                          (pMB->dquant == 0);                                                          (pMB->dquant == 0);
1658    
1659                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1660                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1661                          else if (pEnc->current->coding_type == P_VOP) {                          else if (current->coding_type == P_VOP) {
1662                                  if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))
1663                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1664                                  else                                  else
1665                                          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 1645  Line 1669 
1669    
1670  /* 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 */
1671    
1672                                  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 */
1673                                  {                                  {
1674                                          int bSkip = 1;                                          int bSkip = 1;
1675    
1676                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1677                                          {                                          {
1678                                                  int iSAD;                                                  int iSAD;
1679                                                  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,
1680                                                                          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,
1681                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);                                                                  pParam->edged_width,BFRAME_SKIP_THRESHHOLD);
1682                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1683                                                  {       bSkip = 0;                                                  {       bSkip = 0;
1684                                                          break;                                                          break;
# Line 1662  Line 1686 
1686                                          }                                          }
1687    
1688                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1689                                                  if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                                  if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1690                                                          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);
1691                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1692                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1693                                                  }                                                  }
1694                                                  else {                                                  else {
1695                                                          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);
1696                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1697                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1698                                                  }                                                  }
1699                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1700                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1701                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1702                                                  stop_coding_timer();                                                  stop_coding_timer();
1703    
1704                                                  continue;       /* next MB */                                                  continue;       /* next MB */
# Line 1689  Line 1713 
1713                          }                          }
1714                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1715    
1716                          if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE))                          if ((current->vop_flags & XVID_VOP_GREYSCALE))
1717                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1718                                  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 */
1719                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1720                          }                          }
1721    
1722                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1723                                  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);
1724                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1725                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1726                                  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);
1727                          } else {                          } else {
1728                                  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);
1729                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1730                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1731                                  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 1712  Line 1736 
1736                          {       int k;                          {       int k;
1737                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1738                                  {                                  {
1739                                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1740                                                  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);
1741                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1742                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1743                                  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);
1744                                          } else {                                          } else {
1745                                                  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);
1746                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1747                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1748                                  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 1727  Line 1751 
1751                                  }                                  }
1752                          }                          }
1753    
1754                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1755                          stop_coding_timer();                          stop_coding_timer();
1756    
1757                  }                  }
1758          }          }
1759    
1760          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))          if ((current->vop_flags & XVID_VOP_REDUCED))
1761          {          {
1762                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&current->image, pParam->edged_width,
1763                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          current->mbs, mb_width, mb_height, pParam->mb_width,
1764                          16, 0);                          16, 0);
1765          }          }
1766    
1767          emms();          emms();
1768    
1769      /* XXX: hinted me          if (current->sStat.iMvCount == 0)
1770          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {                  current->sStat.iMvCount = 1;
                 HintedMEGet(pEnc, 0);  
         }*/  
   
         if (pEnc->current->sStat.iMvCount == 0)  
                 pEnc->current->sStat.iMvCount = 1;  
1771    
1772          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);          fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);
1773    
1774          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pParam->m_fcode);
1775    
1776          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1777          && (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 */
1778          {          {
1779                  pEnc->mbParam.m_fcode++;                  pParam->m_fcode++;
1780                  iSearchRange *= 2;                  iSearchRange *= 2;
1781          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1782                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1783                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1784                             && (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 */
1785          {          {
1786                  pEnc->mbParam.m_fcode--;                  pParam->m_fcode--;
1787                  iSearchRange /= 2;                  iSearchRange /= 2;
1788          }          }
1789    
1790          pEnc->fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1791    
1792          /* frame drop code */          /* frame drop code */
1793          //DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
1794          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1795                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)  #endif
1796            if (current->sStat.kblks + current->sStat.mblks <
1797                    (pParam->frame_drop_ratio * mb_width * mb_height) / 100)
1798          {          {
1799                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = 0;
1800                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1801    
1802                  BitstreamReset(bs);                  BitstreamReset(bs);
1803    
1804                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
1805                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1806    
1807                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1808                  pEnc->current->quant = pEnc->reference->quant;                  current->quant = reference->quant;
1809                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  current->motion_flags = reference->motion_flags;
1810                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1811                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  current->fcode = reference->fcode;
1812                  pEnc->current->fcode = pEnc->reference->fcode;                  current->bcode = reference->bcode;
1813                  pEnc->current->bcode = pEnc->reference->bcode;                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1814                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1815                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);          }
1816          }  
1817            pEnc->current->is_edged = 0; /* not edged */
1818            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1819    
1820            /* what was this frame's interpolated reference will become
1821                    forward (past) reference in b-frame coding */
1822    
1823            image_swap(&pEnc->vInterH, &pEnc->f_refh);
1824            image_swap(&pEnc->vInterV, &pEnc->f_refv);
1825            image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1826    
1827    
1828          /* XXX: debug          /* XXX: debug
1829          {          {
1830                  char s[100];                  char s[100];
1831                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1832                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
1833                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1834                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1835    
1836                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1837                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
1838                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1839                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1840          }          }
1841          */          */
1842    
1843  /* 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  
1844    
1845      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          current->length = (BitstreamPos(bs) - bits) / 8;
1846    
1847          return 0;                                       /* inter */          return 0;                                       /* inter */
1848  }  }
# Line 1853  Line 1875 
1875          }          }
1876  #endif  #endif
1877    
         //frame->quarterpel =  pEnc->mbParam.m_quarterpel;  
   
1878          /* forward  */          /* forward  */
1879            if (!pEnc->reference->is_edged) {
1880          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1881                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1882                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1883                    pEnc->current->is_edged = 1;
1884            }
1885    
1886            if (pEnc->reference->is_interpolated != 0) {
1887          start_timer();          start_timer();
1888          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,
1889                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1890                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1891          stop_inter_timer();          stop_inter_timer();
1892                    pEnc->reference->is_interpolated = 0;
1893            }
1894    
1895          /* backward */          /* backward */
1896            if (!pEnc->current->is_edged) {
1897          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1898                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1899                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1900                    pEnc->current->is_edged = 1;
1901            }
1902    
1903            if (pEnc->current->is_interpolated != 0) {
1904          start_timer();          start_timer();
1905          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1906                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1907                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1908          stop_inter_timer();          stop_inter_timer();
1909                    pEnc->current->is_interpolated = 0;
1910            }
1911    
1912          start_timer();          frame->coding_type = B_VOP;
1913            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1914    
1915            start_timer();
1916          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1917                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
1918                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
# Line 1884  Line 1920 
1920                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1921                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
1922                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
   
   
1923          stop_motion_timer();          stop_motion_timer();
1924    
         /*  
         if (test_quant_type(&pEnc->mbParam, pEnc->current)) {  
                 BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
         }  
         */  
   
         frame->coding_type = B_VOP;  
   
1925          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1926          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
1927    
1928          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1929          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
# Line 1909  Line 1935 
1935          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1936                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1937                          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;  
1938    
1939                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1940                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1941                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1942                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1943                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0);
1944                                    }
1945    
1946                                  continue;                                  continue;
1947                          }                          }
1948    
# Line 1936  Line 1965 
1965                                  }                                  }
1966                          }                          }
1967    
1968  #ifdef BFRAMES_DEC_DEBUG                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the
1969          BFRAME_DEBUG                           * coding function for BFrames, that's why we don't zero teh DC
1970  #endif                           * coeffs */
1971                            if ((frame->vop_flags & XVID_VOP_GREYSCALE))
1972                                    mb->cbp &= 0x3C;
1973    
1974                          start_timer();                          start_timer();
1975                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1976                                                   &frame->sStat, direction);                                                   &frame->sStat);
1977                          stop_coding_timer();                          stop_coding_timer();
1978                  }                  }
1979          }          }
# Line 1950  Line 1982 
1982    
1983          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1984    
1985      BitstreamPadAlways(bs);          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1986          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1987    
1988  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.25  
changed lines
  Added in v.1.95.2.55

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