[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.7, Sat Mar 15 17:03:17 2003 UTC revision 1.95.2.12, Sun Mar 23 04:01:48 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 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            inc_frame_num(pEnc);
1010    
1011      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1012      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
# Line 986  Line 1015 
1015          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1016          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
1017    
1018    
1019      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
1020              image_chroma_optimize(&pEnc->current->image,              image_chroma_optimize(&pEnc->current->image,
1021                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1022      }      }
1023    
         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 */  
   
1024          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1025           * frame type & quant selection           * frame type & quant selection
1026           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 1057  Line 1059 
1059          }          }
1060      }      }
1061    
         inc_frame_num(pEnc);  
1062          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1063    
1064          if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
# Line 1074  Line 1075 
1075                          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");
1076                  }                  }
1077    
1078                  if (frame->bquant < 1) {                  if (frame->quant < 1) {
1079                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1080                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1081    
1082                  } else {                  } else {
1083                          pEnc->current->quant = frame->bquant;                          pEnc->current->quant = frame->quant;
1084                  }                  }
1085    
1086                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1119  Line 1120 
1120    
1121                  // place this frame back on the encoding-queue (head)                  // place this frame back on the encoding-queue (head)
1122                  // we will deal with it next time                  // we will deal with it next time
1123            dec_frame_num(pEnc);
1124            pEnc->iFrameNum--;
1125    
1126                  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);
1127          pEnc->queue_size++;          pEnc->queue_size++;
1128                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1129    
         dec_frame_num(pEnc);  
         pEnc->iFrameNum--;  
   
1130                  // grab the last frame from the bframe-queue                  // grab the last frame from the bframe-queue
1131    
1132                  pEnc->bframenum_tail--;                  pEnc->bframenum_tail--;
# Line 1239  Line 1240 
1240  }  }
1241    
1242    
1243    static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1244    {
1245        unsigned int i,j;
1246        int quant = frame->quant;
1247    
1248        for (j=0; j<pParam->mb_height; j++)
1249        for (i=0; i<pParam->mb_width; i++) {
1250            MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];
1251            quant += pMB->dquant;
1252            if (quant > 31)
1253                            quant = 31;
1254                    if (quant < 1)
1255                            quant = 1;
1256            pMB->quant = quant;
1257        }
1258    }
1259    
1260    
1261  static __inline void  static __inline void
1262  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(Encoder * pEnc,
1263                          MACROBLOCK * pMB)                          MACROBLOCK * pMB)
# Line 1252  Line 1271 
1271          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;
1272          pMB->sad16 = 0;          pMB->sad16 = 0;
1273    
1274          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if (pMB->dquant != 0) {
                 if (pMB->dquant != NO_CHANGE) {  
1275                          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;  
                 }  
1276          }          }
   
         pMB->quant = pEnc->current->quant;  
1277  }  }
1278    
1279    
# Line 1300  Line 1309 
1309          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1310          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1311    
1312        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1313    
1314          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1315    
1316          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
# Line 1424  Line 1435 
1435    
1436          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1437    
1438    
1439        SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1440    
1441          start_timer();          start_timer();
1442          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1443                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
# Line 1537  Line 1551 
1551    
1552                          stop_comp_timer();                          stop_comp_timer();
1553    
1554                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 if (pMB->dquant != NO_CHANGE) {  
1555                                          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;  
                                 }  
1556                          }                          }
                         pMB->quant = pEnc->current->quant;  
1557    
1558                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1559    
# Line 1570  Line 1576 
1576                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1577    
1578                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1579                                                          (pMB->dquant == NO_CHANGE);                                                          (pMB->dquant == 0);
1580    
1581                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1582                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
# Line 1852  Line 1858 
1858                                  continue;                                  continue;
1859                          }                          }
1860    
1861                          if (mb->mode != MODE_DIRECT_NONE_MV) {                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1862                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1863                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1864                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
# Line 1862  Line 1868 
1868                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1869                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
1870    
1871                                  mb->cbp =                                  if (mb->mode != MODE_DIRECT_NONE_MV)
1872                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1873    
1874                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1875                                          && (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.7  
changed lines
  Added in v.1.95.2.12

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