[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

revision 198, Thu Jun 13 11:42:15 2002 UTC revision 252, Sun Jun 30 10:46:29 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *      20.06.2002 bframe patch
36   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
37   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
38   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
39   *   *
40   *  $Id: encoder.c,v 1.42 2002-06-13 11:42:15 edgomez Exp $   *  $Id: encoder.c,v 1.51 2002-06-30 10:46:29 suxen_drol Exp $
41   *   *
42   ****************************************************************************/   ****************************************************************************/
43    
# Line 50  Line 51 
51  #include "global.h"  #include "global.h"
52  #include "utils/timer.h"  #include "utils/timer.h"
53  #include "image/image.h"  #include "image/image.h"
54    #ifdef BFRAMES
55    #include "image/font.h"
56    #endif
57  #include "motion/motion.h"  #include "motion/motion.h"
58  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
59  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 132  Line 136 
136  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
137  {  {
138          Encoder *pEnc;          Encoder *pEnc;
139          uint32_t i;          int i;
140    
141          pParam->handle = NULL;          pParam->handle = NULL;
142    
# Line 226  Line 230 
230          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
231          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
232    
233            pEnc->mbParam.m_quant_type = H263_QUANT;
234    
235          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
236    
237          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 257  Line 263 
263    
264          /* try to allocate image memory */          /* try to allocate image memory */
265    
266  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
267          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
268  #endif  #endif
269  #ifdef BFRAMES  #ifdef BFRAMES
# Line 273  Line 279 
279          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
280          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
281    
282  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
283          if (image_create          if (image_create
284                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
285                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 327  Line 333 
333          /* B Frames specific init */          /* B Frames specific init */
334  #ifdef BFRAMES  #ifdef BFRAMES
335    
336            pEnc->global = pParam->global;
337          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
338          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
339          pEnc->bframes = NULL;          pEnc->bframes = NULL;
# Line 371  Line 378 
378          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
379          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
380          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
381            pEnc->bframenum_dx50bvop = -1;
382    
383            pEnc->queue = NULL;
384    
385    
386            if (pEnc->mbParam.max_bframes > 0) {
387                    int n;
388    
389                    pEnc->queue =
390                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
391                                                    CACHE_LINE);
392    
393                    if (pEnc->queue == NULL)
394                            goto xvid_err_memory4;
395    
396                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
397                            image_null(&pEnc->queue[n]);
398    
399                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
400                            if (image_create
401                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
402                                     pEnc->mbParam.edged_height) < 0)
403                                    goto xvid_err_memory5;
404    
405                    }
406            }
407    
408            pEnc->queue_head = 0;
409            pEnc->queue_tail = 0;
410            pEnc->queue_size = 0;
411    
412    
413          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
414          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
415            pEnc->m_framenum = 0;
416  #endif  #endif
417    
418          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
# Line 394  Line 433 
433           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
434           */           */
435  #ifdef BFRAMES  #ifdef BFRAMES
436      xvid_err_memory5:
437    
438    
439            if (pEnc->mbParam.max_bframes > 0) {
440    
441                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
442                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
443                                                      pEnc->mbParam.edged_height);
444                    }
445                    xvid_free(pEnc->queue);
446            }
447    
448    xvid_err_memory4:    xvid_err_memory4:
449    
450            if (pEnc->mbParam.max_bframes > 0) {
451    
452          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
453    
454                  if (pEnc->bframes[i] == NULL)                  if (pEnc->bframes[i] == NULL)
# Line 410  Line 464 
464          }          }
465    
466          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
467            }
468    
469  #endif  #endif
470    
471    xvid_err_memory3:    xvid_err_memory3:
472  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
473          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
474                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
475  #endif  #endif
# Line 471  Line 526 
526  int  int
527  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
528  {  {
529    #ifdef BFRAMES
530            int i;
531    #endif
532    
533          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
534    
535          /* B Frames specific */          /* B Frames specific */
536  #ifdef BFRAMES  #ifdef BFRAMES
537          int i;          if (pEnc->mbParam.max_bframes > 0) {
538    
539                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
540    
541                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
542                                              pEnc->mbParam.edged_height);
543                    }
544                    xvid_free(pEnc->queue);
545            }
546    
547    
548            if (pEnc->mbParam.max_bframes > 0) {
549    
550          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
551    
# Line 488  Line 558 
558                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
559    
560                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
561          }          }
562    
563          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
564    
565            }
566  #endif  #endif
567    
568          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
# Line 519  Line 589 
589          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
590                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
591  #endif  #endif
592  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
593          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
594                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
595  #endif  #endif
# Line 537  Line 607 
607          return XVID_ERR_OK;          return XVID_ERR_OK;
608  }  }
609    
610    
611    #ifdef BFRAMES
612    void inc_frame_num(Encoder * pEnc)
613    {
614            pEnc->iFrameNum++;
615            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
616    
617            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
618            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
619    }
620    #endif
621    
622    
623    #ifdef BFRAMES
624    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
625    {
626            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
627            {
628                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
629                    return;
630            }
631    
632            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
633                                    pEnc->bframenum_head, pEnc->bframenum_tail,
634                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
635    
636    
637            start_timer();
638            if (image_input
639                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
640                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
641                    return;
642            stop_conv_timer();
643    
644            pEnc->queue_size++;
645            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
646    }
647    #endif
648    
649    
650    #ifdef BFRAMES
651  /*****************************************************************************  /*****************************************************************************
652   * 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.  
653   *   *
654   * Returned values :   * Returned values :
655   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 549  Line 657 
657   *                        format   *                        format
658   ****************************************************************************/   ****************************************************************************/
659    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
660  int  int
661  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
662                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
663                             XVID_ENC_STATS * pResult)                             XVID_ENC_STATS * pResult)
664  {  {
# Line 563  Line 666 
666          Bitstream bs;          Bitstream bs;
667          uint32_t bits;          uint32_t bits;
668    
669  #ifdef _DEBUG          int input_valid = 1;
670    
671    #ifdef _DEBUG_PSNR
672          float psnr;          float psnr;
673          char temp[128];          char temp[128];
674  #endif  #endif
675    
676          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
677          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
678            ENC_CHECK(pFrame->image);
679    
680          start_global_timer();          start_global_timer();
681    
682          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
683    
684    ipvop_loop:
685    
686          /*          /*
687           * bframe "flush" code           * bframe "flush" code
688           */           */
# Line 589  Line 697 
697                           * frame as a pframe                           * frame as a pframe
698                           */                           */
699    
700                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
701                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
702                             dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
703                           */  
704                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
705                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
706    
# Line 602  Line 710 
710    
711                          BitstreamPad(&bs);                          BitstreamPad(&bs);
712                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
713                          pFrame->intra = 0;                          pFrame->intra = 0;
714    
715                          return XVID_ERR_OK;                          return XVID_ERR_OK;
716                  }                  }
717    
718                  /* ToDo : remove dprintf calls */  
719                  /*                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
720                     dprintf("--- BFRAME (flush) --- ");                                  pEnc->bframenum_head, pEnc->bframenum_tail,
721                   */                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
722    
723                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
724                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
725    
   
726                  BitstreamPad(&bs);                  BitstreamPad(&bs);
727                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
728                  pFrame->intra = 0;                  pFrame->intra = 0;
729    
730                    if (input_valid)
731                            queue_image(pEnc, pFrame);
732    
733                  return XVID_ERR_OK;                  return XVID_ERR_OK;
734          }          }
735    
736          if (pFrame->image == NULL) {          if (pEnc->bframenum_head > 0) {
737                  pFrame->length = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
738                  pFrame->input_consumed = 1;  
739                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
740    
741                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
742                                    pEnc->bframenum_head, pEnc->bframenum_tail,
743                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
744    
745                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
746                            BitstreamPad(&bs);
747                            BitstreamPutBits(&bs, 0x7f, 8);
748    
749                            pFrame->length = BitstreamLength(&bs);
750                  pFrame->intra = 0;                  pFrame->intra = 0;
751    
752                            if (input_valid)
753                                    queue_image(pEnc, pFrame);
754    
755                  return XVID_ERR_OK;                  return XVID_ERR_OK;
756          }          }
757            }
758    
759          if (pEnc->bframenum_head > 0) {  
760                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  bvop_loop:
761    
762            if (pEnc->bframenum_dx50bvop != -1)
763            {
764    
765                    SWAP(pEnc->current, pEnc->reference);
766                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
767    
768                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
769                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
770                    }
771    
772                    if (input_valid)
773                    {
774                            queue_image(pEnc, pFrame);
775                            input_valid = 0;
776                    }
777    
778            } else if (input_valid) {
779    
780                    SWAP(pEnc->current, pEnc->reference);
781    
782                    start_timer();
783                    if (image_input
784                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
785                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
786                            return XVID_ERR_FORMAT;
787                    stop_conv_timer();
788    
789                    // queue input frame, and dequue next image
790                    if (pEnc->queue_size > 0)
791                    {
792                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
793                            if (pEnc->queue_head != pEnc->queue_tail)
794                            {
795                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
796                            }
797                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
798                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
799                    }
800    
801            } else if (pEnc->queue_size > 0) {
802    
803                    SWAP(pEnc->current, pEnc->reference);
804    
805                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
806                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
807                    pEnc->queue_size--;
808    
809            } else if (BitstreamPos(&bs) == 0) {
810    
811                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
812                                    pEnc->bframenum_head, pEnc->bframenum_tail,
813                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
814    
815                    pFrame->intra = 0;
816    
817                    BitstreamPutBits(&bs, 0x7f, 8);
818                    BitstreamPad(&bs);
819                    pFrame->length = BitstreamLength(&bs);
820    
821                    return XVID_ERR_OK;
822    
823            } else {
824    
825                    pFrame->length = BitstreamLength(&bs);
826                    return XVID_ERR_OK;
827          }          }
828    
829          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
# Line 643  Line 833 
833           * comment style :-)           * comment style :-)
834           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
835    
         SWAP(pEnc->current, pEnc->reference);  
   
836          emms();          emms();
837    
838            // only inc frame num, adapt quant, etc. if we havent seen it before
839            if (pEnc->bframenum_dx50bvop < 0 )
840            {
841          if (pFrame->quant == 0)          if (pFrame->quant == 0)
842                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
843          else          else
# Line 660  Line 851 
851    
852          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
853          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
854          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
855          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
856          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
857          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
858    
859          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
860          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();  
861    
862  #ifdef _DEBUG                  inc_frame_num(pEnc);
863    
864    #ifdef _DEBUG_PSNR
865          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
866                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
867  #endif  #endif
868    
869          emms();          emms();
870    
871                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
872                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
873                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
874                    }
875    
876          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877           * Luminance masking           * Luminance masking
878           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
# Line 709  Line 902 
902                          }                          }
903    
904  #undef OFFSET  #undef OFFSET
   
905                  }                  }
906    
907                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
908          }          }
909    
910            }
911    
912          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
914           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
915    
916    
917          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
918                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
919                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
920                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
# Line 730  Line 924 
924                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
925                   */                   */
926    
927                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
928                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
929                     dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                  */  
930    
931                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
932                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
933                    }
934    
935                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
936    
937                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
938    
939                            pEnc->bframenum_tail--;
940                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
941    
942                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
943                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
944                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
945                            }
946                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
947    
948                            pFrame->intra = 0;
949    
950                    } else {
951    
952                            FrameCodeI(pEnc, &bs, &bits);
953                  pFrame->intra = 1;                  pFrame->intra = 1;
954    
955                            pEnc->bframenum_dx50bvop = -1;
956                    }
957    
958                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
959    
960                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
961                            BitstreamPad(&bs);
962                            input_valid = 0;
963                            goto ipvop_loop;
964                    }
965    
966                  /*                  /*
967                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
968                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
# Line 749  Line 972 
972                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
973                   */                   */
974    
975                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
976                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
977                     dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
978                   */  
979                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
980                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
981                    }
982    
983                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
984                  pFrame->intra = 0;                  pFrame->intra = 0;
985                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
986    
987                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
988                            BitstreamPad(&bs);
989                            input_valid = 0;
990                            goto ipvop_loop;
991                    }
992    
993          } else {          } else {
994                  /*                  /*
995                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
996                   */                   */
997    
998                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
999                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1000                     dprintf("--- BFRAME (store) ---  head=%i tail=%i",                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1001                     pEnc->bframenum_head,  
1002                     pEnc->bframenum_tail);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1003                   */                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1004                    }
1005    
1006                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1007                          pEnc->current->quant =                          pEnc->current->quant =
# Line 785  Line 1019 
1019    
1020                  pFrame->intra = 0;                  pFrame->intra = 0;
1021                  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;  
                 }  
1022    
1023                  return XVID_ERR_OK;                  input_valid = 0;
1024                    goto bvop_loop;
1025          }          }
1026    
1027          BitstreamPad(&bs);          BitstreamPad(&bs);
# Line 809  Line 1037 
1037    
1038          emms();          emms();
1039    
1040  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1041          psnr =          psnr =
1042                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1043                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 824  Line 1052 
1052                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1053          }          }
1054    
         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;  
1055    
1056          stop_global_timer();          stop_global_timer();
1057          write_timer();          write_timer();
1058    
1059          return XVID_ERR_OK;          return XVID_ERR_OK;
1060  }  }
1061  #else  
1062    #endif
1063    
1064    
1065    
1066  /*****************************************************************************  /*****************************************************************************
1067   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1068     *
1069     * Returned values :
1070     *    - XVID_ERR_OK     - no errors
1071     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1072     *                        format
1073   ****************************************************************************/   ****************************************************************************/
1074    
1075  int  int
1076  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1077                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
# Line 851  Line 1082 
1082          uint32_t bits;          uint32_t bits;
1083          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1084    
1085  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1086          float psnr;          float psnr;
1087          uint8_t temp[128];          uint8_t temp[128];
1088  #endif  #endif
# Line 867  Line 1098 
1098    
1099          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1100          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1101    #ifdef BFRAMES
1102            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1103            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1104    #endif
1105          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1106    
1107          start_timer();          start_timer();
# Line 876  Line 1111 
1111                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1112          stop_conv_timer();          stop_conv_timer();
1113    
1114  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1115          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1116                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1117  #endif  #endif
# Line 986  Line 1221 
1221                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1222                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1223          }          }
1224  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1225          psnr =          psnr =
1226                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1227                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 996  Line 1231 
1231          DEBUG(temp);          DEBUG(temp);
1232  #endif  #endif
1233    
1234    #ifdef BFRAMES
1235            inc_frame_num(pEnc);
1236    #else
1237          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1238    #endif
1239    
1240    
1241          stop_global_timer();          stop_global_timer();
1242          write_timer();          write_timer();
1243    
1244          return XVID_ERR_OK;          return XVID_ERR_OK;
1245  }  }
 #endif  
1246    
1247    
1248  static __inline void  static __inline void
# Line 1251  Line 1490 
1490          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1491    
1492          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1493          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  #ifdef BFRAMES
1494    #define DIVX501B481P "DivX501b481p"
1495            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1496                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1497            }
1498    #endif
1499            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1500    
1501          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1502    
# Line 1362  Line 1607 
1607          if (vol_header)          if (vol_header)
1608                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1609    
1610          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1611    
1612          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1613    
# Line 1520  Line 1765 
1765             } */             } */
1766    
1767          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1768          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1769    
1770          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1771    
# Line 1583  Line 1828 
1828                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1829                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1830                          }                          }
1831  //          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);
1832    
1833                          start_timer();                          start_timer();
1834                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,

Legend:
Removed from v.198  
changed lines
  Added in v.252

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