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

Diff of /xvidcore/src/encoder.c

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

revision 1.95.2.7, Sat Mar 15 17:03:17 2003 UTC revision 1.96, Tue Mar 4 10:55:21 2003 UTC
# Line 47  Line 47 
47  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
48  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
49  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
50    #include "utils/ratecontrol.h"
51  #include "utils/emms.h"  #include "utils/emms.h"
52  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
53  #include "quant/adapt_quant.h"  #include "quant/adapt_quant.h"
# Line 57  Line 58 
58   * Local macros   * Local macros
59   ****************************************************************************/   ****************************************************************************/
60    
61    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
62  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
63    
64  /*****************************************************************************  /*****************************************************************************
# Line 64  Line 66 
66   ****************************************************************************/   ****************************************************************************/
67    
68  static int FrameCodeI(Encoder * pEnc,  static int FrameCodeI(Encoder * pEnc,
69                                            Bitstream * bs);                                            Bitstream * bs,
70                                              uint32_t * pBits);
71    
72  static int FrameCodeP(Encoder * pEnc,  static int FrameCodeP(Encoder * pEnc,
73                                            Bitstream * bs,                                            Bitstream * bs,
74                                              uint32_t * pBits,
75                                            bool force_inter,                                            bool force_inter,
76                                            bool vol_header);                                            bool vol_header);
77    
78  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
79                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
80                                             Bitstream * bs);                                             Bitstream * bs,
81                                               uint32_t * pBits);
82    
83  /*****************************************************************************  /*****************************************************************************
84   * Local data   * Local data
# Line 99  Line 104 
104   * and cleaning code.   * and cleaning code.
105   *   *
106   * Returned values :   * Returned values :
107   *    - 0                - no errors   *      - XVID_ERR_OK    - no errors
108   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
109   *                        cleans the structure before exiting.   *                        cleans the structure before exiting.
110   *                        pParam->handle is also set to NULL.   *                        pParam->handle is also set to NULL.
111   *   *
112   ****************************************************************************/   ****************************************************************************/
113    
114    int
115    encoder_create(XVID_ENC_PARAM * pParam)
116    {
117            Encoder *pEnc;
118            int i;
119            pParam->handle = NULL;
120    
121            ENC_CHECK(pParam);
122    
123            ENC_CHECK(pParam->width > 0 && pParam->width <= 1920);
124            ENC_CHECK(pParam->height > 0 && pParam->height <= 1280);
125            ENC_CHECK(!(pParam->width % 2));
126            ENC_CHECK(!(pParam->height % 2));
127    
128            /* Fps */
129    
130            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
131                    pParam->fincr = 1;
132                    pParam->fbase = 25;
133            }
134    
135  /*  /*
136   * Simplify the "fincr/fbase" fraction   * Simplify the "fincr/fbase" fraction
137             * (neccessary, since windows supplies us with huge numbers)
138  */  */
139  static void  
140  simplify_time(int *inc, int *base)          i = pParam->fincr;
 {  
         /* common factor */  
         int i = *inc;  
141          while (i > 1) {          while (i > 1) {
142                  if (*inc % i == 0 && *base % i == 0) {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
143                          *inc /= i;                          pParam->fincr /= i;
144                          *base /= i;                          pParam->fbase /= i;
145                          i = *inc;                          i = pParam->fincr;
146                          continue;                          continue;
147                  }                  }
148                  i--;                  i--;
149          }          }
150    
151          /* if neccessary, round to 65535 accuracy */          if (pParam->fbase > 65535) {
152          if (*base > 65535) {                  float div = (float) pParam->fbase / 65535;
153                  float div = (float) *base / 65535;  
154                  *base = (int) (*base / div);                  pParam->fbase = (int) (pParam->fbase / div);
155                  *inc = (int) (*inc / div);                  pParam->fincr = (int) (pParam->fincr / div);
         }  
156  }  }
157    
158            /* Bitrate allocator defaults */
159    
160  int          if (pParam->rc_bitrate <= 0)
161  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)                  pParam->rc_bitrate = 900000;
 {  
         Encoder *pEnc;  
     int n;  
162    
163          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1))   /* v1.x.x */          if (pParam->rc_reaction_delay_factor <= 0)
164                  return XVID_ERR_VERSION;                  pParam->rc_reaction_delay_factor = 16;
165    
166          if (create->width%2 || create->height%2)          if (pParam->rc_averaging_period <= 0)
167                  return XVID_ERR_FAIL;                  pParam->rc_averaging_period = 100;
168    
169          /* allocate encoder struct */          if (pParam->rc_buffer <= 0)
170                    pParam->rc_buffer = 100;
171    
172          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          /* Max and min quantizers */
         if (pEnc == NULL)  
                 return XVID_ERR_MEMORY;  
         memset(pEnc, 0, sizeof(Encoder));  
173    
174          /* global flags */          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
175      pEnc->mbParam.global_flags = create->global;                  pParam->min_quantizer = 1;
176    
177      /* width, height */          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
178          pEnc->mbParam.width = create->width;                  pParam->max_quantizer = 31;
         pEnc->mbParam.height = create->height;  
         pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;  
         pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;  
         pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;  
         pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;  
179    
180      /* framerate */          if (pParam->max_quantizer < pParam->min_quantizer)
181      pEnc->mbParam.fincr = MAX(create->fincr, 0);                  pParam->max_quantizer = pParam->min_quantizer;
         pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;  
     if (pEnc->mbParam.fincr>0)  
             simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);  
182    
183      /* plugin */          /* 1 keyframe each 10 seconds */
     pEnc->num_plugins = create->num_plugins;  
     pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);  
     if (pEnc->plugins == NULL)  
         goto xvid_err_memory0;  
184    
185      for (n=0; n<pEnc->num_plugins;n++) {          if (pParam->max_key_interval <= 0)
186          xvid_plg_create_t pcreate;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
         xvid_plg_info_t pinfo;  
187    
188          memset(&pinfo, 0, sizeof(xvid_plg_info_t));          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
189          pinfo.version = XVID_VERSION;          if (pEnc == NULL)
190          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {                  return XVID_ERR_MEMORY;
             pEnc->mbParam.plugin_flags |= pinfo.flags;  
         }  
191    
192          memset(&pcreate, 0, sizeof(xvid_plg_create_t));          /* Zero the Encoder Structure */
         pcreate.version = XVID_VERSION;  
         pcreate.width = pEnc->mbParam.width;  
         pcreate.height = pEnc->mbParam.height;  
         pcreate.fincr = pEnc->mbParam.fincr;  
         pcreate.fbase = pEnc->mbParam.fbase;  
         pcreate.param = create->plugins[n].param;  
193    
194          pEnc->plugins[n].func = NULL;   /* disable plugins that fail */          memset(pEnc, 0, sizeof(Encoder));
         if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {  
             pEnc->plugins[n].func = create->plugins[n].func;  
         }  
     }  
195    
196      if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) ||          /* Fill members of Encoder structure */
         (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {  
         pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */  
     }  
197    
198      /* temp dquants */          pEnc->mbParam.width = pParam->width;
199          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *          pEnc->mbParam.height = pParam->height;
                                         pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);  
     /* XXX: error checking */  
200    
201          /* bframes */          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
202          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
         pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);  
         pEnc->mbParam.bquant_offset = create->bquant_offset;  
203    
204          /* frame drop ratio */          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
205          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
206    
207      /* max keyframe interval */          pEnc->mbParam.fbase = pParam->fbase;
208      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval;          pEnc->mbParam.fincr = pParam->fincr;
     /*XXX: replace 250 hard code with "10seconds * framerate" */  
209    
210          /* Bitrate allocator defaults          pEnc->mbParam.m_quant_type = H263_QUANT;
211    
212          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))          pEnc->fMvPrevSigma = -1;
                 create->min_quantizer = 1;  
213    
214          if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))          /* Fill rate control parameters */
                 create->max_quantizer = 31;  
215    
216          if (create->max_quantizer < create->min_quantizer)          pEnc->bitrate = pParam->rc_bitrate;
                 create->max_quantizer = create->min_quantizer; */  
217    
218            pEnc->iFrameNum = -1;
219            pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
220    
221      /* allocate working frame-image memory */          /* try to allocate frame memory */
222    
223          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
224          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
# Line 241  Line 226 
226          if (pEnc->current == NULL || pEnc->reference == NULL)          if (pEnc->current == NULL || pEnc->reference == NULL)
227                  goto xvid_err_memory1;                  goto xvid_err_memory1;
228    
229          /* allocate macroblock memory */          /* try to allocate mb memory */
230    
231          pEnc->current->mbs =          pEnc->current->mbs =
232                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
# Line 253  Line 238 
238          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
239                  goto xvid_err_memory2;                  goto xvid_err_memory2;
240    
241          /* allocate interpolation image memory */          /* try to allocate image memory */
242    
243      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
244          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
         image_null(&pEnc->sOriginal2);  
     }  
245    
246          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
247          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 272  Line 255 
255          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
256          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
257    
258          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
259          if (image_create          {       if (image_create
260                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
261                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
262                          goto xvid_err_memory3;                          goto xvid_err_memory3;
   
         if (image_create  
                         (&pEnc->sOriginal2, pEnc->mbParam.edged_width,  
                          pEnc->mbParam.edged_height) < 0)  
                         goto xvid_err_memory3;  
263          }          }
264    
265          if (image_create          if (image_create
# Line 332  Line 310 
310                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
311                  goto xvid_err_memory3;                  goto xvid_err_memory3;
312    
         /* init bframe image buffers */  
313    
314          pEnc->bframenum_head = 0;  
315          pEnc->bframenum_tail = 0;          pEnc->mbParam.global = pParam->global;
         pEnc->flush_bframes = 0;  
         pEnc->closed_bframenum = -1;  
316    
317          /* B Frames specific init */          /* B Frames specific init */
318            pEnc->mbParam.max_bframes = pParam->max_bframes;
319            pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
320            pEnc->mbParam.bquant_offset = pParam->bquant_offset;
321            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
322          pEnc->bframes = NULL;          pEnc->bframes = NULL;
323    
324          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
325                    int n;
326    
327                  pEnc->bframes =                  pEnc->bframes =
328                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
# Line 378  Line 358 
358                  }                  }
359          }          }
360    
361      /* init incoming frame queue */          pEnc->bframenum_head = 0;
362          pEnc->queue_head = 0;          pEnc->bframenum_tail = 0;
363          pEnc->queue_tail = 0;          pEnc->flush_bframes = 0;
364          pEnc->queue_size = 0;          pEnc->bframenum_dx50bvop = -1;
365    
366            pEnc->queue = NULL;
367    
368            if (pEnc->mbParam.max_bframes > 0) {
369                    int n;
370    
371          pEnc->queue =          pEnc->queue =
372                  xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
373                                          CACHE_LINE);                                          CACHE_LINE);
374    
375          if (pEnc->queue == NULL)          if (pEnc->queue == NULL)
376                  goto xvid_err_memory4;                  goto xvid_err_memory4;
377    
378          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)
379                  image_null(&pEnc->queue[n].image);                          image_null(&pEnc->queue[n]);
   
380    
381          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
         {  
382                  if (image_create                  if (image_create
383                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,                                  (&pEnc->queue[n], pEnc->mbParam.edged_width,
384                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
385                          goto xvid_err_memory5;                          goto xvid_err_memory5;
386    
387          }          }
388            }
389    
390            pEnc->queue_head = 0;
391          /* timestamp stuff */          pEnc->queue_tail = 0;
392            pEnc->queue_size = 0;
393    
394          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
395    
396          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
397          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
398          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
399    
400          /* other stuff */          pParam->handle = (void *) pEnc;
401    
402          pEnc->iFrameNum = 0;          if (pParam->rc_bitrate) {
403          pEnc->fMvPrevSigma = -1;                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
404                                                    pParam->rc_reaction_delay_factor,
405      create->handle = (void *) pEnc;                                                  pParam->rc_averaging_period, pParam->rc_buffer,
406                                                    pParam->fbase * 1000 / pParam->fincr,
407                                                    pParam->max_quantizer, pParam->min_quantizer);
408            }
409    
410          init_timer();          init_timer();
411    
412      return 0;   /* ok */          return XVID_ERR_OK;
413    
414          /*          /*
415           * 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 429  Line 418 
418    xvid_err_memory5:    xvid_err_memory5:
419    
420          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
         int i;  
421    
422                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
423                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
424                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
425                  }                  }
426                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
# Line 441  Line 429 
429    xvid_err_memory4:    xvid_err_memory4:
430    
431          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
         int i;  
432    
433                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
434    
# Line 462  Line 449 
449    
450    xvid_err_memory3:    xvid_err_memory3:
451    
452          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
453                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                                           pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,  
454                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
455          }          }
456    
# Line 503  Line 488 
488    xvid_err_memory1:    xvid_err_memory1:
489          xvid_free(pEnc->current);          xvid_free(pEnc->current);
490          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
   
         xvid_free(pEnc->temp_dquants);  
   
   xvid_err_memory0:  
     for (n=0; n<pEnc->num_plugins;n++) {  
         if (pEnc->plugins[n].func) {  
             pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);  
         }  
     }  
     xvid_free(pEnc->plugins);  
   
491          xvid_free(pEnc);          xvid_free(pEnc);
492    
493          create->handle = NULL;          pParam->handle = NULL;
494    
495          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
496  }  }
# Line 525  Line 499 
499   * Encoder destruction   * Encoder destruction
500   *   *
501   * This function destroy the entire encoder structure created by a previous   * This function destroy the entire encoder structure created by a previous
502   * successful enc_create call.   * successful encoder_create call.
503   *   *
504   * Returned values (for now only one returned value) :   * Returned values (for now only one returned value) :
505   *    - 0     - no errors   *      - XVID_ERR_OK    - no errors
506   *   *
507   ****************************************************************************/   ****************************************************************************/
508    
509  int  int
510  enc_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
511  {  {
512          int i;          int i;
513    
514            ENC_CHECK(pEnc);
515    
516          /* B Frames specific */          /* B Frames specific */
517          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
518    
519                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
520    
521                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
522                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
523                  }                  }
524                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
525          }          }
526    
   
527          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
528    
529                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 592  Line 567 
567          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
568                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
569    
570          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
571                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                                           pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,  
572                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
573          }          }
574    
# Line 607  Line 580 
580          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
581          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
582    
     xvid_free(pEnc->temp_dquants);  
   
     for (i=0; i<pEnc->num_plugins;i++) {  
         if (pEnc->plugins[i].func) {  
             pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0);  
         }  
     }  
     xvid_free(pEnc->plugins);  
   
583          xvid_free(pEnc);          xvid_free(pEnc);
584    
585          return 0;  /* ok */          return XVID_ERR_OK;
586  }  }
587    
588    
589  /*  static __inline void inc_frame_num(Encoder * pEnc)
   call the plugins  
   */  
   
 static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,  
                          int opt, int * type, int * quant, xvid_enc_stats_t * stats)  
590  {  {
591      int i;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
592      xvid_plg_data_t data;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
   
     memset(&data, 0, sizeof(xvid_plg_data_t));  
     data.version = XVID_VERSION;  
   
     data.width = pEnc->mbParam.width;  
     data.height = pEnc->mbParam.height;  
     data.fincr = frame->fincr;  
     data.fbase = pEnc->mbParam.fbase;  
   
     data.reference.csp = XVID_CSP_USER;  
     data.reference.plane[0] = pEnc->reference->image.y;  
     data.reference.plane[1] = pEnc->reference->image.u;  
     data.reference.plane[2] = pEnc->reference->image.v;  
     data.reference.stride[0] = pEnc->mbParam.edged_width;  
     data.reference.stride[1] = pEnc->mbParam.edged_width/2;  
     data.reference.stride[2] = pEnc->mbParam.edged_width/2;  
   
     data.current.csp = XVID_CSP_USER;  
     data.current.plane[0] = frame->image.y;  
     data.current.plane[1] = frame->image.u;  
     data.current.plane[2] = frame->image.v;  
     data.current.stride[0] = pEnc->mbParam.edged_width;  
     data.current.stride[1] = pEnc->mbParam.edged_width/2;  
     data.current.stride[2] = pEnc->mbParam.edged_width/2;  
   
     data.frame_num = frame->frame_num;  
   
     if (opt == XVID_PLG_BEFORE) {  
         data.type = XVID_TYPE_AUTO;  
         data.quant = 2;  
         //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);  
         //data.qscale_stride = pEnc->mbParam.width;  
         //data.qscale_table = pEnc->temp_dquants;  
         /* todo: vol,vop,motion flags */  
   
     } else { // XVID_PLG_AFTER  
         if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {  
             data.original.csp = XVID_CSP_USER;  
             data.original.plane[0] = original->y;  
             data.original.plane[1] = original->u;  
             data.original.plane[2] = original->v;  
             data.original.stride[0] = pEnc->mbParam.edged_width;  
             data.original.stride[1] = pEnc->mbParam.edged_width/2;  
             data.original.stride[2] = pEnc->mbParam.edged_width/2;  
         }  
   
         if ((frame->vol_flags & XVID_EXTRASTATS) ||  
             (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {  
   
                         data.sse_y =  
                             plane_sse( original->y, frame->image.y,  
                                                pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                                pEnc->mbParam.height);  
   
             data.sse_u =  
                 plane_sse( original->u, frame->image.u,  
                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                                pEnc->mbParam.height/2);  
   
                         data.sse_v =  
                 plane_sse( original->v, frame->image.v,  
                                            pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                                pEnc->mbParam.height/2);  
         }  
   
         data.type = coding2type(frame->coding_type);  
         data.quant = frame->quant;  
         /* todo: data.qscale */  
         data.vol_flags = frame->vol_flags;  
         data.vop_flags = frame->vop_flags;  
         data.motion_flags = frame->motion_flags;  
   
         data.length = frame->length;  
         data.kblks = frame->sStat.kblks;  
         data.mblks = frame->sStat.mblks;  
         data.ublks = frame->sStat.ublks;  
   
         if (stats)  
         {  
                 stats->type = coding2type(frame->coding_type);  
                 stats->quant = frame->quant;  
                 stats->vol_flags = frame->vol_flags;  
                 stats->vop_flags = frame->vop_flags;  
                 stats->length = frame->length;  
                 stats->hlength = frame->length - (frame->sStat.iTextBits / 8);  
                 stats->kblks = frame->sStat.kblks;  
                 stats->mblks = frame->sStat.mblks;  
                 stats->ublks = frame->sStat.ublks;  
             stats->sse_y = data.sse_y;  
             stats->sse_u = data.sse_u;  
             stats->sse_v = data.sse_v;  
         }  
     }  
   
     emms();  
     for (i=0; i<pEnc->num_plugins;i++) {  
         if (pEnc->plugins[i].func) {  
             if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {  
                 continue;  
             }  
         }  
593      }      }
     emms();  
594    
     if (opt == XVID_PLG_BEFORE) {  
         *type = data.type;  
         *quant = data.quant;  
         /* todo: copy modified qscale,vol,vop,motion flags into the frame*/  
     }  
595    
596    static __inline void
597    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
598    {
599            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
600            {
601                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
602                    return;
603  }  }
604    
605            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
606                                    pEnc->bframenum_head, pEnc->bframenum_tail,
607                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
608    
609    
610            start_timer();
611            if (image_input
612                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
613                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
614                    return;
615            stop_conv_timer();
616    
617  static __inline void inc_frame_num(Encoder * pEnc)          if ((pFrame->general & XVID_CHROMAOPT)) {
618  {                  image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
619      pEnc->current->frame_num = pEnc->m_framenum;                          pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
         pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */  
   
     pEnc->mbParam.m_stamp += pEnc->current->fincr;  
     pEnc->m_framenum++; /* debug ticker */  
620  }  }
621    
622  static __inline void dec_frame_num(Encoder * pEnc)          pEnc->queue_size++;
623  {          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
     pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;  
     pEnc->m_framenum--; /* debug ticker */  
624  }  }
625    
   
   
626  static __inline void  static __inline void
627  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
628  {  {
# Line 784  Line 644 
644    
645    
646    
647    /* convert pFrame->intra to coding_type */
648    static int intra2coding_type(int intra)
649    {
650            if (intra < 0)  return -1;
651            if (intra == 1) return I_VOP;
652            if (intra == 2) return B_VOP;
653    
654            return P_VOP;
655    }
656    
657    
658    
659  /*****************************************************************************  /*****************************************************************************
660   * IPB frame encoder entry point   * IPB frame encoder entry point
661   *   *
662   * Returned values :   * Returned values :
663   *    - >0               - output bytes   *      - XVID_ERR_OK    - no errors
  *    - 0                - no output  
  *    - XVID_ERR_VERSION - wrong version passed to core  
  *    - XVID_ERR_END     - End of stream reached before end of coding  
664   *    - XVID_ERR_FORMAT  - the image subsystem reported the image had a wrong   *    - XVID_ERR_FORMAT  - the image subsystem reported the image had a wrong
665   *                         format   *                         format
666   ****************************************************************************/   ****************************************************************************/
667    
   
668  int  int
669  enc_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
670                             xvid_enc_frame_t * xFrame,                                  XVID_ENC_FRAME * pFrame,
671                             xvid_enc_stats_t * stats)                                  XVID_ENC_STATS * pResult)
672  {  {
673          xvid_enc_frame_t * frame;          uint16_t x, y;
         int type;  
674          Bitstream bs;          Bitstream bs;
675            uint32_t bits;
676            int mode = -1; /* Just to shut up compiler warning */
677    
678          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */          int input_valid = 1;
679                  return XVID_ERR_VERSION;          int bframes_count = 0;
680    
681          xFrame->out_flags = 0;          ENC_CHECK(pEnc);
682            ENC_CHECK(pFrame);
683            ENC_CHECK(pFrame->image);
684    
685          start_global_timer();          start_global_timer();
         BitstreamInit(&bs, xFrame->bitstream, 0);  
686    
687            BitstreamInit(&bs, pFrame->bitstream, 0);
688    
689          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  ipvop_loop:
          * enqueue image to the encoding-queue  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
690    
691          if (xFrame->input.csp != XVID_CSP_NULL)          /*
692          {           * bframe "flush" code
693                  QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];           */
694    
695                  start_timer();          if ((pFrame->image == NULL || pEnc->flush_bframes)
696                  if (image_input                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
                         (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                         pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,  
                         xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))  
                 {  
                         emms();  
                         return XVID_ERR_FORMAT;  
                 }  
                 stop_conv_timer();  
697    
698                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {                  if (pEnc->flush_bframes == 0) {
699                          image_chroma_optimize(&q->image,                          /*
700                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                           * we have reached the end of stream without getting
701                  }                           * a future reference frame... so encode last final
702                             * frame as a pframe
703                             */
704    
705                  q->frame = *xFrame;                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
706                                    pEnc->bframenum_head, pEnc->bframenum_tail,
707                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
708    
709                  if (xFrame->quant_intra_matrix)                          pEnc->bframenum_tail--;
710                  {                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
                         memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));  
                         q->frame.quant_intra_matrix = q->quant_intra_matrix;  
                 }  
711    
712                  if (xFrame->quant_inter_matrix)                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
                 {  
                         memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));  
                         q->frame.quant_inter_matrix = q->quant_inter_matrix;  
                 }  
713    
714                  pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
715                  pEnc->queue_size++;                          bframes_count = 0;
         }  
716    
717                            BitstreamPadAlways(&bs);
718                            pFrame->length = BitstreamLength(&bs);
719                            pFrame->intra = 0;
720    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * bframe flush code  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
721    
722  repeat:                          emms();
723    
724                            if (pResult) {
725                                    pResult->quant = pEnc->current->quant;
726                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
727                                    pResult->kblks = pEnc->current->sStat.kblks;
728                                    pResult->mblks = pEnc->current->sStat.mblks;
729                                    pResult->ublks = pEnc->current->sStat.ublks;
730                            }
731    
732                            return XVID_ERR_OK;
733                    }
734    
         if (pEnc->flush_bframes)  
         {  
                 if (pEnc->bframenum_head < pEnc->bframenum_tail) {  
735    
736                          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",
737                                          pEnc->bframenum_head, pEnc->bframenum_tail,                                          pEnc->bframenum_head, pEnc->bframenum_tail,
738                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                          pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
739    
740              if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
741                                  image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,                  pEnc->bframenum_head++;
742                                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);  
743                    BitstreamPadAlways(&bs);
744                    pFrame->length = BitstreamLength(&bs);
745                    pFrame->intra = 2;
746    
747                    if (pResult) {
748                            pResult->quant = pEnc->current->quant;
749                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
750                            pResult->kblks = pEnc->current->sStat.kblks;
751                            pResult->mblks = pEnc->current->sStat.mblks;
752                            pResult->ublks = pEnc->current->sStat.ublks;
753                          }                          }
754    
755                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);                  if (input_valid)
756              call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);                          queue_image(pEnc, pFrame);
757                          pEnc->bframenum_head++;  
758                    emms();
759    
760                          goto done;                  return XVID_ERR_OK;
761                  }                  }
762    
763                  /* write an empty marker to the bitstream.          if (pEnc->bframenum_head > 0) {
764                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
765    
766                    /* write an empty marker to the bitstream.
767                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
768                     of a not-coded p-vop, with a time_base of zero, and time_increment                     of a not-coded p-vop, with a time_base of zero, and time_increment
769                     indentical to the future-referece frame.                     indentical to the future-referece frame.
770                  */                  */
771    
772                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
773                          int tmp;                          int tmp;
                         int bits;  
774    
775                          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",
776                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
777                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
778    
                         bits = BitstreamPos(&bs);  
   
779                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
780                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
781    
782                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
                         BitstreamPad(&bs);  
783                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
784    
785                          /* add the not-coded length to the reference frame size */                          BitstreamPadAlways(&bs);
786                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;                          pFrame->length = BitstreamLength(&bs);
787              call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          pFrame->intra = 4;
788    
789                            if (pResult) {
790                                    pResult->quant = pEnc->current->quant;
791                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
792                                    pResult->kblks = pEnc->current->sStat.kblks;
793                                    pResult->mblks = pEnc->current->sStat.mblks;
794                                    pResult->ublks = pEnc->current->sStat.ublks;
795                            }
796    
797              /* flush complete: reset counters */                          if (input_valid)
798                  pEnc->flush_bframes = 0;                                  queue_image(pEnc, pFrame);
                     pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
                         goto done;  
799    
800                  }                          emms();
801    
802          /* flush complete: reset counters */                          return XVID_ERR_OK;
803                  pEnc->flush_bframes = 0;                  }
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
804          }          }
805    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * dequeue frame from the encoding queue  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
806    
807          if (pEnc->queue_size == 0)              /* empty */  bvop_loop:
808    
809            if (pEnc->bframenum_dx50bvop != -1)
810          {          {
811                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */  
812                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
813                    SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
814    
815                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
816                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
817                    }
818    
819                    if (input_valid)
820                  {                  {
821                            queue_image(pEnc, pFrame);
822                            input_valid = 0;
823                    }
824    
825            } else if (input_valid) {
826    
827                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
828    
829                    start_timer();
830                    if (image_input
831                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
832                            pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
833                    {
834                            emms();
835                            return XVID_ERR_FORMAT;
836                    }
837                    stop_conv_timer();
838    
839              if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) {                  if ((pFrame->general & XVID_CHROMAOPT)) {
840                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                          image_chroma_optimize(&pEnc->current->image,
841                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
842              }              }
843    
844              /* if the very last frame is to be b-vop, we must change it to a p-vop */                  /* queue input frame, and dequue next image */
845              if (pEnc->bframenum_tail > 0)                  if (pEnc->queue_size > 0)
846                    {
847                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
848                            if (pEnc->queue_head != pEnc->queue_tail)
849                          {                          {
850                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
851                            }
852                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
853                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
854                    }
855    
856            } else if (pEnc->queue_size > 0) {
857    
858                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
                                 pEnc->bframenum_tail--;  
                                 SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);  
859    
860                                  /* convert B-VOP to P-VOP */                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
861                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
862                    pEnc->queue_size--;
863    
864                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          } else {
865                              image_copy(&pEnc->sOriginal, &pEnc->current->image,  
866                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                  /* if nothing was encoded, write an 'ignore this frame' flag
867                       to the bitstream */
868    
869                    if (BitstreamPos(&bs) == 0) {
870    
871                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
872                                    pEnc->bframenum_head, pEnc->bframenum_tail,
873                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
874    
875                            /* That disabled line of code was supposed to inform VirtualDub
876                             * that the frame was a dummy delay frame - now disabled (thx god :-)
877                             */
878                            //BitstreamPutBits(&bs, 0x7f, 8);
879                            pFrame->intra = 5;
880    
881                            if (pResult) {
882                                    /*
883                                     * We must decide what to put there because i know some apps
884                                     * are storing statistics about quantizers and just do
885                                     * stats[quant]++ or stats[quant-1]++
886                                     * transcode is one of these app with its 2pass module
887                                     */
888    
889                                    /*
890                                     * For now i prefer 31 than 0 that could lead to a segfault
891                                     * in transcode
892                                     */
893                                    pResult->quant = 31;
894    
895                                    pResult->hlength = 0;
896                                    pResult->kblks = 0;
897                                    pResult->mblks = 0;
898                                    pResult->ublks = 0;
899                  }                  }
900                    } else {
901    
902                                  FrameCodeP(pEnc, &bs, 1, 0);                          if (pResult) {
903                                    pResult->quant = pEnc->current->quant;
904                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
905                                    pResult->kblks = pEnc->current->sStat.kblks;
906                                    pResult->mblks = pEnc->current->sStat.mblks;
907                                    pResult->ublks = pEnc->current->sStat.ublks;
908                            }
909    
                                 goto done_flush;  
910                          }                          }
911    
912                    pFrame->length = BitstreamLength(&bs);
913    
914                          emms();                          emms();
915                          return XVID_ERR_END;    /* end of stream reached */  
916                  }                  return XVID_ERR_OK;
                 goto done;      /* nothing to encode yet; encoder lag */  
917          }          }
918    
919          // the current FRAME becomes the reference          pEnc->flush_bframes = 0;
920          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);  
921            emms();
922    
923            /* only inc frame num, adapt quant, etc. if we havent seen it before */
924            if (pEnc->bframenum_dx50bvop < 0 )
925            {
926                    mode = intra2coding_type(pFrame->intra);
927                    if (pFrame->quant == 0)
928                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
929                    else
930                            pEnc->current->quant = pFrame->quant;
931    
932    /*              if (pEnc->current->quant < 1)
933                            pEnc->current->quant = 1;
934    
935                    if (pEnc->current->quant > 31)
936                            pEnc->current->quant = 31;
937    */
938                    pEnc->current->global_flags = pFrame->general;
939                    pEnc->current->motion_flags = pFrame->motion;
940    
941                    /* ToDo : dynamic fcode (in both directions) */
942                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
943                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
944    
945                    inc_frame_num(pEnc);
946    
947                    if (pFrame->general & XVID_EXTRASTATS)
948                    {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
949                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
950                    }
951    
952          // remove frame from encoding-queue (head), and move it into the current                  emms();
         image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);  
         frame = &pEnc->queue[pEnc->queue_head].frame;  
         pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);  
         pEnc->queue_size--;  
953    
954                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
955                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
956                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
957                    }
958    
959          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960           * init pEnc->current fields           * Luminance masking
961           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
962    
963                    if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
964      pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;                          int *temp_dquants =
965      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *
966      pEnc->current->vop_flags = frame->vop_flags;                                                                  pEnc->mbParam.mb_height * sizeof(int),
967          pEnc->current->motion_flags = frame->motion;                                                                  CACHE_LINE);
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
         pEnc->current->bcode = pEnc->mbParam.m_fcode;  
   
     if ((xFrame->vop_flags & XVID_CHROMAOPT)) {  
             image_chroma_optimize(&pEnc->current->image,  
                     pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
     }  
   
         emms();         /* float-point region */  
         if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {  
         unsigned int x, y;  
968    
969                  pEnc->current->quant =                  pEnc->current->quant =
970                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
971                                                            pEnc->mbParam.edged_width, pEnc->temp_dquants,                                                                    pEnc->mbParam.edged_width, temp_dquants,
972                                                            pEnc->current->quant, pEnc->current->quant,                                                            pEnc->current->quant, pEnc->current->quant,
973                                                            2 * pEnc->current->quant,                                                            2 * pEnc->current->quant,
974                                                            pEnc->mbParam.mb_width,                                                            pEnc->mbParam.mb_width,
# Line 1010  Line 981 
981                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
982                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
983    
984                                  pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
985                          }                          }
   
986  #undef OFFSET  #undef OFFSET
987                  }                  }
988                            xvid_free(temp_dquants);
989                    }
990          }          }
     emms();             /* END floating-point region */  
991    
992          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993           * frame type & quant selection           * ivop/pvop/bvop selection
994           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
995            pEnc->iFrameNum++;
996    
997      call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats);          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
998                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
999      if (frame->type > 0)                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) {
1000                  type = frame->type;                  mode = I_VOP;
   
     if (frame->quant > 0)  
                 pEnc->current->quant = frame->quant;  
   
     if (type > 0){      /* XVID_TYPE_?VOP */  
                 type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */  
     } else{             /* XVID_TYPE_AUTO */  
                 if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){  
                         pEnc->iFrameNum = 0;  
                         type = I_VOP;  
1001                  }else{                  }else{
1002                          type = MEanalysis(&pEnc->reference->image, pEnc->current,                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1003                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1004                                          pEnc->iFrameNum, pEnc->bframenum_tail);                                          (/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0,
1005                                            bframes_count++, pFrame->bframe_threshold);
1006            }
1007    
1008            if (mode == I_VOP) {
1009                    /*
1010                     * This will be coded as an Intra Frame
1011                     */
1012                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1013                            pEnc->mbParam.m_quarterpel = 1;
1014                    else
1015                            pEnc->mbParam.m_quarterpel = 0;
1016    
1017                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1018    
1019              if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1020                  type = P_VOP;   /* disable dynamic bframes */                          if (pFrame->quant_intra_matrix != NULL)
1021                                    set_intra_matrix(pFrame->quant_intra_matrix);
1022                            if (pFrame->quant_inter_matrix != NULL)
1023                                    set_inter_matrix(pFrame->quant_inter_matrix);
1024              }              }
1025    
1026    
1027                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1028                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1029                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1030    
1031                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1032                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1033                  }                  }
1034    
1035                    /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */
1036    
1037                    if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1038    
1039                            pEnc->bframenum_tail--;
1040                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1041    
1042                            SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1043                            if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1044                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1045          }          }
1046                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
1047                            bframes_count = 0;
1048                            pFrame->intra = 0;
1049    
     /* bframes buffer overflow check */  
     if (type != I_VOP) {  
         if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
             type = P_VOP;  
1050          }else{          }else{
1051              type = B_VOP;  
1052                            FrameCodeI(pEnc, &bs, &bits);
1053                            bframes_count = 0;
1054                            pFrame->intra = 1;
1055    
1056                            pEnc->bframenum_dx50bvop = -1;
1057          }          }
1058    
1059                    pEnc->flush_bframes = 1;
1060    
1061                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1062                            BitstreamPadAlways(&bs);
1063                            input_valid = 0;
1064                            goto ipvop_loop;
1065      }      }
1066    
1067          inc_frame_num(pEnc);                  /*
1068          pEnc->iFrameNum++;                   * NB : sequences like "IIBB" decode fine with msfdam but,
1069                     *        go screwy with divx 5.00
1070                     */
1071            } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1072                    /*
1073                     * This will be coded as a Predicted Frame
1074                     */
1075    
1076          if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1077                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1078                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1079    
1080                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1081                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1082          }          }
1083    
1084          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1085           * encode this frame as a b-vop                  bframes_count = 0;
1086       * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)                  pFrame->intra = 0;
1087           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */                  pEnc->flush_bframes = 1;
1088          if (type == B_VOP) {  
1089                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1090                            BitstreamPadAlways(&bs);
1091                            input_valid = 0;
1092                            goto ipvop_loop;
1093                    }
1094    
1095            } else {        /* mode == B_VOP */
1096                    /*
1097                     * This will be coded as a Bidirectional Frame
1098                     */
1099    
1100                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1101                          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");
1102                  }                  }
1103    
1104                  if (frame->bquant < 1) {                  if (pFrame->bquant < 1) {
1105                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1106                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1107    
1108                  } else {                  } else {
1109                          pEnc->current->quant = frame->bquant;                          pEnc->current->quant = pFrame->bquant;
1110                  }                  }
1111    
1112                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1087  Line 1114 
1114                  else if (pEnc->current->quant > 31)                  else if (pEnc->current->quant > 31)
1115              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1116    
1117                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i",                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1118                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1119                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1120    
# Line 1097  Line 1124 
1124    
1125                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1126    
1127                  goto repeat;                  /* bframe report by koepi */
1128                    pFrame->intra = 2;
1129                    pFrame->length = 0;
1130    
1131                    input_valid = 0;
1132                    goto bvop_loop;
1133            }
1134    
1135            BitstreamPadAlways(&bs);
1136            pFrame->length = BitstreamLength(&bs);
1137    
1138            if (pResult) {
1139                    pResult->quant = pEnc->current->quant;
1140                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1141                    pResult->kblks = pEnc->current->sStat.kblks;
1142                    pResult->mblks = pEnc->current->sStat.mblks;
1143                    pResult->ublks = pEnc->current->sStat.ublks;
1144    
1145                    if (pFrame->general & XVID_EXTRASTATS)
1146                    {       pResult->sse_y =
1147                                    plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
1148                                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1149                                                       pEnc->mbParam.height);
1150    
1151                            pResult->sse_u =
1152                                    plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1153                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1154                                                       pEnc->mbParam.height/2);
1155    
1156                            pResult->sse_v =
1157                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1158                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1159                                                       pEnc->mbParam.height/2);
1160                    }
1161      }      }
1162    
1163      /* for unpacked bframes, output the stats for the last encoded frame */          emms();
1164      if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)  
1165      {          if (pFrame->quant == 0) {
1166          if (pEnc->current->stamp > 0) {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1167              call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);                                                    pFrame->length, pFrame->intra);
1168          }          }
1169                  else  
1170                          stats->type = XVID_TYPE_NOTHING;          stop_global_timer();
1171            write_timer();
1172    
1173            emms();
1174            return XVID_ERR_OK;
1175      }      }
1176    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * closed-gop  
      * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe  
      * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
1177    
     if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {  
1178    
1179                  // place this frame back on the encoding-queue (head)  /*****************************************************************************
1180                  // we will deal with it next time   * "original" IP frame encoder entry point
1181                  pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);   *
1182          pEnc->queue_size++;   * Returned values :
1183                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);   *      - XVID_ERR_OK    - no errors
1184     *      - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1185     *                                              format
1186     ****************************************************************************/
1187    
1188          dec_frame_num(pEnc);  int
1189          pEnc->iFrameNum--;  encoder_encode(Encoder * pEnc,
1190                               XVID_ENC_FRAME * pFrame,
1191                               XVID_ENC_STATS * pResult)
1192    {
1193            uint16_t x, y;
1194            Bitstream bs;
1195            uint32_t bits;
1196            uint16_t write_vol_header = 0;
1197    
1198                  // grab the last frame from the bframe-queue          float psnr;
1199            char temp[128];
1200    
1201                  pEnc->bframenum_tail--;          start_global_timer();
                 SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);  
1202    
1203                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {          ENC_CHECK(pEnc);
1204                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");          ENC_CHECK(pFrame);
1205                  }          ENC_CHECK(pFrame->bitstream);
1206            ENC_CHECK(pFrame->image);
1207    
1208                  /* convert B-VOP quant to P-VOP */          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
                 pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;  
         type = P_VOP;  
     }  
1209    
1210            pEnc->current->global_flags = pFrame->general;
1211            pEnc->current->motion_flags = pFrame->motion;
1212            pEnc->mbParam.hint = &pFrame->hint;
1213    
1214          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          inc_frame_num(pEnc);
1215           * encode this frame as an i-vop  
1216       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */          /* disable alternate scan flag if interlacing is not enabled */
1217            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1218                    !(pEnc->current->global_flags & XVID_INTERLACING))
1219            {
1220                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1221            }
1222    
1223          if (type == I_VOP) {          start_timer();
1224            if (image_input
1225                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1226                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1227                    return XVID_ERR_FORMAT;
1228            stop_conv_timer();
1229    
1230                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",          if ((pFrame->general & XVID_CHROMAOPT)) {
1231                                  pEnc->bframenum_head, pEnc->bframenum_tail,                  image_chroma_optimize(&pEnc->current->image,
1232                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                          pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1233            }
1234    
1235                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {          if (pFrame->general & XVID_EXTRASTATS)
1236                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");          {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1237                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1238                  }                  }
1239    
1240            emms();
1241    
1242                  /* ---- update vol flags at IVOP ----------- */          BitstreamInit(&bs, pFrame->bitstream, 0);
                 pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;  
1243    
1244          if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {          if (pFrame->quant == 0) {
1245                          if (frame->quant_intra_matrix != NULL)                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1246                                  set_intra_matrix(frame->quant_intra_matrix);          } else {
1247                          if (frame->quant_inter_matrix != NULL)                  pEnc->current->quant = pFrame->quant;
                                 set_inter_matrix(frame->quant_inter_matrix);  
1248                  }                  }
1249    
1250          /* prevent vol/vop misuse */          if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1251                    pEnc->mbParam.m_quarterpel = 1;
1252            else
1253                    pEnc->mbParam.m_quarterpel = 0;
1254    
1255            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1256                    int *temp_dquants =
1257                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
1258                                                                    pEnc->mbParam.mb_height * sizeof(int),
1259                                                                    CACHE_LINE);
1260    
1261                    pEnc->current->quant =
1262                            adaptive_quantization(pEnc->current->image.y,
1263                                                                      pEnc->mbParam.edged_width, temp_dquants,
1264                                                                      pEnc->current->quant, pEnc->current->quant,
1265                                                                      2 * pEnc->current->quant,
1266                                                                      pEnc->mbParam.mb_width,
1267                                                                      pEnc->mbParam.mb_height);
1268    
1269                    for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1270    
1271    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1272    
1273          if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
             pEnc->current->vop_flags &= ~XVID_REDUCED;  
1274    
         if (!(pEnc->current->vol_flags & XVID_INTERLACING))  
             pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);  
1275    
1276                  /* ^^^------------------------ */                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1277    
1278          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                     image_copy(&pEnc->sOriginal, &pEnc->current->image,  
                            pEnc->mbParam.edged_width, pEnc->mbParam.height);  
1279          }          }
1280    
1281                  FrameCodeI(pEnc, &bs);  #undef OFFSET
1282                  xFrame->out_flags |= XVID_KEYFRAME;                  }
1283    
1284          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  xvid_free(temp_dquants);
1285           * encode this frame as an p-vop          }
      * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
1286    
1287      } else { // (type == P_VOP || type == S_VOP)          if (pEnc->current->global_flags & XVID_H263QUANT) {
1288                    if (pEnc->mbParam.m_quant_type != H263_QUANT)
1289                            write_vol_header = 1;
1290                    pEnc->mbParam.m_quant_type = H263_QUANT;
1291            } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1292                    int matrix1_changed, matrix2_changed;
1293    
1294                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  matrix1_changed = matrix2_changed = 0;
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
1295    
1296                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1297                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          write_vol_header = 1;
1298    
1299                    pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1300    
1301                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1302                            if (pFrame->quant_intra_matrix != NULL)
1303                                    matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1304                            if (pFrame->quant_inter_matrix != NULL)
1305                                    matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1306                    } else {
1307                            matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1308                            matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1309                    }
1310                    if (write_vol_header == 0)
1311                            write_vol_header = matrix1_changed | matrix2_changed;
1312                  }                  }
1313    
1314          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {          if (pFrame->intra < 0) {
1315                      image_copy(&pEnc->sOriginal, &pEnc->current->image,                  if ((pEnc->iFrameNum == -1)
1316                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1317                                    && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1318                            pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1319                    } else {
1320                            pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1321                    }
1322            } else {
1323                    if (pFrame->intra == 1) {
1324                            pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1325                    } else {
1326                            pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1327          }          }
1328    
                 FrameCodeP(pEnc, &bs, 1, 0);  
1329      }      }
1330    
1331            /* Relic from OpenDivX - now disabled
1332            BitstreamPutBits(&bs, 0xFFFF, 16);
1333            BitstreamPutBits(&bs, 0xFFFF, 16);
1334            */
1335    
1336      /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          BitstreamPadAlways(&bs);
1337           * on next enc_encode call we must flush bframes          pFrame->length = BitstreamLength(&bs);
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
1338    
1339  done_flush:          if (pResult) {
1340                    pResult->quant = pEnc->current->quant;
1341                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1342                    pResult->kblks = pEnc->current->sStat.kblks;
1343                    pResult->mblks = pEnc->current->sStat.mblks;
1344                    pResult->ublks = pEnc->current->sStat.ublks;
1345            }
1346    
1347      pEnc->flush_bframes = 1;          emms();
1348    
1349          /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */          if (pFrame->quant == 0) {
1350          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1351                  goto repeat;                                                    pFrame->length, pFrame->intra);
1352          }          }
1353            if (pFrame->general & XVID_EXTRASTATS)
1354            {
1355                    psnr =
1356                            image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1357                                               pEnc->mbParam.edged_width, pEnc->mbParam.width,
1358                                               pEnc->mbParam.height);
1359    
1360      /* packed or no-bframes: output stats */                  snprintf(temp, 127, "PSNR: %f\n", psnr);
     if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) {  
         call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);  
1361          }          }
1362    
1363          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          pEnc->iFrameNum++;
          * done; return number of bytes consumed  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
 done:  
1364    
1365          stop_global_timer();          stop_global_timer();
1366          write_timer();          write_timer();
1367    
1368          emms();          return XVID_ERR_OK;
         return BitstreamLength(&bs);  
1369  }  }
1370    
1371    
# Line 1252  Line 1382 
1382          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;
1383          pMB->sad16 = 0;          pMB->sad16 = 0;
1384    
1385          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1386                  if (pMB->dquant != NO_CHANGE) {                  if (pMB->dquant != NO_CHANGE) {
1387                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1388                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1268  Line 1398 
1398  }  }
1399    
1400    
1401    #define FCODEBITS       3
1402    #define MODEBITS        5
1403    
1404    void
1405    HintedMESet(Encoder * pEnc,
1406                            int *intra)
1407    {
1408            HINTINFO *hint;
1409            Bitstream bs;
1410            int length, high;
1411            uint32_t x, y;
1412    
1413            hint = pEnc->mbParam.hint;
1414    
1415            if (hint->rawhints) {
1416                    *intra = hint->mvhint.intra;
1417            } else {
1418                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1419                    *intra = BitstreamGetBit(&bs);
1420            }
1421    
1422            if (*intra) {
1423                    return;
1424            }
1425    
1426            pEnc->current->fcode = (hint->rawhints) ?
1427                    (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
1428    
1429            length = pEnc->current->fcode + 5;
1430            high = 1 << (length - 1);
1431    
1432            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1433                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1434                            MACROBLOCK *pMB =
1435                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1436                            MVBLOCKHINT *bhint =
1437                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1438                            VECTOR pred;
1439                            VECTOR tmp;
1440                            int vec;
1441    
1442                            pMB->mode =     (hint->rawhints) ?
1443                                    (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
1444    
1445                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1446                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1447    
1448                            if (pMB->mode == MODE_INTER) {
1449                                    tmp.x = (hint->rawhints) ?
1450                                            bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
1451                                    tmp.y = (hint->rawhints) ?
1452                                            bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
1453                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1454                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1455    
1456                                    pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1457    
1458                                    for (vec = 0; vec < 4; ++vec) {
1459                                            pMB->mvs[vec].x = tmp.x;
1460                                            pMB->mvs[vec].y = tmp.y;
1461                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1462                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1463                                    }
1464                            } else if (pMB->mode == MODE_INTER4V) {
1465                                    for (vec = 0; vec < 4; ++vec) {
1466                                            tmp.x = (hint->rawhints) ?
1467                                                    bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
1468                                            tmp.y = (hint->rawhints) ?
1469                                                    bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
1470                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1471                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1472    
1473                                            pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1474    
1475                                            pMB->mvs[vec].x = tmp.x;
1476                                            pMB->mvs[vec].y = tmp.y;
1477                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1478                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1479                                    }
1480                            } else                          /* intra / stuffing / not_coded */
1481                            {
1482                                    for (vec = 0; vec < 4; ++vec) {
1483                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1484                                    }
1485                            }
1486    
1487                            if (pMB->mode == MODE_INTER4V &&
1488                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1489                                    && pMB->dquant != NO_CHANGE) {
1490                                    pMB->mode = MODE_INTRA;
1491    
1492                                    for (vec = 0; vec < 4; ++vec) {
1493                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1494                                    }
1495                            }
1496                    }
1497            }
1498    }
1499    
1500    
1501    void
1502    HintedMEGet(Encoder * pEnc,
1503                            int intra)
1504    {
1505            HINTINFO *hint;
1506            Bitstream bs;
1507            uint32_t x, y;
1508            int length, high;
1509    
1510            hint = pEnc->mbParam.hint;
1511    
1512            if (hint->rawhints) {
1513                    hint->mvhint.intra = intra;
1514            } else {
1515                    BitstreamInit(&bs, hint->hintstream, 0);
1516                    BitstreamPutBit(&bs, intra);
1517            }
1518    
1519            if (intra) {
1520                    if (!hint->rawhints) {
1521                            BitstreamPadAlways(&bs);
1522                            hint->hintlength = BitstreamLength(&bs);
1523                    }
1524                    return;
1525            }
1526    
1527            length = pEnc->current->fcode + 5;
1528            high = 1 << (length - 1);
1529    
1530            if (hint->rawhints) {
1531                    hint->mvhint.fcode = pEnc->current->fcode;
1532            } else {
1533                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1534            }
1535    
1536            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1537                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1538                            MACROBLOCK *pMB =
1539                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1540                            MVBLOCKHINT *bhint =
1541                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1542                            VECTOR tmp;
1543    
1544                            if (hint->rawhints) {
1545                                    bhint->mode = pMB->mode;
1546                            } else {
1547                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1548                            }
1549    
1550                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1551                                    tmp.x = pMB->mvs[0].x;
1552                                    tmp.y = pMB->mvs[0].y;
1553                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1554                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1555    
1556                                    if (hint->rawhints) {
1557                                            bhint->mvs[0].x = tmp.x;
1558                                            bhint->mvs[0].y = tmp.y;
1559                                    } else {
1560                                            BitstreamPutBits(&bs, tmp.x, length);
1561                                            BitstreamPutBits(&bs, tmp.y, length);
1562                                    }
1563                            } else if (pMB->mode == MODE_INTER4V) {
1564                                    int vec;
1565    
1566                                    for (vec = 0; vec < 4; ++vec) {
1567                                            tmp.x = pMB->mvs[vec].x;
1568                                            tmp.y = pMB->mvs[vec].y;
1569                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1570                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1571    
1572                                            if (hint->rawhints) {
1573                                                    bhint->mvs[vec].x = tmp.x;
1574                                                    bhint->mvs[vec].y = tmp.y;
1575                                            } else {
1576                                                    BitstreamPutBits(&bs, tmp.x, length);
1577                                                    BitstreamPutBits(&bs, tmp.y, length);
1578                                            }
1579                                    }
1580                            }
1581                    }
1582            }
1583    
1584            if (!hint->rawhints) {
1585                    BitstreamPad(&bs);
1586                    hint->hintlength = BitstreamLength(&bs);
1587            }
1588    }
1589    
1590    
1591  static int  static int
1592  FrameCodeI(Encoder * pEnc,  FrameCodeI(Encoder * pEnc,
1593                     Bitstream * bs)                     Bitstream * bs,
1594                       uint32_t * pBits)
1595  {  {
     int bits = BitstreamPos(bs);  
1596          int mb_width = pEnc->mbParam.mb_width;          int mb_width = pEnc->mbParam.mb_width;
1597          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1598    
# Line 1282  Line 1601 
1601    
1602          uint16_t x, y;          uint16_t x, y;
1603    
1604          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))          if ((pEnc->current->global_flags & XVID_REDUCED))
1605          {          {
1606                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1607                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1295  Line 1614 
1614                          pEnc->mbParam.width, pEnc->mbParam.height);                          pEnc->mbParam.width, pEnc->mbParam.height);
1615                  stop_edges_timer();                  stop_edges_timer();
1616          }          }
1617            pEnc->iFrameNum = 0;
1618          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1619          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1620            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1621          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1622    
1623          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1624    
1625          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1626    
1627          BitstreamPadAlways(bs);          BitstreamPadAlways(bs);
1628          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1629    
1630            *pBits = BitstreamPos(bs);
1631    
1632          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1633          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1634          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
# Line 1326  Line 1648 
1648                          stop_prediction_timer();                          stop_prediction_timer();
1649    
1650                          start_timer();                          start_timer();
1651                          if (pEnc->current->vop_flags & XVID_GREYSCALE)                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1652                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1653                                  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 */
1654                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
# Line 1335  Line 1657 
1657                          stop_coding_timer();                          stop_coding_timer();
1658                  }                  }
1659    
1660          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->global_flags & XVID_REDUCED))
1661          {          {
1662                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1663                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1664                          16, 0);                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1665          }          }
1666          emms();          emms();
1667    
1668          /* for divx5 compatibility, we must always pad between the packed p and b frames */          *pBits = BitstreamPos(bs) - *pBits;
         if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)  
                 BitstreamPadAlways(bs);  
         else  
                 BitstreamPad(bs);  
     pEnc->current->length = (BitstreamPos(bs) - bits) / 8;  
   
1669          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1670          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1671    
     /* XXX: hinted me  
1672          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1673                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1674          }*/          }
1675    
1676          return 1;                                       /* intra */          return 1;                                       /* intra */
1677  }  }
# Line 1370  Line 1685 
1685  static int  static int
1686  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1687                     Bitstream * bs,                     Bitstream * bs,
1688                       uint32_t * pBits,
1689                     bool force_inter,                     bool force_inter,
1690                     bool vol_header)                     bool vol_header)
1691  {  {
1692          float fSigma;          float fSigma;
     int bits = BitstreamPos(bs);  
1693    
1694          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1695          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
# Line 1390  Line 1705 
1705          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1706          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1707    
1708          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->global_flags & XVID_REDUCED))
1709          {          {
1710                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1711                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
1712          }          }
1713    
   
1714          start_timer();          start_timer();
1715          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1716                                     pEnc->mbParam.width, pEnc->mbParam.height);                                     pEnc->mbParam.width, pEnc->mbParam.height);
# Line 1404  Line 1718 
1718    
1719          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1720          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1721          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1722          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1723    
1724          if (!force_inter)          if (!force_inter)
# Line 1412  Line 1726 
1726          else          else
1727                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1728    
1729          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1730                  start_timer();                  start_timer();
1731                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1732                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1733                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1734                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),                                                    pEnc->mbParam.m_quarterpel,
1735                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1736                  stop_inter_timer();                  stop_inter_timer();
1737          }          }
# Line 1425  Line 1739 
1739          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1740    
1741          start_timer();          start_timer();
1742          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1743                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1744          else*/          else
1745                  bIntra =                  bIntra =
1746                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1747                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1435  Line 1749 
1749    
1750          stop_motion_timer();          stop_motion_timer();
1751    
1752          if (bIntra == 1) return FrameCodeI(pEnc, bs);          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1753    
1754          if ( ( pEnc->current->vol_flags & XVID_GMC )          if ( ( pEnc->current->global_flags & XVID_GMC )
1755                  && ( (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) ) )
1756          {          {
1757                  pEnc->current->coding_type = S_VOP;                  pEnc->current->coding_type = S_VOP;
# Line 1449  Line 1763 
1763                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1764                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1765                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1766                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,                                  pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1767                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1768    
1769          }          }
1770    
1771          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1772          if (vol_header)          if (vol_header)
1773          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1774                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1775          }          }
1776    
1777          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1778    
1779            *pBits = BitstreamPos(bs);
1780    
1781          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1782                  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;
1783    
# Line 1508  Line 1824 
1824    
1825                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1826    
1827                                          if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                          if (pEnc->mbParam.m_quarterpel)
1828                                                  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;
1829                                          else                                          else
1830                                                  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 1531  Line 1847 
1847                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pEnc->mbParam.width,
1848                                                                   pEnc->mbParam.height,                                                                   pEnc->mbParam.height,
1849                                                                   pEnc->mbParam.edged_width,                                                                   pEnc->mbParam.edged_width,
1850                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),                                                                   pEnc->mbParam.m_quarterpel,
1851                                                                   (pEnc->current->vop_flags & XVID_REDUCED),                                                                   (pEnc->current->global_flags & XVID_REDUCED),
1852                                                                   pEnc->current->rounding_type);                                                                   pEnc->current->rounding_type);
1853    
1854                          stop_comp_timer();                          stop_comp_timer();
1855    
1856                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {                          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1857                                  if (pMB->dquant != NO_CHANGE) {                                  if (pMB->dquant != NO_CHANGE) {
1858                                          pMB->mode = MODE_INTER_Q;                                          pMB->mode = MODE_INTER_Q;
1859                                          pEnc->current->quant += DQtab[pMB->dquant];                                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1575  Line 1891 
1891                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1892                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1893                          else if (pEnc->current->coding_type == P_VOP) {                          else if (pEnc->current->coding_type == P_VOP) {
1894                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))                                  if (pEnc->mbParam.m_quarterpel)
1895                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1896                                  else                                  else
1897                                          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 1602  Line 1918 
1918                                          }                                          }
1919    
1920                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1921                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                                  if(pEnc->mbParam.m_quarterpel) {
1922                                                          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);
1923                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1924                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
# Line 1629  Line 1945 
1945                          }                          }
1946                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1947    
1948                          if ((pEnc->current->vop_flags & XVID_GREYSCALE))                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1949                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1950                                  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 */
1951                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1952                          }                          }
1953    
1954                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                          if(pEnc->mbParam.m_quarterpel) {
1955                                  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);
1956                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1957                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
# Line 1652  Line 1968 
1968                          {       int k;                          {       int k;
1969                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1970                                  {                                  {
1971                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {                                          if(pEnc->mbParam.m_quarterpel) {
1972                                                  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);
1973                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1974                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
# Line 1673  Line 1989 
1989                  }                  }
1990          }          }
1991    
1992          if ((pEnc->current->vop_flags & XVID_REDUCED))          if ((pEnc->current->global_flags & XVID_REDUCED))
1993          {          {
1994                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1995                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1996                          16, 0);                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1997          }          }
1998    
1999          emms();          emms();
2000    
     /* XXX: hinted me  
2001          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
2002                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2003          }*/          }
2004    
2005          if (pEnc->current->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2006                  pEnc->current->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
# Line 1695  Line 2010 
2010          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2011    
2012          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2013          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0)  ))) /* maximum search range 128 */                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */
2014          {          {
2015                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2016                  iSearchRange *= 2;                  iSearchRange *= 2;
2017          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2018                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2019                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2020                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) )))        /* minimum search range 16 */                             && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel)))      /* minimum search range 16 */
2021          {          {
2022                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2023                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1727  Line 2042 
2042                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
2043                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
2044                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
2045                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  pEnc->current->quarterpel =  pEnc->reference->quarterpel;
2046                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
2047                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
2048                  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 1749  Line 2064 
2064          }          }
2065          */          */
2066    
         /* for divx5 compatibility, we must always pad between the packed p and b frames */  
         if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)  
                 BitstreamPadAlways(bs);  
         else  
                 BitstreamPad(bs);  
2067    
2068      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          *pBits = BitstreamPos(bs) - *pBits;
2069    
2070          return 0;                                       /* inter */          return 0;                                       /* inter */
2071  }  }
# Line 1764  Line 2074 
2074  static void  static void
2075  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2076                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2077                     Bitstream * bs)                     Bitstream * bs,
2078                       uint32_t * pBits)
2079  {  {
     int bits = BitstreamPos(bs);  
2080          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2081          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2082          uint32_t x, y;          uint32_t x, y;
# Line 1781  Line 2091 
2091                  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); \
2092          }          }
2093    
2094          /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */          pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2095    
2096          if (!first){          if (!first){
2097                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2098          }          }
2099  #endif  #endif
2100    
2101          //frame->quarterpel =  pEnc->mbParam.m_quarterpel;          frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2102    
2103          /* forward  */          /* forward  */
2104          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
# Line 1797  Line 2107 
2107          start_timer();          start_timer();
2108          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,
2109                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2110                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                            pEnc->mbParam.m_quarterpel, 0);
2111          stop_inter_timer();          stop_inter_timer();
2112    
2113          /* backward */          /* backward */
# Line 1807  Line 2117 
2117          start_timer();          start_timer();
2118          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2119                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2120                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);                                            pEnc->mbParam.m_quarterpel, 0);
2121          stop_inter_timer();          stop_inter_timer();
2122    
2123          start_timer();          start_timer();
# Line 1822  Line 2132 
2132    
2133    
2134          stop_motion_timer();          stop_motion_timer();
   
2135          /*          /*
2136          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
2137                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
# Line 1834  Line 2143 
2143          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2144          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
2145    
2146            *pBits = BitstreamPos(bs);
2147    
2148          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2149          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2150          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2151          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2152          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;  
         frame->sStat.kblks = frame->sStat.ublks = 0;  
2153    
2154          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2155                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2156                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2157                          int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2158    
2159                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2160                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 1863  Line 2173 
2173                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
2174    
2175                                  mb->cbp =                                  mb->cbp =
2176                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
2177    
2178                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2179                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
# Line 1885  Line 2195 
2195    
2196          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
2197    
2198      BitstreamPad(bs);          *pBits = BitstreamPos(bs) - *pBits;
         frame->length = (BitstreamPos(bs) - bits) / 8;  
2199    
2200  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2201          if (!first){          if (!first){

Legend:
Removed from v.1.95.2.7  
changed lines
  Added in v.1.96

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