[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.41, Wed Jun 12 20:38:40 2002 UTC revision 1.76.2.2, Thu Sep 26 01:54:54 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>
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->queue = NULL;
385    
386    
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    
414          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
415          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
416  #endif          pEnc->m_framenum = 0;
417            pEnc->last_pframe = 0;
418            pEnc->last_sync = 0;
419    
420          pParam->handle = (void *) pEnc;          pParam->handle = (void *) pEnc;
421    
# Line 388  Line 434 
434          /*          /*
435           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
436           */           */
437  #ifdef BFRAMES  
438      xvid_err_memory5:
439    
440    
441            if (pEnc->mbParam.max_bframes > 0) {
442    
443                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
444                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
445                                                      pEnc->mbParam.edged_height);
446                    }
447                    xvid_free(pEnc->queue);
448            }
449    
450    xvid_err_memory4:    xvid_err_memory4:
451    
452            if (pEnc->mbParam.max_bframes > 0) {
453    
454          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
455    
456                  if (pEnc->bframes[i] == NULL)                  if (pEnc->bframes[i] == NULL)
# Line 405  Line 466 
466          }          }
467    
468          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
469            }
 #endif  
470    
471    xvid_err_memory3:    xvid_err_memory3:
472  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
473          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
474                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
475  #endif  #endif
476    
 #ifdef BFRAMES  
477          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
478                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
479          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
480                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
481          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
482                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
 #endif  
483    
484          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
485                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
# Line 466  Line 524 
524  int  int
525  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
526  {  {
527            int i;
528    
529          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
530    
531          /* B Frames specific */          /* B Frames specific */
532  #ifdef BFRAMES          if (pEnc->mbParam.max_bframes > 0) {
533          int i;  
534                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
535    
536                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
537                                              pEnc->mbParam.edged_height);
538                    }
539                    xvid_free(pEnc->queue);
540            }
541    
542    
543            if (pEnc->mbParam.max_bframes > 0) {
544    
545          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
546    
# Line 483  Line 553 
553                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
554    
555                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
556          }          }
557    
558          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
559    
560  #endif          }
561    
562          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
563    
# Line 506  Line 575 
575                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
576          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
577                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
578  #ifdef BFRAMES  
579          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
580                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
581          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
582                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
583          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
584                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
585  #endif  
586  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
587          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
588                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
589  #endif  #endif
# Line 532  Line 601 
601          return XVID_ERR_OK;          return XVID_ERR_OK;
602  }  }
603    
604    
605    static __inline void inc_frame_num(Encoder * pEnc)
606    {
607            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
608    
609            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
610            if (pEnc->mbParam.m_ticks < pEnc->last_sync)
611                    pEnc->mbParam.m_seconds = 1;
612                                    // more than 1 second since last I or P is not supported.
613            else
614                    pEnc->mbParam.m_seconds = 0;
615    
616    }
617    
618    
619    static __inline void
620    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
621    {
622            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
623            {
624                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
625                    return;
626            }
627    
628            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
629                                    pEnc->bframenum_head, pEnc->bframenum_tail,
630                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
631    
632    
633            start_timer();
634            if (image_input
635                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
636                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
637                    return;
638            stop_conv_timer();
639    
640            pEnc->queue_size++;
641            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
642    }
643    
644    
645    
646  /*****************************************************************************  /*****************************************************************************
647   * 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.  
648   *   *
649   * Returned values :   * Returned values :
650   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 544  Line 652 
652   *                        format   *                        format
653   ****************************************************************************/   ****************************************************************************/
654    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
655  int  int
656  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
657                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
658                             XVID_ENC_STATS * pResult)                             XVID_ENC_STATS * pResult)
659  {  {
# Line 558  Line 661 
661          Bitstream bs;          Bitstream bs;
662          uint32_t bits;          uint32_t bits;
663    
664  #ifdef _DEBUG          int input_valid = 1;
665    
666    #ifdef _DEBUG_PSNR
667          float psnr;          float psnr;
668          char temp[128];          char temp[128];
669  #endif  #endif
670    
671          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
672          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
673            ENC_CHECK(pFrame->image);
674    
675          start_global_timer();          start_global_timer();
676    
677          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
678    
679    ipvop_loop:
680    
681          /*          /*
682           * bframe "flush" code           * bframe "flush" code
683           */           */
# Line 584  Line 692 
692                           * frame as a pframe                           * frame as a pframe
693                           */                           */
694    
695                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
696                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
697                             dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
698                           */  
699                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
700                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
701    
# Line 597  Line 705 
705    
706                          BitstreamPad(&bs);                          BitstreamPad(&bs);
707                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
708                          pFrame->intra = 0;                          pFrame->intra = 0;
709    
710                          return XVID_ERR_OK;                          return XVID_ERR_OK;
711                  }                  }
712    
713                  /* ToDo : remove dprintf calls */  
714                  /*                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
715                     dprintf("--- BFRAME (flush) --- ");                                  pEnc->bframenum_head, pEnc->bframenum_tail,
716                   */                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
717    
718                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);                  FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
719                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
720    
   
721                  BitstreamPad(&bs);                  BitstreamPad(&bs);
722                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
723                  pFrame->intra = 0;                  pFrame->intra = 0;
724    
725                    if (input_valid)
726                            queue_image(pEnc, pFrame);
727    
728                  return XVID_ERR_OK;                  return XVID_ERR_OK;
729          }          }
730    
731          if (pFrame->image == NULL) {          if (pEnc->bframenum_head > 0) {
732                  pFrame->length = 0;                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
733                  pFrame->input_consumed = 1;  
734                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
735    
736                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
737                                    pEnc->bframenum_head, pEnc->bframenum_tail,
738                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
739    
740                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
741                            BitstreamPad(&bs);
742                            BitstreamPutBits(&bs, 0x7f, 8);
743    
744                            pFrame->length = BitstreamLength(&bs);
745                  pFrame->intra = 0;                  pFrame->intra = 0;
746    
747                            if (input_valid)
748                                    queue_image(pEnc, pFrame);
749    
750                  return XVID_ERR_OK;                  return XVID_ERR_OK;
751          }          }
752            }
753    
754          if (pEnc->bframenum_head > 0) {  
755                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  bvop_loop:
756    
757            if (pEnc->bframenum_dx50bvop != -1)
758            {
759    
760                    SWAP(pEnc->current, pEnc->reference);
761                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
762    
763                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
764                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
765          }          }
766    
767          pEnc->flush_bframes = 0;                  if (input_valid)
768                    {
769                            queue_image(pEnc, pFrame);
770                            input_valid = 0;
771                    }
772    
773          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          } else if (input_valid) {
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
774    
775          SWAP(pEnc->current, pEnc->reference);          SWAP(pEnc->current, pEnc->reference);
776    
777          EMMS();                  start_timer();
778                    if (image_input
779                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
780                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
781                            return XVID_ERR_FORMAT;
782                    stop_conv_timer();
783    
784                    // queue input frame, and dequue next image
785                    if (pEnc->queue_size > 0)
786                    {
787                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
788                            if (pEnc->queue_head != pEnc->queue_tail)
789                            {
790                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
791                            }
792                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
793                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
794                    }
795    
796            } else if (pEnc->queue_size > 0) {
797    
798                    SWAP(pEnc->current, pEnc->reference);
799    
800                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
801                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
802                    pEnc->queue_size--;
803    
804            } else if (BitstreamPos(&bs) == 0) {
805    
806                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
807                                    pEnc->bframenum_head, pEnc->bframenum_tail,
808                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
809    
810                    pFrame->intra = 0;
811    
812                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
813                    BitstreamPad(&bs);
814                    pFrame->length = BitstreamLength(&bs);
815    
816                    return XVID_ERR_OK;
817    
818            } else {
819    
820                    pFrame->length = BitstreamLength(&bs);
821                    return XVID_ERR_OK;
822            }
823    
824            pEnc->flush_bframes = 0;
825    
826            emms();
827    
828            // only inc frame num, adapt quant, etc. if we havent seen it before
829            if (pEnc->bframenum_dx50bvop < 0 )
830            {
831          if (pFrame->quant == 0)          if (pFrame->quant == 0)
832                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
833          else          else
834                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
835    
836          if (pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
837                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
838    
839          if (pEnc->current->quant > 31)          if (pEnc->current->quant > 31)
840                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
841    */
842          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
843          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
844          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
845          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
846          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
847          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
848    
849          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
850          if (image_input                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
851                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,  
852                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))                  inc_frame_num(pEnc);
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
853    
854  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
855          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
856                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
857  #endif  #endif
858    
859          EMMS();                  emms();
860    
861                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
862                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
863                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
864                    }
865    
866          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867           * Luminance masking           * Luminance masking
# Line 704  Line 892 
892                          }                          }
893    
894  #undef OFFSET  #undef OFFSET
   
895                  }                  }
896    
897                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
898          }          }
899    
900            }
901    
902          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
904           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
905    
906    
907          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
908                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
909                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)                   pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
910                  || image_mad(&pEnc->reference->image, &pEnc->current->image,                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
# Line 725  Line 914 
914                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
915                   */                   */
916    
917                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
918                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
919                     dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                  */  
920    
921                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
922                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
923                    }
924    
925                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
926    
927                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
928    
929                            pEnc->bframenum_tail--;
930                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
931    
932                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
933                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
934                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
935                            }
936                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
937    
938                            pFrame->intra = 0;
939    
940                    } else {
941    
942                            FrameCodeI(pEnc, &bs, &bits);
943                  pFrame->intra = 1;                  pFrame->intra = 1;
944    
945                            pEnc->bframenum_dx50bvop = -1;
946                    }
947    
948                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
949    
950                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
951                            BitstreamPad(&bs);
952                            input_valid = 0;
953                            goto ipvop_loop;
954                    }
955    
956                  /*                  /*
957                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
958                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
# Line 744  Line 962 
962                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
963                   */                   */
964    
965                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
966                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
967                     dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
968                   */  
969                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
970                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
971                    }
972    
973                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
974                  pFrame->intra = 0;                  pFrame->intra = 0;
975                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
976    
977                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
978                            BitstreamPad(&bs);
979                            input_valid = 0;
980                            goto ipvop_loop;
981                    }
982    
983          } else {          } else {
984                  /*                  /*
985                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
986                   */                   */
987    
988                  /* ToDo : Remove dprintf calls */                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
989                  /*                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
990                     dprintf("--- BFRAME (store) ---  head=%i tail=%i",                  }
                    pEnc->bframenum_head,  
                    pEnc->bframenum_tail);  
                  */  
991    
992                  if (pFrame->bquant < 1) {                  if (pFrame->bquant < 1) {
993                          pEnc->current->quant =                          pEnc->current->quant =
# Line 771  Line 996 
996                  } else {                  } else {
997                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
998                  }                  }
999                    if (pEnc->current->quant < 1)
1000                            pEnc->current->quant = 1;
1001    
1002                    if (pEnc->current->quant > 31)
1003                            pEnc->current->quant = 31;
1004    
1005    
1006                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1007                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1008                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1009    
1010    
1011    
1012                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1013                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 780  Line 1017 
1017    
1018                  pFrame->intra = 0;                  pFrame->intra = 0;
1019                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1020    
1021                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1022                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {                  goto bvop_loop;
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
1023                  }                  }
1024    
1025                  return XVID_ERR_OK;          pEnc->iFrameNum++;
         }  
1026    
1027          BitstreamPad(&bs);          BitstreamPad(&bs);
1028          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
# Line 802  Line 1035 
1035                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1036          }          }
1037    
1038          EMMS();          emms();
1039    
1040  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1041          psnr =          psnr =
1042                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1043                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 819  Line 1052 
1052                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1053          }          }
1054    
         pEnc->iFrameNum++;  
         pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;  
         if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {  
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
         }  
         pFrame->input_consumed = 1;  
1055    
1056          stop_global_timer();          stop_global_timer();
1057          write_timer();          write_timer();
1058    
1059          return XVID_ERR_OK;          return XVID_ERR_OK;
1060  }  }
1061  #else  
1062    
1063    
1064  /*****************************************************************************  /*****************************************************************************
1065   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1066     *
1067     * Returned values :
1068     *    - XVID_ERR_OK     - no errors
1069     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1070     *                        format
1071   ****************************************************************************/   ****************************************************************************/
1072    
1073  int  int
1074  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1075                             XVID_ENC_FRAME * pFrame,                             XVID_ENC_FRAME * pFrame,
# Line 846  Line 1080 
1080          uint32_t bits;          uint32_t bits;
1081          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1082    
1083  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1084          float psnr;          float psnr;
1085          uint8_t temp[128];          uint8_t temp[128];
1086  #endif  #endif
# Line 862  Line 1096 
1096    
1097          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1098          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1099            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1100            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1101          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1102    
1103            /* disable alternate scan flag if interlacing is not enabled */
1104            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1105                    !(pEnc->current->global_flags & XVID_INTERLACING))
1106            {
1107                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1108            }
1109    
1110          start_timer();          start_timer();
1111          if (image_input          if (image_input
1112                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
# Line 871  Line 1114 
1114                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1115          stop_conv_timer();          stop_conv_timer();
1116    
1117  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1118          image_copy(&pEnc->sOriginal, &pEnc->current->image,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1119                             pEnc->mbParam.edged_width, pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
1120  #endif  #endif
1121    
1122          EMMS();          emms();
1123    
1124          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1125    
# Line 975  Line 1218 
1218                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1219          }          }
1220    
1221          EMMS();          emms();
1222    
1223          if (pFrame->quant == 0) {          if (pFrame->quant == 0) {
1224                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1225                                                    pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1226          }          }
1227  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1228          psnr =          psnr =
1229                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1230                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
# Line 991  Line 1234 
1234          DEBUG(temp);          DEBUG(temp);
1235  #endif  #endif
1236    
1237            inc_frame_num(pEnc);
1238          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1239    
1240          stop_global_timer();          stop_global_timer();
# Line 998  Line 1242 
1242    
1243          return XVID_ERR_OK;          return XVID_ERR_OK;
1244  }  }
 #endif  
1245    
1246    
1247  static __inline void  static __inline void
# Line 1068  Line 1311 
1311                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1312                          MVBLOCKHINT *bhint =                          MVBLOCKHINT *bhint =
1313                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1314                          VECTOR pred[4];                          VECTOR pred;
1315                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1316                          int vec;                          int vec;
1317    
1318                          pMB->mode =                          pMB->mode =
# Line 1090  Line 1332 
1332                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                  tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1333                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                  tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1334    
1335                                  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);  
1336    
1337                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
1338                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1339                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1340                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1341                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1342                                  }                                  }
1343                          } else if (pMB->mode == MODE_INTER4V) {                          } else if (pMB->mode == MODE_INTER4V) {
1344                                  for (vec = 0; vec < 4; ++vec) {                                  for (vec = 0; vec < 4; ++vec) {
# Line 1110  Line 1351 
1351                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;                                          tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1352                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;                                          tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1353    
1354                                          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);  
1355    
1356                                          pMB->mvs[vec].x = tmp.x;                                          pMB->mvs[vec].x = tmp.x;
1357                                          pMB->mvs[vec].y = tmp.y;                                          pMB->mvs[vec].y = tmp.y;
1358                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1359                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1360                                  }                                  }
1361                          } else                          // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
1362                          {                          {
# Line 1246  Line 1486 
1486          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1487    
1488          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1489          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1490    #define DIVX501B481P "DivX501b481p"
1491            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1492                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1493            }
1494            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1495    
1496          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1497    
# Line 1269  Line 1514 
1514                          stop_prediction_timer();                          stop_prediction_timer();
1515    
1516                          start_timer();                          start_timer();
1517                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1518                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1519                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1520                                    qcoeff[5*64+0]=0;
1521                            }
1522                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1523                          stop_coding_timer();                          stop_coding_timer();
1524                  }                  }
# Line 1281  Line 1531 
1531          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1532          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1533    
1534            pEnc->last_pframe = pEnc->current->ticks;
1535            pEnc->last_sync = pEnc->current->ticks;
1536    
1537          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1538                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1539          }          }
# Line 1290  Line 1543 
1543    
1544    
1545  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1546    #define BFRAME_SKIP_THRESHHOLD 30
1547    
1548  static int  static int
1549  FrameCodeP(Encoder * pEnc,  FrameCodeP(Encoder * pEnc,
# Line 1304  Line 1558 
1558          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1559    
1560          int iLimit;          int iLimit;
1561          uint32_t x, y;          int x, y, k;
1562          int iSearchRange;          int iSearchRange;
1563          int bIntra;          int bIntra;
1564    
# Line 1313  Line 1567 
1567    
1568          start_timer();          start_timer();
1569          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1570                                     pEnc->mbParam.width, pEnc->mbParam.height,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                                    pEnc->current->global_flags & XVID_INTERLACING);  
1571          stop_edges_timer();          stop_edges_timer();
1572    
1573          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 1593 
1593          start_timer();          start_timer();
1594          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1595                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1596                    if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1597                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1598    
1599          } else {          } else {
1600    
1601                  bIntra =                  bIntra =
1602                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1603                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1604                                                           iLimit);                                                           iLimit);
1605    
1606          }          }
1607          stop_motion_timer();          stop_motion_timer();
1608    
1609          if (bIntra == 1) {          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1610    
1611          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1612    
1613          if (vol_header)          if (vol_header)
1614                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1615    
1616          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1617    
1618          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1619    
1620          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1621          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1622    
1623          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
# Line 1398  Line 1652 
1652    
1653                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1654    
1655                                    if (pMB->mode != MODE_NOT_CODED)
1656                                  pMB->cbp =                                  pMB->cbp =
1657                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1658                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
# Line 1422  Line 1677 
1677                          }                          }
1678    
1679                          start_timer();                          start_timer();
1680    
1681                            /* Finished processing the MB, now check if to CODE or SKIP */
1682    
1683                            if ((pMB->mode == MODE_NOT_CODED) ||
1684                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1685                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1686    
1687    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1688    
1689                                    int bSkip = 1;
1690                                    pMB->mode = MODE_NOT_CODED;
1691    
1692                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1693                                    {
1694                                            int iSAD;
1695                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1696                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1697                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1698                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1699                                            {       bSkip = 0;
1700                                                    break;
1701                                            }
1702                                    }
1703                                    if (!bSkip)
1704                                    {
1705                                            VECTOR predMV;
1706                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1707                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1708                                            pMB->mode = MODE_INTER;
1709                                            pMB->cbp = 0;
1710                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1711                                    }
1712                                    else MBSkip(bs);
1713    
1714                            } else {
1715                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1716                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1717                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1718                                            qcoeff[5*64+0]=0;
1719                                    }
1720                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1721                            }
1722    
1723                          stop_coding_timer();                          stop_coding_timer();
1724                  }                  }
1725          }          }
# Line 1456  Line 1753 
1753    
1754          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1755    
1756    #ifdef BFRAMES
1757            /* frame drop code */
1758            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1759            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1760                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1761            {
1762                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1763                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1764    
1765                    BitstreamReset(bs);
1766                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1767    
1768                    // copy reference frame details into the current frame
1769                    pEnc->current->quant = pEnc->reference->quant;
1770                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1771                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1772                    pEnc->current->fcode = pEnc->reference->fcode;
1773                    pEnc->current->bcode = pEnc->reference->bcode;
1774                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1775                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1776    
1777            }
1778    #endif
1779    
1780          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1781    
1782            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1783                            (int32_t)pEnc->mbParam.fbase;
1784            pEnc->last_pframe = pEnc->current->ticks;
1785    
1786          return 0;                                       // inter          return 0;                                       // inter
1787  }  }
1788    
1789    
1790  #ifdef BFRAMES  static __inline void
 static void  
1791  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
1792                     FRAMEINFO * frame,                     FRAMEINFO * frame,
1793                     Bitstream * bs,                     Bitstream * bs,
# Line 1472  Line 1796 
1796          int16_t dct_codes[6 * 64];          int16_t dct_codes[6 * 64];
1797          int16_t qcoeff[6 * 64];          int16_t qcoeff[6 * 64];
1798          uint32_t x, y;          uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1799    
1800          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
1801          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1802    
1803    #ifdef BFRAMES_DEC_DEBUG
1804            FILE *fp;
1805            static char first=0;
1806    #define BFRAME_DEBUG    if (!first && fp){ \
1807                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1808            }
1809    
1810            if (!first){
1811                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1812            }
1813    #endif
1814    
1815          // forward          // forward
1816          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
1817                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1818                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1819          start_timer();          start_timer();
1820          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,
1821                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1492  Line 1825 
1825          // backward          // backward
1826          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
1827                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1828                                     pEnc->mbParam.height,                                     pEnc->mbParam.height);
                                    frame->global_flags & XVID_INTERLACING);  
1829          start_timer();          start_timer();
1830          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1831                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
# Line 1501  Line 1833 
1833          stop_inter_timer();          stop_inter_timer();
1834    
1835          start_timer();          start_timer();
1836          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1837                    ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1838                                                    pEnc->time_pp,
1839                                                    pEnc->reference->mbs, f_ref,
1840                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1841                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1842                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                   &pEnc->vInterV, &pEnc->vInterHV);
# Line 1515  Line 1850 
1850             } */             } */
1851    
1852          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
1853          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1854    
1855          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1856    
# Line 1526  Line 1861 
1861    
1862    
1863          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;  
   
1864                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1865                          MACROBLOCK *f_mb =                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1866                                  &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];  
1867    
1868                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1869                          if (mb->mode == MODE_NOT_CODED) {                          if (mb->mode == MODE_NOT_CODED) {
1870                                  mb->mvs[0].x = 0;                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].y = 0;  
1871                                  continue;                                  continue;
1872                          }                          }
1873    
1874                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1875                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1876                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1877                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1878                                                                           &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->vInterV, &pEnc->vInterHV,
1879                                                                           dct_codes);                                                                           dct_codes);
1880    
1881                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1882                          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);  
1883    
1884                                    mb->cbp =
1885                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1886    
1887                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1888                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1889                                  mb->mode = 5;   // skipped                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
1890                          }                          }
   
                         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;  
1891                          }                          }
1892    
1893                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {  #ifdef BFRAMES_DEC_DEBUG
1894                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;          BFRAME_DEBUG
1895                                  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);  
   
1896                          start_timer();                          start_timer();
1897                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1898                                                   &pEnc->sStat);                                                   &pEnc->sStat, direction);
1899                          stop_coding_timer();                          stop_coding_timer();
1900                  }                  }
1901          }          }
# Line 1592  Line 1905 
1905          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1906    
1907          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1908    
1909    #ifdef BFRAMES_DEC_DEBUG
1910            if (!first){
1911                    first=1;
1912                    if (fp)
1913                            fclose(fp);
1914  }  }
1915  #endif  #endif
1916    }
1917    
1918    
1919    /*      in case internal output is needed somewhere... */
1920    /*      {
1921            FILE *filehandle;
1922            filehandle=fopen("last-b.pgm","wb");
1923            if (filehandle)
1924            {
1925                    fprintf(filehandle,"P5\n\n");           //
1926                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1927                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1928                    fclose(filehandle);
1929                    }
1930            }
1931    */

Legend:
Removed from v.1.41  
changed lines
  Added in v.1.76.2.2

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