[cvs] / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/encoder.c

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

revision 1.42, Thu Jun 13 11:42:15 2002 UTC revision 1.76.2.6, Sun Sep 29 15:56:21 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37     *  20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
# Line 40  Line 43 
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>
# Line 50  Line 54 
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 83  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 132  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 200  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;
# Line 226  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 257  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 273  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 292  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 325  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 371  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->queue = NULL;
385    
386          pEnc->mbParam.m_seconds = 0;  
387          pEnc->mbParam.m_ticks = 0;          if (pEnc->mbParam.max_bframes > 0) {
388  #endif                  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    ;
415            pEnc->m_framenum = 0;
416    
417          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
418    
# Line 393  Line 431 
431          /*          /*
432           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
433           */           */
434  #ifdef BFRAMES  
435      xvid_err_memory5:
436    
437    
438            if (pEnc->mbParam.max_bframes > 0) {
439    
440                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
441                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
442                                                      pEnc->mbParam.edged_height);
443                    }
444                    xvid_free(pEnc->queue);
445            }
446    
447    xvid_err_memory4:    xvid_err_memory4:
448    
449            if (pEnc->mbParam.max_bframes > 0) {
450    
451          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
452    
453                  if (pEnc->bframes[i] == NULL)                  if (pEnc->bframes[i] == NULL)
# Line 410  Line 463 
463          }          }
464    
465          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
466            }
 #endif  
467    
468    xvid_err_memory3:    xvid_err_memory3:
469  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
470          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
471                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
472  #endif  #endif
473    
 #ifdef BFRAMES  
474          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
475                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
476          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
477                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
478          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
479                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
480    
481          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
482                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 471  Line 521 
521  int  int
522  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
523  {  {
524            int i;
525    
526          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
527    
528          /* B Frames specific */          /* B Frames specific */
529  #ifdef BFRAMES          if (pEnc->mbParam.max_bframes > 0) {
530          int i;  
531                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
532    
533                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
534                                              pEnc->mbParam.edged_height);
535                    }
536                    xvid_free(pEnc->queue);
537            }
538    
539    
540            if (pEnc->mbParam.max_bframes > 0) {
541    
542          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
543    
# Line 488  Line 550 
550                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
551    
552                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
553          }          }
554    
555          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
556    
557  #endif          }
558    
559          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
560    
# Line 511  Line 572 
572                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
573          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
574                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
575  #ifdef BFRAMES  
576          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
577                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
578          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
579                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
580          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
581                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
582  #endif  
583  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
584          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
585                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
586  #endif  #endif
# Line 537  Line 598 
598          return XVID_ERR_OK;          return XVID_ERR_OK;
599  }  }
600    
601    
602    static __inline void inc_frame_num(Encoder * pEnc)
603    {
604            pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
605            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
606    }
607    
608    
609    static __inline void
610    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
611    {
612            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
613            {
614                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
615                    return;
616            }
617    
618            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
619                                    pEnc->bframenum_head, pEnc->bframenum_tail,
620                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
621    
622    
623            start_timer();
624            if (image_input
625                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
626                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
627                    return;
628            stop_conv_timer();
629    
630            pEnc->queue_size++;
631            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
632    }
633    
634    static __inline void
635    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
636    {
637    
638                    pCur->ticks = (int32_t)pCur->stamp % time_base;
639                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
640    
641                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
642    
643    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
644                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
645                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
646                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
647                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
648                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
649    
650    */
651    }
652    
653    
654    
655    
656    
657  /*****************************************************************************  /*****************************************************************************
658   * 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.  
659   *   *
660   * Returned values :   * Returned values :
661   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 549  Line 663 
663   *                        format   *                        format
664   ****************************************************************************/   ****************************************************************************/
665    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
666  int  int
667  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
668                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
669                             XVID_ENC_STATS * pResult)                             XVID_ENC_STATS * pResult)
670  {  {
671          uint16_t x, y;          uint16_t x, y;
672          Bitstream bs;          Bitstream bs;
673          uint32_t bits;          uint32_t bits, mode;
674    
675  #ifdef _DEBUG          int input_valid = 1;
676    
677    #ifdef _DEBUG_PSNR
678          float psnr;          float psnr;
679          char temp[128];          char temp[128];
680  #endif  #endif
681    
682          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
683          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
684            ENC_CHECK(pFrame->image);
685    
686          start_global_timer();          start_global_timer();
687    
688          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
689    
690    ipvop_loop:
691    
692          /*          /*
693           * bframe "flush" code           * bframe "flush" code
694           */           */
# Line 589  Line 703 
703                           * frame as a pframe                           * frame as a pframe
704                           */                           */
705    
706                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
707                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
708                             dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
709                           */  
710                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
711                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
712    
# Line 602  Line 716 
716    
717                          BitstreamPad(&bs);                          BitstreamPad(&bs);
718                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
719                          pFrame->intra = 0;                          pFrame->intra = 0;
720    
721                          return XVID_ERR_OK;                          return XVID_ERR_OK;
722                  }                  }
723    
724                  /* ToDo : remove dprintf calls */  
725                  /*                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
726                     dprintf("--- BFRAME (flush) --- ");                                  pEnc->bframenum_head, pEnc->bframenum_tail,
727                   */                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
728    
729                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
730                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
731    
   
732                  BitstreamPad(&bs);                  BitstreamPad(&bs);
733                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
734                  pFrame->intra = 0;                  pFrame->intra = 0;
735    
736                    if (input_valid)
737                            queue_image(pEnc, pFrame);
738    
739                  return XVID_ERR_OK;                  return XVID_ERR_OK;
740          }          }
741    
742          if (pFrame->image == NULL) {          if (pEnc->bframenum_head > 0) {
743                  pFrame->length = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
744                  pFrame->input_consumed = 1;  
745                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
746    
747                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
748                                    pEnc->bframenum_head, pEnc->bframenum_tail,
749                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
750    
751                            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
752                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
753                            BitstreamPad(&bs);
754                            BitstreamPutBits(&bs, 0x7f, 8);
755    
756                            pFrame->length = BitstreamLength(&bs);
757                  pFrame->intra = 0;                  pFrame->intra = 0;
758    
759                            if (input_valid)
760                                    queue_image(pEnc, pFrame);
761    
762                  return XVID_ERR_OK;                  return XVID_ERR_OK;
763          }          }
764            }
765    
766          if (pEnc->bframenum_head > 0) {  
767                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  bvop_loop:
768    
769            if (pEnc->bframenum_dx50bvop != -1)
770            {
771    
772                    SWAP(pEnc->current, pEnc->reference);
773                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
774    
775                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
776                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
777          }          }
778    
779          pEnc->flush_bframes = 0;                  if (input_valid)
780                    {
781                            queue_image(pEnc, pFrame);
782                            input_valid = 0;
783                    }
784    
785          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          } else if (input_valid) {
786           * Well there was a separation here so i put it in ANSI C  
787           * comment style :-)                  SWAP(pEnc->current, pEnc->reference);
788           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
789                    start_timer();
790                    if (image_input
791                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
792                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
793                            return XVID_ERR_FORMAT;
794                    stop_conv_timer();
795    
796                    // queue input frame, and dequue next image
797                    if (pEnc->queue_size > 0)
798                    {
799                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
800                            if (pEnc->queue_head != pEnc->queue_tail)
801                            {
802                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
803                            }
804                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
805                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
806                    }
807    
808            } else if (pEnc->queue_size > 0) {
809    
810          SWAP(pEnc->current, pEnc->reference);          SWAP(pEnc->current, pEnc->reference);
811    
812                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
813                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
814                    pEnc->queue_size--;
815    
816            } else if (BitstreamPos(&bs) == 0) {
817    
818                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
819                                    pEnc->bframenum_head, pEnc->bframenum_tail,
820                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
821    
822                    pFrame->intra = 0;
823    
824                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
825                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
826                    BitstreamPad(&bs);
827                    pFrame->length = BitstreamLength(&bs);
828    
829                    return XVID_ERR_OK;
830    
831            } else {
832    
833                    pFrame->length = BitstreamLength(&bs);
834                    return XVID_ERR_OK;
835            }
836    
837            pEnc->flush_bframes = 0;
838    
839          emms();          emms();
840    
841            // only inc frame num, adapt quant, etc. if we havent seen it before
842            if (pEnc->bframenum_dx50bvop < 0 )
843            {
844          if (pFrame->quant == 0)          if (pFrame->quant == 0)
845                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
846          else          else
847                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
848    
849          if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
850                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
851    
852          if (pEnc->current->quant > 31)          if (pEnc->current->quant > 31)
853                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
854    */
855          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
856          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
857          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
858          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
859          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
860          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
861    
862          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();  
863    
864  #ifdef _DEBUG  #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,
921                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
922                                           pEnc->mbParam.height) > 30) {                                           pEnc->mbParam.height) > 30) {*/
923                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
924                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
925    
926                  /*                  /*
927                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
928                   */                   */
929    
930                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
931                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
932                     dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                  */  
933    
934                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
935                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
936                    }
937    
938                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
939    
940                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
941    
942                            pEnc->bframenum_tail--;
943                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
944    
945                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
946                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
947                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
948                            }
949                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
950    
951                            pFrame->intra = 0;
952    
953                    } else {
954    
955                            FrameCodeI(pEnc, &bs, &bits);
956                  pFrame->intra = 1;                  pFrame->intra = 1;
957    
958                            pEnc->bframenum_dx50bvop = -1;
959                    }
960    
961                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
962    
963                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
964                            BitstreamPad(&bs);
965                            input_valid = 0;
966                            goto ipvop_loop;
967                    }
968    
969                  /*                  /*
970                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
971                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
972                   */                   */
973          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
974                  /*                  /*
975                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
976                   */                   */
977    
978                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
979                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
980                     dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
981                   */  
982                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
983                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
984                    }
985    
986                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
987                  pFrame->intra = 0;                  pFrame->intra = 0;
988                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
989    
990                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
991                            BitstreamPad(&bs);
992                            input_valid = 0;
993                            goto ipvop_loop;
994                    }
995    
996          } else {          } else {
997                  /*                  /*
998                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
999                   */                   */
1000    
1001                  /* ToDo : Remove dprintf calls */                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1002                  /*                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1003                     dprintf("--- BFRAME (store) ---  head=%i tail=%i",                  }
                    pEnc->bframenum_head,  
                    pEnc->bframenum_tail);  
                  */  
1004    
1005                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
1006                          pEnc->current->quant =                          pEnc->current->quant =
# Line 776  Line 1009 
1009                  } else {                  } else {
1010                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1011                  }                  }
1012                    if (pEnc->current->quant < 1)
1013                            pEnc->current->quant = 1;
1014    
1015                    if (pEnc->current->quant > 31)
1016                            pEnc->current->quant = 31;
1017    
1018    
1019                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1020                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1021                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1022    
1023    
1024    
1025                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1026                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 785  Line 1030 
1030    
1031                  pFrame->intra = 0;                  pFrame->intra = 0;
1032                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1033    
1034                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1035                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {                  goto bvop_loop;
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
1036                  }                  }
1037    
1038                  return XVID_ERR_OK;          pEnc->iFrameNum++;
         }  
1039    
1040          BitstreamPad(&bs);          BitstreamPad(&bs);
1041          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
# Line 809  Line 1050 
1050    
1051          emms();          emms();
1052    
1053  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1054          psnr =          psnr =
1055                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1056                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 824  Line 1065 
1065                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1066          }          }
1067    
         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;  
   
1068          stop_global_timer();          stop_global_timer();
1069          write_timer();          write_timer();
1070    
1071          return XVID_ERR_OK;          return XVID_ERR_OK;
1072  }  }
1073  #else  
1074    
1075    
1076  /*****************************************************************************  /*****************************************************************************
1077   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1078     *
1079     * Returned values :
1080     *    - XVID_ERR_OK     - no errors
1081     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1082     *                        format
1083   ****************************************************************************/   ****************************************************************************/
1084    
1085  int  int
1086  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1087                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
# Line 851  Line 1092 
1092          uint32_t bits;          uint32_t bits;
1093          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1094    
1095  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1096          float psnr;          float psnr;
1097          uint8_t temp[128];          uint8_t temp[128];
1098  #endif  #endif
# Line 869  Line 1110 
1110          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1111          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1112    
1113            /* disable alternate scan flag if interlacing is not enabled */
1114            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1115                    !(pEnc->current->global_flags & XVID_INTERLACING))
1116            {
1117                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1118            }
1119    
1120          start_timer();          start_timer();
1121          if (image_input          if (image_input
1122                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
# Line 876  Line 1124 
1124                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1125          stop_conv_timer();          stop_conv_timer();
1126    
1127  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1128          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1129                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1130  #endif  #endif
# Line 986  Line 1234 
1234                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1235                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1236          }          }
1237  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1238          psnr =          psnr =
1239                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1240                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 996  Line 1244 
1244          DEBUG(temp);          DEBUG(temp);
1245  #endif  #endif
1246    
1247            inc_frame_num(pEnc);
1248          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1249    
1250          stop_global_timer();          stop_global_timer();
# Line 1003  Line 1252 
1252    
1253          return XVID_ERR_OK;          return XVID_ERR_OK;
1254  }  }
 #endif  
1255    
1256    
1257  static __inline void  static __inline void
# Line 1073  Line 1321 
1321                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1322                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1323                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1324                          VECTOR pred[4];                          VECTOR pred;
1325                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1326                          int vec;                          int vec;
1327    
1328                          pMB->mode =                          pMB->mode =
# Line 1095  Line 1342 
1342                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1343                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1344    
1345                                  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);  
1346    
1347                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1348                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1349                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1350                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1351                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1352                                  }                                  }
1353                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1354                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1115  Line 1361 
1361                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1362                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1363    
1364                                          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);  
1365    
1366                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1367                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1368                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1369                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1370                                  }                                  }
1371                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1372                          {                          {
# Line 1251  Line 1496 
1496          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1497    
1498          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1499          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1500    #define DIVX501B481P "DivX501b481p"
1501            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1502                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1503            }
1504    
1505            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1506            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1507    
1508          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1509    
# Line 1274  Line 1526 
1526                          stop_prediction_timer();                          stop_prediction_timer();
1527    
1528                          start_timer();                          start_timer();
1529                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1530                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1531                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1532                                    qcoeff[5*64+0]=0;
1533                            }
1534                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1535                          stop_coding_timer();                          stop_coding_timer();
1536                  }                  }
# Line 1295  Line 1552 
1552    
1553    
1554  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1555    #define BFRAME_SKIP_THRESHHOLD 30
1556    
1557  static int  static int
1558  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1309  Line 1567 
1567          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1568    
1569          int iLimit;          int iLimit;
1570          uint32_t x, y;          int x, y, k;
1571          int iSearchRange;          int iSearchRange;
1572          int bIntra;          int bIntra;
1573    
# Line 1318  Line 1576 
1576    
1577          start_timer();          start_timer();
1578          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1579                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1580          stop_edges_timer();          stop_edges_timer();
1581    
1582          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 1345  Line 1602 
1602          start_timer();          start_timer();
1603          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1604                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1605                    if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1606                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1607    
1608          } else {          } else {
1609    
1610                  bIntra =                  bIntra =
1611                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1612                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1613                                                           iLimit);                                                           iLimit);
1614    
1615          }          }
1616          stop_motion_timer();          stop_motion_timer();
1617    
1618          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1619    
1620          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1621    
1622          if (vol_header)          if (vol_header)
1623                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1624    
1625          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1626            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1627            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1628    
1629          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1630    
1631          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1632          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1633    
1634          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1403  Line 1663 
1663    
1664                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1665    
1666                                    if (pMB->mode != MODE_NOT_CODED)
1667                                  pMB->cbp =                                  pMB->cbp =
1668                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1669                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1427  Line 1688 
1688                          }                          }
1689    
1690                          start_timer();                          start_timer();
1691    
1692                            /* Finished processing the MB, now check if to CODE or SKIP */
1693    
1694                            if ((pMB->mode == MODE_NOT_CODED) ||
1695                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1696                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1697    
1698    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1699    
1700                                    int bSkip = 1;
1701                                    pMB->mode = MODE_NOT_CODED;
1702    
1703                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1704                                    {
1705                                            int iSAD;
1706                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1707                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1708                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1709                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1710                                            {       bSkip = 0;
1711                                                    break;
1712                                            }
1713                                    }
1714                                    if (!bSkip)
1715                                    {
1716                                            VECTOR predMV;
1717                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1718                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1719                                            pMB->mode = MODE_INTER;
1720                                            pMB->cbp = 0;
1721                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1722                                    }
1723                                    else MBSkip(bs);
1724    
1725                            } else {
1726                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1727                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1728                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1729                                            qcoeff[5*64+0]=0;
1730                                    }
1731                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1732                            }
1733    
1734                          stop_coding_timer();                          stop_coding_timer();
1735                  }                  }
1736          }          }
# Line 1461  Line 1764 
1764    
1765          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1766    
1767    #ifdef FRAMEDROP
1768            /* frame drop code */
1769            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1770            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1771                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1772            {
1773                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1774                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1775    
1776                    BitstreamReset(bs);
1777    
1778                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1779                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1780    
1781                    // copy reference frame details into the current frame
1782                    pEnc->current->quant = pEnc->reference->quant;
1783                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1784                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1785                    pEnc->current->fcode = pEnc->reference->fcode;
1786                    pEnc->current->bcode = pEnc->reference->bcode;
1787                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1788                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1789    
1790            }
1791    #endif
1792    
1793          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1794    
1795          return 0;                                       // inter          return 0;                                       // inter
1796  }  }
1797    
1798    
1799  #ifdef BFRAMES  static __inline void
 static void  
1800  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1801                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1802                     Bitstream * bs,                     Bitstream * bs,
# Line 1477  Line 1805 
1805          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1806          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1807          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1808    
1809          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1810          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1811    
1812    #ifdef BFRAMES_DEC_DEBUG
1813            FILE *fp;
1814            static char first=0;
1815    #define BFRAME_DEBUG    if (!first && fp){ \
1816                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1817            }
1818    
1819            if (!first){
1820                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1821            }
1822    #endif
1823    
1824          // forward          // forward
1825          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1826                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1827                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1828          start_timer();          start_timer();
1829          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,
1830                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1497  Line 1834 
1834          // backward          // backward
1835          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1836                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1837                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1838          start_timer();          start_timer();
1839          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1840                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1506  Line 1842 
1842          stop_inter_timer();          stop_inter_timer();
1843    
1844          start_timer();          start_timer();
1845          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,  
1846            MotionEstimationBVOP(&pEnc->mbParam, frame,
1847                    ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1848                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1849                            pEnc->reference->mbs, f_ref,
1850                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1851                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1852                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1520  Line 1860 
1860             } */             } */
1861    
1862          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1863          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
1864            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1865            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1866    
1867          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1868    
# Line 1531  Line 1873 
1873    
1874    
1875          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;  
   
1876                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1877                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1878                                  &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];  
1879    
1880                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1881                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1882                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
1883                                  continue;                                  continue;
1884                          }                          }
1885    
1886                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1887                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1888                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1889                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1890                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1891                                                                           dct_codes);                                                                           dct_codes);
1892    
1893                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1894                          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);  
   
1895    
1896                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  mb->cbp =
1897                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mode = 5;   // skipped  
                         }  
1898    
1899                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1900                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1901                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
1902                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {  
                                 mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;  
                                 mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
1903                          }                          }
 //          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);  
1904    
1905    #ifdef BFRAMES_DEC_DEBUG
1906            BFRAME_DEBUG
1907    #endif
1908                          start_timer();                          start_timer();
1909                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1910                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1911                          stop_coding_timer();                          stop_coding_timer();
1912                  }                  }
1913          }          }
# Line 1597  Line 1917 
1917          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1918    
1919          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1920    
1921    #ifdef BFRAMES_DEC_DEBUG
1922            if (!first){
1923                    first=1;
1924                    if (fp)
1925                            fclose(fp);
1926  }  }
1927  #endif  #endif
1928    }
1929    
1930    
1931    /*      in case internal output is needed somewhere... */
1932    /*      {
1933            FILE *filehandle;
1934            filehandle=fopen("last-b.pgm","wb");
1935            if (filehandle)
1936            {
1937                    fprintf(filehandle,"P5\n\n");           //
1938                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1939                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1940                    fclose(filehandle);
1941                    }
1942            }
1943    */

Legend:
Removed from v.1.42  
changed lines
  Added in v.1.76.2.6

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