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

Diff of /trunk/xvidcore/src/encoder.c

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

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

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