[svn] / tags / branch-release-1-0 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /tags/branch-release-1-0/xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 208, Fri Jun 14 13:21:35 2002 UTC revision 295, Fri Jul 12 12:26:55 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
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>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
41   *   *
42   *  $Id: encoder.c,v 1.43 2002-06-14 13:21:35 Isibaar Exp $   *  $Id: encoder.c,v 1.55 2002-07-12 12:26:23 suxen_drol Exp $
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
# Line 50  Line 53 
53  #include "global.h"  #include "global.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "image/image.h"  #include "image/image.h"
56    #ifdef BFRAMES
57    #include "image/font.h"
58    #endif
59  #include "motion/motion.h"  #include "motion/motion.h"
60  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
61  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 62  Line 68 
68  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
69  #include "utils/mem_align.h"  #include "utils/mem_align.h"
70    
71    #ifdef _SMP
72    #include "motion/smp_motion_est.h"
73    #endif
74  /*****************************************************************************  /*****************************************************************************
75   * Local macros   * Local macros
76   ****************************************************************************/   ****************************************************************************/
# Line 132  Line 141 
141  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
142  {  {
143          Encoder *pEnc;          Encoder *pEnc;
144          uint32_t i;          int i;
145    
146          pParam->handle = NULL;          pParam->handle = NULL;
147    
# Line 203  Line 212 
212          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval == 0)
213                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
214    
   
215          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
216          if (pEnc == NULL)          if (pEnc == NULL)
217                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
# Line 228  Line 236 
236    
237          pEnc->mbParam.m_quant_type = H263_QUANT;          pEnc->mbParam.m_quant_type = H263_QUANT;
238    
239    #ifdef _SMP
240            pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);
241    #endif
242    
243          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
244    
245          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 259  Line 271 
271    
272          /* try to allocate image memory */          /* try to allocate image memory */
273    
274  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
275          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
276  #endif  #endif
277  #ifdef BFRAMES  #ifdef BFRAMES
# Line 275  Line 287 
287          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
288          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
289    
290  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
291          if (image_create          if (image_create
292                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
293                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 329  Line 341 
341          /* B Frames specific init */          /* B Frames specific init */
342  #ifdef BFRAMES  #ifdef BFRAMES
343    
344            pEnc->global = pParam->global;
345          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
346          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
347          pEnc->bframes = NULL;          pEnc->bframes = NULL;
# Line 373  Line 386 
386          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
387          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
388          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
389            pEnc->bframenum_dx50bvop = -1;
390    
391            pEnc->queue = NULL;
392    
393    
394            if (pEnc->mbParam.max_bframes > 0) {
395                    int n;
396    
397                    pEnc->queue =
398                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
399                                                    CACHE_LINE);
400    
401                    if (pEnc->queue == NULL)
402                            goto xvid_err_memory4;
403    
404                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
405                            image_null(&pEnc->queue[n]);
406    
407                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
408                            if (image_create
409                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
410                                     pEnc->mbParam.edged_height) < 0)
411                                    goto xvid_err_memory5;
412    
413                    }
414            }
415    
416            pEnc->queue_head = 0;
417            pEnc->queue_tail = 0;
418            pEnc->queue_size = 0;
419    
420    
421          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
422          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
423            pEnc->m_framenum = 0;
424  #endif  #endif
425    
426          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
# Line 396  Line 441 
441           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
442           */           */
443  #ifdef BFRAMES  #ifdef BFRAMES
444      xvid_err_memory5:
445    
446    
447            if (pEnc->mbParam.max_bframes > 0) {
448    
449                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
450                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
451                                                      pEnc->mbParam.edged_height);
452                    }
453                    xvid_free(pEnc->queue);
454            }
455    
456    xvid_err_memory4:    xvid_err_memory4:
457    
458            if (pEnc->mbParam.max_bframes > 0) {
459    
460          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
461    
462                  if (pEnc->bframes[i] == NULL)                  if (pEnc->bframes[i] == NULL)
# Line 412  Line 472 
472          }          }
473    
474          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
475            }
476    
477  #endif  #endif
478    
479    xvid_err_memory3:    xvid_err_memory3:
480  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
481          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
482                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
483  #endif  #endif
# Line 473  Line 534 
534  int  int
535  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
536  {  {
537    #ifdef BFRAMES
538            int i;
539    #endif
540    
541          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
542    
543          /* B Frames specific */          /* B Frames specific */
544  #ifdef BFRAMES  #ifdef BFRAMES
545          int i;          if (pEnc->mbParam.max_bframes > 0) {
546    
547                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
548    
549                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
550                                              pEnc->mbParam.edged_height);
551                    }
552                    xvid_free(pEnc->queue);
553            }
554    
555    
556            if (pEnc->mbParam.max_bframes > 0) {
557    
558          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
559    
# Line 490  Line 566 
566                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
567    
568                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
569          }          }
570    
571          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
572    
573            }
574  #endif  #endif
575    
576          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
# Line 521  Line 597 
597          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
598                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
599  #endif  #endif
600  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
601          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
602                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
603  #endif  #endif
# Line 539  Line 615 
615          return XVID_ERR_OK;          return XVID_ERR_OK;
616  }  }
617    
618    
619    #ifdef BFRAMES
620    void inc_frame_num(Encoder * pEnc)
621    {
622            pEnc->iFrameNum++;
623            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
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
629    
630    
631    #ifdef BFRAMES
632    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
633    {
634            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
635            {
636                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
637                    return;
638            }
639    
640            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
641                                    pEnc->bframenum_head, pEnc->bframenum_tail,
642                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
643    
644    
645            start_timer();
646            if (image_input
647                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
648                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
649                    return;
650            stop_conv_timer();
651    
652            pEnc->queue_size++;
653            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
654    }
655    #endif
656    
657    
658    #ifdef BFRAMES
659  /*****************************************************************************  /*****************************************************************************
660   * Frame encoder entry point   * IPB frame encoder entry point
  *  
  * At this moment 2 versions coexist : one for IPB compatible encoder,  
  *                                     another one for the old IP encoder.  
661   *   *
662   * Returned values :   * Returned values :
663   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 551  Line 665 
665   *                        format   *                        format
666   ****************************************************************************/   ****************************************************************************/
667    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
668  int  int
669  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
670                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
671                             XVID_ENC_STATS * pResult)                             XVID_ENC_STATS * pResult)
672  {  {
# Line 565  Line 674 
674          Bitstream bs;          Bitstream bs;
675          uint32_t bits;          uint32_t bits;
676    
677  #ifdef _DEBUG          int input_valid = 1;
678    
679    #ifdef _DEBUG_PSNR
680          float psnr;          float psnr;
681          char temp[128];          char temp[128];
682  #endif  #endif
683    
684          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
685          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
686            ENC_CHECK(pFrame->image);
687    
688          start_global_timer();          start_global_timer();
689    
690          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
691    
692    ipvop_loop:
693    
694          /*          /*
695           * bframe "flush" code           * bframe "flush" code
696           */           */
# Line 591  Line 705 
705                           * frame as a pframe                           * frame as a pframe
706                           */                           */
707    
708                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
709                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
710                             dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
711                           */  
712                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
713                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
714    
# Line 604  Line 718 
718    
719                          BitstreamPad(&bs);                          BitstreamPad(&bs);
720                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
721                          pFrame->intra = 0;                          pFrame->intra = 0;
722    
723                          return XVID_ERR_OK;                          return XVID_ERR_OK;
724                  }                  }
725    
726                  /* ToDo : remove dprintf calls */  
727                  /*                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
728                     dprintf("--- BFRAME (flush) --- ");                                  pEnc->bframenum_head, pEnc->bframenum_tail,
729                   */                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
730    
731                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
732                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
733    
   
734                  BitstreamPad(&bs);                  BitstreamPad(&bs);
735                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
736                  pFrame->intra = 0;                  pFrame->intra = 0;
737    
738                    if (input_valid)
739                            queue_image(pEnc, pFrame);
740    
741                  return XVID_ERR_OK;                  return XVID_ERR_OK;
742          }          }
743    
744          if (pFrame->image == NULL) {          if (pEnc->bframenum_head > 0) {
745                  pFrame->length = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
746                  pFrame->input_consumed = 1;  
747                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
748    
749                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
750                                    pEnc->bframenum_head, pEnc->bframenum_tail,
751                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
752    
753                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
754                            BitstreamPad(&bs);
755                            BitstreamPutBits(&bs, 0x7f, 8);
756    
757                            pFrame->length = BitstreamLength(&bs);
758                  pFrame->intra = 0;                  pFrame->intra = 0;
759    
760                            if (input_valid)
761                                    queue_image(pEnc, pFrame);
762    
763                  return XVID_ERR_OK;                  return XVID_ERR_OK;
764          }          }
765            }
766    
767          if (pEnc->bframenum_head > 0) {  
768                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  bvop_loop:
769    
770            if (pEnc->bframenum_dx50bvop != -1)
771            {
772    
773                    SWAP(pEnc->current, pEnc->reference);
774                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
775    
776                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
777                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
778                    }
779    
780                    if (input_valid)
781                    {
782                            queue_image(pEnc, pFrame);
783                            input_valid = 0;
784                    }
785    
786            } else if (input_valid) {
787    
788                    SWAP(pEnc->current, pEnc->reference);
789    
790                    start_timer();
791                    if (image_input
792                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
793                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
794                            return XVID_ERR_FORMAT;
795                    stop_conv_timer();
796    
797                    // queue input frame, and dequue next image
798                    if (pEnc->queue_size > 0)
799                    {
800                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
801                            if (pEnc->queue_head != pEnc->queue_tail)
802                            {
803                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
804                            }
805                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
806                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
807                    }
808    
809            } else if (pEnc->queue_size > 0) {
810    
811                    SWAP(pEnc->current, pEnc->reference);
812    
813                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
814                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
815                    pEnc->queue_size--;
816    
817            } else if (BitstreamPos(&bs) == 0) {
818    
819                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
820                                    pEnc->bframenum_head, pEnc->bframenum_tail,
821                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
822    
823                    pFrame->intra = 0;
824    
825                    BitstreamPutBits(&bs, 0x7f, 8);
826                    BitstreamPad(&bs);
827                    pFrame->length = BitstreamLength(&bs);
828    
829                    return XVID_ERR_OK;
830    
831            } else {
832    
833                    pFrame->length = BitstreamLength(&bs);
834                    return XVID_ERR_OK;
835          }          }
836    
837          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
# Line 645  Line 841 
841           * comment style :-)           * comment style :-)
842           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
843    
         SWAP(pEnc->current, pEnc->reference);  
   
844          emms();          emms();
845    
846            // only inc frame num, adapt quant, etc. if we havent seen it before
847            if (pEnc->bframenum_dx50bvop < 0 )
848            {
849          if (pFrame->quant == 0)          if (pFrame->quant == 0)
850                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
851          else          else
# Line 662  Line 859 
859    
860          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
861          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
862          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
863          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
864          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
865          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
866    
867          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
868          if (image_input                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
869                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
870                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                  inc_frame_num(pEnc);
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
871    
872  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
873          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
874                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
875  #endif  #endif
876    
877          emms();          emms();
878    
879                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
880                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
881                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
882                    }
883    
884          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
885           * Luminance masking           * Luminance masking
886           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 711  Line 910 
910                          }                          }
911    
912  #undef OFFSET  #undef OFFSET
   
913                  }                  }
914    
915                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
916          }          }
917    
918            }
919    
920          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
922           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
923    
924    
925          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
926                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
927                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
928                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
# Line 732  Line 932 
932                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
933                   */                   */
934    
935                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
936                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
937                     dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                  */  
938    
939                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
940                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
941                    }
942    
943                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
944    
945                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
946    
947                            pEnc->bframenum_tail--;
948                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
949    
950                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
951                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
952                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
953                            }
954                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
955    
956                            pFrame->intra = 0;
957    
958                    } else {
959    
960                            FrameCodeI(pEnc, &bs, &bits);
961                  pFrame->intra = 1;                  pFrame->intra = 1;
962    
963                            pEnc->bframenum_dx50bvop = -1;
964                    }
965    
966                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
967    
968                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
969                            BitstreamPad(&bs);
970                            input_valid = 0;
971                            goto ipvop_loop;
972                    }
973    
974                  /*                  /*
975                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
976                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
# Line 751  Line 980 
980                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
981                   */                   */
982    
983                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
984                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
985                     dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
986                   */  
987                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
988                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
989                    }
990    
991                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
992                  pFrame->intra = 0;                  pFrame->intra = 0;
993                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
994    
995                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
996                            BitstreamPad(&bs);
997                            input_valid = 0;
998                            goto ipvop_loop;
999                    }
1000    
1001          } else {          } else {
1002                  /*                  /*
1003                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1004                   */                   */
1005    
1006                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1007                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1008                     dprintf("--- BFRAME (store) ---  head=%i tail=%i",                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1009                     pEnc->bframenum_head,  
1010                     pEnc->bframenum_tail);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1011                   */                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1012                    }
1013    
1014                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1015                          pEnc->current->quant =                          pEnc->current->quant =
# Line 787  Line 1027 
1027    
1028                  pFrame->intra = 0;                  pFrame->intra = 0;
1029                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1030    
1031                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1032                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {                  goto bvop_loop;
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
   
                 return XVID_ERR_OK;  
1033          }          }
1034    
1035          BitstreamPad(&bs);          BitstreamPad(&bs);
# Line 811  Line 1045 
1045    
1046          emms();          emms();
1047    
1048  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1049          psnr =          psnr =
1050                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1051                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 826  Line 1060 
1060                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1061          }          }
1062    
         pEnc->iFrameNum++;  
         pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;  
         if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {  
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
         }  
         pFrame->input_consumed = 1;  
1063    
1064          stop_global_timer();          stop_global_timer();
1065          write_timer();          write_timer();
1066    
1067          return XVID_ERR_OK;          return XVID_ERR_OK;
1068  }  }
1069  #else  
1070    #endif
1071    
1072    
1073    
1074  /*****************************************************************************  /*****************************************************************************
1075   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1076     *
1077     * Returned values :
1078     *    - XVID_ERR_OK     - no errors
1079     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1080     *                        format
1081   ****************************************************************************/   ****************************************************************************/
1082    
1083  int  int
1084  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1085                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
# Line 853  Line 1090 
1090          uint32_t bits;          uint32_t bits;
1091          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1092    
1093  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1094          float psnr;          float psnr;
1095          uint8_t temp[128];          uint8_t temp[128];
1096  #endif  #endif
# Line 869  Line 1106 
1106    
1107          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1108          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1109    #ifdef BFRAMES
1110            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1111            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1112    #endif
1113          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1114    
1115          start_timer();          start_timer();
# Line 878  Line 1119 
1119                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1120          stop_conv_timer();          stop_conv_timer();
1121    
1122  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1123          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1124                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1125  #endif  #endif
# Line 988  Line 1229 
1229                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1230                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1231          }          }
1232  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1233          psnr =          psnr =
1234                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1235                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 998  Line 1239 
1239          DEBUG(temp);          DEBUG(temp);
1240  #endif  #endif
1241    
1242    #ifdef BFRAMES
1243            inc_frame_num(pEnc);
1244    #else
1245          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1246    #endif
1247    
1248    
1249          stop_global_timer();          stop_global_timer();
1250          write_timer();          write_timer();
1251    
1252          return XVID_ERR_OK;          return XVID_ERR_OK;
1253  }  }
 #endif  
1254    
1255    
1256  static __inline void  static __inline void
# Line 1075  Line 1320 
1320                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1321                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1322                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1323                          VECTOR pred[4];                          VECTOR pred;
1324                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1325                          int vec;                          int vec;
1326    
1327                          pMB->mode =                          pMB->mode =
# Line 1097  Line 1341 
1341                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1342                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1343    
1344                                  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);  
1345    
1346                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1347                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1348                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1349                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1350                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1351                                  }                                  }
1352                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1353                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1117  Line 1360 
1360                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1361                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1362    
1363                                          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);  
1364    
1365                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1366                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1367                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1368                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1369                                  }                                  }
1370                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1371                          {                          {
# Line 1253  Line 1495 
1495          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1496    
1497          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1498          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  #ifdef BFRAMES
1499    #define DIVX501B481P "DivX501b481p"
1500            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1501                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1502            }
1503    #endif
1504            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1505    
1506          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1507    
# Line 1348  Line 1596 
1596          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1597                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1598          } else {          } else {
1599    
1600    #ifdef _SMP
1601            if (pEnc->mbParam.num_threads > 1)
1602                    bIntra =
1603                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1604                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1605                                                     iLimit);
1606            else
1607    #endif
1608                  bIntra =                  bIntra =
1609                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1610                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1611                                                           iLimit);                                                           iLimit);
1612    
1613          }          }
1614          stop_motion_timer();          stop_motion_timer();
1615    
# Line 1364  Line 1622 
1622          if (vol_header)          if (vol_header)
1623                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1624    
1625          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1626    
1627          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1628    
# Line 1485  Line 1743 
1743          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1744          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1745    
1746    #ifdef BFRAMES_DEC_DEBUG
1747            FILE *fp;
1748            static char first=0;
1749    #define BFRAME_DEBUG    if (!first && fp){ \
1750                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1751            }
1752    
1753            if (!first){
1754                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1755            }
1756    #endif
1757    
1758          // forward          // forward
1759          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1760                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
# Line 1522  Line 1792 
1792             } */             } */
1793    
1794          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1795          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1796    
1797          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1798    
# Line 1551  Line 1821 
1821                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1822                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1823                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1824    
1825                                    mb->cbp = 0;
1826    #ifdef BFRAMES_DEC_DEBUG
1827            BFRAME_DEBUG
1828    #endif
1829                                  continue;                                  continue;
1830                          }                          }
1831    
# Line 1585  Line 1860 
1860                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1861                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1862                          }                          }
1863  //          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);  //                      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);
1864    
1865    #ifdef BFRAMES_DEC_DEBUG
1866            BFRAME_DEBUG
1867    #endif
1868                          start_timer();                          start_timer();
1869                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1870                                                   &pEnc->sStat);                                                   &pEnc->sStat);
# Line 1599  Line 1877 
1877          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1878    
1879          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1880    
1881    #ifdef BFRAMES_DEC_DEBUG
1882            if (!first){
1883                    first=1;
1884                    if (fp)
1885                            fclose(fp);
1886            }
1887    #endif
1888  }  }
1889  #endif  #endif

Legend:
Removed from v.208  
changed lines
  Added in v.295

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