[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.45, Tue Oct 7 13:02:35 2003 UTC revision 1.126, Fri Feb 24 08:46:22 2006 UTC
# Line 49  Line 49 
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
52    # include "motion/motion_smp.h"
53    
54    
55  /*****************************************************************************  /*****************************************************************************
56   * Local function prototypes   * Local function prototypes
57   ****************************************************************************/   ****************************************************************************/
# Line 57  Line 60 
60                                            Bitstream * bs);                                            Bitstream * bs);
61    
62  static int FrameCodeP(Encoder * pEnc,  static int FrameCodeP(Encoder * pEnc,
63                                            Bitstream * bs,                                            Bitstream * bs);
                                           bool force_inter,  
                                           bool vol_header);  
64    
65  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
66                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
# Line 87  Line 88 
88  /*  /*
89   * Simplify the "fincr/fbase" fraction   * Simplify the "fincr/fbase" fraction
90  */  */
91    static int
92    gcd(int a, int b)
93    {
94            int r ;
95    
96            if (b > a) {
97                    r = a;
98                    a = b;
99                    b = r;
100            }
101    
102            while ((r = a % b)) {
103                    a = b;
104                    b = r;
105            }
106            return b;
107    }
108    
109  static void  static void
110  simplify_time(int *inc, int *base)  simplify_time(int *inc, int *base)
111  {  {
112          /* common factor */          /* common factor */
113          int i = *inc;          const int s = gcd(*inc, *base);
114          while (i > 1) {    *inc  /= s;
115                  if (*inc % i == 0 && *base % i == 0) {    *base /= s;
116                          *inc /= i;  
117                          *base /= i;          if (*base > 65535 || *inc > 65535) {
118                          i = *inc;                  int *biggest;
119                          continue;                  int *other;
120                  }                  float div;
121                  i--;  
122                    if (*base > *inc) {
123                            biggest = base;
124                            other = inc;
125                    } else {
126                            biggest = inc;
127                            other = base;
128          }          }
129    
130          /* if neccessary, round to 65535 accuracy */                  div = ((float)*biggest)/((float)65535);
131          if (*base > 65535) {                  *biggest = (unsigned int)(((float)*biggest)/div);
132                  float div = (float) *base / 65535;                  *other = (unsigned int)(((float)*other)/div);
                 *base = (int) (*base / div);  
                 *inc = (int) (*inc / div);  
133          }          }
134  }  }
135    
# Line 123  Line 146 
146          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
147                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
148    
149            if (create->width<=0 || create->height<=0)
150                    return XVID_ERR_FAIL;
151    
152          /* allocate encoder struct */          /* allocate encoder struct */
153    
154          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
# Line 134  Line 160 
160    
161          /* global flags */          /* global flags */
162      pEnc->mbParam.global_flags = create->global;      pEnc->mbParam.global_flags = create->global;
163      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED))
164        pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA;
165    
166      /* width, height */      /* width, height */
167          pEnc->mbParam.width = create->width;          pEnc->mbParam.width = create->width;
# Line 147  Line 175 
175      pEnc->mbParam.fincr = MAX(create->fincr, 0);      pEnc->mbParam.fincr = MAX(create->fincr, 0);
176          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
177      if (pEnc->mbParam.fincr>0)      if (pEnc->mbParam.fincr>0)
178              simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);                  simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase);
179    
180      /* zones */      /* zones */
181      if(create->num_zones > 0) {      if(create->num_zones > 0) {
# Line 178  Line 206 
206    
207          memset(&pinfo, 0, sizeof(xvid_plg_info_t));          memset(&pinfo, 0, sizeof(xvid_plg_info_t));
208          pinfo.version = XVID_VERSION;          pinfo.version = XVID_VERSION;
209          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {                  if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) {
210              pEnc->mbParam.plugin_flags |= pinfo.flags;              pEnc->mbParam.plugin_flags |= pinfo.flags;
211          }          }
212    
# Line 188  Line 216 
216          pcreate.zones = pEnc->zones;          pcreate.zones = pEnc->zones;
217          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
218          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
219                    pcreate.mb_width = pEnc->mbParam.mb_width;
220                    pcreate.mb_height = pEnc->mbParam.mb_height;
221          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
222          pcreate.fbase = pEnc->mbParam.fbase;          pcreate.fbase = pEnc->mbParam.fbase;
223          pcreate.param = create->plugins[n].param;          pcreate.param = create->plugins[n].param;
224    
225          pEnc->plugins[n].func = NULL;   /* disable plugins that fail */          pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
226          if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {                  if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
227              pEnc->plugins[n].func = create->plugins[n].func;              pEnc->plugins[n].func = create->plugins[n].func;
228          }          }
229      }      }
# Line 211  Line 241 
241              goto xvid_err_memory1a;              goto xvid_err_memory1a;
242      }      }
243    
244            /* temp lambdas */
245            if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
246                    pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width *
247                                                    pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE);
248                    if (pEnc->temp_lambda == NULL)
249                            goto xvid_err_memory1a;
250            }
251    
252          /* bframes */          /* bframes */
253          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
254          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
# Line 248  Line 286 
286          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
287                  goto xvid_err_memory2;                  goto xvid_err_memory2;
288    
289            /* allocate quant matrix memory */
290    
291            pEnc->mbParam.mpeg_quant_matrices =
292                    xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
293    
294            if (pEnc->mbParam.mpeg_quant_matrices == NULL)
295                    goto xvid_err_memory2a;
296    
297          /* allocate interpolation image memory */          /* allocate interpolation image memory */
298    
299      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
# Line 263  Line 309 
309          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
310          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
311          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
312          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
313    
314          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
315          if (image_create          if (image_create
# Line 309  Line 353 
353                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
354                  goto xvid_err_memory3;                  goto xvid_err_memory3;
355          if (image_create          if (image_create
                 (&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create  
356                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
357                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
358                  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;  
359    
360  /* Create full bitplane for GMC, this might be wasteful */  /* Create full bitplane for GMC, this might be wasteful */
361          if (image_create          if (image_create
# Line 408  Line 444 
444          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
445          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
446    
447            /* multithreaded stuff */
448            if (create->num_threads > 0) {
449                    int t = create->num_threads;
450                    int rows_per_thread = (pEnc->mbParam.mb_height+t-1)/t;
451                    pEnc->num_threads = t;
452                    pEnc->motionData = xvid_malloc(t*sizeof(SMPmotionData), CACHE_LINE);
453                    if (!pEnc->motionData)
454                            goto xvid_err_nosmp;
455    
456                    for (n = 0; n < t; n++) {
457                            pEnc->motionData[n].complete_count_self =
458                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
459    
460                            if (!pEnc->motionData[n].complete_count_self)
461                                    goto xvid_err_nosmp;
462    
463                            if (n != 0)
464                                    pEnc->motionData[n].complete_count_above =
465                                            pEnc->motionData[n-1].complete_count_self;
466                    }
467                    pEnc->motionData[0].complete_count_above =
468                            pEnc->motionData[t-1].complete_count_self - 1;
469    
470            } else {
471      xvid_err_nosmp:
472                    /* no SMP */
473                    create->num_threads = 0;
474                    pEnc->motionData = NULL;
475            }
476    
477      create->handle = (void *) pEnc;      create->handle = (void *) pEnc;
478    
479          init_timer();          init_timer();
480            init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
481    
482      return 0;   /* ok */      return 0;   /* ok */
483    
# Line 470  Line 537 
537                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
538          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
539                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
540          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
541                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
542    
543  /* destroy GMC image */  /* destroy GMC image */
544          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
545                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
546    
547      xvid_err_memory2a:
548            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
549    
550    xvid_err_memory2:    xvid_err_memory2:
551          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
# Line 495  Line 560 
560              xvid_free(pEnc->temp_dquants);              xvid_free(pEnc->temp_dquants);
561      }      }
562    
563            if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
564                    xvid_free(pEnc->temp_lambda);
565            }
566    
567    xvid_err_memory0:    xvid_err_memory0:
568      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
569          if (pEnc->plugins[n].func) {          if (pEnc->plugins[n].func) {
570              pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);                          pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
571          }          }
572      }      }
573      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
# Line 563  Line 632 
632                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
633          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
634                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
635          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
636                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
   
637          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
638                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
639          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
# Line 608  Line 672 
672    
673          for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<pEnc->num_plugins;i++) {
674              if (pEnc->plugins[i].func) {              if (pEnc->plugins[i].func) {
675                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0);                                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
676              }              }
677          }          }
678          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
679      }      }
680    
681      if (pEnc->num_plugins>0)          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
682    
683            if (pEnc->num_zones > 0)
684          xvid_free(pEnc->zones);          xvid_free(pEnc->zones);
685    
686            if (pEnc->num_threads > 0) {
687                    for (i = 0; i < pEnc->num_threads; i++)
688                            xvid_free(pEnc->motionData[i].complete_count_self);
689    
690                    xvid_free(pEnc->motionData);
691            }
692    
693          xvid_free(pEnc);          xvid_free(pEnc);
694    
695          return 0;  /* ok */          return 0;  /* ok */
# Line 630  Line 703 
703  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
704                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)
705  {  {
706      unsigned int i, j;          unsigned int i, j, k;
707      xvid_plg_data_t data;      xvid_plg_data_t data;
708    
709      /* set data struct */      /* set data struct */
# Line 648  Line 721 
721      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
722      data.fincr = frame->fincr;      data.fincr = frame->fincr;
723      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
724            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
725            data.bquant_offset = pEnc->mbParam.bquant_offset;
726    
727      for (i=0; i<3; i++) {      for (i=0; i<3; i++) {
728          data.min_quant[i] = pEnc->mbParam.min_quant[i];          data.min_quant[i] = pEnc->mbParam.min_quant[i];
729          data.max_quant[i] = pEnc->mbParam.max_quant[i];          data.max_quant[i] = pEnc->mbParam.max_quant[i];
730      }      }
731    
732      data.reference.csp = XVID_CSP_USER;          data.reference.csp = XVID_CSP_PLANAR;
733      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
734      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
735      data.reference.plane[2] = pEnc->reference->image.v;      data.reference.plane[2] = pEnc->reference->image.v;
# Line 662  Line 737 
737      data.reference.stride[1] = pEnc->mbParam.edged_width/2;      data.reference.stride[1] = pEnc->mbParam.edged_width/2;
738      data.reference.stride[2] = pEnc->mbParam.edged_width/2;      data.reference.stride[2] = pEnc->mbParam.edged_width/2;
739    
740      data.current.csp = XVID_CSP_USER;          data.current.csp = XVID_CSP_PLANAR;
741      data.current.plane[0] = frame->image.y;      data.current.plane[0] = frame->image.y;
742      data.current.plane[1] = frame->image.u;      data.current.plane[1] = frame->image.u;
743      data.current.plane[2] = frame->image.v;      data.current.plane[2] = frame->image.v;
# Line 676  Line 751 
751          data.type = *type;          data.type = *type;
752          data.quant = *quant;          data.quant = *quant;
753    
754                    data.vol_flags = frame->vol_flags;
755                    data.vop_flags = frame->vop_flags;
756                    data.motion_flags = frame->motion_flags;
757    
758            } else if (opt == XVID_PLG_FRAME) {
759                    data.type = coding2type(frame->coding_type);
760                    data.quant = frame->quant;
761    
762                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
763              data.dquant = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
764              data.dquant_stride = pEnc->mbParam.mb_width;              data.dquant_stride = pEnc->mbParam.mb_width;
765                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
766          }          }
767    
768          data.vol_flags = frame->vol_flags;                  if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
769          data.vop_flags = frame->vop_flags;                          int block = 0;
770          data.motion_flags = frame->motion_flags;                          data.lambda = pEnc->temp_lambda;
771                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
772                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
773                                            for (k = 0; k < 6; k++)
774                                                    data.lambda[block++] = 1.0f;
775                    }
776    
777      } else { /* XVID_PLG_AFTER */      } else { /* XVID_PLG_AFTER */
778          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
779              data.original.csp = XVID_CSP_USER;                          data.original.csp = XVID_CSP_PLANAR;
780              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
781              data.original.plane[1] = original->u;              data.original.plane[1] = original->u;
782              data.original.plane[2] = original->v;              data.original.plane[2] = original->v;
# Line 738  Line 826 
826          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
827          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
828    
829          if (stats) {                  /* New code */
830                  stats->type = coding2type(frame->coding_type);                  data.stats.type      = coding2type(frame->coding_type);
831                  stats->quant = frame->quant;                  data.stats.quant     = frame->quant;
832                  stats->vol_flags = frame->vol_flags;                  data.stats.vol_flags = frame->vol_flags;
833                  stats->vop_flags = frame->vop_flags;                  data.stats.vop_flags = frame->vop_flags;
834                  stats->length = frame->length;                  data.stats.length    = frame->length;
835                  stats->hlength = frame->length - (frame->sStat.iTextBits / 8);                  data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
836                  stats->kblks = frame->sStat.kblks;                  data.stats.kblks     = frame->sStat.kblks;
837                  stats->mblks = frame->sStat.mblks;                  data.stats.mblks     = frame->sStat.mblks;
838                  stats->ublks = frame->sStat.ublks;                  data.stats.ublks     = frame->sStat.ublks;
839              stats->sse_y = data.sse_y;                  data.stats.sse_y     = data.sse_y;
840              stats->sse_u = data.sse_u;                  data.stats.sse_u     = data.sse_u;
841              stats->sse_v = data.sse_v;                  data.stats.sse_v     = data.sse_v;
842          }  
843                    if (stats)
844                            *stats = data.stats;
845      }      }
846    
847      /* call plugins */      /* call plugins */
848      for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {      for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
849          emms();          emms();
850          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
851              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {                          if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
852                  continue;                  continue;
853              }              }
854          }          }
# Line 770  Line 860 
860          *type = data.type;          *type = data.type;
861          *quant = data.quant > 0 ? data.quant : 2;   /* default */          *quant = data.quant > 0 ? data.quant : 2;   /* default */
862    
863                    frame->vol_flags = data.vol_flags;
864                    frame->vop_flags = data.vop_flags;
865                    frame->motion_flags = data.motion_flags;
866    
867            } else if (opt == XVID_PLG_FRAME) {
868    
869          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
870              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
871              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
# Line 782  Line 878 
878              }              }
879          }          }
880    
881          frame->vol_flags = data.vol_flags;                  if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
882          frame->vop_flags = data.vop_flags;                          for (j = 0; j < pEnc->mbParam.mb_height; j++)
883          frame->motion_flags = data.motion_flags;                                  for (i = 0; i < pEnc->mbParam.mb_width; i++)
884                                            for (k = 0; k < 6; k++) {
885                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
886                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
887                            }
888                    } else {
889                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
890                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
891                                            for (k = 0; k < 6; k++) {
892                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
893      }      }
894  }  }
895    
896    
897                    frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
898            }
899    
900    
901    }
902    
903    
904  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
# Line 806  Line 916 
916      pEnc->m_framenum--; /* debug ticker */      pEnc->m_framenum--; /* debug ticker */
917  }  }
918    
919    static __inline void
920    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
921    {
922            if (pMB->cbp == 0) {
923                    /* we want to code dquant but the quantizer value will not be used yet
924                            let's find out if we can postpone dquant to next MB
925                    */
926                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
927                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
928                            return;
929                    } else {
930                            MACROBLOCK * next = pMB + 1;
931                            const MACROBLOCK * prev = pMB - 1;
932                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
933                                    /* mode allows dquant change in the future */
934                                    if (abs(next->quant - prev->quant) <= 2) {
935                                            /* quant change is not out of range */
936                                            pMB->quant = prev->quant;
937                                            pMB->dquant = 0;
938                                            next->dquant = next->quant - prev->quant;
939                                            return;
940                                    }
941                    }
942            }
943            /* couldn't skip this dquant */
944            pMB->mode = MODE_INTER_Q;
945    }
946    
947    
948    
949  static __inline void  static __inline void
# Line 815  Line 953 
953      pCur->ticks = (int32_t)pCur->stamp % time_base;      pCur->ticks = (int32_t)pCur->stamp % time_base;
954                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
955    
956                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */  #if 0   /* HEAVY DEBUG OUTPUT */
957            fprintf(stderr,"WriteVop:   %d - %d \n",
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
958                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
959                  fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",                  fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
960                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
961                  fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",                  fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
962                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
963    #endif
964    }
965    
966  */  static void
967    simplify_par(int *par_width, int *par_height)
968    {
969    
970            int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
971            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
972            int divisor = gcd(_par_width, _par_height);
973    
974            _par_width  /= divisor;
975            _par_height /= divisor;
976    
977            /* 2^8 precision maximum */
978            if (_par_width>255 || _par_height>255) {
979                    float div;
980                    emms();
981                    if (_par_width>_par_height)
982                            div = (float)_par_width/255;
983                    else
984                            div = (float)_par_height/255;
985    
986                    _par_width  = (int)((float)_par_width/div);
987                    _par_height = (int)((float)_par_height/div);
988  }  }
989    
990            *par_width = _par_width;
991            *par_height = _par_height;
992    
993            return;
994    }
995    
996    
997  /*****************************************************************************  /*****************************************************************************
# Line 923  Line 1088 
1088                          }                          }
1089    
1090                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1091              call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1092                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1093    
1094                          goto done;                          goto done;
# Line 949  Line 1114 
1114                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
1115                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
1116    
1117                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1118                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1119                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
1120    
1121                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1122                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1123              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1124    
1125              /* flush complete: reset counters */              /* flush complete: reset counters */
1126                  pEnc->flush_bframes = 0;                  pEnc->flush_bframes = 0;
# Line 983  Line 1148 
1148                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1149    
1150              if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {              if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1151                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1152              }              }
1153    
1154              /* if the very last frame is to be b-vop, we must change it to a p-vop */              /* if the very last frame is to be b-vop, we must change it to a p-vop */
# Line 994  Line 1159 
1159                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1160    
1161                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
1162                  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;
1163                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1164                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1165    
1166                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1167                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 1004  Line 1171 
1171                  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",
1172                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1173                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1174                                    pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1175    
1176                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs);
1177    
1178    
1179                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1180                      call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1181                  }else{                  }else{
1182                      pEnc->flush_bframes = 1;                      pEnc->flush_bframes = 1;
1183                      goto done;                      goto done;
# Line 1039  Line 1207 
1207    
1208      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1209      inc_frame_num(pEnc);      inc_frame_num(pEnc);
1210      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;          pEnc->current->vol_flags = frame->vol_flags;
1211      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1212          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1213          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
# Line 1058  Line 1226 
1226          type = frame->type;          type = frame->type;
1227          pEnc->current->quant = frame->quant;          pEnc->current->quant = frame->quant;
1228    
1229      call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1230    
1231      if (type > 0){      /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
1232                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
# Line 1069  Line 1237 
1237                  }else{                  }else{
1238                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1239                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1240                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1241                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1242                  }                  }
1243          }          }
1244    
1245            if (type != I_VOP)
1246                    pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1247    
1248      /* bframes buffer overflow check */      /* bframes buffer overflow check */
1249      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1250          type = P_VOP;          type = P_VOP;
# Line 1129  Line 1301 
1301      if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)      if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1302      {      {
1303          if (pEnc->current->stamp > 0) {          if (pEnc->current->stamp > 0) {
1304              call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1305          }          }
1306                  else                  else
1307                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
# Line 1157  Line 1329 
1329                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1330    
1331                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1332                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP");
1333                  }                  }
1334    
1335                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1336                  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;
1337                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1338                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1339          type = P_VOP;          type = P_VOP;
1340      }      }
1341    
# Line 1183  Line 1357 
1357                  pEnc->iFrameNum = 1;                  pEnc->iFrameNum = 1;
1358    
1359                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1360                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1361    
1362                    /* Aspect ratio */
1363                  switch(frame->par) {                  switch(frame->par) {
1364                  case XVID_PAR_11_VGA:                  case XVID_PAR_11_VGA:
1365                  case XVID_PAR_43_PAL:                  case XVID_PAR_43_PAL:
# Line 1194  Line 1370 
1370                          pEnc->mbParam.par = frame->par;                          pEnc->mbParam.par = frame->par;
1371                          break;                          break;
1372                  default:                  default:
1373                          pEnc->mbParam.par = XVID_PAR_EXT;                          pEnc->mbParam.par = XVID_PAR_11_VGA;
1374                          break;                          break;
1375                  }                  }
1376                  pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1;  
1377                  pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1;                  /* For extended PAR only, we try to sanityse/simplify par values */
1378                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1379                            pEnc->mbParam.par_width  = frame->par_width;
1380                            pEnc->mbParam.par_height = frame->par_height;
1381                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1382                    }
1383    
1384          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1385                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1386                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1387                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
1388                                  set_inter_matrix(frame->quant_inter_matrix);                                  set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1389                  }                  }
1390    
1391          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1392    
         if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
             pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;  
   
1393          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1394              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1395    
# Line 1244  Line 1422 
1422                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1423          }          }
1424    
1425                  FrameCodeP(pEnc, &bs, 1, 0);                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1426                            /* N-VOP, we mustn't code b-frames yet */
1427                            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1428                                     pEnc->mbParam.max_bframes == 0)
1429                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1430                            goto done;
1431                    }
1432      }      }
1433    
1434    
# Line 1263  Line 1447 
1447    
1448      /* packed or no-bframes or no-bframes-queued: output stats */      /* packed or no-bframes or no-bframes-queued: output stats */
1449      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1450          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1451          }          }
1452    
1453          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1282  Line 1466 
1466    
1467  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1468  {  {
1469      unsigned int i,j;          unsigned int i;
1470      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1471            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1472      if (quant > 31)      if (quant > 31)
1473                  frame->quant = quant = 31;                  frame->quant = quant = 31;
1474          else if (quant < 1)          else if (quant < 1)
1475                  frame->quant = quant = 1;                  frame->quant = quant = 1;
1476    
1477      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];  
1478          quant += pMB->dquant;          quant += pMB->dquant;
1479          if (quant > 31)          if (quant > 31)
1480                          quant = 31;                          quant = 31;
1481                  else if (quant < 1)                  else if (quant < 1)
1482                          quant = 1;                          quant = 1;
1483          pMB->quant = quant;          pMB->quant = quant;
1484                    pMB++;
1485      }      }
1486  }  }
1487    
# Line 1335  Line 1519 
1519    
1520          uint16_t x, y;          uint16_t x, y;
1521    
         if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
   
                 /* 16x16->8x8 downsample requires 1 additional edge pixel*/  
                 /* XXX: setedges is overkill */  
                 start_timer();  
                 image_setedges(&pEnc->current->image,  
                         pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,  
                         pEnc->mbParam.width, pEnc->mbParam.height);  
                 stop_edges_timer();  
         }  
   
1522          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1523          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1524          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1525    
1526            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1527    
1528      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1529    
1530          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1531    
1532          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1533    
1534          BitstreamPad(bs);          BitstreamPad(bs);
1535    
1536          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1537    
1538          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1539            pEnc->current->sStat.iMVBits = 0;
1540          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1541          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1542    
# Line 1382  Line 1555 
1555                          stop_prediction_timer();                          stop_prediction_timer();
1556    
1557                          start_timer();                          start_timer();
                         if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)  
                         {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                 qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
1558                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1559                          stop_coding_timer();                          stop_coding_timer();
1560                  }                  }
1561    
         if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,  
                         16, 0);  
         }  
1562          emms();          emms();
1563    
1564          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
# Line 1406  Line 1568 
1568          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1569          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1570    
1571            pEnc->current->is_edged = 0; /* not edged */
1572            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1573    
1574          return 1;                                       /* intra */          return 1;                                       /* intra */
1575  }  }
1576    
1577    static __inline void
1578    updateFcode(Statistics * sStat, Encoder * pEnc)
1579    {
1580            float fSigma;
1581            int iSearchRange;
1582    
1583            if (sStat->iMvCount == 0)
1584                    sStat->iMvCount = 1;
1585    
1586  #define INTRA_THRESHOLD 0.5          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
 #define BFRAME_SKIP_THRESHHOLD 30  
1587    
1588            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1589    
1590            if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1591                    pEnc->mbParam.m_fcode++;
1592    
1593            else if ((5.0 * fSigma < iSearchRange)
1594                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1595                               && (pEnc->mbParam.m_fcode >= 2) )
1596                    pEnc->mbParam.m_fcode--;
1597    
1598            pEnc->fMvPrevSigma = fSigma;
1599    }
1600    
1601    #define BFRAME_SKIP_THRESHHOLD 30
1602    
1603  /* FrameCodeP also handles S(GMC)-VOPs */  /* FrameCodeP also handles S(GMC)-VOPs */
1604  static int  static int
1605  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1606                     Bitstream * bs,                     Bitstream * bs)
                    bool force_inter,  
                    bool vol_header)  
1607  {  {
         float fSigma;  
1608      int bits = BitstreamPos(bs);      int bits = BitstreamPos(bs);
1609    
1610          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1611          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1612    
         int iLimit;  
1613          int x, y, k;          int x, y, k;
         int iSearchRange;  
         int bIntra=0, skip_possible;  
1614          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1615          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1616          MBParam * const pParam = &pEnc->mbParam;          MBParam * const pParam = &pEnc->mbParam;
1617          int mb_width = pParam->mb_width;          int mb_width = pParam->mb_width;
1618          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1619            int coded = 1;
1620    
   
         /* IMAGE *pCurrent = &current->image; */  
1621          IMAGE *pRef = &reference->image;          IMAGE *pRef = &reference->image;
1622    
1623          if ((current->vop_flags & XVID_VOP_REDUCED))          if (!reference->is_edged) {
         {  
                 mb_width = (pParam->width + 31) / 32;  
                 mb_height = (pParam->height + 31) / 32;  
         }  
   
   
1624          start_timer();          start_timer();
1625          image_setedges(pRef, pParam->edged_width, pParam->edged_height,          image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1626                                     pParam->width, pParam->height);                                             pParam->width, pParam->height, 0);
1627          stop_edges_timer();          stop_edges_timer();
1628                    reference->is_edged = 1;
1629            }
1630    
1631          pParam->m_rounding_type = 1 - pParam->m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1632          current->rounding_type = pParam->m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1633          current->fcode = pParam->m_fcode;          current->fcode = pParam->m_fcode;
1634    
         if (!force_inter)  
                 iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);  
         else  
                 iLimit = mb_width * mb_height + 1;  
   
1635          if ((current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1636                    if (reference->is_interpolated != current->rounding_type) {
1637                  start_timer();                  start_timer();
1638                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1639                                                    &pEnc->vInterHV, pParam->edged_width,                                                            pEnc->vInterHV.y, pParam->edged_width,
1640                                                    pParam->edged_height,                                                    pParam->edged_height,
1641                                                    (pParam->vol_flags & XVID_VOL_QUARTERPEL),                                                    (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1642                                                    current->rounding_type);                                                    current->rounding_type);
1643                  stop_inter_timer();                  stop_inter_timer();
1644                            reference->is_interpolated = current->rounding_type;
1645                    }
1646          }          }
1647    
1648            current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1649                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1650                    current->sStat.iMVBits = 0;
1651    
1652          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1653    
1654            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1655    
1656      SetMacroblockQuants(&pEnc->mbParam, current);      SetMacroblockQuants(&pEnc->mbParam, current);
1657    
# Line 1492  Line 1670 
1670                                                                  &pEnc->vInterH,                                                                  &pEnc->vInterH,
1671                                                                  &pEnc->vInterV,                                                                  &pEnc->vInterV,
1672                                                                  &pEnc->vInterHV);                                                                  &pEnc->vInterHV);
1673                          gmcval += /*current->quant */ 2 * (int)(pParam->mb_width*pParam->mb_height);                  } else {
                 }  
   
1674                  gmcval = globalSAD(&current->warp, pParam, current->mbs,                  gmcval = globalSAD(&current->warp, pParam, current->mbs,
1675                                                          current,                                                          current,
1676                                                          &reference->image,                                                          &reference->image,
1677                                                          &current->image,                                                          &current->image,
1678                                                          pEnc->vGMC.y);                                                          pEnc->vGMC.y);
1679                    }
1680    
1681                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1682    
1683  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
# Line 1528  Line 1706 
1706                  }                  }
1707          }          }
1708    
1709          bIntra =  
1710            if (pEnc->num_threads > 0) {
1711                    /* multithreaded motion estimation - dispatch threads */
1712                    int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
1713    
1714                    for (k = 0; k < pEnc->num_threads; k++) {
1715                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
1716                            pEnc->motionData[k].pParam = &pEnc->mbParam;
1717                            pEnc->motionData[k].current = current;
1718                            pEnc->motionData[k].reference = reference;
1719                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
1720                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
1721                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
1722                            pEnc->motionData[k].pGMC = &pEnc->vGMC;
1723                            pEnc->motionData[k].y_step = pEnc->num_threads;
1724                            pEnc->motionData[k].start_y = k;
1725                            /* todo: sort out temp space once and for all */
1726                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width;
1727                    }
1728    
1729                    for (k = 0; k < pEnc->num_threads; k++) {
1730                            pthread_create(&pEnc->motionData[k].handle, NULL,
1731                                    (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]);
1732                    }
1733            } else {
1734                    /* regular ME */
1735    
1736                  MotionEstimation(&pEnc->mbParam, current, reference,                  MotionEstimation(&pEnc->mbParam, current, reference,
1737                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1738                                           &pEnc->vGMC, iLimit);                                                   &pEnc->vGMC, 256*4096);
1739            }
1740    
1741          stop_motion_timer();          stop_motion_timer();
1742    
         if (bIntra == 1) return FrameCodeI(pEnc, bs);  
   
1743          set_timecodes(current,reference,pParam->fbase);          set_timecodes(current,reference,pParam->fbase);
         if (vol_header)  
         {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);  
                 BitstreamPad(bs);  
         }  
1744    
1745          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1746    
1747          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =          for (y = 0; y < mb_height; y++) {
                 current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;  
1748    
1749                    if (pEnc->num_threads > 0) {
1750                            /* in multithreaded encoding, only proceed with a row of macroblocks
1751                                    if ME finished with that row */
1752                            while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != mb_width)
1753                                    sched_yield();
1754                    }
1755    
         for (y = 0; y < mb_height; y++) {  
1756                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1757                          MACROBLOCK *pMB =                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1758                                  &current->mbs[x + y * pParam->mb_width];                          int skip_possible;
1759    
1760                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
   
                         if (bIntra) {  
1761                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1762                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1763                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
# Line 1568  Line 1768 
1768    
1769                                  current->sStat.kblks++;                                  current->sStat.kblks++;
1770    
                                 if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)  
                                 {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                         qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                         qcoeff[5*64+0]=0;  
                                 }  
1771                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1772                                  stop_coding_timer();                                  stop_coding_timer();
1773                                  continue;                                  continue;
# Line 1587  Line 1782 
1782                                                                   pParam->height,                                                                   pParam->height,
1783                                                                   pParam->edged_width,                                                                   pParam->edged_width,
1784                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
                                                                  (current->vop_flags & XVID_VOP_REDUCED),  
1785                                                                   current->rounding_type);                                                                   current->rounding_type);
1786    
1787                          stop_comp_timer();                          stop_comp_timer();
1788    
                         if (pMB->dquant != 0) {  
                 pMB->mode = MODE_INTER_Q;  
                         }  
   
1789                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1790    
1791                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1792                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,  
1793                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1794                          }                          }
1795    
1796                            if (pMB->dquant != 0)
1797                                    MBSetDquant(pMB, x, y, &pEnc->mbParam);
1798    
1799    
1800                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1801                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1802                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
# Line 1616  Line 1809 
1809    
1810                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1811    
1812                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                                                         (pMB->dquant == 0);  
1813    
1814                          if (current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1815                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1816                          else if (current->coding_type == P_VOP) {                          else { /* PVOP */
1817                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1818                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                                                                  pMB->qmvs : pMB->mvs;
1819                                  else                                  skip_possible &= ((mv->x|mv->y) == 0);
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
1820                          }                          }
1821    
1822                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
   
1823  /* 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 */
   
                                 if (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */  
                                 {  
1824                                          int bSkip = 1;                                          int bSkip = 1;
1825    
1826                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1827                                          {  
1828                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1829                                                  int iSAD;                                                  int iSAD;
1830                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1831                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1832                                                                  pParam->edged_width,BFRAME_SKIP_THRESHHOLD);                                                                                  pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1833                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1834                                                  {       bSkip = 0;                                                          bSkip = 0; /* could not SKIP */
1835                                                          break;                                                          if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
                                                 }  
                                         }  
   
                                         if (!bSkip) {   /* no SKIP, but trivial block */  
                                                 if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
1836                                                          VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1837                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1838                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1839                                                  }                                                          } else {
                                                 else {  
1840                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1841                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1842                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1843                                                  }                                                  }
1844                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1845                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1846                                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                                                          break;
1847                                                  stop_coding_timer();                                                  }
   
                                                 continue;       /* next MB */  
1848                                          }                                          }
1849                                  }                                  }
                                 /* do SKIP */  
1850    
1851                                    if (bSkip) {
1852                                            /* do SKIP */
1853                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1854                                  MBSkip(bs);                                  MBSkip(bs);
1855                                  stop_coding_timer();                                  stop_coding_timer();
1856                                  continue;       /* next MB */                                  continue;       /* next MB */
1857                          }                          }
                         /* ordinary case: normal coded INTER/INTER4V block */  
   
                         if ((current->vop_flags & XVID_VOP_GREYSCALE))  
                         {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                 qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
   
                         if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.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);  
                         } else {  
                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->mvs[0].y - predMV.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);  
                         }  
   
   
                         if (pMB->mode == MODE_INTER4V)  
                         {       int k;  
                                 for (k=1;k<4;k++)  
                                 {  
                                         if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.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);  
                                         } else {  
                                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->mvs[k].y - predMV.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);  
                                         }  
   
                                 }  
1858                          }                          }
1859    
1860                            /* ordinary case: normal coded INTER/INTER4V block */
1861                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1862                          stop_coding_timer();                          stop_coding_timer();
   
1863                  }                  }
1864          }          }
1865    
1866          if ((current->vop_flags & XVID_VOP_REDUCED))          if (pEnc->num_threads > 0) {
1867          {                  void * status;
1868                  image_deblock_rrv(&current->image, pParam->edged_width,                  for (k = 0; k < pEnc->num_threads; k++) {
1869                          current->mbs, mb_width, mb_height, pParam->mb_width,                          pthread_join(pEnc->motionData[k].handle, &status);
                         16, 0);  
1870          }          }
1871    
1872          emms();                  for (k = 0; k < pEnc->num_threads; k++) {
1873                            current->sStat.iMvSum += pEnc->motionData[k].mvSum;
1874          if (current->sStat.iMvCount == 0)                          current->sStat.iMvCount += pEnc->motionData[k].mvCount;
1875                  current->sStat.iMvCount = 1;                  }
   
         fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);  
   
         iSearchRange = 1 << (3 + pParam->m_fcode);  
   
         if ((fSigma > iSearchRange / 3)  
         && (pParam->m_fcode <= (3 +  (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0)  ))) /* maximum search range 128 */  
         {  
                 pParam->m_fcode++;  
                 iSearchRange *= 2;  
         } else if ((fSigma < iSearchRange / 6)  
                            && (pEnc->fMvPrevSigma >= 0)  
                            && (pEnc->fMvPrevSigma < iSearchRange / 6)  
                            && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) )))        /* minimum search range 16 */  
         {  
                 pParam->m_fcode--;  
                 iSearchRange /= 2;  
1876          }          }
1877    
1878          pEnc->fMvPrevSigma = fSigma;          emms();
1879            updateFcode(&current->sStat, pEnc);
1880    
1881          /* frame drop code */          /* frame drop code */
1882  #if 0  #if 0
1883          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1884  #endif  #endif
1885          if (current->sStat.kblks + current->sStat.mblks <          if (current->sStat.kblks + current->sStat.mblks <=
1886                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100)                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
1887                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
1888          {          {
1889                  current->sStat.kblks = current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
1890                  current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1891    
1892                  BitstreamReset(bs);                  BitstreamReset(bs);
1893    
1894                  set_timecodes(current,reference,pParam->fbase);                  set_timecodes(current,reference,pParam->fbase);
1895                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1896    
1897                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1898                  current->quant = reference->quant;                  current->quant = reference->quant;
# Line 1773  Line 1900 
1900                  current->rounding_type = reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1901                  current->fcode = reference->fcode;                  current->fcode = reference->fcode;
1902                  current->bcode = reference->bcode;                  current->bcode = reference->bcode;
1903                    current->stamp = reference->stamp;
1904                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1905                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1906                    coded = 0;
1907    
1908            } else {
1909    
1910                    pEnc->current->is_edged = 0; /* not edged */
1911                    pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1912    
1913                    /* what was this frame's interpolated reference will become
1914                            forward (past) reference in b-frame coding */
1915    
1916                    image_swap(&pEnc->vInterH, &pEnc->f_refh);
1917                    image_swap(&pEnc->vInterV, &pEnc->f_refv);
1918                    image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1919          }          }
1920    
1921          /* XXX: debug          /* XXX: debug
# Line 1796  Line 1937 
1937    
1938      current->length = (BitstreamPos(bs) - bits) / 8;      current->length = (BitstreamPos(bs) - bits) / 8;
1939    
1940          return 0;                                       /* inter */          return coded;
1941  }  }
1942    
1943    
# Line 1820  Line 1961 
1961                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1962          }          }
1963    
         /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */  
   
1964          if (!first){          if (!first){
1965                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1966          }          }
1967  #endif  #endif
1968    
1969          /* forward  */          /* forward  */
1970            if (!pEnc->reference->is_edged) {
1971          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1972                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1973                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1974                    pEnc->current->is_edged = 1;
1975            }
1976    
1977            if (pEnc->reference->is_interpolated != 0) {
1978          start_timer();          start_timer();
1979          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y,
1980                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1981                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1982          stop_inter_timer();          stop_inter_timer();
1983                    pEnc->reference->is_interpolated = 0;
1984            }
1985    
1986          /* backward */          /* backward */
1987            if (!pEnc->current->is_edged) {
1988          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1989                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1990                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1991                    pEnc->current->is_edged = 1;
1992            }
1993    
1994            if (pEnc->current->is_interpolated != 0) {
1995          start_timer();          start_timer();
1996          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
1997                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1998                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1999          stop_inter_timer();          stop_inter_timer();
2000                    pEnc->current->is_interpolated = 0;
2001            }
2002    
2003            frame->coding_type = B_VOP;
2004            call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2005    
2006            frame->fcode = frame->bcode = pEnc->current->fcode;
2007    
2008            if (pEnc->num_threads > 0) {
2009                    int k;
2010                    /* multithreaded motion estimation - dispatch threads */
2011                    int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
2012    
2013                    for (k = 0; k < pEnc->num_threads; k++) {
2014                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
2015                            pEnc->motionData[k].pParam = &pEnc->mbParam;
2016                            pEnc->motionData[k].current = frame;
2017                            pEnc->motionData[k].reference = pEnc->current;
2018                            pEnc->motionData[k].fRef = f_ref;
2019                            pEnc->motionData[k].fRefH = &pEnc->f_refh;
2020                            pEnc->motionData[k].fRefV = &pEnc->f_refv;
2021                            pEnc->motionData[k].fRefHV = &pEnc->f_refhv;
2022                            pEnc->motionData[k].pRef = b_ref;
2023                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
2024                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
2025                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
2026                            pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);
2027                            pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);
2028                            pEnc->motionData[k].y_step = pEnc->num_threads;
2029                            pEnc->motionData[k].start_y = k;
2030                            /* todo: sort out temp space once and for all */
2031                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width;
2032                    }
2033    
2034                    for (k = 0; k < pEnc->num_threads; k++) {
2035                            pthread_create(&pEnc->motionData[k].handle, NULL,
2036                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]);
2037                    }
2038            } else {
2039          start_timer();          start_timer();
2040          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2041                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
# Line 1856  Line 2045 
2045                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2046                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
2047          stop_motion_timer();          stop_motion_timer();
2048            }
         frame->coding_type = B_VOP;  
2049    
2050          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2051          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2052    
2053          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2054            frame->sStat.iMVBits = 0;
2055          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2056          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2057          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
# Line 1870  Line 2059 
2059          frame->sStat.kblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.ublks = 0;
2060    
2061          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2062    
2063                    if (pEnc->num_threads > 0) {
2064                            /* in multithreaded encoding, only proceed with a row of macroblocks
2065                                    if ME finished with that row */
2066                            while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != pEnc->mbParam.mb_width)
2067                                    sched_yield();
2068                    }
2069    
2070                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2071                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2072    
2073                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2074                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2075                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2076                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2077                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
2078                                    }
2079                                  continue;                                  continue;
2080                          }                          }
2081    
2082                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                          mb->quant = frame->quant;
2083    
2084                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2085                                    /* we have to motion-compensate, transfer etc,
2086                                            because there might be blocks to code */
2087    
2088                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2089                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2090                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2091                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
2092                                                                           dct_codes);                                                                           dct_codes);
2093    
                                 if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;  
                                 mb->quant = frame->quant;  
   
                                 if (mb->mode != MODE_DIRECT_NONE_MV)  
2094                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
   
                                 if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)  
                                         && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {  
                                         mb->mode = MODE_DIRECT_NONE_MV; /* skipped */  
                                 }  
2095                          }                          }
2096    
2097                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the                          if (mb->mode == MODE_DIRECT_NO4V)
2098                           * coding function for BFrames, that's why we don't zero teh DC                                  mb->mode = MODE_DIRECT;
2099                           * coeffs */  
2100                          if ((frame->vop_flags & XVID_VOP_GREYSCALE))                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2101                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2102                            else
2103                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2104                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2105                                  mb->cbp &= 0x3C;                                  mb->cbp &= 0x3C;
2106    
2107                          start_timer();                          start_timer();
# Line 1911  Line 2111 
2111                  }                  }
2112          }          }
2113    
2114          emms();          if (pEnc->num_threads > 0) {
2115                    void * status;
2116                    int k;
2117                    for (k = 0; k < pEnc->num_threads; k++) {
2118                            pthread_join(pEnc->motionData[k].handle, &status);
2119                    }
2120            }
2121    
2122          /* TODO: dynamic fcode/bcode ??? */          emms();
2123    
2124      BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */      BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2125          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;

Legend:
Removed from v.1.95.2.45  
changed lines
  Added in v.1.126

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