[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

trunk/xvidcore/src/encoder.c revision 195, Wed Jun 12 20:38:41 2002 UTC branches/dev-api-3/xvidcore/src/encoder.c revision 557, Sat Sep 28 13:01:15 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.41 2002-06-12 20:38:40 edgomez Exp $   *  $Id: encoder.c,v 1.76.2.4 2002-09-28 13:01:02 chl Exp $
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
46    
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <stdio.h>  #include <stdio.h>
49  #include <math.h>  #include <math.h>
50    #include <string.h>
51    
52  #include "encoder.h"  #include "encoder.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "global.h"  #include "global.h"
55  #include "utils/timer.h"  #include "utils/timer.h"
56  #include "image/image.h"  #include "image/image.h"
57    #ifdef BFRAMES
58    #include "image/font.h"
59    #include "motion/sad.h"
60    #endif
61  #include "motion/motion.h"  #include "motion/motion.h"
62  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
63  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 82  Line 91 
91                                            bool force_inter,                                            bool force_inter,
92                                            bool vol_header);                                            bool vol_header);
93    
 #ifdef BFRAMES  
94  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
95                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
96                                             Bitstream * bs,                                             Bitstream * bs,
97                                             uint32_t * pBits);                                             uint32_t * pBits);
 #endif  
98    
99  /*****************************************************************************  /*****************************************************************************
100   * Local data   * Local data
# Line 131  Line 138 
138  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
139  {  {
140          Encoder *pEnc;          Encoder *pEnc;
141          uint32_t i;          int i;
142    
143          pParam->handle = NULL;          pParam->handle = NULL;
144    
# Line 199  Line 206 
206    
207          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
208    
209          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
210                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
211    
   
212          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
213          if (pEnc == NULL)          if (pEnc == NULL)
214                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
215    
216            /* Zero the Encoder Structure */
217    
218            memset(pEnc, 0, sizeof(Encoder));
219    
220          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
221    
222          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 221  Line 231 
231          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
232          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
233    
234            pEnc->mbParam.m_quant_type = H263_QUANT;
235    
236          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
237    
238          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 252  Line 264 
264    
265          /* try to allocate image memory */          /* try to allocate image memory */
266    
267  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
268          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
269  #endif  #endif
270  #ifdef BFRAMES  
271          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
272          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
273          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
274  #endif  
275          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
276          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
277          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 268  Line 280 
280          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
281          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
282    
283  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
284          if (image_create          if (image_create
285                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
286                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
287                  goto xvid_err_memory3;                  goto xvid_err_memory3;
288  #endif  #endif
289  #ifdef BFRAMES  
290          if (image_create          if (image_create
291                  (&pEnc->f_refh, pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
292                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 287  Line 299 
299                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
300                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
301                  goto xvid_err_memory3;                  goto xvid_err_memory3;
302  #endif  
303          if (image_create          if (image_create
304                  (&pEnc->current->image, pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
305                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
# Line 320  Line 332 
332    
333    
334          /* B Frames specific init */          /* B Frames specific init */
 #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->frame_drop_ratio = pParam->frame_drop_ratio;
340          pEnc->bframes = NULL;          pEnc->bframes = NULL;
341    
342          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
# Line 366  Line 379 
379          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
380          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
381          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
382            pEnc->bframenum_dx50bvop = -1;
383    
384          pEnc->mbParam.m_seconds = 0;          pEnc->queue = NULL;
385          pEnc->mbParam.m_ticks = 0;  
386  #endif  
387            if (pEnc->mbParam.max_bframes > 0) {
388                    int n;
389    
390                    pEnc->queue =
391                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
392                                                    CACHE_LINE);
393    
394                    if (pEnc->queue == NULL)
395                            goto xvid_err_memory4;
396    
397                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
398                            image_null(&pEnc->queue[n]);
399    
400                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
401                            if (image_create
402                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
403                                     pEnc->mbParam.edged_height) < 0)
404                                    goto xvid_err_memory5;
405    
406                    }
407            }
408    
409            pEnc->queue_head = 0;
410            pEnc->queue_tail = 0;
411            pEnc->queue_size = 0;
412    
413            pEnc->mbParam.m_stamp = 0;
414            pEnc->m_framenum = 0;
415    
416          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
417    
# Line 388  Line 430 
430          /*          /*
431           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
432           */           */
433  #ifdef BFRAMES  
434      xvid_err_memory5:
435    
436    
437            if (pEnc->mbParam.max_bframes > 0) {
438    
439                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
440                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
441                                                      pEnc->mbParam.edged_height);
442                    }
443                    xvid_free(pEnc->queue);
444            }
445    
446    xvid_err_memory4:    xvid_err_memory4:
447    
448            if (pEnc->mbParam.max_bframes > 0) {
449    
450          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
451    
452                  if (pEnc->bframes[i] == NULL)                  if (pEnc->bframes[i] == NULL)
# Line 405  Line 462 
462          }          }
463    
464          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
465            }
 #endif  
466    
467    xvid_err_memory3:    xvid_err_memory3:
468  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
469          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
470                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
471  #endif  #endif
472    
 #ifdef BFRAMES  
473          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
474                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
475          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
476                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
477          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
478                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
479    
480          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
481                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 466  Line 520 
520  int  int
521  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
522  {  {
523            int i;
524    
525          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
526    
527          /* B Frames specific */          /* B Frames specific */
528  #ifdef BFRAMES          if (pEnc->mbParam.max_bframes > 0) {
529          int i;  
530                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
531    
532                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
533                                              pEnc->mbParam.edged_height);
534                    }
535                    xvid_free(pEnc->queue);
536            }
537    
538    
539            if (pEnc->mbParam.max_bframes > 0) {
540    
541          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
542    
# Line 483  Line 549 
549                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
550    
551                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
552          }          }
553    
554          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
555    
556  #endif          }
557    
558          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
559    
# Line 506  Line 571 
571                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
572          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
573                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
574  #ifdef BFRAMES  
575          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
576                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
577          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
578                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
579          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
580                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
581  #endif  
582  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
583          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
584                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
585  #endif  #endif
# Line 532  Line 597 
597          return XVID_ERR_OK;          return XVID_ERR_OK;
598  }  }
599    
600    
601    static __inline void inc_frame_num(Encoder * pEnc)
602    {
603            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
604            pEnc->current->stamp = pEnc->mbParam.m_stamp;
605    }
606    
607    
608    static __inline void
609    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
610    {
611            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
612            {
613                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
614                    return;
615            }
616    
617            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
618                                    pEnc->bframenum_head, pEnc->bframenum_tail,
619                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
620    
621    
622            start_timer();
623            if (image_input
624                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
625                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
626                    return;
627            stop_conv_timer();
628    
629            pEnc->queue_size++;
630            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
631    }
632    
633    static __inline void
634    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
635    {
636    
637                    pCur->ticks = (int32_t)pCur->stamp % time_base;
638                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
639    
640    /*              HEAVY DEBUG OUTPUT      remove when timecodes prove to be stable
641    
642                    fprintf(stderr,"WriteVop:   %d - %d \n",
643                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
644                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
645                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
646                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
647                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
648    
649    */
650    }
651    
652    
653    
654    
655    
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 544  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  {  {
670          uint16_t x, y;          uint16_t x, y;
671          Bitstream bs;          Bitstream bs;
672          uint32_t bits;          uint32_t bits, mode;
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 584  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 597  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                            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
751                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
752                            BitstreamPad(&bs);
753                            BitstreamPutBits(&bs, 0x7f, 8);
754    
755                            pFrame->length = BitstreamLength(&bs);
756                  pFrame->intra = 0;                  pFrame->intra = 0;
757    
758                            if (input_valid)
759                                    queue_image(pEnc, pFrame);
760    
761                  return XVID_ERR_OK;                  return XVID_ERR_OK;
762          }          }
763            }
764    
765          if (pEnc->bframenum_head > 0) {  
766                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  bvop_loop:
767    
768            if (pEnc->bframenum_dx50bvop != -1)
769            {
770    
771                    SWAP(pEnc->current, pEnc->reference);
772                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
773    
774                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
775                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
776          }          }
777    
778          pEnc->flush_bframes = 0;                  if (input_valid)
779                    {
780                            queue_image(pEnc, pFrame);
781                            input_valid = 0;
782                    }
783    
784          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          } else if (input_valid) {
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
785    
786          SWAP(pEnc->current, pEnc->reference);          SWAP(pEnc->current, pEnc->reference);
787    
788          EMMS();                  start_timer();
789                    if (image_input
790                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
791                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
792                            return XVID_ERR_FORMAT;
793                    stop_conv_timer();
794    
795                    // queue input frame, and dequue next image
796                    if (pEnc->queue_size > 0)
797                    {
798                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
799                            if (pEnc->queue_head != pEnc->queue_tail)
800                            {
801                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
802                            }
803                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
804                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
805                    }
806    
807            } else if (pEnc->queue_size > 0) {
808    
809                    SWAP(pEnc->current, pEnc->reference);
810    
811                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
812                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
813                    pEnc->queue_size--;
814    
815            } else if (BitstreamPos(&bs) == 0) {
816    
817                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
818                                    pEnc->bframenum_head, pEnc->bframenum_tail,
819                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
820    
821                    pFrame->intra = 0;
822    
823                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
824                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
825                    BitstreamPad(&bs);
826                    pFrame->length = BitstreamLength(&bs);
827    
828                    return XVID_ERR_OK;
829    
830            } else {
831    
832                    pFrame->length = BitstreamLength(&bs);
833                    return XVID_ERR_OK;
834            }
835    
836            pEnc->flush_bframes = 0;
837    
838            emms();
839    
840            // only inc frame num, adapt quant, etc. if we havent seen it before
841            if (pEnc->bframenum_dx50bvop < 0 )
842            {
843          if (pFrame->quant == 0)          if (pFrame->quant == 0)
844                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
845          else          else
846                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
847    
848          if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
849                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
850    
851          if (pEnc->current->quant > 31)          if (pEnc->current->quant > 31)
852                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
853    */
854          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
855          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
856          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
857          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
858          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
859          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
860    
861          start_timer();                  inc_frame_num(pEnc);
         if (image_input  
                 (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))  
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
862    
863  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
864          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
865                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
866  #endif  #endif
867    
868          EMMS();                  emms();
869    
870                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
871                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
872                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
873                    }
874    
875          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876           * Luminance masking           * Luminance masking
# Line 704  Line 901 
901                          }                          }
902    
903  #undef OFFSET  #undef OFFSET
   
904                  }                  }
905    
906                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
907          }          }
908    
909            }
910    
911          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
913           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
914    
915    
916          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
917                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
918                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
919                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
920                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
921                                           pEnc->mbParam.height) > 30) {                                           pEnc->mbParam.height) > 30) {*/
922                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
923                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
924    
925                  /*                  /*
926                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
927                   */                   */
928    
929                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
930                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
931                     dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                  */  
932    
933                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
934                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
935                    }
936    
937                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
938    
939                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
940    
941                            pEnc->bframenum_tail--;
942                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
943    
944                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
945                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
946                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
947                            }
948                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
949    
950                            pFrame->intra = 0;
951    
952                    } else {
953    
954                            FrameCodeI(pEnc, &bs, &bits);
955                  pFrame->intra = 1;                  pFrame->intra = 1;
956    
957                            pEnc->bframenum_dx50bvop = -1;
958                    }
959    
960                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
961    
962                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
963                            BitstreamPad(&bs);
964                            input_valid = 0;
965                            goto ipvop_loop;
966                    }
967    
968                  /*                  /*
969                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
970                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
971                   */                   */
972          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
973                  /*                  /*
974                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
975                   */                   */
976    
977                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
978                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
979                     dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
980                   */  
981                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
982                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
983                    }
984    
985                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
986                  pFrame->intra = 0;                  pFrame->intra = 0;
987                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
988    
989                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
990                            BitstreamPad(&bs);
991                            input_valid = 0;
992                            goto ipvop_loop;
993                    }
994    
995          } else {          } else {
996                  /*                  /*
997                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
998                   */                   */
999    
1000                  /* ToDo : Remove dprintf calls */                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1001                  /*                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1002                     dprintf("--- BFRAME (store) ---  head=%i tail=%i",                  }
                    pEnc->bframenum_head,  
                    pEnc->bframenum_tail);  
                  */  
1003    
1004                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1005                          pEnc->current->quant =                          pEnc->current->quant =
# Line 771  Line 1008 
1008                  } else {                  } else {
1009                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1010                  }                  }
1011                    if (pEnc->current->quant < 1)
1012                            pEnc->current->quant = 1;
1013    
1014                    if (pEnc->current->quant > 31)
1015                            pEnc->current->quant = 31;
1016    
1017    
1018                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1019                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1020                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1021    
1022    
1023    
1024                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1025                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 780  Line 1029 
1029    
1030                  pFrame->intra = 0;                  pFrame->intra = 0;
1031                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1032    
1033                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1034                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {                  goto bvop_loop;
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
1035                  }                  }
1036    
1037                  return XVID_ERR_OK;          pEnc->iFrameNum++;
         }  
1038    
1039          BitstreamPad(&bs);          BitstreamPad(&bs);
1040          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
# Line 802  Line 1047 
1047                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1048          }          }
1049    
1050          EMMS();          emms();
1051    
1052  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1053          psnr =          psnr =
1054                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1055                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 819  Line 1064 
1064                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1065          }          }
1066    
         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;  
   
1067          stop_global_timer();          stop_global_timer();
1068          write_timer();          write_timer();
1069    
1070          return XVID_ERR_OK;          return XVID_ERR_OK;
1071  }  }
1072  #else  
1073    
1074    
1075  /*****************************************************************************  /*****************************************************************************
1076   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1077     *
1078     * Returned values :
1079     *    - XVID_ERR_OK     - no errors
1080     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1081     *                        format
1082   ****************************************************************************/   ****************************************************************************/
1083    
1084  int  int
1085  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1086                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
# Line 846  Line 1091 
1091          uint32_t bits;          uint32_t bits;
1092          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1093    
1094  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1095          float psnr;          float psnr;
1096          uint8_t temp[128];          uint8_t temp[128];
1097  #endif  #endif
# Line 864  Line 1109 
1109          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1110          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1111    
1112            /* disable alternate scan flag if interlacing is not enabled */
1113            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1114                    !(pEnc->current->global_flags & XVID_INTERLACING))
1115            {
1116                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1117            }
1118    
1119          start_timer();          start_timer();
1120          if (image_input          if (image_input
1121                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
# Line 871  Line 1123 
1123                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1124          stop_conv_timer();          stop_conv_timer();
1125    
1126  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1127          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1128                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1129  #endif  #endif
1130    
1131          EMMS();          emms();
1132    
1133          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1134    
# Line 975  Line 1227 
1227                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1228          }          }
1229    
1230          EMMS();          emms();
1231    
1232          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1233                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1234                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1235          }          }
1236  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1237          psnr =          psnr =
1238                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1239                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 991  Line 1243 
1243          DEBUG(temp);          DEBUG(temp);
1244  #endif  #endif
1245    
1246            inc_frame_num(pEnc);
1247          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1248    
1249          stop_global_timer();          stop_global_timer();
# Line 998  Line 1251 
1251    
1252          return XVID_ERR_OK;          return XVID_ERR_OK;
1253  }  }
 #endif  
1254    
1255    
1256  static __inline void  static __inline void
# Line 1068  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 1090  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 1110  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 1246  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);  
1499    #define DIVX501B481P "DivX501b481p"
1500            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1501                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1502            }
1503    
1504            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1505            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1506    
1507          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1508    
# Line 1269  Line 1525 
1525                          stop_prediction_timer();                          stop_prediction_timer();
1526    
1527                          start_timer();                          start_timer();
1528                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1529                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1530                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1531                                    qcoeff[5*64+0]=0;
1532                            }
1533                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1534                          stop_coding_timer();                          stop_coding_timer();
1535                  }                  }
# Line 1281  Line 1542 
1542          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1543          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1544    
1545    //      pEnc->time_pp = ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp));
1546    
1547          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1548                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1549          }          }
1550    //      pEnc->last_pframe = (int32_t)pEnc->mbParam.m_stamp;
1551    
1552          return 1;                                       // intra          return 1;                                       // intra
1553  }  }
1554    
1555    
1556  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1557    #define BFRAME_SKIP_THRESHHOLD 30
1558    
1559  static int  static int
1560  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1304  Line 1569 
1569          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1570    
1571          int iLimit;          int iLimit;
1572          uint32_t x, y;          int x, y, k;
1573          int iSearchRange;          int iSearchRange;
1574          int bIntra;          int bIntra;
1575    
# Line 1313  Line 1578 
1578    
1579          start_timer();          start_timer();
1580          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1581                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1582          stop_edges_timer();          stop_edges_timer();
1583    
1584          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 1340  Line 1604 
1604          start_timer();          start_timer();
1605          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1606                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1607                    if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1608                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1609    
1610          } else {          } else {
1611    
1612                  bIntra =                  bIntra =
1613                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1614                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1615                                                           iLimit);                                                           iLimit);
1616    
1617          }          }
1618          stop_motion_timer();          stop_motion_timer();
1619    
1620          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1621    
1622          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1623    
1624          if (vol_header)          if (vol_header)
1625                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1626    
1627          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1628            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1629            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1630    
1631          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1632    
1633          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1634          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1635    
1636          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1398  Line 1665 
1665    
1666                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1667    
1668                                    if (pMB->mode != MODE_NOT_CODED)
1669                                  pMB->cbp =                                  pMB->cbp =
1670                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1671                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1422  Line 1690 
1690                          }                          }
1691    
1692                          start_timer();                          start_timer();
1693    
1694                            /* Finished processing the MB, now check if to CODE or SKIP */
1695    
1696                            if ((pMB->mode == MODE_NOT_CODED) ||
1697                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1698                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1699    
1700    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1701    
1702                                    int bSkip = 1;
1703                                    pMB->mode = MODE_NOT_CODED;
1704    
1705                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1706                                    {
1707                                            int iSAD;
1708                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1709                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1710                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1711                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1712                                            {       bSkip = 0;
1713                                                    break;
1714                                            }
1715                                    }
1716                                    if (!bSkip)
1717                                    {
1718                                            VECTOR predMV;
1719                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1720                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1721                                            pMB->mode = MODE_INTER;
1722                                            pMB->cbp = 0;
1723                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1724                                    }
1725                                    else MBSkip(bs);
1726    
1727                            } else {
1728                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1729                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1730                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1731                                            qcoeff[5*64+0]=0;
1732                                    }
1733                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1734                            }
1735    
1736                          stop_coding_timer();                          stop_coding_timer();
1737                  }                  }
1738          }          }
# Line 1456  Line 1766 
1766    
1767          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1768    
1769    #ifdef FRAMEDROP
1770            /* frame drop code */
1771            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1772            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1773                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1774            {
1775                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1776                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1777    
1778                    BitstreamReset(bs);
1779    
1780                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1781                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1782    
1783                    // copy reference frame details into the current frame
1784                    pEnc->current->quant = pEnc->reference->quant;
1785                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1786                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1787                    pEnc->current->fcode = pEnc->reference->fcode;
1788                    pEnc->current->bcode = pEnc->reference->bcode;
1789                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1790                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1791    
1792            }
1793    #endif
1794    
1795          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1796    
1797    //      pEnc->time_pp = ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp));
1798    //      pEnc->last_pframe = (int32_t)pEnc->mbParam.m_stamp;
1799    
1800          return 0;                                       // inter          return 0;                                       // inter
1801  }  }
1802    
1803    
1804  #ifdef BFRAMES  static __inline void
 static void  
1805  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1806                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1807                     Bitstream * bs,                     Bitstream * bs,
# Line 1472  Line 1810 
1810          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1811          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1812          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1813    
1814          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1815          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1816    
1817    #ifdef BFRAMES_DEC_DEBUG
1818            FILE *fp;
1819            static char first=0;
1820    #define BFRAME_DEBUG    if (!first && fp){ \
1821                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1822            }
1823    
1824            if (!first){
1825                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1826            }
1827    #endif
1828    
1829          // forward          // forward
1830          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1831                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1832                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1833          start_timer();          start_timer();
1834          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1835                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1492  Line 1839 
1839          // backward          // backward
1840          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1841                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1842                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1843          start_timer();          start_timer();
1844          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1845                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1501  Line 1847 
1847          stop_inter_timer();          stop_inter_timer();
1848    
1849          start_timer();          start_timer();
1850          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,  
1851            MotionEstimationBVOP(&pEnc->mbParam, frame,
1852                    ((int32_t)(frame->stamp - pEnc->reference->stamp)),                             // time_bp
1853                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1854                            pEnc->reference->mbs, f_ref,
1855                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1856                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1857                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1515  Line 1865 
1865             } */             } */
1866    
1867          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1868          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
1869            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1870            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1871    
1872          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1873    
# Line 1526  Line 1878 
1878    
1879    
1880          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 // reset prediction  
   
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
   
1881                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1882                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1883                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                          int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;
                         MACROBLOCK *b_mb =  
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
1884    
1885                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1886                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1887                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
1888                                  continue;                                  continue;
1889                          }                          }
1890    
1891                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1892                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1893                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1894                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1895                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1896                                                                           dct_codes);                                                                           dct_codes);
1897    
1898                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1899                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp =  
                                 MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,  
                                                                   qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
1900    
1901                                    mb->cbp =
1902                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1903    
1904                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1905                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1906                                  mb->mode = 5;   // skipped                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
1907                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {  
                                 mb->pmvs[0].x = mb->mvs[0].x - forward.x;  
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
1908                          }                          }
1909    
1910                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {  #ifdef BFRAMES_DEC_DEBUG
1911                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;          BFRAME_DEBUG
1912                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  #endif
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
                         }  
 //          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);  
   
1913                          start_timer();                          start_timer();
1914                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1915                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1916                          stop_coding_timer();                          stop_coding_timer();
1917                  }                  }
1918          }          }
# Line 1592  Line 1922 
1922          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1923    
1924          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1925    
1926    #ifdef BFRAMES_DEC_DEBUG
1927            if (!first){
1928                    first=1;
1929                    if (fp)
1930                            fclose(fp);
1931  }  }
1932  #endif  #endif
1933    }
1934    
1935    
1936    /*      in case internal output is needed somewhere... */
1937    /*      {
1938            FILE *filehandle;
1939            filehandle=fopen("last-b.pgm","wb");
1940            if (filehandle)
1941            {
1942                    fprintf(filehandle,"P5\n\n");           //
1943                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1944                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1945                    fclose(filehandle);
1946                    }
1947            }
1948    */

Legend:
Removed from v.195  
changed lines
  Added in v.557

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