[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.5, Sat Mar 15 14:32:56 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 169  Line 168 
168          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
169          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
170    
171        /* plugin */
172        pEnc->num_plugins = create->num_plugins;
173        pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
174        if (pEnc->plugins == NULL)
175            goto xvid_err_memory0;
176    
177        for (n=0; n<pEnc->num_plugins;n++) {
178            xvid_plg_create_t pcreate;
179            xvid_plg_info_t pinfo;
180    
181            memset(&pinfo, 0, sizeof(xvid_plg_info_t));
182            pinfo.version = XVID_VERSION;
183            if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {
184                pEnc->mbParam.plugin_flags |= pinfo.flags;
185            }
186    
187            memset(&pcreate, 0, sizeof(xvid_plg_create_t));
188            pcreate.version = XVID_VERSION;
189            pcreate.width = pEnc->mbParam.width;
190            pcreate.height = pEnc->mbParam.height;
191            pcreate.fincr = pEnc->mbParam.fincr;
192            pcreate.fbase = pEnc->mbParam.fbase;
193            pcreate.param = create->plugins[n].param;
194    
195            pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
196            if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
197                pEnc->plugins[n].func = create->plugins[n].func;
198            }
199        }
200    
201        /* temp dquants */
202            pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
203                                            pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
204        /* XXX: error checking */
205    
206          /* bframes */          /* bframes */
207          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
208          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
# Line 183  Line 217 
217    
218          /* Bitrate allocator defaults          /* Bitrate allocator defaults
219    
         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;  
   
   
   
220          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))
221                  create->min_quantizer = 1;                  create->min_quantizer = 1;
222    
# Line 204  Line 224 
224                  create->max_quantizer = 31;                  create->max_quantizer = 31;
225    
226          if (create->max_quantizer < create->min_quantizer)          if (create->max_quantizer < create->min_quantizer)
227                  create->max_quantizer = create->min_quantizer;                  create->max_quantizer = create->min_quantizer; */
   
         pEnc->bitrate = create->rc_bitrate; */  
228    
229    
230      /* allocate working frame-image memory */      /* allocate working frame-image memory */
# Line 231  Line 249 
249    
250          /* allocate interpolation image memory */          /* allocate interpolation image memory */
251    
252          if (create->global & XVID_EXTRASTATS_ENABLE)      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
253                  image_null(&pEnc->sOriginal);                  image_null(&pEnc->sOriginal);
254            image_null(&pEnc->sOriginal2);
255        }
256    
257          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
258          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 246  Line 266 
266          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
267          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
268    
269          if (create->global & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
270          {       if (image_create          if (image_create
271                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
272                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
273                          goto xvid_err_memory3;                          goto xvid_err_memory3;
274    
275            if (image_create
276                            (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
277                             pEnc->mbParam.edged_height) < 0)
278                            goto xvid_err_memory3;
279          }          }
280    
281          if (image_create          if (image_create
# Line 385  Line 410 
410          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
411          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
412    
     /* 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);  
         } */  
   
413      create->handle = (void *) pEnc;      create->handle = (void *) pEnc;
414    
415          init_timer();          init_timer();
# Line 440  Line 456 
456    
457    xvid_err_memory3:    xvid_err_memory3:
458    
459          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
460          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
461                                              pEnc->mbParam.edged_height);
462                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
463                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
464          }          }
465    
# Line 479  Line 497 
497    xvid_err_memory1:    xvid_err_memory1:
498          xvid_free(pEnc->current);          xvid_free(pEnc->current);
499          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
500    
501            xvid_free(pEnc->temp_dquants);
502    
503      xvid_err_memory0:
504        for (n=0; n<pEnc->num_plugins;n++) {
505            if (pEnc->plugins[n].func) {
506                pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);
507            }
508        }
509        xvid_free(pEnc->plugins);
510    
511          xvid_free(pEnc);          xvid_free(pEnc);
512    
513          create->handle = NULL;          create->handle = NULL;
# Line 557  Line 586 
586          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
587                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
588    
589          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
590          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
591                                              pEnc->mbParam.edged_height);
592                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
593                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
594          }          }
595    
# Line 570  Line 601 
601          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
602          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
603    
604        xvid_free(pEnc->temp_dquants);
605    
606        for (i=0; i<pEnc->num_plugins;i++) {
607            if (pEnc->plugins[i].func) {
608                pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0);
609            }
610        }
611        xvid_free(pEnc->plugins);
612    
613          xvid_free(pEnc);          xvid_free(pEnc);
614    
615          return 0;  /* ok */          return 0;  /* ok */
616  }  }
617    
618    
619    /*
620      call the plugins
621      */
622    
623    static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, int opt, int * type, int * quant)
624    {
625        int i;
626        xvid_plg_data_t data;
627    
628        memset(&data, 0, sizeof(xvid_plg_data_t));
629        data.version = XVID_VERSION;
630    
631        data.width = pEnc->mbParam.width;
632        data.height = pEnc->mbParam.height;
633        data.fincr = pEnc->mbParam.fincr;
634        data.fbase = pEnc->mbParam.fbase;
635    
636        data.reference.csp = XVID_CSP_USER;
637        data.reference.plane[0] = pEnc->reference->image.y;
638        data.reference.plane[1] = pEnc->reference->image.u;
639        data.reference.plane[2] = pEnc->reference->image.v;
640        data.reference.stride[0] = pEnc->mbParam.edged_width;
641        data.reference.stride[1] = pEnc->mbParam.edged_width/2;
642        data.reference.stride[2] = pEnc->mbParam.edged_width/2;
643    
644        data.current.csp = XVID_CSP_USER;
645        data.current.plane[0] = frame->image.y;
646        data.current.plane[1] = frame->image.u;
647        data.current.plane[2] = frame->image.v;
648        data.current.stride[0] = pEnc->mbParam.edged_width;
649        data.current.stride[1] = pEnc->mbParam.edged_width/2;
650        data.current.stride[2] = pEnc->mbParam.edged_width/2;
651    
652        data.frame_num = frame->frame_num;
653    
654        if (opt == XVID_PLG_BEFORE) {
655            data.type = XVID_TYPE_AUTO;
656            data.quant = 2;
657            //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);
658            //data.qscale_stride = pEnc->mbParam.width;
659            //data.qscale_table = pEnc->temp_dquants;
660            /* todo: vol,vop,motion flags */
661    
662        } else { // XVID_PLG_AFTER
663            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
664                data.original.csp = XVID_CSP_USER;
665                data.original.plane[0] = original->y;
666                data.original.plane[1] = original->u;
667                data.original.plane[2] = original->v;
668                data.original.stride[0] = pEnc->mbParam.edged_width;
669                data.original.stride[1] = pEnc->mbParam.edged_width/2;
670                data.original.stride[2] = pEnc->mbParam.edged_width/2;
671            }
672    
673            data.type = coding2type(frame->coding_type);
674            data.quant = frame->quant;
675            /* todo: data.qscale */
676            data.vol_flags = frame->vol_flags;
677            data.vop_flags = frame->vop_flags;
678            data.motion_flags = frame->motion_flags;
679    
680            data.length = frame->length;
681            data.kblks = frame->sStat.kblks;
682            data.mblks = frame->sStat.mblks;
683            data.ublks = frame->sStat.ublks;
684        }
685    
686        for (i=0; i<pEnc->num_plugins;i++) {
687            if (pEnc->plugins[i].func) {
688                if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
689                    continue;
690                }
691            }
692        }
693    
694        if (opt == XVID_PLG_BEFORE) {
695            *type = data.type;
696            *quant = data.quant;
697            /* todo: copy modified qscale,vol,vop,motion flags into the frame*/
698        }
699    
700    }
701    
702    
703    
704    
705  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
706  {  {
707        pEnc->current->frame_num = pEnc->m_framenum;
708          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;  
709    
710        pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
711      pEnc->m_framenum++; /* debug ticker */      pEnc->m_framenum++; /* debug ticker */
712  }  }
713    
714    static __inline void dec_frame_num(Encoder * pEnc)
715    {
716        pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
717        pEnc->m_framenum--; /* debug ticker */
718    }
719    
720    
721    
722  static __inline void  static __inline void
# Line 607  Line 740 
740    
741    
742  static void  static void
743  set_stats(xvid_enc_stats_t * stats, RateControl * rc,  set_stats(xvid_enc_stats_t * stats, const MBParam * pParam, const FRAMEINFO * frame)
                   const MBParam * pParam, const FRAMEINFO * frame)  
744  {  {
745          if (stats)          if (stats)
746          {          {
# Line 622  Line 754 
754                  stats->mblks = frame->sStat.mblks;                  stats->mblks = frame->sStat.mblks;
755                  stats->ublks = frame->sStat.ublks;                  stats->ublks = frame->sStat.ublks;
756          }          }
   
         RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP );  
757  }  }
758    
759    
# Line 641  Line 771 
771   ****************************************************************************/   ****************************************************************************/
772    
773    
 /* XXX: dx50bvop broken! something todo with queue */  
   
774  int  int
775  enc_encode(Encoder * pEnc,  enc_encode(Encoder * pEnc,
776                             xvid_enc_frame_t * xFrame,                             xvid_enc_frame_t * xFrame,
# Line 650  Line 778 
778  {  {
779          xvid_enc_frame_t * frame;          xvid_enc_frame_t * frame;
780          int type;          int type;
         uint16_t x, y;  
781          Bitstream bs;          Bitstream bs;
782    
783          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 840 
840    
841          if (pEnc->flush_bframes)          if (pEnc->flush_bframes)
842          {          {
843                  if (pEnc->bframenum_head < pEnc->bframenum_tail)                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
                 {  
844    
845                          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",
846                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
847                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
848    
849                          if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {              if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
850                                  image_copy(&pEnc->sOriginal, &pEnc->bframes[pEnc->bframenum_head]->image,                                  image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
851                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
852                          }                          }
853    
854                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
855    
856                          if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {              emms();
857                                  stats->sse_y =              set_stats(stats, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]);
858                                          plane_sse( pEnc->sOriginal.y, pEnc->bframes[pEnc->bframenum_head]->image.y,              call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0);
859                                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,              emms();
                                                            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]);  
860    
861                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
862    
   
863                          goto done;                          goto done;
864                  }                  }
865    
                 /* flush complete: reset counters */  
   
                 pEnc->flush_bframes = 0;  
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
   
866                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
867    
868                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
# Line 764  Line 870 
870                     indentical to the future-referece frame.                     indentical to the future-referece frame.
871                  */                  */
872    
873                  if ((pEnc->mbParam.global_flags & XVID_PACKED)) {                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {
874                          int tmp;                          int tmp;
875                          int bits;                          int bits;
876    
# Line 783  Line 889 
889    
890                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
891                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
892                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);              emms();
893                set_stats(stats, &pEnc->mbParam, pEnc->current);
894                          // xFrame->out_flags |= XVID_PACKED_FIXUP?;              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);
895                emms();
896    
897                /* flush complete: reset counters */
898                    pEnc->flush_bframes = 0;
899                        pEnc->bframenum_head = pEnc->bframenum_tail = 0;
900                          goto done;                          goto done;
901    
902                  }                  }
         }  
903    
904            /* flush complete: reset counters */
905                    pEnc->flush_bframes = 0;
906                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
907            }
908    
909          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910           * dequeue frame from the encoding queue           * dequeue frame from the encoding queue
# Line 801  Line 914 
914          {          {
915                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
916                  {                  {
917                          if (pEnc->bframenum_tail > 0)   /* are bframes */  
918                if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) {
919                    emms();
920                                set_stats(stats, &pEnc->mbParam, pEnc->current);
921                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);
922                    emms();
923                }
924    
925                /* if the very last frame is to be b-vop, we must change it to a p-vop */
926                if (pEnc->bframenum_tail > 0)
927                          {                          {
928    
929                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
930                                  pEnc->bframenum_tail--;                                  pEnc->bframenum_tail--;
931                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
932    
933                                  /* XXX: convert B-VOP to P-VOP */                                  /* convert B-VOP to P-VOP */
934                                  pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
935    
936                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
937                                image_copy(&pEnc->sOriginal, &pEnc->current->image,
938                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
939                    }
940    
941                                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs, 1, 0);
942    
943                                  goto done_flush;                                  goto done_flush;
944                          }                          }
945    
# Line 831  Line 960 
960    
961    
962          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963           * init pEnc->current field           * init pEnc->current fields
964           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
965    
         emms();         /* float-point region */  
   
966      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
967      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
968          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
# Line 847  Line 974 
974                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
975      }      }
976    
977      if (xFrame->vop_flags & XVID_EXTRASTATS) {          emms();         /* float-point region */
                 image_copy(&pEnc->sOriginal, &pEnc->current->image,  
                        pEnc->mbParam.edged_width, pEnc->mbParam.height);  
     }  
978    
979          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
980                  int *temp_dquants =     (int *) xvid_malloc(pEnc->mbParam.mb_width *          unsigned int x, y;
                                         pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);  
981    
982                  pEnc->current->quant =                  pEnc->current->quant =
983                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
984                                                            pEnc->mbParam.edged_width, temp_dquants,                                                            pEnc->mbParam.edged_width, pEnc->temp_dquants,
985                                                            pEnc->current->quant, pEnc->current->quant,                                                            pEnc->current->quant, pEnc->current->quant,
986                                                            2 * pEnc->current->quant,                                                            2 * pEnc->current->quant,
987                                                            pEnc->mbParam.mb_width,                                                            pEnc->mbParam.mb_width,
# Line 871  Line 994 
994                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
995                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
996    
997                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                  pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];
998                          }                          }
999    
1000  #undef OFFSET  #undef OFFSET
1001                  }                  }
1002    
                 xvid_free(temp_dquants);  
1003          }          }
1004    
1005          if (frame->quant > 0)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006          {           * frame type & quant selection
1007                  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, ...);  
         }  
1008    
1009          emms();         /* END floating-point region */          emms();         /* END floating-point region */
1010        call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant);
1011        emms();
1012    
1013        if (frame->type > 0)
1014                    type = frame->type;
1015    
1016          if (type > 0)   /* XVID_TYPE_?VOP */      if (frame->quant > 0)
1017          {                  pEnc->current->quant = frame->quant;
1018    
1019        if (type > 0){      /* XVID_TYPE_?VOP */
1020                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1021          }      } else{             /* XVID_TYPE_AUTO */
1022          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))  
                 {  
1023                          pEnc->iFrameNum = 0;                          pEnc->iFrameNum = 0;
1024                          type = I_VOP;                          type = I_VOP;
1025                  }else{                  }else{
1026                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1027                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1028                                          pEnc->iFrameNum, pEnc->bframenum_tail);                                          pEnc->iFrameNum, pEnc->bframenum_tail);
1029    
1030                if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) {
1031                    type = P_VOP;   /* disable dynamic bframes */
1032                }
1033                  }                  }
1034          }          }
1035    
1036        /* bframes buffer overflow check */
1037        if (type != I_VOP) {
1038            if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1039                type = P_VOP;
1040            }else{
1041                type = B_VOP;
1042            }
1043        }
1044    
1045          inc_frame_num(pEnc);          inc_frame_num(pEnc);
1046          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1047    
   
1048          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1049                  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,
1050                          "%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);
1051          }          }
1052    
1053          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054           * ivop/pvop/bvop selection           * encode this frame as a b-vop
1055         * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1056           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1057            if (type == B_VOP) {
1058                    if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1059                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1060                    }
1061    
1062          if (type == I_VOP) {                  if (frame->bquant < 1) {
1063                            pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1064                                    pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1065    
1066                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  } else {
1067                            pEnc->current->quant = frame->bquant;
1068                    }
1069    
1070                    if (pEnc->current->quant < 1)
1071                            pEnc->current->quant = 1;
1072                    else if (pEnc->current->quant > 31)
1073                pEnc->current->quant = 31;
1074    
1075                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i",
1076                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1077                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1078    
1079                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  /* store frame into bframe buffer & swap ref back to current */
1080                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1081                    SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1082    
1083                    pEnc->bframenum_tail++;
1084    
1085                    goto repeat;
1086                  }                  }
1087    
1088                  // when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame      /* for unpacked bframes, output the stats for the last encoded frame */
1089                  if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {      if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1090        {
1091            if (pEnc->current->stamp > 0) {
1092                emms();
1093                            set_stats(stats, &pEnc->mbParam, pEnc->reference);
1094                call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);
1095                emms();
1096            }
1097                    else
1098                            stats->type = XVID_TYPE_NOTHING;
1099        }
1100    
1101            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1102             * closed-gop
1103         * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1104         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1105    
1106        if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1107    
1108                          // place this frame back on the encoding-queue (head)                          // place this frame back on the encoding-queue (head)
1109                          // we will deal with it next time                          // we will deal with it next time
1110                          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);
1111            pEnc->queue_size++;
1112                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1113    
1114            dec_frame_num(pEnc);
1115            pEnc->iFrameNum--;
1116    
1117                          // grab the last frame from the bframe-queue                          // grab the last frame from the bframe-queue
1118    
1119                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
# Line 951  Line 1123 
1123                                  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");
1124                          }                          }
1125    
1126                          /* XXX: convert B-VOP to P-VOP */                  /* convert B-VOP quant to P-VOP */
1127                          pEnc->current->quant *= 2;                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
1128            type = P_VOP;
1129        }
1130    
                         FrameCodeP(pEnc, &bs, 1, 0);  
1131    
1132                  } else {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1133             * encode this frame as an i-vop
1134         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1135    
1136            if (type == I_VOP) {
1137    
1138                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1139                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1140                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1141    
1142                    if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1143                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1144                    }
1145    
1146    
1147                          /* ---- update vol flags at IVOP ----------- */                          /* ---- update vol flags at IVOP ----------- */
1148                          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 1164 
1164    
1165                          /* ^^^------------------------ */                          /* ^^^------------------------ */
1166    
1167            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1168                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1169                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1170            }
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                  /*       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1179                   * This will be coded as a Predicted Frame  
1180                   */      } else { // (type == P_VOP || type == S_VOP)
1181    
1182                  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",
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 996  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        /* packed or no-bframes: output stats */
1213          if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)          if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)
1214          {          {
1215                  /* packed(and low_delay) encoding gives us graceful reorded-stats output */          emms();
1216                  set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);                  set_stats(stats, &pEnc->mbParam, pEnc->current);
1217          }          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0);
1218          else          emms();
         {  
                 /* 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;  
1219          }          }
1220    
1221          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1155  Line 1289 
1289                  stop_edges_timer();                  stop_edges_timer();
1290          }          }
1291    
         //pEnc->iFrameNum = 0;  
1292          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1293          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;  
1294          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1295    
1296          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);

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

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