[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.2, Sun Mar 9 00:28:09 2003 UTC
# Line 58  Line 58 
58   * Local macros   * Local macros
59   ****************************************************************************/   ****************************************************************************/
60    
 #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  
61  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
62    
63  /*****************************************************************************  /*****************************************************************************
# Line 66  Line 65 
65   ****************************************************************************/   ****************************************************************************/
66    
67  static int FrameCodeI(Encoder * pEnc,  static int FrameCodeI(Encoder * pEnc,
68                                            Bitstream * bs,                                            Bitstream * bs);
                                           uint32_t * pBits);  
69    
70  static int FrameCodeP(Encoder * pEnc,  static int FrameCodeP(Encoder * pEnc,
71                                            Bitstream * bs,                                            Bitstream * bs,
                                           uint32_t * pBits,  
72                                            bool force_inter,                                            bool force_inter,
73                                            bool vol_header);                                            bool vol_header);
74    
75  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
76                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
77                                             Bitstream * bs,                                             Bitstream * bs);
                                            uint32_t * pBits);  
78    
79  /*****************************************************************************  /*****************************************************************************
80   * Local data   * Local data
# Line 104  Line 100 
100   * and cleaning code.   * and cleaning code.
101   *   *
102   * Returned values :   * Returned values :
103   *    - XVID_ERR_OK     - no errors   *    - 0                - no errors
104   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
105   *                        cleans the structure before exiting.   *                        cleans the structure before exiting.
106   *                        pParam->handle is also set to NULL.   *                        pParam->handle is also set to NULL.
107   *   *
108   ****************************************************************************/   ****************************************************************************/
109    
 int  
 encoder_create(XVID_ENC_PARAM * pParam)  
 {  
         Encoder *pEnc;  
         int i;  
   
         pParam->handle = NULL;  
   
         ENC_CHECK(pParam);  
   
         ENC_CHECK(pParam->width > 0 && pParam->width <= 1920);  
         ENC_CHECK(pParam->height > 0 && pParam->height <= 1280);  
         ENC_CHECK(!(pParam->width % 2));  
         ENC_CHECK(!(pParam->height % 2));  
   
         /* Fps */  
   
         if (pParam->fincr <= 0 || pParam->fbase <= 0) {  
                 pParam->fincr = 1;  
                 pParam->fbase = 25;  
         }  
   
110          /*          /*
111           * Simplify the "fincr/fbase" fraction           * Simplify the "fincr/fbase" fraction
          * (neccessary, since windows supplies us with huge numbers)  
112           */           */
113    static void
114          i = pParam->fincr;  simplify_time(int *inc, int *base)
115    {
116            /* common factor */
117            int i = *inc;
118          while (i > 1) {          while (i > 1) {
119                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {                  if (*inc % i == 0 && *base % i == 0) {
120                          pParam->fincr /= i;                          *inc /= i;
121                          pParam->fbase /= i;                          *base /= i;
122                          i = pParam->fincr;                          i = *inc;
123                          continue;                          continue;
124                  }                  }
125                  i--;                  i--;
126          }          }
127    
128          if (pParam->fbase > 65535) {          /* if neccessary, round to 65535 accuracy */
129                  float div = (float) pParam->fbase / 65535;          if (*base > 65535) {
130                    float div = (float) *base / 65535;
131                  pParam->fbase = (int) (pParam->fbase / div);                  *base = (int) (*base / div);
132                  pParam->fincr = (int) (pParam->fincr / div);                  *inc = (int) (*inc / div);
133            }
134          }          }
135    
         /* Bitrate allocator defaults */  
136    
137          if (pParam->rc_bitrate <= 0)  int
138                  pParam->rc_bitrate = 900000;  enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)
139    {
140            Encoder *pEnc;
141        int n;
142    
143          if (pParam->rc_reaction_delay_factor <= 0)          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1))   /* v1.x.x */
144                  pParam->rc_reaction_delay_factor = 16;                  return XVID_ERR_VERSION;
145    
146          if (pParam->rc_averaging_period <= 0)          if (create->width%2 || create->height%2)
147                  pParam->rc_averaging_period = 100;                  return XVID_ERR_FAIL;
148    
149          if (pParam->rc_buffer <= 0)          /* allocate encoder struct */
                 pParam->rc_buffer = 100;  
150    
151          /* Max and min quantizers */          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
152            if (pEnc == NULL)
153                    return XVID_ERR_MEMORY;
154            memset(pEnc, 0, sizeof(Encoder));
155    
156          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          /* global flags */
157                  pParam->min_quantizer = 1;      pEnc->mbParam.global_flags = create->global;
158    
159          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))      /* width, height */
160                  pParam->max_quantizer = 31;          pEnc->mbParam.width = create->width;
161            pEnc->mbParam.height = create->height;
162            pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
163            pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
164            pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
165            pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
166    
167          if (pParam->max_quantizer < pParam->min_quantizer)      /* framerate */
168                  pParam->max_quantizer = pParam->min_quantizer;      pEnc->mbParam.fincr = MAX(create->fincr, 0);
169            pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
170            simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
171    
172          /* 1 keyframe each 10 seconds */          /* bframes */
173            pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
174            pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
175            pEnc->mbParam.bquant_offset = create->bquant_offset;
176    
177          if (pParam->max_key_interval <= 0)          /* frame drop ratio */
178                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
179    
180          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);      /* max keyframe interval */
181          if (pEnc == NULL)      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval;
182                  return XVID_ERR_MEMORY;      /*XXX: replace 250 hard code with "10seconds * framerate" */
183    
184          /* Zero the Encoder Structure */          /* Bitrate allocator defaults
185    
186          memset(pEnc, 0, sizeof(Encoder));          if (create->rc_bitrate <= 0)
187                    create->rc_bitrate = 900000;
188    
189          /* Fill members of Encoder structure */          if (create->rc_reaction_delay_factor <= 0)
190                    create->rc_reaction_delay_factor = 16;
191    
192          pEnc->mbParam.width = pParam->width;          if (create->rc_averaging_period <= 0)
193          pEnc->mbParam.height = pParam->height;                  create->rc_averaging_period = 100;
194    
195          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;          if (create->rc_buffer <= 0)
196          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;                  create->rc_buffer = 100;
197    
         pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;  
         pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;  
198    
         pEnc->mbParam.fbase = pParam->fbase;  
         pEnc->mbParam.fincr = pParam->fincr;  
199    
200          pEnc->mbParam.m_quant_type = H263_QUANT;          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))
201                    create->min_quantizer = 1;
202    
203          pEnc->fMvPrevSigma = -1;          if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))
204                    create->max_quantizer = 31;
205    
206          /* Fill rate control parameters */          if (create->max_quantizer < create->min_quantizer)
207                    create->max_quantizer = create->min_quantizer;
208    
209          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = create->rc_bitrate; */
210    
         pEnc->iFrameNum = -1;  
         pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;  
211    
212          /* try to allocate frame memory */      /* allocate working frame-image memory */
213    
214          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
215          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
# Line 227  Line 217 
217          if (pEnc->current == NULL || pEnc->reference == NULL)          if (pEnc->current == NULL || pEnc->reference == NULL)
218                  goto xvid_err_memory1;                  goto xvid_err_memory1;
219    
220          /* try to allocate mb memory */          /* allocate macroblock memory */
221    
222          pEnc->current->mbs =          pEnc->current->mbs =
223                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
# Line 239  Line 229 
229          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
230                  goto xvid_err_memory2;                  goto xvid_err_memory2;
231    
232          /* try to allocate image memory */          /* allocate interpolation image memory */
233    
234          if (pParam->global & XVID_GLOBAL_EXTRASTATS)          if (create->global & XVID_EXTRASTATS_ENABLE)
235                  image_null(&pEnc->sOriginal);                  image_null(&pEnc->sOriginal);
236    
237          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
# Line 256  Line 246 
246          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
247          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
248    
249          if (pParam->global & XVID_GLOBAL_EXTRASTATS)          if (create->global & XVID_EXTRASTATS_ENABLE)
250          {       if (image_create          {       if (image_create
251                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
252                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
# Line 311  Line 301 
301                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
302                  goto xvid_err_memory3;                  goto xvid_err_memory3;
303    
304            /* init bframe image buffers */
305    
306            pEnc->bframenum_head = 0;
307          pEnc->mbParam.global = pParam->global;          pEnc->bframenum_tail = 0;
308            pEnc->flush_bframes = 0;
309            pEnc->closed_bframenum = -1;
310    
311          /* 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;  
312          pEnc->bframes = NULL;          pEnc->bframes = NULL;
313    
314          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
                 int n;  
315    
316                  pEnc->bframes =                  pEnc->bframes =
317                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
# Line 359  Line 347 
347                  }                  }
348          }          }
349    
350          pEnc->bframenum_head = 0;      /* init incoming frame queue */
351          pEnc->bframenum_tail = 0;          pEnc->queue_head = 0;
352          pEnc->flush_bframes = 0;          pEnc->queue_tail = 0;
353          pEnc->bframenum_dx50bvop = -1;          pEnc->queue_size = 0;
   
         pEnc->queue = NULL;  
   
   
         if (pEnc->mbParam.max_bframes > 0) {  
                 int n;  
354    
355                  pEnc->queue =                  pEnc->queue =
356                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),                  xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
357                                                  CACHE_LINE);                                                  CACHE_LINE);
358    
359                  if (pEnc->queue == NULL)                  if (pEnc->queue == NULL)
360                          goto xvid_err_memory4;                          goto xvid_err_memory4;
361    
362                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
363                          image_null(&pEnc->queue[n]);                  image_null(&pEnc->queue[n].image);
364    
365                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {  
366            for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
367            {
368                          if (image_create                          if (image_create
369                                  (&pEnc->queue[n], pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
370                                   pEnc->mbParam.edged_height) < 0)                                   pEnc->mbParam.edged_height) < 0)
371                                  goto xvid_err_memory5;                                  goto xvid_err_memory5;
372    
373                  }                  }
         }  
374    
         pEnc->queue_head = 0;  
         pEnc->queue_tail = 0;  
         pEnc->queue_size = 0;  
375    
376          pEnc->mbParam.m_stamp = 0;          /* timestamp stuff */
377    
378            pEnc->mbParam.m_stamp = 0;
379          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
380          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
381          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
382    
383          pParam->handle = (void *) pEnc;          /* other stuff */
384    
385          if (pParam->rc_bitrate) {          pEnc->iFrameNum = 0;
386                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,          pEnc->fMvPrevSigma = -1;
387                                                  pParam->rc_reaction_delay_factor,  
388                                                  pParam->rc_averaging_period, pParam->rc_buffer,      /* XXX: cleanup ratecontol when new code is ready */
389                                                  pParam->fbase * 1000 / pParam->fincr,  /*      if (create->rc_bitrate) {
390                                                  pParam->max_quantizer, pParam->min_quantizer);                  RateControlInit(&pEnc->rate_control, create->rc_bitrate,
391          }                                                  create->rc_reaction_delay_factor,
392                                                    create->rc_averaging_period, create->rc_buffer,
393                                                    create->fbase * 1000 / create->fincr,
394                                                    create->max_quantizer, create->min_quantizer);
395            } */
396    
397        create->handle = (void *) pEnc;
398    
399          init_timer();          init_timer();
400    
401          return XVID_ERR_OK;      return 0;   /* ok */
402    
403          /*          /*
404           * 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 406 
406    
407    xvid_err_memory5:    xvid_err_memory5:
408    
   
409          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
410            int i;
411    
412                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
413                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
414                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
415                  }                  }
416                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
# Line 432  Line 419 
419    xvid_err_memory4:    xvid_err_memory4:
420    
421          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
422            int i;
423    
424                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
425    
# Line 452  Line 440 
440    
441    xvid_err_memory3:    xvid_err_memory3:
442    
443          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)
444          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
445                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
446          }          }
# Line 493  Line 481 
481          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
482          xvid_free(pEnc);          xvid_free(pEnc);
483    
484          pParam->handle = NULL;          create->handle = NULL;
485    
486          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
487  }  }
# Line 502  Line 490 
490   * Encoder destruction   * Encoder destruction
491   *   *
492   * This function destroy the entire encoder structure created by a previous   * This function destroy the entire encoder structure created by a previous
493   * successful encoder_create call.   * successful enc_create call.
494   *   *
495   * Returned values (for now only one returned value) :   * Returned values (for now only one returned value) :
496   *    - XVID_ERR_OK     - no errors   *    - 0     - no errors
497   *   *
498   ****************************************************************************/   ****************************************************************************/
499    
500  int  int
501  encoder_destroy(Encoder * pEnc)  enc_destroy(Encoder * pEnc)
502  {  {
503          int i;          int i;
504    
         ENC_CHECK(pEnc);  
   
505          /* B Frames specific */          /* B Frames specific */
506          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
507    
508                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
509    
510                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
511                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
512                  }                  }
513                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
# Line 571  Line 557 
557          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
558                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
559    
560          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)
561          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
562                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
563          }          }
# Line 586  Line 572 
572    
573          xvid_free(pEnc);          xvid_free(pEnc);
574    
575          return XVID_ERR_OK;          return 0;  /* ok */
576  }  }
577    
578    
# Line 594  Line 580 
580  {  {
581          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
582          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
 }  
583    
584        pEnc->m_framenum++; /* debug ticker */
 static __inline void  
 queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  
 {  
         if (pEnc->queue_size >= pEnc->mbParam.max_bframes)  
         {  
                 DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");  
                 return;  
585          }          }
586    
         DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
   
587    
         start_timer();  
         if (image_input  
                 (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))  
                 return;  
         stop_conv_timer();  
   
         if ((pFrame->general & XVID_CHROMAOPT)) {  
                 image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],  
                         pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
         }  
   
         pEnc->queue_size++;  
         pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;  
 }  
588    
589  static __inline void  static __inline void
590  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
# Line 647  Line 606 
606  }  }
607    
608    
609    static void
610  /* convert pFrame->intra to coding_type */  set_stats(xvid_enc_stats_t * stats, RateControl * rc,
611  static int intra2coding_type(int intra)                    const MBParam * pParam, const FRAMEINFO * frame)
612    {
613            if (stats)
614  {  {
615          if (intra < 0)  return -1;                  stats->type = coding2type(frame->coding_type);
616          if (intra == 1) return I_VOP;                  stats->quant = frame->quant;
617          if (intra == 2) return B_VOP;                  stats->vol_flags = frame->vol_flags;
618                    stats->vop_flags = frame->vop_flags;
619                    stats->length = frame->length;
620                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
621                    stats->kblks = frame->sStat.kblks;
622                    stats->mblks = frame->sStat.mblks;
623                    stats->ublks = frame->sStat.ublks;
624            }
625    
626          return P_VOP;          RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP );
627  }  }
628    
629    
# Line 664  Line 632 
632   * IPB frame encoder entry point   * IPB frame encoder entry point
633   *   *
634   * Returned values :   * Returned values :
635   *    - XVID_ERR_OK     - no errors   *    - >0               - output bytes
636     *    - 0                - no output
637     *    - XVID_ERR_VERSION - wrong version passed to core
638     *    - XVID_ERR_END     - End of stream reached before end of coding
639   *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong   *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
640   *                        format   *                        format
641   ****************************************************************************/   ****************************************************************************/
642    
643    
644    /* XXX: dx50bvop broken! something todo with queue */
645    
646  int  int
647  encoder_encode_bframes(Encoder * pEnc,  enc_encode(Encoder * pEnc,
648                             XVID_ENC_FRAME * pFrame,                             xvid_enc_frame_t * xFrame,
649                             XVID_ENC_STATS * pResult)                             xvid_enc_stats_t * stats)
650  {  {
651            xvid_enc_frame_t * frame;
652            int type;
653          uint16_t x, y;          uint16_t x, y;
654          Bitstream bs;          Bitstream bs;
         uint32_t bits;  
         int mode = -1; /* Just to shut up compiler warning */  
655    
656          int input_valid = 1;          if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */
657          int bframes_count = 0;                  return XVID_ERR_VERSION;
658    
659          ENC_CHECK(pEnc);          xFrame->out_flags = 0;
         ENC_CHECK(pFrame);  
         ENC_CHECK(pFrame->image);  
660    
661          start_global_timer();          start_global_timer();
662            BitstreamInit(&bs, xFrame->bitstream, 0);
663    
         BitstreamInit(&bs, pFrame->bitstream, 0);  
   
 ipvop_loop:  
   
         /*  
          * bframe "flush" code  
          */  
664    
665          if ((pFrame->image == NULL || pEnc->flush_bframes)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {           * enqueue image to the encoding-queue
667             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
668    
669                  if (pEnc->flush_bframes == 0) {          if (xFrame->input.csp != XVID_CSP_NULL)
670                          /*          {
671                           * 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  
                          */  
672    
673                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  start_timer();
674                                  pEnc->bframenum_head, pEnc->bframenum_tail,                  if (image_input
675                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
676                            pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
677                            xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))
678                    {
679                            emms();
680                            return XVID_ERR_FORMAT;
681                    }
682                    stop_conv_timer();
683    
684                          pEnc->bframenum_tail--;                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
685                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                          image_chroma_optimize(&q->image,
686                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
687                    }
688    
689                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  q->frame = *xFrame;
690    
691                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                  if (xFrame->quant_intra_matrix)
692                          bframes_count = 0;                  {
693                            memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));
694                            q->frame.quant_intra_matrix = q->quant_intra_matrix;
695                    }
696    
697                          BitstreamPadAlways(&bs);                  if (xFrame->quant_inter_matrix)
698                          pFrame->length = BitstreamLength(&bs);                  {
699                          pFrame->intra = 0;                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));
700                            q->frame.quant_inter_matrix = q->quant_inter_matrix;
701                    }
702    
703                    pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
704                    pEnc->queue_size++;
705            }
706    
                         emms();  
707    
708                          if (pResult) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709                                  pResult->quant = pEnc->current->quant;           * bframe flush code
710                                  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;  
                         }  
711    
712                          return XVID_ERR_OK;  repeat:
                 }  
713    
714            if (pEnc->flush_bframes)
715            {
716                    if (pEnc->bframenum_head < pEnc->bframenum_tail)
717                    {
718    
719                  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",
720                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
721                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
722    
723                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
724                  pEnc->bframenum_head++;                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]);
   
                 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;  
                 }  
725    
726                  if (input_valid)                          pEnc->bframenum_head++;
                         queue_image(pEnc, pFrame);  
727    
                 emms();  
728    
729                  return XVID_ERR_OK;                          goto done;
730          }          }
731    
732          if (pEnc->bframenum_head > 0) {                  /* flush complete: reset counters */
733    
734                    pEnc->flush_bframes = 0;
735                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
736    
737                  /* write an empty marker to the bitstream.                  /* write an empty marker to the bitstream.
# Line 774  Line 741 
741                     indentical to the future-referece frame.                     indentical to the future-referece frame.
742                  */                  */
743    
744                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->mbParam.global_flags & XVID_PACKED)) {
745                          int tmp;                          int tmp;
746                            int bits;
747    
748                          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",
749                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
750                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
751    
752                            bits = BitstreamPos(&bs);
753    
754                          tmp = pEnc->current->seconds;                          tmp = pEnc->current->seconds;
755                          pEnc->current->seconds = 0; /* force time_base = 0 */                          pEnc->current->seconds = 0; /* force time_base = 0 */
756    
757                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
758                            BitstreamPad(&bs);
759                          pEnc->current->seconds = tmp;                          pEnc->current->seconds = tmp;
760    
761                          BitstreamPadAlways(&bs);                          /* add the not-coded length to the reference frame size */
762                          pFrame->length = BitstreamLength(&bs);                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
763                          pFrame->intra = 4;                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);
   
                         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;  
                         }  
764    
765                          if (input_valid)                          // xFrame->out_flags |= XVID_PACKED_FIXUP?;
                                 queue_image(pEnc, pFrame);  
766    
767                          emms();                          goto done;
768    
                         return XVID_ERR_OK;  
769                  }                  }
770          }          }
771    
772    
773  bvop_loop:          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774             * dequeue frame from the encoding queue
775             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
776    
777          if (pEnc->bframenum_dx50bvop != -1)          if (pEnc->queue_size == 0)              /* empty */
778          {          {
779                    if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
780                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                  {
781                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          if (pEnc->bframenum_tail > 0)   /* are bframes */
   
                 if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {  
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");  
                 }  
   
                 if (input_valid)  
782                  {                  {
                         queue_image(pEnc, pFrame);  
                         input_valid = 0;  
                 }  
   
         } else if (input_valid) {  
   
783                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
784                                    pEnc->bframenum_tail--;
785                                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
786    
787                  start_timer();                                  /* XXX: convert B-VOP to P-VOP */
788                  if (image_input                                  pEnc->current->quant *= 2;
                         (&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;  
                 }  
                 stop_conv_timer();  
789    
790                  if ((pFrame->general & XVID_CHROMAOPT)) {                                  FrameCodeP(pEnc, &bs, 1, 0);
791                          image_chroma_optimize(&pEnc->current->image,                                  goto done_flush;
                                 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
792                  }                  }
793    
794                  /* queue input frame, and dequue next image */                          emms();
795                  if (pEnc->queue_size > 0)                          return XVID_ERR_END;    /* end of stream reached */
                 {  
                         image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);  
                         if (pEnc->queue_head != pEnc->queue_tail)  
                         {  
                                 image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);  
796                          }                          }
797                          pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;                  goto done;      /* nothing to encode yet; encoder lag */
                         pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;  
798                  }                  }
799    
800          } else if (pEnc->queue_size > 0) {          // the current FRAME becomes the reference
   
801                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
802    
803                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);          // remove frame from encoding-queue (head), and move it into the current
804                  pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
805            frame = &pEnc->queue[pEnc->queue_head].frame;
806            pEnc->queue_head = (pEnc->queue_head+1) % (pEnc->mbParam.max_bframes+1);
807                  pEnc->queue_size--;                  pEnc->queue_size--;
808    
         } 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;  
                         }  
809    
810                  } else {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811             * init pEnc->current field
812                          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();  
   
                 return XVID_ERR_OK;  
         }  
   
         pEnc->flush_bframes = 0;  
   
         emms();  
   
         /* only inc frame num, adapt quant, etc. if we havent seen it before */  
         if (pEnc->bframenum_dx50bvop < 0 )  
         {  
                 mode = intra2coding_type(pFrame->intra);  
                 if (pFrame->quant == 0)  
                         pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
                 else  
                         pEnc->current->quant = pFrame->quant;  
   
 /*              if (pEnc->current->quant < 1)  
                         pEnc->current->quant = 1;  
813    
814                  if (pEnc->current->quant > 31)          emms();         /* float-point region */
                         pEnc->current->quant = 31;  
 */  
                 pEnc->current->global_flags = pFrame->general;  
                 pEnc->current->motion_flags = pFrame->motion;  
815    
816                  /* ToDo : dynamic fcode (in both directions) */      pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
817        pEnc->current->vop_flags = frame->vop_flags;
818            pEnc->current->motion_flags = frame->motion;
819                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                  pEnc->current->fcode = pEnc->mbParam.m_fcode;
820                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                  pEnc->current->bcode = pEnc->mbParam.m_fcode;
821    
822                  inc_frame_num(pEnc);      if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
823                image_chroma_optimize(&pEnc->current->image,
824                        pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
825        }
826    
827                  if (pFrame->general & XVID_EXTRASTATS)      if (xFrame->vop_flags & XVID_EXTRASTATS)
828                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
829                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);
830                  }                  }
831    
832                  emms();          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
833                    int *temp_dquants =     (int *) xvid_malloc(pEnc->mbParam.mb_width *
834                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,  
                                 "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);  
                 }  
   
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Luminance masking  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
                 if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {  
                         int *temp_dquants =  
                                 (int *) xvid_malloc(pEnc->mbParam.mb_width *  
                                                                 pEnc->mbParam.mb_height * sizeof(int),  
                                                                 CACHE_LINE);  
835    
836                          pEnc->current->quant =                          pEnc->current->quant =
837                                  adaptive_quantization(pEnc->current->image.y,                                  adaptive_quantization(pEnc->current->image.y,
# Line 997  Line 857 
857                          xvid_free(temp_dquants);                          xvid_free(temp_dquants);
858                  }                  }
859    
860            if (frame->quant > 0)
861            {
862                    pEnc->current->quant = frame->quant;
863                    type = frame->type;
864            }
865            else
866            {
867                    pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
868                    type = XVID_TYPE_AUTO;  //RateControlGetType(&pEnc->rate_control, ...);
869          }          }
870    
871          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          emms();         /* END floating-point region */
872           * ivop/pvop/bvop selection  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
         pEnc->iFrameNum++;  
873    
874          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||          if (type > 0)   /* XVID_TYPE_?VOP */
875                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&          {
876                          pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
877            }
878            else                    /* XVID_TYPE_AUTO */
879          {          {
880                  mode = I_VOP;                  if (pEnc->iFrameNum == 0 ||
881                            (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
882                    {
883                            pEnc->iFrameNum = 0;
884                            type = I_VOP;
885          }else{          }else{
886                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,                          type = MEanalysis(&pEnc->reference->image, pEnc->current,
887                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
888                                          (mode < 0) ? pEnc->iFrameNum : 0,                                          pEnc->iFrameNum, pEnc->bframenum_tail);
                                         bframes_count++);  
889          }          }
890            }
891            inc_frame_num(pEnc);
892            pEnc->iFrameNum++;
893    
         if (mode == I_VOP) {  
                 /*  
                  * This will be coded as an Intra Frame  
                  */  
                 if ((pEnc->current->global_flags & XVID_QUARTERPEL))  
                         pEnc->mbParam.m_quarterpel = 1;  
                 else  
                         pEnc->mbParam.m_quarterpel = 0;  
   
                 if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;  
894    
895                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
896                          if (pFrame->quant_intra_matrix != NULL)                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
897                                  set_intra_matrix(pFrame->quant_intra_matrix);                          "%i  st:%i  if:%i", pEnc->m_framenum, pEnc->current->stamp, pEnc->iFrameNum);
                         if (pFrame->quant_inter_matrix != NULL)  
                                 set_inter_matrix(pFrame->quant_inter_matrix);  
898                  }                  }
899    
900            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901             * ivop/pvop/bvop selection
902             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
903    
904            if (type == I_VOP) {
905    
906                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
907                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
908                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
909    
910                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
911                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
912                  }                  }
913    
914                  /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */                  // when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame
915                    if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
916    
917                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {                          // place this frame back on the encoding-queue (head)
918                            // we will deal with it next time
919                            pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
920                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
921    
922                            // grab the last frame from the bframe-queue
923    
924                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
925                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;                          SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
926    
927                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
                         if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {  
928                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
929                          }                          }
930                          FrameCodeP(pEnc, &bs, &bits, 1, 0);  
931                          bframes_count = 0;                          /* XXX: convert B-VOP to P-VOP */
932                          pFrame->intra = 0;                          pEnc->current->quant *= 2;
933    
934                            FrameCodeP(pEnc, &bs, 1, 0);
935    
936                  } else {                  } else {
937    
938                          FrameCodeI(pEnc, &bs, &bits);                          /* ---- update vol flags at IVOP ----------- */
939                          bframes_count = 0;                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
                         pFrame->intra = 1;  
940    
941                          pEnc->bframenum_dx50bvop = -1;              if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {
942                                    if (frame->quant_intra_matrix != NULL)
943                                            set_intra_matrix(frame->quant_intra_matrix);
944                                    if (frame->quant_inter_matrix != NULL)
945                                            set_inter_matrix(frame->quant_inter_matrix);
946                  }                  }
947    
948                  pEnc->flush_bframes = 1;              /* prevent vol/vop misuse */
949    
950                if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
951                    pEnc->current->vop_flags &= ~XVID_REDUCED;
952    
953                if (!(pEnc->current->vol_flags & XVID_INTERLACING))
954                    pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);
955    
956                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {                          /* ^^^------------------------ */
957                          BitstreamPadAlways(&bs);  
958                          input_valid = 0;                          FrameCodeI(pEnc, &bs);
959                          goto ipvop_loop;                          xFrame->out_flags |= XVID_KEYFRAME;
960                  }                  }
961    
962                  /*          } else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) ||
963                   * NB : sequences like "IIBB" decode fine with msfdam but,                                  pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
                  *      go screwy with divx 5.00  
                  */  
         } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
964                  /*                  /*
965                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
966                   */                   */
967    
968                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** xFrame bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
969                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
970                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
971    
972                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
973                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
974                  }                  }
975    
976                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, 1, 0);
                 bframes_count = 0;  
                 pFrame->intra = 0;  
                 pEnc->flush_bframes = 1;  
977    
978                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {          } else {        /* type == B_VOP */
                         BitstreamPadAlways(&bs);  
                         input_valid = 0;  
                         goto ipvop_loop;  
                 }  
   
         } else {        /* mode == B_VOP */  
                 /*  
                  * This will be coded as a Bidirectional Frame  
                  */  
979    
980                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
981                          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");
982                  }                  }
983    
984                  if (pFrame->bquant < 1) {                  if (frame->bquant < 1) {
985                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
986                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
987    
988                  } else {                  } else {
989                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = frame->bquant;
990                  }                  }
991    
992                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1135  Line 1004 
1004    
1005                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1006    
1007                  /* bframe report by koepi */                  goto repeat;
1008                  pFrame->intra = 2;  
1009                  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;  
1010    
1011                  if (pFrame->general & XVID_EXTRASTATS)          if (xFrame->vop_flags & XVID_EXTRASTATS)
1012                  {       pResult->sse_y =          {       stats->sse_y =
1013                                  plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,                                  plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
1014                                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1015                                                     pEnc->mbParam.height);                                                     pEnc->mbParam.height);
1016    
1017                          pResult->sse_u =                  stats->sse_u =
1018                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1019                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1020                                                     pEnc->mbParam.height/2);                                                     pEnc->mbParam.height/2);
1021    
1022                          pResult->sse_v =                  stats->sse_v =
1023                                  plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,                                  plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1024                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1025                                                     pEnc->mbParam.height/2);                                                     pEnc->mbParam.height/2);
1026                  }                  }
         }  
   
         emms();  
   
         if (pFrame->quant == 0) {  
                 RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,  
                                                   pFrame->length, pFrame->intra);  
         }  
   
         stop_global_timer();  
         write_timer();  
   
         emms();  
         return XVID_ERR_OK;  
 }  
   
   
   
 /*****************************************************************************  
  * "original" IP frame encoder entry point  
  *  
  * Returned values :  
  *    - XVID_ERR_OK     - no errors  
  *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong  
  *                        format  
  ****************************************************************************/  
   
 int  
 encoder_encode(Encoder * pEnc,  
                            XVID_ENC_FRAME * pFrame,  
                            XVID_ENC_STATS * pResult)  
 {  
         uint16_t x, y;  
         Bitstream bs;  
         uint32_t bits;  
         uint16_t write_vol_header = 0;  
   
         float psnr;  
         char temp[128];  
1027    
1028          start_global_timer();          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1029             * okay, on next enc_encode call we must flush bframes
1030          ENC_CHECK(pEnc);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
         ENC_CHECK(pFrame);  
         ENC_CHECK(pFrame->bitstream);  
         ENC_CHECK(pFrame->image);  
   
         SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);  
1031    
1032          pEnc->current->global_flags = pFrame->general;  done_flush:
         pEnc->current->motion_flags = pFrame->motion;  
         pEnc->mbParam.hint = &pFrame->hint;  
1033    
1034          inc_frame_num(pEnc);  #if 0
   
         /* disable alternate scan flag if interlacing is not enabled */  
         if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&  
                 !(pEnc->current->global_flags & XVID_INTERLACING))  
1035          {          {
1036                  pEnc->current->global_flags -= XVID_ALTERNATESCAN;                  char tmp[100];
1037          }                  wsprintf(tmp,"\\frame%03i.pgm", pEnc->m_framenum);
1038                    image_dump_yuvpgm(&pEnc->current->image, pEnc->mbParam.edged_width,
1039          start_timer();                          pEnc->mbParam.width, pEnc->mbParam.height, tmp);
         if (image_input  
                 (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)  
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
   
         if ((pFrame->general & XVID_CHROMAOPT)) {  
                 image_chroma_optimize(&pEnc->current->image,  
                         pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
1040          }          }
1041    #endif
1042    
1043          if (pFrame->general & XVID_EXTRASTATS)          /* packed_mode: repeat */
1044          {       image_copy(&pEnc->sOriginal, &pEnc->current->image,          pEnc->flush_bframes = 1;
1045                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {
1046                    goto repeat;
1047          }          }
1048    
1049          emms();          if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)
1050            {
1051          BitstreamInit(&bs, pFrame->bitstream, 0);                  /* packed(and low_delay) encoding gives us graceful reorded-stats output */
1052                    set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);
         if (pFrame->quant == 0) {  
                 pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
         } else {  
                 pEnc->current->quant = pFrame->quant;  
1053          }          }
   
         if ((pEnc->current->global_flags & XVID_QUARTERPEL))  
                 pEnc->mbParam.m_quarterpel = 1;  
1054          else          else
                 pEnc->mbParam.m_quarterpel = 0;  
   
         if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {  
                 int *temp_dquants =  
                         (int *) xvid_malloc(pEnc->mbParam.mb_width *  
                                                                 pEnc->mbParam.mb_height * sizeof(int),  
                                                                 CACHE_LINE);  
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                                                   pEnc->mbParam.edged_width, temp_dquants,  
                                                                   pEnc->current->quant, pEnc->current->quant,  
                                                                   2 * pEnc->current->quant,  
                                                                   pEnc->mbParam.mb_width,  
                                                                   pEnc->mbParam.mb_height);  
   
                 for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
   
 #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)  
   
                         for (x = 0; x < pEnc->mbParam.mb_width; x++) {  
   
   
                                 MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];  
   
                                 pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];  
                         }  
   
 #undef OFFSET  
                 }  
   
                 xvid_free(temp_dquants);  
         }  
   
         if (pEnc->current->global_flags & XVID_H263QUANT) {  
                 if (pEnc->mbParam.m_quant_type != H263_QUANT)  
                         write_vol_header = 1;  
                 pEnc->mbParam.m_quant_type = H263_QUANT;  
         } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {  
                 int matrix1_changed, matrix2_changed;  
   
                 matrix1_changed = matrix2_changed = 0;  
   
                 if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)  
                         write_vol_header = 1;  
   
                 pEnc->mbParam.m_quant_type = MPEG4_QUANT;  
   
                 if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {  
                         if (pFrame->quant_intra_matrix != NULL)  
                                 matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);  
                         if (pFrame->quant_inter_matrix != NULL)  
                                 matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);  
                 } else {  
                         matrix1_changed = set_intra_matrix(get_default_intra_matrix());  
                         matrix2_changed = set_inter_matrix(get_default_inter_matrix());  
                 }  
                 if (write_vol_header == 0)  
                         write_vol_header = matrix1_changed | matrix2_changed;  
         }  
   
         if (pFrame->intra < 0) {  
                 if ((pEnc->iFrameNum == -1)  
                         || ((pEnc->mbParam.iMaxKeyInterval > 0)  
                                 && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {  
                         pFrame->intra = FrameCodeI(pEnc, &bs, &bits);  
                 } else {  
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);  
                 }  
         } else {  
                 if (pFrame->intra == 1) {  
                         pFrame->intra = FrameCodeI(pEnc, &bs, &bits);  
                 } else {  
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);  
                 }  
   
         }  
   
         /* Relic from OpenDivX - now disabled  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         */  
   
         BitstreamPadAlways(&bs);  
         pFrame->length = BitstreamLength(&bs);  
   
         if (pResult) {  
                 pResult->quant = pEnc->current->quant;  
                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->current->sStat.kblks;  
                 pResult->mblks = pEnc->current->sStat.mblks;  
                 pResult->ublks = pEnc->current->sStat.ublks;  
         }  
   
         emms();  
   
         if (pFrame->quant == 0) {  
                 RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,  
                                                   pFrame->length, pFrame->intra);  
         }  
         if (pFrame->general & XVID_EXTRASTATS)  
1055          {          {
1056                  psnr =                  /* outherwise, output the previous frame */
1057                          image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  /* XXX: for this to work, refernce must be valid for IVOPs too */
1058                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,                  if (pEnc->current->stamp > 0)
1059                                             pEnc->mbParam.height);                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference);
1060                    else
1061                  snprintf(temp, 127, "PSNR: %f\n", psnr);                          stats->type = XVID_TYPE_NOTHING;
1062          }          }
1063    
1064          pEnc->iFrameNum++;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065             * done; return number of bytes consumed
1066             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1067    
1068    done:
1069    
1070          stop_global_timer();          stop_global_timer();
1071          write_timer();          write_timer();
1072    
1073          return XVID_ERR_OK;          emms();
1074            return BitstreamLength(&bs);
1075  }  }
1076    
1077    
# Line 1393  Line 1088 
1088          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;
1089          pMB->sad16 = 0;          pMB->sad16 = 0;
1090    
1091          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1092                  if (pMB->dquant != NO_CHANGE) {                  if (pMB->dquant != NO_CHANGE) {
1093                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1094                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1409  Line 1104 
1104  }  }
1105    
1106    
 #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);  
         }  
 }  
   
1107    
1108  static int  static int
1109  FrameCodeI(Encoder * pEnc,  FrameCodeI(Encoder * pEnc,
1110                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
1111  {  {
1112        int bits = BitstreamPos(bs);
1113          int mb_width = pEnc->mbParam.mb_width;          int mb_width = pEnc->mbParam.mb_width;
1114          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1115    
# Line 1612  Line 1118 
1118    
1119          uint16_t x, y;          uint16_t x, y;
1120    
1121          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
1122          {          {
1123                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1124                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1626  Line 1132 
1132                  stop_edges_timer();                  stop_edges_timer();
1133          }          }
1134    
1135          pEnc->iFrameNum = 0;          //pEnc->iFrameNum = 0;
1136          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1137          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1138          pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1139          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1140    
1141          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1142    
1143          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1144    
1145          BitstreamPadAlways(bs);          BitstreamPadAlways(bs);
1146          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1147    
         *pBits = BitstreamPos(bs);  
   
1148          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1149          pEnc->current->sStat.kblks = mb_width * mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1150          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
# Line 1660  Line 1164 
1164                          stop_prediction_timer();                          stop_prediction_timer();
1165    
1166                          start_timer();                          start_timer();
1167                          if (pEnc->current->global_flags & XVID_GREYSCALE)                          if (pEnc->current->vop_flags & XVID_GREYSCALE)
1168                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1169                                  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 */
1170                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
# Line 1669  Line 1173 
1173                          stop_coding_timer();                          stop_coding_timer();
1174                  }                  }
1175    
1176          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1177          {          {
1178                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1179                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1180                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                          16, 0);
1181          }          }
1182          emms();          emms();
1183    
1184          *pBits = BitstreamPos(bs) - *pBits;          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1185            if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1186                    BitstreamPadAlways(bs);
1187            else
1188                    BitstreamPad(bs);
1189        pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1190    
1191          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1192          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1193    
1194        /* XXX: hinted me
1195          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1196                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1197          }          }*/
1198    
1199          return 1;                                       /* intra */          return 1;                                       /* intra */
1200  }  }
# Line 1697  Line 1208 
1208  static int  static int
1209  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1210                     Bitstream * bs,                     Bitstream * bs,
                    uint32_t * pBits,  
1211                     bool force_inter,                     bool force_inter,
1212                     bool vol_header)                     bool vol_header)
1213  {  {
1214          float fSigma;          float fSigma;
1215        int bits = BitstreamPos(bs);
1216    
1217          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1218          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
# Line 1717  Line 1228 
1228          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1229          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1230    
1231          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1232          {          {
1233                  mb_width = (pEnc->mbParam.width + 31) / 32;                  mb_width = (pEnc->mbParam.width + 31) / 32;
1234                  mb_height = (pEnc->mbParam.height + 31) / 32;                  mb_height = (pEnc->mbParam.height + 31) / 32;
# Line 1731  Line 1242 
1242    
1243          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1244          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1245          pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;          //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1246          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1247    
1248          if (!force_inter)          if (!force_inter)
# Line 1739  Line 1250 
1250          else          else
1251                  iLimit = mb_width * mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1252    
1253          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {
1254                  start_timer();                  start_timer();
1255                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1256                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1257                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1258                                                    pEnc->mbParam.m_quarterpel,                                                    (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),
1259                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1260                  stop_inter_timer();                  stop_inter_timer();
1261          }          }
# Line 1752  Line 1263 
1263          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1264    
1265          start_timer();          start_timer();
1266          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1267                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1268          else          else*/
1269                  bIntra =                  bIntra =
1270                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1271                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
# Line 1762  Line 1273 
1273    
1274          stop_motion_timer();          stop_motion_timer();
1275    
1276          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1277    
1278          if ( ( pEnc->current->global_flags & XVID_GMC )          if ( ( pEnc->current->vol_flags & XVID_GMC )
1279                  && ( (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) ) )
1280          {          {
1281                  pEnc->current->coding_type = S_VOP;                  pEnc->current->coding_type = S_VOP;
# Line 1776  Line 1287 
1287                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,                  generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1288                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,                                  pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1289                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1290                                  pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,                                  pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,
1291                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);                                  pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1292    
1293          }          }
1294    
1295          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1296          if (vol_header)          if (vol_header)
1297          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1298                  BitstreamPadAlways(bs);                  BitstreamPadAlways(bs);
1299          }          }
1300    
1301          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1302    
         *pBits = BitstreamPos(bs);  
   
1303          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1304                  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;
1305    
# Line 1837  Line 1346 
1346    
1347                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */                                  if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1348    
1349                                          if (pEnc->mbParam.m_quarterpel)                                          if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1350                                                  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;
1351                                          else                                          else
1352                                                  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 1369 
1369                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pEnc->mbParam.width,
1370                                                                   pEnc->mbParam.height,                                                                   pEnc->mbParam.height,
1371                                                                   pEnc->mbParam.edged_width,                                                                   pEnc->mbParam.edged_width,
1372                                                                   pEnc->mbParam.m_quarterpel,                                                                   (pEnc->current->vol_flags & XVID_QUARTERPEL),
1373                                                                   (pEnc->current->global_flags & XVID_REDUCED),                                                                   (pEnc->current->vop_flags & XVID_REDUCED),
1374                                                                   pEnc->current->rounding_type);                                                                   pEnc->current->rounding_type);
1375    
1376                          stop_comp_timer();                          stop_comp_timer();
1377    
1378                          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1379                                  if (pMB->dquant != NO_CHANGE) {                                  if (pMB->dquant != NO_CHANGE) {
1380                                          pMB->mode = MODE_INTER_Q;                                          pMB->mode = MODE_INTER_Q;
1381                                          pEnc->current->quant += DQtab[pMB->dquant];                                          pEnc->current->quant += DQtab[pMB->dquant];
# Line 1904  Line 1413 
1413                          if (pEnc->current->coding_type == S_VOP)                          if (pEnc->current->coding_type == S_VOP)
1414                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1415                          else if (pEnc->current->coding_type == P_VOP) {                          else if (pEnc->current->coding_type == P_VOP) {
1416                                  if (pEnc->mbParam.m_quarterpel)                                  if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1417                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1418                                  else                                  else
1419                                          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 1440 
1440                                          }                                          }
1441    
1442                                          if (!bSkip) {   /* no SKIP, but trivial block */                                          if (!bSkip) {   /* no SKIP, but trivial block */
1443                                                  if(pEnc->mbParam.m_quarterpel) {                                                  if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1444                                                          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);
1445                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1446                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
# Line 1958  Line 1467 
1467                          }                          }
1468                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1469    
1470                          if (pEnc->current->global_flags & XVID_GREYSCALE)                          if ((pEnc->current->vop_flags & XVID_GREYSCALE))
1471                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1472                                  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 */
1473                                  qcoeff[5*64+0]=0;                                  qcoeff[5*64+0]=0;
1474                          }                          }
1475    
1476                          if(pEnc->mbParam.m_quarterpel) {                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1477                                  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);
1478                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1479                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
# Line 1981  Line 1490 
1490                          {       int k;                          {       int k;
1491                                  for (k=1;k<4;k++)                                  for (k=1;k<4;k++)
1492                                  {                                  {
1493                                          if(pEnc->mbParam.m_quarterpel) {                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1494                                                  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);
1495                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;                                                  pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1496                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;                                                  pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
# Line 2002  Line 1511 
1511                  }                  }
1512          }          }
1513    
1514          if ((pEnc->current->global_flags & XVID_REDUCED))          if ((pEnc->current->vop_flags & XVID_REDUCED))
1515          {          {
1516                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1517                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1518                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                          16, 0);
1519          }          }
1520    
1521          emms();          emms();
1522    
1523        /* XXX: hinted me
1524          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1525                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1526          }          }*/
1527    
1528          if (pEnc->current->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
1529                  pEnc->current->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
# Line 2023  Line 1533 
1533          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1534    
1535          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1536                  && (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 */
1537          {          {
1538                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1539                  iSearchRange *= 2;                  iSearchRange *= 2;
1540          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
1541                             && (pEnc->fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
1542                             && (pEnc->fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1543                             && (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 */
1544          {          {
1545                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1546                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 2055  Line 1565 
1565                  pEnc->current->quant = pEnc->reference->quant;                  pEnc->current->quant = pEnc->reference->quant;
1566                  pEnc->current->motion_flags = pEnc->reference->motion_flags;                  pEnc->current->motion_flags = pEnc->reference->motion_flags;
1567                  pEnc->current->rounding_type = pEnc->reference->rounding_type;                  pEnc->current->rounding_type = pEnc->reference->rounding_type;
1568                  pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  //pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1569                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->fcode = pEnc->reference->fcode;
1570                  pEnc->current->bcode = pEnc->reference->bcode;                  pEnc->current->bcode = pEnc->reference->bcode;
1571                  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 1587 
1587          }          }
1588          */          */
1589    
1590            /* for divx5 compatibility, we must always pad between the packed p and b frames */
1591            if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1592                    BitstreamPadAlways(bs);
1593            else
1594                    BitstreamPad(bs);
1595    
1596          *pBits = BitstreamPos(bs) - *pBits;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1597    
1598          return 0;                                       /* inter */          return 0;                                       /* inter */
1599  }  }
# Line 2087  Line 1602 
1602  static void  static void
1603  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1604                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1605                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
1606  {  {
1607        int bits = BitstreamPos(bs);
1608          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1609          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1610          uint32_t x, y;          uint32_t x, y;
# Line 2104  Line 1619 
1619                  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); \
1620          }          }
1621    
1622          pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */          /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */
1623    
1624          if (!first){          if (!first){
1625                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1626          }          }
1627  #endif  #endif
1628    
1629          frame->quarterpel =  pEnc->mbParam.m_quarterpel;          //frame->quarterpel =  pEnc->mbParam.m_quarterpel;
1630    
1631          /* forward  */          /* forward  */
1632          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
# Line 2120  Line 1635 
1635          start_timer();          start_timer();
1636          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,
1637                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1638                                            pEnc->mbParam.m_quarterpel, 0);                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1639          stop_inter_timer();          stop_inter_timer();
1640    
1641          /* backward */          /* backward */
# Line 2130  Line 1645 
1645          start_timer();          start_timer();
1646          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1647                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1648                                            pEnc->mbParam.m_quarterpel, 0);                                            (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1649          stop_inter_timer();          stop_inter_timer();
1650    
1651          start_timer();          start_timer();
# Line 2157  Line 1672 
1672          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1673          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1674    
         *pBits = BitstreamPos(bs);  
   
1675          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
1676          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
1677          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
1678          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
1679            frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1680            frame->sStat.kblks = frame->sStat.ublks = 0;
1681    
1682          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1683                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1684                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1685                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;                          int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;
1686    
1687                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1688                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
# Line 2209  Line 1723 
1723    
1724          /* TODO: dynamic fcode/bcode ??? */          /* TODO: dynamic fcode/bcode ??? */
1725    
1726          *pBits = BitstreamPos(bs) - *pBits;      BitstreamPad(bs);
1727            frame->length = (BitstreamPos(bs) - bits) / 8;
1728    
1729  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1730          if (!first){          if (!first){

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

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