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

Legend:
Removed from v.121  
changed lines
  Added in v.541

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