[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.116, Fri Dec 10 05:37:11 2004 UTC revision 1.135.2.5, Tue Mar 8 19:18:44 2011 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Encoder main module -   *  - Encoder main module -
5   *   *
6   *  Copyright(C) 2002     Michael Militzer <isibaar@xvid.org>   *  Copyright(C) 2002-2010 Michael Militzer <isibaar@xvid.org>
7   *                         2002-2003 Peter Ross <pross@xvid.org>   *                         2002-2003 Peter Ross <pross@xvid.org>
8   *                         2002   Daniel Smith <danielsmith@astroboymail.com>   *                         2002   Daniel Smith <danielsmith@astroboymail.com>
9   *   *
# 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 146  Line 160 
160    
161          /* global flags */          /* global flags */
162          pEnc->mbParam.global_flags = create->global;          pEnc->mbParam.global_flags = create->global;
163      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED))
164        pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA;
165    
166          /* width, height */          /* width, height */
167          pEnc->mbParam.width = create->width;          pEnc->mbParam.width = create->width;
# Line 159  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 190  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 207  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 225  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 411  Line 435 
435          /* timestamp stuff */          /* timestamp stuff */
436    
437          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
438          pEnc->m_framenum = 0;          pEnc->m_framenum = create->start_frame_num;
439          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
440          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
441    
# Line 420  Line 444 
444          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
445          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
446    
447            /* slices */
448            pEnc->num_slices = MIN(MAX(1, create->num_slices), (int) pEnc->mbParam.mb_height);
449    
450            /* multithreaded stuff */
451            if (create->num_threads > 0) {
452                    int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */
453                    int threads_per_slice = MAX(1, (t / pEnc->num_slices));
454                    int rows_per_thread = (pEnc->mbParam.mb_height + threads_per_slice - 1) / threads_per_slice;
455    
456                    pEnc->num_threads = t;
457                    pEnc->smpData = xvid_malloc(t*sizeof(SMPData), CACHE_LINE);
458                    if (!pEnc->smpData)
459                            goto xvid_err_nosmp;
460    
461                    /* tmp bitstream buffer for slice coding */
462                    pEnc->smpData[0].tmp_buffer = xvid_malloc(16*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height*sizeof(uint8_t), CACHE_LINE);
463                    if (! pEnc->smpData[0].tmp_buffer) goto xvid_err_nosmp;
464    
465                    for (n = 0; n < t; n++) {
466                            int s = MIN(pEnc->num_threads, pEnc->num_slices);
467    
468                            pEnc->smpData[n].complete_count_self =
469                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
470    
471                            if (!pEnc->smpData[n].complete_count_self)
472                                    goto xvid_err_nosmp;
473    
474                            if (n > 0 && n < s) {
475                                    pEnc->smpData[n].bs = (Bitstream *) xvid_malloc(sizeof(Bitstream), CACHE_LINE);
476                                    if (!pEnc->smpData[n].bs)
477                                            goto xvid_err_nosmp;
478    
479                                    pEnc->smpData[n].sStat = (Statistics *) xvid_malloc(sizeof(Statistics), CACHE_LINE);
480                                    if (!pEnc->smpData[n].sStat)
481                                            goto xvid_err_nosmp;
482    
483                                    pEnc->smpData[n].tmp_buffer = pEnc->smpData[0].tmp_buffer + 16*(((n-1)*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height)/s);
484                                    BitstreamInit(pEnc->smpData[n].bs, pEnc->smpData[n].tmp_buffer, 0);
485                            }
486    
487                            if (n != 0)
488                                    pEnc->smpData[n].complete_count_above =
489                                            pEnc->smpData[n-1].complete_count_self;
490                    }
491                    pEnc->smpData[0].complete_count_above =
492                            pEnc->smpData[t-1].complete_count_self - 1;
493    
494            } else {
495      xvid_err_nosmp:
496                    /* no SMP */
497                    if (pEnc->smpData) {
498                            if (pEnc->smpData[0].tmp_buffer)
499                                    xvid_free(pEnc->smpData[0].tmp_buffer);
500                    }
501                    else {
502                            pEnc->smpData = xvid_malloc(1*sizeof(SMPData), CACHE_LINE);
503                            if (pEnc->smpData == NULL)
504                                    goto xvid_err_memory5;
505                    }
506    
507                    create->num_threads = 0;
508            }
509    
510          create->handle = (void *) pEnc;          create->handle = (void *) pEnc;
511    
512          init_timer();          init_timer();
# Line 506  Line 593 
593                  xvid_free(pEnc->temp_dquants);                  xvid_free(pEnc->temp_dquants);
594          }          }
595    
596            if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
597                    xvid_free(pEnc->temp_lambda);
598            }
599    
600    xvid_err_memory0:    xvid_err_memory0:
601          for (n=0; n<pEnc->num_plugins;n++) {          for (n=0; n<pEnc->num_plugins;n++) {
602                  if (pEnc->plugins[n].func) {                  if (pEnc->plugins[n].func) {
603                          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);
604                  }                  }
605          }          }
606          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
# Line 604  Line 695 
695                  xvid_free(pEnc->temp_dquants);                  xvid_free(pEnc->temp_dquants);
696          }          }
697    
698            if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
699                    xvid_free(pEnc->temp_lambda);
700            }
701    
702          if (pEnc->num_plugins>0) {          if (pEnc->num_plugins>0) {
703                  xvid_plg_destroy_t pdestroy;                  xvid_plg_destroy_t pdestroy;
# Line 614  Line 708 
708    
709                  for (i=0; i<pEnc->num_plugins;i++) {                  for (i=0; i<pEnc->num_plugins;i++) {
710                          if (pEnc->plugins[i].func) {                          if (pEnc->plugins[i].func) {
711                                  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);
712                          }                          }
713                  }                  }
714                  xvid_free(pEnc->plugins);                  xvid_free(pEnc->plugins);
# Line 622  Line 716 
716    
717          xvid_free(pEnc->mbParam.mpeg_quant_matrices);          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
718    
719          if (pEnc->num_plugins>0)          if (pEnc->num_zones > 0)
720                  xvid_free(pEnc->zones);                  xvid_free(pEnc->zones);
721    
722            if (pEnc->num_threads > 0) {
723                    for (i = 1; i < MAX(1, MIN(pEnc->num_threads, pEnc->num_slices)); i++) {
724                            xvid_free(pEnc->smpData[i].bs);
725                            xvid_free(pEnc->smpData[i].sStat);
726                    }
727                    if (pEnc->smpData[0].tmp_buffer) xvid_free(pEnc->smpData[0].tmp_buffer);
728    
729                    for (i = 0; i < pEnc->num_threads; i++)
730                            xvid_free(pEnc->smpData[i].complete_count_self);
731            }
732            xvid_free(pEnc->smpData);
733    
734          xvid_free(pEnc);          xvid_free(pEnc);
735    
736          return 0;  /* ok */          return 0;  /* ok */
# Line 638  Line 744 
744  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
745                                                   int opt, int * type, int * quant, xvid_enc_stats_t * stats)                                                   int opt, int * type, int * quant, xvid_enc_stats_t * stats)
746  {  {
747          unsigned int i, j;          unsigned int i, j, k;
748          xvid_plg_data_t data;          xvid_plg_data_t data;
749    
750          /* set data struct */          /* set data struct */
# Line 697  Line 803 
803                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
804                          data.dquant = pEnc->temp_dquants;                          data.dquant = pEnc->temp_dquants;
805                          data.dquant_stride = pEnc->mbParam.mb_width;                          data.dquant_stride = pEnc->mbParam.mb_width;
806                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
807                    }
808    
809                    if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
810                            int block = 0;
811                            emms();
812                            data.lambda = pEnc->temp_lambda;
813                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
814                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
815                                            for (k = 0; k < 6; k++)
816                                                    data.lambda[block++] = 1.0f;
817                  }                  }
818    
819          } else { /* XVID_PLG_AFTER */          } else { /* XVID_PLG_AFTER */
# Line 774  Line 890 
890          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
891                  emms();                  emms();
892                  if (pEnc->plugins[i].func) {                  if (pEnc->plugins[i].func) {
893                          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) {
894                                  continue;                                  continue;
895                          }                          }
896                  }                  }
# Line 803  Line 919 
919                                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
920                          }                          }
921                  }                  }
922    
923                    if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
924                            for (j = 0; j < pEnc->mbParam.mb_height; j++)
925                                    for (i = 0; i < pEnc->mbParam.mb_width; i++)
926                                            for (k = 0; k < 6; k++) {
927                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
928                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
929                                            }
930                    } else {
931                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
932                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
933                                            for (k = 0; k < 6; k++) {
934                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
935                                            }
936                    }
937    
938    
939                  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 */
940          }          }
941    
# Line 872  Line 1005 
1005  #endif  #endif
1006  }  }
1007    
 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;  
 }  
   
1008  static void  static void
1009  simplify_par(int *par_width, int *par_height)  simplify_par(int *par_width, int *par_height)
1010  {  {
# Line 920  Line 1035 
1035          return;          return;
1036  }  }
1037    
   
1038  /*****************************************************************************  /*****************************************************************************
1039   * IPB frame encoder entry point   * IPB frame encoder entry point
1040   *   *
# Line 1015  Line 1129 
1129                          }                          }
1130    
1131                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1132                          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);
1133                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1134    
1135                          goto done;                          goto done;
# Line 1047  Line 1161 
1161    
1162                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1163                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1164                          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);
1165    
1166                          /* flush complete: reset counters */                          /* flush complete: reset counters */
1167                          pEnc->flush_bframes = 0;                          pEnc->flush_bframes = 0;
# Line 1075  Line 1189 
1189                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1190    
1191                          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) {
1192                                  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);
1193                          }                          }
1194    
1195                          /* 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 1104  Line 1218 
1218    
1219    
1220                                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {                                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1221                                          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);
1222                                  }else{                                  }else{
1223                                          pEnc->flush_bframes = 1;                                          pEnc->flush_bframes = 1;
1224                                          goto done;                                          goto done;
# Line 1153  Line 1267 
1267          type = frame->type;          type = frame->type;
1268          pEnc->current->quant = frame->quant;          pEnc->current->quant = frame->quant;
1269    
1270          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);
1271    
1272          if (type > 0){  /* XVID_TYPE_?VOP */          if (type > 0){  /* XVID_TYPE_?VOP */
1273                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
# Line 1228  Line 1342 
1342          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)
1343          {          {
1344                  if (pEnc->current->stamp > 0) {                  if (pEnc->current->stamp > 0) {
1345                          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);
1346                  }                  }
1347                  else          else if (stats) {
1348                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
1349          }          }
1350            }
1351    
1352          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353           * closed-gop           * closed-gop
# Line 1256  Line 1371 
1371                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1372    
1373                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1374                          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, "CLOSED GOP BVOP->PVOP");
1375                  }                  }
1376    
1377                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
# Line 1351  Line 1466 
1466    
1467                  if ( FrameCodeP(pEnc, &bs) == 0 ) {                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1468                          /* N-VOP, we mustn't code b-frames yet */                          /* N-VOP, we mustn't code b-frames yet */
1469                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1470                                     pEnc->mbParam.max_bframes == 0)
1471                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1472                          goto done;                          goto done;
1473                  }                  }
1474          }          }
# Line 1372  Line 1489 
1489    
1490          /* packed or no-bframes or no-bframes-queued: output stats */          /* packed or no-bframes or no-bframes-queued: output stats */
1491          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 ) {
1492                  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);
1493          }          }
1494    
1495          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1412  Line 1529 
1529    
1530    
1531  static __inline void  static __inline void
1532  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(MACROBLOCK * pMB)
                         MACROBLOCK * pMB)  
1533  {  {
   
1534          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1535    
1536          /* zero mv statistics */          /* zero mv statistics */
# Line 1429  Line 1544 
1544          }          }
1545  }  }
1546    
1547    static void
1548    SliceCodeI(SMPData *data)
1549    {
1550            Encoder *pEnc = (Encoder *) data->pEnc;
1551            Bitstream *bs = (Bitstream *) data->bs;
1552    
1553            uint16_t x, y;
1554            int mb_width = pEnc->mbParam.mb_width;
1555            int mb_height = pEnc->mbParam.mb_height;
1556    
1557            int bound = 0, num_slices = pEnc->num_slices;
1558            FRAMEINFO *const current = pEnc->current;
1559    
1560            DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1561            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1562    
1563            if (data->start_y > 0) { /* write resync marker */
1564                    bound = data->start_y*mb_width;
1565                    write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1566            }
1567    
1568            for (y = data->start_y; y < data->stop_y; y++) {
1569                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1570    
1571                    if (new_bound > bound) {
1572                            bound = new_bound;
1573                            BitstreamPadAlways(bs);
1574                            write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1575                    }
1576    
1577                    for (x = 0; x < mb_width; x++) {
1578                            MACROBLOCK *pMB = &current->mbs[x + y * mb_width];
1579    
1580                            CodeIntraMB(pMB);
1581    
1582                            MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1583                                                              dct_codes, qcoeff);
1584    
1585                            start_timer();
1586                            MBPrediction(current, x, y, mb_width, qcoeff, bound);
1587                            stop_prediction_timer();
1588    
1589                            start_timer();
1590                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1591                            stop_coding_timer();
1592    
1593                    }
1594            }
1595    
1596            emms();
1597            BitstreamPadAlways(bs);
1598    }
1599    
1600    static __inline void
1601    SerializeBitstreams(Encoder *pEnc, FRAMEINFO *current, Bitstream *bs, int num_threads)
1602    {
1603            int k;
1604            uint32_t pos = BitstreamLength(bs);
1605    
1606            for (k = 1; k < num_threads; k++) {
1607                    uint32_t len = BitstreamLength(pEnc->smpData[k].bs);
1608    
1609                    memcpy((void *)((ptr_t)bs->start + pos),
1610                               (void *)((ptr_t)pEnc->smpData[k].bs->start), len);
1611    
1612                    current->length += len;
1613                    pos += len;
1614    
1615                    /* collect stats */
1616                    current->sStat.iTextBits += pEnc->smpData[k].sStat->iTextBits;
1617                    current->sStat.kblks += pEnc->smpData[k].sStat->kblks;
1618                    current->sStat.mblks += pEnc->smpData[k].sStat->mblks;
1619                    current->sStat.ublks += pEnc->smpData[k].sStat->ublks;
1620                    current->sStat.iMVBits += pEnc->smpData[k].sStat->iMVBits;
1621            }
1622    
1623            if (num_threads > 1) {
1624                    uint32_t pos32 = pos>>2;
1625                    bs->tail = bs->start + pos32;
1626                    bs->pos = 8*(pos - (pos32<<2));
1627                    bs->buf = 0;
1628    
1629                    if (bs->pos > 0) {
1630                            uint32_t pos8 = bs->pos/8;
1631                            memset((void *)((ptr_t)bs->tail+pos8), 0, (4-pos8));
1632                            pos = *bs->tail;
1633    #ifndef ARCH_IS_BIG_ENDIAN
1634                            BSWAP(pos);
1635    #endif
1636                            bs->buf = pos;
1637                    }
1638            }
1639    }
1640    
1641  static int  static int
1642  FrameCodeI(Encoder * pEnc,  FrameCodeI(Encoder * pEnc,
1643                     Bitstream * bs)                     Bitstream * bs)
1644  {  {
1645          int bits = BitstreamPos(bs);          int bits = BitstreamPos(bs);
1646          int mb_width = pEnc->mbParam.mb_width;          int bound = 0, num_slices = pEnc->num_slices;
1647            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1648            int slices_per_thread = (num_slices*1024 / num_threads);
1649          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1650            void * status = NULL;
1651          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          uint16_t k;
         DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);  
   
         uint16_t x, y;  
1652    
1653          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1654          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1452  Line 1658 
1658    
1659          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1660    
1661          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current, num_slices);
1662    
1663          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1664    
# Line 1461  Line 1667 
1667          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1668    
1669          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
         pEnc->current->sStat.kblks = mb_width * mb_height;  
         pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;  
1670    
1671          for (y = 0; y < mb_height; y++)          /* multithreaded intra coding - dispatch threads */
1672                  for (x = 0; x < mb_width; x++) {          for (k = 0; k < num_threads; k++) {
1673                          MACROBLOCK *pMB =                  int add = ((slices_per_thread + 512) >> 10);
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1674    
1675                          CodeIntraMB(pEnc, pMB);                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
1676    
1677                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                  pEnc->smpData[k].pEnc = (void *) pEnc;
1678                                                            dct_codes, qcoeff);                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
1679                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
1680    
1681                          start_timer();                  bound += add;
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1682    
1683                          start_timer();                  if (k > 0) {
1684                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          BitstreamReset(pEnc->smpData[k].bs);
1685                          stop_coding_timer();                          pEnc->smpData[k].sStat->iTextBits = 0;
1686                  }                  }
1687            }
1688            pEnc->smpData[0].bs = bs;
1689            pEnc->smpData[0].sStat = &pEnc->current->sStat;
1690    
1691          emms();          /* create threads */
1692            for (k = 1; k < num_threads; k++) {
1693                    pthread_create(&pEnc->smpData[k].handle, NULL,
1694                                   (void*)SliceCodeI, (void*)&pEnc->smpData[k]);
1695            }
1696    
1697          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          SliceCodeI(&pEnc->smpData[0]);
1698    
1699          pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          /* wait until all threads are finished */
1700            for (k = 1; k < num_threads; k++) {
1701                    pthread_join(pEnc->smpData[k].handle, &status);
1702            }
1703    
1704            pEnc->current->length = BitstreamLength(bs) - (bits/8);
1705    
1706            /* reassemble the pieces together */
1707            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
1708    
1709            pEnc->current->sStat.iMVBits = 0;
1710            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1711            pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1712    
1713          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1714          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
# Line 1524  Line 1745 
1745    
1746  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1747    
1748  /* FrameCodeP also handles S(GMC)-VOPs */  static void
1749  static int  SliceCodeP(SMPData *data)
 FrameCodeP(Encoder * pEnc,  
                    Bitstream * bs)  
1750  {  {
1751          int bits = BitstreamPos(bs);          Encoder *pEnc = (Encoder *) data->pEnc;
1752            Bitstream *bs = (Bitstream *) data->bs;
1753    
1754            int x, y, k;
1755            FRAMEINFO *const current = pEnc->current;
1756            FRAMEINFO *const reference = pEnc->reference;
1757            MBParam * const pParam = &pEnc->mbParam;
1758            int mb_width = pParam->mb_width;
1759            int mb_height = pParam->mb_height;
1760    
1761          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1762          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1763    
1764          int x, y, k;          int bound = 0, num_slices = pEnc->num_slices;
1765    
1766            if (data->start_y > 0) { /* write resync marker */
1767                    bound = data->start_y*mb_width;
1768                    write_video_packet_header(bs, pParam, current, bound);
1769            }
1770    
1771            for (y = data->start_y; y < data->stop_y; y++) {
1772                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1773    
1774                    if (new_bound > bound) {
1775                            bound = new_bound;
1776                            BitstreamPadAlways(bs);
1777                            write_video_packet_header(bs, pParam, current, bound);
1778                    }
1779    
1780                    for (x = 0; x < mb_width; x++) {
1781                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1782                            int skip_possible;
1783    
1784                            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1785                                    CodeIntraMB(pMB);
1786                                    MBTransQuantIntra(pParam, current, pMB, x, y,
1787                                                                      dct_codes, qcoeff);
1788    
1789                                    start_timer();
1790                                    MBPrediction(current, x, y, pParam->mb_width, qcoeff, bound);
1791                                    stop_prediction_timer();
1792    
1793                                    data->sStat->kblks++;
1794    
1795                                    MBCoding(current, pMB, qcoeff, bs, data->sStat);
1796                                    stop_coding_timer();
1797                                    continue;
1798                            }
1799    
1800                            start_timer();
1801                            MBMotionCompensation(pMB, x, y, &reference->image,
1802                                                                     &pEnc->vInterH, &pEnc->vInterV,
1803                                                                     &pEnc->vInterHV, &pEnc->vGMC,
1804                                                                     &current->image,
1805                                                                     dct_codes, pParam->width,
1806                                                                     pParam->height,
1807                                                                     pParam->edged_width,
1808                                                                     (current->vol_flags & XVID_VOL_QUARTERPEL),
1809                                                                     current->rounding_type,
1810                                                                     data->RefQ);
1811    
1812                            stop_comp_timer();
1813    
1814                            pMB->field_pred = 0;
1815    
1816                            if (pMB->cbp != 0) {
1817                                    pMB->cbp = MBTransQuantInter(pParam, current, pMB, x, y,
1818                                                                 dct_codes, qcoeff);
1819                            }
1820    
1821                            if (pMB->dquant != 0)
1822                                    MBSetDquant(pMB, x, y, pParam);
1823    
1824    
1825                            if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1826                                       pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1827                                       pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1828                                    data->sStat->mblks++;
1829                            }  else {
1830                                    data->sStat->ublks++;
1831                            }
1832    
1833                            start_timer();
1834    
1835                            /* Finished processing the MB, now check if to CODE or SKIP */
1836    
1837                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
1838    
1839                            if (current->coding_type == S_VOP)
1840                                    skip_possible &= (pMB->mcsel == 1);
1841                            else { /* PVOP */
1842                                    const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1843                                                                                    pMB->qmvs : pMB->mvs;
1844                                    skip_possible &= ((mv->x|mv->y) == 0);
1845                            }
1846    
1847                            if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1848                                    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1849                                    int bSkip = 1;
1850    
1851                                    if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1852                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1853                                                    int iSAD;
1854                                                    iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1855                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1856                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1857                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) &&
1858                                                            ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after
1859                                                                                                                                                                               resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */
1860                                                            bSkip = 0; /* could not SKIP */
1861                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1862                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1863                                                                    pMB->pmvs[0].x = - predMV.x;
1864                                                                    pMB->pmvs[0].y = - predMV.y;
1865                                                            } else {
1866                                                                    VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1867                                                                    pMB->pmvs[0].x = - predMV.x;
1868                                                                    pMB->pmvs[0].y = - predMV.y;
1869                                                            }
1870                                                            pMB->mode = MODE_INTER;
1871                                                            pMB->cbp = 0;
1872                                                            break;
1873                                                    }
1874                                            }
1875                                    }
1876    
1877                                    if (bSkip) {
1878                                            /* do SKIP */
1879                                            pMB->mode = MODE_NOT_CODED;
1880                                            MBSkip(bs);
1881                                            stop_coding_timer();
1882                                            continue;       /* next MB */
1883                                    }
1884                            }
1885    
1886                            /* ordinary case: normal coded INTER/INTER4V block */
1887                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1888                            stop_coding_timer();
1889                    }
1890            }
1891    
1892            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1893            emms();
1894    }
1895    
1896    /* FrameCodeP also handles S(GMC)-VOPs */
1897    static int
1898    FrameCodeP(Encoder * pEnc, Bitstream * bs)
1899    {
1900            int bits = BitstreamPos(bs);
1901    
1902          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1903          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1904          MBParam * const pParam = &pEnc->mbParam;          MBParam * const pParam = &pEnc->mbParam;
# Line 1542  Line 1906 
1906          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1907          int coded = 1;          int coded = 1;
1908    
1909            int k = 0, bound = 0, num_slices = pEnc->num_slices;
1910            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1911            void * status = NULL;
1912            int slices_per_thread = (num_slices*1024 / num_threads);
1913            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
1914    
1915          IMAGE *pRef = &reference->image;          IMAGE *pRef = &reference->image;
1916    
1917          if (!reference->is_edged) {          if (!reference->is_edged) {
1918                  start_timer();                  start_timer();
1919                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1920                                             pParam->width, pParam->height, 0);                                             pParam->width, pParam->height, XVID_BS_VERSION);
1921                  stop_edges_timer();                  stop_edges_timer();
1922                  reference->is_edged = 1;                  reference->is_edged = 1;
1923          }          }
# Line 1559  Line 1929 
1929          if ((current->vop_flags & XVID_VOP_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1930                  if (reference->is_interpolated != current->rounding_type) {                  if (reference->is_interpolated != current->rounding_type) {
1931                          start_timer();                          start_timer();
1932                          image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1933                                                            &pEnc->vInterHV, pParam->edged_width,                                                            pEnc->vInterHV.y, pParam->edged_width,
1934                                                            pParam->edged_height,                                                            pParam->edged_height,
1935                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1936                                                            current->rounding_type);                                                            current->rounding_type);
# Line 1570  Line 1940 
1940          }          }
1941    
1942          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1943                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1944                    current->sStat.iMVBits = 0;
1945    
1946          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1947    
1948            if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1949                    image_block_variance(&current->image, pParam->edged_width, current->mbs,
1950                                         pParam->mb_width, pParam->mb_height);
1951            }
1952    
1953          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1954    
1955          SetMacroblockQuants(&pEnc->mbParam, current);          SetMacroblockQuants(&pEnc->mbParam, current);
# Line 1582  Line 1958 
1958          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1959          {       int gmcval;          {       int gmcval;
1960                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1961                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, num_slices);
1962    
1963                  if (current->motion_flags & XVID_ME_GME_REFINE) {                  if (current->motion_flags & XVID_ME_GME_REFINE) {
1964                          gmcval = GlobalMotionEstRefine(&current->warp,                          gmcval = GlobalMotionEstRefine(&current->warp,
# Line 1629  Line 2005 
2005                  }                  }
2006          }          }
2007    
2008          MotionEstimation(&pEnc->mbParam, current, reference,          if (pEnc->num_threads > 0) {
                                          &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                          &pEnc->vGMC, 256*4096);  
   
2009    
2010          stop_motion_timer();                  /* multithreaded motion estimation - dispatch threads */
2011                    while (k < pEnc->num_threads) {
2012                            int i, add_s = (slices_per_thread + 512) >> 10;
2013                            int add_t = (threads_per_slice + 512) >> 10;
2014    
2015          set_timecodes(current,reference,pParam->fbase);                          int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2016                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2017                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2018    
2019          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);                          slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2020                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2021    
2022          for (y = 0; y < mb_height; y++) {                          for (i = 0; i < add_t; i++) {
2023                  for (x = 0; x < mb_width; x++) {                                  memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
                         MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];  
                         int skip_possible;  
2024    
2025                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {                                  pEnc->smpData[k+i].pEnc = (void *) pEnc;
2026                                  CodeIntraMB(pEnc, pMB);                                  pEnc->smpData[k+i].y_row = i;
2027                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,                                  pEnc->smpData[k+i].y_step = add_t;
2028                                                                    dct_codes, qcoeff);                                  pEnc->smpData[k+i].stop_y = stop_y;
2029                                    pEnc->smpData[k+i].start_y = start_y;
2030    
2031                                  start_timer();                                  /* todo: sort out temp space once and for all */
2032                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);                                  pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2033                                  stop_prediction_timer();                                                                                          16*((k+i)>>1)*pParam->edged_width;
2034                            }
2035    
2036                                  current->sStat.kblks++;                          pEnc->smpData[k].complete_count_above =
2037                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2038    
2039                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                          bound += add_s;
2040                                  stop_coding_timer();                          k += add_t;
                                 continue;  
2041                          }                          }
2042    
2043                          start_timer();                  for (k = 1; k < pEnc->num_threads; k++) {
2044                          MBMotionCompensation(pMB, x, y, &reference->image,                          pthread_create(&pEnc->smpData[k].handle, NULL,
2045                                                                   &pEnc->vInterH, &pEnc->vInterV,                                  (void*)MotionEstimateSMP, (void*)&pEnc->smpData[k]);
2046                                                                   &pEnc->vInterHV, &pEnc->vGMC,                  }
                                                                  &current->image,  
                                                                  dct_codes, pParam->width,  
                                                                  pParam->height,  
                                                                  pParam->edged_width,  
                                                                  (current->vol_flags & XVID_VOL_QUARTERPEL),  
                                                                  current->rounding_type);  
2047    
2048                          stop_comp_timer();                  MotionEstimateSMP(&pEnc->smpData[0]);
2049    
2050                          pMB->field_pred = 0;                  for (k = 1; k < pEnc->num_threads; k++) {
2051                            pthread_join(pEnc->smpData[k].handle, &status);
2052                    }
2053    
2054                          if (pMB->cbp != 0) {                  current->fcode = 0;
2055                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,                  for (k = 0; k < pEnc->num_threads; k++) {
2056                                                                            dct_codes, qcoeff);                          current->sStat.iMvSum += pEnc->smpData[k].mvSum;
2057                            current->sStat.iMvCount += pEnc->smpData[k].mvCount;
2058                            if (pEnc->smpData[k].minfcode > current->fcode)
2059                                    current->fcode = pEnc->smpData[k].minfcode;
2060                          }                          }
2061    
2062                          if (pMB->dquant != 0)          } else {
                                 MBSetDquant(pMB, x, y, &pEnc->mbParam);  
2063    
2064                    /* regular ME */
2065    
2066                    MotionEstimation(&pEnc->mbParam, current, reference,
2067                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2068                                                     &pEnc->vGMC, 256*4096, num_slices);
2069    
                         if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||  
                                    pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||  
                                    pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {  
                                 current->sStat.mblks++;  
                         }  else {  
                                 current->sStat.ublks++;  
2070                          }                          }
2071    
2072                          start_timer();          stop_motion_timer();
2073    
2074                          /* Finished processing the MB, now check if to CODE or SKIP */          set_timecodes(current,reference,pParam->fbase);
2075    
2076                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
2077    
2078                          if (current->coding_type == S_VOP)          /* multithreaded inter coding - dispatch threads */
                                 skip_possible &= (pMB->mcsel == 1);  
                         else { /* PVOP */  
                                 const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?  
                                                                                 pMB->qmvs : pMB->mvs;  
                                 skip_possible &= ((mv->x|mv->y) == 0);  
                         }  
2079    
2080                          if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {          bound = 0;
2081                                  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */          slices_per_thread = (num_slices*1024 / num_threads);
                                 int bSkip = 1;  
2082    
2083                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */          for (k = 0; k < num_threads; k++) {
2084                    int add = ((slices_per_thread + 512) >> 10);
2085    
2086                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
                                                 int iSAD;  
                                                 iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,  
                                                                                 pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,  
                                                                                 pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);  
                                                 if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {  
                                                         bSkip = 0; /* could not SKIP */  
                                                         if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {  
                                                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                                                 pMB->pmvs[0].x = - predMV.x;  
                                                                 pMB->pmvs[0].y = - predMV.y;  
                                                         } else {  
                                                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                                                 pMB->pmvs[0].x = - predMV.x;  
                                                                 pMB->pmvs[0].y = - predMV.y;  
                                                         }  
                                                         pMB->mode = MODE_INTER;  
                                                         pMB->cbp = 0;  
                                                         break;  
                                                 }  
                                         }  
                                 }  
2087    
2088                                  if (bSkip) {                  pEnc->smpData[k].pEnc = (void *) pEnc;
2089                                          /* do SKIP */                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2090                                          pMB->mode = MODE_NOT_CODED;                  pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2091                                          MBSkip(bs);                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2092                                          stop_coding_timer();  
2093                                          continue;       /* next MB */                  bound += add;
2094    
2095                    if (k > 0) {
2096                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2097                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks =
2098                            pEnc->smpData[k].sStat->iMVBits = 0;
2099    
2100                            BitstreamReset(pEnc->smpData[k].bs);
2101                                  }                                  }
2102                          }                          }
2103            pEnc->smpData[0].bs = bs;
2104            pEnc->smpData[0].sStat = &current->sStat;
2105    
2106                          /* ordinary case: normal coded INTER/INTER4V block */          /* create threads */
2107                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);          for (k = 1; k < num_threads; k++) {
2108                          stop_coding_timer();                  pthread_create(&pEnc->smpData[k].handle, NULL,
2109                            (void*)SliceCodeP, (void*)&pEnc->smpData[k]);
2110                  }                  }
2111    
2112            SliceCodeP(&pEnc->smpData[0]);
2113    
2114            /* wait until all threads are finished */
2115            for (k = 1; k < num_threads; k++) {
2116                    pthread_join(pEnc->smpData[k].handle, &status);
2117          }          }
2118    
2119          emms();          current->length = BitstreamLength(bs) - (bits/8);
2120    
2121            /* reassemble the pieces together */
2122            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
2123    
2124          updateFcode(&current->sStat, pEnc);          updateFcode(&current->sStat, pEnc);
2125    
2126          /* frame drop code */          /* frame drop code */
2127  #if 0  #if 0
2128          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
2129  #endif  #endif
2130          if (current->sStat.kblks + current->sStat.mblks <=  
2131            if (current->sStat.kblks + current->sStat.mblks <
2132                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
2133                  ( (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)) &&
2134                    (current->coding_type == P_VOP) )
2135          {          {
2136                  current->sStat.kblks = current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
2137                  current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
2138    
2139                  BitstreamReset(bs);                  BitstreamReset(bs);
# Line 1781  Line 2152 
2152                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2153                  coded = 0;                  coded = 0;
2154    
2155                    BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2156    
2157                    current->length = (BitstreamPos(bs) - bits) / 8;
2158    
2159          } else {          } else {
2160    
2161                  pEnc->current->is_edged = 0; /* not edged */                  pEnc->current->is_edged = 0; /* not edged */
# Line 1809  Line 2184 
2184          }          }
2185          */          */
2186    
2187          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          return coded;
2188    }
2189    
2190          current->length = (BitstreamPos(bs) - bits) / 8;  static void
2191    SliceCodeB(SMPData *data)
2192    {
2193            Encoder *pEnc = (Encoder *) data->pEnc;
2194            Bitstream *bs = (Bitstream *) data->bs;
2195    
2196          return coded;          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2197            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2198    
2199            int x, y;
2200            FRAMEINFO * const frame = (FRAMEINFO * const) data->current;
2201            MBParam * const pParam = &pEnc->mbParam;
2202            int mb_width = pParam->mb_width;
2203            int mb_height = pParam->mb_height;
2204            IMAGE *f_ref = &pEnc->reference->image;
2205            IMAGE *b_ref = &pEnc->current->image;
2206    
2207            int bound = data->start_y*mb_width;
2208            int num_slices = pEnc->num_slices;
2209    
2210            if (data->start_y > 0) { /* write resync marker */
2211                    write_video_packet_header(bs, pParam, frame, bound+1);
2212            }
2213    
2214            for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) {
2215                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
2216                    int stop_x = (y == data->stop_y) ? 1 : mb_width;
2217                    int start_x = (y == data->start_y && y > 0) ? 1 : 0;
2218    
2219                    for (x = start_x; x < stop_x; x++) {
2220                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2221    
2222                            /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2223                            if (mb->mode == MODE_NOT_CODED) {
2224                                    if (pParam->plugin_flags & XVID_REQORIGINAL) {
2225                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2226                                                                                     NULL, 0, 0, pParam->edged_width, 0, 0, data->RefQ);
2227                                    }
2228                                    continue;
2229  }  }
2230    
2231                            if (new_bound > bound && x > 0) {
2232                                    bound = new_bound;
2233                                    BitstreamPadAlways(bs);
2234                                    write_video_packet_header(bs, pParam, frame, y*mb_width+x);
2235                            }
2236    
2237                            mb->quant = frame->quant;
2238    
2239                            if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) {
2240                                    /* we have to motion-compensate, transfer etc,
2241                                            because there might be blocks to code */
2242    
2243                                    MBMotionCompensationBVOP(pParam, mb, x, y, &frame->image,
2244                                                                                     f_ref, &pEnc->f_refh, &pEnc->f_refv,
2245                                                                                     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2246                                                                                     &pEnc->vInterV, &pEnc->vInterHV, dct_codes,
2247                                                                                     data->RefQ);
2248    
2249                                    mb->cbp = MBTransQuantInterBVOP(pParam, frame, mb, x, y,  dct_codes, qcoeff);
2250                            }
2251    
2252                            if (mb->mode == MODE_DIRECT_NO4V)
2253                                    mb->mode = MODE_DIRECT;
2254    
2255                            if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2256                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2257                            else
2258                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2259                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2260                                            mb->cbp &= 0x3C;
2261    
2262                            start_timer();
2263                            MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, data->sStat);
2264                            stop_coding_timer();
2265                    }
2266            }
2267    
2268            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2269            emms();
2270    }
2271    
2272  static void  static void
2273  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
# Line 1823  Line 2275 
2275                     Bitstream * bs)                     Bitstream * bs)
2276  {  {
2277          int bits = BitstreamPos(bs);          int bits = BitstreamPos(bs);
2278          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          int k = 0, bound = 0, num_slices = pEnc->num_slices;
2279          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
2280          uint32_t x, y;          void * status = NULL;
2281            int slices_per_thread = (num_slices*1024 / num_threads);
2282            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
2283    
2284          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2285          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2286    
2287            MBParam * const pParam = &pEnc->mbParam;
2288            int mb_height = pParam->mb_height;
2289    
2290          #ifdef BFRAMES_DEC_DEBUG          #ifdef BFRAMES_DEC_DEBUG
2291          FILE *fp;          FILE *fp;
2292          static char first=0;          static char first=0;
# Line 1846  Line 2303 
2303          if (!pEnc->reference->is_edged) {          if (!pEnc->reference->is_edged) {
2304                  image_setedges(f_ref, pEnc->mbParam.edged_width,                  image_setedges(f_ref, pEnc->mbParam.edged_width,
2305                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,
2306                                             pEnc->mbParam.height, 0);                                             pEnc->mbParam.height, XVID_BS_VERSION);
2307                  pEnc->current->is_edged = 1;                  pEnc->reference->is_edged = 1;
2308          }          }
2309    
2310          if (pEnc->reference->is_interpolated != 0) {          if (pEnc->reference->is_interpolated != 0) {
2311                  start_timer();                  start_timer();
2312                  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,
2313                                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2314                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2315                  stop_inter_timer();                  stop_inter_timer();
# Line 1863  Line 2320 
2320          if (!pEnc->current->is_edged) {          if (!pEnc->current->is_edged) {
2321                  image_setedges(b_ref, pEnc->mbParam.edged_width,                  image_setedges(b_ref, pEnc->mbParam.edged_width,
2322                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,
2323                                             pEnc->mbParam.height, 0);                                             pEnc->mbParam.height, XVID_BS_VERSION);
2324                  pEnc->current->is_edged = 1;                  pEnc->current->is_edged = 1;
2325          }          }
2326    
2327          if (pEnc->current->is_interpolated != 0) {          if (pEnc->current->is_interpolated != 0) {
2328                  start_timer();                  start_timer();
2329                  image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
2330                                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2331                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2332                  stop_inter_timer();                  stop_inter_timer();
# Line 1877  Line 2334 
2334          }          }
2335    
2336          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2337          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);  
2338            if ((frame->vop_flags & XVID_VOP_RD_PSNRHVSM) && (frame->vop_flags & XVID_VOP_RD_BVOP)) {
2339                    image_block_variance(&frame->image, pEnc->mbParam.edged_width, frame->mbs,
2340                                         pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2341            }
2342    
2343            call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2344    
2345            frame->fcode = frame->bcode = pEnc->current->fcode;
2346    
2347          start_timer();          start_timer();
2348    
2349            if (pEnc->num_threads > 0) {
2350    
2351                    /* multithreaded motion estimation - dispatch threads */
2352                    while (k < pEnc->num_threads) {
2353                            int i, add_s = (slices_per_thread + 512) >> 10;
2354                            int add_t = (threads_per_slice + 512) >> 10;
2355    
2356                            int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2357                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2358                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2359    
2360                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2361                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2362    
2363                            for (i = 0; i < add_t; i++) {
2364                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2365    
2366                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2367                                    pEnc->smpData[k+i].current = frame;
2368    
2369                                    pEnc->smpData[k+i].y_row = i;
2370                                    pEnc->smpData[k+i].y_step = add_t;
2371                                    pEnc->smpData[k+i].stop_y = stop_y;
2372                                    pEnc->smpData[k+i].start_y = start_y;
2373    
2374                                    /* todo: sort out temp space once and for all */
2375                                    pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2376                                                                                            16*((k+i)>>1)*pParam->edged_width;
2377                            }
2378    
2379                            pEnc->smpData[k].complete_count_above =
2380                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2381    
2382                            bound += add_s;
2383                            k += add_t;
2384                    }
2385    
2386                    for (k = 1; k < pEnc->num_threads; k++) {
2387                            pthread_create(&pEnc->smpData[k].handle, NULL,
2388                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->smpData[k]);
2389                    }
2390    
2391                    SMPMotionEstimationBVOP(&pEnc->smpData[0]);
2392    
2393                    for (k = 1; k < pEnc->num_threads; k++) {
2394                            pthread_join(pEnc->smpData[k].handle, &status);
2395                    }
2396    
2397                    frame->fcode = frame->bcode = 0;
2398                    for (k = 0; k < pEnc->num_threads; k++) {
2399                            if (pEnc->smpData[k].minfcode > frame->fcode)
2400                                    frame->fcode = pEnc->smpData[k].minfcode;
2401                            if (pEnc->smpData[k].minbcode > frame->bcode)
2402                                    frame->bcode = pEnc->smpData[k].minbcode;
2403                    }
2404            } else {
2405    
2406          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2407                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2408                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2409                                                   pEnc->reference->mbs, f_ref,                                                   pEnc->reference->mbs, f_ref,
2410                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2411                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2412                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                           &pEnc->vInterV, &pEnc->vInterHV,
2413                                                             pEnc->num_slices);
2414            }
2415          stop_motion_timer();          stop_motion_timer();
2416    
2417          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2418          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2419    
2420            /* reset stats */
2421          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2422            frame->sStat.iMVBits = 0;
2423          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2424          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2425          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2426          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2427          frame->sStat.kblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.ublks = 0;
2428    
2429          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          /* multithreaded inter coding - dispatch threads */
2430                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          bound = 0;
2431                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];          slices_per_thread = (num_slices*1024 / num_threads);
2432    
2433                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */          for (k = 0; k < num_threads; k++) {
2434                          if (mb->mode == MODE_NOT_CODED) {                  int add = ((slices_per_thread + 512) >> 10);
                                 if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {  
                                         MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,  
                                                                                         NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);  
                                 }  
                                 continue;  
                         }  
2435    
2436                          mb->quant = frame->quant;                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2437    
2438                          if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                  pEnc->smpData[k].pEnc = (void *) pEnc;
2439                                  /* we have to motion-compensate, transfer etc,                  pEnc->smpData[k].current = frame;
2440                                          because there might be blocks to code */                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2441                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2442                    bound += add;
2443    
2444                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                  /* todo: sort out temp space once and for all */
2445                                                                                   f_ref, &pEnc->f_refh, &pEnc->f_refv,                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
                                                                                  &pEnc->f_refhv, b_ref, &pEnc->vInterH,  
                                                                                  &pEnc->vInterV, &pEnc->vInterHV,  
                                                                                  dct_codes);  
2446    
2447                                  mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                  if (k > 0) {
2448                            BitstreamReset(pEnc->smpData[k].bs);
2449                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2450                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks = pEnc->smpData[k].sStat->iMVBits = 0;
2451                    }
2452                          }                          }
2453    
2454                          if (mb->mode == MODE_DIRECT_NO4V)          for (k = 1; k < num_threads; k++) {
2455                                  mb->mode = MODE_DIRECT;                  pthread_create(&pEnc->smpData[k].handle, NULL,
2456                            (void*)SliceCodeB, (void*)&pEnc->smpData[k]);
2457            }
2458    
2459                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)          pEnc->smpData[0].bs = bs;
2460                                  mb->mode = MODE_DIRECT_NONE_MV; /* skipped */          pEnc->smpData[0].sStat = &frame->sStat;
2461                          else          SliceCodeB(&pEnc->smpData[0]);
                                 if (frame->vop_flags & XVID_VOP_GREYSCALE)  
                                         /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */  
                                         mb->cbp &= 0x3C;  
2462    
2463                          start_timer();          for (k = 1; k < num_threads; k++) {
2464                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,                  pthread_join(pEnc->smpData[k].handle, &status);
                                                  &frame->sStat);  
                         stop_coding_timer();  
                 }  
2465          }          }
2466    
2467          emms();          frame->length = BitstreamLength(bs) - (bits/8);
2468    
2469          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          /* reassemble the pieces together */
2470          frame->length = (BitstreamPos(bs) - bits) / 8;          SerializeBitstreams(pEnc, frame, bs, num_threads);
2471    
2472  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2473          if (!first){          if (!first){

Legend:
Removed from v.1.116  
changed lines
  Added in v.1.135.2.5

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