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

Diff of /xvidcore/src/encoder.c

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

revision 1.95.2.66, Tue Mar 30 12:30:48 2004 UTC revision 1.141, Wed Apr 6 14:30:14 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 57  Line 60 
60                                            Bitstream * bs);                                            Bitstream * bs);
61    
62  static int FrameCodeP(Encoder * pEnc,  static int FrameCodeP(Encoder * pEnc,
63                                            Bitstream * bs,                                            Bitstream * bs);
                                           bool force_inter,  
                                           bool vol_header);  
64    
65  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
66                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
# Line 87  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;
116                          *inc /= i;  
117                          *base /= i;          if (*base > 65535 || *inc > 65535) {
118                          i = *inc;                  int *biggest;
119                          continue;                  int *other;
120                  }                  float div;
121                  i--;  
122                    if (*base > *inc) {
123                            biggest = base;
124                            other = inc;
125                    } else {
126                            biggest = inc;
127                            other = base;
128          }          }
129    
130          /* if neccessary, round to 65535 accuracy */                  div = ((float)*biggest)/((float)65535);
131          if (*base > 65535) {                  *biggest = (unsigned int)(((float)*biggest)/div);
132                  float div = (float) *base / 65535;                  *other = (unsigned int)(((float)*other)/div);
                 *base = (int) (*base / div);  
                 *inc = (int) (*inc / div);  
133          }          }
134  }  }
135    
# Line 137  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 150  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 181  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 198  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 216  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 402  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 411  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    #ifndef HAVE_PTHREAD
453                    int t = MAX(1, create->num_threads);
454    #else
455                    int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */
456    #endif
457                    int threads_per_slice = MAX(1, (t / pEnc->num_slices));
458                    int rows_per_thread = (pEnc->mbParam.mb_height + threads_per_slice - 1) / threads_per_slice;
459    
460                    pEnc->num_threads = t;
461                    pEnc->smpData = xvid_malloc(t*sizeof(SMPData), CACHE_LINE);
462                    if (!pEnc->smpData)
463                            goto xvid_err_nosmp;
464    
465                    /* tmp bitstream buffer for slice coding */
466                    pEnc->smpData[0].tmp_buffer = xvid_malloc(16*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height*sizeof(uint8_t), CACHE_LINE);
467                    if (! pEnc->smpData[0].tmp_buffer) goto xvid_err_nosmp;
468    
469                    for (n = 0; n < t; n++) {
470                            int s = MIN(pEnc->num_threads, pEnc->num_slices);
471    
472                            pEnc->smpData[n].complete_count_self =
473                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
474    
475                            if (!pEnc->smpData[n].complete_count_self)
476                                    goto xvid_err_nosmp;
477    
478                            if (n > 0 && n < s) {
479                                    pEnc->smpData[n].bs = (Bitstream *) xvid_malloc(sizeof(Bitstream), CACHE_LINE);
480                                    if (!pEnc->smpData[n].bs)
481                                            goto xvid_err_nosmp;
482    
483                                    pEnc->smpData[n].sStat = (Statistics *) xvid_malloc(sizeof(Statistics), CACHE_LINE);
484                                    if (!pEnc->smpData[n].sStat)
485                                            goto xvid_err_nosmp;
486    
487                                    pEnc->smpData[n].tmp_buffer = pEnc->smpData[0].tmp_buffer + 16*(((n-1)*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height)/s);
488                                    BitstreamInit(pEnc->smpData[n].bs, pEnc->smpData[n].tmp_buffer, 0);
489                            }
490    
491                            if (n != 0)
492                                    pEnc->smpData[n].complete_count_above =
493                                            pEnc->smpData[n-1].complete_count_self;
494                    }
495                    pEnc->smpData[0].complete_count_above =
496                            pEnc->smpData[t-1].complete_count_self - 1;
497    
498            } else {
499      xvid_err_nosmp:
500                    /* no SMP */
501                    if (pEnc->smpData) {
502                            if (pEnc->smpData[0].tmp_buffer)
503                                    xvid_free(pEnc->smpData[0].tmp_buffer);
504                    }
505                    else {
506                            pEnc->smpData = xvid_malloc(1*sizeof(SMPData), CACHE_LINE);
507                            if (pEnc->smpData == NULL)
508                                    goto xvid_err_memory5;
509                    }
510    
511                    create->num_threads = 0;
512            }
513    
514          create->handle = (void *) pEnc;          create->handle = (void *) pEnc;
515    
516          init_timer();          init_timer();
# Line 497  Line 597 
597                  xvid_free(pEnc->temp_dquants);                  xvid_free(pEnc->temp_dquants);
598          }          }
599    
600            if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
601                    xvid_free(pEnc->temp_lambda);
602            }
603    
604    xvid_err_memory0:    xvid_err_memory0:
605          for (n=0; n<pEnc->num_plugins;n++) {          for (n=0; n<pEnc->num_plugins;n++) {
606                  if (pEnc->plugins[n].func) {                  if (pEnc->plugins[n].func) {
607                          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);
608                  }                  }
609          }          }
610          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
# Line 595  Line 699 
699                  xvid_free(pEnc->temp_dquants);                  xvid_free(pEnc->temp_dquants);
700          }          }
701    
702            if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
703                    xvid_free(pEnc->temp_lambda);
704            }
705    
706          if (pEnc->num_plugins>0) {          if (pEnc->num_plugins>0) {
707                  xvid_plg_destroy_t pdestroy;                  xvid_plg_destroy_t pdestroy;
# Line 605  Line 712 
712    
713                  for (i=0; i<pEnc->num_plugins;i++) {                  for (i=0; i<pEnc->num_plugins;i++) {
714                          if (pEnc->plugins[i].func) {                          if (pEnc->plugins[i].func) {
715                                  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);
716                          }                          }
717                  }                  }
718                  xvid_free(pEnc->plugins);                  xvid_free(pEnc->plugins);
# Line 613  Line 720 
720    
721          xvid_free(pEnc->mbParam.mpeg_quant_matrices);          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
722    
723          if (pEnc->num_plugins>0)          if (pEnc->num_zones > 0)
724                  xvid_free(pEnc->zones);                  xvid_free(pEnc->zones);
725    
726            if (pEnc->num_threads > 0) {
727                    for (i = 1; i < MAX(1, MIN(pEnc->num_threads, pEnc->num_slices)); i++) {
728                            xvid_free(pEnc->smpData[i].bs);
729                            xvid_free(pEnc->smpData[i].sStat);
730                    }
731                    if (pEnc->smpData[0].tmp_buffer) xvid_free(pEnc->smpData[0].tmp_buffer);
732    
733                    for (i = 0; i < pEnc->num_threads; i++)
734                            xvid_free(pEnc->smpData[i].complete_count_self);
735            }
736            xvid_free(pEnc->smpData);
737    
738          xvid_free(pEnc);          xvid_free(pEnc);
739    
740          return 0;  /* ok */          return 0;  /* ok */
# Line 629  Line 748 
748  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
749                                                   int opt, int * type, int * quant, xvid_enc_stats_t * stats)                                                   int opt, int * type, int * quant, xvid_enc_stats_t * stats)
750  {  {
751          unsigned int i, j;          unsigned int i, j, k;
752          xvid_plg_data_t data;          xvid_plg_data_t data;
753    
754          /* set data struct */          /* set data struct */
# Line 688  Line 807 
807                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
808                          data.dquant = pEnc->temp_dquants;                          data.dquant = pEnc->temp_dquants;
809                          data.dquant_stride = pEnc->mbParam.mb_width;                          data.dquant_stride = pEnc->mbParam.mb_width;
810                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
811                    }
812    
813                    if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
814                            int block = 0;
815                            emms();
816                            data.lambda = pEnc->temp_lambda;
817                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
818                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
819                                            for (k = 0; k < 6; k++)
820                                                    data.lambda[block++] = 1.0f;
821                  }                  }
822    
823          } else { /* XVID_PLG_AFTER */          } else { /* XVID_PLG_AFTER */
# Line 765  Line 894 
894          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
895                  emms();                  emms();
896                  if (pEnc->plugins[i].func) {                  if (pEnc->plugins[i].func) {
897                          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) {
898                                  continue;                                  continue;
899                          }                          }
900                  }                  }
# Line 794  Line 923 
923                                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;                                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
924                          }                          }
925                  }                  }
926    
927                    if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
928                            for (j = 0; j < pEnc->mbParam.mb_height; j++)
929                                    for (i = 0; i < pEnc->mbParam.mb_width; i++)
930                                            for (k = 0; k < 6; k++) {
931                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
932                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
933                                            }
934                    } else {
935                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
936                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
937                                            for (k = 0; k < 6; k++) {
938                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
939                                            }
940                    }
941    
942    
943                  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 */
944          }          }
945    
# Line 863  Line 1009 
1009  #endif  #endif
1010  }  }
1011    
 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;  
 }  
   
1012  static void  static void
1013  simplify_par(int *par_width, int *par_height)  simplify_par(int *par_width, int *par_height)
1014  {  {
# Line 911  Line 1039 
1039          return;          return;
1040  }  }
1041    
   
1042  /*****************************************************************************  /*****************************************************************************
1043   * IPB frame encoder entry point   * IPB frame encoder entry point
1044   *   *
# Line 1006  Line 1133 
1133                          }                          }
1134    
1135                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1136                          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);
1137                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1138    
1139                          goto done;                          goto done;
# Line 1038  Line 1165 
1165    
1166                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1167                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1168                          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);
1169    
1170                          /* flush complete: reset counters */                          /* flush complete: reset counters */
1171                          pEnc->flush_bframes = 0;                          pEnc->flush_bframes = 0;
# Line 1066  Line 1193 
1193                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1194    
1195                          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) {
1196                                  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);
1197                          }                          }
1198    
1199                          /* 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 1091  Line 1218 
1218                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1219                                  pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */                                  pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1220    
1221                                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs);
1222    
1223    
1224                                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {                                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1225                                          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);
1226                                  }else{                                  }else{
1227                                          pEnc->flush_bframes = 1;                                          pEnc->flush_bframes = 1;
1228                                          goto done;                                          goto done;
# Line 1144  Line 1271 
1271          type = frame->type;          type = frame->type;
1272          pEnc->current->quant = frame->quant;          pEnc->current->quant = frame->quant;
1273    
1274          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);
1275    
1276          if (type > 0){  /* XVID_TYPE_?VOP */          if (type > 0){  /* XVID_TYPE_?VOP */
1277                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
# Line 1219  Line 1346 
1346          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)
1347          {          {
1348                  if (pEnc->current->stamp > 0) {                  if (pEnc->current->stamp > 0) {
1349                          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);
1350                  }                  }
1351                  else          else if (stats) {
1352                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
1353          }          }
1354            }
1355    
1356          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1357           * closed-gop           * closed-gop
# Line 1247  Line 1375 
1375                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1376    
1377                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1378                          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");
1379                  }                  }
1380    
1381                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
# Line 1308  Line 1436 
1436    
1437                  /* prevent vol/vop misuse */                  /* prevent vol/vop misuse */
1438    
                 if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
                         pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;  
   
1439                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1440                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1441    
# Line 1343  Line 1468 
1468                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1469                  }                  }
1470    
1471                  if ( FrameCodeP(pEnc, &bs, 1, 0) == 0 ) {                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1472                          /* N-VOP, we mustn't code b-frames yet */                          /* N-VOP, we mustn't code b-frames yet */
1473                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1474                                     pEnc->mbParam.max_bframes == 0)
1475                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1476                          goto done;                          goto done;
1477                  }                  }
1478          }          }
# Line 1366  Line 1493 
1493    
1494          /* packed or no-bframes or no-bframes-queued: output stats */          /* packed or no-bframes or no-bframes-queued: output stats */
1495          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 ) {
1496                  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);
1497          }          }
1498    
1499          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1406  Line 1533 
1533    
1534    
1535  static __inline void  static __inline void
1536  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(MACROBLOCK * pMB)
                         MACROBLOCK * pMB)  
1537  {  {
   
1538          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1539    
1540          /* zero mv statistics */          /* zero mv statistics */
# Line 1423  Line 1548 
1548          }          }
1549  }  }
1550    
1551    static void
1552    SliceCodeI(SMPData *data)
 static int  
 FrameCodeI(Encoder * pEnc,  
                    Bitstream * bs)  
1553  {  {
1554          int bits = BitstreamPos(bs);          Encoder *pEnc = (Encoder *) data->pEnc;
1555            Bitstream *bs = (Bitstream *) data->bs;
1556    
1557            uint16_t x, y;
1558          int mb_width = pEnc->mbParam.mb_width;          int mb_width = pEnc->mbParam.mb_width;
1559          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1560    
1561            int bound = 0, num_slices = pEnc->num_slices;
1562            FRAMEINFO *const current = pEnc->current;
1563    
1564          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1565          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1566    
1567          uint16_t x, y;          if (data->start_y > 0) { /* write resync marker */
1568                    bound = data->start_y*mb_width;
1569                    write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1570            }
1571    
1572          if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))          for (y = data->start_y; y < data->stop_y; y++) {
1573          {                  int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1574                  mb_width = (pEnc->mbParam.width + 31) / 32;  
1575                  mb_height = (pEnc->mbParam.height + 31) / 32;                  if (new_bound > bound) {
1576                            bound = new_bound;
1577                            BitstreamPadAlways(bs);
1578                            write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1579                    }
1580    
1581                    for (x = 0; x < mb_width; x++) {
1582                            MACROBLOCK *pMB = &current->mbs[x + y * mb_width];
1583    
1584                            CodeIntraMB(pMB);
1585    
1586                            MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1587                                                              dct_codes, qcoeff);
1588    
                 /* 16x16->8x8 downsample requires 1 additional edge pixel*/  
                 /* XXX: setedges is overkill */  
1589                  start_timer();                  start_timer();
1590                  image_setedges(&pEnc->current->image,                          MBPrediction(current, x, y, mb_width, qcoeff, bound);
1591                          pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                          stop_prediction_timer();
1592                          pEnc->mbParam.width, pEnc->mbParam.height, 0);  
1593                  stop_edges_timer();                          start_timer();
1594                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1595                            stop_coding_timer();
1596    
1597                    }
1598            }
1599    
1600            emms();
1601            BitstreamPadAlways(bs);
1602    }
1603    
1604    static __inline void
1605    SerializeBitstreams(Encoder *pEnc, FRAMEINFO *current, Bitstream *bs, int num_threads)
1606    {
1607            int k;
1608            uint32_t pos = BitstreamLength(bs);
1609    
1610            for (k = 1; k < num_threads; k++) {
1611                    uint32_t len = BitstreamLength(pEnc->smpData[k].bs);
1612    
1613                    memcpy((void *)((ptr_t)bs->start + pos),
1614                               (void *)((ptr_t)pEnc->smpData[k].bs->start), len);
1615    
1616                    current->length += len;
1617                    pos += len;
1618    
1619                    /* collect stats */
1620                    current->sStat.iTextBits += pEnc->smpData[k].sStat->iTextBits;
1621                    current->sStat.kblks += pEnc->smpData[k].sStat->kblks;
1622                    current->sStat.mblks += pEnc->smpData[k].sStat->mblks;
1623                    current->sStat.ublks += pEnc->smpData[k].sStat->ublks;
1624                    current->sStat.iMVBits += pEnc->smpData[k].sStat->iMVBits;
1625            }
1626    
1627            if (num_threads > 1) {
1628                    uint32_t pos32 = pos>>2;
1629                    bs->tail = bs->start + pos32;
1630                    bs->pos = 8*(pos - (pos32<<2));
1631                    bs->buf = 0;
1632    
1633                    if (bs->pos > 0) {
1634                            uint32_t pos8 = bs->pos/8;
1635                            memset((void *)((ptr_t)bs->tail+pos8), 0, (4-pos8));
1636                            pos = *bs->tail;
1637    #ifndef ARCH_IS_BIG_ENDIAN
1638                            BSWAP(pos);
1639    #endif
1640                            bs->buf = pos;
1641          }          }
1642            }
1643    }
1644    
1645    static int
1646    FrameCodeI(Encoder * pEnc,
1647                       Bitstream * bs)
1648    {
1649            int bits = BitstreamPos(bs);
1650            int bound = 0, num_slices = pEnc->num_slices;
1651            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1652            int slices_per_thread = (num_slices*1024 / num_threads);
1653            int mb_height = pEnc->mbParam.mb_height;
1654    #ifdef HAVE_PTHREAD
1655            void * status = NULL;
1656    #endif
1657            uint16_t k;
1658    
1659          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1660          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1460  Line 1664 
1664    
1665          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1666    
1667          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current, num_slices);
1668    
1669          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1670    
# Line 1469  Line 1673 
1673          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1674    
1675          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;  
1676    
1677          for (y = 0; y < mb_height; y++)          /* multithreaded intra coding - dispatch threads */
1678                  for (x = 0; x < mb_width; x++) {          for (k = 0; k < num_threads; k++) {
1679                          MACROBLOCK *pMB =                  int add = ((slices_per_thread + 512) >> 10);
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1680    
1681                          CodeIntraMB(pEnc, pMB);                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
1682    
1683                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                  pEnc->smpData[k].pEnc = (void *) pEnc;
1684                                                            dct_codes, qcoeff);                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
1685                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
1686    
1687                          start_timer();                  bound += add;
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1688    
1689                          start_timer();                  if (k > 0) {
1690                          if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)                          BitstreamReset(pEnc->smpData[k].bs);
1691                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          pEnc->smpData[k].sStat->iTextBits = 0;
                                 qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
1692                          }                          }
                         MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);  
                         stop_coding_timer();  
1693                  }                  }
1694            pEnc->smpData[0].bs = bs;
1695            pEnc->smpData[0].sStat = &pEnc->current->sStat;
1696    
1697          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))  #ifdef HAVE_PTHREAD
1698          {          /* create threads */
1699                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,          for (k = 1; k < num_threads; k++) {
1700                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                  pthread_create(&pEnc->smpData[k].handle, NULL,
1701                          16, 0);                                 (void*)SliceCodeI, (void*)&pEnc->smpData[k]);
1702          }          }
1703          emms();  #endif
1704    
1705          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          SliceCodeI(&pEnc->smpData[0]);
1706    
1707    #ifdef HAVE_PTHREAD
1708            /* wait until all threads are finished */
1709            for (k = 1; k < num_threads; k++) {
1710                    pthread_join(pEnc->smpData[k].handle, &status);
1711            }
1712    #endif
1713    
1714          pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          pEnc->current->length = BitstreamLength(bs) - (bits/8);
1715    
1716            /* reassemble the pieces together */
1717            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
1718    
1719            pEnc->current->sStat.iMVBits = 0;
1720            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1721            pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1722    
1723          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1724          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
# Line 1517  Line 1729 
1729          return 1;                                       /* intra */          return 1;                                       /* intra */
1730  }  }
1731    
1732    static __inline void
1733    updateFcode(Statistics * sStat, Encoder * pEnc)
1734    {
1735            float fSigma;
1736            int iSearchRange;
1737    
1738            if (sStat->iMvCount == 0)
1739                    sStat->iMvCount = 1;
1740    
1741  #define INTRA_THRESHOLD 0.5          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
 #define BFRAME_SKIP_THRESHHOLD 30  
1742    
1743            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1744    
1745  /* FrameCodeP also handles S(GMC)-VOPs */          if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1746  static int                  pEnc->mbParam.m_fcode++;
 FrameCodeP(Encoder * pEnc,  
                    Bitstream * bs,  
                    bool force_inter,  
                    bool vol_header)  
 {  
         float fSigma;  
         int bits = BitstreamPos(bs);  
1747    
1748          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          else if ((5.0 * fSigma < iSearchRange)
1749          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);                             && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1750                               && (pEnc->mbParam.m_fcode >= 2) )
1751                    pEnc->mbParam.m_fcode--;
1752    
1753            pEnc->fMvPrevSigma = fSigma;
1754    }
1755    
1756    #define BFRAME_SKIP_THRESHHOLD 30
1757    
1758    static void
1759    SliceCodeP(SMPData *data)
1760    {
1761            Encoder *pEnc = (Encoder *) data->pEnc;
1762            Bitstream *bs = (Bitstream *) data->bs;
1763    
         int iLimit;  
1764          int x, y, k;          int x, y, k;
         int iSearchRange;  
         int bIntra=0, skip_possible;  
1765          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1766          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1767          MBParam * const pParam = &pEnc->mbParam;          MBParam * const pParam = &pEnc->mbParam;
1768          int mb_width = pParam->mb_width;          int mb_width = pParam->mb_width;
1769          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
         int coded = 1;  
1770    
1771            DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1772            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1773    
1774          /* IMAGE *pCurrent = &current->image; */          int bound = 0, num_slices = pEnc->num_slices;
         IMAGE *pRef = &reference->image;  
1775    
1776          if ((current->vop_flags & XVID_VOP_REDUCED))          if (data->start_y > 0) { /* write resync marker */
1777          {                  bound = data->start_y*mb_width;
1778                  mb_width = (pParam->width + 31) / 32;                  write_video_packet_header(bs, pParam, current, bound);
                 mb_height = (pParam->height + 31) / 32;  
1779          }          }
1780    
1781            for (y = data->start_y; y < data->stop_y; y++) {
1782                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1783    
1784          if (!reference->is_edged) {                  if (new_bound > bound) {
1785                  start_timer();                          bound = new_bound;
1786                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,                          BitstreamPadAlways(bs);
1787                                             pParam->width, pParam->height, 0);                          write_video_packet_header(bs, pParam, current, bound);
                 stop_edges_timer();  
                 reference->is_edged = 1;  
1788          }          }
1789    
1790          pParam->m_rounding_type = 1 - pParam->m_rounding_type;                  for (x = 0; x < mb_width; x++) {
1791          current->rounding_type = pParam->m_rounding_type;                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1792          current->fcode = pParam->m_fcode;                          int skip_possible;
1793    
1794          if (!force_inter)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1795                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);                                  CodeIntraMB(pMB);
1796          else                                  MBTransQuantIntra(pParam, current, pMB, x, y,
1797                  iLimit = mb_width * mb_height + 1;                                                                    dct_codes, qcoeff);
1798    
         if ((current->vop_flags & XVID_VOP_HALFPEL)) {  
                 if (reference->is_interpolated != current->rounding_type) {  
1799                          start_timer();                          start_timer();
1800                          image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff, bound);
1801                                                            &pEnc->vInterHV, pParam->edged_width,                                  stop_prediction_timer();
1802                                                            pParam->edged_height,  
1803                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),                                  data->sStat->kblks++;
1804                                                            current->rounding_type);  
1805                          stop_inter_timer();                                  MBCoding(current, pMB, qcoeff, bs, data->sStat);
1806                          reference->is_interpolated = current->rounding_type;                                  stop_coding_timer();
1807                  }                                  continue;
1808          }          }
1809    
1810          current->coding_type = P_VOP;                          start_timer();
1811                            MBMotionCompensation(pMB, x, y, &reference->image,
1812                                                                     &pEnc->vInterH, &pEnc->vInterV,
1813                                                                     &pEnc->vInterHV, &pEnc->vGMC,
1814                                                                     &current->image,
1815                                                                     dct_codes, pParam->width,
1816                                                                     pParam->height,
1817                                                                     pParam->edged_width,
1818                                                                     (current->vol_flags & XVID_VOL_QUARTERPEL),
1819                                                                     current->rounding_type,
1820                                                                     data->RefQ);
1821    
1822          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);                          stop_comp_timer();
1823    
1824          SetMacroblockQuants(&pEnc->mbParam, current);                          pMB->field_pred = 0;
1825    
1826          start_timer();                          if (pMB->cbp != 0) {
1827          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */                                  pMB->cbp = MBTransQuantInter(pParam, current, pMB, x, y,
1828          {       int gmcval;                                                               dct_codes, qcoeff);
1829                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,                          }
                                                                  &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);  
1830    
1831                  if (current->motion_flags & XVID_ME_GME_REFINE) {                          if (pMB->dquant != 0)
1832                          gmcval = GlobalMotionEstRefine(&current->warp,                                  MBSetDquant(pMB, x, y, pParam);
1833                                                                                     current->mbs, pParam,  
1834                                                                                     current, reference,  
1835                                                                                     &current->image,                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1836                                                                                     &reference->image,                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1837                                                                                     &pEnc->vInterH,                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1838                                                                                     &pEnc->vInterV,                                  data->sStat->mblks++;
                                                                                    &pEnc->vInterHV);  
1839                  } else {                  } else {
1840                          gmcval = globalSAD(&current->warp, pParam, current->mbs,                                  data->sStat->ublks++;
                                                            current,  
                                                            &reference->image,  
                                                            &current->image,  
                                                            pEnc->vGMC.y);  
1841                  }                  }
1842    
1843                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);                          start_timer();
1844    
1845                  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */                          /* Finished processing the MB, now check if to CODE or SKIP */
                 generate_GMCparameters( 3, 3, &current->warp,  
                                 pParam->width, pParam->height,  
                                 &current->new_gmc_data);  
1846    
1847                  if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                          (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )  
                 {  
                         current->coding_type = S_VOP;  
1848    
1849                          generate_GMCimage(&current->new_gmc_data, &reference->image,                          if (current->coding_type == S_VOP)
1850                                  pParam->mb_width, pParam->mb_height,                                  skip_possible &= (pMB->mcsel == 1);
1851                                  pParam->edged_width, pParam->edged_width/2,                          else { /* PVOP */
1852                                  pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1853                                  current->rounding_type, current->mbs, &pEnc->vGMC);                                                                                  pMB->qmvs : pMB->mvs;
1854                                    skip_possible &= ((mv->x|mv->y) == 0);
1855                            }
1856    
1857                  } else {                          if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1858                                    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1859                                    int bSkip = 1;
1860    
1861                          generate_GMCimage(&current->new_gmc_data, &reference->image,                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1862                                  pParam->mb_width, pParam->mb_height,                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1863                                  pParam->edged_width, pParam->edged_width/2,                                                  int iSAD;
1864                                  pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1865                                  current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */                                                                                  pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1866                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1867                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) &&
1868                                                            ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after
1869                                                                                                                                                                               resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */
1870                                                            bSkip = 0; /* could not SKIP */
1871                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1872                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1873                                                                    pMB->pmvs[0].x = - predMV.x;
1874                                                                    pMB->pmvs[0].y = - predMV.y;
1875                                                            } else {
1876                                                                    VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1877                                                                    pMB->pmvs[0].x = - predMV.x;
1878                                                                    pMB->pmvs[0].y = - predMV.y;
1879                                                            }
1880                                                            pMB->mode = MODE_INTER;
1881                                                            pMB->cbp = 0;
1882                                                            break;
1883                                                    }
1884                  }                  }
1885          }          }
1886    
1887          bIntra =                                  if (bSkip) {
1888                  MotionEstimation(&pEnc->mbParam, current, reference,                                          /* do SKIP */
1889                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                          pMB->mode = MODE_NOT_CODED;
1890                                           &pEnc->vGMC, iLimit);                                          MBSkip(bs);
1891                                            stop_coding_timer();
1892                                            continue;       /* next MB */
1893          stop_motion_timer();                                  }
   
         if (bIntra == 1) return FrameCodeI(pEnc, bs);  
   
         set_timecodes(current,reference,pParam->fbase);  
         if (vol_header)  
         {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, current);  
                 BitstreamPad(bs);  
1894          }          }
1895    
1896          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);                          /* ordinary case: normal coded INTER/INTER4V block */
1897                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1898                            stop_coding_timer();
1899                    }
1900            }
1901    
1902          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1903                  current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;          emms();
1904    }
1905    
1906    /* FrameCodeP also handles S(GMC)-VOPs */
1907    static int
1908    FrameCodeP(Encoder * pEnc, Bitstream * bs)
1909    {
1910            int bits = BitstreamPos(bs);
1911    
1912          for (y = 0; y < mb_height; y++) {          FRAMEINFO *const current = pEnc->current;
1913                  for (x = 0; x < mb_width; x++) {          FRAMEINFO *const reference = pEnc->reference;
1914                          MACROBLOCK *pMB =          MBParam * const pParam = &pEnc->mbParam;
1915                                  &current->mbs[x + y * pParam->mb_width];          int mb_width = pParam->mb_width;
1916            int mb_height = pParam->mb_height;
1917            int coded = 1;
1918    
1919                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);          int k = 0, bound = 0, num_slices = pEnc->num_slices;
1920            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1921    #ifdef HAVE_PTHREAD
1922            void * status = NULL;
1923            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
1924    #endif
1925            int slices_per_thread = (num_slices*1024 / num_threads);
1926    
1927                          if (bIntra) {          IMAGE *pRef = &reference->image;
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1928    
1929            if (!reference->is_edged) {
1930                                  start_timer();                                  start_timer();
1931                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1932                                  stop_prediction_timer();                                             pParam->width, pParam->height, XVID_BS_VERSION);
1933                    stop_edges_timer();
1934                                  current->sStat.kblks++;                  reference->is_edged = 1;
   
                                 if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)  
                                 {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
                                         qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                         qcoeff[5*64+0]=0;  
                                 }  
                                 MBCoding(current, pMB, qcoeff, bs, &current->sStat);  
                                 stop_coding_timer();  
                                 continue;  
1935                          }                          }
1936    
1937            pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1938            current->rounding_type = pParam->m_rounding_type;
1939            current->fcode = pParam->m_fcode;
1940    
1941            if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1942                    if (reference->is_interpolated != current->rounding_type) {
1943                          start_timer();                          start_timer();
1944                          MBMotionCompensation(pMB, x, y, &reference->image,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1945                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                            pEnc->vInterHV.y, pParam->edged_width,
1946                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                            pParam->edged_height,
1947                                                                   &current->image,                                                            (pParam->vol_flags & XVID_VOL_QUARTERPEL),
                                                                  dct_codes, pParam->width,  
                                                                  pParam->height,  
                                                                  pParam->edged_width,  
                                                                  (current->vol_flags & XVID_VOL_QUARTERPEL),  
                                                                  (current->vop_flags & XVID_VOP_REDUCED),  
1948                                                                   current->rounding_type);                                                                   current->rounding_type);
1949                            stop_inter_timer();
1950                            reference->is_interpolated = current->rounding_type;
1951                    }
1952            }
1953    
1954                          stop_comp_timer();          current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1955                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1956                    current->sStat.iMVBits = 0;
1957    
1958                          pMB->field_pred = 0;          current->coding_type = P_VOP;
1959    
1960                          if (pMB->mode != MODE_NOT_CODED)          if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1961                          {       pMB->cbp =                  image_block_variance(&current->image, pParam->edged_width, current->mbs,
1962                                          MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,                                       pParam->mb_width, pParam->mb_height);
                                                                           dct_codes, qcoeff);  
1963                          }                          }
1964    
1965                          if (pMB->dquant != 0)          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
                                 MBSetDquant(pMB, x, y, &pEnc->mbParam);  
1966    
1967            SetMacroblockQuants(&pEnc->mbParam, current);
1968    
1969                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||          start_timer();
1970                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||          if (current->vol_flags & XVID_VOL_GMC)  /* GMC only for S(GMC)-VOPs */
1971                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {          {       int gmcval;
1972                                  current->sStat.mblks++;                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1973                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, num_slices);
1974    
1975                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1976                            gmcval = GlobalMotionEstRefine(&current->warp,
1977                                                                                       current->mbs, pParam,
1978                                                                                       current, reference,
1979                                                                                       &current->image,
1980                                                                                       &reference->image,
1981                                                                                       &pEnc->vInterH,
1982                                                                                       &pEnc->vInterV,
1983                                                                                       &pEnc->vInterHV);
1984                          }  else {                          }  else {
1985                                  current->sStat.ublks++;                          gmcval = globalSAD(&current->warp, pParam, current->mbs,
1986                                                               current,
1987                                                               &reference->image,
1988                                                               &current->image,
1989                                                               pEnc->vGMC.y);
1990                          }                          }
1991    
1992                          start_timer();                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1993    
1994                          /* Finished processing the MB, now check if to CODE or SKIP */                  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1995                    generate_GMCparameters( 3, 3, &current->warp,
1996                                    pParam->width, pParam->height,
1997                                    &current->new_gmc_data);
1998    
1999                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                  if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
2000                                                          (pMB->dquant == 0);                           (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
2001                    {
2002                            current->coding_type = S_VOP;
2003    
2004                          if (current->coding_type == S_VOP)                          generate_GMCimage(&current->new_gmc_data, &reference->image,
2005                                  skip_possible &= (pMB->mcsel == 1);                                  pParam->mb_width, pParam->mb_height,
2006                          else if (current->coding_type == P_VOP) {                                  pParam->edged_width, pParam->edged_width/2,
2007                                  if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))                                  pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
2008                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                  current->rounding_type, current->mbs, &pEnc->vGMC);
2009                                  else  
2010                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );                  } else {
2011    
2012                            generate_GMCimage(&current->new_gmc_data, &reference->image,
2013                                    pParam->mb_width, pParam->mb_height,
2014                                    pParam->edged_width, pParam->edged_width/2,
2015                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
2016                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
2017                    }
2018                          }                          }
2019    
2020                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {  #ifdef HAVE_PTHREAD
2021            if (pEnc->num_threads > 0) {
2022    
2023  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */                  /* multithreaded motion estimation - dispatch threads */
2024                    while (k < pEnc->num_threads) {
2025                            int i, add_s = (slices_per_thread + 512) >> 10;
2026                            int add_t = (threads_per_slice + 512) >> 10;
2027    
2028                                  if (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */                          int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2029                                  {                          int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2030                                          int bSkip = 1;                          int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2031    
2032                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                          slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2033                                          {                          threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*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);  
                                                 if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)  
                                                 {       bSkip = 0;  
                                                         break;  
                                                 }  
                                         }  
2034    
2035                                          if (!bSkip) {   /* no SKIP, but trivial block */                          for (i = 0; i < add_t; i++) {
2036                                                  if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {                                  memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
                                                         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;  
                                                 MBCoding(current, pMB, qcoeff, bs, &current->sStat);  
                                                 stop_coding_timer();  
2037    
2038                                                  continue;       /* next MB */                                  pEnc->smpData[k+i].pEnc = (void *) pEnc;
2039                                          }                                  pEnc->smpData[k+i].y_row = i;
2040                                  }                                  pEnc->smpData[k+i].y_step = add_t;
2041                                  /* do SKIP */                                  pEnc->smpData[k+i].stop_y = stop_y;
2042                                    pEnc->smpData[k+i].start_y = start_y;
2043    
2044                                  pMB->mode = MODE_NOT_CODED;                                  /* todo: sort out temp space once and for all */
2045                                  MBSkip(bs);                                  pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2046                                  stop_coding_timer();                                                                                          16*((k+i)>>1)*pParam->edged_width;
                                 continue;       /* next MB */  
2047                          }                          }
                         /* ordinary case: normal coded INTER/INTER4V block */  
2048    
2049                          if ((current->vop_flags & XVID_VOP_GREYSCALE))                          pEnc->smpData[k].complete_count_above =
2050                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                  pEnc->smpData[k+add_t-1].complete_count_self - 1;
2051                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */  
2052                                  qcoeff[5*64+0]=0;                          bound += add_s;
2053                            k += add_t;
2054                          }                          }
2055    
2056                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {                  for (k = 1; k < pEnc->num_threads; k++) {
2057                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                          pthread_create(&pEnc->smpData[k].handle, NULL,
2058                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  (void*)MotionEstimateSMP, (void*)&pEnc->smpData[k]);
                                 pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;  
                                 DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);  
                         } else {  
                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                 pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;  
                                 pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;  
                                 DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);  
2059                          }                          }
2060    
2061                    MotionEstimateSMP(&pEnc->smpData[0]);
2062    
2063                          if (pMB->mode == MODE_INTER4V)                  for (k = 1; k < pEnc->num_threads; k++) {
2064                          {       int k;                          pthread_join(pEnc->smpData[k].handle, &status);
                                 for (k=1;k<4;k++)  
                                 {  
                                         if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;  
                                 DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);  
                                         } else {  
                                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;  
                                 DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);  
2065                                          }                                          }
2066    
2067                    current->fcode = 0;
2068                    for (k = 0; k < pEnc->num_threads; k++) {
2069                            current->sStat.iMvSum += pEnc->smpData[k].mvSum;
2070                            current->sStat.iMvCount += pEnc->smpData[k].mvCount;
2071                            if (pEnc->smpData[k].minfcode > current->fcode)
2072                                    current->fcode = pEnc->smpData[k].minfcode;
2073                                  }                                  }
2074    
2075            } else
2076    #endif
2077            {
2078    
2079                    /* regular ME */
2080    
2081                    MotionEstimation(&pEnc->mbParam, current, reference,
2082                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2083                                                     &pEnc->vGMC, 256*4096, num_slices);
2084    
2085                          }                          }
2086    
2087                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);          stop_motion_timer();
2088                          stop_coding_timer();  
2089            set_timecodes(current,reference,pParam->fbase);
2090    
2091            BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
2092    
2093            /* multithreaded inter coding - dispatch threads */
2094    
2095            bound = 0;
2096            slices_per_thread = (num_slices*1024 / num_threads);
2097    
2098            for (k = 0; k < num_threads; k++) {
2099                    int add = ((slices_per_thread + 512) >> 10);
2100    
2101                    slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2102    
2103                    pEnc->smpData[k].pEnc = (void *) pEnc;
2104                    pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2105                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2106                    pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2107    
2108                    bound += add;
2109    
2110                    if (k > 0) {
2111                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2112                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks =
2113                            pEnc->smpData[k].sStat->iMVBits = 0;
2114    
2115                            BitstreamReset(pEnc->smpData[k].bs);
2116                  }                  }
2117          }          }
2118            pEnc->smpData[0].bs = bs;
2119            pEnc->smpData[0].sStat = &current->sStat;
2120    
2121          if ((current->vop_flags & XVID_VOP_REDUCED))  #ifdef HAVE_PTHREAD
2122          {          /* create threads */
2123                  image_deblock_rrv(&current->image, pParam->edged_width,          for (k = 1; k < num_threads; k++) {
2124                          current->mbs, mb_width, mb_height, pParam->mb_width,                  pthread_create(&pEnc->smpData[k].handle, NULL,
2125                          16, 0);                          (void*)SliceCodeP, (void*)&pEnc->smpData[k]);
2126          }          }
2127    #endif
2128    
2129          emms();          SliceCodeP(&pEnc->smpData[0]);
2130    
2131          if (current->sStat.iMvCount == 0)  #ifdef HAVE_PTHREAD
2132                  current->sStat.iMvCount = 1;          /* wait until all threads are finished */
2133            for (k = 1; k < num_threads; k++) {
2134                    pthread_join(pEnc->smpData[k].handle, &status);
2135            }
2136    #endif
2137    
2138          fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);          current->length = BitstreamLength(bs) - (bits/8);
2139    
2140          iSearchRange = 1 << (3 + pParam->m_fcode);          /* reassemble the pieces together */
2141            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
2142    
2143          if ((fSigma > iSearchRange / 3)          updateFcode(&current->sStat, pEnc);
                 && (pParam->m_fcode <= (3 +  (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0)  ))) /* maximum search range 128 */  
         {  
                 pParam->m_fcode++;  
                 iSearchRange *= 2;  
         } else if ((fSigma < iSearchRange / 6)  
                            && (pEnc->fMvPrevSigma >= 0)  
                            && (pEnc->fMvPrevSigma < iSearchRange / 6)  
                            && (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) )))        /* minimum search range 16 */  
         {  
                 pParam->m_fcode--;  
                 iSearchRange /= 2;  
         }  
   
         pEnc->fMvPrevSigma = fSigma;  
2144    
2145          /* frame drop code */          /* frame drop code */
2146  #if 0  #if 0
2147          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);
2148  #endif  #endif
2149          if (current->sStat.kblks + current->sStat.mblks <=  
2150                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100)          if (current->sStat.kblks + current->sStat.mblks <
2151                    (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
2152                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) &&
2153                    (current->coding_type == P_VOP) )
2154          {          {
2155                  current->sStat.kblks = current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
2156                  current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
2157    
2158                  BitstreamReset(bs);                  BitstreamReset(bs);
# Line 1894  Line 2171 
2171                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2172                  coded = 0;                  coded = 0;
2173    
2174                    BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2175    
2176                    current->length = (BitstreamPos(bs) - bits) / 8;
2177    
2178          } else {          } else {
2179    
2180                  pEnc->current->is_edged = 0; /* not edged */                  pEnc->current->is_edged = 0; /* not edged */
# Line 1922  Line 2203 
2203          }          }
2204          */          */
2205    
2206          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          return coded;
2207    }
2208    
2209          current->length = (BitstreamPos(bs) - bits) / 8;  static void
2210    SliceCodeB(SMPData *data)
2211    {
2212            Encoder *pEnc = (Encoder *) data->pEnc;
2213            Bitstream *bs = (Bitstream *) data->bs;
2214    
2215          return coded;          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2216            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2217    
2218            int x, y;
2219            FRAMEINFO * const frame = (FRAMEINFO * const) data->current;
2220            MBParam * const pParam = &pEnc->mbParam;
2221            int mb_width = pParam->mb_width;
2222            int mb_height = pParam->mb_height;
2223            IMAGE *f_ref = &pEnc->reference->image;
2224            IMAGE *b_ref = &pEnc->current->image;
2225    
2226            int bound = data->start_y*mb_width;
2227            int num_slices = pEnc->num_slices;
2228    
2229            if (data->start_y > 0) { /* write resync marker */
2230                    write_video_packet_header(bs, pParam, frame, bound+1);
2231            }
2232    
2233            for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) {
2234                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
2235                    int stop_x = (y == data->stop_y) ? 1 : mb_width;
2236                    int start_x = (y == data->start_y && y > 0) ? 1 : 0;
2237    
2238                    for (x = start_x; x < stop_x; x++) {
2239                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2240    
2241                            /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2242                            if (mb->mode == MODE_NOT_CODED) {
2243                                    if (pParam->plugin_flags & XVID_REQORIGINAL) {
2244                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2245                                                                                     NULL, 0, 0, pParam->edged_width, 0, 0, data->RefQ);
2246  }  }
2247                                    continue;
2248                            }
2249    
2250                            if (new_bound > bound && x > 0) {
2251                                    bound = new_bound;
2252                                    BitstreamPadAlways(bs);
2253                                    write_video_packet_header(bs, pParam, frame, y*mb_width+x);
2254                            }
2255    
2256                            mb->quant = frame->quant;
2257    
2258                            if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) {
2259                                    /* we have to motion-compensate, transfer etc,
2260                                            because there might be blocks to code */
2261    
2262                                    MBMotionCompensationBVOP(pParam, mb, x, y, &frame->image,
2263                                                                                     f_ref, &pEnc->f_refh, &pEnc->f_refv,
2264                                                                                     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2265                                                                                     &pEnc->vInterV, &pEnc->vInterHV, dct_codes,
2266                                                                                     data->RefQ);
2267    
2268                                    mb->cbp = MBTransQuantInterBVOP(pParam, frame, mb, x, y,  dct_codes, qcoeff);
2269                            }
2270    
2271                            if (mb->mode == MODE_DIRECT_NO4V)
2272                                    mb->mode = MODE_DIRECT;
2273    
2274                            if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2275                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2276                            else
2277                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2278                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2279                                            mb->cbp &= 0x3C;
2280    
2281                            start_timer();
2282                            MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, data->sStat);
2283                            stop_coding_timer();
2284                    }
2285            }
2286    
2287            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2288            emms();
2289    }
2290    
2291  static void  static void
2292  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
# Line 1936  Line 2294 
2294                     Bitstream * bs)                     Bitstream * bs)
2295  {  {
2296          int bits = BitstreamPos(bs);          int bits = BitstreamPos(bs);
2297          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          int k = 0, bound = 0, num_slices = pEnc->num_slices;
2298          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
2299          uint32_t x, y;  #ifdef HAVE_PTHREAD
2300            void * status = NULL;
2301            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
2302    #endif
2303            int slices_per_thread = (num_slices*1024 / num_threads);
2304    
2305          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2306          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2307    
2308            MBParam * const pParam = &pEnc->mbParam;
2309            int mb_height = pParam->mb_height;
2310    
2311          #ifdef BFRAMES_DEC_DEBUG          #ifdef BFRAMES_DEC_DEBUG
2312          FILE *fp;          FILE *fp;
2313          static char first=0;          static char first=0;
# Line 1950  Line 2315 
2315                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2316          }          }
2317    
         /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */  
   
2318          if (!first){          if (!first){
2319                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2320          }          }
# Line 1961  Line 2324 
2324          if (!pEnc->reference->is_edged) {          if (!pEnc->reference->is_edged) {
2325                  image_setedges(f_ref, pEnc->mbParam.edged_width,                  image_setedges(f_ref, pEnc->mbParam.edged_width,
2326                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,
2327                                             pEnc->mbParam.height, 0);                                             pEnc->mbParam.height, XVID_BS_VERSION);
2328                  pEnc->current->is_edged = 1;                  pEnc->reference->is_edged = 1;
2329          }          }
2330    
2331          if (pEnc->reference->is_interpolated != 0) {          if (pEnc->reference->is_interpolated != 0) {
2332                  start_timer();                  start_timer();
2333                  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,
2334                                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2335                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2336                  stop_inter_timer();                  stop_inter_timer();
# Line 1978  Line 2341 
2341          if (!pEnc->current->is_edged) {          if (!pEnc->current->is_edged) {
2342                  image_setedges(b_ref, pEnc->mbParam.edged_width,                  image_setedges(b_ref, pEnc->mbParam.edged_width,
2343                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,
2344                                             pEnc->mbParam.height, 0);                                             pEnc->mbParam.height, XVID_BS_VERSION);
2345                  pEnc->current->is_edged = 1;                  pEnc->current->is_edged = 1;
2346          }          }
2347    
2348          if (pEnc->current->is_interpolated != 0) {          if (pEnc->current->is_interpolated != 0) {
2349                  start_timer();                  start_timer();
2350                  image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
2351                                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2352                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2353                  stop_inter_timer();                  stop_inter_timer();
# Line 1992  Line 2355 
2355          }          }
2356    
2357          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2358          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);  
2359            if ((frame->vop_flags & XVID_VOP_RD_PSNRHVSM) && (frame->vop_flags & XVID_VOP_RD_BVOP)) {
2360                    image_block_variance(&frame->image, pEnc->mbParam.edged_width, frame->mbs,
2361                                         pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2362            }
2363    
2364            call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2365    
2366            frame->fcode = frame->bcode = pEnc->current->fcode;
2367    
2368          start_timer();          start_timer();
2369    
2370    #ifdef HAVE_PTHREAD
2371            if (pEnc->num_threads > 0) {
2372    
2373                    /* multithreaded motion estimation - dispatch threads */
2374                    while (k < pEnc->num_threads) {
2375                            int i, add_s = (slices_per_thread + 512) >> 10;
2376                            int add_t = (threads_per_slice + 512) >> 10;
2377    
2378                            int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2379                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2380                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2381    
2382                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2383                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2384    
2385                            for (i = 0; i < add_t; i++) {
2386                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2387    
2388                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2389                                    pEnc->smpData[k+i].current = frame;
2390    
2391                                    pEnc->smpData[k+i].y_row = i;
2392                                    pEnc->smpData[k+i].y_step = add_t;
2393                                    pEnc->smpData[k+i].stop_y = stop_y;
2394                                    pEnc->smpData[k+i].start_y = start_y;
2395    
2396                                    /* todo: sort out temp space once and for all */
2397                                    pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2398                                                                                            16*((k+i)>>1)*pParam->edged_width;
2399                            }
2400    
2401                            pEnc->smpData[k].complete_count_above =
2402                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2403    
2404                            bound += add_s;
2405                            k += add_t;
2406                    }
2407    
2408                    for (k = 1; k < pEnc->num_threads; k++) {
2409                            pthread_create(&pEnc->smpData[k].handle, NULL,
2410                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->smpData[k]);
2411                    }
2412    
2413                    SMPMotionEstimationBVOP(&pEnc->smpData[0]);
2414    
2415                    for (k = 1; k < pEnc->num_threads; k++) {
2416                            pthread_join(pEnc->smpData[k].handle, &status);
2417                    }
2418    
2419                    frame->fcode = frame->bcode = 0;
2420                    for (k = 0; k < pEnc->num_threads; k++) {
2421                            if (pEnc->smpData[k].minfcode > frame->fcode)
2422                                    frame->fcode = pEnc->smpData[k].minfcode;
2423                            if (pEnc->smpData[k].minbcode > frame->bcode)
2424                                    frame->bcode = pEnc->smpData[k].minbcode;
2425                    }
2426            } else
2427    #endif
2428            {
2429    
2430          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2431                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2432                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2433                                                   pEnc->reference->mbs, f_ref,                                                   pEnc->reference->mbs, f_ref,
2434                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2435                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2436                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                           &pEnc->vInterV, &pEnc->vInterHV,
2437                                                             pEnc->num_slices);
2438            }
2439          stop_motion_timer();          stop_motion_timer();
2440    
2441          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2442          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2443    
2444            /* reset stats */
2445          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2446            frame->sStat.iMVBits = 0;
2447          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2448          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2449          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2450          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2451          frame->sStat.kblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.ublks = 0;
2452    
2453          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          /* multithreaded inter coding - dispatch threads */
2454                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          bound = 0;
2455                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];          slices_per_thread = (num_slices*1024 / num_threads);
   
                         /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */  
                         if (mb->mode == MODE_NOT_CODED) {  
                                 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, 0);  
                                 }  
2456    
2457                                  continue;          for (k = 0; k < num_threads; k++) {
2458                          }                  int add = ((slices_per_thread + 512) >> 10);
2459    
2460                          if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
                                 MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,  
                                                                          f_ref, &pEnc->f_refh, &pEnc->f_refv,  
                                                                          &pEnc->f_refhv, b_ref, &pEnc->vInterH,  
                                                                          &pEnc->vInterV, &pEnc->vInterHV,  
                                                                          dct_codes);  
2461    
2462                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                  pEnc->smpData[k].pEnc = (void *) pEnc;
2463                                  mb->quant = frame->quant;                  pEnc->smpData[k].current = frame;
2464                    pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2465                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2466                    bound += add;
2467    
2468                                  if (mb->mode != MODE_DIRECT_NONE_MV)                  /* todo: sort out temp space once and for all */
2469                                          mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2470    
2471                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                  if (k > 0) {
2472                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                          BitstreamReset(pEnc->smpData[k].bs);
2473                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */                          pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2474                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks = pEnc->smpData[k].sStat->iMVBits = 0;
2475                                  }                                  }
2476                          }                          }
2477    
2478                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the  #ifdef HAVE_PTHREAD
2479                           * coding function for BFrames, that's why we don't zero teh DC          for (k = 1; k < num_threads; k++) {
2480                           * coeffs */                  pthread_create(&pEnc->smpData[k].handle, NULL,
2481                          if ((frame->vop_flags & XVID_VOP_GREYSCALE))                          (void*)SliceCodeB, (void*)&pEnc->smpData[k]);
                                 mb->cbp &= 0x3C;  
   
                         start_timer();  
                         MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,  
                                                  &frame->sStat);  
                         stop_coding_timer();  
                 }  
2482          }          }
2483    #endif
2484    
2485          emms();          pEnc->smpData[0].bs = bs;
2486            pEnc->smpData[0].sStat = &frame->sStat;
2487            SliceCodeB(&pEnc->smpData[0]);
2488    
2489    #ifdef HAVE_PTHREAD
2490            for (k = 1; k < num_threads; k++) {
2491                    pthread_join(pEnc->smpData[k].handle, &status);
2492            }
2493    #endif
2494    
2495          /* TODO: dynamic fcode/bcode ??? */          frame->length = BitstreamLength(bs) - (bits/8);
2496    
2497          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          /* reassemble the pieces together */
2498          frame->length = (BitstreamPos(bs) - bits) / 8;          SerializeBitstreams(pEnc, frame, bs, num_threads);
2499    
2500  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2501          if (!first){          if (!first){

Legend:
Removed from v.1.95.2.66  
changed lines
  Added in v.1.141

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