[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.3, Mon Mar 10 00:38:49 2003 UTC revision 1.95.2.9, Sun Mar 16 12:04:13 2003 UTC
# 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 */
155        pEnc->num_plugins = create->num_plugins;
156        pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
157        if (pEnc->plugins == NULL)
158            goto xvid_err_memory0;
159    
160        for (n=0; n<pEnc->num_plugins;n++) {
161            xvid_plg_create_t pcreate;
162            xvid_plg_info_t pinfo;
163    
164            memset(&pinfo, 0, sizeof(xvid_plg_info_t));
165            pinfo.version = XVID_VERSION;
166            if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {
167                pEnc->mbParam.plugin_flags |= pinfo.flags;
168            }
169    
170            memset(&pcreate, 0, sizeof(xvid_plg_create_t));
171            pcreate.version = XVID_VERSION;
172            pcreate.width = pEnc->mbParam.width;
173            pcreate.height = pEnc->mbParam.height;
174            pcreate.fincr = pEnc->mbParam.fincr;
175            pcreate.fbase = pEnc->mbParam.fbase;
176            pcreate.param = create->plugins[n].param;
177    
178            pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
179            if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
180                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 */
190        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
191                pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
192                                                pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
193        }
194        /* XXX: error checking */
195    
196          /* bframes */          /* bframes */
197          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
198          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
# Line 183  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 204  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 231  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 246  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 385  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 440  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 479  Line 487 
487    xvid_err_memory1:    xvid_err_memory1:
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);
493        }
494    
495      xvid_err_memory0:
496        for (n=0; n<pEnc->num_plugins;n++) {
497            if (pEnc->plugins[n].func) {
498                pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);
499            }
500        }
501        xvid_free(pEnc->plugins);
502    
503          xvid_free(pEnc);          xvid_free(pEnc);
504    
505          create->handle = NULL;          create->handle = NULL;
# Line 557  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 570  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);
598        }
599    
600        for (i=0; i<pEnc->num_plugins;i++) {
601            if (pEnc->plugins[i].func) {
602                pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0);
603            }
604        }
605        xvid_free(pEnc->plugins);
606    
607          xvid_free(pEnc);          xvid_free(pEnc);
608    
609          return 0;  /* ok */          return 0;  /* ok */
610  }  }
611    
612    
613    /*
614      call the plugins
615      */
616    
617    static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
618                             int opt, int * type, int * quant, xvid_enc_stats_t * stats)
619    {
620        unsigned int i, j;
621        xvid_plg_data_t data;
622    
623        if (pEnc->num_plugins == 0) {
624            for (j=0; j<pEnc->mbParam.mb_height; j++)
625            for (i=0; i<pEnc->mbParam.mb_width; i++) {
626                frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
627            }
628            return;
629        }
630    
631        /* set data struct */
632    
633        memset(&data, 0, sizeof(xvid_plg_data_t));
634        data.version = XVID_VERSION;
635    
636        data.width = pEnc->mbParam.width;
637        data.height = pEnc->mbParam.height;
638        data.mb_width = pEnc->mbParam.mb_width;
639        data.mb_height = pEnc->mbParam.mb_height;
640        data.fincr = frame->fincr;
641        data.fbase = pEnc->mbParam.fbase;
642    
643        data.reference.csp = XVID_CSP_USER;
644        data.reference.plane[0] = pEnc->reference->image.y;
645        data.reference.plane[1] = pEnc->reference->image.u;
646        data.reference.plane[2] = pEnc->reference->image.v;
647        data.reference.stride[0] = pEnc->mbParam.edged_width;
648        data.reference.stride[1] = pEnc->mbParam.edged_width/2;
649        data.reference.stride[2] = pEnc->mbParam.edged_width/2;
650    
651        data.current.csp = XVID_CSP_USER;
652        data.current.plane[0] = frame->image.y;
653        data.current.plane[1] = frame->image.u;
654        data.current.plane[2] = frame->image.v;
655        data.current.stride[0] = pEnc->mbParam.edged_width;
656        data.current.stride[1] = pEnc->mbParam.edged_width/2;
657        data.current.stride[2] = pEnc->mbParam.edged_width/2;
658    
659        data.frame_num = frame->frame_num;
660    
661        if (opt == XVID_PLG_BEFORE) {
662            data.type = XVID_TYPE_AUTO;
663            data.quant = 2;
664    
665            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
666                data.dquant = pEnc->temp_dquants;
667                data.dquant_stride = pEnc->mbParam.mb_width;
668                memset(data.dquant, 0, data.mb_width*data.mb_height);
669            }
670    
671            /* todo: [vol,vop,motion]_flags*/
672    
673        } else { // XVID_PLG_AFTER
674            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
675                data.original.csp = XVID_CSP_USER;
676                data.original.plane[0] = original->y;
677                data.original.plane[1] = original->u;
678                data.original.plane[2] = original->v;
679                data.original.stride[0] = pEnc->mbParam.edged_width;
680                data.original.stride[1] = pEnc->mbParam.edged_width/2;
681                data.original.stride[2] = pEnc->mbParam.edged_width/2;
682            }
683    
684            if ((frame->vol_flags & XVID_EXTRASTATS) ||
685                (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
686    
687                            data.sse_y =
688                                plane_sse( original->y, frame->image.y,
689                                                   pEnc->mbParam.edged_width, pEnc->mbParam.width,
690                                                   pEnc->mbParam.height);
691    
692                data.sse_u =
693                    plane_sse( original->u, frame->image.u,
694                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
695                                                   pEnc->mbParam.height/2);
696    
697                            data.sse_v =
698                    plane_sse( original->v, frame->image.v,
699                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
700                                                   pEnc->mbParam.height/2);
701            }
702    
703            data.type = coding2type(frame->coding_type);
704            data.quant = frame->quant;
705    
706            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
707                data.dquant = pEnc->temp_dquants;
708                data.dquant_stride = pEnc->mbParam.mb_width;
709    
710                for (j=0; j<pEnc->mbParam.mb_height; j++)
711                for (i=0; i<pEnc->mbParam.mb_width; i++) {
712                    data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;
713                }
714            }
715    
716            data.vol_flags = frame->vol_flags;
717            data.vop_flags = frame->vop_flags;
718            data.motion_flags = frame->motion_flags;
719    
720            data.length = frame->length;
721            data.kblks = frame->sStat.kblks;
722            data.mblks = frame->sStat.mblks;
723            data.ublks = frame->sStat.ublks;
724    
725            if (stats) {
726                    stats->type = coding2type(frame->coding_type);
727                    stats->quant = frame->quant;
728                    stats->vol_flags = frame->vol_flags;
729                    stats->vop_flags = frame->vop_flags;
730                    stats->length = frame->length;
731                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
732                    stats->kblks = frame->sStat.kblks;
733                    stats->mblks = frame->sStat.mblks;
734                    stats->ublks = frame->sStat.ublks;
735                stats->sse_y = data.sse_y;
736                stats->sse_u = data.sse_u;
737                stats->sse_v = data.sse_v;
738            }
739        }
740    
741        /* call plugins */
742        for (i=0; i<pEnc->num_plugins;i++) {
743            emms();
744            if (pEnc->plugins[i].func) {
745                if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
746                    continue;
747                }
748            }
749        }
750        emms();
751    
752        /* copy modified values back into frame*/
753        if (opt == XVID_PLG_BEFORE) {
754            *type = data.type;
755            *quant = data.quant;
756    
757            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
758                for (j=0; j<pEnc->mbParam.mb_height; j++)
759                for (i=0; i<pEnc->mbParam.mb_width; i++) {
760                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
761                }
762            }else{
763                for (j=0; j<pEnc->mbParam.mb_height; j++)
764                for (i=0; i<pEnc->mbParam.mb_width; i++) {
765                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
766                }
767            }
768            /* todo: [vol,vop,motion]_flags*/
769        }
770    }
771    
772    
773    
774    
775  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
776  {  {
777        pEnc->current->frame_num = pEnc->m_framenum;
778          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;  
779    
780        pEnc->mbParam.m_stamp += pEnc->current->fincr;
781      pEnc->m_framenum++; /* debug ticker */      pEnc->m_framenum++; /* debug ticker */
782  }  }
783    
784    static __inline void dec_frame_num(Encoder * pEnc)
785    {
786        pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
787        pEnc->m_framenum--; /* debug ticker */
788    }
789    
790    
791    
792  static __inline void  static __inline void
# Line 606  Line 809 
809  }  }
810    
811    
 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 );  
 }  
   
   
812    
813  /*****************************************************************************  /*****************************************************************************
814   * IPB frame encoder entry point   * IPB frame encoder entry point
# Line 641  Line 823 
823   ****************************************************************************/   ****************************************************************************/
824    
825    
 /* XXX: dx50bvop broken! something todo with queue */  
   
826  int  int
827  enc_encode(Encoder * pEnc,  enc_encode(Encoder * pEnc,
828                             xvid_enc_frame_t * xFrame,                             xvid_enc_frame_t * xFrame,
# Line 650  Line 830 
830  {  {
831          xvid_enc_frame_t * frame;          xvid_enc_frame_t * frame;
832          int type;          int type;
         uint16_t x, y;  
833          Bitstream bs;          Bitstream bs;
834    
835          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 713  Line 892 
892    
893          if (pEnc->flush_bframes)          if (pEnc->flush_bframes)
894          {          {
895                  if (pEnc->bframenum_head < pEnc->bframenum_tail)                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
                 {  
896    
897                          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",
898                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
899                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
900    
901                          if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {              if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
902                                  image_copy(&pEnc->sOriginal, &pEnc->bframes[pEnc->bframenum_head]->image,                                  image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
903                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
904                          }                          }
905    
906                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
907                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]);  
   
908                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
909    
   
910                          goto done;                          goto done;
911                  }                  }
912    
                 /* flush complete: reset counters */  
   
                 pEnc->flush_bframes = 0;  
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
   
913                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
914    
915                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
# Line 764  Line 917 
917                     indentical to the future-referece frame.                     indentical to the future-referece frame.
918                  */                  */
919    
920                  if ((pEnc->mbParam.global_flags & XVID_PACKED)) {                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {
921                          int tmp;                          int tmp;
922                          int bits;                          int bits;
923    
# Line 783  Line 936 
936    
937                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
938                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
939                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
   
                         // xFrame->out_flags |= XVID_PACKED_FIXUP?;  
940    
941                /* flush complete: reset counters */
942                    pEnc->flush_bframes = 0;
943                        pEnc->bframenum_head = pEnc->bframenum_tail = 0;
944                          goto done;                          goto done;
945    
946                  }                  }
         }  
947    
948            /* flush complete: reset counters */
949                    pEnc->flush_bframes = 0;
950                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
951            }
952    
953          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954           * dequeue frame from the encoding queue           * dequeue frame from the encoding queue
# Line 801  Line 958 
958          {          {
959                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
960                  {                  {
961                          if (pEnc->bframenum_tail > 0)   /* are bframes */  
962                if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) {
963                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
964                }
965    
966                /* if the very last frame is to be b-vop, we must change it to a p-vop */
967                if (pEnc->bframenum_tail > 0)
968                          {                          {
969    
970                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
971                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
972                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
973    
974                                  /* XXX: convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
975                                  pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
976    
977                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
978                                image_copy(&pEnc->sOriginal, &pEnc->current->image,
979                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
980                    }
981    
982                                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs, 1, 0);
983    
984                                  goto done_flush;                                  goto done_flush;
985                          }                          }
986    
# Line 831  Line 1001 
1001    
1002    
1003          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004           * init pEnc->current field           * init pEnc->current fields
1005           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1006    
1007          emms();         /* float-point region */      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
   
1008      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
1009      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1010          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
# Line 847  Line 1016 
1016                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1017      }      }
1018    
1019      if (xFrame->vop_flags & XVID_EXTRASTATS) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1020                  image_copy(&pEnc->sOriginal, &pEnc->current->image,           * frame type & quant selection
1021                         pEnc->mbParam.edged_width, pEnc->mbParam.height);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
     }  
   
         if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {  
                 int *temp_dquants =     (int *) xvid_malloc(pEnc->mbParam.mb_width *  
                                         pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);  
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                                           pEnc->mbParam.edged_width, 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)];  
1022    
1023                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];      call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);
                         }  
1024    
1025  #undef OFFSET      if (frame->type > 0)
1026                  }                  type = frame->type;
   
                 xvid_free(temp_dquants);  
         }  
1027    
1028          if (frame->quant > 0)          if (frame->quant > 0)
         {  
1029                  pEnc->current->quant = frame->quant;                  pEnc->current->quant = frame->quant;
                 type = frame->type;  
         }  
         else  
         {  
                 pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
                 type = XVID_TYPE_AUTO;  //RateControlGetType(&pEnc->rate_control, ...);  
         }  
   
         emms();         /* END floating-point region */  
1030    
1031        if (type > 0){      /* XVID_TYPE_?VOP */
         if (type > 0)   /* XVID_TYPE_?VOP */  
         {  
1032                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1033          }      } else{             /* XVID_TYPE_AUTO */
1034          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))  
                 {  
1035                          pEnc->iFrameNum = 0;                          pEnc->iFrameNum = 0;
1036                          type = I_VOP;                          type = I_VOP;
1037                  }else{                  }else{
1038                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1039                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1040                                          pEnc->iFrameNum, pEnc->bframenum_tail);                                          pEnc->iFrameNum, pEnc->bframenum_tail);
1041    
1042                if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) {
1043                    type = P_VOP;   /* disable dynamic bframes */
1044                }
1045                    }
1046            }
1047    
1048        /* bframes buffer overflow check */
1049        if (type != I_VOP) {
1050            if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1051                type = P_VOP;
1052            }else{
1053                type = B_VOP;
1054                  }                  }
1055          }          }
1056    
1057          inc_frame_num(pEnc);          inc_frame_num(pEnc);
1058          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1059    
   
1060          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1061                  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,
1062                          "%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);
1063          }          }
1064    
1065          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066           * ivop/pvop/bvop selection           * encode this frame as a b-vop
1067         * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1068           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1069            if (type == B_VOP) {
1070                    if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1071                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1072                    }
1073    
1074          if (type == I_VOP) {                  if (frame->quant < 1) {
1075                            pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1076                                    pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1077    
1078                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  } else {
1079                            pEnc->current->quant = frame->quant;
1080                    }
1081    
1082                    if (pEnc->current->quant < 1)
1083                            pEnc->current->quant = 1;
1084                    else if (pEnc->current->quant > 31)
1085                pEnc->current->quant = 31;
1086    
1087                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i",
1088                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1089                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1090    
1091                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  /* store frame into bframe buffer & swap ref back to current */
1092                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1093                    SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1094    
1095                    pEnc->bframenum_tail++;
1096    
1097                    goto repeat;
1098        }
1099    
1100        /* for unpacked bframes, output the stats for the last encoded frame */
1101        if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1102        {
1103            if (pEnc->current->stamp > 0) {
1104                call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1105            }
1106                    else
1107                            stats->type = XVID_TYPE_NOTHING;
1108                  }                  }
1109    
1110                  // when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1111                  if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {           * closed-gop
1112         * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1113         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1114    
1115        if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1116    
1117                          // place this frame back on the encoding-queue (head)                          // place this frame back on the encoding-queue (head)
1118                          // we will deal with it next time                          // we will deal with it next time
1119            dec_frame_num(pEnc);
1120            pEnc->iFrameNum--;
1121    
1122                          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);
1123            pEnc->queue_size++;
1124                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1125    
1126                          // grab the last frame from the bframe-queue                          // grab the last frame from the bframe-queue
# Line 951  Line 1132 
1132                                  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");
1133                          }                          }
1134    
1135                          /* XXX: convert B-VOP to P-VOP */                  /* convert B-VOP quant to P-VOP */
1136                          pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
1137            type = P_VOP;
1138        }
1139    
                         FrameCodeP(pEnc, &bs, 1, 0);  
1140    
1141                  } else {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1142             * encode this frame as an i-vop
1143         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1144    
1145            if (type == I_VOP) {
1146    
1147                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1148                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1149                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1150    
1151                    if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1152                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1153                    }
1154    
1155    
1156                          /* ---- update vol flags at IVOP ----------- */                          /* ---- update vol flags at IVOP ----------- */
1157                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
# Line 978  Line 1173 
1173    
1174                          /* ^^^------------------------ */                          /* ^^^------------------------ */
1175    
1176            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1177                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1178                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1179            }
1180    
1181                          FrameCodeI(pEnc, &bs);                          FrameCodeI(pEnc, &bs);
1182                          xFrame->out_flags |= XVID_KEYFRAME;                          xFrame->out_flags |= XVID_KEYFRAME;
                 }  
1183    
1184          } else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) ||          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185                                  pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {           * encode this frame as an p-vop
1186                  /*       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1187                   * This will be coded as a Predicted Frame  
1188                   */      } else { // (type == P_VOP || type == S_VOP)
1189    
1190                  DPRINTF(DPRINTF_DEBUG,"*** xFrame bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1191                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1192                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1193    
# Line 996  Line 1195 
1195                          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");
1196                  }                  }
1197    
1198                  FrameCodeP(pEnc, &bs, 1, 0);          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1199                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1200          } 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;  
1201                  }                  }
1202    
1203                  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;  
   
1204      }      }
1205    
         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);  
         }  
1206    
1207          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208           * okay, on next enc_encode call we must flush bframes           * on next enc_encode call we must flush bframes
1209           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1210    
1211  done_flush:  done_flush:
1212    
 #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 */  
1213          pEnc->flush_bframes = 1;          pEnc->flush_bframes = 1;
1214    
1215            /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */
1216          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {
1217                  goto repeat;                  goto repeat;
1218          }          }
1219    
1220          if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)      /* packed or no-bframes: output stats */
1221          {      if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) {
1222                  /* 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);  
         }  
         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);  
                 else  
                         stats->type = XVID_TYPE_NOTHING;  
1223          }          }
1224    
1225          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1098  Line 1236 
1236  }  }
1237    
1238    
1239    static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1240    {
1241        unsigned int i,j;
1242        int quant = frame->quant;
1243    
1244        for (j=0; j<pParam->mb_height; j++)
1245        for (i=0; i<pParam->mb_width; i++) {
1246            MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];
1247            quant += pMB->dquant;
1248            if (quant > 31)
1249                            quant = 31;
1250                    if (quant < 1)
1251                            quant = 1;
1252            pMB->quant = quant;
1253        }
1254    }
1255    
1256    
1257  static __inline void  static __inline void
1258  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(Encoder * pEnc,
1259                          MACROBLOCK * pMB)                          MACROBLOCK * pMB)
# Line 1111  Line 1267 
1267          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;
1268          pMB->sad16 = 0;          pMB->sad16 = 0;
1269    
1270          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if (pMB->dquant != 0) {
                 if (pMB->dquant != NO_CHANGE) {  
1271                          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;  
                 }  
1272          }          }
   
         pMB->quant = pEnc->current->quant;  
1273  }  }
1274    
1275    
# Line 1155  Line 1301 
1301                  stop_edges_timer();                  stop_edges_timer();
1302          }          }
1303    
         //pEnc->iFrameNum = 0;  
1304          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1305          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;  
1306          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1307    
1308        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1309    
1310          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1311    
1312          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
# Line 1285  Line 1431 
1431    
1432          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1433    
1434    
1435        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1436    
1437          start_timer();          start_timer();
1438          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1439                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
# Line 1398  Line 1547 
1547    
1548                          stop_comp_timer();                          stop_comp_timer();
1549    
1550                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 if (pMB->dquant != NO_CHANGE) {  
1551                                          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;  
                                 }  
1552                          }                          }
                         pMB->quant = pEnc->current->quant;  
1553    
1554                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1555    
# Line 1431  Line 1572 
1572                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1573    
1574                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1575                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == 0);
1576    
1577                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1578                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);

Legend:
Removed from v.1.95.2.3  
changed lines
  Added in v.1.95.2.9

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