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

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

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

revision 919, Thu Mar 13 11:07:20 2003 UTC revision 936, Sat Mar 22 13:49:49 2003 UTC
# Line 26  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
29   *  $Id: encoder.c,v 1.95.2.4 2003-03-13 11:07:20 suxen_drol Exp $   *  $Id: encoder.c,v 1.95.2.11 2003-03-22 13:49:49 syskin Exp $
30   *   *
31   ****************************************************************************/   ****************************************************************************/
32    
# Line 47  Line 47 
47  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
48  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
49  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "utils/ratecontrol.h"  
50  #include "utils/emms.h"  #include "utils/emms.h"
51  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
52  #include "quant/adapt_quant.h"  #include "quant/adapt_quant.h"
# Line 55  Line 54 
54  #include "utils/mem_align.h"  #include "utils/mem_align.h"
55    
56  /*****************************************************************************  /*****************************************************************************
  * Local macros  
  ****************************************************************************/  
   
 #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }  
   
 /*****************************************************************************  
57   * Local function prototypes   * Local function prototypes
58   ****************************************************************************/   ****************************************************************************/
59    
# Line 76  Line 69 
69                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
70                                             Bitstream * bs);                                             Bitstream * bs);
71    
 /*****************************************************************************  
  * Local data  
  ****************************************************************************/  
   
 static int DQtab[4] = {  
         -1, -2, 1, 2  
 };  
   
 static int iDQtab[5] = {  
         1, 0, NO_CHANGE, 2, 3  
 };  
   
72    
73  /*****************************************************************************  /*****************************************************************************
74   * Encoder creation   * Encoder creation
# Line 167  Line 148 
148      /* framerate */      /* framerate */
149      pEnc->mbParam.fincr = MAX(create->fincr, 0);      pEnc->mbParam.fincr = MAX(create->fincr, 0);
150          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
151        if (pEnc->mbParam.fincr>0)
152          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
153    
154      /* plugin */      /* plugin */
# Line 182  Line 164 
164          memset(&pinfo, 0, sizeof(xvid_plg_info_t));          memset(&pinfo, 0, sizeof(xvid_plg_info_t));
165          pinfo.version = XVID_VERSION;          pinfo.version = XVID_VERSION;
166          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {
167              pEnc->plugin_flags |= pinfo.flags;              pEnc->mbParam.plugin_flags |= pinfo.flags;
168          }          }
169    
170          memset(&pcreate, 0, sizeof(xvid_plg_create_t));          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
# Line 197  Line 179 
179          if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {          if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
180              pEnc->plugins[n].func = create->plugins[n].func;              pEnc->plugins[n].func = create->plugins[n].func;
181          }          }
182        }
183    
184        if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) ||
185            (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
186            pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
187      }      }
188    
189      /* temp dquants */      /* temp dquants */
190        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
191          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
192                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
193        }
194      /* XXX: error checking */      /* XXX: error checking */
195    
196          /* bframes */          /* bframes */
# Line 219  Line 207 
207    
208          /* Bitrate allocator defaults          /* Bitrate allocator defaults
209    
         if (create->rc_bitrate <= 0)  
                 create->rc_bitrate = 900000;  
   
         if (create->rc_reaction_delay_factor <= 0)  
                 create->rc_reaction_delay_factor = 16;  
   
         if (create->rc_averaging_period <= 0)  
                 create->rc_averaging_period = 100;  
   
         if (create->rc_buffer <= 0)  
                 create->rc_buffer = 100;  
   
   
   
210          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))
211                  create->min_quantizer = 1;                  create->min_quantizer = 1;
212    
# Line 240  Line 214 
214                  create->max_quantizer = 31;                  create->max_quantizer = 31;
215    
216          if (create->max_quantizer < create->min_quantizer)          if (create->max_quantizer < create->min_quantizer)
217                  create->max_quantizer = create->min_quantizer;                  create->max_quantizer = create->min_quantizer; */
   
         pEnc->bitrate = create->rc_bitrate; */  
218    
219    
220      /* allocate working frame-image memory */      /* allocate working frame-image memory */
# Line 267  Line 239 
239    
240          /* allocate interpolation image memory */          /* allocate interpolation image memory */
241    
242          if (create->global & XVID_EXTRASTATS_ENABLE)      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
243                  image_null(&pEnc->sOriginal);                  image_null(&pEnc->sOriginal);
244            image_null(&pEnc->sOriginal2);
245        }
246    
247          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
248          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 282  Line 256 
256          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
257          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
258    
259          if (create->global & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
260          {       if (image_create          if (image_create
261                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
262                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
263                          goto xvid_err_memory3;                          goto xvid_err_memory3;
264    
265            if (image_create
266                            (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
267                             pEnc->mbParam.edged_height) < 0)
268                            goto xvid_err_memory3;
269          }          }
270    
271          if (image_create          if (image_create
# Line 421  Line 400 
400          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
401          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
402    
     /* XXX: cleanup ratecontol when new code is ready */  
 /*      if (create->rc_bitrate) {  
                 RateControlInit(&pEnc->rate_control, create->rc_bitrate,  
                                                 create->rc_reaction_delay_factor,  
                                                 create->rc_averaging_period, create->rc_buffer,  
                                                 create->fbase * 1000 / create->fincr,  
                                                 create->max_quantizer, create->min_quantizer);  
         } */  
   
403      create->handle = (void *) pEnc;      create->handle = (void *) pEnc;
404    
405          init_timer();          init_timer();
# Line 476  Line 446 
446    
447    xvid_err_memory3:    xvid_err_memory3:
448    
449          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
450          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
451                                              pEnc->mbParam.edged_height);
452                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
453                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
454          }          }
455    
# Line 516  Line 488 
488          xvid_free(pEnc->current);          xvid_free(pEnc->current);
489          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
490    
491        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
492          xvid_free(pEnc->temp_dquants);          xvid_free(pEnc->temp_dquants);
493        }
494    
495    xvid_err_memory0:    xvid_err_memory0:
496      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
# Line 604  Line 578 
578          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
579                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
580    
581          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
582          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
583                                              pEnc->mbParam.edged_height);
584                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
585                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
586          }          }
587    
# Line 617  Line 593 
593          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
594          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
595    
596        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
597      xvid_free(pEnc->temp_dquants);      xvid_free(pEnc->temp_dquants);
598        }
599    
600      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
601          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
# Line 636  Line 614 
614    call the plugins    call the plugins
615    */    */
616    
617  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, int opt, int * type, int * quant)  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
618                             int opt, int * type, int * quant, xvid_enc_stats_t * stats)
619  {  {
620      int i;      unsigned int i, j;
621      xvid_plg_data_t data;      xvid_plg_data_t data;
622    
623        /* set data struct */
624    
625      memset(&data, 0, sizeof(xvid_plg_data_t));      memset(&data, 0, sizeof(xvid_plg_data_t));
626      data.version = XVID_VERSION;      data.version = XVID_VERSION;
627    
628        data.width = pEnc->mbParam.width;
629        data.height = pEnc->mbParam.height;
630        data.mb_width = pEnc->mbParam.mb_width;
631        data.mb_height = pEnc->mbParam.mb_height;
632        data.fincr = frame->fincr;
633        data.fbase = pEnc->mbParam.fbase;
634    
635      data.reference.csp = XVID_CSP_USER;      data.reference.csp = XVID_CSP_USER;
636      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
637      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
# Line 660  Line 648 
648      data.current.stride[1] = pEnc->mbParam.edged_width/2;      data.current.stride[1] = pEnc->mbParam.edged_width/2;
649      data.current.stride[2] = pEnc->mbParam.edged_width/2;      data.current.stride[2] = pEnc->mbParam.edged_width/2;
650    
651      data.original.csp = XVID_CSP_NULL;      data.frame_num = frame->frame_num;
     /* todo: data.original */  
652    
653      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
654          data.type = XVID_TYPE_AUTO;          data.type = XVID_TYPE_AUTO;
655          data.quant = 2;          data.quant = 2;
656          //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);  
657          //data.qscale_stride = pEnc->mbParam.width;          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
658          //data.qscale_table = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
659          /* todo: vol,vop,motion flags */              data.dquant_stride = pEnc->mbParam.mb_width;
660                memset(data.dquant, 0, data.mb_width*data.mb_height);
661            }
662    
663            /* todo: [vol,vop,motion]_flags*/
664    
665      } else { // XVID_PLG_AFTER      } else { // XVID_PLG_AFTER
666            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
667                data.original.csp = XVID_CSP_USER;
668                data.original.plane[0] = original->y;
669                data.original.plane[1] = original->u;
670                data.original.plane[2] = original->v;
671                data.original.stride[0] = pEnc->mbParam.edged_width;
672                data.original.stride[1] = pEnc->mbParam.edged_width/2;
673                data.original.stride[2] = pEnc->mbParam.edged_width/2;
674            }
675    
676            if ((frame->vol_flags & XVID_EXTRASTATS) ||
677                (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
678    
679                            data.sse_y =
680                                plane_sse( original->y, frame->image.y,
681                                                   pEnc->mbParam.edged_width, pEnc->mbParam.width,
682                                                   pEnc->mbParam.height);
683    
684                data.sse_u =
685                    plane_sse( original->u, frame->image.u,
686                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
687                                                   pEnc->mbParam.height/2);
688    
689                            data.sse_v =
690                    plane_sse( original->v, frame->image.v,
691                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
692                                                   pEnc->mbParam.height/2);
693            }
694    
695          data.type = coding2type(frame->coding_type);          data.type = coding2type(frame->coding_type);
696          data.quant = frame->quant;          data.quant = frame->quant;
697          /* todo: data.qscale */  
698            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
699                data.dquant = pEnc->temp_dquants;
700                data.dquant_stride = pEnc->mbParam.mb_width;
701    
702                for (j=0; j<pEnc->mbParam.mb_height; j++)
703                for (i=0; i<pEnc->mbParam.mb_width; i++) {
704                    data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;
705                }
706            }
707    
708          data.vol_flags = frame->vol_flags;          data.vol_flags = frame->vol_flags;
709          data.vop_flags = frame->vop_flags;          data.vop_flags = frame->vop_flags;
710          data.motion_flags = frame->motion_flags;          data.motion_flags = frame->motion_flags;
# Line 684  Line 713 
713          data.kblks = frame->sStat.kblks;          data.kblks = frame->sStat.kblks;
714          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
715          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
716    
717            if (stats) {
718                    stats->type = coding2type(frame->coding_type);
719                    stats->quant = frame->quant;
720                    stats->vol_flags = frame->vol_flags;
721                    stats->vop_flags = frame->vop_flags;
722                    stats->length = frame->length;
723                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
724                    stats->kblks = frame->sStat.kblks;
725                    stats->mblks = frame->sStat.mblks;
726                    stats->ublks = frame->sStat.ublks;
727                stats->sse_y = data.sse_y;
728                stats->sse_u = data.sse_u;
729                stats->sse_v = data.sse_v;
730            }
731      }      }
732    
733        /* call plugins */
734      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
735            emms();
736          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
737              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) {
738                  continue;                  continue;
739              }              }
740          }          }
741      }      }
742        emms();
743    
744        /* copy modified values back into frame*/
745      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
746          *type = data.type;          *type = data.type;
747          *quant = data.quant;          *quant = data.quant;
         /* todo: copy modified qscale,vol,vop,motion flags into the frame*/  
     }  
748    
749            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
750                for (j=0; j<pEnc->mbParam.mb_height; j++)
751                for (i=0; i<pEnc->mbParam.mb_width; i++) {
752                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
753                }
754            }else{
755                for (j=0; j<pEnc->mbParam.mb_height; j++)
756                for (i=0; i<pEnc->mbParam.mb_width; i++) {
757                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
758                }
759            }
760            /* todo: [vol,vop,motion]_flags*/
761        }
762  }  }
763    
764    
# Line 707  Line 766 
766    
767  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
768  {  {
769        pEnc->current->frame_num = pEnc->m_framenum;
770          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
         pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;  
771    
772        pEnc->mbParam.m_stamp += pEnc->current->fincr;
773      pEnc->m_framenum++; /* debug ticker */      pEnc->m_framenum++; /* debug ticker */
774  }  }
775    
776    static __inline void dec_frame_num(Encoder * pEnc)
777    {
778        pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
779        pEnc->m_framenum--; /* debug ticker */
780    }
781    
782    
783    
784  static __inline void  static __inline void
# Line 735  Line 801 
801  }  }
802    
803    
 static void  
 set_stats(xvid_enc_stats_t * stats, RateControl * rc,  
                   const MBParam * pParam, const FRAMEINFO * frame)  
 {  
         if (stats)  
         {  
                 stats->type = coding2type(frame->coding_type);  
                 stats->quant = frame->quant;  
                 stats->vol_flags = frame->vol_flags;  
                 stats->vop_flags = frame->vop_flags;  
                 stats->length = frame->length;  
                 stats->hlength = frame->length - (frame->sStat.iTextBits / 8);  
                 stats->kblks = frame->sStat.kblks;  
                 stats->mblks = frame->sStat.mblks;  
                 stats->ublks = frame->sStat.ublks;  
         }  
   
         RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP );  
 }  
   
   
804    
805  /*****************************************************************************  /*****************************************************************************
806   * IPB frame encoder entry point   * IPB frame encoder entry point
# Line 770  Line 815 
815   ****************************************************************************/   ****************************************************************************/
816    
817    
 /* XXX: dx50bvop broken! something todo with queue */  
   
818  int  int
819  enc_encode(Encoder * pEnc,  enc_encode(Encoder * pEnc,
820                             xvid_enc_frame_t * xFrame,                             xvid_enc_frame_t * xFrame,
# Line 779  Line 822 
822  {  {
823          xvid_enc_frame_t * frame;          xvid_enc_frame_t * frame;
824          int type;          int type;
         uint16_t x, y;  
825          Bitstream bs;          Bitstream bs;
826    
827          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */
# Line 842  Line 884 
884    
885          if (pEnc->flush_bframes)          if (pEnc->flush_bframes)
886          {          {
887                  if (pEnc->bframenum_head < pEnc->bframenum_tail)                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
                 {  
888    
889                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
890                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
891                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
892    
893                          if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {              if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
894                                  image_copy(&pEnc->sOriginal, &pEnc->bframes[pEnc->bframenum_head]->image,                                  image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
895                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
896                          }                          }
897    
898                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
899                call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);
                         if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {  
                                 stats->sse_y =  
                                         plane_sse( pEnc->sOriginal.y, pEnc->bframes[pEnc->bframenum_head]->image.y,  
                                                            pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                                            pEnc->mbParam.height);  
   
                                 stats->sse_u =  
                                         plane_sse( pEnc->sOriginal.u, pEnc->bframes[pEnc->bframenum_head]->image.u,  
                                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                                            pEnc->mbParam.height/2);  
   
                                 stats->sse_v =  
                                         plane_sse( pEnc->sOriginal.v, pEnc->bframes[pEnc->bframenum_head]->image.v,  
                                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                                            pEnc->mbParam.height/2);  
                         }  
   
                         set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]);  
             emms();  
             call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
             emms();  
   
900                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
901    
   
902                          goto done;                          goto done;
903                  }                  }
904    
                 /* flush complete: reset counters */  
   
                 pEnc->flush_bframes = 0;  
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
   
905                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
906    
907                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
# Line 896  Line 909 
909                     indentical to the future-referece frame.                     indentical to the future-referece frame.
910                  */                  */
911    
912                  if ((pEnc->mbParam.global_flags & XVID_PACKED)) {                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {
913                          int tmp;                          int tmp;
914                          int bits;                          int bits;
915    
# Line 915  Line 928 
928    
929                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
930                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
931                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
             emms();  
             call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
             emms();  
   
                         // xFrame->out_flags |= XVID_PACKED_FIXUP?;  
932    
933                /* flush complete: reset counters */
934                    pEnc->flush_bframes = 0;
935                        pEnc->bframenum_head = pEnc->bframenum_tail = 0;
936                          goto done;                          goto done;
937    
938                  }                  }
         }  
939    
940            /* flush complete: reset counters */
941                    pEnc->flush_bframes = 0;
942                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
943            }
944    
945          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946           * dequeue frame from the encoding queue           * dequeue frame from the encoding queue
# Line 936  Line 950 
950          {          {
951                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
952                  {                  {
953                          if (pEnc->bframenum_tail > 0)   /* are bframes */  
954                if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) {
955                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
956                }
957    
958                /* if the very last frame is to be b-vop, we must change it to a p-vop */
959                if (pEnc->bframenum_tail > 0)
960                          {                          {
961    
962                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
963                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
964                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
965    
966                                  /* XXX: convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
967                                  pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
968    
969                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
970                                image_copy(&pEnc->sOriginal, &pEnc->current->image,
971                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
972                    }
973    
974                                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs, 1, 0);
975    
976                                  goto done_flush;                                  goto done_flush;
977                          }                          }
978    
# Line 966  Line 993 
993    
994    
995          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
996           * init pEnc->current field           * init pEnc->current fields
997           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
998    
999          emms();         /* float-point region */      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
   
1000      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
1001      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1002          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
# Line 982  Line 1008 
1008                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1009      }      }
1010    
1011      if (xFrame->vop_flags & XVID_EXTRASTATS) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012                  image_copy(&pEnc->sOriginal, &pEnc->current->image,           * frame type & quant selection
1013                         pEnc->mbParam.edged_width, pEnc->mbParam.height);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
     }  
   
         if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {  
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                                           pEnc->mbParam.edged_width, pEnc->temp_dquants,  
                                                           pEnc->current->quant, pEnc->current->quant,  
                                                           2 * pEnc->current->quant,  
                                                           pEnc->mbParam.mb_width,  
                                                           pEnc->mbParam.mb_height);  
   
                 for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
   
 #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)  
   
                         for (x = 0; x < pEnc->mbParam.mb_width; x++) {  
                                 MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];  
   
                                 pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];  
                         }  
   
 #undef OFFSET  
                 }  
   
         }  
1014    
1015          emms();         /* END floating-point region */      call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);
     call_plugins(pEnc, pEnc->current, XVID_PLG_BEFORE, &type, &pEnc->current->quant);  
     emms();  
1016    
1017      if (frame->type > 0)      if (frame->type > 0)
1018                  type = frame->type;                  type = frame->type;
# Line 1022  Line 1020 
1020      if (frame->quant > 0)      if (frame->quant > 0)
1021                  pEnc->current->quant = frame->quant;                  pEnc->current->quant = frame->quant;
1022    
1023      if (type > 0)       /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
         {  
1024                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1025          }      } else{             /* XVID_TYPE_AUTO */
1026          else                    /* XVID_TYPE_AUTO */                  if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){
         {  
                 if (pEnc->iFrameNum == 0 ||  
                         (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))  
                 {  
1027                          pEnc->iFrameNum = 0;                          pEnc->iFrameNum = 0;
1028                          type = I_VOP;                          type = I_VOP;
1029                  }else{                  }else{
1030                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1031                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1032                                          pEnc->iFrameNum, pEnc->bframenum_tail);                                          pEnc->iFrameNum, pEnc->bframenum_tail);
1033    
1034                if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) {
1035                    type = P_VOP;   /* disable dynamic bframes */
1036                }
1037                    }
1038            }
1039    
1040        /* bframes buffer overflow check */
1041        if (type != I_VOP) {
1042            if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1043                type = P_VOP;
1044            }else{
1045                type = B_VOP;
1046                  }                  }
1047          }          }
1048    
1049          inc_frame_num(pEnc);          inc_frame_num(pEnc);
1050          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1051    
   
1052          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1053                  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,
1054                          "%i  st:%i  if:%i", pEnc->m_framenum, pEnc->current->stamp, pEnc->iFrameNum);                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1055          }          }
1056    
1057          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1058           * ivop/pvop/bvop selection           * encode this frame as a b-vop
1059         * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1060           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1061            if (type == B_VOP) {
1062                    if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1063                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1064                    }
1065    
1066          if (type == I_VOP) {                  if (frame->quant < 1) {
1067                            pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1068                                    pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1069    
1070                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  } else {
1071                            pEnc->current->quant = frame->quant;
1072                    }
1073    
1074                    if (pEnc->current->quant < 1)
1075                            pEnc->current->quant = 1;
1076                    else if (pEnc->current->quant > 31)
1077                pEnc->current->quant = 31;
1078    
1079                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i",
1080                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1081                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1082    
1083                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  /* store frame into bframe buffer & swap ref back to current */
1084                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1085                    SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1086    
1087                    pEnc->bframenum_tail++;
1088    
1089                    goto repeat;
1090        }
1091    
1092        /* for unpacked bframes, output the stats for the last encoded frame */
1093        if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1094        {
1095            if (pEnc->current->stamp > 0) {
1096                call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1097            }
1098                    else
1099                            stats->type = XVID_TYPE_NOTHING;
1100                  }                  }
1101    
1102                  // when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103                  if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {           * closed-gop
1104         * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1105         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1106    
1107        if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1108    
1109                          // place this frame back on the encoding-queue (head)                          // place this frame back on the encoding-queue (head)
1110                          // we will deal with it next time                          // we will deal with it next time
1111            dec_frame_num(pEnc);
1112            pEnc->iFrameNum--;
1113    
1114                          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);
1115            pEnc->queue_size++;
1116                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1117    
1118                          // grab the last frame from the bframe-queue                          // grab the last frame from the bframe-queue
# Line 1079  Line 1124 
1124                                  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");
1125                          }                          }
1126    
1127                          /* XXX: convert B-VOP to P-VOP */                  /* convert B-VOP quant to P-VOP */
1128                          pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
1129            type = P_VOP;
1130        }
1131    
                         FrameCodeP(pEnc, &bs, 1, 0);  
1132    
1133                  } else {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1134             * encode this frame as an i-vop
1135         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1136    
1137            if (type == I_VOP) {
1138    
1139                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1140                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1141                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1142    
1143                    if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1144                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1145                    }
1146    
1147    
1148                          /* ---- update vol flags at IVOP ----------- */                          /* ---- update vol flags at IVOP ----------- */
1149                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
# Line 1106  Line 1165 
1165    
1166                          /* ^^^------------------------ */                          /* ^^^------------------------ */
1167    
1168            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1169                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1170                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1171            }
1172    
1173                          FrameCodeI(pEnc, &bs);                          FrameCodeI(pEnc, &bs);
1174                          xFrame->out_flags |= XVID_KEYFRAME;                          xFrame->out_flags |= XVID_KEYFRAME;
                 }  
1175    
1176          } else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) ||          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1177                                  pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {           * encode this frame as an p-vop
1178                  /*       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                  * This will be coded as a Predicted Frame  
                  */  
1179    
1180                  DPRINTF(DPRINTF_DEBUG,"*** xFrame bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",      } else { // (type == P_VOP || type == S_VOP)
1181    
1182                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1183                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1184                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1185    
# Line 1124  Line 1187 
1187                          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");
1188                  }                  }
1189    
1190                  FrameCodeP(pEnc, &bs, 1, 0);          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1191                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1192          } else {        /* type == B_VOP */                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
   
                 if ((pEnc->current->vop_flags & XVID_DEBUG)) {  
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");  
                 }  
   
                 if (frame->bquant < 1) {  
                         pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *  
                                 pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;  
   
                 } else {  
                         pEnc->current->quant = frame->bquant;  
1193                  }                  }
1194    
1195                  if (pEnc->current->quant < 1)                  FrameCodeP(pEnc, &bs, 1, 0);
                         pEnc->current->quant = 1;  
                 else if (pEnc->current->quant > 31)  
             pEnc->current->quant = 31;  
   
                 DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);  
   
                 /* store frame into bframe buffer & swap ref back to current */  
                 SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);  
                 SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);  
   
                 pEnc->bframenum_tail++;  
   
                 goto repeat;  
   
1196      }      }
1197    
         if (xFrame->vop_flags & XVID_EXTRASTATS) {  
                 stats->sse_y =  
                         plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,  
                                            pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                            pEnc->mbParam.height);  
   
                 stats->sse_u =  
                         plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,  
                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                            pEnc->mbParam.height/2);  
   
                 stats->sse_v =  
                         plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,  
                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                            pEnc->mbParam.height/2);  
         }  
1198    
1199          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200           * okay, on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1201           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1202    
1203  done_flush:  done_flush:
1204    
 #if 0  
         {  
                 char tmp[100];  
                 wsprintf(tmp,"\\frame%03i.pgm", pEnc->m_framenum);  
                 image_dump_yuvpgm(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->mbParam.width, pEnc->mbParam.height, tmp);  
         }  
 #endif  
   
         /* packed_mode: repeat */  
1205          pEnc->flush_bframes = 1;          pEnc->flush_bframes = 1;
1206    
1207            /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */
1208          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {
1209                  goto repeat;                  goto repeat;
1210          }          }
1211    
1212          if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)      /* packed or no-bframes: output stats */
1213          {      if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) {
1214                  /* packed(and low_delay) encoding gives us graceful reorded-stats output */          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
                 set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);  
         emms();  
         call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
         emms();  
         }  
         else  
         {  
                 /* outherwise, output the previous frame */  
                 /* XXX: for this to work, refernce must be valid for IVOPs too */  
         if (pEnc->current->stamp > 0) {  
                         set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference);  
             emms();  
             call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);  
             emms();  
         }  
                 else  
                         stats->type = XVID_TYPE_NOTHING;  
1215          }          }
1216    
1217          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1233  Line 1228 
1228  }  }
1229    
1230    
1231    static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1232    {
1233        unsigned int i,j;
1234        int quant = frame->quant;
1235    
1236        for (j=0; j<pParam->mb_height; j++)
1237        for (i=0; i<pParam->mb_width; i++) {
1238            MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];
1239            quant += pMB->dquant;
1240            if (quant > 31)
1241                            quant = 31;
1242                    if (quant < 1)
1243                            quant = 1;
1244            pMB->quant = quant;
1245        }
1246    }
1247    
1248    
1249  static __inline void  static __inline void
1250  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(Encoder * pEnc,
1251                          MACROBLOCK * pMB)                          MACROBLOCK * pMB)
# Line 1246  Line 1259 
1259          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;
1260          pMB->sad16 = 0;          pMB->sad16 = 0;
1261    
1262          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if (pMB->dquant != 0) {
                 if (pMB->dquant != NO_CHANGE) {  
1263                          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;  
1264                  }                  }
1265          }          }
1266    
         pMB->quant = pEnc->current->quant;  
 }  
   
1267    
1268    
1269  static int  static int
# Line 1290  Line 1293 
1293                  stop_edges_timer();                  stop_edges_timer();
1294          }          }
1295    
         //pEnc->iFrameNum = 0;  
1296          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1297          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
         //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;  
1298          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1299    
1300        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1301    
1302          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1303    
1304          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
# Line 1420  Line 1423 
1423    
1424          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1425    
1426    
1427        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1428    
1429          start_timer();          start_timer();
1430          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1431                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
# Line 1533  Line 1539 
1539    
1540                          stop_comp_timer();                          stop_comp_timer();
1541    
1542                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 if (pMB->dquant != NO_CHANGE) {  
1543                                          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;  
                                 }  
1544                          }                          }
                         pMB->quant = pEnc->current->quant;  
1545    
1546                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1547    
# Line 1566  Line 1564 
1564                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1565    
1566                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1567                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == 0);
1568    
1569                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1570                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
# Line 1848  Line 1846 
1846                                  continue;                                  continue;
1847                          }                          }
1848    
1849                          if (mb->mode != MODE_DIRECT_NONE_MV) {                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1850                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1851                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1852                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
# Line 1858  Line 1856 
1856                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1857                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
1858    
1859                                  mb->cbp =                                  if (mb->mode != MODE_DIRECT_NONE_MV)
1860                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1861    
1862                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1863                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {

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

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