[svn] / trunk / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /trunk/xvidcore/src/encoder.c

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

revision 1607, Sun Mar 27 03:59:42 2005 UTC revision 1693, Fri Mar 3 11:54:58 2006 UTC
# Line 21  Line 21 
21   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   * $Id: encoder.c,v 1.117 2005-03-27 03:59:42 suxen_drol Exp $   * $Id: encoder.c,v 1.128 2006-03-03 11:54:58 syskin Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 49  Line 49 
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
52    # include "motion/motion_smp.h"
53    
54    
55  /*****************************************************************************  /*****************************************************************************
56   * Local function prototypes   * Local function prototypes
57   ****************************************************************************/   ****************************************************************************/
# Line 85  Line 88 
88  /*  /*
89   * Simplify the "fincr/fbase" fraction   * Simplify the "fincr/fbase" fraction
90  */  */
91    static int
92    gcd(int a, int b)
93    {
94            int r ;
95    
96            if (b > a) {
97                    r = a;
98                    a = b;
99                    b = r;
100            }
101    
102            while ((r = a % b)) {
103                    a = b;
104                    b = r;
105            }
106            return b;
107    }
108    
109  static void  static void
110  simplify_time(int *inc, int *base)  simplify_time(int *inc, int *base)
111  {  {
112          /* common factor */          /* common factor */
113          int i = *inc;          const int s = gcd(*inc, *base);
114          while (i > 1) {    *inc  /= s;
115                  if (*inc % i == 0 && *base % i == 0) {    *base /= s;
                         *inc /= i;  
                         *base /= i;  
                         i = *inc;  
                         continue;  
                 }  
                 i--;  
         }  
116    
117          if (*base > 65535 || *inc > 65535) {          if (*base > 65535 || *inc > 65535) {
118                  int *biggest;                  int *biggest;
# Line 114  Line 128 
128                  }                  }
129    
130                  div = ((float)*biggest)/((float)65535);                  div = ((float)*biggest)/((float)65535);
131                  *biggest = (int)(((float)*biggest)/div);                  *biggest = (unsigned int)(((float)*biggest)/div);
132                  *other = (int)(((float)*other)/div);                  *other = (unsigned int)(((float)*other)/div);
133          }          }
134  }  }
135    
# Line 161  Line 175 
175          pEnc->mbParam.fincr = MAX(create->fincr, 0);          pEnc->mbParam.fincr = MAX(create->fincr, 0);
176          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
177          if (pEnc->mbParam.fincr>0)          if (pEnc->mbParam.fincr>0)
178                  simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);                  simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase);
179    
180          /* zones */          /* zones */
181          if(create->num_zones > 0) {          if(create->num_zones > 0) {
# Line 192  Line 206 
206    
207                  memset(&pinfo, 0, sizeof(xvid_plg_info_t));                  memset(&pinfo, 0, sizeof(xvid_plg_info_t));
208                  pinfo.version = XVID_VERSION;                  pinfo.version = XVID_VERSION;
209                  if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {                  if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) {
210                          pEnc->mbParam.plugin_flags |= pinfo.flags;                          pEnc->mbParam.plugin_flags |= pinfo.flags;
211                  }                  }
212    
# Line 209  Line 223 
223                  pcreate.param = create->plugins[n].param;                  pcreate.param = create->plugins[n].param;
224    
225                  pEnc->plugins[n].func = NULL;   /* disable plugins that fail */                  pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
226                  if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {                  if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
227                          pEnc->plugins[n].func = create->plugins[n].func;                          pEnc->plugins[n].func = create->plugins[n].func;
228                  }                  }
229          }          }
# Line 227  Line 241 
241                          goto xvid_err_memory1a;                          goto xvid_err_memory1a;
242          }          }
243    
244            /* temp lambdas */
245            if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
246                    pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width *
247                                                    pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE);
248                    if (pEnc->temp_lambda == NULL)
249                            goto xvid_err_memory1a;
250            }
251    
252          /* bframes */          /* bframes */
253          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
254          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);          pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
# Line 422  Line 444 
444          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
445          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
446    
447            /* multithreaded stuff */
448            if (create->num_threads > 0) {
449                    int t = create->num_threads;
450                    int rows_per_thread = (pEnc->mbParam.mb_height+t-1)/t;
451                    pEnc->num_threads = t;
452                    pEnc->motionData = xvid_malloc(t*sizeof(SMPmotionData), CACHE_LINE);
453                    if (!pEnc->motionData)
454                            goto xvid_err_nosmp;
455    
456                    for (n = 0; n < t; n++) {
457                            pEnc->motionData[n].complete_count_self =
458                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
459    
460                            if (!pEnc->motionData[n].complete_count_self)
461                                    goto xvid_err_nosmp;
462    
463                            if (n != 0)
464                                    pEnc->motionData[n].complete_count_above =
465                                            pEnc->motionData[n-1].complete_count_self;
466                    }
467                    pEnc->motionData[0].complete_count_above =
468                            pEnc->motionData[t-1].complete_count_self - 1;
469    
470            } else {
471      xvid_err_nosmp:
472                    /* no SMP */
473                    create->num_threads = 0;
474                    pEnc->motionData = NULL;
475            }
476    
477          create->handle = (void *) pEnc;          create->handle = (void *) pEnc;
478    
479          init_timer();          init_timer();
# Line 508  Line 560 
560                  xvid_free(pEnc->temp_dquants);                  xvid_free(pEnc->temp_dquants);
561          }          }
562    
563            if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
564                    xvid_free(pEnc->temp_lambda);
565            }
566    
567    xvid_err_memory0:    xvid_err_memory0:
568          for (n=0; n<pEnc->num_plugins;n++) {          for (n=0; n<pEnc->num_plugins;n++) {
569                  if (pEnc->plugins[n].func) {                  if (pEnc->plugins[n].func) {
570                          pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);                          pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
571                  }                  }
572          }          }
573          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
# Line 606  Line 662 
662                  xvid_free(pEnc->temp_dquants);                  xvid_free(pEnc->temp_dquants);
663          }          }
664    
665            if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
666                    xvid_free(pEnc->temp_lambda);
667            }
668    
669          if (pEnc->num_plugins>0) {          if (pEnc->num_plugins>0) {
670                  xvid_plg_destroy_t pdestroy;                  xvid_plg_destroy_t pdestroy;
# Line 616  Line 675 
675    
676                  for (i=0; i<pEnc->num_plugins;i++) {                  for (i=0; i<pEnc->num_plugins;i++) {
677                          if (pEnc->plugins[i].func) {                          if (pEnc->plugins[i].func) {
678                                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0);                                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
679                          }                          }
680                  }                  }
681                  xvid_free(pEnc->plugins);                  xvid_free(pEnc->plugins);
# Line 624  Line 683 
683    
684          xvid_free(pEnc->mbParam.mpeg_quant_matrices);          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
685    
686          if (pEnc->num_plugins>0)          if (pEnc->num_zones > 0)
687                  xvid_free(pEnc->zones);                  xvid_free(pEnc->zones);
688    
689            if (pEnc->num_threads > 0) {
690                    for (i = 0; i < pEnc->num_threads; i++)
691                            xvid_free(pEnc->motionData[i].complete_count_self);
692    
693                    xvid_free(pEnc->motionData);
694            }
695    
696          xvid_free(pEnc);          xvid_free(pEnc);
697    
698          return 0;  /* ok */          return 0;  /* ok */
# Line 640  Line 706 
706  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
707                                                   int opt, int * type, int * quant, xvid_enc_stats_t * stats)                                                   int opt, int * type, int * quant, xvid_enc_stats_t * stats)
708  {  {
709          unsigned int i, j;          unsigned int i, j, k;
710          xvid_plg_data_t data;          xvid_plg_data_t data;
711    
712          /* set data struct */          /* set data struct */
# Line 699  Line 765 
765                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
766                          data.dquant = pEnc->temp_dquants;                          data.dquant = pEnc->temp_dquants;
767                          data.dquant_stride = pEnc->mbParam.mb_width;                          data.dquant_stride = pEnc->mbParam.mb_width;
768                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
769                    }
770    
771                    if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
772                            int block = 0;
773                            data.lambda = pEnc->temp_lambda;
774                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
775                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
776                                            for (k = 0; k < 6; k++)
777                                                    data.lambda[block++] = 1.0f;
778                  }                  }
779    
780          } else { /* XVID_PLG_AFTER */          } else { /* XVID_PLG_AFTER */
# Line 776  Line 851 
851          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
852                  emms();                  emms();
853                  if (pEnc->plugins[i].func) {                  if (pEnc->plugins[i].func) {
854                          if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {                          if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
855                                  continue;                                  continue;
856                          }                          }
857                  }                  }
# Line 805  Line 880 
880                                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
881                          }                          }
882                  }                  }
883    
884                    if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
885                            for (j = 0; j < pEnc->mbParam.mb_height; j++)
886                                    for (i = 0; i < pEnc->mbParam.mb_width; i++)
887                                            for (k = 0; k < 6; k++) {
888                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
889                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
890                            }
891                    } else {
892                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
893                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
894                                            for (k = 0; k < 6; k++) {
895                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
896                            }
897                    }
898    
899    
900                  frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */                  frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
901          }          }
902    
# Line 874  Line 966 
966  #endif  #endif
967  }  }
968    
 static int  
 gcd(int a, int b)  
 {  
         int r ;  
   
         if (b > a) {  
                 r = a;  
                 a = b;  
                 b = r;  
         }  
   
         while ((r = a % b)) {  
                 a = b;  
                 b = r;  
         }  
         return b;  
 }  
   
969  static void  static void
970  simplify_par(int *par_width, int *par_height)  simplify_par(int *par_width, int *par_height)
971  {  {
# Line 1017  Line 1091 
1091                          }                          }
1092    
1093                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1094                          call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1095                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1096    
1097                          goto done;                          goto done;
# Line 1049  Line 1123 
1123    
1124                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1125                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1126                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1127    
1128                          /* flush complete: reset counters */                          /* flush complete: reset counters */
1129                          pEnc->flush_bframes = 0;                          pEnc->flush_bframes = 0;
# Line 1077  Line 1151 
1151                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1152    
1153                          if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {                          if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1154                                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1155                          }                          }
1156    
1157                          /* if the very last frame is to be b-vop, we must change it to a p-vop */                          /* if the very last frame is to be b-vop, we must change it to a p-vop */
# Line 1106  Line 1180 
1180    
1181    
1182                                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {                                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1183                                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1184                                  }else{                                  }else{
1185                                          pEnc->flush_bframes = 1;                                          pEnc->flush_bframes = 1;
1186                                          goto done;                                          goto done;
# Line 1155  Line 1229 
1229          type = frame->type;          type = frame->type;
1230          pEnc->current->quant = frame->quant;          pEnc->current->quant = frame->quant;
1231    
1232          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1233    
1234          if (type > 0){  /* XVID_TYPE_?VOP */          if (type > 0){  /* XVID_TYPE_?VOP */
1235                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
# Line 1230  Line 1304 
1304          if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)          if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1305          {          {
1306                  if (pEnc->current->stamp > 0) {                  if (pEnc->current->stamp > 0) {
1307                          call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1308                  }                  }
1309                  else                  else
1310                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
# Line 1353  Line 1427 
1427    
1428                  if ( FrameCodeP(pEnc, &bs) == 0 ) {                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1429                          /* N-VOP, we mustn't code b-frames yet */                          /* N-VOP, we mustn't code b-frames yet */
1430                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1431                                     pEnc->mbParam.max_bframes == 0)
1432                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1433                          goto done;                          goto done;
1434                  }                  }
1435          }          }
# Line 1374  Line 1450 
1450    
1451          /* packed or no-bframes or no-bframes-queued: output stats */          /* packed or no-bframes or no-bframes-queued: output stats */
1452          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1453                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1454          }          }
1455    
1456          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1463  Line 1539 
1539          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1540    
1541          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1542            pEnc->current->sStat.iMVBits = 0;
1543          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1544          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1545    
# Line 1561  Line 1638 
1638          if ((current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1639                  if (reference->is_interpolated != current->rounding_type) {                  if (reference->is_interpolated != current->rounding_type) {
1640                          start_timer();                          start_timer();
1641                          image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1642                                                            &pEnc->vInterHV, pParam->edged_width,                                                            pEnc->vInterHV.y, pParam->edged_width,
1643                                                            pParam->edged_height,                                                            pParam->edged_height,
1644                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1645                                                            current->rounding_type);                                                            current->rounding_type);
# Line 1572  Line 1649 
1649          }          }
1650    
1651          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1652                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1653                    current->sStat.iMVBits = 0;
1654    
1655          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1656    
# Line 1631  Line 1709 
1709                  }                  }
1710          }          }
1711    
1712    
1713            if (pEnc->num_threads > 0) {
1714                    /* multithreaded motion estimation - dispatch threads */
1715    
1716                    void * status;
1717                    int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
1718    
1719                    for (k = 0; k < pEnc->num_threads; k++) {
1720                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
1721                            pEnc->motionData[k].pParam = &pEnc->mbParam;
1722                            pEnc->motionData[k].current = current;
1723                            pEnc->motionData[k].reference = reference;
1724                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
1725                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
1726                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
1727                            pEnc->motionData[k].pGMC = &pEnc->vGMC;
1728                            pEnc->motionData[k].y_step = pEnc->num_threads;
1729                            pEnc->motionData[k].start_y = k;
1730                            /* todo: sort out temp space once and for all */
1731                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width;
1732                    }
1733    
1734                    for (k = 1; k < pEnc->num_threads; k++) {
1735                            pthread_create(&pEnc->motionData[k].handle, NULL,
1736                                    (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]);
1737                    }
1738    
1739                    MotionEstimateSMP(&pEnc->motionData[0]);
1740    
1741                    for (k = 1; k < pEnc->num_threads; k++) {
1742                            pthread_join(pEnc->motionData[k].handle, &status);
1743                    }
1744    
1745                    current->fcode = 0;
1746                    for (k = 0; k < pEnc->num_threads; k++) {
1747                            current->sStat.iMvSum += pEnc->motionData[k].mvSum;
1748                            current->sStat.iMvCount += pEnc->motionData[k].mvCount;
1749                            if (pEnc->motionData[k].minfcode > current->fcode)
1750                                    current->fcode = pEnc->motionData[k].minfcode;
1751                    }
1752    
1753            } else {
1754                    /* regular ME */
1755    
1756          MotionEstimation(&pEnc->mbParam, current, reference,          MotionEstimation(&pEnc->mbParam, current, reference,
1757                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1758                                           &pEnc->vGMC, 256*4096);                                           &pEnc->vGMC, 256*4096);
1759            }
1760    
1761          stop_motion_timer();          stop_motion_timer();
1762    
# Line 1764  Line 1886 
1886                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
1887                  ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )                  ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
1888          {          {
1889                  current->sStat.kblks = current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
1890                  current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
1891    
1892                  BitstreamReset(bs);                  BitstreamReset(bs);
# Line 1854  Line 1976 
1976    
1977          if (pEnc->reference->is_interpolated != 0) {          if (pEnc->reference->is_interpolated != 0) {
1978                  start_timer();                  start_timer();
1979                  image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y,
1980                                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1981                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1982                  stop_inter_timer();                  stop_inter_timer();
# Line 1871  Line 1993 
1993    
1994          if (pEnc->current->is_interpolated != 0) {          if (pEnc->current->is_interpolated != 0) {
1995                  start_timer();                  start_timer();
1996                  image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
1997                                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1998                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1999                  stop_inter_timer();                  stop_inter_timer();
# Line 1879  Line 2001 
2001          }          }
2002    
2003          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2004          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);          call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2005    
2006            frame->fcode = frame->bcode = pEnc->current->fcode;
2007    
2008          start_timer();          start_timer();
2009            if (pEnc->num_threads > 0) {
2010                    void * status;
2011                    int k;
2012                    /* multithreaded motion estimation - dispatch threads */
2013                    int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
2014    
2015                    for (k = 0; k < pEnc->num_threads; k++) {
2016                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
2017                            pEnc->motionData[k].pParam = &pEnc->mbParam;
2018                            pEnc->motionData[k].current = frame;
2019                            pEnc->motionData[k].reference = pEnc->current;
2020                            pEnc->motionData[k].fRef = f_ref;
2021                            pEnc->motionData[k].fRefH = &pEnc->f_refh;
2022                            pEnc->motionData[k].fRefV = &pEnc->f_refv;
2023                            pEnc->motionData[k].fRefHV = &pEnc->f_refhv;
2024                            pEnc->motionData[k].pRef = b_ref;
2025                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
2026                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
2027                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
2028                            pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);
2029                            pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);
2030                            pEnc->motionData[k].y_step = pEnc->num_threads;
2031                            pEnc->motionData[k].start_y = k;
2032                            /* todo: sort out temp space once and for all */
2033                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width;
2034                    }
2035    
2036                    for (k = 1; k < pEnc->num_threads; k++) {
2037                            pthread_create(&pEnc->motionData[k].handle, NULL,
2038                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]);
2039                    }
2040    
2041                    SMPMotionEstimationBVOP(&pEnc->motionData[0]);
2042    
2043                    for (k = 1; k < pEnc->num_threads; k++) {
2044                            pthread_join(pEnc->motionData[k].handle, &status);
2045                    }
2046    
2047                    frame->fcode = frame->bcode = 0;
2048                    for (k = 0; k < pEnc->num_threads; k++) {
2049                            if (pEnc->motionData[k].minfcode > frame->fcode)
2050                                    frame->fcode = pEnc->motionData[k].minfcode;
2051                            if (pEnc->motionData[k].minbcode > frame->bcode)
2052                                    frame->bcode = pEnc->motionData[k].minbcode;
2053                    }
2054            } else {
2055          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2056                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2057                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
# Line 1889  Line 2059 
2059                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2060                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2061                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
2062            }
2063          stop_motion_timer();          stop_motion_timer();
2064    
2065          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2066          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2067    
2068          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2069            frame->sStat.iMVBits = 0;
2070          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2071          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2072          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
# Line 1945  Line 2117 
2117                          stop_coding_timer();                          stop_coding_timer();
2118                  }                  }
2119          }          }
   
2120          emms();          emms();
2121    
2122          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */

Legend:
Removed from v.1607  
changed lines
  Added in v.1693

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