[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.41, Wed Jun 12 20:38:40 2002 UTC revision 1.99, Wed Apr 9 12:02:26 2003 UTC
# Line 26  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
  ****************************************************************************/  
   
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
29   *  $Id$   *  $Id$
30   *   *
31   ****************************************************************************/   ****************************************************************************/
# Line 43  Line 33 
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <stdio.h>  #include <stdio.h>
35  #include <math.h>  #include <math.h>
36    #include <string.h>
37    
38  #include "encoder.h"  #include "encoder.h"
39  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
40  #include "global.h"  #include "global.h"
41  #include "utils/timer.h"  #include "utils/timer.h"
42  #include "image/image.h"  #include "image/image.h"
43    #include "image/font.h"
44    #include "motion/sad.h"
45  #include "motion/motion.h"  #include "motion/motion.h"
46  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
47  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 66  Line 59 
59   ****************************************************************************/   ****************************************************************************/
60    
61  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
62  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)   { _T_ tmp = A; A = B; B = tmp; }
63    
64  /*****************************************************************************  /*****************************************************************************
65   * Local function prototypes   * Local function prototypes
# Line 82  Line 75 
75                                            bool force_inter,                                            bool force_inter,
76                                            bool vol_header);                                            bool vol_header);
77    
 #ifdef BFRAMES  
78  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
79                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
80                                             Bitstream * bs,                                             Bitstream * bs,
81                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
82    
83  /*****************************************************************************  /*****************************************************************************
84   * Local data   * Local data
# Line 102  Line 93 
93  };  };
94    
95    
 static void __inline  
 image_null(IMAGE * image)  
 {  
         image->y = image->u = image->v = NULL;  
 }  
   
   
96  /*****************************************************************************  /*****************************************************************************
97   * Encoder creation   * Encoder creation
98   *   *
# Line 131  Line 115 
115  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
116  {  {
117          Encoder *pEnc;          Encoder *pEnc;
118          uint32_t i;          int i;
   
119          pParam->handle = NULL;          pParam->handle = NULL;
120    
121          ENC_CHECK(pParam);          ENC_CHECK(pParam);
# Line 199  Line 182 
182    
183          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
184    
185          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
186                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
187    
   
188          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
189          if (pEnc == NULL)          if (pEnc == NULL)
190                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
191    
192            /* Zero the Encoder Structure */
193    
194            memset(pEnc, 0, sizeof(Encoder));
195    
196          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
197    
198          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 221  Line 207 
207          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
208          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
209    
210          pEnc->sStat.fMvPrevSigma = -1;          pEnc->mbParam.m_quant_type = H263_QUANT;
211    
212            pEnc->fMvPrevSigma = -1;
213    
214          /* Fill rate control parameters */          /* Fill rate control parameters */
215    
216          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
217    
218          pEnc->iFrameNum = 0;          pEnc->iFrameNum = -1;
219          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
220    
221          /* try to allocate frame memory */          /* try to allocate frame memory */
222    
# Line 252  Line 240 
240    
241          /* try to allocate image memory */          /* try to allocate image memory */
242    
243  #ifdef _DEBUG          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
244          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
245  #endif  
 #ifdef BFRAMES  
246          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
247          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
248          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
249  #endif  
250          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
251          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
252          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 268  Line 255 
255          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
256          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
257    
258  #ifdef _DEBUG          if (pParam->global & XVID_GLOBAL_EXTRASTATS)
259          if (image_create          {       if (image_create
260                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
261                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
262                  goto xvid_err_memory3;                  goto xvid_err_memory3;
263  #endif          }
264  #ifdef BFRAMES  
265          if (image_create          if (image_create
266                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
267                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 287  Line 274 
274                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
275                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
276                  goto xvid_err_memory3;                  goto xvid_err_memory3;
277  #endif  
278          if (image_create          if (image_create
279                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
280                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 317  Line 304 
304                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
305                  goto xvid_err_memory3;                  goto xvid_err_memory3;
306    
307    /* Create full bitplane for GMC, this might be wasteful */
308            if (image_create
309                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
310                     pEnc->mbParam.edged_height) < 0)
311                    goto xvid_err_memory3;
312    
313    
         /* B Frames specific init */  
 #ifdef BFRAMES  
314    
315            pEnc->mbParam.global = pParam->global;
316    
317            /* B Frames specific init */
318          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
319          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
320            pEnc->mbParam.bquant_offset = pParam->bquant_offset;
321            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
322          pEnc->bframes = NULL;          pEnc->bframes = NULL;
323    
324          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 366  Line 361 
361          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
362          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
363          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
364            pEnc->bframenum_dx50bvop = -1;
365    
366          pEnc->mbParam.m_seconds = 0;          pEnc->queue = NULL;
367          pEnc->mbParam.m_ticks = 0;  
368  #endif          if (pEnc->mbParam.max_bframes > 0) {
369                    int n;
370    
371                    pEnc->queue =
372                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
373                                                    CACHE_LINE);
374    
375                    if (pEnc->queue == NULL)
376                            goto xvid_err_memory4;
377    
378                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
379                            image_null(&pEnc->queue[n]);
380    
381                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
382                            if (image_create
383                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
384                                     pEnc->mbParam.edged_height) < 0)
385                                    goto xvid_err_memory5;
386    
387                    }
388            }
389    
390            pEnc->queue_head = 0;
391            pEnc->queue_tail = 0;
392            pEnc->queue_size = 0;
393    
394            pEnc->mbParam.m_stamp = 0;
395    
396            pEnc->m_framenum = 0;
397            pEnc->current->stamp = 0;
398            pEnc->reference->stamp = 0;
399    
400          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
401    
# Line 388  Line 414 
414          /*          /*
415           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
416           */           */
417  #ifdef BFRAMES  
418      xvid_err_memory5:
419    
420            if (pEnc->mbParam.max_bframes > 0) {
421    
422                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
423                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
424                                                      pEnc->mbParam.edged_height);
425                    }
426                    xvid_free(pEnc->queue);
427            }
428    
429    xvid_err_memory4:    xvid_err_memory4:
430    
431            if (pEnc->mbParam.max_bframes > 0) {
432    
433          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
434    
435                  if (pEnc->bframes[i] == NULL)                  if (pEnc->bframes[i] == NULL)
# Line 405  Line 445 
445          }          }
446    
447          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
448            }
 #endif  
449    
450    xvid_err_memory3:    xvid_err_memory3:
451  #ifdef _DEBUG  
452          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
453            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
454                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
455  #endif          }
456    
 #ifdef BFRAMES  
457          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
458                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
459          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
460                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
461          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
462                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
463    
464          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
465                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 438  Line 476 
476          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
477                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
478    
479    /* destroy GMC image */
480            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
481                                      pEnc->mbParam.edged_height);
482    
483    
484    xvid_err_memory2:    xvid_err_memory2:
485          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
486          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 466  Line 509 
509  int  int
510  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
511  {  {
512            int i;
513    
514          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
515    
516          /* B Frames specific */          /* B Frames specific */
517  #ifdef BFRAMES          if (pEnc->mbParam.max_bframes > 0) {
518          int i;  
519                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
520    
521                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
522                                              pEnc->mbParam.edged_height);
523                    }
524                    xvid_free(pEnc->queue);
525            }
526    
527            if (pEnc->mbParam.max_bframes > 0) {
528    
529          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
530    
# Line 483  Line 537 
537                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
538    
539                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
540          }          }
541    
542          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
543    
544  #endif          }
545    
546          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
547    
# Line 506  Line 559 
559                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
560          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
561                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
562  #ifdef BFRAMES  
563          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
564                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
565          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
566                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
567          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
568                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
569  #endif  
570  #ifdef _DEBUG          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
571          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
573  #endif          }
574    
575          /* Encoder structure */          /* Encoder structure */
576    
# Line 532  Line 585 
585          return XVID_ERR_OK;          return XVID_ERR_OK;
586  }  }
587    
588    
589    static __inline void inc_frame_num(Encoder * pEnc)
590    {
591            pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
592            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
593    }
594    
595    
596    static __inline void
597    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
598    {
599            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
600            {
601                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
602                    return;
603            }
604    
605            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
606                                    pEnc->bframenum_head, pEnc->bframenum_tail,
607                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
608    
609    
610            start_timer();
611            if (image_input
612                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
613                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
614                    return;
615            stop_conv_timer();
616    
617            if ((pFrame->general & XVID_CHROMAOPT)) {
618                    image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
619                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
620            }
621    
622            pEnc->queue_size++;
623            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
624    }
625    
626    static __inline void
627    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
628    {
629    
630                    pCur->ticks = (int32_t)pCur->stamp % time_base;
631                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
632    
633                    /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */
634    
635    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
636                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
637                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
638                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
639                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
640                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
641    
642    */
643    }
644    
645    
646    
647    /* convert pFrame->intra to coding_type */
648    static int intra2coding_type(int intra)
649    {
650            if (intra < 0)  return -1;
651            if (intra == 1) return I_VOP;
652            if (intra == 2) return B_VOP;
653    
654            return P_VOP;
655    }
656    
657    
658    
659  /*****************************************************************************  /*****************************************************************************
660   * Frame encoder entry point   * IPB frame encoder entry point
  *  
  * At this moment 2 versions coexist : one for IPB compatible encoder,  
  *                                     another one for the old IP encoder.  
661   *   *
662   * Returned values :   * Returned values :
663   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 544  Line 665 
665   *                        format   *                        format
666   ****************************************************************************/   ****************************************************************************/
667    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
668  int  int
669  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
670                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
671                             XVID_ENC_STATS * pResult)                             XVID_ENC_STATS * pResult)
672  {  {
673          uint16_t x, y;          uint16_t x, y;
674          Bitstream bs;          Bitstream bs;
675          uint32_t bits;          uint32_t bits;
676            int mode = -1; /* Just to shut up compiler warning */
677    
678  #ifdef _DEBUG          int input_valid = 1;
679          float psnr;          int bframes_count = 0;
         char temp[128];  
 #endif  
680    
681          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
682          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
683            ENC_CHECK(pFrame->image);
684    
685          start_global_timer();          start_global_timer();
686    
687          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
688    
689    ipvop_loop:
690    
691          /*          /*
692           * bframe "flush" code           * bframe "flush" code
693           */           */
# Line 584  Line 702 
702                           * frame as a pframe                           * frame as a pframe
703                           */                           */
704    
705                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
706                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
707                             dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
708                           */  
709                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
710                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
711    
712                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
713    
714                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
715                            bframes_count = 0;
716    
717                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
718                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
719                          pFrame->intra = 0;                          pFrame->intra = 0;
720    
721    
722                            emms();
723    
724                            if (pResult) {
725                                    pResult->quant = pEnc->current->quant;
726                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
727                                    pResult->kblks = pEnc->current->sStat.kblks;
728                                    pResult->mblks = pEnc->current->sStat.mblks;
729                                    pResult->ublks = pEnc->current->sStat.ublks;
730                            }
731    
732                          return XVID_ERR_OK;                          return XVID_ERR_OK;
733                  }                  }
734    
735                  /* ToDo : remove dprintf calls */  
736                  /*                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
737                     dprintf("--- BFRAME (flush) --- ");                                  pEnc->bframenum_head, pEnc->bframenum_tail,
738                   */                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
739    
740                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
741                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
742    
743                    BitstreamPadAlways(&bs);
                 BitstreamPad(&bs);  
744                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
745                  pFrame->input_consumed = 0;                  pFrame->intra = 2;
                 pFrame->intra = 0;  
746    
747                  return XVID_ERR_OK;                  if (pResult) {
748                            pResult->quant = pEnc->current->quant;
749                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
750                            pResult->kblks = pEnc->current->sStat.kblks;
751                            pResult->mblks = pEnc->current->sStat.mblks;
752                            pResult->ublks = pEnc->current->sStat.ublks;
753          }          }
754    
755          if (pFrame->image == NULL) {                  emms();
756                  pFrame->length = 0;  
757                  pFrame->input_consumed = 1;                  if (pFrame->quant == 0) {
758                  pFrame->intra = 0;                          RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
759                                                              pFrame->length, pFrame->intra);
760                    }
761    
762                    if (input_valid)
763                            queue_image(pEnc, pFrame);
764    
765                    emms();
766    
767                  return XVID_ERR_OK;                  return XVID_ERR_OK;
768          }          }
769    
770          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
771                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
772    
773                    /* write an empty marker to the bitstream.
774                       for divx5 decoder compatibility, this marker must consist
775                       of a not-coded p-vop, with a time_base of zero, and time_increment
776                       indentical to the future-referece frame.
777                    */
778    
779                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
780                            int tmp;
781    
782                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
783                                    pEnc->bframenum_head, pEnc->bframenum_tail,
784                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
785    
786                            tmp = pEnc->current->seconds;
787                            pEnc->current->seconds = 0; /* force time_base = 0 */
788    
789                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
790                            pEnc->current->seconds = tmp;
791    
792                            BitstreamPadAlways(&bs);
793                            pFrame->length = BitstreamLength(&bs);
794                            pFrame->intra = 4;
795    
796                            if (pResult) {
797                                    pResult->quant = pEnc->current->quant;
798                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
799                                    pResult->kblks = pEnc->current->sStat.kblks;
800                                    pResult->mblks = pEnc->current->sStat.mblks;
801                                    pResult->ublks = pEnc->current->sStat.ublks;
802          }          }
803    
804          pEnc->flush_bframes = 0;                          emms();
805    
806          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                          if (pFrame->quant == 0) {
807           * Well there was a separation here so i put it in ANSI C                                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
808           * comment style :-)                                                                    pFrame->length, pFrame->intra);
809           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */                          }
810    
811                            if (input_valid)
812                                    queue_image(pEnc, pFrame);
813    
814                            emms();
815    
816                            return XVID_ERR_OK;
817                    }
818            }
819    
820    
821    bvop_loop:
822    
823            if (pEnc->bframenum_dx50bvop != -1)
824            {
825    
826                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
827                    SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
828    
829          SWAP(pEnc->current, pEnc->reference);                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
830                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
831                    }
832    
833                    if (input_valid)
834                    {
835                            queue_image(pEnc, pFrame);
836                            input_valid = 0;
837                    }
838    
839            } else if (input_valid) {
840    
841          EMMS();                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
842    
843                    start_timer();
844                    if (image_input
845                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
846                            pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
847                    {
848                            emms();
849                            return XVID_ERR_FORMAT;
850                    }
851                    stop_conv_timer();
852    
853                    if ((pFrame->general & XVID_CHROMAOPT)) {
854                            image_chroma_optimize(&pEnc->current->image,
855                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
856                    }
857    
858                    /* queue input frame, and dequue next image */
859                    if (pEnc->queue_size > 0)
860                    {
861                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
862                            if (pEnc->queue_head != pEnc->queue_tail)
863                            {
864                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
865                            }
866                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
867                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
868                    }
869    
870            } else if (pEnc->queue_size > 0) {
871    
872                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
873    
874                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
875                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
876                    pEnc->queue_size--;
877    
878            } else {
879    
880                    /* if nothing was encoded, write an 'ignore this frame' flag
881                       to the bitstream */
882    
883                    if (BitstreamPos(&bs) == 0) {
884    
885                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
886                                    pEnc->bframenum_head, pEnc->bframenum_tail,
887                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
888    
889                            /* That disabled line of code was supposed to inform VirtualDub
890                             * that the frame was a dummy delay frame - now disabled (thx god :-)
891                             */
892                            //BitstreamPutBits(&bs, 0x7f, 8);
893                            pFrame->intra = 5;
894    
895                            if (pResult) {
896                                    /*
897                                     * We must decide what to put there because i know some apps
898                                     * are storing statistics about quantizers and just do
899                                     * stats[quant]++ or stats[quant-1]++
900                                     * transcode is one of these app with its 2pass module
901                                     */
902    
903                                    /*
904                                     * For now i prefer 31 than 0 that could lead to a segfault
905                                     * in transcode
906                                     */
907                                    pResult->quant = 31;
908    
909                                    pResult->hlength = 0;
910                                    pResult->kblks = 0;
911                                    pResult->mblks = 0;
912                                    pResult->ublks = 0;
913                            }
914                    } else {
915    
916                            if (pResult) {
917                                    pResult->quant = pEnc->current->quant;
918                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
919                                    pResult->kblks = pEnc->current->sStat.kblks;
920                                    pResult->mblks = pEnc->current->sStat.mblks;
921                                    pResult->ublks = pEnc->current->sStat.ublks;
922                            }
923    
924                    }
925    
926                    pFrame->length = BitstreamLength(&bs);
927    
928                    emms();
929    
930                    return XVID_ERR_OK;
931            }
932    
933            pEnc->flush_bframes = 0;
934    
935            emms();
936    
937            /* only inc frame num, adapt quant, etc. if we havent seen it before */
938            if (pEnc->bframenum_dx50bvop < 0 )
939            {
940                    mode = intra2coding_type(pFrame->intra);
941          if (pFrame->quant == 0)          if (pFrame->quant == 0)
942                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
943          else          else
944                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
945    
946          if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
947                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
948    
949          if (pEnc->current->quant > 31)          if (pEnc->current->quant > 31)
950                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
951    */
952          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
953          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
954          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
955          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
956          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
957          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
958    
959          start_timer();                  inc_frame_num(pEnc);
         if (image_input  
                 (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))  
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
960    
961  #ifdef _DEBUG                  if (pFrame->general & XVID_EXTRASTATS)
962          image_copy(&pEnc->sOriginal, &pEnc->current->image,                  {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
963                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
964  #endif                  }
965    
966          EMMS();                  emms();
967    
968                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
969                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
970                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
971                    }
972    
973          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974           * Luminance masking           * Luminance masking
# Line 700  Line 995 
995                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
996                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
997    
998                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
999                                    }
1000    #undef OFFSET
1001                            }
1002                            xvid_free(temp_dquants);
1003                    }
1004            }
1005    
1006            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1007             * ivop/pvop/bvop selection
1008             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1009            pEnc->iFrameNum++;
1010    
1011            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1012                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1013                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) {
1014                    mode = I_VOP;
1015            } else {
1016                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1017                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1018                                            (/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0,
1019                                            bframes_count++, pFrame->bframe_threshold);
1020            }
1021    
1022            if (mode == I_VOP) {
1023                    /*
1024                     * This will be coded as an Intra Frame
1025                     */
1026                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1027                            pEnc->mbParam.m_quarterpel = 1;
1028                    else
1029                            pEnc->mbParam.m_quarterpel = 0;
1030    
1031                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1032    
1033                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1034                            if (pFrame->quant_intra_matrix != NULL)
1035                                    set_intra_matrix(pFrame->quant_intra_matrix);
1036                            if (pFrame->quant_inter_matrix != NULL)
1037                                    set_inter_matrix(pFrame->quant_inter_matrix);
1038                          }                          }
1039    
 #undef OFFSET  
1040    
1041                  }                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1042                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1043                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1044    
1045                  xvid_free(temp_dquants);                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1046                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1047          }          }
1048    
1049          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */
          * ivop/pvop/bvop selection  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
1050    
1051                    if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1052    
1053          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||                          pEnc->bframenum_tail--;
1054                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
                  pEnc->iFrameNum >= pEnc->iMaxKeyInterval)  
                 || image_mad(&pEnc->reference->image, &pEnc->current->image,  
                                          pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                          pEnc->mbParam.height) > 30) {  
                 /*  
                  * This will be coded as an Intra Frame  
                  */  
1055    
1056                  /* ToDo : Remove dprintf calls */                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1057                  /*                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1058                     dprintf("--- IFRAME ---");                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1059                   */                          }
1060                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
1061                            bframes_count = 0;
1062                            pFrame->intra = 0;
1063    
1064                  FrameCodeI(pEnc, &bs, &bits);                  } else {
1065    
1066                            FrameCodeI(pEnc, &bs, &bits);
1067                            bframes_count = 0;
1068                  pFrame->intra = 1;                  pFrame->intra = 1;
1069    
1070                            pEnc->bframenum_dx50bvop = -1;
1071                    }
1072    
1073                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1074    
1075                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1076                            BitstreamPadAlways(&bs);
1077                            input_valid = 0;
1078                            goto ipvop_loop;
1079                    }
1080    
1081                  /*                  /*
1082                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
1083                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
1084                   */                   */
1085          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1086                  /*                  /*
1087                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
1088                   */                   */
1089    
1090                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1091                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1092                     dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1093                   */  
1094                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1095                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1096                    }
1097    
1098                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1099                    bframes_count = 0;
1100                  pFrame->intra = 0;                  pFrame->intra = 0;
1101                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1102          } else {  
1103                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1104                            BitstreamPadAlways(&bs);
1105                            input_valid = 0;
1106                            goto ipvop_loop;
1107                    }
1108    
1109            } else {        /* mode == B_VOP */
1110                  /*                  /*
1111                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1112                   */                   */
1113    
1114                  /* ToDo : Remove dprintf calls */                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1115                  /*                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1116                     dprintf("--- BFRAME (store) ---  head=%i tail=%i",                  }
                    pEnc->bframenum_head,  
                    pEnc->bframenum_tail);  
                  */  
1117    
1118                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1119                          pEnc->current->quant =                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1120                                  ((pEnc->reference->quant +                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1121                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
1122                  } else {                  } else {
1123                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1124                  }                  }
1125    
1126                    if (pEnc->current->quant < 1)
1127                            pEnc->current->quant = 1;
1128                    else if (pEnc->current->quant > 31)
1129                            pEnc->current->quant = 31;
1130    
1131                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1132                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1133                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1134    
1135                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1136                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1137                  SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1138    
1139                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1140    
1141                  pFrame->intra = 0;                  /* bframe report by koepi */
1142                    pFrame->intra = 2;
1143                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1144    
1145                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1146                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {                  goto bvop_loop;
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
1147                  }                  }
1148    
1149                  return XVID_ERR_OK;          BitstreamPadAlways(&bs);
         }  
   
         BitstreamPad(&bs);  
1150          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1151    
1152          if (pResult) {          if (pResult) {
1153                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1154                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1155                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1156                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1157                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1158          }  
1159                    if (pFrame->general & XVID_EXTRASTATS)
1160          EMMS();                  {       pResult->sse_y =
1161                                    plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
 #ifdef _DEBUG  
         psnr =  
                 image_psnr(&pEnc->sOriginal, &pEnc->current->image,  
1162                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1163                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1164    
1165          snprintf(temp, 127, "PSNR: %f\n", psnr);                          pResult->sse_u =
1166          DEBUG(temp);                                  plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1167  #endif                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1168                                                       pEnc->mbParam.height/2);
1169    
1170                            pResult->sse_v =
1171                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1172                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1173                                                       pEnc->mbParam.height/2);
1174                    }
1175            }
1176    
1177            emms();
1178    
1179          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1180                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1181                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1182          }          }
1183    
         pEnc->iFrameNum++;  
         pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;  
         if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {  
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
         }  
         pFrame->input_consumed = 1;  
   
1184          stop_global_timer();          stop_global_timer();
1185          write_timer();          write_timer();
1186    
1187            emms();
1188          return XVID_ERR_OK;          return XVID_ERR_OK;
1189  }  }
1190  #else  
1191    
1192    
1193  /*****************************************************************************  /*****************************************************************************
1194   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1195     *
1196     * Returned values :
1197     *      - XVID_ERR_OK    - no errors
1198     *      - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1199     *                                              format
1200   ****************************************************************************/   ****************************************************************************/
1201    
1202  int  int
1203  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1204                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
# Line 846  Line 1209 
1209          uint32_t bits;          uint32_t bits;
1210          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1211    
 #ifdef _DEBUG  
1212          float psnr;          float psnr;
1213          uint8_t temp[128];          char temp[128];
 #endif  
1214    
1215          start_global_timer();          start_global_timer();
1216    
# Line 858  Line 1219 
1219          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1220          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1221    
1222          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1223    
1224          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1225          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1226          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1227    
1228            inc_frame_num(pEnc);
1229    
1230            /* disable alternate scan flag if interlacing is not enabled */
1231            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1232                    !(pEnc->current->global_flags & XVID_INTERLACING))
1233            {
1234                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1235            }
1236    
1237          start_timer();          start_timer();
1238          if (image_input          if (image_input
1239                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1240                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1241                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1242          stop_conv_timer();          stop_conv_timer();
1243    
1244  #ifdef _DEBUG          if ((pFrame->general & XVID_CHROMAOPT)) {
1245          image_copy(&pEnc->sOriginal, &pEnc->current->image,                  image_chroma_optimize(&pEnc->current->image,
1246                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1247            }
1248    
1249            if (pFrame->general & XVID_EXTRASTATS)
1250            {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1251                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1252  #endif          }
1253    
1254          EMMS();          emms();
1255    
1256          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1257    
# Line 886  Line 1261 
1261                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1262          }          }
1263    
1264            if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1265                    pEnc->mbParam.m_quarterpel = 1;
1266            else
1267                    pEnc->mbParam.m_quarterpel = 0;
1268    
1269          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1270                  int *temp_dquants =                  int *temp_dquants =
1271                          (int *) xvid_malloc(pEnc->mbParam.mb_width *                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
# Line 946  Line 1326 
1326          }          }
1327    
1328          if (pFrame->intra < 0) {          if (pFrame->intra < 0) {
1329                  if ((pEnc->iFrameNum == 0)                  if ((pEnc->iFrameNum == -1)
1330                          || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1331                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1332                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1333                  } else {                  } else {
1334                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
# Line 962  Line 1342 
1342    
1343          }          }
1344    
1345            /* Relic from OpenDivX - now disabled
1346          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1347          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1348          BitstreamPad(&bs);          */
1349    
1350            BitstreamPadAlways(&bs);
1351          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1352    
1353          if (pResult) {          if (pResult) {
1354                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1355                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1356                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1357                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1358                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1359          }          }
1360    
1361          EMMS();          emms();
1362    
1363          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1364                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1365                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1366          }          }
1367  #ifdef _DEBUG          if (pFrame->general & XVID_EXTRASTATS)
1368            {
1369          psnr =          psnr =
1370                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1371                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1372                                     pEnc->mbParam.height);                                     pEnc->mbParam.height);
1373    
1374          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1375          DEBUG(temp);          }
 #endif  
1376    
1377          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1378    
# Line 998  Line 1381 
1381    
1382          return XVID_ERR_OK;          return XVID_ERR_OK;
1383  }  }
 #endif  
1384    
1385    
1386  static __inline void  static __inline void
# Line 1055  Line 1437 
1437                  return;                  return;
1438          }          }
1439    
1440          pEnc->current->fcode =          pEnc->current->fcode = (hint->rawhints) ?
1441                  (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,                  (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
                                                                                                                                  FCODEBITS);  
1442    
1443          length = pEnc->current->fcode + 5;          length = pEnc->current->fcode + 5;
1444          high = 1 << (length - 1);          high = 1 << (length - 1);
# Line 1068  Line 1449 
1449                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1450                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1451                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1452                          VECTOR pred[4];                          VECTOR pred;
1453                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1454                          int vec;                          int vec;
1455    
1456                          pMB->mode =                          pMB->mode =     (hint->rawhints) ?
1457                                  (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,                                  (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
                                                                                                                                   MODEBITS);  
1458    
1459                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1460                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1461    
1462                          if (pMB->mode == MODE_INTER) {                          if (pMB->mode == MODE_INTER) {
1463                                  tmp.x =                                  tmp.x = (hint->rawhints) ?
1464                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,                                          bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
1465                                                                                                                                                    length);                                  tmp.y = (hint->rawhints) ?
1466                                  tmp.y =                                          bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
                                         (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,  
                                                                                                                                                   length);  
1467                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1468                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1469    
1470                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
                                                         0, pred, dummy);  
1471    
1472                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1473                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1474                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1475                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1476                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1477                                  }                                  }
1478                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1479                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1480                                          tmp.x =                                          tmp.x = (hint->rawhints) ?
1481                                                  (hint->rawhints) ? bhint->mvs[vec].                                                  bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
1482                                                  x : BitstreamGetBits(&bs, length);                                          tmp.y = (hint->rawhints) ?
1483                                          tmp.y =                                                  bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
                                                 (hint->rawhints) ? bhint->mvs[vec].  
                                                 y : BitstreamGetBits(&bs, length);  
1484                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1485                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1486    
1487                                          get_pmvdata(pEnc->current->mbs, x, y,                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
                                                                 pEnc->mbParam.mb_width, vec, pred, dummy);  
1488    
1489                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1490                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1491                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1492                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1493                                  }                                  }
1494                          } else                          // intra / stuffing / not_coded                          } else                          /* intra / stuffing / not_coded */
1495                          {                          {
1496                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1497                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
# Line 1159  Line 1532 
1532    
1533          if (intra) {          if (intra) {
1534                  if (!hint->rawhints) {                  if (!hint->rawhints) {
1535                          BitstreamPad(&bs);                          BitstreamPadAlways(&bs);
1536                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1537                  }                  }
1538                  return;                  return;
# Line 1234  Line 1607 
1607                     Bitstream * bs,                     Bitstream * bs,
1608                     uint32_t * pBits)                     uint32_t * pBits)
1609  {  {
1610            int mb_width = pEnc->mbParam.mb_width;
1611            int mb_height = pEnc->mbParam.mb_height;
1612    
1613          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1614          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1615    
1616          uint16_t x, y;          uint16_t x, y;
1617    
1618            if ((pEnc->current->global_flags & XVID_REDUCED))
1619            {
1620                    mb_width = (pEnc->mbParam.width + 31) / 32;
1621                    mb_height = (pEnc->mbParam.height + 31) / 32;
1622    
1623                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1624                    /* XXX: setedges is overkill */
1625                    start_timer();
1626                    image_setedges(&pEnc->current->image,
1627                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1628                            pEnc->mbParam.width, pEnc->mbParam.height);
1629                    stop_edges_timer();
1630            }
1631          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1632          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1633          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1634            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1635          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1636    
1637          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1638          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1639            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1640    
1641            BitstreamPadAlways(bs);
1642            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1643    
1644          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1645    
1646          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
1647          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          pEnc->current->sStat.kblks = mb_width * mb_height;
1648          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1649    
1650          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++)
1651                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
1652                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1653                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1654    
# Line 1269  Line 1662 
1662                          stop_prediction_timer();                          stop_prediction_timer();
1663    
1664                          start_timer();                          start_timer();
1665                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1666                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1667                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1668                                    qcoeff[5*64+0]=0;
1669                            }
1670                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1671                          stop_coding_timer();                          stop_coding_timer();
1672                  }                  }
1673    
1674            if ((pEnc->current->global_flags & XVID_REDUCED))
1675            {
1676                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1677                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1678                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1679            }
1680          emms();          emms();
1681    
1682          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1683          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1684          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1685    
1686          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1687                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1688          }          }
1689    
1690          return 1;                                       // intra          return 1;                                       /* intra */
1691  }  }
1692    
1693    
1694  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1695    #define BFRAME_SKIP_THRESHHOLD 30
1696    
1697    
1698    /* FrameCodeP also handles S(GMC)-VOPs */
1699  static int  static int
1700  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
1701                     Bitstream * bs,                     Bitstream * bs,
# Line 1303  Line 1708 
1708          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1709          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1710    
1711            int mb_width = pEnc->mbParam.mb_width;
1712            int mb_height = pEnc->mbParam.mb_height;
1713    
1714          int iLimit;          int iLimit;
1715          uint32_t x, y;          int x, y, k;
1716          int iSearchRange;          int iSearchRange;
1717          int bIntra;          int bIntra, skip_possible;
1718    
1719          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1720          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1721    
1722            if ((pEnc->current->global_flags & XVID_REDUCED))
1723            {
1724                    mb_width = (pEnc->mbParam.width + 31) / 32;
1725                    mb_height = (pEnc->mbParam.height + 31) / 32;
1726            }
1727    
1728          start_timer();          start_timer();
1729          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1730                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1731          stop_edges_timer();          stop_edges_timer();
1732    
1733          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1734          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1735            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1736          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1737    
1738          if (!force_inter)          if (!force_inter)
1739                  iLimit =                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
                         (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *  
                                    INTRA_THRESHOLD);  
1740          else          else
1741                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1742    
1743          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1744                  start_timer();                  start_timer();
1745                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1746                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1747                                                    pEnc->mbParam.edged_height,                                                    pEnc->mbParam.edged_height,
1748                                                      pEnc->mbParam.m_quarterpel,
1749                                                    pEnc->current->rounding_type);                                                    pEnc->current->rounding_type);
1750                  stop_inter_timer();                  stop_inter_timer();
1751          }          }
1752    
1753            pEnc->current->coding_type = P_VOP;
1754    
1755          start_timer();          start_timer();
1756          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1757                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1758          } else {          else
1759                  bIntra =                  bIntra =
1760                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1761                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1762                                                           iLimit);                                                           iLimit);
1763          }  
1764          stop_motion_timer();          stop_motion_timer();
1765    
1766          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1767    
1768          pEnc->current->coding_type = P_VOP;          if ( ( pEnc->current->global_flags & XVID_GMC )
1769                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1770            {
1771                    pEnc->current->coding_type = S_VOP;
1772    
1773                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1774                                            pEnc->mbParam.width, pEnc->mbParam.height,
1775                                            &pEnc->current->gmc_data);
1776    
1777                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1778                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1779                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1780                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1781                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1782    
1783            }
1784    
1785            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1786          if (vol_header)          if (vol_header)
1787                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1788                    BitstreamPadAlways(bs);
1789            }
1790    
1791          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1792    
1793          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1794    
1795          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1796          pEnc->sStat.iMvSum = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
         pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1797    
1798          for (y = 0; y < pEnc->mbParam.mb_height; y++) {  
1799                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          for (y = 0; y < mb_height; y++) {
1800                    for (x = 0; x < mb_width; x++) {
1801                          MACROBLOCK *pMB =                          MACROBLOCK *pMB =
1802                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1803    
1804    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1805    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1806    
1807                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1808    
1809                          if (!bIntra) {                          if (bIntra) {
1810                                    CodeIntraMB(pEnc, pMB);
1811                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1812                                                                      dct_codes, qcoeff);
1813    
1814                                    start_timer();
1815                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1816                                    stop_prediction_timer();
1817    
1818                                    pEnc->current->sStat.kblks++;
1819    
1820                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1821                                    stop_coding_timer();
1822                                    continue;
1823                            }
1824    
1825                            if (pEnc->current->coding_type == S_VOP) {
1826    
1827                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1828                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1829                                            pEnc->mbParam.edged_width, 65536);
1830    
1831                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1832                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1833                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1834    
1835                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1836                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1837                                    }
1838    
1839                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1840    
1841                                            if (pEnc->mbParam.m_quarterpel)
1842                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1843                                            else
1844                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1845    
1846                                            pMB->mode = MODE_INTER;
1847                                            pMB->mcsel = 1;
1848                                            pMB->sad16 = iSAD;
1849                                    } else {
1850                                            pMB->mcsel = 0;
1851                                    }
1852                            } else {
1853                                    pMB->mcsel = 0; /* just a precaution */
1854                            }
1855    
1856                                  start_timer();                                  start_timer();
1857                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1858                                                                           &pEnc->vInterH, &pEnc->vInterV,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1859                                                                           &pEnc->vInterHV, &pEnc->current->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1860                                                                     &pEnc->current->image,
1861                                                                           dct_codes, pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1862                                                                           pEnc->mbParam.height,                                                                           pEnc->mbParam.height,
1863                                                                           pEnc->mbParam.edged_width,                                                                           pEnc->mbParam.edged_width,
1864                                                                     pEnc->mbParam.m_quarterpel,
1865                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1866                                                                           pEnc->current->rounding_type);                                                                           pEnc->current->rounding_type);
1867    
1868                                  stop_comp_timer();                                  stop_comp_timer();
1869    
1870                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
# Line 1398  Line 1881 
1881    
1882                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1883    
1884                                  pMB->cbp =                          if (pMB->mode != MODE_NOT_CODED)
1885                            {       pMB->cbp =
1886                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1887                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
                         } else {  
                                 CodeIntraMB(pEnc, pMB);  
                                 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
1888                          }                          }
1889    
1890                          start_timer();                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
   
                         if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {  
                                 pEnc->sStat.kblks++;  
                         } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||  
1891                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1892                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1893                                  pEnc->sStat.mblks++;                                  pEnc->current->sStat.mblks++;
1894                          } else {                          } else {
1895                                  pEnc->sStat.ublks++;                                  pEnc->current->sStat.ublks++;
1896                          }                          }
1897    
1898                          start_timer();                          start_timer();
1899                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1900                            /* Finished processing the MB, now check if to CODE or SKIP */
1901    
1902                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1903                                                            (pMB->dquant == NO_CHANGE);
1904    
1905                            if (pEnc->current->coding_type == S_VOP)
1906                                    skip_possible &= (pMB->mcsel == 1);
1907                            else if (pEnc->current->coding_type == P_VOP) {
1908                                    if (pEnc->mbParam.m_quarterpel)
1909                                            skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1910                                    else
1911                                            skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
1912                            }
1913    
1914                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1915    
1916    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1917    
1918                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1919                                    {
1920                                            int bSkip = 1;
1921    
1922                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1923                                            {
1924                                                    int iSAD;
1925                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1926                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1927                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1928                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1929                                                    {       bSkip = 0;
1930                                                            break;
1931                                                    }
1932                                            }
1933    
1934                                            if (!bSkip) {   /* no SKIP, but trivial block */
1935                                                    if(pEnc->mbParam.m_quarterpel) {
1936                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1937                                                            pMB->pmvs[0].x = - predMV.x;
1938                                                            pMB->pmvs[0].y = - predMV.y;
1939                                                    }
1940                                                    else {
1941                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1942                                                            pMB->pmvs[0].x = - predMV.x;
1943                                                            pMB->pmvs[0].y = - predMV.y;
1944                                                    }
1945                                                    pMB->mode = MODE_INTER;
1946                                                    pMB->cbp = 0;
1947                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1948                                                    stop_coding_timer();
1949    
1950                                                    continue;       /* next MB */
1951                                            }
1952                                    }
1953                                    /* do SKIP */
1954    
1955                                    pMB->mode = MODE_NOT_CODED;
1956                                    MBSkip(bs);
1957                                    stop_coding_timer();
1958                                    continue;       /* next MB */
1959                            }
1960                            /* ordinary case: normal coded INTER/INTER4V block */
1961    
1962                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1963                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1964                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1965                                    qcoeff[5*64+0]=0;
1966                            }
1967    
1968                            if(pEnc->mbParam.m_quarterpel) {
1969                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1970                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1971                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1972                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1973                            } else {
1974                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1975                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1976                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1977                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1978                            }
1979    
1980    
1981                            if (pMB->mode == MODE_INTER4V)
1982                            {       int k;
1983                                    for (k=1;k<4;k++)
1984                                    {
1985                                            if(pEnc->mbParam.m_quarterpel) {
1986                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1987                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1988                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1989                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1990                                            } else {
1991                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1992                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1993                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1994                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1995                                            }
1996    
1997                                    }
1998                            }
1999    
2000                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2001                          stop_coding_timer();                          stop_coding_timer();
2002    
2003                  }                  }
2004          }          }
2005    
2006            if ((pEnc->current->global_flags & XVID_REDUCED))
2007            {
2008                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2009                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2010                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2011            }
2012    
2013          emms();          emms();
2014    
2015          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
2016                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2017          }          }
2018    
2019          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2020                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2021    
2022          fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2023    
2024          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2025    
2026          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2027                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */
2028          {          {
2029                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
2030                  iSearchRange *= 2;                  iSearchRange *= 2;
2031          } else if ((fSigma < iSearchRange / 6)          } else if ((fSigma < iSearchRange / 6)
2032                             && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma >= 0)
2033                             && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2034                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16                             && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel)))      /* minimum search range 16 */
2035          {          {
2036                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
2037                  iSearchRange /= 2;                  iSearchRange /= 2;
2038          }          }
2039    
2040          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2041    
2042            /* frame drop code */
2043            DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
2044            if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2045                    (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2046            {
2047                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2048                    pEnc->current->sStat.ublks = mb_width * mb_height;
2049    
2050                    BitstreamReset(bs);
2051    
2052                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2053                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2054    
2055                    /* copy reference frame details into the current frame */
2056                    pEnc->current->quant = pEnc->reference->quant;
2057                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
2058                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
2059                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
2060                    pEnc->current->fcode = pEnc->reference->fcode;
2061                    pEnc->current->bcode = pEnc->reference->bcode;
2062                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
2063                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2064            }
2065    
2066            /* XXX: debug
2067            {
2068                    char s[100];
2069                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2070                    image_dump_yuvpgm(&pEnc->current->image,
2071                            pEnc->mbParam.edged_width,
2072                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2073    
2074                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2075                    image_dump_yuvpgm(&pEnc->reference->image,
2076                            pEnc->mbParam.edged_width,
2077                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2078            }
2079            */
2080    
2081    
2082          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2083    
2084          return 0;                                       // inter          return 0;                                       /* inter */
2085  }  }
2086    
2087    
 #ifdef BFRAMES  
2088  static void  static void
2089  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2090                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2091                     Bitstream * bs,                     Bitstream * bs,
2092                     uint32_t * pBits)                     uint32_t * pBits)
2093  {  {
2094          int16_t dct_codes[6 * 64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2095          int16_t qcoeff[6 * 64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2096          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
2097    
2098          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2099          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2100    
2101          // forward  #ifdef BFRAMES_DEC_DEBUG
2102            FILE *fp;
2103            static char first=0;
2104    #define BFRAME_DEBUG    if (!first && fp){ \
2105                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2106            }
2107    
2108            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2109    
2110            if (!first){
2111                    fp=fopen("C:\\XVIDDBGE.TXT","w");
2112            }
2113    #endif
2114    
2115            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2116    
2117            /* forward  */
2118          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2119                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2120                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2121          start_timer();          start_timer();
2122          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,
2123                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2124                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2125          stop_inter_timer();          stop_inter_timer();
2126    
2127          // backward          /* backward */
2128          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2129                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2130                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
2131          start_timer();          start_timer();
2132          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2133                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2134                                            0);                                            pEnc->mbParam.m_quarterpel, 0);
2135          stop_inter_timer();          stop_inter_timer();
2136    
2137          start_timer();          start_timer();
2138          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,  
2139            MotionEstimationBVOP(&pEnc->mbParam, frame,
2140                                                     ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2141                                                     ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2142                                                     pEnc->reference->mbs, f_ref,
2143                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2144                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2145                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
2146    
2147    
2148          stop_motion_timer();          stop_motion_timer();
2149            /*
2150          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
            {  
2151             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);             BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
2152             } */          }
2153            */
2154    
2155          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2156          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
2157            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2158            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
2159    
2160          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
2161    
2162          pEnc->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2163          pEnc->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2164          pEnc->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2165          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2166    
2167    
2168          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 // reset prediction  
   
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
   
2169                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2170                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2171                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
                         MACROBLOCK *b_mb =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
2172    
2173                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2174                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
2175                                  mb->mvs[0].x = 0;                                  /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */
                                 mb->mvs[0].y = 0;  
2176                                  continue;                                  continue;
2177                          }                          }
2178    
2179                            if (mb->mode != MODE_DIRECT_NONE_MV) {
2180                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2181                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2182                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2183                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
2184                                                                           dct_codes);                                                                           dct_codes);
2185    
2186                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2187                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp =  
                                 MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,  
                                                                   qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
   
2188    
2189                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  mb->cbp =
2190                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mode = 5;   // skipped  
                         }  
2191    
2192                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2193                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2194                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
2195                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {  
                                 mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;  
                                 mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
2196                          }                          }
 //          printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  
2197    
2198    #ifdef BFRAMES_DEC_DEBUG
2199            BFRAME_DEBUG
2200    #endif
2201                          start_timer();                          start_timer();
2202                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2203                                                   &pEnc->sStat);                                                   &frame->sStat, direction);
2204                          stop_coding_timer();                          stop_coding_timer();
2205                  }                  }
2206          }          }
2207    
2208          emms();          emms();
2209    
2210          // TODO: dynamic fcode/bcode ???          /* TODO: dynamic fcode/bcode ??? */
2211    
2212          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2213    
2214    #ifdef BFRAMES_DEC_DEBUG
2215            if (!first){
2216                    first=1;
2217                    if (fp)
2218                            fclose(fp);
2219  }  }
2220  #endif  #endif
2221    }

Legend:
Removed from v.1.41  
changed lines
  Added in v.1.99

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