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

Diff of /xvidcore/src/encoder.c

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

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

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

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