[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.46, Mon Oct 27 00:50:05 2003 UTC revision 1.136, Tue Dec 28 19:19:43 2010 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 123  Line 146 
146          if (create->width%2 || create->height%2)          if (create->width%2 || create->height%2)
147                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
148    
149            if (create->width<=0 || create->height<=0)
150                    return XVID_ERR_FAIL;
151    
152          /* allocate encoder struct */          /* allocate encoder struct */
153    
154          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
# Line 134  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 147  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 178  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 188  Line 216 
216          pcreate.zones = pEnc->zones;          pcreate.zones = pEnc->zones;
217          pcreate.width = pEnc->mbParam.width;          pcreate.width = pEnc->mbParam.width;
218          pcreate.height = pEnc->mbParam.height;          pcreate.height = pEnc->mbParam.height;
219                    pcreate.mb_width = pEnc->mbParam.mb_width;
220                    pcreate.mb_height = pEnc->mbParam.mb_height;
221          pcreate.fincr = pEnc->mbParam.fincr;          pcreate.fincr = pEnc->mbParam.fincr;
222          pcreate.fbase = pEnc->mbParam.fbase;          pcreate.fbase = pEnc->mbParam.fbase;
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 211  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 248  Line 286 
286          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
287                  goto xvid_err_memory2;                  goto xvid_err_memory2;
288    
289            /* allocate quant matrix memory */
290    
291            pEnc->mbParam.mpeg_quant_matrices =
292                    xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
293    
294            if (pEnc->mbParam.mpeg_quant_matrices == NULL)
295                    goto xvid_err_memory2a;
296    
297          /* allocate interpolation image memory */          /* allocate interpolation image memory */
298    
299      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
# Line 263  Line 309 
309          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
310          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
311          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
312          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
313    
314          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
315          if (image_create          if (image_create
# Line 309  Line 353 
353                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
354                  goto xvid_err_memory3;                  goto xvid_err_memory3;
355          if (image_create          if (image_create
                 (&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create  
356                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
357                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
358                  goto xvid_err_memory3;                  goto xvid_err_memory3;
         if (image_create  
                 (&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
359    
360  /* Create full bitplane for GMC, this might be wasteful */  /* Create full bitplane for GMC, this might be wasteful */
361          if (image_create          if (image_create
# Line 399  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 408  Line 444 
444          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
445          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
446    
447            /* slices */
448            pEnc->num_slices = MIN(MAX(1, create->num_slices), (int) pEnc->mbParam.mb_height);
449    
450            /* multithreaded stuff */
451            if (create->num_threads > 0) {
452                    int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */
453                    int threads_per_slice = MAX(1, (t / pEnc->num_slices));
454                    int rows_per_thread = (pEnc->mbParam.mb_height + threads_per_slice - 1) / threads_per_slice;
455    
456                    pEnc->num_threads = t;
457                    pEnc->smpData = xvid_malloc(t*sizeof(SMPData), CACHE_LINE);
458                    if (!pEnc->smpData)
459                            goto xvid_err_nosmp;
460    
461                    /* tmp bitstream buffer for slice coding */
462                    pEnc->smpData[0].tmp_buffer = xvid_malloc(16*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height*sizeof(uint8_t), CACHE_LINE);
463                    if (! pEnc->smpData[0].tmp_buffer) goto xvid_err_nosmp;
464    
465                    for (n = 0; n < t; n++) {
466                            int s = MIN(pEnc->num_threads, pEnc->num_slices);
467    
468                            pEnc->smpData[n].complete_count_self =
469                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
470    
471                            if (!pEnc->smpData[n].complete_count_self)
472                                    goto xvid_err_nosmp;
473    
474                            if (n > 0 && n < s) {
475                                    pEnc->smpData[n].bs = (Bitstream *) xvid_malloc(sizeof(Bitstream), CACHE_LINE);
476                                    if (!pEnc->smpData[n].bs)
477                                            goto xvid_err_nosmp;
478    
479                                    pEnc->smpData[n].sStat = (Statistics *) xvid_malloc(sizeof(Statistics), CACHE_LINE);
480                                    if (!pEnc->smpData[n].sStat)
481                                            goto xvid_err_nosmp;
482    
483                                    pEnc->smpData[n].tmp_buffer = pEnc->smpData[0].tmp_buffer + 16*(((n-1)*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height)/s);
484                                    BitstreamInit(pEnc->smpData[n].bs, pEnc->smpData[n].tmp_buffer, 0);
485                            }
486    
487                            if (n != 0)
488                                    pEnc->smpData[n].complete_count_above =
489                                            pEnc->smpData[n-1].complete_count_self;
490                    }
491                    pEnc->smpData[0].complete_count_above =
492                            pEnc->smpData[t-1].complete_count_self - 1;
493    
494            } else {
495      xvid_err_nosmp:
496                    /* no SMP */
497                    if (pEnc->smpData) {
498                            if (pEnc->smpData[0].tmp_buffer)
499                                    xvid_free(pEnc->smpData[0].tmp_buffer);
500                    }
501                    else {
502                            pEnc->smpData = xvid_malloc(1*sizeof(SMPData), CACHE_LINE);
503                            if (pEnc->smpData == NULL)
504                                    goto xvid_err_memory5;
505                    }
506    
507                    create->num_threads = 0;
508            }
509    
510      create->handle = (void *) pEnc;      create->handle = (void *) pEnc;
511    
512          init_timer();          init_timer();
513            init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
514    
515      return 0;   /* ok */      return 0;   /* ok */
516    
# Line 470  Line 570 
570                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
571          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
573          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
574                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
575    
576  /* destroy GMC image */  /* destroy GMC image */
577          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
578                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
579    
580      xvid_err_memory2a:
581            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
582    
583    xvid_err_memory2:    xvid_err_memory2:
584          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
# Line 495  Line 593 
593              xvid_free(pEnc->temp_dquants);              xvid_free(pEnc->temp_dquants);
594      }      }
595    
596            if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
597                    xvid_free(pEnc->temp_lambda);
598            }
599    
600    xvid_err_memory0:    xvid_err_memory0:
601      for (n=0; n<pEnc->num_plugins;n++) {      for (n=0; n<pEnc->num_plugins;n++) {
602          if (pEnc->plugins[n].func) {          if (pEnc->plugins[n].func) {
603              pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);                          pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
604          }          }
605      }      }
606      xvid_free(pEnc->plugins);      xvid_free(pEnc->plugins);
# Line 563  Line 665 
665                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
666          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
667                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
668          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
669                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
   
670          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
671                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
672          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
# Line 598  Line 695 
695          xvid_free(pEnc->temp_dquants);          xvid_free(pEnc->temp_dquants);
696      }      }
697    
698            if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
699                    xvid_free(pEnc->temp_lambda);
700            }
701    
702      if (pEnc->num_plugins>0) {      if (pEnc->num_plugins>0) {
703          xvid_plg_destroy_t pdestroy;          xvid_plg_destroy_t pdestroy;
# Line 608  Line 708 
708    
709          for (i=0; i<pEnc->num_plugins;i++) {          for (i=0; i<pEnc->num_plugins;i++) {
710              if (pEnc->plugins[i].func) {              if (pEnc->plugins[i].func) {
711                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0);                                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
712              }              }
713          }          }
714          xvid_free(pEnc->plugins);          xvid_free(pEnc->plugins);
715      }      }
716    
717      if (pEnc->num_plugins>0)          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
718    
719            if (pEnc->num_zones > 0)
720          xvid_free(pEnc->zones);          xvid_free(pEnc->zones);
721    
722            if (pEnc->num_threads > 0) {
723                    for (i = 1; i < MAX(1, MIN(pEnc->num_threads, pEnc->num_slices)); i++) {
724                            xvid_free(pEnc->smpData[i].bs);
725                            xvid_free(pEnc->smpData[i].sStat);
726                    }
727                    if (pEnc->smpData[0].tmp_buffer) xvid_free(pEnc->smpData[0].tmp_buffer);
728    
729                    for (i = 0; i < pEnc->num_threads; i++)
730                            xvid_free(pEnc->smpData[i].complete_count_self);
731            }
732            xvid_free(pEnc->smpData);
733    
734          xvid_free(pEnc);          xvid_free(pEnc);
735    
736          return 0;  /* ok */          return 0;  /* ok */
# Line 630  Line 744 
744  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
745                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)                           int opt, int * type, int * quant, xvid_enc_stats_t * stats)
746  {  {
747      unsigned int i, j;          unsigned int i, j, k;
748      xvid_plg_data_t data;      xvid_plg_data_t data;
749    
750      /* set data struct */      /* set data struct */
# Line 648  Line 762 
762      data.mb_height = pEnc->mbParam.mb_height;      data.mb_height = pEnc->mbParam.mb_height;
763      data.fincr = frame->fincr;      data.fincr = frame->fincr;
764      data.fbase = pEnc->mbParam.fbase;      data.fbase = pEnc->mbParam.fbase;
765            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
766            data.bquant_offset = pEnc->mbParam.bquant_offset;
767    
768      for (i=0; i<3; i++) {      for (i=0; i<3; i++) {
769          data.min_quant[i] = pEnc->mbParam.min_quant[i];          data.min_quant[i] = pEnc->mbParam.min_quant[i];
770          data.max_quant[i] = pEnc->mbParam.max_quant[i];          data.max_quant[i] = pEnc->mbParam.max_quant[i];
771      }      }
772    
773      data.reference.csp = XVID_CSP_USER;          data.reference.csp = XVID_CSP_PLANAR;
774      data.reference.plane[0] = pEnc->reference->image.y;      data.reference.plane[0] = pEnc->reference->image.y;
775      data.reference.plane[1] = pEnc->reference->image.u;      data.reference.plane[1] = pEnc->reference->image.u;
776      data.reference.plane[2] = pEnc->reference->image.v;      data.reference.plane[2] = pEnc->reference->image.v;
# Line 662  Line 778 
778      data.reference.stride[1] = pEnc->mbParam.edged_width/2;      data.reference.stride[1] = pEnc->mbParam.edged_width/2;
779      data.reference.stride[2] = pEnc->mbParam.edged_width/2;      data.reference.stride[2] = pEnc->mbParam.edged_width/2;
780    
781      data.current.csp = XVID_CSP_USER;          data.current.csp = XVID_CSP_PLANAR;
782      data.current.plane[0] = frame->image.y;      data.current.plane[0] = frame->image.y;
783      data.current.plane[1] = frame->image.u;      data.current.plane[1] = frame->image.u;
784      data.current.plane[2] = frame->image.v;      data.current.plane[2] = frame->image.v;
# Line 676  Line 792 
792          data.type = *type;          data.type = *type;
793          data.quant = *quant;          data.quant = *quant;
794    
795                    data.vol_flags = frame->vol_flags;
796                    data.vop_flags = frame->vop_flags;
797                    data.motion_flags = frame->motion_flags;
798    
799            } else if (opt == XVID_PLG_FRAME) {
800                    data.type = coding2type(frame->coding_type);
801                    data.quant = frame->quant;
802    
803                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
804              data.dquant = pEnc->temp_dquants;              data.dquant = pEnc->temp_dquants;
805              data.dquant_stride = pEnc->mbParam.mb_width;              data.dquant_stride = pEnc->mbParam.mb_width;
806                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
807          }          }
808    
809          data.vol_flags = frame->vol_flags;                  if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
810          data.vop_flags = frame->vop_flags;                          int block = 0;
811          data.motion_flags = frame->motion_flags;                          emms();
812                            data.lambda = pEnc->temp_lambda;
813                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
814                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
815                                            for (k = 0; k < 6; k++)
816                                                    data.lambda[block++] = 1.0f;
817                    }
818    
819      } else { /* XVID_PLG_AFTER */      } else { /* XVID_PLG_AFTER */
820          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
821              data.original.csp = XVID_CSP_USER;                          data.original.csp = XVID_CSP_PLANAR;
822              data.original.plane[0] = original->y;              data.original.plane[0] = original->y;
823              data.original.plane[1] = original->u;              data.original.plane[1] = original->u;
824              data.original.plane[2] = original->v;              data.original.plane[2] = original->v;
# Line 738  Line 868 
868          data.mblks = frame->sStat.mblks;          data.mblks = frame->sStat.mblks;
869          data.ublks = frame->sStat.ublks;          data.ublks = frame->sStat.ublks;
870    
871          if (stats) {                  /* New code */
872                  stats->type = coding2type(frame->coding_type);                  data.stats.type      = coding2type(frame->coding_type);
873                  stats->quant = frame->quant;                  data.stats.quant     = frame->quant;
874                  stats->vol_flags = frame->vol_flags;                  data.stats.vol_flags = frame->vol_flags;
875                  stats->vop_flags = frame->vop_flags;                  data.stats.vop_flags = frame->vop_flags;
876                  stats->length = frame->length;                  data.stats.length    = frame->length;
877                  stats->hlength = frame->length - (frame->sStat.iTextBits / 8);                  data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
878                  stats->kblks = frame->sStat.kblks;                  data.stats.kblks     = frame->sStat.kblks;
879                  stats->mblks = frame->sStat.mblks;                  data.stats.mblks     = frame->sStat.mblks;
880                  stats->ublks = frame->sStat.ublks;                  data.stats.ublks     = frame->sStat.ublks;
881              stats->sse_y = data.sse_y;                  data.stats.sse_y     = data.sse_y;
882              stats->sse_u = data.sse_u;                  data.stats.sse_u     = data.sse_u;
883              stats->sse_v = data.sse_v;                  data.stats.sse_v     = data.sse_v;
884          }  
885                    if (stats)
886                            *stats = data.stats;
887      }      }
888    
889      /* call plugins */      /* call plugins */
890      for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {      for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
891          emms();          emms();
892          if (pEnc->plugins[i].func) {          if (pEnc->plugins[i].func) {
893              if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {                          if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
894                  continue;                  continue;
895              }              }
896          }          }
# Line 770  Line 902 
902          *type = data.type;          *type = data.type;
903          *quant = data.quant > 0 ? data.quant : 2;   /* default */          *quant = data.quant > 0 ? data.quant : 2;   /* default */
904    
905                    frame->vol_flags = data.vol_flags;
906                    frame->vop_flags = data.vop_flags;
907                    frame->motion_flags = data.motion_flags;
908    
909            } else if (opt == XVID_PLG_FRAME) {
910    
911          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
912              for (j=0; j<pEnc->mbParam.mb_height; j++)              for (j=0; j<pEnc->mbParam.mb_height; j++)
913              for (i=0; i<pEnc->mbParam.mb_width; i++) {              for (i=0; i<pEnc->mbParam.mb_width; i++) {
# Line 782  Line 920 
920              }              }
921          }          }
922    
923          frame->vol_flags = data.vol_flags;                  if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
924          frame->vop_flags = data.vop_flags;                          for (j = 0; j < pEnc->mbParam.mb_height; j++)
925          frame->motion_flags = data.motion_flags;                                  for (i = 0; i < pEnc->mbParam.mb_width; i++)
926                                            for (k = 0; k < 6; k++) {
927                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
928                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
929                                            }
930                    } else {
931                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
932                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
933                                            for (k = 0; k < 6; k++) {
934                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
935      }      }
936  }  }
937    
938    
939                    frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
940            }
941    
942    
943    }
944    
945    
946  static __inline void inc_frame_num(Encoder * pEnc)  static __inline void inc_frame_num(Encoder * pEnc)
# Line 806  Line 958 
958      pEnc->m_framenum--; /* debug ticker */      pEnc->m_framenum--; /* debug ticker */
959  }  }
960    
961    static __inline void
962    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
963    {
964            if (pMB->cbp == 0) {
965                    /* we want to code dquant but the quantizer value will not be used yet
966                            let's find out if we can postpone dquant to next MB
967                    */
968                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
969                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
970                            return;
971                    } else {
972                            MACROBLOCK * next = pMB + 1;
973                            const MACROBLOCK * prev = pMB - 1;
974                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
975                                    /* mode allows dquant change in the future */
976                                    if (abs(next->quant - prev->quant) <= 2) {
977                                            /* quant change is not out of range */
978                                            pMB->quant = prev->quant;
979                                            pMB->dquant = 0;
980                                            next->dquant = next->quant - prev->quant;
981                                            return;
982                                    }
983                    }
984            }
985            /* couldn't skip this dquant */
986            pMB->mode = MODE_INTER_Q;
987    }
988    
989    
990    
991  static __inline void  static __inline void
# Line 815  Line 995 
995      pCur->ticks = (int32_t)pCur->stamp % time_base;      pCur->ticks = (int32_t)pCur->stamp % time_base;
996                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
997    
998                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */  #if 0   /* HEAVY DEBUG OUTPUT */
999            fprintf(stderr,"WriteVop:   %d - %d \n",
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
1000                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
1001                  fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",                  fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
1002                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
1003                  fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",                  fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
1004                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
1005    #endif
1006    }
1007    
1008  */  static void
1009    simplify_par(int *par_width, int *par_height)
1010    {
1011    
1012            int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
1013            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
1014            int divisor = gcd(_par_width, _par_height);
1015    
1016            _par_width  /= divisor;
1017            _par_height /= divisor;
1018    
1019            /* 2^8 precision maximum */
1020            if (_par_width>255 || _par_height>255) {
1021                    float div;
1022                    emms();
1023                    if (_par_width>_par_height)
1024                            div = (float)_par_width/255;
1025                    else
1026                            div = (float)_par_height/255;
1027    
1028                    _par_width  = (int)((float)_par_width/div);
1029                    _par_height = (int)((float)_par_height/div);
1030  }  }
1031    
1032            *par_width = _par_width;
1033            *par_height = _par_height;
1034    
1035            return;
1036    }
1037    
1038  /*****************************************************************************  /*****************************************************************************
1039   * IPB frame encoder entry point   * IPB frame encoder entry point
# Line 923  Line 1129 
1129                          }                          }
1130    
1131                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1132              call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1133                          pEnc->bframenum_head++;                          pEnc->bframenum_head++;
1134    
1135                          goto done;                          goto done;
# Line 949  Line 1155 
1155                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
1156                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
1157    
1158                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1159                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1160                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
1161    
1162                          /* add the not-coded length to the reference frame size */                          /* add the not-coded length to the reference frame size */
1163                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1164              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1165    
1166              /* flush complete: reset counters */              /* flush complete: reset counters */
1167                  pEnc->flush_bframes = 0;                  pEnc->flush_bframes = 0;
# Line 983  Line 1189 
1189                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1190    
1191              if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {              if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1192                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1193              }              }
1194    
1195              /* if the very last frame is to be b-vop, we must change it to a p-vop */              /* if the very last frame is to be b-vop, we must change it to a p-vop */
# Line 1006  Line 1212 
1212                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1213                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1214                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1215                                    pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1216    
1217                  FrameCodeP(pEnc, &bs, 1, 0);                                  FrameCodeP(pEnc, &bs);
1218    
1219    
1220                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1221                      call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1222                  }else{                  }else{
1223                      pEnc->flush_bframes = 1;                      pEnc->flush_bframes = 1;
1224                      goto done;                      goto done;
# Line 1041  Line 1248 
1248    
1249      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1250      inc_frame_num(pEnc);      inc_frame_num(pEnc);
1251      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;          pEnc->current->vol_flags = frame->vol_flags;
1252      pEnc->current->vop_flags = frame->vop_flags;      pEnc->current->vop_flags = frame->vop_flags;
1253          pEnc->current->motion_flags = frame->motion;          pEnc->current->motion_flags = frame->motion;
1254          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
# Line 1060  Line 1267 
1267          type = frame->type;          type = frame->type;
1268          pEnc->current->quant = frame->quant;          pEnc->current->quant = frame->quant;
1269    
1270      call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1271    
1272      if (type > 0){      /* XVID_TYPE_?VOP */      if (type > 0){      /* XVID_TYPE_?VOP */
1273                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
# Line 1071  Line 1278 
1278                  }else{                  }else{
1279                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1280                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1281                                          pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold);                                                            pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1282                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1283                  }                  }
1284          }          }
1285    
1286            if (type != I_VOP)
1287                    pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1288    
1289      /* bframes buffer overflow check */      /* bframes buffer overflow check */
1290      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {      if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1291          type = P_VOP;          type = P_VOP;
# Line 1131  Line 1342 
1342      if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)      if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1343      {      {
1344          if (pEnc->current->stamp > 0) {          if (pEnc->current->stamp > 0) {
1345              call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1346          }          }
1347                  else          else if (stats) {
1348                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
1349      }      }
1350            }
1351    
1352          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353           * closed-gop           * closed-gop
# Line 1159  Line 1371 
1371                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1372    
1373                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1374                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP");
1375                  }                  }
1376    
1377                  /* convert B-VOP quant to P-VOP */                  /* convert B-VOP quant to P-VOP */
# Line 1187  Line 1399 
1399                  pEnc->iFrameNum = 1;                  pEnc->iFrameNum = 1;
1400    
1401                  /* ---- update vol flags at IVOP ----------- */                  /* ---- update vol flags at IVOP ----------- */
1402                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;                  pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1403    
1404                    /* Aspect ratio */
1405                  switch(frame->par) {                  switch(frame->par) {
1406                  case XVID_PAR_11_VGA:                  case XVID_PAR_11_VGA:
1407                  case XVID_PAR_43_PAL:                  case XVID_PAR_43_PAL:
# Line 1198  Line 1412 
1412                          pEnc->mbParam.par = frame->par;                          pEnc->mbParam.par = frame->par;
1413                          break;                          break;
1414                  default:                  default:
1415                          pEnc->mbParam.par = XVID_PAR_EXT;                          pEnc->mbParam.par = XVID_PAR_11_VGA;
1416                          break;                          break;
1417                  }                  }
1418                  pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1;  
1419                  pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1;                  /* For extended PAR only, we try to sanityse/simplify par values */
1420                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1421                            pEnc->mbParam.par_width  = frame->par_width;
1422                            pEnc->mbParam.par_height = frame->par_height;
1423                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1424                    }
1425    
1426          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {          if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1427                          if (frame->quant_intra_matrix != NULL)                          if (frame->quant_intra_matrix != NULL)
1428                                  set_intra_matrix(frame->quant_intra_matrix);                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1429                          if (frame->quant_inter_matrix != NULL)                          if (frame->quant_inter_matrix != NULL)
1430                                  set_inter_matrix(frame->quant_inter_matrix);                                  set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1431                  }                  }
1432    
1433          /* prevent vol/vop misuse */          /* prevent vol/vop misuse */
1434    
         if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))  
             pEnc->current->vop_flags &= ~XVID_VOP_REDUCED;  
   
1435          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))          if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1436              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);              pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1437    
# Line 1248  Line 1464 
1464                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1465          }          }
1466    
1467                  FrameCodeP(pEnc, &bs, 1, 0);                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1468                            /* N-VOP, we mustn't code b-frames yet */
1469                            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1470                                     pEnc->mbParam.max_bframes == 0)
1471                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1472                            goto done;
1473                    }
1474      }      }
1475    
1476    
# Line 1267  Line 1489 
1489    
1490      /* packed or no-bframes or no-bframes-queued: output stats */      /* packed or no-bframes or no-bframes-queued: output stats */
1491      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1492          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1493          }          }
1494    
1495          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Line 1286  Line 1508 
1508    
1509  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1510  {  {
1511      unsigned int i,j;          unsigned int i;
1512      int quant = frame->quant;          MACROBLOCK * pMB = frame->mbs;
1513            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1514      if (quant > 31)      if (quant > 31)
1515                  frame->quant = quant = 31;                  frame->quant = quant = 31;
1516          else if (quant < 1)          else if (quant < 1)
1517                  frame->quant = quant = 1;                  frame->quant = quant = 1;
1518    
1519      for (j=0; j<pParam->mb_height; j++)          for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
     for (i=0; i<pParam->mb_width; i++) {  
         MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i];  
1520          quant += pMB->dquant;          quant += pMB->dquant;
1521          if (quant > 31)          if (quant > 31)
1522                          quant = 31;                          quant = 31;
1523                  else if (quant < 1)                  else if (quant < 1)
1524                          quant = 1;                          quant = 1;
1525          pMB->quant = quant;          pMB->quant = quant;
1526                    pMB++;
1527      }      }
1528  }  }
1529    
1530    
1531  static __inline void  static __inline void
1532  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(MACROBLOCK * pMB)
                         MACROBLOCK * pMB)  
1533  {  {
   
1534          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1535    
1536          /* zero mv statistics */          /* zero mv statistics */
# Line 1324  Line 1544 
1544      }      }
1545  }  }
1546    
1547    static void
1548    SliceCodeI(SMPData *data)
 static int  
 FrameCodeI(Encoder * pEnc,  
                    Bitstream * bs)  
1549  {  {
1550      int bits = BitstreamPos(bs);          Encoder *pEnc = (Encoder *) data->pEnc;
1551            Bitstream *bs = (Bitstream *) data->bs;
1552    
1553            uint16_t x, y;
1554          int mb_width = pEnc->mbParam.mb_width;          int mb_width = pEnc->mbParam.mb_width;
1555          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1556    
1557            int bound = 0, num_slices = pEnc->num_slices;
1558            FRAMEINFO *const current = pEnc->current;
1559    
1560          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1561          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1562    
1563          uint16_t x, y;          if (data->start_y > 0) { /* write resync marker */
1564                    bound = data->start_y*mb_width;
1565                    write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1566            }
1567    
1568          if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE))          for (y = data->start_y; y < data->stop_y; y++) {
1569          {                  int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1570                  mb_width = (pEnc->mbParam.width + 31) / 32;  
1571                  mb_height = (pEnc->mbParam.height + 31) / 32;                  if (new_bound > bound) {
1572                            bound = new_bound;
1573                            BitstreamPadAlways(bs);
1574                            write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1575                    }
1576    
1577                    for (x = 0; x < mb_width; x++) {
1578                            MACROBLOCK *pMB = &current->mbs[x + y * mb_width];
1579    
1580                            CodeIntraMB(pMB);
1581    
1582                            MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1583                                                              dct_codes, qcoeff);
1584    
                 /* 16x16->8x8 downsample requires 1 additional edge pixel*/  
                 /* XXX: setedges is overkill */  
1585                  start_timer();                  start_timer();
1586                  image_setedges(&pEnc->current->image,                          MBPrediction(current, x, y, mb_width, qcoeff, bound);
1587                          pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                          stop_prediction_timer();
1588                          pEnc->mbParam.width, pEnc->mbParam.height);  
1589                  stop_edges_timer();                          start_timer();
1590                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1591                            stop_coding_timer();
1592    
1593                    }
1594            }
1595    
1596            emms();
1597            BitstreamPadAlways(bs);
1598    }
1599    
1600    static __inline void
1601    SerializeBitstreams(Encoder *pEnc, FRAMEINFO *current, Bitstream *bs, int num_threads)
1602    {
1603            int k;
1604            uint32_t pos = BitstreamLength(bs);
1605    
1606            for (k = 1; k < num_threads; k++) {
1607                    uint32_t len = BitstreamLength(pEnc->smpData[k].bs);
1608    
1609                    memcpy((void *)((ptr_t)bs->start + pos),
1610                               (void *)((ptr_t)pEnc->smpData[k].bs->start), len);
1611    
1612                    current->length = pos += len;
1613    
1614                    /* collect stats */
1615                    current->sStat.iTextBits += pEnc->smpData[k].sStat->iTextBits;
1616                    current->sStat.kblks += pEnc->smpData[k].sStat->kblks;
1617                    current->sStat.mblks += pEnc->smpData[k].sStat->mblks;
1618                    current->sStat.ublks += pEnc->smpData[k].sStat->ublks;
1619                    current->sStat.iMVBits += pEnc->smpData[k].sStat->iMVBits;
1620            }
1621    
1622            if (num_threads > 1) {
1623                    uint32_t pos32 = pos>>2;
1624                    bs->tail = bs->start + pos32;
1625                    bs->pos = 8*(pos - (pos32<<2));
1626                    bs->buf = 0;
1627    
1628                    if (bs->pos > 0) {
1629                            uint32_t pos8 = bs->pos/8;
1630                            memset((void *)((ptr_t)bs->tail+pos8), 0, (4-pos8));
1631                            pos = *bs->tail;
1632    #ifndef ARCH_IS_BIG_ENDIAN
1633                            BSWAP(pos);
1634    #endif
1635                            bs->buf = pos;
1636          }          }
1637            }
1638    }
1639    
1640    static int
1641    FrameCodeI(Encoder * pEnc,
1642                       Bitstream * bs)
1643    {
1644            int bits = BitstreamPos(bs);
1645            int bound = 0, num_slices = pEnc->num_slices;
1646            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1647            int slices_per_thread = (num_slices*1024 / num_threads);
1648            int mb_height = pEnc->mbParam.mb_height;
1649            void * status = NULL;
1650            uint16_t k;
1651    
1652          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1653          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1654          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1655    
1656            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1657    
1658      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);      SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1659    
1660          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current, num_slices);
1661    
1662          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1663    
1664          BitstreamPad(bs);          BitstreamPad(bs);
1665    
1666          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1667    
1668          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;  
1669    
1670          for (y = 0; y < mb_height; y++)          /* multithreaded intra coding - dispatch threads */
1671                  for (x = 0; x < mb_width; x++) {          for (k = 0; k < num_threads; k++) {
1672                          MACROBLOCK *pMB =                  int add = ((slices_per_thread + 512) >> 10);
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1673    
1674                          CodeIntraMB(pEnc, pMB);                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
1675    
1676                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                  pEnc->smpData[k].pEnc = (void *) pEnc;
1677                                                            dct_codes, qcoeff);                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
1678                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
1679    
1680                          start_timer();                  bound += add;
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1681    
1682                          start_timer();                  if (k > 0) {
1683                          if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)                          BitstreamReset(pEnc->smpData[k].bs);
1684                          {       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;  
1685                          }                          }
                         MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);  
                         stop_coding_timer();  
1686                  }                  }
1687            pEnc->smpData[0].bs = bs;
1688            pEnc->smpData[0].sStat = &pEnc->current->sStat;
1689    
1690          if ((pEnc->current->vop_flags & XVID_VOP_REDUCED))          /* create threads */
1691          {          for (k = 1; k < num_threads; k++) {
1692                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  pthread_create(&pEnc->smpData[k].handle, NULL,
1693                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                                 (void*)SliceCodeI, (void*)&pEnc->smpData[k]);
                         16, 0);  
1694          }          }
         emms();  
1695    
1696          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          SliceCodeI(&pEnc->smpData[0]);
1697    
1698      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          /* wait until all threads are finished */
1699            for (k = 1; k < num_threads; k++) {
1700                    pthread_join(pEnc->smpData[k].handle, &status);
1701            }
1702    
1703            pEnc->current->length = BitstreamLength(bs) - (bits/8);
1704    
1705            /* reassemble the pieces together */
1706            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
1707    
1708            pEnc->current->sStat.iMVBits = 0;
1709            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1710            pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1711    
1712          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1713          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1714    
1715            pEnc->current->is_edged = 0; /* not edged */
1716            pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1717    
1718          return 1;                                       /* intra */          return 1;                                       /* intra */
1719  }  }
1720    
1721    static __inline void
1722    updateFcode(Statistics * sStat, Encoder * pEnc)
1723    {
1724            float fSigma;
1725            int iSearchRange;
1726    
1727            if (sStat->iMvCount == 0)
1728                    sStat->iMvCount = 1;
1729    
1730  #define INTRA_THRESHOLD 0.5          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
 #define BFRAME_SKIP_THRESHHOLD 30  
1731    
1732            iSearchRange = 16 << pEnc->mbParam.m_fcode;
1733    
1734  /* FrameCodeP also handles S(GMC)-VOPs */          if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1735  static int                  pEnc->mbParam.m_fcode++;
 FrameCodeP(Encoder * pEnc,  
                    Bitstream * bs,  
                    bool force_inter,  
                    bool vol_header)  
 {  
         float fSigma;  
     int bits = BitstreamPos(bs);  
1736    
1737          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          else if ((5.0 * fSigma < iSearchRange)
1738          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);                             && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1739                               && (pEnc->mbParam.m_fcode >= 2) )
1740                    pEnc->mbParam.m_fcode--;
1741    
1742            pEnc->fMvPrevSigma = fSigma;
1743    }
1744    
1745    #define BFRAME_SKIP_THRESHHOLD 30
1746    
1747    static void
1748    SliceCodeP(SMPData *data)
1749    {
1750            Encoder *pEnc = (Encoder *) data->pEnc;
1751            Bitstream *bs = (Bitstream *) data->bs;
1752    
         int iLimit;  
1753          int x, y, k;          int x, y, k;
         int iSearchRange;  
         int bIntra=0, skip_possible;  
1754          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1755          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1756          MBParam * const pParam = &pEnc->mbParam;          MBParam * const pParam = &pEnc->mbParam;
1757          int mb_width = pParam->mb_width;          int mb_width = pParam->mb_width;
1758          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1759    
1760            DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1761            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1762    
1763          /* IMAGE *pCurrent = &current->image; */          int bound = 0, num_slices = pEnc->num_slices;
         IMAGE *pRef = &reference->image;  
1764    
1765          if ((current->vop_flags & XVID_VOP_REDUCED))          if (data->start_y > 0) { /* write resync marker */
1766          {                  bound = data->start_y*mb_width;
1767                  mb_width = (pParam->width + 31) / 32;                  write_video_packet_header(bs, pParam, current, bound);
                 mb_height = (pParam->height + 31) / 32;  
1768          }          }
1769    
1770            for (y = data->start_y; y < data->stop_y; y++) {
1771                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1772    
1773          start_timer();                  if (new_bound > bound) {
1774          image_setedges(pRef, pParam->edged_width, pParam->edged_height,                          bound = new_bound;
1775                                     pParam->width, pParam->height);                          BitstreamPadAlways(bs);
1776          stop_edges_timer();                          write_video_packet_header(bs, pParam, current, bound);
1777                    }
1778    
1779          pParam->m_rounding_type = 1 - pParam->m_rounding_type;                  for (x = 0; x < mb_width; x++) {
1780          current->rounding_type = pParam->m_rounding_type;                          MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1781          current->fcode = pParam->m_fcode;                          int skip_possible;
1782    
1783          if (!force_inter)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1784                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);                                  CodeIntraMB(pMB);
1785          else                                  MBTransQuantIntra(pParam, current, pMB, x, y,
1786                  iLimit = mb_width * mb_height + 1;                                                                    dct_codes, qcoeff);
1787    
         if ((current->vop_flags & XVID_VOP_HALFPEL)) {  
1788                  start_timer();                  start_timer();
1789                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff, bound);
1790                                                    &pEnc->vInterHV, pParam->edged_width,                                  stop_prediction_timer();
1791                                                    pParam->edged_height,  
1792                                                    (pParam->vol_flags & XVID_VOL_QUARTERPEL),                                  data->sStat->kblks++;
1793                                                    current->rounding_type);  
1794                  stop_inter_timer();                                  MBCoding(current, pMB, qcoeff, bs, data->sStat);
1795                                    stop_coding_timer();
1796                                    continue;
1797          }          }
1798    
1799          current->coding_type = P_VOP;                          start_timer();
1800                            MBMotionCompensation(pMB, x, y, &reference->image,
1801                                                                     &pEnc->vInterH, &pEnc->vInterV,
1802                                                                     &pEnc->vInterHV, &pEnc->vGMC,
1803                                                                     &current->image,
1804                                                                     dct_codes, pParam->width,
1805                                                                     pParam->height,
1806                                                                     pParam->edged_width,
1807                                                                     (current->vol_flags & XVID_VOL_QUARTERPEL),
1808                                                                     current->rounding_type,
1809                                                                     data->RefQ);
1810    
1811                            stop_comp_timer();
1812    
1813      SetMacroblockQuants(&pEnc->mbParam, current);                          pMB->field_pred = 0;
1814    
1815          start_timer();                          if (pMB->cbp != 0) {
1816          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */                                  pMB->cbp = MBTransQuantInter(pParam, current, pMB, x, y,
1817          {       int gmcval;                                                               dct_codes, qcoeff);
1818                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,                          }
                                                                  &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);  
1819    
1820                  if (current->motion_flags & XVID_ME_GME_REFINE) {                          if (pMB->dquant != 0)
1821                                    MBSetDquant(pMB, x, y, pParam);
1822    
1823    
1824                            if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1825                                       pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1826                                       pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1827                                    data->sStat->mblks++;
1828                            }  else {
1829                                    data->sStat->ublks++;
1830                            }
1831    
1832                            start_timer();
1833    
1834                            /* Finished processing the MB, now check if to CODE or SKIP */
1835    
1836                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
1837    
1838                            if (current->coding_type == S_VOP)
1839                                    skip_possible &= (pMB->mcsel == 1);
1840                            else { /* PVOP */
1841                                    const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1842                                                                                    pMB->qmvs : pMB->mvs;
1843                                    skip_possible &= ((mv->x|mv->y) == 0);
1844                            }
1845    
1846                            if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1847                                    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1848                                    int bSkip = 1;
1849    
1850                                    if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1851                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1852                                                    int iSAD;
1853                                                    iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1854                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1855                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1856                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) &&
1857                                                            ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after
1858                                                                                                                                                                               resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */
1859                                                            bSkip = 0; /* could not SKIP */
1860                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1861                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1862                                                                    pMB->pmvs[0].x = - predMV.x;
1863                                                                    pMB->pmvs[0].y = - predMV.y;
1864                                                            } else {
1865                                                                    VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1866                                                                    pMB->pmvs[0].x = - predMV.x;
1867                                                                    pMB->pmvs[0].y = - predMV.y;
1868                                                            }
1869                                                            pMB->mode = MODE_INTER;
1870                                                            pMB->cbp = 0;
1871                                                            break;
1872                                                    }
1873                                            }
1874                                    }
1875    
1876                                    if (bSkip) {
1877                                            /* do SKIP */
1878                                            pMB->mode = MODE_NOT_CODED;
1879                                            MBSkip(bs);
1880                                            stop_coding_timer();
1881                                            continue;       /* next MB */
1882                                    }
1883                            }
1884    
1885                            /* ordinary case: normal coded INTER/INTER4V block */
1886                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1887                            stop_coding_timer();
1888                    }
1889            }
1890    
1891            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1892            emms();
1893    }
1894    
1895    /* FrameCodeP also handles S(GMC)-VOPs */
1896    static int
1897    FrameCodeP(Encoder * pEnc, Bitstream * bs)
1898    {
1899            int bits = BitstreamPos(bs);
1900    
1901            FRAMEINFO *const current = pEnc->current;
1902            FRAMEINFO *const reference = pEnc->reference;
1903            MBParam * const pParam = &pEnc->mbParam;
1904            int mb_width = pParam->mb_width;
1905            int mb_height = pParam->mb_height;
1906            int coded = 1;
1907    
1908            int k = 0, bound = 0, num_slices = pEnc->num_slices;
1909            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1910            void * status = NULL;
1911            int slices_per_thread = (num_slices*1024 / num_threads);
1912            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
1913    
1914            IMAGE *pRef = &reference->image;
1915    
1916            if (!reference->is_edged) {
1917                    start_timer();
1918                    image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1919                                               pParam->width, pParam->height, 0);
1920                    stop_edges_timer();
1921                    reference->is_edged = 1;
1922            }
1923    
1924            pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1925            current->rounding_type = pParam->m_rounding_type;
1926            current->fcode = pParam->m_fcode;
1927    
1928            if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1929                    if (reference->is_interpolated != current->rounding_type) {
1930                            start_timer();
1931                            image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1932                                                              pEnc->vInterHV.y, pParam->edged_width,
1933                                                              pParam->edged_height,
1934                                                              (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1935                                                              current->rounding_type);
1936                            stop_inter_timer();
1937                            reference->is_interpolated = current->rounding_type;
1938                    }
1939            }
1940    
1941            current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1942                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1943                    current->sStat.iMVBits = 0;
1944    
1945            current->coding_type = P_VOP;
1946    
1947            if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1948                    image_block_variance(&current->image, pParam->edged_width, current->mbs,
1949                                         pParam->mb_width, pParam->mb_height);
1950            }
1951    
1952            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1953    
1954            SetMacroblockQuants(&pEnc->mbParam, current);
1955    
1956            start_timer();
1957            if (current->vol_flags & XVID_VOL_GMC)  /* GMC only for S(GMC)-VOPs */
1958            {       int gmcval;
1959                    current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1960                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, num_slices);
1961    
1962                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1963                          gmcval = GlobalMotionEstRefine(&current->warp,                          gmcval = GlobalMotionEstRefine(&current->warp,
1964                                                                  current->mbs, pParam,                                                                  current->mbs, pParam,
1965                                                                  current, reference,                                                                  current, reference,
# Line 1496  Line 1968 
1968                                                                  &pEnc->vInterH,                                                                  &pEnc->vInterH,
1969                                                                  &pEnc->vInterV,                                                                  &pEnc->vInterV,
1970                                                                  &pEnc->vInterHV);                                                                  &pEnc->vInterHV);
1971                          gmcval += /*current->quant */ 2 * (int)(pParam->mb_width*pParam->mb_height);                  } else {
                 }  
   
1972                  gmcval = globalSAD(&current->warp, pParam, current->mbs,                  gmcval = globalSAD(&current->warp, pParam, current->mbs,
1973                                                          current,                                                          current,
1974                                                          &reference->image,                                                          &reference->image,
1975                                                          &current->image,                                                          &current->image,
1976                                                          pEnc->vGMC.y);                                                          pEnc->vGMC.y);
1977                    }
1978    
1979                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1980    
1981  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */  /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
# Line 1532  Line 2004 
2004                  }                  }
2005          }          }
2006    
2007          bIntra =          if (pEnc->num_threads > 0) {
                 MotionEstimation(&pEnc->mbParam, current, reference,  
                                          &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                          &pEnc->vGMC, iLimit);  
   
   
         stop_motion_timer();  
   
         if (bIntra == 1) return FrameCodeI(pEnc, bs);  
   
         set_timecodes(current,reference,pParam->fbase);  
         if (vol_header)  
         {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);  
                 BitstreamPad(bs);  
         }  
   
         BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1);  
   
         current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =  
                 current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;  
   
2008    
2009          for (y = 0; y < mb_height; y++) {                  /* multithreaded motion estimation - dispatch threads */
2010                  for (x = 0; x < mb_width; x++) {                  while (k < pEnc->num_threads) {
2011                          MACROBLOCK *pMB =                          int i, add_s = (slices_per_thread + 512) >> 10;
2012                                  &current->mbs[x + y * pParam->mb_width];                          int add_t = (threads_per_slice + 512) >> 10;
2013    
2014                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2015                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2016                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2017    
2018                          if (bIntra) {                          slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2019                                  CodeIntraMB(pEnc, pMB);                          threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
                                 MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
2020    
2021                                  start_timer();                          for (i = 0; i < add_t; i++) {
2022                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);                                  memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
                                 stop_prediction_timer();  
2023    
2024                                  current->sStat.kblks++;                                  pEnc->smpData[k+i].pEnc = (void *) pEnc;
2025                                    pEnc->smpData[k+i].y_row = i;
2026                                    pEnc->smpData[k+i].y_step = add_t;
2027                                    pEnc->smpData[k+i].stop_y = stop_y;
2028                                    pEnc->smpData[k+i].start_y = start_y;
2029    
2030                                  if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE)                                  /* todo: sort out temp space once and for all */
2031                                  {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                                  pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2032                                          qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */                                                                                          16*((k+i)>>1)*pParam->edged_width;
                                         qcoeff[5*64+0]=0;  
                                 }  
                                 MBCoding(current, pMB, qcoeff, bs, &current->sStat);  
                                 stop_coding_timer();  
                                 continue;  
2033                          }                          }
2034    
2035                          start_timer();                          pEnc->smpData[k].complete_count_above =
2036                          MBMotionCompensation(pMB, x, y, &reference->image,                                  pEnc->smpData[k+add_t-1].complete_count_self - 1;
                                                                  &pEnc->vInterH, &pEnc->vInterV,  
                                                                  &pEnc->vInterHV, &pEnc->vGMC,  
                                                                  &current->image,  
                                                                  dct_codes, pParam->width,  
                                                                  pParam->height,  
                                                                  pParam->edged_width,  
                                                                  (current->vol_flags & XVID_VOL_QUARTERPEL),  
                                                                  (current->vop_flags & XVID_VOP_REDUCED),  
                                                                  current->rounding_type);  
2037    
2038                          stop_comp_timer();                          bound += add_s;
2039                            k += add_t;
2040                    }
2041    
2042                          if (pMB->dquant != 0) {                  for (k = 1; k < pEnc->num_threads; k++) {
2043                  pMB->mode = MODE_INTER_Q;                          pthread_create(&pEnc->smpData[k].handle, NULL,
2044                                    (void*)MotionEstimateSMP, (void*)&pEnc->smpData[k]);
2045                          }                          }
2046    
2047                          pMB->field_pred = 0;                  MotionEstimateSMP(&pEnc->smpData[0]);
2048    
2049                          if (pMB->mode != MODE_NOT_CODED)                  for (k = 1; k < pEnc->num_threads; k++) {
2050                          {       pMB->cbp =                          pthread_join(pEnc->smpData[k].handle, &status);
                                         MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,  
                                                                           dct_codes, qcoeff);  
2051                          }                          }
2052    
2053                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                  current->fcode = 0;
2054                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                  for (k = 0; k < pEnc->num_threads; k++) {
2055                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                          current->sStat.iMvSum += pEnc->smpData[k].mvSum;
2056                                  current->sStat.mblks++;                          current->sStat.iMvCount += pEnc->smpData[k].mvCount;
2057                          }  else {                          if (pEnc->smpData[k].minfcode > current->fcode)
2058                                  current->sStat.ublks++;                                  current->fcode = pEnc->smpData[k].minfcode;
2059                          }                          }
2060    
2061                          start_timer();          } else {
2062    
2063                          /* Finished processing the MB, now check if to CODE or SKIP */                  /* regular ME */
2064    
2065                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                  MotionEstimation(&pEnc->mbParam, current, reference,
2066                                                          (pMB->dquant == 0);                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2067                                                     &pEnc->vGMC, 256*4096, num_slices);
2068    
                         if (current->coding_type == S_VOP)  
                                 skip_possible &= (pMB->mcsel == 1);  
                         else if (current->coding_type == P_VOP) {  
                                 if ((pParam->vol_flags & XVID_VOL_QUARTERPEL))  
                                         skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );  
                                 else  
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
2069                          }                          }
2070    
2071                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {          stop_motion_timer();
   
 /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */  
   
                                 if (current->coding_type == P_VOP)      /* special rule for P-VOP's SKIP */  
                                 {  
                                         int bSkip = 1;  
   
                                         for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)  
                                         {  
                                                 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;  
                                                 }  
                                         }  
2072    
2073                                          if (!bSkip) {   /* no SKIP, but trivial block */          set_timecodes(current,reference,pParam->fbase);
                                                 if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {  
                                                         VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                                         pMB->pmvs[0].x = - predMV.x;  
                                                         pMB->pmvs[0].y = - predMV.y;  
                                                 }  
                                                 else {  
                                                         VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                                         pMB->pmvs[0].x = - predMV.x;  
                                                         pMB->pmvs[0].y = - predMV.y;  
                                                 }  
                                                 pMB->mode = MODE_INTER;  
                                                 pMB->cbp = 0;  
                                                 MBCoding(current, pMB, qcoeff, bs, &current->sStat);  
                                                 stop_coding_timer();  
2074    
2075                                                  continue;       /* next MB */          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
                                         }  
                                 }  
                                 /* do SKIP */  
2076    
2077                                  pMB->mode = MODE_NOT_CODED;          /* multithreaded inter coding - dispatch threads */
                                 MBSkip(bs);  
                                 stop_coding_timer();  
                                 continue;       /* next MB */  
                         }  
                         /* ordinary case: normal coded INTER/INTER4V block */  
2078    
2079                          if ((current->vop_flags & XVID_VOP_GREYSCALE))          bound = 0;
2080                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */          slices_per_thread = (num_slices*1024 / num_threads);
                                 qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
                         }  
2081    
2082                          if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) {          for (k = 0; k < num_threads; k++) {
2083                                  VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);                  int add = ((slices_per_thread + 512) >> 10);
                                 pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  
                                 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);  
                         }  
2084    
2085                    slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2086    
2087                          if (pMB->mode == MODE_INTER4V)                  pEnc->smpData[k].pEnc = (void *) pEnc;
2088                          {       int k;                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2089                                  for (k=1;k<4;k++)                  pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2090                                  {                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
                                         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);  
                                         }  
2091    
2092                                  }                  bound += add;
                         }  
2093    
2094                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);                  if (k > 0) {
2095                          stop_coding_timer();                          pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2096                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks =
2097                            pEnc->smpData[k].sStat->iMVBits = 0;
2098    
2099                            BitstreamReset(pEnc->smpData[k].bs);
2100                  }                  }
2101          }          }
2102            pEnc->smpData[0].bs = bs;
2103            pEnc->smpData[0].sStat = &current->sStat;
2104    
2105          if ((current->vop_flags & XVID_VOP_REDUCED))          /* create threads */
2106          {          for (k = 1; k < num_threads; k++) {
2107                  image_deblock_rrv(&current->image, pParam->edged_width,                  pthread_create(&pEnc->smpData[k].handle, NULL,
2108                          current->mbs, mb_width, mb_height, pParam->mb_width,                          (void*)SliceCodeP, (void*)&pEnc->smpData[k]);
                         16, 0);  
2109          }          }
2110    
2111          emms();          SliceCodeP(&pEnc->smpData[0]);
2112    
2113          if (current->sStat.iMvCount == 0)          /* wait until all threads are finished */
2114                  current->sStat.iMvCount = 1;          for (k = 1; k < num_threads; k++) {
2115                    pthread_join(pEnc->smpData[k].handle, &status);
2116            }
2117    
2118          fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount);          current->length = BitstreamLength(bs) - (bits/8);
2119    
2120          iSearchRange = 1 << (3 + pParam->m_fcode);          /* reassemble the pieces together */
2121            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
2122    
2123          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;  
2124    
2125          /* frame drop code */          /* frame drop code */
2126  #if 0  #if 0
2127          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);
2128  #endif  #endif
2129    
2130          if (current->sStat.kblks + current->sStat.mblks <          if (current->sStat.kblks + current->sStat.mblks <
2131                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100)                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
2132                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
2133          {          {
2134                  current->sStat.kblks = current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
2135                  current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
2136    
2137                  BitstreamReset(bs);                  BitstreamReset(bs);
2138    
2139                  set_timecodes(current,reference,pParam->fbase);                  set_timecodes(current,reference,pParam->fbase);
2140                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
2141    
2142                  /* copy reference frame details into the current frame */                  /* copy reference frame details into the current frame */
2143                  current->quant = reference->quant;                  current->quant = reference->quant;
# Line 1777  Line 2145 
2145                  current->rounding_type = reference->rounding_type;                  current->rounding_type = reference->rounding_type;
2146                  current->fcode = reference->fcode;                  current->fcode = reference->fcode;
2147                  current->bcode = reference->bcode;                  current->bcode = reference->bcode;
2148                    current->stamp = reference->stamp;
2149                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);                  image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
2150                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2151                    coded = 0;
2152    
2153                    BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2154    
2155                    current->length = (BitstreamPos(bs) - bits) / 8;
2156    
2157            } else {
2158    
2159                    pEnc->current->is_edged = 0; /* not edged */
2160                    pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
2161    
2162                    /* what was this frame's interpolated reference will become
2163                            forward (past) reference in b-frame coding */
2164    
2165                    image_swap(&pEnc->vInterH, &pEnc->f_refh);
2166                    image_swap(&pEnc->vInterV, &pEnc->f_refv);
2167                    image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
2168          }          }
2169    
2170          /* XXX: debug          /* XXX: debug
# Line 1796  Line 2182 
2182          }          }
2183          */          */
2184    
2185          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          return coded;
2186    }
2187    
2188      current->length = (BitstreamPos(bs) - bits) / 8;  static void
2189    SliceCodeB(SMPData *data)
2190    {
2191            Encoder *pEnc = (Encoder *) data->pEnc;
2192            Bitstream *bs = (Bitstream *) data->bs;
2193    
2194          return 0;                                       /* inter */          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2195            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2196    
2197            int x, y;
2198            FRAMEINFO * const frame = (FRAMEINFO * const) data->current;
2199            MBParam * const pParam = &pEnc->mbParam;
2200            int mb_width = pParam->mb_width;
2201            int mb_height = pParam->mb_height;
2202            IMAGE *f_ref = &pEnc->reference->image;
2203            IMAGE *b_ref = &pEnc->current->image;
2204    
2205            int bound = data->start_y*mb_width;
2206            int num_slices = pEnc->num_slices;
2207    
2208            if (data->start_y > 0) { /* write resync marker */
2209                    write_video_packet_header(bs, pParam, frame, bound+1);
2210            }
2211    
2212            for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) {
2213                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
2214                    int stop_x = (y == data->stop_y) ? 1 : mb_width;
2215                    int start_x = (y == data->start_y && y > 0) ? 1 : 0;
2216    
2217                    for (x = start_x; x < stop_x; x++) {
2218                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2219    
2220                            /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2221                            if (mb->mode == MODE_NOT_CODED) {
2222                                    if (pParam->plugin_flags & XVID_REQORIGINAL) {
2223                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2224                                                                                     NULL, 0, 0, pParam->edged_width, 0, 0, data->RefQ);
2225                                    }
2226                                    continue;
2227  }  }
2228    
2229                            if (new_bound > bound && x > 0) {
2230                                    bound = new_bound;
2231                                    BitstreamPadAlways(bs);
2232                                    write_video_packet_header(bs, pParam, frame, y*mb_width+x);
2233                            }
2234    
2235                            mb->quant = frame->quant;
2236    
2237                            if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) {
2238                                    /* we have to motion-compensate, transfer etc,
2239                                            because there might be blocks to code */
2240    
2241                                    MBMotionCompensationBVOP(pParam, mb, x, y, &frame->image,
2242                                                                                     f_ref, &pEnc->f_refh, &pEnc->f_refv,
2243                                                                                     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2244                                                                                     &pEnc->vInterV, &pEnc->vInterHV, dct_codes,
2245                                                                                     data->RefQ);
2246    
2247                                    mb->cbp = MBTransQuantInterBVOP(pParam, frame, mb, x, y,  dct_codes, qcoeff);
2248                            }
2249    
2250                            if (mb->mode == MODE_DIRECT_NO4V)
2251                                    mb->mode = MODE_DIRECT;
2252    
2253                            if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2254                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2255                            else
2256                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2257                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2258                                            mb->cbp &= 0x3C;
2259    
2260                            start_timer();
2261                            MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, data->sStat);
2262                            stop_coding_timer();
2263                    }
2264            }
2265    
2266            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2267            emms();
2268    }
2269    
2270  static void  static void
2271  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
# Line 1810  Line 2273 
2273                     Bitstream * bs)                     Bitstream * bs)
2274  {  {
2275      int bits = BitstreamPos(bs);      int bits = BitstreamPos(bs);
2276          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          int k = 0, bound = 0, num_slices = pEnc->num_slices;
2277          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
2278          uint32_t x, y;          void * status = NULL;
2279            int slices_per_thread = (num_slices*1024 / num_threads);
2280            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
2281    
2282          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2283          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2284    
2285            MBParam * const pParam = &pEnc->mbParam;
2286            int mb_height = pParam->mb_height;
2287    
2288      #ifdef BFRAMES_DEC_DEBUG      #ifdef BFRAMES_DEC_DEBUG
2289          FILE *fp;          FILE *fp;
2290          static char first=0;          static char first=0;
# Line 1824  Line 2292 
2292                  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); \
2293          }          }
2294    
         /* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED;  reduced resoltion not yet supported */  
   
2295          if (!first){          if (!first){
2296                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2297          }          }
2298  #endif  #endif
2299    
2300          /* forward  */          /* forward  */
2301            if (!pEnc->reference->is_edged) {
2302          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2303                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2304                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
2305                    pEnc->current->is_edged = 1;
2306            }
2307    
2308            if (pEnc->reference->is_interpolated != 0) {
2309          start_timer();          start_timer();
2310          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,
2311                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2312                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2313          stop_inter_timer();          stop_inter_timer();
2314                    pEnc->reference->is_interpolated = 0;
2315            }
2316    
2317          /* backward */          /* backward */
2318            if (!pEnc->current->is_edged) {
2319          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2320                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2321                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, 0);
2322                    pEnc->current->is_edged = 1;
2323            }
2324    
2325            if (pEnc->current->is_interpolated != 0) {
2326          start_timer();          start_timer();
2327          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
2328                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2329                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);                                            (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2330          stop_inter_timer();          stop_inter_timer();
2331                    pEnc->current->is_interpolated = 0;
2332            }
2333    
2334            frame->coding_type = B_VOP;
2335    
2336            if (pEnc->current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
2337                    image_block_variance(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->current->mbs,
2338                                         pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2339            }
2340    
2341            call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2342    
2343            frame->fcode = frame->bcode = pEnc->current->fcode;
2344    
2345          start_timer();          start_timer();
2346    
2347            if (pEnc->num_threads > 0) {
2348    
2349                    /* multithreaded motion estimation - dispatch threads */
2350                    while (k < pEnc->num_threads) {
2351                            int i, add_s = (slices_per_thread + 512) >> 10;
2352                            int add_t = (threads_per_slice + 512) >> 10;
2353    
2354                            int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2355                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2356                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2357    
2358                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2359                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2360    
2361                            for (i = 0; i < add_t; i++) {
2362                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2363    
2364                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2365                                    pEnc->smpData[k+i].current = frame;
2366    
2367                                    pEnc->smpData[k+i].y_row = i;
2368                                    pEnc->smpData[k+i].y_step = add_t;
2369                                    pEnc->smpData[k+i].stop_y = stop_y;
2370                                    pEnc->smpData[k+i].start_y = start_y;
2371    
2372                                    /* todo: sort out temp space once and for all */
2373                                    pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2374                                                                                            16*((k+i)>>1)*pParam->edged_width;
2375                            }
2376    
2377                            pEnc->smpData[k].complete_count_above =
2378                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2379    
2380                            bound += add_s;
2381                            k += add_t;
2382                    }
2383    
2384                    for (k = 1; k < pEnc->num_threads; k++) {
2385                            pthread_create(&pEnc->smpData[k].handle, NULL,
2386                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->smpData[k]);
2387                    }
2388    
2389                    SMPMotionEstimationBVOP(&pEnc->smpData[0]);
2390    
2391                    for (k = 1; k < pEnc->num_threads; k++) {
2392                            pthread_join(pEnc->smpData[k].handle, &status);
2393                    }
2394    
2395                    frame->fcode = frame->bcode = 0;
2396                    for (k = 0; k < pEnc->num_threads; k++) {
2397                            if (pEnc->smpData[k].minfcode > frame->fcode)
2398                                    frame->fcode = pEnc->smpData[k].minfcode;
2399                            if (pEnc->smpData[k].minbcode > frame->bcode)
2400                                    frame->bcode = pEnc->smpData[k].minbcode;
2401                    }
2402            } else {
2403    
2404          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2405                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2406                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2407                                                   pEnc->reference->mbs, f_ref,                                                   pEnc->reference->mbs, f_ref,
2408                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2409                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2410                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                           &pEnc->vInterV, &pEnc->vInterHV,
2411                                                             pEnc->num_slices);
2412            }
2413          stop_motion_timer();          stop_motion_timer();
2414    
         frame->coding_type = B_VOP;  
   
2415          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2416          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2417    
2418            /* reset stats */
2419          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2420            frame->sStat.iMVBits = 0;
2421          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2422          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2423          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2424          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2425          frame->sStat.kblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.ublks = 0;
2426    
2427          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          /* multithreaded inter coding - dispatch threads */
2428                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          bound = 0;
2429                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];          slices_per_thread = (num_slices*1024 / num_threads);
2430    
2431                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */          for (k = 0; k < num_threads; k++) {
2432                          if (mb->mode == MODE_NOT_CODED) {                  int add = ((slices_per_thread + 512) >> 10);
                                 /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */  
                                 continue;  
                         }  
2433    
2434                          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);  
2435    
2436                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                  pEnc->smpData[k].pEnc = (void *) pEnc;
2437                                  mb->quant = frame->quant;                  pEnc->smpData[k].current = frame;
2438                    pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2439                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2440                    bound += add;
2441    
2442                                  if (mb->mode != MODE_DIRECT_NONE_MV)                  /* todo: sort out temp space once and for all */
2443                                          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;
2444    
2445                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                  if (k > 0) {
2446                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                          BitstreamReset(pEnc->smpData[k].bs);
2447                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */                          pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2448                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks = pEnc->smpData[k].sStat->iMVBits = 0;
2449                                  }                                  }
2450                          }                          }
2451    
2452                          /* keep only bits 5-2 -- Chroma blocks will just be skipped by the          for (k = 1; k < num_threads; k++) {
2453                           * coding function for BFrames, that's why we don't zero teh DC                  pthread_create(&pEnc->smpData[k].handle, NULL,
2454                           * coeffs */                          (void*)SliceCodeB, (void*)&pEnc->smpData[k]);
                         if ((frame->vop_flags & XVID_VOP_GREYSCALE))  
                                 mb->cbp &= 0x3C;  
   
                         start_timer();  
                         MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,  
                                                  &frame->sStat);  
                         stop_coding_timer();  
                 }  
2455          }          }
2456    
2457          emms();          pEnc->smpData[0].bs = bs;
2458            pEnc->smpData[0].sStat = &frame->sStat;
2459            SliceCodeB(&pEnc->smpData[0]);
2460    
2461          /* TODO: dynamic fcode/bcode ??? */          for (k = 1; k < num_threads; k++) {
2462                    pthread_join(pEnc->smpData[k].handle, &status);
2463            }
2464    
2465      BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          frame->length = BitstreamLength(bs) - (bits/8);
2466          frame->length = (BitstreamPos(bs) - bits) / 8;  
2467            /* reassemble the pieces together */
2468            SerializeBitstreams(pEnc, frame, bs, num_threads);
2469    
2470  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2471          if (!first){          if (!first){

Legend:
Removed from v.1.95.2.46  
changed lines
  Added in v.1.136

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