[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.5, Sat Mar 15 14:32:56 2003 UTC revision 1.95.2.28, Mon Jun 9 19:20:56 2003 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002      Michael Militzer <isibaar@xvid.org>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2003 Peter Ross <pross@xvid.org>
8   *  to use this software module in hardware or software products are   *               2002      Daniel Smith <danielsmith@astroboymail.com>
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 49  Line 44 
44  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
45  #include "utils/emms.h"  #include "utils/emms.h"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "quant/adapt_quant.h"  
47  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
48  #include "utils/mem_align.h"  #include "utils/mem_align.h"
49    
50  /*****************************************************************************  /*****************************************************************************
  * Local macros  
  ****************************************************************************/  
   
 #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }  
   
 /*****************************************************************************  
51   * Local function prototypes   * Local function prototypes
52   ****************************************************************************/   ****************************************************************************/
53    
# Line 75  Line 63 
63                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
64                                             Bitstream * bs);                                             Bitstream * bs);
65    
 /*****************************************************************************  
  * Local data  
  ****************************************************************************/  
   
 static int DQtab[4] = {  
         -1, -2, 1, 2  
 };  
   
 static int iDQtab[5] = {  
         1, 0, NO_CHANGE, 2, 3  
 };  
   
66    
67  /*****************************************************************************  /*****************************************************************************
68   * Encoder creation   * Encoder creation
# Line 134  Line 110 
110    
111    
112  int  int
113  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)  enc_create(xvid_enc_create_t * create)
114  {  {
115          Encoder *pEnc;          Encoder *pEnc;
116      int n;      int n;
117    
118          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1))   /* v1.x.x */          if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */
119                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
120    
121          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
# Line 152  Line 128 
128                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
129          memset(pEnc, 0, sizeof(Encoder));          memset(pEnc, 0, sizeof(Encoder));
130    
131        pEnc->mbParam.profile = create->profile;
132    
133          /* global flags */          /* global flags */
134      pEnc->mbParam.global_flags = create->global;      pEnc->mbParam.global_flags = create->global;
135    
# Line 166  Line 144 
144      /* framerate */      /* framerate */
145      pEnc->mbParam.fincr = MAX(create->fincr, 0);      pEnc->mbParam.fincr = MAX(create->fincr, 0);
146          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
147        if (pEnc->mbParam.fincr>0)
148          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
149    
150      /* plugin */      /* zones */
151        if(create->num_zones > 0) {
152                    pEnc->num_zones = create->num_zones;
153                    pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
154                    if (pEnc->zones == NULL)
155                            goto xvid_err_memory0;
156            memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
157            } else {
158                    pEnc->num_zones = 0;
159                    pEnc->zones = NULL;
160            }
161    
162        /* plugins */
163        if(create->num_plugins > 0) {
164      pEnc->num_plugins = create->num_plugins;      pEnc->num_plugins = create->num_plugins;
165      pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);      pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
166      if (pEnc->plugins == NULL)      if (pEnc->plugins == NULL)
167          goto xvid_err_memory0;          goto xvid_err_memory0;
168            } else {
169                    pEnc->num_plugins = 0;
170                    pEnc->plugins = NULL;
171            }
172    
173      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
174          xvid_plg_create_t pcreate;          xvid_plg_create_t pcreate;
# Line 186  Line 182 
182    
183          memset(&pcreate, 0, sizeof(xvid_plg_create_t));          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
184          pcreate.version = XVID_VERSION;          pcreate.version = XVID_VERSION;
185            pcreate.num_zones = pEnc->num_zones;
186            pcreate.zones = pEnc->zones;
187          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
188          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
189          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
# Line 198  Line 196 
196          }          }
197      }      }
198    
199        if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
200            (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
201            pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
202        }
203    
204      /* temp dquants */      /* temp dquants */
205        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
206          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
207                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
208        }
209      /* XXX: error checking */      /* XXX: error checking */
210    
211          /* bframes */          /* bframes */
# Line 208  Line 213 
213          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
214          pEnc->mbParam.bquant_offset = create->bquant_offset;          pEnc->mbParam.bquant_offset = create->bquant_offset;
215    
216        /* min/max quant */
217        for (n=0; n<3; n++) {
218            pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
219            pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
220        }
221    
222          /* frame drop ratio */          /* frame drop ratio */
223          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
224    
225      /* max keyframe interval */      /* max keyframe interval */
226      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval;      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ?
227      /*XXX: replace 250 hard code with "10seconds * framerate" */                  10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval;
228    
229          /* Bitrate allocator defaults          /* Bitrate allocator defaults
230    
# Line 498  Line 509 
509          xvid_free(pEnc->current);          xvid_free(pEnc->current);
510          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
511    
512        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
513          xvid_free(pEnc->temp_dquants);          xvid_free(pEnc->temp_dquants);
514        }
515    
516    xvid_err_memory0:    xvid_err_memory0:
517      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
# Line 508  Line 521 
521      }      }
522      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
523    
524        xvid_free(pEnc->zones);
525    
526          xvid_free(pEnc);          xvid_free(pEnc);
527    
528          create->handle = NULL;          create->handle = NULL;
# Line 585  Line 600 
600                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
601          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
602                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
603            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
604                                      pEnc->mbParam.edged_height);
605    
606          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
607                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
# Line 601  Line 618 
618          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
619          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
620    
621        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
622      xvid_free(pEnc->temp_dquants);      xvid_free(pEnc->temp_dquants);
623        }
624    
625    
626        if (pEnc->num_plugins>0) {
627            xvid_plg_destroy_t pdestroy;
628            memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
629    
630            pdestroy.version = XVID_VERSION;
631            pdestroy.num_frames = pEnc->m_framenum;
632    
633      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
634          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
635              pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0);                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0);
636          }          }
637      }      }
638      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
639        }
640    
641        if (pEnc->num_plugins>0)
642            xvid_free(pEnc->zones);
643    
644          xvid_free(pEnc);          xvid_free(pEnc);
645    
# Line 620  Line 651 
651    call the plugins    call the plugins
652    */    */
653    
654  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, int opt, int * type, int * quant)  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
655                             int opt, int * type, int * quant, xvid_enc_stats_t * stats)
656  {  {
657      int i;      unsigned int i, j;
658      xvid_plg_data_t data;      xvid_plg_data_t data;
659    
660        /* set data struct */
661    
662      memset(&data, 0, sizeof(xvid_plg_data_t));      memset(&data, 0, sizeof(xvid_plg_data_t));
663      data.version = XVID_VERSION;      data.version = XVID_VERSION;
664    
665        /* find zone */
666        for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
667        data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
668    
669      data.width = pEnc->mbParam.width;      data.width = pEnc->mbParam.width;
670      data.height = pEnc->mbParam.height;      data.height = pEnc->mbParam.height;
671      data.fincr = pEnc->mbParam.fincr;      data.mb_width = pEnc->mbParam.mb_width;
672        data.mb_height = pEnc->mbParam.mb_height;
673        data.fincr = frame->fincr;
674      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
675    
676        for (i=0; i<3; i++) {
677            data.min_quant[i] = pEnc->mbParam.min_quant[i];
678            data.max_quant[i] = pEnc->mbParam.max_quant[i];
679        }
680    
681      data.reference.csp = XVID_CSP_USER;      data.reference.csp = XVID_CSP_USER;
682      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
683      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
# Line 653  Line 698 
698    
699      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
700          data.type = XVID_TYPE_AUTO;          data.type = XVID_TYPE_AUTO;
701          data.quant = 2;          data.quant = 0;
702          //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);  
703          //data.qscale_stride = pEnc->mbParam.width;                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
704          //data.qscale_table = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
705          /* todo: vol,vop,motion flags */              data.dquant_stride = pEnc->mbParam.mb_width;
706                            memset(data.dquant, 0, data.mb_width*data.mb_height);
707            }
708    
709            /* todo: [vol,vop,motion]_flags*/
710    
711      } else { // XVID_PLG_AFTER      } else { /* XVID_PLG_AFTER */
712          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
713              data.original.csp = XVID_CSP_USER;              data.original.csp = XVID_CSP_USER;
714              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
# Line 670  Line 719 
719              data.original.stride[2] = pEnc->mbParam.edged_width/2;              data.original.stride[2] = pEnc->mbParam.edged_width/2;
720          }          }
721    
722            if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
723                (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
724    
725                            data.sse_y =
726                                plane_sse( original->y, frame->image.y,
727                                                   pEnc->mbParam.edged_width, pEnc->mbParam.width,
728                                                   pEnc->mbParam.height);
729    
730                data.sse_u =
731                    plane_sse( original->u, frame->image.u,
732                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
733                                                   pEnc->mbParam.height/2);
734    
735                            data.sse_v =
736                    plane_sse( original->v, frame->image.v,
737                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
738                                                   pEnc->mbParam.height/2);
739            }
740    
741          data.type = coding2type(frame->coding_type);          data.type = coding2type(frame->coding_type);
742          data.quant = frame->quant;          data.quant = frame->quant;
743          /* todo: data.qscale */  
744            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
745                data.dquant = pEnc->temp_dquants;
746                data.dquant_stride = pEnc->mbParam.mb_width;
747    
748                for (j=0; j<pEnc->mbParam.mb_height; j++)
749                for (i=0; i<pEnc->mbParam.mb_width; i++) {
750                    data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;
751                }
752            }
753    
754          data.vol_flags = frame->vol_flags;          data.vol_flags = frame->vol_flags;
755          data.vop_flags = frame->vop_flags;          data.vop_flags = frame->vop_flags;
756          data.motion_flags = frame->motion_flags;          data.motion_flags = frame->motion_flags;
# Line 681  Line 759 
759          data.kblks = frame->sStat.kblks;          data.kblks = frame->sStat.kblks;
760          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
761          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
762    
763            if (stats) {
764                    stats->type = coding2type(frame->coding_type);
765                    stats->quant = frame->quant;
766                    stats->vol_flags = frame->vol_flags;
767                    stats->vop_flags = frame->vop_flags;
768                    stats->length = frame->length;
769                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
770                    stats->kblks = frame->sStat.kblks;
771                    stats->mblks = frame->sStat.mblks;
772                    stats->ublks = frame->sStat.ublks;
773                stats->sse_y = data.sse_y;
774                stats->sse_u = data.sse_u;
775                stats->sse_v = data.sse_v;
776            }
777      }      }
778    
779        /* call plugins */
780      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
781            emms();
782          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
783              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
784                  continue;                  continue;
785              }              }
786          }          }
787      }      }
788        emms();
789    
790        /* copy modified values back into frame*/
791      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
792          *type = data.type;          *type = data.type;
793          *quant = data.quant;          *quant = data.quant > 0 ? data.quant : 2;   /* default */
         /* todo: copy modified qscale,vol,vop,motion flags into the frame*/  
     }  
794    
795            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
796                for (j=0; j<pEnc->mbParam.mb_height; j++)
797                for (i=0; i<pEnc->mbParam.mb_width; i++) {
798                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
799                }
800            }else{
801                for (j=0; j<pEnc->mbParam.mb_height; j++)
802                for (i=0; i<pEnc->mbParam.mb_width; i++) {
803                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
804                }
805            }
806            /* todo: [vol,vop,motion]_flags*/
807        }
808  }  }
809    
810    
# Line 707  Line 815 
815      pEnc->current->frame_num = pEnc->m_framenum;      pEnc->current->frame_num = pEnc->m_framenum;
816          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
817    
818      pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;      pEnc->mbParam.m_stamp += pEnc->current->fincr;
819      pEnc->m_framenum++; /* debug ticker */      pEnc->m_framenum++; /* debug ticker */
820  }  }
821    
# Line 739  Line 847 
847  }  }
848    
849    
 static void  
 set_stats(xvid_enc_stats_t * stats, const MBParam * pParam, const FRAMEINFO * frame)  
 {  
         if (stats)  
         {  
                 stats->type = coding2type(frame->coding_type);  
                 stats->quant = frame->quant;  
                 stats->vol_flags = frame->vol_flags;  
                 stats->vop_flags = frame->vop_flags;  
                 stats->length = frame->length;  
                 stats->hlength = frame->length - (frame->sStat.iTextBits / 8);  
                 stats->kblks = frame->sStat.kblks;  
                 stats->mblks = frame->sStat.mblks;  
                 stats->ublks = frame->sStat.ublks;  
         }  
 }  
   
   
850    
851  /*****************************************************************************  /*****************************************************************************
852   * IPB frame encoder entry point   * IPB frame encoder entry point
# Line 801  Line 891 
891                  if (image_input                  if (image_input
892                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
893                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
894                          xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))                          xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
895                  {                  {
896                          emms();                          emms();
897                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
898                  }                  }
899                  stop_conv_timer();                  stop_conv_timer();
900    
901                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {                  if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
902                          image_chroma_optimize(&q->image,                          image_chroma_optimize(&q->image,
903                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
904                  }                  }
# Line 842  Line 932 
932          {          {
933                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
934    
935                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
936                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
937                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
938    
# Line 852  Line 942 
942                          }                          }
943    
944                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
945                call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);
             emms();  
             set_stats(stats, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]);  
             call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0);  
             emms();  
   
946                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
947    
948                          goto done;                          goto done;
# Line 870  Line 955 
955                     indentical to the future-referece frame.                     indentical to the future-referece frame.
956                  */                  */
957    
958                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
959                          int tmp;                          int tmp;
960                          int bits;                          int bits;
961    
962                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
963                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
964                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
965    
# Line 889  Line 974 
974    
975                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
976                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
977              emms();              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
             set_stats(stats, &pEnc->mbParam, pEnc->current);  
             call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);  
             emms();  
978    
979              /* flush complete: reset counters */              /* flush complete: reset counters */
980                  pEnc->flush_bframes = 0;                  pEnc->flush_bframes = 0;
# Line 915  Line 997 
997                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
998                  {                  {
999    
1000              if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) {              DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1001                  emms();                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1002                              set_stats(stats, &pEnc->mbParam, pEnc->current);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1003                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);  
1004                  emms();              if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1005                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1006              }              }
1007    
1008              /* 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 */
1009              if (pEnc->bframenum_tail > 0)              if (pEnc->bframenum_tail > 0) {
                         {  
1010    
1011                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1012                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
# Line 938  Line 1020 
1020                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1021                  }                  }
1022    
1023                    DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1024                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1025                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1026    
1027                                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs, 1, 0);
1028    
1029                                  goto done_flush;  
1030                    if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1031                        call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1032                    }else{
1033                        pEnc->flush_bframes = 1;
1034                        goto done;
1035                          }                          }
1036                }
1037                DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1038    
1039                          emms();                          emms();
1040                          return XVID_ERR_END;    /* end of stream reached */                          return XVID_ERR_END;    /* end of stream reached */
# Line 949  Line 1042 
1042                  goto done;      /* nothing to encode yet; encoder lag */                  goto done;      /* nothing to encode yet; encoder lag */
1043          }          }
1044    
1045          // the current FRAME becomes the reference          /* the current FRAME becomes the reference */
1046          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1047    
1048          // remove frame from encoding-queue (head), and move it into the current          /* remove frame from encoding-queue (head), and move it into the current */
1049          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1050          frame = &pEnc->queue[pEnc->queue_head].frame;          frame = &pEnc->queue[pEnc->queue_head].frame;
1051          pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);          pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
# Line 963  Line 1056 
1056           * init pEnc->current fields           * init pEnc->current fields
1057           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1058    
1059        pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1060        inc_frame_num(pEnc);
1061      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
1062      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1063          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1064          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1065          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
1066    
1067      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {  
1068        if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1069              image_chroma_optimize(&pEnc->current->image,              image_chroma_optimize(&pEnc->current->image,
1070                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1071      }      }
1072    
         emms();         /* float-point region */  
   
         if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {  
         unsigned int x, y;  
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                                           pEnc->mbParam.edged_width, pEnc->temp_dquants,  
                                                           pEnc->current->quant, pEnc->current->quant,  
                                                           2 * pEnc->current->quant,  
                                                           pEnc->mbParam.mb_width,  
                                                           pEnc->mbParam.mb_height);  
   
                 for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
   
 #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)  
   
                         for (x = 0; x < pEnc->mbParam.mb_width; x++) {  
                                 MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];  
   
                                 pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];  
                         }  
   
 #undef OFFSET  
                 }  
   
         }  
   
1073          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1074           * frame type & quant selection           * frame type & quant selection
1075           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1076    
         emms();         /* END floating-point region */  
     call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant);  
     emms();  
   
     if (frame->type > 0)  
1077                  type = frame->type;                  type = frame->type;
   
     if (frame->quant > 0)  
1078                  pEnc->current->quant = frame->quant;                  pEnc->current->quant = frame->quant;
1079    
1080        call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);
1081    
1082      if (type > 0){      /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
1083                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1084      } else{             /* XVID_TYPE_AUTO */      } else{             /* XVID_TYPE_AUTO */
# Line 1025  Line 1088 
1088                  }else{                  }else{
1089                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1090                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1091                                          pEnc->iFrameNum, pEnc->bframenum_tail);                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);
   
             if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) {  
                 type = P_VOP;   /* disable dynamic bframes */  
             }  
1092                  }                  }
1093          }          }
1094    
1095      /* bframes buffer overflow check */      /* bframes buffer overflow check */
1096      if (type != I_VOP) {      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
         if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
1097              type = P_VOP;              type = P_VOP;
         }else{  
             type = B_VOP;  
         }  
1098      }      }
1099    
         inc_frame_num(pEnc);  
1100          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1101    
1102          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1103                  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,
1104                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1105          }          }
# Line 1055  Line 1109 
1109       * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)       * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1110           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1111          if (type == B_VOP) {          if (type == B_VOP) {
1112                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1113                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1114                  }                  }
1115    
1116                  if (frame->bquant < 1) {                  if (frame->quant < 1) {
1117                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1118                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1119    
1120                  } else {                  } else {
1121                          pEnc->current->quant = frame->bquant;                          pEnc->current->quant = frame->quant;
1122                  }                  }
1123    
1124                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1072  Line 1126 
1126                  else if (pEnc->current->quant > 31)                  else if (pEnc->current->quant > 31)
1127              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1128    
1129                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1130                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1131                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1132    
# Line 1085  Line 1139 
1139                  goto repeat;                  goto repeat;
1140      }      }
1141    
1142    
1143                    DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1144                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1145                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1146    
1147      /* for unpacked bframes, output the stats for the last encoded frame */      /* for unpacked bframes, output the stats for the last encoded frame */
1148      if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)      if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1149      {      {
1150          if (pEnc->current->stamp > 0) {          if (pEnc->current->stamp > 0) {
1151              emms();              call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
                         set_stats(stats, &pEnc->mbParam, pEnc->reference);  
             call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);  
             emms();  
1152          }          }
1153                  else                  else
1154                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
# Line 1103  Line 1159 
1159       * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe       * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1160       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1161    
1162      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {      if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1163    
1164                    /* place this frame back on the encoding-queue (head) */
1165                    /* we will deal with it next time */
1166            dec_frame_num(pEnc);
1167            pEnc->iFrameNum--;
1168    
                 // place this frame back on the encoding-queue (head)  
                 // we will deal with it next time  
1169                  pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);                  pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1170          pEnc->queue_size++;          pEnc->queue_size++;
1171                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1172    
1173          dec_frame_num(pEnc);                  /* grab the last frame from the bframe-queue */
         pEnc->iFrameNum--;  
   
                 // grab the last frame from the bframe-queue  
1174    
1175                  pEnc->bframenum_tail--;                  pEnc->bframenum_tail--;
1176                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1177    
1178                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1179                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1180                  }                  }
1181    
# Line 1135  Line 1191 
1191    
1192          if (type == I_VOP) {          if (type == I_VOP) {
1193    
1194                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1195                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1196                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1197    
1198                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1199                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1200                  }                  }
1201    
# Line 1147  Line 1203 
1203                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1204                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1205    
1206          if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1207                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1208                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(frame->quant_intra_matrix);
1209                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
# Line 1156  Line 1212 
1212    
1213          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1214    
1215          if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))          if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))
1216              pEnc->current->vop_flags &= ~XVID_REDUCED;              pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;
1217    
1218          if (!(pEnc->current->vol_flags & XVID_INTERLACING))          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1219              pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1220    
1221                  /* ^^^------------------------ */                  /* ^^^------------------------ */
1222    
# Line 1169  Line 1225 
1225                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1226          }          }
1227    
   
1228                  FrameCodeI(pEnc, &bs);                  FrameCodeI(pEnc, &bs);
1229                  xFrame->out_flags |= XVID_KEYFRAME;                  xFrame->out_flags |= XVID_KEYFRAME;
1230    
# Line 1177  Line 1232 
1232           * encode this frame as an p-vop           * encode this frame as an p-vop
1233       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1234    
1235      } else { // (type == P_VOP || type == S_VOP)      } else { /* (type == P_VOP || type == S_VOP) */
1236    
1237                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1238                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1239                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1240    
1241                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1242                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1243                  }                  }
1244    
# Line 1204  Line 1259 
1259    
1260      pEnc->flush_bframes = 1;      pEnc->flush_bframes = 1;
1261    
1262          /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */          /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1263          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1264                  goto repeat;                  goto repeat;
1265          }          }
1266    
1267      /* packed or no-bframes: output stats */      /* packed or no-bframes or no-bframes-queued: output stats */
1268      if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1269          {          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
         emms();  
                 set_stats(stats, &pEnc->mbParam, pEnc->current);  
         call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);  
         emms();  
1270          }          }
1271    
1272          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1232  Line 1283 
1283  }  }
1284    
1285    
1286    static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1287    {
1288        unsigned int i,j;
1289        int quant = frame->quant;
1290    
1291        for (j=0; j<pParam->mb_height; j++)
1292        for (i=0; i<pParam->mb_width; i++) {
1293            MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];
1294            quant += pMB->dquant;
1295            if (quant > 31)
1296                            quant = 31;
1297                    if (quant < 1)
1298                            quant = 1;
1299            pMB->quant = quant;
1300        }
1301    }
1302    
1303    
1304  static __inline void  static __inline void
1305  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(Encoder * pEnc,
1306                          MACROBLOCK * pMB)                          MACROBLOCK * pMB)
# Line 1245  Line 1314 
1314          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1315          pMB->sad16 = 0;          pMB->sad16 = 0;
1316    
1317          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if (pMB->dquant != 0) {
                 if (pMB->dquant != NO_CHANGE) {  
1318                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
                         pEnc->current->quant += DQtab[pMB->dquant];  
   
                         if (pEnc->current->quant > 31)  
                                 pEnc->current->quant = 31;  
                         if (pEnc->current->quant < 1)  
                                 pEnc->current->quant = 1;  
1319                  }                  }
1320          }          }
1321    
         pMB->quant = pEnc->current->quant;  
 }  
   
1322    
1323    
1324  static int  static int
# Line 1275  Line 1334 
1334    
1335          uint16_t x, y;          uint16_t x, y;
1336    
1337          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))          if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))
1338          {          {
1339                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1340                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1293  Line 1352 
1352          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1353          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1354    
1355        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1356    
1357          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1358    
1359          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
# Line 1319  Line 1380 
1380                          stop_prediction_timer();                          stop_prediction_timer();
1381    
1382                          start_timer();                          start_timer();
1383                          if (pEnc->current->vop_flags & XVID_GREYSCALE)                          if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)
1384                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1385                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1386                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
# Line 1328  Line 1389 
1389                          stop_coding_timer();                          stop_coding_timer();
1390                  }                  }
1391    
1392          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))
1393          {          {
1394                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1395                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
# Line 1336  Line 1397 
1397          }          }
1398          emms();          emms();
1399    
1400    /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */
1401    #if 0
1402          /* for divx5 compatibility, we must always pad between the packed p and b frames */          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1403          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)
1404    #endif
1405                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1406    #if 0
1407          else          else
1408                  BitstreamPad(bs);                  BitstreamPad(bs);
1409    #endif
1410      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1411    
1412          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
# Line 1383  Line 1449 
1449          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1450          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1451    
1452          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))
1453          {          {
1454                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1455                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1397  Line 1463 
1463    
1464          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1465          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1466          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          /* pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel; */
1467          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1468    
1469          if (!force_inter)          if (!force_inter)
# Line 1405  Line 1471 
1471          else          else
1472                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1473    
1474          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {          if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) {
1475                  start_timer();                  start_timer();
1476                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1477                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1478                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1479                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL),
1480                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1481                  stop_inter_timer();                  stop_inter_timer();
1482          }          }
1483    
1484          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1485    
1486    
1487        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1488    
1489          start_timer();          start_timer();
1490          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1491                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
# Line 1430  Line 1499 
1499    
1500          if (bIntra == 1) return FrameCodeI(pEnc, bs);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1501    
1502          if ( ( pEnc->current->vol_flags & XVID_GMC )          if ( ( pEnc->current->vol_flags & XVID_VOL_GMC )
1503                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1504          {          {
1505                  pEnc->current->coding_type = S_VOP;                  pEnc->current->coding_type = S_VOP;
# Line 1442  Line 1511 
1511                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1512                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1513                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1514                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0,
1515                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1516    
1517          }          }
# Line 1491  Line 1560 
1560                                          pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                          pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1561                                          pEnc->mbParam.edged_width, 65536);                                          pEnc->mbParam.edged_width, 65536);
1562    
1563                                  if (pEnc->current->motion_flags & PMV_CHROMA16) {                                  if (pEnc->current->motion_flags & XVID_ME_CHROMA16) {
1564                                          iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,                                          iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1565                                          pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);                                          pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1566    
# Line 1501  Line 1570 
1570    
1571                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1572    
1573                                          if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                          if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))
1574                                                  pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;                                                  pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1575                                          else                                          else
1576                                                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;                                                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
# Line 1524  Line 1593 
1593                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pEnc->mbParam.width,
1594                                                                   pEnc->mbParam.height,                                                                   pEnc->mbParam.height,
1595                                                                   pEnc->mbParam.edged_width,                                                                   pEnc->mbParam.edged_width,
1596                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),                                                                   (pEnc->current->vol_flags & XVID_VOL_QUARTERPEL),
1597                                                                   (pEnc->current->vop_flags & XVID_REDUCED),                                                                   (pEnc->current->vop_flags & XVID_VOP_REDUCED),
1598                                                                   pEnc->current->rounding_type);                                                                   pEnc->current->rounding_type);
1599    
1600                          stop_comp_timer();                          stop_comp_timer();
1601    
1602                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 if (pMB->dquant != NO_CHANGE) {  
1603                                          pMB->mode = MODE_INTER_Q;                                          pMB->mode = MODE_INTER_Q;
                                         pEnc->current->quant += DQtab[pMB->dquant];  
                                         if (pEnc->current->quant > 31)  
                                                 pEnc->current->quant = 31;  
                                         else if (pEnc->current->quant < 1)  
                                                 pEnc->current->quant = 1;  
                                 }  
1604                          }                          }
                         pMB->quant = pEnc->current->quant;  
1605    
1606                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1607    
# Line 1563  Line 1624 
1624                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1625    
1626                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1627                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == 0);
1628    
1629                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1630                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1631                          else if (pEnc->current->coding_type == P_VOP) {                          else if (pEnc->current->coding_type == P_VOP) {
1632                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                  if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL))
1633                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1634                                  else                                  else
1635                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
# Line 1595  Line 1656 
1656                                          }                                          }
1657    
1658                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1659                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                                  if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {
1660                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1661                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1662                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
# Line 1622  Line 1683 
1683                          }                          }
1684                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1685    
1686                          if ((pEnc->current->vop_flags & XVID_GREYSCALE))                          if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE))
1687                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1688                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1689                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1690                          }                          }
1691    
1692                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {
1693                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1694                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1695                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1696                                  DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1697                          } else {                          } else {
1698                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1699                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1700                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1701                                  DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1702                          }                          }
1703    
1704    
# Line 1645  Line 1706 
1706                          {       int k;                          {       int k;
1707                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1708                                  {                                  {
1709                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                          if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) {
1710                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1711                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1712                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1713                                  DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1714                                          } else {                                          } else {
1715                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1716                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1717                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1718                                  DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);                                  DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1719                                          }                                          }
1720    
1721                                  }                                  }
# Line 1666  Line 1727 
1727                  }                  }
1728          }          }
1729    
1730          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))
1731          {          {
1732                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1733                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
# Line 1688  Line 1749 
1749          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1750    
1751          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1752          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0)  ))) /* maximum search range 128 */          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0)  )))     /* maximum search range 128 */
1753          {          {
1754                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1755                  iSearchRange *= 2;                  iSearchRange *= 2;
1756          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1757                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1758                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1759                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) )))        /* minimum search range 16 */                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) )))    /* minimum search range 16 */
1760          {          {
1761                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1762                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1704  Line 1765 
1765          pEnc->fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1766    
1767          /* frame drop code */          /* frame drop code */
1768          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
1769            DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
1770    #endif
1771          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
1772                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
1773          {          {
# Line 1720  Line 1783 
1783                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
1784                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
1785                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
1786                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  /* pEnc->current->quarterpel =  pEnc->reference->quarterpel; */
1787                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1788                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1789                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
# Line 1742  Line 1805 
1805          }          }
1806          */          */
1807    
1808    /* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */
1809    #if 0
1810          /* for divx5 compatibility, we must always pad between the packed p and b frames */          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1811          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0)
1812    #endif
1813                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1814    #if 0
1815          else          else
1816                  BitstreamPad(bs);                  BitstreamPad(bs);
1817    #endif
1818    
1819      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1820    
# Line 1774  Line 1842 
1842                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1843          }          }
1844    
1845          /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */          /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */
1846    
1847          if (!first){          if (!first){
1848                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1849          }          }
1850  #endif  #endif
1851    
1852          //frame->quarterpel =  pEnc->mbParam.m_quarterpel;          /* frame->quarterpel =  pEnc->mbParam.m_quarterpel; */
1853    
1854          /* forward  */          /* forward  */
1855          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
# Line 1790  Line 1858 
1858          start_timer();          start_timer();
1859          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,
1860                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1861                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1862          stop_inter_timer();          stop_inter_timer();
1863    
1864          /* backward */          /* backward */
# Line 1800  Line 1868 
1868          start_timer();          start_timer();
1869          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1870                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1871                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1872          stop_inter_timer();          stop_inter_timer();
1873    
1874          start_timer();          start_timer();
# Line 1837  Line 1905 
1905          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1906                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1907                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1908                          int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0;
1909    
1910                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1911                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1845  Line 1913 
1913                                  continue;                                  continue;
1914                          }                          }
1915    
1916                          if (mb->mode != MODE_DIRECT_NONE_MV) {                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1917                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1918                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1919                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
# Line 1855  Line 1923 
1923                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1924                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
1925    
1926                                  mb->cbp =                                  if (mb->mode != MODE_DIRECT_NONE_MV)
1927                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1928    
1929                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1930                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
# Line 1878  Line 1946 
1946    
1947          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1948    
1949      BitstreamPad(bs);      BitstreamPadAlways(bs);
1950          frame->length = (BitstreamPos(bs) - bits) / 8;          frame->length = (BitstreamPos(bs) - bits) / 8;
1951    
1952  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG

Legend:
Removed from v.1.95.2.5  
changed lines
  Added in v.1.95.2.28

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