[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.45, Sat Jun 22 07:23:10 2002 UTC revision 1.77, Wed Sep 4 21:16:02 2002 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6     *  Copyright(C) 2002 Michael Militzer
7     *
8   *  This program is an implementation of a part of one or more MPEG-4   *  This program is an implementation of a part of one or more MPEG-4
9   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
10   *  to use this software module in hardware or software products are   *  to use this software module in hardware or software products are
# Line 28  Line 30 
30   *   *
31   ****************************************************************************/   ****************************************************************************/
32    
 /*****************************************************************************  
  *  
  *  History  
  *  
  *      20.06.2002 bframe patch  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
  *  $Id$  
  *  
  ****************************************************************************/  
   
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <stdio.h>  #include <stdio.h>
35  #include <math.h>  #include <math.h>
# Line 51  Line 40 
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    #ifdef BFRAMES
44    #include "image/font.h"
45    #include "motion/sad.h"
46    #endif
47  #include "motion/motion.h"  #include "motion/motion.h"
48  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
49  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 63  Line 56 
56  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
57  #include "utils/mem_align.h"  #include "utils/mem_align.h"
58    
59    #ifdef _SMP
60    #include "motion/smp_motion_est.h"
61    #endif
62  /*****************************************************************************  /*****************************************************************************
63   * Local macros   * Local macros
64   ****************************************************************************/   ****************************************************************************/
# Line 201  Line 197 
197    
198          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
199    
200          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
201                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
202    
   
203          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
204          if (pEnc == NULL)          if (pEnc == NULL)
205                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
# Line 229  Line 224 
224    
225          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
226    
227    #ifdef _SMP
228            pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);
229    #endif
230    
231          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
232    
233          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 330  Line 329 
329          /* B Frames specific init */          /* B Frames specific init */
330  #ifdef BFRAMES  #ifdef BFRAMES
331    
332          pEnc->packed = pParam->packed;          pEnc->global = pParam->global;
333          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
334          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
335            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
336          pEnc->bframes = NULL;          pEnc->bframes = NULL;
337    
338          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 375  Line 375 
375          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
376          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
377          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
378            pEnc->bframenum_dx50bvop = -1;
379    
380          pEnc->queue = NULL;          pEnc->queue = NULL;
381    
# Line 408  Line 409 
409    
410          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
411          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
412            pEnc->m_framenum = 0;
413            pEnc->last_pframe = 0;
414            pEnc->last_sync = 0;
415  #endif  #endif
416    
417          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
# Line 603  Line 607 
607  }  }
608    
609    
 #ifdef BFRAMES  
610  void inc_frame_num(Encoder * pEnc)  void inc_frame_num(Encoder * pEnc)
611  {  {
         pEnc->iFrameNum++;  
612          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
613          if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {  
614                  pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;  #ifdef BFRAMES
615                  pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;                  pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
616          }          if (pEnc->mbParam.m_ticks < pEnc->last_sync)
617  }                  pEnc->mbParam.m_seconds = 1;            // more than 1 second since last I or P is not supported.
618            else
619                    pEnc->mbParam.m_seconds = 0;
620    
621            if (pEnc->current->coding_type != B_VOP)
622                    pEnc->last_sync = pEnc->mbParam.m_ticks;
623    #else
624    
625            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
626            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
627    
628  #endif  #endif
629    
630    }
631    
632    
633  #ifdef BFRAMES  #ifdef BFRAMES
634  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
635  {  {
636          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
637          {          {
638                  DPRINTF("FATAL: QUEUE FULL");                  DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
639                  return;                  return;
640          }          }
641    
642          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",
643                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
644                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
645    
# Line 693  Line 707 
707                           * frame as a pframe                           * frame as a pframe
708                           */                           */
709    
710                          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",
711                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
712                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
713    
# Line 712  Line 726 
726                  }                  }
727    
728    
729                  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",
730                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
731                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
732    
# Line 732  Line 746 
746          if (pEnc->bframenum_head > 0) {          if (pEnc->bframenum_head > 0) {
747                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
748    
749                  if (pEnc->packed) {                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
750    
751                          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",
752                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
753                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
754    
   
755                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
756                          BitstreamPad(&bs);                          BitstreamPad(&bs);
757                          BitstreamPutBits(&bs, 0x7f, 8);                          BitstreamPutBits(&bs, 0x7f, 8);
# Line 756  Line 769 
769    
770  bvop_loop:  bvop_loop:
771    
772          if (input_valid) {          if (pEnc->bframenum_dx50bvop != -1)
773            {
774    
775                    SWAP(pEnc->current, pEnc->reference);
776                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
777    
778                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
779                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
780                    }
781    
782                    if (input_valid)
783                    {
784                            queue_image(pEnc, pFrame);
785                            input_valid = 0;
786                    }
787    
788            } else if (input_valid) {
789    
790                  SWAP(pEnc->current, pEnc->reference);                  SWAP(pEnc->current, pEnc->reference);
791    
# Line 789  Line 818 
818    
819          } else if (BitstreamPos(&bs) == 0) {          } else if (BitstreamPos(&bs) == 0) {
820    
821                  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",
822                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
823                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
824    
   
825                  pFrame->intra = 0;                  pFrame->intra = 0;
826    
827                  BitstreamPutBits(&bs, 0x7f, 8);                  BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
828                  BitstreamPad(&bs);                  BitstreamPad(&bs);
829                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
830    
# Line 815  Line 843 
843           * comment style :-)           * comment style :-)
844           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
845    
 //$$    SWAP(pEnc->current, pEnc->reference);  
   
846          emms();          emms();
847    
848            // only inc frame num, adapt quant, etc. if we havent seen it before
849            if (pEnc->bframenum_dx50bvop < 0 )
850            {
851          if (pFrame->quant == 0)          if (pFrame->quant == 0)
852                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
853          else          else
854                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
855    
856          if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
857                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
858    
859          if (pEnc->current->quant > 31)          if (pEnc->current->quant > 31)
860                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
861    */
862          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
863          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
864          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
865          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
866          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
867          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
868    
869  //$$$   start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
870  //$$$   if (image_input                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
871  //$$$           (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
872  //$$$            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                  inc_frame_num(pEnc);
 //$$$           return XVID_ERR_FORMAT;  
 //$$$   stop_conv_timer();  
873    
874  #ifdef _DEBUG_PSNR  #ifdef _DEBUG_PSNR
875          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
# Line 852  Line 878 
878    
879          emms();          emms();
880    
881                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
882                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
883                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
884                    }
885    
886          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887           * Luminance masking           * Luminance masking
888           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 881  Line 912 
912                          }                          }
913    
914  #undef OFFSET  #undef OFFSET
   
915                  }                  }
916    
917                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
918          }          }
919    
920            }
921    
922          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
924           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
925    
926    
927          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
928                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
929                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
930                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
# Line 902  Line 934 
934                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
935                   */                   */
936    
937                  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",
938                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
939                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
940    
941                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
942                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
943                    }
944    
945                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
946    
947                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
948    
949                            pEnc->bframenum_tail--;
950                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
951    
952                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
953                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
954                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
955                            }
956                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
957    
958                            pFrame->intra = 0;
959    
960                    } else {
961    
962                            FrameCodeI(pEnc, &bs, &bits);
963                  pFrame->intra = 1;                  pFrame->intra = 1;
                 pEnc->flush_bframes = 1;  
964    
965                  inc_frame_num(pEnc);                          pEnc->bframenum_dx50bvop = -1;
966                    }
967    
968                    pEnc->flush_bframes = 1;
969    
970                  if (pEnc->packed) {                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
971                          BitstreamPad(&bs);                          BitstreamPad(&bs);
972                          input_valid = 0;                          input_valid = 0;
973                          goto ipvop_loop;                          goto ipvop_loop;
# Line 928  Line 982 
982                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
983                   */                   */
984    
985                  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",
986                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
987                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
988    
989                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
990                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
991                    }
992    
993                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
994                  pFrame->intra = 0;                  pFrame->intra = 0;
995                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
996    
997                  inc_frame_num(pEnc);                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
   
                 if (pEnc->packed) {  
998                          BitstreamPad(&bs);                          BitstreamPad(&bs);
999                          input_valid = 0;                          input_valid = 0;
1000                          goto ipvop_loop;                          goto ipvop_loop;
# Line 949  Line 1005 
1005                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1006                   */                   */
1007    
1008                  DPRINTF("*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1009                                  pEnc->bframenum_head, pEnc->bframenum_tail,                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1010                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                  }
1011    
1012                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1013                          pEnc->current->quant =                          pEnc->current->quant =
# Line 960  Line 1016 
1016                  } else {                  } else {
1017                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1018                  }                  }
1019                    if (pEnc->current->quant < 1)
1020                            pEnc->current->quant = 1;
1021    
1022                    if (pEnc->current->quant > 31)
1023                            pEnc->current->quant = 31;
1024    
1025    
1026                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1027                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1028                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1029    
1030    
1031    
1032                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1033                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 970  Line 1038 
1038                  pFrame->intra = 0;                  pFrame->intra = 0;
1039                  pFrame->length = 0;                  pFrame->length = 0;
1040    
                 inc_frame_num(pEnc);  
   
1041                  input_valid = 0;                  input_valid = 0;
1042                  goto bvop_loop;                  goto bvop_loop;
1043          }          }
1044    
1045            pEnc->iFrameNum++;
1046    
1047          BitstreamPad(&bs);          BitstreamPad(&bs);
1048          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1049    
# Line 1050  Line 1118 
1118    
1119          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1120          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
 #ifdef BFRAMES  
1121          pEnc->current->seconds = pEnc->mbParam.m_seconds;          pEnc->current->seconds = pEnc->mbParam.m_seconds;
1122          pEnc->current->ticks = pEnc->mbParam.m_ticks;          pEnc->current->ticks = pEnc->mbParam.m_ticks;
 #endif  
1123          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1124    
1125          start_timer();          start_timer();
# Line 1183  Line 1249 
1249          DEBUG(temp);          DEBUG(temp);
1250  #endif  #endif
1251    
 #ifdef BFRAMES  
1252          inc_frame_num(pEnc);          inc_frame_num(pEnc);
 #else  
1253          pEnc->iFrameNum++;          pEnc->iFrameNum++;
 #endif  
   
1254    
1255          stop_global_timer();          stop_global_timer();
1256          write_timer();          write_timer();
# Line 1264  Line 1326 
1326                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1327                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1328                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1329                          VECTOR pred[4];                          VECTOR pred;
1330                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1331                          int vec;                          int vec;
1332    
1333                          pMB->mode =                          pMB->mode =
# Line 1286  Line 1347 
1347                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1348                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1349    
1350                                  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);  
1351    
1352                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1353                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1354                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1355                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1356                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1357                                  }                                  }
1358                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1359                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1306  Line 1366 
1366                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1367                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1368    
1369                                          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);  
1370    
1371                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1372                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1373                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1374                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1375                                  }                                  }
1376                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1377                          {                          {
# Line 1444  Line 1503 
1503          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1504  #ifdef BFRAMES  #ifdef BFRAMES
1505  #define DIVX501B481P "DivX501b481p"  #define DIVX501B481P "DivX501b481p"
1506          if (pEnc->packed) {          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1507                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));                  BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1508          }          }
1509  #endif  #endif
# Line 1471  Line 1530 
1530                          stop_prediction_timer();                          stop_prediction_timer();
1531    
1532                          start_timer();                          start_timer();
1533                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1534                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1535                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1536                                    qcoeff[5*64+0]=0;
1537                            }
1538                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1539                          stop_coding_timer();                          stop_coding_timer();
1540                  }                  }
# Line 1492  Line 1556 
1556    
1557    
1558  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1559    #define BFRAME_SKIP_THRESHHOLD 16
1560    
1561  static int  static int
1562  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1506  Line 1571 
1571          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1572    
1573          int iLimit;          int iLimit;
1574          uint32_t x, y;          int x, y, k;
1575          int iSearchRange;          int iSearchRange;
1576          int bIntra;          int bIntra;
1577    
# Line 1543  Line 1608 
1608          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1609                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1610          } else {          } else {
1611    
1612    #ifdef _SMP
1613            if (pEnc->mbParam.num_threads > 1)
1614                    bIntra =
1615                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1616                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1617                                                     iLimit);
1618            else
1619    #endif
1620                  bIntra =                  bIntra =
1621                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1622                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1623                                                           iLimit);                                                           iLimit);
1624    
1625          }          }
1626          stop_motion_timer();          stop_motion_timer();
1627    
# Line 1624  Line 1699 
1699                          }                          }
1700    
1701                          start_timer();                          start_timer();
1702    
1703                            /* Finished processing the MB, now check if to CODE or SKIP */
1704    
1705                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1706                                    pMB->mvs[0].y == 0) {
1707    
1708    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1709    
1710    #ifdef BFRAMES
1711                                    int iSAD=BFRAME_SKIP_THRESHHOLD;
1712                                    int bSkip=1;
1713    
1714                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1715                                    {
1716                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1717                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1718                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1719                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD)
1720                                            {       bSkip = 0;
1721                                                    break;
1722                                            }
1723                                    }
1724                                    if (!bSkip)
1725                                    {
1726                                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1727                                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1728                                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1729                                                    qcoeff[5*64+0]=0;
1730                                            }
1731                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1732                                            pMB->cbp = 0x80;                /* trick! so cbp!=0, but still nothing is written to bs */
1733                                    }
1734                                    else
1735                                            MBSkip(bs);
1736    
1737    #else
1738                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1739    
1740    #endif
1741    
1742                            } else {
1743                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1744                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1745                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1746                                            qcoeff[5*64+0]=0;
1747                                    }
1748                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1749                            }
1750    
1751                          stop_coding_timer();                          stop_coding_timer();
1752                  }                  }
1753          }          }
# Line 1658  Line 1781 
1781    
1782          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1783    
1784    #ifdef BFRAMES
1785            /* frame drop code */
1786            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1787            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1788                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1789            {
1790                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1791                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1792    
1793                    BitstreamReset(bs);
1794                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1795    
1796                    // copy reference frame details into the current frame
1797                    pEnc->current->quant = pEnc->reference->quant;
1798                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1799                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1800                    pEnc->current->fcode = pEnc->reference->fcode;
1801                    pEnc->current->bcode = pEnc->reference->bcode;
1802                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1803                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1804    
1805            }
1806    #endif
1807    
1808          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1809    
1810    #ifdef BFRAMES
1811            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1812                            (int32_t)pEnc->mbParam.fbase;
1813            pEnc->last_pframe = pEnc->current->ticks;
1814    #endif
1815    
1816          return 0;                                       // inter          return 0;                                       // inter
1817  }  }
1818    
# Line 1680  Line 1833 
1833          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1834          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1835    
1836    #ifdef BFRAMES_DEC_DEBUG
1837            FILE *fp;
1838            static char first=0;
1839    #define BFRAME_DEBUG    if (!first && fp){ \
1840                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1841            }
1842    
1843            if (!first){
1844                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1845            }
1846    #endif
1847    
1848          // forward          // forward
1849          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1850                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
# Line 1703  Line 1868 
1868          stop_inter_timer();          stop_inter_timer();
1869    
1870          start_timer();          start_timer();
1871          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1872                    ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1873                                                    pEnc->time_pp,
1874                                                    pEnc->reference->mbs, f_ref,
1875                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1876                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1877                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1746  Line 1914 
1914                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1915                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1916                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1917    
1918                                    mb->cbp = 0;
1919    #ifdef BFRAMES_DEC_DEBUG
1920            BFRAME_DEBUG
1921    #endif
1922                                  continue;                                  continue;
1923                          }                          }
1924    
# Line 1757  Line 1930 
1930    
1931                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1932                          mb->cbp =                          mb->cbp =
1933                                  MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,                                  MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                                                   qcoeff);  
1934                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1935    
1936                            if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1937                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  && (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) {
1938                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 mb->mode = 5;   // skipped  
1939                          }                          }
1940    
1941    /* update predictors for forward and backward vectors */
1942                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1943                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1944                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;
# Line 1780  Line 1952 
1952                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1953                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1954                          }                          }
 //          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);  
1955    
1956    //                      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);
1957    
1958    #ifdef BFRAMES_DEC_DEBUG
1959            BFRAME_DEBUG
1960    #endif
1961                          start_timer();                          start_timer();
1962                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1963                                                   &pEnc->sStat);                                                   &pEnc->sStat);
# Line 1794  Line 1970 
1970          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1971    
1972          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1973    
1974    #ifdef BFRAMES_DEC_DEBUG
1975            if (!first){
1976                    first=1;
1977                    if (fp)
1978                            fclose(fp);
1979  }  }
1980  #endif  #endif
1981    }
1982    #endif
1983    
1984    
1985    /*      in case internal output is needed somewhere... */
1986    /*      {
1987            FILE *filehandle;
1988            filehandle=fopen("last-b.pgm","wb");
1989            if (filehandle)
1990            {
1991                    fprintf(filehandle,"P5\n\n");           //
1992                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1993                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1994                    fclose(filehandle);
1995                    }
1996            }
1997    */

Legend:
Removed from v.1.45  
changed lines
  Added in v.1.77

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