[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 145, Sun Apr 28 21:55:06 2002 UTC revision 686, Thu Nov 28 07:27:37 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002 Peter Ross <pross@xvid.org>
8   *  to use this software module in hardware or software products are   *               2002 Daniel Smith <danielsmith@astroboymail.com>
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
11   *  it under the terms of the GNU General Public License as published by   *
12     *  XviD is free software; you can redistribute it and/or modify it
13     *  under the terms of the GNU General Public License as published by
14   *  the Free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
15   *  (at your option) any later version.   *  (at your option) any later version.
16   *   *
# Line 26  Line 23 
23   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
24   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25   *   *
26   ***************************************************************************/   *  Under section 8 of the GNU General Public License, the copyright
27     *  holders of XVID explicitly forbid distribution in the following
28  /****************************************************************************   *  countries:
29     *
30     *    - Japan
31     *    - United States of America
32   *   *
33   *  History   *  Linking XviD statically or dynamically with other modules is making a
34     *  combined work based on XviD.  Thus, the terms and conditions of the
35     *  GNU General Public License cover the whole combination.
36   *   *
37   *  14.04.2002 added FrameCodeB()   *  As a special exception, the copyright holders of XviD give you
38     *  permission to link XviD with independent modules that communicate with
39     *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
40     *  license terms of these independent modules, and to copy and distribute
41     *  the resulting combined work under terms of your choice, provided that
42     *  every copy of the combined work is accompanied by a complete copy of
43     *  the source code of XviD (the version of XviD used to produce the
44     *  combined work), being distributed under the terms of the GNU General
45     *  Public License plus this exception.  An independent module is a module
46     *  which is not derived from or based on XviD.
47   *   *
48   *  $Id: encoder.c,v 1.29 2002-04-28 21:55:06 edgomez Exp $   *  Note that people who make modified versions of XviD are not obligated
49     *  to grant this special exception for their modified versions; it is
50     *  their choice whether to do so.  The GNU General Public License gives
51     *  permission to release a modified version without this exception; this
52     *  exception also makes it possible to release a modified version which
53     *  carries forward this exception.
54   *   *
55   ***************************************************************************/   * $Id: encoder.c,v 1.89 2002-11-28 07:27:37 suxen_drol Exp $
56     *
57     ****************************************************************************/
58    
59  #include <stdlib.h>  #include <stdlib.h>
60  #include <stdio.h>  #include <stdio.h>
61  #include <math.h>  #include <math.h>
62    #include <string.h>
63    
64  #include "encoder.h"  #include "encoder.h"
65  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
66  #include "global.h"  #include "global.h"
67  #include "utils/timer.h"  #include "utils/timer.h"
68  #include "image/image.h"  #include "image/image.h"
69    #include "motion/motion.h"
70  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
71  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
72  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 58  Line 78 
78  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
79  #include "utils/mem_align.h"  #include "utils/mem_align.h"
80    
81    /*****************************************************************************
82     * Local macros
83     ****************************************************************************/
84    
85  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
86  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
87    
88    /*****************************************************************************
89     * Local function prototypes
90     ****************************************************************************/
91    
92    static int FrameCodeI(Encoder * pEnc,
93                                              Bitstream * bs,
94                                              uint32_t * pBits);
95    
96    static int FrameCodeP(Encoder * pEnc,
97                                              Bitstream * bs,
98                                              uint32_t * pBits,
99                                              bool force_inter,
100                                              bool vol_header);
101    
102    /*****************************************************************************
103     * Local data
104     ****************************************************************************/
105    
106  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  static int DQtab[4] = {
 static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);  
   
 static int DQtab[4] =  
 {  
107          -1, -2, 1, 2          -1, -2, 1, 2
108  };  };
109    
110  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
111          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
112  };  };
113    
114    
115  void static image_null(IMAGE * image)  static void __inline
116    image_null(IMAGE * image)
117  {  {
118          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
119  }  }
120    
121    
122  int encoder_create(XVID_ENC_PARAM * pParam)  /*****************************************************************************
123     * Encoder creation
124     *
125     * This function creates an Encoder instance, it allocates all necessary
126     * image buffers (reference, current) and initialize the internal xvid
127     * encoder paremeters according to the XVID_ENC_PARAM input parameter.
128     *
129     * The code seems to be very long but is very basic, mainly memory allocation
130     * and cleaning code.
131     *
132     * Returned values :
133     *    - XVID_ERR_OK     - no errors
134     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
135     *                        cleans the structure before exiting.
136     *                        pParam->handle is also set to NULL.
137     *
138     ****************************************************************************/
139    
140    int
141    encoder_create(XVID_ENC_PARAM * pParam)
142  {  {
143          Encoder *pEnc;          Encoder *pEnc;
144          uint32_t i;          int i;
145    
146          pParam->handle = NULL;          pParam->handle = NULL;
147    
# Line 96  Line 152 
152          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
153          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
154    
155          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
156          {  
157            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
158                  pParam->fincr = 1;                  pParam->fincr = 1;
159                  pParam->fbase = 25;                  pParam->fbase = 25;
160          }          }
161    
162          // simplify the "fincr/fbase" fraction          /*
163          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
164             * (neccessary, since windows supplies us with huge numbers)
165             */
166    
167          i = pParam->fincr;          i = pParam->fincr;
168          while (i > 1)          while (i > 1) {
169          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
170                          pParam->fincr /= i;                          pParam->fincr /= i;
171                          pParam->fbase /= i;                          pParam->fbase /= i;
172                          i = pParam->fincr;                          i = pParam->fincr;
# Line 118  Line 175 
175                  i--;                  i--;
176          }          }
177    
178          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
179                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
180    
181                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
182                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
183          }          }
184    
185            /* Bitrate allocator defaults */
186    
187          if (pParam->rc_bitrate <= 0)          if (pParam->rc_bitrate <= 0)
188                  pParam->rc_bitrate = 900000;                  pParam->rc_bitrate = 900000;
189    
# Line 137  Line 196 
196          if (pParam->rc_buffer <= 0)          if (pParam->rc_buffer <= 0)
197                  pParam->rc_buffer = 100;                  pParam->rc_buffer = 100;
198    
199            /* Max and min quantizers */
200    
201          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
202                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
203    
204          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
205                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
206    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
207          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
208                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
209    
210          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)          /* 1 keyframe each 10 seconds */
211    
212            if (pParam->max_key_interval <= 0)
213                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
214    
215            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
216            if (pEnc == NULL)
217                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
218    
219            /* Zero the Encoder Structure */
220    
221            memset(pEnc, 0, sizeof(Encoder));
222    
223          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
224    
225          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 163  Line 231 
231          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
232          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
233    
234            pEnc->mbParam.fbase = pParam->fbase;
235            pEnc->mbParam.fincr = pParam->fincr;
236    
237            pEnc->mbParam.m_quant_type = H263_QUANT;
238    
239          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
240    
241          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 174  Line 247 
247    
248          /* try to allocate frame memory */          /* try to allocate frame memory */
249    
250          pEnc->current = NULL;          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
251          pEnc->reference = NULL;          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
252          if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL ||  
253                   (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL)          if (pEnc->current == NULL || pEnc->reference == NULL)
254          {                  goto xvid_err_memory1;
                 if (pEnc->current) xvid_free(pEnc->current);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
255    
256          /* try to allocate mb memory */          /* try to allocate mb memory */
257    
258          pEnc->current->mbs = NULL;          pEnc->current->mbs =
259          pEnc->reference->mbs = NULL;                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
260                                            pEnc->mbParam.mb_height, CACHE_LINE);
261            pEnc->reference->mbs =
262                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
263                                            pEnc->mbParam.mb_height, CACHE_LINE);
264    
265  #ifdef _DEBUG          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
266  #ifdef WIN32                  goto xvid_err_memory2;
 OutputDebugString("malloc mbs");  
 #endif  
 #endif  
   
         if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL ||  
                 (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)  
         {  
                 if (pEnc->current->mbs) xvid_free(pEnc->current->mbs);  
                 xvid_free(pEnc->current);  
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
         }  
267    
268          /* try to allocate image memory */          /* try to allocate image memory */
269    
270  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
271          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
272  #endif  #endif
273          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
274          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
275          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
276          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
277          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
278    
279  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
280  #ifdef WIN32          if (image_create
281  OutputDebugString("malloc images");                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
282  #endif                   pEnc->mbParam.edged_height) < 0)
283                    goto xvid_err_memory3;
284  #endif  #endif
285          if (          if (image_create
286  #ifdef _DEBUG                  (&pEnc->current->image, pEnc->mbParam.edged_width,
287                  image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                   pEnc->mbParam.edged_height) < 0)
288  #endif                  goto xvid_err_memory3;
289                  image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create
290                  image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
291                  image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                   pEnc->mbParam.edged_height) < 0)
292                  image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  goto xvid_err_memory3;
293                  image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create
294                  image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
295                  image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
296          {                  goto xvid_err_memory3;
297  #ifdef _DEBUG          if (image_create
298                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
299  #endif                   pEnc->mbParam.edged_height) < 0)
300                  image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  goto xvid_err_memory3;
301                  image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (image_create
302                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
303                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   pEnc->mbParam.edged_height) < 0)
304                  image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  goto xvid_err_memory3;
                 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
   
                 xvid_free(pEnc->current);  
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
305    
306          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
307    
308          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
309          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
310                  RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor,                                                  pParam->rc_reaction_delay_factor,
311                          pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
312                                                    pParam->fbase * 1000 / pParam->fincr,
313                          pParam->max_quantizer, pParam->min_quantizer);                          pParam->max_quantizer, pParam->min_quantizer);
314          }          }
315    
316          init_timer();          init_timer();
317    
318          return XVID_ERR_OK;          return XVID_ERR_OK;
319    
320            /*
321             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
322             */
323    
324      xvid_err_memory3:
325    #ifdef _DEBUG_PSNR
326            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
327                                      pEnc->mbParam.edged_height);
328    #endif
329    
330            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
331                                      pEnc->mbParam.edged_height);
332            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
333                                      pEnc->mbParam.edged_height);
334            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
335                                      pEnc->mbParam.edged_height);
336            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
337                                      pEnc->mbParam.edged_height);
338            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
339                                      pEnc->mbParam.edged_height);
340    
341      xvid_err_memory2:
342            xvid_free(pEnc->current->mbs);
343            xvid_free(pEnc->reference->mbs);
344    
345      xvid_err_memory1:
346            xvid_free(pEnc->current);
347            xvid_free(pEnc->reference);
348            xvid_free(pEnc);
349    
350            pParam->handle = NULL;
351    
352            return XVID_ERR_MEMORY;
353  }  }
354    
355    /*****************************************************************************
356     * Encoder destruction
357     *
358     * This function destroy the entire encoder structure created by a previous
359     * successful encoder_create call.
360     *
361     * Returned values (for now only one returned value) :
362     *    - XVID_ERR_OK     - no errors
363     *
364     ****************************************************************************/
365    
366  int encoder_destroy(Encoder * pEnc)  int
367    encoder_destroy(Encoder * pEnc)
368  {  {
369    
370          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
371    
372          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* All images, reference, current etc ... */
373          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
374          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
375          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
376          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
377          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
378          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
379  #ifdef _DEBUG          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
380                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
381            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
382                                      pEnc->mbParam.edged_height);
383    
384    #ifdef _DEBUG_PSNR
385            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
386                                      pEnc->mbParam.edged_height);
387  #endif  #endif
388    
389            /* Encoder structure */
390          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
391          xvid_free(pEnc->current);          xvid_free(pEnc->current);
392    
# Line 287  Line 394 
394          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
395    
396          xvid_free(pEnc);          xvid_free(pEnc);
397    
398          return XVID_ERR_OK;          return XVID_ERR_OK;
399  }  }
400    
401  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
402    void inc_frame_num(Encoder * pEnc)
403    {
404            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
405            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
406            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
407    }
408    
409    /*****************************************************************************
410     * "original" IP frame encoder entry point
411     *
412     * Returned values :
413     *    - XVID_ERR_OK     - no errors
414     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
415     *                        format
416     ****************************************************************************/
417    
418    int
419    encoder_encode(Encoder * pEnc,
420                               XVID_ENC_FRAME * pFrame,
421                               XVID_ENC_STATS * pResult)
422  {  {
423          uint16_t x, y;          uint16_t x, y;
424          Bitstream bs;          Bitstream bs;
425          uint32_t bits;          uint32_t bits;
426          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
427  #ifdef _DEBUG  
428    #ifdef _DEBUG_PSNR
429          float psnr;          float psnr;
430          uint8_t temp[100];          uint8_t temp[128];
431  #endif  #endif
432    
433          start_global_timer();          start_global_timer();
# Line 312  Line 441 
441    
442          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
443          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
444            pEnc->current->seconds = pEnc->mbParam.m_seconds;
445            pEnc->current->ticks = pEnc->mbParam.m_ticks;
446          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
447    
448          start_timer();          start_timer();
449          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
450                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
451          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
452                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
453          stop_conv_timer();          stop_conv_timer();
454    
455  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
456          image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->current->image,
457                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
458  #endif  #endif
459    
460          EMMS();          emms();
461    
462          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
463    
464          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
465          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
466                  pEnc->current->quant = RateControlGetQ(0);          } else {
         }  
         else  
         {  
467                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
468          }          }
469    
470          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
471          {                  int *temp_dquants =
472                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
473                                                                    pEnc->mbParam.mb_height * sizeof(int),
474                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                                                                  CACHE_LINE);
475                                                              pEnc->mbParam.edged_width,  // stride  
476                                                              temp_dquants,                  pEnc->current->quant =
477                                                              pEnc->current->quant,                          adaptive_quantization(pEnc->current->image.y,
478                                                              pEnc->current->quant,       // min_quant                                                                    pEnc->mbParam.edged_width, temp_dquants,
479                                                              2*pEnc->current->quant,     // max_quant                                                                    pEnc->current->quant, pEnc->current->quant,
480                                                                      2 * pEnc->current->quant,
481                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
482                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
483    
484                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
485                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
486                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
487                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
488                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
489    
490    
491                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
492    
493                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
494                            }
495    
496    #undef OFFSET
497                          }                          }
498    
499                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
500          }          }
501    
# Line 365  Line 503 
503                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
504                          write_vol_header = 1;                          write_vol_header = 1;
505                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
506          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
507          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
508    
509                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
510    
511                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)
512                          write_vol_header = 1;                          write_vol_header = 1;
# Line 378  Line 515 
515    
516                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
517                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
518                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
519                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
520                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
521                  }                  } else {
522                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
523                          ret1 = set_intra_matrix(get_default_intra_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
                         ret2 = set_inter_matrix(get_default_inter_matrix());  
524                  }                  }
525                  if(write_vol_header == 0)                  if(write_vol_header == 0)
526                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
527          }          }
528    
529          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
530          {                  if ((pEnc->iFrameNum == 0)
531                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
532                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
533                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
534                  else                  } else {
535                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
536          }          }
537          else          } else {
538          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
539                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
540                  else                  } else {
541                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
542          }          }
543    
544            }
545    
546          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
547          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
548          BitstreamPad(&bs);          BitstreamPad(&bs);
549          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
550    
551          if (pResult)          if (pResult) {
         {  
552                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
553                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
554                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 421  Line 556 
556                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
557          }          }
558    
559          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);  
         }  
560    
561  #ifdef _DEBUG          if (pFrame->quant == 0) {
562          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,                  RateControlUpdate(&pEnc->rate_control, (int16_t)pEnc->current->quant,
563                                  pEnc->mbParam.width, pEnc->mbParam.height);                                                    pFrame->length, pFrame->intra);
564            }
565    #ifdef _DEBUG_PSNR
566            psnr =
567                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
568                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
569                                       pEnc->mbParam.height);
570    
571          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
572          DEBUG(temp);          DEBUG(temp);
573  #endif  #endif
574    
575            inc_frame_num(pEnc);
576          pEnc->iFrameNum++;          pEnc->iFrameNum++;
577    
578          stop_global_timer();          stop_global_timer();
# Line 445  Line 582 
582  }  }
583    
584    
585  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
586    CodeIntraMB(Encoder * pEnc,
587                            MACROBLOCK * pMB)
588    {
589    
590          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
591    
# Line 456  Line 596 
596          pMB->sad16 = 0;          pMB->sad16 = 0;
597    
598          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
599                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
600                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
601                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
602    
603                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
604                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
605                            if (pEnc->current->quant < 1)
606                                    pEnc->current->quant = 1;
607                  }                  }
608          }          }
609    
# Line 473  Line 614 
614  #define FCODEBITS       3  #define FCODEBITS       3
615  #define MODEBITS        5  #define MODEBITS        5
616    
617  void HintedMESet(Encoder * pEnc, int * intra)  void
618    HintedMESet(Encoder * pEnc,
619                            int *intra)
620  {  {
621          HINTINFO * hint;          HINTINFO * hint;
622          Bitstream bs;          Bitstream bs;
# Line 482  Line 625 
625    
626          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
627    
628          if (hint->rawhints)          if (hint->rawhints) {
         {  
629                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
630          }          } else {
         else  
         {  
631                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
632                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
633          }          }
634    
635          if (*intra)          if (*intra) {
         {  
636                  return;                  return;
637          }          }
638    
639          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
640                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
641                                                                                                                                     FCODEBITS);
642    
643          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
644          high    = 1 << (length - 1);          high    = 1 << (length - 1);
645    
646          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
647          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
648                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
649                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
650                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
651                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
652                          VECTOR pred[4];                          VECTOR pred;
653                          VECTOR tmp;                          VECTOR tmp;
                         uint32_t dummy[4];  
654                          int vec;                          int vec;
655    
656                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
657                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
658                                                                                                                                      MODEBITS);
659    
660                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
661                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
662    
663                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
664                          {                                  tmp.x =
665                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
666                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
667                                    tmp.y =
668                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
669                                                                                                                                                      length);
670                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
671                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
672    
673                                  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);
674    
675                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
676                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
677                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
678                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
679                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
                                 }  
680                          }                          }
681                          else if (pMB->mode == MODE_INTER4V)                          } else if (pMB->mode == MODE_INTER4V) {
682                          {                                  for (vec = 0; vec < 4; ++vec) {
683                                  for (vec=0 ; vec<4 ; ++vec)                                          tmp.x =
684                                  {                                                  (hint->rawhints) ? bhint->mvs[vec].
685                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                                  x : BitstreamGetBits(&bs, length);
686                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                          tmp.y =
687                                                    (hint->rawhints) ? bhint->mvs[vec].
688                                                    y : BitstreamGetBits(&bs, length);
689                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
690                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
691    
692                                          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);
693    
694                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
695                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
696                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
697                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
698                                  }                                  }
699                          }                          } else                          /* intra / stuffing / not_coded */
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
700                                  {                                  {
701                                    for (vec = 0; vec < 4; ++vec) {
702                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
703                                  }                                  }
704                          }                          }
705    
706                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
707                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
708                          {                                  && pMB->dquant != NO_CHANGE) {
709                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
710    
711                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
712                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
713                                  }                                  }
714                          }                          }
# Line 575  Line 717 
717  }  }
718    
719    
720  void HintedMEGet(Encoder * pEnc, int intra)  void
721    HintedMEGet(Encoder * pEnc,
722                            int intra)
723  {  {
724          HINTINFO * hint;          HINTINFO * hint;
725          Bitstream bs;          Bitstream bs;
# Line 584  Line 728 
728    
729          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
730    
731          if (hint->rawhints)          if (hint->rawhints) {
         {  
732                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
733          }          } else {
         else  
         {  
734                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
735                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
736          }          }
737    
738          if (intra)          if (intra) {
739          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
740                          BitstreamPad(&bs);                          BitstreamPad(&bs);
741                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
742                  }                  }
# Line 607  Line 746 
746          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
747          high    = 1 << (length - 1);          high    = 1 << (length - 1);
748    
749          if (hint->rawhints)          if (hint->rawhints) {
         {  
750                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
751          }          } else {
         else  
         {  
752                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
753          }          }
754    
755          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
756          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
757                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
758                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
759                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
760                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
761                          VECTOR tmp;                          VECTOR tmp;
762    
763                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
764                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
765                          }                          } else {
                         else  
                         {  
766                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
767                          }                          }
768    
769                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
770                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
771                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
772                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
773                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
774    
775                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
776                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
777                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
778                                  }                                  } else {
                                 else  
                                 {  
779                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
780                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
781                                  }                                  }
782                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
783                                  int vec;                                  int vec;
784    
785                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
786                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
787                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
788                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
789                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
790    
791                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
792                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
793                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
794                                          }                                          } else {
                                         else  
                                         {  
795                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
796                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
797                                          }                                          }
# Line 677  Line 800 
800                  }                  }
801          }          }
802    
803          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
804                  BitstreamPad(&bs);                  BitstreamPad(&bs);
805                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
806          }          }
807  }  }
808    
809    
810  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
811    FrameCodeI(Encoder * pEnc,
812                       Bitstream * bs,
813                       uint32_t * pBits)
814  {  {
815    
816          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 699  Line 824 
824          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
825    
826          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
827          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
828            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
829    
830          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
831    
# Line 708  Line 834 
834          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
835    
836          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
837                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
838                  {                          MACROBLOCK *pMB =
839                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
840    
841                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
842    
843                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
844                                                              dct_codes, qcoeff);
845    
846                          start_timer();                          start_timer();
847                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
848                          stop_prediction_timer();                          stop_prediction_timer();
849    
850                          start_timer();                          start_timer();
851                            if (pEnc->current->global_flags & XVID_GREYSCALE)
852                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
853                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
854                                    qcoeff[5*64+0]=0;
855                            }
856                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
857                          stop_coding_timer();                          stop_coding_timer();
858                  }                  }
# Line 733  Line 865 
865          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
866          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
867    
868          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
869                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
870          }          }
871    
872          return 1;                                        // intra          return 1;                                       /* intra */
873  }  }
874    
875    
876  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
877    
878  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
879    FrameCodeP(Encoder * pEnc,
880                       Bitstream * bs,
881                       uint32_t * pBits,
882                       bool force_inter,
883                       bool vol_header)
884  {  {
885          float fSigma;          float fSigma;
886    
# Line 752  Line 888 
888          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
889    
890          int iLimit;          int iLimit;
891          uint32_t x, y;          unsigned int x, y;
892          int iSearchRange;          int iSearchRange;
893          int bIntra;          int bIntra;
894    
# Line 760  Line 896 
896          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
897    
898          start_timer();          start_timer();
899          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
900                         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);  
901          stop_edges_timer();          stop_edges_timer();
902    
903          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 773  Line 905 
905          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
906    
907          if (!force_inter)          if (!force_inter)
908                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
909                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
910                                       INTRA_THRESHOLD);
911          else          else
912                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
913    
914          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
915                  start_timer();                  start_timer();
916                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
917                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
918                                                      pEnc->mbParam.edged_height,
919                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
920                  stop_inter_timer();                  stop_inter_timer();
921          }          }
922    
923          start_timer();          start_timer();
924          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
925                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
926          }          } else {
927          else  
928          {                  bIntra =
929                  bIntra = MotionEstimation(                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
930                          &pEnc->mbParam,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         pEnc->current,  
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
931                          iLimit);                          iLimit);
932    
933          }          }
934          stop_motion_timer();          stop_motion_timer();
935    
936          if (bIntra == 1)          if (bIntra == 1) {
         {  
937                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
938          }          }
939    
# Line 813  Line 942 
942          if(vol_header)          if(vol_header)
943                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
944    
945          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
946    
947          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
948    
# Line 822  Line 951 
951          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
952          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
953    
954          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
955          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
956                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
957                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
958    
959                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
960    
961                          if (!bIntra)                          if (!bIntra) {
                         {  
962                                  start_timer();                                  start_timer();
963                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
964                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
965                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
966                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
967                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
968                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
969                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 851  Line 973 
973                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
974                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
975                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
976                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
977                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
978                                                    else if (pEnc->current->quant < 1)
979                                                            pEnc->current->quant = 1;
980                                          }                                          }
981                                  }                                  }
982                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
983    
984                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
985    
986                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
987                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
988                          else                                                                            dct_codes, qcoeff);
989                          {                          } else {
990                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
991                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
992                          }                                                                    dct_codes, qcoeff);
993    
994                          start_timer();                          start_timer();
995                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
996                          stop_prediction_timer();                          stop_prediction_timer();
997                            }
998    
999                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1000                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1001                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1002                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1003                                   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)  
                         {  
1004                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1005                          }                          }  else {
                         else  
                         {  
1006                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1007                          }                          }
1008    
1009                          start_timer();                          start_timer();
1010    
1011                            /* Finished processing the MB, now check if to CODE or SKIP */
1012    
1013                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1014                                    pMB->mvs[0].y == 0) {
1015    
1016                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1017    
1018                            }
1019                            else {
1020                                    if (pEnc->current->global_flags & XVID_GREYSCALE) {
1021                                            pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1022                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1023                                            qcoeff[5*64+0]=0;
1024                                    }
1025                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1026                            }
1027    
1028                          stop_coding_timer();                          stop_coding_timer();
1029                  }                  }
1030          }          }
1031    
1032          emms();          emms();
1033    
1034          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1035                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1036          }          }
1037    
# Line 909  Line 1043 
1043          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1044    
1045          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1046              && (pEnc->mbParam.m_fcode <= 3))    // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        /* maximum search range 128 */
1047          {          {
1048                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1049                  iSearchRange *= 2;                  iSearchRange *= 2;
1050          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1051                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1052                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1053                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     /* minimum search range 16 */
1054          {          {
1055                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1056                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 927  Line 1060 
1060    
1061          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1062    
1063          return 0;                                        // inter          return 0;                                       /* inter */
 }  
   
   
 #if 0  
   
 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);  
         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();  
1064    
         /* backward */  
         image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);  
         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);  
1065          }          }
   
         frame->coding_type = B_VOP;  
         BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0,  
                                 frame->quant, frame->fcode, frame->bcode);  
   
         *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;  
   
   
         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, 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;  
                         }  
   
                         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(frame, mb, qcoeff, bs, &pEnc->sStat);  
                         stop_coding_timer();  
                 }  
         }  
   
         emms();  
   
         // TODO: dynamic fcode/bcode ???  
   
         *pBits = BitstreamPos(bs) - *pBits;  
   
 }  
   
 #endif  

Legend:
Removed from v.145  
changed lines
  Added in v.686

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