[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.33, Sun Jul 13 09:57:51 2003 UTC revision 1.120, Tue Nov 22 10:23:01 2005 UTC
# Line 57  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 87  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 117  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 157 
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 147  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      /* zones */      /* zones */
178      if(create->num_zones > 0) {      if(create->num_zones > 0) {
# Line 178  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    
# Line 188  Line 213 
213          pcreate.zones = pEnc->zones;          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 226  Line 253 
253          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
254    
255      /* max keyframe interval */      /* max keyframe interval */
256      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;  
257    
258      /* allocate working frame-image memory */      /* allocate working frame-image memory */
259    
# Line 249  Line 275 
275          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
276                  goto xvid_err_memory2;                  goto xvid_err_memory2;
277    
278            /* allocate quant matrix memory */
279    
280            pEnc->mbParam.mpeg_quant_matrices =
281                    xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
282    
283            if (pEnc->mbParam.mpeg_quant_matrices == NULL)
284                    goto xvid_err_memory2a;
285    
286          /* allocate interpolation image memory */          /* allocate interpolation image memory */
287    
288      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
# Line 264  Line 298 
298          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
299          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
300          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
301          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
302    
303          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
304          if (image_create          if (image_create
# Line 310  Line 342 
342                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
343                  goto xvid_err_memory3;                  goto xvid_err_memory3;
344          if (image_create          if (image_create
                 (&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create  
345                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
346                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
347                  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;  
348    
349  /* Create full bitplane for GMC, this might be wasteful */  /* Create full bitplane for GMC, this might be wasteful */
350          if (image_create          if (image_create
# Line 390  Line 414 
414                  image_null(&pEnc->queue[n].image);                  image_null(&pEnc->queue[n].image);
415    
416    
417          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
         {  
418                  if (image_create                  if (image_create
419                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
420                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
421                          goto xvid_err_memory5;                          goto xvid_err_memory5;
   
422          }          }
423    
   
424          /* timestamp stuff */          /* timestamp stuff */
425    
426          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
# Line 415  Line 436 
436      create->handle = (void *) pEnc;      create->handle = (void *) pEnc;
437    
438          init_timer();          init_timer();
439            init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
440    
441      return 0;   /* ok */      return 0;   /* ok */
442    
# Line 424  Line 446 
446    
447    xvid_err_memory5:    xvid_err_memory5:
448    
449          if (pEnc->mbParam.max_bframes > 0) {          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
450          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,  
451                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
452                  }                  }
453    
454                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
455    
456    xvid_err_memory4:    xvid_err_memory4:
457    
# Line 446  Line 465 
465    
466                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
467                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
468                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
469                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
470                  }                  }
471    
472                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 480  Line 496 
496                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
497          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
498                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
499          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
500                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
501    
502  /* destroy GMC image */  /* destroy GMC image */
503          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
504                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
505    
506      xvid_err_memory2a:
507            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
508    
509    xvid_err_memory2:    xvid_err_memory2:
510          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
# Line 508  Line 522 
522    xvid_err_memory0:    xvid_err_memory0:
523      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
524          if (pEnc->plugins[n].func) {          if (pEnc->plugins[n].func) {
525              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);
526          }          }
527      }      }
528      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
# Line 539  Line 553 
553          int i;          int i;
554    
555          /* B Frames specific */          /* B Frames specific */
         if (pEnc->mbParam.max_bframes > 0) {  
   
556                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
   
557                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
558                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
559                  }                  }
                 xvid_free(pEnc->queue);  
         }  
560    
561            xvid_free(pEnc->queue);
562    
563          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
564    
# Line 559  Line 569 
569    
570                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
571                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
572                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
573                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
574                  }                  }
575    
# Line 579  Line 587 
587                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
588          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
589                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
590          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
591                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
   
592          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
593                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
594          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
# Line 624  Line 627 
627    
628          for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<pEnc->num_plugins;i++) {
629              if (pEnc->plugins[i].func) {              if (pEnc->plugins[i].func) {
630                  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);
631              }              }
632          }          }
633          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
634      }      }
635    
636            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
637    
638      if (pEnc->num_plugins>0)      if (pEnc->num_plugins>0)
639          xvid_free(pEnc->zones);          xvid_free(pEnc->zones);
640    
# Line 664  Line 669 
669      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
670      data.fincr = frame->fincr;      data.fincr = frame->fincr;
671      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
672            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
673            data.bquant_offset = pEnc->mbParam.bquant_offset;
674    
675      for (i=0; i<3; i++) {      for (i=0; i<3; i++) {
676          data.min_quant[i] = pEnc->mbParam.min_quant[i];          data.min_quant[i] = pEnc->mbParam.min_quant[i];
677          data.max_quant[i] = pEnc->mbParam.max_quant[i];          data.max_quant[i] = pEnc->mbParam.max_quant[i];
678      }      }
679    
680      data.reference.csp = XVID_CSP_USER;          data.reference.csp = XVID_CSP_PLANAR;
681      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
682      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
683      data.reference.plane[2] = pEnc->reference->image.v;      data.reference.plane[2] = pEnc->reference->image.v;
# Line 678  Line 685 
685      data.reference.stride[1] = pEnc->mbParam.edged_width/2;      data.reference.stride[1] = pEnc->mbParam.edged_width/2;
686      data.reference.stride[2] = pEnc->mbParam.edged_width/2;      data.reference.stride[2] = pEnc->mbParam.edged_width/2;
687    
688      data.current.csp = XVID_CSP_USER;          data.current.csp = XVID_CSP_PLANAR;
689      data.current.plane[0] = frame->image.y;      data.current.plane[0] = frame->image.y;
690      data.current.plane[1] = frame->image.u;      data.current.plane[1] = frame->image.u;
691      data.current.plane[2] = frame->image.v;      data.current.plane[2] = frame->image.v;
# Line 692  Line 699 
699          data.type = *type;          data.type = *type;
700          data.quant = *quant;          data.quant = *quant;
701    
702                    data.vol_flags = frame->vol_flags;
703                    data.vop_flags = frame->vop_flags;
704                    data.motion_flags = frame->motion_flags;
705    
706            } else if (opt == XVID_PLG_FRAME) {
707                    data.type = coding2type(frame->coding_type);
708                    data.quant = frame->quant;
709    
710                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
711              data.dquant = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
712              data.dquant_stride = pEnc->mbParam.mb_width;              data.dquant_stride = pEnc->mbParam.mb_width;
713                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height);
714          }          }
715    
         data.vol_flags = frame->vol_flags;  
         data.vop_flags = frame->vop_flags;  
         data.motion_flags = frame->motion_flags;  
   
716      } else { /* XVID_PLG_AFTER */      } else { /* XVID_PLG_AFTER */
717          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
718              data.original.csp = XVID_CSP_USER;                          data.original.csp = XVID_CSP_PLANAR;
719              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
720              data.original.plane[1] = original->u;              data.original.plane[1] = original->u;
721              data.original.plane[2] = original->v;              data.original.plane[2] = original->v;
# Line 741  Line 752 
752    
753              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
754              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
755                  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;
756              }              }
757          }          }
758    
# Line 754  Line 765 
765          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
766          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
767    
768          if (stats) {                  /* New code */
769                  stats->type = coding2type(frame->coding_type);                  data.stats.type      = coding2type(frame->coding_type);
770                  stats->quant = frame->quant;                  data.stats.quant     = frame->quant;
771                  stats->vol_flags = frame->vol_flags;                  data.stats.vol_flags = frame->vol_flags;
772                  stats->vop_flags = frame->vop_flags;                  data.stats.vop_flags = frame->vop_flags;
773                  stats->length = frame->length;                  data.stats.length    = frame->length;
774                  stats->hlength = frame->length - (frame->sStat.iTextBits / 8);                  data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
775                  stats->kblks = frame->sStat.kblks;                  data.stats.kblks     = frame->sStat.kblks;
776                  stats->mblks = frame->sStat.mblks;                  data.stats.mblks     = frame->sStat.mblks;
777                  stats->ublks = frame->sStat.ublks;                  data.stats.ublks     = frame->sStat.ublks;
778              stats->sse_y = data.sse_y;                  data.stats.sse_y     = data.sse_y;
779              stats->sse_u = data.sse_u;                  data.stats.sse_u     = data.sse_u;
780              stats->sse_v = data.sse_v;                  data.stats.sse_v     = data.sse_v;
781          }  
782                    if (stats)
783                            *stats = data.stats;
784      }      }
785    
786      /* call plugins */      /* call plugins */
787      for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
788          emms();          emms();
789          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
790              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) {
791                  continue;                  continue;
792              }              }
793          }          }
# Line 786  Line 799 
799          *type = data.type;          *type = data.type;
800          *quant = data.quant > 0 ? data.quant : 2;   /* default */          *quant = data.quant > 0 ? data.quant : 2;   /* default */
801    
802                    frame->vol_flags = data.vol_flags;
803                    frame->vop_flags = data.vop_flags;
804                    frame->motion_flags = data.motion_flags;
805    
806            } else if (opt == XVID_PLG_FRAME) {
807    
808          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
809              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
810              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
# Line 797  Line 816 
816                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
817              }              }
818          }          }
819                    frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
         frame->vol_flags = data.vol_flags;  
         frame->vop_flags = data.vop_flags;  
         frame->motion_flags = data.motion_flags;  
     }  
820  }  }
821    
822    
823    }
824    
825    
826  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
# Line 822  Line 838 
838      pEnc->m_framenum--; /* debug ticker */      pEnc->m_framenum--; /* debug ticker */
839  }  }
840    
841    static __inline void
842    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
843    {
844            if (pMB->cbp == 0) {
845                    /* we want to code dquant but the quantizer value will not be used yet
846                            let's find out if we can postpone dquant to next MB
847                    */
848                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
849                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
850                            return;
851                    } else {
852                            MACROBLOCK * next = pMB + 1;
853                            const MACROBLOCK * prev = pMB - 1;
854                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
855                                    /* mode allows dquant change in the future */
856                                    if (abs(next->quant - prev->quant) <= 2) {
857                                            /* quant change is not out of range */
858                                            pMB->quant = prev->quant;
859                                            pMB->dquant = 0;
860                                            next->dquant = next->quant - prev->quant;
861                                            return;
862                                    }
863                    }
864            }
865            /* couldn't skip this dquant */
866            pMB->mode = MODE_INTER_Q;
867    }
868    
869    
870    
871  static __inline void  static __inline void
# Line 831  Line 875 
875      pCur->ticks = (int32_t)pCur->stamp % time_base;      pCur->ticks = (int32_t)pCur->stamp % time_base;
876                  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) ;
877    
878                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */  #if 0   /* HEAVY DEBUG OUTPUT */
879            fprintf(stderr,"WriteVop:   %d - %d \n",
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
880                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
881                  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",
882                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
883                  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",
884                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
885    #endif
886    }
887    
888  */  static void
889    simplify_par(int *par_width, int *par_height)
890    {
891    
892            int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
893            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
894            int divisor = gcd(_par_width, _par_height);
895    
896            _par_width  /= divisor;
897            _par_height /= divisor;
898    
899            /* 2^8 precision maximum */
900            if (_par_width>255 || _par_height>255) {
901                    float div;
902                    emms();
903                    if (_par_width>_par_height)
904                            div = (float)_par_width/255;
905                    else
906                            div = (float)_par_height/255;
907    
908                    _par_width  = (int)((float)_par_width/div);
909                    _par_height = (int)((float)_par_height/div);
910  }  }
911    
912            *par_width = _par_width;
913            *par_height = _par_height;
914    
915            return;
916    }
917    
918    
919  /*****************************************************************************  /*****************************************************************************
# Line 867  Line 938 
938          int type;          int type;
939          Bitstream bs;          Bitstream bs;
940    
941          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 */
942                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
943    
944          xFrame->out_flags = 0;          xFrame->out_flags = 0;
# Line 904  Line 975 
975    
976                  if (xFrame->quant_intra_matrix)                  if (xFrame->quant_intra_matrix)
977                  {                  {
978                          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));
979                          q->frame.quant_intra_matrix = q->quant_intra_matrix;                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
980                  }                  }
981    
982                  if (xFrame->quant_inter_matrix)                  if (xFrame->quant_inter_matrix)
983                  {                  {
984                          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));
985                          q->frame.quant_inter_matrix = q->quant_inter_matrix;                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
986                  }                  }
987    
# Line 939  Line 1010 
1010                          }                          }
1011    
1012                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1013              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);
1014                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1015    
1016                          goto done;                          goto done;
# Line 965  Line 1036 
1036                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
1037                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
1038    
1039                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1040                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1041                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
1042    
1043                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1044                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1045              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);
1046    
1047              /* flush complete: reset counters */              /* flush complete: reset counters */
1048                  pEnc->flush_bframes = 0;                  pEnc->flush_bframes = 0;
# Line 999  Line 1070 
1070                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1071    
1072              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) {
1073                  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);
1074              }              }
1075    
1076              /* if the very last frame is to be b-vop, we must change it to a p-vop */              /* if the very last frame is to be b-vop, we must change it to a p-vop */
# Line 1010  Line 1081 
1081                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1082    
1083                                  /* convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
1084                  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;
1085                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1086                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1087    
1088                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1089                              image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 1020  Line 1093 
1093                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1094                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1095                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1096                                    pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1097    
1098                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs);
1099    
1100    
1101                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1102                      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);
1103                  }else{                  }else{
1104                      pEnc->flush_bframes = 1;                      pEnc->flush_bframes = 1;
1105                      goto done;                      goto done;
# Line 1055  Line 1129 
1129    
1130      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1131      inc_frame_num(pEnc);      inc_frame_num(pEnc);
1132      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;          pEnc->current->vol_flags = frame->vol_flags;
1133      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1134          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1135          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
# Line 1074  Line 1148 
1148          type = frame->type;          type = frame->type;
1149          pEnc->current->quant = frame->quant;          pEnc->current->quant = frame->quant;
1150    
1151      call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1152    
1153      if (type > 0){      /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
1154                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
# Line 1085  Line 1159 
1159                  }else{                  }else{
1160                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1161                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1162                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1163                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1164                  }                  }
1165          }          }
1166    
1167            if (type != I_VOP)
1168                    pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1169    
1170      /* bframes buffer overflow check */      /* bframes buffer overflow check */
1171      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1172          type = P_VOP;          type = P_VOP;
# Line 1098  Line 1176 
1176    
1177          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1178                  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,
1179                          "%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);
1180          }          }
1181    
1182          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1145  Line 1223 
1223      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)
1224      {      {
1225          if (pEnc->current->stamp > 0) {          if (pEnc->current->stamp > 0) {
1226              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);
1227          }          }
1228                  else                  else
1229                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
# Line 1173  Line 1251 
1251                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1252    
1253                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1254                          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");
1255                  }                  }
1256    
1257                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
1258                  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;
1259                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1260                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1261          type = P_VOP;          type = P_VOP;
1262      }      }
1263    
# Line 1199  Line 1279 
1279                  pEnc->iFrameNum = 1;                  pEnc->iFrameNum = 1;
1280    
1281                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1282                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1283    
1284                    /* Aspect ratio */
1285                    switch(frame->par) {
1286                    case XVID_PAR_11_VGA:
1287                    case XVID_PAR_43_PAL:
1288                    case XVID_PAR_43_NTSC:
1289                    case XVID_PAR_169_PAL:
1290                    case XVID_PAR_169_NTSC:
1291                    case XVID_PAR_EXT:
1292                            pEnc->mbParam.par = frame->par;
1293                            break;
1294                    default:
1295                            pEnc->mbParam.par = XVID_PAR_11_VGA;
1296                            break;
1297                    }
1298    
1299                    /* For extended PAR only, we try to sanityse/simplify par values */
1300                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1301                            pEnc->mbParam.par_width  = frame->par_width;
1302                            pEnc->mbParam.par_height = frame->par_height;
1303                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1304                    }
1305    
1306          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1307                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1308                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1309                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
1310                                  set_inter_matrix(frame->quant_inter_matrix);                                  set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1311                  }                  }
1312    
1313          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1314    
         if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
             pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;  
   
1315          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1316              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1317    
# Line 1245  Line 1344 
1344                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1345          }          }
1346    
1347                  FrameCodeP(pEnc, &bs, 1, 0);                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1348                            /* N-VOP, we mustn't code b-frames yet */
1349                            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1350                            goto done;
1351                    }
1352      }      }
1353    
1354    
# Line 1253  Line 1356 
1356           * on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1357           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1358    
1359  done_flush:  /*done_flush:*/
1360    
1361      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1362    
# Line 1264  Line 1367 
1367    
1368      /* packed or no-bframes or no-bframes-queued: output stats */      /* packed or no-bframes or no-bframes-queued: output stats */
1369      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 ) {
1370          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);
1371          }          }
1372    
1373          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1283  Line 1386 
1386    
1387  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1388  {  {
1389      unsigned int i,j;          unsigned int i;
1390      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1391            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1392            if (quant > 31)
1393                    frame->quant = quant = 31;
1394            else if (quant < 1)
1395                    frame->quant = quant = 1;
1396    
1397      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];  
1398          quant += pMB->dquant;          quant += pMB->dquant;
1399          if (quant > 31)          if (quant > 31)
1400                          quant = 31;                          quant = 31;
1401                  if (quant < 1)                  else if (quant < 1)
1402                          quant = 1;                          quant = 1;
1403          pMB->quant = quant;          pMB->quant = quant;
1404                    pMB++;
1405      }      }
1406  }  }
1407    
# Line 1332  Line 1439 
1439    
1440          uint16_t x, y;          uint16_t x, y;
1441    
         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();  
         }  
   
1442          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1443          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1444          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1445    
1446            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1447    
1448      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1449    
1450          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1451    
1452          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1453    
1454          BitstreamPadAlways(bs);          BitstreamPad(bs);
1455          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1456            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1457    
1458          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1459          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
# Line 1378  Line 1474 
1474                          stop_prediction_timer();                          stop_prediction_timer();
1475    
1476                          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;  
                         }  
1477                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1478                          stop_coding_timer();                          stop_coding_timer();
1479                  }                  }
1480    
         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);  
         }  
1481          emms();          emms();
1482    
1483  /* 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() */
1484  #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  
1485      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1486    
1487          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1488          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1489    
1490            pEnc->current->is_edged = 0; /* not edged */
1491            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1492    
1493          return 1;                                       /* intra */          return 1;                                       /* intra */
1494  }  }
1495    
1496    static __inline void
1497    updateFcode(Statistics * sStat, Encoder * pEnc)
1498    {
1499            float fSigma;
1500            int iSearchRange;
1501    
1502            if (sStat->iMvCount == 0)
1503                    sStat->iMvCount = 1;
1504    
1505  #define INTRA_THRESHOLD 0.5          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
 #define BFRAME_SKIP_THRESHHOLD 30  
1506    
1507            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1508    
1509            if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1510                    pEnc->mbParam.m_fcode++;
1511    
1512            else if ((5.0 * fSigma < iSearchRange)
1513                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1514                               && (pEnc->mbParam.m_fcode >= 2) )
1515                    pEnc->mbParam.m_fcode--;
1516    
1517            pEnc->fMvPrevSigma = fSigma;
1518    }
1519    
1520    #define BFRAME_SKIP_THRESHHOLD 30
1521    
1522  /* FrameCodeP also handles S(GMC)-VOPs */  /* FrameCodeP also handles S(GMC)-VOPs */
1523  static int  static int
1524  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1525                     Bitstream * bs,                     Bitstream * bs)
                    bool force_inter,  
                    bool vol_header)  
1526  {  {
         float fSigma;  
1527      int bits = BitstreamPos(bs);      int bits = BitstreamPos(bs);
1528    
1529          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1530          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1531    
         int iLimit;  
1532          int x, y, k;          int x, y, k;
         int iSearchRange;  
         int bIntra=0, skip_possible;  
1533          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1534          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1535    MBParam * const pParam = &pEnc->mbParam;    MBParam * const pParam = &pEnc->mbParam;
1536          int mb_width = pParam->mb_width;          int mb_width = pParam->mb_width;
1537          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1538            int coded = 1;
1539    
   
         /* IMAGE *pCurrent = &current->image; */  
1540          IMAGE *pRef = &reference->image;          IMAGE *pRef = &reference->image;
1541    
1542          if ((current->vop_flags & XVID_VOP_REDUCED))          if (!reference->is_edged) {
         {  
                 mb_width = (pParam->width + 31) / 32;  
                 mb_height = (pParam->height + 31) / 32;  
         }  
   
   
1543          start_timer();          start_timer();
1544          image_setedges(pRef, pParam->edged_width, pParam->edged_height,          image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1545                                     pParam->width, pParam->height);                                             pParam->width, pParam->height, 0);
1546          stop_edges_timer();          stop_edges_timer();
1547                    reference->is_edged = 1;
1548            }
1549    
1550          pParam->m_rounding_type = 1 - pParam->m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1551          current->rounding_type = pParam->m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1552          current->fcode = pParam->m_fcode;          current->fcode = pParam->m_fcode;
1553    
         if (!force_inter)  
                 iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);  
         else  
                 iLimit = mb_width * mb_height + 1;  
   
1554          if ((current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1555                    if (reference->is_interpolated != current->rounding_type) {
1556                  start_timer();                  start_timer();
1557                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1558                                                    &pEnc->vInterHV, pParam->edged_width,                                                    &pEnc->vInterHV, pParam->edged_width,
# Line 1474  Line 1560 
1560                                                    (pParam->vol_flags & XVID_VOL_QUARTERPEL),                                                    (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1561                                                    current->rounding_type);                                                    current->rounding_type);
1562                  stop_inter_timer();                  stop_inter_timer();
1563                            reference->is_interpolated = current->rounding_type;
1564                    }
1565          }          }
1566    
1567            current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1568                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1569    
1570          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1571    
1572            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1573    
1574      SetMacroblockQuants(&pEnc->mbParam, current);      SetMacroblockQuants(&pEnc->mbParam, current);
1575    
# Line 1487  Line 1579 
1579                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1580                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1581    
1582                  if (current->motion_flags & XVID_GME_REFINE) {                  if (current->motion_flags & XVID_ME_GME_REFINE) {
1583                          gmcval = GlobalMotionEstRefine(&current->warp,                          gmcval = GlobalMotionEstRefine(&current->warp,
1584                                                                  current->mbs, pParam,                                                                  current->mbs, pParam,
1585                                                                  current, reference,                                                                  current, reference,
# Line 1496  Line 1588 
1588                                                                  &pEnc->vInterH,                                                                  &pEnc->vInterH,
1589                                                                  &pEnc->vInterV,                                                                  &pEnc->vInterV,
1590                                                                  &pEnc->vInterHV);                                                                  &pEnc->vInterHV);
1591                          gmcval += /*current->quant */ 2 * (int)(pParam->mb_width*pParam->mb_height);                  } else {
                 }  
   
1592                  gmcval = globalSAD(&current->warp, pParam, current->mbs,                  gmcval = globalSAD(&current->warp, pParam, current->mbs,
1593                                                          current,                                                          current,
1594                                                          &reference->image,                                                          &reference->image,
1595                                                          &current->image,                                                          &current->image,
1596                                                          pEnc->vGMC.y);                                                          pEnc->vGMC.y);
1597                    }
1598    
1599                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1600    
1601  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
# Line 1532  Line 1624 
1624                  }                  }
1625          }          }
1626    
         bIntra =  
1627                  MotionEstimation(&pEnc->mbParam, current, reference,                  MotionEstimation(&pEnc->mbParam, current, reference,
1628                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1629                                           &pEnc->vGMC, iLimit);                                           &pEnc->vGMC, 256*4096);
1630    
1631    
1632          stop_motion_timer();          stop_motion_timer();
1633    
         if (bIntra == 1) return FrameCodeI(pEnc, bs);  
   
1634          set_timecodes(current,reference,pParam->fbase);          set_timecodes(current,reference,pParam->fbase);
         if (vol_header)  
         {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);  
                 BitstreamPadAlways(bs);  
         }  
   
         BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1);  
   
         current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =  
                 current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;  
1635    
1636            BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1637    
1638          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
1639                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1640                          MACROBLOCK *pMB =                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1641                                  &current->mbs[x + y * pParam->mb_width];                          int skip_possible;
   
                         bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);  
1642    
1643                          if (bIntra) {                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1644                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1645                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1646                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
# Line 1586  Line 1665 
1665                                                                   pParam->height,                                                                   pParam->height,
1666                                                                   pParam->edged_width,                                                                   pParam->edged_width,
1667                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
                                                                  (current->vop_flags & XVID_VOP_REDUCED),  
1668                                                                   current->rounding_type);                                                                   current->rounding_type);
1669    
1670                          stop_comp_timer();                          stop_comp_timer();
1671    
                         if (pMB->dquant != 0) {  
                 pMB->mode = MODE_INTER_Q;  
                         }  
   
1672                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1673    
1674                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1675                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,  
1676                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1677                          }                          }
1678    
1679                            if (pMB->dquant != 0)
1680                                    MBSetDquant(pMB, x, y, &pEnc->mbParam);
1681    
1682    
1683                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1684                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1685                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
# Line 1615  Line 1692 
1692    
1693                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1694    
1695                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                                                         (pMB->dquant == 0);  
1696    
1697                          if (current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1698                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1699                          else if (current->coding_type == P_VOP) {                          else { /* PVOP */
1700                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1701                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                                                                  pMB->qmvs : pMB->mvs;
1702                                  else                                  skip_possible &= ((mv->x|mv->y) == 0);
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
1703                          }                          }
1704    
1705                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
   
1706  /* 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 (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */  
                                 {  
1707                                          int bSkip = 1;                                          int bSkip = 1;
1708    
1709                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1710                                          {  
1711                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1712                                                  int iSAD;                                                  int iSAD;
1713                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1714                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,                                                                          pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1715                                                                  pParam->edged_width,BFRAME_SKIP_THRESHHOLD);                                                                                  pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1716                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1717                                                  {       bSkip = 0;                                                          bSkip = 0; /* could not SKIP */
1718                                                          break;                                                          if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
                                                 }  
                                         }  
   
                                         if (!bSkip) {   /* no SKIP, but trivial block */  
                                                 if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
1719                                                          VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1720                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1721                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1722                                                  }                                                          } else {
                                                 else {  
1723                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1724                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1725                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1726                                                  }                                                  }
1727                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1728                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1729                                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                                                          break;
1730                                                  stop_coding_timer();                                                  }
   
                                                 continue;       /* next MB */  
1731                                          }                                          }
1732                                  }                                  }
                                 /* do SKIP */  
1733    
1734                                    if (bSkip) {
1735                                            /* do SKIP */
1736                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1737                                  MBSkip(bs);                                  MBSkip(bs);
1738                                  stop_coding_timer();                                  stop_coding_timer();
1739                                  continue;       /* next MB */                                  continue;       /* next MB */
1740                          }                          }
                         /* ordinary case: normal coded INTER/INTER4V block */  
   
                         if ((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((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->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(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);  
                         } else {  
                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;  
                                 DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", 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((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;  
                                 DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", 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(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;  
                                 DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);  
                                         }  
   
                                 }  
1741                          }                          }
1742    
1743                            /* ordinary case: normal coded INTER/INTER4V block */
1744                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1745                          stop_coding_timer();                          stop_coding_timer();
   
1746                  }                  }
1747          }          }
1748    
         if ((current->vop_flags & XVID_VOP_REDUCED))  
         {  
                 image_deblock_rrv(&current->image, pParam->edged_width,  
                         current->mbs, mb_width, mb_height, pParam->mb_width,  
                         16, 0);  
         }  
   
1749          emms();          emms();
1750            updateFcode(&current->sStat, pEnc);
         if (current->sStat.iMvCount == 0)  
                 current->sStat.iMvCount = 1;  
   
         fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);  
   
         iSearchRange = 1 << (3 + pParam->m_fcode);  
   
         if ((fSigma > iSearchRange / 3)  
         && (pParam->m_fcode <= (3 +  (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0)  ))) /* maximum search range 128 */  
         {  
                 pParam->m_fcode++;  
                 iSearchRange *= 2;  
         } else if ((fSigma < iSearchRange / 6)  
                            && (pEnc->fMvPrevSigma >= 0)  
                            && (pEnc->fMvPrevSigma < iSearchRange / 6)  
                            && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) )))        /* minimum search range 16 */  
         {  
                 pParam->m_fcode--;  
                 iSearchRange /= 2;  
         }  
   
         pEnc->fMvPrevSigma = fSigma;  
1751    
1752          /* frame drop code */          /* frame drop code */
1753  #if 0  #if 0
1754          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1755  #endif  #endif
1756          if (current->sStat.kblks + current->sStat.mblks <          if (current->sStat.kblks + current->sStat.mblks <=
1757                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100)                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
1758                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
1759          {          {
1760                  current->sStat.kblks = current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = 0;
1761                  current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
# Line 1764  Line 1763 
1763                  BitstreamReset(bs);                  BitstreamReset(bs);
1764    
1765                  set_timecodes(current,reference,pParam->fbase);                  set_timecodes(current,reference,pParam->fbase);
1766                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1767    
1768                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
1769                  current->quant = reference->quant;                  current->quant = reference->quant;
# Line 1772  Line 1771 
1771                  current->rounding_type = reference->rounding_type;                  current->rounding_type = reference->rounding_type;
1772                  current->fcode = reference->fcode;                  current->fcode = reference->fcode;
1773                  current->bcode = reference->bcode;                  current->bcode = reference->bcode;
1774                    current->stamp = reference->stamp;
1775                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1776                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1777                    coded = 0;
1778    
1779            } else {
1780    
1781                    pEnc->current->is_edged = 0; /* not edged */
1782                    pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1783    
1784                    /* what was this frame's interpolated reference will become
1785                            forward (past) reference in b-frame coding */
1786    
1787                    image_swap(&pEnc->vInterH, &pEnc->f_refh);
1788                    image_swap(&pEnc->vInterV, &pEnc->f_refv);
1789                    image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1790          }          }
1791    
1792          /* XXX: debug          /* XXX: debug
# Line 1791  Line 1804 
1804          }          }
1805          */          */
1806    
1807  /* 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 ((pParam->global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)  
 #endif  
                 BitstreamPadAlways(bs);  
 #if 0  
         else  
                 BitstreamPad(bs);  
 #endif  
1808    
1809      current->length = (BitstreamPos(bs) - bits) / 8;      current->length = (BitstreamPos(bs) - bits) / 8;
1810    
1811          return 0;                                       /* inter */          return coded;
1812  }  }
1813    
1814    
# Line 1828  Line 1832 
1832                  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); \
1833          }          }
1834    
         /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */  
   
1835          if (!first){          if (!first){
1836                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1837          }          }
1838  #endif  #endif
1839    
1840          /* forward  */          /* forward  */
1841            if (!pEnc->reference->is_edged) {
1842          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1843                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1844                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1845                    pEnc->current->is_edged = 1;
1846            }
1847    
1848            if (pEnc->reference->is_interpolated != 0) {
1849          start_timer();          start_timer();
1850          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1851                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1852                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1853          stop_inter_timer();          stop_inter_timer();
1854                    pEnc->reference->is_interpolated = 0;
1855            }
1856    
1857          /* backward */          /* backward */
1858            if (!pEnc->current->is_edged) {
1859          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1860                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1861                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
1862                    pEnc->current->is_edged = 1;
1863            }
1864    
1865            if (pEnc->current->is_interpolated != 0) {
1866          start_timer();          start_timer();
1867          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1868                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1869                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1870          stop_inter_timer();          stop_inter_timer();
1871                    pEnc->current->is_interpolated = 0;
1872            }
1873    
1874            frame->coding_type = B_VOP;
1875            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1876    
1877          start_timer();          start_timer();
1878          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
# Line 1865  Line 1884 
1884                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
1885          stop_motion_timer();          stop_motion_timer();
1886    
         frame->coding_type = B_VOP;  
   
1887          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1888          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
1889    
1890          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1891          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
# Line 1880  Line 1897 
1897          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1898                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1899                          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;  
1900    
1901                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1902                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1903                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1904                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1905                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
1906                                    }
1907                                  continue;                                  continue;
1908                          }                          }
1909    
1910                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                          mb->quant = frame->quant;
1911    
1912                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1913                                    /* we have to motion-compensate, transfer etc,
1914                                            because there might be blocks to code */
1915    
1916                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1917                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1918                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1919                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1920                                                                           dct_codes);                                                                           dct_codes);
1921    
                                 if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;  
                                 mb->quant = frame->quant;  
   
                                 if (mb->mode != MODE_DIRECT_NONE_MV)  
1922                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1923                            }
1924    
1925                            if (mb->mode == MODE_DIRECT_NO4V)
1926                                    mb->mode = MODE_DIRECT;
1927    
1928                                  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) ) {  
1929                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
1930                                  }                          else
1931                          }                                  if (frame->vop_flags & XVID_VOP_GREYSCALE)
1932                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
1933                                            mb->cbp &= 0x3C;
1934    
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
1935                          start_timer();                          start_timer();
1936                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1937                                                   &frame->sStat, direction);                                                   &frame->sStat);
1938                          stop_coding_timer();                          stop_coding_timer();
1939                  }                  }
1940          }          }
1941    
1942          emms();          emms();
1943    
1944          /* TODO: dynamic fcode/bcode ??? */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
   
     BitstreamPadAlways(bs);  
1945          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1946    
1947  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.33  
changed lines
  Added in v.1.120

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