[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.46, Sun Jun 23 03:58:32 2002 UTC revision 1.76.2.3, Fri Sep 27 17:25:17 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37   *      20.06.2002 bframe patch   *      20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
# Line 41  Line 43 
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
46    
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <stdio.h>  #include <stdio.h>
49  #include <math.h>  #include <math.h>
# Line 51  Line 54 
54  #include "global.h"  #include "global.h"
55  #include "utils/timer.h"  #include "utils/timer.h"
56  #include "image/image.h"  #include "image/image.h"
57    #ifdef BFRAMES
58  #include "image/font.h"  #include "image/font.h"
59    #include "motion/sad.h"
60    #endif
61  #include "motion/motion.h"  #include "motion/motion.h"
62  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
63  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 85  Line 91 
91                                            bool force_inter,                                            bool force_inter,
92                                            bool vol_header);                                            bool vol_header);
93    
 #ifdef BFRAMES  
94  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
95                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
96                                             Bitstream * bs,                                             Bitstream * bs,
97                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
98    
99  /*****************************************************************************  /*****************************************************************************
100   * Local data   * Local data
# Line 202  Line 206 
206    
207          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
208    
209          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
210                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
211    
   
212          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
213          if (pEnc == NULL)          if (pEnc == NULL)
214                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
# Line 264  Line 267 
267  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
268          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
269  #endif  #endif
270  #ifdef BFRAMES  
271          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
272          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
273          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
274  #endif  
275          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
276          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
277          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 283  Line 286 
286                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
287                  goto xvid_err_memory3;                  goto xvid_err_memory3;
288  #endif  #endif
289  #ifdef BFRAMES  
290          if (image_create          if (image_create
291                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
292                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 296  Line 299 
299                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
300                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
301                  goto xvid_err_memory3;                  goto xvid_err_memory3;
302  #endif  
303          if (image_create          if (image_create
304                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
305                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 329  Line 332 
332    
333    
334          /* B Frames specific init */          /* B Frames specific init */
 #ifdef BFRAMES  
335    
336          pEnc->global = pParam->global;          pEnc->global = pParam->global;
337          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
338          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
339            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
340          pEnc->bframes = NULL;          pEnc->bframes = NULL;
341    
342          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 376  Line 379 
379          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
380          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
381          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
382            pEnc->bframenum_dx50bvop = -1;
383    
384          pEnc->queue = NULL;          pEnc->queue = NULL;
385    
# Line 410  Line 414 
414          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
415          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
416          pEnc->m_framenum = 0;          pEnc->m_framenum = 0;
417  #endif          pEnc->last_pframe = 0;
418            pEnc->last_sync = 0;
419    
420          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
421    
# Line 429  Line 434 
434          /*          /*
435           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
436           */           */
437  #ifdef BFRAMES  
438    xvid_err_memory5:    xvid_err_memory5:
439    
440    
# Line 463  Line 468 
468                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
469          }          }
470    
 #endif  
   
471    xvid_err_memory3:    xvid_err_memory3:
472  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
473          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
474                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
475  #endif  #endif
476    
 #ifdef BFRAMES  
477          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
478                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
479          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
480                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
481          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
482                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
483    
484          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
485                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 523  Line 524 
524  int  int
525  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
526  {  {
 #ifdef BFRAMES  
527          int i;          int i;
 #endif  
528    
529          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
530    
531          /* B Frames specific */          /* B Frames specific */
 #ifdef BFRAMES  
532          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
533    
534                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
# Line 560  Line 558 
558                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
559    
560          }          }
 #endif  
561    
562          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
563    
# Line 578  Line 575 
575                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
576          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
577                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
578  #ifdef BFRAMES  
579          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
580                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
581          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
582                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
583          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
584                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
585  #endif  
586  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
587          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
588                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 605  Line 602 
602  }  }
603    
604    
605  #ifdef BFRAMES  static __inline void inc_frame_num(Encoder * pEnc)
 void inc_frame_num(Encoder * pEnc)  
606  {  {
         pEnc->iFrameNum++;  
607          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
608    
         pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;  
609          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
610            if (pEnc->mbParam.m_ticks < pEnc->last_sync)
611                    pEnc->mbParam.m_seconds = 1;
612                                    // more than 1 second since last I or P is not supported.
613            else
614                    pEnc->mbParam.m_seconds = 0;
615    
616  }  }
 #endif  
617    
618    
619  #ifdef BFRAMES  static __inline void
620  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
621  {  {
622          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
623          {          {
624                  DPRINTF("FATAL: QUEUE FULL");                  DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
625                  return;                  return;
626          }          }
627    
628          DPRINTF("*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",          DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
629                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
630                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
631    
# Line 641  Line 640 
640          pEnc->queue_size++;          pEnc->queue_size++;
641          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
642  }  }
 #endif  
643    
644    
645  #ifdef BFRAMES  
646  /*****************************************************************************  /*****************************************************************************
647   * IPB frame encoder entry point   * IPB frame encoder entry point
648   *   *
# Line 661  Line 659 
659  {  {
660          uint16_t x, y;          uint16_t x, y;
661          Bitstream bs;          Bitstream bs;
662          uint32_t bits;          uint32_t bits, mode;
663    
664          int input_valid = 1;          int input_valid = 1;
665    
# Line 694  Line 692 
692                           * frame as a pframe                           * frame as a pframe
693                           */                           */
694    
695                          DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
696                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
697                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
698    
# Line 713  Line 711 
711                  }                  }
712    
713    
714                  DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
715                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
716                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
717    
# Line 735  Line 733 
733    
734                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
735    
736                          DPRINTF("*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
737                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
738                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
739    
   
740                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
741                          BitstreamPad(&bs);                          BitstreamPad(&bs);
742                          BitstreamPutBits(&bs, 0x7f, 8);                          BitstreamPutBits(&bs, 0x7f, 8);
# Line 757  Line 754 
754    
755  bvop_loop:  bvop_loop:
756    
757          if (input_valid) {          if (pEnc->bframenum_dx50bvop != -1)
758            {
759    
760                    SWAP(pEnc->current, pEnc->reference);
761                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
762    
763                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
764                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
765                    }
766    
767                    if (input_valid)
768                    {
769                            queue_image(pEnc, pFrame);
770                            input_valid = 0;
771                    }
772    
773            } else if (input_valid) {
774    
775                  SWAP(pEnc->current, pEnc->reference);                  SWAP(pEnc->current, pEnc->reference);
776    
# Line 790  Line 803 
803    
804          } else if (BitstreamPos(&bs) == 0) {          } else if (BitstreamPos(&bs) == 0) {
805    
806                  DPRINTF("*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
807                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
808                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
809    
   
810                  pFrame->intra = 0;                  pFrame->intra = 0;
811    
812                  BitstreamPutBits(&bs, 0x7f, 8);                  BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
813                  BitstreamPad(&bs);                  BitstreamPad(&bs);
814                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
815    
# Line 811  Line 823 
823    
824          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
825    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
 //$$    SWAP(pEnc->current, pEnc->reference);  
   
826          emms();          emms();
827    
828            // only inc frame num, adapt quant, etc. if we havent seen it before
829            if (pEnc->bframenum_dx50bvop < 0 )
830            {
831          if (pFrame->quant == 0)          if (pFrame->quant == 0)
832                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
833          else          else
834                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
835    
836          if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
837                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
838    
839          if (pEnc->current->quant > 31)          if (pEnc->current->quant > 31)
840                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
841    */
842          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
843          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
844          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
845          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
846          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
847          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
848    
849  //$$$   start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
850  //$$$   if (image_input                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
851  //$$$           (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
852  //$$$            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                  inc_frame_num(pEnc);
 //$$$           return XVID_ERR_FORMAT;  
 //$$$   stop_conv_timer();  
853    
854  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
855          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 858  Line 863 
863                          "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);                          "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
864          }          }
865    
   
866          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867           * Luminance masking           * Luminance masking
868           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 888  Line 892 
892                          }                          }
893    
894  #undef OFFSET  #undef OFFSET
   
895                  }                  }
896    
897                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
898          }          }
899    
900            }
901    
902          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
904           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
905    
906    
907          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
908                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
909                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
910                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
911                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
912                                           pEnc->mbParam.height) > 30) {                                           pEnc->mbParam.height) > 30) {*/
913                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
914                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
915    
916                  /*                  /*
917                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
918                   */                   */
919    
920                  DPRINTF("*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
921                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
922                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
923    
# Line 917  Line 925 
925                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
926                  }                  }
927    
928                  FrameCodeI(pEnc, &bs, &bits);                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
929    
930                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
931    
932                            pEnc->bframenum_tail--;
933                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
934    
935                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
936                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
937                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
938                            }
939                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
940    
941                            pFrame->intra = 0;
942    
943                    } else {
944    
945                            FrameCodeI(pEnc, &bs, &bits);
946                  pFrame->intra = 1;                  pFrame->intra = 1;
                 pEnc->flush_bframes = 1;  
947    
948                  inc_frame_num(pEnc);                          pEnc->bframenum_dx50bvop = -1;
949                    }
950    
951                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  pEnc->flush_bframes = 1;
952    
953                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
954                          BitstreamPad(&bs);                          BitstreamPad(&bs);
955                          input_valid = 0;                          input_valid = 0;
956                          goto ipvop_loop;                          goto ipvop_loop;
# Line 934  Line 960 
960                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
961                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
962                   */                   */
963          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
964                  /*                  /*
965                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
966                   */                   */
967    
968                  DPRINTF("*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
969                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
970                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
971    
# Line 951  Line 977 
977                  pFrame->intra = 0;                  pFrame->intra = 0;
978                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
979    
                 inc_frame_num(pEnc);  
   
980                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
981                          BitstreamPad(&bs);                          BitstreamPad(&bs);
982                          input_valid = 0;                          input_valid = 0;
# Line 964  Line 988 
988                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
989                   */                   */
990    
                 DPRINTF("*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",  
                                 pEnc->bframenum_head, pEnc->bframenum_tail,  
                                 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);  
   
991                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
992                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
993                  }                  }
# Line 979  Line 999 
999                  } else {                  } else {
1000                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1001                  }                  }
1002                    if (pEnc->current->quant < 1)
1003                            pEnc->current->quant = 1;
1004    
1005                    if (pEnc->current->quant > 31)
1006                            pEnc->current->quant = 31;
1007    
1008    
1009                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1010                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1011                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1012    
1013    
1014    
1015                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1016                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 989  Line 1021 
1021                  pFrame->intra = 0;                  pFrame->intra = 0;
1022                  pFrame->length = 0;                  pFrame->length = 0;
1023    
                 inc_frame_num(pEnc);  
   
1024                  input_valid = 0;                  input_valid = 0;
1025                  goto bvop_loop;                  goto bvop_loop;
1026          }          }
1027    
1028            pEnc->iFrameNum++;
1029    
1030          BitstreamPad(&bs);          BitstreamPad(&bs);
1031          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1032    
# Line 1030  Line 1062 
1062          return XVID_ERR_OK;          return XVID_ERR_OK;
1063  }  }
1064    
 #endif  
   
1065    
1066    
1067  /*****************************************************************************  /*****************************************************************************
# Line 1069  Line 1099 
1099    
1100          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1101          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
 #ifdef BFRAMES  
1102          pEnc->current->seconds = pEnc->mbParam.m_seconds;          pEnc->current->seconds = pEnc->mbParam.m_seconds;
1103          pEnc->current->ticks = pEnc->mbParam.m_ticks;          pEnc->current->ticks = pEnc->mbParam.m_ticks;
 #endif  
1104          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1105    
1106            /* disable alternate scan flag if interlacing is not enabled */
1107            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1108                    !(pEnc->current->global_flags & XVID_INTERLACING))
1109            {
1110                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1111            }
1112    
1113          start_timer();          start_timer();
1114          if (image_input          if (image_input
1115                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
# Line 1202  Line 1237 
1237          DEBUG(temp);          DEBUG(temp);
1238  #endif  #endif
1239    
 #ifdef BFRAMES  
1240          inc_frame_num(pEnc);          inc_frame_num(pEnc);
 #else  
1241          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1242    
1243          stop_global_timer();          stop_global_timer();
1244          write_timer();          write_timer();
# Line 1283  Line 1314 
1314                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1315                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1316                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1317                          VECTOR pred[4];                          VECTOR pred;
1318                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1319                          int vec;                          int vec;
1320    
1321                          pMB->mode =                          pMB->mode =
# Line 1305  Line 1335 
1335                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1336                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1337    
1338                                  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);  
1339    
1340                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1341                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1342                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1343                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1344                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1345                                  }                                  }
1346                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1347                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1325  Line 1354 
1354                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1355                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1356    
1357                                          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);  
1358    
1359                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1360                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1361                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1362                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1363                                  }                                  }
1364                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1365                          {                          {
# Line 1461  Line 1489 
1489          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1490    
1491          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1492  #ifdef BFRAMES  
1493  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1494          if ((pEnc->global & XVID_GLOBAL_PACKED)) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1495                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1496          }          }
 #endif  
1497          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1498    
1499          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
# Line 1490  Line 1517 
1517                          stop_prediction_timer();                          stop_prediction_timer();
1518    
1519                          start_timer();                          start_timer();
1520                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1521                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1522                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1523                                    qcoeff[5*64+0]=0;
1524                            }
1525                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1526                          stop_coding_timer();                          stop_coding_timer();
1527                  }                  }
# Line 1502  Line 1534 
1534          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1535          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1536    
1537            pEnc->last_pframe = pEnc->current->ticks;
1538            pEnc->last_sync = pEnc->current->ticks;
1539    
1540          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1541                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1542          }          }
# Line 1511  Line 1546 
1546    
1547    
1548  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1549    #define BFRAME_SKIP_THRESHHOLD 30
1550    
1551  static int  static int
1552  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1525  Line 1561 
1561          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1562    
1563          int iLimit;          int iLimit;
1564          uint32_t x, y;          int x, y, k;
1565          int iSearchRange;          int iSearchRange;
1566          int bIntra;          int bIntra;
1567    
# Line 1534  Line 1570 
1570    
1571          start_timer();          start_timer();
1572          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1573                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1574          stop_edges_timer();          stop_edges_timer();
1575    
1576          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 1561  Line 1596 
1596          start_timer();          start_timer();
1597          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1598                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1599                    if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1600                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1601    
1602          } else {          } else {
1603    
1604                  bIntra =                  bIntra =
1605                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1606                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1607                                                           iLimit);                                                           iLimit);
1608    
1609          }          }
1610          stop_motion_timer();          stop_motion_timer();
1611    
1612          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1613    
1614          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1615    
# Line 1582  Line 1620 
1620    
1621          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1622    
1623          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1624          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1625    
1626          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1619  Line 1655 
1655    
1656                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1657    
1658                                    if (pMB->mode != MODE_NOT_CODED)
1659                                  pMB->cbp =                                  pMB->cbp =
1660                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1661                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1643  Line 1680 
1680                          }                          }
1681    
1682                          start_timer();                          start_timer();
1683    
1684                            /* Finished processing the MB, now check if to CODE or SKIP */
1685    
1686                            if ((pMB->mode == MODE_NOT_CODED) ||
1687                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1688                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1689    
1690    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1691    
1692                                    int bSkip = 1;
1693                                    pMB->mode = MODE_NOT_CODED;
1694    
1695                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1696                                    {
1697                                            int iSAD;
1698                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1699                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1700                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1701                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1702                                            {       bSkip = 0;
1703                                                    break;
1704                                            }
1705                                    }
1706                                    if (!bSkip)
1707                                    {
1708                                            VECTOR predMV;
1709                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1710                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1711                                            pMB->mode = MODE_INTER;
1712                                            pMB->cbp = 0;
1713                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1714                                    }
1715                                    else MBSkip(bs);
1716    
1717                            } else {
1718                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1719                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1720                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1721                                            qcoeff[5*64+0]=0;
1722                                    }
1723                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1724                            }
1725    
1726                          stop_coding_timer();                          stop_coding_timer();
1727                  }                  }
1728          }          }
# Line 1677  Line 1756 
1756    
1757          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1758    
1759    #ifdef BFRAMES
1760            /* frame drop code */
1761            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1762            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1763                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1764            {
1765                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1766                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1767    
1768                    BitstreamReset(bs);
1769                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1770    
1771                    // copy reference frame details into the current frame
1772                    pEnc->current->quant = pEnc->reference->quant;
1773                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1774                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1775                    pEnc->current->fcode = pEnc->reference->fcode;
1776                    pEnc->current->bcode = pEnc->reference->bcode;
1777                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1778                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1779    
1780            }
1781    #endif
1782    
1783          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1784    
1785            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1786                            (int32_t)pEnc->mbParam.fbase;
1787            pEnc->last_pframe = pEnc->current->ticks;
1788    
1789          return 0;                                       // inter          return 0;                                       // inter
1790  }  }
1791    
1792    
1793  #ifdef BFRAMES  static __inline void
 static void  
1794  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1795                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1796                     Bitstream * bs,                     Bitstream * bs,
# Line 1693  Line 1799 
1799          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1800          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1801          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1802    
1803          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1804          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1805    
1806    #ifdef BFRAMES_DEC_DEBUG
1807            FILE *fp;
1808            static char first=0;
1809    #define BFRAME_DEBUG    if (!first && fp){ \
1810                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1811            }
1812    
1813            if (!first){
1814                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1815            }
1816    #endif
1817    
1818          // forward          // forward
1819          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1820                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1821                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1822          start_timer();          start_timer();
1823          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,
1824                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1713  Line 1828 
1828          // backward          // backward
1829          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1830                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1831                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1832          start_timer();          start_timer();
1833          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1834                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1722  Line 1836 
1836          stop_inter_timer();          stop_inter_timer();
1837    
1838          start_timer();          start_timer();
1839          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1840                    ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1841                                                    pEnc->time_pp,
1842                                                    pEnc->reference->mbs, f_ref,
1843                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1844                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1845                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1747  Line 1864 
1864    
1865    
1866          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;  
   
1867                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1868                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1869                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                          int direction = pEnc->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];  
1870    
1871                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1872                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1873                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
1874                                  continue;                                  continue;
1875                          }                          }
1876    
1877                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1878                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1879                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1880                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1881                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1882                                                                           dct_codes);                                                                           dct_codes);
1883    
1884                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1885                          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);  
   
1886    
1887                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  mb->cbp =
1888                                  && 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  
                         }  
1889    
1890                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1891                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1892                                  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;  
1893                          }                          }
   
                         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;  
1894                          }                          }
 //                      DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y);  
1895    
1896    #ifdef BFRAMES_DEC_DEBUG
1897            BFRAME_DEBUG
1898    #endif
1899                          start_timer();                          start_timer();
1900                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1901                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1902                          stop_coding_timer();                          stop_coding_timer();
1903                  }                  }
1904          }          }
# Line 1813  Line 1908 
1908          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1909    
1910          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1911    
1912    #ifdef BFRAMES_DEC_DEBUG
1913            if (!first){
1914                    first=1;
1915                    if (fp)
1916                            fclose(fp);
1917  }  }
1918  #endif  #endif
1919    }
1920    
1921    
1922    /*      in case internal output is needed somewhere... */
1923    /*      {
1924            FILE *filehandle;
1925            filehandle=fopen("last-b.pgm","wb");
1926            if (filehandle)
1927            {
1928                    fprintf(filehandle,"P5\n\n");           //
1929                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1930                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1931                    fclose(filehandle);
1932                    }
1933            }
1934    */

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.76.2.3

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