[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.40, Sun Jun 9 23:30:50 2002 UTC revision 1.78, Wed Sep 4 21:41:57 2002 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6     *  Copyright(C) 2002 Michael Militzer
7     *
8   *  This program is an implementation of a part of one or more MPEG-4   *  This program is an implementation of a part of one or more MPEG-4
9   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
10   *  to use this software module in hardware or software products are   *  to use this software module in hardware or software products are
# Line 28  Line 30 
30   *   *
31   ****************************************************************************/   ****************************************************************************/
32    
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
  *  $Id$  
  *  
  ****************************************************************************/  
   
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <stdio.h>  #include <stdio.h>
35  #include <math.h>  #include <math.h>
36    #include <string.h>
37    
38  #include "encoder.h"  #include "encoder.h"
39  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 61  Line 52 
52  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
53  #include "utils/mem_align.h"  #include "utils/mem_align.h"
54    
55    #ifdef _SMP
56    #include "motion/smp_motion_est.h"
57    #endif
58  /*****************************************************************************  /*****************************************************************************
59   * Local macros   * Local macros
60   ****************************************************************************/   ****************************************************************************/
# Line 82  Line 76 
76                        bool force_inter,                        bool force_inter,
77                        bool vol_header);                        bool vol_header);
78    
 #ifdef BFRAMES  
 static void FrameCodeB(Encoder * pEnc,  
                        FRAMEINFO * frame,  
                        Bitstream * bs,  
                        uint32_t *pBits);  
 #endif  
   
79  /*****************************************************************************  /*****************************************************************************
80   * Local data   * Local data
81   ****************************************************************************/   ****************************************************************************/
82    
83  static int DQtab[4] =  static int DQtab[4] = {
 {  
84          -1, -2, 1, 2          -1, -2, 1, 2
85  };  };
86    
87  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
88          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
89  };  };
90    
91    
92  static void __inline image_null(IMAGE * image)  static void __inline
93    image_null(IMAGE * image)
94  {  {
95          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
96  }  }
# Line 132  Line 118 
118  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
119  {  {
120          Encoder *pEnc;          Encoder *pEnc;
121          uint32_t i;          int i;
122    
123          pParam->handle = NULL;          pParam->handle = NULL;
124    
# Line 145  Line 131 
131    
132          /* Fps */          /* Fps */
133    
134          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
135                  pParam->fincr = 1;                  pParam->fincr = 1;
136                  pParam->fbase = 25;                  pParam->fbase = 25;
137          }          }
# Line 157  Line 142 
142           */           */
143    
144          i = pParam->fincr;          i = pParam->fincr;
145          while (i > 1)          while (i > 1) {
146          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
147                          pParam->fincr /= i;                          pParam->fincr /= i;
148                          pParam->fbase /= i;                          pParam->fbase /= i;
149                          i = pParam->fincr;                          i = pParam->fincr;
# Line 169  Line 152 
152                  i--;                  i--;
153          }          }
154    
155          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
156                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
157    
158                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
159                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
160          }          }
# Line 203  Line 186 
186    
187          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
188    
189          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
190                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
191    
   
192          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
193          if (pEnc == NULL)          if (pEnc == NULL)
194                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
195    
196            /* Zero the Encoder Structure */
197    
198            memset(pEnc, 0, sizeof(Encoder));
199    
200          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
201    
202          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 225  Line 211 
211          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
212          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
213    
214            pEnc->mbParam.m_quant_type = H263_QUANT;
215    
216    #ifdef _SMP
217            pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);
218    #endif
219    
220          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
221    
222          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 244  Line 236 
236    
237          /* try to allocate mb memory */          /* try to allocate mb memory */
238    
239          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
240                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
241                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
242                                           CACHE_LINE);          pEnc->reference->mbs =
243          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
244                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
245    
246          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
247                  goto xvid_err_memory2;                  goto xvid_err_memory2;
248    
249          /* try to allocate image memory */          /* try to allocate image memory */
250    
251  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
252          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
253  #endif  #endif
 #ifdef BFRAMES  
         image_null(&pEnc->f_refh);  
         image_null(&pEnc->f_refv);  
         image_null(&pEnc->f_refhv);  
 #endif  
254          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
255          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
256          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 274  Line 259 
259          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
260          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
261    
262  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
263          if (image_create(&pEnc->sOriginal,          if (image_create
264                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
265                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
266                  goto xvid_err_memory3;                  goto xvid_err_memory3;
267  #endif  #endif
268  #ifdef BFRAMES          if (image_create
269          if (image_create(&pEnc->f_refh,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
                          pEnc->mbParam.edged_width,  
270                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
271                  goto xvid_err_memory3;                  goto xvid_err_memory3;
272          if (image_create(&pEnc->f_refv,          if (image_create
273                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
274                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
275                  goto xvid_err_memory3;                  goto xvid_err_memory3;
276          if (image_create(&pEnc->f_refhv,          if (image_create
277                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
278                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
279                  goto xvid_err_memory3;                  goto xvid_err_memory3;
280  #endif          if (image_create
281          if (image_create(&pEnc->current->image,                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
                          pEnc->mbParam.edged_width,  
282                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
283                  goto xvid_err_memory3;                  goto xvid_err_memory3;
284          if (image_create(&pEnc->reference->image,          if (image_create
285                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, 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          if (image_create(&pEnc->vInterH,          if (image_create
289                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
290                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
291                  goto xvid_err_memory3;                  goto xvid_err_memory3;
292          if (image_create(&pEnc->vInterV,          if (image_create
293                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
294                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
295                  goto xvid_err_memory3;                  goto xvid_err_memory3;
         if (image_create(&pEnc->vInterVf,  
                          pEnc->mbParam.edged_width,  
                          pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create(&pEnc->vInterHV,  
                          pEnc->mbParam.edged_width,  
                          pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create(&pEnc->vInterHVf,  
                          pEnc->mbParam.edged_width,  
                          pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
   
   
   
         /* B Frames specific init */  
 #ifdef BFRAMES  
   
         pEnc->mbParam.max_bframes = pParam->max_bframes;  
         pEnc->bquant_ratio = pParam->bquant_ratio;  
         pEnc->bframes = NULL;  
   
         if (pEnc->mbParam.max_bframes > 0)  
         {  
                 int n;  
   
                 pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \  
                                             sizeof(FRAMEINFO *),  
                                             CACHE_LINE);  
   
                 if (pEnc->bframes == NULL)  
                         goto xvid_err_memory3;  
   
                 for (n=0; n<pEnc->mbParam.max_bframes; n++)  
                         pEnc->bframes[n] = NULL;  
   
   
                 for (n = 0; n < pEnc->mbParam.max_bframes; n++)  
                 {  
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
   
                         if (pEnc->bframes[n] == NULL)  
                                 goto xvid_err_memory4;  
   
                         pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \  
                                                             pEnc->mbParam.mb_width * \  
                                                             pEnc->mbParam.mb_height,  
                                                             CACHE_LINE);  
   
                         if (pEnc->bframes[n]->mbs == NULL)  
                                 goto xvid_err_memory4;  
   
                         image_null(&pEnc->bframes[n]->image);  
   
                         if (image_create(&pEnc->bframes[n]->image,  
                                          pEnc->mbParam.edged_width,  
                                          pEnc->mbParam.edged_height) < 0)  
                                 goto xvid_err_memory4;  
   
                 }  
         }  
   
         pEnc->bframenum_head = 0;  
         pEnc->bframenum_tail = 0;  
         pEnc->flush_bframes = 0;  
   
         pEnc->mbParam.m_seconds = 0;  
         pEnc->mbParam.m_ticks = 0;  
 #endif  
296    
297          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
298    
299          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
300          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
301                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
302                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
303                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
304                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
305          }          }
306    
307          init_timer();          init_timer();
# Line 402  Line 311 
311          /*          /*
312           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
313           */           */
 #ifdef BFRAMES  
  xvid_err_memory4:  
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
   
                 if (pEnc->bframes[i] == NULL) continue;  
   
                 image_destroy(&pEnc->bframes[i]->image,  
                               pEnc->mbParam.edged_width,  
                               pEnc->mbParam.edged_height);  
   
                 xvid_free(pEnc->bframes[i]->mbs);  
   
                 xvid_free(pEnc->bframes[i]);  
   
         }  
   
         xvid_free(pEnc->bframes);  
   
 #endif  
314    
315   xvid_err_memory3:   xvid_err_memory3:
316  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
317          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
318                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
319  #endif  #endif
320    
321  #ifdef BFRAMES          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
         image_destroy(&pEnc->f_refh,  
                       pEnc->mbParam.edged_width,  
322                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
323          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
324                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
325          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
326                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
327  #endif          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
   
         image_destroy(&pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
328                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
329          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
330                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
331          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
332                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
333          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterVf,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHV,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHVf,  
                       pEnc->mbParam.edged_width,  
334                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
335    
336   xvid_err_memory2:   xvid_err_memory2:
# Line 492  Line 361 
361  int  int
362  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
363  {  {
         ENC_CHECK(pEnc);  
   
         /* B Frames specific */  
 #ifdef BFRAMES  
         int i;  
   
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
364    
365                  if (pEnc->bframes[i] == NULL) continue;          ENC_CHECK(pEnc);
   
                 image_destroy(&pEnc->bframes[i]->image,  
                               pEnc->mbParam.edged_width,  
                               pEnc->mbParam.edged_height);  
   
                 xvid_free(pEnc->bframes[i]->mbs);  
   
                 xvid_free(pEnc->bframes[i]);  
   
         }  
   
         xvid_free(pEnc->bframes);  
   
 #endif  
366    
367          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
368            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
         image_destroy(&pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->reference->image,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterH,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterV,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterVf,  
                       pEnc->mbParam.edged_width,  
369                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
370          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
371                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
372          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
373                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
374  #ifdef BFRAMES          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
         image_destroy(&pEnc->f_refh,  
                       pEnc->mbParam.edged_width,  
375                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
376          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
377                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
378          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
379                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
380  #endif          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
 #ifdef _DEBUG  
         image_destroy(&pEnc->sOriginal,  
                       pEnc->mbParam.edged_width,  
381                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
 #endif  
   
         /* Encoder structure */  
   
         xvid_free(pEnc->current->mbs);  
         xvid_free(pEnc->current);  
   
         xvid_free(pEnc->reference->mbs);  
         xvid_free(pEnc->reference);  
   
         xvid_free(pEnc);  
   
         return XVID_ERR_OK;  
 }  
   
 /*****************************************************************************  
  * Frame encoder entry point  
  *  
  * At this moment 2 versions coexist : one for IPB compatible encoder,  
  *                                     another one for the old IP encoder.  
  *  
  * Returned values :  
  *    - XVID_ERR_OK     - no errors  
  *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong  
  *                        format  
  ****************************************************************************/  
   
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
 int  
 encoder_encode(Encoder * pEnc,  
                XVID_ENC_FRAME * pFrame,  
                XVID_ENC_STATS * pResult)  
 {  
         uint16_t x, y;  
         Bitstream bs;  
         uint32_t bits;  
   
 #ifdef _DEBUG  
         float psnr;  
         char temp[128];  
 #endif  
   
         ENC_CHECK(pEnc);  
         ENC_CHECK(pFrame);  
   
         start_global_timer();  
   
         BitstreamInit(&bs, pFrame->bitstream, 0);  
   
         /*  
          * bframe "flush" code  
          */  
   
         if ( (pFrame->image == NULL || pEnc->flush_bframes) &&  
              (pEnc->bframenum_head < pEnc->bframenum_tail))  
         {  
   
                 if (pEnc->flush_bframes == 0)  
                 {  
                         /*  
                          * we have reached the end of stream without getting  
                          * a future reference frame... so encode last final  
                          * frame as a pframe  
                          */  
   
                         /* ToDo : remove dprintf calls */  
                         /*  
                           dprintf("--- PFRAME (final frame correction) --- ");  
                         */  
                         pEnc->bframenum_tail--;  
                         SWAP(pEnc->current, pEnc->reference);  
   
                         SWAP(pEnc->current,  
                              pEnc->bframes[pEnc->bframenum_tail]);  
   
                         FrameCodeP(pEnc, &bs, &bits, 1, 0);  
   
                         BitstreamPad(&bs);  
                         pFrame->length = BitstreamLength(&bs);  
                         pFrame->input_consumed = 0;  
                         pFrame->intra = 0;  
   
                         return XVID_ERR_OK;  
                 }  
   
                 /* ToDo : remove dprintf calls */  
                 /*  
                   dprintf("--- BFRAME (flush) --- ");  
                 */  
                 FrameCodeB(pEnc,  
                            pEnc->bframes[pEnc->bframenum_head],  
                            &bs,  
                            &bits);  
                 pEnc->bframenum_head++;  
   
   
                 BitstreamPad(&bs);  
                 pFrame->length = BitstreamLength(&bs);  
                 pFrame->input_consumed = 0;  
                 pFrame->intra = 0;  
   
                 return XVID_ERR_OK;  
         }  
   
         if (pFrame->image == NULL)  
         {  
                 pFrame->length = 0;  
                 pFrame->input_consumed = 1;  
                 pFrame->intra = 0;  
   
                 return XVID_ERR_OK;  
         }  
   
         if (pEnc->bframenum_head > 0)  
         {  
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
         }  
   
         pEnc->flush_bframes = 0;  
   
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
         SWAP(pEnc->current, pEnc->reference);  
   
         EMMS();  
   
         if (pFrame->quant == 0)  
                 pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
         else  
                 pEnc->current->quant = pFrame->quant;  
   
         if(pEnc->current->quant < 1)  
                 pEnc->current->quant = 1;  
   
         if(pEnc->current->quant > 31)  
                 pEnc->current->quant = 31;  
   
         pEnc->current->global_flags = pFrame->general;  
         pEnc->current->motion_flags = pFrame->motion;  
         pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
         /* ToDo : dynamic fcode (in both directions) */  
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
         pEnc->current->bcode = pEnc->mbParam.m_fcode;  
   
         start_timer();  
         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();  
   
 #ifdef _DEBUG  
         image_copy(&pEnc->sOriginal,  
                    &pEnc->current->image,  
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
 #endif  
   
         EMMS();  
   
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * Luminance masking  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
         if ((pEnc->current->global_flags & XVID_LUMIMASKING))  
         {  
                 int *temp_dquants =  
                         (int *) xvid_malloc(pEnc->mbParam.mb_width * \  
                                             pEnc->mbParam.mb_height * \  
                                             sizeof(int),  
                                             CACHE_LINE);  
   
                 pEnc->current->quant =  
                         adaptive_quantization(pEnc->current->image.y,  
                                               pEnc->mbParam.edged_width,  
                                               temp_dquants,  
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
                                               2*pEnc->current->quant,  
                                               pEnc->mbParam.mb_width,  
                                               pEnc->mbParam.mb_height);  
   
                 for (y = 0; y < pEnc->mbParam.mb_height; y++)  
                 {  
   
                         #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)  
   
                         for (x = 0; x < pEnc->mbParam.mb_width; x++)  
                         {  
                                 MACROBLOCK *pMB =  
                                         &pEnc->current->mbs[OFFSET(x,y)];  
                                 pMB->dquant =  
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
                         }  
   
                         #undef OFFSET  
   
                 }  
   
                 xvid_free(temp_dquants);  
         }  
   
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * ivop/pvop/bvop selection  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
   
   
         if (pEnc->iFrameNum == 0 ||  
             pFrame->intra   == 1 ||  
   
             (pFrame->intra < 0  &&  
              pEnc->iMaxKeyInterval > 0 &&  
              pEnc->iFrameNum >= pEnc->iMaxKeyInterval) ||  
   
             image_mad(&pEnc->reference->image,  
                       &pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.width,  
                       pEnc->mbParam.height) > 30)  
         {  
                 /*  
                  * This will be coded as an Intra Frame  
                  */  
   
                 /* ToDo : Remove dprintf calls */  
                 /*  
                   dprintf("--- IFRAME ---");  
                 */  
   
                 FrameCodeI(pEnc, &bs, &bits);  
   
                 pFrame->intra = 1;  
                 pEnc->flush_bframes = 1;  
   
                 /*  
                  * NB : sequences like "IIBB" decode fine with msfdam but,  
                  *      go screwy with divx 5.00  
                  */  
         }  
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
                 /*  
                  * This will be coded as a Predicted Frame  
                  */  
   
                 /* ToDo : Remove dprintf calls */  
                 /*  
                   dprintf("--- PFRAME ---");  
                 */  
   
                 FrameCodeP(pEnc, &bs, &bits, 1, 0);  
                 pFrame->intra = 0;  
                 pEnc->flush_bframes = 1;  
         }  
         else  
         {  
                 /*  
                  * This will be coded as a Bidirectional Frame  
                  */  
   
                 /* ToDo : Remove dprintf calls */  
                 /*  
                   dprintf("--- BFRAME (store) ---  head=%i tail=%i",  
                   pEnc->bframenum_head,  
                   pEnc->bframenum_tail);  
                 */  
   
                 if (pFrame->bquant < 1)  
                 {  
                         pEnc->current->quant =  
                                 ((pEnc->reference->quant+pEnc->current->quant)*\  
                                  pEnc->bquant_ratio) / 200;  
                 }  
                 else  
                 {  
                         pEnc->current->quant = pFrame->bquant;  
                 }  
382    
383                  /* store frame into bframe buffer & swap ref back to current */  #ifdef _DEBUG_PSNR
384                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
385                  SWAP(pEnc->current, pEnc->reference);                                    pEnc->mbParam.edged_height);
386    #endif
387    
388                  pEnc->bframenum_tail++;          /* Encoder structure */
389            xvid_free(pEnc->current->mbs);
390            xvid_free(pEnc->current);
391    
392                  pFrame->intra = 0;          xvid_free(pEnc->reference->mbs);
393                  pFrame->length = 0;          xvid_free(pEnc->reference);
                 pFrame->input_consumed = 1;  
394    
395                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          xvid_free(pEnc);
                 if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)  
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
396    
397                  return XVID_ERR_OK;                  return XVID_ERR_OK;
398          }          }
399    
         BitstreamPad(&bs);  
         pFrame->length = BitstreamLength(&bs);  
   
         if (pResult)  
         {  
                 pResult->quant = pEnc->current->quant;  
                 pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);  
                 pResult->kblks = pEnc->sStat.kblks;  
                 pResult->mblks = pEnc->sStat.mblks;  
                 pResult->ublks = pEnc->sStat.ublks;  
         }  
   
         EMMS();  
   
 #ifdef _DEBUG  
         psnr = image_psnr(&pEnc->sOriginal,  
                           &pEnc->current->image,  
                           pEnc->mbParam.edged_width,  
                           pEnc->mbParam.width,  
                           pEnc->mbParam.height);  
   
         snprintf(temp, 127, "PSNR: %f\n", psnr);  
         DEBUG(temp);  
 #endif  
400    
401          if (pFrame->quant == 0)  void inc_frame_num(Encoder * pEnc)
402          {          {
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
   
         pEnc->iFrameNum++;  
403          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
404          if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)          pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
405          {          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
406          }          }
         pFrame->input_consumed = 1;  
   
         stop_global_timer();  
         write_timer();  
407    
         return XVID_ERR_OK;  
 }  
 #else  
408  /*****************************************************************************  /*****************************************************************************
409   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
410     *
411     * Returned values :
412     *    - XVID_ERR_OK     - no errors
413     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
414     *                        format
415   ****************************************************************************/   ****************************************************************************/
416    
417  int  int
418  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
419                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
# Line 924  Line 423 
423          Bitstream bs;          Bitstream bs;
424          uint32_t bits;          uint32_t bits;
425          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
426  #ifdef _DEBUG  
427    #ifdef _DEBUG_PSNR
428          float psnr;          float psnr;
429          uint8_t temp[128];          uint8_t temp[128];
430  #endif  #endif
# Line 940  Line 440 
440    
441          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
442          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
443            pEnc->current->seconds = pEnc->mbParam.m_seconds;
444            pEnc->current->ticks = pEnc->mbParam.m_ticks;
445          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
446    
447          start_timer();          start_timer();
448          if (image_input(&pEnc->current->image,          if (image_input
449                          pEnc->mbParam.width,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
450                          pEnc->mbParam.height,                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace) < 0)  
451                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
452          stop_conv_timer();          stop_conv_timer();
453    
454  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
455          image_copy(&pEnc->sOriginal,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
456                     &pEnc->current->image,                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
457  #endif  #endif
458    
459          EMMS();          emms();
460    
461          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
462    
463          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
464                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
465          }          } else {
         else  
         {  
466                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
467          }          }
468    
469          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
470          {                  int *temp_dquants =
471                  int * temp_dquants = (int *)                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
472                          xvid_malloc(pEnc->mbParam.mb_width * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                     pEnc->mbParam.mb_height * \  
                                     sizeof(int),  
473                                      CACHE_LINE);                                      CACHE_LINE);
474    
475                  pEnc->current->quant =                  pEnc->current->quant =
476                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
477                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
478                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
479                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
480                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
481                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
482    
483                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
484    
485                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
486    
487                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
                         {  
488    
489    
490                                  MACROBLOCK *pMB =                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
491                                          &pEnc->current->mbs[OFFSET(x,y)];  
492                                  pMB->dquant =                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
493                          }                          }
494    
495                          #undef OFFSET                          #undef OFFSET
# Line 1011  Line 498 
498                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
499          }          }
500    
501          if (pEnc->current->global_flags & XVID_H263QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
         {  
502                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
503                          write_vol_header = 1;                          write_vol_header = 1;
504                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
505          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
         else if (pEnc->current->global_flags & XVID_MPEGQUANT)  
         {  
506                  int matrix1_changed, matrix2_changed;                  int matrix1_changed, matrix2_changed;
507    
508                  matrix1_changed = matrix2_changed = 0;                  matrix1_changed = matrix2_changed = 0;
# Line 1030  Line 514 
514    
515                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
516                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
517                                  matrix1_changed =                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
                                         set_intra_matrix(pFrame->quant_intra_matrix);  
518                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
519                                  matrix2_changed                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
520                                          = set_inter_matrix(pFrame->quant_inter_matrix);                  } else {
                 }  
                 else {  
521                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
522                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
523                  }                  }
# Line 1044  Line 525 
525                          write_vol_header = matrix1_changed | matrix2_changed;                          write_vol_header = matrix1_changed | matrix2_changed;
526          }          }
527    
528          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
529          {                  if ((pEnc->iFrameNum == 0)
530                  if ((pEnc->iFrameNum == 0) ||                          || ((pEnc->iMaxKeyInterval > 0)
531                                    && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
532                      ((pEnc->iMaxKeyInterval > 0) &&                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
533                       (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                  } else {
534                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
535                          pFrame->intra = FrameCodeI(pEnc,                  }
536                                                     &bs,          } else {
537                                                     &bits);                  if (pFrame->intra == 1) {
538                  }                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
539                  else                  } else {
540                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
                         pFrame->intra = FrameCodeP(pEnc,  
                                                    &bs,  
                                                    &bits,  
                                                    0,  
                                                    write_vol_header);  
                 }  
         }  
         else  
         {  
                 if (pFrame->intra == 1)  
                 {  
                         pFrame->intra = FrameCodeI(pEnc,  
                                                    &bs,  
                                                    &bits);  
                 }  
                 else  
                 {  
                         pFrame->intra = FrameCodeP(pEnc,  
                                                    &bs,  
                                                    &bits,  
                                                    1,  
                                                    write_vol_header);  
541                  }                  }
542    
543          }          }
# Line 1088  Line 547 
547          BitstreamPad(&bs);          BitstreamPad(&bs);
548          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
549    
550          if (pResult)          if (pResult) {
         {  
551                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
552                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
553                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 1097  Line 555 
555                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
556          }          }
557    
558          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
559    
560  #ifdef _DEBUG          if (pFrame->quant == 0) {
561          psnr = image_psnr(&pEnc->sOriginal,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
562                            &pEnc->current->image,                                                    pFrame->length, pFrame->intra);
563                            pEnc->mbParam.edged_width,          }
564                            pEnc->mbParam.width,  #ifdef _DEBUG_PSNR
565            psnr =
566                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
567                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
568                            pEnc->mbParam.height);                            pEnc->mbParam.height);
569    
570          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
571          DEBUG(temp);          DEBUG(temp);
572  #endif  #endif
573    
574            inc_frame_num(pEnc);
575          pEnc->iFrameNum++;          pEnc->iFrameNum++;
576    
577          stop_global_timer();          stop_global_timer();
# Line 1125  Line 579 
579    
580          return XVID_ERR_OK;          return XVID_ERR_OK;
581  }  }
 #endif  
582    
583    
584  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
585    CodeIntraMB(Encoder * pEnc,
586                            MACROBLOCK * pMB)
587    {
588    
589          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
590    
# Line 1139  Line 595 
595          pMB->sad16 = 0;          pMB->sad16 = 0;
596    
597          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
598                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
599                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
600                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
601    
602                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
603                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
604                            if (pEnc->current->quant < 1)
605                                    pEnc->current->quant = 1;
606                  }                  }
607          }          }
608    
# Line 1156  Line 613 
613  #define FCODEBITS       3  #define FCODEBITS       3
614  #define MODEBITS        5  #define MODEBITS        5
615    
616  void HintedMESet(Encoder * pEnc, int * intra)  void
617    HintedMESet(Encoder * pEnc,
618                            int *intra)
619  {  {
620          HINTINFO * hint;          HINTINFO * hint;
621          Bitstream bs;          Bitstream bs;
# Line 1165  Line 624 
624    
625          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
626    
627          if (hint->rawhints)          if (hint->rawhints) {
         {  
628                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
629          }          } else {
         else  
         {  
630                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
631                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
632          }          }
633    
634          if (*intra)          if (*intra) {
         {  
635                  return;                  return;
636          }          }
637    
638          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
639                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
640                                                                                                                                     FCODEBITS);
641    
642          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
643          high    = 1 << (length - 1);          high    = 1 << (length - 1);
644    
645          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
646          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
647                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
648                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
649                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
650                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
651                          VECTOR pred[4];                          VECTOR pred;
652                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
653                          int vec;                          int vec;
654    
655                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
656                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
657                                                                                                                                      MODEBITS);
658    
659                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
660                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
661    
662                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
663                          {                                  tmp.x =
664                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
665                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
666                                    tmp.y =
667                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
668                                                                                                                                                      length);
669                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
670                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
671    
672                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
673    
674                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
675                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
676                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
677                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
678                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
                                 }  
679                          }                          }
680                          else if (pMB->mode == MODE_INTER4V)                          } else if (pMB->mode == MODE_INTER4V) {
681                          {                                  for (vec = 0; vec < 4; ++vec) {
682                                  for (vec=0 ; vec<4 ; ++vec)                                          tmp.x =
683                                  {                                                  (hint->rawhints) ? bhint->mvs[vec].
684                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                                  x : BitstreamGetBits(&bs, length);
685                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                          tmp.y =
686                                                    (hint->rawhints) ? bhint->mvs[vec].
687                                                    y : BitstreamGetBits(&bs, length);
688                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
689                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
690    
691                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
692    
693                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
694                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
695                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
696                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
697                                  }                                  }
698                          }                          } else                          // intra / stuffing / not_coded
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
699                                  {                                  {
700                                    for (vec = 0; vec < 4; ++vec) {
701                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
702                                  }                                  }
703                          }                          }
704    
705                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
706                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
707                          {                                  && pMB->dquant != NO_CHANGE) {
708                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
709    
710                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
711                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
712                                  }                                  }
713                          }                          }
# Line 1258  Line 716 
716  }  }
717    
718    
719  void HintedMEGet(Encoder * pEnc, int intra)  void
720    HintedMEGet(Encoder * pEnc,
721                            int intra)
722  {  {
723          HINTINFO * hint;          HINTINFO * hint;
724          Bitstream bs;          Bitstream bs;
# Line 1267  Line 727 
727    
728          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
729    
730          if (hint->rawhints)          if (hint->rawhints) {
         {  
731                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
732          }          } else {
         else  
         {  
733                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
734                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
735          }          }
736    
737          if (intra)          if (intra) {
738          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
739                          BitstreamPad(&bs);                          BitstreamPad(&bs);
740                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
741                  }                  }
# Line 1290  Line 745 
745          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
746          high    = 1 << (length - 1);          high    = 1 << (length - 1);
747    
748          if (hint->rawhints)          if (hint->rawhints) {
         {  
749                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
750          }          } else {
         else  
         {  
751                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
752          }          }
753    
754          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
755          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
756                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
757                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
758                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
759                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
760                          VECTOR tmp;                          VECTOR tmp;
761    
762                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
763                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
764                          }                          } else {
                         else  
                         {  
765                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
766                          }                          }
767    
768                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
769                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
770                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
771                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
772                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
773    
774                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
775                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
776                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
777                                  }                                  } else {
                                 else  
                                 {  
778                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
779                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
780                                  }                                  }
781                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
782                                  int vec;                                  int vec;
783    
784                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
785                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
786                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
787                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
788                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
789    
790                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
791                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
792                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
793                                          }                                          } else {
                                         else  
                                         {  
794                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
795                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
796                                          }                                          }
# Line 1360  Line 799 
799                  }                  }
800          }          }
801    
802          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
803                  BitstreamPad(&bs);                  BitstreamPad(&bs);
804                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
805          }          }
806  }  }
807    
808    
809  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
810    FrameCodeI(Encoder * pEnc,
811                       Bitstream * bs,
812                       uint32_t * pBits)
813  {  {
814    
815          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 1382  Line 823 
823          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
824    
825          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
826          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
827            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
828    
829          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
830    
# Line 1391  Line 833 
833          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
834    
835          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
836                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
837                  {                          MACROBLOCK *pMB =
838                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
839    
840                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
841    
842                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
843                                                              dct_codes, qcoeff);
844    
845                          start_timer();                          start_timer();
846                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
847                          stop_prediction_timer();                          stop_prediction_timer();
848    
849                          start_timer();                          start_timer();
850                            if (pEnc->current->global_flags & XVID_GREYSCALE)
851                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
852                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
853                                    qcoeff[5*64+0]=0;
854                            }
855                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
856                          stop_coding_timer();                          stop_coding_timer();
857                  }                  }
# Line 1416  Line 864 
864          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
865          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
866    
867          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
868                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
869          }          }
870    
# Line 1427  Line 874 
874    
875  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
876    
877  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
878    FrameCodeP(Encoder * pEnc,
879                       Bitstream * bs,
880                       uint32_t * pBits,
881                       bool force_inter,
882                       bool vol_header)
883  {  {
884          float fSigma;          float fSigma;
885    
# Line 1435  Line 887 
887          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
888    
889          int iLimit;          int iLimit;
890          uint32_t x, y;          int x, y, k;
891          int iSearchRange;          int iSearchRange;
892          int bIntra;          int bIntra;
893    
# Line 1443  Line 895 
895          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
896    
897          start_timer();          start_timer();
898          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
899                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height,
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
900                         pEnc->current->global_flags & XVID_INTERLACING);                         pEnc->current->global_flags & XVID_INTERLACING);
901          stop_edges_timer();          stop_edges_timer();
902    
# Line 1456  Line 905 
905          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
906    
907          if (!force_inter)          if (!force_inter)
908                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
909                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
910                                       INTRA_THRESHOLD);
911          else          else
912                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
913    
914          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
915                  start_timer();                  start_timer();
916                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
917                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
918                                                      pEnc->mbParam.edged_height,
919                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
920                  stop_inter_timer();                  stop_inter_timer();
921          }          }
922    
923          start_timer();          start_timer();
924          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
925                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
926          }          } else {
927    
928    #ifdef _SMP
929            if (pEnc->mbParam.num_threads > 1)
930                    bIntra =
931                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
932                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
933                                                     iLimit);
934          else          else
935          {  #endif
936                  bIntra = MotionEstimation(                  bIntra =
937                          &pEnc->mbParam,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
938                          pEnc->current,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
939                          iLimit);                          iLimit);
940    
941          }          }
942          stop_motion_timer();          stop_motion_timer();
943    
944          if (bIntra == 1)          if (bIntra == 1) {
         {  
945                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
946          }          }
947    
# Line 1496  Line 950 
950          if(vol_header)          if(vol_header)
951                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
952    
953          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
954    
955          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
956    
# Line 1505  Line 959 
959          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
960          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
961    
962          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
963          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
964                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
965                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
966    
967                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
968    
969                          if (!bIntra)                          if (!bIntra) {
                         {  
970                                  start_timer();                                  start_timer();
971                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
972                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
973                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
974                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
975                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
976                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
977                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1534  Line 981 
981                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
982                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
983                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
984                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
985                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
986                                                    else if (pEnc->current->quant < 1)
987                                                            pEnc->current->quant = 1;
988                                          }                                          }
989                                  }                                  }
990                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
991    
992                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
993    
994                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
995                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
996                          else                                                                            dct_codes, qcoeff);
997                          {                          } else {
998                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
999                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1000                                                                      dct_codes, qcoeff);
1001                          }                          }
1002    
1003                          start_timer();                          start_timer();
1004                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1005                          stop_prediction_timer();                          stop_prediction_timer();
1006    
1007                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1008                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1009                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1010                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1011                                   pMB->mvs[0].x || pMB->mvs[0].y ||                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
                                  pMB->mvs[1].x || pMB->mvs[1].y ||  
                                  pMB->mvs[2].x || pMB->mvs[2].y ||  
                                  pMB->mvs[3].x || pMB->mvs[3].y)  
                         {  
1012                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1013                          }                          }  else {
                         else  
                         {  
1014                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1015                          }                          }
1016    
1017                          start_timer();                          start_timer();
1018    
1019                            /* Finished processing the MB, now check if to CODE or SKIP */
1020    
1021                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1022                                    pMB->mvs[0].y == 0) {
1023    
1024                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1025    
1026                            }
1027                            else {
1028                                    if (pEnc->current->global_flags & XVID_GREYSCALE) {
1029                                            pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1030                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1031                                            qcoeff[5*64+0]=0;
1032                                    }
1033                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1034                            }
1035    
1036                          stop_coding_timer();                          stop_coding_timer();
1037                  }                  }
1038          }          }
1039    
1040          emms();          emms();
1041    
1042          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1043                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1044          }          }
1045    
# Line 1596  Line 1055 
1055          {          {
1056                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1057                  iSearchRange *= 2;                  iSearchRange *= 2;
1058          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1059                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1060                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1061                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1611  Line 1069 
1069          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1070    
1071          return 0;                                        // inter          return 0;                                        // inter
 }  
   
   
 #ifdef BFRAMES  
 static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  
 {  
     int16_t dct_codes[6*64];  
     int16_t qcoeff[6*64];  
     uint32_t x, y;  
         VECTOR forward;  
         VECTOR backward;  
   
     IMAGE *f_ref = &pEnc->reference->image;  
         IMAGE *b_ref = &pEnc->current->image;  
   
         // forward  
         image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);  
         start_timer();  
         image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,  
                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);  
         stop_inter_timer();  
   
         // backward  
         image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);  
     start_timer();  
         image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);  
         stop_inter_timer();  
   
         start_timer();  
         MotionEstimationBVOP(&pEnc->mbParam, frame,  
                 pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,  
                 pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);  
   
   
         stop_motion_timer();  
   
         /*if (test_quant_type(&pEnc->mbParam, pEnc->current))  
         {  
                 BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
         }*/  
   
     frame->coding_type = B_VOP;  
     BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
   
     *pBits = BitstreamPos(bs);  
   
     pEnc->sStat.iTextBits = 0;  
     pEnc->sStat.iMvSum = 0;  
     pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
   
1072    
     for (y = 0; y < pEnc->mbParam.mb_height; y++)  
         {  
                 // reset prediction  
   
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
   
                 for (x = 0; x < pEnc->mbParam.mb_width; x++)  
                 {  
                         MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
   
                         // decoder ignores mb when refence block is INTER(0,0), CBP=0  
                         if (mb->mode == MODE_NOT_CODED)  
                         {  
                                 mb->mvs[0].x = 0;  
                                 mb->mvs[0].y = 0;  
                                 continue;  
                         }  
   
                         MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,  
                                         f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,  
                                         b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                         dct_codes);  
   
                         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);  
   
   
                         if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&  
                                 mb->cbp == 0 &&  
                                 mb->mvs[0].x == 0 &&  
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
                         }  
   
                         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;  
1073                          }                          }
   
                         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;  
                         }  
   
 //                      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);  
   
                         start_timer();  
                         MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);  
                         stop_coding_timer();  
                 }  
         }  
   
         emms();  
   
         // TODO: dynamic fcode/bcode ???  
   
         *pBits = BitstreamPos(bs) - *pBits;  
 }  
 #endif  

Legend:
Removed from v.1.40  
changed lines
  Added in v.1.78

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