[svn] / branches / dev-api-4 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 919, Thu Mar 13 11:07:20 2003 UTC revision 1054, Mon Jun 9 13:55:56 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 26  Line 21 
21   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   *  $Id: encoder.c,v 1.95.2.4 2003-03-13 11:07:20 suxen_drol Exp $   * $Id: encoder.c,v 1.95.2.27 2003-06-09 13:50:34 edgomez Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 47  Line 42 
42  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
43  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
44  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "utils/ratecontrol.h"  
45  #include "utils/emms.h"  #include "utils/emms.h"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
47  #include "quant/adapt_quant.h"  #include "quant/adapt_quant.h"
# Line 55  Line 49 
49  #include "utils/mem_align.h"  #include "utils/mem_align.h"
50    
51  /*****************************************************************************  /*****************************************************************************
  * Local macros  
  ****************************************************************************/  
   
 #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }  
   
 /*****************************************************************************  
52   * Local function prototypes   * Local function prototypes
53   ****************************************************************************/   ****************************************************************************/
54    
# Line 76  Line 64 
64                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
65                                             Bitstream * bs);                                             Bitstream * bs);
66    
 /*****************************************************************************  
  * Local data  
  ****************************************************************************/  
   
 static int DQtab[4] = {  
         -1, -2, 1, 2  
 };  
   
 static int iDQtab[5] = {  
         1, 0, NO_CHANGE, 2, 3  
 };  
   
67    
68  /*****************************************************************************  /*****************************************************************************
69   * Encoder creation   * Encoder creation
# Line 135  Line 111 
111    
112    
113  int  int
114  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)  enc_create(xvid_enc_create_t * create)
115  {  {
116          Encoder *pEnc;          Encoder *pEnc;
117      int n;      int n;
118    
119          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1))   /* v1.x.x */          if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */
120                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
121    
122          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
# Line 153  Line 129 
129                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
130          memset(pEnc, 0, sizeof(Encoder));          memset(pEnc, 0, sizeof(Encoder));
131    
132        pEnc->mbParam.profile = create->profile;
133    
134          /* global flags */          /* global flags */
135      pEnc->mbParam.global_flags = create->global;      pEnc->mbParam.global_flags = create->global;
136    
# Line 167  Line 145 
145      /* framerate */      /* framerate */
146      pEnc->mbParam.fincr = MAX(create->fincr, 0);      pEnc->mbParam.fincr = MAX(create->fincr, 0);
147          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
148        if (pEnc->mbParam.fincr>0)
149          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
150    
151      /* plugin */      /* zones */
152        if(create->num_zones > 0) {
153                    pEnc->num_zones = create->num_zones;
154                    pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
155                    if (pEnc->zones == NULL)
156                            goto xvid_err_memory0;
157            memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
158            } else {
159                    pEnc->num_zones = 0;
160                    pEnc->zones = NULL;
161            }
162    
163        /* plugins */
164        if(create->num_plugins > 0) {
165      pEnc->num_plugins = create->num_plugins;      pEnc->num_plugins = create->num_plugins;
166      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);
167      if (pEnc->plugins == NULL)      if (pEnc->plugins == NULL)
168          goto xvid_err_memory0;          goto xvid_err_memory0;
169            } else {
170                    pEnc->num_plugins = 0;
171                    pEnc->plugins = NULL;
172            }
173    
174      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
175          xvid_plg_create_t pcreate;          xvid_plg_create_t pcreate;
# Line 182  Line 178 
178          memset(&pinfo, 0, sizeof(xvid_plg_info_t));          memset(&pinfo, 0, sizeof(xvid_plg_info_t));
179          pinfo.version = XVID_VERSION;          pinfo.version = XVID_VERSION;
180          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {
181              pEnc->plugin_flags |= pinfo.flags;              pEnc->mbParam.plugin_flags |= pinfo.flags;
182          }          }
183    
184          memset(&pcreate, 0, sizeof(xvid_plg_create_t));          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
185          pcreate.version = XVID_VERSION;          pcreate.version = XVID_VERSION;
186            pcreate.num_zones = pEnc->num_zones;
187            pcreate.zones = pEnc->zones;
188          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
189          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
190          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
# Line 197  Line 195 
195          if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {          if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
196              pEnc->plugins[n].func = create->plugins[n].func;              pEnc->plugins[n].func = create->plugins[n].func;
197          }          }
198        }
199    
200        if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
201            (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
202            pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
203      }      }
204    
205      /* temp dquants */      /* temp dquants */
206        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
207          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
208                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
209        }
210      /* XXX: error checking */      /* XXX: error checking */
211    
212          /* bframes */          /* bframes */
# Line 210  Line 214 
214          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
215          pEnc->mbParam.bquant_offset = create->bquant_offset;          pEnc->mbParam.bquant_offset = create->bquant_offset;
216    
217        /* min/max quant */
218        for (n=0; n<3; n++) {
219            pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
220            pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
221        }
222    
223          /* frame drop ratio */          /* frame drop ratio */
224          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
225    
226      /* max keyframe interval */      /* max keyframe interval */
227      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval;      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ?
228      /*XXX: replace 250 hard code with "10seconds * framerate" */                  10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval;
229    
230          /* Bitrate allocator defaults          /* Bitrate allocator defaults
231    
         if (create->rc_bitrate <= 0)  
                 create->rc_bitrate = 900000;  
   
         if (create->rc_reaction_delay_factor <= 0)  
                 create->rc_reaction_delay_factor = 16;  
   
         if (create->rc_averaging_period <= 0)  
                 create->rc_averaging_period = 100;  
   
         if (create->rc_buffer <= 0)  
                 create->rc_buffer = 100;  
   
   
   
232          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))
233                  create->min_quantizer = 1;                  create->min_quantizer = 1;
234    
# Line 240  Line 236 
236                  create->max_quantizer = 31;                  create->max_quantizer = 31;
237    
238          if (create->max_quantizer < create->min_quantizer)          if (create->max_quantizer < create->min_quantizer)
239                  create->max_quantizer = create->min_quantizer;                  create->max_quantizer = create->min_quantizer; */
   
         pEnc->bitrate = create->rc_bitrate; */  
240    
241    
242      /* allocate working frame-image memory */      /* allocate working frame-image memory */
# Line 267  Line 261 
261    
262          /* allocate interpolation image memory */          /* allocate interpolation image memory */
263    
264          if (create->global & XVID_EXTRASTATS_ENABLE)      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
265                  image_null(&pEnc->sOriginal);                  image_null(&pEnc->sOriginal);
266            image_null(&pEnc->sOriginal2);
267        }
268    
269          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
270          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 282  Line 278 
278          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
279          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
280    
281          if (create->global & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
282          {       if (image_create          if (image_create
283                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
284                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
285                          goto xvid_err_memory3;                          goto xvid_err_memory3;
286    
287            if (image_create
288                            (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
289                             pEnc->mbParam.edged_height) < 0)
290                            goto xvid_err_memory3;
291          }          }
292    
293          if (image_create          if (image_create
# Line 421  Line 422 
422          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
423          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
424    
     /* XXX: cleanup ratecontol when new code is ready */  
 /*      if (create->rc_bitrate) {  
                 RateControlInit(&pEnc->rate_control, create->rc_bitrate,  
                                                 create->rc_reaction_delay_factor,  
                                                 create->rc_averaging_period, create->rc_buffer,  
                                                 create->fbase * 1000 / create->fincr,  
                                                 create->max_quantizer, create->min_quantizer);  
         } */  
   
425      create->handle = (void *) pEnc;      create->handle = (void *) pEnc;
426    
427          init_timer();          init_timer();
# Line 476  Line 468 
468    
469    xvid_err_memory3:    xvid_err_memory3:
470    
471          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
472          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
473                                              pEnc->mbParam.edged_height);
474                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
475                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
476          }          }
477    
# Line 516  Line 510 
510          xvid_free(pEnc->current);          xvid_free(pEnc->current);
511          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
512    
513        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
514          xvid_free(pEnc->temp_dquants);          xvid_free(pEnc->temp_dquants);
515        }
516    
517    xvid_err_memory0:    xvid_err_memory0:
518      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
# Line 526  Line 522 
522      }      }
523      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
524    
525        xvid_free(pEnc->zones);
526    
527          xvid_free(pEnc);          xvid_free(pEnc);
528    
529          create->handle = NULL;          create->handle = NULL;
# Line 603  Line 601 
601                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
602          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
603                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
604            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
605                                      pEnc->mbParam.edged_height);
606    
607          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
608          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
609                                              pEnc->mbParam.edged_height);
610                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
611                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
612          }          }
613    
# Line 617  Line 619 
619          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
620          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
621    
622        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
623      xvid_free(pEnc->temp_dquants);      xvid_free(pEnc->temp_dquants);
624        }
625    
626    
627        if (pEnc->num_plugins>0) {
628            xvid_plg_destroy_t pdestroy;
629            memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
630    
631            pdestroy.version = XVID_VERSION;
632            pdestroy.num_frames = pEnc->m_framenum;
633    
634      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
635          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
636              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);
637          }          }
638      }      }
639      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
640        }
641    
642        if (pEnc->num_plugins>0)
643            xvid_free(pEnc->zones);
644    
645          xvid_free(pEnc);          xvid_free(pEnc);
646    
# Line 636  Line 652 
652    call the plugins    call the plugins
653    */    */
654    
655  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, int opt, int * type, int * quant)  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
656                             int opt, int * type, int * quant, xvid_enc_stats_t * stats)
657  {  {
658      int i;      unsigned int i, j;
659      xvid_plg_data_t data;      xvid_plg_data_t data;
660    
661        /* set data struct */
662    
663      memset(&data, 0, sizeof(xvid_plg_data_t));      memset(&data, 0, sizeof(xvid_plg_data_t));
664      data.version = XVID_VERSION;      data.version = XVID_VERSION;
665    
666        /* find zone */
667        for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
668        data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
669    
670        data.width = pEnc->mbParam.width;
671        data.height = pEnc->mbParam.height;
672        data.mb_width = pEnc->mbParam.mb_width;
673        data.mb_height = pEnc->mbParam.mb_height;
674        data.fincr = frame->fincr;
675        data.fbase = pEnc->mbParam.fbase;
676    
677        for (i=0; i<3; i++) {
678            data.min_quant[i] = pEnc->mbParam.min_quant[i];
679            data.max_quant[i] = pEnc->mbParam.max_quant[i];
680        }
681    
682      data.reference.csp = XVID_CSP_USER;      data.reference.csp = XVID_CSP_USER;
683      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
684      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
# Line 660  Line 695 
695      data.current.stride[1] = pEnc->mbParam.edged_width/2;      data.current.stride[1] = pEnc->mbParam.edged_width/2;
696      data.current.stride[2] = pEnc->mbParam.edged_width/2;      data.current.stride[2] = pEnc->mbParam.edged_width/2;
697    
698      data.original.csp = XVID_CSP_NULL;      data.frame_num = frame->frame_num;
     /* todo: data.original */  
699    
700      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
701          data.type = XVID_TYPE_AUTO;          data.type = XVID_TYPE_AUTO;
702          data.quant = 2;          data.quant = 0;
703          //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);  
704          //data.qscale_stride = pEnc->mbParam.width;                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
705          //data.qscale_table = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
706          /* todo: vol,vop,motion flags */              data.dquant_stride = pEnc->mbParam.mb_width;
707                            memset(data.dquant, 0, data.mb_width*data.mb_height);
708            }
709    
710            /* todo: [vol,vop,motion]_flags*/
711    
712        } else { /* XVID_PLG_AFTER */
713            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
714                data.original.csp = XVID_CSP_USER;
715                data.original.plane[0] = original->y;
716                data.original.plane[1] = original->u;
717                data.original.plane[2] = original->v;
718                data.original.stride[0] = pEnc->mbParam.edged_width;
719                data.original.stride[1] = pEnc->mbParam.edged_width/2;
720                data.original.stride[2] = pEnc->mbParam.edged_width/2;
721            }
722    
723      } else { // XVID_PLG_AFTER          if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
724                (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
725    
726                            data.sse_y =
727                                plane_sse( original->y, frame->image.y,
728                                                   pEnc->mbParam.edged_width, pEnc->mbParam.width,
729                                                   pEnc->mbParam.height);
730    
731                data.sse_u =
732                    plane_sse( original->u, frame->image.u,
733                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
734                                                   pEnc->mbParam.height/2);
735    
736                            data.sse_v =
737                    plane_sse( original->v, frame->image.v,
738                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
739                                                   pEnc->mbParam.height/2);
740            }
741    
742          data.type = coding2type(frame->coding_type);          data.type = coding2type(frame->coding_type);
743          data.quant = frame->quant;          data.quant = frame->quant;
744          /* todo: data.qscale */  
745            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
746                data.dquant = pEnc->temp_dquants;
747                data.dquant_stride = pEnc->mbParam.mb_width;
748    
749                for (j=0; j<pEnc->mbParam.mb_height; j++)
750                for (i=0; i<pEnc->mbParam.mb_width; i++) {
751                    data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;
752                }
753            }
754    
755          data.vol_flags = frame->vol_flags;          data.vol_flags = frame->vol_flags;
756          data.vop_flags = frame->vop_flags;          data.vop_flags = frame->vop_flags;
757          data.motion_flags = frame->motion_flags;          data.motion_flags = frame->motion_flags;
# Line 684  Line 760 
760          data.kblks = frame->sStat.kblks;          data.kblks = frame->sStat.kblks;
761          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
762          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
763    
764            if (stats) {
765                    stats->type = coding2type(frame->coding_type);
766                    stats->quant = frame->quant;
767                    stats->vol_flags = frame->vol_flags;
768                    stats->vop_flags = frame->vop_flags;
769                    stats->length = frame->length;
770                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
771                    stats->kblks = frame->sStat.kblks;
772                    stats->mblks = frame->sStat.mblks;
773                    stats->ublks = frame->sStat.ublks;
774                stats->sse_y = data.sse_y;
775                stats->sse_u = data.sse_u;
776                stats->sse_v = data.sse_v;
777            }
778      }      }
779    
780        /* call plugins */
781      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
782            emms();
783          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
784              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) {
785                  continue;                  continue;
786              }              }
787          }          }
788      }      }
789        emms();
790    
791        /* copy modified values back into frame*/
792      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
793          *type = data.type;          *type = data.type;
794          *quant = data.quant;          *quant = data.quant > 0 ? data.quant : 2;   /* default */
         /* todo: copy modified qscale,vol,vop,motion flags into the frame*/  
     }  
795    
796            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
797                for (j=0; j<pEnc->mbParam.mb_height; j++)
798                for (i=0; i<pEnc->mbParam.mb_width; i++) {
799                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
800                }
801            }else{
802                for (j=0; j<pEnc->mbParam.mb_height; j++)
803                for (i=0; i<pEnc->mbParam.mb_width; i++) {
804                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
805                }
806            }
807            /* todo: [vol,vop,motion]_flags*/
808        }
809  }  }
810    
811    
# Line 707  Line 813 
813    
814  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
815  {  {
816        pEnc->current->frame_num = pEnc->m_framenum;
817          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
         pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;  
818    
819        pEnc->mbParam.m_stamp += pEnc->current->fincr;
820      pEnc->m_framenum++; /* debug ticker */      pEnc->m_framenum++; /* debug ticker */
821  }  }
822    
823    static __inline void dec_frame_num(Encoder * pEnc)
824    {
825        pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
826        pEnc->m_framenum--; /* debug ticker */
827    }
828    
829    
830    
831  static __inline void  static __inline void
# Line 735  Line 848 
848  }  }
849    
850    
 static void  
 set_stats(xvid_enc_stats_t * stats, RateControl * rc,  
                   const MBParam * pParam, const FRAMEINFO * frame)  
 {  
         if (stats)  
         {  
                 stats->type = coding2type(frame->coding_type);  
                 stats->quant = frame->quant;  
                 stats->vol_flags = frame->vol_flags;  
                 stats->vop_flags = frame->vop_flags;  
                 stats->length = frame->length;  
                 stats->hlength = frame->length - (frame->sStat.iTextBits / 8);  
                 stats->kblks = frame->sStat.kblks;  
                 stats->mblks = frame->sStat.mblks;  
                 stats->ublks = frame->sStat.ublks;  
         }  
   
         RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP );  
 }  
   
   
851    
852  /*****************************************************************************  /*****************************************************************************
853   * IPB frame encoder entry point   * IPB frame encoder entry point
# Line 770  Line 862 
862   ****************************************************************************/   ****************************************************************************/
863    
864    
 /* XXX: dx50bvop broken! something todo with queue */  
   
865  int  int
866  enc_encode(Encoder * pEnc,  enc_encode(Encoder * pEnc,
867                             xvid_enc_frame_t * xFrame,                             xvid_enc_frame_t * xFrame,
# Line 779  Line 869 
869  {  {
870          xvid_enc_frame_t * frame;          xvid_enc_frame_t * frame;
871          int type;          int type;
         uint16_t x, y;  
872          Bitstream bs;          Bitstream bs;
873    
874          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */
# Line 803  Line 892 
892                  if (image_input                  if (image_input
893                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
894                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
895                          xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))                          xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
896                  {                  {
897                          emms();                          emms();
898                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
899                  }                  }
900                  stop_conv_timer();                  stop_conv_timer();
901    
902                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {                  if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
903                          image_chroma_optimize(&q->image,                          image_chroma_optimize(&q->image,
904                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
905                  }                  }
# Line 842  Line 931 
931    
932          if (pEnc->flush_bframes)          if (pEnc->flush_bframes)
933          {          {
934                  if (pEnc->bframenum_head < pEnc->bframenum_tail)                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
                 {  
935    
936                          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",
937                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
938                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
939    
940                          if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {              if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
941                                  image_copy(&pEnc->sOriginal, &pEnc->bframes[pEnc->bframenum_head]->image,                                  image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
942                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
943                          }                          }
944    
945                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
946                call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);
                         if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {  
                                 stats->sse_y =  
                                         plane_sse( pEnc->sOriginal.y, pEnc->bframes[pEnc->bframenum_head]->image.y,  
                                                            pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                                            pEnc->mbParam.height);  
   
                                 stats->sse_u =  
                                         plane_sse( pEnc->sOriginal.u, pEnc->bframes[pEnc->bframenum_head]->image.u,  
                                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                                            pEnc->mbParam.height/2);  
   
                                 stats->sse_v =  
                                         plane_sse( pEnc->sOriginal.v, pEnc->bframes[pEnc->bframenum_head]->image.v,  
                                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                                            pEnc->mbParam.height/2);  
                         }  
   
                         set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]);  
             emms();  
             call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
             emms();  
   
947                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
948    
   
949                          goto done;                          goto done;
950                  }                  }
951    
                 /* flush complete: reset counters */  
   
                 pEnc->flush_bframes = 0;  
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
   
952                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
953    
954                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
# Line 896  Line 956 
956                     indentical to the future-referece frame.                     indentical to the future-referece frame.
957                  */                  */
958    
959                  if ((pEnc->mbParam.global_flags & XVID_PACKED)) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
960                          int tmp;                          int tmp;
961                          int bits;                          int bits;
962    
963                          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",
964                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
965                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
966    
# Line 915  Line 975 
975    
976                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
977                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
978                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
             emms();  
             call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
             emms();  
   
                         // xFrame->out_flags |= XVID_PACKED_FIXUP?;  
979    
980                /* flush complete: reset counters */
981                    pEnc->flush_bframes = 0;
982                        pEnc->bframenum_head = pEnc->bframenum_tail = 0;
983                          goto done;                          goto done;
984    
985                  }                  }
         }  
986    
987            /* flush complete: reset counters */
988                    pEnc->flush_bframes = 0;
989                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
990            }
991    
992          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993           * dequeue frame from the encoding queue           * dequeue frame from the encoding queue
# Line 936  Line 997 
997          {          {
998                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
999                  {                  {
1000                          if (pEnc->bframenum_tail > 0)   /* are bframes */  
1001                          {              DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1002                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1003                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1004    
1005                if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1006                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1007                }
1008    
1009                /* if the very last frame is to be b-vop, we must change it to a p-vop */
1010                if (pEnc->bframenum_tail > 0) {
1011    
1012                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1013                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
1014                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1015    
1016                                  /* XXX: convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
1017                                  pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
1018    
1019                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1020                                image_copy(&pEnc->sOriginal, &pEnc->current->image,
1021                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1022                    }
1023    
1024                    DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1025                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1026                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1027    
1028                                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs, 1, 0);
1029                                  goto done_flush;  
1030    
1031                    if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1032                        call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1033                    }else{
1034                        pEnc->flush_bframes = 1;
1035                        goto done;
1036                    }
1037                          }                          }
1038                DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1039    
1040                          emms();                          emms();
1041                          return XVID_ERR_END;    /* end of stream reached */                          return XVID_ERR_END;    /* end of stream reached */
# Line 955  Line 1043 
1043                  goto done;      /* nothing to encode yet; encoder lag */                  goto done;      /* nothing to encode yet; encoder lag */
1044          }          }
1045    
1046          // the current FRAME becomes the reference          /* the current FRAME becomes the reference */
1047          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1048    
1049          // remove frame from encoding-queue (head), and move it into the current          /* remove frame from encoding-queue (head), and move it into the current */
1050          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1051          frame = &pEnc->queue[pEnc->queue_head].frame;          frame = &pEnc->queue[pEnc->queue_head].frame;
1052          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 966  Line 1054 
1054    
1055    
1056          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1057           * init pEnc->current field           * init pEnc->current fields
1058           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1059    
1060          emms();         /* float-point region */      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1061        inc_frame_num(pEnc);
1062      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
1063      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1064          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1065          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1066          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
1067    
1068      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {  
1069        if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1070              image_chroma_optimize(&pEnc->current->image,              image_chroma_optimize(&pEnc->current->image,
1071                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1072      }      }
1073    
1074      if (xFrame->vop_flags & XVID_EXTRASTATS) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1075                  image_copy(&pEnc->sOriginal, &pEnc->current->image,           * frame type & quant selection
1076                         pEnc->mbParam.edged_width, pEnc->mbParam.height);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
     }  
   
         if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {  
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                                           pEnc->mbParam.edged_width, pEnc->temp_dquants,  
                                                           pEnc->current->quant, pEnc->current->quant,  
                                                           2 * pEnc->current->quant,  
                                                           pEnc->mbParam.mb_width,  
                                                           pEnc->mbParam.mb_height);  
   
                 for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
   
 #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)  
   
                         for (x = 0; x < pEnc->mbParam.mb_width; x++) {  
                                 MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];  
   
                                 pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];  
                         }  
   
 #undef OFFSET  
                 }  
   
         }  
   
         emms();         /* END floating-point region */  
     call_plugins(pEnc, pEnc->current, XVID_PLG_BEFORE, &type, &pEnc->current->quant);  
     emms();  
1077    
     if (frame->type > 0)  
1078                  type = frame->type;                  type = frame->type;
   
     if (frame->quant > 0)  
1079                  pEnc->current->quant = frame->quant;                  pEnc->current->quant = frame->quant;
1080    
1081      if (type > 0)       /* XVID_TYPE_?VOP */      call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);
1082          {  
1083        if (type > 0){      /* XVID_TYPE_?VOP */
1084                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1085          }      } else{             /* XVID_TYPE_AUTO */
1086          else                    /* XVID_TYPE_AUTO */                  if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){
         {  
                 if (pEnc->iFrameNum == 0 ||  
                         (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))  
                 {  
1087                          pEnc->iFrameNum = 0;                          pEnc->iFrameNum = 0;
1088                          type = I_VOP;                          type = I_VOP;
1089                  }else{                  }else{
1090                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1091                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1092                                          pEnc->iFrameNum, pEnc->bframenum_tail);                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);
1093                  }                  }
1094          }          }
         inc_frame_num(pEnc);  
         pEnc->iFrameNum++;  
1095    
1096        /* bframes buffer overflow check */
1097        if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1098            type = P_VOP;
1099        }
1100    
1101          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          pEnc->iFrameNum++;
1102    
1103            if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1104                  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,
1105                          "%i  st:%i  if:%i", pEnc->m_framenum, pEnc->current->stamp, pEnc->iFrameNum);                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1106          }          }
1107    
1108          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1109           * ivop/pvop/bvop selection           * encode this frame as a b-vop
1110         * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1111           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1112            if (type == B_VOP) {
1113                    if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1114                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1115                    }
1116    
1117          if (type == I_VOP) {                  if (frame->quant < 1) {
1118                            pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1119                                    pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1120    
1121                    } else {
1122                            pEnc->current->quant = frame->quant;
1123                    }
1124    
1125                    if (pEnc->current->quant < 1)
1126                            pEnc->current->quant = 1;
1127                    else if (pEnc->current->quant > 31)
1128                pEnc->current->quant = 31;
1129    
1130                    DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1131                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1132                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1133    
1134                    /* store frame into bframe buffer & swap ref back to current */
1135                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1136                    SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1137    
1138                    pEnc->bframenum_tail++;
1139    
1140                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  goto repeat;
1141        }
1142    
1143    
1144                    DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1145                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1146                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1147    
1148                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {      /* for unpacked bframes, output the stats for the last encoded frame */
1149                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");      if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1150        {
1151            if (pEnc->current->stamp > 0) {
1152                call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1153                  }                  }
1154                    else
1155                            stats->type = XVID_TYPE_NOTHING;
1156        }
1157    
1158            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1159             * closed-gop
1160         * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1161         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1162    
1163                  // when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1164                  if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {  
1165                    /* place this frame back on the encoding-queue (head) */
1166                    /* we will deal with it next time */
1167            dec_frame_num(pEnc);
1168            pEnc->iFrameNum--;
1169    
                         // place this frame back on the encoding-queue (head)  
                         // we will deal with it next time  
1170                          pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);                          pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1171            pEnc->queue_size++;
1172                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1173    
1174                          // grab the last frame from the bframe-queue                  /* grab the last frame from the bframe-queue */
1175    
1176                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
1177                          SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1178    
1179                          if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1180                                  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");
1181                          }                          }
1182    
1183                          /* XXX: convert B-VOP to P-VOP */                  /* convert B-VOP quant to P-VOP */
1184                          pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
1185            type = P_VOP;
1186        }
1187    
                         FrameCodeP(pEnc, &bs, 1, 0);  
1188    
1189                  } else {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1190             * encode this frame as an i-vop
1191         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1192    
1193            if (type == I_VOP) {
1194    
1195                    DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1196                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1197                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1198    
1199                    if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1200                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1201                    }
1202    
1203    
1204                          /* ---- update vol flags at IVOP ----------- */                          /* ---- update vol flags at IVOP ----------- */
1205                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1206    
1207              if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1208                                  if (frame->quant_intra_matrix != NULL)                                  if (frame->quant_intra_matrix != NULL)
1209                                          set_intra_matrix(frame->quant_intra_matrix);                                          set_intra_matrix(frame->quant_intra_matrix);
1210                                  if (frame->quant_inter_matrix != NULL)                                  if (frame->quant_inter_matrix != NULL)
# Line 1098  Line 1213 
1213    
1214              /* prevent vol/vop misuse */              /* prevent vol/vop misuse */
1215    
1216              if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))          if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))
1217                  pEnc->current->vop_flags &= ~XVID_REDUCED;              pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;
1218    
1219              if (!(pEnc->current->vol_flags & XVID_INTERLACING))          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1220                  pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1221    
1222                          /* ^^^------------------------ */                          /* ^^^------------------------ */
1223    
1224            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1225                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1226                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1227            }
1228    
1229                          FrameCodeI(pEnc, &bs);                          FrameCodeI(pEnc, &bs);
1230                          xFrame->out_flags |= XVID_KEYFRAME;                          xFrame->out_flags |= XVID_KEYFRAME;
                 }  
1231    
1232          } else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) ||          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233                                  pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {           * encode this frame as an p-vop
1234                  /*       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1235                   * This will be coded as a Predicted Frame  
1236                   */      } else { /* (type == P_VOP || type == S_VOP) */
1237    
1238                  DPRINTF(DPRINTF_DEBUG,"*** xFrame 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",
1239                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1240                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1241    
1242                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1243                          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");
1244                  }                  }
1245    
1246                  FrameCodeP(pEnc, &bs, 1, 0);          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1247                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1248          } else {        /* type == B_VOP */                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
   
                 if ((pEnc->current->vop_flags & XVID_DEBUG)) {  
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");  
                 }  
   
                 if (frame->bquant < 1) {  
                         pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *  
                                 pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;  
   
                 } else {  
                         pEnc->current->quant = frame->bquant;  
1249                  }                  }
1250    
1251                  if (pEnc->current->quant < 1)                  FrameCodeP(pEnc, &bs, 1, 0);
                         pEnc->current->quant = 1;  
                 else if (pEnc->current->quant > 31)  
             pEnc->current->quant = 31;  
   
                 DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);  
   
                 /* store frame into bframe buffer & swap ref back to current */  
                 SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);  
                 SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);  
   
                 pEnc->bframenum_tail++;  
   
                 goto repeat;  
   
1252      }      }
1253    
         if (xFrame->vop_flags & XVID_EXTRASTATS) {  
                 stats->sse_y =  
                         plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,  
                                            pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                            pEnc->mbParam.height);  
   
                 stats->sse_u =  
                         plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,  
                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                            pEnc->mbParam.height/2);  
   
                 stats->sse_v =  
                         plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,  
                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                            pEnc->mbParam.height/2);  
         }  
1254    
1255          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256           * okay, on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1257           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1258    
1259  done_flush:  done_flush:
1260    
 #if 0  
         {  
                 char tmp[100];  
                 wsprintf(tmp,"\\frame%03i.pgm", pEnc->m_framenum);  
                 image_dump_yuvpgm(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->mbParam.width, pEnc->mbParam.height, tmp);  
         }  
 #endif  
   
         /* packed_mode: repeat */  
1261          pEnc->flush_bframes = 1;          pEnc->flush_bframes = 1;
1262          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {  
1263            /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1264            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1265                  goto repeat;                  goto repeat;
1266          }          }
1267    
1268          if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)      /* packed or no-bframes or no-bframes-queued: output stats */
1269          {      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1270                  /* packed(and low_delay) encoding gives us graceful reorded-stats output */          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
                 set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);  
         emms();  
         call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
         emms();  
         }  
         else  
         {  
                 /* outherwise, output the previous frame */  
                 /* XXX: for this to work, refernce must be valid for IVOPs too */  
         if (pEnc->current->stamp > 0) {  
                         set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference);  
             emms();  
             call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
             emms();  
         }  
                 else  
                         stats->type = XVID_TYPE_NOTHING;  
1271          }          }
1272    
1273          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1233  Line 1284 
1284  }  }
1285    
1286    
1287    static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1288    {
1289        unsigned int i,j;
1290        int quant = frame->quant;
1291    
1292        for (j=0; j<pParam->mb_height; j++)
1293        for (i=0; i<pParam->mb_width; i++) {
1294            MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];
1295            quant += pMB->dquant;
1296            if (quant > 31)
1297                            quant = 31;
1298                    if (quant < 1)
1299                            quant = 1;
1300            pMB->quant = quant;
1301        }
1302    }
1303    
1304    
1305  static __inline void  static __inline void
1306  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(Encoder * pEnc,
1307                          MACROBLOCK * pMB)                          MACROBLOCK * pMB)
# Line 1246  Line 1315 
1315          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1316          pMB->sad16 = 0;          pMB->sad16 = 0;
1317    
1318          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if (pMB->dquant != 0) {
                 if (pMB->dquant != NO_CHANGE) {  
1319                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
                         pEnc->current->quant += DQtab[pMB->dquant];  
   
                         if (pEnc->current->quant > 31)  
                                 pEnc->current->quant = 31;  
                         if (pEnc->current->quant < 1)  
                                 pEnc->current->quant = 1;  
                 }  
1320          }          }
   
         pMB->quant = pEnc->current->quant;  
1321  }  }
1322    
1323    
# Line 1276  Line 1335 
1335    
1336          uint16_t x, y;          uint16_t x, y;
1337    
1338          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))          if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))
1339          {          {
1340                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1341                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1290  Line 1349 
1349                  stop_edges_timer();                  stop_edges_timer();
1350          }          }
1351    
         //pEnc->iFrameNum = 0;  
1352          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1353          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
         //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;  
1354          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1355    
1356        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1357    
1358          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1359    
1360          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
# Line 1322  Line 1381 
1381                          stop_prediction_timer();                          stop_prediction_timer();
1382    
1383                          start_timer();                          start_timer();
1384                          if (pEnc->current->vop_flags & XVID_GREYSCALE)                          if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1385                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1386                                  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 */
1387                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
# Line 1331  Line 1390 
1390                          stop_coding_timer();                          stop_coding_timer();
1391                  }                  }
1392    
1393          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))
1394          {          {
1395                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1396                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
# Line 1339  Line 1398 
1398          }          }
1399          emms();          emms();
1400    
1401    /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */
1402    #if 0
1403          /* for divx5 compatibility, we must always pad between the packed p and b frames */          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1404          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)
1405    #endif
1406                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1407    #if 0
1408          else          else
1409                  BitstreamPad(bs);                  BitstreamPad(bs);
1410    #endif
1411      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1412    
1413          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
# Line 1386  Line 1450 
1450          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1451          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1452    
1453          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))
1454          {          {
1455                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1456                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1400  Line 1464 
1464    
1465          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1466          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1467          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          /* pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel; */
1468          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1469    
1470          if (!force_inter)          if (!force_inter)
# Line 1408  Line 1472 
1472          else          else
1473                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1474    
1475          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {          if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) {
1476                  start_timer();                  start_timer();
1477                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1478                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1479                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1480                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL),
1481                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1482                  stop_inter_timer();                  stop_inter_timer();
1483          }          }
1484    
1485          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1486    
1487    
1488        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1489    
1490          start_timer();          start_timer();
1491          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1492                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
# Line 1433  Line 1500 
1500    
1501          if (bIntra == 1) return FrameCodeI(pEnc, bs);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1502    
1503          if ( ( pEnc->current->vol_flags & XVID_GMC )          if ( ( pEnc->current->vol_flags & XVID_VOL_GMC )
1504                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1505          {          {
1506                  pEnc->current->coding_type = S_VOP;                  pEnc->current->coding_type = S_VOP;
# Line 1445  Line 1512 
1512                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1513                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1514                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1515                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0,
1516                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1517    
1518          }          }
# Line 1494  Line 1561 
1561                                          pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                          pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1562                                          pEnc->mbParam.edged_width, 65536);                                          pEnc->mbParam.edged_width, 65536);
1563    
1564                                  if (pEnc->current->motion_flags & PMV_CHROMA16) {                                  if (pEnc->current->motion_flags & XVID_ME_CHROMA16) {
1565                                          iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,                                          iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1566                                          pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);                                          pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1567    
# Line 1504  Line 1571 
1571    
1572                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1573    
1574                                          if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                          if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))
1575                                                  pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;                                                  pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1576                                          else                                          else
1577                                                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;                                                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
# Line 1527  Line 1594 
1594                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pEnc->mbParam.width,
1595                                                                   pEnc->mbParam.height,                                                                   pEnc->mbParam.height,
1596                                                                   pEnc->mbParam.edged_width,                                                                   pEnc->mbParam.edged_width,
1597                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),                                                                   (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL),
1598                                                                   (pEnc->current->vop_flags & XVID_REDUCED),                                                                   (pEnc->current->vop_flags & XVID_VOP_REDUCED),
1599                                                                   pEnc->current->rounding_type);                                                                   pEnc->current->rounding_type);
1600    
1601                          stop_comp_timer();                          stop_comp_timer();
1602    
1603                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 if (pMB->dquant != NO_CHANGE) {  
1604                                          pMB->mode = MODE_INTER_Q;                                          pMB->mode = MODE_INTER_Q;
                                         pEnc->current->quant += DQtab[pMB->dquant];  
                                         if (pEnc->current->quant > 31)  
                                                 pEnc->current->quant = 31;  
                                         else if (pEnc->current->quant < 1)  
                                                 pEnc->current->quant = 1;  
                                 }  
1605                          }                          }
                         pMB->quant = pEnc->current->quant;  
1606    
1607                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1608    
# Line 1566  Line 1625 
1625                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1626    
1627                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1628                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == 0);
1629    
1630                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1631                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1632                          else if (pEnc->current->coding_type == P_VOP) {                          else if (pEnc->current->coding_type == P_VOP) {
1633                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                  if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))
1634                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1635                                  else                                  else
1636                                          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 1598  Line 1657 
1657                                          }                                          }
1658    
1659                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1660                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                                  if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {
1661                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1662                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1663                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
# Line 1625  Line 1684 
1684                          }                          }
1685                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1686    
1687                          if ((pEnc->current->vop_flags & XVID_GREYSCALE))                          if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE))
1688                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1689                                  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 */
1690                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1691                          }                          }
1692    
1693                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {
1694                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1695                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1696                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1697                                  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);
1698                          } else {                          } else {
1699                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1700                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1701                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1702                                  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);
1703                          }                          }
1704    
1705    
# Line 1648  Line 1707 
1707                          {       int k;                          {       int k;
1708                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1709                                  {                                  {
1710                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {
1711                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1712                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1713                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1714                                  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);
1715                                          } else {                                          } else {
1716                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1717                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1718                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1719                                  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);
1720                                          }                                          }
1721    
1722                                  }                                  }
# Line 1669  Line 1728 
1728                  }                  }
1729          }          }
1730    
1731          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))
1732          {          {
1733                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1734                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
# Line 1691  Line 1750 
1750          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1751    
1752          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1753          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0)  ))) /* maximum search range 128 */          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0)  )))     /* maximum search range 128 */
1754          {          {
1755                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1756                  iSearchRange *= 2;                  iSearchRange *= 2;
1757          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1758                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1759                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1760                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) )))        /* minimum search range 16 */                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) )))    /* minimum search range 16 */
1761          {          {
1762                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1763                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1707  Line 1766 
1766          pEnc->fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1767    
1768          /* frame drop code */          /* frame drop code */
1769          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
1770            DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
1771    #endif
1772          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
1773                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
1774          {          {
# Line 1723  Line 1784 
1784                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
1785                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
1786                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
1787                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  /* pEnc->current->quarterpel =  pEnc->reference->quarterpel; */
1788                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1789                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1790                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
# Line 1745  Line 1806 
1806          }          }
1807          */          */
1808    
1809    /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */
1810    #if 0
1811          /* for divx5 compatibility, we must always pad between the packed p and b frames */          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1812          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)
1813    #endif
1814                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1815    #if 0
1816          else          else
1817                  BitstreamPad(bs);                  BitstreamPad(bs);
1818    #endif
1819    
1820      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1821    
# Line 1777  Line 1843 
1843                  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); \
1844          }          }
1845    
1846          /* 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 */
1847    
1848          if (!first){          if (!first){
1849                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1850          }          }
1851  #endif  #endif
1852    
1853          //frame->quarterpel =  pEnc->mbParam.m_quarterpel;          /* frame->quarterpel =  pEnc->mbParam.m_quarterpel; */
1854    
1855          /* forward  */          /* forward  */
1856          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
# Line 1793  Line 1859 
1859          start_timer();          start_timer();
1860          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,
1861                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1862                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1863          stop_inter_timer();          stop_inter_timer();
1864    
1865          /* backward */          /* backward */
# Line 1803  Line 1869 
1869          start_timer();          start_timer();
1870          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1871                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1872                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1873          stop_inter_timer();          stop_inter_timer();
1874    
1875          start_timer();          start_timer();
# Line 1840  Line 1906 
1906          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1907                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1908                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1909                          int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0;
1910    
1911                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1912                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1848  Line 1914 
1914                                  continue;                                  continue;
1915                          }                          }
1916    
1917                          if (mb->mode != MODE_DIRECT_NONE_MV) {                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1918                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1919                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1920                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
# Line 1858  Line 1924 
1924                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1925                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
1926    
1927                                  mb->cbp =                                  if (mb->mode != MODE_DIRECT_NONE_MV)
1928                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1929    
1930                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1931                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
# Line 1881  Line 1947 
1947    
1948          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1949    
1950      BitstreamPad(bs);      BitstreamPadAlways(bs);
1951          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1952    
1953  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.919  
changed lines
  Added in v.1054

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