[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.10, Thu Mar 20 08:04:18 2003 UTC revision 1.133, Sun Nov 28 15:18:21 2010 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-2010 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    
136    
137  int  int
138  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)  enc_create(xvid_enc_create_t * create)
139  {  {
140          Encoder *pEnc;          Encoder *pEnc;
141      int n;      int n;
142    
143          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->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 134  Line 156 
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
157          memset(pEnc, 0, sizeof(Encoder));          memset(pEnc, 0, sizeof(Encoder));
158    
159            pEnc->mbParam.profile = create->profile;
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 149  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      /* plugin */          /* zones */
181            if(create->num_zones > 0) {
182                    pEnc->num_zones = create->num_zones;
183                    pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
184                    if (pEnc->zones == NULL)
185                            goto xvid_err_memory0;
186                    memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
187            } else {
188                    pEnc->num_zones = 0;
189                    pEnc->zones = NULL;
190            }
191    
192            /* plugins */
193            if(create->num_plugins > 0) {
194      pEnc->num_plugins = create->num_plugins;      pEnc->num_plugins = create->num_plugins;
195      pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);      pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
196      if (pEnc->plugins == NULL)      if (pEnc->plugins == NULL)
197          goto xvid_err_memory0;          goto xvid_err_memory0;
198            } else {
199                    pEnc->num_plugins = 0;
200                    pEnc->plugins = NULL;
201            }
202    
203      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
204          xvid_plg_create_t pcreate;          xvid_plg_create_t pcreate;
# Line 163  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    
213          memset(&pcreate, 0, sizeof(xvid_plg_create_t));          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
214          pcreate.version = XVID_VERSION;          pcreate.version = XVID_VERSION;
215                    pcreate.num_zones = pEnc->num_zones;
216                    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      }      }
230    
231      if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) ||          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
232          (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {          (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
233          pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */          pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
234      }      }
# Line 190  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);
254          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
255          pEnc->mbParam.bquant_offset = create->bquant_offset;          pEnc->mbParam.bquant_offset = create->bquant_offset;
256    
257            /* min/max quant */
258            for (n=0; n<3; n++) {
259                    pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
260                    pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
261            }
262    
263          /* frame drop ratio */          /* frame drop ratio */
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 ? 250 : create->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
     /*XXX: replace 250 hard code with "10seconds * framerate" */  
   
         /* 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 237  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 252  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 298  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 378  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;
438          pEnc->m_framenum = 0;          pEnc->m_framenum = create->start_frame_num;
439          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
440          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
441    
# Line 400  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 412  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 434  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 468  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 488  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);
574    
575            xvid_free(pEnc->zones);
576    
577          xvid_free(pEnc);          xvid_free(pEnc);
578    
579          create->handle = NULL;          create->handle = NULL;
# Line 524  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 544  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 564  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,
640                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
641          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
642                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
643            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
644                                      pEnc->mbParam.edged_height);
645    
646          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
647                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
# Line 597  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) {
670                    xvid_plg_destroy_t pdestroy;
671                    memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
672    
673                    pdestroy.version = XVID_VERSION;
674                    pdestroy.num_frames = pEnc->m_framenum;
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, 0, 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            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
685    
686            if (pEnc->num_zones > 0)
687                    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    
# Line 617  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 625  Line 714 
714      memset(&data, 0, sizeof(xvid_plg_data_t));      memset(&data, 0, sizeof(xvid_plg_data_t));
715      data.version = XVID_VERSION;      data.version = XVID_VERSION;
716    
717            /* find zone */
718            for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
719            data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
720    
721      data.width = pEnc->mbParam.width;      data.width = pEnc->mbParam.width;
722      data.height = pEnc->mbParam.height;      data.height = pEnc->mbParam.height;
723      data.mb_width = pEnc->mbParam.mb_width;      data.mb_width = pEnc->mbParam.mb_width;
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      data.reference.csp = XVID_CSP_USER;          for (i=0; i<3; i++) {
731                    data.min_quant[i] = pEnc->mbParam.min_quant[i];
732                    data.max_quant[i] = pEnc->mbParam.max_quant[i];
733            }
734    
735            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 640  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 651  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 = 2;                  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                            emms();
774                            data.lambda = pEnc->temp_lambda;
775                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
776                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
777                                            for (k = 0; k < 6; k++)
778                                                    data.lambda[block++] = 1.0f;
779                    }
780    
781      } else { // XVID_PLG_AFTER          } else { /* XVID_PLG_AFTER */
782          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
783              data.original.csp = XVID_CSP_USER;                          data.original.csp = XVID_CSP_PLANAR;
784              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
785              data.original.plane[1] = original->u;              data.original.plane[1] = original->u;
786              data.original.plane[2] = original->v;              data.original.plane[2] = original->v;
# Line 673  Line 789 
789              data.original.stride[2] = pEnc->mbParam.edged_width/2;              data.original.stride[2] = pEnc->mbParam.edged_width/2;
790          }          }
791    
792          if ((frame->vol_flags & XVID_EXTRASTATS) ||                  if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
793              (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {              (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
794    
795                          data.sse_y =                          data.sse_y =
# Line 701  Line 817 
817    
818              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
819              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
820                  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;
821              }              }
822          }          }
823    
# Line 714  Line 830 
830          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
831          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
832    
833          if (stats) {                  /* New code */
834                  stats->type = coding2type(frame->coding_type);                  data.stats.type      = coding2type(frame->coding_type);
835                  stats->quant = frame->quant;                  data.stats.quant     = frame->quant;
836                  stats->vol_flags = frame->vol_flags;                  data.stats.vol_flags = frame->vol_flags;
837                  stats->vop_flags = frame->vop_flags;                  data.stats.vop_flags = frame->vop_flags;
838                  stats->length = frame->length;                  data.stats.length    = frame->length;
839                  stats->hlength = frame->length - (frame->sStat.iTextBits / 8);                  data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
840                  stats->kblks = frame->sStat.kblks;                  data.stats.kblks     = frame->sStat.kblks;
841                  stats->mblks = frame->sStat.mblks;                  data.stats.mblks     = frame->sStat.mblks;
842                  stats->ublks = frame->sStat.ublks;                  data.stats.ublks     = frame->sStat.ublks;
843              stats->sse_y = data.sse_y;                  data.stats.sse_y     = data.sse_y;
844              stats->sse_u = data.sse_u;                  data.stats.sse_u     = data.sse_u;
845              stats->sse_v = data.sse_v;                  data.stats.sse_v     = data.sse_v;
846          }  
847                    if (stats)
848                            *stats = data.stats;
849      }      }
850    
851      /* call plugins */      /* call plugins */
852      for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
853          emms();          emms();
854          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
855              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) {
856                  continue;                  continue;
857              }              }
858          }          }
# Line 744  Line 862 
862      /* copy modified values back into frame*/      /* copy modified values back into frame*/
863      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
864          *type = data.type;          *type = data.type;
865          *quant = data.quant;                  *quant = data.quant > 0 ? data.quant : 2;   /* default */
866    
867                    frame->vol_flags = data.vol_flags;
868                    frame->vop_flags = data.vop_flags;
869                    frame->motion_flags = data.motion_flags;
870    
871            } else if (opt == XVID_PLG_FRAME) {
872    
873          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
874              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
# Line 757  Line 881 
881                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
882              }              }
883          }          }
884          /* todo: [vol,vop,motion]_flags*/  
885                    if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
886                            for (j = 0; j < pEnc->mbParam.mb_height; j++)
887                                    for (i = 0; i < pEnc->mbParam.mb_width; i++)
888                                            for (k = 0; k < 6; k++) {
889                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
890                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
891                                            }
892                    } else {
893                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
894                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
895                                            for (k = 0; k < 6; k++) {
896                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
897                                            }
898      }      }
899    
900    
901                    frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
902  }  }
903    
904    
905    }
906    
907    
908  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
# Line 779  Line 920 
920      pEnc->m_framenum--; /* debug ticker */      pEnc->m_framenum--; /* debug ticker */
921  }  }
922    
923    static __inline void
924    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
925    {
926            if (pMB->cbp == 0) {
927                    /* we want to code dquant but the quantizer value will not be used yet
928                            let's find out if we can postpone dquant to next MB
929                    */
930                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
931                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
932                            return;
933                    } else {
934                            MACROBLOCK * next = pMB + 1;
935                            const MACROBLOCK * prev = pMB - 1;
936                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
937                                    /* mode allows dquant change in the future */
938                                    if (abs(next->quant - prev->quant) <= 2) {
939                                            /* quant change is not out of range */
940                                            pMB->quant = prev->quant;
941                                            pMB->dquant = 0;
942                                            next->dquant = next->quant - prev->quant;
943                                            return;
944                                    }
945                    }
946            }
947            /* couldn't skip this dquant */
948            pMB->mode = MODE_INTER_Q;
949    }
950    
951    
952    
953  static __inline void  static __inline void
# Line 788  Line 957 
957                  pCur->ticks = (int32_t)pCur->stamp % time_base;                  pCur->ticks = (int32_t)pCur->stamp % time_base;
958                  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) ;
959    
960                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */  #if 0   /* HEAVY DEBUG OUTPUT */
961            fprintf(stderr,"WriteVop:   %d - %d \n",
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
962                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
963                  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",
964                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
965                  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",
966                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
967    #endif
968    }
969    
970  */  static void
971    simplify_par(int *par_width, int *par_height)
972    {
973    
974            int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
975            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
976            int divisor = gcd(_par_width, _par_height);
977    
978            _par_width  /= divisor;
979            _par_height /= divisor;
980    
981            /* 2^8 precision maximum */
982            if (_par_width>255 || _par_height>255) {
983                    float div;
984                    emms();
985                    if (_par_width>_par_height)
986                            div = (float)_par_width/255;
987                    else
988                            div = (float)_par_height/255;
989    
990                    _par_width  = (int)((float)_par_width/div);
991                    _par_height = (int)((float)_par_height/div);
992  }  }
993    
994            *par_width = _par_width;
995            *par_height = _par_height;
996    
997            return;
998    }
999    
1000  /*****************************************************************************  /*****************************************************************************
1001   * IPB frame encoder entry point   * IPB frame encoder entry point
# Line 824  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 845  Line 1040 
1040                  if (image_input                  if (image_input
1041                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
1042                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
1043                          xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))                          xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
1044                  {                  {
1045                          emms();                          emms();
1046                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
1047                  }                  }
1048                  stop_conv_timer();                  stop_conv_timer();
1049    
1050                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {                  if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1051                          image_chroma_optimize(&q->image,                          image_chroma_optimize(&q->image,
1052                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1053                  }                  }
# Line 861  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 886  Line 1081 
1081          {          {
1082                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
1083    
1084                          DPRINTF(DPRINTF_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 896  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 909  Line 1104 
1104                     indentical to the future-referece frame.                     indentical to the future-referece frame.
1105                  */                  */
1106    
1107                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
1108                          int tmp;                          int tmp;
1109                          int bits;                          int bits;
1110    
1111                          DPRINTF(DPRINTF_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 922  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 951  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              if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) {                          DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1150                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1151                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1152    
1153                            if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1154                                    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 */
1158              if (pEnc->bframenum_tail > 0)                          if (pEnc->bframenum_tail > 0) {
                         {  
1159    
1160                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1161                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
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                                  FrameCodeP(pEnc, &bs, 1, 0);                                  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,
1176                                    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);
1180    
1181                                  goto done_flush;  
1182                                    if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1183                                            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1184                                    }else{
1185                                            pEnc->flush_bframes = 1;
1186                                            goto done;
1187                                    }
1188                          }                          }
1189                            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 982  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 997  Line 1209 
1209           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
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      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;          inc_frame_num(pEnc);
1213            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;
1217          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
1218    
1219      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {  
1220            if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1221              image_chroma_optimize(&pEnc->current->image,              image_chroma_optimize(&pEnc->current->image,
1222                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1223      }      }
# Line 1012  Line 1226 
1226           * frame type & quant selection           * frame type & quant selection
1227           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1228    
     call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);  
   
     if (frame->type > 0)  
1229                  type = frame->type;                  type = frame->type;
   
     if (frame->quant > 0)  
1230                  pEnc->current->quant = frame->quant;                  pEnc->current->quant = frame->quant;
1231    
1232            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 */
1236      } else{             /* XVID_TYPE_AUTO */      } else{             /* XVID_TYPE_AUTO */
# Line 1029  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);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1244                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
             if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) {  
                 type = P_VOP;   /* disable dynamic bframes */  
             }  
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 != I_VOP) {          if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
         if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
1253              type = P_VOP;              type = P_VOP;
         }else{  
             type = B_VOP;  
         }  
1254      }      }
1255    
         inc_frame_num(pEnc);  
1256          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1257    
1258          if ((pEnc->current->vop_flags & XVID_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 1059  Line 1265 
1265       * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)       * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1266           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1267          if (type == B_VOP) {          if (type == B_VOP) {
1268                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1269                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1270                  }                  }
1271    
# Line 1076  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(DPRINTF_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 1089  Line 1295 
1295                  goto repeat;                  goto repeat;
1296      }      }
1297    
1298    
1299                    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,
1301                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1302    
1303      /* for unpacked bframes, output the stats for the last encoded frame */      /* for unpacked bframes, output the stats for the last encoded frame */
1304      if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 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 if (stats) {
1310                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
1311      }      }
1312            }
1313    
1314          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1315           * closed-gop           * closed-gop
1316       * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe       * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1317       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1318    
1319      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {          if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1320    
1321                  // place this frame back on the encoding-queue (head)                  /* place this frame back on the encoding-queue (head) */
1322                  // we will deal with it next time                  /* we will deal with it next time */
1323          dec_frame_num(pEnc);          dec_frame_num(pEnc);
1324          pEnc->iFrameNum--;          pEnc->iFrameNum--;
1325    
# Line 1115  Line 1327 
1327          pEnc->queue_size++;          pEnc->queue_size++;
1328                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1329    
1330                  // grab the last frame from the bframe-queue                  /* grab the last frame from the bframe-queue */
1331    
1332                  pEnc->bframenum_tail--;                  pEnc->bframenum_tail--;
1333                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1334    
1335                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1336                          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");
1337                  }                  }
1338    
1339                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1340                  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;
1341                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1342                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1343          type = P_VOP;          type = P_VOP;
1344      }      }
1345    
# Line 1136  Line 1350 
1350    
1351          if (type == I_VOP) {          if (type == I_VOP) {
1352    
1353                  DPRINTF(DPRINTF_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",
1354                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1355                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1356    
1357                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1358                          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");
1359                  }                  }
1360    
1361                    pEnc->iFrameNum = 1;
1362    
1363                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1364                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1365    
1366                    /* Aspect ratio */
1367                    switch(frame->par) {
1368                    case XVID_PAR_11_VGA:
1369                    case XVID_PAR_43_PAL:
1370                    case XVID_PAR_43_NTSC:
1371                    case XVID_PAR_169_PAL:
1372                    case XVID_PAR_169_NTSC:
1373                    case XVID_PAR_EXT:
1374                            pEnc->mbParam.par = frame->par;
1375                            break;
1376                    default:
1377                            pEnc->mbParam.par = XVID_PAR_11_VGA;
1378                            break;
1379                    }
1380    
1381                    /* For extended PAR only, we try to sanityse/simplify par values */
1382                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1383                            pEnc->mbParam.par_width  = frame->par_width;
1384                            pEnc->mbParam.par_height = frame->par_height;
1385                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1386                    }
1387    
1388          if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {                  if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1389                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1390                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1391                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
1392                                  set_inter_matrix(frame->quant_inter_matrix);                                  set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1393                  }                  }
1394    
1395          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1396    
1397          if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1398              pEnc->current->vop_flags &= ~XVID_REDUCED;                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
   
         if (!(pEnc->current->vol_flags & XVID_INTERLACING))  
             pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);  
1399    
1400                  /* ^^^------------------------ */                  /* ^^^------------------------ */
1401    
# Line 1177  Line 1411 
1411           * encode this frame as an p-vop           * encode this frame as an p-vop
1412       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1413    
1414      } else { // (type == P_VOP || type == S_VOP)          } else { /* (type == P_VOP || type == S_VOP) */
1415    
1416                  DPRINTF(DPRINTF_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",
1417                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1418                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1419    
1420                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1421                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1422                  }                  }
1423    
# Line 1192  Line 1426 
1426                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1427          }          }
1428    
1429                  FrameCodeP(pEnc, &bs, 1, 0);                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1430                            /* N-VOP, we mustn't code b-frames yet */
1431                            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1432                                     pEnc->mbParam.max_bframes == 0)
1433                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1434                            goto done;
1435                    }
1436      }      }
1437    
1438    
# Line 1200  Line 1440 
1440           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1441           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1442    
1443  done_flush:  /*done_flush:*/
1444    
1445      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1446    
1447          /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */          /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1448          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1449                  goto repeat;                  goto repeat;
1450          }          }
1451    
1452      /* packed or no-bframes: output stats */          /* packed or no-bframes or no-bframes-queued: output stats */
1453      if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) {          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1454          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);
1455          }          }
1456    
1457          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1230  Line 1470 
1470    
1471  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1472  {  {
1473      unsigned int i,j;          unsigned int i;
1474      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1475            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1476            if (quant > 31)
1477                    frame->quant = quant = 31;
1478            else if (quant < 1)
1479                    frame->quant = quant = 1;
1480    
1481      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];  
1482          quant += pMB->dquant;          quant += pMB->dquant;
1483          if (quant > 31)          if (quant > 31)
1484                          quant = 31;                          quant = 31;
1485                  if (quant < 1)                  else if (quant < 1)
1486                          quant = 1;                          quant = 1;
1487          pMB->quant = quant;          pMB->quant = quant;
1488                    pMB++;
1489      }      }
1490  }  }
1491    
# Line 1279  Line 1523 
1523    
1524          uint16_t x, y;          uint16_t x, y;
1525    
         if ((pEnc->current->vol_flags & XVID_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();  
         }  
   
1526          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1527          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1528          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1529    
1530            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1531    
1532      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1533    
1534          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1535    
1536          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1537    
1538          BitstreamPadAlways(bs);          BitstreamPad(bs);
1539          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1540            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1541    
1542          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1543            pEnc->current->sStat.iMVBits = 0;
1544          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1545          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1546    
# Line 1325  Line 1559 
1559                          stop_prediction_timer();                          stop_prediction_timer();
1560    
1561                          start_timer();                          start_timer();
                         if (pEnc->current->vop_flags & XVID_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;  
                         }  
1562                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1563                          stop_coding_timer();                          stop_coding_timer();
1564                  }                  }
1565    
         if ((pEnc->current->vop_flags & XVID_REDUCED))  
         {  
                 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,  
                         16, 0);  
         }  
1566          emms();          emms();
1567    
1568          /* for divx5 compatibility, we must always pad between the packed p and b frames */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1569          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)  
                 BitstreamPadAlways(bs);  
         else  
                 BitstreamPad(bs);  
1570      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1571    
1572          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1573          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1574    
1575      /* XXX: hinted me          pEnc->current->is_edged = 0; /* not edged */
1576          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
                 HintedMEGet(pEnc, 1);  
         }*/  
1577    
1578          return 1;                                       /* intra */          return 1;                                       /* intra */
1579  }  }
1580    
1581    static __inline void
1582    updateFcode(Statistics * sStat, Encoder * pEnc)
1583    {
1584            float fSigma;
1585            int iSearchRange;
1586    
1587            if (sStat->iMvCount == 0)
1588                    sStat->iMvCount = 1;
1589    
1590  #define INTRA_THRESHOLD 0.5          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
1591  #define BFRAME_SKIP_THRESHHOLD 30  
1592            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1593    
1594            if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1595                    pEnc->mbParam.m_fcode++;
1596    
1597            else if ((5.0 * fSigma < iSearchRange)
1598                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1599                               && (pEnc->mbParam.m_fcode >= 2) )
1600                    pEnc->mbParam.m_fcode--;
1601    
1602            pEnc->fMvPrevSigma = fSigma;
1603    }
1604    
1605    #define BFRAME_SKIP_THRESHHOLD 30
1606    
1607  /* FrameCodeP also handles S(GMC)-VOPs */  /* FrameCodeP also handles S(GMC)-VOPs */
1608  static int  static int
1609  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1610                     Bitstream * bs,                     Bitstream * bs)
                    bool force_inter,  
                    bool vol_header)  
1611  {  {
         float fSigma;  
1612      int bits = BitstreamPos(bs);      int bits = BitstreamPos(bs);
1613    
1614          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1615          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1616    
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
         int iLimit;  
1617          int x, y, k;          int x, y, k;
1618          int iSearchRange;          FRAMEINFO *const current = pEnc->current;
1619          int bIntra, skip_possible;          FRAMEINFO *const reference = pEnc->reference;
1620            MBParam * const pParam = &pEnc->mbParam;
1621          /* IMAGE *pCurrent = &pEnc->current->image; */          int mb_width = pParam->mb_width;
1622          IMAGE *pRef = &pEnc->reference->image;          int mb_height = pParam->mb_height;
1623            int coded = 1;
         if ((pEnc->current->vop_flags & XVID_REDUCED))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
         }  
1624    
1625            IMAGE *pRef = &reference->image;
1626    
1627            if (!reference->is_edged) {
1628          start_timer();          start_timer();
1629          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1630                                     pEnc->mbParam.width, pEnc->mbParam.height);                                             pParam->width, pParam->height, 0);
1631          stop_edges_timer();          stop_edges_timer();
1632                    reference->is_edged = 1;
1633            }
1634    
1635          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1636          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1637          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          current->fcode = pParam->m_fcode;
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
   
         if (!force_inter)  
                 iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);  
         else  
                 iLimit = mb_width * mb_height + 1;  
1638    
1639          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1640                    if (reference->is_interpolated != current->rounding_type) {
1641                  start_timer();                  start_timer();
1642                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1643                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                            pEnc->vInterHV.y, pParam->edged_width,
1644                                                    pEnc->mbParam.edged_height,                                                            pParam->edged_height,
1645                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1646                                                    pEnc->current->rounding_type);                                                            current->rounding_type);
1647                  stop_inter_timer();                  stop_inter_timer();
1648                            reference->is_interpolated = current->rounding_type;
1649                    }
1650          }          }
1651    
1652          pEnc->current->coding_type = P_VOP;          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1653                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1654                    current->sStat.iMVBits = 0;
1655    
1656            current->coding_type = P_VOP;
1657    
1658      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1659                    image_block_variance(&current->image, pParam->edged_width, current->mbs,
1660                                         pParam->mb_width, pParam->mb_height);
1661            }
1662    
1663            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1664    
1665            SetMacroblockQuants(&pEnc->mbParam, current);
1666    
1667          start_timer();          start_timer();
1668          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1669                  HintedMESet(pEnc, &bIntra);          {       int gmcval;
1670          else*/                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1671                  bIntra =                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1672                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
1673                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  if (current->motion_flags & XVID_ME_GME_REFINE) {
1674                           iLimit);                          gmcval = GlobalMotionEstRefine(&current->warp,
1675                                                                                       current->mbs, pParam,
1676                                                                                       current, reference,
1677                                                                                       &current->image,
1678                                                                                       &reference->image,
1679                                                                                       &pEnc->vInterH,
1680                                                                                       &pEnc->vInterV,
1681                                                                                       &pEnc->vInterHV);
1682                    } else {
1683                            gmcval = globalSAD(&current->warp, pParam, current->mbs,
1684                                                               current,
1685                                                               &reference->image,
1686                                                               &current->image,
1687                                                               pEnc->vGMC.y);
1688                    }
1689    
1690                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1691    
1692                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1693                    generate_GMCparameters( 3, 3, &current->warp,
1694                                    pParam->width, pParam->height,
1695                                    &current->new_gmc_data);
1696    
1697                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1698                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1699                    {
1700                            current->coding_type = S_VOP;
1701    
1702                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1703                                    pParam->mb_width, pParam->mb_height,
1704                                    pParam->edged_width, pParam->edged_width/2,
1705                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1706                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1707    
1708          stop_motion_timer();                  } else {
1709    
1710          if (bIntra == 1) return FrameCodeI(pEnc, bs);                          generate_GMCimage(&current->new_gmc_data, &reference->image,
1711                                    pParam->mb_width, pParam->mb_height,
1712                                    pParam->edged_width, pParam->edged_width/2,
1713                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1714                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1715                    }
1716            }
1717    
         if ( ( pEnc->current->vol_flags & XVID_GMC )  
                 && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )  
         {  
                 pEnc->current->coding_type = S_VOP;  
1718    
1719                  generate_GMCparameters( 2, 16, &pEnc->current->warp,          if (pEnc->num_threads > 0) {
1720                                          pEnc->mbParam.width, pEnc->mbParam.height,                  /* multithreaded motion estimation - dispatch threads */
1721                                          &pEnc->current->gmc_data);  
1722                    void * status;
1723                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,                  int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
                                 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_QUARTERPEL), 0,  
                                 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);  
1724    
1725                    for (k = 0; k < pEnc->num_threads; k++) {
1726                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
1727                            pEnc->motionData[k].pParam = &pEnc->mbParam;
1728                            pEnc->motionData[k].current = current;
1729                            pEnc->motionData[k].reference = reference;
1730                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
1731                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
1732                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
1733                            pEnc->motionData[k].pGMC = &pEnc->vGMC;
1734                            pEnc->motionData[k].y_step = pEnc->num_threads;
1735                            pEnc->motionData[k].start_y = k;
1736                            /* todo: sort out temp space once and for all */
1737                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width;
1738          }          }
1739    
1740          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  for (k = 1; k < pEnc->num_threads; k++) {
1741          if (vol_header)                          pthread_create(&pEnc->motionData[k].handle, NULL,
1742          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);                                  (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]);
                 BitstreamPadAlways(bs);  
1743          }          }
1744    
1745          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);                  MotionEstimateSMP(&pEnc->motionData[0]);
1746    
1747          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =                  for (k = 1; k < pEnc->num_threads; k++) {
1748                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                          pthread_join(pEnc->motionData[k].handle, &status);
1749                    }
1750    
1751                    current->fcode = 0;
1752                    for (k = 0; k < pEnc->num_threads; k++) {
1753                            current->sStat.iMvSum += pEnc->motionData[k].mvSum;
1754                            current->sStat.iMvCount += pEnc->motionData[k].mvCount;
1755                            if (pEnc->motionData[k].minfcode > current->fcode)
1756                                    current->fcode = pEnc->motionData[k].minfcode;
1757                    }
1758    
1759          for (y = 0; y < mb_height; y++) {          } else {
1760                  for (x = 0; x < mb_width; x++) {                  /* regular ME */
1761                          MACROBLOCK *pMB =  
1762                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                  MotionEstimation(&pEnc->mbParam, current, reference,
1763                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1764                                                     &pEnc->vGMC, 256*4096);
1765            }
1766    
1767            stop_motion_timer();
1768    
1769            set_timecodes(current,reference,pParam->fbase);
1770    
1771  /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
 /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */  
1772    
1773                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);          for (y = 0; y < mb_height; y++) {
1774                    for (x = 0; x < mb_width; x++) {
1775                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1776                            int skip_possible;
1777    
1778                          if (bIntra) {                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1779                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1780                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1781                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1782    
1783                                  start_timer();                                  start_timer();
1784                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1785                                  stop_prediction_timer();                                  stop_prediction_timer();
1786    
1787                                  pEnc->current->sStat.kblks++;                                  current->sStat.kblks++;
1788    
1789                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1790                                  stop_coding_timer();                                  stop_coding_timer();
1791                                  continue;                                  continue;
1792                          }                          }
1793    
                         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 & PMV_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_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 */  
                         }  
   
1794                          start_timer();                          start_timer();
1795                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1796                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1797                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1798                                                                   &pEnc->current->image,                                                                   &current->image,
1799                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1800                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1801                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1802                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1803                                                                   (pEnc->current->vop_flags & XVID_REDUCED),                                                                   current->rounding_type);
                                                                  pEnc->current->rounding_type);  
1804    
1805                          stop_comp_timer();                          stop_comp_timer();
1806    
                         if (pMB->dquant != 0) {  
                 pMB->mode = MODE_INTER_Q;  
                         }  
   
1807                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1808    
1809                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1810                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,  
1811                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1812                          }                          }
1813    
1814                            if (pMB->dquant != 0)
1815                                    MBSetDquant(pMB, x, y, &pEnc->mbParam);
1816    
1817    
1818                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1819                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1820                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1821                                  pEnc->current->sStat.mblks++;                                  current->sStat.mblks++;
1822                          }  else {                          }  else {
1823                                  pEnc->current->sStat.ublks++;                                  current->sStat.ublks++;
1824                          }                          }
1825    
1826                          start_timer();                          start_timer();
1827    
1828                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1829    
1830                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                                                         (pMB->dquant == 0);  
1831    
1832                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1833                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1834                          else if (pEnc->current->coding_type == P_VOP) {                          else { /* PVOP */
1835                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1836                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                                                                  pMB->qmvs : pMB->mvs;
1837                                  else                                  skip_possible &= ((mv->x|mv->y) == 0);
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
1838                          }                          }
1839    
1840                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
   
1841  /* 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 */  
                                 {  
1842                                          int bSkip = 1;                                          int bSkip = 1;
1843    
1844                                          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;  
                                                 }  
                                         }  
1845    
1846                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1847                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                                  int iSAD;
1848                                                          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,
1849                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1850                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1851                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1852                                                            bSkip = 0; /* could not SKIP */
1853                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1854                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1855                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1856                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1857                                                  }                                                          } else {
1858                                                  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);  
1859                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1860                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1861                                                  }                                                  }
1862                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1863                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1864                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                          break;
1865                                                  stop_coding_timer();                                                  }
   
                                                 continue;       /* next MB */  
1866                                          }                                          }
1867                                  }                                  }
                                 /* do SKIP */  
1868    
1869                                    if (bSkip) {
1870                                            /* do SKIP */
1871                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1872                                  MBSkip(bs);                                  MBSkip(bs);
1873                                  stop_coding_timer();                                  stop_coding_timer();
1874                                  continue;       /* next MB */                                  continue;       /* next MB */
1875                          }                          }
                         /* ordinary case: normal coded INTER/INTER4V block */  
   
                         if ((pEnc->current->vop_flags & XVID_GREYSCALE))  
                         {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                 qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
   
                         if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {  
                                 VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;  
                                 DPRINTF(DPRINTF_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(DPRINTF_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_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(DPRINTF_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(DPRINTF_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);  
                                         }  
   
                                 }  
1876                          }                          }
1877    
1878                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          /* ordinary case: normal coded INTER/INTER4V block */
1879                            MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1880                          stop_coding_timer();                          stop_coding_timer();
   
1881                  }                  }
1882          }          }
1883    
         if ((pEnc->current->vop_flags & XVID_REDUCED))  
         {  
                 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,  
                         16, 0);  
         }  
   
1884          emms();          emms();
1885            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_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_QUARTERPEL?1:0) )))        /* minimum search range 16 */  
         {  
                 pEnc->mbParam.m_fcode--;  
                 iSearchRange /= 2;  
         }  
   
         pEnc->fMvPrevSigma = fSigma;  
1886    
1887          /* frame drop code */          /* frame drop code */
1888          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
1889          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);
1890                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)  #endif
1891            if (current->sStat.kblks + current->sStat.mblks <
1892                    (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
1893                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
1894          {          {
1895                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
1896                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1897    
1898                  BitstreamReset(bs);                  BitstreamReset(bs);
1899    
1900                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
1901                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1902    
1903                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1904                  pEnc->current->quant = pEnc->reference->quant;                  current->quant = reference->quant;
1905                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  current->motion_flags = reference->motion_flags;
1906                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1907                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  current->fcode = reference->fcode;
1908                  pEnc->current->fcode = pEnc->reference->fcode;                  current->bcode = reference->bcode;
1909                  pEnc->current->bcode = pEnc->reference->bcode;                  current->stamp = reference->stamp;
1910                  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);
1911                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1912                    coded = 0;
1913    
1914            } else {
1915    
1916                    pEnc->current->is_edged = 0; /* not edged */
1917                    pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1918    
1919                    /* what was this frame's interpolated reference will become
1920                            forward (past) reference in b-frame coding */
1921    
1922                    image_swap(&pEnc->vInterH, &pEnc->f_refh);
1923                    image_swap(&pEnc->vInterV, &pEnc->f_refv);
1924                    image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1925          }          }
1926    
1927          /* XXX: debug          /* XXX: debug
1928          {          {
1929                  char s[100];                  char s[100];
1930                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1931                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
1932                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1933                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1934    
1935                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1936                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
1937                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1938                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1939          }          }
1940          */          */
1941    
1942          /* for divx5 compatibility, we must always pad between the packed p and b frames */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
         if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)  
                 BitstreamPadAlways(bs);  
         else  
                 BitstreamPad(bs);  
1943    
1944      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          current->length = (BitstreamPos(bs) - bits) / 8;
1945    
1946          return 0;                                       /* inter */          return coded;
1947  }  }
1948    
1949    
# Line 1775  Line 1967 
1967                  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); \
1968          }          }
1969    
         /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */  
   
1970          if (!first){          if (!first){
1971                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1972          }          }
1973  #endif  #endif
1974    
         //frame->quarterpel =  pEnc->mbParam.m_quarterpel;  
   
1975          /* forward  */          /* forward  */
1976            if (!pEnc->reference->is_edged) {
1977          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1978                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1979                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1980                    pEnc->current->is_edged = 1;
1981            }
1982    
1983            if (pEnc->reference->is_interpolated != 0) {
1984          start_timer();          start_timer();
1985          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,
1986                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1987                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1988          stop_inter_timer();          stop_inter_timer();
1989                    pEnc->reference->is_interpolated = 0;
1990            }
1991    
1992          /* backward */          /* backward */
1993            if (!pEnc->current->is_edged) {
1994          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1995                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1996                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1997                    pEnc->current->is_edged = 1;
1998            }
1999    
2000            if (pEnc->current->is_interpolated != 0) {
2001          start_timer();          start_timer();
2002          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
2003                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2004                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2005          stop_inter_timer();          stop_inter_timer();
2006                    pEnc->current->is_interpolated = 0;
2007            }
2008    
2009          start_timer();          frame->coding_type = B_VOP;
2010    
2011            if (pEnc->current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
2012                    image_block_variance(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->current->mbs,
2013                                         pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2014            }
2015    
2016            call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2017    
2018            frame->fcode = frame->bcode = pEnc->current->fcode;
2019    
2020            start_timer();
2021            if (pEnc->num_threads > 0) {
2022                    void * status;
2023                    int k;
2024                    /* multithreaded motion estimation - dispatch threads */
2025                    int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
2026    
2027                    for (k = 0; k < pEnc->num_threads; k++) {
2028                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
2029                            pEnc->motionData[k].pParam = &pEnc->mbParam;
2030                            pEnc->motionData[k].current = frame;
2031                            pEnc->motionData[k].reference = pEnc->current;
2032                            pEnc->motionData[k].fRef = f_ref;
2033                            pEnc->motionData[k].fRefH = &pEnc->f_refh;
2034                            pEnc->motionData[k].fRefV = &pEnc->f_refv;
2035                            pEnc->motionData[k].fRefHV = &pEnc->f_refhv;
2036                            pEnc->motionData[k].pRef = b_ref;
2037                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
2038                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
2039                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
2040                            pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);
2041                            pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);
2042                            pEnc->motionData[k].y_step = pEnc->num_threads;
2043                            pEnc->motionData[k].start_y = k;
2044                            /* todo: sort out temp space once and for all */
2045                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width;
2046                    }
2047    
2048                    for (k = 1; k < pEnc->num_threads; k++) {
2049                            pthread_create(&pEnc->motionData[k].handle, NULL,
2050                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]);
2051                    }
2052    
2053                    SMPMotionEstimationBVOP(&pEnc->motionData[0]);
2054    
2055                    for (k = 1; k < pEnc->num_threads; k++) {
2056                            pthread_join(pEnc->motionData[k].handle, &status);
2057                    }
2058    
2059                    frame->fcode = frame->bcode = 0;
2060                    for (k = 0; k < pEnc->num_threads; k++) {
2061                            if (pEnc->motionData[k].minfcode > frame->fcode)
2062                                    frame->fcode = pEnc->motionData[k].minfcode;
2063                            if (pEnc->motionData[k].minbcode > frame->bcode)
2064                                    frame->bcode = pEnc->motionData[k].minbcode;
2065                    }
2066            } else {
2067          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2068                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2069                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
# Line 1813  Line 2071 
2071                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2072                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2073                                                   &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);  
2074          }          }
2075          */          stop_motion_timer();
   
         frame->coding_type = B_VOP;  
2076    
2077          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2078          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2079    
2080          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2081            frame->sStat.iMVBits = 0;
2082          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2083          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2084          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
# Line 1838  Line 2088 
2088          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2089                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2090                          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_ALTERNATESCAN ? 2 : 0;  
2091    
2092                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2093                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2094                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2095                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2096                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
2097                                    }
2098                                  continue;                                  continue;
2099                          }                          }
2100    
2101                          if (mb->mode != MODE_DIRECT_NONE_MV) {                          mb->quant = frame->quant;
2102    
2103                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2104                                    /* we have to motion-compensate, transfer etc,
2105                                            because there might be blocks to code */
2106    
2107                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2108                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2109                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2110                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
2111                                                                           dct_codes);                                                                           dct_codes);
2112    
2113                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                                  mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
2114                                  mb->quant = frame->quant;                          }
2115    
2116                                  mb->cbp =                          if (mb->mode == MODE_DIRECT_NO4V)
2117                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                  mb->mode = MODE_DIRECT;
2118    
2119                                  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) ) {  
2120                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2121                                  }                          else
2122                          }                                  if (frame->vop_flags & XVID_VOP_GREYSCALE)
2123                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2124                                            mb->cbp &= 0x3C;
2125    
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
2126                          start_timer();                          start_timer();
2127                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
2128                                                   &frame->sStat, direction);                                                   &frame->sStat);
2129                          stop_coding_timer();                          stop_coding_timer();
2130                  }                  }
2131          }          }
   
2132          emms();          emms();
2133    
2134          /* TODO: dynamic fcode/bcode ??? */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
   
     BitstreamPad(bs);  
2135          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
2136    
2137  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.10  
changed lines
  Added in v.1.133

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