[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.11, Sat Mar 22 13:49:49 2003 UTC revision 1.95.2.54, Wed Nov 19 15:42:38 2003 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002     Michael Militzer <isibaar@xvid.org>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *                         2002-2003 Peter Ross <pross@xvid.org>
8   *  to use this software module in hardware or software products are   *                         2002   Daniel Smith <danielsmith@astroboymail.com>
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 43  Line 38 
38  #include "image/font.h"  #include "image/font.h"
39  #include "motion/sad.h"  #include "motion/sad.h"
40  #include "motion/motion.h"  #include "motion/motion.h"
41    #include "motion/gmc.h"
42    
43  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
44  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
45  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
47  #include "utils/emms.h"  #include "utils/emms.h"
48  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "quant/adapt_quant.h"  
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
# Line 116  Line 112 
112    
113    
114  int  int
115  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)  enc_create(xvid_enc_create_t * create)
116  {  {
117          Encoder *pEnc;          Encoder *pEnc;
118      int n;      int n;
119    
120          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 */
121                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
122    
123          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
# Line 134  Line 130 
130                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
131          memset(pEnc, 0, sizeof(Encoder));          memset(pEnc, 0, sizeof(Encoder));
132    
133            pEnc->mbParam.profile = create->profile;
134    
135          /* global flags */          /* global flags */
136      pEnc->mbParam.global_flags = create->global;      pEnc->mbParam.global_flags = create->global;
137    
# Line 151  Line 149 
149      if (pEnc->mbParam.fincr>0)      if (pEnc->mbParam.fincr>0)
150              simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);              simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
151    
152      /* plugin */          /* zones */
153            if(create->num_zones > 0) {
154                    pEnc->num_zones = create->num_zones;
155                    pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
156                    if (pEnc->zones == NULL)
157                            goto xvid_err_memory0;
158                    memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
159            } else {
160                    pEnc->num_zones = 0;
161                    pEnc->zones = NULL;
162            }
163    
164            /* plugins */
165            if(create->num_plugins > 0) {
166      pEnc->num_plugins = create->num_plugins;      pEnc->num_plugins = create->num_plugins;
167      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);
168      if (pEnc->plugins == NULL)      if (pEnc->plugins == NULL)
169          goto xvid_err_memory0;          goto xvid_err_memory0;
170            } else {
171                    pEnc->num_plugins = 0;
172                    pEnc->plugins = NULL;
173            }
174    
175      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
176          xvid_plg_create_t pcreate;          xvid_plg_create_t pcreate;
# Line 169  Line 184 
184    
185          memset(&pcreate, 0, sizeof(xvid_plg_create_t));          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
186          pcreate.version = XVID_VERSION;          pcreate.version = XVID_VERSION;
187                    pcreate.num_zones = pEnc->num_zones;
188                    pcreate.zones = pEnc->zones;
189          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
190          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
191                    pcreate.mb_width = pEnc->mbParam.mb_width;
192                    pcreate.mb_height = pEnc->mbParam.mb_height;
193          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
194          pcreate.fbase = pEnc->mbParam.fbase;          pcreate.fbase = pEnc->mbParam.fbase;
195          pcreate.param = create->plugins[n].param;          pcreate.param = create->plugins[n].param;
# Line 181  Line 200 
200          }          }
201      }      }
202    
203      if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) ||          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
204          (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {          (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
205          pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */          pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
206      }      }
# Line 190  Line 209 
209      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
210              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
211                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
212                    if (pEnc->temp_dquants==NULL)
213                            goto xvid_err_memory1a;
214      }      }
     /* XXX: error checking */  
215    
216          /* bframes */          /* bframes */
217          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
218          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
219          pEnc->mbParam.bquant_offset = create->bquant_offset;          pEnc->mbParam.bquant_offset = create->bquant_offset;
220    
221            /* min/max quant */
222            for (n=0; n<3; n++) {
223                    pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
224                    pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
225            }
226    
227          /* frame drop ratio */          /* frame drop ratio */
228          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
229    
230      /* max keyframe interval */      /* max keyframe interval */
231      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 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; */  
   
232    
233      /* allocate working frame-image memory */      /* allocate working frame-image memory */
234    
# Line 252  Line 265 
265          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
266          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
267          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
268          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
269    
270          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
271          if (image_create          if (image_create
# Line 298  Line 309 
309                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
310                  goto xvid_err_memory3;                  goto xvid_err_memory3;
311          if (image_create          if (image_create
                 (&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create  
312                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
313                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
314                  goto xvid_err_memory3;                  goto xvid_err_memory3;
         if (image_create  
                 (&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
315    
316  /* Create full bitplane for GMC, this might be wasteful */  /* Create full bitplane for GMC, this might be wasteful */
317          if (image_create          if (image_create
# Line 378  Line 381 
381                  image_null(&pEnc->queue[n].image);                  image_null(&pEnc->queue[n].image);
382    
383    
384          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
         {  
385                  if (image_create                  if (image_create
386                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
387                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
388                          goto xvid_err_memory5;                          goto xvid_err_memory5;
   
389          }          }
390    
   
391          /* timestamp stuff */          /* timestamp stuff */
392    
393          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
# Line 412  Line 412 
412    
413    xvid_err_memory5:    xvid_err_memory5:
414    
415          if (pEnc->mbParam.max_bframes > 0) {          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
416          int i;                          image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
   
                 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {  
                         image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,  
417                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
418                  }                  }
419    
420                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
421    
422    xvid_err_memory4:    xvid_err_memory4:
423    
# Line 434  Line 431 
431    
432                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
433                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
434                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
435                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
436                  }                  }
437    
438                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 468  Line 462 
462                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
463          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
464                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
465          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
466                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
467    
468  /* destroy GMC image */  /* destroy GMC image */
469          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
# Line 488  Line 478 
478          xvid_free(pEnc->current);          xvid_free(pEnc->current);
479          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
480    
481      xvid_err_memory1a:
482      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
483              xvid_free(pEnc->temp_dquants);              xvid_free(pEnc->temp_dquants);
484      }      }
# Line 500  Line 491 
491      }      }
492      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
493    
494            xvid_free(pEnc->zones);
495    
496          xvid_free(pEnc);          xvid_free(pEnc);
497    
498          create->handle = NULL;          create->handle = NULL;
# Line 524  Line 517 
517          int i;          int i;
518    
519          /* B Frames specific */          /* B Frames specific */
         if (pEnc->mbParam.max_bframes > 0) {  
   
520                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
   
521                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
522                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
523                  }                  }
                 xvid_free(pEnc->queue);  
         }  
524    
525            xvid_free(pEnc->queue);
526    
527          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
528    
# Line 544  Line 533 
533    
534                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
535                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
536                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
537                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
538                  }                  }
539    
# Line 564  Line 551 
551                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
552          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
553                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
554          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
555                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
   
556          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
557                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
558          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
559                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
560          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
561                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
562            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
563                                      pEnc->mbParam.edged_height);
564    
565          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
566                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
# Line 597  Line 581 
581          xvid_free(pEnc->temp_dquants);          xvid_free(pEnc->temp_dquants);
582      }      }
583    
584    
585            if (pEnc->num_plugins>0) {
586                    xvid_plg_destroy_t pdestroy;
587                    memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
588    
589                    pdestroy.version = XVID_VERSION;
590                    pdestroy.num_frames = pEnc->m_framenum;
591    
592      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
593          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
594              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, 0);
595          }          }
596      }      }
597      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
598            }
599    
600            if (pEnc->num_plugins>0)
601                    xvid_free(pEnc->zones);
602    
603          xvid_free(pEnc);          xvid_free(pEnc);
604    
# Line 625  Line 621 
621      memset(&data, 0, sizeof(xvid_plg_data_t));      memset(&data, 0, sizeof(xvid_plg_data_t));
622      data.version = XVID_VERSION;      data.version = XVID_VERSION;
623    
624            /* find zone */
625            for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
626            data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
627    
628      data.width = pEnc->mbParam.width;      data.width = pEnc->mbParam.width;
629      data.height = pEnc->mbParam.height;      data.height = pEnc->mbParam.height;
630      data.mb_width = pEnc->mbParam.mb_width;      data.mb_width = pEnc->mbParam.mb_width;
631      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
632      data.fincr = frame->fincr;      data.fincr = frame->fincr;
633      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
634            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
635            data.bquant_offset = pEnc->mbParam.bquant_offset;
636    
637            for (i=0; i<3; i++) {
638                    data.min_quant[i] = pEnc->mbParam.min_quant[i];
639                    data.max_quant[i] = pEnc->mbParam.max_quant[i];
640            }
641    
642      data.reference.csp = XVID_CSP_USER;      data.reference.csp = XVID_CSP_USER;
643      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
# Line 651  Line 658 
658      data.frame_num = frame->frame_num;      data.frame_num = frame->frame_num;
659    
660      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
661          data.type = XVID_TYPE_AUTO;                  data.type = *type;
662          data.quant = 2;                  data.quant = *quant;
663    
664                    data.vol_flags = frame->vol_flags;
665                    data.vop_flags = frame->vop_flags;
666                    data.motion_flags = frame->motion_flags;
667    
668            } else if (opt == XVID_PLG_FRAME) {
669                    data.type = coding2type(frame->coding_type);
670                    data.quant = frame->quant;
671    
672          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
673              data.dquant = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
# Line 660  Line 675 
675              memset(data.dquant, 0, data.mb_width*data.mb_height);              memset(data.dquant, 0, data.mb_width*data.mb_height);
676          }          }
677    
678          /* todo: [vol,vop,motion]_flags*/          } else { /* XVID_PLG_AFTER */
   
     } else { // XVID_PLG_AFTER  
679          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
680              data.original.csp = XVID_CSP_USER;              data.original.csp = XVID_CSP_USER;
681              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
# Line 673  Line 686 
686              data.original.stride[2] = pEnc->mbParam.edged_width/2;              data.original.stride[2] = pEnc->mbParam.edged_width/2;
687          }          }
688    
689          if ((frame->vol_flags & XVID_EXTRASTATS) ||                  if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
690              (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {              (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
691    
692                          data.sse_y =                          data.sse_y =
# Line 701  Line 714 
714    
715              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
716              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
717                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;                                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
718              }              }
719          }          }
720    
# Line 731  Line 744 
744      }      }
745    
746      /* call plugins */      /* call plugins */
747      for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
748          emms();          emms();
749          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
750              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
# Line 744  Line 757 
757      /* copy modified values back into frame*/      /* copy modified values back into frame*/
758      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
759          *type = data.type;          *type = data.type;
760          *quant = data.quant;                  *quant = data.quant > 0 ? data.quant : 2;   /* default */
761    
762                    frame->vol_flags = data.vol_flags;
763                    frame->vop_flags = data.vop_flags;
764                    frame->motion_flags = data.motion_flags;
765    
766            } else if (opt == XVID_PLG_FRAME) {
767    
768          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
769              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
# Line 757  Line 776 
776                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
777              }              }
778          }          }
779          /* todo: [vol,vop,motion]_flags*/                  frame->mbs[0].quant = data.quant; /* BEFORE2 will not affect the quant in stats */
780      }      }
781    
782    
783  }  }
784    
785    
# Line 824  Line 845 
845          int type;          int type;
846          Bitstream bs;          Bitstream bs;
847    
848          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))     /* v1.x.x */
849                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
850    
851          xFrame->out_flags = 0;          xFrame->out_flags = 0;
# Line 845  Line 866 
866                  if (image_input                  if (image_input
867                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
868                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
869                          xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))                          xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
870                  {                  {
871                          emms();                          emms();
872                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
873                  }                  }
874                  stop_conv_timer();                  stop_conv_timer();
875    
876                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {                  if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
877                          image_chroma_optimize(&q->image,                          image_chroma_optimize(&q->image,
878                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
879                  }                  }
# Line 861  Line 882 
882    
883                  if (xFrame->quant_intra_matrix)                  if (xFrame->quant_intra_matrix)
884                  {                  {
885                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
886                          q->frame.quant_intra_matrix = q->quant_intra_matrix;                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
887                  }                  }
888    
889                  if (xFrame->quant_inter_matrix)                  if (xFrame->quant_inter_matrix)
890                  {                  {
891                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
892                          q->frame.quant_inter_matrix = q->quant_inter_matrix;                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
893                  }                  }
894    
# Line 886  Line 907 
907          {          {
908                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
909    
910                          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",
911                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
912                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
913    
# Line 909  Line 930 
930                     indentical to the future-referece frame.                     indentical to the future-referece frame.
931                  */                  */
932    
933                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
934                          int tmp;                          int tmp;
935                          int bits;                          int bits;
936    
937                          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",
938                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
939                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
940    
# Line 922  Line 943 
943                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
944                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
945    
946                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
947                          BitstreamPad(&bs);                          BitstreamPad(&bs);
948                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
949    
# Line 951  Line 972 
972                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
973                  {                  {
974    
975              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",
976                                    pEnc->bframenum_head, pEnc->bframenum_tail,
977                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
978    
979                            if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
980                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
981              }              }
982    
983              /* 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 */
984              if (pEnc->bframenum_tail > 0)                          if (pEnc->bframenum_tail > 0) {
                         {  
985    
986                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
987                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
988                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
989    
990                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
991                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
992                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
993                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
994    
995                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
996                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
997                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
998                  }                  }
999    
1000                                    DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1001                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1002                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1003    
1004                                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs, 1, 0);
1005    
1006                                  goto done_flush;  
1007                                    if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1008                                            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1009                                    }else{
1010                                            pEnc->flush_bframes = 1;
1011                                            goto done;
1012                          }                          }
1013                            }
1014                            DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1015    
1016                          emms();                          emms();
1017                          return XVID_ERR_END;    /* end of stream reached */                          return XVID_ERR_END;    /* end of stream reached */
# Line 982  Line 1019 
1019                  goto done;      /* nothing to encode yet; encoder lag */                  goto done;      /* nothing to encode yet; encoder lag */
1020          }          }
1021    
1022          // the current FRAME becomes the reference          /* the current FRAME becomes the reference */
1023          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1024    
1025          // remove frame from encoding-queue (head), and move it into the current          /* remove frame from encoding-queue (head), and move it into the current */
1026          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1027          frame = &pEnc->queue[pEnc->queue_head].frame;          frame = &pEnc->queue[pEnc->queue_head].frame;
1028          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 1034 
1034           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1035    
1036      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1037            inc_frame_num(pEnc);
1038      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
1039      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1040          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1041          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1042          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
1043    
1044      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {  
1045            if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1046              image_chroma_optimize(&pEnc->current->image,              image_chroma_optimize(&pEnc->current->image,
1047                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1048      }      }
# Line 1012  Line 1051 
1051           * frame type & quant selection           * frame type & quant selection
1052           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1053    
     call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);  
   
     if (frame->type > 0)  
1054                  type = frame->type;                  type = frame->type;
   
     if (frame->quant > 0)  
1055                  pEnc->current->quant = frame->quant;                  pEnc->current->quant = frame->quant;
1056    
1057            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);
1058    
1059      if (type > 0){      /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
1060                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1061      } else{             /* XVID_TYPE_AUTO */      } else{             /* XVID_TYPE_AUTO */
# Line 1029  Line 1065 
1065                  }else{                  }else{
1066                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1067                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1068                                          pEnc->iFrameNum, pEnc->bframenum_tail);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1069                                                              (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 */  
             }  
1070                  }                  }
1071          }          }
1072    
1073      /* bframes buffer overflow check */      /* bframes buffer overflow check */
1074      if (type != I_VOP) {          if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
         if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
1075              type = P_VOP;              type = P_VOP;
         }else{  
             type = B_VOP;  
         }  
1076      }      }
1077    
         inc_frame_num(pEnc);  
1078          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1079    
1080          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1081                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1082                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                          "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1083          }          }
1084    
1085          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1059  Line 1087 
1087       * (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)
1088           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1089          if (type == B_VOP) {          if (type == B_VOP) {
1090                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1091                          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");
1092                  }                  }
1093    
# Line 1076  Line 1104 
1104                  else if (pEnc->current->quant > 31)                  else if (pEnc->current->quant > 31)
1105              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1106    
1107                  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",
1108                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1109                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1110    
# Line 1089  Line 1117 
1117                  goto repeat;                  goto repeat;
1118      }      }
1119    
1120    
1121                    DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1122                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1123                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1124    
1125      /* for unpacked bframes, output the stats for the last encoded frame */      /* for unpacked bframes, output the stats for the last encoded frame */
1126      if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)          if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1127      {      {
1128          if (pEnc->current->stamp > 0) {          if (pEnc->current->stamp > 0) {
1129              call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);              call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
# Line 1104  Line 1137 
1137       * 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
1138       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1139    
1140      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) {
1141    
1142                  // place this frame back on the encoding-queue (head)                  /* place this frame back on the encoding-queue (head) */
1143                  // we will deal with it next time                  /* we will deal with it next time */
1144          dec_frame_num(pEnc);          dec_frame_num(pEnc);
1145          pEnc->iFrameNum--;          pEnc->iFrameNum--;
1146    
# Line 1115  Line 1148 
1148          pEnc->queue_size++;          pEnc->queue_size++;
1149                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1150    
1151                  // grab the last frame from the bframe-queue                  /* grab the last frame from the bframe-queue */
1152    
1153                  pEnc->bframenum_tail--;                  pEnc->bframenum_tail--;
1154                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1155    
1156                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1157                          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, "DX50 BVOP->PVOP");
1158                  }                  }
1159    
1160                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1161                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1162                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1163                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1164          type = P_VOP;          type = P_VOP;
1165      }      }
1166    
# Line 1136  Line 1171 
1171    
1172          if (type == I_VOP) {          if (type == I_VOP) {
1173    
1174                  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",
1175                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1176                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1177    
1178                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1179                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1180                  }                  }
1181    
1182                    pEnc->iFrameNum = 1;
1183    
1184                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1185                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1186                    switch(frame->par) {
1187                    case XVID_PAR_11_VGA:
1188                    case XVID_PAR_43_PAL:
1189                    case XVID_PAR_43_NTSC:
1190                    case XVID_PAR_169_PAL:
1191                    case XVID_PAR_169_NTSC:
1192                    case XVID_PAR_EXT:
1193                            pEnc->mbParam.par = frame->par;
1194                            break;
1195                    default:
1196                            pEnc->mbParam.par = XVID_PAR_EXT;
1197                            break;
1198                    }
1199                    pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1;
1200                    pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1;
1201    
1202          if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {                  if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1203                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1204                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(frame->quant_intra_matrix);
1205                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
# Line 1157  Line 1208 
1208    
1209          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1210    
1211          if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))                  if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))
1212              pEnc->current->vop_flags &= ~XVID_REDUCED;                          pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;
1213    
1214          if (!(pEnc->current->vol_flags & XVID_INTERLACING))                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1215              pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1216    
1217                  /* ^^^------------------------ */                  /* ^^^------------------------ */
1218    
# Line 1177  Line 1228 
1228           * encode this frame as an p-vop           * encode this frame as an p-vop
1229       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1230    
1231      } else { // (type == P_VOP || type == S_VOP)          } else { /* (type == P_VOP || type == S_VOP) */
1232    
1233                  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",
1234                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1235                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1236    
1237                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1238                          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");
1239                  }                  }
1240    
# Line 1200  Line 1251 
1251           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1252           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1253    
1254  done_flush:  /*done_flush:*/
1255    
1256      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1257    
1258          /* 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 */
1259          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1260                  goto repeat;                  goto repeat;
1261          }          }
1262    
1263      /* packed or no-bframes: output stats */          /* packed or no-bframes or no-bframes-queued: output stats */
1264      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 ) {
1265          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1266          }          }
1267    
# Line 1230  Line 1281 
1281    
1282  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1283  {  {
1284      unsigned int i,j;          unsigned int i;
1285      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1286            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1287            if (quant > 31)
1288                    frame->quant = quant = 31;
1289            else if (quant < 1)
1290                    frame->quant = quant = 1;
1291    
1292      for (j=0; j<pParam->mb_height; j++)          for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
     for (i=0; i<pParam->mb_width; i++) {  
         MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];  
1293          quant += pMB->dquant;          quant += pMB->dquant;
1294          if (quant > 31)          if (quant > 31)
1295                          quant = 31;                          quant = 31;
1296                  if (quant < 1)                  else if (quant < 1)
1297                          quant = 1;                          quant = 1;
1298          pMB->quant = quant;          pMB->quant = quant;
1299                    pMB++;
1300      }      }
1301  }  }
1302    
# Line 1279  Line 1334 
1334    
1335          uint16_t x, y;          uint16_t x, y;
1336    
1337          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))          if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))
1338          {          {
1339                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1340                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1297  Line 1352 
1352          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1353          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1354    
1355            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1356    
1357      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1358    
1359          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1360    
1361          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1362    
1363          BitstreamPadAlways(bs);          BitstreamPad(bs);
1364          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1365            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1366    
1367          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1368          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
# Line 1325  Line 1383 
1383                          stop_prediction_timer();                          stop_prediction_timer();
1384    
1385                          start_timer();                          start_timer();
1386                          if (pEnc->current->vop_flags & XVID_GREYSCALE)                          if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1387                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1388                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1389                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
# Line 1334  Line 1392 
1392                          stop_coding_timer();                          stop_coding_timer();
1393                  }                  }
1394    
1395          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))
1396          {          {
1397                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1398                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
# Line 1342  Line 1400 
1400          }          }
1401          emms();          emms();
1402    
1403          /* for divx5 compatibility, we must always pad between the packed p and b frames */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1404          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)  
                 BitstreamPadAlways(bs);  
         else  
                 BitstreamPad(bs);  
1405      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1406    
1407          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1408          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1409    
1410      /* XXX: hinted me          pEnc->current->is_edged = 0; /* not edged */
1411          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
                 HintedMEGet(pEnc, 1);  
         }*/  
1412    
1413          return 1;                                       /* intra */          return 1;                                       /* intra */
1414  }  }
# Line 1378  Line 1431 
1431          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1432          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1433    
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
1434          int iLimit;          int iLimit;
1435          int x, y, k;          int x, y, k;
1436          int iSearchRange;          int iSearchRange;
1437          int bIntra, skip_possible;          int bIntra=0, skip_possible;
1438            FRAMEINFO *const current = pEnc->current;
1439            FRAMEINFO *const reference = pEnc->reference;
1440            MBParam * const pParam = &pEnc->mbParam;
1441            int mb_width = pParam->mb_width;
1442            int mb_height = pParam->mb_height;
1443    
         /* IMAGE *pCurrent = &pEnc->current->image; */  
         IMAGE *pRef = &pEnc->reference->image;  
1444    
1445          if ((pEnc->current->vop_flags & XVID_REDUCED))          /* IMAGE *pCurrent = &current->image; */
1446            IMAGE *pRef = &reference->image;
1447    
1448            if ((current->vop_flags & XVID_VOP_REDUCED))
1449          {          {
1450                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
1451                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pParam->height + 31) / 32;
1452          }          }
1453    
1454    
1455            if (!reference->is_edged) {
1456          start_timer();          start_timer();
1457          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1458                                     pEnc->mbParam.width, pEnc->mbParam.height);                                             pParam->width, pParam->height);
1459          stop_edges_timer();          stop_edges_timer();
1460                    reference->is_edged = 1;
1461            }
1462    
1463          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1464          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1465          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          current->fcode = pParam->m_fcode;
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
1466    
1467          if (!force_inter)          if (!force_inter)
1468                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
1469          else          else
1470                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1471    
1472          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1473                    if (reference->is_interpolated != current->rounding_type) {
1474                  start_timer();                  start_timer();
1475                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1476                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                            &pEnc->vInterHV, pParam->edged_width,
1477                                                    pEnc->mbParam.edged_height,                                                            pParam->edged_height,
1478                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1479                                                    pEnc->current->rounding_type);                                                            current->rounding_type);
1480                  stop_inter_timer();                  stop_inter_timer();
1481                            reference->is_interpolated = current->rounding_type;
1482                    }
1483          }          }
1484    
1485          pEnc->current->coding_type = P_VOP;          current->coding_type = P_VOP;
1486    
1487            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1488    
1489      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          SetMacroblockQuants(&pEnc->mbParam, current);
1490    
1491          start_timer();          start_timer();
1492          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1493                  HintedMESet(pEnc, &bIntra);          {       int gmcval;
1494          else*/                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1495                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1496    
1497                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1498                            gmcval = GlobalMotionEstRefine(&current->warp,
1499                                                                                       current->mbs, pParam,
1500                                                                                       current, reference,
1501                                                                                       &current->image,
1502                                                                                       &reference->image,
1503                                                                                       &pEnc->vInterH,
1504                                                                                       &pEnc->vInterV,
1505                                                                                       &pEnc->vInterHV);
1506                    } else {
1507                            gmcval = globalSAD(&current->warp, pParam, current->mbs,
1508                                                               current,
1509                                                               &reference->image,
1510                                                               &current->image,
1511                                                               pEnc->vGMC.y);
1512                    }
1513    
1514                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1515    
1516                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1517                    generate_GMCparameters( 3, 3, &current->warp,
1518                                    pParam->width, pParam->height,
1519                                    &current->new_gmc_data);
1520    
1521                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1522                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1523                    {
1524                            current->coding_type = S_VOP;
1525    
1526                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1527                                    pParam->mb_width, pParam->mb_height,
1528                                    pParam->edged_width, pParam->edged_width/2,
1529                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1530                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1531    
1532                    } else {
1533    
1534                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1535                                    pParam->mb_width, pParam->mb_height,
1536                                    pParam->edged_width, pParam->edged_width/2,
1537                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1538                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1539                    }
1540            }
1541    
1542                  bIntra =                  bIntra =
1543                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                  MotionEstimation(&pEnc->mbParam, current, reference,
1544                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1545                           iLimit);                                           &pEnc->vGMC, iLimit);
1546    
1547    
1548          stop_motion_timer();          stop_motion_timer();
1549    
1550          if (bIntra == 1) return FrameCodeI(pEnc, bs);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1551    
1552          if ( ( pEnc->current->vol_flags & XVID_GMC )          set_timecodes(current,reference,pParam->fbase);
                 && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )  
         {  
                 pEnc->current->coding_type = S_VOP;  
   
                 generate_GMCparameters( 2, 16, &pEnc->current->warp,  
                                         pEnc->mbParam.width, pEnc->mbParam.height,  
                                         &pEnc->current->gmc_data);  
   
                 generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,  
                                 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,  
                                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,  
                                 pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,  
                                 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);  
   
         }  
   
         set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);  
1553          if (vol_header)          if (vol_header)
1554          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1555                  BitstreamPadAlways(bs);                  BitstreamPad(bs);
1556          }          }
1557    
1558          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1559    
1560          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1561                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1562    
1563    
1564          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1565                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1566                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1567                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &current->mbs[x + y * pParam->mb_width];
   
 /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */  
 /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */  
1568    
1569                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1570    
1571                          if (bIntra) {                          if (bIntra) {
1572                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1573                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1574                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1575    
1576                                  start_timer();                                  start_timer();
1577                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1578                                  stop_prediction_timer();                                  stop_prediction_timer();
1579    
1580                                  pEnc->current->sStat.kblks++;                                  current->sStat.kblks++;
1581    
1582                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1583                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1584                                            qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1585                                            qcoeff[5*64+0]=0;
1586                                    }
1587                                    MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1588                                  stop_coding_timer();                                  stop_coding_timer();
1589                                  continue;                                  continue;
1590                          }                          }
1591    
                         if (pEnc->current->coding_type == S_VOP) {  
   
                                 int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->mbParam.edged_width, 65536);  
   
                                 if (pEnc->current->motion_flags & 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 */  
                         }  
   
1592                          start_timer();                          start_timer();
1593                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1594                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1595                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1596                                                                   &pEnc->current->image,                                                                   &current->image,
1597                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1598                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1599                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1600                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1601                                                                   (pEnc->current->vop_flags & XVID_REDUCED),                                                                   (current->vop_flags & XVID_VOP_REDUCED),
1602                                                                   pEnc->current->rounding_type);                                                                   current->rounding_type);
1603    
1604                          stop_comp_timer();                          stop_comp_timer();
1605    
# Line 1547  Line 1611 
1611    
1612                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->mode != MODE_NOT_CODED)
1613                          {       pMB->cbp =                          {       pMB->cbp =
1614                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
1615                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1616                          }                          }
1617    
1618                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1619                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1620                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1621                                  pEnc->current->sStat.mblks++;                                  current->sStat.mblks++;
1622                          }  else {                          }  else {
1623                                  pEnc->current->sStat.ublks++;                                  current->sStat.ublks++;
1624                          }                          }
1625    
1626                          start_timer();                          start_timer();
# Line 1566  Line 1630 
1630                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1631                                                          (pMB->dquant == 0);                                                          (pMB->dquant == 0);
1632    
1633                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1634                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1635                          else if (pEnc->current->coding_type == P_VOP) {                          else if (current->coding_type == P_VOP) {
1636                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))
1637                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1638                                  else                                  else
1639                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
# Line 1579  Line 1643 
1643    
1644  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1645    
1646                                  if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */                                  if (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */
1647                                  {                                  {
1648                                          int bSkip = 1;                                          int bSkip = 1;
1649    
1650                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1651                                          {                                          {
1652                                                  int iSAD;                                                  int iSAD;
1653                                                  iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1654                                                                          pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1655                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);                                                                  pParam->edged_width,BFRAME_SKIP_THRESHHOLD);
1656                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1657                                                  {       bSkip = 0;                                                  {       bSkip = 0;
1658                                                          break;                                                          break;
# Line 1596  Line 1660 
1660                                          }                                          }
1661    
1662                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1663                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                                  if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1664                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1665                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1666                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1667                                                  }                                                  }
1668                                                  else {                                                  else {
1669                                                          VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1670                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1671                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1672                                                  }                                                  }
1673                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1674                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1675                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1676                                                  stop_coding_timer();                                                  stop_coding_timer();
1677    
1678                                                  continue;       /* next MB */                                                  continue;       /* next MB */
# Line 1623  Line 1687 
1687                          }                          }
1688                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1689    
1690                          if ((pEnc->current->vop_flags & XVID_GREYSCALE))                          if ((current->vop_flags & XVID_VOP_GREYSCALE))
1691                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1692                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1693                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1694                          }                          }
1695    
1696                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1697                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1698                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1699                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1700                                  DPRINTF(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);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1701                          } else {                          } else {
1702                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1703                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1704                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1705                                  DPRINTF(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);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1706                          }                          }
1707    
1708    
# Line 1646  Line 1710 
1710                          {       int k;                          {       int k;
1711                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1712                                  {                                  {
1713                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {
1714                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k);
1715                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1716                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1717                                  DPRINTF(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);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1718                                          } else {                                          } else {
1719                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k);
1720                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1721                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1722                                  DPRINTF(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);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1723                                          }                                          }
1724    
1725                                  }                                  }
1726                          }                          }
1727    
1728                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1729                          stop_coding_timer();                          stop_coding_timer();
1730    
1731                  }                  }
1732          }          }
1733    
1734          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((current->vop_flags & XVID_VOP_REDUCED))
1735          {          {
1736                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&current->image, pParam->edged_width,
1737                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          current->mbs, mb_width, mb_height, pParam->mb_width,
1738                          16, 0);                          16, 0);
1739          }          }
1740    
1741          emms();          emms();
1742    
1743      /* XXX: hinted me          if (current->sStat.iMvCount == 0)
1744          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {                  current->sStat.iMvCount = 1;
                 HintedMEGet(pEnc, 0);  
         }*/  
   
         if (pEnc->current->sStat.iMvCount == 0)  
                 pEnc->current->sStat.iMvCount = 1;  
1745    
1746          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);          fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);
1747    
1748          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pParam->m_fcode);
1749    
1750          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1751          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0)  ))) /* maximum search range 128 */                  && (pParam->m_fcode <= (3 +  (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0)  ))) /* maximum search range 128 */
1752          {          {
1753                  pEnc->mbParam.m_fcode++;                  pParam->m_fcode++;
1754                  iSearchRange *= 2;                  iSearchRange *= 2;
1755          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1756                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1757                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1758                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) )))        /* minimum search range 16 */                             && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) )))        /* minimum search range 16 */
1759          {          {
1760                  pEnc->mbParam.m_fcode--;                  pParam->m_fcode--;
1761                  iSearchRange /= 2;                  iSearchRange /= 2;
1762          }          }
1763    
1764          pEnc->fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1765    
1766          /* frame drop code */          /* frame drop code */
1767          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
1768          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);
1769                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)  #endif
1770            if (current->sStat.kblks + current->sStat.mblks <
1771                    (pParam->frame_drop_ratio * mb_width * mb_height) / 100)
1772          {          {
1773                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = 0;
1774                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1775    
1776                  BitstreamReset(bs);                  BitstreamReset(bs);
1777    
1778                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
1779                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1780    
1781                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1782                  pEnc->current->quant = pEnc->reference->quant;                  current->quant = reference->quant;
1783                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  current->motion_flags = reference->motion_flags;
1784                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1785                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  current->fcode = reference->fcode;
1786                  pEnc->current->fcode = pEnc->reference->fcode;                  current->bcode = reference->bcode;
1787                  pEnc->current->bcode = pEnc->reference->bcode;                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1788                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1789                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);          }
1790          }  
1791            pEnc->current->is_edged = 0; /* not edged */
1792            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1793    
1794            /* what was this frame's interpolated reference will become
1795                    forward (past) reference in b-frame coding */
1796    
1797            image_swap(&pEnc->vInterH, &pEnc->f_refh);
1798            image_swap(&pEnc->vInterV, &pEnc->f_refv);
1799            image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1800    
1801    
1802          /* XXX: debug          /* XXX: debug
1803          {          {
1804                  char s[100];                  char s[100];
1805                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1806                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
1807                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1808                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1809    
1810                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1811                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
1812                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1813                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1814          }          }
1815          */          */
1816    
1817          /* 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);  
1818    
1819      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          current->length = (BitstreamPos(bs) - bits) / 8;
1820    
1821          return 0;                                       /* inter */          return 0;                                       /* inter */
1822  }  }
# Line 1775  Line 1842 
1842                  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); \
1843          }          }
1844    
1845          /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */          /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */
1846    
1847          if (!first){          if (!first){
1848                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1849          }          }
1850  #endif  #endif
1851    
         //frame->quarterpel =  pEnc->mbParam.m_quarterpel;  
   
1852          /* forward  */          /* forward  */
1853            if (!pEnc->reference->is_edged) {
1854          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1855                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1856                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1857                    pEnc->current->is_edged = 1;
1858            }
1859    
1860            if (pEnc->reference->is_interpolated != 0) {
1861          start_timer();          start_timer();
1862          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1863                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1864                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1865          stop_inter_timer();          stop_inter_timer();
1866                    pEnc->reference->is_interpolated = 0;
1867            }
1868    
1869          /* backward */          /* backward */
1870            if (!pEnc->current->is_edged) {
1871          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1872                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1873                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1874                    pEnc->current->is_edged = 1;
1875            }
1876    
1877            if (pEnc->current->is_interpolated != 0) {
1878          start_timer();          start_timer();
1879          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1880                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1881                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1882          stop_inter_timer();          stop_inter_timer();
1883                    pEnc->current->is_interpolated = 0;
1884            }
1885    
1886          start_timer();          frame->coding_type = B_VOP;
1887            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1888    
1889            start_timer();
1890          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1891                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
1892                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
# Line 1813  Line 1894 
1894                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1895                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
1896                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
   
   
1897          stop_motion_timer();          stop_motion_timer();
1898    
         /*  
         if (test_quant_type(&pEnc->mbParam, pEnc->current)) {  
                 BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
         }  
         */  
   
         frame->coding_type = B_VOP;  
   
1899          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1900          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
1901    
1902          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1903          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
# Line 1838  Line 1909 
1909          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1910                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1911                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
                         int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;  
1912    
1913                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1914                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1915                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1916                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1917                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0);
1918                                    }
1919    
1920                                  continue;                                  continue;
1921                          }                          }
1922    
# Line 1865  Line 1939 
1939                                  }                                  }
1940                          }                          }
1941    
1942  #ifdef BFRAMES_DEC_DEBUG                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the
1943          BFRAME_DEBUG                           * coding function for BFrames, that's why we don't zero teh DC
1944  #endif                           * coeffs */
1945                            if ((frame->vop_flags & XVID_VOP_GREYSCALE))
1946                                    mb->cbp &= 0x3C;
1947    
1948                          start_timer();                          start_timer();
1949                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1950                                                   &frame->sStat, direction);                                                   &frame->sStat);
1951                          stop_coding_timer();                          stop_coding_timer();
1952                  }                  }
1953          }          }
# Line 1879  Line 1956 
1956    
1957          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1958    
1959      BitstreamPad(bs);          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1960          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1961    
1962  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.11  
changed lines
  Added in v.1.95.2.54

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