[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.8, Sat Mar 15 17:06:53 2003 UTC revision 1.95.2.14, Wed Mar 26 11:01:03 2003 UTC
# Line 54  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 75  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 134  Line 116 
116    
117    
118  int  int
119  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)  enc_create(xvid_enc_create_t * create)
120  {  {
121          Encoder *pEnc;          Encoder *pEnc;
122      int n;      int n;
123    
124          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1))   /* v1.x.x */          if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */
125                  return XVID_ERR_VERSION;                  return XVID_ERR_VERSION;
126    
127          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
# Line 205  Line 187 
187      }      }
188    
189      /* temp dquants */      /* temp dquants */
190        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
191          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
192                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
193        }
194      /* XXX: error checking */      /* XXX: error checking */
195    
196          /* bframes */          /* bframes */
# Line 504  Line 488 
488          xvid_free(pEnc->current);          xvid_free(pEnc->current);
489          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
490    
491        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
492          xvid_free(pEnc->temp_dquants);          xvid_free(pEnc->temp_dquants);
493        }
494    
495    xvid_err_memory0:    xvid_err_memory0:
496      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
# Line 607  Line 593 
593          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
594          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
595    
596        if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
597      xvid_free(pEnc->temp_dquants);      xvid_free(pEnc->temp_dquants);
598        }
599    
600    
601        if (pEnc->num_plugins>0)
602        {
603            xvid_plg_destroy_t pdestroy;
604            memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
605    
606            pdestroy.version = XVID_VERSION;
607            pdestroy.num_frames = pEnc->m_framenum;
608    
609      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
610          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
611              pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0);                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0);
612          }          }
613      }      }
614      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
615        }
616    
617          xvid_free(pEnc);          xvid_free(pEnc);
618    
# Line 629  Line 627 
627  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
628                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)
629  {  {
630      int i;      unsigned int i, j;
631      xvid_plg_data_t data;      xvid_plg_data_t data;
632    
633        /* set data struct */
634    
635      memset(&data, 0, sizeof(xvid_plg_data_t));      memset(&data, 0, sizeof(xvid_plg_data_t));
636      data.version = XVID_VERSION;      data.version = XVID_VERSION;
637    
638      data.width = pEnc->mbParam.width;      data.width = pEnc->mbParam.width;
639      data.height = pEnc->mbParam.height;      data.height = pEnc->mbParam.height;
640        data.mb_width = pEnc->mbParam.mb_width;
641        data.mb_height = pEnc->mbParam.mb_height;
642      data.fincr = frame->fincr;      data.fincr = frame->fincr;
643      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
644    
# Line 661  Line 663 
663      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
664          data.type = XVID_TYPE_AUTO;          data.type = XVID_TYPE_AUTO;
665          data.quant = 2;          data.quant = 2;
666          //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);  
667          //data.qscale_stride = pEnc->mbParam.width;          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
668          //data.qscale_table = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
669          /* todo: vol,vop,motion flags */              data.dquant_stride = pEnc->mbParam.mb_width;
670                memset(data.dquant, 0, data.mb_width*data.mb_height);
671            }
672    
673            /* todo: [vol,vop,motion]_flags*/
674    
675      } else { // XVID_PLG_AFTER      } else { // XVID_PLG_AFTER
676          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
# Line 698  Line 704 
704    
705          data.type = coding2type(frame->coding_type);          data.type = coding2type(frame->coding_type);
706          data.quant = frame->quant;          data.quant = frame->quant;
707          /* todo: data.qscale */  
708            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
709                data.dquant = pEnc->temp_dquants;
710                data.dquant_stride = pEnc->mbParam.mb_width;
711    
712                for (j=0; j<pEnc->mbParam.mb_height; j++)
713                for (i=0; i<pEnc->mbParam.mb_width; i++) {
714                    data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;;
715                }
716            }
717    
718          data.vol_flags = frame->vol_flags;          data.vol_flags = frame->vol_flags;
719          data.vop_flags = frame->vop_flags;          data.vop_flags = frame->vop_flags;
720          data.motion_flags = frame->motion_flags;          data.motion_flags = frame->motion_flags;
# Line 708  Line 724 
724          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
725          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
726    
727          if (stats)          if (stats) {
         {  
728                  stats->type = coding2type(frame->coding_type);                  stats->type = coding2type(frame->coding_type);
729                  stats->quant = frame->quant;                  stats->quant = frame->quant;
730                  stats->vol_flags = frame->vol_flags;                  stats->vol_flags = frame->vol_flags;
# Line 725  Line 740 
740          }          }
741      }      }
742    
743      emms();      /* call plugins */
744      for (i=0; i<pEnc->num_plugins;i++) {      for (i=0; i<pEnc->num_plugins;i++) {
745            emms();
746          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
747              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
748                  continue;                  continue;
# Line 735  Line 751 
751      }      }
752      emms();      emms();
753    
754        /* copy modified values back into frame*/
755      if (opt == XVID_PLG_BEFORE) {      if (opt == XVID_PLG_BEFORE) {
756          *type = data.type;          *type = data.type;
757          *quant = data.quant;          *quant = data.quant;
         /* todo: copy modified qscale,vol,vop,motion flags into the frame*/  
     }  
758    
759            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
760                for (j=0; j<pEnc->mbParam.mb_height; j++)
761                for (i=0; i<pEnc->mbParam.mb_width; i++) {
762                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
763                }
764            }else{
765                for (j=0; j<pEnc->mbParam.mb_height; j++)
766                for (i=0; i<pEnc->mbParam.mb_width; i++) {
767                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
768                }
769            }
770            /* todo: [vol,vop,motion]_flags*/
771        }
772  }  }
773    
774    
# Line 978  Line 1006 
1006           * init pEnc->current fields           * init pEnc->current fields
1007           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1008    
   
1009      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1010        inc_frame_num(pEnc);
1011      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
1012      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1013          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1014          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1015          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
1016    
1017    
1018      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
1019              image_chroma_optimize(&pEnc->current->image,              image_chroma_optimize(&pEnc->current->image,
1020                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1021      }      }
1022    
         emms();         /* float-point region */  
         if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {  
         unsigned int x, y;  
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                                           pEnc->mbParam.edged_width, pEnc->temp_dquants,  
                                                           pEnc->current->quant, pEnc->current->quant,  
                                                           2 * pEnc->current->quant,  
                                                           pEnc->mbParam.mb_width,  
                                                           pEnc->mbParam.mb_height);  
   
                 for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
   
 #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)  
   
                         for (x = 0; x < pEnc->mbParam.mb_width; x++) {  
                                 MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];  
   
                                 pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];  
                         }  
   
 #undef OFFSET  
                 }  
   
         }  
     emms();             /* END floating-point region */  
   
1023          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1024           * frame type & quant selection           * frame type & quant selection
1025           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 1057  Line 1058 
1058          }          }
1059      }      }
1060    
         inc_frame_num(pEnc);  
1061          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1062    
1063          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
# Line 1074  Line 1074 
1074                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1075                  }                  }
1076    
1077                  if (frame->bquant < 1) {                  if (frame->quant < 1) {
1078                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1079                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1080    
1081                  } else {                  } else {
1082                          pEnc->current->quant = frame->bquant;                          pEnc->current->quant = frame->quant;
1083                  }                  }
1084    
1085                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1239  Line 1239 
1239  }  }
1240    
1241    
1242    static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1243    {
1244        unsigned int i,j;
1245        int quant = frame->quant;
1246    
1247        for (j=0; j<pParam->mb_height; j++)
1248        for (i=0; i<pParam->mb_width; i++) {
1249            MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];
1250            quant += pMB->dquant;
1251            if (quant > 31)
1252                            quant = 31;
1253                    if (quant < 1)
1254                            quant = 1;
1255            pMB->quant = quant;
1256        }
1257    }
1258    
1259    
1260  static __inline void  static __inline void
1261  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(Encoder * pEnc,
1262                          MACROBLOCK * pMB)                          MACROBLOCK * pMB)
# Line 1252  Line 1270 
1270          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;
1271          pMB->sad16 = 0;          pMB->sad16 = 0;
1272    
1273          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if (pMB->dquant != 0) {
                 if (pMB->dquant != NO_CHANGE) {  
1274                          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;  
1275                  }                  }
1276          }          }
1277    
         pMB->quant = pEnc->current->quant;  
 }  
   
1278    
1279    
1280  static int  static int
# Line 1300  Line 1308 
1308          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1309          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1310    
1311        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1312    
1313          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1314    
1315          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
# Line 1424  Line 1434 
1434    
1435          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1436    
1437    
1438        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1439    
1440          start_timer();          start_timer();
1441          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1442                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
# Line 1537  Line 1550 
1550    
1551                          stop_comp_timer();                          stop_comp_timer();
1552    
1553                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 if (pMB->dquant != NO_CHANGE) {  
1554                                          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;  
                                 }  
1555                          }                          }
                         pMB->quant = pEnc->current->quant;  
1556    
1557                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1558    
# Line 1570  Line 1575 
1575                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1576    
1577                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1578                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == 0);
1579    
1580                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1581                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
# Line 1852  Line 1857 
1857                                  continue;                                  continue;
1858                          }                          }
1859    
1860                          if (mb->mode != MODE_DIRECT_NONE_MV) {                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1861                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1862                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1863                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
# Line 1862  Line 1867 
1867                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1868                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
1869    
1870                                  mb->cbp =                                  if (mb->mode != MODE_DIRECT_NONE_MV)
1871                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1872    
1873                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1874                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {

Legend:
Removed from v.1.95.2.8  
changed lines
  Added in v.1.95.2.14

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