[cvs] / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/encoder.c

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

revision 1.95.2.18, Wed Apr 2 20:43:56 2003 UTC revision 1.123, Thu Jan 19 22:25:18 2006 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002     Michael Militzer <isibaar@xvid.org>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *                         2002-2003 Peter Ross <pross@xvid.org>
8   *  to use this software module in hardware or software products are   *                         2002   Daniel Smith <danielsmith@astroboymail.com>
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 43  Line 38 
38  #include "image/font.h"  #include "image/font.h"
39  #include "motion/sad.h"  #include "motion/sad.h"
40  #include "motion/motion.h"  #include "motion/motion.h"
41    #include "motion/gmc.h"
42    
43  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
44  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
45  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
47  #include "utils/emms.h"  #include "utils/emms.h"
48  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "quant/adapt_quant.h"  
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
# Line 61  Line 57 
57                                            Bitstream * bs);                                            Bitstream * bs);
58    
59  static int FrameCodeP(Encoder * pEnc,  static int FrameCodeP(Encoder * pEnc,
60                                            Bitstream * bs,                                            Bitstream * bs);
                                           bool force_inter,  
                                           bool vol_header);  
61    
62  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
63                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
# Line 91  Line 85 
85  /*  /*
86   * Simplify the "fincr/fbase" fraction   * Simplify the "fincr/fbase" fraction
87  */  */
88    static int
89    gcd(int a, int b)
90    {
91            int r ;
92    
93            if (b > a) {
94                    r = a;
95                    a = b;
96                    b = r;
97            }
98    
99            while ((r = a % b)) {
100                    a = b;
101                    b = r;
102            }
103            return b;
104    }
105    
106  static void  static void
107  simplify_time(int *inc, int *base)  simplify_time(int *inc, int *base)
108  {  {
109          /* common factor */          /* common factor */
110          int i = *inc;          const int s = gcd(*inc, *base);
111          while (i > 1) {    *inc  /= s;
112                  if (*inc % i == 0 && *base % i == 0) {    *base /= s;
113                          *inc /= i;  
114                          *base /= i;          if (*base > 65535 || *inc > 65535) {
115                          i = *inc;                  int *biggest;
116                          continue;                  int *other;
117                  }                  float div;
118                  i--;  
119                    if (*base > *inc) {
120                            biggest = base;
121                            other = inc;
122                    } else {
123                            biggest = inc;
124                            other = base;
125          }          }
126    
127          /* if neccessary, round to 65535 accuracy */                  div = ((float)*biggest)/((float)65535);
128          if (*base > 65535) {                  *biggest = (unsigned int)(((float)*biggest)/div);
129                  float div = (float) *base / 65535;                  *other = (unsigned int)(((float)*other)/div);
                 *base = (int) (*base / div);  
                 *inc = (int) (*inc / div);  
130          }          }
131  }  }
132    
# Line 121  Line 137 
137          Encoder *pEnc;          Encoder *pEnc;
138      int n;      int n;
139    
140          if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */          if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
141                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
142    
143          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
144                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
145    
146            if (create->width<=0 || create->height<=0)
147                    return XVID_ERR_FAIL;
148    
149          /* allocate encoder struct */          /* allocate encoder struct */
150    
151          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
# Line 134  Line 153 
153                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
154          memset(pEnc, 0, sizeof(Encoder));          memset(pEnc, 0, sizeof(Encoder));
155    
156            pEnc->mbParam.profile = create->profile;
157    
158          /* global flags */          /* global flags */
159      pEnc->mbParam.global_flags = create->global;      pEnc->mbParam.global_flags = create->global;
160      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED))
161        pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA;
162    
163      /* width, height */      /* width, height */
164          pEnc->mbParam.width = create->width;          pEnc->mbParam.width = create->width;
# Line 149  Line 172 
172      pEnc->mbParam.fincr = MAX(create->fincr, 0);      pEnc->mbParam.fincr = MAX(create->fincr, 0);
173          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
174      if (pEnc->mbParam.fincr>0)      if (pEnc->mbParam.fincr>0)
175              simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);                  simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase);
176    
177      /* plugin */          /* zones */
178            if(create->num_zones > 0) {
179                    pEnc->num_zones = create->num_zones;
180                    pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
181                    if (pEnc->zones == NULL)
182                            goto xvid_err_memory0;
183                    memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
184            } else {
185                    pEnc->num_zones = 0;
186                    pEnc->zones = NULL;
187            }
188    
189            /* plugins */
190            if(create->num_plugins > 0) {
191      pEnc->num_plugins = create->num_plugins;      pEnc->num_plugins = create->num_plugins;
192      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);
193      if (pEnc->plugins == NULL)      if (pEnc->plugins == NULL)
194          goto xvid_err_memory0;          goto xvid_err_memory0;
195            } else {
196                    pEnc->num_plugins = 0;
197                    pEnc->plugins = NULL;
198            }
199    
200      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
201          xvid_plg_create_t pcreate;          xvid_plg_create_t pcreate;
# Line 163  Line 203 
203    
204          memset(&pinfo, 0, sizeof(xvid_plg_info_t));          memset(&pinfo, 0, sizeof(xvid_plg_info_t));
205          pinfo.version = XVID_VERSION;          pinfo.version = XVID_VERSION;
206          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {                  if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) {
207              pEnc->mbParam.plugin_flags |= pinfo.flags;              pEnc->mbParam.plugin_flags |= pinfo.flags;
208          }          }
209    
210          memset(&pcreate, 0, sizeof(xvid_plg_create_t));          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
211          pcreate.version = XVID_VERSION;          pcreate.version = XVID_VERSION;
212                    pcreate.num_zones = pEnc->num_zones;
213                    pcreate.zones = pEnc->zones;
214          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
215          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
216                    pcreate.mb_width = pEnc->mbParam.mb_width;
217                    pcreate.mb_height = pEnc->mbParam.mb_height;
218          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
219          pcreate.fbase = pEnc->mbParam.fbase;          pcreate.fbase = pEnc->mbParam.fbase;
220          pcreate.param = create->plugins[n].param;          pcreate.param = create->plugins[n].param;
221    
222          pEnc->plugins[n].func = NULL;   /* disable plugins that fail */          pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
223          if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {                  if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
224              pEnc->plugins[n].func = create->plugins[n].func;              pEnc->plugins[n].func = create->plugins[n].func;
225          }          }
226      }      }
# Line 190  Line 234 
234      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
235              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *              pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
236                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                              pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
237                    if (pEnc->temp_dquants==NULL)
238                            goto xvid_err_memory1a;
239            }
240    
241            /* temp lambdas */
242            if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
243                    pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width *
244                                                    pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE);
245                    if (pEnc->temp_lambda == NULL)
246                            goto xvid_err_memory1a;
247      }      }
     /* XXX: error checking */  
248    
249          /* bframes */          /* bframes */
250          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
251          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
252          pEnc->mbParam.bquant_offset = create->bquant_offset;          pEnc->mbParam.bquant_offset = create->bquant_offset;
253    
254            /* min/max quant */
255            for (n=0; n<3; n++) {
256                    pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
257                    pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
258            }
259    
260          /* frame drop ratio */          /* frame drop ratio */
261          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
262    
263      /* max keyframe interval */      /* max keyframe interval */
264      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ?          pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
                 10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval;  
   
         /* Bitrate allocator defaults  
   
         if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))  
                 create->min_quantizer = 1;  
   
         if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))  
                 create->max_quantizer = 31;  
   
         if (create->max_quantizer < create->min_quantizer)  
                 create->max_quantizer = create->min_quantizer; */  
   
265    
266      /* allocate working frame-image memory */      /* allocate working frame-image memory */
267    
# Line 237  Line 283 
283          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
284                  goto xvid_err_memory2;                  goto xvid_err_memory2;
285    
286            /* allocate quant matrix memory */
287    
288            pEnc->mbParam.mpeg_quant_matrices =
289                    xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
290    
291            if (pEnc->mbParam.mpeg_quant_matrices == NULL)
292                    goto xvid_err_memory2a;
293    
294          /* allocate interpolation image memory */          /* allocate interpolation image memory */
295    
296      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
# Line 252  Line 306 
306          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
307          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
308          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
309          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
310    
311          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
312          if (image_create          if (image_create
# Line 298  Line 350 
350                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
351                  goto xvid_err_memory3;                  goto xvid_err_memory3;
352          if (image_create          if (image_create
                 (&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create  
353                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
354                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
355                  goto xvid_err_memory3;                  goto xvid_err_memory3;
         if (image_create  
                 (&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
356    
357  /* Create full bitplane for GMC, this might be wasteful */  /* Create full bitplane for GMC, this might be wasteful */
358          if (image_create          if (image_create
# Line 378  Line 422 
422                  image_null(&pEnc->queue[n].image);                  image_null(&pEnc->queue[n].image);
423    
424    
425          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
         {  
426                  if (image_create                  if (image_create
427                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
428                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
429                          goto xvid_err_memory5;                          goto xvid_err_memory5;
   
430          }          }
431    
   
432          /* timestamp stuff */          /* timestamp stuff */
433    
434          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
# Line 403  Line 444 
444      create->handle = (void *) pEnc;      create->handle = (void *) pEnc;
445    
446          init_timer();          init_timer();
447            init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
448    
449      return 0;   /* ok */      return 0;   /* ok */
450    
# Line 412  Line 454 
454    
455    xvid_err_memory5:    xvid_err_memory5:
456    
457          if (pEnc->mbParam.max_bframes > 0) {          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
458          int i;                          image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
   
                 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {  
                         image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,  
459                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
460                  }                  }
461    
462                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
463    
464    xvid_err_memory4:    xvid_err_memory4:
465    
# Line 434  Line 473 
473    
474                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
475                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
476                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
477                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
478                  }                  }
479    
480                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 468  Line 504 
504                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
505          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
506                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
507          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
508                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
509    
510  /* destroy GMC image */  /* destroy GMC image */
511          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
512                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
513    
514      xvid_err_memory2a:
515            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
516    
517    xvid_err_memory2:    xvid_err_memory2:
518          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
# Line 488  Line 522 
522          xvid_free(pEnc->current);          xvid_free(pEnc->current);
523          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
524    
525      xvid_err_memory1a:
526      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
527              xvid_free(pEnc->temp_dquants);              xvid_free(pEnc->temp_dquants);
528      }      }
529    
530            if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
531                    xvid_free(pEnc->temp_lambda);
532            }
533    
534    xvid_err_memory0:    xvid_err_memory0:
535      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
536          if (pEnc->plugins[n].func) {          if (pEnc->plugins[n].func) {
537              pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);                          pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
538          }          }
539      }      }
540      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
541    
542            xvid_free(pEnc->zones);
543    
544          xvid_free(pEnc);          xvid_free(pEnc);
545    
546          create->handle = NULL;          create->handle = NULL;
# Line 524  Line 565 
565          int i;          int i;
566    
567          /* B Frames specific */          /* B Frames specific */
         if (pEnc->mbParam.max_bframes > 0) {  
   
568                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
   
569                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
570                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
571                  }                  }
                 xvid_free(pEnc->queue);  
         }  
572    
573            xvid_free(pEnc->queue);
574    
575          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
576    
# Line 544  Line 581 
581    
582                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
583                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
584                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
585                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
586                  }                  }
587    
# Line 564  Line 599 
599                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
600          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
601                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
602          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
603                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
   
604          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
605                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
606          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
607                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
608          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
609                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
610            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
611                                      pEnc->mbParam.edged_height);
612    
613          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
614                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
# Line 598  Line 630 
630      }      }
631    
632    
633      if (pEnc->num_plugins>0)          if (pEnc->num_plugins>0) {
     {  
634          xvid_plg_destroy_t pdestroy;          xvid_plg_destroy_t pdestroy;
635          memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));          memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
636    
# Line 608  Line 639 
639    
640          for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<pEnc->num_plugins;i++) {
641              if (pEnc->plugins[i].func) {              if (pEnc->plugins[i].func) {
642                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0);                                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
643              }              }
644          }          }
645          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
646      }      }
647    
648            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
649    
650            if (pEnc->num_plugins>0)
651                    xvid_free(pEnc->zones);
652    
653          xvid_free(pEnc);          xvid_free(pEnc);
654    
655          return 0;  /* ok */          return 0;  /* ok */
# Line 627  Line 663 
663  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
664                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)
665  {  {
666      unsigned int i, j;          unsigned int i, j, k;
667      xvid_plg_data_t data;      xvid_plg_data_t data;
668    
669      /* set data struct */      /* set data struct */
# Line 635  Line 671 
671      memset(&data, 0, sizeof(xvid_plg_data_t));      memset(&data, 0, sizeof(xvid_plg_data_t));
672      data.version = XVID_VERSION;      data.version = XVID_VERSION;
673    
674            /* find zone */
675            for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
676            data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
677    
678      data.width = pEnc->mbParam.width;      data.width = pEnc->mbParam.width;
679      data.height = pEnc->mbParam.height;      data.height = pEnc->mbParam.height;
680      data.mb_width = pEnc->mbParam.mb_width;      data.mb_width = pEnc->mbParam.mb_width;
681      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
682      data.fincr = frame->fincr;      data.fincr = frame->fincr;
683      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
684            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
685            data.bquant_offset = pEnc->mbParam.bquant_offset;
686    
687      data.reference.csp = XVID_CSP_USER;          for (i=0; i<3; i++) {
688                    data.min_quant[i] = pEnc->mbParam.min_quant[i];
689                    data.max_quant[i] = pEnc->mbParam.max_quant[i];
690            }
691    
692            data.reference.csp = XVID_CSP_PLANAR;
693      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
694      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
695      data.reference.plane[2] = pEnc->reference->image.v;      data.reference.plane[2] = pEnc->reference->image.v;
# Line 650  Line 697 
697      data.reference.stride[1] = pEnc->mbParam.edged_width/2;      data.reference.stride[1] = pEnc->mbParam.edged_width/2;
698      data.reference.stride[2] = pEnc->mbParam.edged_width/2;      data.reference.stride[2] = pEnc->mbParam.edged_width/2;
699    
700      data.current.csp = XVID_CSP_USER;          data.current.csp = XVID_CSP_PLANAR;
701      data.current.plane[0] = frame->image.y;      data.current.plane[0] = frame->image.y;
702      data.current.plane[1] = frame->image.u;      data.current.plane[1] = frame->image.u;
703      data.current.plane[2] = frame->image.v;      data.current.plane[2] = frame->image.v;
# Line 661  Line 708 
708      data.frame_num = frame->frame_num;      data.frame_num = frame->frame_num;
709    
710      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
711          data.type = XVID_TYPE_AUTO;                  data.type = *type;
712          data.quant = 2;                  data.quant = *quant;
713    
714                    data.vol_flags = frame->vol_flags;
715                    data.vop_flags = frame->vop_flags;
716                    data.motion_flags = frame->motion_flags;
717    
718            } else if (opt == XVID_PLG_FRAME) {
719                    data.type = coding2type(frame->coding_type);
720                    data.quant = frame->quant;
721    
722                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
723              data.dquant = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
# Line 670  Line 725 
725                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height);
726          }          }
727    
728          /* todo: [vol,vop,motion]_flags*/                  if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
729                            int block = 0;
730                            data.lambda = pEnc->temp_lambda;
731                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
732                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
733                                            for (k = 0; k < 6; k++)
734                                                    data.lambda[block++] = 1.0f;
735                    }
736    
737      } else { // XVID_PLG_AFTER          } else { /* XVID_PLG_AFTER */
738          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
739              data.original.csp = XVID_CSP_USER;                          data.original.csp = XVID_CSP_PLANAR;
740              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
741              data.original.plane[1] = original->u;              data.original.plane[1] = original->u;
742              data.original.plane[2] = original->v;              data.original.plane[2] = original->v;
# Line 711  Line 773 
773    
774              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
775              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
776                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;                                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
777              }              }
778          }          }
779    
# Line 724  Line 786 
786          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
787          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
788    
789          if (stats) {                  /* New code */
790                  stats->type = coding2type(frame->coding_type);                  data.stats.type      = coding2type(frame->coding_type);
791                  stats->quant = frame->quant;                  data.stats.quant     = frame->quant;
792                  stats->vol_flags = frame->vol_flags;                  data.stats.vol_flags = frame->vol_flags;
793                  stats->vop_flags = frame->vop_flags;                  data.stats.vop_flags = frame->vop_flags;
794                  stats->length = frame->length;                  data.stats.length    = frame->length;
795                  stats->hlength = frame->length - (frame->sStat.iTextBits / 8);                  data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
796                  stats->kblks = frame->sStat.kblks;                  data.stats.kblks     = frame->sStat.kblks;
797                  stats->mblks = frame->sStat.mblks;                  data.stats.mblks     = frame->sStat.mblks;
798                  stats->ublks = frame->sStat.ublks;                  data.stats.ublks     = frame->sStat.ublks;
799              stats->sse_y = data.sse_y;                  data.stats.sse_y     = data.sse_y;
800              stats->sse_u = data.sse_u;                  data.stats.sse_u     = data.sse_u;
801              stats->sse_v = data.sse_v;                  data.stats.sse_v     = data.sse_v;
802          }  
803                    if (stats)
804                            *stats = data.stats;
805      }      }
806    
807      /* call plugins */      /* call plugins */
808      for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
809          emms();          emms();
810          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
811              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {                          if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
812                  continue;                  continue;
813              }              }
814          }          }
# Line 754  Line 818 
818      /* copy modified values back into frame*/      /* copy modified values back into frame*/
819      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
820          *type = data.type;          *type = data.type;
821          *quant = data.quant;                  *quant = data.quant > 0 ? data.quant : 2;   /* default */
822    
823                    frame->vol_flags = data.vol_flags;
824                    frame->vop_flags = data.vop_flags;
825                    frame->motion_flags = data.motion_flags;
826    
827            } else if (opt == XVID_PLG_FRAME) {
828    
829          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
830              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
# Line 767  Line 837 
837                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
838              }              }
839          }          }
840          /* todo: [vol,vop,motion]_flags*/  
841                    if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
842                            for (j = 0; j < pEnc->mbParam.mb_height; j++)
843                                    for (i = 0; i < pEnc->mbParam.mb_width; i++)
844                                            for (k = 0; k < 6; k++) {
845                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
846                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
847      }      }
848                    } else {
849                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
850                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
851                                            for (k = 0; k < 6; k++) {
852                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
853                            }
854                    }
855    
856    
857                    frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
858  }  }
859    
860    
861    }
862    
863    
864  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
# Line 789  Line 876 
876      pEnc->m_framenum--; /* debug ticker */      pEnc->m_framenum--; /* debug ticker */
877  }  }
878    
879    static __inline void
880    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
881    {
882            if (pMB->cbp == 0) {
883                    /* we want to code dquant but the quantizer value will not be used yet
884                            let's find out if we can postpone dquant to next MB
885                    */
886                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
887                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
888                            return;
889                    } else {
890                            MACROBLOCK * next = pMB + 1;
891                            const MACROBLOCK * prev = pMB - 1;
892                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
893                                    /* mode allows dquant change in the future */
894                                    if (abs(next->quant - prev->quant) <= 2) {
895                                            /* quant change is not out of range */
896                                            pMB->quant = prev->quant;
897                                            pMB->dquant = 0;
898                                            next->dquant = next->quant - prev->quant;
899                                            return;
900                                    }
901                    }
902            }
903            /* couldn't skip this dquant */
904            pMB->mode = MODE_INTER_Q;
905    }
906    
907    
908    
909  static __inline void  static __inline void
# Line 798  Line 913 
913      pCur->ticks = (int32_t)pCur->stamp % time_base;      pCur->ticks = (int32_t)pCur->stamp % time_base;
914                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
915    
916                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */  #if 0   /* HEAVY DEBUG OUTPUT */
917            fprintf(stderr,"WriteVop:   %d - %d \n",
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
918                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
919                  fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",                  fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
920                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
921                  fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",                  fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
922                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
923    #endif
924    }
925    
926  */  static void
927    simplify_par(int *par_width, int *par_height)
928    {
929    
930            int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
931            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
932            int divisor = gcd(_par_width, _par_height);
933    
934            _par_width  /= divisor;
935            _par_height /= divisor;
936    
937            /* 2^8 precision maximum */
938            if (_par_width>255 || _par_height>255) {
939                    float div;
940                    emms();
941                    if (_par_width>_par_height)
942                            div = (float)_par_width/255;
943                    else
944                            div = (float)_par_height/255;
945    
946                    _par_width  = (int)((float)_par_width/div);
947                    _par_height = (int)((float)_par_height/div);
948  }  }
949    
950            *par_width = _par_width;
951            *par_height = _par_height;
952    
953            return;
954    }
955    
956    
957  /*****************************************************************************  /*****************************************************************************
# Line 834  Line 976 
976          int type;          int type;
977          Bitstream bs;          Bitstream bs;
978    
979          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))     /* v1.x.x */
980                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
981    
982          xFrame->out_flags = 0;          xFrame->out_flags = 0;
# Line 871  Line 1013 
1013    
1014                  if (xFrame->quant_intra_matrix)                  if (xFrame->quant_intra_matrix)
1015                  {                  {
1016                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
1017                          q->frame.quant_intra_matrix = q->quant_intra_matrix;                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
1018                  }                  }
1019    
1020                  if (xFrame->quant_inter_matrix)                  if (xFrame->quant_inter_matrix)
1021                  {                  {
1022                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
1023                          q->frame.quant_inter_matrix = q->quant_inter_matrix;                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
1024                  }                  }
1025    
# Line 896  Line 1038 
1038          {          {
1039                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
1040    
1041                          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",
1042                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
1043                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1044    
# Line 906  Line 1048 
1048                          }                          }
1049    
1050                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1051              call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1052                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1053    
1054                          goto done;                          goto done;
# Line 923  Line 1065 
1065                          int tmp;                          int tmp;
1066                          int bits;                          int bits;
1067    
1068                          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",
1069                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1070                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1071    
# Line 932  Line 1074 
1074                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
1075                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
1076    
1077                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1078                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1079                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
1080    
1081                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1082                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1083              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1084    
1085              /* flush complete: reset counters */              /* flush complete: reset counters */
1086                  pEnc->flush_bframes = 0;                  pEnc->flush_bframes = 0;
# Line 961  Line 1103 
1103                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
1104                  {                  {
1105    
1106                            DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1107                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1108                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1109    
1110              if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {              if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1111                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1112              }              }
1113    
1114              /* if the very last frame is to be b-vop, we must change it to a p-vop */              /* if the very last frame is to be b-vop, we must change it to a p-vop */
1115              if (pEnc->bframenum_tail > 0)                          if (pEnc->bframenum_tail > 0) {
                         {  
1116    
1117                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1118                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
1119                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1120    
1121                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
1122                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1123                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1124                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1125    
1126                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1127                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
1128                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1129                  }                  }
1130    
1131                                  FrameCodeP(pEnc, &bs, 1, 0);                                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1132                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1133                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1134                                    pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1135    
1136                                    FrameCodeP(pEnc, &bs);
1137    
1138                                  goto done_flush;  
1139                                    if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1140                                            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1141                                    }else{
1142                                            pEnc->flush_bframes = 1;
1143                                            goto done;
1144                                    }
1145                          }                          }
1146                            DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1147    
1148                          emms();                          emms();
1149                          return XVID_ERR_END;    /* end of stream reached */                          return XVID_ERR_END;    /* end of stream reached */
# Line 992  Line 1151 
1151                  goto done;      /* nothing to encode yet; encoder lag */                  goto done;      /* nothing to encode yet; encoder lag */
1152          }          }
1153    
1154          // the current FRAME becomes the reference          /* the current FRAME becomes the reference */
1155          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1156    
1157          // remove frame from encoding-queue (head), and move it into the current          /* remove frame from encoding-queue (head), and move it into the current */
1158          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1159          frame = &pEnc->queue[pEnc->queue_head].frame;          frame = &pEnc->queue[pEnc->queue_head].frame;
1160          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 1008  Line 1167 
1167    
1168      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1169      inc_frame_num(pEnc);      inc_frame_num(pEnc);
1170      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;          pEnc->current->vol_flags = frame->vol_flags;
1171      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1172          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1173          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
# Line 1024  Line 1183 
1183           * frame type & quant selection           * frame type & quant selection
1184           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1185    
     call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);  
   
     if (frame->type > 0)  
1186                  type = frame->type;                  type = frame->type;
   
     if (frame->quant > 0)  
1187                  pEnc->current->quant = frame->quant;                  pEnc->current->quant = frame->quant;
1188    
1189            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1190    
1191      if (type > 0){      /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
1192                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1193      } else{             /* XVID_TYPE_AUTO */      } else{             /* XVID_TYPE_AUTO */
# Line 1041  Line 1197 
1197                  }else{                  }else{
1198                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1199                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1200                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1201                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
             if (type == B_VOP && !(pEnc->current->vop_flags & XVID_VOP_DYNAMIC_BFRAMES)) {  
                 type = P_VOP;   /* disable dynamic bframes */  
             }  
1202                  }                  }
1203          }          }
1204    
1205            if (type != I_VOP)
1206                    pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1207    
1208      /* bframes buffer overflow check */      /* bframes buffer overflow check */
1209      if (type != I_VOP) {          if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
         if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
1210              type = P_VOP;              type = P_VOP;
         }else{  
             type = B_VOP;  
         }  
1211      }      }
1212    
1213          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1214    
1215          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1216                  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,
1217                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                          "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1218          }          }
1219    
1220          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1087  Line 1239 
1239                  else if (pEnc->current->quant > 31)                  else if (pEnc->current->quant > 31)
1240              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1241    
1242                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1243                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1244                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1245    
# Line 1100  Line 1252 
1252                  goto repeat;                  goto repeat;
1253      }      }
1254    
1255    
1256                    DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1257                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1258                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1259    
1260      /* for unpacked bframes, output the stats for the last encoded frame */      /* for unpacked bframes, output the stats for the last encoded frame */
1261      if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)          if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1262      {      {
1263          if (pEnc->current->stamp > 0) {          if (pEnc->current->stamp > 0) {
1264              call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1265          }          }
1266                  else                  else
1267                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
# Line 1117  Line 1274 
1274    
1275      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1276    
1277                  // place this frame back on the encoding-queue (head)                  /* place this frame back on the encoding-queue (head) */
1278                  // we will deal with it next time                  /* we will deal with it next time */
1279          dec_frame_num(pEnc);          dec_frame_num(pEnc);
1280          pEnc->iFrameNum--;          pEnc->iFrameNum--;
1281    
# Line 1126  Line 1283 
1283          pEnc->queue_size++;          pEnc->queue_size++;
1284                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1285    
1286                  // grab the last frame from the bframe-queue                  /* grab the last frame from the bframe-queue */
1287    
1288                  pEnc->bframenum_tail--;                  pEnc->bframenum_tail--;
1289                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1290    
1291                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1292                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP");
1293                  }                  }
1294    
1295                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1296                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1297                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1298                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1299          type = P_VOP;          type = P_VOP;
1300      }      }
1301    
# Line 1147  Line 1306 
1306    
1307          if (type == I_VOP) {          if (type == I_VOP) {
1308    
1309                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1310                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1311                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1312    
# Line 1155  Line 1314 
1314                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1315                  }                  }
1316    
1317                    pEnc->iFrameNum = 1;
1318    
1319                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1320                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1321    
1322                    /* Aspect ratio */
1323                    switch(frame->par) {
1324                    case XVID_PAR_11_VGA:
1325                    case XVID_PAR_43_PAL:
1326                    case XVID_PAR_43_NTSC:
1327                    case XVID_PAR_169_PAL:
1328                    case XVID_PAR_169_NTSC:
1329                    case XVID_PAR_EXT:
1330                            pEnc->mbParam.par = frame->par;
1331                            break;
1332                    default:
1333                            pEnc->mbParam.par = XVID_PAR_11_VGA;
1334                            break;
1335                    }
1336    
1337                    /* For extended PAR only, we try to sanityse/simplify par values */
1338                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1339                            pEnc->mbParam.par_width  = frame->par_width;
1340                            pEnc->mbParam.par_height = frame->par_height;
1341                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1342                    }
1343    
1344          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1345                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1346                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1347                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
1348                                  set_inter_matrix(frame->quant_inter_matrix);                                  set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1349                  }                  }
1350    
1351          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1352    
         if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
             pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;  
   
1353          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1354              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1355    
# Line 1188  Line 1367 
1367           * encode this frame as an p-vop           * encode this frame as an p-vop
1368       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1369    
1370      } else { // (type == P_VOP || type == S_VOP)          } else { /* (type == P_VOP || type == S_VOP) */
1371    
1372                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1373                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1374                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1375    
# Line 1203  Line 1382 
1382                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1383          }          }
1384    
1385                  FrameCodeP(pEnc, &bs, 1, 0);                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1386                            /* N-VOP, we mustn't code b-frames yet */
1387                            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1388                            goto done;
1389                    }
1390      }      }
1391    
1392    
# Line 1211  Line 1394 
1394           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1395           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1396    
1397  done_flush:  /*done_flush:*/
1398    
1399      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1400    
1401          /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */          /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1402          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1403                  goto repeat;                  goto repeat;
1404          }          }
1405    
1406      /* packed or no-bframes: output stats */          /* packed or no-bframes or no-bframes-queued: output stats */
1407      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0) {      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0) {
1408          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1409          }          }
1410    
1411          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1241  Line 1424 
1424    
1425  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1426  {  {
1427      unsigned int i,j;          unsigned int i;
1428      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1429            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1430            if (quant > 31)
1431                    frame->quant = quant = 31;
1432            else if (quant < 1)
1433                    frame->quant = quant = 1;
1434    
1435      for (j=0; j<pParam->mb_height; j++)          for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
     for (i=0; i<pParam->mb_width; i++) {  
         MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];  
1436          quant += pMB->dquant;          quant += pMB->dquant;
1437          if (quant > 31)          if (quant > 31)
1438                          quant = 31;                          quant = 31;
1439                  if (quant < 1)                  else if (quant < 1)
1440                          quant = 1;                          quant = 1;
1441          pMB->quant = quant;          pMB->quant = quant;
1442                    pMB++;
1443      }      }
1444  }  }
1445    
# Line 1290  Line 1477 
1477    
1478          uint16_t x, y;          uint16_t x, y;
1479    
         if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
   
                 /* 16x16->8x8 downsample requires 1 additional edge pixel*/  
                 /* XXX: setedges is overkill */  
                 start_timer();  
                 image_setedges(&pEnc->current->image,  
                         pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,  
                         pEnc->mbParam.width, pEnc->mbParam.height);  
                 stop_edges_timer();  
         }  
   
1480          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1481          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1482          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1483    
1484            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1485    
1486      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1487    
1488          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1489    
1490          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1491    
1492          BitstreamPadAlways(bs);          BitstreamPad(bs);
1493          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1494            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1495    
1496          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1497            pEnc->current->sStat.iMVBits = 0;
1498          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1499          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1500    
# Line 1336  Line 1513 
1513                          stop_prediction_timer();                          stop_prediction_timer();
1514    
1515                          start_timer();                          start_timer();
                         if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)  
                         {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                 qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
1516                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1517                          stop_coding_timer();                          stop_coding_timer();
1518                  }                  }
1519    
         if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,  
                         16, 0);  
         }  
1520          emms();          emms();
1521    
1522  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1523  #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1524      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1525    
1526          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1527          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1528    
1529      /* XXX: hinted me          pEnc->current->is_edged = 0; /* not edged */
1530          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
                 HintedMEGet(pEnc, 1);  
         }*/  
1531    
1532          return 1;                                       /* intra */          return 1;                                       /* intra */
1533  }  }
1534    
1535    static __inline void
1536    updateFcode(Statistics * sStat, Encoder * pEnc)
1537    {
1538            float fSigma;
1539            int iSearchRange;
1540    
1541            if (sStat->iMvCount == 0)
1542                    sStat->iMvCount = 1;
1543    
1544  #define INTRA_THRESHOLD 0.5          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
 #define BFRAME_SKIP_THRESHHOLD 30  
1545    
1546            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1547    
1548            if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1549                    pEnc->mbParam.m_fcode++;
1550    
1551            else if ((5.0 * fSigma < iSearchRange)
1552                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1553                               && (pEnc->mbParam.m_fcode >= 2) )
1554                    pEnc->mbParam.m_fcode--;
1555    
1556            pEnc->fMvPrevSigma = fSigma;
1557    }
1558    
1559    #define BFRAME_SKIP_THRESHHOLD 30
1560    
1561  /* FrameCodeP also handles S(GMC)-VOPs */  /* FrameCodeP also handles S(GMC)-VOPs */
1562  static int  static int
1563  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1564                     Bitstream * bs,                     Bitstream * bs)
                    bool force_inter,  
                    bool vol_header)  
1565  {  {
         float fSigma;  
1566      int bits = BitstreamPos(bs);      int bits = BitstreamPos(bs);
1567    
1568          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1569          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1570    
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
         int iLimit;  
1571          int x, y, k;          int x, y, k;
1572          int iSearchRange;          FRAMEINFO *const current = pEnc->current;
1573          int bIntra, skip_possible;          FRAMEINFO *const reference = pEnc->reference;
1574            MBParam * const pParam = &pEnc->mbParam;
1575          /* IMAGE *pCurrent = &pEnc->current->image; */          int mb_width = pParam->mb_width;
1576          IMAGE *pRef = &pEnc->reference->image;          int mb_height = pParam->mb_height;
1577            int coded = 1;
         if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
         }  
1578    
1579            IMAGE *pRef = &reference->image;
1580    
1581            if (!reference->is_edged) {
1582          start_timer();          start_timer();
1583          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1584                                     pEnc->mbParam.width, pEnc->mbParam.height);                                             pParam->width, pParam->height, 0);
1585          stop_edges_timer();          stop_edges_timer();
1586                    reference->is_edged = 1;
1587            }
1588    
1589          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1590          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1591          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          current->fcode = pParam->m_fcode;
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
   
         if (!force_inter)  
                 iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);  
         else  
                 iLimit = mb_width * mb_height + 1;  
1592    
1593          if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1594                    if (reference->is_interpolated != current->rounding_type) {
1595                  start_timer();                  start_timer();
1596                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1597                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                            pEnc->vInterHV.y, pParam->edged_width,
1598                                                    pEnc->mbParam.edged_height,                                                            pParam->edged_height,
1599                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1600                                                    pEnc->current->rounding_type);                                                            current->rounding_type);
1601                  stop_inter_timer();                  stop_inter_timer();
1602                            reference->is_interpolated = current->rounding_type;
1603                    }
1604          }          }
1605    
1606          pEnc->current->coding_type = P_VOP;          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1607                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1608                    current->sStat.iMVBits = 0;
     SetMacroblockQuants(&pEnc->mbParam, pEnc->current);  
1609    
1610          start_timer();          current->coding_type = P_VOP;
         /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)  
                 HintedMESet(pEnc, &bIntra);  
         else*/  
                 bIntra =  
                         MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                          &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                          iLimit);  
1611    
1612          stop_motion_timer();          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1613    
1614          if (bIntra == 1) return FrameCodeI(pEnc, bs);          SetMacroblockQuants(&pEnc->mbParam, current);
1615    
1616          if ( ( pEnc->current->vol_flags & XVID_VOL_GMC )          start_timer();
1617                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1618          {          {       int gmcval;
1619                  pEnc->current->coding_type = S_VOP;                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1620                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1621    
1622                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1623                            gmcval = GlobalMotionEstRefine(&current->warp,
1624                                                                                       current->mbs, pParam,
1625                                                                                       current, reference,
1626                                                                                       &current->image,
1627                                                                                       &reference->image,
1628                                                                                       &pEnc->vInterH,
1629                                                                                       &pEnc->vInterV,
1630                                                                                       &pEnc->vInterHV);
1631                    } else {
1632                            gmcval = globalSAD(&current->warp, pParam, current->mbs,
1633                                                               current,
1634                                                               &reference->image,
1635                                                               &current->image,
1636                                                               pEnc->vGMC.y);
1637                    }
1638    
1639                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1640    
1641                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1642                    generate_GMCparameters( 3, 3, &current->warp,
1643                                    pParam->width, pParam->height,
1644                                    &current->new_gmc_data);
1645    
1646                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1647                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1648                    {
1649                            current->coding_type = S_VOP;
1650    
1651                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1652                                    pParam->mb_width, pParam->mb_height,
1653                                    pParam->edged_width, pParam->edged_width/2,
1654                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1655                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1656    
1657                  generate_GMCparameters( 2, 16, &pEnc->current->warp,                  } else {
                                         pEnc->mbParam.width, pEnc->mbParam.height,  
                                         &pEnc->current->gmc_data);  
   
                 generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,  
                                 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,  
                                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,  
                                 pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0,  
                                 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);  
1658    
1659                            generate_GMCimage(&current->new_gmc_data, &reference->image,
1660                                    pParam->mb_width, pParam->mb_height,
1661                                    pParam->edged_width, pParam->edged_width/2,
1662                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1663                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1664          }          }
   
         set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);  
         if (vol_header)  
         {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);  
                 BitstreamPadAlways(bs);  
1665          }          }
1666    
1667          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          MotionEstimation(&pEnc->mbParam, current, reference,
1668                                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1669                                             &pEnc->vGMC, 256*4096);
1670    
         pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =  
                 pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;  
1671    
1672            stop_motion_timer();
1673    
1674          for (y = 0; y < mb_height; y++) {          set_timecodes(current,reference,pParam->fbase);
                 for (x = 0; x < mb_width; x++) {  
                         MACROBLOCK *pMB =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1675    
1676  /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
 /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */  
1677    
1678                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);          for (y = 0; y < mb_height; y++) {
1679                    for (x = 0; x < mb_width; x++) {
1680                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1681                            int skip_possible;
1682    
1683                          if (bIntra) {                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1684                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1685                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1686                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1687    
1688                                  start_timer();                                  start_timer();
1689                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1690                                  stop_prediction_timer();                                  stop_prediction_timer();
1691    
1692                                  pEnc->current->sStat.kblks++;                                  current->sStat.kblks++;
1693    
1694                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1695                                  stop_coding_timer();                                  stop_coding_timer();
1696                                  continue;                                  continue;
1697                          }                          }
1698    
                         if (pEnc->current->coding_type == S_VOP) {  
   
                                 int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->mbParam.edged_width, 65536);  
   
                                 if (pEnc->current->motion_flags & XVID_ME_CHROMA16) {  
                                         iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
   
                                         iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
                                 }  
   
                                 if (iSAD <= pMB->sad16) {               /* mode decision GMC */  
   
                                         if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))  
                                                 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;  
                                         else  
                                                 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
                                         pMB->mode = MODE_INTER;  
                                         pMB->mcsel = 1;  
                                         pMB->sad16 = iSAD;  
                                 } else {  
                                         pMB->mcsel = 0;  
                                 }  
                         } else {  
                                 pMB->mcsel = 0; /* just a precaution */  
                         }  
   
1699                          start_timer();                          start_timer();
1700                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1701                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1702                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1703                                                                   &pEnc->current->image,                                                                   &current->image,
1704                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1705                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1706                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1707                                                                   (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1708                                                                   (pEnc->current->vop_flags & XVID_VOP_REDUCED),                                                                   current->rounding_type);
                                                                  pEnc->current->rounding_type);  
1709    
1710                          stop_comp_timer();                          stop_comp_timer();
1711    
                         if (pMB->dquant != 0) {  
                 pMB->mode = MODE_INTER_Q;  
                         }  
   
1712                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1713    
1714                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1715                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,  
1716                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1717                          }                          }
1718    
1719                            if (pMB->dquant != 0)
1720                                    MBSetDquant(pMB, x, y, &pEnc->mbParam);
1721    
1722    
1723                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1724                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1725                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1726                                  pEnc->current->sStat.mblks++;                                  current->sStat.mblks++;
1727                          }  else {                          }  else {
1728                                  pEnc->current->sStat.ublks++;                                  current->sStat.ublks++;
1729                          }                          }
1730    
1731                          start_timer();                          start_timer();
1732    
1733                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1734    
1735                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                                                         (pMB->dquant == 0);  
1736    
1737                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1738                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1739                          else if (pEnc->current->coding_type == P_VOP) {                          else { /* PVOP */
1740                                  if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1741                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                                                                  pMB->qmvs : pMB->mvs;
1742                                  else                                  skip_possible &= ((mv->x|mv->y) == 0);
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
1743                          }                          }
1744    
1745                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
   
1746  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
   
                                 if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */  
                                 {  
1747                                          int bSkip = 1;                                          int bSkip = 1;
1748    
1749                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
                                         {  
                                                 int iSAD;  
                                                 iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                                                         pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                                                 pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);  
                                                 if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)  
                                                 {       bSkip = 0;  
                                                         break;  
                                                 }  
                                         }  
1750    
1751                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1752                                                  if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {                                                  int iSAD;
1753                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1754                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1755                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1756                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1757                                                            bSkip = 0; /* could not SKIP */
1758                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1759                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1760                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1761                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1762                                                  }                                                          } else {
1763                                                  else {                                                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
                                                         VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
1764                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1765                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1766                                                  }                                                  }
1767                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1768                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1769                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                          break;
1770                                                  stop_coding_timer();                                                  }
   
                                                 continue;       /* next MB */  
1771                                          }                                          }
1772                                  }                                  }
                                 /* do SKIP */  
1773    
1774                                    if (bSkip) {
1775                                            /* do SKIP */
1776                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1777                                  MBSkip(bs);                                  MBSkip(bs);
1778                                  stop_coding_timer();                                  stop_coding_timer();
1779                                  continue;       /* next MB */                                  continue;       /* next MB */
1780                          }                          }
                         /* ordinary case: normal coded INTER/INTER4V block */  
   
                         if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE))  
                         {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                 qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
   
                         if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {  
                                 VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;  
                                 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);  
                         } else {  
                                 VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;  
                                 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);  
                         }  
   
   
                         if (pMB->mode == MODE_INTER4V)  
                         {       int k;  
                                 for (k=1;k<4;k++)  
                                 {  
                                         if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {  
                                                 VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;  
                                 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);  
                                         } else {  
                                                 VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;  
                                 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);  
1781                                          }                                          }
1782    
1783                                  }                          /* ordinary case: normal coded INTER/INTER4V block */
1784                          }                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
   
                         MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);  
1785                          stop_coding_timer();                          stop_coding_timer();
   
                 }  
1786          }          }
   
         if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,  
                         16, 0);  
1787          }          }
1788    
1789          emms();          emms();
1790            updateFcode(&current->sStat, pEnc);
     /* XXX: hinted me  
         if (pEnc->current->global_flags & XVID_HINTEDME_GET) {  
                 HintedMEGet(pEnc, 0);  
         }*/  
   
         if (pEnc->current->sStat.iMvCount == 0)  
                 pEnc->current->sStat.iMvCount = 1;  
   
         fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);  
   
         iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);  
   
         if ((fSigma > iSearchRange / 3)  
         && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0)  )))     /* maximum search range 128 */  
         {  
                 pEnc->mbParam.m_fcode++;  
                 iSearchRange *= 2;  
         } else if ((fSigma < iSearchRange / 6)  
                            && (pEnc->fMvPrevSigma >= 0)  
                            && (pEnc->fMvPrevSigma < iSearchRange / 6)  
                            && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) )))    /* minimum search range 16 */  
         {  
                 pEnc->mbParam.m_fcode--;  
                 iSearchRange /= 2;  
         }  
   
         pEnc->fMvPrevSigma = fSigma;  
1791    
1792          /* frame drop code */          /* frame drop code */
1793          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
1794          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1795                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)  #endif
1796            if (current->sStat.kblks + current->sStat.mblks <=
1797                    (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
1798                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
1799          {          {
1800                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = 0;
1801                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1802    
1803                  BitstreamReset(bs);                  BitstreamReset(bs);
1804    
1805                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
1806                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1807    
1808                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1809                  pEnc->current->quant = pEnc->reference->quant;                  current->quant = reference->quant;
1810                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  current->motion_flags = reference->motion_flags;
1811                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1812                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  current->fcode = reference->fcode;
1813                  pEnc->current->fcode = pEnc->reference->fcode;                  current->bcode = reference->bcode;
1814                  pEnc->current->bcode = pEnc->reference->bcode;                  current->stamp = reference->stamp;
1815                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1816                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1817                    coded = 0;
1818    
1819            } else {
1820    
1821                    pEnc->current->is_edged = 0; /* not edged */
1822                    pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1823    
1824                    /* what was this frame's interpolated reference will become
1825                            forward (past) reference in b-frame coding */
1826    
1827                    image_swap(&pEnc->vInterH, &pEnc->f_refh);
1828                    image_swap(&pEnc->vInterV, &pEnc->f_refv);
1829                    image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1830          }          }
1831    
1832          /* XXX: debug          /* XXX: debug
1833          {          {
1834                  char s[100];                  char s[100];
1835                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1836                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
1837                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1838                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1839    
1840                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1841                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
1842                          pEnc->mbParam.edged_width,                          pParam->edged_width,
1843                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
1844          }          }
1845          */          */
1846    
1847  /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
 #if 0  
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1848    
1849      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          current->length = (BitstreamPos(bs) - bits) / 8;
1850    
1851          return 0;                                       /* inter */          return coded;
1852  }  }
1853    
1854    
# Line 1796  Line 1872 
1872                  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); \
1873          }          }
1874    
         /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */  
   
1875          if (!first){          if (!first){
1876                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1877          }          }
1878  #endif  #endif
1879    
         //frame->quarterpel =  pEnc->mbParam.m_quarterpel;  
   
1880          /* forward  */          /* forward  */
1881            if (!pEnc->reference->is_edged) {
1882          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1883                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1884                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1885                    pEnc->current->is_edged = 1;
1886            }
1887    
1888            if (pEnc->reference->is_interpolated != 0) {
1889          start_timer();          start_timer();
1890          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y,
1891                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1892                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1893          stop_inter_timer();          stop_inter_timer();
1894                    pEnc->reference->is_interpolated = 0;
1895            }
1896    
1897          /* backward */          /* backward */
1898            if (!pEnc->current->is_edged) {
1899          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1900                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1901                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1902                    pEnc->current->is_edged = 1;
1903            }
1904    
1905            if (pEnc->current->is_interpolated != 0) {
1906          start_timer();          start_timer();
1907          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
1908                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1909                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1910          stop_inter_timer();          stop_inter_timer();
1911                    pEnc->current->is_interpolated = 0;
1912            }
1913    
1914          start_timer();          frame->coding_type = B_VOP;
1915            call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1916    
1917            start_timer();
1918          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1919                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
1920                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
# Line 1834  Line 1922 
1922                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1923                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
1924                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
   
   
1925          stop_motion_timer();          stop_motion_timer();
1926    
         /*  
         if (test_quant_type(&pEnc->mbParam, pEnc->current)) {  
                 BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
         }  
         */  
   
         frame->coding_type = B_VOP;  
   
1927          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1928          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
1929    
1930          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1931            frame->sStat.iMVBits = 0;
1932          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
1933          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
1934          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
# Line 1859  Line 1938 
1938          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1939                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1940                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
                         int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0;  
1941    
1942                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1943                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1944                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1945                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1946                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
1947                                    }
1948                                  continue;                                  continue;
1949                          }                          }
1950    
1951                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                          mb->quant = frame->quant;
1952    
1953                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1954                                    /* we have to motion-compensate, transfer etc,
1955                                            because there might be blocks to code */
1956    
1957                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1958                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1959                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1960                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1961                                                                           dct_codes);                                                                           dct_codes);
1962    
                                 if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;  
                                 mb->quant = frame->quant;  
   
                                 if (mb->mode != MODE_DIRECT_NONE_MV)  
1963                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1964                            }
1965    
1966                            if (mb->mode == MODE_DIRECT_NO4V)
1967                                    mb->mode = MODE_DIRECT;
1968    
1969                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
                                         && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {  
1970                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
1971                                  }                          else
1972                          }                                  if (frame->vop_flags & XVID_VOP_GREYSCALE)
1973                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
1974                                            mb->cbp &= 0x3C;
1975    
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
1976                          start_timer();                          start_timer();
1977                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1978                                                   &frame->sStat, direction);                                                   &frame->sStat);
1979                          stop_coding_timer();                          stop_coding_timer();
1980                  }                  }
1981          }          }
1982    
1983          emms();          emms();
1984    
1985          /* TODO: dynamic fcode/bcode ??? */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
   
     BitstreamPadAlways(bs);  
1986          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1987    
1988  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.18  
changed lines
  Added in v.1.123

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