[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.84, Wed Sep 25 23:37:08 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 26  Line 28 
28   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
29   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
30   *   *
  ****************************************************************************/  
   
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
31   *  $Id$   *  $Id$
32   *   *
33   ****************************************************************************/   ****************************************************************************/
# Line 43  Line 35 
35  #include <stdlib.h>  #include <stdlib.h>
36  #include <stdio.h>  #include <stdio.h>
37  #include <math.h>  #include <math.h>
38    #include <string.h>
39    
40  #include "encoder.h"  #include "encoder.h"
41  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 61  Line 54 
54  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
55  #include "utils/mem_align.h"  #include "utils/mem_align.h"
56    
57    #ifdef _SMP
58    #include "motion/smp_motion_est.h"
59    #endif
60  /*****************************************************************************  /*****************************************************************************
61   * Local macros   * Local macros
62   ****************************************************************************/   ****************************************************************************/
# Line 82  Line 78 
78                        bool force_inter,                        bool force_inter,
79                        bool vol_header);                        bool vol_header);
80    
 #ifdef BFRAMES  
 static void FrameCodeB(Encoder * pEnc,  
                        FRAMEINFO * frame,  
                        Bitstream * bs,  
                        uint32_t *pBits);  
 #endif  
   
81  /*****************************************************************************  /*****************************************************************************
82   * Local data   * Local data
83   ****************************************************************************/   ****************************************************************************/
84    
85  static int DQtab[4] =  static int DQtab[4] = {
 {  
86          -1, -2, 1, 2          -1, -2, 1, 2
87  };  };
88    
89  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
90          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
91  };  };
92    
93    
94  static void __inline image_null(IMAGE * image)  static void __inline
95    image_null(IMAGE * image)
96  {  {
97          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
98  }  }
# Line 114  Line 102 
102   * Encoder creation   * Encoder creation
103   *   *
104   * This function creates an Encoder instance, it allocates all necessary   * This function creates an Encoder instance, it allocates all necessary
105   * image buffers (reference, current and bframes) and initialize the internal   * image buffers (reference, current) and initialize the internal xvid
106   * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.   * encoder paremeters according to the XVID_ENC_PARAM input parameter.
107   *   *
108   * The code seems to be very long but is very basic, mainly memory allocation   * The code seems to be very long but is very basic, mainly memory allocation
109   * and cleaning code.   * and cleaning code.
# Line 132  Line 120 
120  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
121  {  {
122          Encoder *pEnc;          Encoder *pEnc;
123          uint32_t i;          int i;
124    
125          pParam->handle = NULL;          pParam->handle = NULL;
126    
# Line 145  Line 133 
133    
134          /* Fps */          /* Fps */
135    
136          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
137                  pParam->fincr = 1;                  pParam->fincr = 1;
138                  pParam->fbase = 25;                  pParam->fbase = 25;
139          }          }
# Line 157  Line 144 
144           */           */
145    
146          i = pParam->fincr;          i = pParam->fincr;
147          while (i > 1)          while (i > 1) {
148          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
149                          pParam->fincr /= i;                          pParam->fincr /= i;
150                          pParam->fbase /= i;                          pParam->fbase /= i;
151                          i = pParam->fincr;                          i = pParam->fincr;
# Line 169  Line 154 
154                  i--;                  i--;
155          }          }
156    
157          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
158                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
159    
160                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
161                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
162          }          }
# Line 203  Line 188 
188    
189          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
190    
191          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
192                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
193    
   
194          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
195          if (pEnc == NULL)          if (pEnc == NULL)
196                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
197    
198            /* Zero the Encoder Structure */
199    
200            memset(pEnc, 0, sizeof(Encoder));
201    
202          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
203    
204          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 225  Line 213 
213          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
214          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
215    
216            pEnc->mbParam.m_quant_type = H263_QUANT;
217    
218    #ifdef _SMP
219            pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);
220    #endif
221    
222          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
223    
224          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 244  Line 238 
238    
239          /* try to allocate mb memory */          /* try to allocate mb memory */
240    
241          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
242                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
243                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
244                                           CACHE_LINE);          pEnc->reference->mbs =
245          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
246                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
247    
248          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
249                  goto xvid_err_memory2;                  goto xvid_err_memory2;
250    
251          /* try to allocate image memory */          /* try to allocate image memory */
252    
253  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
254          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
255  #endif  #endif
 #ifdef BFRAMES  
         image_null(&pEnc->f_refh);  
         image_null(&pEnc->f_refv);  
         image_null(&pEnc->f_refhv);  
 #endif  
256          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
257          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
258          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
259          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
260          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         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,  
                          pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create(&pEnc->f_refv,  
                          pEnc->mbParam.edged_width,  
                          pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create(&pEnc->f_refhv,  
                          pEnc->mbParam.edged_width,  
                          pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
 #endif  
         if (image_create(&pEnc->current->image,  
                          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->reference->image,          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->vInterH,          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          if (image_create(&pEnc->vInterV,          if (image_create
281                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, 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->vInterVf,          if (image_create
285                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
286                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
287                  goto xvid_err_memory3;                  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  
288    
289          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
290    
291          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
292          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
293                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
294                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
295                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
296                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
297          }          }
298    
299          init_timer();          init_timer();
# Line 402  Line 303 
303          /*          /*
304           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
305           */           */
 #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  
306    
307   xvid_err_memory3:   xvid_err_memory3:
308  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
309          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
310                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
311  #endif  #endif
312    
313  #ifdef BFRAMES          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
         image_destroy(&pEnc->f_refh,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refv,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refhv,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
 #endif  
   
         image_destroy(&pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->reference->image,  
                       pEnc->mbParam.edged_width,  
314                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
315          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
316                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
317          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
318                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
319          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
320                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
321          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHVf,  
                       pEnc->mbParam.edged_width,  
322                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
323    
324   xvid_err_memory2:   xvid_err_memory2:
# Line 492  Line 349 
349  int  int
350  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
351  {  {
         ENC_CHECK(pEnc);  
   
         /* B Frames specific */  
 #ifdef BFRAMES  
         int i;  
   
         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);  
352    
353  #endif          ENC_CHECK(pEnc);
354    
355          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
356            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
         image_destroy(&pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
357                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
358          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
359                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
360          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
361                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
362          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, 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,  
363                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
364          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
 #ifdef BFRAMES  
         image_destroy(&pEnc->f_refh,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refv,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refhv,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.edged_height);  
 #endif  
 #ifdef _DEBUG  
         image_destroy(&pEnc->sOriginal,  
                       pEnc->mbParam.edged_width,  
365                        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);  
366    
367          return XVID_ERR_OK;  #ifdef _DEBUG_PSNR
368  }          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
369                                      pEnc->mbParam.edged_height);
370  /*****************************************************************************  #endif
  * 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;  
                 }  
   
                 /* store frame into bframe buffer & swap ref back to current */  
                 SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);  
                 SWAP(pEnc->current, pEnc->reference);  
371    
372                  pEnc->bframenum_tail++;          /* Encoder structure */
373            xvid_free(pEnc->current->mbs);
374            xvid_free(pEnc->current);
375    
376                  pFrame->intra = 0;          xvid_free(pEnc->reference->mbs);
377                  pFrame->length = 0;          xvid_free(pEnc->reference);
                 pFrame->input_consumed = 1;  
378    
379                  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;  
                 }  
380    
381                  return XVID_ERR_OK;                  return XVID_ERR_OK;
382          }          }
383    
         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  
384    
385          if (pFrame->quant == 0)  void inc_frame_num(Encoder * pEnc)
386          {          {
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
   
         pEnc->iFrameNum++;  
387          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
388          if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)          pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
389          {          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
390          }          }
         pFrame->input_consumed = 1;  
391    
         stop_global_timer();  
         write_timer();  
   
         return XVID_ERR_OK;  
 }  
 #else  
392  /*****************************************************************************  /*****************************************************************************
393   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
394     *
395     * Returned values :
396     *    - XVID_ERR_OK     - no errors
397     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
398     *                        format
399   ****************************************************************************/   ****************************************************************************/
400    
401  int  int
402  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
403                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
# Line 924  Line 407 
407          Bitstream bs;          Bitstream bs;
408          uint32_t bits;          uint32_t bits;
409          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
410  #ifdef _DEBUG  
411    #ifdef _DEBUG_PSNR
412          float psnr;          float psnr;
413          uint8_t temp[128];          uint8_t temp[128];
414  #endif  #endif
# Line 940  Line 424 
424    
425          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
426          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
427            pEnc->current->seconds = pEnc->mbParam.m_seconds;
428            pEnc->current->ticks = pEnc->mbParam.m_ticks;
429          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
430    
431          start_timer();          start_timer();
432          if (image_input(&pEnc->current->image,          if (image_input
433                          pEnc->mbParam.width,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
434                          pEnc->mbParam.height,                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace) < 0)  
435                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
436          stop_conv_timer();          stop_conv_timer();
437    
438  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
439          image_copy(&pEnc->sOriginal,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
440                     &pEnc->current->image,                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
441  #endif  #endif
442    
443          EMMS();          emms();
444    
445          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
446    
447          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
448                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
449          }          } else {
         else  
         {  
450                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
451          }          }
452    
453          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
454          {                  int *temp_dquants =
455                  int * temp_dquants = (int *)                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
456                          xvid_malloc(pEnc->mbParam.mb_width * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                     pEnc->mbParam.mb_height * \  
                                     sizeof(int),  
457                                      CACHE_LINE);                                      CACHE_LINE);
458    
459                  pEnc->current->quant =                  pEnc->current->quant =
460                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
461                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
462                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
463                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
464                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
465                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
466    
467                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
468    
469                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
470    
471                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
                         {  
472    
473    
474                                  MACROBLOCK *pMB =                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
475                                          &pEnc->current->mbs[OFFSET(x,y)];  
476                                  pMB->dquant =                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
477                          }                          }
478    
479                          #undef OFFSET                          #undef OFFSET
# Line 1011  Line 482 
482                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
483          }          }
484    
485          if (pEnc->current->global_flags & XVID_H263QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
         {  
486                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
487                          write_vol_header = 1;                          write_vol_header = 1;
488                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
489          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
         else if (pEnc->current->global_flags & XVID_MPEGQUANT)  
         {  
490                  int matrix1_changed, matrix2_changed;                  int matrix1_changed, matrix2_changed;
491    
492                  matrix1_changed = matrix2_changed = 0;                  matrix1_changed = matrix2_changed = 0;
# Line 1030  Line 498 
498    
499                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
500                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
501                                  matrix1_changed =                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
                                         set_intra_matrix(pFrame->quant_intra_matrix);  
502                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
503                                  matrix2_changed                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
504                                          = set_inter_matrix(pFrame->quant_inter_matrix);                  } else {
                 }  
                 else {  
505                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
506                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
507                  }                  }
# Line 1044  Line 509 
509                          write_vol_header = matrix1_changed | matrix2_changed;                          write_vol_header = matrix1_changed | matrix2_changed;
510          }          }
511    
512          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
513          {                  if ((pEnc->iFrameNum == 0)
514                  if ((pEnc->iFrameNum == 0) ||                          || ((pEnc->iMaxKeyInterval > 0)
515                                    && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
516                      ((pEnc->iMaxKeyInterval > 0) &&                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
517                       (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                  } else {
518                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
519                          pFrame->intra = FrameCodeI(pEnc,                  }
520                                                     &bs,          } else {
521                                                     &bits);                  if (pFrame->intra == 1) {
522                  }                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
523                  else                  } else {
524                  {                          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);  
525                  }                  }
526    
527          }          }
# Line 1088  Line 531 
531          BitstreamPad(&bs);          BitstreamPad(&bs);
532          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
533    
534          if (pResult)          if (pResult) {
         {  
535                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
536                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
537                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 1097  Line 539 
539                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
540          }          }
541    
542          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
543    
544  #ifdef _DEBUG          if (pFrame->quant == 0) {
545          psnr = image_psnr(&pEnc->sOriginal,                  RateControlUpdate(&pEnc->rate_control, (int16_t)pEnc->current->quant,
546                            &pEnc->current->image,                                                    pFrame->length, pFrame->intra);
547                            pEnc->mbParam.edged_width,          }
548                            pEnc->mbParam.width,  #ifdef _DEBUG_PSNR
549            psnr =
550                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
551                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
552                            pEnc->mbParam.height);                            pEnc->mbParam.height);
553    
554          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
555          DEBUG(temp);          DEBUG(temp);
556  #endif  #endif
557    
558            inc_frame_num(pEnc);
559          pEnc->iFrameNum++;          pEnc->iFrameNum++;
560    
561          stop_global_timer();          stop_global_timer();
# Line 1125  Line 563 
563    
564          return XVID_ERR_OK;          return XVID_ERR_OK;
565  }  }
 #endif  
566    
567    
568  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
569    CodeIntraMB(Encoder * pEnc,
570                            MACROBLOCK * pMB)
571    {
572    
573          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
574    
# Line 1139  Line 579 
579          pMB->sad16 = 0;          pMB->sad16 = 0;
580    
581          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
582                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
583                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
584                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
585    
586                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
587                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
588                            if (pEnc->current->quant < 1)
589                                    pEnc->current->quant = 1;
590                  }                  }
591          }          }
592    
# Line 1156  Line 597 
597  #define FCODEBITS       3  #define FCODEBITS       3
598  #define MODEBITS        5  #define MODEBITS        5
599    
600  void HintedMESet(Encoder * pEnc, int * intra)  void
601    HintedMESet(Encoder * pEnc,
602                            int *intra)
603  {  {
604          HINTINFO * hint;          HINTINFO * hint;
605          Bitstream bs;          Bitstream bs;
# Line 1165  Line 608 
608    
609          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
610    
611          if (hint->rawhints)          if (hint->rawhints) {
         {  
612                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
613          }          } else {
         else  
         {  
614                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
615                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
616          }          }
617    
618          if (*intra)          if (*intra) {
         {  
619                  return;                  return;
620          }          }
621    
622          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
623                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
624                                                                                                                                     FCODEBITS);
625    
626          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
627          high    = 1 << (length - 1);          high    = 1 << (length - 1);
628    
629          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
630          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
631                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
632                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
633                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
634                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
635                          VECTOR pred[4];                          VECTOR pred;
636                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
637                          int vec;                          int vec;
638    
639                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
640                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
641                                                                                                                                      MODEBITS);
642    
643                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
644                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
645    
646                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
647                          {                                  tmp.x =
648                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
649                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
650                                    tmp.y =
651                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
652                                                                                                                                                      length);
653                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
654                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
655    
656                                  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);
657    
658                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
659                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
660                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
661                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
662                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
                                 }  
663                          }                          }
664                          else if (pMB->mode == MODE_INTER4V)                          } else if (pMB->mode == MODE_INTER4V) {
665                          {                                  for (vec = 0; vec < 4; ++vec) {
666                                  for (vec=0 ; vec<4 ; ++vec)                                          tmp.x =
667                                  {                                                  (hint->rawhints) ? bhint->mvs[vec].
668                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                                  x : BitstreamGetBits(&bs, length);
669                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                          tmp.y =
670                                                    (hint->rawhints) ? bhint->mvs[vec].
671                                                    y : BitstreamGetBits(&bs, length);
672                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
673                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
674    
675                                          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);
676    
677                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
678                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
679                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
680                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
681                                  }                                  }
682                          }                          } else                          // intra / stuffing / not_coded
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
683                                  {                                  {
684                                    for (vec = 0; vec < 4; ++vec) {
685                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
686                                  }                                  }
687                          }                          }
688    
689                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
690                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
691                          {                                  && pMB->dquant != NO_CHANGE) {
692                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
693    
694                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
695                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
696                                  }                                  }
697                          }                          }
# Line 1258  Line 700 
700  }  }
701    
702    
703  void HintedMEGet(Encoder * pEnc, int intra)  void
704    HintedMEGet(Encoder * pEnc,
705                            int intra)
706  {  {
707          HINTINFO * hint;          HINTINFO * hint;
708          Bitstream bs;          Bitstream bs;
# Line 1267  Line 711 
711    
712          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
713    
714          if (hint->rawhints)          if (hint->rawhints) {
         {  
715                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
716          }          } else {
         else  
         {  
717                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
718                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
719          }          }
720    
721          if (intra)          if (intra) {
722          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
723                          BitstreamPad(&bs);                          BitstreamPad(&bs);
724                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
725                  }                  }
# Line 1290  Line 729 
729          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
730          high    = 1 << (length - 1);          high    = 1 << (length - 1);
731    
732          if (hint->rawhints)          if (hint->rawhints) {
         {  
733                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
734          }          } else {
         else  
         {  
735                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
736          }          }
737    
738          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
739          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
740                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
741                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
742                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
743                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
744                          VECTOR tmp;                          VECTOR tmp;
745    
746                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
747                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
748                          }                          } else {
                         else  
                         {  
749                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
750                          }                          }
751    
752                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
753                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
754                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
755                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
756                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
757    
758                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
759                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
760                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
761                                  }                                  } else {
                                 else  
                                 {  
762                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
763                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
764                                  }                                  }
765                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
766                                  int vec;                                  int vec;
767    
768                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
769                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
770                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].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[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
776                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].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                                          }                                          }
# Line 1360  Line 783 
783                  }                  }
784          }          }
785    
786          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
787                  BitstreamPad(&bs);                  BitstreamPad(&bs);
788                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
789          }          }
790  }  }
791    
792    
793  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
794    FrameCodeI(Encoder * pEnc,
795                       Bitstream * bs,
796                       uint32_t * pBits)
797  {  {
798    
799          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 807 
807          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
808    
809          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
810          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
811            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
812    
813          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
814    
# Line 1391  Line 817 
817          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
818    
819          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
820                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
821                  {                          MACROBLOCK *pMB =
822                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
823    
824                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
825    
826                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
827                                                              dct_codes, qcoeff);
828    
829                          start_timer();                          start_timer();
830                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
831                          stop_prediction_timer();                          stop_prediction_timer();
832    
833                          start_timer();                          start_timer();
834                            if (pEnc->current->global_flags & XVID_GREYSCALE)
835                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
836                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
837                                    qcoeff[5*64+0]=0;
838                            }
839                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
840                          stop_coding_timer();                          stop_coding_timer();
841                  }                  }
# Line 1416  Line 848 
848          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
849          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
850    
851          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
852                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
853          }          }
854    
# Line 1427  Line 858 
858    
859  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
860    
861  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
862    FrameCodeP(Encoder * pEnc,
863                       Bitstream * bs,
864                       uint32_t * pBits,
865                       bool force_inter,
866                       bool vol_header)
867  {  {
868          float fSigma;          float fSigma;
869    
# Line 1435  Line 871 
871          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
872    
873          int iLimit;          int iLimit;
874          uint32_t x, y;          unsigned int x, y;
875          int iSearchRange;          int iSearchRange;
876          int bIntra;          int bIntra;
877    
# Line 1443  Line 879 
879          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
880    
881          start_timer();          start_timer();
882          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
883                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->current->global_flags & XVID_INTERLACING);  
884          stop_edges_timer();          stop_edges_timer();
885    
886          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 1456  Line 888 
888          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
889    
890          if (!force_inter)          if (!force_inter)
891                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
892                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
893                                       INTRA_THRESHOLD);
894          else          else
895                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
896    
897          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
898                  start_timer();                  start_timer();
899                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
900                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
901                                                      pEnc->mbParam.edged_height,
902                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
903                  stop_inter_timer();                  stop_inter_timer();
904          }          }
905    
906          start_timer();          start_timer();
907          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
908                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
909          }          } else {
910    
911    #ifdef _SMP
912            if (pEnc->mbParam.num_threads > 1)
913                    bIntra =
914                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
915                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
916                                                     iLimit);
917          else          else
918          {  #endif
919                  bIntra = MotionEstimation(                  bIntra =
920                          &pEnc->mbParam,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
921                          pEnc->current,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
922                          iLimit);                          iLimit);
923    
924          }          }
925          stop_motion_timer();          stop_motion_timer();
926    
927          if (bIntra == 1)          if (bIntra == 1) {
         {  
928                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
929          }          }
930    
# Line 1496  Line 933 
933          if(vol_header)          if(vol_header)
934                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
935    
936          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
937    
938          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
939    
# Line 1505  Line 942 
942          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
943          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
944    
945          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
946          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
947                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
948                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
949    
950                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
951    
952                          if (!bIntra)                          if (!bIntra) {
                         {  
953                                  start_timer();                                  start_timer();
954                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
955                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
956                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
957                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
958                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
959                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
960                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1534  Line 964 
964                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
965                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
966                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
967                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
968                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
969                                                    else if (pEnc->current->quant < 1)
970                                                            pEnc->current->quant = 1;
971                                          }                                          }
972                                  }                                  }
973                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
974    
975                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
976    
977                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
978                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
979                          else                                                                            dct_codes, qcoeff);
980                          {                          } else {
981                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
982                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
983                          }                                                                    dct_codes, qcoeff);
984    
985                          start_timer();                          start_timer();
986                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
987                          stop_prediction_timer();                          stop_prediction_timer();
988                            }
989    
990                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
991                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
992                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
993                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
994                                   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)  
                         {  
995                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
996                          }                          }  else {
                         else  
                         {  
997                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
998                          }                          }
999    
1000                          start_timer();                          start_timer();
1001    
1002                            /* Finished processing the MB, now check if to CODE or SKIP */
1003    
1004                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1005                                    pMB->mvs[0].y == 0) {
1006    
1007                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1008    
1009                            }
1010                            else {
1011                                    if (pEnc->current->global_flags & XVID_GREYSCALE) {
1012                                            pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1013                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1014                                            qcoeff[5*64+0]=0;
1015                                    }
1016                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1017                            }
1018    
1019                          stop_coding_timer();                          stop_coding_timer();
1020                  }                  }
1021          }          }
1022    
1023          emms();          emms();
1024    
1025          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1026                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1027          }          }
1028    
# Line 1596  Line 1038 
1038          {          {
1039                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1040                  iSearchRange *= 2;                  iSearchRange *= 2;
1041          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1042                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1043                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1044                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1611  Line 1052 
1052          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1053    
1054          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;  
   
1055    
     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;  
1056                          }                          }
   
                         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.84

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