[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.24, Sat May 17 13:24:56 2003 UTC revision 1.128, Fri Mar 3 11:54:58 2006 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002     Michael Militzer <isibaar@xvid.org>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *                         2002-2003 Peter Ross <pross@xvid.org>
8   *  to use this software module in hardware or software products are   *                         2002   Daniel Smith <danielsmith@astroboymail.com>
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 43  Line 38 
38  #include "image/font.h"  #include "image/font.h"
39  #include "motion/sad.h"  #include "motion/sad.h"
40  #include "motion/motion.h"  #include "motion/motion.h"
41    #include "motion/gmc.h"
42    
43  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
44  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
45  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
47  #include "utils/emms.h"  #include "utils/emms.h"
48  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "quant/adapt_quant.h"  
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
52    # include "motion/motion_smp.h"
53    
54    
55  /*****************************************************************************  /*****************************************************************************
56   * Local function prototypes   * Local function prototypes
57   ****************************************************************************/   ****************************************************************************/
# Line 61  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 91  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 121  Line 140 
140          Encoder *pEnc;          Encoder *pEnc;
141      int n;      int n;
142    
143          if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */          if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
144                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
145    
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 138  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 151  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 182  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 192  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 237 
237      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
238              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
239                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
240                    if (pEnc->temp_dquants==NULL)
241                            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      }      }
     /* XXX: error checking */  
251    
252          /* bframes */          /* bframes */
253          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
# Line 229  Line 264 
264          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
265    
266      /* max keyframe interval */      /* max keyframe interval */
267      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ?          pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
                 10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval;  
   
         /* Bitrate allocator defaults  
   
         if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))  
                 create->min_quantizer = 1;  
   
         if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))  
                 create->max_quantizer = 31;  
   
         if (create->max_quantizer < create->min_quantizer)  
                 create->max_quantizer = create->min_quantizer; */  
   
268    
269      /* allocate working frame-image memory */      /* allocate working frame-image memory */
270    
# Line 264  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 279  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 325  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 405  Line 425 
425                  image_null(&pEnc->queue[n].image);                  image_null(&pEnc->queue[n].image);
426    
427    
428          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
         {  
429                  if (image_create                  if (image_create
430                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
431                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
432                          goto xvid_err_memory5;                          goto xvid_err_memory5;
   
433          }          }
434    
   
435          /* timestamp stuff */          /* timestamp stuff */
436    
437          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
# Line 427  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 439  Line 487 
487    
488    xvid_err_memory5:    xvid_err_memory5:
489    
490          if (pEnc->mbParam.max_bframes > 0) {          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
491          int i;                          image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
   
                 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {  
                         image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,  
492                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
493                  }                  }
494    
495                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
496    
497    xvid_err_memory4:    xvid_err_memory4:
498    
# Line 461  Line 506 
506    
507                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
508                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
509                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
510                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
511                  }                  }
512    
513                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 495  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 515  Line 555 
555          xvid_free(pEnc->current);          xvid_free(pEnc->current);
556          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
557    
558      xvid_err_memory1a:
559      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
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 553  Line 598 
598          int i;          int i;
599    
600          /* B Frames specific */          /* B Frames specific */
         if (pEnc->mbParam.max_bframes > 0) {  
   
601                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
   
602                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
603                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
604                  }                  }
                 xvid_free(pEnc->queue);  
         }  
605    
606            xvid_free(pEnc->queue);
607    
608          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
609    
# Line 573  Line 614 
614    
615                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
616                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
617                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
618                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
619                  }                  }
620    
# Line 593  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 628  Line 662 
662          xvid_free(pEnc->temp_dquants);          xvid_free(pEnc->temp_dquants);
663      }      }
664    
665            if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
666                    xvid_free(pEnc->temp_lambda);
667            }
668    
669      if (pEnc->num_plugins>0) {      if (pEnc->num_plugins>0) {
670          xvid_plg_destroy_t pdestroy;          xvid_plg_destroy_t pdestroy;
# Line 638  Line 675 
675    
676          for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<pEnc->num_plugins;i++) {
677              if (pEnc->plugins[i].func) {              if (pEnc->plugins[i].func) {
678                  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);
679              }              }
680          }          }
681          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
682      }      }
683    
684      if (pEnc->num_plugins>0)          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
685    
686            if (pEnc->num_zones > 0)
687          xvid_free(pEnc->zones);          xvid_free(pEnc->zones);
688    
689            if (pEnc->num_threads > 0) {
690                    for (i = 0; i < pEnc->num_threads; i++)
691                            xvid_free(pEnc->motionData[i].complete_count_self);
692    
693                    xvid_free(pEnc->motionData);
694            }
695    
696          xvid_free(pEnc);          xvid_free(pEnc);
697    
698          return 0;  /* ok */          return 0;  /* ok */
# Line 660  Line 706 
706  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
707                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)
708  {  {
709      unsigned int i, j;          unsigned int i, j, k;
710      xvid_plg_data_t data;      xvid_plg_data_t data;
711    
712      /* set data struct */      /* set data struct */
# Line 678  Line 724 
724      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
725      data.fincr = frame->fincr;      data.fincr = frame->fincr;
726      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
727            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
728            data.bquant_offset = pEnc->mbParam.bquant_offset;
729    
730      for (i=0; i<3; i++) {      for (i=0; i<3; i++) {
731          data.min_quant[i] = pEnc->mbParam.min_quant[i];          data.min_quant[i] = pEnc->mbParam.min_quant[i];
732          data.max_quant[i] = pEnc->mbParam.max_quant[i];          data.max_quant[i] = pEnc->mbParam.max_quant[i];
733      }      }
734    
735      data.reference.csp = XVID_CSP_USER;          data.reference.csp = XVID_CSP_PLANAR;
736      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
737      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
738      data.reference.plane[2] = pEnc->reference->image.v;      data.reference.plane[2] = pEnc->reference->image.v;
# Line 692  Line 740 
740      data.reference.stride[1] = pEnc->mbParam.edged_width/2;      data.reference.stride[1] = pEnc->mbParam.edged_width/2;
741      data.reference.stride[2] = pEnc->mbParam.edged_width/2;      data.reference.stride[2] = pEnc->mbParam.edged_width/2;
742    
743      data.current.csp = XVID_CSP_USER;          data.current.csp = XVID_CSP_PLANAR;
744      data.current.plane[0] = frame->image.y;      data.current.plane[0] = frame->image.y;
745      data.current.plane[1] = frame->image.u;      data.current.plane[1] = frame->image.u;
746      data.current.plane[2] = frame->image.v;      data.current.plane[2] = frame->image.v;
# Line 703  Line 751 
751      data.frame_num = frame->frame_num;      data.frame_num = frame->frame_num;
752    
753      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
754          data.type = XVID_TYPE_AUTO;                  data.type = *type;
755          data.quant = 0;                  data.quant = *quant;
756    
757                    data.vol_flags = frame->vol_flags;
758                    data.vop_flags = frame->vop_flags;
759                    data.motion_flags = frame->motion_flags;
760    
761            } else if (opt == XVID_PLG_FRAME) {
762                    data.type = coding2type(frame->coding_type);
763                    data.quant = frame->quant;
764    
765                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
766              data.dquant = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
767              data.dquant_stride = pEnc->mbParam.mb_width;              data.dquant_stride = pEnc->mbParam.mb_width;
768                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
769          }          }
770    
771          /* todo: [vol,vop,motion]_flags*/                  if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
772                            int block = 0;
773                            data.lambda = pEnc->temp_lambda;
774                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
775                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
776                                            for (k = 0; k < 6; k++)
777                                                    data.lambda[block++] = 1.0f;
778                    }
779    
780      } else { // XVID_PLG_AFTER          } else { /* XVID_PLG_AFTER */
781          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
782              data.original.csp = XVID_CSP_USER;                          data.original.csp = XVID_CSP_PLANAR;
783              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
784              data.original.plane[1] = original->u;              data.original.plane[1] = original->u;
785              data.original.plane[2] = original->v;              data.original.plane[2] = original->v;
# Line 753  Line 816 
816    
817              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
818              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
819                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;                                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
820              }              }
821          }          }
822    
# Line 766  Line 829 
829          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
830          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
831    
832          if (stats) {                  /* New code */
833                  stats->type = coding2type(frame->coding_type);                  data.stats.type      = coding2type(frame->coding_type);
834                  stats->quant = frame->quant;                  data.stats.quant     = frame->quant;
835                  stats->vol_flags = frame->vol_flags;                  data.stats.vol_flags = frame->vol_flags;
836                  stats->vop_flags = frame->vop_flags;                  data.stats.vop_flags = frame->vop_flags;
837                  stats->length = frame->length;                  data.stats.length    = frame->length;
838                  stats->hlength = frame->length - (frame->sStat.iTextBits / 8);                  data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
839                  stats->kblks = frame->sStat.kblks;                  data.stats.kblks     = frame->sStat.kblks;
840                  stats->mblks = frame->sStat.mblks;                  data.stats.mblks     = frame->sStat.mblks;
841                  stats->ublks = frame->sStat.ublks;                  data.stats.ublks     = frame->sStat.ublks;
842              stats->sse_y = data.sse_y;                  data.stats.sse_y     = data.sse_y;
843              stats->sse_u = data.sse_u;                  data.stats.sse_u     = data.sse_u;
844              stats->sse_v = data.sse_v;                  data.stats.sse_v     = data.sse_v;
845          }  
846                    if (stats)
847                            *stats = data.stats;
848      }      }
849    
850      /* call plugins */      /* call plugins */
851      for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
852          emms();          emms();
853          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
854              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) {
855                  continue;                  continue;
856              }              }
857          }          }
# Line 798  Line 863 
863          *type = data.type;          *type = data.type;
864          *quant = data.quant > 0 ? data.quant : 2;   /* default */          *quant = data.quant > 0 ? data.quant : 2;   /* default */
865    
866                    frame->vol_flags = data.vol_flags;
867                    frame->vop_flags = data.vop_flags;
868                    frame->motion_flags = data.motion_flags;
869    
870            } else if (opt == XVID_PLG_FRAME) {
871    
872          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
873              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
874              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
# Line 809  Line 880 
880                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
881              }              }
882          }          }
883          /* todo: [vol,vop,motion]_flags*/  
884                    if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
885                            for (j = 0; j < pEnc->mbParam.mb_height; j++)
886                                    for (i = 0; i < pEnc->mbParam.mb_width; i++)
887                                            for (k = 0; k < 6; k++) {
888                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
889                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
890                            }
891                    } else {
892                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
893                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
894                                            for (k = 0; k < 6; k++) {
895                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
896      }      }
897  }  }
898    
899    
900                    frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
901            }
902    
903    
904    }
905    
906    
907  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
# Line 831  Line 919 
919      pEnc->m_framenum--; /* debug ticker */      pEnc->m_framenum--; /* debug ticker */
920  }  }
921    
922    static __inline void
923    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
924    {
925            if (pMB->cbp == 0) {
926                    /* we want to code dquant but the quantizer value will not be used yet
927                            let's find out if we can postpone dquant to next MB
928                    */
929                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
930                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
931                            return;
932                    } else {
933                            MACROBLOCK * next = pMB + 1;
934                            const MACROBLOCK * prev = pMB - 1;
935                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
936                                    /* mode allows dquant change in the future */
937                                    if (abs(next->quant - prev->quant) <= 2) {
938                                            /* quant change is not out of range */
939                                            pMB->quant = prev->quant;
940                                            pMB->dquant = 0;
941                                            next->dquant = next->quant - prev->quant;
942                                            return;
943                                    }
944                    }
945            }
946            /* couldn't skip this dquant */
947            pMB->mode = MODE_INTER_Q;
948    }
949    
950    
951    
952  static __inline void  static __inline void
# Line 840  Line 956 
956      pCur->ticks = (int32_t)pCur->stamp % time_base;      pCur->ticks = (int32_t)pCur->stamp % time_base;
957                  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) ;
958    
959                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */  #if 0   /* HEAVY DEBUG OUTPUT */
960            fprintf(stderr,"WriteVop:   %d - %d \n",
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
961                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
962                  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",
963                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
964                  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",
965                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
966    #endif
967    }
968    
969  */  static void
970    simplify_par(int *par_width, int *par_height)
971    {
972    
973            int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
974            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
975            int divisor = gcd(_par_width, _par_height);
976    
977            _par_width  /= divisor;
978            _par_height /= divisor;
979    
980            /* 2^8 precision maximum */
981            if (_par_width>255 || _par_height>255) {
982                    float div;
983                    emms();
984                    if (_par_width>_par_height)
985                            div = (float)_par_width/255;
986                    else
987                            div = (float)_par_height/255;
988    
989                    _par_width  = (int)((float)_par_width/div);
990                    _par_height = (int)((float)_par_height/div);
991  }  }
992    
993            *par_width = _par_width;
994            *par_height = _par_height;
995    
996            return;
997    }
998    
999    
1000  /*****************************************************************************  /*****************************************************************************
# Line 876  Line 1019 
1019          int type;          int type;
1020          Bitstream bs;          Bitstream bs;
1021    
1022          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))     /* v1.x.x */
1023                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
1024    
1025          xFrame->out_flags = 0;          xFrame->out_flags = 0;
# Line 913  Line 1056 
1056    
1057                  if (xFrame->quant_intra_matrix)                  if (xFrame->quant_intra_matrix)
1058                  {                  {
1059                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
1060                          q->frame.quant_intra_matrix = q->quant_intra_matrix;                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
1061                  }                  }
1062    
1063                  if (xFrame->quant_inter_matrix)                  if (xFrame->quant_inter_matrix)
1064                  {                  {
1065                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
1066                          q->frame.quant_inter_matrix = q->quant_inter_matrix;                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
1067                  }                  }
1068    
# Line 938  Line 1081 
1081          {          {
1082                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
1083    
1084                          DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1085                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
1086                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1087    
# Line 948  Line 1091 
1091                          }                          }
1092    
1093                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1094              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);
1095                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1096    
1097                          goto done;                          goto done;
# Line 965  Line 1108 
1108                          int tmp;                          int tmp;
1109                          int bits;                          int bits;
1110    
1111                          DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1112                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1113                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1114    
# Line 974  Line 1117 
1117                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
1118                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
1119    
1120                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1121                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1122                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
1123    
1124                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1125                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1126              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);
1127    
1128              /* flush complete: reset counters */              /* flush complete: reset counters */
1129                  pEnc->flush_bframes = 0;                  pEnc->flush_bframes = 0;
# Line 1003  Line 1146 
1146                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
1147                  {                  {
1148    
1149              DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1150                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1151                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1152    
1153              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) {
1154                  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);
1155              }              }
1156    
1157              /* 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 1019  Line 1162 
1162                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1163    
1164                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
1165                  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;
1166                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1167                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1168    
1169                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1170                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
1171                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1172                  }                  }
1173    
1174                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1175                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1176                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1177                                    pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1178    
1179                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs);
1180    
1181    
1182                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1183                      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);
1184                  }else{                  }else{
1185                      pEnc->flush_bframes = 1;                      pEnc->flush_bframes = 1;
1186                      goto done;                      goto done;
1187                  }                  }
1188              }              }
1189              DPRINTF(XVID_DEBUG_DEBUG, "*** END");                          DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1190    
1191                          emms();                          emms();
1192                          return XVID_ERR_END;    /* end of stream reached */                          return XVID_ERR_END;    /* end of stream reached */
# Line 1048  Line 1194 
1194                  goto done;      /* nothing to encode yet; encoder lag */                  goto done;      /* nothing to encode yet; encoder lag */
1195          }          }
1196    
1197          // the current FRAME becomes the reference          /* the current FRAME becomes the reference */
1198          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1199    
1200          // remove frame from encoding-queue (head), and move it into the current          /* remove frame from encoding-queue (head), and move it into the current */
1201          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1202          frame = &pEnc->queue[pEnc->queue_head].frame;          frame = &pEnc->queue[pEnc->queue_head].frame;
1203          pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);          pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
# Line 1064  Line 1210 
1210    
1211      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1212      inc_frame_num(pEnc);      inc_frame_num(pEnc);
1213      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;          pEnc->current->vol_flags = frame->vol_flags;
1214      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1215          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1216          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
# Line 1083  Line 1229 
1229          type = frame->type;          type = frame->type;
1230          pEnc->current->quant = frame->quant;          pEnc->current->quant = frame->quant;
1231    
1232      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);
1233    
1234      if (type > 0){      /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
1235                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
# Line 1094  Line 1240 
1240                  }else{                  }else{
1241                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1242                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1243                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1244                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1245                  }                  }
1246          }          }
1247    
1248            if (type != I_VOP)
1249                    pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1250    
1251      /* bframes buffer overflow check */      /* bframes buffer overflow check */
1252      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1253          type = P_VOP;          type = P_VOP;
# Line 1107  Line 1257 
1257    
1258          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1259                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1260                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                          "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1261          }          }
1262    
1263          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1132  Line 1282 
1282                  else if (pEnc->current->quant > 31)                  else if (pEnc->current->quant > 31)
1283              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1284    
1285                  DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1286                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1287                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1288    
# Line 1146  Line 1296 
1296      }      }
1297    
1298    
1299                  DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1300                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1301                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1302    
# Line 1154  Line 1304 
1304      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)
1305      {      {
1306          if (pEnc->current->stamp > 0) {          if (pEnc->current->stamp > 0) {
1307              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);
1308          }          }
1309                  else                  else
1310                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
# Line 1167  Line 1317 
1317    
1318      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1319    
1320                  // place this frame back on the encoding-queue (head)                  /* place this frame back on the encoding-queue (head) */
1321                  // we will deal with it next time                  /* we will deal with it next time */
1322          dec_frame_num(pEnc);          dec_frame_num(pEnc);
1323          pEnc->iFrameNum--;          pEnc->iFrameNum--;
1324    
# Line 1176  Line 1326 
1326          pEnc->queue_size++;          pEnc->queue_size++;
1327                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1328    
1329                  // grab the last frame from the bframe-queue                  /* grab the last frame from the bframe-queue */
1330    
1331                  pEnc->bframenum_tail--;                  pEnc->bframenum_tail--;
1332                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1333    
1334                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1335                          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");
1336                  }                  }
1337    
1338                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1339                  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;
1340                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1341                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1342          type = P_VOP;          type = P_VOP;
1343      }      }
1344    
# Line 1197  Line 1349 
1349    
1350          if (type == I_VOP) {          if (type == I_VOP) {
1351    
1352                  DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1353                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1354                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1355    
# Line 1205  Line 1357 
1357                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1358                  }                  }
1359    
1360                    pEnc->iFrameNum = 1;
1361    
1362                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1363                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1364    
1365                    /* Aspect ratio */
1366                    switch(frame->par) {
1367                    case XVID_PAR_11_VGA:
1368                    case XVID_PAR_43_PAL:
1369                    case XVID_PAR_43_NTSC:
1370                    case XVID_PAR_169_PAL:
1371                    case XVID_PAR_169_NTSC:
1372                    case XVID_PAR_EXT:
1373                            pEnc->mbParam.par = frame->par;
1374                            break;
1375                    default:
1376                            pEnc->mbParam.par = XVID_PAR_11_VGA;
1377                            break;
1378                    }
1379    
1380                    /* For extended PAR only, we try to sanityse/simplify par values */
1381                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1382                            pEnc->mbParam.par_width  = frame->par_width;
1383                            pEnc->mbParam.par_height = frame->par_height;
1384                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1385                    }
1386    
1387          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1388                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1389                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1390                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
1391                                  set_inter_matrix(frame->quant_inter_matrix);                                  set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1392                  }                  }
1393    
1394          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1395    
         if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
             pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;  
   
1396          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1397              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1398    
# Line 1238  Line 1410 
1410           * encode this frame as an p-vop           * encode this frame as an p-vop
1411       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1412    
1413      } else { // (type == P_VOP || type == S_VOP)          } else { /* (type == P_VOP || type == S_VOP) */
1414    
1415                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1416                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1417                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1418    
# Line 1253  Line 1425 
1425                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1426          }          }
1427    
1428                  FrameCodeP(pEnc, &bs, 1, 0);                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1429                            /* N-VOP, we mustn't code b-frames yet */
1430                            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1431                                     pEnc->mbParam.max_bframes == 0)
1432                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1433                            goto done;
1434                    }
1435      }      }
1436    
1437    
# Line 1261  Line 1439 
1439           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1440           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1441    
1442  done_flush:  /*done_flush:*/
1443    
1444      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1445    
# Line 1272  Line 1450 
1450    
1451      /* packed or no-bframes or no-bframes-queued: output stats */      /* packed or no-bframes or no-bframes-queued: output stats */
1452      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 ) {
1453          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);
1454          }          }
1455    
1456          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1291  Line 1469 
1469    
1470  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1471  {  {
1472      unsigned int i,j;          unsigned int i;
1473      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1474            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1475            if (quant > 31)
1476                    frame->quant = quant = 31;
1477            else if (quant < 1)
1478                    frame->quant = quant = 1;
1479    
1480      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];  
1481          quant += pMB->dquant;          quant += pMB->dquant;
1482          if (quant > 31)          if (quant > 31)
1483                          quant = 31;                          quant = 31;
1484                  if (quant < 1)                  else if (quant < 1)
1485                          quant = 1;                          quant = 1;
1486          pMB->quant = quant;          pMB->quant = quant;
1487                    pMB++;
1488      }      }
1489  }  }
1490    
# Line 1340  Line 1522 
1522    
1523          uint16_t x, y;          uint16_t x, y;
1524    
         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();  
         }  
   
1525          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1526          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1527          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1528    
1529            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1530    
1531      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1532    
1533          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1534    
1535          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1536    
1537          BitstreamPadAlways(bs);          BitstreamPad(bs);
1538          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1539            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1540    
1541          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1542            pEnc->current->sStat.iMVBits = 0;
1543          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1544          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1545    
# Line 1386  Line 1558 
1558                          stop_prediction_timer();                          stop_prediction_timer();
1559    
1560                          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;  
                         }  
1561                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1562                          stop_coding_timer();                          stop_coding_timer();
1563                  }                  }
1564    
         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);  
         }  
1565          emms();          emms();
1566    
1567  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1568  #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1569      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1570    
1571          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1572          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1573    
1574      /* XXX: hinted me          pEnc->current->is_edged = 0; /* not edged */
1575          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
                 HintedMEGet(pEnc, 1);  
         }*/  
1576    
1577          return 1;                                       /* intra */          return 1;                                       /* intra */
1578  }  }
1579    
1580    static __inline void
1581    updateFcode(Statistics * sStat, Encoder * pEnc)
1582    {
1583            float fSigma;
1584            int iSearchRange;
1585    
1586            if (sStat->iMvCount == 0)
1587                    sStat->iMvCount = 1;
1588    
1589  #define INTRA_THRESHOLD 0.5          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
1590  #define BFRAME_SKIP_THRESHHOLD 30  
1591            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1592    
1593            if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1594                    pEnc->mbParam.m_fcode++;
1595    
1596            else if ((5.0 * fSigma < iSearchRange)
1597                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1598                               && (pEnc->mbParam.m_fcode >= 2) )
1599                    pEnc->mbParam.m_fcode--;
1600    
1601            pEnc->fMvPrevSigma = fSigma;
1602    }
1603    
1604    #define BFRAME_SKIP_THRESHHOLD 30
1605    
1606  /* FrameCodeP also handles S(GMC)-VOPs */  /* FrameCodeP also handles S(GMC)-VOPs */
1607  static int  static int
1608  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1609                     Bitstream * bs,                     Bitstream * bs)
                    bool force_inter,  
                    bool vol_header)  
1610  {  {
         float fSigma;  
1611      int bits = BitstreamPos(bs);      int bits = BitstreamPos(bs);
1612    
1613          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1614          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1615    
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
         int iLimit;  
1616          int x, y, k;          int x, y, k;
1617          int iSearchRange;          FRAMEINFO *const current = pEnc->current;
1618          int bIntra, skip_possible;          FRAMEINFO *const reference = pEnc->reference;
1619            MBParam * const pParam = &pEnc->mbParam;
1620          /* IMAGE *pCurrent = &pEnc->current->image; */          int mb_width = pParam->mb_width;
1621          IMAGE *pRef = &pEnc->reference->image;          int mb_height = pParam->mb_height;
1622            int coded = 1;
         if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
         }  
1623    
1624            IMAGE *pRef = &reference->image;
1625    
1626            if (!reference->is_edged) {
1627          start_timer();          start_timer();
1628          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1629                                     pEnc->mbParam.width, pEnc->mbParam.height);                                             pParam->width, pParam->height, 0);
1630          stop_edges_timer();          stop_edges_timer();
1631                    reference->is_edged = 1;
1632            }
1633    
1634          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1635          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1636          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          current->fcode = pParam->m_fcode;
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
1637    
1638          if (!force_inter)          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1639                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);                  if (reference->is_interpolated != current->rounding_type) {
         else  
                 iLimit = mb_width * mb_height + 1;  
   
         if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) {  
1640                  start_timer();                  start_timer();
1641                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1642                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                            pEnc->vInterHV.y, pParam->edged_width,
1643                                                    pEnc->mbParam.edged_height,                                                            pParam->edged_height,
1644                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1645                                                    pEnc->current->rounding_type);                                                            current->rounding_type);
1646                  stop_inter_timer();                  stop_inter_timer();
1647                            reference->is_interpolated = current->rounding_type;
1648                    }
1649          }          }
1650    
1651          pEnc->current->coding_type = P_VOP;          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1652                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1653                    current->sStat.iMVBits = 0;
1654    
1655            current->coding_type = P_VOP;
1656    
1657      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1658    
1659            SetMacroblockQuants(&pEnc->mbParam, current);
1660    
1661          start_timer();          start_timer();
1662          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1663                  HintedMESet(pEnc, &bIntra);          {       int gmcval;
1664          else*/                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1665                  bIntra =                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1666                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
1667                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  if (current->motion_flags & XVID_ME_GME_REFINE) {
1668                           iLimit);                          gmcval = GlobalMotionEstRefine(&current->warp,
1669                                                                                       current->mbs, pParam,
1670                                                                                       current, reference,
1671                                                                                       &current->image,
1672                                                                                       &reference->image,
1673                                                                                       &pEnc->vInterH,
1674                                                                                       &pEnc->vInterV,
1675                                                                                       &pEnc->vInterHV);
1676                    } else {
1677                            gmcval = globalSAD(&current->warp, pParam, current->mbs,
1678                                                               current,
1679                                                               &reference->image,
1680                                                               &current->image,
1681                                                               pEnc->vGMC.y);
1682                    }
1683    
1684                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1685    
1686                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1687                    generate_GMCparameters( 3, 3, &current->warp,
1688                                    pParam->width, pParam->height,
1689                                    &current->new_gmc_data);
1690    
1691                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1692                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1693                    {
1694                            current->coding_type = S_VOP;
1695    
1696                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1697                                    pParam->mb_width, pParam->mb_height,
1698                                    pParam->edged_width, pParam->edged_width/2,
1699                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1700                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1701    
1702          stop_motion_timer();                  } else {
1703    
1704                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1705                                    pParam->mb_width, pParam->mb_height,
1706                                    pParam->edged_width, pParam->edged_width/2,
1707                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1708                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1709                    }
1710            }
1711    
         if (bIntra == 1) return FrameCodeI(pEnc, bs);  
1712    
1713          if ( ( pEnc->current->vol_flags & XVID_VOL_GMC )          if (pEnc->num_threads > 0) {
1714                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )                  /* multithreaded motion estimation - dispatch threads */
         {  
                 pEnc->current->coding_type = S_VOP;  
1715    
1716                  generate_GMCparameters( 2, 16, &pEnc->current->warp,                  void * status;
1717                                          pEnc->mbParam.width, pEnc->mbParam.height,                  int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
                                         &pEnc->current->gmc_data);  
   
                 generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,  
                                 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,  
                                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,  
                                 pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0,  
                                 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);  
1718    
1719                    for (k = 0; k < pEnc->num_threads; k++) {
1720                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
1721                            pEnc->motionData[k].pParam = &pEnc->mbParam;
1722                            pEnc->motionData[k].current = current;
1723                            pEnc->motionData[k].reference = reference;
1724                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
1725                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
1726                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
1727                            pEnc->motionData[k].pGMC = &pEnc->vGMC;
1728                            pEnc->motionData[k].y_step = pEnc->num_threads;
1729                            pEnc->motionData[k].start_y = k;
1730                            /* todo: sort out temp space once and for all */
1731                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width;
1732          }          }
1733    
1734          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  for (k = 1; k < pEnc->num_threads; k++) {
1735          if (vol_header)                          pthread_create(&pEnc->motionData[k].handle, NULL,
1736          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);                                  (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]);
                 BitstreamPadAlways(bs);  
1737          }          }
1738    
1739          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);                  MotionEstimateSMP(&pEnc->motionData[0]);
1740    
1741          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =                  for (k = 1; k < pEnc->num_threads; k++) {
1742                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                          pthread_join(pEnc->motionData[k].handle, &status);
1743                    }
1744    
1745                    current->fcode = 0;
1746                    for (k = 0; k < pEnc->num_threads; k++) {
1747                            current->sStat.iMvSum += pEnc->motionData[k].mvSum;
1748                            current->sStat.iMvCount += pEnc->motionData[k].mvCount;
1749                            if (pEnc->motionData[k].minfcode > current->fcode)
1750                                    current->fcode = pEnc->motionData[k].minfcode;
1751                    }
1752    
1753          for (y = 0; y < mb_height; y++) {          } else {
1754                  for (x = 0; x < mb_width; x++) {                  /* regular ME */
                         MACROBLOCK *pMB =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1755    
1756  /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */                  MotionEstimation(&pEnc->mbParam, current, reference,
1757  /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1758                                                     &pEnc->vGMC, 256*4096);
1759            }
1760    
1761            stop_motion_timer();
1762    
1763            set_timecodes(current,reference,pParam->fbase);
1764    
1765            BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1766    
1767                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);          for (y = 0; y < mb_height; y++) {
1768                    for (x = 0; x < mb_width; x++) {
1769                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1770                            int skip_possible;
1771    
1772                          if (bIntra) {                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1773                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1774                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1775                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1776    
1777                                  start_timer();                                  start_timer();
1778                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1779                                  stop_prediction_timer();                                  stop_prediction_timer();
1780    
1781                                  pEnc->current->sStat.kblks++;                                  current->sStat.kblks++;
1782    
1783                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1784                                  stop_coding_timer();                                  stop_coding_timer();
1785                                  continue;                                  continue;
1786                          }                          }
1787    
                         if (pEnc->current->coding_type == S_VOP) {  
   
                                 int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->mbParam.edged_width, 65536);  
   
                                 if (pEnc->current->motion_flags & XVID_ME_CHROMA16) {  
                                         iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
   
                                         iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
                                 }  
   
                                 if (iSAD <= pMB->sad16) {               /* mode decision GMC */  
   
                                         if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))  
                                                 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;  
                                         else  
                                                 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
                                         pMB->mode = MODE_INTER;  
                                         pMB->mcsel = 1;  
                                         pMB->sad16 = iSAD;  
                                 } else {  
                                         pMB->mcsel = 0;  
                                 }  
                         } else {  
                                 pMB->mcsel = 0; /* just a precaution */  
                         }  
   
1788                          start_timer();                          start_timer();
1789                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1790                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1791                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1792                                                                   &pEnc->current->image,                                                                   &current->image,
1793                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1794                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1795                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1796                                                                   (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1797                                                                   (pEnc->current->vop_flags & XVID_VOP_REDUCED),                                                                   current->rounding_type);
                                                                  pEnc->current->rounding_type);  
1798    
1799                          stop_comp_timer();                          stop_comp_timer();
1800    
                         if (pMB->dquant != 0) {  
                 pMB->mode = MODE_INTER_Q;  
                         }  
   
1801                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1802    
1803                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1804                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,  
1805                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1806                          }                          }
1807    
1808                            if (pMB->dquant != 0)
1809                                    MBSetDquant(pMB, x, y, &pEnc->mbParam);
1810    
1811    
1812                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1813                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1814                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1815                                  pEnc->current->sStat.mblks++;                                  current->sStat.mblks++;
1816                          }  else {                          }  else {
1817                                  pEnc->current->sStat.ublks++;                                  current->sStat.ublks++;
1818                          }                          }
1819    
1820                          start_timer();                          start_timer();
1821    
1822                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1823    
1824                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                                                         (pMB->dquant == 0);  
1825    
1826                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1827                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1828                          else if (pEnc->current->coding_type == P_VOP) {                          else { /* PVOP */
1829                                  if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1830                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                                                                  pMB->qmvs : pMB->mvs;
1831                                  else                                  skip_possible &= ((mv->x|mv->y) == 0);
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
1832                          }                          }
1833    
1834                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
   
1835  /* 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 (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */  
                                 {  
1836                                          int bSkip = 1;                                          int bSkip = 1;
1837    
1838                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
                                         {  
                                                 int iSAD;  
                                                 iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                                                         pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                                                 pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);  
                                                 if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)  
                                                 {       bSkip = 0;  
                                                         break;  
                                                 }  
                                         }  
1839    
1840                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1841                                                  if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                                  int iSAD;
1842                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1843                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1844                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1845                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1846                                                            bSkip = 0; /* could not SKIP */
1847                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1848                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1849                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1850                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1851                                                  }                                                          } else {
1852                                                  else {                                                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
                                                         VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
1853                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1854                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1855                                                  }                                                  }
1856                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1857                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1858                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                          break;
1859                                                  stop_coding_timer();                                                  }
   
                                                 continue;       /* next MB */  
1860                                          }                                          }
1861                                  }                                  }
                                 /* do SKIP */  
1862    
1863                                    if (bSkip) {
1864                                            /* do SKIP */
1865                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1866                                  MBSkip(bs);                                  MBSkip(bs);
1867                                  stop_coding_timer();                                  stop_coding_timer();
1868                                  continue;       /* next MB */                                  continue;       /* next MB */
1869                          }                          }
                         /* ordinary case: normal coded INTER/INTER4V block */  
   
                         if ((pEnc->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;  
1870                          }                          }
1871    
1872                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                          /* ordinary case: normal coded INTER/INTER4V block */
1873                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
                                 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)", 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(pEnc->current->mbs, pEnc->mbParam.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)", 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((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {  
                                                 VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.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)", 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(pEnc->current->mbs, pEnc->mbParam.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)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);  
                                         }  
   
                                 }  
                         }  
   
                         MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);  
1874                          stop_coding_timer();                          stop_coding_timer();
   
                 }  
1875          }          }
   
         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);  
1876          }          }
1877    
1878          emms();          emms();
1879            updateFcode(&current->sStat, pEnc);
     /* XXX: hinted me  
         if (pEnc->current->global_flags & XVID_HINTEDME_GET) {  
                 HintedMEGet(pEnc, 0);  
         }*/  
   
         if (pEnc->current->sStat.iMvCount == 0)  
                 pEnc->current->sStat.iMvCount = 1;  
   
         fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);  
   
         iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);  
   
         if ((fSigma > iSearchRange / 3)  
         && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0)  )))     /* maximum search range 128 */  
         {  
                 pEnc->mbParam.m_fcode++;  
                 iSearchRange *= 2;  
         } else if ((fSigma < iSearchRange / 6)  
                            && (pEnc->fMvPrevSigma >= 0)  
                            && (pEnc->fMvPrevSigma < iSearchRange / 6)  
                            && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) )))    /* minimum search range 16 */  
         {  
                 pEnc->mbParam.m_fcode--;  
                 iSearchRange /= 2;  
         }  
   
         pEnc->fMvPrevSigma = fSigma;  
1880    
1881          /* frame drop code */          /* frame drop code */
1882          //DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
1883          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1884                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)  #endif
1885            if (current->sStat.kblks + current->sStat.mblks <=
1886                    (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                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
1890                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1891    
1892                  BitstreamReset(bs);                  BitstreamReset(bs);
1893    
1894                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
1895                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->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                  pEnc->current->quant = pEnc->reference->quant;                  current->quant = reference->quant;
1899                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  current->motion_flags = reference->motion_flags;
1900                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1901                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  current->fcode = reference->fcode;
1902                  pEnc->current->fcode = pEnc->reference->fcode;                  current->bcode = reference->bcode;
1903                  pEnc->current->bcode = pEnc->reference->bcode;                  current->stamp = reference->stamp;
1904                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1905                  memcpy(pEnc->current->mbs, pEnc->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
1922          {          {
1923                  char s[100];                  char s[100];
1924                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1925                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
1926                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1927                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1928    
1929                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1930                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
1931                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1932                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1933          }          }
1934          */          */
1935    
1936  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
 #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1937    
1938      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          current->length = (BitstreamPos(bs) - bits) / 8;
1939    
1940          return 0;                                       /* inter */          return coded;
1941  }  }
1942    
1943    
# Line 1846  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    
         //frame->quarterpel =  pEnc->mbParam.m_quarterpel;  
   
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          start_timer();          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            start_timer();
2009            if (pEnc->num_threads > 0) {
2010                    void * status;
2011                    int k;
2012                    /* multithreaded motion estimation - dispatch threads */
2013                    int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
2014    
2015                    for (k = 0; k < pEnc->num_threads; k++) {
2016                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
2017                            pEnc->motionData[k].pParam = &pEnc->mbParam;
2018                            pEnc->motionData[k].current = frame;
2019                            pEnc->motionData[k].reference = pEnc->current;
2020                            pEnc->motionData[k].fRef = f_ref;
2021                            pEnc->motionData[k].fRefH = &pEnc->f_refh;
2022                            pEnc->motionData[k].fRefV = &pEnc->f_refv;
2023                            pEnc->motionData[k].fRefHV = &pEnc->f_refhv;
2024                            pEnc->motionData[k].pRef = b_ref;
2025                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
2026                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
2027                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
2028                            pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);
2029                            pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);
2030                            pEnc->motionData[k].y_step = pEnc->num_threads;
2031                            pEnc->motionData[k].start_y = k;
2032                            /* todo: sort out temp space once and for all */
2033                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width;
2034                    }
2035    
2036                    for (k = 1; k < pEnc->num_threads; k++) {
2037                            pthread_create(&pEnc->motionData[k].handle, NULL,
2038                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]);
2039                    }
2040    
2041                    SMPMotionEstimationBVOP(&pEnc->motionData[0]);
2042    
2043                    for (k = 1; k < pEnc->num_threads; k++) {
2044                            pthread_join(pEnc->motionData[k].handle, &status);
2045                    }
2046    
2047                    frame->fcode = frame->bcode = 0;
2048                    for (k = 0; k < pEnc->num_threads; k++) {
2049                            if (pEnc->motionData[k].minfcode > frame->fcode)
2050                                    frame->fcode = pEnc->motionData[k].minfcode;
2051                            if (pEnc->motionData[k].minbcode > frame->bcode)
2052                                    frame->bcode = pEnc->motionData[k].minbcode;
2053                    }
2054            } else {
2055          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2056                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2057                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
# Line 1884  Line 2059 
2059                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2060                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2061                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
   
   
         stop_motion_timer();  
   
         /*  
         if (test_quant_type(&pEnc->mbParam, pEnc->current)) {  
                 BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
2062          }          }
2063          */          stop_motion_timer();
   
         frame->coding_type = B_VOP;  
2064    
2065          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2066          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2067    
2068          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2069            frame->sStat.iMVBits = 0;
2070          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2071          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2072          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
# Line 1909  Line 2076 
2076          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2077                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2078                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
                         int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0;  
2079    
2080                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2081                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2082                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2083                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2084                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
2085                                    }
2086                                  continue;                                  continue;
2087                          }                          }
2088    
2089                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                          mb->quant = frame->quant;
2090    
2091                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2092                                    /* we have to motion-compensate, transfer etc,
2093                                            because there might be blocks to code */
2094    
2095                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2096                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2097                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2098                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
2099                                                                           dct_codes);                                                                           dct_codes);
2100    
                                 if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;  
                                 mb->quant = frame->quant;  
   
                                 if (mb->mode != MODE_DIRECT_NONE_MV)  
2101                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
2102                            }
2103    
2104                            if (mb->mode == MODE_DIRECT_NO4V)
2105                                    mb->mode = MODE_DIRECT;
2106    
2107                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
                                         && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {  
2108                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2109                                  }                          else
2110                          }                                  if (frame->vop_flags & XVID_VOP_GREYSCALE)
2111                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2112                                            mb->cbp &= 0x3C;
2113    
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
2114                          start_timer();                          start_timer();
2115                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
2116                                                   &frame->sStat, direction);                                                   &frame->sStat);
2117                          stop_coding_timer();                          stop_coding_timer();
2118                  }                  }
2119          }          }
   
2120          emms();          emms();
2121    
2122          /* TODO: dynamic fcode/bcode ??? */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
   
     BitstreamPadAlways(bs);  
2123          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
2124    
2125  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.24  
changed lines
  Added in v.1.128

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