[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.43, Fri Jun 14 13:21:35 2002 UTC revision 1.54, Thu Jul 11 00:15:59 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()
# 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 259  Line 268 
268    
269          /* try to allocate image memory */          /* try to allocate image memory */
270    
271  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
272          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
273  #endif  #endif
274  #ifdef BFRAMES  #ifdef BFRAMES
# Line 275  Line 284 
284          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
285          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
286    
287  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
288          if (image_create          if (image_create
289                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
290                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 329  Line 338 
338          /* B Frames specific init */          /* B Frames specific init */
339  #ifdef BFRAMES  #ifdef BFRAMES
340    
341            pEnc->global = pParam->global;
342          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
343          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
344          pEnc->bframes = NULL;          pEnc->bframes = NULL;
# Line 373  Line 383 
383          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
384          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
385          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
386            pEnc->bframenum_dx50bvop = -1;
387    
388            pEnc->queue = NULL;
389    
390    
391            if (pEnc->mbParam.max_bframes > 0) {
392                    int n;
393    
394                    pEnc->queue =
395                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
396                                                    CACHE_LINE);
397    
398                    if (pEnc->queue == NULL)
399                            goto xvid_err_memory4;
400    
401                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
402                            image_null(&pEnc->queue[n]);
403    
404                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
405                            if (image_create
406                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
407                                     pEnc->mbParam.edged_height) < 0)
408                                    goto xvid_err_memory5;
409    
410                    }
411            }
412    
413            pEnc->queue_head = 0;
414            pEnc->queue_tail = 0;
415            pEnc->queue_size = 0;
416    
417    
418          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
419          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
420            pEnc->m_framenum = 0;
421  #endif  #endif
422    
423          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
# Line 396  Line 438 
438           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
439           */           */
440  #ifdef BFRAMES  #ifdef BFRAMES
441      xvid_err_memory5:
442    
443    
444            if (pEnc->mbParam.max_bframes > 0) {
445    
446                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
447                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
448                                                      pEnc->mbParam.edged_height);
449                    }
450                    xvid_free(pEnc->queue);
451            }
452    
453    xvid_err_memory4:    xvid_err_memory4:
454    
455            if (pEnc->mbParam.max_bframes > 0) {
456    
457          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
458    
459                  if (pEnc->bframes[i] == NULL)                  if (pEnc->bframes[i] == NULL)
# Line 412  Line 469 
469          }          }
470    
471          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
472            }
473    
474  #endif  #endif
475    
476    xvid_err_memory3:    xvid_err_memory3:
477  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
478          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
479                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
480  #endif  #endif
# Line 473  Line 531 
531  int  int
532  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
533  {  {
534    #ifdef BFRAMES
535            int i;
536    #endif
537    
538          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
539    
540          /* B Frames specific */          /* B Frames specific */
541  #ifdef BFRAMES  #ifdef BFRAMES
542          int i;          if (pEnc->mbParam.max_bframes > 0) {
543    
544                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
545    
546                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
547                                              pEnc->mbParam.edged_height);
548                    }
549                    xvid_free(pEnc->queue);
550            }
551    
552    
553            if (pEnc->mbParam.max_bframes > 0) {
554    
555          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
556    
# Line 490  Line 563 
563                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
564    
565                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
566          }          }
567    
568          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
569    
570            }
571  #endif  #endif
572    
573          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
# Line 521  Line 594 
594          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
595                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
596  #endif  #endif
597  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
598          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
599                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
600  #endif  #endif
# Line 539  Line 612 
612          return XVID_ERR_OK;          return XVID_ERR_OK;
613  }  }
614    
615    
616    #ifdef BFRAMES
617    void inc_frame_num(Encoder * pEnc)
618    {
619            pEnc->iFrameNum++;
620            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
621    
622            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
623            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
624    }
625    #endif
626    
627    
628    #ifdef BFRAMES
629    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
630    {
631            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
632            {
633                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
634                    return;
635            }
636    
637            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
638                                    pEnc->bframenum_head, pEnc->bframenum_tail,
639                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
640    
641    
642            start_timer();
643            if (image_input
644                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
645                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
646                    return;
647            stop_conv_timer();
648    
649            pEnc->queue_size++;
650            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
651    }
652    #endif
653    
654    
655    #ifdef BFRAMES
656  /*****************************************************************************  /*****************************************************************************
657   * 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.  
658   *   *
659   * Returned values :   * Returned values :
660   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 551  Line 662 
662   *                        format   *                        format
663   ****************************************************************************/   ****************************************************************************/
664    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
665  int  int
666  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
667                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
668                             XVID_ENC_STATS * pResult)                             XVID_ENC_STATS * pResult)
669  {  {
# Line 565  Line 671 
671          Bitstream bs;          Bitstream bs;
672          uint32_t bits;          uint32_t bits;
673    
674  #ifdef _DEBUG          int input_valid = 1;
675    
676    #ifdef _DEBUG_PSNR
677          float psnr;          float psnr;
678          char temp[128];          char temp[128];
679  #endif  #endif
680    
681          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
682          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
683            ENC_CHECK(pFrame->image);
684    
685          start_global_timer();          start_global_timer();
686    
687          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
688    
689    ipvop_loop:
690    
691          /*          /*
692           * bframe "flush" code           * bframe "flush" code
693           */           */
# Line 591  Line 702 
702                           * frame as a pframe                           * frame as a pframe
703                           */                           */
704    
705                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
706                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
707                             dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
708                           */  
709                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
710                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
711    
# Line 604  Line 715 
715    
716                          BitstreamPad(&bs);                          BitstreamPad(&bs);
717                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
718                          pFrame->intra = 0;                          pFrame->intra = 0;
719    
720                          return XVID_ERR_OK;                          return XVID_ERR_OK;
721                  }                  }
722    
723                  /* ToDo : remove dprintf calls */  
724                  /*                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
725                     dprintf("--- BFRAME (flush) --- ");                                  pEnc->bframenum_head, pEnc->bframenum_tail,
726                   */                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
727    
728                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
729                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
730    
   
731                  BitstreamPad(&bs);                  BitstreamPad(&bs);
732                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
733                  pFrame->intra = 0;                  pFrame->intra = 0;
734    
735                    if (input_valid)
736                            queue_image(pEnc, pFrame);
737    
738                  return XVID_ERR_OK;                  return XVID_ERR_OK;
739          }          }
740    
741          if (pFrame->image == NULL) {          if (pEnc->bframenum_head > 0) {
742                  pFrame->length = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
743                  pFrame->input_consumed = 1;  
744                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
745    
746                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
747                                    pEnc->bframenum_head, pEnc->bframenum_tail,
748                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
749    
750                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
751                            BitstreamPad(&bs);
752                            BitstreamPutBits(&bs, 0x7f, 8);
753    
754                            pFrame->length = BitstreamLength(&bs);
755                  pFrame->intra = 0;                  pFrame->intra = 0;
756    
757                            if (input_valid)
758                                    queue_image(pEnc, pFrame);
759    
760                  return XVID_ERR_OK;                  return XVID_ERR_OK;
761          }          }
762            }
763    
764          if (pEnc->bframenum_head > 0) {  
765                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  bvop_loop:
766    
767            if (pEnc->bframenum_dx50bvop != -1)
768            {
769    
770                    SWAP(pEnc->current, pEnc->reference);
771                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
772    
773                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
774                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
775                    }
776    
777                    if (input_valid)
778                    {
779                            queue_image(pEnc, pFrame);
780                            input_valid = 0;
781                    }
782    
783            } else if (input_valid) {
784    
785                    SWAP(pEnc->current, pEnc->reference);
786    
787                    start_timer();
788                    if (image_input
789                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
790                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
791                            return XVID_ERR_FORMAT;
792                    stop_conv_timer();
793    
794                    // queue input frame, and dequue next image
795                    if (pEnc->queue_size > 0)
796                    {
797                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
798                            if (pEnc->queue_head != pEnc->queue_tail)
799                            {
800                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
801                            }
802                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
803                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
804                    }
805    
806            } else if (pEnc->queue_size > 0) {
807    
808                    SWAP(pEnc->current, pEnc->reference);
809    
810                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
811                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
812                    pEnc->queue_size--;
813    
814            } else if (BitstreamPos(&bs) == 0) {
815    
816                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
817                                    pEnc->bframenum_head, pEnc->bframenum_tail,
818                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
819    
820                    pFrame->intra = 0;
821    
822                    BitstreamPutBits(&bs, 0x7f, 8);
823                    BitstreamPad(&bs);
824                    pFrame->length = BitstreamLength(&bs);
825    
826                    return XVID_ERR_OK;
827    
828            } else {
829    
830                    pFrame->length = BitstreamLength(&bs);
831                    return XVID_ERR_OK;
832          }          }
833    
834          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
# Line 645  Line 838 
838           * comment style :-)           * comment style :-)
839           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
840    
         SWAP(pEnc->current, pEnc->reference);  
   
841          emms();          emms();
842    
843            // only inc frame num, adapt quant, etc. if we havent seen it before
844            if (pEnc->bframenum_dx50bvop < 0 )
845            {
846          if (pFrame->quant == 0)          if (pFrame->quant == 0)
847                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
848          else          else
# Line 662  Line 856 
856    
857          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
858          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
859          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
860          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
861          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
862          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
863    
864          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
865          if (image_input                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
                 (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))  
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
866    
867  #ifdef _DEBUG                  inc_frame_num(pEnc);
868    
869    #ifdef _DEBUG_PSNR
870          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
871                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
872  #endif  #endif
873    
874          emms();          emms();
875    
876                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
877                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
878                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
879                    }
880    
881          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
882           * Luminance masking           * Luminance masking
883           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 711  Line 907 
907                          }                          }
908    
909  #undef OFFSET  #undef OFFSET
   
910                  }                  }
911    
912                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
913          }          }
914    
915            }
916    
917          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
919           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
920    
921    
922          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
923                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
924                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
925                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
# Line 732  Line 929 
929                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
930                   */                   */
931    
932                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
933                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
934                     dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                  */  
935    
936                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
937                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
938                    }
939    
940                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
941    
942                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
943    
944                            pEnc->bframenum_tail--;
945                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
946    
947                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
948                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
949                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
950                            }
951                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
952    
953                            pFrame->intra = 0;
954    
955                    } else {
956    
957                            FrameCodeI(pEnc, &bs, &bits);
958                  pFrame->intra = 1;                  pFrame->intra = 1;
959    
960                            pEnc->bframenum_dx50bvop = -1;
961                    }
962    
963                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
964    
965                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
966                            BitstreamPad(&bs);
967                            input_valid = 0;
968                            goto ipvop_loop;
969                    }
970    
971                  /*                  /*
972                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
973                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
# Line 751  Line 977 
977                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
978                   */                   */
979    
980                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
981                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
982                     dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
983                   */  
984                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
985                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
986                    }
987    
988                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
989                  pFrame->intra = 0;                  pFrame->intra = 0;
990                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
991    
992                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
993                            BitstreamPad(&bs);
994                            input_valid = 0;
995                            goto ipvop_loop;
996                    }
997    
998          } else {          } else {
999                  /*                  /*
1000                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1001                   */                   */
1002    
1003                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1004                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1005                     dprintf("--- BFRAME (store) ---  head=%i tail=%i",                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1006                     pEnc->bframenum_head,  
1007                     pEnc->bframenum_tail);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1008                   */                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1009                    }
1010    
1011                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1012                          pEnc->current->quant =                          pEnc->current->quant =
# Line 787  Line 1024 
1024    
1025                  pFrame->intra = 0;                  pFrame->intra = 0;
1026                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
   
                 pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;  
                 if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
1027    
1028                  return XVID_ERR_OK;                  input_valid = 0;
1029                    goto bvop_loop;
1030          }          }
1031    
1032          BitstreamPad(&bs);          BitstreamPad(&bs);
# Line 811  Line 1042 
1042    
1043          emms();          emms();
1044    
1045  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1046          psnr =          psnr =
1047                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1048                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 826  Line 1057 
1057                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1058          }          }
1059    
         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;  
1060    
1061          stop_global_timer();          stop_global_timer();
1062          write_timer();          write_timer();
1063    
1064          return XVID_ERR_OK;          return XVID_ERR_OK;
1065  }  }
1066  #else  
1067    #endif
1068    
1069    
1070    
1071  /*****************************************************************************  /*****************************************************************************
1072   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1073     *
1074     * Returned values :
1075     *    - XVID_ERR_OK     - no errors
1076     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1077     *                        format
1078   ****************************************************************************/   ****************************************************************************/
1079    
1080  int  int
1081  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1082                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
# Line 853  Line 1087 
1087          uint32_t bits;          uint32_t bits;
1088          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1089    
1090  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1091          float psnr;          float psnr;
1092          uint8_t temp[128];          uint8_t temp[128];
1093  #endif  #endif
# Line 869  Line 1103 
1103    
1104          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1105          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1106    #ifdef BFRAMES
1107            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1108            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1109    #endif
1110          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1111    
1112          start_timer();          start_timer();
# Line 878  Line 1116 
1116                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1117          stop_conv_timer();          stop_conv_timer();
1118    
1119  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1120          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1121                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1122  #endif  #endif
# Line 988  Line 1226 
1226                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1227                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1228          }          }
1229  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1230          psnr =          psnr =
1231                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1232                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 998  Line 1236 
1236          DEBUG(temp);          DEBUG(temp);
1237  #endif  #endif
1238    
1239    #ifdef BFRAMES
1240            inc_frame_num(pEnc);
1241    #else
1242          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1243    #endif
1244    
1245    
1246          stop_global_timer();          stop_global_timer();
1247          write_timer();          write_timer();
1248    
1249          return XVID_ERR_OK;          return XVID_ERR_OK;
1250  }  }
 #endif  
1251    
1252    
1253  static __inline void  static __inline void
# Line 1075  Line 1317 
1317                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1318                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1319                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1320                          VECTOR pred[4];                          VECTOR pred;
1321                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1322                          int vec;                          int vec;
1323    
1324                          pMB->mode =                          pMB->mode =
# Line 1097  Line 1338 
1338                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1339                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1340    
1341                                  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);  
1342    
1343                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1344                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1345                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1346                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1347                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1348                                  }                                  }
1349                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1350                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1117  Line 1357 
1357                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1358                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1359    
1360                                          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);  
1361    
1362                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1363                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1364                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1365                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1366                                  }                                  }
1367                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1368                          {                          {
# Line 1253  Line 1492 
1492          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1493    
1494          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1495          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  #ifdef BFRAMES
1496    #define DIVX501B481P "DivX501b481p"
1497            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1498                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1499            }
1500    #endif
1501            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1502    
1503          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1504    
# Line 1348  Line 1593 
1593          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1594                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1595          } else {          } else {
1596    
1597    #ifdef _SMP
1598                    if (NUMTHREADS > 1)
1599                            bIntra =
1600                                    SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1601                                                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1602                                                             iLimit);
1603                    else
1604    #endif
1605    
1606                  bIntra =                  bIntra =
1607                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1608                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1609                                                           iLimit);                                                           iLimit);
1610    
1611    
1612          }          }
1613          stop_motion_timer();          stop_motion_timer();
1614    
# Line 1364  Line 1621 
1621          if (vol_header)          if (vol_header)
1622                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1623    
1624          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1625    
1626          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1627    
# Line 1485  Line 1742 
1742          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1743          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1744    
1745    #ifdef BFRAMES_DEC_DEBUG
1746            FILE *fp;
1747            static char first=0;
1748    #define BFRAME_DEBUG    if (!first && fp){ \
1749                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1750            }
1751    
1752            if (!first){
1753                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1754            }
1755    #endif
1756    
1757          // forward          // forward
1758          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1759                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
# Line 1522  Line 1791 
1791             } */             } */
1792    
1793          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1794          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1795    
1796          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1797    
# Line 1551  Line 1820 
1820                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1821                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1822                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1823    
1824                                    mb->cbp = 0;
1825    #ifdef BFRAMES_DEC_DEBUG
1826            BFRAME_DEBUG
1827    #endif
1828                                  continue;                                  continue;
1829                          }                          }
1830    
# Line 1585  Line 1859 
1859                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1860                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1861                          }                          }
1862  //          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);
1863    
1864    #ifdef BFRAMES_DEC_DEBUG
1865            BFRAME_DEBUG
1866    #endif
1867                          start_timer();                          start_timer();
1868                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1869                                                   &pEnc->sStat);                                                   &pEnc->sStat);
# Line 1599  Line 1876 
1876          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1877    
1878          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1879    
1880    #ifdef BFRAMES_DEC_DEBUG
1881            if (!first){
1882                    first=1;
1883                    if (fp)
1884                            fclose(fp);
1885            }
1886    #endif
1887  }  }
1888  #endif  #endif

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.54

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