[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.6, Sat Mar 15 16:04:38 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            simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
170    
171          /* 1 keyframe each 10 seconds */      /* plugin */
172        pEnc->num_plugins = create->num_plugins;
173        pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
174        if (pEnc->plugins == NULL)
175            goto xvid_err_memory0;
176    
177          if (pParam->max_key_interval <= 0)      for (n=0; n<pEnc->num_plugins;n++) {
178                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;          xvid_plg_create_t pcreate;
179            xvid_plg_info_t pinfo;
180    
181          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          memset(&pinfo, 0, sizeof(xvid_plg_info_t));
182          if (pEnc == NULL)          pinfo.version = XVID_VERSION;
183                  return XVID_ERR_MEMORY;          if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) {
184                pEnc->mbParam.plugin_flags |= pinfo.flags;
185            }
186    
187          /* Zero the Encoder Structure */          memset(&pcreate, 0, sizeof(xvid_plg_create_t));
188            pcreate.version = XVID_VERSION;
189            pcreate.width = pEnc->mbParam.width;
190            pcreate.height = pEnc->mbParam.height;
191            pcreate.fincr = pEnc->mbParam.fincr;
192            pcreate.fbase = pEnc->mbParam.fbase;
193            pcreate.param = create->plugins[n].param;
194    
195          memset(pEnc, 0, sizeof(Encoder));          pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
196            if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
197                pEnc->plugins[n].func = create->plugins[n].func;
198            }
199        }
200    
201          /* Fill members of Encoder structure */      if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) ||
202            (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
203            pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
204        }
205    
206          pEnc->mbParam.width = pParam->width;      /* temp dquants */
207          pEnc->mbParam.height = pParam->height;          pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
208                                            pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
209        /* XXX: error checking */
210    
211          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;          /* bframes */
212          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
213            pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
214            pEnc->mbParam.bquant_offset = create->bquant_offset;
215    
216          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          /* frame drop ratio */
217          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
218    
219          pEnc->mbParam.fbase = pParam->fbase;      /* max keyframe interval */
220          pEnc->mbParam.fincr = pParam->fincr;      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval;
221        /*XXX: replace 250 hard code with "10seconds * framerate" */
222    
223          pEnc->mbParam.m_quant_type = H263_QUANT;          /* Bitrate allocator defaults
224    
225          pEnc->fMvPrevSigma = -1;          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))
226                    create->min_quantizer = 1;
227    
228          /* Fill rate control parameters */          if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))
229                    create->max_quantizer = 31;
230    
231          pEnc->bitrate = pParam->rc_bitrate;          if (create->max_quantizer < create->min_quantizer)
232                    create->max_quantizer = create->min_quantizer; */
233    
         pEnc->iFrameNum = -1;  
         pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;  
234    
235          /* try to allocate frame memory */      /* allocate working frame-image memory */
236    
237          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
238          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
# Line 227  Line 240 
240          if (pEnc->current == NULL || pEnc->reference == NULL)          if (pEnc->current == NULL || pEnc->reference == NULL)
241                  goto xvid_err_memory1;                  goto xvid_err_memory1;
242    
243          /* try to allocate mb memory */          /* allocate macroblock memory */
244    
245          pEnc->current->mbs =          pEnc->current->mbs =
246                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
# Line 239  Line 252 
252          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
253                  goto xvid_err_memory2;                  goto xvid_err_memory2;
254    
255          /* try to allocate image memory */          /* allocate interpolation image memory */
256    
257          if (pParam->global & XVID_GLOBAL_EXTRASTATS)      if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
258                  image_null(&pEnc->sOriginal);                  image_null(&pEnc->sOriginal);
259            image_null(&pEnc->sOriginal2);
260        }
261    
262          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
263          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 256  Line 271 
271          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
272          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
273    
274          if (pParam->global & XVID_GLOBAL_EXTRASTATS)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
275          {       if (image_create          if (image_create
276                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
277                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
278                          goto xvid_err_memory3;                          goto xvid_err_memory3;
279    
280            if (image_create
281                            (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
282                             pEnc->mbParam.edged_height) < 0)
283                            goto xvid_err_memory3;
284          }          }
285    
286          if (image_create          if (image_create
# Line 311  Line 331 
331                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
332                  goto xvid_err_memory3;                  goto xvid_err_memory3;
333    
334            /* init bframe image buffers */
335    
336            pEnc->bframenum_head = 0;
337          pEnc->mbParam.global = pParam->global;          pEnc->bframenum_tail = 0;
338            pEnc->flush_bframes = 0;
339            pEnc->closed_bframenum = -1;
340    
341          /* 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;  
342          pEnc->bframes = NULL;          pEnc->bframes = NULL;
343    
344          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
                 int n;  
345    
346                  pEnc->bframes =                  pEnc->bframes =
347                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
# Line 359  Line 377 
377                  }                  }
378          }          }
379    
380          pEnc->bframenum_head = 0;      /* init incoming frame queue */
381          pEnc->bframenum_tail = 0;          pEnc->queue_head = 0;
382          pEnc->flush_bframes = 0;          pEnc->queue_tail = 0;
383          pEnc->bframenum_dx50bvop = -1;          pEnc->queue_size = 0;
   
         pEnc->queue = NULL;  
   
   
         if (pEnc->mbParam.max_bframes > 0) {  
                 int n;  
384    
385                  pEnc->queue =                  pEnc->queue =
386                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),                  xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
387                                                  CACHE_LINE);                                                  CACHE_LINE);
388    
389                  if (pEnc->queue == NULL)                  if (pEnc->queue == NULL)
390                          goto xvid_err_memory4;                          goto xvid_err_memory4;
391    
392                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
393                          image_null(&pEnc->queue[n]);                  image_null(&pEnc->queue[n].image);
394    
395                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {  
396            for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
397            {
398                          if (image_create                          if (image_create
399                                  (&pEnc->queue[n], pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
400                                   pEnc->mbParam.edged_height) < 0)                                   pEnc->mbParam.edged_height) < 0)
401                                  goto xvid_err_memory5;                                  goto xvid_err_memory5;
402    
403                  }                  }
         }  
404    
         pEnc->queue_head = 0;  
         pEnc->queue_tail = 0;  
         pEnc->queue_size = 0;  
405    
406          pEnc->mbParam.m_stamp = 0;          /* timestamp stuff */
407    
408            pEnc->mbParam.m_stamp = 0;
409          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
410          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
411          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
412    
413          pParam->handle = (void *) pEnc;          /* other stuff */
414    
415          if (pParam->rc_bitrate) {          pEnc->iFrameNum = 0;
416                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,          pEnc->fMvPrevSigma = -1;
417                                                  pParam->rc_reaction_delay_factor,  
418                                                  pParam->rc_averaging_period, pParam->rc_buffer,      create->handle = (void *) pEnc;
                                                 pParam->fbase * 1000 / pParam->fincr,  
                                                 pParam->max_quantizer, pParam->min_quantizer);  
         }  
419    
420          init_timer();          init_timer();
421    
422          return XVID_ERR_OK;      return 0;   /* ok */
423    
424          /*          /*
425           * 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 427 
427    
428    xvid_err_memory5:    xvid_err_memory5:
429    
   
430          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
431            int i;
432    
433                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
434                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
435                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
436                  }                  }
437                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
# Line 432  Line 440 
440    xvid_err_memory4:    xvid_err_memory4:
441    
442          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
443            int i;
444    
445                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
446    
# Line 452  Line 461 
461    
462    xvid_err_memory3:    xvid_err_memory3:
463    
464          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
465          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
466                                              pEnc->mbParam.edged_height);
467                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
468                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
469          }          }
470    
# Line 491  Line 502 
502    xvid_err_memory1:    xvid_err_memory1:
503          xvid_free(pEnc->current);          xvid_free(pEnc->current);
504          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
505    
506            xvid_free(pEnc->temp_dquants);
507    
508      xvid_err_memory0:
509        for (n=0; n<pEnc->num_plugins;n++) {
510            if (pEnc->plugins[n].func) {
511                pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0);
512            }
513        }
514        xvid_free(pEnc->plugins);
515    
516          xvid_free(pEnc);          xvid_free(pEnc);
517    
518          pParam->handle = NULL;          create->handle = NULL;
519    
520          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
521  }  }
# Line 502  Line 524 
524   * Encoder destruction   * Encoder destruction
525   *   *
526   * This function destroy the entire encoder structure created by a previous   * This function destroy the entire encoder structure created by a previous
527   * successful encoder_create call.   * successful enc_create call.
528   *   *
529   * Returned values (for now only one returned value) :   * Returned values (for now only one returned value) :
530   *    - XVID_ERR_OK     - no errors   *    - 0     - no errors
531   *   *
532   ****************************************************************************/   ****************************************************************************/
533    
534  int  int
535  encoder_destroy(Encoder * pEnc)  enc_destroy(Encoder * pEnc)
536  {  {
537          int i;          int i;
538    
         ENC_CHECK(pEnc);  
   
539          /* B Frames specific */          /* B Frames specific */
540          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
541    
542                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
543    
544                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
545                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
546                  }                  }
547                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
# Line 571  Line 591 
591          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
592                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
593    
594          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
595          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
596                                              pEnc->mbParam.edged_height);
597                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
598                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
599          }          }
600    
# Line 584  Line 606 
606          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
607          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
608    
609        xvid_free(pEnc->temp_dquants);
610    
611        for (i=0; i<pEnc->num_plugins;i++) {
612            if (pEnc->plugins[i].func) {
613                pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0);
614            }
615        }
616        xvid_free(pEnc->plugins);
617    
618          xvid_free(pEnc);          xvid_free(pEnc);
619    
620          return XVID_ERR_OK;          return 0;  /* ok */
621  }  }
622    
623    
624  static __inline void inc_frame_num(Encoder * pEnc)  /*
625      call the plugins
626      */
627    
628    static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
629                             int opt, int * type, int * quant, xvid_enc_stats_t * stats)
630  {  {
631          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */      int i;
632          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;      xvid_plg_data_t data;
633    
634        memset(&data, 0, sizeof(xvid_plg_data_t));
635        data.version = XVID_VERSION;
636    
637        data.width = pEnc->mbParam.width;
638        data.height = pEnc->mbParam.height;
639        data.fincr = pEnc->mbParam.fincr;
640        data.fbase = pEnc->mbParam.fbase;
641    
642        data.reference.csp = XVID_CSP_USER;
643        data.reference.plane[0] = pEnc->reference->image.y;
644        data.reference.plane[1] = pEnc->reference->image.u;
645        data.reference.plane[2] = pEnc->reference->image.v;
646        data.reference.stride[0] = pEnc->mbParam.edged_width;
647        data.reference.stride[1] = pEnc->mbParam.edged_width/2;
648        data.reference.stride[2] = pEnc->mbParam.edged_width/2;
649    
650        data.current.csp = XVID_CSP_USER;
651        data.current.plane[0] = frame->image.y;
652        data.current.plane[1] = frame->image.u;
653        data.current.plane[2] = frame->image.v;
654        data.current.stride[0] = pEnc->mbParam.edged_width;
655        data.current.stride[1] = pEnc->mbParam.edged_width/2;
656        data.current.stride[2] = pEnc->mbParam.edged_width/2;
657    
658        data.frame_num = frame->frame_num;
659    
660        if (opt == XVID_PLG_BEFORE) {
661            data.type = XVID_TYPE_AUTO;
662            data.quant = 2;
663            //memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height);
664            //data.qscale_stride = pEnc->mbParam.width;
665            //data.qscale_table = pEnc->temp_dquants;
666            /* todo: vol,vop,motion flags */
667    
668        } else { // XVID_PLG_AFTER
669            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
670                data.original.csp = XVID_CSP_USER;
671                data.original.plane[0] = original->y;
672                data.original.plane[1] = original->u;
673                data.original.plane[2] = original->v;
674                data.original.stride[0] = pEnc->mbParam.edged_width;
675                data.original.stride[1] = pEnc->mbParam.edged_width/2;
676                data.original.stride[2] = pEnc->mbParam.edged_width/2;
677  }  }
678    
679            if ((frame->vol_flags & XVID_EXTRASTATS) ||
680                (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
681    
682  static __inline void                          data.sse_y =
683  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)                              plane_sse( original->y, frame->image.y,
684  {                                                 pEnc->mbParam.edged_width, pEnc->mbParam.width,
685          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)                                                 pEnc->mbParam.height);
686          {  
687                  DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");              data.sse_u =
688                  return;                  plane_sse( original->u, frame->image.u,
689                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
690                                                   pEnc->mbParam.height/2);
691    
692                            data.sse_v =
693                    plane_sse( original->v, frame->image.v,
694                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
695                                                   pEnc->mbParam.height/2);
696          }          }
697    
698          DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",          data.type = coding2type(frame->coding_type);
699                                  pEnc->bframenum_head, pEnc->bframenum_tail,          data.quant = frame->quant;
700                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);          /* todo: data.qscale */
701            data.vol_flags = frame->vol_flags;
702            data.vop_flags = frame->vop_flags;
703            data.motion_flags = frame->motion_flags;
704    
705            data.length = frame->length;
706            data.kblks = frame->sStat.kblks;
707            data.mblks = frame->sStat.mblks;
708            data.ublks = frame->sStat.ublks;
709    
710            if (stats)
711            {
712                    stats->type = coding2type(frame->coding_type);
713                    stats->quant = frame->quant;
714                    stats->vol_flags = frame->vol_flags;
715                    stats->vop_flags = frame->vop_flags;
716                    stats->length = frame->length;
717                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
718                    stats->kblks = frame->sStat.kblks;
719                    stats->mblks = frame->sStat.mblks;
720                    stats->ublks = frame->sStat.ublks;
721                stats->sse_y = data.sse_y;
722                stats->sse_u = data.sse_u;
723                stats->sse_v = data.sse_v;
724            }
725        }
726    
727        emms();
728        for (i=0; i<pEnc->num_plugins;i++) {
729            if (pEnc->plugins[i].func) {
730                if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) {
731                    continue;
732                }
733            }
734        }
735        emms();
736    
737          start_timer();      if (opt == XVID_PLG_BEFORE) {
738          if (image_input          *type = data.type;
739                  (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,          *quant = data.quant;
740                   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*/
741                  return;      }
         stop_conv_timer();  
742    
         if ((pFrame->general & XVID_CHROMAOPT)) {  
                 image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],  
                         pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
743          }          }
744    
745          pEnc->queue_size++;  
746          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;  
747    
748    static __inline void inc_frame_num(Encoder * pEnc)
749    {
750        pEnc->current->frame_num = pEnc->m_framenum;
751            pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
752    
753        pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
754        pEnc->m_framenum++; /* debug ticker */
755  }  }
756    
757    static __inline void dec_frame_num(Encoder * pEnc)
758    {
759        pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
760        pEnc->m_framenum--; /* debug ticker */
761    }
762    
763    
764    
765  static __inline void  static __inline void
766  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
767  {  {
# Line 648  Line 783 
783    
784    
785    
 /* 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;  
 }  
   
   
   
786  /*****************************************************************************  /*****************************************************************************
787   * IPB frame encoder entry point   * IPB frame encoder entry point
788   *   *
789   * Returned values :   * Returned values :
790   *    - XVID_ERR_OK     - no errors   *    - >0               - output bytes
791     *    - 0                - no output
792     *    - XVID_ERR_VERSION - wrong version passed to core
793     *    - XVID_ERR_END     - End of stream reached before end of coding
794   *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong   *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
795   *                        format   *                        format
796   ****************************************************************************/   ****************************************************************************/
797    
798    
799  int  int
800  encoder_encode_bframes(Encoder * pEnc,  enc_encode(Encoder * pEnc,
801                             XVID_ENC_FRAME * pFrame,                             xvid_enc_frame_t * xFrame,
802                             XVID_ENC_STATS * pResult)                             xvid_enc_stats_t * stats)
803  {  {
804          uint16_t x, y;          xvid_enc_frame_t * frame;
805            int type;
806          Bitstream bs;          Bitstream bs;
         uint32_t bits;  
         int mode = -1; /* Just to shut up compiler warning */  
807    
808          int input_valid = 1;          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */
809          int bframes_count = 0;                  return XVID_ERR_VERSION;
810    
811          ENC_CHECK(pEnc);          xFrame->out_flags = 0;
         ENC_CHECK(pFrame);  
         ENC_CHECK(pFrame->image);  
812    
813          start_global_timer();          start_global_timer();
814            BitstreamInit(&bs, xFrame->bitstream, 0);
815    
         BitstreamInit(&bs, pFrame->bitstream, 0);  
   
 ipvop_loop:  
816    
817          /*          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818           * bframe "flush" code           * enqueue image to the encoding-queue
819           */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
   
         if ((pFrame->image == NULL || pEnc->flush_bframes)  
                 && (pEnc->bframenum_head < pEnc->bframenum_tail)) {  
820    
821                  if (pEnc->flush_bframes == 0) {          if (xFrame->input.csp != XVID_CSP_NULL)
822                          /*          {
823                           * we have reached the end of stream without getting                  QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];
                          * a future reference frame... so encode last final  
                          * frame as a pframe  
                          */  
824    
825                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  start_timer();
826                                  pEnc->bframenum_head, pEnc->bframenum_tail,                  if (image_input
827                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
828                            pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
829                            xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))
830                    {
831                            emms();
832                            return XVID_ERR_FORMAT;
833                    }
834                    stop_conv_timer();
835    
836                          pEnc->bframenum_tail--;                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
837                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                          image_chroma_optimize(&q->image,
838                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
839                    }
840    
841                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  q->frame = *xFrame;
842    
843                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                  if (xFrame->quant_intra_matrix)
844                          bframes_count = 0;                  {
845                            memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));
846                            q->frame.quant_intra_matrix = q->quant_intra_matrix;
847                    }
848    
849                          BitstreamPadAlways(&bs);                  if (xFrame->quant_inter_matrix)
850                          pFrame->length = BitstreamLength(&bs);                  {
851                          pFrame->intra = 0;                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));
852                            q->frame.quant_inter_matrix = q->quant_inter_matrix;
853                    }
854    
855                    pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
856                    pEnc->queue_size++;
857            }
858    
                         emms();  
859    
860                          if (pResult) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861                                  pResult->quant = pEnc->current->quant;           * bframe flush code
862                                  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;  
                         }  
863    
864                          return XVID_ERR_OK;  repeat:
                 }  
865    
866            if (pEnc->flush_bframes)
867            {
868                    if (pEnc->bframenum_head < pEnc->bframenum_tail) {
869    
870                  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",
871                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
872                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
873    
874                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);              if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
875                  pEnc->bframenum_head++;                                  image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
876                                                       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;  
877                  }                  }
878    
879                  if (input_valid)                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
880                          queue_image(pEnc, pFrame);              call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats);
881                            pEnc->bframenum_head++;
                 emms();  
882    
883                  return XVID_ERR_OK;                          goto done;
884          }          }
885    
         if (pEnc->bframenum_head > 0) {  
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
   
886                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
887    
888                     for divx5 decoder compatibility, this marker must consist                     for divx5 decoder compatibility, this marker must consist
# Line 774  Line 890 
890                     indentical to the future-referece frame.                     indentical to the future-referece frame.
891                  */                  */
892    
893                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) {
894                          int tmp;                          int tmp;
895                            int bits;
896    
897                          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",
898                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
899                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
900    
901                            bits = BitstreamPos(&bs);
902    
903                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
904                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
905    
906                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
907                            BitstreamPad(&bs);
908                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
909    
910                          BitstreamPadAlways(&bs);                          /* add the not-coded length to the reference frame size */
911                          pFrame->length = BitstreamLength(&bs);                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
912                          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();  
   
                         return XVID_ERR_OK;  
                 }  
         }  
   
   
 bvop_loop:  
   
         if (pEnc->bframenum_dx50bvop != -1)  
         {  
   
                 SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);  
                 SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);  
913    
914                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {              /* flush complete: reset counters */
915                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");                  pEnc->flush_bframes = 0;
916                  }                      pEnc->bframenum_head = pEnc->bframenum_tail = 0;
917                            goto done;
918    
                 if (input_valid)  
                 {  
                         queue_image(pEnc, pFrame);  
                         input_valid = 0;  
919                  }                  }
920    
921          } else if (input_valid) {          /* flush complete: reset counters */
922                    pEnc->flush_bframes = 0;
923                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
   
                 start_timer();  
                 if (image_input  
                         (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                         pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))  
                 {  
                         emms();  
                         return XVID_ERR_FORMAT;  
924                  }                  }
                 stop_conv_timer();  
925    
926                  if ((pFrame->general & XVID_CHROMAOPT)) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927                          image_chroma_optimize(&pEnc->current->image,           * dequeue frame from the encoding queue
928                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                 }  
929    
930                  /* queue input frame, and dequue next image */          if (pEnc->queue_size == 0)              /* empty */
                 if (pEnc->queue_size > 0)  
931                  {                  {
932                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
                         if (pEnc->queue_head != pEnc->queue_tail)  
933                          {                          {
                                 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) {  
   
                 SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);  
   
                 image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);  
                 pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;  
                 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();  
934    
935                  return XVID_ERR_OK;              if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) {
936                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
937          }          }
938    
939          pEnc->flush_bframes = 0;              /* if the very last frame is to be b-vop, we must change it to a p-vop */
940                if (pEnc->bframenum_tail > 0)
         emms();  
   
         /* only inc frame num, adapt quant, etc. if we havent seen it before */  
         if (pEnc->bframenum_dx50bvop < 0 )  
941          {          {
                 mode = intra2coding_type(pFrame->intra);  
                 if (pFrame->quant == 0)  
                         pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
                 else  
                         pEnc->current->quant = pFrame->quant;  
942    
943  /*              if (pEnc->current->quant < 1)                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
944                          pEnc->current->quant = 1;                                  pEnc->bframenum_tail--;
945                                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
                 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;  
946    
947                  inc_frame_num(pEnc);                                  /* convert B-VOP to P-VOP */
948                    pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
949    
950                  if (pFrame->general & XVID_EXTRASTATS)                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
951                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,                              image_copy(&pEnc->sOriginal, &pEnc->current->image,
952                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
953                  }                  }
954    
955                  emms();                                  FrameCodeP(pEnc, &bs, 1, 0);
956    
957                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                                  goto done_flush;
958                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,                          }
959                                  "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);  
960                            emms();
961                            return XVID_ERR_END;    /* end of stream reached */
962                    }
963                    goto done;      /* nothing to encode yet; encoder lag */
964                  }                  }
965    
966            // the current FRAME becomes the reference
967            SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
968    
969            // remove frame from encoding-queue (head), and move it into the current
970            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
971            frame = &pEnc->queue[pEnc->queue_head].frame;
972            pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
973            pEnc->queue_size--;
974    
975    
976          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
977           * Luminance masking           * init pEnc->current fields
978           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
979    
980                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
981                          int *temp_dquants =      pEnc->current->vop_flags = frame->vop_flags;
982                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *          pEnc->current->motion_flags = frame->motion;
983                                                                  pEnc->mbParam.mb_height * sizeof(int),          pEnc->current->fcode = pEnc->mbParam.m_fcode;
984                                                                  CACHE_LINE);          pEnc->current->bcode = pEnc->mbParam.m_fcode;
985    
986        if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
987                image_chroma_optimize(&pEnc->current->image,
988                        pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
989        }
990    
991            emms();         /* float-point region */
992            if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
993            unsigned int x, y;
994    
995                          pEnc->current->quant =                          pEnc->current->quant =
996                                  adaptive_quantization(pEnc->current->image.y,                                  adaptive_quantization(pEnc->current->image.y,
997                                                                    pEnc->mbParam.edged_width, temp_dquants,                                                            pEnc->mbParam.edged_width, pEnc->temp_dquants,
998                                                                    pEnc->current->quant, pEnc->current->quant,                                                                    pEnc->current->quant, pEnc->current->quant,
999                                                                    2 * pEnc->current->quant,                                                                    2 * pEnc->current->quant,
1000                                                                    pEnc->mbParam.mb_width,                                                                    pEnc->mbParam.mb_width,
# Line 988  Line 1007 
1007                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1008                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1009    
1010                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                  pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2];
1011                                  }                                  }
1012    
1013  #undef OFFSET  #undef OFFSET
1014                          }                          }
1015    
                         xvid_free(temp_dquants);  
                 }  
   
1016          }          }
1017        emms();             /* END floating-point region */
1018    
1019          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1020           * ivop/pvop/bvop selection           * frame type & quant selection
1021           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
         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;  
1022    
1023                  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);
1024    
1025                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {      if (frame->type > 0)
1026                          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);  
                 }  
1027    
1028        if (frame->quant > 0)
1029                    pEnc->current->quant = frame->quant;
1030    
1031                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",      if (type > 0){      /* XVID_TYPE_?VOP */
1032                                  pEnc->bframenum_head, pEnc->bframenum_tail,                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1033                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);      } else{             /* XVID_TYPE_AUTO */
1034                    if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){
1035                            pEnc->iFrameNum = 0;
1036                            type = I_VOP;
1037                    }else{
1038                            type = MEanalysis(&pEnc->reference->image, pEnc->current,
1039                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1040                                            pEnc->iFrameNum, pEnc->bframenum_tail);
1041    
1042                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {              if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) {
1043                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                  type = P_VOP;   /* disable dynamic bframes */
1044                  }                  }
   
                 /* 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");  
1045                          }                          }
                         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;  
1046                  }                  }
1047    
1048                  pEnc->flush_bframes = 1;      /* bframes buffer overflow check */
1049        if (type != I_VOP) {
1050                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {          if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1051                          BitstreamPadAlways(&bs);              type = P_VOP;
1052                          input_valid = 0;          }else{
1053                          goto ipvop_loop;              type = B_VOP;
1054                  }                  }
   
                 /*  
                  * 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");  
1055                  }                  }
1056    
1057                  FrameCodeP(pEnc, &bs, &bits, 1, 0);          inc_frame_num(pEnc);
1058                  bframes_count = 0;          pEnc->iFrameNum++;
                 pFrame->intra = 0;  
                 pEnc->flush_bframes = 1;  
1059    
1060                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1061                          BitstreamPadAlways(&bs);                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1062                          input_valid = 0;                          "%i  st:%i  if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
                         goto ipvop_loop;  
1063                  }                  }
1064    
1065          } else {        /* mode == B_VOP */          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066                  /*           * encode this frame as a b-vop
1067                   * 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)
1068                   */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1069            if (type == B_VOP) {
1070                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1071                          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");
1072                  }                  }
1073    
1074                  if (pFrame->bquant < 1) {                  if (frame->bquant < 1) {
1075                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1076                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1077    
1078                  } else {                  } else {
1079                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = frame->bquant;
1080                  }                  }
1081    
1082                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1125  Line 1084 
1084                  else if (pEnc->current->quant > 31)                  else if (pEnc->current->quant > 31)
1085              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1086    
1087                  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",
1088                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1089                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1090    
# Line 1135  Line 1094 
1094    
1095                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1096    
1097                  /* 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);  
1098                  }                  }
         }  
   
         emms();  
1099    
1100          if (pFrame->quant == 0) {      /* for unpacked bframes, output the stats for the last encoded frame */
1101                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,      if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1102                                                    pFrame->length, pFrame->intra);      {
1103            if (pEnc->current->stamp > 0) {
1104                call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1105          }          }
1106                    else
1107          stop_global_timer();                          stats->type = XVID_TYPE_NOTHING;
         write_timer();  
   
         emms();  
         return XVID_ERR_OK;  
1108  }  }
1109    
1110            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1111             * closed-gop
1112         * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1113         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1114    
1115        if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1116    
1117  /*****************************************************************************                  // place this frame back on the encoding-queue (head)
1118   * "original" IP frame encoder entry point                  // we will deal with it next time
1119   *                  pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1120   * Returned values :          pEnc->queue_size++;
1121   *    - 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;  
1122    
1123          inc_frame_num(pEnc);          dec_frame_num(pEnc);
1124            pEnc->iFrameNum--;
1125    
1126          /* 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;  
         }  
1127    
1128          start_timer();                  pEnc->bframenum_tail--;
1129          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();  
1130    
1131          if ((pFrame->general & XVID_CHROMAOPT)) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1132                  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);  
1133          }          }
1134    
1135          if (pFrame->general & XVID_EXTRASTATS)                  /* convert B-VOP quant to P-VOP */
1136          {       image_copy(&pEnc->sOriginal, &pEnc->current->image,                  pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio;
1137                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);          type = P_VOP;
1138          }          }
1139    
         emms();  
   
         BitstreamInit(&bs, pFrame->bitstream, 0);  
1140    
1141          if (pFrame->quant == 0) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1142                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);           * encode this frame as an i-vop
1143          } else {       * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                 pEnc->current->quant = pFrame->quant;  
         }  
1144    
1145          if ((pEnc->current->global_flags & XVID_QUARTERPEL))          if (type == I_VOP) {
                 pEnc->mbParam.m_quarterpel = 1;  
         else  
                 pEnc->mbParam.m_quarterpel = 0;  
1146    
1147          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1148                  int *temp_dquants =                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1149                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                                                                 pEnc->mbParam.mb_height * sizeof(int),  
                                                                 CACHE_LINE);  
1150    
1151                  pEnc->current->quant =                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1152                          adaptive_quantization(pEnc->current->image.y,                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1153                                                                    pEnc->mbParam.edged_width, temp_dquants,                  }
                                                                   pEnc->current->quant, pEnc->current->quant,  
                                                                   2 * pEnc->current->quant,  
                                                                   pEnc->mbParam.mb_width,  
                                                                   pEnc->mbParam.mb_height);  
1154    
                 for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1155    
1156  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                  /* ---- update vol flags at IVOP ----------- */
1157                    pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
1158    
1159                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {          if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {
1160                            if (frame->quant_intra_matrix != NULL)
1161                                    set_intra_matrix(frame->quant_intra_matrix);
1162                            if (frame->quant_inter_matrix != NULL)
1163                                    set_inter_matrix(frame->quant_inter_matrix);
1164                    }
1165    
1166            /* prevent vol/vop misuse */
1167    
1168                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];          if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
1169                pEnc->current->vop_flags &= ~XVID_REDUCED;
1170    
1171                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];          if (!(pEnc->current->vol_flags & XVID_INTERLACING))
1172                          }              pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);
1173    
1174  #undef OFFSET                  /* ^^^------------------------ */
                 }  
1175    
1176                  xvid_free(temp_dquants);          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1177                        image_copy(&pEnc->sOriginal, &pEnc->current->image,
1178                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1179          }          }
1180    
1181          if (pEnc->current->global_flags & XVID_H263QUANT) {                  FrameCodeI(pEnc, &bs);
1182                  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;  
1183    
1184                  matrix1_changed = matrix2_changed = 0;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185             * encode this frame as an p-vop
1186         * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1187    
1188                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)      } else { // (type == P_VOP || type == S_VOP)
                         write_vol_header = 1;  
1189    
1190                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1191                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1192                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1193    
1194                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
1195                          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;  
1196          }          }
1197    
1198          if (pFrame->intra < 0) {          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1199                  if ((pEnc->iFrameNum == -1)                      image_copy(&pEnc->sOriginal, &pEnc->current->image,
1200                          || ((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);  
1201                  }                  }
1202    
1203                    FrameCodeP(pEnc, &bs, 1, 0);
1204          }          }
1205    
         /* Relic from OpenDivX - now disabled  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         */  
1206    
1207          BitstreamPadAlways(&bs);      /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208          pFrame->length = BitstreamLength(&bs);           * on next enc_encode call we must flush bframes
1209             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1210    
1211          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;  
         }  
1212    
1213          emms();      pEnc->flush_bframes = 1;
1214    
1215          if (pFrame->quant == 0) {          /* packed & queued_bframes: dont bother outputting stats, we do so after the flush */
1216                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {
1217                                                    pFrame->length, pFrame->intra);                  goto repeat;
1218          }          }
         if (pFrame->general & XVID_EXTRASTATS)  
         {  
                 psnr =  
                         image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
                                            pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                            pEnc->mbParam.height);  
1219    
1220                  snprintf(temp, 127, "PSNR: %f\n", psnr);      /* packed or no-bframes: output stats */
1221        if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) {
1222            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats);
1223          }          }
1224    
1225          pEnc->iFrameNum++;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226             * done; return number of bytes consumed
1227             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1228    
1229    done:
1230    
1231          stop_global_timer();          stop_global_timer();
1232          write_timer();          write_timer();
1233    
1234          return XVID_ERR_OK;          emms();
1235            return BitstreamLength(&bs);
1236  }  }
1237    
1238    
# Line 1393  Line 1249 
1249          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;
1250          pMB->sad16 = 0;          pMB->sad16 = 0;
1251    
1252          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1253                  if (pMB->dquant != NO_CHANGE) {                  if (pMB->dquant != NO_CHANGE) {
1254                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1255                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1409  Line 1265 
1265  }  }
1266    
1267    
 #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);  
         }  
 }  
   
1268    
1269  static int  static int
1270  FrameCodeI(Encoder * pEnc,  FrameCodeI(Encoder * pEnc,
1271                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
1272  {  {
1273        int bits = BitstreamPos(bs);
1274          int mb_width = pEnc->mbParam.mb_width;          int mb_width = pEnc->mbParam.mb_width;
1275          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1276    
# Line 1612  Line 1279 
1279    
1280          uint16_t x, y;          uint16_t x, y;
1281    
1282          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
1283          {          {
1284                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1285                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1626  Line 1293 
1293                  stop_edges_timer();                  stop_edges_timer();
1294          }          }
1295    
         pEnc->iFrameNum = 0;  
1296          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1297          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;  
1298          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1299    
1300          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1301    
1302          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1303    
1304          BitstreamPadAlways(bs);          BitstreamPadAlways(bs);
1305          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1306    
         *pBits = BitstreamPos(bs);  
   
1307          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1308          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1309          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
# Line 1660  Line 1323 
1323                          stop_prediction_timer();                          stop_prediction_timer();
1324    
1325                          start_timer();                          start_timer();
1326                          if (pEnc->current->global_flags & XVID_GREYSCALE)                          if (pEnc->current->vop_flags & XVID_GREYSCALE)
1327                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1328                                  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 */
1329                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
# Line 1669  Line 1332 
1332                          stop_coding_timer();                          stop_coding_timer();
1333                  }                  }
1334    
1335          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1336          {          {
1337                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1338                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1339                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                          16, 0);
1340          }          }
1341          emms();          emms();
1342    
1343          *pBits = BitstreamPos(bs) - *pBits;          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1344            if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1345                    BitstreamPadAlways(bs);
1346            else
1347                    BitstreamPad(bs);
1348        pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1349    
1350          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1351          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1352    
1353        /* XXX: hinted me
1354          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1355                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1356          }          }*/
1357    
1358          return 1;                                       /* intra */          return 1;                                       /* intra */
1359  }  }
# Line 1697  Line 1367 
1367  static int  static int
1368  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1369                     Bitstream * bs,                     Bitstream * bs,
                    uint32_t * pBits,  
1370                     bool force_inter,                     bool force_inter,
1371                     bool vol_header)                     bool vol_header)
1372  {  {
1373          float fSigma;          float fSigma;
1374        int bits = BitstreamPos(bs);
1375    
1376          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1377          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
# Line 1717  Line 1387 
1387          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1388          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1389    
1390          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1391          {          {
1392                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1393                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1731  Line 1401 
1401    
1402          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1403          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1404          pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1405          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1406    
1407          if (!force_inter)          if (!force_inter)
# Line 1739  Line 1409 
1409          else          else
1410                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1411    
1412          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {
1413                  start_timer();                  start_timer();
1414                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1415                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1416                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1417                                                    pEnc->mbParam.m_quarterpel,                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),
1418                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1419                  stop_inter_timer();                  stop_inter_timer();
1420          }          }
# Line 1752  Line 1422 
1422          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1423    
1424          start_timer();          start_timer();
1425          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1426                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1427          else          else*/
1428                  bIntra =                  bIntra =
1429                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1430                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1762  Line 1432 
1432    
1433          stop_motion_timer();          stop_motion_timer();
1434    
1435          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1436    
1437          if ( ( pEnc->current->global_flags & XVID_GMC )          if ( ( pEnc->current->vol_flags & XVID_GMC )
1438                  && ( (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) ) )
1439          {          {
1440                  pEnc->current->coding_type = S_VOP;                  pEnc->current->coding_type = S_VOP;
# Line 1776  Line 1446 
1446                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1447                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1448                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1449                                  pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,
1450                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1451    
1452          }          }
1453    
1454          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1455          if (vol_header)          if (vol_header)
1456          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1457                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1458          }          }
1459    
1460          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1461    
         *pBits = BitstreamPos(bs);  
   
1462          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1463                  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;
1464    
# Line 1837  Line 1505 
1505    
1506                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1507    
1508                                          if (pEnc->mbParam.m_quarterpel)                                          if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1509                                                  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;
1510                                          else                                          else
1511                                                  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 1528 
1528                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pEnc->mbParam.width,
1529                                                                   pEnc->mbParam.height,                                                                   pEnc->mbParam.height,
1530                                                                   pEnc->mbParam.edged_width,                                                                   pEnc->mbParam.edged_width,
1531                                                                   pEnc->mbParam.m_quarterpel,                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),
1532                                                                   (pEnc->current->global_flags & XVID_REDUCED),                                                                   (pEnc->current->vop_flags & XVID_REDUCED),
1533                                                                   pEnc->current->rounding_type);                                                                   pEnc->current->rounding_type);
1534    
1535                          stop_comp_timer();                          stop_comp_timer();
1536    
1537                          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1538                                  if (pMB->dquant != NO_CHANGE) {                                  if (pMB->dquant != NO_CHANGE) {
1539                                          pMB->mode = MODE_INTER_Q;                                          pMB->mode = MODE_INTER_Q;
1540                                          pEnc->current->quant += DQtab[pMB->dquant];                                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1904  Line 1572 
1572                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1573                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1574                          else if (pEnc->current->coding_type == P_VOP) {                          else if (pEnc->current->coding_type == P_VOP) {
1575                                  if (pEnc->mbParam.m_quarterpel)                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1576                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1577                                  else                                  else
1578                                          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 1599 
1599                                          }                                          }
1600    
1601                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1602                                                  if(pEnc->mbParam.m_quarterpel) {                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1603                                                          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);
1604                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1605                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
# Line 1958  Line 1626 
1626                          }                          }
1627                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1628    
1629                          if (pEnc->current->global_flags & XVID_GREYSCALE)                          if ((pEnc->current->vop_flags & XVID_GREYSCALE))
1630                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1631                                  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 */
1632                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1633                          }                          }
1634    
1635                          if(pEnc->mbParam.m_quarterpel) {                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1636                                  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);
1637                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1638                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
# Line 1981  Line 1649 
1649                          {       int k;                          {       int k;
1650                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1651                                  {                                  {
1652                                          if(pEnc->mbParam.m_quarterpel) {                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1653                                                  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);
1654                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1655                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
# Line 2002  Line 1670 
1670                  }                  }
1671          }          }
1672    
1673          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1674          {          {
1675                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1676                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1677                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                          16, 0);
1678          }          }
1679    
1680          emms();          emms();
1681    
1682        /* XXX: hinted me
1683          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1684                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1685          }          }*/
1686    
1687          if (pEnc->current->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
1688                  pEnc->current->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
# Line 2023  Line 1692 
1692          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1693    
1694          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1695                  && (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 */
1696          {          {
1697                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1698                  iSearchRange *= 2;                  iSearchRange *= 2;
1699          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1700                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1701                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1702                             && (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 */
1703          {          {
1704                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1705                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 2055  Line 1724 
1724                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
1725                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
1726                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
1727                  pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1728                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1729                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1730                  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 1746 
1746          }          }
1747          */          */
1748    
1749            /* for divx5 compatibility, we must always pad between the packed p and b frames */
1750            if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1751                    BitstreamPadAlways(bs);
1752            else
1753                    BitstreamPad(bs);
1754    
1755          *pBits = BitstreamPos(bs) - *pBits;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1756    
1757          return 0;                                       /* inter */          return 0;                                       /* inter */
1758  }  }
# Line 2087  Line 1761 
1761  static void  static void
1762  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1763                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1764                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
1765  {  {
1766        int bits = BitstreamPos(bs);
1767          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1768          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1769          uint32_t x, y;          uint32_t x, y;
# Line 2104  Line 1778 
1778                  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); \
1779          }          }
1780    
1781          pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */          /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */
1782    
1783          if (!first){          if (!first){
1784                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1785          }          }
1786  #endif  #endif
1787    
1788          frame->quarterpel =  pEnc->mbParam.m_quarterpel;          //frame->quarterpel =  pEnc->mbParam.m_quarterpel;
1789    
1790          /* forward  */          /* forward  */
1791          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
# Line 2120  Line 1794 
1794          start_timer();          start_timer();
1795          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,
1796                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1797                                            pEnc->mbParam.m_quarterpel, 0);                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1798          stop_inter_timer();          stop_inter_timer();
1799    
1800          /* backward */          /* backward */
# Line 2130  Line 1804 
1804          start_timer();          start_timer();
1805          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1806                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1807                                            pEnc->mbParam.m_quarterpel, 0);                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1808          stop_inter_timer();          stop_inter_timer();
1809    
1810          start_timer();          start_timer();
# Line 2157  Line 1831 
1831          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1832          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1833    
         *pBits = BitstreamPos(bs);  
   
1834          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1835          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
1836          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
1837          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
1838            frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1839            frame->sStat.kblks = frame->sStat.ublks = 0;
1840    
1841          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1842                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1843                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1844                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;
1845    
1846                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1847                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 2187  Line 1860 
1860                                  mb->quant = frame->quant;                                  mb->quant = frame->quant;
1861    
1862                                  mb->cbp =                                  mb->cbp =
1863                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
1864    
1865                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1866                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
# Line 2209  Line 1882 
1882    
1883          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1884    
1885          *pBits = BitstreamPos(bs) - *pBits;      BitstreamPad(bs);
1886            frame->length = (BitstreamPos(bs) - bits) / 8;
1887    
1888  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1889          if (!first){          if (!first){

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

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