[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, Wed Feb 19 21:30:52 2003 UTC revision 1.95.2.4, Thu Mar 13 11:07:20 2003 UTC
# Line 58  Line 58 
58   * Local macros   * Local macros
59   ****************************************************************************/   ****************************************************************************/
60    
 #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  
61  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
62    
63  /*****************************************************************************  /*****************************************************************************
# Line 66  Line 65 
65   ****************************************************************************/   ****************************************************************************/
66    
67  static int FrameCodeI(Encoder * pEnc,  static int FrameCodeI(Encoder * pEnc,
68                                            Bitstream * bs,                                            Bitstream * bs);
                                           uint32_t * pBits);  
69    
70  static int FrameCodeP(Encoder * pEnc,  static int FrameCodeP(Encoder * pEnc,
71                                            Bitstream * bs,                                            Bitstream * bs,
                                           uint32_t * pBits,  
72                                            bool force_inter,                                            bool force_inter,
73                                            bool vol_header);                                            bool vol_header);
74    
75  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
76                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
77                                             Bitstream * bs,                                             Bitstream * bs);
                                            uint32_t * pBits);  
78    
79  /*****************************************************************************  /*****************************************************************************
80   * Local data   * Local data
# Line 104  Line 100 
100   * and cleaning code.   * and cleaning code.
101   *   *
102   * Returned values :   * Returned values :
103   *    - XVID_ERR_OK     - no errors   *    - 0                - no errors
104   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
105   *                        cleans the structure before exiting.   *                        cleans the structure before exiting.
106   *                        pParam->handle is also set to NULL.   *                        pParam->handle is also set to NULL.
107   *   *
108   ****************************************************************************/   ****************************************************************************/
109    
 int  
 encoder_create(XVID_ENC_PARAM * pParam)  
 {  
         Encoder *pEnc;  
         int i;  
   
         pParam->handle = NULL;  
   
         ENC_CHECK(pParam);  
   
         ENC_CHECK(pParam->width > 0 && pParam->width <= 1920);  
         ENC_CHECK(pParam->height > 0 && pParam->height <= 1280);  
         ENC_CHECK(!(pParam->width % 2));  
         ENC_CHECK(!(pParam->height % 2));  
   
         /* Fps */  
   
         if (pParam->fincr <= 0 || pParam->fbase <= 0) {  
                 pParam->fincr = 1;  
                 pParam->fbase = 25;  
         }  
   
110          /*          /*
111           * Simplify the "fincr/fbase" fraction           * Simplify the "fincr/fbase" fraction
          * (neccessary, since windows supplies us with huge numbers)  
112           */           */
113    static void
114          i = pParam->fincr;  simplify_time(int *inc, int *base)
115    {
116            /* common factor */
117            int i = *inc;
118          while (i > 1) {          while (i > 1) {
119                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {                  if (*inc % i == 0 && *base % i == 0) {
120                          pParam->fincr /= i;                          *inc /= i;
121                          pParam->fbase /= i;                          *base /= i;
122                          i = pParam->fincr;                          i = *inc;
123                          continue;                          continue;
124                  }                  }
125                  i--;                  i--;
126          }          }
127    
128          if (pParam->fbase > 65535) {          /* if neccessary, round to 65535 accuracy */
129                  float div = (float) pParam->fbase / 65535;          if (*base > 65535) {
130                    float div = (float) *base / 65535;
131                  pParam->fbase = (int) (pParam->fbase / div);                  *base = (int) (*base / div);
132                  pParam->fincr = (int) (pParam->fincr / div);                  *inc = (int) (*inc / div);
133            }
134          }          }
135    
         /* Bitrate allocator defaults */  
136    
137          if (pParam->rc_bitrate <= 0)  int
138                  pParam->rc_bitrate = 900000;  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)
139    {
140            Encoder *pEnc;
141        int n;
142    
143          if (pParam->rc_reaction_delay_factor <= 0)          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1))   /* v1.x.x */
144                  pParam->rc_reaction_delay_factor = 16;                  return XVID_ERR_VERSION;
145    
146          if (pParam->rc_averaging_period <= 0)          if (create->width%2 || create->height%2)
147                  pParam->rc_averaging_period = 100;                  return XVID_ERR_FAIL;
148    
149          if (pParam->rc_buffer <= 0)          /* allocate encoder struct */
                 pParam->rc_buffer = 100;  
150    
151          /* Max and min quantizers */          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
152            if (pEnc == NULL)
153                    return XVID_ERR_MEMORY;
154            memset(pEnc, 0, sizeof(Encoder));
155    
156          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          /* global flags */
157                  pParam->min_quantizer = 1;      pEnc->mbParam.global_flags = create->global;
158    
159          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))      /* width, height */
160                  pParam->max_quantizer = 31;          pEnc->mbParam.width = create->width;
161            pEnc->mbParam.height = create->height;
162            pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
163            pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
164            pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
165            pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
166    
167          if (pParam->max_quantizer < pParam->min_quantizer)      /* framerate */
168                  pParam->max_quantizer = pParam->min_quantizer;      pEnc->mbParam.fincr = MAX(create->fincr, 0);
169            pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
170            simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
171    
172          /* 1 keyframe each 10 seconds */      /* plugin */
173        pEnc->num_plugins = create->num_plugins;
174        pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
175        if (pEnc->plugins == NULL)
176            goto xvid_err_memory0;
177    
178          if (pParam->max_key_interval <= 0)      for (n=0; n<pEnc->num_plugins;n++) {
179                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;          xvid_plg_create_t pcreate;
180            xvid_plg_info_t pinfo;
181    
182          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          memset(&pinfo, 0, sizeof(xvid_plg_info_t));
183          if (pEnc == NULL)          pinfo.version = XVID_VERSION;
184                  return XVID_ERR_MEMORY;          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {
185                pEnc->plugin_flags |= pinfo.flags;
186            }
187    
188          /* Zero the Encoder Structure */          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
189            pcreate.version = XVID_VERSION;
190            pcreate.width = pEnc->mbParam.width;
191            pcreate.height = pEnc->mbParam.height;
192            pcreate.fincr = pEnc->mbParam.fincr;
193            pcreate.fbase = pEnc->mbParam.fbase;
194            pcreate.param = create->plugins[n].param;
195    
196          memset(pEnc, 0, sizeof(Encoder));          pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
197            if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
198                pEnc->plugins[n].func = create->plugins[n].func;
199            }
200    
201          /* Fill members of Encoder structure */      }
202    
203          pEnc->mbParam.width = pParam->width;      /* temp dquants */
204          pEnc->mbParam.height = pParam->height;          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
205                                            pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
206        /* XXX: error checking */
207    
208          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;          /* bframes */
209          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
210            pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
211            pEnc->mbParam.bquant_offset = create->bquant_offset;
212    
213          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          /* frame drop ratio */
214          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
215    
216          pEnc->mbParam.fbase = pParam->fbase;      /* max keyframe interval */
217          pEnc->mbParam.fincr = pParam->fincr;      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval;
218        /*XXX: replace 250 hard code with "10seconds * framerate" */
219    
220          pEnc->mbParam.m_quant_type = H263_QUANT;          /* Bitrate allocator defaults
221    
222          pEnc->fMvPrevSigma = -1;          if (create->rc_bitrate <= 0)
223                    create->rc_bitrate = 900000;
224    
225            if (create->rc_reaction_delay_factor <= 0)
226                    create->rc_reaction_delay_factor = 16;
227    
228            if (create->rc_averaging_period <= 0)
229                    create->rc_averaging_period = 100;
230    
231            if (create->rc_buffer <= 0)
232                    create->rc_buffer = 100;
233    
234    
235    
236            if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))
237                    create->min_quantizer = 1;
238    
239          /* Fill rate control parameters */          if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))
240                    create->max_quantizer = 31;
241    
242          pEnc->bitrate = pParam->rc_bitrate;          if (create->max_quantizer < create->min_quantizer)
243                    create->max_quantizer = create->min_quantizer;
244    
245          pEnc->iFrameNum = -1;          pEnc->bitrate = create->rc_bitrate; */
         pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;  
246    
247          /* try to allocate frame memory */  
248        /* allocate working frame-image memory */
249    
250          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
251          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
# Line 227  Line 253 
253          if (pEnc->current == NULL || pEnc->reference == NULL)          if (pEnc->current == NULL || pEnc->reference == NULL)
254                  goto xvid_err_memory1;                  goto xvid_err_memory1;
255    
256          /* try to allocate mb memory */          /* allocate macroblock memory */
257    
258          pEnc->current->mbs =          pEnc->current->mbs =
259                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
# Line 239  Line 265 
265          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
266                  goto xvid_err_memory2;                  goto xvid_err_memory2;
267    
268          /* try to allocate image memory */          /* allocate interpolation image memory */
269    
270          if (pParam->global & XVID_GLOBAL_EXTRASTATS)          if (create->global & XVID_EXTRASTATS_ENABLE)
271                  image_null(&pEnc->sOriginal);                  image_null(&pEnc->sOriginal);
272    
273          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
# Line 256  Line 282 
282          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
283          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
284    
285          if (pParam->global & XVID_GLOBAL_EXTRASTATS)          if (create->global & XVID_EXTRASTATS_ENABLE)
286          {       if (image_create          {       if (image_create
287                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
288                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
# Line 311  Line 337 
337                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
338                  goto xvid_err_memory3;                  goto xvid_err_memory3;
339    
340            /* init bframe image buffers */
341    
342            pEnc->bframenum_head = 0;
343          pEnc->mbParam.global = pParam->global;          pEnc->bframenum_tail = 0;
344            pEnc->flush_bframes = 0;
345            pEnc->closed_bframenum = -1;
346    
347          /* B Frames specific init */          /* B Frames specific init */
         pEnc->mbParam.max_bframes = pParam->max_bframes;  
         pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;  
         pEnc->mbParam.bquant_offset = pParam->bquant_offset;  
         pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;  
348          pEnc->bframes = NULL;          pEnc->bframes = NULL;
349    
350          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
                 int n;  
351    
352                  pEnc->bframes =                  pEnc->bframes =
353                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
# Line 359  Line 383 
383                  }                  }
384          }          }
385    
386          pEnc->bframenum_head = 0;      /* init incoming frame queue */
387          pEnc->bframenum_tail = 0;          pEnc->queue_head = 0;
388          pEnc->flush_bframes = 0;          pEnc->queue_tail = 0;
389          pEnc->bframenum_dx50bvop = -1;          pEnc->queue_size = 0;
   
         pEnc->queue = NULL;  
   
   
         if (pEnc->mbParam.max_bframes > 0) {  
                 int n;  
390    
391                  pEnc->queue =                  pEnc->queue =
392                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),                  xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
393                                                  CACHE_LINE);                                                  CACHE_LINE);
394    
395                  if (pEnc->queue == NULL)                  if (pEnc->queue == NULL)
396                          goto xvid_err_memory4;                          goto xvid_err_memory4;
397    
398                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
399                          image_null(&pEnc->queue[n]);                  image_null(&pEnc->queue[n].image);
400    
401                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {  
402            for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
403            {
404                          if (image_create                          if (image_create
405                                  (&pEnc->queue[n], pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
406                                   pEnc->mbParam.edged_height) < 0)                                   pEnc->mbParam.edged_height) < 0)
407                                  goto xvid_err_memory5;                                  goto xvid_err_memory5;
408    
409                  }                  }
         }  
410    
         pEnc->queue_head = 0;  
         pEnc->queue_tail = 0;  
         pEnc->queue_size = 0;  
411    
412          pEnc->mbParam.m_stamp = 0;          /* timestamp stuff */
413    
414            pEnc->mbParam.m_stamp = 0;
415          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
416          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
417          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
418    
419          pParam->handle = (void *) pEnc;          /* other stuff */
420    
421          if (pParam->rc_bitrate) {          pEnc->iFrameNum = 0;
422                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,          pEnc->fMvPrevSigma = -1;
423                                                  pParam->rc_reaction_delay_factor,  
424                                                  pParam->rc_averaging_period, pParam->rc_buffer,      /* XXX: cleanup ratecontol when new code is ready */
425                                                  pParam->fbase * 1000 / pParam->fincr,  /*      if (create->rc_bitrate) {
426                                                  pParam->max_quantizer, pParam->min_quantizer);                  RateControlInit(&pEnc->rate_control, create->rc_bitrate,
427          }                                                  create->rc_reaction_delay_factor,
428                                                    create->rc_averaging_period, create->rc_buffer,
429                                                    create->fbase * 1000 / create->fincr,
430                                                    create->max_quantizer, create->min_quantizer);
431            } */
432    
433        create->handle = (void *) pEnc;
434    
435          init_timer();          init_timer();
436    
437          return XVID_ERR_OK;      return 0;   /* ok */
438    
439          /*          /*
440           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
# Line 419  Line 442 
442    
443    xvid_err_memory5:    xvid_err_memory5:
444    
   
445          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
446            int i;
447    
448                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
449                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
450                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
451                  }                  }
452                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
# Line 432  Line 455 
455    xvid_err_memory4:    xvid_err_memory4:
456    
457          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
458            int i;
459    
460                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
461    
# Line 452  Line 476 
476    
477    xvid_err_memory3:    xvid_err_memory3:
478    
479          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)
480          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
481                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
482          }          }
# Line 491  Line 515 
515    xvid_err_memory1:    xvid_err_memory1:
516          xvid_free(pEnc->current);          xvid_free(pEnc->current);
517          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
518    
519            xvid_free(pEnc->temp_dquants);
520    
521      xvid_err_memory0:
522        for (n=0; n<pEnc->num_plugins;n++) {
523            if (pEnc->plugins[n].func) {
524                pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);
525            }
526        }
527        xvid_free(pEnc->plugins);
528    
529          xvid_free(pEnc);          xvid_free(pEnc);
530    
531          pParam->handle = NULL;          create->handle = NULL;
532    
533          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
534  }  }
# Line 502  Line 537 
537   * Encoder destruction   * Encoder destruction
538   *   *
539   * This function destroy the entire encoder structure created by a previous   * This function destroy the entire encoder structure created by a previous
540   * successful encoder_create call.   * successful enc_create call.
541   *   *
542   * Returned values (for now only one returned value) :   * Returned values (for now only one returned value) :
543   *    - XVID_ERR_OK     - no errors   *    - 0     - no errors
544   *   *
545   ****************************************************************************/   ****************************************************************************/
546    
547  int  int
548  encoder_destroy(Encoder * pEnc)  enc_destroy(Encoder * pEnc)
549  {  {
550          int i;          int i;
551    
         ENC_CHECK(pEnc);  
   
552          /* B Frames specific */          /* B Frames specific */
553          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
554    
555                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
556    
557                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
558                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
559                  }                  }
560                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
# Line 571  Line 604 
604          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
605                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
606    
607          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)
608          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
609                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
610          }          }
# Line 584  Line 617 
617          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
618          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
619    
620        xvid_free(pEnc->temp_dquants);
621    
622        for (i=0; i<pEnc->num_plugins;i++) {
623            if (pEnc->plugins[i].func) {
624                pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0);
625            }
626        }
627        xvid_free(pEnc->plugins);
628    
629          xvid_free(pEnc);          xvid_free(pEnc);
630    
631          return XVID_ERR_OK;          return 0;  /* ok */
632  }  }
633    
634    
635  static __inline void inc_frame_num(Encoder * pEnc)  /*
636      call the plugins
637      */
638    
639    static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, int opt, int * type, int * quant)
640  {  {
641          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */      int i;
642          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;      xvid_plg_data_t data;
643    
644        memset(&data, 0, sizeof(xvid_plg_data_t));
645        data.version = XVID_VERSION;
646    
647        data.reference.csp = XVID_CSP_USER;
648        data.reference.plane[0] = pEnc->reference->image.y;
649        data.reference.plane[1] = pEnc->reference->image.u;
650        data.reference.plane[2] = pEnc->reference->image.v;
651        data.reference.stride[0] = pEnc->mbParam.edged_width;
652        data.reference.stride[1] = pEnc->mbParam.edged_width/2;
653        data.reference.stride[2] = pEnc->mbParam.edged_width/2;
654    
655        data.current.csp = XVID_CSP_USER;
656        data.current.plane[0] = frame->image.y;
657        data.current.plane[1] = frame->image.u;
658        data.current.plane[2] = frame->image.v;
659        data.current.stride[0] = pEnc->mbParam.edged_width;
660        data.current.stride[1] = pEnc->mbParam.edged_width/2;
661        data.current.stride[2] = pEnc->mbParam.edged_width/2;
662    
663        data.original.csp = XVID_CSP_NULL;
664        /* todo: data.original */
665    
666        if (opt == XVID_PLG_BEFORE) {
667            data.type = XVID_TYPE_AUTO;
668            data.quant = 2;
669            //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);
670            //data.qscale_stride = pEnc->mbParam.width;
671            //data.qscale_table = pEnc->temp_dquants;
672            /* todo: vol,vop,motion flags */
673    
674        } else { // XVID_PLG_AFTER
675    
676            data.type = coding2type(frame->coding_type);
677            data.quant = frame->quant;
678            /* todo: data.qscale */
679            data.vol_flags = frame->vol_flags;
680            data.vop_flags = frame->vop_flags;
681            data.motion_flags = frame->motion_flags;
682    
683            data.length = frame->length;
684            data.kblks = frame->sStat.kblks;
685            data.mblks = frame->sStat.mblks;
686            data.ublks = frame->sStat.ublks;
687        }
688    
689        for (i=0; i<pEnc->num_plugins;i++) {
690            if (pEnc->plugins[i].func) {
691                if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
692                    continue;
693                }
694            }
695  }  }
696    
697        if (opt == XVID_PLG_BEFORE) {
698            *type = data.type;
699            *quant = data.quant;
700            /* todo: copy modified qscale,vol,vop,motion flags into the frame*/
701        }
702    
 static __inline void  
 queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  
 {  
         if (pEnc->queue_size >= pEnc->mbParam.max_bframes)  
         {  
                 DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");  
                 return;  
703          }          }
704    
         DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
705    
706    
         start_timer();  
         if (image_input  
                 (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))  
                 return;  
         stop_conv_timer();  
707    
708          if ((pFrame->general & XVID_CHROMAOPT)) {  static __inline void inc_frame_num(Encoder * pEnc)
709                  image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],  {
710                          pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
711          }          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
712    
713          pEnc->queue_size++;      pEnc->m_framenum++; /* debug ticker */
         pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;  
714  }  }
715    
716    
717    
718  static __inline void  static __inline void
719  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
720  {  {
# Line 647  Line 735 
735  }  }
736    
737    
738    static void
739  /* convert pFrame->intra to coding_type */  set_stats(xvid_enc_stats_t * stats, RateControl * rc,
740  static int intra2coding_type(int intra)                    const MBParam * pParam, const FRAMEINFO * frame)
741  {  {
742          if (intra < 0)  return -1;          if (stats)
743          if (intra == 1) return I_VOP;          {
744          if (intra == 2) return B_VOP;                  stats->type = coding2type(frame->coding_type);
745                    stats->quant = frame->quant;
746                    stats->vol_flags = frame->vol_flags;
747                    stats->vop_flags = frame->vop_flags;
748                    stats->length = frame->length;
749                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
750                    stats->kblks = frame->sStat.kblks;
751                    stats->mblks = frame->sStat.mblks;
752                    stats->ublks = frame->sStat.ublks;
753            }
754    
755          return P_VOP;          RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP );
756  }  }
757    
758    
# Line 664  Line 761 
761   * IPB frame encoder entry point   * IPB frame encoder entry point
762   *   *
763   * Returned values :   * Returned values :
764   *    - XVID_ERR_OK     - no errors   *    - >0               - output bytes
765     *    - 0                - no output
766     *    - XVID_ERR_VERSION - wrong version passed to core
767     *    - XVID_ERR_END     - End of stream reached before end of coding
768   *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong   *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
769   *                        format   *                        format
770   ****************************************************************************/   ****************************************************************************/
771    
772    
773    /* XXX: dx50bvop broken! something todo with queue */
774    
775  int  int
776  encoder_encode_bframes(Encoder * pEnc,  enc_encode(Encoder * pEnc,
777                             XVID_ENC_FRAME * pFrame,                             xvid_enc_frame_t * xFrame,
778                             XVID_ENC_STATS * pResult)                             xvid_enc_stats_t * stats)
779  {  {
780            xvid_enc_frame_t * frame;
781            int type;
782          uint16_t x, y;          uint16_t x, y;
783          Bitstream bs;          Bitstream bs;
         uint32_t bits;  
         int mode = -1; /* Just to shut up compiler warning */  
784    
785          int input_valid = 1;          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */
786          int bframes_count = 0;                  return XVID_ERR_VERSION;
787    
788          ENC_CHECK(pEnc);          xFrame->out_flags = 0;
         ENC_CHECK(pFrame);  
         ENC_CHECK(pFrame->image);  
789    
790          start_global_timer();          start_global_timer();
791            BitstreamInit(&bs, xFrame->bitstream, 0);
792    
         BitstreamInit(&bs, pFrame->bitstream, 0);  
   
 ipvop_loop:  
   
         /*  
          * bframe "flush" code  
          */  
793    
794          if ((pFrame->image == NULL || pEnc->flush_bframes)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {           * enqueue image to the encoding-queue
796             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
797    
798                  if (pEnc->flush_bframes == 0) {          if (xFrame->input.csp != XVID_CSP_NULL)
799                          /*          {
800                           * we have reached the end of stream without getting                  QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];
                          * a future reference frame... so encode last final  
                          * frame as a pframe  
                          */  
801    
802                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  start_timer();
803                                  pEnc->bframenum_head, pEnc->bframenum_tail,                  if (image_input
804                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
805                            pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
806                            xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))
807                    {
808                            emms();
809                            return XVID_ERR_FORMAT;
810                    }
811                    stop_conv_timer();
812    
813                          pEnc->bframenum_tail--;                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
814                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                          image_chroma_optimize(&q->image,
815                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
816                    }
817    
818                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  q->frame = *xFrame;
819    
820                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                  if (xFrame->quant_intra_matrix)
821                          bframes_count = 0;                  {
822                            memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));
823                            q->frame.quant_intra_matrix = q->quant_intra_matrix;
824                    }
825    
826                          BitstreamPadAlways(&bs);                  if (xFrame->quant_inter_matrix)
827                          pFrame->length = BitstreamLength(&bs);                  {
828                          pFrame->intra = 0;                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));
829                            q->frame.quant_inter_matrix = q->quant_inter_matrix;
830                    }
831    
832                    pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
833                    pEnc->queue_size++;
834            }
835    
                         emms();  
836    
837                          if (pResult) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838                                  pResult->quant = pEnc->current->quant;           * bframe flush code
839                                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                                 pResult->kblks = pEnc->current->sStat.kblks;  
                                 pResult->mblks = pEnc->current->sStat.mblks;  
                                 pResult->ublks = pEnc->current->sStat.ublks;  
                         }  
840    
841                          return XVID_ERR_OK;  repeat:
                 }  
842    
843            if (pEnc->flush_bframes)
844            {
845                    if (pEnc->bframenum_head < pEnc->bframenum_tail)
846                    {
847    
848                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
849                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
850                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
851    
852                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                          if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {
853                  pEnc->bframenum_head++;                                  image_copy(&pEnc->sOriginal, &pEnc->bframes[pEnc->bframenum_head]->image,
854                                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
                 BitstreamPadAlways(&bs);  
                 pFrame->length = BitstreamLength(&bs);  
                 pFrame->intra = 2;  
   
                 if (pResult) {  
                         pResult->quant = pEnc->current->quant;  
                         pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                         pResult->kblks = pEnc->current->sStat.kblks;  
                         pResult->mblks = pEnc->current->sStat.mblks;  
                         pResult->ublks = pEnc->current->sStat.ublks;  
855                  }                  }
856    
857                  if (input_valid)                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
858                          queue_image(pEnc, pFrame);  
859                            if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) {
860                                    stats->sse_y =
861                                            plane_sse( pEnc->sOriginal.y, pEnc->bframes[pEnc->bframenum_head]->image.y,
862                                                               pEnc->mbParam.edged_width, pEnc->mbParam.width,
863                                                               pEnc->mbParam.height);
864    
865                                    stats->sse_u =
866                                            plane_sse( pEnc->sOriginal.u, pEnc->bframes[pEnc->bframenum_head]->image.u,
867                                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
868                                                               pEnc->mbParam.height/2);
869    
870                                    stats->sse_v =
871                                            plane_sse( pEnc->sOriginal.v, pEnc->bframes[pEnc->bframenum_head]->image.v,
872                                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
873                                                               pEnc->mbParam.height/2);
874                            }
875    
876                            set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]);
877                emms();
878                call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);
879                  emms();                  emms();
880    
881                  return XVID_ERR_OK;                          pEnc->bframenum_head++;
882    
883    
884                            goto done;
885          }          }
886    
887          if (pEnc->bframenum_head > 0) {                  /* flush complete: reset counters */
888    
889                    pEnc->flush_bframes = 0;
890                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
891    
892                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
# Line 774  Line 896 
896                     indentical to the future-referece frame.                     indentical to the future-referece frame.
897                  */                  */
898    
899                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global_flags & XVID_PACKED)) {
900                          int tmp;                          int tmp;
901                            int bits;
902    
903                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
904                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
905                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
906    
907                            bits = BitstreamPos(&bs);
908    
909                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
910                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
911    
912                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
913                            BitstreamPad(&bs);
914                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
915    
916                          BitstreamPadAlways(&bs);                          /* add the not-coded length to the reference frame size */
917                          pFrame->length = BitstreamLength(&bs);                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
918                          pFrame->intra = 4;                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);
919                emms();
920                          if (pResult) {              call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);
                                 pResult->quant = pEnc->current->quant;  
                                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                                 pResult->kblks = pEnc->current->sStat.kblks;  
                                 pResult->mblks = pEnc->current->sStat.mblks;  
                                 pResult->ublks = pEnc->current->sStat.ublks;  
                         }  
   
                         if (input_valid)  
                                 queue_image(pEnc, pFrame);  
   
921                          emms();                          emms();
922    
923                          return XVID_ERR_OK;                          // xFrame->out_flags |= XVID_PACKED_FIXUP?;
                 }  
         }  
   
   
 bvop_loop:  
   
         if (pEnc->bframenum_dx50bvop != -1)  
         {  
924    
925                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                          goto done;
                 SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);  
926    
                 if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {  
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");  
927                  }                  }
   
                 if (input_valid)  
                 {  
                         queue_image(pEnc, pFrame);  
                         input_valid = 0;  
928                  }                  }
929    
         } else if (input_valid) {  
930    
931                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932             * dequeue frame from the encoding queue
933             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
934    
935                  start_timer();          if (pEnc->queue_size == 0)              /* empty */
                 if (image_input  
                         (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                         pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))  
936                  {                  {
937                          emms();                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
                         return XVID_ERR_FORMAT;  
                 }  
                 stop_conv_timer();  
   
                 if ((pFrame->general & XVID_CHROMAOPT)) {  
                         image_chroma_optimize(&pEnc->current->image,  
                                 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
                 }  
   
                 /* queue input frame, and dequue next image */  
                 if (pEnc->queue_size > 0)  
938                  {                  {
939                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);                          if (pEnc->bframenum_tail > 0)   /* are bframes */
                         if (pEnc->queue_head != pEnc->queue_tail)  
940                          {                          {
                                 image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);  
                         }  
                         pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;  
                         pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;  
                 }  
   
         } else if (pEnc->queue_size > 0) {  
   
941                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
942                                    pEnc->bframenum_tail--;
943                                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
944    
945                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                                  /* XXX: convert B-VOP to P-VOP */
946                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                                  pEnc->current->quant *= 2;
                 pEnc->queue_size--;  
   
         } else {  
   
                 /* if nothing was encoded, write an 'ignore this frame' flag  
                    to the bitstream */  
   
                 if (BitstreamPos(&bs) == 0) {  
   
                         DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
   
                         /* That disabled line of code was supposed to inform VirtualDub  
                          * that the frame was a dummy delay frame - now disabled (thx god :-)  
                          */  
                         /* BitstreamPutBits(&bs, 0x7f, 8); */  
                         pFrame->intra = 5;  
   
                         if (pResult) {  
                                 /*  
                                  * We must decide what to put there because i know some apps  
                                  * are storing statistics about quantizers and just do  
                                  * stats[quant]++ or stats[quant-1]++  
                                  * transcode is one of these app with its 2pass module  
                                  */  
947    
948                                  /*                                  FrameCodeP(pEnc, &bs, 1, 0);
949                                   * For now i prefer 31 than 0 that could lead to a segfault                                  goto done_flush;
                                  * in transcode  
                                  */  
                                 pResult->quant = 31;  
   
                                 pResult->hlength = 0;  
                                 pResult->kblks = 0;  
                                 pResult->mblks = 0;  
                                 pResult->ublks = 0;  
950                          }                          }
951    
952                  } else {                          emms();
953                            return XVID_ERR_END;    /* end of stream reached */
                         if (pResult) {  
                                 pResult->quant = pEnc->current->quant;  
                                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                                 pResult->kblks = pEnc->current->sStat.kblks;  
                                 pResult->mblks = pEnc->current->sStat.mblks;  
                                 pResult->ublks = pEnc->current->sStat.ublks;  
954                          }                          }
955                    goto done;      /* nothing to encode yet; encoder lag */
956                  }                  }
957    
958                  pFrame->length = BitstreamLength(&bs);          // the current FRAME becomes the reference
959            SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
                 emms();  
   
                 return XVID_ERR_OK;  
         }  
960    
961          pEnc->flush_bframes = 0;          // remove frame from encoding-queue (head), and move it into the current
962            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
963            frame = &pEnc->queue[pEnc->queue_head].frame;
964            pEnc->queue_head = (pEnc->queue_head+1) % (pEnc->mbParam.max_bframes+1);
965            pEnc->queue_size--;
966    
         emms();  
967    
968          /* only inc frame num, adapt quant, etc. if we havent seen it before */          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969          if (pEnc->bframenum_dx50bvop < 0 )           * init pEnc->current field
970          {           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                 mode = intra2coding_type(pFrame->intra);  
                 if (pFrame->quant == 0)  
                         pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
                 else  
                         pEnc->current->quant = pFrame->quant;  
   
 /*              if (pEnc->current->quant < 1)  
                         pEnc->current->quant = 1;  
971    
972                  if (pEnc->current->quant > 31)          emms();         /* float-point region */
                         pEnc->current->quant = 31;  
 */  
                 pEnc->current->global_flags = pFrame->general;  
                 pEnc->current->motion_flags = pFrame->motion;  
973    
974                  /* ToDo : dynamic fcode (in both directions) */      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
975        pEnc->current->vop_flags = frame->vop_flags;
976            pEnc->current->motion_flags = frame->motion;
977                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
978                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
979    
980                  inc_frame_num(pEnc);      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
981                image_chroma_optimize(&pEnc->current->image,
982                  if (pFrame->general & XVID_EXTRASTATS)                      pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
                 {       image_copy(&pEnc->sOriginal, &pEnc->current->image,  
                                    pEnc->mbParam.edged_width, pEnc->mbParam.height);  
983                  }                  }
984    
985                  emms();      if (xFrame->vop_flags & XVID_EXTRASTATS) {
986                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
987                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                         pEnc->mbParam.edged_width, pEnc->mbParam.height);
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,  
                                 "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);  
988                  }                  }
989    
990          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
          * Luminance masking  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
                 if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {  
                         int *temp_dquants =  
                                 (int *) xvid_malloc(pEnc->mbParam.mb_width *  
                                                                 pEnc->mbParam.mb_height * sizeof(int),  
                                                                 CACHE_LINE);  
991    
992                          pEnc->current->quant =                          pEnc->current->quant =
993                                  adaptive_quantization(pEnc->current->image.y,                                  adaptive_quantization(pEnc->current->image.y,
994                                                                    pEnc->mbParam.edged_width, temp_dquants,                                                            pEnc->mbParam.edged_width, pEnc->temp_dquants,
995                                                                    pEnc->current->quant, pEnc->current->quant,                                                                    pEnc->current->quant, pEnc->current->quant,
996                                                                    2 * pEnc->current->quant,                                                                    2 * pEnc->current->quant,
997                                                                    pEnc->mbParam.mb_width,                                                                    pEnc->mbParam.mb_width,
# Line 988  Line 1004 
1004                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1005                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1006    
1007                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                  pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];
1008                                  }                                  }
1009    
1010  #undef OFFSET  #undef OFFSET
1011                          }                          }
1012    
                         xvid_free(temp_dquants);  
1013                  }                  }
1014    
1015          }          emms();         /* END floating-point region */
1016        call_plugins(pEnc, pEnc->current, XVID_PLG_BEFORE, &type, &pEnc->current->quant);
1017        emms();
1018    
1019          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      if (frame->type > 0)
1020           * ivop/pvop/bvop selection                  type = frame->type;
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
         pEnc->iFrameNum++;  
1021    
1022          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||      if (frame->quant > 0)
1023                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&                  pEnc->current->quant = frame->quant;
1024                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))  
1025        if (type > 0)       /* XVID_TYPE_?VOP */
1026            {
1027                    type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1028            }
1029            else                    /* XVID_TYPE_AUTO */
1030            {
1031                    if (pEnc->iFrameNum == 0 ||
1032                            (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
1033          {          {
1034                  mode = I_VOP;                          pEnc->iFrameNum = 0;
1035                            type = I_VOP;
1036          }else{          }else{
1037                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
1038                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1039                                          (mode < 0) ? pEnc->iFrameNum : 0,                                          pEnc->iFrameNum, pEnc->bframenum_tail);
                                         bframes_count++);  
1040          }          }
1041            }
1042            inc_frame_num(pEnc);
1043            pEnc->iFrameNum++;
1044    
         if (mode == I_VOP) {  
                 /*  
                  * This will be coded as an Intra Frame  
                  */  
                 if ((pEnc->current->global_flags & XVID_QUARTERPEL))  
                         pEnc->mbParam.m_quarterpel = 1;  
                 else  
                         pEnc->mbParam.m_quarterpel = 0;  
   
                 if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;  
1045    
1046                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1047                          if (pFrame->quant_intra_matrix != NULL)                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1048                                  set_intra_matrix(pFrame->quant_intra_matrix);                          "%i  st:%i  if:%i", pEnc->m_framenum, pEnc->current->stamp, pEnc->iFrameNum);
                         if (pFrame->quant_inter_matrix != NULL)  
                                 set_inter_matrix(pFrame->quant_inter_matrix);  
1049                  }                  }
1050    
1051            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1052             * ivop/pvop/bvop selection
1053             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1054    
1055            if (type == I_VOP) {
1056    
1057                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1058                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1059                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1060    
1061                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1062                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1063                  }                  }
1064    
1065                  /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */                  // when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame
1066                    if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1067    
1068                            // place this frame back on the encoding-queue (head)
1069                            // we will deal with it next time
1070                            pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1071                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1072    
1073                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                          // grab the last frame from the bframe-queue
1074    
1075                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
1076                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1077    
1078                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
                         if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {  
1079                                  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, "DX50 BVOP->PVOP");
1080                          }                          }
1081                          FrameCodeP(pEnc, &bs, &bits, 1, 0);  
1082                          bframes_count = 0;                          /* XXX: convert B-VOP to P-VOP */
1083                          pFrame->intra = 0;                          pEnc->current->quant *= 2;
1084    
1085                            FrameCodeP(pEnc, &bs, 1, 0);
1086    
1087                  } else {                  } else {
1088    
1089                          FrameCodeI(pEnc, &bs, &bits);                          /* ---- update vol flags at IVOP ----------- */
1090                          bframes_count = 0;                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
                         pFrame->intra = 1;  
1091    
1092                          pEnc->bframenum_dx50bvop = -1;              if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {
1093                                    if (frame->quant_intra_matrix != NULL)
1094                                            set_intra_matrix(frame->quant_intra_matrix);
1095                                    if (frame->quant_inter_matrix != NULL)
1096                                            set_inter_matrix(frame->quant_inter_matrix);
1097                  }                  }
1098    
1099                  pEnc->flush_bframes = 1;              /* prevent vol/vop misuse */
1100    
1101                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {              if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
1102                          BitstreamPadAlways(&bs);                  pEnc->current->vop_flags &= ~XVID_REDUCED;
1103                          input_valid = 0;  
1104                          goto ipvop_loop;              if (!(pEnc->current->vol_flags & XVID_INTERLACING))
1105                    pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);
1106    
1107                            /* ^^^------------------------ */
1108    
1109                            FrameCodeI(pEnc, &bs);
1110                            xFrame->out_flags |= XVID_KEYFRAME;
1111                  }                  }
1112    
1113                  /*          } else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) ||
1114                   * NB : sequences like "IIBB" decode fine with msfdam but,                                  pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
                  *      go screwy with divx 5.00  
                  */  
         } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
1115                  /*                  /*
1116                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1117                   */                   */
1118    
1119                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** xFrame bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1120                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1121                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1122    
1123                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1124                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1125                  }                  }
1126    
1127                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, 1, 0);
                 bframes_count = 0;  
                 pFrame->intra = 0;  
                 pEnc->flush_bframes = 1;  
1128    
1129                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {          } else {        /* type == B_VOP */
                         BitstreamPadAlways(&bs);  
                         input_valid = 0;  
                         goto ipvop_loop;  
                 }  
1130    
1131          } else {        /* mode == B_VOP */                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
                 /*  
                  * This will be coded as a Bidirectional Frame  
                  */  
   
                 if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {  
1132                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1133                  }                  }
1134    
1135                  if (pFrame->bquant < 1) {                  if (frame->bquant < 1) {
1136                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1137                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1138    
1139                  } else {                  } else {
1140                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = frame->bquant;
1141                  }                  }
1142    
1143                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1135  Line 1155 
1155    
1156                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1157    
1158                  /* bframe report by koepi */                  goto repeat;
1159                  pFrame->intra = 2;  
1160                  pFrame->length = 0;      }
   
                 input_valid = 0;  
                 goto bvop_loop;  
         }  
   
         BitstreamPadAlways(&bs);  
         pFrame->length = BitstreamLength(&bs);  
   
         if (pResult) {  
                 pResult->quant = pEnc->current->quant;  
                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->current->sStat.kblks;  
                 pResult->mblks = pEnc->current->sStat.mblks;  
                 pResult->ublks = pEnc->current->sStat.ublks;  
1161    
1162                  if (pFrame->general & XVID_EXTRASTATS)          if (xFrame->vop_flags & XVID_EXTRASTATS) {
1163                  {       pResult->sse_y =                  stats->sse_y =
1164                                  plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,                                  plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
1165                                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1166                                                     pEnc->mbParam.height);                                                     pEnc->mbParam.height);
1167    
1168                          pResult->sse_u =                  stats->sse_u =
1169                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1170                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1171                                                     pEnc->mbParam.height/2);                                                     pEnc->mbParam.height/2);
1172    
1173                          pResult->sse_v =                  stats->sse_v =
1174                                  plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,                                  plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1175                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1176                                                     pEnc->mbParam.height/2);                                                     pEnc->mbParam.height/2);
1177                  }                  }
         }  
   
         emms();  
1178    
1179          if (pFrame->quant == 0) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,           * okay, on next enc_encode call we must flush bframes
1181                                                    pFrame->length, pFrame->intra);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
         }  
   
         stop_global_timer();  
         write_timer();  
   
         emms();  
         return XVID_ERR_OK;  
 }  
   
   
   
 /*****************************************************************************  
  * "original" IP frame encoder entry point  
  *  
  * Returned values :  
  *    - XVID_ERR_OK     - no errors  
  *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong  
  *                        format  
  ****************************************************************************/  
   
 int  
 encoder_encode(Encoder * pEnc,  
                            XVID_ENC_FRAME * pFrame,  
                            XVID_ENC_STATS * pResult)  
 {  
         uint16_t x, y;  
         Bitstream bs;  
         uint32_t bits;  
         uint16_t write_vol_header = 0;  
   
         float psnr;  
         char temp[128];  
   
         start_global_timer();  
   
         ENC_CHECK(pEnc);  
         ENC_CHECK(pFrame);  
         ENC_CHECK(pFrame->bitstream);  
         ENC_CHECK(pFrame->image);  
   
         SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);  
   
         pEnc->current->global_flags = pFrame->general;  
         pEnc->current->motion_flags = pFrame->motion;  
         pEnc->mbParam.hint = &pFrame->hint;  
1182    
1183          inc_frame_num(pEnc);  done_flush:
1184    
1185          /* disable alternate scan flag if interlacing is not enabled */  #if 0
         if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&  
                 !(pEnc->current->global_flags & XVID_INTERLACING))  
1186          {          {
1187                  pEnc->current->global_flags -= XVID_ALTERNATESCAN;                  char tmp[100];
1188          }                  wsprintf(tmp,"\\frame%03i.pgm", pEnc->m_framenum);
1189                    image_dump_yuvpgm(&pEnc->current->image, pEnc->mbParam.edged_width,
1190          start_timer();                          pEnc->mbParam.width, pEnc->mbParam.height, tmp);
         if (image_input  
                 (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)  
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
   
         if ((pFrame->general & XVID_CHROMAOPT)) {  
                 image_chroma_optimize(&pEnc->current->image,  
                         pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
1191          }          }
1192    #endif
1193    
1194          if (pFrame->general & XVID_EXTRASTATS)          /* packed_mode: repeat */
1195          {       image_copy(&pEnc->sOriginal, &pEnc->current->image,          pEnc->flush_bframes = 1;
1196                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {
1197                    goto repeat;
1198          }          }
1199    
1200            if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)
1201            {
1202                    /* packed(and low_delay) encoding gives us graceful reorded-stats output */
1203                    set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);
1204            emms();
1205            call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);
1206          emms();          emms();
   
         BitstreamInit(&bs, pFrame->bitstream, 0);  
   
         if (pFrame->quant == 0) {  
                 pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
         } else {  
                 pEnc->current->quant = pFrame->quant;  
1207          }          }
   
         if ((pEnc->current->global_flags & XVID_QUARTERPEL))  
                 pEnc->mbParam.m_quarterpel = 1;  
1208          else          else
1209                  pEnc->mbParam.m_quarterpel = 0;          {
1210                    /* outherwise, output the previous frame */
1211          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                  /* XXX: for this to work, refernce must be valid for IVOPs too */
1212                  int *temp_dquants =          if (pEnc->current->stamp > 0) {
1213                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference);
1214                                                                  pEnc->mbParam.mb_height * sizeof(int),              emms();
1215                                                                  CACHE_LINE);              call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0);
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                                                   pEnc->mbParam.edged_width, temp_dquants,  
                                                                   pEnc->current->quant, pEnc->current->quant,  
                                                                   2 * pEnc->current->quant,  
                                                                   pEnc->mbParam.mb_width,  
                                                                   pEnc->mbParam.mb_height);  
   
                 for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
   
 #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)  
   
                         for (x = 0; x < pEnc->mbParam.mb_width; x++) {  
   
   
                                 MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];  
   
                                 pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];  
                         }  
   
 #undef OFFSET  
                 }  
   
                 xvid_free(temp_dquants);  
         }  
   
         if (pEnc->current->global_flags & XVID_H263QUANT) {  
                 if (pEnc->mbParam.m_quant_type != H263_QUANT)  
                         write_vol_header = 1;  
                 pEnc->mbParam.m_quant_type = H263_QUANT;  
         } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {  
                 int matrix1_changed, matrix2_changed;  
   
                 matrix1_changed = matrix2_changed = 0;  
   
                 if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)  
                         write_vol_header = 1;  
   
                 pEnc->mbParam.m_quant_type = MPEG4_QUANT;  
   
                 if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {  
                         if (pFrame->quant_intra_matrix != NULL)  
                                 matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);  
                         if (pFrame->quant_inter_matrix != NULL)  
                                 matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);  
                 } else {  
                         matrix1_changed = set_intra_matrix(get_default_intra_matrix());  
                         matrix2_changed = set_inter_matrix(get_default_inter_matrix());  
                 }  
                 if (write_vol_header == 0)  
                         write_vol_header = matrix1_changed | matrix2_changed;  
         }  
   
         if (pFrame->intra < 0) {  
                 if ((pEnc->iFrameNum == -1)  
                         || ((pEnc->mbParam.iMaxKeyInterval > 0)  
                                 && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {  
                         pFrame->intra = FrameCodeI(pEnc, &bs, &bits);  
                 } else {  
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);  
                 }  
         } else {  
                 if (pFrame->intra == 1) {  
                         pFrame->intra = FrameCodeI(pEnc, &bs, &bits);  
                 } else {  
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);  
                 }  
   
         }  
   
         /* Relic from OpenDivX - now disabled  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         */  
   
         BitstreamPadAlways(&bs);  
         pFrame->length = BitstreamLength(&bs);  
   
         if (pResult) {  
                 pResult->quant = pEnc->current->quant;  
                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->current->sStat.kblks;  
                 pResult->mblks = pEnc->current->sStat.mblks;  
                 pResult->ublks = pEnc->current->sStat.ublks;  
         }  
   
1216          emms();          emms();
   
         if (pFrame->quant == 0) {  
                 RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,  
                                                   pFrame->length, pFrame->intra);  
1217          }          }
1218          if (pFrame->general & XVID_EXTRASTATS)                  else
1219          {                          stats->type = XVID_TYPE_NOTHING;
                 psnr =  
                         image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
                                            pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                            pEnc->mbParam.height);  
   
                 snprintf(temp, 127, "PSNR: %f\n", psnr);  
1220          }          }
1221    
1222          pEnc->iFrameNum++;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1223             * done; return number of bytes consumed
1224             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1225    
1226    done:
1227    
1228          stop_global_timer();          stop_global_timer();
1229          write_timer();          write_timer();
1230    
1231          return XVID_ERR_OK;          emms();
1232            return BitstreamLength(&bs);
1233  }  }
1234    
1235    
# Line 1393  Line 1246 
1246          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1247          pMB->sad16 = 0;          pMB->sad16 = 0;
1248    
1249          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1250                  if (pMB->dquant != NO_CHANGE) {                  if (pMB->dquant != NO_CHANGE) {
1251                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1252                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1409  Line 1262 
1262  }  }
1263    
1264    
 #define FCODEBITS       3  
 #define MODEBITS        5  
   
 void  
 HintedMESet(Encoder * pEnc,  
                         int *intra)  
 {  
         HINTINFO *hint;  
         Bitstream bs;  
         int length, high;  
         uint32_t x, y;  
   
         hint = pEnc->mbParam.hint;  
   
         if (hint->rawhints) {  
                 *intra = hint->mvhint.intra;  
         } else {  
                 BitstreamInit(&bs, hint->hintstream, hint->hintlength);  
                 *intra = BitstreamGetBit(&bs);  
         }  
   
         if (*intra) {  
                 return;  
         }  
   
         pEnc->current->fcode = (hint->rawhints) ?  
                 (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);  
   
         length = pEnc->current->fcode + 5;  
         high = 1 << (length - 1);  
   
         for (y = 0; y < pEnc->mbParam.mb_height; ++y) {  
                 for (x = 0; x < pEnc->mbParam.mb_width; ++x) {  
                         MACROBLOCK *pMB =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MVBLOCKHINT *bhint =  
                                 &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];  
                         VECTOR pred;  
                         VECTOR tmp;  
                         int vec;  
   
                         pMB->mode =     (hint->rawhints) ?  
                                 (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);  
   
                         pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;  
                         pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;  
   
                         if (pMB->mode == MODE_INTER) {  
                                 tmp.x = (hint->rawhints) ?  
                                         bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);  
                                 tmp.y = (hint->rawhints) ?  
                                         bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);  
                                 tmp.x -= (tmp.x >= high) ? high * 2 : 0;  
                                 tmp.y -= (tmp.y >= high) ? high * 2 : 0;  
   
                                 pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);  
   
                                 for (vec = 0; vec < 4; ++vec) {  
                                         pMB->mvs[vec].x = tmp.x;  
                                         pMB->mvs[vec].y = tmp.y;  
                                         pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;  
                                         pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;  
                                 }  
                         } else if (pMB->mode == MODE_INTER4V) {  
                                 for (vec = 0; vec < 4; ++vec) {  
                                         tmp.x = (hint->rawhints) ?  
                                                 bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);  
                                         tmp.y = (hint->rawhints) ?  
                                                 bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);  
                                         tmp.x -= (tmp.x >= high) ? high * 2 : 0;  
                                         tmp.y -= (tmp.y >= high) ? high * 2 : 0;  
   
                                         pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);  
   
                                         pMB->mvs[vec].x = tmp.x;  
                                         pMB->mvs[vec].y = tmp.y;  
                                         pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;  
                                         pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;  
                                 }  
                         } else                          /* intra / stuffing / not_coded */  
                         {  
                                 for (vec = 0; vec < 4; ++vec) {  
                                         pMB->mvs[vec].x = pMB->mvs[vec].y = 0;  
                                 }  
                         }  
   
                         if (pMB->mode == MODE_INTER4V &&  
                                 (pEnc->current->global_flags & XVID_LUMIMASKING)  
                                 && pMB->dquant != NO_CHANGE) {  
                                 pMB->mode = MODE_INTRA;  
   
                                 for (vec = 0; vec < 4; ++vec) {  
                                         pMB->mvs[vec].x = pMB->mvs[vec].y = 0;  
                                 }  
                         }  
                 }  
         }  
 }  
   
   
 void  
 HintedMEGet(Encoder * pEnc,  
                         int intra)  
 {  
         HINTINFO *hint;  
         Bitstream bs;  
         uint32_t x, y;  
         int length, high;  
   
         hint = pEnc->mbParam.hint;  
   
         if (hint->rawhints) {  
                 hint->mvhint.intra = intra;  
         } else {  
                 BitstreamInit(&bs, hint->hintstream, 0);  
                 BitstreamPutBit(&bs, intra);  
         }  
   
         if (intra) {  
                 if (!hint->rawhints) {  
                         BitstreamPadAlways(&bs);  
                         hint->hintlength = BitstreamLength(&bs);  
                 }  
                 return;  
         }  
   
         length = pEnc->current->fcode + 5;  
         high = 1 << (length - 1);  
   
         if (hint->rawhints) {  
                 hint->mvhint.fcode = pEnc->current->fcode;  
         } else {  
                 BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);  
         }  
   
         for (y = 0; y < pEnc->mbParam.mb_height; ++y) {  
                 for (x = 0; x < pEnc->mbParam.mb_width; ++x) {  
                         MACROBLOCK *pMB =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MVBLOCKHINT *bhint =  
                                 &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];  
                         VECTOR tmp;  
   
                         if (hint->rawhints) {  
                                 bhint->mode = pMB->mode;  
                         } else {  
                                 BitstreamPutBits(&bs, pMB->mode, MODEBITS);  
                         }  
   
                         if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {  
                                 tmp.x = pMB->mvs[0].x;  
                                 tmp.y = pMB->mvs[0].y;  
                                 tmp.x += (tmp.x < 0) ? high * 2 : 0;  
                                 tmp.y += (tmp.y < 0) ? high * 2 : 0;  
   
                                 if (hint->rawhints) {  
                                         bhint->mvs[0].x = tmp.x;  
                                         bhint->mvs[0].y = tmp.y;  
                                 } else {  
                                         BitstreamPutBits(&bs, tmp.x, length);  
                                         BitstreamPutBits(&bs, tmp.y, length);  
                                 }  
                         } else if (pMB->mode == MODE_INTER4V) {  
                                 int vec;  
   
                                 for (vec = 0; vec < 4; ++vec) {  
                                         tmp.x = pMB->mvs[vec].x;  
                                         tmp.y = pMB->mvs[vec].y;  
                                         tmp.x += (tmp.x < 0) ? high * 2 : 0;  
                                         tmp.y += (tmp.y < 0) ? high * 2 : 0;  
   
                                         if (hint->rawhints) {  
                                                 bhint->mvs[vec].x = tmp.x;  
                                                 bhint->mvs[vec].y = tmp.y;  
                                         } else {  
                                                 BitstreamPutBits(&bs, tmp.x, length);  
                                                 BitstreamPutBits(&bs, tmp.y, length);  
                                         }  
                                 }  
                         }  
                 }  
         }  
   
         if (!hint->rawhints) {  
                 BitstreamPad(&bs);  
                 hint->hintlength = BitstreamLength(&bs);  
         }  
 }  
   
1265    
1266  static int  static int
1267  FrameCodeI(Encoder * pEnc,  FrameCodeI(Encoder * pEnc,
1268                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
1269  {  {
1270        int bits = BitstreamPos(bs);
1271          int mb_width = pEnc->mbParam.mb_width;          int mb_width = pEnc->mbParam.mb_width;
1272          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1273    
# Line 1612  Line 1276 
1276    
1277          uint16_t x, y;          uint16_t x, y;
1278    
1279          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
1280          {          {
1281                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1282                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1626  Line 1290 
1290                  stop_edges_timer();                  stop_edges_timer();
1291          }          }
1292    
1293          pEnc->iFrameNum = 0;          //pEnc->iFrameNum = 0;
1294          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1295          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1296          pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1297          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1298    
1299          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1300    
1301          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1302    
1303          BitstreamPadAlways(bs);          BitstreamPadAlways(bs);
1304          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1305    
         *pBits = BitstreamPos(bs);  
   
1306          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1307          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1308          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
# Line 1660  Line 1322 
1322                          stop_prediction_timer();                          stop_prediction_timer();
1323    
1324                          start_timer();                          start_timer();
1325                          if (pEnc->current->global_flags & XVID_GREYSCALE)                          if (pEnc->current->vop_flags & XVID_GREYSCALE)
1326                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1327                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1328                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
# Line 1669  Line 1331 
1331                          stop_coding_timer();                          stop_coding_timer();
1332                  }                  }
1333    
1334          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1335          {          {
1336                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1337                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1338                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                          16, 0);
1339          }          }
1340          emms();          emms();
1341    
1342          *pBits = BitstreamPos(bs) - *pBits;          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1343            if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1344                    BitstreamPadAlways(bs);
1345            else
1346                    BitstreamPad(bs);
1347        pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1348    
1349          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1350          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1351    
1352        /* XXX: hinted me
1353          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1354                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1355          }          }*/
1356    
1357          return 1;                                       /* intra */          return 1;                                       /* intra */
1358  }  }
# Line 1697  Line 1366 
1366  static int  static int
1367  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1368                     Bitstream * bs,                     Bitstream * bs,
                    uint32_t * pBits,  
1369                     bool force_inter,                     bool force_inter,
1370                     bool vol_header)                     bool vol_header)
1371  {  {
1372          float fSigma;          float fSigma;
1373        int bits = BitstreamPos(bs);
1374    
1375          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1376          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
# Line 1717  Line 1386 
1386          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1387          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1388    
1389          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1390          {          {
1391                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1392                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1731  Line 1400 
1400    
1401          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1402          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1403          pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1404          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1405    
1406          if (!force_inter)          if (!force_inter)
# Line 1739  Line 1408 
1408          else          else
1409                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1410    
1411          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {
1412                  start_timer();                  start_timer();
1413                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1414                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1415                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1416                                                    pEnc->mbParam.m_quarterpel,                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),
1417                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1418                  stop_inter_timer();                  stop_inter_timer();
1419          }          }
# Line 1752  Line 1421 
1421          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1422    
1423          start_timer();          start_timer();
1424          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1425                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1426          else          else*/
1427                  bIntra =                  bIntra =
1428                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1429                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1762  Line 1431 
1431    
1432          stop_motion_timer();          stop_motion_timer();
1433    
1434          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1435    
1436          if ( ( pEnc->current->global_flags & XVID_GMC )          if ( ( pEnc->current->vol_flags & XVID_GMC )
1437                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )                  && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1438          {          {
1439                  pEnc->current->coding_type = S_VOP;                  pEnc->current->coding_type = S_VOP;
# Line 1776  Line 1445 
1445                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1446                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1447                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1448                                  pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,
1449                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1450    
1451          }          }
1452    
1453          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1454          if (vol_header)          if (vol_header)
1455          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1456                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1457          }          }
1458    
1459          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1460    
         *pBits = BitstreamPos(bs);  
   
1461          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1462                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1463    
# Line 1837  Line 1504 
1504    
1505                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1506    
1507                                          if (pEnc->mbParam.m_quarterpel)                                          if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1508                                                  pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;                                                  pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1509                                          else                                          else
1510                                                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;                                                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
# Line 1860  Line 1527 
1527                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pEnc->mbParam.width,
1528                                                                   pEnc->mbParam.height,                                                                   pEnc->mbParam.height,
1529                                                                   pEnc->mbParam.edged_width,                                                                   pEnc->mbParam.edged_width,
1530                                                                   pEnc->mbParam.m_quarterpel,                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),
1531                                                                   (pEnc->current->global_flags & XVID_REDUCED),                                                                   (pEnc->current->vop_flags & XVID_REDUCED),
1532                                                                   pEnc->current->rounding_type);                                                                   pEnc->current->rounding_type);
1533    
1534                          stop_comp_timer();                          stop_comp_timer();
1535    
1536                          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1537                                  if (pMB->dquant != NO_CHANGE) {                                  if (pMB->dquant != NO_CHANGE) {
1538                                          pMB->mode = MODE_INTER_Q;                                          pMB->mode = MODE_INTER_Q;
1539                                          pEnc->current->quant += DQtab[pMB->dquant];                                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1904  Line 1571 
1571                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1572                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1573                          else if (pEnc->current->coding_type == P_VOP) {                          else if (pEnc->current->coding_type == P_VOP) {
1574                                  if (pEnc->mbParam.m_quarterpel)                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1575                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1576                                  else                                  else
1577                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );                                          skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
# Line 1931  Line 1598 
1598                                          }                                          }
1599    
1600                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1601                                                  if(pEnc->mbParam.m_quarterpel) {                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1602                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                                          VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1603                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1604                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
# Line 1958  Line 1625 
1625                          }                          }
1626                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1627    
1628                          if (pEnc->current->global_flags & XVID_GREYSCALE)                          if ((pEnc->current->vop_flags & XVID_GREYSCALE))
1629                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1630                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1631                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1632                          }                          }
1633    
1634                          if(pEnc->mbParam.m_quarterpel) {                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1635                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1636                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1637                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
# Line 1981  Line 1648 
1648                          {       int k;                          {       int k;
1649                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1650                                  {                                  {
1651                                          if(pEnc->mbParam.m_quarterpel) {                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1652                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);                                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1653                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1654                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
# Line 2002  Line 1669 
1669                  }                  }
1670          }          }
1671    
1672          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1673          {          {
1674                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1675                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1676                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                          16, 0);
1677          }          }
1678    
1679          emms();          emms();
1680    
1681        /* XXX: hinted me
1682          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1683                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1684          }          }*/
1685    
1686          if (pEnc->current->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
1687                  pEnc->current->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
# Line 2023  Line 1691 
1691          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1692    
1693          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1694                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0)  ))) /* maximum search range 128 */
1695          {          {
1696                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1697                  iSearchRange *= 2;                  iSearchRange *= 2;
1698          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1699                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1700                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1701                             && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel)))      /* minimum search range 16 */                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) )))        /* minimum search range 16 */
1702          {          {
1703                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1704                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 2055  Line 1723 
1723                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
1724                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
1725                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
1726                  pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1727                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1728                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1729                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
# Line 2077  Line 1745 
1745          }          }
1746          */          */
1747    
1748            /* for divx5 compatibility, we must always pad between the packed p and b frames */
1749            if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1750                    BitstreamPadAlways(bs);
1751            else
1752                    BitstreamPad(bs);
1753    
1754          *pBits = BitstreamPos(bs) - *pBits;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1755    
1756          return 0;                                       /* inter */          return 0;                                       /* inter */
1757  }  }
# Line 2087  Line 1760 
1760  static void  static void
1761  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1762                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1763                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
1764  {  {
1765        int bits = BitstreamPos(bs);
1766          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1767          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1768          uint32_t x, y;          uint32_t x, y;
# Line 2104  Line 1777 
1777                  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); \
1778          }          }
1779    
1780          pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */          /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */
1781    
1782          if (!first){          if (!first){
1783                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1784          }          }
1785  #endif  #endif
1786    
1787          frame->quarterpel =  pEnc->mbParam.m_quarterpel;          //frame->quarterpel =  pEnc->mbParam.m_quarterpel;
1788    
1789          /* forward  */          /* forward  */
1790          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
# Line 2120  Line 1793 
1793          start_timer();          start_timer();
1794          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1795                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1796                                            pEnc->mbParam.m_quarterpel, 0);                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1797          stop_inter_timer();          stop_inter_timer();
1798    
1799          /* backward */          /* backward */
# Line 2130  Line 1803 
1803          start_timer();          start_timer();
1804          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1805                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1806                                            pEnc->mbParam.m_quarterpel, 0);                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1807          stop_inter_timer();          stop_inter_timer();
1808    
1809          start_timer();          start_timer();
# Line 2157  Line 1830 
1830          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1831          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1832    
         *pBits = BitstreamPos(bs);  
   
1833          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1834          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
1835          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
1836          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
1837            frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1838            frame->sStat.kblks = frame->sStat.ublks = 0;
1839    
1840          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1841                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1842                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1843                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;
1844    
1845                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1846                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 2187  Line 1859 
1859                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
1860    
1861                                  mb->cbp =                                  mb->cbp =
1862                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1863    
1864                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1865                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
# Line 2209  Line 1881 
1881    
1882          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1883    
1884          *pBits = BitstreamPos(bs) - *pBits;      BitstreamPad(bs);
1885            frame->length = (BitstreamPos(bs) - bits) / 8;
1886    
1887  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1888          if (!first){          if (!first){

Legend:
Removed from v.1.95  
changed lines
  Added in v.1.95.2.4

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